目录

gdb-6.0a.tar.bz2 中的 a 所代表的意思請見 Making up for a release mistake in GDB versions 6.0 - 6.6 1)

除錯

$ ulimit -a
core file size          (blocks, -c) 0
$ ulimit -c unlimited

多緒程除錯

(gdb) info threads
(gdb) thread 2
(gdb) thread apply ID1 ID2 command 
(gdb) thread apply all command 
(gdb) set scheduler-locking off|on|step
(gdb) show scheduler-locking 

小技巧

注意! GCC -g 可以調整除錯訊息。

       -glevel
       -ggdblevel
       -gstabslevel
       -gcofflevel
       -gxcofflevel
       -gvmslevel
           Request debugging information and also use level to specify how much information.  The default level is 2.

           Level 0 produces no debug information at all.  Thus, -g0 negates -g.

           Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug.  This
           includes descriptions of functions and external variables, but no information about local variables and no line numbers.

           Level 3 includes extra information, such as all the macro definitions present in the program.  Some debuggers support macro expan-
           sion when you use -g3.

           -gdwarf-2 does not accept a concatenated debug level, because GCC used to support an option -gdwarf that meant to generate debug
           information in version 1 of the DWARF format (which is very different from version 2), and it would have been too confusing.  That
           debug format is long obsolete, but the option cannot be changed now.  Instead use an additional -glevel option to change the debug
           level for DWARF2.
# apropos 可以列出所有與其後字串相關的所有命令
(gdb) apropos pending
# 再使用 help 進行更深入的查詢
(gdb) help show breakpoint pending
# 在 addr 的位址下中斷點,注意加上星號
(gdb) break *addr
# 由目前 pc 位址往後翻成組語
(gdb) display /3i $pc
# 將 addr1 到 addr2 的區段翻成組語
(gdb) disas addr1 addr2
# 印出某函式的位址
(gdb) p helper_printf
$1 = {void ()} 0x5e09fe <helper_printf>
$ gdb
(gdb) file a.out
(gdb) run < input
# 執行 gdb 腳本
$ gdb -x command.gdb
或
$ gdb
(gdb) source command.gdb
# 改變控制流
$ (gdb) set var gdb = 1
# 回傳指定的值
$ (gdb) return value
# 跳過迴圈
$ (gdb) until
# 忽略特定 signal
(gdb) info signals
(gdb) handle SIGUSR2 ignore
# 條件斷點
(gdb) break tb_add_jump
(gdb) condition 1 n == 2
# 
(gdb) break function if n == 5
$ gdb --directory=/local/chenwj/temp/tmp/ ~/install/bin/wine64-preloader
(gdb) show path
(gdb) show environment
# x86_64 上的 eip 叫 rip
# 反組譯目前 pc 位址以後的五條指令
(gdb) x/5i $rip
# 每按一次 enter 都會 display 目前 pc 位址以後的五條指令
(gdb) display/5i $rip
# 查看當前暫存器的內容
(gdb) info registers

MI

MI 主要應用在 IDE 方面。IDE 透過 MI 介面對 GDB 下指令,某些命令反饋上相對於 CLI 會比較快,如 IDE 透過 dump 指令讀取記憶體視窗 (memory windows),CLI 是以 byte 為單位讀取,MI 是以 block 為單位讀取。

$ gdb --interpreter mi a.exe
(gdb)
break main
&"break main\n"
~"Breakpoint 1 at 0x1004010ed: file hello.cpp, line 9.\n"
=breakpoint-created,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x00000001004010ed",func="main()",file="hello.cpp",fullname="/home/wjchen/tmp/hello.cpp",line="9",thread-groups=["i1"],times="0",original-location="main"}
^done
(gdb)

GDB Server

OpenOCD

$ sudo apt-get install libusb-1.0-0-dev libhidapi-dev
$ cat openocd.cfg
source [find interface/cmsis-dap.cfg]
source [find target/stm32f4x.cfg]
$ sudo openocd -f openocd.cfg

腳本

Internal

安裝 GDB

$ configure --build=x86_64-portbld-freebsd8.1 --disable-nls

GDBServer

術語

參考資料

外部連結