在嵌入式设备中快速体验gdb调试 一、参考资料gdbgdbserver远程调试 - lsgxeva - 博客园嵌入式 程序调试之gdb和gdbserver的交叉编译及使用-CSDN博客二、快速体验gdb示例一test.c测试代码#includestdio.h#includestring.hintmain(){char*p;memcpy(p,hello,5);return0;}编译源码arm-linux-gnueabihf-gcc test.c-otest-g用gdb调试代码时必须加上-g选项如果没有-g你将看不见程序的函数名、变量名。启动gdbserver将编译好的程序拷贝到目标机中并在目标机上启动gdbservergdbserver192.168.10.90:1234 ./test连接调试在宿主机上启动gdbarm-linux-gnueabihf-gdb ./test连接gdbserver(gdb)target remote192.168.10.90:1234连接成功gdb调试开始执行调试(gdb)c Continuing. Reading /lib64/libc.so.6 from remote target... Program received signal SIGSEGV, Segmentation fault. 0x0000007ff7ed40ccinmemcpy()from target:/lib64/libc.so.6查看堆栈信息简易程序(gdb)bt#0 0x0000007ff7ed40cc in memcpy () from target:/lib64/libc.so.6#1 0x00000000004005d4 in main () at test.c:8查看堆栈信息复杂程序(gdb)where#0 0x0000007ff7ed40cc in memcpy () from target:/lib64/libc.so.6#1 0x00000000004005d4 in main () at test.c:8抓取coredump文件设置 ulimitLinux系统默认coredump文件的大小限制为0即产生segmentation-fault段错误时不会生成coredump文件可以通过ulimit -c指令来查看系统限制。可以通过ulimit -c filesize命令来修改系统对coredump文件大小的限制但如果coredump文件超过限制大小filesize的单位为kbyte将会被裁剪最终生成一个不完整的coredump文件。在调试此core文件的时候gdb会提示错误。所以一般情况下不会限制core文件的大小。# 查看coredump文件大小ulimit-c# 修改coredump文件大小ulimit-c# 不限制coredump文件大小ulimit-cunlimited生成core文件当运行程序出现segmentation-fault段错误时默认ulimit0不产生core文件输出示例~# ./testSegmentation fault当运行程序出现segmentation-fault段错误时不限制core大小则生成core文件输出示例~# ulimit -c unlimited~# ulimit -cunlimited ~# ./testSegmentation fault(core dumped)调试core文件core文件产生之后将其拷贝到宿主机不需要连接目标机便可调试。运行下列指令启动gdbyoyoyoyo:/media/sda3/share/cache$ arm-linux-gnueabihf-gdbtest./core GNU gdb(GDB)10.1Copyright(C)2020Free Software Foundation, Inc. License GPLv3: GNU GPL version3or laterhttp://gnu.org/licenses/gpl.htmlThis isfreesoftware: you arefreeto change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Typeshow copyingandshow warrantyfordetails. This GDB was configured asarm-linux-gnueabihf.Typeshow configurationforconfiguration 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,typehelp.Typeapropos wordto searchforcommands related toword... Reading symbols from test... warning: Cant open file /root/test during file-backed mapping note processing [New LWP 8823] Core was generated by ./test.Program terminated with signal SIGSEGV, Segmentation fault.#0 0x0000007fa197c0cc in memcpy () from /lib64/libc.so.6示例二demo.c测试代码#includestdio.h#includeunistd.hintmain(intargc,char*argv[]){inti;printf(Hello gdb\n);for(i0;i5;i){printf(i%d\n,i);}while(i10){sleep(1);}printf(I am exit\n);return0;}编译源码arm-linux-gnueabihf-gcc demo.c-odemo-g启动gdbserver将编译好的程序拷贝到目标机中并在目标机上启动gdbservergdbserver192.168.10.90:1234 ./demo连接调试在宿主机上启动gdbarm-linux-gnueabihf-gdb ./demo连接gdbserver(gdb)target remote192.168.10.90:1234gdb调试# 打断点(gdb)b main# 开始执行程序(gdb)c# 单步执行(gdb)n# 修改变量值(gdb)setvari10宿主机输出目标机输出三、FAQQwarning: Cant open file /lib64/libc-2.29.so during file-backed mapping note processingyoyoyoyo:/media/sda3/share/cache$ arm-linux-gnueabihf-gdbtestcore GNU gdb(GDB)10.1Copyright(C)2020Free Software Foundation, Inc. License GPLv3: GNU GPL version3or laterhttp://gnu.org/licenses/gpl.htmlThis isfreesoftware: you arefreeto change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Typeshow copyingandshow warrantyfordetails. This GDB was configured asarm-linux-gnueabihf.Typeshow configurationforconfiguration 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,typehelp.Typeapropos wordto searchforcommands related toword... Reading symbols from test... warning: Cant open file /root/test during file-backed mapping note processing warning: Cantopenfile/lib64/libc-2.29.so during file-backed mapping note processing warning: Cant open file /lib/ld-2.29.so during file-backed mapping note processing [New LWP 8823] warning: Could not load shared library symbols for /lib64/libc.so.6. Do you need set solib-search-path or set sysroot? Core was generated by ./test.Program terminated with signal SIGSEGV, Segmentation fault.#0 0x0000007fa197c0cc in ?? ()解决方法sudoln-s/home/yoyo/360Downloads/toolchains/arm-linux-gnueabihf/target/lib/libc-2.29.so /lib64/sudoln-s/home/yoyo/360Downloads/toolchains/arm-linux-gnueabihf/target/lib/ld-2.29.so /lib/Qarm-linux-gnueabihf-gdb: error while loading shared libraries: libstdc.so.6: cannot open shared object file: No such file or directoryyoyoyoyo:/media/sda3/share/cache$ arm-linux-gnueabihf-gdb ./demo /home/yoyo/360Downloads/toolchains/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb: errorwhileloading shared libraries: libdl.so.2: cannotopenshared object file: No suchfileor director yoyoyoyo:/media/sda3/share/cache$ arm-linux-gnueabihf-gdb ./demo/home/yoyo/360Downloads/toolchains/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb: errorwhileloading shared libraries: libstdc.so.6: cannotopenshared object file: No suchfileor directory错误原因未设置LD_LIBRARY_PATH环境变量找不到lib库。解决方法设置LD_LIBRARY_PATH环境变量。yoyoyoyo:/media/sda3/share/cache$exportLD_LIBRARY_PATH/home/yoyo/360Downloads/toolchains/arm-linux-gnueabihf/target/lib:$LD_LIBRARY_PATHyoyoyoyo:/media/sda3/share/cache$exportLD_LIBRARY_PATH/home/yoyo/360Downloads/toolchains/arm-linux-gnueabihf/aarch64-linux-gnu/lib64:$LD_LIBRARY_PATH