2013-11-14 50 views

回答

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 [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) 

爲說明上述情況:在不停止模式

  • 轉按GDB文檔
  • 把一個斷點在pthread_create()來確定該程序調用該
  • 跑命中斷點
  • 回溯並列表找到下一行代碼
  • 在下一行鱈魚放置斷點e和線程啓動函數(add_thread,在這種情況下)
  • 運行
  • 注意我們在原始線程的下一行代碼處觸發了斷點。新線程在後臺繼續,然後在線程啓動功能中點擊斷點。
+0

嗨,謝謝你的回答。但它可以完全解決我的問題。我的要求是:我有一個庫,我沒有源代碼,所以我只是想在線程創建之前阻止創建線程事件(例如塊分叉事件)?怎麼做? – Dafan

+0

@Dafan - phtread_create()用於啓動一個新的線程。 fork()用於啓動一個新進程。無論哪種情況,您都應該能夠在相關的符號處添加斷點,例如'break pthread_create'。即使你的程序/庫沒有符號,這也可以工作,因爲這些符號存在於標準庫中,希望它們應該有符號。至少它在我的Ubuntu VM上工作。 –

相關問題