gdb高级调试技巧
1. 反向調試
gdb支持程序反向執行。
record??讓程序開始記錄反向調試所必要的信息
rn :?reverse next
rc: reverse continue ,Continue program being debugged but run?it in reverse
record stop:?停止記錄
2.?格式化(pretty print)打印結構體內容
GDB調試C程序時,默認情況下輸出很不直觀,看起來很費勁.
set print pretty on
3.AddressSanitizerAndDebugger
https://github.com/google/sanitizers/wiki/AddressSanitizerAndDebugger
b __asan::ReportGenericError
p?__asan_describe_address(0x7ffff73c3f80)
4. 條件斷點不生效
設置條件斷點:
cond 1?$rdi==0x0? ?//在斷點一設置條件斷點
b 57 if?$rdi==0x0? ?//在當前文件的57行設置條件斷點
有時候你會發現條件斷點沒有生效,我的解決方案是,先單步運行(n)幾行,在運行c.
Greg 在他的blog中也提到過這個問題:http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html
5. Command 阻塞
上例中在斷點1設置command,? 先打印堆棧,再continue, 你會發現gdb 還是會被自動阻塞住。
你需要在gdb的配置文件.gdbinit中添加?set height 0,?會自動避免阻塞問題
6.ptype
Print definition of type TYPE. 可以顯示類型的定義。
有個重要參數: \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中運行 file a.out?命令,新編譯的a.out會自動加載到gdb。
8.?調試多線程
info threads
thread 1
set scheduler-locking on/off/step
set?schedule-multiple on/off
thead apply all bt
thead apply 1 bt
總結
- 上一篇: 求一个蒲公英伤感的个性签名。
- 下一篇: 3天打鱼下一句是什么啊?