使用c++filt工具demangle C++符号

demangle符号名

在调试C++程序时, 经常会遇到未demangle的C++符号名, 不了解mangle的规则时, 并不太容易确定具体是哪个API. 比如, 使用objdump将boost日志动态库的符号表导出, 你是否能够很快辨别出对应的实际的函数名称.

[jinguang1@centos7-dev ~]$ objdump -T /usr/lib/libboost_log.so

/usr/lib/libboost_log.so:     file format elf64-x86-64

DYNAMIC SYMBOL TABLE:
0000000000000000      DF *UND*	0000000000000000  GLIBCXX_3.4 _ZNSs6appendEPKcm
0000000000000000      DO *UND*	0000000000000000  GLIBCXX_3.4 _ZTVSt16invalid_argument
0000000000000000      DF *UND*	0000000000000000  GLIBCXX_3.4 _ZNSt8bad_castD2Ev
0000000000000000      DF *UND*	0000000000000000  GLIBCXX_3.4 _ZSt9use_facetISt7codecvtIwc11__mbstate_tEERKT_RKSt6locale
0000000000000000      DF *UND*	0000000000000000  GLIBCXX_3.4 _ZSt20__throw_length_errorPKc
0000000000000000      DF *UND*	0000000000000000  GLIBCXX_3.4 _ZNSt9basic_iosIwSt11char_traitsIwEE5imbueERKSt6locale
0000000000000000      DF *UND*	0000000000000000  GLIBCXX_3.4 _ZNSt15basic_streambufIwSt11char_traitsIwEE7seekoffElSt12_Ios_SeekdirSt13_Ios_Openmode

c++filt工具

c++filt能够将上面的mangle过的符号表转换(demangel)成我们能够看懂的符号名, 以上面最后一个符号名为例子:

[jinguang1@centos7-dev ~]$ c++filt _ZNSt15basic_streambufIwSt11char_traitsIwEE7seekoffElSt12_Ios_SeekdirSt13_Ios_Openmode
std::basic_streambuf >::seekoff(long, std::_Ios_Seekdir, std::_Ios_Openmode)

详细的使用方法可以参考man手册:

C++FILT(1)                                                              GNU Development Tools                                                             C++FILT(1)

NAME
       c++filt - Demangle C++ and Java symbols.

SYNOPSIS
       c++filt [-_|--strip-underscore]
               [-n|--no-strip-underscore]
               [-p|--no-params]
               [-t|--types]
               [-i|--no-verbose]
               [-s format|--format=format]
               [--help]  [--version]  [symbol...]

DESCRIPTION
       The C++ and Java languages provide function overloading, which means that you can write many functions with the same name, providing that each function takes
       parameters of different types.  In order to be able to distinguish these similarly named functions C++ and Java encode them into a low-level assembler name
       which uniquely identifies each different version.  This process is known as mangling. The c++filt [1] program does the inverse mapping: it decodes
       (demangles) low-level names into user-level names so that they can be read.

       Every alphanumeric word (consisting of letters, digits, underscores, dollars, or periods) seen in the input is a potential mangled name.  If the name decodes
       into a C++ name, the C++ name replaces the low-level name in the output, otherwise the original word is output.  In this way you can pass an entire assembler
       source file, containing mangled names, through c++filt and see the same source file containing demangled names.

       You can also use c++filt to decipher individual symbols by passing them on the command line:

               c++filt 

       If no symbol arguments are given, c++filt reads symbol names from the standard input instead.  All the results are printed on the standard output.  The
       difference between reading names from the command line versus reading names from the standard input is that command line arguments are expected to be just
       mangled names and no checking is performed to separate them from surrounding text.  Thus for example:

               c++filt -n _Z1fv

       will work and demangle the name to "f()" whereas:

               c++filt -n _Z1fv,

       will not work.  (Note the extra comma at the end of the mangled name which makes it invalid).  This command however will work:

               echo _Z1fv, | c++filt -n

       and will display "f(),", i.e., the demangled name followed by a trailing comma.  This behaviour is because when the names are read from the standard input it
       is expected that they might be part of an assembler source file where there might be extra, extraneous characters trailing after a mangled name.  For
       example:

                   .type   _Z1fv, @function

你可能感兴趣的:(C/C++)