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 <inttypes.h> 33 #include <malloc.h> 34 #include <unistd.h> 35 #include <unwind.h> 36 #include <sys/types.h> 37 38 #include "debug_mapinfo.h" 39 #include "malloc_debug_disable.h" 40 #include "private/libc_logging.h" 41 42 #if defined(__LP64__) 43 #define PAD_PTR "016" PRIxPTR 44 #else 45 #define PAD_PTR "08" PRIxPTR 46 #endif 47 48 typedef struct _Unwind_Context __unwind_context; 49 50 extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*); 51 52 static mapinfo_t* g_map_info = NULL; 53 54 __LIBC_HIDDEN__ void backtrace_startup() { 55 ScopedDisableDebugCalls disable; 56 57 g_map_info = mapinfo_create(getpid()); 58 } 59 60 __LIBC_HIDDEN__ void backtrace_shutdown() { 61 ScopedDisableDebugCalls disable; 62 63 mapinfo_destroy(g_map_info); 64 } 65 66 struct stack_crawl_state_t { 67 uintptr_t* frames; 68 size_t frame_count; 69 size_t max_depth; 70 bool have_skipped_self; 71 72 stack_crawl_state_t(uintptr_t* frames, size_t max_depth) 73 : frames(frames), frame_count(0), max_depth(max_depth), have_skipped_self(false) { 74 } 75 }; 76 77 static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) { 78 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); 79 80 uintptr_t ip = _Unwind_GetIP(context); 81 82 // The first stack frame is get_backtrace itself. Skip it. 83 if (ip != 0 && !state->have_skipped_self) { 84 state->have_skipped_self = true; 85 return _URC_NO_REASON; 86 } 87 88 #if defined(__arm__) 89 /* 90 * The instruction pointer is pointing at the instruction after the bl(x), and 91 * the _Unwind_Backtrace routine already masks the Thumb mode indicator (LSB 92 * in PC). So we need to do a quick check here to find out if the previous 93 * instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise 4 from PC. 94 */ 95 if (ip != 0) { 96 short* ptr = reinterpret_cast<short*>(ip); 97 // Thumb BLX(2) 98 if ((*(ptr-1) & 0xff80) == 0x4780) { 99 ip -= 2; 100 } else { 101 ip -= 4; 102 } 103 } 104 #endif 105 106 state->frames[state->frame_count++] = ip; 107 return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON; 108 } 109 110 __LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) { 111 ScopedDisableDebugCalls disable; 112 113 stack_crawl_state_t state(frames, max_depth); 114 _Unwind_Backtrace(trace_function, &state); 115 return state.frame_count; 116 } 117 118 __LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) { 119 ScopedDisableDebugCalls disable; 120 121 uintptr_t self_bt[16]; 122 if (frames == NULL) { 123 frame_count = get_backtrace(self_bt, 16); 124 frames = self_bt; 125 } 126 127 __libc_format_log(ANDROID_LOG_ERROR, "libc", 128 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); 129 130 for (size_t i = 0 ; i < frame_count; ++i) { 131 uintptr_t offset = 0; 132 const char* symbol = NULL; 133 134 Dl_info info; 135 if (dladdr((void*) frames[i], &info) != 0) { 136 offset = reinterpret_cast<uintptr_t>(info.dli_saddr); 137 symbol = info.dli_sname; 138 } 139 140 uintptr_t rel_pc = offset; 141 const mapinfo_t* mi = (g_map_info != NULL) ? mapinfo_find(g_map_info, frames[i], &rel_pc) : NULL; 142 const char* soname = (mi != NULL) ? mi->name : info.dli_fname; 143 if (soname == NULL) { 144 soname = "<unknown>"; 145 } 146 if (symbol != NULL) { 147 char* demangled_symbol = __cxa_demangle(symbol, NULL, NULL, NULL); 148 const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol; 149 150 __libc_format_log(ANDROID_LOG_ERROR, "libc", 151 " #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")", 152 i, rel_pc, soname, best_name, frames[i] - offset); 153 154 free(demangled_symbol); 155 } else { 156 __libc_format_log(ANDROID_LOG_ERROR, "libc", 157 " #%02zd pc %" PAD_PTR " %s", 158 i, rel_pc, soname); 159 } 160 } 161 } 162