参考链接:https://www.jianshu.com/p/30ffc01380a0
实例代码
#include <stdio.h>
int add(int low,int high)
{
int i,sum;
for(i=low;i<=high;i++)
sum = sum + i;
return sum;
}
int main(int argc,char **argv)
{
int result[100];
result[0] = add(1,10);
result[1] = add(1,100);
printf("result[0] is %d\nresult[1] is %d \n",result[0],result[1]);
return 0;
}
编译: gcc -g add.c -o add 需加上 -g选项
调试: gdb add
nic@nic-Precision-T1700:~/文档/gdb$ gdb add
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from add...done.
(gdb)
帮助信息 help
(gdb) help
List of classes of commands:
aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands
Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb)
基本指令
list(简写为l) 列出当前程序的代码,默认列出10行
start 命令开始执行程序
quit 退出gdb调试环境
next(简写为n) 不进入子函数中单步调试
step(简写s) 跳入子函数中进行调试
backtrace(简写为bt) 查看函数调用的栈帧, 进入子函数后使用
frame(简写为f) 切换栈帧 frame 1
info(简写为i) 查看函数中局部变量的值 i local
print(简写为p) 打印出变量sum的值
finish 让程序一直运行到从当前函数返回为止
continue(简写为c) 运行到程序结束
set var 变量=XX 进行变量赋值
display 变量 时刻观察值的变化
undisplay 编号 取消跟踪显示
(gdb) bt
#0 add (low=1, high=10) at gdb.c:11
#1 0x08048409 in main (argc=1, argv=0xbffff324) at gdb.c:18
(gdb)
main函数的栈帧编号为1, add_range的栈帧编号为0
(gdb) i local
i = -136427546
sum = 32767
(gdb)
(gdb) frame 1
#1 0x0000000000400592 in main (argc=1, argv=0x7fffffffe028) at add.c:14
14 result[1] = add(1,100);
(gdb) i local
result = {32822, 32767, 64550200, 0, -8496, 32767, -134223128, 32767, 0, 0,
-134251752, 32767, -134253112, 32767, 4195114, 0, -140366392, 32767,
4195048, 0, 0, 1, 2099, 1, 0, 0, -8296, 32767, -8336, 32767, -134251752,
32767, 1, 0, -134224608, 32767, -134225464, 32767, -136427167, 32767, 0, 0,
-134251752, 32767, 1, 0, 0, 0, 1, 32767, -134225464, 32767, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, -134224608, 32767, -8480, 32767, -8496, 32767, -163754450, 0,
4195114, 0, -1, 0, -8136, 32767, -140369416, 32767, -134253112, 32767,
-134225464, 32767, 0, 0, 1, 0, 4195853, 0, -8384, 32767, 0, 0, 4195776, 0,
4195392, 0, -8160, 32767, 0, 0}
(gdb) frame 0
#0 add (low=1, high=100) at add.c:5
5 for(i=low;i<=high;i++)
(gdb) i local
i = 11
sum = 32822
(gdb)
断点调试
break命令(简写为b) 设置断点, break命令可以跟行数也可以跟函数名设置断点
info命令(简写为i) breakpoints 查看当前的断点信息
delete breakpoints 断点编号(info 出来的编号) 进行断点删除
delete breakpoints命令是删除所有断点
disable breakpoints 断点编号 禁用断点
enbale 断点编号 启用断点