多線程調(diào)試方法_第1頁
多線程調(diào)試方法_第2頁
多線程調(diào)試方法_第3頁
多線程調(diào)試方法_第4頁
多線程調(diào)試方法_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、set target-async 1set pagination offset non-stop oninfo threads 顯示當前可調(diào)試的所有線程,每個線程會有一個GDB為其分配的ID,后面操作線程的時候會用到這個ID。 前面有*的是當前調(diào)試的線程。 thread ID 切換當前調(diào)試的線程為指定ID的線程。 break thread_test.c:123 thread all在所有線程中相應(yīng)的行上設(shè)置斷點thread apply ID1 ID2 command 讓一個或者多個線程執(zhí)行GDB命令command。 thre

2、ad apply all command 讓所有被調(diào)試線程執(zhí)行GDB命令command。 set scheduler-locking off|on|step 估計是實際使用過多線程調(diào)試的人都可以發(fā)現(xiàn),在使用step或者continue命令調(diào)試當前被調(diào)試線程的時候,其他線程也是同時執(zhí)行的,怎么只讓被調(diào)試程序執(zhí)行呢?通過這個命令就可以實現(xiàn)這個需求。off 不鎖定任何線程,也就是所有線程都執(zhí)行,這是默認值。 on 只有當前被調(diào)試程序會執(zhí)行。 step 在單步的時候,除了next過一個函數(shù)的情況(熟悉情況的人可能知道,這其實是一個設(shè)置斷點然后conti

3、nue的行為)以外,只有當前線程會執(zhí)行。 作者:破砂鍋 開源的GDB被廣泛使用在Linux、OSX、Unix和各種嵌入式系統(tǒng)(例如手機),這次它又帶給我們一個驚喜。 多線程調(diào)試之痛 調(diào)試器(如VS2008和老版GDB)往往只支持all-stop模式,調(diào)試多線程程序時,如果某個線程斷在一個斷點上,你的調(diào)試器會讓整個程序freeze,直到你continue這個線程,程序中的其他線程才會繼續(xù)運行。這個限制使得被調(diào)試的程序不能夠像真實環(huán)境中那樣運行-當某個線程斷在一個斷點上,讓其他線程并行運行。GDBv7.0引入的non-stop模式使得這個問題迎刃而解。在這個模式下,· 當某個或

4、多個線程斷在一個斷點上,其他線程仍會并行運行 · 你可以選擇某個被斷的線程,并讓它繼續(xù)運行 讓我們想象一下,有了這個功能后· 當其他線程斷在斷點上時,程序里的定時器線程可以正常的運行了,從而避免不必要得超時  · 當其他線程斷在斷點上時,程序里的watchdog線程可以正常的運行了,從而避免嵌入式硬件以為系統(tǒng)崩潰而重啟 · 可以控制多個線程運行的順序,從而重現(xiàn)deadlock場景了。由于GDB可以用python腳本驅(qū)動調(diào)試,理論上可以對程序在不同的線程運行順序下進行自動化測試。因此,non-stop模式理所當然成為多線程調(diào)試“必殺技”。這200

5、9年下半年之后發(fā)布的Linux版本里都帶有GDBv7.0之后的版本。很好奇,不知道VS2010里是不是也支持類似的調(diào)試模式了。演示GDB的non-stop模式  讓破砂鍋用一個C+小程序在Ubuntu Linux 09.10下demo這個必殺技。雖然我的demo使用命令行版gdb,如果你喜歡圖形化的調(diào)試器,Eclipse2009年5月之后的版本可以輕松的調(diào) 用這個功能,詳情參見Eclipse參見/node/723   1. 編譯以下程序nonstop  1 / gdb non-st

6、op mode demo 2 / build instruction: g+ -g -o nonstop nonstop.cpp -lboost_thread 3  4 #include <iostream> 5 #include <boost/thread/thread.hpp> 6  7 struct op 8

7、  9         op(int id): m_id(id) 10 11         void operator()()12         13         

8、;        std:cout << m_id << " begin" << std:endl;14                 std:cout << m_id <&

9、lt; " end" << std:endl;15         16 17         int m_id;18 19 20 int main(int argc, char * argv)21 22   

10、;      boost:thread t1(op(1), t2(op(2), t3(op(3);23         t1.join(); t2.join(); t3.join();24         return 0;25 26  2. 把一下3行添加到/.gdb

11、init來打開non-stop模式 set target-async 1set pagination offset non-stop on 3. 啟動gdb,設(shè)斷點,運行.可以看到主線程1是running,3個子線程都斷在斷點上,而不是只有一個子線程斷在斷點上./devroot/nonstop$ gdb ./nonstopGNU gdb (GDB) 7.0-ubuntuReading symbols from /home/frankwu/devr

12、oot/nonstop/nonstop.done.(gdb) break 14Breakpoint 1 at 0x402058: file nonstop.cpp, line 14.(gdb) break 24Breakpoint 3 at 0x401805: file nonstop.cpp, line 24.(gdb) runStarting program: /home/frank

13、wu/devroot/nonstop/nonstopThread debugging using libthread_db enabledNew Thread 0x7ffff6c89910 (LWP 2762)New Thread 0x7ffff6488910 (LWP 2763)1 beginBreakpoint 1, op:operator() (this=0x605118) at nonstop.cpp:141

14、4                  std:cout << m_id << " end" << std:endl;2 beginBreakpoint 1, op:operator() (this=0x605388) at nonst

15、op.cpp:1414                  std:cout << m_id << " end" << std:endl;New Thread 0x7ffff5c87910 (LWP 2764)3 beginBreakpoint&

16、#160;1, op:operator() (this=0x605618) at nonstop.cpp:1414                  std:cout << m_id << " end" << std:endl;(gdb) in

17、fo threads  4 Thread 0x7ffff5c87910 (LWP 2764)  op:operator() (this=0x605618) at nonstop.cpp:14  3 Thread 0x7ffff6488910 (LWP 2763)  op:operator() (this=0x605388) at nonstop.cpp:14 

18、; 2 Thread 0x7ffff6c89910 (LWP 2762)  op:operator() (this=0x605118) at nonstop.cpp:14* 1 Thread 0x7ffff7fe3710 (LWP 2759)  (running)4. 讓線程3繼續(xù)運行,注意我顧意把主線程1也continue,這是我發(fā)現(xiàn)的workaround,否則gdb不能切回thread 1.(gdb) threa

19、d apply 3 1 continueThread 3 (Thread 0x7ffff6488910 (LWP 2763):Continuing.Thread 1 (Thread 0x7ffff7fe3710 (LWP 2759):Continuing.Cannot execute this command while the selected thread is 

20、running.2 endThread 0x7ffff6488910 (LWP 2763) exitedwarning: Unknown thread 3.Thread 1 (Thread 0x7ffff7fe3710 (LWP 2759):Continuing.Cannot execute this command while the selected thread is r

21、unning.(gdb) info threads  4 Thread 0x7ffff5c87910 (LWP 2764)  op:operator() (this=0x605618) at nonstop.cpp:14  2 Thread 0x7ffff6c89910 (LWP 2762)  op:operator() (this=0x605118) at 

22、;nonstop.cpp:14* 1 Thread 0x7ffff7fe3710 (LWP 2759)  (running)5. 讓另外兩個線程繼續(xù)運行而結(jié)束,主線程斷在第24行,最后結(jié)束.(gdb) thread apply 4 2 1 continueThread 4 (Thread 0x7ffff5c87910 (LWP 2764):Continuing.Thread 2 (Thread 0x7ffff6c89910 (LWP 2762):Continuin

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論