生成調(diào)試信息
一般地,只需要添加-g編譯器參數(shù)即可,如g++ -g -std=c++17 main.cpp -o prog
| 參數(shù) | 說明 |
|---|---|
-g |
Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF). |
-ggdb |
Produce debugging information for use by GDB. |
-g1 |
Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug. |
-g2 |
The default level is 2 |
-g3 |
Level 3 includes extra information, such as all the macro definitions present in the program. |
GDB TUI (Text User Interface)
命令:gdb --tui
也可以使用gdb --tui --slient減少不必要的信息
關(guān)鍵命令
| 命令 | 短命令 | 說明 |
|---|---|---|
start |
啟動程序并在main函數(shù)暫停 |
|
run |
r |
啟動程序 |
next |
n |
下一步 |
step |
s |
進(jìn)入函數(shù) |
up |
跳出函數(shù) | |
break |
br or b
|
斷點(diǎn),可以指定行號br n或者函數(shù)名 br fun
|
continue |
c or fg
|
繼續(xù)運(yùn)行直到觸發(fā)斷點(diǎn) |
info locals |
查看本地變量 | |
print |
p |
打印變量print var
|
watch |
監(jiān)控變量值的變化watch var
|
|
backtrace or where
|
bt |
打印調(diào)用棧 |
layout |
切換源碼視圖layout src或者匯編視圖layout asm
|
附加到進(jìn)程
步驟:
- 通過
ps -elf | grep program_name或ps aux | grep program_name獲得進(jìn)程PID - 使用命令
sudo gdb attach pidorgdb --pid=pidorgdb -p pid
回退
在程序運(yùn)行后使用target record-full
需要時(shí)使用命令reverse-next和 reverse-step 回退