Bug 46751 - backtrace_symbols wrong when in unexpected_handler
Summary: backtrace_symbols wrong when in unexpected_handler
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.5
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-01 19:13 UTC by Erik J Groeneveld
Modified: 2012-02-04 00:25 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Erik J Groeneveld 2010-12-01 19:13:46 UTC
When in an std::unexpected_handler, backtrace_symbols returns a trace with functions not even called at all.  

I noticed strange functions to appear on the trace, and missed functions that should be there.

I reproduced it with the following minimal code:

#include <exception>
#include <execinfo.h>

extern "C" void my_unexpected_handler() {
        void* trace[100];
        int size = backtrace(trace, 100);
        backtrace_symbols_fd(trace, size, 2);
}

void throw_0() throw() {
        throw 0;
}

extern "C" void not_called_at_all_but_appearing_on_the_backtrace() { }

extern "C" int main(int, char*[]) {
        std::set_unexpected(my_unexpected_handler);
        throw_0();
        return 0;
}

I compile this with: g++ -g -rdynamic <filename>
It then produces:

./a.out(my_unexpected_handler+0x1f)[0x8048973]
/usr/lib/libstdc++.so.6(+0xbd465)[0xb7853465]
/usr/lib/libstdc++.so.6(__cxa_call_unexpected+0x45)[0xb78528b5]
./a.out(not_called_at_all_but_appearing_on_the_backtrace+0x0)[0x80489dc]
./a.out(main+0x1a)[0x80489fb]
/lib/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb7621c76]
./a.out[0x80488c1]
terminate called after throwing an instance of 'int'
Aborted

As you can see, "not_called_at_all_but_appearing_on_the_backtrace" is not called anywhere, but yet it is on the stack trace.

The function throw_0 however is missing.

I believe this is a bug.  If not, what is going on here?

Best regards
Erik Groeneveld
Comment 1 Andrew Pinski 2010-12-01 19:18:02 UTC
backtrace is part of libc and not GCC.
Comment 2 Erik J Groeneveld 2010-12-01 19:27:26 UTC
(In reply to comment #1)
> backtrace is part of libc and not GCC.

Thank you, I'll post it there too.

Could it also have to do something with exception handling code in the c++ runtime that calls unexpected_handler?  I think it somehow leaves the stack in a state that backtrace does not expect. That still leaves open the question where the bug is, if any.
Comment 3 Andrew Pinski 2012-02-04 00:25:04 UTC
The backtrace is mostly right.  The issue is that the function after _Z7throw_0v is not_called_at_all_but_appearing_on_the_backtrace.  

The debugging info is correct.  The call return place is really not_called_at_all_but_appearing_on_the_backtrace.