Skip to content

Instantly share code, notes, and snippets.

@jcaesar
Last active February 10, 2021 10:08
Show Gist options
  • Select an option

  • Save jcaesar/cf5001cd4d65dbde024861726aa850ac to your computer and use it in GitHub Desktop.

Select an option

Save jcaesar/cf5001cd4d65dbde024861726aa850ac to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstdlib>
#include <execinfo.h>
#include <iostream>
#include <stdexcept>
#include <unistd.h>
void exhnd() {
fprintf(stderr, __FILE__ ": Custom exception handler invoked in pid %d\n", getpid());
void *trace_elems[20];
int trace_elem_count(backtrace(trace_elems, 20));
char **stack_syms(backtrace_symbols(trace_elems, trace_elem_count));
for (int i = 0; i < trace_elem_count; ++i) {
std::cout << stack_syms[i] << "\n";
}
free(stack_syms);
exit(1);
}
extern "C" {
static void con() __attribute__((constructor));
void con() {
std::set_terminate(exhnd);
fprintf(stderr, __FILE__ ": Custom exception handler installed in pid %d\n", getpid());
}
}
/*
The problem:
terminate called after throwing an instance of 'std::system_error'
what(): Resource temporarily unavailable
The solution:
Compile with: c++ -shared -o cat.so -fPIC cat-exception.cpp (or similar)
Before running the target: export LD_PRELOAD="$(realpath cat.so)"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment