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 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 29 | #include <dlfcn.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 30 | #include <errno.h> |
Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 31 | #include <inttypes.h> |
Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 32 | #include <malloc.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 33 | #include <pthread.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/types.h> |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 36 | #include <unistd.h> |
| 37 | #include <unwind.h> |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 38 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 39 | #include "backtrace.h" |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 40 | #include "debug_log.h" |
| 41 | #include "MapData.h" |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 42 | |
Elliott Hughes | ba76572 | 2014-02-25 15:32:01 -0800 | [diff] [blame] | 43 | #if defined(__LP64__) |
| 44 | #define PAD_PTR "016" PRIxPTR |
| 45 | #else |
| 46 | #define PAD_PTR "08" PRIxPTR |
| 47 | #endif |
| 48 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 49 | typedef struct _Unwind_Context __unwind_context; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 50 | |
Elliott Hughes | 6e54c3e | 2015-02-05 12:05:34 -0800 | [diff] [blame] | 51 | extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*); |
| 52 | |
Colin Cross | d75d4be | 2016-02-08 14:29:03 -0800 | [diff] [blame^] | 53 | static MapData g_map_data; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 54 | static const MapEntry* g_current_code_map = nullptr; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 55 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 56 | static _Unwind_Reason_Code find_current_map(__unwind_context* context, void*) { |
| 57 | uintptr_t ip = _Unwind_GetIP(context); |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 58 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 59 | if (ip == 0) { |
| 60 | return _URC_END_OF_STACK; |
| 61 | } |
Colin Cross | d75d4be | 2016-02-08 14:29:03 -0800 | [diff] [blame^] | 62 | g_current_code_map = g_map_data.find(ip); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 63 | return _URC_END_OF_STACK; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 66 | void backtrace_startup() { |
Colin Cross | d75d4be | 2016-02-08 14:29:03 -0800 | [diff] [blame^] | 67 | _Unwind_Backtrace(find_current_map, nullptr); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void backtrace_shutdown() { |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | struct stack_crawl_state_t { |
| 74 | uintptr_t* frames; |
| 75 | size_t frame_count; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 76 | size_t cur_frame = 0; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 77 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 78 | stack_crawl_state_t(uintptr_t* frames, size_t frame_count) |
| 79 | : frames(frames), frame_count(frame_count) {} |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 82 | static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) { |
| 83 | stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 84 | |
| 85 | uintptr_t ip = _Unwind_GetIP(context); |
| 86 | |
Christopher Ferris | 224bef8 | 2015-08-18 15:41:31 -0700 | [diff] [blame] | 87 | // The instruction pointer is pointing at the instruction after the return |
| 88 | // call on all architectures. |
| 89 | // Modify the pc to point at the real function. |
Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 90 | if (ip != 0) { |
Christopher Ferris | 224bef8 | 2015-08-18 15:41:31 -0700 | [diff] [blame] | 91 | #if defined(__arm__) |
Christopher Ferris | b72c9d8 | 2015-08-25 20:48:46 -0700 | [diff] [blame] | 92 | // If the ip is suspiciously low, do nothing to avoid a segfault trying |
| 93 | // to access this memory. |
| 94 | if (ip >= 4096) { |
| 95 | // Check bits [15:11] of the first halfword assuming the instruction |
| 96 | // is 32 bits long. If the bits are any of these values, then our |
| 97 | // assumption was correct: |
| 98 | // b11101 |
| 99 | // b11110 |
| 100 | // b11111 |
| 101 | // Otherwise, this is a 16 bit instruction. |
| 102 | uint16_t value = (*reinterpret_cast<uint16_t*>(ip - 2)) >> 11; |
| 103 | if (value == 0x1f || value == 0x1e || value == 0x1d) { |
| 104 | ip -= 4; |
| 105 | } else { |
| 106 | ip -= 2; |
| 107 | } |
Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 108 | } |
Christopher Ferris | 224bef8 | 2015-08-18 15:41:31 -0700 | [diff] [blame] | 109 | #elif defined(__aarch64__) |
| 110 | // All instructions are 4 bytes long, skip back one instruction. |
| 111 | ip -= 4; |
| 112 | #elif defined(__i386__) || defined(__x86_64__) |
| 113 | // It's difficult to decode exactly where the previous instruction is, |
| 114 | // so subtract 1 to estimate where the instruction lives. |
| 115 | ip--; |
Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 116 | #endif |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 117 | |
| 118 | // Do not record the frames that fall in our own shared library. |
| 119 | if (g_current_code_map && (ip >= g_current_code_map->start) && ip < g_current_code_map->end) { |
| 120 | return _URC_NO_REASON; |
| 121 | } |
Christopher Ferris | 224bef8 | 2015-08-18 15:41:31 -0700 | [diff] [blame] | 122 | } |
Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 123 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 124 | state->frames[state->cur_frame++] = ip; |
| 125 | return (state->cur_frame >= state->frame_count) ? _URC_END_OF_STACK : _URC_NO_REASON; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 128 | size_t backtrace_get(uintptr_t* frames, size_t frame_count) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 129 | stack_crawl_state_t state(frames, frame_count); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 130 | _Unwind_Backtrace(trace_function, &state); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 131 | return state.cur_frame; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 134 | std::string backtrace_string(const uintptr_t* frames, size_t frame_count) { |
| 135 | std::string str; |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 136 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 137 | for (size_t frame_num = 0; frame_num < frame_count; frame_num++) { |
Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 138 | uintptr_t offset = 0; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 139 | const char* symbol = nullptr; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 140 | |
| 141 | Dl_info info; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 142 | if (dladdr(reinterpret_cast<void*>(frames[frame_num]), &info) != 0) { |
Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 143 | offset = reinterpret_cast<uintptr_t>(info.dli_saddr); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 144 | symbol = info.dli_sname; |
| 145 | } |
| 146 | |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 147 | uintptr_t rel_pc = offset; |
Colin Cross | d75d4be | 2016-02-08 14:29:03 -0800 | [diff] [blame^] | 148 | const MapEntry* entry = g_map_data.find(frames[frame_num], &rel_pc); |
| 149 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 150 | const char* soname = (entry != nullptr) ? entry->name.c_str() : info.dli_fname; |
| 151 | if (soname == nullptr) { |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 152 | soname = "<unknown>"; |
| 153 | } |
Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 154 | char buf[1024]; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 155 | if (symbol != nullptr) { |
| 156 | char* demangled_symbol = __cxa_demangle(symbol, nullptr, nullptr, nullptr); |
| 157 | const char* best_name = (demangled_symbol != nullptr) ? demangled_symbol : symbol; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 158 | |
Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 159 | __libc_format_buffer(buf, sizeof(buf), |
| 160 | " #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")\n", frame_num, |
| 161 | rel_pc, soname, best_name, frames[frame_num] - offset); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 162 | free(demangled_symbol); |
| 163 | } else { |
Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 164 | __libc_format_buffer(buf, sizeof(buf), |
| 165 | " #%02zd pc %" PAD_PTR " %s\n", frame_num, rel_pc, soname); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 166 | } |
Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 167 | str += buf; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 168 | } |
Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 169 | |
| 170 | return str; |
| 171 | } |
| 172 | |
| 173 | void backtrace_log(const uintptr_t* frames, size_t frame_count) { |
| 174 | error_log_string(backtrace_string(frames, frame_count).c_str()); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 175 | } |