where is c++filt source code?

前端 未结 3 1520
-上瘾入骨i
-上瘾入骨i 2021-02-08 06:14

Does anyone known the link of c++filt source code. I want call c++filt in my code as a library.

相关标签:
3条回答
  • 2021-02-08 06:52

    On Linux you can use /usr/include/demangle.h which comes with binutils-dev package. You'll have to link to the libiberty from binutils.

    0 讨论(0)
  • 2021-02-08 06:56

    it's part of binutils:

    http://ftp.gnu.org/gnu/binutils/

    0 讨论(0)
  • 2021-02-08 06:56

    Given different compilers can mangle differently, each tends to ship with a custom c++filt. But, most systems will already have a demangling library function available somewhere. On my Linux box I found /usr/include/c++/version/cxxabi.h header defining __cxa_demangle() (see http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html). I thought I'd used some other function in the past though, but can't find the details (EDIT: probably the demangle version İsmail documents). On AIX there's a demangle.h.

    EDIT: on most systems with pstack and c++filt programs (i.e. Linux and Solaris), the following should work...

    #include <cstdio>
    #include <iostream>
    #include <sstream>
    
    struct X
    {
        void f()
        {
            std::ostringstream cmd;
            cmd << "pstack " << getpid() << " | c++filt";
            if (FILE* f = popen(cmd.str().c_str(), "r"))
            {
                char buffer[1024];
                int n;
                while ((n = fread(buffer, 1, sizeof buffer, f)) > 0)
                    std::cout.write(buffer, n);
            }
            else
                std::cerr << "popen() failed\n";
        }
    };
    
    int main()
    {
        X x;
        x.f();
    }
    

    ...output...

    #0  0x003539be in __read_nocancel () from /lib/tls/i686/libc.so.6
    #1  0x002ff590 in _IO_file_read_internal () from /lib/tls/i686/libc.so.6
    #2  0x002fe522 in _IO_new_file_underflow () from /lib/tls/i686/libc.so.6
    #3  0x00300371 in __underflow () from /lib/tls/i686/libc.so.6
    #4  0x0030079d in _IO_default_xsgetn_internal () from /lib/tls/i686/libc.so.6
    #5  0x00300733 in _IO_sgetn_internal () from /lib/tls/i686/libc.so.6
    #6  0x002f666c in fread () from /lib/tls/i686/libc.so.6
    #7  0x08048c36 in X::f ()
    #8  0x08048ac0 in main ()
    

    Notice that __read_nocancel etc are NOT C++-mangled identifiers: they're just internal C function names, using the reserved-for-implementation leading-underscore-and-uppercase-letter or leading-double-underscore convensions.

    X::f() was a mangled identifier and has been demangled.

    0 讨论(0)
提交回复
热议问题