blob: 41009117b59ccfadec89795b9fbc6ae53d4e74bd [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 Hughes05fc1d72015-01-28 18:02:33 -080033#include <malloc.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080034#include <unistd.h>
35#include <unwind.h>
36#include <sys/types.h>
37
Elliott Hughes1e980b62013-01-17 18:36:06 -080038#include "debug_mapinfo.h"
Christopher Ferris861c0ef2014-07-24 17:52:23 -070039#include "malloc_debug_disable.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070040#include "private/libc_logging.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080041
Elliott Hughesba765722014-02-25 15:32:01 -080042#if defined(__LP64__)
43#define PAD_PTR "016" PRIxPTR
44#else
45#define PAD_PTR "08" PRIxPTR
46#endif
47
Elliott Hughes1e980b62013-01-17 18:36:06 -080048typedef struct _Unwind_Context __unwind_context;
Elliott Hughes1e980b62013-01-17 18:36:06 -080049
Elliott Hughes6e54c3e2015-02-05 12:05:34 -080050extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
51
Elliott Hughes1728b232014-05-14 10:02:03 -070052static mapinfo_t* g_map_info = NULL;
Elliott Hughes35b621c2013-01-28 16:27:36 -080053
54__LIBC_HIDDEN__ void backtrace_startup() {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070055 ScopedDisableDebugCalls disable;
56
Elliott Hughes1728b232014-05-14 10:02:03 -070057 g_map_info = mapinfo_create(getpid());
Elliott Hughes35b621c2013-01-28 16:27:36 -080058}
59
60__LIBC_HIDDEN__ void backtrace_shutdown() {
Christopher Ferris861c0ef2014-07-24 17:52:23 -070061 ScopedDisableDebugCalls disable;
62
Elliott Hughes1728b232014-05-14 10:02:03 -070063 mapinfo_destroy(g_map_info);
Elliott Hughes35b621c2013-01-28 16:27:36 -080064}
65
66struct stack_crawl_state_t {
67 uintptr_t* frames;
68 size_t frame_count;
69 size_t max_depth;
70 bool have_skipped_self;
71
72 stack_crawl_state_t(uintptr_t* frames, size_t max_depth)
73 : frames(frames), frame_count(0), max_depth(max_depth), have_skipped_self(false) {
74 }
75};
76
Elliott Hughes1e980b62013-01-17 18:36:06 -080077static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
78 stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
Elliott Hughes35b621c2013-01-28 16:27:36 -080079
80 uintptr_t ip = _Unwind_GetIP(context);
81
82 // The first stack frame is get_backtrace itself. Skip it.
83 if (ip != 0 && !state->have_skipped_self) {
84 state->have_skipped_self = true;
85 return _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -080086 }
Elliott Hughes35b621c2013-01-28 16:27:36 -080087
Christopher Ferris224bef82015-08-18 15:41:31 -070088 // The instruction pointer is pointing at the instruction after the return
89 // call on all architectures.
90 // Modify the pc to point at the real function.
Ben Cheng52171b92013-05-07 14:22:43 -070091 if (ip != 0) {
Christopher Ferris224bef82015-08-18 15:41:31 -070092#if defined(__arm__)
93 // We need to do a quick check here to find out if the previous
94 // instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise
95 // 4 from PC.
Ben Cheng52171b92013-05-07 14:22:43 -070096 short* ptr = reinterpret_cast<short*>(ip);
97 // Thumb BLX(2)
Ben Cheng63dd03c2013-05-07 16:53:33 -070098 if ((*(ptr-1) & 0xff80) == 0x4780) {
Ben Cheng52171b92013-05-07 14:22:43 -070099 ip -= 2;
100 } else {
101 ip -= 4;
102 }
Christopher Ferris224bef82015-08-18 15:41:31 -0700103#elif defined(__aarch64__)
104 // All instructions are 4 bytes long, skip back one instruction.
105 ip -= 4;
106#elif defined(__i386__) || defined(__x86_64__)
107 // It's difficult to decode exactly where the previous instruction is,
108 // so subtract 1 to estimate where the instruction lives.
109 ip--;
Ben Cheng52171b92013-05-07 14:22:43 -0700110#endif
Christopher Ferris224bef82015-08-18 15:41:31 -0700111 }
Ben Cheng52171b92013-05-07 14:22:43 -0700112
Elliott Hughes35b621c2013-01-28 16:27:36 -0800113 state->frames[state->frame_count++] = ip;
114 return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800115}
116
Elliott Hughes35b621c2013-01-28 16:27:36 -0800117__LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700118 ScopedDisableDebugCalls disable;
119
Elliott Hughes35b621c2013-01-28 16:27:36 -0800120 stack_crawl_state_t state(frames, max_depth);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800121 _Unwind_Backtrace(trace_function, &state);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800122 return state.frame_count;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800123}
124
Elliott Hughes35b621c2013-01-28 16:27:36 -0800125__LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700126 ScopedDisableDebugCalls disable;
127
Elliott Hughes239e7a02013-01-25 17:13:45 -0800128 uintptr_t self_bt[16];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800129 if (frames == NULL) {
130 frame_count = get_backtrace(self_bt, 16);
131 frames = self_bt;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800132 }
133
134 __libc_format_log(ANDROID_LOG_ERROR, "libc",
135 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
136
Elliott Hughes35b621c2013-01-28 16:27:36 -0800137 for (size_t i = 0 ; i < frame_count; ++i) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700138 uintptr_t offset = 0;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800139 const char* symbol = NULL;
140
141 Dl_info info;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800142 if (dladdr((void*) frames[i], &info) != 0) {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700143 offset = reinterpret_cast<uintptr_t>(info.dli_saddr);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800144 symbol = info.dli_sname;
145 }
146
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700147 uintptr_t rel_pc = offset;
Elliott Hughes1728b232014-05-14 10:02:03 -0700148 const mapinfo_t* mi = (g_map_info != NULL) ? mapinfo_find(g_map_info, frames[i], &rel_pc) : NULL;
Elliott Hughes35b621c2013-01-28 16:27:36 -0800149 const char* soname = (mi != NULL) ? mi->name : info.dli_fname;
150 if (soname == NULL) {
151 soname = "<unknown>";
152 }
153 if (symbol != NULL) {
Elliott Hughes6e54c3e2015-02-05 12:05:34 -0800154 char* demangled_symbol = __cxa_demangle(symbol, NULL, NULL, NULL);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800155 const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol;
156
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700157 __libc_format_log(ANDROID_LOG_ERROR, "libc",
Elliott Hughesba765722014-02-25 15:32:01 -0800158 " #%02zd pc %" PAD_PTR " %s (%s+%" PRIuPTR ")",
159 i, rel_pc, soname, best_name, frames[i] - offset);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800160
161 free(demangled_symbol);
162 } else {
Elliott Hughesc7c5f852013-10-08 17:02:26 -0700163 __libc_format_log(ANDROID_LOG_ERROR, "libc",
Elliott Hughesba765722014-02-25 15:32:01 -0800164 " #%02zd pc %" PAD_PTR " %s",
165 i, rel_pc, soname);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800166 }
167 }
168}