gdb-6.0a.tar.bz2 中的 a 所代表的意思請見 [[http://www.cygwin.com/ml/gdb-announce/2011/msg00003.html|Making up for a release mistake in GDB versions 6.0 - 6.6 ]]。[(http://people.cs.nctu.edu.tw/~chenwj/log/GDB/sergiodj-2011-11-15.txt)] * [[https://gist.github.com/skyscribe/3978082|GDB init file to print STL containers and data members]] ====== 除錯 ====== $ 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 * [[go>gdbtui]] * [[http://clewn.sourceforge.net/index.html|Clewn]] * [[http://cgdb.sourceforge.net/|CGDB]] * [[http://www.study-area.org/cyril/opentools/opentools/debug.html|Chapter 6. 除錯工具]] ===== 小技巧 ===== 注意! 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 $ 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 * [[http://elinux.org/GDB]] * [[http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html]] * [[http://stackoverflow.com/questions/2435232/how-to-skip-past-loops-in-ddd-gdb|How to skip past loops in ddd (gdb)]] $ gdb --directory=/local/chenwj/temp/tmp/ ~/install/bin/wine64-preloader * [[http://sourceware.org/gdb/onlinedocs/gdb/Source-Path.html|9.5 Specifying Source Directories]] (gdb) show path (gdb) show environment * [[http://www.faqs.org/docs/ldev/0130091154_177.htm|Displaying Environment Variables]] # x86_64 上的 eip 叫 rip # 反組譯目前 pc 位址以後的五條指令 (gdb) x/5i $rip # 每按一次 enter 都會 display 目前 pc 位址以後的五條指令 (gdb) display/5i $rip # 查看當前暫存器的內容 (gdb) info registers * [[http://sourceware.org/gdb/onlinedocs/gdb/Memory.html|10.6 Examining Memory]] * [[http://sourceware.org/gdb/onlinedocs/gdb/Auto-Display.html|10.7 Automatic Display]] ====== 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 模式下使用。 * [[http://www.ibm.com/developerworks/library/os-eclipse-cdt-debug2/|Interfacing with the CDT debugger, Part 2: Accessing gdb with the Eclipse CDT and MI]] * [[http://blog.csdn.net/coutcin/article/details/1074330|GDB/MI 层命令介绍]] * [[https://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI.html|27 The gdb/mi Interface]] ====== GDB Server ====== * [[wp>gdbserver]] * [[http://sourceware.org/gdb/onlinedocs/gdb/Remote-Debugging.html|20 Debugging Remote Programs]] * [[https://sourceware.org/gdb/onlinedocs/gdb/Connecting.html|20.1 Connecting to a Remote Target]] * 本機端的 gdb 透過串口或 TCP/IP,傳送命令 ([[http://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html|Remote Serial Protocol]]) 給本機或遠端的 gdbserver 或 gdb stub。 * [[http://neptune.billgatliff.com/debugger.html|Implementing a Debugging Agent for the GNU Debugger]] * [[http://www.embecosm.com/appnotes/ean4/embecosm-howto-rsp-server-ean4-issue-2.html|Howto: GDB Remote Serial Protocol]] * [[https://sourceware.org/gdb/onlinedocs/gdb/Server.html|20.3 Using the gdbserver Program]] * gdb 和 gdbserver 必須運行在作業系統之上。 * [[https://sourceware.org/gdb/onlinedocs/gdb/Remote-Stub.html|20.5 Implementing a Remote Stub]] * gdb stub 提供 [[http://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html|Remote Serial Protocol]] 的基本實現,可以視做為輕量級的 gdb server,用於不支援作業系統的目標平台。目標程序需要與 gdb stub 鏈結。 * [[https://sourceware.org/gdb/onlinedocs/gdb/Stub-Contents.html|20.5.1 What the Stub Can Do for You]] * set_debug_traps * 於目標程序開始時調用,設定中斷向量表。當目標程序發生例外時,調用對映的例外處理函式。 * handle_exception * 當目標程序觸發斷點,由此函式處理並與 GDB 交互。其中實現 [[http://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html|Remote Serial Protocol]]。 * breakpoint * 設置斷點。 * [[https://sourceware.org/gdb/onlinedocs/gdb/Bootstrapping.html|20.5.2 What You Must Do for the Stub]] * 提供 gdb stub 串口通信能力: * getDebugChar * putDebugChar * 提供設置中斷向量表的功能供 set_debug_traps 使用。 * exceptionHandler * [[https://sourceware.org/gdb/onlinedocs/gdb/Debug-Session.html|20.5.3 Putting it All Together]] * 目標代碼應該提供 [[https://sourceware.org/gdb/onlinedocs/gdb/Bootstrapping.html|20.5.2 What You Must Do for the Stub]] 描述的接口供 gdb stub 使用,並於目標代碼起始處調用 [[https://sourceware.org/gdb/onlinedocs/gdb/Stub-Contents.html|20.5.1 What the Stub Can Do for You]] 提供的接口。 * 編譯並鏈結目標代碼。 * 連結本機和目標機器。 * 下載目標代碼至目標機器。 * 啟動 GDB 連接至目標機器進行除錯。 * [[http://128.59.11.212/~sedwards/classes/2002/w4995-02/tan-final.pdf|A minimal GDB stub for embedded remote debugging]] * [[http://velep.com/archives/852.html|eCos调试技术2—连接J-Link进行调试(命令行)]] * [[http://www.coocox.org/CoIDE/How_to_use_J-Link.html|How to Use J-Link]] * [[https://www.segger.com/jlink-gdb-server.html|J-Link GDB Server]] * [[https://www.segger.com/admin/uploads/productDocs/UM08005_JLinkGDBServer.pdf|User guide of the J-Link GDB Server]] * 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 應該是[[http://www.embecosm.com/appnotes/ean4/embecosm-howto-rsp-server-ean4-issue-2.html#id3033275|客製化版本]],可以連接到 USB 接口。 * [[http://stackoverflow.com/questions/26921960/how-debugger-works-with-the-probe-in-coide|How debugger works with the probe in CoIDE?]] * [[http://www.coocox.org/forum/topic.php?id=4886|How debugger works in CoIDE?]] * [[http://mcuoneclipse.com/2013/07/22/diy-free-toolchain-for-kinetis-part-3-debugger-gdb-server-with-pe-and-segger/|DIY Free Toolchain for Kinetis: Part 3 – Debugger]] * [[http://gnuarmeclipse.livius.net/wiki/How_to_use_the_J-Link_probe_with_the_GDB_Server|How to use the J-Link probe with the GDB Server]] * [[http://www.atollic.com/index.php/kb/3-debugging/38-kb_remote_gdb_server|Using the ST-LINK device/probe remotely]] * [[https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html|Appendix E GDB Remote Serial Protocol]] * [[https://sourceware.org/gdb/onlinedocs/gdb/General-Query-Packets.html#General-Query-Packets|E.4 General Query Packets]] * Packets starting with ‘q’ are general query packets; packets starting with ‘Q’ are general set packets. ===== OpenOCD ===== * [[http://openocd.sourceforge.net/|OpenOCD]] * The Open On-Chip Debugger (OpenOCD) aims to provide debugging, in-system programming and boundary-scan testing for embedded target devices. * [[http://openocd.sourceforge.net/doc/html/Config-File-Guidelines.html|6 Config File Guidelines]] * interface * ULINK 或 J-Link。支援韌體更新為 CMSIS 的 ULINK2[(http://people.cs.nctu.edu.tw/~chenwj/log/OpenOCD/PaulFertser-2015-01-27.txt)]。 * [[http://developer.mbed.org/handbook/CMSIS-DAP|mbed]] 實現的 CMSIS-DAP 已知可行。 * OpenOCD 需要編譯成支援 CMSIS-DAP。 * board * 目標開發版 * target * 目標平台 CPU * [[http://openocd.sourceforge.net/doc/html/GDB-and-OpenOCD.html|22 GDB and OpenOCD]] * [[http://blog.csdn.net/hcx25909/article/details/8710792|K60(Cortex-M4)开源开发探索(四)—— 交叉编译环境设计]] * [[http://blog.csdn.net/hcx25909/article/details/8711065|K60(Cortex-M4)开源开发探索(五)—— 交叉编译环境构建]] * [[http://blog.csdn.net/hcx25909/article/details/8711186|K60(Cortex-M4)开源开发探索(六)—— 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 * [[http://sourceforge.net/p/openocd/mailman/message/31779185/|[OpenOCD-devel] Problems building openocd for cmsis-dap using HIDAPI]] * [[http://www.freddiechopin.info/en/download|Windows OpenOCD]] * Windows 請安裝此版本。 ==== 腳本 ==== * [[http://openocd.org/doc/html/Config-File-Guidelines.html|6 Config File Guidelines]] * [[http://openocd.org/doc/html/Debug-Adapter-Configuration.html|8 Debug Adapter Configuration]] * 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. * [[http://openocd.org/doc/html/Debug-Adapter-Configuration.html#JTAG-Transport|8.3.1 JTAG Transport]] * 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. * [[http://openocd.org/doc/html/Debug-Adapter-Configuration.html#SWD-Transport|8.3.2 SWD Transport]] * 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.) * [[http://openocd.org/doc/html/TAP-Declaration.html|10 TAP Declaration]] * 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. * [[http://openocd.org/doc/html/TAP-Declaration.html#TAP-Declaration-Commands|10.3 TAP Declaration Commands]] * [[http://openocd.org/doc/html/JTAG-Commands.html|17 JTAG Commands]] * [[http://openocd.org/doc/html/Tcl-Crash-Course.html|24 Tcl Crash Course]] * [[http://openocd.org/doc/html/Tcl-Crash-Course.html|24.6 OpenOCD Tcl Usage]] * 於 lpc4350 出現底下錯誤訊息。有可能是 transport autoselect 與腳本欲執行命令不匹配的問題,即 transport autoselect 為 cmsis-dap ,但欲執行 'jtag newtap' 命令。[(http://people.cs.nctu.edu.tw/~chenwj/log/OpenOCD/Juv1228-2015-05-27.txt)]。 * [[http://sourceforge.net/p/openocd/mailman/message/32442659/|[OpenOCD-devel] jtag newtap changed?]] * [[http://sourceforge.net/p/openocd/mailman/message/32637202/|[OpenOCD-user] jtag newtap "requires more arguments"]] * [[http://sourceforge.net/p/openocd/mailman/message/32474495/|[OpenOCD-devel] Trouble selecting transport.]] ====== Internal ====== * [[https://sourceware.org/gdb/wiki/HomePage|GDB Wiki]] * [[https://sourceware.org/gdb/wiki/InternalsManual|GDB Internals Manual]] ===== 安裝 GDB ===== $ configure --build=x86_64-portbld-freebsd8.1 --disable-nls * [[http://www.gnuarm.com/pdf/gdbint.pdf|gdb Internals - A guide to the internals of the GNU debugger]] * [[http://sourceware.org/ml/gdb/2011-11/msg00012.html|Toward multicore GDB - Overview]] * [[http://www.linuxjournal.com/node/6100/print|Playing with ptrace, Part I]] * [[http://www.linuxjournal.com/node/6210/print|Playing with ptrace, Part II]] * [[http://www.ibm.com/developerworks/cn/linux/l-cn-utrace/index.html?ca=drs-|玩转 utrace]] ===== GDBServer ===== * gdb/server * configure; make * main * remote_prepare * start_event_loop ====== 術語 ====== * [[https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-and-Programs.html|4.9 Debugging Multiple Inferiors and Programs]] * gdb represents the state of each program execution with an object called an inferior. * GDB 內部用來表示調試程序 (debugee) 狀態的物件,稱為 inferior。inferior 有下級或是下屬的意思。 * [[http://stackoverflow.com/questions/16591485/what-does-inferior-mean-in-the-term-inferior-debugger|What does 'inferior' mean in the term 'inferior debugger'?]] * 被作為調試程序,被另一個 GDB 除錯的 GDB,稱為 inferior debugger。 ====== 參考資料 ====== * [[http://tetralet.luna.com.tw/index.php?op=ViewArticle&articleId=187&blogId=1|Linux 除錯利器 - GDB 簡介]] * [[http://osdir.com/ml/gnu.mingw.msys/2005-09/msg00001.html|where/how to get libintl.h]] * [[http://www.aosabook.org/en/gdb.html|GDB]] * [[http://www.embecosm.com/appnotes/ean3/embecosm-howto-gdb-porting-ean3-issue-2.pdf|Howto: Porting the GNU Debugger]] * [[wp>Call stack]] * [[https://gnu.wildebeest.org/blog/mjw/2007/08/23/stack-unwinding/|STACK UNWINDING]] * [[http://blog.reverberate.org/2013/05/deep-wizardry-stack-unwinding.html|Deep Wizardry: Stack Unwinding]] * [[https://blogs.oracle.com/eschrock/entry/debugging_on_amd64_part_one|Debugging on AMD64 - Part One]] * [[https://blogs.oracle.com/eschrock/entry/debugging_on_amd64_part_two|Debugging on AMD64 - Part Two]] * x86-64 ABI 不再要求使用 frame pointer。改用 .eh_frame 或 .debug_frame 段。 * [[http://stackoverflow.com/questions/4349162/how-gdb-reconstructs-stacktrace-for-c|How gdb reconstructs stacktrace for C++?]] * [[http://yosefk.com/blog/getting-the-call-stack-without-a-frame-pointer.html|Getting the call stack without a frame pointer]] * [[http://mcternan.me.uk/ArmStackUnwinding/|ARM Stack Unwinding]] ====== 外部連結 ====== * [[http://www.gnu.org/software/gdb/|GDB: The GNU Project Debugger]] * [[http://sourceware.org/gdb/onlinedocs/gdb.html|Debugging with gdb]] * [[http://sourceware.org/gdb/wiki/|GDB Wiki]] * [[wp>GNU Debugger]] * [[http://sourceware.org/gdb/wiki/ProjectArcher]] * [[http://www.yolinux.com/TUTORIALS/GDB-Commands.html|GNU GDB Debugger Command Cheat Sheet]] * [[http://www.linuxforum.net/forum/gshowflat.php?Cat=&Board=program&Number=692404&page=0&view=collapsed|[精华] GDB 多线程调试基本命令 实现简介 以及一个问题的解决]] * [[http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html|RMS's gdb Debugger Tutorial]] * [[http://dwarfstd.org/|The DWARF Debugging Standard]] * [[http://www.dwarfstd.org/doc/Debugging%20using%20DWARF.pdf|Introduction to the DWARF Debugging Format]] * [[http://stackoverflow.com/questions/16591485/what-does-inferior-mean-in-the-term-inferior-debugger|What does 'inferior' mean in the term 'inferior debugger'?]]