blob: 17b71b94d14593c43aaac8d508330d598cf82477 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "libbacktrace"
18
19#include <sys/types.h>
20
Christopher Ferris46756822014-01-14 20:16:30 -080021#include <backtrace/Backtrace.h>
22#include <backtrace/BacktraceMap.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070023
24#define UNW_LOCAL_ONLY
25#include <libunwind.h>
26
27#include "UnwindCurrent.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080028#include "UnwindMap.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070029
Christopher Ferris8ed46272013-10-29 15:44:25 -070030// Define the ucontext_t structures needed for each supported arch.
Christopher Ferris17e91d42013-10-21 13:30:52 -070031#if defined(__arm__)
Christopher Ferris8ed46272013-10-29 15:44:25 -070032 // The current version of the <signal.h> doesn't define ucontext_t.
Christopher Ferris17e91d42013-10-21 13:30:52 -070033 #include <asm/sigcontext.h> // Ensure 'struct sigcontext' is defined.
34
35 // Machine context at the time a signal was raised.
36 typedef struct ucontext {
37 uint32_t uc_flags;
38 struct ucontext* uc_link;
39 stack_t uc_stack;
40 struct sigcontext uc_mcontext;
41 uint32_t uc_sigmask;
42 } ucontext_t;
Christopher Ferris8ed46272013-10-29 15:44:25 -070043#elif defined(__i386__)
44 #include <asm/sigcontext.h>
45 #include <asm/ucontext.h>
46 typedef struct ucontext ucontext_t;
Christopher Ferrisedbe3b42014-01-27 10:50:58 -080047#elif !defined(__mips__) && !defined(__aarch64__)
Christopher Ferris8ed46272013-10-29 15:44:25 -070048 #error Unsupported architecture.
49#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -070050
51//-------------------------------------------------------------------------
52// UnwindCurrent functions.
53//-------------------------------------------------------------------------
54UnwindCurrent::UnwindCurrent() {
55}
56
57UnwindCurrent::~UnwindCurrent() {
58}
59
60bool UnwindCurrent::Unwind(size_t num_ignore_frames) {
61 int ret = unw_getcontext(&context_);
62 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070063 BACK_LOGW("unw_getcontext failed %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070064 return false;
65 }
66 return UnwindFromContext(num_ignore_frames, true);
67}
68
69std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
70 *offset = 0;
71 char buf[512];
72 unw_word_t value;
73 if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
74 &value, &context_) >= 0 && buf[0] != '\0') {
75 *offset = static_cast<uintptr_t>(value);
76 return buf;
77 }
78 return "";
79}
80
81bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, bool resolve) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070082 // The cursor structure is pretty large, do not put it on the stack.
83 unw_cursor_t* cursor = new unw_cursor_t;
84 int ret = unw_init_local(cursor, &context_);
85 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070086 BACK_LOGW("unw_init_local failed %d", ret);
Christopher Ferrisddc4f092014-01-07 14:31:07 -080087 delete cursor;
Christopher Ferris17e91d42013-10-21 13:30:52 -070088 return false;
89 }
90
Christopher Ferris46756822014-01-14 20:16:30 -080091 std::vector<backtrace_frame_data_t>* frames = GetFrames();
92 frames->reserve(MAX_BACKTRACE_FRAMES);
93 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070094 do {
95 unw_word_t pc;
96 ret = unw_get_reg(cursor, UNW_REG_IP, &pc);
97 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070098 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070099 break;
100 }
101 unw_word_t sp;
102 ret = unw_get_reg(cursor, UNW_REG_SP, &sp);
103 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700104 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700105 break;
106 }
107
108 if (num_ignore_frames == 0) {
Christopher Ferris46756822014-01-14 20:16:30 -0800109 frames->resize(num_frames+1);
110 backtrace_frame_data_t* frame = &frames->at(num_frames);
Christopher Ferris20303f82014-01-10 16:33:16 -0800111 frame->num = num_frames;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700112 frame->pc = static_cast<uintptr_t>(pc);
113 frame->sp = static_cast<uintptr_t>(sp);
114 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700115
116 if (num_frames > 0) {
117 // Set the stack size for the previous frame.
Christopher Ferris46756822014-01-14 20:16:30 -0800118 backtrace_frame_data_t* prev = &frames->at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700119 prev->stack_size = frame->sp - prev->sp;
120 }
121
122 if (resolve) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800123 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
124 frame->map = FindMap(frame->pc);
Christopher Ferris46756822014-01-14 20:16:30 -0800125 } else {
126 frame->map = NULL;
127 frame->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700128 }
Christopher Ferris46756822014-01-14 20:16:30 -0800129 num_frames++;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700130 } else {
131 num_ignore_frames--;
132 }
133 ret = unw_step (cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800134 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700135
136 delete cursor;
137 return true;
138}
139
140void UnwindCurrent::ExtractContext(void* sigcontext) {
141 unw_tdep_context_t* context = reinterpret_cast<unw_tdep_context_t*>(&context_);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700142 const ucontext_t* uc = reinterpret_cast<const ucontext_t*>(sigcontext);
143
Christopher Ferris8ed46272013-10-29 15:44:25 -0700144#if defined(__arm__)
Christopher Ferris17e91d42013-10-21 13:30:52 -0700145 context->regs[0] = uc->uc_mcontext.arm_r0;
146 context->regs[1] = uc->uc_mcontext.arm_r1;
147 context->regs[2] = uc->uc_mcontext.arm_r2;
148 context->regs[3] = uc->uc_mcontext.arm_r3;
149 context->regs[4] = uc->uc_mcontext.arm_r4;
150 context->regs[5] = uc->uc_mcontext.arm_r5;
151 context->regs[6] = uc->uc_mcontext.arm_r6;
152 context->regs[7] = uc->uc_mcontext.arm_r7;
153 context->regs[8] = uc->uc_mcontext.arm_r8;
154 context->regs[9] = uc->uc_mcontext.arm_r9;
155 context->regs[10] = uc->uc_mcontext.arm_r10;
156 context->regs[11] = uc->uc_mcontext.arm_fp;
157 context->regs[12] = uc->uc_mcontext.arm_ip;
158 context->regs[13] = uc->uc_mcontext.arm_sp;
159 context->regs[14] = uc->uc_mcontext.arm_lr;
160 context->regs[15] = uc->uc_mcontext.arm_pc;
Christopher Ferrisddc4f092014-01-07 14:31:07 -0800161#elif defined(__mips__) || defined(__i386__)
162 context->uc_mcontext = uc->uc_mcontext;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700163#endif
164}
165
166//-------------------------------------------------------------------------
167// UnwindThread functions.
168//-------------------------------------------------------------------------
169UnwindThread::UnwindThread() {
170}
171
172UnwindThread::~UnwindThread() {
173}
174
Christopher Ferris17e91d42013-10-21 13:30:52 -0700175void UnwindThread::ThreadUnwind(
176 siginfo_t* /*siginfo*/, void* sigcontext, size_t num_ignore_frames) {
177 ExtractContext(sigcontext);
178 UnwindFromContext(num_ignore_frames, false);
179}
180
181//-------------------------------------------------------------------------
182// C++ object creation function.
183//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800184Backtrace* CreateCurrentObj(BacktraceMap* map) {
185 return new BacktraceCurrent(new UnwindCurrent(), map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700186}
187
Christopher Ferris46756822014-01-14 20:16:30 -0800188Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700189 UnwindThread* thread_obj = new UnwindThread();
Christopher Ferris46756822014-01-14 20:16:30 -0800190 return new BacktraceThread(thread_obj, thread_obj, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700191}