I was trying to see if SIGSEGV signal hander could help to deal with unhandled exception of C++, I experiment it:
#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void handler(int sig) {
void *array[10];
size_t size = backtrace(array, 10);
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
int main()
{
signal(SIGSEGV, handler);
throw 1;
}
$ g++ -g h.cpp -rdynamic && ./a.out
terminate called after throwing an instance of 'int' Aborted (Core dump)
Well, the program doesn't print crash call stack back trace as I expected. My question is:
As long as it terminates, does it through out any signals like SIGSEGV? Is there a system call or posix api that could catch even C++ exceptions and prints out the call stack?
Thanks!