blob: 716e6726ac670980d1dfb7a0f2caf9fe327ebf46 [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
Elliott Hughes1e980b62013-01-17 18:36:06 -080029#include <dlfcn.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080030#include <errno.h>
Elliott Hughesc7c5f852013-10-08 17:02:26 -070031#include <inttypes.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080032#include <malloc.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080033#include <pthread.h>
34#include <string.h>
35#include <sys/types.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080036#include <unistd.h>
37#include <unwind.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080038
Christopher Ferris63860cb2015-11-16 17:30:32 -080039#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080040#include "debug_log.h"
41#include "MapData.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080042
Elliott Hughesba765722014-02-25 15:32:01 -080043#if defined(__LP64__)
44#define PAD_PTR "016" PRIxPTR
45#else
46#define PAD_PTR "08" PRIxPTR
47#endif
48
Elliott Hughes1e980b62013-01-17 18:36:06 -080049typedef struct _Unwind_Context __unwind_context;
Elliott Hughes1e980b62013-01-17 18:36:06 -080050
Elliott Hughes6e54c3e2015-02-05 12:05:34 -080051extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
52
Christopher Ferris63860cb2015-11-16 17:30:32 -080053static MapData* g_map_data = nullptr;
54static const MapEntry* g_current_code_map = nullptr;
Elliott Hughes35b621c2013-01-28 16:27:36 -080055
Christopher Ferris63860cb2015-11-16 17:30:32 -080056static _Unwind_Reason_Code find_current_map(__unwind_context* context, void*) {
57 uintptr_t ip = _Unwind_GetIP(context);
Christopher Ferris861c0ef2014-07-24 17:52:23 -070058
Christopher Ferris63860cb2015-11-16 17:30:32 -080059 if (ip == 0) {
60 return _URC_END_OF_STACK;
61 }
62 g_current_code_map = g_map_data->find(ip);
63 return _URC_END_OF_STACK;
Elliott Hughes35b621c2013-01-28 16:27:36 -080064}
65
Christopher Ferris63860cb2015-11-16 17:30:32 -080066void backtrace_startup() {
Christopher Ferris63860cb2015-11-16 17:30:32 -080067 g_map_data = MapData::Create();
68 if (g_map_data) {
69 _Unwind_Backtrace(find_current_map, nullptr);
70 }
71}
72
73void backtrace_shutdown() {
Christopher Ferris63860cb2015-11-16 17:30:32 -080074 delete g_map_data;
75 g_map_data = nullptr;
Elliott Hughes35b621c2013-01-28 16:27:36 -080076}
77
78struct stack_crawl_state_t {
79 uintptr_t* frames;
80 size_t frame_count;
Christopher Ferris63860cb2015-11-16 17:30:32 -080081 size_t cur_frame = 0;
Elliott Hughes35b621c2013-01-28 16:27:36 -080082
Christopher Ferris63860cb2015-11-16 17:30:32 -080083 stack_crawl_state_t(uintptr_t* frames, size_t frame_count)
84 : frames(frames), frame_count(frame_count) {}
Elliott Hughes35b621c2013-01-28 16:27:36 -080085};
86
Elliott Hughes1e980b62013-01-17 18:36:06 -080087static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
88 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
Elliott Hughes35b621c2013-01-28 16:27:36 -080089
90 uintptr_t ip = _Unwind_GetIP(context);
91
Christopher Ferris224bef82015-08-18 15:41:31 -070092 // The instruction pointer is pointing at the instruction after the return
93 // call on all architectures.
94 // Modify the pc to point at the real function.
Ben Cheng52171b92013-05-07 14:22:43 -070095 if (ip != 0) {
Christopher Ferris224bef82015-08-18 15:41:31 -070096#if defined(__arm__)
Christopher Ferrisb72c9d82015-08-25 20:48:46 -070097 // If the ip is suspiciously low, do nothing to avoid a segfault trying
98 // to access this memory.
99 if (ip >= 4096) {
100 // Check bits [15:11] of the first halfword assuming the instruction
101 // is 32 bits long. If the bits are any of these values, then our
102 // assumption was correct:
103 // b11101
104 // b11110
105 // b11111
106 // Otherwise, this is a 16 bit instruction.
107 uint16_t value = (*reinterpret_cast<uint16_t*>(ip - 2)) >> 11;
108 if (value == 0x1f || value == 0x1e || value == 0x1d) {
109 ip -= 4;
110 } else {
111 ip -= 2;
112 }
Ben Cheng52171b92013-05-07 14:22:43 -0700113 }
Christopher Ferris224bef82015-08-18 15:41:31 -0700114#elif defined(__aarch64__)
115 // All instructions are 4 bytes long, skip back one instruction.
116 ip -= 4;
117#elif defined(__i386__) || defined(__x86_64__)
118 // It's difficult to decode exactly where the previous instruction is,
119 // so subtract 1 to estimate where the instruction lives.
120 ip--;
Ben Cheng52171b92013-05-07 14:22:43 -0700121#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800122
123 // Do not record the frames that fall in our own shared library.
124 if (g_current_code_map && (ip >= g_current_code_map->start) && ip < g_current_code_map->end) {
125 return _URC_NO_REASON;
126 }
Christopher Ferris224bef82015-08-18 15:41:31 -0700127 }
Ben Cheng52171b92013-05-07 14:22:43 -0700128
Christopher Ferris63860cb2015-11-16 17:30:32 -0800129 state->frames[state->cur_frame++] = ip;
130 return (state->cur_frame >= state->frame_count) ? _URC_END_OF_STACK : _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800131}
132
Christopher Ferris63860cb2015-11-16 17:30:32 -0800133size_t backtrace_get(uintptr_t* frames, size_t frame_count) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800134 stack_crawl_state_t state(frames, frame_count);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800135 _Unwind_Backtrace(trace_function, &state);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800136 return state.cur_frame;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800137}
138
Colin Cross2c759912016-02-05 16:17:39 -0800139std::string backtrace_string(const uintptr_t* frames, size_t frame_count) {
140 std::string str;
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700141
Christopher Ferris63860cb2015-11-16 17:30:32 -0800142 for (size_t frame_num = 0; frame_num < frame_count; frame_num++) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700143 uintptr_t offset = 0;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800144 const char* symbol = nullptr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800145
146 Dl_info info;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800147 if (dladdr(reinterpret_cast<void*>(frames[frame_num]), &info) != 0) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700148 offset = reinterpret_cast<uintptr_t>(info.dli_saddr);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800149 symbol = info.dli_sname;
150 }
151
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700152 uintptr_t rel_pc = offset;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800153 const MapEntry* entry = nullptr;
154 if (g_map_data) {
155 entry = g_map_data->find(frames[frame_num], &rel_pc);
156 }
157 const char* soname = (entry != nullptr) ? entry->name.c_str() : info.dli_fname;
158 if (soname == nullptr) {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800159 soname = "<unknown>";
160 }
Colin Cross2c759912016-02-05 16:17:39 -0800161 char buf[1024];
Christopher Ferris63860cb2015-11-16 17:30:32 -0800162 if (symbol != nullptr) {
163 char* demangled_symbol = __cxa_demangle(symbol, nullptr, nullptr, nullptr);
164 const char* best_name = (demangled_symbol != nullptr) ? demangled_symbol : symbol;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800165
Colin Cross2c759912016-02-05 16:17:39 -0800166 __libc_format_buffer(buf, sizeof(buf),
167 " #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")\n", frame_num,
168 rel_pc, soname, best_name, frames[frame_num] - offset);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800169 free(demangled_symbol);
170 } else {
Colin Cross2c759912016-02-05 16:17:39 -0800171 __libc_format_buffer(buf, sizeof(buf),
172 " #%02zd pc %" PAD_PTR " %s\n", frame_num, rel_pc, soname);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800173 }
Colin Cross2c759912016-02-05 16:17:39 -0800174 str += buf;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800175 }
Colin Cross2c759912016-02-05 16:17:39 -0800176
177 return str;
178}
179
180void backtrace_log(const uintptr_t* frames, size_t frame_count) {
181 error_log_string(backtrace_string(frames, frame_count).c_str());
Elliott Hughes1e980b62013-01-17 18:36:06 -0800182}