start gdb using a pid

In general i see the process's pid which is running in the background and start dbx on that process using the command dbx -a <pid>

similarly how could i do it using gdb?


In addition to the previous you can directly use

gdb -p <pid>

There are two ways.

From the command line, include the pid as an argument after the executable name:

gdb /path/to/prog PID

From within gdb, you can use the attach command:

gdb /path/to/prog
gdb> attach PID

While the specifying on the command line is more concise, there is a slight risk that if you have a core file that has a name that is the same as the pid (i.e. for pid 2345, the core file would have to be named "2345") then gdb will open the core file. Admittedly, the chance of this happening is minuscule.


From the gdb man page:

You can, instead, specify a process ID as a second argument, if you want to debug a running process:

gdb program 1234