28

Inside gdb, what command will provide the pid of the process getting debugged?

Most of the google results discuss only how to attach gdb to a process once we know the pid.

merlin2011
  • 71,677
  • 44
  • 195
  • 329

3 Answers3

34

One simple way is info inferior. Here I'm debugging gdb with itself and this command shows the PID of the debuggee:

(top-gdb) info inferior
  Num  Description       Executable        
* 1    process 14068     /home/tromey/gdb/build/gdb/gdb 

You can also just call the ordinary C function:

(top-gdb) print getpid()
$3 = 14068
Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
4

Another method:

(gdb) python print(gdb.selected_inferior().pid)
32737

See more info about the gdb Python inferior API: https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-In-Python.html#Inferiors-In-Python

罗泽轩
  • 1,603
  • 2
  • 14
  • 19
1

On Linux info proc shows debugged process's pid and some other info

(gdb) info proc
process 28289
cmdline = '...'
cwd = '...'
exe = '...'

See https://sourceware.org/gdb/onlinedocs/gdb/Process-Information.html for detail

ten0s
  • 839
  • 8
  • 11