GDB Cheat Sheet Of Commands
Commands [1]
help
help
help category
help command
apropos
apropos [-v] search-word
- 在幫助說明中顯示包含「search-word」指令列表。
- [-v]: 在完整文檔中顯示包含「search-word」指令列表
run / r
r
You may specify arguments to give it. Args may include "*", or "[…]"; they are expanded using the shell that will start the program (specified by the "$SHELL" environment variable). Input and output redirection with ">", "<", or ">>" are also allowed.
With no arguments, uses arguments last specified (with "run" or "set args"). To cancel previous arguments and run with no arguments, use "set args" without arguments.
To start the inferior without using a shell, use "set startup-with-shell off".
break / b
b fuction
b N
b file.c:N
b ClassName::funcName
- 設立中斷點在「ClassName::funcName」
b +line-offset
- 設立中斷點到目前執行到行數加上位移量的行數 e.g. b +3 //current line no = 26. then set b 29
b -line-offset
b *address
b line-no if condition
- 目前檔案的行數去設立條件中斷點,只有當條件為true時,執行的程式會暫停。
b line thread thread-no
save
save breakpoints file-name.record
save gdb-index directory
- 儲存一個gdb-index檔案(symbol file)
save tracepoints file-name.record
commands [3]
commands breakpoint-num
- 指定在遇到特定中斷點時所要執行的指令,以end當作輸入完成。
source [-s][-v] FILE
source file-name.record
- 載入之前儲存的script
- [-s] : 在來源搜尋路徑尋找script,即便FILE包含資料夾(path)。
- [-v] : 當FILE被執行時,在FILE中每條命令都會被echoed(顯示出來)。
trace
trace [arg]
- 基本上設立方式類似break,兩者差別可以看link
- 更深入用法link =>待了解…
file [3]
file
tbreak
tbreak
- tbreak類似break,但是它是暫時性中斷點。也就是,一旦它被碰到(hit)時,這個臨時breakpoint將被刪除。
watch
watch condition
- 為給定條件設立中斷點。當條件為true時,程式將暫停。
delete / d
d N
d range
- 以編號範圍清除中斷點、監看點、catchpoints
disable
disable breakpoint-no
- 禁用由數字指定的中斷點、監看點、catchpoints
disable range
- 禁用以編號範圍指定的中斷點、監看點、catchpoints
enable
enable breakpoint-no
- 啟用由數字指定的中斷點、監看點、catchpoints
enable range
- 啟用以編號範圍指定的中斷點、監看點、catchpoints
enable breakpoint-no once
- 啟用給定的中斷點一次,之後被碰到(hit)就禁用它。
call
call function, args…
The argument is the function name and arguments, in the notation of the
current working language. The result is printed and saved in the value history, if it is not void.
frame
frame
- 顯示正在執行的行數、副程式名稱、及其所傳送的參數等等 frame 資訊。[3]
- frame 2:看到 #2,也就是上上一層的 frame 的資訊。[3]
up [3]
up
- 直接回到上一層的 frame,並顯示其 stack 資訊,如進入點及傳入的參數等。
up 2
- 直接回到上三層的 frame,並顯示其 stack 資訊。
down [3]
down
- 直接跳到下一層的 frame,並顯示其 stack 資訊。
clear
clear function
clear line-no
info / i
i breakpoints/break/b
i args
i break/b breakpoint-number
- 顯示帶有「breakpoint-number」的中斷點(內容)說明
i watchpoints
i registers/r
i threads
i all-reg [3]
i frame
註: frame and stack[3]
- 在GDB中為了方便除錯,它將程式碼以副程式為單位區分成一個一個的區塊(frame),比如main()是一個區塊,自己所寫的function也是一個區塊,而正在執行的區塊稱作frame 0,呼叫此區塊的就是frame 1,以此類推。在進入另一個frame之前,會先將目前所在的frame變數值、函數名稱之類的儲存在stack
i stack
i locals
i set
i line file[dot]xxx:line-no
i line *$pc
- 列出該行程式碼或program counter對應到的組合語言
continue / c
c
finish / f
f
step / s
s
s N
next / n
n
print / p
p var
註:
- GDB會產生臨時變數$number。使用$取用最後產生的臨時變數;使用$$來取用上一個產生的臨時變數
- 印出陣列從array_name[number0]到array_name[numberX]的值
- 可以在print後面加slash(/)以及字母
- 格式字母(format letters):
- d:十進位
- o:八進位
- f:浮點數
- u:無符號
- x:十六進位
- t:二進位
i:指令 (meaningless)
- s:字串
- c:字元
- z:十六進位,在左側填充0
display [3]
display var
- 指定每次程式停止時要顯示的變數內容
- 可以在display後面加slash(/)以及字母
- 格式字母(format letters):
- d:十進位
- o:八進位
- f:浮點數
- u:無符號
- x:十六進位
- t:二進位
- i:指令
- s:字串
- c:字元
- z:十六進位,在左側填充0
undisplay
undisplay var
x
x /FMT ADDRESS
-
ADDRESS是用來檢查記憶體位置的表示式 (e.g., x/d 0xbffff2ef)。
-
FMT由三個部分所組成: repeat count(不敲出來的話,預設為1)、format letter、size letter。
-
重複數量(repeat count):根據格式(由後兩者組成的),印出指定大小的指定數量的物件,如果數量是負的,檢查記憶體是從address向後(變小)的方式去做(e.g., x /-1db 0xff4
會輸出記憶體位置 0xff3
:十進位的值。)
-
格式字母(format letters):
- d:十進位
- o:八進位
- f:浮點數
- u:無符號
- x:十六進位
- t:二進位
- i:指令
- s:字串
- c:字元
- z:十六進位,在左側填充0
-
長度字母(size letter):
- b(byte):單位元組
- h(halfword):雙位元組
- w(word):四位元組
- g(giant):八位元組
set
set var=val
set args
set print pretty
unset [3]
unset
- 取消特定參數。如:unset env,刪除環境變數
show [3]
show
- 顯示特定參數。如:show environment,顯示環境變數
attach [3]
attach PID
- 載入已執行中的程式以進行除錯。其中的 PID 可由 ps 指令取得
detach [3]
detach PID
list / l
l
- 印出程式碼,執行到的該行程式碼上面五行和下面四行,包含執行中的該行程式碼共十行程式碼。
- 透過行數range,以comma分隔印出特定段落程式碼。 e.g., l 5,10
shell [3]
shell
- 執行 Shell 指令。如:shell ls,呼叫 sh 以執行 ls 指令
whatis
whatis var/function name
where [4]
where
until
until line-no
Execute until past the current line or past a LOCATION.
Execute until the program reaches a source line greater than the current or a specified location (same args as break command) within the current frame.
backtrace / bt
bt
dprintf
- 設定在指定地點上(line-no)的動態printf
e.g., dprintf 20,"Hello, World!\n"
kill
kill
quit / q
q
<enter> key
<enter> key
GDB 除錯 fork 產生的子行程設定
先找出行程的 pid:
To see every process on the system using standard syntax
將 child process 連上目前的行程
顯示目前設定
切換到 child
non-GDB commands
非GDB指令,但是與除錯「完」相關
strip [3]
strip file(binary file)
- 從可執行的二進位程式和目的檔,移除不必要資訊,像是the line number information, relocation information, the debug section, the typchk section, the comment section, file headers, and all or part of the symbol table from the XCOFF object files –> 簡單來說,就是使執行檔size變小
條件編譯
使用條件編譯如#ifdef幫助debug,不過需要在compile時下「-DDEBUG」的指令。
待辦清單
-
Reference
[1] GDB Tutorial
[2] GDB 介紹
[3] 除錯程式: gdb
Doc
[1] Debugging with GDB
[2] Jason Note