I can make use of gcc's backtrace to obtain a stack trace at any given point of a program, but I would like to obtain the trace from whatever frame the stack was in at the time an exception is thrown, ie prior to the stack unwinding.
For instance, the following block
func() {
throw std::exception();
}
try {
func();
}
catch ( std::exception ) {
std::cout << print_trace();
//do stuff
}
ought to still be able to retain a frame for func() somehow.
This has been asked before, but it involved an unhandled exception that would terminate the program and presumably didn't give the callstack a chance to unwind?
Is there a way to do this while still being able to catch and handle the exception normally?
There could be an approach like having a handler for all exceptions that do nothing but generate the trace and re-throw the exceptions. Ideally I should be able to generate traces within Exception class constructors, but here I do not necessarily have control over the exceptions that could be encountered.