操作环境:
[root@localhost zhangsan]# cat /proc/version
Linux version 2.6.32-696.el6.x86_64 (mockbuild@c1bm.rdu2.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) ) #1 SMP Tue Mar 21 19:29:05 UTC 2017
编译安装gdb
[root@localhost zhangsan]# wget http://ftp.gnu.org/gnu/gdb/gdb-7.6.1.tar.gz
[root@localhost zhangsan]# tar -zxvf gdb-7.6.1.tar.gz
[root@localhost zhangsan]# cd gdb-7.6.1
[root@localhost gdb-7.6.1]# ./configure
[root@localhost gdb-7.6.1]# make
[root@localhost gdb-7.6.1]# make install
[root@localhost gdb-7.6.1]# cp /home/zhangsan/gdb-7.6.1/gdb/gdb /usr/bin/gdb //将编译好的gdb拷贝到/usr/bin目录中
[root@localhost gdb-7.6.1]# gdb -v //查看安装是否成功
GNU gdb (GDB) 7.6.1
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-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
单步执行和跟踪函数调用
函数调试实例:
#include <stdio.h>
int add_range(int low, int high)
{
int i,sum;
for (i = low; i <= high; i++)
sum = sum + i;
return sum;
}
int main(void)
{
int result[100];
result[0] = add_range(1, 10);
result[1] = add_range(1,100);
printf("result[0]= %d\nresult[1]=%d\n", result[0],result[1]);
return 0;
}
[zhangsan@localhost study-c]$ gcc -Wall -g gdb1.c -o gdb1
[zhangsan@localhost study-c]$ ./gdb1
result[0]= 55
result[1]=5105
可见result[1]=5105显然是不正确的,在编译时要加上-g选项,生成的可执行文件才能用gdb进行源码级调试:
[zhangsan@localhost study-c]$ gdb gdb1
GNU gdb (GDB) 7.6.1
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 fre