Reputation: 134177
Under linux, can I use GDB to debug a process that is currently running?
Upvotes: 155
Views: 258021
Reputation: 224962
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.
Upvotes: 113
Reputation: 1406
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>
Upvotes: 10
Reputation: 454
If one want to attach a process, this process must have the same owner. The root is able to attach to any process.
Upvotes: 3
Reputation: 71
ps -elf doesn't seem to show the PID. I recommend using instead:
ps -ld | grep foo
gdb -p PID
Upvotes: 2
Reputation: 34592
Yes you can. Assume a process foo
is running...
ps -elf | grep foo look for the PID number gdb -a {PID number}
Upvotes: 4
Reputation: 12481
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`
Upvotes: 36
Reputation: 12613
The command to use is gdb attach pid
where pid is the process id of the process you want to attach to.
Upvotes: 16