Linux 我可以使用 GDB 来调试正在运行的进程吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2308653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use GDB to debug a running process?
提问by Justin Ethier
Under linux, can I use GDB to debug a process that is currently running?
linux下可以用GDB调试当前正在运行的进程吗?
采纳答案by Carl Norum
Yes. Use the attach
command. Check out this linkfor more information. Typing help attach
at a GDB console gives the following:
是的。使用attach
命令。查看此链接了解更多信息。help attach
在 GDB 控制台中键入以下内容:
(gdb) help attach
Attach to a process or file outside of GDB. This command attaches to another target, of the same type as your last "
target
" command ("info files
" will show your target stack). The command may take as argument a process id, a process name (with an optional process-id as a suffix), or a device file. For a process id, you must have permission to send the process a signal, and it must have the same effective uid as the debugger. When using "attach
" to an existing process, the debugger finds the program running in the process, looking first in the current working directory, or (if not found there) using the source file search path (see the "directory
" command). You can also use the "file
" command to specify the program, and to load its symbol table.
(gdb) help attach
附加到 GDB 之外的进程或文件。此命令附加到另一个目标,与您上一个“
target
”命令的类型相同(“info files
”将显示您的目标堆栈)。该命令可以将进程 ID、进程名称(带有可选的进程 ID 作为后缀)或设备文件作为参数。对于进程id,你必须有权限向进程发送信号,并且必须与调试器具有相同的有效uid。当attach
对现有进程使用“ ”时,调试器会查找进程中运行的程序,首先在当前工作目录中查找,或者(如果在那里找不到)使用源文件搜索路径(请参阅“directory
”命令)。您还可以使用“file
”命令来指定程序,并加载其符号表。
NOTE: You may have difficulty attaching to a process due to improved security in the Linux kernel- for example attaching to the child of one shell from another.
注意:由于Linux 内核中的安全性提高,您可能难以附加到进程- 例如从另一个附加到一个 shell 的子进程。
You'll likely need to set /proc/sys/kernel/yama/ptrace_scope
depending on your requirements. Many systems now default to 1
or higher.
您可能需要/proc/sys/kernel/yama/ptrace_scope
根据您的要求进行设置。许多系统现在默认为1
或更高。
The sysctl settings (writable only with CAP_SYS_PTRACE) are:
0 - classic ptrace permissions: a process can PTRACE_ATTACH to any other
process running under the same uid, as long as it is dumpable (i.e.
did not transition uids, start privileged, or have called
prctl(PR_SET_DUMPABLE...) already). Similarly, PTRACE_TRACEME is
unchanged.
1 - restricted ptrace: a process must have a predefined relationship
with the inferior it wants to call PTRACE_ATTACH on. By default,
this relationship is that of only its descendants when the above
classic criteria is also met. To change the relationship, an
inferior can call prctl(PR_SET_PTRACER, debugger, ...) to declare
an allowed debugger PID to call PTRACE_ATTACH on the inferior.
Using PTRACE_TRACEME is unchanged.
2 - admin-only attach: only processes with CAP_SYS_PTRACE may use ptrace
with PTRACE_ATTACH, or through children calling PTRACE_TRACEME.
3 - no attach: no processes may use ptrace with PTRACE_ATTACH nor via
PTRACE_TRACEME. Once set, this sysctl value cannot be changed.
回答by David Kanarek
The command to use is gdb attach pid
where pid is the process id of the process you want to attach to.
要使用的命令是gdb attach pid
其中 pid 是要附加到的进程的进程 ID。
回答by Nikolai Fetissov
You can attach to a running process with gdb -p PID
.
您可以使用 附加到正在运行的进程gdb -p PID
。
回答by t0mm13b
Yes you can. Assume a process foo
is running...
是的你可以。假设一个进程foo
正在运行...
ps -elf | grep foo look for the PID number gdb -a {PID number}
回答by J. Polfer
Yes. You can do:
是的。你可以做:
gdb program_name program_pid
A shortcut would be (assuming only one instance is running):
一个快捷方式是(假设只有一个实例正在运行):
gdb program_name `pidof program_name`
回答by Nino Pereira
ps -elf doesn't seem to show the PID. I recommend using instead:
ps -elf 似乎没有显示 PID。我建议改用:
ps -ld | grep foo
gdb -p PID
回答by Milan Kerslager
If one want to attach a process, this process must have the same owner. The root is able to attach to any process.
如果要附加一个进程,该进程必须具有相同的所有者。根能够附加到任何进程。
回答by shuva
Easiest way is to provide the process id.
最简单的方法是提供进程 id。
gdb -p `pidof your_running_program_name`
Please get the full list of option in man gdb
command.
请获取man gdb
命令中选项的完整列表。
In case there are multiple process for the same program running, then the following command will list the processes.
如果同一程序运行有多个进程,则以下命令将列出进程。
ps -C program -o pid h
<number>
Then the output process id(number) can be used as argument to gdb.
然后输出进程 id(number) 可以用作 gdb 的参数。
gdb -p <process id>