通常来讲,GDB主要帮忙你完成下面四个方面的功能:app
一、启动你的程序,能够按照你的自定义的要求为所欲为的运行程序。
二、可以让被调试的程序在你所指定的调置的断点处停住。(断点能够是条件表达式)
三、当程序被停住时,能够检查此时你的程序中所发生的事。
四、动态的改变你程序的执行环境。函数
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:spa
(参考:Debugging with GDB https://sourceware.org/gdb/current/onlinedocs/gdb/)debug
一个调试示例:调试
一、源程序:tst.ccode
1 #include <stdio.h> 2 3 int func(int n) 4 { 5 int sum=0,i; 6 for(i=0; i<n; i++) 7 { 8 sum+=i; 9 } 10 return sum; 11 } 12 13 14 int main() 15 { 16 int i; 17 long result = 0; 18 for(i=1; i<=100; i++) 19 { 20 result += i; 21 } 22 23 printf("result[1-100] = %ld /n", result ); 24 printf("result[1-250] = %d /n", func(250) ); 25 }
二、 编译生成执行文件:#gcc -g tst.c -o tstblog
三、启用GDB调试程序ci
四、#gdb tst //启动gdb,进入gdb调试环境get
五、(gdb)list //list命令 从第一行开始列出源码源码
8 sum+=i; 9 } 10 return sum; 11 } 12 13 14 int main() 15 { 16 int i; 17 long result = 0;
六、(gdb) //直接回车,表示执行上一条命令
18 for(i=1; i<=100; i++) 19 { 20 result += i; 21 } 22 23 printf("result[1-100] = %ld /n", result ); 24 printf("result[1-250] = %d /n", func(250) ); 25 }
七、(gdb) //直接回车,表示执行上一条命令
Line number 26 out of range; tst.c has 25 lines.
八、(gdb) break 16 //在源程序tst.c第16行设置断点
Breakpoint 1 at 0x400563: file tst.c, line 16.
九、(gdb)break func //在源程序中函数func()的入口处设置断点,断点在第5行
Breakpoint 2 at 0x400534: file tst.c, line 5.
十、(gdb)info break //查看断点信息
Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000400563 in main at tst.c:16 2 breakpoint keep y 0x0000000000400534 in func at tst.c:5
十一、(gdb)r //输入r,r是run命令的简写
Starting program: /root/c_study/tst Breakpoint 1, main () at tst.c:17 17 long result = 0; Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64
十二、(gdb)n //输入n,n是next命令的简写,next命令:执行下一条程序
18 for(i=1; i<=100; i++)
1三、(gdb)n
20 result += i;
1四、(gdb)n
18 for(i=1; i<=100; i++)
1五、(gdb)n
20 result += i;