blob: 7d3c76e9b6ce65d9ebb4bb1d7f302bc374977817 [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
29#include "debug_stacktrace.h"
30
31#include <dlfcn.h>
Elliott Hughesc7c5f852013-10-08 17:02:26 -070032#include <inttypes.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080033#include <unistd.h>
34#include <unwind.h>
35#include <sys/types.h>
36
Elliott Hughes1e980b62013-01-17 18:36:06 -080037#include "debug_mapinfo.h"
Christopher Ferris861c0ef2014-07-24 17:52:23 -070038#include "malloc_debug_disable.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070039#include "private/libc_logging.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080040
Elliott Hughesba765722014-02-25 15:32:01 -080041#if defined(__LP64__)
42#define PAD_PTR "016" PRIxPTR
43#else
44#define PAD_PTR "08" PRIxPTR
45#endif
46
Elliott Hughes1e980b62013-01-17 18:36:06 -080047typedef struct _Unwind_Context __unwind_context;
Elliott Hughes1e980b62013-01-17 18:36:06 -080048
Elliott Hughes1728b232014-05-14 10:02:03 -070049static mapinfo_t* g_map_info = NULL;
50static void* g_demangler;
Elliott Hughes35b621c2013-01-28 16:27:36 -080051typedef char* (*DemanglerFn)(const char*, char*, size_t*, int*);
Elliott Hughes1728b232014-05-14 10:02:03 -070052static DemanglerFn g_demangler_fn = NULL;
Elliott Hughes35b621c2013-01-28 16:27:36 -080053
54__LIBC_HIDDEN__ void backtrace_startup() {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070055 ScopedDisableDebugCalls disable;
56
Elliott Hughes1728b232014-05-14 10:02:03 -070057 g_map_info = mapinfo_create(getpid());
58 g_demangler = dlopen("libgccdemangle.so", RTLD_NOW);
59 if (g_demangler != NULL) {
60 void* sym = dlsym(g_demangler, "__cxa_demangle");
61 g_demangler_fn = reinterpret_cast<DemanglerFn>(sym);
Elliott Hughes35b621c2013-01-28 16:27:36 -080062 }
63}
64
65__LIBC_HIDDEN__ void backtrace_shutdown() {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070066 ScopedDisableDebugCalls disable;
67
Elliott Hughes1728b232014-05-14 10:02:03 -070068 mapinfo_destroy(g_map_info);
69 dlclose(g_demangler);
Elliott Hughes35b621c2013-01-28 16:27:36 -080070}
71
72static char* demangle(const char* symbol) {
Elliott Hughes1728b232014-05-14 10:02:03 -070073 if (g_demangler_fn == NULL) {
Elliott Hughes35b621c2013-01-28 16:27:36 -080074 return NULL;
75 }
Elliott Hughes1728b232014-05-14 10:02:03 -070076 return (*g_demangler_fn)(symbol, NULL, NULL, NULL);
Elliott Hughes35b621c2013-01-28 16:27:36 -080077}
78
79struct stack_crawl_state_t {
80 uintptr_t* frames;
81 size_t frame_count;
82 size_t max_depth;
83 bool have_skipped_self;
84
85 stack_crawl_state_t(uintptr_t* frames, size_t max_depth)
86 : frames(frames), frame_count(0), max_depth(max_depth), have_skipped_self(false) {
87 }
88};
89
Elliott Hughes1e980b62013-01-17 18:36:06 -080090static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
91 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
Elliott Hughes35b621c2013-01-28 16:27:36 -080092
93 uintptr_t ip = _Unwind_GetIP(context);
94
95 // The first stack frame is get_backtrace itself. Skip it.
96 if (ip != 0 && !state->have_skipped_self) {
97 state->have_skipped_self = true;
98 return _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -080099 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800100
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700101#if defined(__arm__)
Ben Cheng52171b92013-05-07 14:22:43 -0700102 /*
103 * The instruction pointer is pointing at the instruction after the bl(x), and
104 * the _Unwind_Backtrace routine already masks the Thumb mode indicator (LSB
105 * in PC). So we need to do a quick check here to find out if the previous
106 * instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise 4 from PC.
107 */
108 if (ip != 0) {
109 short* ptr = reinterpret_cast<short*>(ip);
110 // Thumb BLX(2)
Ben Cheng63dd03c2013-05-07 16:53:33 -0700111 if ((*(ptr-1) & 0xff80) == 0x4780) {
Ben Cheng52171b92013-05-07 14:22:43 -0700112 ip -= 2;
113 } else {
114 ip -= 4;
115 }
116 }
117#endif
118
Elliott Hughes35b621c2013-01-28 16:27:36 -0800119 state->frames[state->frame_count++] = ip;
120 return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800121}
122
Elliott Hughes35b621c2013-01-28 16:27:36 -0800123__LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700124 ScopedDisableDebugCalls disable;
125
Elliott Hughes35b621c2013-01-28 16:27:36 -0800126 stack_crawl_state_t state(frames, max_depth);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800127 _Unwind_Backtrace(trace_function, &state);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800128 return state.frame_count;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800129}
130
Elliott Hughes35b621c2013-01-28 16:27:36 -0800131__LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700132 ScopedDisableDebugCalls disable;
133
Elliott Hughes239e7a02013-01-25 17:13:45 -0800134 uintptr_t self_bt[16];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800135 if (frames == NULL) {
136 frame_count = get_backtrace(self_bt, 16);
137 frames = self_bt;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800138 }
139
140 __libc_format_log(ANDROID_LOG_ERROR, "libc",
141 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
142
Elliott Hughes35b621c2013-01-28 16:27:36 -0800143 for (size_t i = 0 ; i < frame_count; ++i) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700144 uintptr_t offset = 0;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800145 const char* symbol = NULL;
146
147 Dl_info info;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800148 if (dladdr((void*) frames[i], &info) != 0) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700149 offset = reinterpret_cast<uintptr_t>(info.dli_saddr);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800150 symbol = info.dli_sname;
151 }
152
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700153 uintptr_t rel_pc = offset;
Elliott Hughes1728b232014-05-14 10:02:03 -0700154 const mapinfo_t* mi = (g_map_info != NULL) ? mapinfo_find(g_map_info, frames[i], &rel_pc) : NULL;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800155 const char* soname = (mi != NULL) ? mi->name : info.dli_fname;
156 if (soname == NULL) {
157 soname = "<unknown>";
158 }
159 if (symbol != NULL) {
160 // TODO: we might need a flag to say whether it's safe to allocate (demangling allocates).
161 char* demangled_symbol = demangle(symbol);
162 const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol;
163
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700164 __libc_format_log(ANDROID_LOG_ERROR, "libc",
Elliott Hughesba765722014-02-25 15:32:01 -0800165 " #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")",
166 i, rel_pc, soname, best_name, frames[i] - offset);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800167
168 free(demangled_symbol);
169 } else {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700170 __libc_format_log(ANDROID_LOG_ERROR, "libc",
Elliott Hughesba765722014-02-25 15:32:01 -0800171 " #%02zd pc %" PAD_PTR " %s",
172 i, rel_pc, soname);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800173 }
174 }
175}