輸入gdb進入gdb調試環境。或者直接輸入gdb + progfile來加載文件。
注意該文件是使用gcc(或g++)編譯得到的。爲了使 gdb 正常工作, 必須
使你的程序在編譯時包含調試信息,編譯時必須使用-g參數來。
或者進入gdb環境後,通過命令file + progfile來加載需要調試的可
執行文件文件。
查看源代碼:list [函數名][行數]
設置程序運行參數:set args
二、暫停程序
gdb可以使用幾種方式來暫停程序:斷點,觀察點,捕捉點,信號,線
程停止。當程序被暫停後,可以使用continue、next、step來繼續執行程序。
continue 執行到下一暫停點或程序結束。
next 執行一行源代碼但不進入函數內部。
step 執行一行源代碼而且進入函數內部。
1、設置斷點:
a、break + [源代碼行號][源代碼函數名][內存地址]
b、break ... if condition ...可以是上述任一參數,condition
條件。例如在循環體中可以設置break ... if i = 100 來設置循環次數。
2、設置觀察點:
a、watch + [變量][表達式] 當變量或表達式值改變時即停住程序。
b、rwatch + [變量][表達式] 當變量或表達式被讀時,停住程序。
c、awatch + [變量][表達式] 當變量或表達式被讀或被寫時,停住程序。
3、設置捕捉點:
catch + event 當event發生時,停住程序。event可以是下面的內容:
1)、throw 一個C++拋出的異常。(throw爲關鍵字)
2)、catch 一個C++捕捉到的異常。(catch爲關鍵字)
3)、exec 調用系統調用exec時。(exec爲關鍵字,目前此功能只在HP-UX下有用)
4)、fork 調用系統調用fork時。(fork爲關鍵字,目前此功能只在HP-UX下有用)
5)、vfork 調用系統調用vfork時。(vfork爲關鍵字,目前此功能只在HP-UX下有用)
6)、load 或 load 載入共享庫(動態鏈接庫)時。(load爲關鍵字,
目前此功能只在HP-UX下有用)
7)、unload 或 unload 卸載共享庫(動態鏈接庫)時。(unload爲關
鍵字,目前此功能只在HP-UX下有用)
4、捕捉信號:
handle + [argu] + signals
signals:是Linux/Unix定義的信號,SIGINT表示中斷字符信號,也就是
Ctrl+C的信號,SIGBUS表示硬件故障的信號;SIGCHLD表示子進程狀態改
變信號; SIGKILL表示終止程序運行的信號,等等。
argu:
nostop 當被調試的程序收到信號時,GDB不會停住程序的運行,但
會打出消息告訴你收到這種信號。
stop 當被調試的程序收到信號時,GDB會停住你的程序。
print 當被調試的程序收到信號時,GDB會顯示出一條信息。
noprint 當被調試的程序收到信號時,GDB不會告訴你收到信號的信息。
pass or noignore 當被調試的程序收到信號時,GDB不處理信號。
這表示,GDB會把這個信號交給被調試程序會處理。
nopass or ignore 當被調試的程序收到信號時,GDB不會讓被調
試程序來處理這個信號。
5、線程中斷:
break [linespec] thread [threadno] [if ...]
linespec 斷點設置所在的源代碼的行號。如: test.c:12表示文件爲
test.c中的第12行設置一個斷點。
threadno 線程的ID。是GDB分配的,通過輸入info threads來查看正在
運行中程序的線程信息。
if ... 設置中斷條件。
三、查看信息
1、查看數據
print variable 查看變量
print *array@len 查看數組(array是數組指針,len是需要數據長度)
可以通過添加參數來設置輸出格式:
/x 按十六進制格式顯示變量。
/d 按十進制格式顯示變量。
/u 按十六進制格式顯示無符號整型。
/o 按八進制格式顯示變量。
/t 按二進制格式顯示變量。
/a 按十六進制格式顯示變量。
/c 按字符格式顯示變量。
/f 按浮點數格式顯示變量。
2、查看內存
examine /n f u + 內存地址(指針變量)
n 表示顯示內存長度
f 表示輸出格式(見上)
u 表示字節數制定(b 單字節;h 雙字節;w 四字節;g 八字節;默認爲四字節)
如:
x /10cw pFilePath (pFilePath爲一個字符串指針,指針佔4字節)
x 爲examine命令的簡寫。
3、查看棧信息
backtrace [-n][n]
n 表示只打印棧頂上n層的棧信息。
-n 表示只打印棧底上n層的棧信息。
不加參數,表示打印所有棧信息。
4、info
info address -- Describe where symbol SYM is stored
info all-registers -- List of all registers and their contents
info args -- Argument variables of current stack frame
info auxv -- Display the inferior's auxiliary vector
info breakpoints -- Status of user-settable breakpoints
info catch -- Exceptions that can be caught in the current stack frame
info checkpoints -- IDs of currently known forks/checkpoints
info classes -- All Objective-C classes
info common -- Print out the values contained in a Fortran COMMON block
info copying -- Conditions for redistributing copies of GDB
info dcache -- Print information on the dcache performance
info display -- Expressions to display when program stops
info extensions -- All filename extensions associated with a source language
info files -- Names of targets and files being debugged
info float -- Print the status of the floating point unit
info forks -- IDs of currently known forks/checkpoints
info frame -- All about selected stack frame
info functions -- All function names
info handle -- What debugger does when program gets various signals
info line -- Core addresses of the code for a source line
info linkmap -- Display the inferior's linkmap
info locals -- Local variables of current stack frame
info macro -- Show the definition of MACRO
info mem -- Memory region attributes
info proc -- Show /proc process information about any running process
info program -- Execution status of the program
info registers -- List of integer registers and their contents
info scope -- List the variables local to a scope
info selectors -- All Objective-C selectors
info set -- Show all GDB settings
info sharedlibrary -- Status of loaded shared object libraries
info signals -- What debugger does when program gets various signals
info source -- Information about the current source file
info sources -- Source files in the program
info stack -- Backtrace of the stack
info symbol -- Describe what symbol is at location ADDR
info target -- Names of targets and files being debugged
info terminal -- Print inferior's saved terminal status
info threads -- IDs of currently known threads
info tracepoints -- Status of tracepoints
info types -- All type names
info variables -- All global and static variable names
info vector -- Print the status of the vector unit
info warranty -- Various kinds of warranty you do not have
info watchpoints -- Synonym for ``info breakpoints''
info win -- List of all displayed windows
附註:
基本gdb命令:
---------------------------------------------------------------------
命令 簡寫 功能
---------------------------------------------------------------------
file 裝入想要調試的可執行文件.
kill k 終止正在調試的程序.
list l 列出產生執行文件的源代碼的一部分.
next n 執行一行源代碼但不進入函數內部.
step s 執行一行源代碼而且進入函數內部.
continue c 繼續執行程序,直至下一中斷或者程序結束。
run r 執行當前被調試的程序.
quit q 終止 gdb.
watch 使你能監視一個變量的值而不管它何時被改變.
catch 設置捕捉點.
thread t 查看當前運行程序的線程信息.
break b 在代碼裏設置斷點, 這將使程序執行到這裏時被掛起.
make 使你能不退出 gdb 就可以重新產生可執行文件.
shell 使你能不離開 gdb 就執行 UNIX shell 命令.
print p 打印數據內容。
examine x 打印內存內容。
backtrace bt 查看函數調用棧的所有信息。