source: memlog.c @ 14e2ab9

Revision 14e2ab9, 5.0 KB checked in by Hal Finkel <hfinkel@…>, 9 years ago (diff)

rm support for libunwind; only using backtrace() now

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