EX:线程A创建线程B. 我希望在它开始创建线程B时阻止线程A. 但我不知道如何用GDB做到这一点。 任何帮助,将不胜感激! 感谢
答案 0 :(得分:1)
使用GDB non-stop mode并在程序调用pthread_create()后在该行放置一个断点。
Reading symbols from bad_thread...done. (gdb) set target-async 1 (gdb) set pagination off (gdb) set non-stop on (gdb) b pthread_create Breakpoint 1 at 0x400570 (gdb) r Starting program: bad_thread [Thread debugging using libthread_db enabled] Breakpoint 1, 0x0000000000400570 in pthread_create@plt () (gdb) bt #0 0x0000000000400570 in pthread_create@plt () #1 0x00000000004006d8 in main () at bad_thread.c:21 (gdb) list 12 pthread_mutex_unlock(&x_mutex); 13 14 pthread_exit(NULL); 15 } 16 17 int 18 main () { 19 pthread_t tid1, tid2; 20 21 pthread_create(&tid1, NULL, add_thread, NULL); (gdb) b bad_thread.c:22 Breakpoint 2 at 0x4006d8: file bad_thread.c, line 22. (gdb) b add_thread Breakpoint 3 at 0x400694: file bad_thread.c, line 10. (gdb) c Continuing. [New Thread 0x40a00960 (LWP 26829)] Breakpoint 2, main () at bad_thread.c:22 22 pthread_create(&tid2, NULL, add_thread, NULL); (gdb) Breakpoint 3, add_thread (arg=0x0) at bad_thread.c:10 10 pthread_mutex_lock(&x_mutex); bt #0 main () at bad_thread.c:22 (gdb)
解释上述内容: