GDB調試是應用程序在開發板上運行,然後在PC機上對開發板上得應用程序進行調試,PC機運行GDB,開發板上運行GDBServer。在應用程序調試的時候,pc機上的gdb向開發板上的GDBServer發出命令,而開發板上的gdbserver就會向應用程序發出信號,使應用程序停下來或者完成其他一些工作。
2、安裝GDB和GDBServer(gdb-7.4.tar.bz2 )
linux-arm-low.c: In function `arm_stopped_by_watchpoint': linux-arm-low.c:642: error: `PTRACE_GETSIGINFO' undeclared (first use in this function) linux-arm-low.c:642: error: (Each undeclared identifier is reported only once linux-arm-low.c:642: error: for each function it appears in.)
該錯誤是因為找不到PTRACE_GETSIGINFO宏,導致編譯錯誤。我們到交叉編譯鏈去搜索一下,我們交叉編譯地址為 /work/tools/gcc-3.4.5-glibc-2.3.6
# cd /work/tools/gcc-3.4.5-glibc-2.3.6 # grep "PTRACE_GETSIGINFO" * -nR arm-linux/sys-include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO 0x4202 arm-linux/include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO 0x4202 distributed/arm-linux/sys-include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO 0x4202 distributed/arm-linux/include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO 0x4202
可以看到,在交叉編譯鏈裡面,定義了PTRACE_GETSIGINFO宏為0x4202,頭文件為include<linux/ptrace.h>中。
有兩種解決辦法,可任選其一:
① 在linux-arm-low.c中直接添加宏 #define PTRACE_GETSIGINFO 0x4202
② 在linux-arm-low.c中將#include <sys/ptrace.h> 更改為 #include <linux/ptrace.h>
再次編譯,編譯通過。
#cp gdbserver /work/nfs_root/first_fs/bin
#include <stdio.h> void C(int *p) { *p = 0x12; } void B(int *p) { C(p); } void A(int *p) { B(p); } void A2(int *p) { C(p); } int main(int argc, char **argv) { int a; int *p = NULL; A2(&a); // A2 > C printf("a = 0x%x\n", a); A(p); // A > B > C return 0; }
編譯:
#arm-linux-gcc -g -o test_debug test_debug.c
#gdbserver 192.168.1.10:123 ./test_debug注釋:192.168.1.10:本開發板的ip 123:端口號,自己隨便寫的 ./test_debug:要調試的程序
Process ./test_debug created; pid = 751 Listening on port 2345
/bin/arm-linux-gdb ./test-debug target remote 192.168.183.127:2345
GNU gdb (GDB) 7.4 Copyright (C) 2012 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=i686-pc-linux-gnu --target=arm-linux". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/share/jz2440/test_debug...done. [New LWP 748] warning: `/lib/libc.so.6': Shared library architecture unknown is not compatible with target architecture arm. warning: `/lib/ld-linux.so.2': Shared library architecture unknown is not compatible with target architecture arm. Core was generated by `./test_debug'. Program terminated with signal 11, Segmentation fault. #0 0x000084ac in C (p=0x0) at test_debug.c:6 6 *p = 0x12;4.4、bt:可以顯示調用關系
#0 0x000084ac in C (p=0x0) at test_debug.c:6 #1 0x000084d0 in B (p=0x0) at test_debug.c:12 #2 0x000084f0 in A (p=0x0) at test_debug.c:17 #3 0x00008554 in main (argc=1, argv=0xbeb32eb4) at test_debug.c:34
GDB調試程序用法 http://www.linuxidc.com/Linux/2013-06/86044.htm
GDB+GDBserver無源碼調試Android 動態鏈接庫的技巧 http://www.linuxidc.com/Linux/2013-06/85936.htm
使用hello-gl2建立ndk-GDB環境(有源碼和無源碼調試環境) http://www.linuxidc.com/Linux/2013-06/85935.htm
在Ubuntu上用GDB調試printf源碼 http://www.linuxidc.com/Linux/2013-03/80346.htm
Linux下用GDB調試可加載模塊 http://www.linuxidc.com/Linux/2013-01/77969.htm
強大的C/C++ 程序調試工具GDB http://www.linuxidc.com/Linux/2016-09/135171.htm
Linux GDB調試 詳述 http://www.linuxidc.com/Linux/2016-11/137505.htm
使用GDB命令行調試器調試C/C++程序 http://www.linuxidc.com/Linux/2014-11/109845.htm
GDB調試命令總結 http://www.linuxidc.com/Linux/2016-08/133988.htm
GDB調試工具入門 http://www.linuxidc.com/Linux/2016-09/135168.htm
GDB 的詳細介紹:請點這裡
GDB 的下載地址:請點這裡