2
EX:線程A創建線程B. 我希望在線程A開始創建線程B時阻塞線程。 但我不知道如何使用GDB。 任何幫助,將不勝感激! 感謝如何在創建新線程時阻止程序執行? (與GDB)
EX:線程A創建線程B. 我希望在線程A開始創建線程B時阻塞線程。 但我不知道如何使用GDB。 任何幫助,將不勝感激! 感謝如何在創建新線程時阻止程序執行? (與GDB)
使用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 [email protected]() (gdb) bt #0 0x0000000000400570 in [email protected]() #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)
爲說明上述情況:在不停止模式
嗨,謝謝你的回答。但它可以完全解決我的問題。我的要求是:我有一個庫,我沒有源代碼,所以我只是想在線程創建之前阻止創建線程事件(例如塊分叉事件)?怎麼做? – Dafan
@Dafan - phtread_create()用於啓動一個新的線程。 fork()用於啓動一個新進程。無論哪種情況,您都應該能夠在相關的符號處添加斷點,例如'break pthread_create'。即使你的程序/庫沒有符號,這也可以工作,因爲這些符號存在於標準庫中,希望它們應該有符號。至少它在我的Ubuntu VM上工作。 –