用GDB偵錯工具(一)

來源:互聯網
上載者:User

標籤:http   使用   io   strong   檔案   for   ar   art   div   

用GDB偵錯工具

GDB概述
————

GDB是GNU開源組織公布的一個強大的UNIX下的程式調試工具。也許,各位比較喜歡那種圖形介面方式的,像VC、BCB等IDE的調試,但假設你是在UNIX平台下做軟體,你會發現GDB這個調試工具有比VC、BCB的圖形化調試器更強大的功能。所謂“寸有所長,尺有所短”就是這個道理。

一般來說,GDB主要幫忙你完畢以下四個方面的功能:

    1、啟動你的程式,能夠依照你的自己定義的要求隨心所欲的執行程式。
    2、可讓被調試的程式在你所指定的調置的斷點處停住。(斷點能夠是條件運算式)
    3、當程式被停住時,能夠檢查此時你的程式中所發生的事。
    4、動態改變你程式的執行環境。

從上面看來,GDB和一般的調試工具沒有什麼兩樣,基本上也是完畢這些功能,只是在細節上,你會發現GDB這個調試工具的強大,大家可能比較習慣了圖形化的調試工具,但有時候,命令列的調試工具卻有著圖形化工具所不能完畢的功能。讓我們一一看來。


一個調試示範範例
——————

來源程式:tst.c

     1 #include <stdio.h>
     2
     3 int func(int n)
     4 {
     5         int sum=0,i;
     6         for(i=0; i<n; i++)
     7         {
     8                 sum+=i;
     9         }
    10         return sum;
    11 }
    12
    13
    14 main()
    15 {
    16         int i;
    17         long result = 0;
    18         for(i=1; i<=100; i++)
    19         {
    20                 result += i;
    21         }
    22
    23        printf("result[1-100] = %d /n", result );
    24        printf("result[1-250] = %d /n", func(250) );
    25 }

編譯產生運行檔案:(Linux下)
    hchen/test> cc -g tst.c -o tst

使用GDB調試:

hchen/test> gdb tst  <---------- 啟動GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-suse-linux"...
(gdb) l     <-------------------- l命令相當於list,從第一行開始例出原碼。
1        #include <stdio.h>
2
3        int func(int n)
4        {
5                int sum=0,i;
6                for(i=0; i<n; i++)
7                {
8                        sum+=i;
9                }
10               return sum;
(gdb)       <-------------------- 直接斷行符號表示,反覆上一次命令
11       }
12
13
14       main()
15       {
16               int i;
17               long result = 0;
18               for(i=1; i<=100; i++)
19               {
20                       result += i;   
(gdb) break 16    <-------------------- 設定斷點,在來源程式第16行處。
Breakpoint 1 at 0x8048496: file tst.c, line 16.
(gdb) break func  <-------------------- 設定斷點,在函數func()入口處。
Breakpoint 2 at 0x8048456: file tst.c, line 5.
(gdb) info break  <-------------------- 查看斷點資訊。
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x08048496 in main at tst.c:16
2   breakpoint     keep y   0x08048456 in func at tst.c:5
(gdb) r           <--------------------- 執行程式,run命令簡寫
Starting program: /home/hchen/test/tst

Breakpoint 1, main () at tst.c:17    <---------- 在斷點處停住。
17               long result = 0;
(gdb) n          <--------------------- 單條語句執行,next命令簡寫。
18               for(i=1; i<=100; i++)
(gdb) n
20                       result += i;
(gdb) n
18               for(i=1; i<=100; i++)
(gdb) n
20                       result += i;
(gdb) c          <--------------------- 繼續執行程式,continue命令簡寫。
Continuing.
result[1-100] = 5050       <----------程式輸出。

Breakpoint 2, func (n=250) at tst.c:5
5                int sum=0,i;
(gdb) n
6                for(i=1; i<=n; i++)
(gdb) p i        <--------------------- 列印變數i的值,print命令簡寫。
$1 = 134513808
(gdb) n
8                        sum+=i;
(gdb) n
6                for(i=1; i<=n; i++)
(gdb) p sum
$2 = 1
(gdb) n
8                        sum+=i;
(gdb) p i
$3 = 2
(gdb) n
6                for(i=1; i<=n; i++)
(gdb) p sum
$4 = 3
(gdb) bt        <--------------------- 查看函數堆棧。
#0  func (n=250) at tst.c:5
#1  0x080484e4 in main () at tst.c:24
#2  0x400409ed in __libc_start_main () from /lib/libc.so.6
(gdb) finish    <--------------------- 退出函數。
Run till exit from #0  func (n=250) at tst.c:5
0x080484e4 in main () at tst.c:24
24              printf("result[1-250] = %d /n", func(250) );
Value returned is $6 = 31375
(gdb) c     <--------------------- 繼續執行。
Continuing.
result[1-250] = 31375    <----------程式輸出。

Program exited with code 027. <--------程式退出,調試結束。
(gdb) q     <--------------------- 退出gdb。
hchen/test>

好了,有了以上的感性認識,還是讓我們來系統地認識一下gdb吧。

 


使用GDB
————

一般來說GDB主要調試的是C/C++的程式。要調試C/C++的程式,首先在編譯時間,我們必需要把調試資訊加到可運行檔案裡。使用編譯器(cc/gcc/g++)的 -g 參數能夠做到這一點。如:

    > cc -g hello.c -o hello
    > g++ -g hello.cpp -o hello

假設沒有-g,你將看不見程式的函數名、變數名,所取代的全是執行時的記憶體位址。當你用-g把調試資訊增加之後,並成功編譯目標代碼以後,讓我們來看看怎樣用gdb來調試他。

啟動GDB的方法有下面幾種:

    1、gdb <program>
       program也就是你的運行檔案,一般在當然檔案夾下。

    2、gdb <program> core
       用gdb同一時候調試一個執行程式和core檔案,core是程式非法執行後core dump後產生的檔案。

    3、gdb <program> <PID>
       假設你的程式是一個服務程式,那麼你能夠指定這個服務程式執行時的進程ID。gdb會自己主動attach上去,並調試他。program應該在PATH環境變數中搜尋得到。

 

GDB啟動時,能夠加上一些GDB的啟動開關,具體的開關能夠用gdb -help查看。我在以下僅僅例舉一些比較經常使用的參數:

    -symbols <file>
    -s <file>
    從指定檔案裡讀取符號表。

    -se file
    從指定檔案裡讀取符號表資訊,並把他用在可運行檔案裡。

    -core <file>
    -c <file>
    調試時core dump的core檔案。

    -directory <directory>
    -d <directory>
    增加一個源檔案的搜尋路徑。預設搜尋路徑是環境變數中PATH所定義的路徑。

下一頁->

(著作權全部,轉載時請註明作者和出處) 

用GDB偵錯工具(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.