有有些程序会包含一个特殊的 gnu link 的section:
$ objdump -h test.gnu_debuglink | grep debug
test.gnu_debuglink: file format elf32-i386
26 .gnu_debuglink 00000020 00000000 00000000 00001044 2**0
这个 gnu debug link 是为了调试用的。可以用如下的方法创建:
1) 写一个小程序 test.c 如下:
int i = 100;
void fun(void)
{
i = 10;
}
int main(void)
{
fun();
return 0;
}
gcc -g test.c -o test
2) 把debug section 取出来,另存为一个文件:
objcopy --only-keep-debug test test.gnu_debuglink.debug
3)去掉 符号和debug 信息,
objcopy --strip-all test test.gnu_debuglink
4)把 debug文件和 stripped 的文件关联:
objcopy --add-gnu-debuglink=test.gnu_debuglink.debug test.gnu_debuglink
可以用下面的方法来确认关联:
$ objdump -s -j .gnu_debuglink test.gnu_debuglink
test.gnu_debuglink: file format elf32-i386
Contents of section .gnu_debuglink:
0000 74657374 2e676e75 5f646562 75676c69 test.gnu_debugli
0010 6e6b2e64 65627567 00000000 459748f2 nk.debug....E.H.
在调试的时候,gdb会去特定的目录下寻找 debug文件。
~/tmp$ gdb test.gnu_debuglink --quiet
Reading symbols from test.gnu_debuglink...Reading symbols from /home/charles/tmp/test.gnu_debuglink.debug...done.
done.
(gdb) list main
3 {
4 i = 10;
5 }
6
7 int main(void)
8 {
9 fun();
10 return 0;
11 }
(gdb)
gdb是这样来搜索debug文件的:
gdb looks up the named file in the directory of the executable file, then in a subdirectory of that directory named.debug, and finally under each one of the global debug directories, in a subdirectory whose name is identical to the leading directories of the executable's absolute file name.
举例来说,假如可执行文件的当前目录是 /home/charles/tmp, 全局的调试目录是 /usr/lib/debug, 那么,gdb 将会依次在如下的目录中搜索debug 文件:
1) /home/charles/tmp
2)/home/charles/tmp/.debug
3)/usr/lib/debug/home/charles/tmp
GDB 设置的调试信息目录是通过配置选项 --with-separate-debug-dir 设定的,
(gdb) show configuration
This GDB was configured as follows:
configure --host=i686-linux-gnu --target=i686-linux-gnu
--with-auto-load-dir=$debugdir:$datadir/auto-load
--with-auto-load-safe-path=$debugdir:$datadir/auto-load
--with-expat
--with-gdb-datadir=/usr/share/gdb (relocatable)
--with-jit-reader-dir=/usr/lib/gdb (relocatable)
--without-libunwind-ia64
--with-lzma
--with-python=/usr (relocatable)
--with-separate-debug-dir=/usr/lib/debug (relocatable)
--with-system-gdbinit=/etc/gdb/gdbinit
--with-zlib
--without-babeltrace
gdb在调试过程中,也可以用
set debug-file-directory
directories 设置全局的调试信息目录.
(gdb) show debug-file-directory
The directory where separate debug symbols are searched for is "/usr/lib/debug".