set follow-fork-mode [parent|child] set detach-on-fork [on|off]
follow-fork-mode
detach-on-fork
說明
parent
on
只調試主進程(GDB默認)
child
on
只調試子進程
parent
off
同時調試兩個進程,gdb跟主進程,子進程block在fork位置
child
off
同時調試兩個進程,gdb跟子進程,主進程block在fork位置
查詢正在調試的進程:info inferiors
切換調試的進程: inferior +inferior number
catch fork命令可以捕獲進程的創建
attach + pid , 可以附到一個正在運行的進程上進行調試
gdb多線程調試
show scheduler-locking //顯示當前scheduler-locking
set scheduler-locking [on/off/step] //設置scheduler-locking
on:只有當前調試線程運行,其他線程處於暫停狀態。
off:當前調試線程外的其他線程一直在正常運行。
step:其他線程跟隨當前調試線程運行,但具體怎麼協同運行,測試中無法體現。
注意:set scheduler-locking要處於線程運行環境下才能生效,也就是程序已經運行並且暫停在某個斷點處,否則會出現“Target ‘exec’ cannot support this command.”這樣的錯誤;而且經過測試,設置後的scheduler-locking值在整個進程內有效,不屬於某個線程。
gdb多進程/多線程調試實戰例子
b@b-VirtualBox:~/Documents/temp_test$ sudo gdb ./o_multi_thread_process
[sudo] password for b:
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"and"show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./o_multi_thread_process...done.
(gdb) attach 3027
Attaching to program: /home/b/Documents/temp_test/o_multi_thread_process, process 3027
Reading symbols from /lib/x86_64-linux-gnu/libpthread.so.0...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libpthread-2.19.so...done.
done.
[New LWP 3029]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Loaded symbols for /lib/x86_64-linux-gnu/libpthread.so.0
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.19.so...done.
done.
Loaded symbols for /lib/x86_64-linux-gnu/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.19.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.20x00007f5c9acb8dfdin nanosleep () at ../sysdeps/unix/syscall-template.S:8181 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) set follow-fork-mode parent
(gdb) set detach-on-fork off
(gdb) catch fork
Catchpoint 1 (fork)
(gdb) r
Starting program: /home/b/Documents/temp_test/o_multi_thread_process
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Catchpoint 1 (forked process 3002), 0x00007ffff78b7ee4in __libc_fork ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/../fork.c:130130 ../nptl/sysdeps/unix/sysv/linux/x86_64/../fork.c: No such file or directory.
(gdb) info inferiors
Num Description Executable
* 1 process 2998 /home/b/Documents/temp_test/o_multi_thread_process
(gdb) b 14
Breakpoint 2 at 0x7ffff78b7f5b: file ../nptl/sysdeps/unix/sysv/linux/x86_64/../fork.c, line 14.
(gdb) info breakpoints
Num Type Disp Enb Address What
1 catchpoint keep y fork, process 3002
catchpoint already hit 1 time
2 breakpoint keep y0x00007ffff78b7f5bin __libc_fork
at ../nptl/sysdeps/unix/sysv/linux/x86_64/../fork.c:14
(gdb) d 2
(gdb) info breakpoints
Num Type Disp Enb Address What
1 catchpoint keep y fork, process 3002
catchpoint already hit 1 time
(gdb) b multi_thread_process.cpp : 14
Breakpoint 3 at 0x4007f4: file ./multi_thread_process.cpp, line 14.
(gdb) c
Continuing.
[New process 3002]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/libpthread-2.19.so...done.
Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.19.so...done.
Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.19.so...done.
Breakpoint 3, main (argc=1, argv=0x7fffffffe598) at ./multi_thread_process.cpp:1515 if(pid != 0)
(gdb) info inferiors
Num Description Executable
2 process 3002 /home/b/Documents/temp_test/o_multi_thread_process
* 1 process 2998 /home/b/Documents/temp_test/o_multi_thread_process
(gdb) inferior 2
[Switching to inferior 2 [process 3002] (/home/b/Documents/temp_test/o_multi_thread_process)]
[Switching to thread 2 (Thread 0x7ffff7fdf740 (LWP 3002))]
00x00007ffff78b7ee4in __libc_fork () at ../nptl/sysdeps/unix/sysv/linux/x86_64/../fork.c:130130 ../nptl/sysdeps/unix/sysv/linux/x86_64/../fork.c: No such file or directory.
(gdb) set scheduler-locking on
(gdb) b multi_thread_process.cpp : 50
Breakpoint 4 at 0x400916: multi_thread_process.cpp:50. (2 locations)
(gdb) info threads
Id Target Id Frame
* 2 Thread 0x7ffff7fdf740 (LWP 3002) "o_multi_thread_"0x00007ffff78b7ee4in __libc_fork ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/../fork.c:1301 Thread 0x7ffff7fdf740 (LWP 2998) "o_multi_thread_" main (argc=1, argv=0x7fffffffe598)
at ./multi_thread_process.cpp:15
(gdb) c
Continuing.
Breakpoint 3, main (argc=1, argv=0x7fffffffe598) at ./multi_thread_process.cpp:1515 if(pid != 0)
(gdb) info threads
Id Target Id Frame
* 2 Thread 0x7ffff7fdf740 (LWP 3002) "o_multi_thread_" main (argc=1, argv=0x7fffffffe598)
at ./multi_thread_process.cpp:151 Thread 0x7ffff7fdf740 (LWP 2998) "o_multi_thread_" main (argc=1, argv=0x7fffffffe598)
at ./multi_thread_process.cpp:15
(gdb) c
Continuing.
ProcessB:3002 step1
ProcessB:3002 step2
ProcessB:3002 step3
^C
Program received signal SIGINT, Interrupt.
0x00007ffff78b7de0in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:8181 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) info threads
Id Target Id Frame
* 2 Thread 0x7ffff7fdf740 (LWP 3002) "o_multi_thread_"0x00007ffff78b7de0in __nanosleep_nocancel ()
at ../sysdeps/unix/syscall-template.S:811 Thread 0x7ffff7fdf740 (LWP 2998) "o_multi_thread_" main (argc=1, argv=0x7fffffffe598)
at ./multi_thread_process.cpp:15
(gdb) info inferiors
Num Description Executable
* 2 process 3002 /home/b/Documents/temp_test/o_multi_thread_process
1 process 2998 /home/b/Documents/temp_test/o_multi_thread_process
(gdb) inferior 1
[Switching to inferior 1 [process 2998] (/home/b/Documents/temp_test/o_multi_thread_process)]
[Switching to thread 1 (Thread 0x7ffff7fdf740 (LWP 2998))]
0 main (argc=1, argv=0x7fffffffe598) at ./multi_thread_process.cpp:1515 if(pid != 0)
(gdb) list
10 {
11 int pid;1213 pid = fork();1415 if(pid != 0)
16 processA();17 else
18 processB();19
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (yor n) n
Program not restarted.
(gdb) c
Continuing.
ProcessA:2998 step1
[New Thread 0x7ffff77f6700 (LWP 3017)]
^C
Program received signal SIGINT, Interrupt.
0x00007ffff78b7dfdin nanosleep () at ../sysdeps/unix/syscall-template.S:8181 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) info threads
Id Target Id Frame
3 Thread 0x7ffff77f6700 (LWP 3017) "o_multi_thread_" clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:812 Thread 0x7ffff7fdf740 (LWP 3002) "o_multi_thread_"0x00007ffff78b7de0in __nanosleep_nocancel ()
at ../sysdeps/unix/syscall-template.S:81
* 1 Thread 0x7ffff7fdf740 (LWP 2998) "o_multi_thread_"0x00007ffff78b7dfdin nanosleep ()
at ../sysdeps/unix/syscall-template.S:81
(gdb)