我可以使用GDB来调试一个正在运行的进程吗?

[英]Can I use GDB to debug a running process?


Under linux, can I use GDB to debug a process that is currently running?

在linux下,我可以使用GDB来调试当前正在运行的进程吗?

7 个解决方案

#1


67  

Yes. Use the attach command. Check out this link for more information. Typing help attach at a GDB console gives the following:

是的。使用附加命令。查看这个链接获得更多信息。在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之外的进程或文件。此命令附加到另一个目标,与您的上一个“目标”命令具有相同的类型(“info文件”将显示您的目标堆栈)。该命令可以以进程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的子shell附加到另一个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.

#2


89  

You can attach to a running process with gdb -p PID.

您可以使用gdb -p PID附加到正在运行的进程。

#3


22  

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`

#4


13  

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。

#5


3  

Yes you can. Assume a process foo is running...

是的,你可以。假设进程foo正在运行……

ps -elf | grep foo

look for the PID number

gdb -a {PID number}

#6


2  

ps -elf doesn't seem to show the PID. I recommend using instead:

ps -elf似乎没有显示PID。我建议使用:

ps -ld | grep foo
gdb -p PID

#7


1  

If one want to attach a process, this process must have the same owner. The root is able to attach to any process.

如果要附加一个进程,这个进程必须具有相同的所有者。根可以连接到任何进程。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/02/22/19624236869b4d7e0698480cd5267f2e.html



 
  © 2014-2022 ITdaan.com 联系我们: