blob: 9d53ad2b3e6aa38344eb768f650f45d6f74ea15b [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
36#include "debug_format.h"
37#include "debug_mapinfo.h"
38#include "logd.h"
39
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
Elliott Hughes1e980b62013-01-17 18:36:06 -080084static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
85 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
Elliott Hughes35b621c2013-01-28 16:27:36 -080086
87 uintptr_t ip = _Unwind_GetIP(context);
88
89 // The first stack frame is get_backtrace itself. Skip it.
90 if (ip != 0 && !state->have_skipped_self) {
91 state->have_skipped_self = true;
92 return _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -080093 }
Elliott Hughes35b621c2013-01-28 16:27:36 -080094
95 state->frames[state->frame_count++] = ip;
96 return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -080097}
98
Elliott Hughes35b621c2013-01-28 16:27:36 -080099__LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) {
100 stack_crawl_state_t state(frames, max_depth);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800101 _Unwind_Backtrace(trace_function, &state);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800102 return state.frame_count;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800103}
104
Elliott Hughes35b621c2013-01-28 16:27:36 -0800105__LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800106 uintptr_t self_bt[16];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800107 if (frames == NULL) {
108 frame_count = get_backtrace(self_bt, 16);
109 frames = self_bt;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800110 }
111
112 __libc_format_log(ANDROID_LOG_ERROR, "libc",
113 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
114
Elliott Hughes35b621c2013-01-28 16:27:36 -0800115 for (size_t i = 0 ; i < frame_count; ++i) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800116 void* offset = 0;
117 const char* symbol = NULL;
118
119 Dl_info info;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800120 if (dladdr((void*) frames[i], &info) != 0) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800121 offset = info.dli_saddr;
122 symbol = info.dli_sname;
123 }
124
Elliott Hughes35b621c2013-01-28 16:27:36 -0800125 uintptr_t rel_pc;
126 const mapinfo_t* mi = (gMapInfo != NULL) ? mapinfo_find(gMapInfo, frames[i], &rel_pc) : NULL;
127 const char* soname = (mi != NULL) ? mi->name : info.dli_fname;
128 if (soname == NULL) {
129 soname = "<unknown>";
130 }
131 if (symbol != NULL) {
132 // TODO: we might need a flag to say whether it's safe to allocate (demangling allocates).
133 char* demangled_symbol = demangle(symbol);
134 const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol;
135
136 __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s (%s+0x%x)",
137 i, rel_pc, soname, best_name, frames[i] - (uintptr_t) offset);
138
139 free(demangled_symbol);
140 } else {
141 __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s",
142 i, rel_pc, soname);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800143 }
144 }
145}