当前位置: 首页 > news >正文

在嵌入式设备中快速体验gdb调试

一、参考资料

gdb+gdbserver远程调试 - lsgxeva - 博客园

嵌入式 程序调试之gdb和gdbserver的交叉编译及使用-CSDN博客

二、快速体验gdb

示例一

test.c

测试代码:

#include<stdio.h>#include<string.h>intmain(){char*p;memcpy(p,"hello",5);return0;}

编译源码:

arm-linux-gnueabihf-gcc test.c-otest-g

用gdb调试代码时必须加上-g选项,如果没有-g,你将看不见程序的函数名、变量名。

启动gdbserver

将编译好的程序拷贝到目标机中,并在目标机上启动gdbserver:

gdbserver192.168.10.90:1234 ./test

连接调试

在宿主机上启动gdb:

arm-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文件

设置 ulimit

Linux系统默认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段错误时,默认ulimit=0,不产生core文件,输出示例:

~# ./testSegmentation fault

当运行程序出现segmentation-fault段错误时,不限制core大小,则生成core文件,输出示例:

~# ulimit -c unlimited~# ulimit -cunlimited ~# ./testSegmentation fault(core dumped)
调试core文件

core文件产生之后将其拷贝到宿主机,不需要连接目标机便可调试。

运行下列指令启动gdb:

yoyo@yoyo:/media/sda3/share/cache$ arm-linux-gnueabihf-gdbtest./core GNU gdb(GDB)10.1Copyright(C)2020Free Software Foundation, Inc. License GPLv3+: GNU GPL version3or later<http://gnu.org/licenses/gpl.html>This isfreesoftware: you arefreeto change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type"show copying"and"show warranty"fordetails. This GDB was configured as"arm-linux-gnueabihf".Type"show configuration"forconfiguration 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 searchforcommands related to"word"... Reading symbols from test... warning: Can't 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

测试代码:

#include<stdio.h>#include<unistd.h>intmain(intargc,char*argv[]){inti;printf("Hello gdb\n");for(i=0;i<5;i++){printf("i=%d\n",i);}while(i<10){sleep(1);}printf("I am exit\n");return0;}

编译源码:

arm-linux-gnueabihf-gcc demo.c-odemo-g

启动gdbserver

将编译好的程序拷贝到目标机中,并在目标机上启动gdbserver:

gdbserver192.168.10.90:1234 ./demo

连接调试

在宿主机上启动gdb:

arm-linux-gnueabihf-gdb ./demo

连接gdbserver:

(gdb)target remote192.168.10.90:1234

gdb调试

# 打断点(gdb)b main# 开始执行程序(gdb)c# 单步执行(gdb)n# 修改变量值(gdb)setvari=10

宿主机输出:

目标机输出:

三、FAQ

Q:warning: Can't open file /lib64/libc-2.29.so during file-backed mapping note processing

yoyo@yoyo:/media/sda3/share/cache$ arm-linux-gnueabihf-gdbtestcore GNU gdb(GDB)10.1Copyright(C)2020Free Software Foundation, Inc. License GPLv3+: GNU GPL version3or later<http://gnu.org/licenses/gpl.html>This isfreesoftware: you arefreeto change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type"show copying"and"show warranty"fordetails. This GDB was configured as"arm-linux-gnueabihf".Type"show configuration"forconfiguration 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 searchforcommands related to"word"... Reading symbols from test... warning: Can't open file /root/test during file-backed mapping note processing warning: Can'topenfile/lib64/libc-2.29.so during file-backed mapping note processing warning: Can't 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/

Q:arm-linux-gnueabihf-gdb: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

yoyo@yoyo:/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 yoyo@yoyo:/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环境变量。

yoyo@yoyo:/media/sda3/share/cache$exportLD_LIBRARY_PATH=/home/yoyo/360Downloads/toolchains/arm-linux-gnueabihf/target/lib:$LD_LIBRARY_PATHyoyo@yoyo:/media/sda3/share/cache$exportLD_LIBRARY_PATH=/home/yoyo/360Downloads/toolchains/arm-linux-gnueabihf/aarch64-linux-gnu/lib64:$LD_LIBRARY_PATH
http://www.jsqmd.com/news/546920/

相关文章:

  • OpenArk内核模式加载失败实战:深度解析与分级解决方案
  • 3大核心技术突破语言壁垒:VRCT让VRChat实现跨语言实时交流
  • 安卓逆向环境搭建:用清华源快速搞定Frida 16.1.4 + Frida-tools 12.3.0黄金组合
  • 解析 Ranges 库:为什么说管道符 `|` 操作是 C++ 容器处理的一次革命?
  • Kimi,Minimax教你的客服怎么做客服
  • 电子数据取证分析师必备:DeepSeek在APK逆向与密码破解中的高效应用指南
  • 百考通:AI赋能设计都高效落地
  • SBK_MAX72xx嵌入式LED点阵驱动库:硬件SPI/软件SPI统一抽象
  • 从DVWA存储型XSS看Web安全:开发者常踩的坑与Impossible级别的启示
  • 效率提升:基于快马平台快速集成openclaw开发局域网协作工具
  • OpenClaw+GLM-4.7-Flash自动化爬虫:智能数据采集方案
  • 终极ESLyric歌词源配置指南:轻松实现酷狗QQ网易云逐字歌词
  • 【手把手教】ROS软路由配置L2TP代理IP全流程指南
  • 收藏!程序员/小白入门大模型必看,我的AI学习踩坑与正确路线分享
  • app下载app 有进度条
  • 嵌入式工程师技术成长路径:从单片机到Linux驱动开发
  • 基于时间序列预测的流行趋势推荐模型
  • PP实战指南:ECN工程变更在物料计划中的关键应用与系统操作解析
  • 别再死磕主机了!我用VMware虚拟机+USB2.0模式,半天搞定Nvidia AGX Xavier刷Jetpack5.0.2
  • 2026年泄爆墙应用白皮书工业领域深度剖析:折叠门/泄压门/泄爆墙/泄爆窗/泄爆门/电磁屏蔽门/监狱门/钢制平开门/选择指南 - 优质品牌商家
  • 售前客户需求深度挖掘:从表面诉求到核心痛点的五步法
  • 从华大九天到芯华章:国产EDA厂商的崛起之路与技术突破
  • 华为交换机流量统计配置全攻略:从ACL到流策略的保姆级教程
  • 2026年必看:专业婚恋软件推荐,找到真爱不迷路
  • 北京GEO服务商推荐:5家优质机构怎么选?
  • 汽车域控制器电源设计避坑:用NXP VR5510实现ASIL-D安全等级的实战配置指南
  • 【数据洞察】2025年中国地铁网络:从客流强度到智慧运营的深度解析
  • NeurIPS2024论文趋势前瞻:从接收列表看AI研究新动向【附历年论文分析】
  • 逆向某鱼x-sign算法时,我踩过的那些坑:从内存Trace到参数拼接的避坑指南
  • 职场效率提升利器:printPDF电子发票批量打印工具使用教程