My C++ program has an embedded web server (based on CivetWeb). If an exception occurs while handling an HTTP request, I'd like to not crash, but I'd also like to trigger a core dump for future debugging.
Here's my current attempt.
// See http://stackoverflow.com/a/131539/25507
void create_dump(void)
{
if(!fork()) {
abort() || (*((void*)0) = 42);
}
}
void HandleHttpRequest(mg_conn *conn)
{
try {
// Lots of application-specific logic
} catch (std::exception& e) {
create_dump();
WriteHttp500(conn);
}
}
This is mostly working. However, if I then bring up gdb to do a postmortem debugging session with the core dump, the stack trace is at the create_dump()
line of HandleHttpRequest
.
If I have a core dump of a catch
block, is there any way to view the stack trace that threw the exception?
Or is there some better way of accomplishing my goal (of automatically turning uncaught exceptions into an HTTP 500 error code while also capturing full debugging information for them)?