(nonportable) print stack trace when enforcer fires

.. also, print all messages to stderr instead of stdout

backtrace() and backtrace_symbols_fd() are GNU extensions in libc
enabled by _GNU_SOURCE
This commit is contained in:
Jeff Epler 2012-09-03 21:29:29 -05:00
parent 509b0c58b5
commit 3b4e63a696

View file

@ -34,6 +34,9 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <execinfo.h>
#include "tmperamental.h"
@ -55,9 +58,16 @@ static void tmperamental_init ( void ) {
orig_freopen = dlsym(RTLD_NEXT, "freopen");
}
#define SIZE (100)
#define FIRST_FRAME (1)
void enforcer ( const char * pathname ) {
if ( strncmp("/tmp/", pathname, 5) == 0 ) {
printf("tmperamental: caught a write to /tmp.\n");
int nframes;
void *buffer[SIZE];
fprintf(stderr, "tmperamental: caught a write to /tmp.\n");
nframes = backtrace(buffer, SIZE);
if(nframes > FIRST_FRAME)
backtrace_symbols_fd(buffer+FIRST_FRAME, nframes+FIRST_FRAME, 2);
abort();
}
}