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)