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",n); if(n>2){ digui(n-1); } printf("level2-value of %d\n",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",n); 15 if(n>2){ 16 digui(n-1); 17 } 18 printf("level2-value of %d\n",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",n); 15 if(n>2){ 16 digui(n-1); 17 } 18 printf("level2-value of %d\n",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",n); (gdb) continue Continuing. level2-value of 2 Breakpoint 2, digui (n=3) at test03.c:18 18 printf("level2-value of %d\n",n); (gdb) continue Continuing. level2-value of 3 Breakpoint 2, digui (n=4) at test03.c:18 18 printf("level2-value of %d\n",n); (gdb) continue Continuing. level2-value of 4 Breakpoint 2, digui (n=5) at test03.c:18 18 printf("level2-value of %d\n",n); (gdb) continue Continuing. level2-value of 5 Breakpoint 2, digui (n=6) at test03.c:18 18 printf("level2-value of %d\n",n); (gdb) continue Continuing. level2-value of 6 Breakpoint 2, digui (n=7) at test03.c:18 18 printf("level2-value of %d\n",n); (gdb) continue Continuing. level2-value of 7 Breakpoint 2, digui (n=8) at test03.c:18 18 printf("level2-value of %d\n",n); (gdb) continue Continuing. level2-value of 8 Breakpoint 2, digui (n=9) at test03.c:18 18 printf("level2-value of %d\n",n); (gdb) continue Continuing. level2-value of 9 Breakpoint 2, digui (n=10) at test03.c:18 18 printf("level2-value of %d\n",n); (gdb) continue Continuing. level2-value of 10 Program exited normally.
run,開始運行程序;
continue,程序暫停時繼續運行程序的命令;
print 變量名或表達式,打印該變量或者該表達式的值。whatis 變量名或者表達式,可以顯示該變量或表達式的數據類型。
print 變量=值,這種形式還可以給對應的變量賦值;類似的還有set variable 變量=值。作用和用print賦值相同。
next,繼續執行下一條語句;還有一條命令step,與之類似,不同的是,當下一條語句遇到函數調用的時候,next不會跟蹤進入函數,而是繼續執行下面的語句,而step命令則會跟蹤進入函數內部。