| 1 | #define _GNU_SOURCE |
| 2 | #include <dlfcn.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #include "marker.h" |
| 7 | |
| 8 | //INTERCEPT_PROTOTYPE(void, malloc, size_t size) |
| 9 | //INTERCEPT_TRACE("size %d", size) |
| 10 | //INTERCEPT_CALL_ARGS(size) |
| 11 | //INTERCEPT() |
| 12 | // |
| 13 | //#define INTERCEPT_FUNC(type, name, args...) \ |
| 14 | //__I_FUNC_TYPE(type) \ |
| 15 | //__I_FUNC_NAME(name) \ |
| 16 | //__I_FUNC_ARGS(args) |
| 17 | // |
| 18 | //#define INTERCEPT_TRACE(fmt, args...) \ |
| 19 | //#define __I_TRACE_FMT fmt \ |
| 20 | //#define __I_TRACE_ARGS args |
| 21 | // |
| 22 | //#define INTERCEPT_CALL_ARGS(args...) \ |
| 23 | //#define __I_CALL_ARGS args |
| 24 | // |
| 25 | //#define INTERCEPT() \ |
| 26 | //__I_FUNC_TYPE __I_FUNC_NAME(__I_FUNC_ARGS) \ |
| 27 | //{ \ |
| 28 | // static __I_FUNC_TYPE (*plibc_ ## __I_FUNC_NAME)(args) = NULL; \ |
| 29 | // \ |
| 30 | // if(plibc_ ## __I_FUNC_NAME == NULL) { \ |
| 31 | // plibc_ ## __I_FUNC_NAME = dlsym(RTLD_NEXT, "malloc"); \ |
| 32 | // if(plibc_ ## __I_FUNC_NAME == NULL) { \ |
| 33 | // fprintf(stderr, "mallocwrap: unable to find malloc\n"); \ |
| 34 | // return NULL; \ |
| 35 | // } \ |
| 36 | // } \ |
| 37 | // \ |
| 38 | // trace_mark(ust, __I_FUNC_NAME, __I_TRACE_FMT, __I_TRACE_ARGS); \ |
| 39 | // \ |
| 40 | // return plibc_ ## __I_FUNC_NAME (__I_CALL_ARGS); \ |
| 41 | //} |
| 42 | |
| 43 | void *malloc(size_t size) |
| 44 | { |
| 45 | static void *(*plibc_malloc)(size_t size) = NULL; |
| 46 | |
| 47 | void *retval; |
| 48 | |
| 49 | if(plibc_malloc == NULL) { |
| 50 | plibc_malloc = dlsym(RTLD_NEXT, "malloc"); |
| 51 | if(plibc_malloc == NULL) { |
| 52 | fprintf(stderr, "mallocwrap: unable to find malloc\n"); |
| 53 | return NULL; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | retval = plibc_malloc(size); |
| 58 | |
| 59 | trace_mark(ust, malloc, "size %d ptr %p", (int)size, retval); |
| 60 | |
| 61 | return retval; |
| 62 | } |
| 63 | |
| 64 | void free(void *ptr) |
| 65 | { |
| 66 | static void *(*plibc_free)(void *ptr) = NULL; |
| 67 | |
| 68 | if(plibc_free == NULL) { |
| 69 | plibc_free = dlsym(RTLD_NEXT, "free"); |
| 70 | if(plibc_free == NULL) { |
| 71 | fprintf(stderr, "mallocwrap: unable to find free\n"); |
| 72 | return NULL; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | trace_mark(ust, free, "%p", ptr); |
| 77 | |
| 78 | return plibc_free(ptr); |
| 79 | } |
| 80 | |
| 81 | MARKER_LIB |