Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwoc committed Dec 5, 2013
1 parent 29cb284 commit c16b696
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -11,3 +11,5 @@
*.lai
*.la
*.a

handlertest
2 changes: 2 additions & 0 deletions Makefile
@@ -0,0 +1,2 @@
all:
g++ -O3 -std=c++0x -Wall -rdynamic -o handlertest main.cpp
69 changes: 69 additions & 0 deletions main.cpp
@@ -0,0 +1,69 @@
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <exception>
#include <stdexcept>

#include <execinfo.h>
#include <signal.h>
#include <unistd.h>

void printStacktrace()
{
void *array[20];
size_t size = backtrace(array, sizeof(array) / sizeof(array[0]));
backtrace_symbols_fd(array, size, STDERR_FILENO);
}

void signalHandler(int sig)
{
std::fprintf(stderr, "Error: signal %d\n", sig);
printStacktrace();
std::abort();
}

void terminateHandler()
{
std::exception_ptr exptr = std::current_exception();
if (exptr != 0)
{
// the only useful feature of std::exception_ptr is that it can be rethrown...
try
{
std::rethrow_exception(exptr);
}
catch (std::exception &ex)
{
std::fprintf(stderr, "Terminated due to exception: %s\n", ex.what());
}
catch (...)
{
std::fprintf(stderr, "Terminated due to unknown exception\n");
}
}
else
{
std::fprintf(stderr, "Terminated due to unknown reason :(\n");
}
printStacktrace();
std::abort();
}

int main()
{
signal(SIGSEGV, signalHandler);
std::set_terminate(terminateHandler);

srand(time(NULL));
if (rand() % 2)
{
// segfault
int *bad = NULL;
std::fprintf(stderr, "%d", *bad);
}
else
{
// exception
throw std::runtime_error("Hello, world!");
}
}

0 comments on commit c16b696

Please sign in to comment.