GDB簡單講解

2021-10-18 22:03:06 字數 2338 閱讀 4634

gdb是一款除錯工具

(1) 首先檢查gdb是否安裝

gdb -v
如果出現下面內容就是已安裝,否則就沒有

gnu gdb (ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git

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".

(2) 使用命令安裝,例如,ubuntu系統安裝gdb,命令列輸入:sudo apt-get install gdb。

(3)再次檢查是否安裝 gdb -v

test.c的**如下:

#include

#include

intget_sum

(int n)

return sum;

}int

main()

首先使用gcc -g .c檔案 -o 可執行檔名 進行編譯,再使用gdb + 可執行檔名進入gdb環境,進行除錯。

命令如下如:

(1) gcc -g test.c -o test

(2) gdb test

(3) list等gdb命令;

2.出現問題的可能性:

(1)當編譯時,未加 - g 選項,則進入gdb環境中執行命令會出現no symbol table is loaded. use the 「file」 command.提示;

(2)當進入gdb環境時,未加可執行檔名,也會出現no symbol table is loaded. use the 「file」 command.提示;

3.補充說明一下-g選項的作用:

在linux c中gcc編譯器一章有說,-g選項的意義是「生成除錯資訊,該程式可以被偵錯程式除錯」

列出**

list 列出10行**,再次執行列出接下來10行**

(gdb) list

2 #include 3

4 int get_sum(int n)

5 {

6 int sum = 0;

7 for(int i = 0;ilist 5,10 列出第5行到地10行**

(gdb) list 5,10

5 {

6 int sum = 0;

7 for(int i = 0;ilist get_sum 列出get_sum周圍的**

(gdb) list get_sum

1 #include2 #include 3

4 int get_sum(int n)

5 {

6 int sum = 0;

7 for(int i = 0;i你可以自己試試下面兩個命令

list test.c:5,10

list test.c:get_sum

推出gdb

設定斷點

next執行下一步,next會把函式當一步進行操作,但step會追蹤進入函式,一條一條的執行函式裡的語句

清除斷點

(gdb) help list

(gdb) help all
使用gcc -q test.c -o test和gdb test只是裝入程式,並沒有執行,如果要使程式執行,在gdb提示符下輸入run

(gdb) run

GDB簡單教程

本文的內容基本來自 這篇教程,我在使用裡面的示例程式時遇到了 is not a file or directory 的錯誤,因此修改了一下原來到源程式。這個示例程式很簡單,包含兩個類 node和linkedlist。為了方便除錯,我們將這兩個類放到乙個檔案中。首先檢查是否安裝gdb。如果您的系統中有...

簡單GDB除錯

生成可用gdb除錯的可執行程式 g 會保留原始檔中的函式名和變數名 啟動gdbgdb 可執行程式名 set args 給程式傳參檢視程式中的源 當前檔案 l list l 行號 l 函式名 非當前檔案 l 檔名 行號 l 檔名 函式名 檢視當前可顯示的行數 show listsize 預設是顯示10...

gdb除錯講解,快速入門

使用gdb步驟 啟動程式的方法 gdb 執行程式 一般在當前目錄下,如果不是,要指定路徑 gdb 執行程式 core檔案。通過這個方法可以載入異常出錯時的堆疊資訊 gdb 執行程式 程序id。通過這種方法可以對你正在執行的程式進行除錯。如 設定執行時引數,有兩種方法 在gdb 執行程式 執行引數 啟...