gdb常用命令及参考文档
gdb常用命令及參考文檔
- 前言
- 使能方式
- 啟動(dòng)方式
- 常用命令
- !
- help
- proc
- l/list
- b、break + line 或者函數(shù)名
- break-if
- delete、 clear
- info
- r/run
- n
- s/step
- p/print
- 打印數(shù)組
- set
- finish
- c/continue
- kill
- file
- watch
- start
- display/disp
- catch
- x
- 技巧
- backtrace/bt
- frame/f
- args
- 信號(hào)調(diào)試
- 多線程調(diào)試
- attach
- coredump 文件
- 開始調(diào)試core文件
- 使用gdb dump內(nèi)存
- gdb讀取內(nèi)存
- 使用gdb打印棧幀
- 參考鏈接
- 調(diào)試心得
前言
本文的目的只是為了列述gdb常用命令,供日常調(diào)試時(shí)自查
使能方式
編譯的時(shí)候加上-g 去掉-s選項(xiàng)
-s strip - Discard symbols from object files.
-fvisibility=hidden 這個(gè)也不能加
-g gdb 編譯 -g3 3只是級(jí)別。這個(gè)級(jí)別會(huì)產(chǎn)生更多的額外debug信息。3這個(gè)級(jí)別可以調(diào)試宏。
啟動(dòng)方式
gdb -xxx # 如果想要增加更多啟動(dòng)參數(shù) gdb --args ./SSDTEST.exe -slot 1 -ns 1 -sn 1 #后面加上所有的參數(shù)常用命令
!
在gdb內(nèi)部使用!開頭的命令,可以直接調(diào)用外部命令,如
(gdb) ! grep heap /proc/1131/maps 00c7d000-00d94000 rw-p 00000000 00:00 0 [heap]help
幫助信息
proc
顯示/proc中的一些信息
(gdb) help info proc Show /proc process information about any running process. Specify any process id, or use the program being debugged by default.List of info proc subcommands:info proc all -- List all available /proc info info proc cmdline -- List command line arguments of the process info proc cwd -- List current working directory of the process info proc exe -- List absolute filename for executable of the process info proc mappings -- List of mapped memory regions info proc stat -- List process info from /proc/PID/stat info proc status -- List process info from /proc/PID/statusl/list
list 顯示多行源代碼
b、break + line 或者函數(shù)名
加斷點(diǎn)
同理也可以對(duì)多個(gè)文件中的某一個(gè)文件的函數(shù)打斷點(diǎn),例: b gdb_test.c:func
break-if
利用break if命令,可以設(shè)置一個(gè)條件斷點(diǎn),如下
break line-or-function if expr例:
break 46 if testsize==100delete、 clear
如果我們想刪除某個(gè)斷點(diǎn),有兩種方法:
- delete break 刪除所有的斷點(diǎn)
- delete break n 刪除某個(gè)斷點(diǎn) n為斷點(diǎn)號(hào)
- clear 行號(hào) 刪除設(shè)在某一行的斷點(diǎn)
info
info、delete、clear 后面可以加很多東西, 包括,break, watch, display
| info break/breakpoint | 查看斷點(diǎn) |
| info break/breakpoint N | 查看第N個(gè)斷點(diǎn) |
| info local | 或locals 查看當(dāng)前stack frame局部變量 |
| info variables | 查看全局和靜態(tài)變量 |
| info args | 查看當(dāng)前stack frame參數(shù) |
| info thread | 查看所有的thread |
r/run
開始
n
單步執(zhí)行,但是不會(huì)進(jìn)入函數(shù)內(nèi)部
s/step
單步執(zhí)行,會(huì)進(jìn)入函數(shù)內(nèi)部
p/print
print查看當(dāng)前某個(gè)變量 p空格后面加變量名
打印數(shù)組
可以用以下各式打印數(shù)組,頭指針頭加@數(shù)量
p *arrayPtr@256set
使用set或者print可以修改變量的值
print array[1] = 12 set variable array[1] = 12finish
跳出某個(gè)函數(shù)
c/continue
繼續(xù)運(yùn)行
kill
退出調(diào)試
file
裝入需要調(diào)試的exe file
watch
監(jiān)視變量
start
開始執(zhí)行程序,在main函數(shù)的第一條語句前面停下來
display/disp
跟蹤查看某個(gè)變量, 每次停下來都現(xiàn)實(shí)哦
catch
捕捉
catch assert -- Catch failed Ada assertions catch catch -- Catch an exception ####重要 catch exception -- Catch Ada exceptions catch exec -- Catch calls to exec catch fork -- Catch calls to fork catch load -- Catch loads of shared libraries catch signal -- Catch signals by their names and/or numbers catch syscall -- Catch system calls by their names and/or numbers catch throw -- Catch an exception catch unload -- Catch unloads of shared libraries catch vfork -- Catch calls to vforkx
查看內(nèi)存
格式: x /nfu <addr>
說明:
x 是 examine 的縮寫
n表示要顯示的內(nèi)存單元的個(gè)數(shù)
f表示顯示方式, 可取如下值
x 按十六進(jìn)制格式顯示變量。
d 按十進(jìn)制格式顯示變量。
u 按十進(jìn)制格式顯示無符號(hào)整型。
o 按八進(jìn)制格式顯示變量。
t 按二進(jìn)制格式顯示變量。
a 按十六進(jìn)制格式顯示變量。
i 指令地址格式
c 按字符格式顯示變量。
f 按浮點(diǎn)數(shù)格式顯示變量。
u表示一個(gè)地址單元的長(zhǎng)度
b表示單字節(jié),
h表示雙字節(jié),
w表示四字節(jié),
g表示八字節(jié)
舉例
x/3uh buf技巧
可以使用如下命令,把一個(gè)內(nèi)存地址轉(zhuǎn)化為一個(gè)指針并打印
p *(struct virtio_net*)0x20007ffefa00backtrace/bt
查看函數(shù)調(diào)用信息(堆棧)
frame/f
查看站幀(目前運(yùn)行到的地方)
args
啟動(dòng)程序的三種方式
信號(hào)調(diào)試
handle <signal> <keywords...>- 在GDB中定義一個(gè)信號(hào)處理。信號(hào)可以以SIG開頭或不以SIG開頭,可以用定義一個(gè)要處理信號(hào)的范圍(如:SIGIO- SIGKILL,表示處理從SIGIO信號(hào)到SIGKILL的信號(hào),其中包括SIGIO, SIGIOT,SIGKILL三個(gè)信號(hào)),也可以使用關(guān)鍵字 all來標(biāo)明要處理所有的信號(hào)。一旦被調(diào)試的程序接收到信號(hào),運(yùn)行程序馬上會(huì)被GDB停住,以供調(diào)試。其可以是以下幾種關(guān)鍵字的一個(gè)或多個(gè)。
| nostop | 當(dāng)被調(diào)試的程序收到信號(hào)時(shí),GDB不會(huì)停住程序的運(yùn)行,但會(huì)打出消息告訴你收到這種信號(hào)。 |
| stop | 當(dāng)被調(diào)試的程序收到信號(hào)時(shí),GDB會(huì)停住你的程序。 |
| 當(dāng)被調(diào)試的程序收到信號(hào)時(shí),GDB會(huì)顯示出一條信息。 | |
| noprint | 當(dāng)被調(diào)試的程序收到信號(hào)時(shí),GDB不會(huì)告訴你收到信號(hào)的信息。 |
| pass/noignore | 當(dāng)被調(diào)試的程序收到信號(hào)時(shí),GDB不處理信號(hào)。這表示,GDB會(huì)把這個(gè)信號(hào)交給被調(diào)試程序會(huì)處理。 |
| nopass/ignore | 當(dāng)被調(diào)試的程序收到信號(hào)時(shí),GDB不會(huì)讓被調(diào)試程序來處理這個(gè)信號(hào)。 |
多線程調(diào)試
attach
- 常規(guī)的通過gdb cmd這種方式開啟調(diào)試,特別說明的是通過attach的方法附加到一個(gè)指定的進(jìn)程上去進(jìn)行調(diào)試,這種方法適合于調(diào)試一個(gè)已經(jīng)運(yùn)行的進(jìn)程,具體用法:gdb -p [pid]
- 此時(shí)被attach的進(jìn)程會(huì)阻塞,進(jìn)入T模式(ps 命令看到STATE為T),如果調(diào)試完畢了,使用 detach 命令就釋放了進(jìn)程,它就自由運(yùn)行了。
coredump 文件
- 開啟coredump
- 設(shè)置coredump的命名規(guī)則
- 通過cat /proc/sys/kernel/core_pattern 驗(yàn)證設(shè)置的pattern
開始調(diào)試core文件
調(diào)試的話輸入: gdb filename core
filename就是產(chǎn)生core文件的可執(zhí)行文件,core就是產(chǎn)生的dump文件
使用gdb dump內(nèi)存
然后可以使用如[xxd](https://man.cx/ xxd(1))查看內(nèi)存
gdb讀取內(nèi)存
– (gdb) define xxd dump binary memory dump.bin $arg0 $arg0+$arg1 shell xxd dump.bin shell rm -f dump.bin end (gdb) xxd 0x00007f3498000000 32 0000000: 2000 001c 367f 0000 0000 00a4 347f 0000 ...6.......4... 0000010: 0000 0004 0000 0000 0000 0004 0000 0000 ................使用gdb打印棧幀
gdb中輸入
set logging file my_back_trace.txt thread apply all bt full quit或者把上述命令放到一個(gè)文件gdb-instructions.txt中并執(zhí)行命令
echo -ne "set logging file my_back_trace.txt\nthread apply all bt full\nquit" > gdb-instructions.txt gdb /exe $(pidof exe) -x gdb-instructions.txt參考鏈接
調(diào)試心得
總結(jié)
以上是生活随笔為你收集整理的gdb常用命令及参考文档的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云存储快照技术
- 下一篇: raft引入no-op解决了什么问题