1.此文档演示如何使用gdb调试c语言代码。
代码如下:
#include <stdio.h> /*函数声明*/ void digui(int n); int main() { int n=10; digui(n); return 0; } void digui(int n) { printf("level1-value of %d ",n); if(n>2){ digui(n-1); } printf("level2-value of %d ",n); }
2.编译debug模式下的程序,编译方式如下:
[zsd@TOMCAT ~]$ gcc -g test03.c -o test03debug
3.进入gdb的debug模式,如下:
[zsd@TOMCAT ~]$ gdb test03debug GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1) Copyright (C) 2010 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/zsd/test03debug...done. (gdb)
4.gdb模式下,list命令,查看源代码:
(gdb) list 1 #include <stdio.h> 2 /*函数声明*/ 3 void digui(int n); 4 5 int main() 6 { 7 int n=10; 8 digui(n); 9 return 0; 10 } (gdb) 11 12 void digui(int n) 13 { 14 printf("level1-value of %d ",n); 15 if(n>2){ 16 digui(n-1); 17 } 18 printf("level2-value of %d ",n); 19 } 20
5.list的相关命令,如下:
(gdb) set listsize 30 //设置显示行数 (gdb) show listsize; //查看显示行数 Number of source lines gdb will list by default is 30. (gdb) list 1,20 //显示1~20行的源代码 1 #include <stdio.h> 2 /*函数声明*/ 3 void digui(int n); 4 5 int main() 6 { 7 int n=10; 8 digui(n); 9 return 0; 10 } 11 12 void digui(int n) 13 { 14 printf("level1-value of %d ",n); 15 if(n>2){ 16 digui(n-1); 17 } 18 printf("level2-value of %d ",n); 19 } 20
6.设置断点。
个人思路:由于希望研究递归函数的过程,所以对目前程序的16行和18行,设置断点。操作如下:
(gdb) break 16 //设置16行的断点 Breakpoint 1 at 0x40050c: file test03.c, line 16. (gdb) break 18 //设置18行的断点 Breakpoint 2 at 0x400519: file test03.c, line 18. (gdb) info breakpoints //查看断点信息 Num Type Disp Enb Address What 1 breakpoint keep y 0x000000000040050c in digui at test03.c:16 2 breakpoint keep y 0x0000000000400519 in digui at test03.c:18
删除断点的命令:(这里不执行,只是告知断点的方式)
(gdb) clear 16
(gdb) clear 18
7.开始调试程序
(gdb) run //开始执行调试程序 Starting program: /home/zsd/test03debug level1-value of 10 Breakpoint 1, digui (n=10) at test03.c:16 16 digui(n-1); //停止到设置在第一个断点,程序在第16行暂停。 Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 (gdb) contiune //continue命令,是在碰到断点的情况下,停止 Undefined command: "contiune". Try "help". (gdb) continue Continuing. level1-value of 9 Breakpoint 1, digui (n=9) at test03.c:16 16 digui(n-1); //第二次碰到断点,程序停止,依次递推 (gdb) continue Continuing. level1-value of 8 Breakpoint 1, digui (n=8) at test03.c:16 16 digui(n-1); (gdb) continue Continuing. level1-value of 7 Breakpoint 1, digui (n=7) at test03.c:16 16 digui(n-1); (gdb) continue Continuing. level1-value of 6 Breakpoint 1, digui (n=6) at test03.c:16 16 digui(n-1); (gdb) continue Continuing. level1-value of 5 Breakpoint 1, digui (n=5) at test03.c:16 16 digui(n-1); (gdb) continue Continuing. level1-value of 4 Breakpoint 1, digui (n=4) at test03.c:16 16 digui(n-1); (gdb) continue Continuing. level1-value of 3 Breakpoint 1, digui (n=3) at test03.c:16 16 digui(n-1); (gdb) continue Continuing. level1-value of 2 Breakpoint 2, digui (n=2) at test03.c:18 18 printf("level2-value of %d ",n); (gdb) continue Continuing. level2-value of 2 Breakpoint 2, digui (n=3) at test03.c:18 18 printf("level2-value of %d ",n); (gdb) continue Continuing. level2-value of 3 Breakpoint 2, digui (n=4) at test03.c:18 18 printf("level2-value of %d ",n); (gdb) continue Continuing. level2-value of 4 Breakpoint 2, digui (n=5) at test03.c:18 18 printf("level2-value of %d ",n); (gdb) continue Continuing. level2-value of 5 Breakpoint 2, digui (n=6) at test03.c:18 18 printf("level2-value of %d ",n); (gdb) continue Continuing. level2-value of 6 Breakpoint 2, digui (n=7) at test03.c:18 18 printf("level2-value of %d ",n); (gdb) continue Continuing. level2-value of 7 Breakpoint 2, digui (n=8) at test03.c:18 18 printf("level2-value of %d ",n); (gdb) continue Continuing. level2-value of 8 Breakpoint 2, digui (n=9) at test03.c:18 18 printf("level2-value of %d ",n); (gdb) continue Continuing. level2-value of 9 Breakpoint 2, digui (n=10) at test03.c:18 18 printf("level2-value of %d ",n); (gdb) continue Continuing. level2-value of 10 Program exited normally.
run,开始运行程序;
continue,程序暂停时继续运行程序的命令;
print 变量名或表达式,打印该变量或者该表达式的值。whatis 变量名或者表达式,可以显示该变量或表达式的数据类型。
print 变量=值,这种形式还可以给对应的变量赋值;类似的还有set variable 变量=值。作用和用print赋值相同。
next,继续执行下一条语句;还有一条命令step,与之类似,不同的是,当下一条语句遇到函数调用的时候,next不会跟踪进入函数,而是继续执行下面的语句,而step命令则会跟踪进入函数内部。