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> |
| 32 | #include <unistd.h> |
| 33 | #include <unwind.h> |
| 34 | #include <sys/types.h> |
| 35 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 36 | #include "debug_mapinfo.h" |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 37 | #include "libc_logging.h" |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 38 | |
| 39 | /* depends how the system includes define this */ |
| 40 | #ifdef HAVE_UNWIND_CONTEXT_STRUCT |
| 41 | typedef struct _Unwind_Context __unwind_context; |
| 42 | #else |
| 43 | typedef _Unwind_Context __unwind_context; |
| 44 | #endif |
| 45 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 46 | static mapinfo_t* gMapInfo = NULL; |
| 47 | static void* gDemangler; |
| 48 | typedef char* (*DemanglerFn)(const char*, char*, size_t*, int*); |
| 49 | static DemanglerFn gDemanglerFn = NULL; |
| 50 | |
| 51 | __LIBC_HIDDEN__ void backtrace_startup() { |
| 52 | gMapInfo = mapinfo_create(getpid()); |
| 53 | gDemangler = dlopen("libgccdemangle.so", RTLD_NOW); |
| 54 | if (gDemangler != NULL) { |
| 55 | void* sym = dlsym(gDemangler, "__cxa_demangle"); |
| 56 | gDemanglerFn = reinterpret_cast<DemanglerFn>(sym); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | __LIBC_HIDDEN__ void backtrace_shutdown() { |
| 61 | mapinfo_destroy(gMapInfo); |
| 62 | dlclose(gDemangler); |
| 63 | } |
| 64 | |
| 65 | static char* demangle(const char* symbol) { |
| 66 | if (gDemanglerFn == NULL) { |
| 67 | return NULL; |
| 68 | } |
| 69 | return (*gDemanglerFn)(symbol, NULL, NULL, NULL); |
| 70 | } |
| 71 | |
| 72 | struct stack_crawl_state_t { |
| 73 | uintptr_t* frames; |
| 74 | size_t frame_count; |
| 75 | size_t max_depth; |
| 76 | bool have_skipped_self; |
| 77 | |
| 78 | stack_crawl_state_t(uintptr_t* frames, size_t max_depth) |
| 79 | : frames(frames), frame_count(0), max_depth(max_depth), have_skipped_self(false) { |
| 80 | } |
| 81 | }; |
| 82 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 83 | static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) { |
| 84 | stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 85 | |
| 86 | uintptr_t ip = _Unwind_GetIP(context); |
| 87 | |
| 88 | // The first stack frame is get_backtrace itself. Skip it. |
| 89 | if (ip != 0 && !state->have_skipped_self) { |
| 90 | state->have_skipped_self = true; |
| 91 | return _URC_NO_REASON; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 92 | } |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 93 | |
Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 94 | #ifdef __arm__ |
| 95 | /* |
| 96 | * The instruction pointer is pointing at the instruction after the bl(x), and |
| 97 | * the _Unwind_Backtrace routine already masks the Thumb mode indicator (LSB |
| 98 | * in PC). So we need to do a quick check here to find out if the previous |
| 99 | * instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise 4 from PC. |
| 100 | */ |
| 101 | if (ip != 0) { |
| 102 | short* ptr = reinterpret_cast<short*>(ip); |
| 103 | // Thumb BLX(2) |
Ben Cheng | 63dd03c | 2013-05-07 16:53:33 -0700 | [diff] [blame^] | 104 | if ((*(ptr-1) & 0xff80) == 0x4780) { |
Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 105 | ip -= 2; |
| 106 | } else { |
| 107 | ip -= 4; |
| 108 | } |
| 109 | } |
| 110 | #endif |
| 111 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 112 | state->frames[state->frame_count++] = ip; |
| 113 | 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] | 114 | } |
| 115 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 116 | __LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) { |
| 117 | stack_crawl_state_t state(frames, max_depth); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 118 | _Unwind_Backtrace(trace_function, &state); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 119 | return state.frame_count; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 122 | __LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) { |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 123 | uintptr_t self_bt[16]; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 124 | if (frames == NULL) { |
| 125 | frame_count = get_backtrace(self_bt, 16); |
| 126 | frames = self_bt; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | __libc_format_log(ANDROID_LOG_ERROR, "libc", |
| 130 | "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); |
| 131 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 132 | for (size_t i = 0 ; i < frame_count; ++i) { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 133 | void* offset = 0; |
| 134 | const char* symbol = NULL; |
| 135 | |
| 136 | Dl_info info; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 137 | if (dladdr((void*) frames[i], &info) != 0) { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 138 | offset = info.dli_saddr; |
| 139 | symbol = info.dli_sname; |
| 140 | } |
| 141 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 142 | uintptr_t rel_pc; |
| 143 | const mapinfo_t* mi = (gMapInfo != NULL) ? mapinfo_find(gMapInfo, frames[i], &rel_pc) : NULL; |
| 144 | const char* soname = (mi != NULL) ? mi->name : info.dli_fname; |
| 145 | if (soname == NULL) { |
| 146 | soname = "<unknown>"; |
| 147 | } |
| 148 | if (symbol != NULL) { |
| 149 | // TODO: we might need a flag to say whether it's safe to allocate (demangling allocates). |
| 150 | char* demangled_symbol = demangle(symbol); |
| 151 | const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol; |
| 152 | |
| 153 | __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s (%s+0x%x)", |
| 154 | i, rel_pc, soname, best_name, frames[i] - (uintptr_t) offset); |
| 155 | |
| 156 | free(demangled_symbol); |
| 157 | } else { |
| 158 | __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s", |
| 159 | i, rel_pc, soname); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |