blob: 26d5c6a15a24601d175d3d1b7ec3e59f9d64520f [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"
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070038#include "libc_logging.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080039
40/* depends how the system includes define this */
41#ifdef HAVE_UNWIND_CONTEXT_STRUCT
42typedef struct _Unwind_Context __unwind_context;
43#else
44typedef _Unwind_Context __unwind_context;
45#endif
46
Elliott Hughes35b621c2013-01-28 16:27:36 -080047static mapinfo_t* gMapInfo = NULL;
48static void* gDemangler;
49typedef char* (*DemanglerFn)(const char*, char*, size_t*, int*);
50static DemanglerFn gDemanglerFn = NULL;
51
52__LIBC_HIDDEN__ void backtrace_startup() {
53 gMapInfo = mapinfo_create(getpid());
54 gDemangler = dlopen("libgccdemangle.so", RTLD_NOW);
55 if (gDemangler != NULL) {
56 void* sym = dlsym(gDemangler, "__cxa_demangle");
57 gDemanglerFn = reinterpret_cast<DemanglerFn>(sym);
58 }
59}
60
61__LIBC_HIDDEN__ void backtrace_shutdown() {
62 mapinfo_destroy(gMapInfo);
63 dlclose(gDemangler);
64}
65
66static char* demangle(const char* symbol) {
67 if (gDemanglerFn == NULL) {
68 return NULL;
69 }
70 return (*gDemanglerFn)(symbol, NULL, NULL, NULL);
71}
72
73struct stack_crawl_state_t {
74 uintptr_t* frames;
75 size_t frame_count;
76 size_t max_depth;
77 bool have_skipped_self;
78
79 stack_crawl_state_t(uintptr_t* frames, size_t max_depth)
80 : frames(frames), frame_count(0), max_depth(max_depth), have_skipped_self(false) {
81 }
82};
83
Stephen Hines5f6cfce2013-10-01 18:20:51 -070084#if defined(__arm__) && !defined(_Unwind_GetIP)
85// Older versions of Clang don't provide a definition of _Unwind_GetIP(), so
86// we include an appropriate version of our own. Once we have updated to
87// Clang 3.4, this code can be removed.
88static __inline__
89uintptr_t _Unwind_GetIP(struct _Unwind_Context *__context) {
90 uintptr_t __ip = 0;
91 _Unwind_VRS_Get(__context, _UVRSC_CORE, 15, _UVRSD_UINT32, &__ip);
92 return __ip & ~0x1;
93}
94#endif
95
Elliott Hughes1e980b62013-01-17 18:36:06 -080096static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
97 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
Elliott Hughes35b621c2013-01-28 16:27:36 -080098
99 uintptr_t ip = _Unwind_GetIP(context);
100
101 // The first stack frame is get_backtrace itself. Skip it.
102 if (ip != 0 && !state->have_skipped_self) {
103 state->have_skipped_self = true;
104 return _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800105 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800106
Ben Cheng52171b92013-05-07 14:22:43 -0700107#ifdef __arm__
108 /*
109 * The instruction pointer is pointing at the instruction after the bl(x), and
110 * the _Unwind_Backtrace routine already masks the Thumb mode indicator (LSB
111 * in PC). So we need to do a quick check here to find out if the previous
112 * instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise 4 from PC.
113 */
114 if (ip != 0) {
115 short* ptr = reinterpret_cast<short*>(ip);
116 // Thumb BLX(2)
Ben Cheng63dd03c2013-05-07 16:53:33 -0700117 if ((*(ptr-1) & 0xff80) == 0x4780) {
Ben Cheng52171b92013-05-07 14:22:43 -0700118 ip -= 2;
119 } else {
120 ip -= 4;
121 }
122 }
123#endif
124
Elliott Hughes35b621c2013-01-28 16:27:36 -0800125 state->frames[state->frame_count++] = ip;
126 return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800127}
128
Elliott Hughes35b621c2013-01-28 16:27:36 -0800129__LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) {
130 stack_crawl_state_t state(frames, max_depth);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800131 _Unwind_Backtrace(trace_function, &state);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800132 return state.frame_count;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800133}
134
Elliott Hughes35b621c2013-01-28 16:27:36 -0800135__LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800136 uintptr_t self_bt[16];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800137 if (frames == NULL) {
138 frame_count = get_backtrace(self_bt, 16);
139 frames = self_bt;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800140 }
141
142 __libc_format_log(ANDROID_LOG_ERROR, "libc",
143 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
144
Elliott Hughes35b621c2013-01-28 16:27:36 -0800145 for (size_t i = 0 ; i < frame_count; ++i) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700146 uintptr_t offset = 0;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800147 const char* symbol = NULL;
148
149 Dl_info info;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800150 if (dladdr((void*) frames[i], &info) != 0) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700151 offset = reinterpret_cast<uintptr_t>(info.dli_saddr);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800152 symbol = info.dli_sname;
153 }
154
Elliott Hughes35b621c2013-01-28 16:27:36 -0800155 uintptr_t rel_pc;
156 const mapinfo_t* mi = (gMapInfo != NULL) ? mapinfo_find(gMapInfo, frames[i], &rel_pc) : NULL;
157 const char* soname = (mi != NULL) ? mi->name : info.dli_fname;
158 if (soname == NULL) {
159 soname = "<unknown>";
160 }
161 if (symbol != NULL) {
162 // TODO: we might need a flag to say whether it's safe to allocate (demangling allocates).
163 char* demangled_symbol = demangle(symbol);
164 const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol;
165
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700166 __libc_format_log(ANDROID_LOG_ERROR, "libc",
167 " #%02zd pc %0*" PRIxPTR " %s (%s+%" PRIuPTR ")",
168 i,
169 static_cast<int>(2 * sizeof(void*)), rel_pc,
170 soname,
171 best_name, frames[i] - offset);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800172
173 free(demangled_symbol);
174 } else {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700175 __libc_format_log(ANDROID_LOG_ERROR, "libc",
176 " #%02zd pc %0*" PRIxPTR " %s",
177 i,
178 static_cast<int>(2 * sizeof(void*)), rel_pc,
179 soname);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800180 }
181 }
182}