gdb 常见调试问题

 

 1、gdb 无符号表信息

[root@localhost asan_test]# gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/asan_test/a.out...Dwarf Error: wrong version in compilation unit header (is 5, should be 2, 3, or 4) [in module /root/t24685/asan_test/a.out]
(no debugging symbols found)...done.
(gdb) b main
Breakpoint 1 at 0x400506
(gdb)  b 1.c:7
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb)

此问题是由于gcc和gdb的对Dwarf的版本不配套导致的,指定gcc编译时的版本。(-gdwarf-2、-gdwarf-3、-gdwarf-4)

[root@localhost asan_test]# gcc -g -gdwarf-4 -gstrict-dwarf 1.c -o a.out
[root@localhost asan_test]# gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/t24685/asan_test/a.out...done.
(gdb) b main
Breakpoint 1 at 0x40050a: file 1.c, line 6.

参考:https://blog.csdn.net/fandroid/article/details/32914203

 

1、剥离可执行文件的符号表:

objcopy --only-keep-debug a.out out.symbol
//将a.out中symbol等剥离出来放到out.symbol

2、剥离符号表中的可执行文件

objcopy --strip-debug a.out no.aout
//将a.out可执行文件剥离symbol等变成可执行文件no.aout

3、添加符号表到可执行文件

objcopy --add-gnu-debuglink=out.symbol no.aout
//将没有symbol的可执行文件no.aout 添加上符号表out.symbol

4、gdb 调试没有符号表的可执行文件

gdb -s out.symbol -e no.aout
//gdb调试没有符号表的no.aout(通过-s 指定symbol在out.symbol)  
//或gdb启动进去后 symbol-file 读取符号表,查看可执行文件是否带符号表,则file  可执行文件,查看是否有stripped 标识
[root@localhost test]# gdb   no.aout
(gdb) symbol-file out.symbol

5、指定地址添加符号表(用于4没有添加成功的情况)

 

info sharedlibrary //查看address
add-symbol-file /path/to/library.so符号表 <address>

 

 

2、出现 Type <RET> for more, q to quit, c to continue without paging

在 GDB 中出现 "Type <RET> for more, q to quit, c to continue without paging" 的提示时,这通常是因为 GDB 检测到输出信息太多而无法完全显示在一个屏幕上。GDB 提供了分页功能以便查看大量的信息,以防止信息被快速滚动而难以阅读。

1、按回车键(<RET>):

  按回车键会显示更多的输出信息。您可以一直按回车键,直到看到您感兴趣的信息。

2、按 'q' 键退出分页:

  如果您只想看到当前屏幕的信息,可以按 'q' 键退出分页,然后 GDB 将不再进行分页显示。

3、使用 'set height' 命令:

  在 GDB 中,您可以使用 set height 命令设置分页的行数。例如,set height 0 将关闭分页功能。

4、在启动 GDB 时禁用分页:

  在启动 GDB 时,您可以使用 -nx 选项来禁用初始化文件(.gdbinit)中的配置,这样也可以禁用分页。例如:

gdb -nx ./your_executable

 3、查看app 是否带符号表,以及能支持gdb 调试

sh-4.4$ nm /test_app
nm: /test_app: no symbols

nm 查看 no symbol 则表示没有符号表给gdb 调试

支持gdb 调试nm 查看到的

[localhost ~]$ nm test_trunk
                 U abort@@GLIBC_2.17
                 U access@@GLIBC_2.17
0000000000420098 B __bss_end__
0000000000420098 B _bss_end__
0000000000420094 B __bss_start
0000000000420094 B __bss_start__
00000000004009e0 t call_weak_fn
                 U close@@GLIBC_2.17
0000000000420094 b completed.8225

 

posted on 2022-02-08 09:54  红旗kernel  阅读(1079)  评论(0)    收藏  举报

导航