we can debug a process that has already been started outside the debugger. There are two ways of doing this:
- Using command line arguments
- Using the attach command.
command line arguments
start GDB in another console with an argument list of the executable and the process ID.
$ gdb beer-process 17399
Attaching to program: code/running_process/beer-process, process 17399
0x410c64fb in nanosleep () from /lib/tls/libc.so.6
(gdb)
Whenever GDB attaches to a running process, the process is paused so you can get a handle on what the call stack looks like. Let’s do some interesting things.
When GDB detaches from the process, the process will continue along its merry way. We could also use the detach
command to detach from the process without quiting GDB.
the attach command
We can also debug an already running process using GDB’s attach
command to attach to a running process. Again, once attached, we can use the detach
command to detach from the process.
Start GDB with no command line arguments. But use the attach command to attach to the running process.
$ gdb
(gdb) attach 17399
Attaching to process 17399
Reading symbols from code/running_process/beer-process...done.
0x410c64fb in nanosleep () from /lib/tls/libc.so.6
(gdb)