source: memlog.c @ 1e5cce6

Revision 1e5cce6, 5.8 KB checked in by Hal Finkel <hfinkel@…>, 9 years ago (diff)

no backtrace on free -- that is a waste of time/space

  • Property mode set to 100644
Line 
1#ifndef _GNU_SOURCE
2#define _GNU_SOURCE
3#endif
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <limits.h>
8#include <string.h>
9
10#include <malloc.h>
11#include <execinfo.h>
12#include <sys/syscall.h>
13#include <sys/time.h>
14#include <sys/resource.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <sys/utsname.h>
18#include <fcntl.h>
19#include <unistd.h>
20
21#include <pthread.h>
22
23#include <dlfcn.h>
24
25// NOTE: When static linking, this depends on linker wrapping.
26// Add to your LDFLAGS:
27//   -Wl,--wrap,malloc,--wrap,free,--wrap,realloc,--wrap,calloc,--wrap,memalign /path/to/memlog_s.o -lpthread -ldl
28
29FILE *log_file = NULL;
30static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
31
32// The malloc hook might use functions that call malloc, and we need to make
33// sure this does not cause an infinite loop.
34static __thread int in_malloc = 0;
35static char self_path[PATH_MAX+1] = { '\0' };
36
37__attribute__((__constructor__))
38static void record_init() {
39  struct utsname u;
40  uname(&u);
41
42  char log_name[PATH_MAX+1];
43  snprintf(log_name, PATH_MAX+1, "%s.%d.memlog", u.nodename, getpid());
44  log_file = fopen(log_name, "w");
45  if (!log_file)
46    fprintf(stderr, "fopen failed for '%s': %m\n", log_name);
47
48  const char *link_name = "/proc/self/exe";
49  readlink(link_name, self_path, PATH_MAX);
50}
51
52__attribute__((__destructor__))
53static void record_cleanup() {
54  if (!log_file)
55    return;
56
57  // These functions might call free, but we're shutting down, so don't try to
58  // unwind the stack from here...
59  in_malloc = 1;
60
61  (void) fflush(log_file);
62  (void) fclose(log_file);
63}
64
65static void print_context(const void *caller, int show_backtrace) {
66  struct rusage usage;
67  if (getrusage(RUSAGE_SELF, &usage)) {
68    fprintf(stderr, "getrusage failed: %m\n");
69    return;
70  }
71
72  fprintf(log_file, "\t%ld.%06ld %ld %ld", usage.ru_utime.tv_sec,
73          usage.ru_utime.tv_usec, usage.ru_maxrss, syscall(SYS_gettid));
74
75  if (!show_backtrace)
76    return;
77
78  void *pcs[1024];
79  int num_pcs = backtrace(pcs, 1024);
80
81  int found_caller = 0;
82  caller =  __builtin_extract_return_addr((void*) caller);
83  for (int pci = 0; pci < num_pcs; ++pci) {
84    intptr_t pc = (intptr_t) pcs[pci];
85
86    if (!pc)
87      break;
88
89    if (!found_caller) {
90      if (pc != (intptr_t) caller)
91        continue;
92
93      found_caller = 1;
94    }
95
96    intptr_t off, relpc;
97    const char *proc_name;
98    const char *file_name;
99    Dl_info dlinfo;
100    if (dladdr((void *) pc, &dlinfo) && dlinfo.dli_fname &&
101        *dlinfo.dli_fname) {
102      intptr_t saddr = (intptr_t) dlinfo.dli_saddr;
103      if (saddr) {
104#if defined(__powerpc64__) && !defined(__powerpc64le__)
105        // On PPC64 ELFv1, the symbol address points to the function descriptor, not
106        // the actual starting address.
107        saddr = *(intptr_t*) saddr;
108#endif
109
110        off = pc - saddr;
111        relpc = pc - ((intptr_t) dlinfo.dli_fbase);
112      } else {
113        off = 0;
114        relpc = 0;
115      }
116
117      proc_name = dlinfo.dli_sname;
118      if (!proc_name)
119        proc_name = "?";
120
121      file_name = dlinfo.dli_fname;
122    } else {
123      // We don't know these...
124      off = 0;
125      relpc = 0;
126      proc_name = "?";
127
128      // If we can't determine the file, assume it is the base executable
129      // (which does the right thing for statically-linked binaries).
130      file_name = self_path;
131    }
132
133    fprintf(log_file, "\t%s (%s+0x%x) [0x%lx (0x%lx)]", file_name, proc_name, (int) off,
134            (long) pc, (long) relpc);
135  }
136}
137
138static void record_malloc(size_t size, void *ptr, const void *caller) {
139  if (!log_file)
140    return;
141
142  if (pthread_mutex_lock(&log_mutex))
143    return;
144
145  fprintf(log_file, "M: %zd %p", size, ptr);
146  print_context(caller, 1);
147  fprintf(log_file, "\n");
148
149done:
150  pthread_mutex_unlock(&log_mutex);
151}
152
153static void record_free(void *ptr, const void *caller) {
154  if (!log_file)
155    return;
156
157  if (pthread_mutex_lock(&log_mutex))
158    return;
159
160  fprintf(log_file, "F: %p", ptr);
161  print_context(caller, 0);
162  fprintf(log_file, "\n");
163
164done:
165  pthread_mutex_unlock(&log_mutex);
166}
167
168// glibc exports its underlying malloc implementation under the name
169// __libc_malloc so that hooks like this can use it.
170extern void *__libc_malloc(size_t size);
171extern void *__libc_realloc(void *ptr, size_t size);
172extern void *__libc_calloc(size_t nmemb, size_t size);
173extern void *__libc_memalign(size_t boundary, size_t size);
174extern void __libc_free(void *ptr);
175
176#ifdef __PIC__
177#define FUNC(x) x
178#else
179#define FUNC(x) __wrap_ ## x
180#endif
181
182void *FUNC(malloc)(size_t size) {
183  const void *caller = __builtin_return_address(0);
184
185  if (in_malloc)
186    return __libc_malloc(size);
187
188  in_malloc = 1;
189
190  void *ptr = __libc_malloc(size);
191
192  record_malloc(size, ptr, caller);
193
194  in_malloc = 0;
195  return ptr;
196}
197
198void *FUNC(realloc)(void *ptr, size_t size) {
199  const void *caller = __builtin_return_address(0);
200
201  if (in_malloc)
202    return __libc_realloc(ptr, size);
203
204  in_malloc = 1;
205
206  void *nptr = __libc_realloc(ptr, size);
207
208  if (ptr)
209    record_free(ptr, caller);
210  record_malloc(size, nptr, caller);
211
212  in_malloc = 0;
213
214  return nptr;
215}
216
217void *FUNC(calloc)(size_t nmemb, size_t size) {
218  const void *caller = __builtin_return_address(0);
219
220  if (in_malloc)
221    return __libc_calloc(nmemb, size);
222
223  in_malloc = 1;
224
225  void *ptr = __libc_calloc(nmemb, size);
226
227  record_malloc(nmemb*size, ptr, caller);
228
229  in_malloc = 0;
230
231  return ptr;
232}
233
234void *FUNC(memalign)(size_t boundary, size_t size) {
235  const void *caller = __builtin_return_address(0);
236
237  if (in_malloc)
238    return __libc_memalign(boundary, size);
239
240  in_malloc = 1;
241
242  void *ptr = __libc_memalign(boundary, size);
243
244  record_malloc(size, ptr, caller);
245
246  in_malloc = 0;
247
248  return ptr;
249}
250
251void FUNC(free)(void *ptr) {
252  const void *caller = __builtin_return_address(0);
253
254  if (in_malloc || !ptr)
255    return __libc_free(ptr);
256
257  in_malloc = 1;
258
259  record_free(ptr, caller);
260
261  __libc_free(ptr);
262
263  in_malloc = 0;
264}
265
Note: See TracBrowser for help on using the repository browser.