where is c++filt source code?
Asked Answered
S

3

13

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

Soma answered 24/1, 2011 at 8:9 Comment(0)
M
7

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.

Monosymmetric answered 24/1, 2011 at 8:27 Comment(0)
O
6

it's part of binutils:

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

Overthrust answered 24/1, 2011 at 8:14 Comment(0)
G
4

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.

Gorgias answered 24/1, 2011 at 8:24 Comment(9)
how about solaris, the printstack just doesn't demangle. should I print stack to a pipe, and read , demangle the msg?Soma
@dma: Sample code above - tested under Linux but should work on Solaris. Cheers.Gorgias
I like the libstdc++ link. But I can't in good conscience give a +1 to something that advocates using popen() like this. :PHerr
@asveikau: I wasn't actually advocating it - simply illustrating how to do what @dma asked about in his comment above. That said, would I advocate it? I'm indifferent. This kind of stack printing thing is typically done as a near-final step in the deliberately fatal handling of a failed assertion or catastrophic error: there's no particular need to internalise the code: if the extra memory overhead of popen()/sh etc stops it working, so be it; if it's slower, who cares - it's once in a blue moon stuff. And pstack | c++filt is more portable and less heavily wedded to any given OS.Gorgias
how about AIX, mt__trce can't used in c++?Soma
@dma: I'm not sure.. sounds good, but I haven't got an AIX box handy to test on. It may or may not demangle....Gorgias
@Tony: thank you so much, the demangle.h works good in AIX; but I wanna know wether c++ abi was supported by aix?Soma
@dma: Every C++ compiler must embrace some ABI standards, but they may use different ones on different OS. There's no single industry-wide "C++ ABI", not even a single standard for name mangling. That's why you often need to use the c++filt or library function shipped by the same compiler that compiled the code. I'm not an expert on this stuff though: for more details, you could put it to the wider SO community in a new question. Cheers.Gorgias
I just would like to add that your comments also provide great info and could be on the answer itself :)Mitinger

© 2022 - 2024 — McMap. All rights reserved.