GDB,是The GNU Project Debugger 的缩写,是 Linux 下功能全面的调试工具。 GDB支持断点、单步执行、打印变量、观察变量、查看寄存器、查看堆栈等调试手段。
gdb 文件名
[wkj@VM-4-13-centos lesson8]$ gdb mytest
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/wkj/lesson8/mytest...done.
输入list可显示代码,简写为l,不会一次显示全部代码。
由于gdb会记录历史命令即最近一条命令,如果命令无变化可以回车继续执行该命令。
丝滑小连招:l 0+回车+回车……即可打印全部代码
(gdb) l 0
1 #include
2
3 int AddToTop(int top)
4 {
5 int res = 0;
6 int i = 0;
7 for(i = 0;i <= 100;i++)
8 {
9 res+=i;
10 }
(gdb)
11 return res;
12 }
13
14
15 int main()
16 {
17 int top = 100;
18 int result = AddToTop(top);
19
20 printf("result:%d\n",result);
(gdb)
21
22 return 0;
23 }
r :开始调试,若没有断点,则直接结束
(gdb) b 6
Breakpoint 1 at 0x40053b: file mytest.c, line 6.
(gdb) b 9
Breakpoint 2 at 0x40054b: file mytest.c, line 9.
见下文。
由于打断点时系统会给断点编号,删除时用不得行号,可用编号进行删除。
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040053b in AddToTop at mytest.c:6
2 breakpoint keep y 0x000000000040054b in AddToTop at mytest.c:9
(gdb) d 2
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040053b in AddToTop at mytest.c:6
(gdb) n
7 for(i = 0;i <= 100;i++)
(gdb) n
9 res+=i;
(gdb) s
7 for(i = 0;i <= 100;i++)
(gdb) bt
#0 AddToTop (top=100) at mytest.c:7
#1 0x0000000000400579 in main () at mytest.c:18
(gdb) finish
Run till exit from #0 AddToTop (top=100) at mytest.c:7
0x0000000000400579 in main () at mytest.c:18
18 int result = AddToTop(top);
Value returned is $1 = 5050
(gdb) bt
#0 0x0000000000400579 in main () at mytest.c:18
(gdb) display res
1: res = 0
(gdb) n
7 for(i = 0;i <= 100;i++)
1: res = 0
(gdb) n
9 res+=i;
1: res = 0
(gdb) n
7 for(i = 0;i <= 100;i++)
1: res = 0
(gdb) n
9 res+=i;
1: res = 0
(gdb) n
7 for(i = 0;i <= 100;i++)
1: res = 1
(gdb) n
9 res+=i;
1: res = 1
(gdb) undisplay 1
(gdb) n
7 for(i = 0;i <= 100;i++)
(gdb) until 18
0x0000000000400579 in main () at mytest.c:18
18 int result = AddToTop(top);
(gdb) c
Continuing.
result:5050
[Inferior 1 (process 18009) exited normally]
(gdb) disable 1
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep n 0x000000000040053b in AddToTop at mytest.c:6breakpoint already hit 1 time
注意看,断点1的End变成了n,表示被禁用
(gdb) enable 1
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040053b in AddToTop at mytest.c:6breakpoint already hit 1 time
退出调试
以上就是gdb的一些基础命令啦。
