GDB(GNU Project Debugger)是在Linux環(huán)境下功能全面的調(diào)試工具。它提供了一系列強(qiáng)大的調(diào)試功能,包括設(shè)置斷點(diǎn)、單步執(zhí)行、打印與觀察變量、查看寄存器及堆棧信息等。在Linux軟件開發(fā)的實踐中,GDB被視為調(diào)試C及C++程序的核心工具,廣泛用于幫助開發(fā)者定位和解決程序中的問題。
?
為避免符號解析錯誤或架構(gòu)不兼容問題,用于遠(yuǎn)程調(diào)試的GDB應(yīng)在版本上與開發(fā)板的工具鏈對齊。
elf@ubuntu:~/work/EDU/sdk/ELF2-linux-source$?./build.sh bconfig
?
路徑:
?Toolchain ? ??

?
選擇GDB版本,這里選擇gdb12.x ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

?
elf@ubuntu:~/work/EDU/sdk/ELF2-linux-source$?./build.sh bconfig
?
路徑:
?Target packages ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??Debugging, profiling and benchmark

?
elf@ubuntu:~/work/EDU/sdk/ELF2-linux-source$?./build.sh buildroot
這樣就可以將軟件包直接編譯到文件系統(tǒng)的鏡像中了。
?
elf@ubuntu:~/work$?vi gdbdemo.c
?
例程代碼如下:
#include?int?main() {? ??int?a =?10;? ??int?b =?20;? ??int?sum = a + b;? ??printf("a =?%d, b =?%d\n", a, b);? ??printf("sum =?%d\n", sum);? ??for?(int?i =?0; i 5; i++) {? ? ? ??printf("Loop i =?%d\n", i);? ? }? ??return?0;}
?
添加交叉編譯器路徑,進(jìn)行交叉編譯,編譯要調(diào)試的應(yīng)用程序:必須要加-g選項
elf@ubuntu:~/work$?export?PATH=/home/elf/aarch64-buildroot-linux-gnu_sdk-buildroot/bin/:$PATHelf@ubuntu:~/work$ aarch64-linux-gcc -g gdbdemo.c -o gdbdemo
?
將編譯生成的gdbdemo通過U盤拷貝到開發(fā)板上,比如/home/elf路徑下,下述以U盤為例,拷貝到開發(fā)板。
root@elf2-buildroot:~#?cp?/mnt/udisk/gdbdemo /home/elf
?
root@elf2-buildroot:~# gdbserver?172.20.8.7:2345?/home/elf/gdbdemo?Process?/home/elf/gdbdemo created; pid =?1314Listening?on?port?2345
?
GDB工具所在路徑:
ELF2-linux-source/buildroot/output/elf2_fs/host/bin/aarch64-linux-gdb
elf:~/work$?./EDU/sdk/ELF2-linux-source/buildroot/output/elf2_fs/host/bin/aarch64-linux-gdb gdbdemoGNU?gdb (GDB)?12.1Copyright?(C)?2022?Free?Software?Foundation,?Inc.License?GPLv3+:?GNU?GPL?version?3?or later?<http://gnu.org/licenses/gpl.html>This?is?free software: you are free to change and redistribute it.There?is?NO?WARRANTY, to the extent permitted by law.Type?"show copying"?and?"show warranty"?for?details.This?GDB?was configured?as?"--host=x86_64-pc-linux-gnu --target=aarch64-buildroot-linux-gnu".Type?"show configuration"?for?configuration details.For?bug reporting instructions, please see:<https://www.gnu.org/software/gdb/bugs/>.Find?the?GDB?manual and other documentation resources online at:? ??<http://www.gnu.org/software/gdb/documentation/>.For?help, type?"help".Type?"apropos word"?to search?for?commands related to?"word"...Reading?symbols from gdbdemo...(gdb) target remote?172.20.8.7:2345? ?//連接開發(fā)板Remote?debugging using?172.20.8.7:2345Reading?/lib/ld-linux-aarch64.so.1?from remote target...warning:?File?transfers from remote targets can be slow.?Use?"set sysroot"?to access files locally instead.Reading?/lib/ld-linux-aarch64.so.1?from remote target...Reading?symbols from target:/lib/ld-linux-aarch64.so.1...(No?debugging symbols found?in?target:/lib/ld-linux-aarch64.so.1)Reading?/home/elf/work/EDU/sdk/ELF2-linux-source/buildroot/output/elf2_fs/host/lib/debug/.build-id/01/bd8db25550e790a84285a6377baa031748d93c.debug from remote target...0x0000007ff7ff1900?in?_start () from target:/lib/ld-linux-aarch64.so.1(gdb)?
?
此時就可以根據(jù)需求進(jìn)行調(diào)試了,下面是幾個常用的命令
(1)l:列出所有源代碼
(2)b:設(shè)置斷點(diǎn)
(3)c:運(yùn)行到斷點(diǎn)處
(4)s:單步運(yùn)行執(zhí)行
(5)n:單步執(zhí)行,但是step會進(jìn)入函數(shù)里面,但是next不會
(6)p a:打印a這個變量的值
(7)q:退出,輸入此命令則開發(fā)板上的gdbserver也退出
?
下面以具體示例介紹參數(shù)的使用方法。
(gdb) l ??//列出源代碼1?23int?main()?{4? ??int?a =?10;5? ??int?b =?20;6? ??int?sum = a + b;7? ??8? ? printf("a = %d, b = %d\n", a, b);9? ? printf("sum = %d\n", sum);10? ??(gdb) b?9??//在第9行設(shè)置斷點(diǎn)Breakpoint?1?at?0x55555557d0:?file?gdbdemo.c, line?9.(gdb) c ?// 繼續(xù)執(zhí)行程序,直到遇到斷點(diǎn),此時終端打印a = 10, b = 20Continuing.Reading /lib/libc.so.6?from?remote target...Breakpoint?1, main () at gdbdemo.c:99? ? printf("sum = %d\n", sum);(gdb) c ?//從當(dāng)前斷點(diǎn)繼續(xù)執(zhí)行到程序結(jié)束。Continuing.[](gdb)

