日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

GDB调试手册

發(fā)布時(shí)間:2025/3/21 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GDB调试手册 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

from http://www.programlife.net/gdb-manual.html


Linux 包含了一個(gè)叫g(shù)db 的GNU 調(diào)試程序。gdb 是一個(gè)用來調(diào)試C和C++程序的強(qiáng)力調(diào)試器。它使你能在程序運(yùn)行時(shí)觀察程序的內(nèi)部結(jié)構(gòu)和內(nèi)存的使用情況。以下是 gdb 所提供的一些功能:它使你能監(jiān)視你程序中變量的值、它使你能設(shè)置斷點(diǎn)以使程序在指定的代碼行上停止執(zhí)行、它使你能一行行的執(zhí)行你的代碼。在命令行上鍵入gdb并按回車鍵就可以運(yùn)行g(shù)db 了。(Windows需要安裝MinGW或者CygWin并且需要配置環(huán)境變量才可以使用)

?

GDB?命令行參數(shù)

啟動?GDB:

l??gdb executable

l??gdb -e executable -c core-file

l??gdb executable -pid process-id

(使用命令?'ps -auxw'?可以查看進(jìn)程的?pid)

選項(xiàng)

含義

–help
-h

列出命令行參數(shù)。

–exec=file
-e?file

指定可執(zhí)行文件。

–core=core-file
-c?core-file

指明?core?文件。

–command=file
-x?file

從指定文件中讀取?gdb?命令。

–directory=directory
-d?directory

把指定目錄加入到源文件搜索路徑中。

–cd=directory

以指定目錄作為當(dāng)前路徑來運(yùn)行?gdb?。

–nx
-n

不要執(zhí)行?.gdbinit?文件中的命令。默認(rèn)情況下,這個(gè)文件中的命令會在所有命令行參數(shù)處理完后被執(zhí)行。

–batch

在非交互模式下運(yùn)行?gdb?。從文件中讀取命令,所以需要?-x?選項(xiàng)。

–symbols=file
-s?file

從指定文件中讀取符號表。

-write

允許對可執(zhí)行文件和?core?文件進(jìn)行寫操作。

–quiet
-q

不要打印介紹和版權(quán)信息。

–tty=device

指定?device?為運(yùn)行程序的標(biāo)準(zhǔn)輸入輸出。

–pid=process-id
-p?process-id

指定要附屬的進(jìn)程?ID?。

?

GDB命令

GDB?中使用的命令:

命令

描述

help

列出?gdb?幫助信息。

help?topic

列出相關(guān)話題中的?gdb?命令。

help?command

列出命令描述信息。

apropos?search-word

搜索相關(guān)的話題。

info args
i args

列出運(yùn)行程序的命令行參數(shù)。

info breakpoints

列出斷點(diǎn)。

info break

列出斷點(diǎn)號。

info break?breakpoint-number

列出指定斷點(diǎn)的信息。

info watchpoints

列出觀察點(diǎn)。

info registers

列出使用的寄存器。

info threads

列出當(dāng)前的線程。

info set

列出可以設(shè)置的選項(xiàng)。

Break and Watch

?

break?funtion
break?line-number

在指定的函數(shù),或者行號處設(shè)置斷點(diǎn)。

break +offset
break -offset

在當(dāng)前停留的地方前面或后面的幾行處設(shè)置斷點(diǎn)。

break?file:func

在指定的file文件中的func處設(shè)置斷點(diǎn)。

break?file:nth

在指定的file文件中的第nth行設(shè)置斷點(diǎn)。

break *address

在指定的地址處設(shè)置斷點(diǎn)。一般在沒有源代碼時(shí)使用。

break?line-number?ifcondition

如果條件滿足,在指定位置設(shè)置斷點(diǎn)。

break?line?threadthread-number

在指定的線程中中斷。使用info threads可以顯示線程號。

tbreak

設(shè)置臨時(shí)的斷點(diǎn)。中斷一次后斷點(diǎn)會被刪除。

watch?condition

當(dāng)條件滿足時(shí)設(shè)置觀察點(diǎn)。

clear
clear?func
clear?nth

清除函數(shù)func處的斷點(diǎn)。
清除第nth行處的斷點(diǎn)。

delete
d

刪除所有的斷點(diǎn)或觀察點(diǎn)。

delete?breakpoint-number
delete?range

刪除指定的斷點(diǎn),觀察點(diǎn)。

disable?breakpoint-number-or-range
enable?breakpoint-number-or-range

不刪除斷點(diǎn),僅僅把它設(shè)置為無效,或有效。
例子:
顯示斷點(diǎn):?info break
設(shè)置無效:?disable 2-9

enable oncebreakpoint-number

設(shè)置指定斷點(diǎn)有效,當(dāng)?shù)竭_(dá)斷點(diǎn)時(shí)置為無效。

enable del?breakpoint-number

設(shè)置指定斷點(diǎn)有效,當(dāng)?shù)竭_(dá)斷點(diǎn)時(shí)刪除它。

finish

繼續(xù)執(zhí)行到函數(shù)結(jié)束。

Line Execution

?

step
s
step?number-of-steps-to-perform

進(jìn)入下一行代碼的執(zhí)行,會進(jìn)入函數(shù)內(nèi)部。

next
n
next?number

執(zhí)行下一行代碼。但不會進(jìn)入函數(shù)內(nèi)部。

until
until?line-number

until?line-number?ifcondition

繼續(xù)運(yùn)行直到到達(dá)指定行號,或者函數(shù),地址等。

return
return?expression

彈出選中的棧幀(stack frame)。如果后面指定參數(shù),則返回表達(dá)式的值。

stepi
si
nexti
ni

執(zhí)行下一條匯編/CPU指令。

info signals
info handle
handle?SIGNAL-NAMEoption

當(dāng)收到信號時(shí)執(zhí)行下列動作:nostop(不要停止程序),stop(停止程序執(zhí)行),print(顯示信號),noprint(不顯示),pass/noignore(允許程序處理信號),nopass/ignore(不讓程序接受信號)

where

顯示當(dāng)前的行號和所處的函數(shù)。

Program Stack

?

backtrace
bt
bt?inner-function-nesting-depth
bt -outer-function-nesting-depth

顯示當(dāng)前堆棧的追蹤,當(dāng)前所在的函數(shù)。

backtrace full

打印所有局部變量的值。

frame?number
f?number

選擇指定的棧幀。

up?number
down?number

向上或向下移動指定個(gè)數(shù)的棧幀。

info frame?addr

描述選中的棧幀。

info args
info all-reg
info locals
info catch

顯示選中棧幀的參數(shù),局部變量,異常處理函數(shù)。all-reg也會列出浮點(diǎn)寄存器。

Source Code

?

list
l
list?line-number
list?function
list -
list?start#,end#
list?filename:function

列出相應(yīng)的源代碼。

set listsize?count
show listsize

設(shè)置list命令打印源代碼時(shí)的行數(shù)。

directory?directory-name
dir?directory-name
show directories

在源代碼路徑前添加指定的目錄。

directory

當(dāng)后面沒有參數(shù)時(shí),清除源代碼目錄。

Examine Variables

?

print?variable
p?variable
p?file::variable
p 'file'::variable

打印指定變量的值。

p *array-var@length

打印arrary-var中的前l(fā)ength項(xiàng)。

p/x?var

以十六進(jìn)制打印整數(shù)變量var。

p/d?var

把變量var當(dāng)作有符號整數(shù)打印。

p/u?var

把變量var作為無符號整數(shù)打印。

p/o?var

把變量var作為八進(jìn)制數(shù)打印。

p/t?var
x/b?address
x/b &variable

以整數(shù)二進(jìn)制的形式打印var變量的值。

p/c?variable

當(dāng)字符打印。

p/f?variable

以浮點(diǎn)數(shù)格式打印變量var。

p/a?variable

打印十六進(jìn)制形式的地址。

x/w?address
x/4b &variable

打印指定的地址,以四字節(jié)一組的方式。

call?expression

類似于print,但不打印?void?。

disassem?addr

對指定地址中的指令進(jìn)行反匯編。

Controlling GDB

?

set?gdb-option?value

設(shè)置?GDB?的選項(xiàng)。

set print array on
set print array off
show print array

以可讀形式打印數(shù)組。默認(rèn)是?off?。

set print array-indexes on
set print array-indexes off
show print array-indexes

打印數(shù)組元素的下標(biāo)。默認(rèn)是?off?。

set print pretty on
set print pretty off
show print pretty

格式化打印?C?結(jié)構(gòu)體的輸出。

set print union on
set print union off
show print union

打印?C?中的聯(lián)合體。默認(rèn)是?on?。

set print demangle on
set print demangle off
show print demangle

控制?C++?中名字的打印。默認(rèn)是?on?。

Working Files

?

info files
info share

列出當(dāng)前的文件,共享庫。

file?file

把file當(dāng)作調(diào)試的程序。如果沒指定參數(shù),丟棄。

core?file

把file當(dāng)作?core?文件。如果沒指定參數(shù),則丟棄。

exec?file

把file當(dāng)作執(zhí)行程序。如果沒指定參數(shù),則丟棄。

symbol?file

從file中讀取符號表。如果沒指定參數(shù),則丟棄。

load?file

動態(tài)鏈入file文件,并讀取它的符號表。

path?directory

把目錄directory加入到搜索可執(zhí)行文件和符號文件的路徑中。

Start and Stop

?

run
r
run?command-line-arguments
run <?infile?>?outfile

從頭開始執(zhí)行程序,也允許進(jìn)行重定向。

continue
c

繼續(xù)執(zhí)行直到下一個(gè)斷點(diǎn)或觀察點(diǎn)。

continue?number

繼續(xù)執(zhí)行,但會忽略當(dāng)前的斷點(diǎn)number次。當(dāng)斷點(diǎn)在循環(huán)中時(shí)非常有用。

kill

停止程序執(zhí)行。

quit
q

退出?GDB?調(diào)試器。

GDB?操作提示

l??在編譯可執(zhí)行文件時(shí)需要給?gcc?加上?"-g"?選項(xiàng),這樣它才會為生成的可執(zhí)行文件加入額外的調(diào)試信息。

l??不要使用編譯器的優(yōu)化選項(xiàng),比如:?"-O","-O2"。因?yàn)榫幾g器會為了優(yōu)化而改變程序流程,那樣不利于調(diào)試。

l??在?GDB?中執(zhí)行?shell?命令可以使用:shell command

l??GDB?命令可以使用?TAB?鍵來補(bǔ)全。按兩次?TAB?鍵可以看到所有可能的匹配。

l??GDB?命令縮寫:例如?info bre?中的?bre?相當(dāng)于?breakpoints。

GDB?在?Emacs?中的操作:

emacs?按鍵

動作

M-x gdb

切換到?gdb?模式。

C-h m

顯示?gdb?模式介紹信息。

M-s

等同于gdb?中的?step?命令。

M-n

等同于gdb?中的?next?命令。

M-i

等同于gdb?中的?stepi?命令。

C-c C-f

等同于gdb?中的?finish?命令。

M-c

等同于gdb?中的?continue?命令。

M-u

等同于gdb?中的?up?命令。

M-d

等同于gdb?中的?down?命令。

總結(jié)

以上是生活随笔為你收集整理的GDB调试手册的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。