gdb之info/show命令

一、gdb命令
相对于常见和常用的backtrace,break,watch,step之类的指令来说,这两个都是相对比较小众的功能。只管来说,小众的功能就是使用的人比较少的功能,而使用的比较少的原因也可能是多方面的,一个可能的原因就是大部分人没有使用这种功能的场景和需求,或者可以套用一下比较万能的二八原则,这里就不再铺垫了。
相对来说 info和show这两类(注意它不是两个)命令上镜的机会就比较少,可能就是很多人对gdb的使用只是作为学习或者简单功能的使用,因此不需要这种相对边缘化的辅助性功能。但是事实上这两类命令中包含了比较多的有用信息,对于复杂一些的工程它可以减少很多尝试过程,即使是小型项目,如果你对它不熟悉,这一点同样适用。一个熟悉的项目可以减少对辅助性功能/工具的依赖,因为很多东西就在你的脑海中。
现在假设一个比较特殊的例子,比方说在一个环境中开启了很多的调试会话,或者说一个调试会话,过一段时间之后,你想关掉这个会话,但是又不确定会不会有什么影响(这一点在虚拟机或者是公用开发环境中是可能出现的)。或者说调试了一段时间之后,不太确认自己当时是如何启动的,run的时候有没有加参数,或者gdb是否使用了上次传递的参数等,此时就需要看一下调试的文件和调试的参数命令。
当然这些的确小众一些,一个更加现实的例子是在C++中,类、命名空间、函数重载等这些机制让一个变量的搜索更加的麻烦,而且一些动态库及第三方代码更是增加了工程的复杂度,此时在代码中看到一个函数,也无法知道这个函数是不是在某个特定的作用域中,或者说模糊的记得一个函数的名称,但是不记得全拼,此时如何查看符号的信息呢?
二、info 命令家族登场
1、命令枚举
(gdb) help info
Generic command for showing things about the program being debugged.

List of info subcommands:

info address -- Describe where symbol SYM is stored
info all-registers -- List of all registers and their contents
info args -- Argument variables of current stack frame
info auxv -- Display the inferior's auxiliary vector
info breakpoints -- Status of user-settable breakpoints
info catch -- Exceptions that can be caught in the current stack frame
info checkpoints -- IDs of currently known checkpoints
info classes -- All Objective-C classes
info common -- Print out the values contained in a Fortran COMMON block
info copying -- Conditions for redistributing copies of GDB
info dcache -- Print information on the dcache performance
info display -- Expressions to display when program stops
info extensions -- All filename extensions associated with a source language
info files -- Names of targets and files being debugged
info float -- Print the status of the floating point unit
info frame -- All about selected stack frame
info functions -- All function names
info handle -- What debugger does when program gets various signals
info inferiors -- IDs of currently known inferiors
---Type <return> to continue, or q <return> to quit---
info line -- Core addresses of the code for a source line
info linkmap -- Display the inferior's linkmap
info locals -- Local variables of current stack frame
info macro -- Show the definition of MACRO
info mem -- Memory region attributes
info os -- Show OS data ARG
info proc -- Show /proc process information about any running process
info program -- Execution status of the program
info record -- Info record options
info registers -- List of integer registers and their contents
info scope -- List the variables local to a scope
info selectors -- All Objective-C selectors
info set -- Show all GDB settings
info sharedlibrary -- Status of loaded shared object libraries
info signals -- What debugger does when program gets various signals
info source -- Information about the current source file
info sources -- Source files in the program
info stack -- Backtrace of the stack
info symbol -- Describe what symbol is at location ADDR
info target -- Names of targets and files being debugged
info tasks -- Provide information about all known Ada tasks
info terminal -- Print inferior's saved terminal status
info threads -- IDs of currently known threads
---Type <return> to continue, or q <return> to quit---
info tracepoints -- Status of tracepoints
info types -- All type names
info variables -- All global and static variable names
info vector -- Print the status of the vector unit
info warranty -- Various kinds of warranty you do not have
info watchpoints -- Synonym for ``info breakpoints''
info win -- List of all displayed windows

Type "help info" followed by info subcommand name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
这些命令提供的功能有些是可以通过其它方法获得的,例如sharedlibrary可以通过被调试任务的maps中找打,有些是相对比较常用的,例如info register,有些是大部分时候不太需要的,例如info args,auxv,有些是大家需要但是通常不知道在哪里的,例如 info function ,info type ,info variable等,还有通过info address 来看一个变量的位置的。
这里引入写这篇总结的原因是其中的info function 命令,因为对于一些函数,我们知道它的名称,或者大致名称,但是不确定它是否在一个类或者命名空间中,现象就是直接的断点命令 break 无法找到该命令,所以需要使用这里的通配符来尝试列出所有可能的组合。
2、info function 使用例子
(gdb) help info function
All function names, or those matching REGEXP.
[tsecer@Harry classspace]$ cat fuzzy.cpp
#include <stdio.h>
#include <stdlib.h>

namespace space
{
int foo(void) {printf("In %s\n",__FUNCTION__);}
}
struct cls
{
int foo(void) {printf("In class\n");}
};
int foo(void)
{
    printf ("bare function");
}
int main()
{
    cls mycls;
    mycls.foo();
    return 0;
}
[tsecer@Harry classspace]$ g++ -g fuzzy.cpp -o fuzzy.cpp.exe
[tsecer@Harry classspace]$ gdb fuzzy.cpp.exe
GNU gdb (GDB) Fedora (7.0-3.fc12)
Copyright (C) 2009 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 "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/tsecer/CodeTest/classspace/fuzzy.cpp.exe...done.
(gdb) info function foo
All functions matching regular expression "foo":

File fuzzy.cpp:
int cls::foo();
int foo();
int space::foo();
(gdb)
例子中定义了三个函数,分别位于 命名空间 、类以及全局空间中,此时可以通过
info function foo
来列出所有的函数列表,它包含了所有的类、命名空间以及全局函数。
3、gdb实现简单说明
实现代码位于functions_info--->>symtab_symbol_info--->>>search_symbols--->>re_exec (SYMBOL_NATURAL_NAME (*psym))
这里就涉及到了C++(以及其它的例如java、objc之类的语言)的名字粉碎机制,他为了支持重载函数,所以将函数的名字进行了编译时粉碎,从而让一个名字看起来尽可能的全局唯一,对应的,gdb就需要完成这个逆向的反粉碎过程,将一个粉碎之后的变量名称再还原为原始的源代码中定义的变量名,从而可以达到和程序猿的交互。我们现学现用,看一下gdb内部的反粉碎接口实现
[tsecer@Harry classspace]$ gdb `which gdb`
GNU gdb (GDB) Fedora (7.0-3.fc12)
Copyright (C) 2009 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 "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /usr/bin/gdb...(no debugging symbols found)...done.
Missing separate debuginfos, use: debuginfo-install gdb-7.0-3.fc12.i686
(gdb) info function demangle
All functions matching regular expression "demangle":

Non-debugging symbols:
0x0810c700  cp_demangled_name_to_comp
0x08143e40  print_address_demangle
0x08149230  symbol_demangled_name
0x08199a60  language_demangle
0x081a1110  _initialize_demangler
0x081dea00  objc_demangle
0x081e8570  java_demangle_type_signature
0x0823d830  bfd_demangle
0x082b7cf0  cplus_demangle_set_style
0x082b88e0  cplus_demangle_name_to_style
0x082ba620  cplus_demangle
0x082bcea0  cplus_demangle_opname
0x082bd2e0  cplus_demangle_fill_name
0x082bd320  cplus_demangle_fill_extended_operator
0x082bd360  cplus_demangle_fill_ctor
0x082bd3a0  cplus_demangle_fill_dtor
0x082bd9c0  cplus_demangle_init_info
0x082bdad0  cplus_demangle_type
0x082bf560  cplus_demangle_mangled_name
0x082c11b0  cplus_demangle_print_callback
---Type <return> to continue, or q <return> to quit---
0x082c1250  cplus_demangle_print
0x082c1580  java_demangle_v3_callback
0x082c1610  java_demangle_v3
0x082c1630  cplus_demangle_v3
0x082c1650  cplus_demangle_v3_callback
0x082c2450  cplus_demangle_fill_component
0x082c24a0  cplus_demangle_v3_components
0x082c25d0  cplus_demangle_fill_operator
0x082c2670  cplus_demangle_fill_builtin_type
(gdb)
具体这个反粉碎过程如何完成,答案是按照相关ABI的规定完成,至于ABI是如何的,那有兴趣有时间的同学可以结合代码看一下ABI的相关文档,这里从略。
三、show命令家族说明
(gdb) help show
Generic command for showing things about the debugger.

List of show subcommands:
和info相对应,show是显示调试器本身的信息,而info则显示被调试任务的信息。相对来说,我对show命令的使用也不多,但是show的子命令却要比info命令多很多,我使用的版本有103项。show命令其实是和set命令对应的,其中可能比较有用的功能包括
follow-fork-mode
substitute-path
args
prompt

不再详细说明,有兴趣的同学可以自己看看。




 转自:http://tsecer.blog.163.com/blog/static/1501817201252322735816/


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值