linux 下分离 debuginfo 到独立文件.


gcc -ggdb utmp.c -o utmp
readelf -S utmp
# 创建一个包含 debuginfo 文件
objcopy --only-keep-debug utmp utmp.debug
# 添加一个包含路径文件的 .gnu_debuglink section, 文件必须存在.
objcopy --add-gnu-debuglink=utmp.debug utmp
# 查看 .gnu_debuglink section
objdump -s -j .gnu_debuglink utmp


utmp:     file format elf64-x86-64


Contents of section .gnu_debuglink:
 0000 75746d70 2e646562 75670000 0068651a  utmp.debug...he.


# 
objcopy --strip-debug utmp


指定gdb 加载 debuginfo 即可 
gdb utmp -s utmp.debug
or
(gdb) file utmp
(gdb) symbol utmp.debug

Build ID

linux 下二进制执行文件在built阶段都会根据时间戳生成一个唯一build-id, 并加入到一个"gnu.build-id" section中. 而gdb默认搜索debuginfo会搜索指定目录(show debug-file-directory) 下build-id关联的.debug. 默认搜索的文件名为 nn/nnnn...nnnn.debug, 前两个"nn"就是它的build-id前两位,后面的nnnn...nnnn则是build-id的剩余部分. 

以下命令都可查看 "gnu.build-id" section 信息.

readelf -n utmp


Notes at offset 0x0000021c with length 0x00000020:
  Owner                 Data size       Description
  GNU                  0x00000010       NT_GNU_ABI_TAG (ABI version tag)
    OS: Linux, ABI: 2.6.18


Notes at offset 0x0000023c with length 0x00000024:
  Owner                 Data size       Description
  GNU                  0x00000014       NT_GNU_BUILD_ID (unique build ID bitstring)
    Build ID: 3b756a4a68a963bcc368ff7174da8a0fae61c37f


readelf  -t utmp  |grep build-id
  [ 3] .note.gnu.build-id


readelf --wide --sections utmp |grep build
  [ 3] .note.gnu.build-id NOTE            000000000040023c 00023c 000024 00   A  0   0  4


objdump -s -j .note.gnu.build-id utmp
utmp:     file format elf64-x86-64


Contents of section .note.gnu.build-id:
 40023c 04000000 14000000 03000000 474e5500  ............GNU.
 40024c 3b756a4a 68a963bc c368ff71 74da8a0f  ;ujJh.c..h.qt...
 40025c ae61c37f                             .a..

示例:


# 显示当前debuginfo默认搜索目录,也可以通过 "set debug-file-directory path" 重新指定.
(gdb) show debug-file-directory
The directory where separate debug symbols are searched for is "/usr/lib/debug".


# 修改debuginfo为关联build-id debuginfo
readelf -n utmp |grep Build
    Build ID: 3b756a4a68a963bcc368ff7174da8a0fae61c37f


mkdir -p /usr/lib/debug/.build-id/3b
mv utmp.debug  /usr/lib/debug/.build-id/3b/756a4a68a963bcc368ff7174da8a0fae61c37f.debug


(gdb) file utmp
Reading symbols from /mnt/hgfs/DPDK/dsw/utmp...Reading symbols from /usr/lib/debug/.build-id/3b/756a4a68a963bcc368ff7174da8a0fae61c37f.debug...done.
done.

参考:

https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html

GitHub 加速计划 / li / linux-dash
10
2
下载
A beautiful web dashboard for Linux
最近提交(Master分支:1 个月前 )
186a802e added ecosystem file for PM2 5 年前
5def40a3 Add host customization support for the NodeJS version 5 年前
Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐