(gdb) b 8(gdb) b main (gdb) b *main (gdb) b *0x804835c (gdb) d
s, n
s: 执行一行源程序代码,如果此行代码中有函数调用,则进入该函数; n: 执行一行源程序代码,此行代码中的函数调用也一并执行。 s 相当于其它调试器中的“Step Into (单步跟踪进入)”; n 相当于其它调试器中的“Step Over (单步跟踪)”。 这两个命令必须在有源代码调试信息的情况下才可以使用(GCC编译时使用“-g”参数)。
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh) Copyright 2003 free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux-gnu". (gdb)
(gdb) file gdb-sample Reading symbols from gdb-sample...done.
上面最后一行提示已经加载成功。
下面使用“r”命令执行(Run)被调试文件,因为尚未设置任何断点,将直接执行到程序结束:
1 2 3 4 5 6
(gdb) r Starting program: /home/liigo/temp/test_jmp/test_jmp/gdb-sample n = 1, nGlobalVar = 88 tempFunction is called, a = 1, b = 2 n = 3 Program exited normally.
下面使用“b”命令在 main 函数开头设置一个断点(Breakpoint):
1 2
(gdb) b main Breakpoint 1 at 0x804835c: file gdb-sample.c, line 19.
上面最后一行提示已经成功设置断点,并给出了该断点信息:在源文件 gdb-sample.c 第19行处设置断点;这是本程序的第一个断点(序号为1);断点处的代码地址为 0x804835c(此值可能仅在本次调试过程中有效)。回过头去看源代码,第19行中的代码为“n = 1”,恰好是 main 函数中的第一个可执行语句(前面的“int n;”为变量定义语句,并非可执行语句)。
再次使用“r”命令执行(Run)被调试程序:
1 2 3 4 5
(gdb) r Starting program: /home/liigo/temp/gdb-sample
Breakpoint 1, main () at gdb-sample.c:19 19 n = 1;
Breakpoint 2, main () at gdb-sample.c:26 26 printf("n = %d, nGlobalVar = %d /n", n, nGlobalVar); (gdb) p nGlobalVar $2 = 88 (gdb) c Continuing. n = 1, nGlobalVar = 88
Breakpoint 3, tempFunction (a=1, b=2) at gdb-sample.c:12 12 printf("tempFunction is called, a = %d, b = %d /n", a, b); (gdb) p a $3 = 1 (gdb) p b $4 = 2
上面反馈的信息一切都在我们预料之中~~
再一次执行“c”命令(Continue),因为后面再也没有其它断点,程序将一直执行到结束:
1 2 3 4 5
(gdb) c Continuing. tempFunction is called, a = 1, b = 2 n = 3 Program exited normally.
有时候需要看到编译器生成的汇编代码,以进行汇编级的调试或跟踪,又该如何操作呢?
这就要用到display命令“display /i $pc”了(此命令前面已有详细解释):
1 2
(gdb) display /i $pc (gdb)
此后程序再中断时,就可以显示出汇编代码了:
1 2 3 4 5 6
(gdb) r Starting program: /home/liigo/temp/test_jmp/test_jmp/gdb-sample
Breakpoint 1, main () at gdb-sample.c:19 19 n = 1; 1: x/i $pc 0x804835c <main+16>: movl $0x1,0xfffffffc(%ebp)
(gdb) b *main Breakpoint 4 at 0x804834c: file gdb-sample.c, line 17. (gdb) r The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /home/liigo/temp/test_jmp/test_jmp/gdb-sample
Breakpoint 4, main () at gdb-sample.c:17 17 { 1: x/i $pc 0x804834c <main>: push %ebp (gdb) si 0x0804834d 17 { 1: x/i $pc 0x804834d <main+1>: mov %esp,%ebp (gdb) si 0x0804834f in main () at gdb-sample.c:17 17 { 1: x/i $pc 0x804834f <main+3>: sub $0x8,%esp (gdb) si 0x08048352 17 { 1: x/i $pc 0x8048352 <main+6>: and $0xfffffff0,%esp (gdb) si 0x08048355 17 { 1: x/i $pc 0x8048355 <main+9>: mov $0x0,%eax (gdb) si 0x0804835a 17 { 1: x/i $pc 0x804835a <main+14>: sub %eax,%esp (gdb) si 19 n = 1; 1: x/i $pc 0x804835c <main+16>: movl $0x1,0xfffffffc(%ebp)