blob: 5c8220564cdb5d8b6dff89ded209a819a32cc159 [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
47static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
48 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
49 if (state->count) {
Elliott Hughes239e7a02013-01-25 17:13:45 -080050 uintptr_t ip = _Unwind_GetIP(context);
Elliott Hughes1e980b62013-01-17 18:36:06 -080051 if (ip) {
52 state->addrs[0] = ip;
53 state->addrs++;
54 state->count--;
55 return _URC_NO_REASON;
56 }
57 }
58 // If we run out of space to record the address or 0 has been seen, stop
59 // unwinding the stack.
60 return _URC_END_OF_STACK;
61}
62
Elliott Hughes239e7a02013-01-25 17:13:45 -080063__LIBC_HIDDEN__ int get_backtrace(uintptr_t* addrs, size_t max_entries) {
Elliott Hughes1e980b62013-01-17 18:36:06 -080064 stack_crawl_state_t state;
65 state.count = max_entries;
66 state.addrs = addrs;
67 _Unwind_Backtrace(trace_function, &state);
68 return max_entries - state.count;
69}
70
Elliott Hughes239e7a02013-01-25 17:13:45 -080071__LIBC_HIDDEN__ void log_backtrace(mapinfo_t* map_info, uintptr_t* addrs, size_t c) {
72 uintptr_t self_bt[16];
Elliott Hughes1e980b62013-01-17 18:36:06 -080073 if (addrs == NULL) {
74 c = get_backtrace(self_bt, 16);
75 addrs = self_bt;
76 }
77
78 __libc_format_log(ANDROID_LOG_ERROR, "libc",
79 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
80
81 int index = 0;
82 for (size_t i = 0 ; i < c; ++i) {
83 void* offset = 0;
84 const char* symbol = NULL;
85
86 Dl_info info;
87 if (dladdr((void*) addrs[i], &info) != 0) {
88 offset = info.dli_saddr;
89 symbol = info.dli_sname;
90 }
91
92 // This test is a bit sketchy, but it allows us to skip the
93 // stack trace entries due to this debugging code. it works
94 // because those don't have a symbol (they're not exported).
95 if (symbol != NULL || index > 0) {
96 unsigned int rel_pc;
97 const mapinfo_t* mi = mapinfo_find(map_info, addrs[i], &rel_pc);
98 const char* soname = mi ? mi->name : info.dli_fname;
99 if (soname == NULL) {
100 soname = "unknown";
101 }
102 if (symbol) {
103 __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s (%s+0x%x)",
Elliott Hughes239e7a02013-01-25 17:13:45 -0800104 index, rel_pc, soname, symbol, addrs[i] - (uintptr_t) offset);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800105 } else {
106 __libc_format_log(ANDROID_LOG_ERROR, "libc", " #%02d pc %08x %s",
107 index, rel_pc, soname);
108 }
109 ++index;
110 }
111 }
112}