多執行緒除錯必殺技 GDB的non stop模式

2021-05-28 17:33:39 字數 4423 閱讀 3076

開源的gdb被廣泛使用在linux、osx、unix和各種嵌入式系統(例如手機),這次它又帶給我們乙個驚喜。

偵錯程式(如vs2008和老版gdb)往往只支援all-stop模式,除錯多執行緒程式時,如果某個執行緒斷在乙個斷點上,你的偵錯程式會讓整個程式freeze,直到你continue這個執行緒,程式中的其他執行緒才會繼續執行。這個限制使得被除錯的程式不能夠像真實環境中那樣執行--當某個執行緒斷在乙個斷點上,讓其他執行緒並行執行。

gdbv7.0引入的non-stop模式使得這個問題迎刃而解。在這個模式下,

讓我們想象一下,有了這個功能後

因此,non-stop模式理所當然成為多執行緒除錯「必殺技」。這2023年下半年之後發布的linux版本裡都帶有gdbv7.0之後的版本。很好奇,不知道vs2010裡是不是也支援類似的除錯模式了。

1. 編譯以下程式nonstop 1//

gdb non-stop mode demo2//

build instruction: g++ -g -o nonstop nonstop.cpp -lboost_thread34

#include 

<

iostream

>

5#include 

<

boost

/thread

/thread.hpp

>67

structop8

1011

void

operator

()()

1216

17int

m_id;

18};

1920

intmain(

intargc, 

char

**argv)

2126

2. 把一下3行新增到~/.gdbinit來開啟non-stop模式

settarget-async 

1set

pagination 

offset

non-stop on

3. 啟動gdb,設斷點,執行.可以看到主線程1是running,3個子執行緒都斷在斷點上,而不是只有乙個子執行緒斷在斷點上.

~/devroot/nonstop$ gdb ./nonstop

gnu gdb (gdb) 

7.0-ubuntu

reading symbols from /home/frankwu/devroot/nonstop/nonstop...done.

(gdb) break 

14breakpoint 

1at 0x402058: file nonstop.cpp

,line 14.

(gdb) break 

24breakpoint 

3at 0x401805: file nonstop.cpp

,line 24.

(gdb) run

starting program: /home/frankwu/devroot/nonstop/nonstop

[thread debugging using libthread_db enabled][

new thread 0x7ffff6c89910 (lwp 2762)][

new thread 0x7ffff6488910 (lwp 2763)]1

begin

breakpoint 1,

op::operator() (this

=0x605118) at nonstop.cpp:

1414

std::cout << m_id << 

"end

"<< std::endl;2

begin

breakpoint 1,

op::operator() (this

=0x605388) at nonstop.cpp:

1414

std::cout << m_id << 

"end

"<< std::endl;[

new thread 0x7ffff5c87910 (lwp 2764)]3

begin

breakpoint 1,

op::operator() (this

=0x605618) at nonstop.cpp:

1414

std::cout << m_id << 

"end

"<< std::endl

;(gdb) info threads

4thread 0x7ffff5c87910 (lwp 

2764

)  op::operator() (this

=0x605618) at nonstop.cpp:143

thread 0x7ffff6488910 (lwp 

2763

)  op::operator() (this

=0x605388) at nonstop.cpp:142

thread 0x7ffff6c89910 (lwp 

2762

)  op::operator() (this

=0x605118) at nonstop.cpp:

14* 

1thread 0x7ffff7fe3710 (lwp 

2759

)  (running)

4. 讓執行緒3繼續執行,注意我顧意把主線程1也continue,這是我發現的workaround,否則gdb不能切回thread 1.31

continue

thread 

3(thread 0x7ffff6488910 (lwp 

2763

)):continuing.

thread 

1(thread 0x7ffff7fe3710 (lwp 

2759

)):continuing.

cannot execute this command while the selected thread is running.

2end

[thread 0x7ffff6488910 (lwp 2763) exited

]warning: unknown thread 3.

thread 

1(thread 0x7ffff7fe3710 (lwp 

2759

)):continuing.

cannot execute this command while the selected thread is running.

(gdb) info threads

4thread 0x7ffff5c87910 (lwp 

2764

)  op::operator() (this

=0x605618) at nonstop.cpp:142

thread 0x7ffff6c89910 (lwp 

2762

)  op::operator() (this

=0x605118) at nonstop.cpp:

14* 

1thread 0x7ffff7fe3710 (lwp 

2759

)  (running)421

continue

thread 

4(thread 0x7ffff5c87910 (lwp 

2764

)):continuing.

thread 

2(thread 0x7ffff6c89910 (lwp 

2762

)):continuing.

thread 

1(thread 0x7ffff7fe3710 (lwp 

2759

)):continuing.

cannot execute this command while the selected thread is running.

3end

1end

[thread 0x7ffff5c87910 (lwp 2764) exited][

thread 0x7ffff6c89910 (lwp 2762) exited

]breakpoint 3,

main (argc=1

,argv

=0x7fffffffe348) at nonstop.cpp:

2424

return 0;

(gdb) continue

thread 

1(thread 0x7ffff7fe3710 (lwp 

2759

)):continuing.

program exited normally.

debugging with gdb

reverse debugging, multi-process and non-stop debugging come to the cdt 

拯救頻繁跳槽的必殺技!

對於任何乙個公司而言,員工的不穩定是最大的隱患,這不僅導致公司招聘成本的增加,也破壞了工作的延續性。所以在面試過程中,公司也會特別關注應聘者的跳槽頻率。我對於頻繁跳槽的人並沒有太多的鄙夷。很多任務作在應聘時是看不出好壞的。就像公司對新錄用的員工有乙個所謂的試用期,那麼員工也可以在 試用期 後因為自己...

來往乾掉微信的必殺技

一 打通所有主流作業系統 android,ios,ubuntu,mac,windows,windows phone 二 利用 的各個商家的優勢,推出類似谷歌藝術計畫的功能。讓消費者可以實時檢視店鋪內的衣服。同時拖動一件衣服就可以共享給好友 易用性 四 為各大商家提供智慧型安防的服務。當晚上店鋪關門的...

硬體工程師的必殺技

1 充分了解各方的設計需求,確定合適的解決方案 啟動乙個硬體開發專案,原始的推動力會來自於很多方面,比如市場的需要,基於整個 系統架構的需要,應用軟體部門的功能實現需要,提高系統某方面能力的需要等等,所以作為乙個硬體系統的 設計者,要主動的去了解各個方面的需求,並且綜合起來,提出最合適的硬體解決方案...