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