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