Try   HackMD

GDB Cheat Sheet Of Commands

tags: GDB Commands Cheat Sheet

Commands [1]

help

help

  • 列出GDB指令分類

help category

  • 列出GDB分類的命令由上一個提供的分類

help command

  • 顯示那個指令的說明

apropos

apropos [-v] search-word

  • 在幫助說明中顯示包含「search-word」指令列表。
  • [-v]: 在完整文檔中顯示包含「search-word」指令列表

run / r

r

  • 啟動debugged程式。

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

  • 設立中斷點在函數「function」

b N

  • 設立中斷點在當前執行來源檔的第N行

b file.c:N

  • 設立中斷點在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

  • 儲存目前中斷點定義作成script

save gdb-index directory

  • 儲存一個gdb-index檔案(symbol file)

save tracepoints file-name.record

  • 儲存目前追蹤點定義作成script

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

  • 開啟檔案,等同於 gdb filename

tbreak

tbreak

  • tbreak類似break,但是它是暫時性中斷點。也就是,一旦它被碰到(hit)時,這個臨時breakpoint將被刪除。

watch

watch condition

  • 為給定條件設立中斷點。當條件為true時,程式將暫停。

delete / d

d N

  • 清除第N個中斷點、監看點、catchpoints

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

  • 呼叫在程式中的函數,類似print

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

  • 刪除在「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

  • 顯示堆疊資訊,詳細資訊link

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

  • 執行程式接下來的N行

next / n

n

  • 類似s,但是不進入函數

p var

  • 印出目前變數「var」的值
註:
  • GDB會產生臨時變數$number。使用$取用最後產生的臨時變數;使用$$來取用上一個產生的臨時變數

p array_name[number0]@numberX

  • 印出陣列從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

  • 移除display命令的內容

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

  • 設定變數「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

  • 釋放已 attach 的程式

list / l

l

  • 印出程式碼,執行到的該行程式碼上面五行和下面四行,包含執行中的該行程式碼共十行程式碼。
  • 透過行數range,以comma分隔印出特定段落程式碼。 e.g., l 5,10

shell [3]

shell

  • 執行 Shell 指令。如:shell ls,呼叫 sh 以執行 ls 指令

whatis

whatis var/function name

  • 印出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

  • 印出堆疊追蹤(stack trace)

dprintf

dprintf location, format string, arg1, arg2

  • 設定在指定地點上(line-no)的動態printf
    e.g., dprintf 20,"Hello, World!\n"

kill

kill

  • 中止程式的執行

quit / q

q

  • 退出gdb

<enter> key

<enter> key

  • 重複上一個指令

GDB 除錯 fork 產生的子行程設定

先找出行程的 pid:
To see every process on the system using standard syntax

ps -ef | grep <行程名>

將 child process 連上目前的行程

gdb -p <pid>
gdb attach <pid>

顯示目前設定

show follow-fork-mode

切換到 child

set follow-fork-mode 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」的指令。

#ifdef DEBUG cout << "something" //只有在Debug時才印出來。 #endif

待辦清單

    • -GDB manual trace用法 link

Reference

[1] GDB Tutorial
[2] GDB 介紹
[3] 除錯程式: gdb

Doc

[1] Debugging with GDB
[2] Jason Note