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
-
-
-
- 本機端的 gdb 透過串口或 TCP/IP,傳送命令 (Remote Serial Protocol) 給本機或遠端的 gdbserver 或 gdb stub。
-
- gdb 和 gdbserver 必須運行在作業系統之上。
-
- gdb stub 提供 Remote Serial Protocol 的基本實現,可以視做為輕量級的 gdb server,用於不支援作業系統的目標平台。目標程序需要與 gdb stub 鏈結。
-
- set_debug_traps
- 於目標程序開始時調用,設定中斷向量表。當目標程序發生例外時,調用對映的例外處理函式。
- handle_exception
- 當目標程序觸發斷點,由此函式處理並與 GDB 交互。其中實現 Remote Serial Protocol。
- breakpoint
- 設置斷點。
-
- 提供 gdb stub 串口通信能力:
- getDebugChar
- putDebugChar
- 提供設置中斷向量表的功能供 set_debug_traps 使用。
- exceptionHandler
-
- 目標代碼應該提供 20.5.2 What You Must Do for the Stub 描述的接口供 gdb stub 使用,並於目標代碼起始處調用 20.5.1 What the Stub Can Do for You 提供的接口。
- 編譯並鏈結目標代碼。
- 連結本機和目標機器。
- 下載目標代碼至目標機器。
- 啟動 GDB 連接至目標機器進行除錯。
-
-
-
The GDB and GDB Server communicate via a TCP/IP connection, using the standard GDB remote serial protocol. The GDB supports a standard set of commands like open elf/bin files, reading/writing memory, etc. Beside this, the GDB also supports so called monitor commands which are passed to the GDB Server and interpreted by it, allowing it to implement J-Link specific commands like reading/writing CP15 registers, enabling flash download via J-Link, using Unlimited Flash Breakpoints, enabling semihosting, etc.
- gdb 連上 2009 埠,gdbserver 於 2009 埠監聽; 兩者皆在本機端。gdbserver 透過 J-Link 連上版子。
--------------------------- ------------------ | TCP/IP | | | | gdb <--------> gdbserver | <---> J-Link <----> | debugee | | | USB JTAG | | | (Host PC) | | (Target Board) | --------------------------- ------------------
- CooCox 的 gdbserver 應該是客製化版本,可以連接到 USB 接口。
-
-
-
Packets starting with ‘q’ are general query packets; packets starting with ‘Q’ are general set packets.
-
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
-
- Windows 請安裝此版本。
腳本
-
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.
-
- 於 lpc4350 出現底下錯誤訊息。有可能是 transport autoselect 與腳本欲執行命令不匹配的問題,即 transport autoselect 為 cmsis-dap ,但欲執行 'jtag newtap' 命令。3)。
Internal
安裝 GDB
$ configure --build=x86_64-portbld-freebsd8.1 --disable-nls
GDBServer
- gdb/server
- configure; make
- main
- remote_prepare
- start_event_loop
術語
-
gdb represents the state of each program execution with an object called an inferior.
- GDB 內部用來表示調試程序 (debugee) 狀態的物件,稱為 inferior。inferior 有下級或是下屬的意思。
-
- 被作為調試程序,被另一個 GDB 除錯的 GDB,稱為 inferior debugger。
參考資料
-
-
- x86-64 ABI 不再要求使用 frame pointer。改用 .eh_frame 或 .debug_frame 段。