I need to catch some "fatal" C++ exception
, then flush logs and rethrow the former, with its own backtrace.
My current solution, however, displays (correctly) the wrong stacktrace.
#include <exception>
#include <iostream>
void fatal(const std::exception & E)
{
// do - something - extremely - important
throw E;
}
int foo()
{
throw std::runtime_error("yet another foo function");
}
int main()
{
try
{
return foo();
}
catch (const std::exception & E)
{
fatal(E);
}
return -1;
}
Program being wrapped by
$ cat ./backtrace
backtrace
quit
$ ulimit -c unlimited
$ ./a.out
$ gdb -q ./a.out core -x ./backtrace
Result is
Program terminated with signal SIGABRT, Aborted.
..................................................
4 0x00007f496eb53701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
5 0x00007f496eb53919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
6 0x0000000000400d71 in fatal(std::exception const&) ()
7 0x0000000000400e5b in main ()
I thought rethrowing an exception (by const ref) was a technique to pass the original backtrace; I'm interested in backtracing foo()
, not fatal()
.