gdb to list process/thread information (state, command, stack..etc )

Do you know,  how powerful are debuggers ? It has lots of capabilities to track debugged program.. Here in this blog, I will describe some of the commands which are very handy with “GDB”..
I was tracking down a running program in my setup with gdb ( gdb -p),  it is nothing but “qemu-kvm”.. which is a “multithreaded application”..
I will show some example gdb commands below.
info threads” will show you the threads of debugged program…
(gdb) info threads

 4 Thread 0x7f7a6ee2b700 (LWP 8120)  0x000000392a8d82a7 in ioctl () from /lib64/libc.so.6

 3 Thread 0x7f7a6e62a700 (LWP 8121)  0x000000392a8d82a7 in ioctl () from /lib64/libc.so.6

 2 Thread 0x7f7a67875700 (LWP 8127)  0x000000392ac0b82f in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0

 * 1 Thread 0x7f7a6f039980 (LWP 8119)  0x000000392a8d89a3 in select () from /lib64/libc.so.6
below is shortform of "thread apply all bt" command which will show you back trace of all the threads..
(gdb) thr a a bt
Thread 4 (Thread 0x7f7a6ee2b700 (LWP 8120)):

 #0  0x000000392a8d82a7 in ioctl () from /lib64/libc.so.6

 #1  0x0000000000429869 in kvm_run (env=0x1a50580) at /usr/src/debug/qemu-kvm-0.13.0/qemu-kvm.c:610

 #2  0x000000000042a669 in kvm_cpu_exec (env=) at /usr/src/debug/qemu-kvm-0.13.0/qemu-kvm.c:1238

 #3  0x000000000042a8a5 in kvm_main_loop_cpu (_env=0x1a50580) at /usr/src/debug/qemu-kvm-0.13.0/qemu-kvm.c:1495

 #4  ap_main_loop (_env=0x1a50580) at /usr/src/debug/qemu-kvm-0.13.0/qemu-kvm.c:1541

 #5  0x000000392ac07b41 in start_thread () from /lib64/libpthread.so.0

 #6  0x000000392a8df49d in clone () from /lib64/libc.so.6
Thread 3 (Thread 0x7f7a6e62a700 (LWP 8121)):

 #0  0x000000392a8d82a7 in ioctl () from /lib64/libc.so.6

 #1  0x0000000000429869 in kvm_run (env=0x1a6c080) at /usr/src/debug/qemu-kvm-0.13.0/qemu-kvm.c:610

 #2  0x000000000042a669 in kvm_cpu_exec (env=) at /usr/src/debug/qemu-kvm-0.13.0/qemu-kvm.c:1238

 #3  0x000000000042a8a5 in kvm_main_loop_cpu (_env=0x1a6c080) at /usr/src/debug/qemu-kvm-0.13.0/qemu-kvm.c:1495

 #4  ap_main_loop (_env=0x1a6c080) at /usr/src/debug/qemu-kvm-0.13.0/qemu-kvm.c:1541

 #5  0x000000392ac07b41 in start_thread () from /lib64/libpthread.so.0

 #6  0x000000392a8df49d in clone () from /lib64/libc.so.6

Thread 2 (Thread 0x7f7a67875700 (LWP 8127)):

 #0  0x000000392ac0b82f in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0

 #1  0x000000000043d4bb in cond_timedwait (unused=) at posix-aio-compat.c:103

 #2  aio_thread (unused=<value optimized out>) at posix-aio-compat.c:324

 #3  0x000000392ac07b41 in start_thread () from /lib64/libpthread.so.0

 #4  0x000000392a8df49d in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7f7a6f039980 (LWP 8119)):

 #0  0x000000392a8d89a3 in select () from /lib64/libc.so.6

 #1  0x00000000004196d4 in main_loop_wait (nonblocking=) at /usr/src/debug/qemu-kvm-0.13.0/vl.c:1291

 #2  0x000000000042b067 in kvm_main_loop () at /usr/src/debug/qemu-kvm-0.13.0/qemu-kvm.c:1710

 #3  0x000000000041a51e in main_loop (argc=, argv=, envp=)

 

"thread <thread number>" this will help you to switch between "threads"...

 

(gdb) thread 3

 [Switching to thread 3 (Thread 0x7f7a6e62a700 (LWP 8121))]#0  0x000000392a8d82a7 in ioctl () from /lib64/libc.so.6

 (gdb)

Suppose you want to show "stat" or any related information of a process, you can print it in "gdb" as shown below..

** 8127" is the process id.. Here "STATE" says "t" ..

because it is being traced by the debugger..

(gdb) info proc 8127 stat

 process 8127

 cmdline = '/usr/bin/qemu-kvm'

 cwd = '/'

 exe = '/usr/bin/qemu-kvm'

 Process: 8127

 Exec file: qemu-kvm

 State: t

 Parent process: 1

 Process group: 8118

 Session id: 8118

 TTY: 0

 TTY owner process group: -1

 Flags: 0x84020c0

 Minor faults (no memory page): 11392

 Minor faults, children: 0

 Major faults (memory page faults): 1

 Major faults, children: 0

 utime: 344

 stime: 427

 utime, children: 0

 stime, children: 0

 jiffies remaining in current time slice: 20

 'nice' value: 0

 jiffies until next timeout: 4

 jiffies until next SIGALRM: 0

 start time (jiffies since system boot): 1005403

 Virtual memory size: 892162048

 Resident set size: 10937

 rlim: 18446744073709551615

 Start of text: 0x400000

 End of text: 0x659d4c

 Start of stack: 0x7fff86b785b0

 (gdb) info proc 1 stat

 process 1

 cmdline = '/sbin/init'

 cwd = '/'

 exe = '/sbin/upstart'

 Process: 1

 Exec file: init

 State: S

 Parent process: 0

 Process group: 1

 Session id: 1

 TTY: 0

 TTY owner process group: -1

 Flags: 0x402100

 Minor faults (no memory page): 2662

 Minor faults, children: 4307085

 Major faults (memory page faults): 11

 Major faults, children: 808

 utime: 6

 stime: 116

 utime, children: 210588

 stime, children: 24321

 jiffies remaining in current time slice: 20

 'nice' value: 0

 jiffies until next timeout: 1

 jiffies until next SIGALRM: 0

 start time (jiffies since system boot): 2

 Virtual memory size: 19865600

 Resident set size: 381

 rlim: 18446744073709551615

 Start of text: 0x400000

 End of text: 0x421dc6

 Start of stack: 0x7fffa2558780
           I thought of printing information of "init" process :
(gdb) info proc 1 stat

 process 1

 cmdline = '/sbin/init'

 cwd = '/'

 exe = '/sbin/upstart'

 Process: 1

 Exec file: init

 State: S

 Parent process: 0

 Process group: 1

 Session id: 1

 TTY: 0

 TTY owner process group: -1

 Flags: 0x402100

 Minor faults (no memory page): 2662

 Minor faults, children: 4307085

 Major faults (memory page faults): 11

 Major faults, children: 808

 utime: 6

 stime: 116

 utime, children: 210588

 stime, children: 24321

 jiffies remaining in current time slice: 20

 'nice' value: 0

 jiffies until next timeout: 1

 jiffies until next SIGALRM: 0

 start time (jiffies since system boot): 2

 Virtual memory size: 19865600

 Resident set size: 381

 rlim: 18446744073709551615

 Start of text: 0x400000

 End of text: 0x421dc6

 Start of stack: 0x7fffa2558780
Thats it guys.. I hope this helps you when playing with debuggers.!!! Njoy!!!
[Note: modified..]