|
| 1 | +#define _GNU_SOURCE |
| 2 | +#include <dlfcn.h> |
| 3 | +#include <errno.h> |
| 4 | +#include <fcntl.h> |
| 5 | +#include <pthread.h> |
| 6 | +#include <signal.h> |
| 7 | +#include <stddef.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | +#include <sys/syscall.h> |
| 12 | +#include <sys/types.h> |
| 13 | +#include <unistd.h> |
| 14 | + |
| 15 | +static void *(*real_malloc)(size_t) = NULL; |
| 16 | +static void (*real_free)(void *) = NULL; |
| 17 | +static void *(*real_calloc)(size_t, size_t) = NULL; |
| 18 | +static void *(*real_realloc)(void *, size_t) = NULL; |
| 19 | +static int log_fd = -1; |
| 20 | +static pthread_once_t init_once = PTHREAD_ONCE_INIT; |
| 21 | + |
| 22 | +static int is_enabled(void) { |
| 23 | + const char *v = getenv("MALLOC_LOG_ENABLED"); |
| 24 | + return v && v[0] == '1'; |
| 25 | +} |
| 26 | + |
| 27 | +static void init_logger(void) { |
| 28 | + const char *path = getenv("MALLOC_LOG_PATH"); |
| 29 | + if (path == NULL || path[0] == '\0') { |
| 30 | + path = "/tmp/malloc_logger.log"; |
| 31 | + } |
| 32 | + |
| 33 | + log_fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 34 | + real_malloc = dlsym(RTLD_NEXT, "malloc"); |
| 35 | + real_free = dlsym(RTLD_NEXT, "free"); |
| 36 | + real_calloc = dlsym(RTLD_NEXT, "calloc"); |
| 37 | + real_realloc = dlsym(RTLD_NEXT, "realloc"); |
| 38 | + |
| 39 | + // No logging here; deferred until enabled. |
| 40 | +} |
| 41 | + |
| 42 | +__attribute__((constructor)) static void preload_ctor(void) { |
| 43 | + pthread_once(&init_once, init_logger); |
| 44 | +} |
| 45 | + |
| 46 | +__attribute__((destructor)) static void preload_dtor(void) { |
| 47 | + if (log_fd >= 0) { |
| 48 | + close(log_fd); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +static void log_line(const char *tag, size_t size, void *ptr) { |
| 53 | + if (log_fd < 0) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (!is_enabled()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + char buf[200]; |
| 62 | + pid_t pid = getpid(); |
| 63 | + long tid = syscall(SYS_gettid); |
| 64 | + int len; |
| 65 | + |
| 66 | + if (strcmp(tag, "malloc") == 0) { |
| 67 | + len = snprintf(buf, sizeof(buf), "pid=%d tid=%ld malloc size=%zu ptr=%p\n", pid, tid, size, ptr); |
| 68 | + } else if (strcmp(tag, "calloc") == 0) { |
| 69 | + len = snprintf(buf, sizeof(buf), "pid=%d tid=%ld calloc size=%zu ptr=%p\n", pid, tid, size, ptr); |
| 70 | + } else if (strcmp(tag, "realloc") == 0) { |
| 71 | + len = snprintf(buf, sizeof(buf), "pid=%d tid=%ld realloc size=%zu ptr=%p\n", pid, tid, size, ptr); |
| 72 | + } else { // free |
| 73 | + len = snprintf(buf, sizeof(buf), "pid=%d tid=%ld free ptr=%p\n", pid, tid, ptr); |
| 74 | + } |
| 75 | + |
| 76 | + if (len > 0) { |
| 77 | + (void)write(log_fd, buf, (size_t)len); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void *malloc(size_t size) { |
| 82 | + pthread_once(&init_once, init_logger); |
| 83 | + |
| 84 | + if (real_malloc == NULL) { |
| 85 | + errno = ENOMEM; |
| 86 | + return NULL; |
| 87 | + } |
| 88 | + |
| 89 | + void *ptr = real_malloc(size); |
| 90 | + log_line("malloc", size, ptr); |
| 91 | + return ptr; |
| 92 | +} |
| 93 | + |
| 94 | +void free(void *ptr) { |
| 95 | + pthread_once(&init_once, init_logger); |
| 96 | + |
| 97 | + if (real_free == NULL) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + log_line("free", 0, ptr); |
| 102 | + real_free(ptr); |
| 103 | +} |
| 104 | + |
| 105 | +void *calloc(size_t nmemb, size_t size) { |
| 106 | + pthread_once(&init_once, init_logger); |
| 107 | + |
| 108 | + if (real_calloc == NULL) { |
| 109 | + errno = ENOMEM; |
| 110 | + return NULL; |
| 111 | + } |
| 112 | + |
| 113 | + void *ptr = real_calloc(nmemb, size); |
| 114 | + log_line("calloc", nmemb * size, ptr); |
| 115 | + return ptr; |
| 116 | +} |
| 117 | + |
| 118 | +void *realloc(void *ptr, size_t size) { |
| 119 | + pthread_once(&init_once, init_logger); |
| 120 | + |
| 121 | + if (real_realloc == NULL) { |
| 122 | + errno = ENOMEM; |
| 123 | + return NULL; |
| 124 | + } |
| 125 | + |
| 126 | + void *new_ptr = real_realloc(ptr, size); |
| 127 | + log_line("realloc", size, new_ptr); |
| 128 | + return new_ptr; |
| 129 | +} |
0 commit comments