1. 程式人生 > >gdb除錯工具的使用

gdb除錯工具的使用

1.gdb介紹

GDB是一個強大的命令列除錯工具。大家知道命令列的強大就是在於,其可以形成執行序列,形成指令碼。UNIX的軟體全是命令列的,這給程式開發提代供了極大的便利,命令列軟體的優勢在於,它們可以非常容易的整合在一起,使用幾個簡單的已有工具的命令,就可以做出一個非常強大的功能。
於是UNⅨ下的軟體比Windows下的軟體更能有機地結合,各自發揮各自的長處,組合成更為強勁的功能。而Windows下的圖形軟體基本上是各自為營,互相不能呼叫,很不利於各種軟體的相互整合。在這裡並不是要和Windows做個什麼比較,所謂“寸有所長,尺有所短”,圖形化工具還有時不如命令列的地方。

2.gdb的常用命令

  • help(h)———按模組列出命令類
  • help class——檢視某一型別的具體命令
  • list(l)———檢視程式碼,可跟行號和函式名
  • quit(q)———退出gdb
  • run(r)———-全速執行程式
  • start———–單步執行,執行程式,停在第一行執行語句
  • next(n)———逐過程執行
  • step(s)———逐語句執行,遇到函式,跳到函式內執行
  • backtrace(bt)–檢視函式的呼叫的棧幀和層級關係
  • info(i)———檢視GDB內部區域性變數的數值,info breakpoints切換函式的棧幀。
  • finish———-結束當前函式,返回到函式呼叫點
  • set————-設定變數的值 set var n = 100
  • run argv[1] argv[2]–除錯時命令列傳參
  • print(p)——–列印變數和地址
  • break(b)——–設定斷點,可根據行號和函式名
  • delete(d)——-刪除斷點d breakpoints NUM
  • display———設定觀察變數
  • undisplay——-取消觀察變數
  • continue(c)—–繼續全速執行剩下的程式碼
  • enable breakpoints ——-啟用斷點
  • disable breakpoints——-禁用斷點
  • x —————–檢視記憶體 x /20xw 顯示20個單元,16進位制,4位元組每單元
  • watch————被設定觀察點的變數發生修改時,列印顯示

  • i watch———-顯示觀察點

  • core檔案———ulimit -c 1024 開啟core檔案,除錯時gdb a.out core

3.gdb工具的使用

要想使用gdb工具進行除錯編譯好的程式,需要gcc或者g++在編譯程式的時候使用-g選項。
下面以一個迴圈連結串列程式用來除錯。

  • help
(gdb) h
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 [可以跟函式名也可以跟行號]

    (gdb) l
    6   
    7   typedef struct _Student
    8   {
    9       CircleListNode circlenode;
    10      int age;
    11  }Student;
    12  
    13  int main()
    14  {
    15      CircleList *list = CircleList_Create();
    (gdb)
  • break

(gdb) l
6   
7   typedef struct _Student
8   {
9       CircleListNode circlenode;
10      int age;
11  }Student;
12  
13  int main()
14  {
15      CircleList *list = CircleList_Create();
(gdb) b 15
Breakpoint 1 at 0x804884d: file test.c, line 15.
  • start
(gdb) start
Temporary breakpoint 2 at 0x804884d: file test.c, line 15.
Starting program: /home/cat/struct_data/a.out 

Breakpoint 1, main () at test.c:15
15      CircleList *list = CircleList_Create();
(gdb) 
  • step
(gdb) s
CircleList_Create () at circlelist.c:16
16      TCircleList *ret = (TCircleList *)malloc(sizeof(TCircleList));
(gdb) l 16
11      int length;
12  }TCircleList;
13  
14  CircleList* CircleList_Create()
15  {
16      TCircleList *ret = (TCircleList *)malloc(sizeof(TCircleList));
17      
18      if (ret == NULL)
19      {
20          return NULL;
(gdb) 
  • next
(gdb) n
18      if (ret == NULL)
(gdb) n
23      ret->header.next = NULL;
(gdb) 
24      ret->slider = NULL;
(gdb) 
25      ret->length = 0;
(gdb) 
27      return ret;
(gdb) 
28  }
(gdb) 
main () at test.c:19
19      int i = 0;
(gdb) l
14  {
15      CircleList *list = CircleList_Create();
16  
17      Student t1, t2, t3, t4;
18  
19      int i = 0;
20  
21      t1.age = 21;
22      t2.age = 22;
23      t3.age = 23;
(gdb) 

列舉幾個試試,其他的自己練習吧。