blob: fb933e6f9617546625d54dfe898782412214fb95 [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
Elliott Hughes1e980b62013-01-17 18:36:06 -080083static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
84 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
Elliott Hughes35b621c2013-01-28 16:27:36 -080085
86 uintptr_t ip = _Unwind_GetIP(context);
87
88 // The first stack frame is get_backtrace itself. Skip it.
89 if (ip != 0 && !state->have_skipped_self) {
90 state->have_skipped_self = true;
91 return _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -080092 }
Elliott Hughes35b621c2013-01-28 16:27:36 -080093
Ben Cheng52171b92013-05-07 14:22:43 -070094#ifdef __arm__
95 /*
96 * The instruction pointer is pointing at the instruction after the bl(x), and
97 * the _Unwind_Backtrace routine already masks the Thumb mode indicator (LSB
98 * in PC). So we need to do a quick check here to find out if the previous
99 * instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise 4 from PC.
100 */
101 if (ip != 0) {
102 short* ptr = reinterpret_cast<short*>(ip);
103 // Thumb BLX(2)
Ben Cheng63dd03c2013-05-07 16:53:33 -0700104 if ((*(ptr-1) & 0xff80) == 0x4780) {
Ben Cheng52171b92013-05-07 14:22:43 -0700105 ip -= 2;
106 } else {
107 ip -= 4;
108 }
109 }
110#endif
111
Elliott Hughes35b621c2013-01-28 16:27:36 -0800112 state->frames[state->frame_count++] = ip;
113 return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800114}
115
Elliott Hughes35b621c2013-01-28 16:27:36 -0800116__LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) {
117 stack_crawl_state_t state(frames, max_depth);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800118 _Unwind_Backtrace(trace_function, &state);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800119 return state.frame_count;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800120}
121
Elliott Hughes35b621c2013-01-28 16:27:36 -0800122__LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800123 uintptr_t self_bt[16];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800124 if (frames == NULL) {
125 frame_count = get_backtrace(self_bt, 16);
126 frames = self_bt;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800127 }
128
129 __libc_format_log(ANDROID_LOG_ERROR, "libc",
130 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
131
Elliott Hughes35b621c2013-01-28 16:27:36 -0800132 for (size_t i = 0 ; i < frame_count; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800133 void* offset = 0;
134 const char* symbol = NULL;
135
136 Dl_info info;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800137 if (dladdr((void*) frames[i], &info) != 0) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800138 offset = info.dli_saddr;
139 symbol = info.dli_sname;
140 }
141
Elliott Hughes35b621c2013-01-28 16:27:36 -0800142 uintptr_t rel_pc;
143 const mapinfo_t* mi = (gMapInfo != NULL) ? mapinfo_find(gMapInfo, frames[i], &rel_pc) : NULL;
144 const char* soname = (mi != NULL) ? mi->name : info.dli_fname;
145 if (soname == NULL) {
146 soname = "<unknown>";
147 }
148 if (symbol != NULL) {
149 // TODO: we might need a flag to say whether it's safe to allocate (demangling allocates).
150 char* demangled_symbol = demangle(symbol);
151 const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol;
152
153 __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s (%s+0x%x)",
154 i, rel_pc, soname, best_name, frames[i] - (uintptr_t) offset);
155
156 free(demangled_symbol);
157 } else {
158 __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s",
159 i, rel_pc, soname);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800160 }
161 }
162}