手把手教你gdb除錯

2021-09-02 21:38:04 字數 2923 閱讀 1765

gdb使用

啟動gdb除錯的必須步驟

1、編譯生成可執行檔案

gcc -g xx.c -o xx

2、啟動gdb

gdb xx

3、在main函式處設定斷點

break main

4、執行程式

run

選用gdb命令對程式進行後續除錯

[括號裡為快捷鍵]

list(l)             檢視程式

break(b) 函式名    在某函式入口處新增斷點

break(b) 行號         在指定行新增斷點

break(b) 檔名:行號 在指定檔案的指定行新增斷點

info break          檢視所有設定的斷點

delete 斷點編號     刪除斷點

next(n)             單步執行程式(不進入子函式)

step(s)             單步執行程式(進入子函式)

continue(c)         繼續執行程式

print(p) 變數名    檢視指定變數值

set var=value       設定變數的值

quit(q)             退出

例子

例 利用gdb追蹤變數值

#include int main()

chh001@lucky:~$gcc -g a.c -o achh001@lucky:~$gdb agnu gdb (ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1

license gplv3+: gnu gpl version 3 or later 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-linux-gnu".

type "show configuration" for configuration details.

for bug reporting instructions, please see:

.find the gdb manual and other documentation resources online at:

.for help, type "help".

type "apropos word" to search for commands related to "word"...

reading symbols from a...done.

(gdb)b mainbreakpoint 1 at 0x4004da: file a.c, line 5.

(gdb)runstarting program: /home/chh001/a 

breakpoint 1, main () at a.c:5

5        int a=0;

(gdb)p a   ---->列印a$1 = 0      ---->a為0

(gdb)s6        int i=0;

(gdb) s

7        for(i=0;i<5;i++)

(gdb) s

8            a++;

(gdb)p a$2 = 0    --->a為0(gdb) s

7        for(i=0;i<5;i++)

(gdb) p a

$3 = 1

(gdb) s

8            a++;

(gdb) s

7        for(i=0;i<5;i++)

(gdb) s

8            a++;

(gdb)p a

$4 = 2      --->a為2(gdb) s

7        for(i=0;i<5;i++)

(gdb) s

8            a++;

(gdb) s

7        for(i=0;i<5;i++)

(gdb) s

8            a++;

(gdb) s

7        for(i=0;i<5;i++)

(gdb) s

9        if(a<5) 

(gdb) s

12            a*=5;

(gdb) p a

$5 = 5

(gdb) s

13        return 0;

(gdb)p a

$6 = 25      ---->最終結果a為25(gdb)q     ----->退出a debugging session is active.

inferior 1 [process 47418] will be killed.

quit anyway? (y or n) y

手把手教你OA選型

oa選型永遠是oa行業的重要焦點,在選型問題上困擾了很多客戶,雲全oa從這幾個方面教你如何選型。了解研發技術 技術是硬道理。只有過關的技術才會研發出過硬的產品。如果技術不過關,後期將會帶來一系列的問題。同時需要考慮技術的先進性。在現如今社會發展日新月異,今天還遙遙領先的管理模式也許明天就會被淘汰。所...

手把手教你玩轉git

使用以上來寫git命令。mkdir xx 建立乙個空目錄 xx指目錄名 pwd 顯示當前目錄的路徑。git init 把當前的目錄變成可以管理的git倉庫,生成隱藏.git檔案。git add xx 把xx檔案新增到暫存區去。git commit m xx 提交檔案 m 後面的是注釋。git sta...

npm發布 手把手教你

註冊完畢,回到我們的電腦。windows直接cmd到命令列 輸入以下命令,會提示輸入使用者名稱 密碼 郵箱,這些都是註冊時填寫過的。發布之前首先準備 手動建立乙個命名為lib的資料夾,手動建立index.js package.sjon和typing.d.ts文件,建立完成之後就開始下一步,向裡面新增...