start gdb using a pid

65,089

Solution 1

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.

Solution 2

In addition to the previous you can directly use

gdb -p <pid>

Solution 3

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
Share:
65,089

Related videos on Youtube

Vijay
Author by

Vijay

http://theunixshell.blogspot.com/

Updated on October 03, 2020

Comments

  • Vijay
    Vijay over 3 years

    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?

  • AnT stands with Russia
    AnT stands with Russia over 6 years
    This is the correct answer. Attaching GDB to a PID should not in any way involve specifying the path to the executable. The correct solution shall involve only PID and nothing else.