前面准备工作做好后,下面开始 gdb 调试,默认情况下此时 gdb 没有找到 glibc 的符号信息 (No debugging symbols found in /lib/x86_64-linux-gnu/libc.so.6):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
lhx@ubuntu:~$ gdb a.out Reading symbols from a.out... (gdb) set verbose on (gdb) b 5 Reading in symbols for hello.c... Breakpoint 1 at 0x1151: file hello.c, line 5. (gdb) r Starting program: /home/lhx/a.out Using PIE (Position Independent Executable) displacement 0x555555554000 for "/home/lhx/a.out". Reading symbols from /lib64/ld-linux-x86-64.so.2... (No debugging symbols found in /lib64/ld-linux-x86-64.so.2) Reading symbols from system-supplied DSO at 0x7ffff7fcd000... (No debugging symbols found in system-supplied DSO at 0x7ffff7fcd000) Reading symbols from /lib/x86_64-linux-gnu/libc.so.6... (No debugging symbols found in /lib/x86_64-linux-gnu/libc.so.6)
Breakpoint 1, hello () at hello.c:5 5 printf("Hello World !\n"); (gdb)
查看下 gdb 符号搜索路径是否正确,不同的话,需要手动设置 debug-file-directory 后才能找到符号信息,可以看到已经成功找到了:Reading symbols from /usr/lib/debug/.build-id/18/78e6b475720c7c51969e69ab2d276fae6d1dee.debug…。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
(gdb) show debug-file-directory The directory where separate debug symbols are searched for is "/usr/local/lib/debug". (gdb) set debug-file-directory /usr/lib/debug/ (gdb) r Starting program: /home/lhx/a.out Using PIE (Position Independent Executable) displacement 0x555555554000 for "/home/lhx/a.out". Reading symbols from /lib64/ld-linux-x86-64.so.2... Reading symbols from /usr/lib/debug/.build-id/45/87364908de169dec62ffa538170118c1c3a078.debug... Reading symbols from system-supplied DSO at 0x7ffff7fcd000... (No debugging symbols found in system-supplied DSO at 0x7ffff7fcd000) Reading in symbols for rtld.c... Reading symbols from /lib/x86_64-linux-gnu/libc.so.6... Reading symbols from /usr/lib/debug/.build-id/18/78e6b475720c7c51969e69ab2d276fae6d1dee.debug...
Breakpoint 1, hello () at hello.c:5 5 printf("Hello World !\n");
(gdb) s Reading in symbols for ioputs.c... __GI__IO_puts (str=0x555555556004 "Hello World !") at ioputs.c:33 33 ioputs.c: 没有那个文件或目录. (gdb) dir ~/test1/glibc-2.31/libio Source directories searched: /home/lhx/test1/glibc-2.31/libio:$cdir:$cwd (gdb) n 35 size_t len = strlen (str); (gdb) l 30 31 int 32 _IO_puts (const char *str) 33 { 34 int result = EOF; 35 size_t len = strlen (str); 36 _IO_acquire_lock (stdout); 37 38 if ((_IO_vtable_offset (stdout) != 0 39 || _IO_fwide (stdout, -1) == -1) (gdb) bt #0 __GI__IO_puts (str=0x555555556004 "Hello World !") at ioputs.c:35 #1 0x000055555555515d in hello () at hello.c:5 Reading in symbols for ../sysdeps/x86/libc-start.c... #2 0x0000555555555172 in main () at hello.c:10 (gdb)