views:

690

answers:

5

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

+5  A: 

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.

Carl Norum
A: 

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

David Kanarek
+2  A: 

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

Nikolai N Fetissov
+1  A: 

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

ps -elf | grep foo

look for the PID number

gdb -a {PID number}

Hope this helps, Best regards, Tom.

tommieb75
What distribution are you running on? Using a recent version of Fedora, 'gdb -a' prints an "option -a is ambiguous" error.
Justin Ethier
+1  A: 

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`
sheepsimulator