Can I use GDB to debug a running process?

LinuxDebuggingGdb

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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

> (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.


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.

You'll likely need to set /proc/sys/kernel/yama/ptrace_scope depending on your requirements. Many systems now default to 1 or higher.

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.

Solution 3 - Linux

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`

Solution 4 - Linux

The command to use is gdb attach pid where pid is the process id of the process you want to attach to.

Solution 5 - Linux

Easiest way is to provide the process id.

gdb -p `pidof your_running_program_name`

Please get the full list of option in man gdb command.

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.

gdb -p <process id>

Solution 6 - Linux

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

ps -elf | grep foo

look for the PID number

gdb -a {PID number}

Solution 7 - Linux

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

Solution 8 - Linux

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

ps -ld | grep foo
gdb -p PID

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustin EthierView Question on Stackoverflow
Solution 1 - LinuxNikolai FetissovView Answer on Stackoverflow
Solution 2 - LinuxCarl NorumView Answer on Stackoverflow
Solution 3 - LinuxJ. PolferView Answer on Stackoverflow
Solution 4 - LinuxDavid KanarekView Answer on Stackoverflow
Solution 5 - LinuxKRoyView Answer on Stackoverflow
Solution 6 - Linuxt0mm13bView Answer on Stackoverflow
Solution 7 - LinuxMilan KerslagerView Answer on Stackoverflow
Solution 8 - LinuxNino PereiraView Answer on Stackoverflow