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)
  • CLI 的命令仍然可以在 MI 模式下使用。

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

腳本

    • Debug Adapters/Interfaces/Dongles are normally configured through commands in an interface configuration file which is sourced by your openocd.cfg file, or through a command line -f interface/….cfg option.
      • JTAG transports expose a chain of one or more Test Access Points (TAPs), each of which must be explicitly declared. JTAG supports both debugging and boundary scan testing. Flash programming support is built on top of debug support.
      • SWD (Serial Wire Debug) is an ARM-specific transport which exposes one Debug Access Point (DAP, which must be explicitly declared. (SWD uses fewer signal wires than JTAG.) SWD is debug-oriented, and does not support boundary scan testing. Flash programming support is built on top of debug support. (Some processors support both JTAG and SWD.)
    • transport 若是選擇 jtag,則必須設置目標平台上欲啟用的 TAP (Test Access Port)。
    • 目標平台上可以有多個 TAP,通常以串聯形式 (daisy chain) 存在,通常又稱 scan chain。必須用 'jtag newtap' 命令依序列舉出目標平台上的 TAP。
      • Note that the order in which TAPs are declared is very important. That declaration order must match the order in the JTAG scan chain, both inside a single chip and between them.

Internal

安裝 GDB

GDBServer

  • gdb/server
  • configure; make
  • main
  • remote_prepare
  • start_event_loop

術語

參考資料

外部連結

登录