gdb
GDB是一個強大的linux下的程序調(diào)試工具
其使用過程如下:
debug方式編譯
在編譯前加上 -g
gcc test.c -g -o test
通過gdb啟動程序
gdb test
為代碼添加斷點
在gdb 命令模式下 (gdb):
list [n] 可以顯示從n到n+10行的代碼 ->l
break [n] 給第n行添加一個斷點 ->b
break [func name] 給函數(shù)func入口設(shè)置斷點
break if <condition> 設(shè)置條件斷點
delete [i] 刪除第i個斷點
-
info break 顯示斷點信息
(gdb) b 8 Breakpoint 1 at 0x40053d: file test.c, line 8. (gdb) b func Breakpoint 2 at 0x40052d: file test.c, line 5. (gdb) b 20 Breakpoint 3 at 0x40056c: file test.c, line 20. (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x000000000040053d in func at test.c:8 2 breakpoint keep y 0x000000000040052d in func at test.c:5 3 breakpoint keep y 0x000000000040056c in main at test.c:20
run 運行程序 -> r
-
watch <expr> 觀察點
Hardware watchpoint 4: i>50 Old value = 0 New value = 1 0x0000000000400547 in func (n=250) at test.c:6
在運行過程中:
next 單步運行 ->n
step 單步進行 ->s
-
print [x] 打印變量x的值 ->p
(gdb) p i $25 = 51
-
backtrace 查看調(diào)用的函數(shù)堆棧 ->bt
(gdb) bt #0 0x0000000000400547 in func (n=250) at test.c:6 #1 0x000000000040059a in main () at test.c:24