Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "debug_stacktrace.h" |
| 30 | |
| 31 | #include <dlfcn.h> |
Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 32 | #include <inttypes.h> |
Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 33 | #include <malloc.h> |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | #include <unwind.h> |
| 36 | #include <sys/types.h> |
| 37 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 38 | #include "debug_mapinfo.h" |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 39 | #include "malloc_debug_disable.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 40 | #include "private/libc_logging.h" |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 41 | |
Elliott Hughes | ba76572 | 2014-02-25 15:32:01 -0800 | [diff] [blame] | 42 | #if defined(__LP64__) |
| 43 | #define PAD_PTR "016" PRIxPTR |
| 44 | #else |
| 45 | #define PAD_PTR "08" PRIxPTR |
| 46 | #endif |
| 47 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 48 | typedef struct _Unwind_Context __unwind_context; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 50 | static mapinfo_t* g_map_info = NULL; |
| 51 | static void* g_demangler; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 52 | typedef char* (*DemanglerFn)(const char*, char*, size_t*, int*); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 53 | static DemanglerFn g_demangler_fn = NULL; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 54 | |
| 55 | __LIBC_HIDDEN__ void backtrace_startup() { |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 56 | ScopedDisableDebugCalls disable; |
| 57 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 58 | g_map_info = mapinfo_create(getpid()); |
| 59 | g_demangler = dlopen("libgccdemangle.so", RTLD_NOW); |
| 60 | if (g_demangler != NULL) { |
| 61 | void* sym = dlsym(g_demangler, "__cxa_demangle"); |
| 62 | g_demangler_fn = reinterpret_cast<DemanglerFn>(sym); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | __LIBC_HIDDEN__ void backtrace_shutdown() { |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 67 | ScopedDisableDebugCalls disable; |
| 68 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 69 | mapinfo_destroy(g_map_info); |
| 70 | dlclose(g_demangler); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static char* demangle(const char* symbol) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 74 | if (g_demangler_fn == NULL) { |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 77 | return (*g_demangler_fn)(symbol, NULL, NULL, NULL); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | struct stack_crawl_state_t { |
| 81 | uintptr_t* frames; |
| 82 | size_t frame_count; |
| 83 | size_t max_depth; |
| 84 | bool have_skipped_self; |
| 85 | |
| 86 | stack_crawl_state_t(uintptr_t* frames, size_t max_depth) |
| 87 | : frames(frames), frame_count(0), max_depth(max_depth), have_skipped_self(false) { |
| 88 | } |
| 89 | }; |
| 90 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 91 | static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) { |
| 92 | stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 93 | |
| 94 | uintptr_t ip = _Unwind_GetIP(context); |
| 95 | |
| 96 | // The first stack frame is get_backtrace itself. Skip it. |
| 97 | if (ip != 0 && !state->have_skipped_self) { |
| 98 | state->have_skipped_self = true; |
| 99 | return _URC_NO_REASON; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 100 | } |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 101 | |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 102 | #if defined(__arm__) |
Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 103 | /* |
| 104 | * The instruction pointer is pointing at the instruction after the bl(x), and |
| 105 | * the _Unwind_Backtrace routine already masks the Thumb mode indicator (LSB |
| 106 | * in PC). So we need to do a quick check here to find out if the previous |
| 107 | * instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise 4 from PC. |
| 108 | */ |
| 109 | if (ip != 0) { |
| 110 | short* ptr = reinterpret_cast<short*>(ip); |
| 111 | // Thumb BLX(2) |
Ben Cheng | 63dd03c | 2013-05-07 16:53:33 -0700 | [diff] [blame] | 112 | if ((*(ptr-1) & 0xff80) == 0x4780) { |
Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 113 | ip -= 2; |
| 114 | } else { |
| 115 | ip -= 4; |
| 116 | } |
| 117 | } |
| 118 | #endif |
| 119 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 120 | state->frames[state->frame_count++] = ip; |
| 121 | return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 124 | __LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) { |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 125 | ScopedDisableDebugCalls disable; |
| 126 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 127 | stack_crawl_state_t state(frames, max_depth); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 128 | _Unwind_Backtrace(trace_function, &state); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 129 | return state.frame_count; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 132 | __LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) { |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 133 | ScopedDisableDebugCalls disable; |
| 134 | |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 135 | uintptr_t self_bt[16]; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 136 | if (frames == NULL) { |
| 137 | frame_count = get_backtrace(self_bt, 16); |
| 138 | frames = self_bt; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | __libc_format_log(ANDROID_LOG_ERROR, "libc", |
| 142 | "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); |
| 143 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 144 | for (size_t i = 0 ; i < frame_count; ++i) { |
Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 145 | uintptr_t offset = 0; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 146 | const char* symbol = NULL; |
| 147 | |
| 148 | Dl_info info; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 149 | if (dladdr((void*) frames[i], &info) != 0) { |
Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 150 | offset = reinterpret_cast<uintptr_t>(info.dli_saddr); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 151 | symbol = info.dli_sname; |
| 152 | } |
| 153 | |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 154 | uintptr_t rel_pc = offset; |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 155 | const mapinfo_t* mi = (g_map_info != NULL) ? mapinfo_find(g_map_info, frames[i], &rel_pc) : NULL; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 156 | const char* soname = (mi != NULL) ? mi->name : info.dli_fname; |
| 157 | if (soname == NULL) { |
| 158 | soname = "<unknown>"; |
| 159 | } |
| 160 | if (symbol != NULL) { |
| 161 | // TODO: we might need a flag to say whether it's safe to allocate (demangling allocates). |
| 162 | char* demangled_symbol = demangle(symbol); |
| 163 | const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol; |
| 164 | |
Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 165 | __libc_format_log(ANDROID_LOG_ERROR, "libc", |
Elliott Hughes | ba76572 | 2014-02-25 15:32:01 -0800 | [diff] [blame] | 166 | " #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")", |
| 167 | i, rel_pc, soname, best_name, frames[i] - offset); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 168 | |
| 169 | free(demangled_symbol); |
| 170 | } else { |
Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 171 | __libc_format_log(ANDROID_LOG_ERROR, "libc", |
Elliott Hughes | ba76572 | 2014-02-25 15:32:01 -0800 | [diff] [blame] | 172 | " #%02zd pc %" PAD_PTR " %s", |
| 173 | i, rel_pc, soname); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |