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 <fcntl.h> | |
7 #include <stdbool.h> | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #include <sys/mman.h> | |
11 #include <unistd.h> | |
12 #include <unwind.h> | |
13 | |
14 #include "tools/android/heap_profiler/heap_profiler.h" | |
15 | |
16 | |
17 static int get_backtrace(uintptr_t* frames, size_t max_depth); | |
18 | |
19 // Function pointers typedefs for the hooked symbols. | |
20 typedef void* (*mmap_t)(void*, size_t, int, int, int, off_t); | |
21 typedef void* (*mmap2_t)(void*, size_t, int, int, int, off_t); | |
22 typedef void* (*mmap64_t)(void*, size_t, int, int, int, off64_t); | |
23 typedef void* (*mremap_t)(void*, size_t, size_t, unsigned long); | |
24 typedef int (*munmap_t)(void*, size_t); | |
25 typedef void* (*malloc_t)(size_t); | |
26 typedef void* (*calloc_t)(size_t, size_t); | |
27 typedef void* (*realloc_t)(void*, size_t); | |
28 typedef void (*free_t)(void*); | |
29 | |
30 // And their actual definitions. | |
pasko
2014/06/10 16:59:52
s/definitions/declarations/
Primiano Tucci (use gerrit)
2014/06/19 12:27:18
Well, technically speaking this is a variable defi
| |
31 static mmap_t real_mmap; | |
32 static mmap2_t real_mmap2; | |
33 static mmap64_t real_mmap64; | |
34 static mremap_t real_mremap; | |
35 static munmap_t real_munmap; | |
36 static malloc_t real_malloc; | |
37 static calloc_t real_calloc; | |
38 static realloc_t real_realloc; | |
39 static free_t real_free; | |
40 static int* has_forked_off_zygote; | |
41 | |
42 // +---------------------------------------------------------------------------+ | |
43 // + Initialization of heap_profiler and lookup of hooks' addresses + | |
44 // +---------------------------------------------------------------------------+ | |
45 __attribute__((constructor)) | |
46 static void initialize() { | |
47 real_mmap = (mmap_t) dlsym(RTLD_NEXT, "mmap"); | |
48 real_mmap2 = (mmap_t) dlsym(RTLD_NEXT, "mmap2"); | |
49 real_mmap64 = (mmap64_t) dlsym(RTLD_NEXT, "mmap64"); | |
50 real_mremap = (mremap_t) dlsym(RTLD_NEXT, "mremap"); | |
51 real_munmap = (munmap_t) dlsym(RTLD_NEXT, "munmap"); | |
52 real_malloc = (malloc_t) dlsym(RTLD_NEXT, "malloc"); | |
53 real_calloc = (calloc_t) dlsym(RTLD_NEXT, "calloc"); | |
54 real_realloc = (realloc_t) dlsym(RTLD_NEXT, "realloc"); | |
55 real_free = (free_t) dlsym(RTLD_NEXT, "free"); | |
56 | |
57 // gMallocLeakZygoteChild is an extra useful piece of information to have. | |
58 // When available, it tels whether we're in the zygote (=0) or forked (=1) | |
59 // a child off it. In the worst case it will be NULL and we'll just ignore it. | |
60 has_forked_off_zygote = (int*) dlsym(RTLD_NEXT, "gMallocLeakZygoteChild"); | |
61 | |
62 // Allocate room for the HeapStats area and initialize the heap profiler. | |
63 // Make an explicit map of /dev/zero (instead of MAP_ANONYMOUS), so that the | |
64 // hdump tool can easily spot the mapping in the target process. | |
65 int fd = open("/dev/zero", O_RDONLY); | |
66 if (fd < 0) | |
67 abort(); // This world has gone wrong. Good night Vienna. | |
pasko
2014/06/10 16:59:52
please do perror("open") before aborting to print
Primiano Tucci (use gerrit)
2014/06/19 12:27:18
Done.
| |
68 heap_profiler_init((HeapStats*) real_mmap( | |
69 0, sizeof(HeapStats), PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)); | |
70 } | |
71 | |
72 static void unwind_and_record_alloc(void* start, size_t size, uint32_t flags) { | |
73 uintptr_t frames[HEAP_PROFILER_MAX_DEPTH]; | |
74 const size_t depth = get_backtrace(frames, HEAP_PROFILER_MAX_DEPTH); | |
pasko
2014/06/10 16:59:52
this function returns int, then converts to size_t
Primiano Tucci (use gerrit)
2014/06/19 12:27:18
Done.
| |
75 if (has_forked_off_zygote != NULL && *has_forked_off_zygote == 0) | |
76 flags |= HEAP_PROFILER_FLAGS_IN_ZYGOTE; | |
77 heap_profiler_alloc(start, size, frames, depth, flags); | |
78 } | |
79 | |
80 static uint32_t get_flags_for_mmap(int fd) { | |
pasko
2014/06/10 16:59:52
it is worth a comment:
1. what flags==0 means
2. w
Primiano Tucci (use gerrit)
2014/06/19 12:27:18
Added a comment to explain what flags are.
| |
81 return HEAP_PROFILER_FLAGS_MMAP | (fd ? HEAP_PROFILER_FLAGS_MMAP_FILE : 0); | |
82 } | |
83 | |
84 // +---------------------------------------------------------------------------+ | |
85 // + Actual mmap/malloc hooks + | |
86 // +---------------------------------------------------------------------------+ | |
87 HEAP_PROFILER_EXPORT void* mmap( | |
88 void* addr, size_t size, int prot, int flags, int fd, off_t offset) { | |
89 void* ret = real_mmap(addr, size, prot, flags, fd, offset); | |
90 if (ret != NULL) | |
91 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); | |
pasko
2014/06/10 16:59:52
I did not check properly, but it seems this functi
Primiano Tucci (use gerrit)
2014/06/19 12:27:18
This is a very good catch. I didn't think about er
| |
92 return ret; | |
93 } | |
94 | |
95 HEAP_PROFILER_EXPORT void* mmap2( | |
96 void* addr, size_t size, int prot, int flags, int fd, off_t pgoffset) { | |
97 void* ret = real_mmap2(addr, size, prot, flags, fd, pgoffset); | |
98 if (ret != NULL) | |
99 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); | |
100 return ret; | |
101 } | |
102 | |
103 HEAP_PROFILER_EXPORT void* mmap64( | |
104 void* addr, size_t size, int prot, int flags, int fd, off64_t offset) { | |
105 void* ret = real_mmap64(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* mremap( | |
112 void* addr, size_t oldlen, size_t newlen, unsigned long flags) { | |
113 void* ret = real_mremap(addr, oldlen, newlen, flags); | |
114 if (ret != NULL) { | |
115 uint32_t flags = 0; | |
116 if (addr) | |
117 heap_profiler_free(addr, oldlen, &flags); | |
118 if (newlen > 0) | |
119 unwind_and_record_alloc(ret, newlen, flags); | |
120 } | |
121 return ret; | |
122 } | |
123 | |
124 HEAP_PROFILER_EXPORT int munmap( | |
125 void* ptr, size_t size) { | |
126 int ret = real_munmap(ptr, size); | |
127 heap_profiler_free(ptr, size, /*old_flags=*/NULL); | |
128 return ret; | |
129 } | |
130 | |
131 HEAP_PROFILER_EXPORT void* malloc( | |
132 size_t byte_count) { | |
133 void* ret = real_malloc(byte_count); | |
134 if (ret != NULL) | |
135 unwind_and_record_alloc(ret, byte_count, HEAP_PROFILER_FLAGS_MALLOC); | |
136 return ret; | |
137 } | |
138 | |
139 HEAP_PROFILER_EXPORT void* calloc( | |
140 size_t nmemb, size_t size) { | |
141 void* ret = real_calloc(nmemb, size); | |
142 if (ret != NULL) | |
143 unwind_and_record_alloc(ret, nmemb * size, HEAP_PROFILER_FLAGS_MALLOC); | |
144 return ret; | |
145 } | |
146 | |
147 HEAP_PROFILER_EXPORT void* realloc( | |
148 void* ptr, size_t size) { | |
149 void* ret = real_realloc(ptr, size); | |
150 uint32_t flags = 0; | |
151 if (ptr) | |
152 heap_profiler_free(ptr, 0, &flags); | |
153 if (ret != NULL) | |
154 unwind_and_record_alloc(ret, size, flags | HEAP_PROFILER_FLAGS_MALLOC); | |
155 return ret; | |
156 } | |
157 | |
158 HEAP_PROFILER_EXPORT void free( | |
159 void* ptr) { | |
160 real_free(ptr); | |
161 heap_profiler_free(ptr, 0, /*old_flags=*/NULL); | |
162 } | |
163 | |
164 // +---------------------------------------------------------------------------+ | |
165 // + Stack unwinder + | |
166 // +---------------------------------------------------------------------------+ | |
167 typedef struct { | |
168 uintptr_t* frames; | |
169 size_t frame_count; | |
170 size_t max_depth; | |
171 bool have_skipped_self; | |
172 } stack_crawl_state_t; | |
173 | |
174 static _Unwind_Reason_Code unwind_fn(struct _Unwind_Context* ctx, void* arg) { | |
175 stack_crawl_state_t* state = (stack_crawl_state_t*) arg; | |
176 uintptr_t ip = _Unwind_GetIP(ctx); | |
177 | |
178 if (ip != 0 && !state->have_skipped_self) { | |
179 state->have_skipped_self = true; | |
pasko
2014/06/10 16:59:52
Is this to skip the get_backtrace() from the trace
Primiano Tucci (use gerrit)
2014/06/19 12:27:18
The latter.
| |
180 return _URC_NO_REASON; | |
181 } | |
182 | |
183 state->frames[state->frame_count++] = ip; | |
184 return (state->frame_count >= state->max_depth) ? | |
185 _URC_END_OF_STACK : _URC_NO_REASON; | |
186 } | |
187 | |
188 static int get_backtrace(uintptr_t* frames, size_t max_depth) { | |
189 stack_crawl_state_t state = {.frames = frames, .max_depth = max_depth}; | |
pasko
2014/06/10 16:59:52
I ❤ C
| |
190 _Unwind_Backtrace(unwind_fn, &state); | |
191 return state.frame_count; | |
192 } | |
OLD | NEW |