thesis: Complete 2.2
This commit is contained in:
35
thesis/listings/intercept-preload.c
Normal file
35
thesis/listings/intercept-preload.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
static void *(*__real_malloc)(size_t);
|
||||
|
||||
static int mode = 0;
|
||||
|
||||
static void fin(void) {
|
||||
if (mode > 0) fprintf(stderr, "intercept: stopped\n");
|
||||
mode = 0;
|
||||
}
|
||||
|
||||
static void init(void) {
|
||||
if (mode) return;
|
||||
mode = -1;
|
||||
if (((__real_malloc) = dlsym(RTLD_NEXT, "malloc")) == NULL) {
|
||||
fprintf(stderr, "intercept: unable to load symbol '%s': %s",
|
||||
"malloc", strerror(errno));
|
||||
return;
|
||||
}
|
||||
atexit(fin);
|
||||
fprintf(stderr, "intercept: intercepting function calls\n");
|
||||
mode = 1;
|
||||
}
|
||||
|
||||
void *malloc(size_t size) {
|
||||
init();
|
||||
// before call to malloc
|
||||
void *ret = __real_malloc(size);
|
||||
// after call to malloc
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user