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 threads4 Thread 0x7f7a6ee2b700 (LWP 8120) 0x000000392a8d82a7 in ioctl () from /lib64/libc.so.63 Thread 0x7f7a6e62a700 (LWP 8121) 0x000000392a8d82a7 in ioctl () from /lib64/libc.so.62 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.6Thread 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.6Thread 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 statprocess 8127cmdline = '/usr/bin/qemu-kvm'cwd = '/'exe = '/usr/bin/qemu-kvm'Process: 8127Exec file: qemu-kvmState: tParent process: 1Process group: 8118Session id: 8118TTY: 0TTY owner process group: -1Flags: 0x84020c0Minor faults (no memory page): 11392Minor faults, children: 0Major faults (memory page faults): 1Major faults, children: 0utime: 344stime: 427utime, children: 0stime, children: 0jiffies remaining in current time slice: 20'nice' value: 0jiffies until next timeout: 4jiffies until next SIGALRM: 0start time (jiffies since system boot): 1005403Virtual memory size: 892162048Resident set size: 10937rlim: 18446744073709551615Start of text: 0x400000End of text: 0x659d4cStart of stack: 0x7fff86b785b0(gdb) info proc 1 statprocess 1cmdline = '/sbin/init'cwd = '/'exe = '/sbin/upstart'Process: 1Exec file: initState: SParent process: 0Process group: 1Session id: 1TTY: 0TTY owner process group: -1Flags: 0x402100Minor faults (no memory page): 2662Minor faults, children: 4307085Major faults (memory page faults): 11Major faults, children: 808utime: 6stime: 116utime, children: 210588stime, children: 24321jiffies remaining in current time slice: 20'nice' value: 0jiffies until next timeout: 1jiffies until next SIGALRM: 0start time (jiffies since system boot): 2Virtual memory size: 19865600Resident set size: 381rlim: 18446744073709551615Start of text: 0x400000End of text: 0x421dc6Start of stack: 0x7fffa2558780I thought of printing information of "init" process :(gdb) info proc 1 statprocess 1cmdline = '/sbin/init'cwd = '/'exe = '/sbin/upstart'Process: 1Exec file: initState: SParent process: 0Process group: 1Session id: 1TTY: 0TTY owner process group: -1Flags: 0x402100Minor faults (no memory page): 2662Minor faults, children: 4307085Major faults (memory page faults): 11Major faults, children: 808utime: 6stime: 116utime, children: 210588stime, children: 24321jiffies remaining in current time slice: 20'nice' value: 0jiffies until next timeout: 1jiffies until next SIGALRM: 0start time (jiffies since system boot): 2Virtual memory size: 19865600Resident set size: 381rlim: 18446744073709551615Start of text: 0x400000End of text: 0x421dc6Start of stack: 0x7fffa2558780Thats it guys.. I hope this helps you when playing with debuggers.!!! Njoy!!![Note: modified..]