文章目录
一、gdb简介
GDB,是The GNU Project Debugger 的缩写,是 Linux 下功能全面的调试工具。 GDB支持断点、单步执行、打印变量、观察变量、查看寄存器、查看堆栈等调试手段。
二、gdb基础命令操作
1.进入gdb :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 <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/wkj/lesson8/mytest...done.
2.显示代码:l
输入list可显示代码,简写为l,不会一次显示全部代码。
由于gdb会记录历史命令即最近一条命令,如果命令无变化可以回车继续执行该命令。
丝滑小连招:l 0+回车+回车……即可打印全部代码
(gdb) l 0
1 #include<stdio.h>
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 }
3.开始调试:r
r :开始调试,若没有断点,则直接结束
4.打断点:b 行号
(gdb) b 6
Breakpoint 1 at 0x40053b: file mytest.c, line 6.
(gdb) b 9
Breakpoint 2 at 0x40054b: file mytest.c, line 9.
5.显示断点信息:info b
见下文。
6.删断点:d 编号
由于打断点时系统会给断点编号,删除时用不得行号,可用编号进行删除。
(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
7.逐过程:n
(gdb) n
7 for(i = 0;i <= 100;i++)
(gdb) n
9 res+=i;
8.逐语句:s
(gdb) s
7 for(i = 0;i <= 100;i++)
9.进入堆栈:bt
(gdb) bt
#0 AddToTop (top=100) at mytest.c:7
#1 0x0000000000400579 in main () at mytest.c:18
10.直接跑完当前函数并停下:finish
(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
11.常显示:dispaly 变量名
(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
12.取消常显示:undisplay 编号
(gdb) undisplay 1
(gdb) n
7 for(i = 0;i <= 100;i++)
13.跳转到指定行:until 指定行
(gdb) until 18
0x0000000000400579 in main () at mytest.c:18
18 int result = AddToTop(top);
14.运行至下一个断点处停下:c
(gdb) c
Continuing.
result:5050
[Inferior 1 (process 18009) exited normally]
15.禁用断点:disable 编号
(gdb) disable 1
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep n 0x000000000040053b in AddToTop at mytest.c:6
breakpoint already hit 1 time
注意看,断点1的End变成了n,表示被禁用
16.取消禁用:enable 编号
(gdb) enable 1
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040053b in AddToTop at mytest.c:6
breakpoint already hit 1 time
17.退出gdb:quit
退出调试
总结
以上就是gdb的一些基础命令啦。