在 gdb 的 运行 程序中查看环境变量
Viewing environmental variables in a running program in gdb
在核心转储文件中,我可以通过以下方式查看环境变量:
$ gdb file core
>>> p __environ[0]
= 0x7ffe6420dcbd "SHELL=/bin/bash"
>>> p __environ[1]
= 0x7ffe6420dccd "TMUX=/tmp/tmux-1001/default,13871,0"
# basically the same as doing $ printenv
但是,当我尝试调试一个普通的可执行文件时:
# file.s
.globl main
main:
ret
$ gcc file.s -ggdb3 -o file2
$ gdb -q file2
Reading symbols from file...
(No debugging symbols found in file)
>>> starti
>>> p __environ
No symbol "__environ" in current context.
如何访问 environ
和 argv
?一种可能的方法是检查其进程文件:
>>> python print(open('/proc/48011/environ').read().replace('\x00','\n'))
SHELL=/bin/bash
TMUX=/tmp/tmux-1001/default,13871,0
SSH_AUTH_SOCK=/tmp/ssh-RtmbGp8HSFcN/agent.14219
SSH_AGENT_PID=14220
EDITOR=/usr/bin/vim
48011
来自:
─── Threads ───────────────────────────────────────────────────────────────────
[1] id 48011 name file from 0x0000000000401005 in _start
根据 its documentation, GDB has a show environment
command. On Linux, read also gdb(1), gcore(1), core(5), proc(5), signal(7) and gcc(1) and conventions documented in environ(7). See also getenv(3) and putenv(3). You could also play with objdump(1) and readelf(1) and nm(1). Be sure to read more documentation about ELF, perhaps in the Linkers and loaders 书。
但是,如果您使用 gcc file.s -ggdb3 -o file2
在 LInux 上编译一个汇编文件,该汇编文件缺少 DWARF 调试信息,这几乎是 GDB 所需要的。
所以试着写一个小的 hello-world.c
程序,使用 gcc -Wall -Wextra -fverbose-asm -g -S hello-world.c
编译它并检查(例如 less(1)) the emitted hello-world.s
assembler file. You'll see there a lot of DWARF related assembler directives. Compile it again with gcc -v -Wall -Wextra -g -O hello-world.c -o helloworld
to understand what programs GCC is starting for you. Read also the documentation about invoking GCC.
当然要了解 crt0 startup code. On Linux, study the source code of both GNU libc (or musl-libc) and of GCC and binutils. All of them are free software or at least open source 软件,您可以下载、研究和改进其源代码。
另请阅读 Linux ABI. See this answer. Consider also studying the source code of your Linux kernel. See also kernelnewbies 的规范。
您可能会对 linuxfromscratch.org which gives detailed step by step instructions (to build an entire Linux distribution from scratch). Read Advanced Linux Programming and Linux Assembler HowTo 感兴趣。预算然后几周的工作。
另请阅读 execve(2), elf(5), syscalls(2) and study the source code of GNU bash。
阅读有关 processes, system calls, computer files, file systems and executables. Consider reading a good textbook on operating systems and the Modern C book and perhaps the C11 standard n1570 的维基页面。
在核心转储文件中,我可以通过以下方式查看环境变量:
$ gdb file core
>>> p __environ[0]
= 0x7ffe6420dcbd "SHELL=/bin/bash"
>>> p __environ[1]
= 0x7ffe6420dccd "TMUX=/tmp/tmux-1001/default,13871,0"
# basically the same as doing $ printenv
但是,当我尝试调试一个普通的可执行文件时:
# file.s
.globl main
main:
ret
$ gcc file.s -ggdb3 -o file2
$ gdb -q file2
Reading symbols from file...
(No debugging symbols found in file)
>>> starti
>>> p __environ
No symbol "__environ" in current context.
如何访问 environ
和 argv
?一种可能的方法是检查其进程文件:
>>> python print(open('/proc/48011/environ').read().replace('\x00','\n'))
SHELL=/bin/bash
TMUX=/tmp/tmux-1001/default,13871,0
SSH_AUTH_SOCK=/tmp/ssh-RtmbGp8HSFcN/agent.14219
SSH_AGENT_PID=14220
EDITOR=/usr/bin/vim
48011
来自:
─── Threads ───────────────────────────────────────────────────────────────────
[1] id 48011 name file from 0x0000000000401005 in _start
根据 its documentation, GDB has a show environment
command. On Linux, read also gdb(1), gcore(1), core(5), proc(5), signal(7) and gcc(1) and conventions documented in environ(7). See also getenv(3) and putenv(3). You could also play with objdump(1) and readelf(1) and nm(1). Be sure to read more documentation about ELF, perhaps in the Linkers and loaders 书。
但是,如果您使用 gcc file.s -ggdb3 -o file2
在 LInux 上编译一个汇编文件,该汇编文件缺少 DWARF 调试信息,这几乎是 GDB 所需要的。
所以试着写一个小的 hello-world.c
程序,使用 gcc -Wall -Wextra -fverbose-asm -g -S hello-world.c
编译它并检查(例如 less(1)) the emitted hello-world.s
assembler file. You'll see there a lot of DWARF related assembler directives. Compile it again with gcc -v -Wall -Wextra -g -O hello-world.c -o helloworld
to understand what programs GCC is starting for you. Read also the documentation about invoking GCC.
当然要了解 crt0 startup code. On Linux, study the source code of both GNU libc (or musl-libc) and of GCC and binutils. All of them are free software or at least open source 软件,您可以下载、研究和改进其源代码。
另请阅读 Linux ABI. See this answer. Consider also studying the source code of your Linux kernel. See also kernelnewbies 的规范。
您可能会对 linuxfromscratch.org which gives detailed step by step instructions (to build an entire Linux distribution from scratch). Read Advanced Linux Programming and Linux Assembler HowTo 感兴趣。预算然后几周的工作。
另请阅读 execve(2), elf(5), syscalls(2) and study the source code of GNU bash。
阅读有关 processes, system calls, computer files, file systems and executables. Consider reading a good textbook on operating systems and the Modern C book and perhaps the C11 standard n1570 的维基页面。