OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <dlfcn.h> | |
6 #include <errno.h> | |
7 #include <fcntl.h> | |
8 #include <stdbool.h> | |
9 #include <stdlib.h> | |
10 #include <string.h> | |
11 #include <sys/mman.h> | |
12 #include <unistd.h> | |
13 #include <unwind.h> | |
14 | |
15 #include "tools/android/heap_profiler/heap_profiler.h" | |
16 | |
17 #define HEAP_PROFILER_EXPORT __attribute__((visibility("default"))) | |
18 | |
19 | |
20 static inline __attribute__((always_inline)) | |
21 uint32_t get_backtrace(uintptr_t* frames, uint32_t max_depth); | |
22 | |
23 // Function pointers typedefs for the hooked symbols. | |
24 typedef void* (*mmap_t)(void*, size_t, int, int, int, off_t); | |
25 typedef void* (*mmap2_t)(void*, size_t, int, int, int, off_t); | |
26 typedef void* (*mmap64_t)(void*, size_t, int, int, int, off64_t); | |
27 typedef void* (*mremap_t)(void*, size_t, size_t, unsigned long); | |
28 typedef int (*munmap_t)(void*, size_t); | |
29 typedef void* (*malloc_t)(size_t); | |
30 typedef void* (*calloc_t)(size_t, size_t); | |
31 typedef void* (*realloc_t)(void*, size_t); | |
32 typedef void (*free_t)(void*); | |
33 | |
34 // And their actual definitions. | |
35 static mmap_t real_mmap; | |
36 static mmap2_t real_mmap2; | |
37 static mmap64_t real_mmap64; | |
38 static mremap_t real_mremap; | |
39 static munmap_t real_munmap; | |
40 static malloc_t real_malloc; | |
41 static calloc_t real_calloc; | |
42 static realloc_t real_realloc; | |
43 static free_t real_free; | |
44 static int* has_forked_off_zygote; | |
45 | |
46 HEAP_PROFILER_EXPORT const HeapStats* heap_profiler_stats_for_tests; | |
47 | |
48 // +---------------------------------------------------------------------------+ | |
49 // + Initialization of heap_profiler and lookup of hooks' addresses + | |
50 // +---------------------------------------------------------------------------+ | |
51 __attribute__((constructor)) | |
52 static void initialize() { | |
53 real_mmap = (mmap_t) dlsym(RTLD_NEXT, "mmap"); | |
54 real_mmap2 = (mmap_t) dlsym(RTLD_NEXT, "mmap2"); | |
55 real_mmap64 = (mmap64_t) dlsym(RTLD_NEXT, "mmap64"); | |
56 real_mremap = (mremap_t) dlsym(RTLD_NEXT, "mremap"); | |
57 real_munmap = (munmap_t) dlsym(RTLD_NEXT, "munmap"); | |
58 real_malloc = (malloc_t) dlsym(RTLD_NEXT, "malloc"); | |
59 real_calloc = (calloc_t) dlsym(RTLD_NEXT, "calloc"); | |
60 real_realloc = (realloc_t) dlsym(RTLD_NEXT, "realloc"); | |
61 real_free = (free_t) dlsym(RTLD_NEXT, "free"); | |
62 | |
63 // gMallocLeakZygoteChild is an extra useful piece of information to have. | |
64 // When available, it tels whether we're in the zygote (=0) or forked (=1) | |
pasko
2014/06/20 22:07:18
s/tels/tells/
Primiano Tucci (use gerrit)
2014/06/23 14:00:32
Done.
| |
65 // a child off it. In the worst case it will be NULL and we'll just ignore it. | |
66 has_forked_off_zygote = (int*) dlsym(RTLD_NEXT, "gMallocLeakZygoteChild"); | |
67 | |
68 // Allocate room for the HeapStats area and initialize the heap profiler. | |
69 // Make an explicit map of /dev/zero (instead of MAP_ANONYMOUS), so that the | |
70 // heap_dump tool can easily spot the mapping in the target process. | |
71 int fd = open("/dev/zero", O_RDONLY); | |
72 if (fd < 0) { | |
73 perror("open"); | |
74 abort(); // This world has gone wrong. Good night Vienna. | |
75 } | |
76 | |
77 HeapStats* stats = (HeapStats*) real_mmap( | |
78 0, sizeof(HeapStats), PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
79 heap_profiler_stats_for_tests = stats; | |
80 heap_profiler_init(stats); | |
81 } | |
82 | |
83 static inline __attribute__((always_inline)) void unwind_and_record_alloc( | |
84 void* start, size_t size, uint32_t flags) { | |
85 const int errno_save = errno; | |
86 uintptr_t frames[HEAP_PROFILER_MAX_DEPTH]; | |
87 const uint32_t depth = get_backtrace(frames, HEAP_PROFILER_MAX_DEPTH); | |
88 if (has_forked_off_zygote != NULL && *has_forked_off_zygote == 0) | |
89 flags |= HEAP_PROFILER_FLAGS_IN_ZYGOTE; | |
pasko
2014/06/20 22:07:18
maybe _not_ worth a comment, but please just educa
Primiano Tucci (use gerrit)
2014/06/23 14:00:32
Kind of it. Means that the allocation was made by
| |
90 heap_profiler_alloc(start, size, frames, depth, flags); | |
91 errno = errno_save; | |
92 } | |
93 | |
94 // Flags are non-functional extra decorators that are made available to the | |
95 // final heap_dump tool, to get more details about the source of the allocation. | |
96 static uint32_t get_flags_for_mmap(int fd) { | |
97 return HEAP_PROFILER_FLAGS_MMAP | (fd ? HEAP_PROFILER_FLAGS_MMAP_FILE : 0); | |
98 } | |
99 | |
100 // +---------------------------------------------------------------------------+ | |
101 // + Actual mmap/malloc hooks + | |
102 // +---------------------------------------------------------------------------+ | |
103 HEAP_PROFILER_EXPORT void* mmap( | |
104 void* addr, size_t size, int prot, int flags, int fd, off_t offset) { | |
105 void* ret = real_mmap(addr, size, prot, flags, fd, offset); | |
106 if (ret != NULL) | |
107 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); | |
108 return ret; | |
109 } | |
110 | |
111 HEAP_PROFILER_EXPORT void* mmap2( | |
112 void* addr, size_t size, int prot, int flags, int fd, off_t pgoffset) { | |
113 void* ret = real_mmap2(addr, size, prot, flags, fd, pgoffset); | |
114 if (ret != NULL) | |
115 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); | |
116 return ret; | |
117 } | |
118 | |
119 HEAP_PROFILER_EXPORT void* mmap64( | |
120 void* addr, size_t size, int prot, int flags, int fd, off64_t offset) { | |
121 void* ret = real_mmap64(addr, size, prot, flags, fd, offset); | |
122 if (ret != NULL) | |
123 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); | |
124 return ret; | |
125 } | |
126 | |
127 HEAP_PROFILER_EXPORT void* mremap( | |
128 void* addr, size_t oldlen, size_t newlen, unsigned long flags) { | |
129 void* ret = real_mremap(addr, oldlen, newlen, flags); | |
130 if (ret != NULL) { | |
pasko
2014/06/20 22:07:18
mremap returns MAP_FAILED on error (i.e. -1)
also
Primiano Tucci (use gerrit)
2014/06/23 14:00:32
Good catch, thanks.
Actually all the mmap* functio
| |
131 uint32_t flags = 0; | |
132 if (addr) | |
133 heap_profiler_free(addr, oldlen, &flags); | |
134 if (newlen > 0) | |
135 unwind_and_record_alloc(ret, newlen, flags); | |
136 } | |
137 return ret; | |
138 } | |
139 | |
140 HEAP_PROFILER_EXPORT int munmap(void* ptr, size_t size) { | |
141 int ret = real_munmap(ptr, size); | |
142 heap_profiler_free(ptr, size, /*old_flags=*/NULL); | |
143 return ret; | |
144 } | |
145 | |
146 HEAP_PROFILER_EXPORT void* malloc(size_t byte_count) { | |
147 void* ret = real_malloc(byte_count); | |
148 if (ret != NULL) | |
149 unwind_and_record_alloc(ret, byte_count, HEAP_PROFILER_FLAGS_MALLOC); | |
150 return ret; | |
151 } | |
152 | |
153 HEAP_PROFILER_EXPORT void* calloc(size_t nmemb, size_t size) { | |
154 void* ret = real_calloc(nmemb, size); | |
155 if (ret != NULL) | |
156 unwind_and_record_alloc(ret, nmemb * size, HEAP_PROFILER_FLAGS_MALLOC); | |
157 return ret; | |
158 } | |
159 | |
160 HEAP_PROFILER_EXPORT void* realloc(void* ptr, size_t size) { | |
161 void* ret = real_realloc(ptr, size); | |
162 uint32_t flags = 0; | |
163 if (ptr) | |
164 heap_profiler_free(ptr, 0, &flags); | |
pasko
2014/06/20 22:07:18
this function can also modify errno
Primiano Tucci (use gerrit)
2014/06/23 14:00:32
Right. Introduced wrapper also for heap_profiler_f
| |
165 if (ret != NULL) | |
166 unwind_and_record_alloc(ret, size, flags | HEAP_PROFILER_FLAGS_MALLOC); | |
167 return ret; | |
168 } | |
169 | |
170 HEAP_PROFILER_EXPORT void free(void* ptr) { | |
171 real_free(ptr); | |
172 heap_profiler_free(ptr, 0, /*old_flags=*/NULL); | |
173 } | |
174 | |
175 // +---------------------------------------------------------------------------+ | |
176 // + Stack unwinder + | |
177 // +---------------------------------------------------------------------------+ | |
178 typedef struct { | |
179 uintptr_t* frames; | |
180 uint32_t frame_count; | |
181 uint32_t max_depth; | |
182 bool have_skipped_self; | |
183 } stack_crawl_state_t; | |
184 | |
185 static _Unwind_Reason_Code unwind_fn(struct _Unwind_Context* ctx, void* arg) { | |
186 stack_crawl_state_t* state = (stack_crawl_state_t*) arg; | |
187 uintptr_t ip = _Unwind_GetIP(ctx); | |
188 | |
189 if (ip != 0 && !state->have_skipped_self) { | |
190 state->have_skipped_self = true; | |
191 return _URC_NO_REASON; | |
192 } | |
193 | |
194 state->frames[state->frame_count++] = ip; | |
195 return (state->frame_count >= state->max_depth) ? | |
196 _URC_END_OF_STACK : _URC_NO_REASON; | |
197 } | |
198 | |
199 static uint32_t get_backtrace(uintptr_t* frames, uint32_t max_depth) { | |
200 stack_crawl_state_t state = {.frames = frames, .max_depth = max_depth}; | |
201 _Unwind_Backtrace(unwind_fn, &state); | |
202 return state.frame_count; | |
203 } | |
OLD | NEW |