Binutils - c++filt工具

简介

  • c++filt是C++源码编译后生成二进制文件中符号表中的符号名还原工具。
  • C++语言是允许函数重载的,但是程序中一个函数名只能拥有一个内存地址,为了支持函数重载,c++编译时会在符号表中对函数名进行修改(根据参数类型等信息),因此符号表中的函数名将会变得人眼不可读,如下:
0000000000019176 t _ZL18__gthread_active_pv
00000000000190ad T _ZN10TimeWalker10abstime2tsERKSs

使用

  1. 帮助信息
xxx@chejiser:~$ c++filt -h
Usage: c++filt [options] [mangled names]
Options are:
  [-_|--strip-underscore]     Ignore first leading underscore
  [-n|--no-strip-underscore]  Do not ignore a leading underscore (default)
  [-p|--no-params]            Do not display function arguments
  [-i|--no-verbose]           Do not show implementation details (if any)
  [-t|--types]                Also attempt to demangle type encodings
  [-s|--format {none,auto,gnu,lucid,arm,hp,edg,gnu-v3,java,gnat}]
  [@<file>]                   Read extra options from <file>
  [-h|--help]                 Display this information
  [-v|--version]              Show the version information
Demangled names are displayed to stdout.
If a name cannot be demangled it is just echoed to stdout.
If no names are provided on the command line, stdin is read.
Report bugs to <http://www.sourceware.org/bugzilla/>.
  1. 直接跟符号名
$ c++filt _ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7compareEPKc
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(char const*) const
  1. 和nm配合使用
nm xxx.so | xargs c++filt

使用场景

  • 由于C++源码编译后函数名变得不可读,程序链接或者运行时出现的一些错误,程序员就不好定位,如下:
  1. 链接时提示函数undefined问题
  • 有时候,确认了其它条件都正确(路径正确,文件存在,链接正确等),但是链接时依然会提示undefined,可以通过查看符号表的方式来确认undefined的符号名是否存在,C++符号名不可读,可先使用c++filt转换为人眼可读,再进行查找。
nm libxxx.so | xargs c++filt | grep 报错函数名
  1. 运行崩溃,定位问题点
  • 使用backtrace可打印出崩溃时的函数调用栈,但是C++程序打印出的函数名可能是人眼不可读的,因此需要使用c++filt还原回来。
$ ./xxx
stack traceback:
./a.out() [0x401aff]
/lib/x86_64-linux-gnu/libc.so.6(+0x354b0) [0x7fd5f98b54b0]
/lib/x86_64-linux-gnu/libc.so.6(+0x16eff6) [0x7fd5f99eeff6]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7compareEPKc+0x3a) [0x7fd5f9f9145a]
....
  1. 与汇编代码对应
  • 反汇编后查看汇编代码时,可以将汇编代码和C++代码对应起来,如下:
* 汇编代码
   movl    -8(%rbp), %edx
    movl    -12(%rbp), %eax
    movl    %edx, %esi
    movl    %eax, %edi
    call    _Z5myaddii  //调用函数
    movl    %eax, -4(%rbp)
    movl    $0, %eax
* 对应C++函数名
$ c++filt _Z5myaddii
myadd(int, int)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值