| 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 |  | 
| Ryan Prichard | c2adad1 | 2022-12-08 16:52:39 -0800 | [diff] [blame] | 29 | #include <cxxabi.h> | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 30 | #include <dlfcn.h> | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 31 | #include <errno.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> | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 34 | #include <pthread.h> | 
|  | 35 | #include <string.h> | 
|  | 36 | #include <sys/types.h> | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 37 | #include <unistd.h> | 
|  | 38 | #include <unwind.h> | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 39 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 40 | #include "MapData.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 41 | #include "backtrace.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 42 | #include "debug_log.h" | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 43 |  | 
| Elliott Hughes | ba76572 | 2014-02-25 15:32:01 -0800 | [diff] [blame] | 44 | #if defined(__LP64__) | 
|  | 45 | #define PAD_PTR "016" PRIxPTR | 
|  | 46 | #else | 
|  | 47 | #define PAD_PTR "08" PRIxPTR | 
|  | 48 | #endif | 
|  | 49 |  | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 50 | typedef struct _Unwind_Context __unwind_context; | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 51 |  | 
| Colin Cross | d75d4be | 2016-02-08 14:29:03 -0800 | [diff] [blame] | 52 | static MapData g_map_data; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 53 | static const MapEntry* g_current_code_map = nullptr; | 
| Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 54 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 55 | static _Unwind_Reason_Code find_current_map(__unwind_context* context, void*) { | 
|  | 56 | uintptr_t ip = _Unwind_GetIP(context); | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 57 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 58 | if (ip == 0) { | 
|  | 59 | return _URC_END_OF_STACK; | 
|  | 60 | } | 
| Colin Cross | d75d4be | 2016-02-08 14:29:03 -0800 | [diff] [blame] | 61 | g_current_code_map = g_map_data.find(ip); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 62 | return _URC_END_OF_STACK; | 
| Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 65 | void backtrace_startup() { | 
| Colin Cross | d75d4be | 2016-02-08 14:29:03 -0800 | [diff] [blame] | 66 | _Unwind_Backtrace(find_current_map, nullptr); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 69 | void backtrace_shutdown() {} | 
| Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 70 |  | 
|  | 71 | struct stack_crawl_state_t { | 
|  | 72 | uintptr_t* frames; | 
|  | 73 | size_t frame_count; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 74 | size_t cur_frame = 0; | 
| Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 75 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 76 | stack_crawl_state_t(uintptr_t* frames, size_t frame_count) | 
|  | 77 | : frames(frames), frame_count(frame_count) {} | 
| Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 78 | }; | 
|  | 79 |  | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 80 | static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) { | 
|  | 81 | stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg); | 
| Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 82 |  | 
|  | 83 | uintptr_t ip = _Unwind_GetIP(context); | 
|  | 84 |  | 
| Elliott Hughes | eeaf9ed | 2022-11-10 20:26:31 +0000 | [diff] [blame] | 85 | // `ip` is the address of the instruction *after* the call site in | 
|  | 86 | // `context`, so we want to back up by one instruction. This is hard for | 
|  | 87 | // every architecture except arm64, so we just make sure we're *inside* | 
|  | 88 | // that instruction, not necessarily at the start of it. (If the value | 
|  | 89 | // is too low to be valid, we just leave it alone.) | 
|  | 90 | if (ip >= 4096) { | 
|  | 91 | #if defined(__aarch64__) | 
|  | 92 | ip -= 4;  // Exactly. | 
|  | 93 | #elif defined(__arm__) || defined(__riscv) | 
|  | 94 | ip -= 2;  // At least. | 
| Christopher Ferris | 224bef8 | 2015-08-18 15:41:31 -0700 | [diff] [blame] | 95 | #elif defined(__i386__) || defined(__x86_64__) | 
| Elliott Hughes | eeaf9ed | 2022-11-10 20:26:31 +0000 | [diff] [blame] | 96 | ip -= 1;  // At least. | 
| Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 97 | #endif | 
| Elliott Hughes | eeaf9ed | 2022-11-10 20:26:31 +0000 | [diff] [blame] | 98 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 99 |  | 
| Elliott Hughes | eeaf9ed | 2022-11-10 20:26:31 +0000 | [diff] [blame] | 100 | // Do not record the frames that fall in our own shared library. | 
|  | 101 | if (g_current_code_map && (ip >= g_current_code_map->start) && ip < g_current_code_map->end) { | 
|  | 102 | return _URC_NO_REASON; | 
| Christopher Ferris | 224bef8 | 2015-08-18 15:41:31 -0700 | [diff] [blame] | 103 | } | 
| Ben Cheng | 52171b9 | 2013-05-07 14:22:43 -0700 | [diff] [blame] | 104 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 105 | state->frames[state->cur_frame++] = ip; | 
|  | 106 | 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] | 107 | } | 
|  | 108 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 109 | size_t backtrace_get(uintptr_t* frames, size_t frame_count) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 110 | stack_crawl_state_t state(frames, frame_count); | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 111 | _Unwind_Backtrace(trace_function, &state); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 112 | return state.cur_frame; | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 115 | std::string backtrace_string(const uintptr_t* frames, size_t frame_count) { | 
|  | 116 | std::string str; | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 117 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 118 | for (size_t frame_num = 0; frame_num < frame_count; frame_num++) { | 
| Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 119 | uintptr_t offset = 0; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 120 | const char* symbol = nullptr; | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 121 |  | 
|  | 122 | Dl_info info; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 123 | if (dladdr(reinterpret_cast<void*>(frames[frame_num]), &info) != 0) { | 
| Elliott Hughes | c7c5f85 | 2013-10-08 17:02:26 -0700 | [diff] [blame] | 124 | offset = reinterpret_cast<uintptr_t>(info.dli_saddr); | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 125 | symbol = info.dli_sname; | 
| Christopher Ferris | a19bc2d | 2016-11-15 14:04:16 -0800 | [diff] [blame] | 126 | } else { | 
|  | 127 | info.dli_fname = nullptr; | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 130 | uintptr_t rel_pc = offset; | 
| Colin Cross | d75d4be | 2016-02-08 14:29:03 -0800 | [diff] [blame] | 131 | const MapEntry* entry = g_map_data.find(frames[frame_num], &rel_pc); | 
|  | 132 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 133 | const char* soname = (entry != nullptr) ? entry->name.c_str() : info.dli_fname; | 
|  | 134 | if (soname == nullptr) { | 
| Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 135 | soname = "<unknown>"; | 
|  | 136 | } | 
| Christopher Ferris | c0352bb | 2017-05-25 18:26:37 -0700 | [diff] [blame] | 137 |  | 
|  | 138 | char offset_buf[128]; | 
| Christopher Ferris | b233fab | 2018-12-18 16:44:42 -0800 | [diff] [blame] | 139 | if (entry != nullptr && entry->elf_start_offset != 0) { | 
|  | 140 | snprintf(offset_buf, sizeof(offset_buf), " (offset 0x%" PRIxPTR ")", entry->elf_start_offset); | 
| Christopher Ferris | c0352bb | 2017-05-25 18:26:37 -0700 | [diff] [blame] | 141 | } else { | 
|  | 142 | offset_buf[0] = '\0'; | 
|  | 143 | } | 
|  | 144 |  | 
| Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 145 | char buf[1024]; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 146 | if (symbol != nullptr) { | 
| Ryan Prichard | c2adad1 | 2022-12-08 16:52:39 -0800 | [diff] [blame] | 147 | char* demangled_name = abi::__cxa_demangle(symbol, nullptr, nullptr, nullptr); | 
| Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 148 | const char* name; | 
|  | 149 | if (demangled_name != nullptr) { | 
|  | 150 | name = demangled_name; | 
|  | 151 | } else { | 
|  | 152 | name = symbol; | 
|  | 153 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 154 | async_safe_format_buffer(buf, sizeof(buf), | 
|  | 155 | "          #%02zd  pc %" PAD_PTR "  %s%s (%s+%" PRIuPTR ")\n", | 
| Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 156 | frame_num, rel_pc, soname, offset_buf, name, | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 157 | frames[frame_num] - offset); | 
| Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 158 | free(demangled_name); | 
| Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 159 | } else { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 160 | async_safe_format_buffer(buf, sizeof(buf), "          #%02zd  pc %" PAD_PTR "  %s%s\n", | 
|  | 161 | frame_num, rel_pc, soname, offset_buf); | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 162 | } | 
| Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 163 | str += buf; | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 164 | } | 
| Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 165 |  | 
|  | 166 | return str; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | void backtrace_log(const uintptr_t* frames, size_t frame_count) { | 
|  | 170 | error_log_string(backtrace_string(frames, frame_count).c_str()); | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 171 | } |