Command Description file load PRogram b set breakpoint r run c continue s step (line) si step (machine instrUCtion) n next (step over function call) finish run until function returns i r show all registers i r show specific register l list source p display value set args set command line arguments
要設置基本的斷點,您可以在某個函數名稱或行號上中斷。例如,b 27 將在當前文件的第 27 行上設置了一個斷點。有兩種使用函數名稱的方式:b main 在函數 main 中的第一行可執行代碼上中斷,b *main 在 main 的入口地址上設置一個斷點(假如打算單步調試函數的每條指令,這樣是很有用的)。
一旦設置了第一個斷點,可使用 run 或 r 來啟動程序并運行到第一個斷點。還可以不帶任何斷點運行程序,假如您不知道程序是在何處崩潰的,這樣將很有幫助。當您命中一個斷點 c 或 continue 時,程序將恢復執行,直至命中下一個斷點。
step“單步”調試源代碼行。Step instruction (si) 單步調試機器代碼行(當您單步調試優化過的代碼時,si 指令可能非凡有用,這將在后面介紹)。 next 工作起來就像 step,但是它不跟蹤進入函數調用(假如的確錯誤地跟蹤進入了函數調用,可使用 finish 來完成該函數,然后在它返回的地方中斷)。
Command Description file load program core load core file BT back trace where same as back trace i f frame information up move up stack down move down stack frame jump to frame disassem display function’s machine code i locals display local variable values
圖 2
圖 2 突出顯示了一系列便利的 post mortem 命令。
(gdb) file simple Reading symbols from simple...done. (gdb) core core Core was generated by `./simple’. Program terminated with signal 11, Segmentation fault. Reading symbols from /lib/libc.so.6...done. Loaded symbols for /lib/libc.so.6 Reading symbols from /lib/ld.so.1...done. Loaded symbols for /lib/ld.so.1 #0 0x400ab738 in memcpy () from /lib/libc.so.6 (gdb) where #0 0x400ab738 in memcpy () from /lib/libc.so.6 #1 0x40066e in main () at simple.c:34 #2 0x40041eb8 in __libc_start_main () from /lib/libc.so.6 #3 0x4004ac in _start () (gdb) i f Stack level 0, frame at 0x7ffff7a0: pswa = 0x400ab738 in memcpy; saved pswa 0x0 (FRAMELESS), called by frame at 0x7ffff7a0 Arglist at 0x7ffff7a0, args: Locals at 0x7ffff7a0, Previous frame’s sp is 0x0 (gdb) up #1 0x40066e in main () at simple.c:34 34 memcpy (doink.boik, boink.boik, sizeof(boink.boik)); (gdb) i locals doink = {boik = 0x4019a0} boink = {boik = 0x0} (gdb) ptype boink.boik type = int * (gdb) print *boink.boik Cannot access memory at address 0x0 (gdb) print *doink.boik $1 = 4
(gdb) break main Breakpoint 1 at 0x800007a8: file simple.c, line 32. (gdb) r Starting program: /home/grundym/foo/simple Breakpoint 1, main () at simple.c:32 32 do_one_thing(&doink); (gdb) s 30 doink.boik = &r1; (gdb) 32 do_one_thing(&doink); (gdb) do_one_thing (pnum_times=0x1fffffff690) at simple.c:47 47 for (i = 0; i < 4; i++) {
圖 4
如何處理這種情況呢?使用 si 和 ni(next instruction;它類似 si,但是會跳過子例程調用)將非常有幫助。 在這個層次上,很好理解 zArchitecture 是有所幫助的。
(gdb) break *main Breakpoint 1 at 0x80000794: file simple.c, line 27. (gdb) display /i $pswa (gdb) r Starting program: /home/grundym/foo/simple
Breakpoint 1, main () at simple.c:27 27 { 1: x/i $pswa 0x80000794 : EB AF F0 50 00 24 stmg %r10,%r15,80(%r15)