如何用gdb-multiarch调试Yocto生成的ARM应用核心转储?
问题描述
我有一个通过Yocto构建系统编译的ARM架构应用,运行时崩溃并生成了core dump。在目标设备上安装GDB和应用的dbg ipk包后,能正常加载core dump并查看栈跟踪。但在无目标设备的环境下,用gdb-multiarch加载应用可执行文件和core dump时,无法查看有效的栈跟踪内容——虽然gdb提示已加载调试符号,且执行list命令能看到main.cpp的代码,但栈跟踪只显示??,还提示栈可能损坏。不需要所有共享库的栈符号,仅需应用自身的符号。
附gdb-multiarch会话日志:
# gdb-multiarch GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1 Copyright (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 "x86_64-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". (gdb) file my_application Reading symbols from my_application... Reading symbols from .debug/my_application... (gdb) core core-dump-file warning: Can't open file /lib/libc-2.21.so during file-backed mapping note processing warning: Can't open file /lib/libgcc_s.so.1 during file-backed mapping note processing warning: Can't open file /lib/libm-2.21.so during file-backed mapping note processing warning: Can't open file /lib/libstdc++.so.6.0.21 during file-backed mapping note processing warning: Can't open file /usr/lib/libboost_system.so.1.60.0 during file-backed mapping note processing warning: Can't open file /usr/lib/libboost_filesystem.so.1.60.0 during file-backed mapping note processing warning: Can't open file /usr/lib/libcrypto.so.1.0.0 during file-backed mapping note processing warning: Can't open file /usr/lib/libssl.so.1.0.0 during file-backed mapping note processing warning: Can't open file /lib/librt-2.21.so during file-backed mapping note processing warning: Can't open file /usr/lib/libjsoncpp.so.1.8.4 during file-backed mapping note processing warning: Can't open file /lib/libpthread-2.21.so during file-backed mapping note processing warning: Can't open file /lib/libatomic.so.1.1.0 during file-backed mapping note processing warning: Can't open file /lib/libdl-2.21.so during file-backed mapping note processing warning: Can't open file /lib/ld-2.21.so during file-backed mapping note processing [New LWP 9334] [New LWP 9344] [New LWP 9346] [New LWP 9347] [New LWP 9348] warning: Could not load shared library symbols for 14 libraries, e.g. /lib/libdl.so.2. Use the "info sharedlibrary" command to see the complete listing. Do you need "set solib-search-path" or "set sysroot"? Core was generated by `/usr/sbin/my_application'. Program terminated with signal SIGABRT, Aborted. #0 0xb6aa0996 in ?? () [Current thread is 1 (LWP 9334)] (gdb) bt #0 0xb6aa0996 in ?? () #1 0xb6aae448 in ?? () Backtrace stopped: previous frame identical to this frame (corrupt stack?) (gdb) list warning: Source file is more recent than executable. 13 #include .... 14 ...... ..... shows code from the main.cpp in my application 15 ...... (gdb)
解决方法
- 确保符号、可执行文件与core dump版本完全匹配:gdb提示“Source file is more recent than executable”,说明当前使用的源码/符号文件和生成core dump的应用版本不一致。必须使用与生成core dump的应用完全同一次构建的调试符号(从对应的dbg ipk中提取)和可执行文件,不能用新编译的版本。
- 指定调试符号目录:如果dbg ipk解压后调试符号在单独的
.debug目录,用set debug-file-directory命令让gdb定位到该目录:set debug-file-directory /path/to/extracted-dbg-package/.debug - 手动加载应用符号到对应内存地址:
- 在目标设备上重新运行应用,用
cat /proc/<pid>/maps获取应用的加载基地址(比如输出中my_application对应的起始地址,格式类似0x00010000-0x00020000 r-xp ...,其中0x00010000就是基地址)。 - 在gdb-multiarch中执行:
这会强制将应用符号映射到正确的内存区域,帮助gdb解析栈帧。add-symbol-file my_application 0x<获取到的基地址>
- 在目标设备上重新运行应用,用
- 强制解析栈帧:
- 切换到当前栈帧:
frame 0 - 查看寄存器信息:
info registers - 查看当前PC指向的指令:
x/i $pc - 结合应用的符号表(可以用
nm my_application | grep <函数名>提前查看),手动匹配指令地址对应的函数。
- 切换到当前栈帧:
- 可选:使用Yocto构建的sysroot:如果能获取到Yocto构建时的sysroot(包含目标系统的库和符号),执行
set sysroot /path/to/yocto-sysroot,让gdb能解析系统库的符号,这会帮助gdb正确识别栈结构,进而显示应用自身的栈帧信息。
内容的提问来源于stack exchange,提问作者sickgemini
相关产品推荐
相关产品推荐

