gdb高级调试技巧
1. 反向調(diào)試
gdb支持程序反向執(zhí)行。
record??讓程序開始記錄反向調(diào)試所必要的信息
rn :?reverse next
rc: reverse continue ,Continue program being debugged but run?it in reverse
record stop:?停止記錄
2.?格式化(pretty print)打印結(jié)構(gòu)體內(nèi)容
GDB調(diào)試C程序時(shí),默認(rèn)情況下輸出很不直觀,看起來很費(fèi)勁.
set print pretty on
3.AddressSanitizerAndDebugger
https://github.com/google/sanitizers/wiki/AddressSanitizerAndDebugger
b __asan::ReportGenericError
p?__asan_describe_address(0x7ffff73c3f80)
4. 條件斷點(diǎn)不生效
設(shè)置條件斷點(diǎn):
cond 1?$rdi==0x0? ?//在斷點(diǎn)一設(shè)置條件斷點(diǎn)
b 57 if?$rdi==0x0? ?//在當(dāng)前文件的57行設(shè)置條件斷點(diǎn)
有時(shí)候你會(huì)發(fā)現(xiàn)條件斷點(diǎn)沒有生效,我的解決方案是,先單步運(yùn)行(n)幾行,在運(yùn)行c.
Greg 在他的blog中也提到過這個(gè)問題:http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html
5. Command 阻塞
上例中在斷點(diǎn)1設(shè)置command,? 先打印堆棧,再continue, 你會(huì)發(fā)現(xiàn)gdb 還是會(huì)被自動(dòng)阻塞住。
你需要在gdb的配置文件.gdbinit中添加?set height 0,?會(huì)自動(dòng)避免阻塞問題
6.ptype
Print definition of type TYPE. 可以顯示類型的定義。
有個(gè)重要參數(shù): \o
/o ? ?? ?print offsets and sizes??of fields in a struct (like pahole)
struct tuv
{
int a1;
char *a2;
int a3;
};
(gdb) ptype /o struct tuv
/* offset | size */ type = struct tuv {
/* 0 | 4 */ int a1;
/* XXX 4-byte hole */
/* 8 | 8 */ char *a2;
/* 16 | 4 */ int a3;
/* total size (bytes): 24 */
}
Notice the format of the first column of comments. There, you
can find two parts separated by the ‘|’ character: the offset, which
indicates where the field is located inside the struct, in bytes, and
the size of the field. Another interesting line is the marker of a hole
in the struct, indicating that it may be possible to pack the struct
and make it use less space by reorganizing its fields.
7 File filename
修改code后,重新build。
gdb無須退出,在gdb中運(yùn)行 file a.out?命令,新編譯的a.out會(huì)自動(dòng)加載到gdb。
8.?調(diào)試多線程
info threads
thread 1
set scheduler-locking on/off/step
set?schedule-multiple on/off
thead apply all bt
thead apply 1 bt
總結(jié)
- 上一篇: 求一个蒲公英伤感的个性签名。
- 下一篇: perf + 火焰图分析程序性能