blob: 0f93b26c456217a6df202ffaf510c23675c6523f [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>
32#include <unistd.h>
33#include <unwind.h>
34#include <sys/types.h>
35
Elliott Hughes1e980b62013-01-17 18:36:06 -080036#include "debug_mapinfo.h"
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070037#include "libc_logging.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080038
39/* depends how the system includes define this */
40#ifdef HAVE_UNWIND_CONTEXT_STRUCT
41typedef struct _Unwind_Context __unwind_context;
42#else
43typedef _Unwind_Context __unwind_context;
44#endif
45
Elliott Hughes35b621c2013-01-28 16:27:36 -080046static mapinfo_t* gMapInfo = NULL;
47static void* gDemangler;
48typedef char* (*DemanglerFn)(const char*, char*, size_t*, int*);
49static DemanglerFn gDemanglerFn = NULL;
50
51__LIBC_HIDDEN__ void backtrace_startup() {
52 gMapInfo = mapinfo_create(getpid());
53 gDemangler = dlopen("libgccdemangle.so", RTLD_NOW);
54 if (gDemangler != NULL) {
55 void* sym = dlsym(gDemangler, "__cxa_demangle");
56 gDemanglerFn = reinterpret_cast<DemanglerFn>(sym);
57 }
58}
59
60__LIBC_HIDDEN__ void backtrace_shutdown() {
61 mapinfo_destroy(gMapInfo);
62 dlclose(gDemangler);
63}
64
65static char* demangle(const char* symbol) {
66 if (gDemanglerFn == NULL) {
67 return NULL;
68 }
69 return (*gDemanglerFn)(symbol, NULL, NULL, NULL);
70}
71
72struct stack_crawl_state_t {
73 uintptr_t* frames;
74 size_t frame_count;
75 size_t max_depth;
76 bool have_skipped_self;
77
78 stack_crawl_state_t(uintptr_t* frames, size_t max_depth)
79 : frames(frames), frame_count(0), max_depth(max_depth), have_skipped_self(false) {
80 }
81};
82
Stephen Hines5f6cfce2013-10-01 18:20:51 -070083#if defined(__arm__) && !defined(_Unwind_GetIP)
84// Older versions of Clang don't provide a definition of _Unwind_GetIP(), so
85// we include an appropriate version of our own. Once we have updated to
86// Clang 3.4, this code can be removed.
87static __inline__
88uintptr_t _Unwind_GetIP(struct _Unwind_Context *__context) {
89 uintptr_t __ip = 0;
90 _Unwind_VRS_Get(__context, _UVRSC_CORE, 15, _UVRSD_UINT32, &__ip);
91 return __ip & ~0x1;
92}
93#endif
94
Elliott Hughes1e980b62013-01-17 18:36:06 -080095static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
96 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
Elliott Hughes35b621c2013-01-28 16:27:36 -080097
98 uintptr_t ip = _Unwind_GetIP(context);
99
100 // The first stack frame is get_backtrace itself. Skip it.
101 if (ip != 0 && !state->have_skipped_self) {
102 state->have_skipped_self = true;
103 return _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800104 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800105
Ben Cheng52171b92013-05-07 14:22:43 -0700106#ifdef __arm__
107 /*
108 * The instruction pointer is pointing at the instruction after the bl(x), and
109 * the _Unwind_Backtrace routine already masks the Thumb mode indicator (LSB
110 * in PC). So we need to do a quick check here to find out if the previous
111 * instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise 4 from PC.
112 */
113 if (ip != 0) {
114 short* ptr = reinterpret_cast<short*>(ip);
115 // Thumb BLX(2)
Ben Cheng63dd03c2013-05-07 16:53:33 -0700116 if ((*(ptr-1) & 0xff80) == 0x4780) {
Ben Cheng52171b92013-05-07 14:22:43 -0700117 ip -= 2;
118 } else {
119 ip -= 4;
120 }
121 }
122#endif
123
Elliott Hughes35b621c2013-01-28 16:27:36 -0800124 state->frames[state->frame_count++] = ip;
125 return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800126}
127
Elliott Hughes35b621c2013-01-28 16:27:36 -0800128__LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) {
129 stack_crawl_state_t state(frames, max_depth);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800130 _Unwind_Backtrace(trace_function, &state);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800131 return state.frame_count;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800132}
133
Elliott Hughes35b621c2013-01-28 16:27:36 -0800134__LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800135 uintptr_t self_bt[16];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800136 if (frames == NULL) {
137 frame_count = get_backtrace(self_bt, 16);
138 frames = self_bt;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800139 }
140
141 __libc_format_log(ANDROID_LOG_ERROR, "libc",
142 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
143
Elliott Hughes35b621c2013-01-28 16:27:36 -0800144 for (size_t i = 0 ; i < frame_count; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800145 void* offset = 0;
146 const char* symbol = NULL;
147
148 Dl_info info;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800149 if (dladdr((void*) frames[i], &info) != 0) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800150 offset = info.dli_saddr;
151 symbol = info.dli_sname;
152 }
153
Elliott Hughes35b621c2013-01-28 16:27:36 -0800154 uintptr_t rel_pc;
155 const mapinfo_t* mi = (gMapInfo != NULL) ? mapinfo_find(gMapInfo, frames[i], &rel_pc) : NULL;
156 const char* soname = (mi != NULL) ? mi->name : info.dli_fname;
157 if (soname == NULL) {
158 soname = "<unknown>";
159 }
160 if (symbol != NULL) {
161 // TODO: we might need a flag to say whether it's safe to allocate (demangling allocates).
162 char* demangled_symbol = demangle(symbol);
163 const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol;
164
165 __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s (%s+0x%x)",
166 i, rel_pc, soname, best_name, frames[i] - (uintptr_t) offset);
167
168 free(demangled_symbol);
169 } else {
170 __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s",
171 i, rel_pc, soname);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800172 }
173 }
174}