blob: 6a32fcaff3ff540cd251c96fec97bbe48e2b134a [file] [log] [blame]
Elliott Hughes1e980b62013-01-17 18:36:06 -08001/*
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 Prichardc2adad12022-12-08 16:52:39 -080029#include <cxxabi.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080030#include <dlfcn.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080031#include <errno.h>
Elliott Hughesc7c5f852013-10-08 17:02:26 -070032#include <inttypes.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080033#include <malloc.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080034#include <pthread.h>
35#include <string.h>
36#include <sys/types.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080037#include <unistd.h>
38#include <unwind.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080039
Christopher Ferris4da25032018-03-07 13:38:48 -080040#include "MapData.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080041#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080042#include "debug_log.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080043
Elliott Hughesba765722014-02-25 15:32:01 -080044#if defined(__LP64__)
45#define PAD_PTR "016" PRIxPTR
46#else
47#define PAD_PTR "08" PRIxPTR
48#endif
49
Elliott Hughes1e980b62013-01-17 18:36:06 -080050typedef struct _Unwind_Context __unwind_context;
Elliott Hughes1e980b62013-01-17 18:36:06 -080051
Colin Crossd75d4be2016-02-08 14:29:03 -080052static MapData g_map_data;
Christopher Ferris65e349d2024-05-16 14:15:15 -070053static MapEntry g_current_code_map;
Elliott Hughes35b621c2013-01-28 16:27:36 -080054
Christopher Ferris63860cb2015-11-16 17:30:32 -080055static _Unwind_Reason_Code find_current_map(__unwind_context* context, void*) {
56 uintptr_t ip = _Unwind_GetIP(context);
Christopher Ferris861c0ef2014-07-24 17:52:23 -070057
Christopher Ferris63860cb2015-11-16 17:30:32 -080058 if (ip == 0) {
59 return _URC_END_OF_STACK;
60 }
Christopher Ferris65e349d2024-05-16 14:15:15 -070061 auto map = g_map_data.find(ip);
62 if (map != nullptr) {
63 g_current_code_map = *map;
64 }
Christopher Ferris63860cb2015-11-16 17:30:32 -080065 return _URC_END_OF_STACK;
Elliott Hughes35b621c2013-01-28 16:27:36 -080066}
67
Christopher Ferris63860cb2015-11-16 17:30:32 -080068void backtrace_startup() {
Christopher Ferris65e349d2024-05-16 14:15:15 -070069 g_map_data.ReadMaps();
Colin Crossd75d4be2016-02-08 14:29:03 -080070 _Unwind_Backtrace(find_current_map, nullptr);
Christopher Ferris63860cb2015-11-16 17:30:32 -080071}
72
Christopher Ferris4da25032018-03-07 13:38:48 -080073void backtrace_shutdown() {}
Elliott Hughes35b621c2013-01-28 16:27:36 -080074
75struct stack_crawl_state_t {
76 uintptr_t* frames;
77 size_t frame_count;
Christopher Ferris63860cb2015-11-16 17:30:32 -080078 size_t cur_frame = 0;
Elliott Hughes35b621c2013-01-28 16:27:36 -080079
Christopher Ferris63860cb2015-11-16 17:30:32 -080080 stack_crawl_state_t(uintptr_t* frames, size_t frame_count)
81 : frames(frames), frame_count(frame_count) {}
Elliott Hughes35b621c2013-01-28 16:27:36 -080082};
83
Elliott Hughes1e980b62013-01-17 18:36:06 -080084static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
85 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
Elliott Hughes35b621c2013-01-28 16:27:36 -080086
87 uintptr_t ip = _Unwind_GetIP(context);
88
Elliott Hugheseeaf9ed2022-11-10 20:26:31 +000089 // `ip` is the address of the instruction *after* the call site in
90 // `context`, so we want to back up by one instruction. This is hard for
91 // every architecture except arm64, so we just make sure we're *inside*
92 // that instruction, not necessarily at the start of it. (If the value
93 // is too low to be valid, we just leave it alone.)
94 if (ip >= 4096) {
95#if defined(__aarch64__)
96 ip -= 4; // Exactly.
97#elif defined(__arm__) || defined(__riscv)
98 ip -= 2; // At least.
Christopher Ferris224bef82015-08-18 15:41:31 -070099#elif defined(__i386__) || defined(__x86_64__)
Elliott Hugheseeaf9ed2022-11-10 20:26:31 +0000100 ip -= 1; // At least.
Ben Cheng52171b92013-05-07 14:22:43 -0700101#endif
Elliott Hugheseeaf9ed2022-11-10 20:26:31 +0000102 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800103
Elliott Hugheseeaf9ed2022-11-10 20:26:31 +0000104 // Do not record the frames that fall in our own shared library.
Christopher Ferris65e349d2024-05-16 14:15:15 -0700105 if (g_current_code_map.start() != 0 && (ip >= g_current_code_map.start()) &&
106 ip < g_current_code_map.end()) {
Elliott Hugheseeaf9ed2022-11-10 20:26:31 +0000107 return _URC_NO_REASON;
Christopher Ferris224bef82015-08-18 15:41:31 -0700108 }
Ben Cheng52171b92013-05-07 14:22:43 -0700109
Christopher Ferris63860cb2015-11-16 17:30:32 -0800110 state->frames[state->cur_frame++] = ip;
111 return (state->cur_frame >= state->frame_count) ? _URC_END_OF_STACK : _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800112}
113
Christopher Ferris63860cb2015-11-16 17:30:32 -0800114size_t backtrace_get(uintptr_t* frames, size_t frame_count) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800115 stack_crawl_state_t state(frames, frame_count);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800116 _Unwind_Backtrace(trace_function, &state);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800117 return state.cur_frame;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800118}
119
Colin Cross2c759912016-02-05 16:17:39 -0800120std::string backtrace_string(const uintptr_t* frames, size_t frame_count) {
Christopher Ferris65e349d2024-05-16 14:15:15 -0700121 if (g_map_data.NumMaps() == 0) {
122 g_map_data.ReadMaps();
123 }
124
Colin Cross2c759912016-02-05 16:17:39 -0800125 std::string str;
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700126
Christopher Ferris63860cb2015-11-16 17:30:32 -0800127 for (size_t frame_num = 0; frame_num < frame_count; frame_num++) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700128 uintptr_t offset = 0;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800129 const char* symbol = nullptr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800130
131 Dl_info info;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800132 if (dladdr(reinterpret_cast<void*>(frames[frame_num]), &info) != 0) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700133 offset = reinterpret_cast<uintptr_t>(info.dli_saddr);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800134 symbol = info.dli_sname;
Christopher Ferrisa19bc2d2016-11-15 14:04:16 -0800135 } else {
136 info.dli_fname = nullptr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800137 }
138
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700139 uintptr_t rel_pc = offset;
Colin Crossd75d4be2016-02-08 14:29:03 -0800140 const MapEntry* entry = g_map_data.find(frames[frame_num], &rel_pc);
141
Christopher Ferris65e349d2024-05-16 14:15:15 -0700142 const char* soname = (entry != nullptr) ? entry->name().c_str() : info.dli_fname;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800143 if (soname == nullptr) {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800144 soname = "<unknown>";
145 }
Christopher Ferrisc0352bb2017-05-25 18:26:37 -0700146
147 char offset_buf[128];
Christopher Ferris65e349d2024-05-16 14:15:15 -0700148 if (entry != nullptr && entry->elf_start_offset() != 0) {
149 snprintf(offset_buf, sizeof(offset_buf), " (offset 0x%" PRIxPTR ")",
150 entry->elf_start_offset());
Christopher Ferrisc0352bb2017-05-25 18:26:37 -0700151 } else {
152 offset_buf[0] = '\0';
153 }
154
Colin Cross2c759912016-02-05 16:17:39 -0800155 char buf[1024];
Christopher Ferris63860cb2015-11-16 17:30:32 -0800156 if (symbol != nullptr) {
Ryan Prichardc2adad12022-12-08 16:52:39 -0800157 char* demangled_name = abi::__cxa_demangle(symbol, nullptr, nullptr, nullptr);
Christopher Ferris9782b872019-07-18 13:36:50 -0700158 const char* name;
159 if (demangled_name != nullptr) {
160 name = demangled_name;
161 } else {
162 name = symbol;
163 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800164 async_safe_format_buffer(buf, sizeof(buf),
165 " #%02zd pc %" PAD_PTR " %s%s (%s+%" PRIuPTR ")\n",
Christopher Ferris9782b872019-07-18 13:36:50 -0700166 frame_num, rel_pc, soname, offset_buf, name,
Christopher Ferris4da25032018-03-07 13:38:48 -0800167 frames[frame_num] - offset);
Christopher Ferris9782b872019-07-18 13:36:50 -0700168 free(demangled_name);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800169 } else {
Christopher Ferris4da25032018-03-07 13:38:48 -0800170 async_safe_format_buffer(buf, sizeof(buf), " #%02zd pc %" PAD_PTR " %s%s\n",
171 frame_num, rel_pc, soname, offset_buf);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800172 }
Colin Cross2c759912016-02-05 16:17:39 -0800173 str += buf;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800174 }
Colin Cross2c759912016-02-05 16:17:39 -0800175
176 return str;
177}
178
179void backtrace_log(const uintptr_t* frames, size_t frame_count) {
Christopher Ferris65e349d2024-05-16 14:15:15 -0700180 g_map_data.ReadMaps();
Colin Cross2c759912016-02-05 16:17:39 -0800181 error_log_string(backtrace_string(frames, frame_count).c_str());
Elliott Hughes1e980b62013-01-17 18:36:06 -0800182}