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 tells whether we're in the zygote (=0) or forked (=1) |
| 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; |
| 90 heap_profiler_alloc(start, size, frames, depth, flags); |
| 91 errno = errno_save; |
| 92 } |
| 93 |
| 94 static inline __attribute__((always_inline)) void discard_alloc( |
| 95 void* start, size_t size, uint32_t* old_flags) { |
| 96 const int errno_save = errno; |
| 97 heap_profiler_free(start, size, old_flags); |
| 98 errno = errno_save; |
| 99 } |
| 100 |
| 101 // Flags are non-functional extra decorators that are made available to the |
| 102 // final heap_dump tool, to get more details about the source of the allocation. |
| 103 static uint32_t get_flags_for_mmap(int fd) { |
| 104 return HEAP_PROFILER_FLAGS_MMAP | (fd ? HEAP_PROFILER_FLAGS_MMAP_FILE : 0); |
| 105 } |
| 106 |
| 107 // +---------------------------------------------------------------------------+ |
| 108 // + Actual mmap/malloc hooks + |
| 109 // +---------------------------------------------------------------------------+ |
| 110 HEAP_PROFILER_EXPORT void* mmap( |
| 111 void* addr, size_t size, int prot, int flags, int fd, off_t offset) { |
| 112 void* ret = real_mmap(addr, size, prot, flags, fd, offset); |
| 113 if (ret != MAP_FAILED) |
| 114 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); |
| 115 return ret; |
| 116 } |
| 117 |
| 118 HEAP_PROFILER_EXPORT void* mmap2( |
| 119 void* addr, size_t size, int prot, int flags, int fd, off_t pgoffset) { |
| 120 void* ret = real_mmap2(addr, size, prot, flags, fd, pgoffset); |
| 121 if (ret != MAP_FAILED) |
| 122 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); |
| 123 return ret; |
| 124 } |
| 125 |
| 126 HEAP_PROFILER_EXPORT void* mmap64( |
| 127 void* addr, size_t size, int prot, int flags, int fd, off64_t offset) { |
| 128 void* ret = real_mmap64(addr, size, prot, flags, fd, offset); |
| 129 if (ret != MAP_FAILED) |
| 130 unwind_and_record_alloc(ret, size, get_flags_for_mmap(fd)); |
| 131 return ret; |
| 132 } |
| 133 |
| 134 HEAP_PROFILER_EXPORT void* mremap( |
| 135 void* addr, size_t oldlen, size_t newlen, unsigned long flags) { |
| 136 void* ret = real_mremap(addr, oldlen, newlen, flags); |
| 137 if (ret != MAP_FAILED) { |
| 138 uint32_t flags = 0; |
| 139 if (addr) |
| 140 discard_alloc(addr, oldlen, &flags); |
| 141 if (newlen > 0) |
| 142 unwind_and_record_alloc(ret, newlen, flags); |
| 143 } |
| 144 return ret; |
| 145 } |
| 146 |
| 147 HEAP_PROFILER_EXPORT int munmap(void* ptr, size_t size) { |
| 148 int ret = real_munmap(ptr, size); |
| 149 discard_alloc(ptr, size, /*old_flags=*/NULL); |
| 150 return ret; |
| 151 } |
| 152 |
| 153 HEAP_PROFILER_EXPORT void* malloc(size_t byte_count) { |
| 154 void* ret = real_malloc(byte_count); |
| 155 if (ret != NULL) |
| 156 unwind_and_record_alloc(ret, byte_count, HEAP_PROFILER_FLAGS_MALLOC); |
| 157 return ret; |
| 158 } |
| 159 |
| 160 HEAP_PROFILER_EXPORT void* calloc(size_t nmemb, size_t size) { |
| 161 void* ret = real_calloc(nmemb, size); |
| 162 if (ret != NULL) |
| 163 unwind_and_record_alloc(ret, nmemb * size, HEAP_PROFILER_FLAGS_MALLOC); |
| 164 return ret; |
| 165 } |
| 166 |
| 167 HEAP_PROFILER_EXPORT void* realloc(void* ptr, size_t size) { |
| 168 void* ret = real_realloc(ptr, size); |
| 169 uint32_t flags = 0; |
| 170 if (ptr) |
| 171 discard_alloc(ptr, 0, &flags); |
| 172 if (ret != NULL) |
| 173 unwind_and_record_alloc(ret, size, flags | HEAP_PROFILER_FLAGS_MALLOC); |
| 174 return ret; |
| 175 } |
| 176 |
| 177 HEAP_PROFILER_EXPORT void free(void* ptr) { |
| 178 real_free(ptr); |
| 179 discard_alloc(ptr, 0, /*old_flags=*/NULL); |
| 180 } |
| 181 |
| 182 // +---------------------------------------------------------------------------+ |
| 183 // + Stack unwinder + |
| 184 // +---------------------------------------------------------------------------+ |
| 185 typedef struct { |
| 186 uintptr_t* frames; |
| 187 uint32_t frame_count; |
| 188 uint32_t max_depth; |
| 189 bool have_skipped_self; |
| 190 } stack_crawl_state_t; |
| 191 |
| 192 static _Unwind_Reason_Code unwind_fn(struct _Unwind_Context* ctx, void* arg) { |
| 193 stack_crawl_state_t* state = (stack_crawl_state_t*) arg; |
| 194 uintptr_t ip = _Unwind_GetIP(ctx); |
| 195 |
| 196 if (ip != 0 && !state->have_skipped_self) { |
| 197 state->have_skipped_self = true; |
| 198 return _URC_NO_REASON; |
| 199 } |
| 200 |
| 201 state->frames[state->frame_count++] = ip; |
| 202 return (state->frame_count >= state->max_depth) ? |
| 203 _URC_END_OF_STACK : _URC_NO_REASON; |
| 204 } |
| 205 |
| 206 static uint32_t get_backtrace(uintptr_t* frames, uint32_t max_depth) { |
| 207 stack_crawl_state_t state = {.frames = frames, .max_depth = max_depth}; |
| 208 _Unwind_Backtrace(unwind_fn, &state); |
| 209 return state.frame_count; |
| 210 } |
OLD | NEW |