Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one obtain the pid of the currently debugged process in gdb?

Tags:

c++

linux

gdb

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.

like image 219
merlin2011 Avatar asked Sep 05 '25 03:09

merlin2011


2 Answers

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
like image 115
Tom Tromey Avatar answered Sep 07 '25 16:09

Tom Tromey


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

like image 32
罗泽轩 Avatar answered Sep 07 '25 16:09

罗泽轩