blob: 83171add91c18be9ca76dfac7b670fffca8d841d [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 Hugheseb847bc2013-10-09 15:50:50 -070038#include "private/libc_logging.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080039
Elliott Hughesba765722014-02-25 15:32:01 -080040#if defined(__LP64__)
41#define PAD_PTR "016" PRIxPTR
42#else
43#define PAD_PTR "08" PRIxPTR
44#endif
45
Elliott Hughes1e980b62013-01-17 18:36:06 -080046/* depends how the system includes define this */
47#ifdef HAVE_UNWIND_CONTEXT_STRUCT
48typedef struct _Unwind_Context __unwind_context;
49#else
50typedef _Unwind_Context __unwind_context;
51#endif
52
Elliott Hughes35b621c2013-01-28 16:27:36 -080053static mapinfo_t* gMapInfo = NULL;
54static void* gDemangler;
55typedef char* (*DemanglerFn)(const char*, char*, size_t*, int*);
56static DemanglerFn gDemanglerFn = NULL;
57
58__LIBC_HIDDEN__ void backtrace_startup() {
59 gMapInfo = mapinfo_create(getpid());
60 gDemangler = dlopen("libgccdemangle.so", RTLD_NOW);
61 if (gDemangler != NULL) {
62 void* sym = dlsym(gDemangler, "__cxa_demangle");
63 gDemanglerFn = reinterpret_cast<DemanglerFn>(sym);
64 }
65}
66
67__LIBC_HIDDEN__ void backtrace_shutdown() {
68 mapinfo_destroy(gMapInfo);
69 dlclose(gDemangler);
70}
71
72static char* demangle(const char* symbol) {
73 if (gDemanglerFn == NULL) {
74 return NULL;
75 }
76 return (*gDemanglerFn)(symbol, NULL, NULL, NULL);
77}
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
Stephen Hines5f6cfce2013-10-01 18:20:51 -070090#if defined(__arm__) && !defined(_Unwind_GetIP)
91// Older versions of Clang don't provide a definition of _Unwind_GetIP(), so
92// we include an appropriate version of our own. Once we have updated to
93// Clang 3.4, this code can be removed.
94static __inline__
95uintptr_t _Unwind_GetIP(struct _Unwind_Context *__context) {
96 uintptr_t __ip = 0;
97 _Unwind_VRS_Get(__context, _UVRSC_CORE, 15, _UVRSD_UINT32, &__ip);
98 return __ip & ~0x1;
99}
100#endif
101
Elliott Hughes1e980b62013-01-17 18:36:06 -0800102static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
103 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800104
105 uintptr_t ip = _Unwind_GetIP(context);
106
107 // The first stack frame is get_backtrace itself. Skip it.
108 if (ip != 0 && !state->have_skipped_self) {
109 state->have_skipped_self = true;
110 return _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800111 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800112
Ben Cheng52171b92013-05-07 14:22:43 -0700113#ifdef __arm__
114 /*
115 * The instruction pointer is pointing at the instruction after the bl(x), and
116 * the _Unwind_Backtrace routine already masks the Thumb mode indicator (LSB
117 * in PC). So we need to do a quick check here to find out if the previous
118 * instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise 4 from PC.
119 */
120 if (ip != 0) {
121 short* ptr = reinterpret_cast<short*>(ip);
122 // Thumb BLX(2)
Ben Cheng63dd03c2013-05-07 16:53:33 -0700123 if ((*(ptr-1) & 0xff80) == 0x4780) {
Ben Cheng52171b92013-05-07 14:22:43 -0700124 ip -= 2;
125 } else {
126 ip -= 4;
127 }
128 }
129#endif
130
Elliott Hughes35b621c2013-01-28 16:27:36 -0800131 state->frames[state->frame_count++] = ip;
132 return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800133}
134
Elliott Hughes35b621c2013-01-28 16:27:36 -0800135__LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) {
136 stack_crawl_state_t state(frames, max_depth);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800137 _Unwind_Backtrace(trace_function, &state);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800138 return state.frame_count;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800139}
140
Elliott Hughes35b621c2013-01-28 16:27:36 -0800141__LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800142 uintptr_t self_bt[16];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800143 if (frames == NULL) {
144 frame_count = get_backtrace(self_bt, 16);
145 frames = self_bt;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800146 }
147
148 __libc_format_log(ANDROID_LOG_ERROR, "libc",
149 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
150
Elliott Hughes35b621c2013-01-28 16:27:36 -0800151 for (size_t i = 0 ; i < frame_count; ++i) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700152 uintptr_t offset = 0;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800153 const char* symbol = NULL;
154
155 Dl_info info;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800156 if (dladdr((void*) frames[i], &info) != 0) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700157 offset = reinterpret_cast<uintptr_t>(info.dli_saddr);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800158 symbol = info.dli_sname;
159 }
160
Elliott Hughes35b621c2013-01-28 16:27:36 -0800161 uintptr_t rel_pc;
162 const mapinfo_t* mi = (gMapInfo != NULL) ? mapinfo_find(gMapInfo, frames[i], &rel_pc) : NULL;
163 const char* soname = (mi != NULL) ? mi->name : info.dli_fname;
164 if (soname == NULL) {
165 soname = "<unknown>";
166 }
167 if (symbol != NULL) {
168 // TODO: we might need a flag to say whether it's safe to allocate (demangling allocates).
169 char* demangled_symbol = demangle(symbol);
170 const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol;
171
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700172 __libc_format_log(ANDROID_LOG_ERROR, "libc",
Elliott Hughesba765722014-02-25 15:32:01 -0800173 " #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")",
174 i, rel_pc, soname, best_name, frames[i] - offset);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800175
176 free(demangled_symbol);
177 } else {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700178 __libc_format_log(ANDROID_LOG_ERROR, "libc",
Elliott Hughesba765722014-02-25 15:32:01 -0800179 " #%02zd pc %" PAD_PTR " %s",
180 i, rel_pc, soname);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800181 }
182 }
183}