------- Comment #5 from pluto at agmk dot net  2007-11-05 15:44 -------
(In reply to comment #4)
> This is a possible solution.  Not ideal though, because, then all exceptions
> will be trigger the breakpoint at __cxa_throw.
> 
> If however, I put a breakpoint in the catch in question, then I know I will 
> get
> the exception that I was looking for, and if bakctrace info was part of this
> exception, then I could find out exactly what happened where.

gnu/ld has a nice option called --wrap that you can use to hack
the g++/throw() and embbed stacktrace in way you like ;-)

$ cat u.cpp
#include <cstdio>
#include <unwind.h>
#include <dlfcn.h>

_Unwind_Reason_Code helper( struct _Unwind_Context* ctx, void* )
{
        void* p = reinterpret_cast< void* >( _Unwind_GetIP( ctx ) );
        Dl_info info;
        if ( dladdr( p, &info ) )
        {
                if ( info.dli_saddr )
                {
                        long d = reinterpret_cast< long >( p )
                                - reinterpret_cast< long >( info.dli_saddr );
                        printf( "%p %s+0x%lx\n", p, info.dli_sname, d );
                }
        }
        return _URC_NO_REASON;
}

extern "C" void __real___cxa_throw( void* thrown_exception,
        std::type_info* tinfo, void ( *dest )( void* ) )
        __attribute__(( noreturn ));

extern "C" void __wrap___cxa_throw( void* thrown_exception,
        std::type_info* tinfo, void ( *dest )( void* ) )
{
        _Unwind_Backtrace( helper, 0 );
        __real___cxa_throw( thrown_exception, tinfo, dest );
}

void __attribute__(( noinline )) zoo( void )
{
        try
        {
                std::puts( "ping!" );
                throw 1;
        }
        catch ( int )
        {
                std::puts( "pong!" );
        }
}

void __attribute__(( noinline )) bar( void ( *f )( /*void*/ ) )
{
        f();
}

void __attribute__(( noinline )) foo( void )
{
        bar( &zoo );
}

int main()
{
        foo();
        return 0;
}


$ g++ u.cpp -ldl -rdynamic -Wl,--wrap,__cxa_throw && ./a.out
ping!
0x80489c7 __wrap___cxa_throw+0x15
0x8048a16 _Z3zoov+0x3a
0x8048a7d _Z3barPFvvE+0xb
0x8048a93 _Z3foov+0x13
0x8048ab9 main+0x21
0x2e3e23 __libc_start_main+0xd3
pong!


-- 

pluto at agmk dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pluto at agmk dot net


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33903

Reply via email to