blob: d1195ee5f7c055fd73a1895fa30db278529d9495 [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"
28
Christopher Ferris8ed46272013-10-29 15:44:25 -070029// Define the ucontext_t structures needed for each supported arch.
Christopher Ferris17e91d42013-10-21 13:30:52 -070030#if defined(__arm__)
Christopher Ferris8ed46272013-10-29 15:44:25 -070031 // The current version of the <signal.h> doesn't define ucontext_t.
Christopher Ferris17e91d42013-10-21 13:30:52 -070032 #include <asm/sigcontext.h> // Ensure 'struct sigcontext' is defined.
33
34 // Machine context at the time a signal was raised.
35 typedef struct ucontext {
36 uint32_t uc_flags;
37 struct ucontext* uc_link;
38 stack_t uc_stack;
39 struct sigcontext uc_mcontext;
40 uint32_t uc_sigmask;
41 } ucontext_t;
Christopher Ferris8ed46272013-10-29 15:44:25 -070042#elif defined(__i386__)
43 #include <asm/sigcontext.h>
44 #include <asm/ucontext.h>
45 typedef struct ucontext ucontext_t;
Christopher Ferrisedbe3b42014-01-27 10:50:58 -080046#elif !defined(__mips__) && !defined(__aarch64__)
Christopher Ferris8ed46272013-10-29 15:44:25 -070047 #error Unsupported architecture.
48#endif
Christopher Ferris17e91d42013-10-21 13:30:52 -070049
50//-------------------------------------------------------------------------
51// UnwindCurrent functions.
52//-------------------------------------------------------------------------
53UnwindCurrent::UnwindCurrent() {
54}
55
56UnwindCurrent::~UnwindCurrent() {
57}
58
59bool UnwindCurrent::Unwind(size_t num_ignore_frames) {
60 int ret = unw_getcontext(&context_);
61 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070062 BACK_LOGW("unw_getcontext failed %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070063 return false;
64 }
65 return UnwindFromContext(num_ignore_frames, true);
66}
67
68std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
69 *offset = 0;
70 char buf[512];
71 unw_word_t value;
72 if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
73 &value, &context_) >= 0 && buf[0] != '\0') {
74 *offset = static_cast<uintptr_t>(value);
75 return buf;
76 }
77 return "";
78}
79
80bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, bool resolve) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070081 // The cursor structure is pretty large, do not put it on the stack.
82 unw_cursor_t* cursor = new unw_cursor_t;
83 int ret = unw_init_local(cursor, &context_);
84 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070085 BACK_LOGW("unw_init_local failed %d", ret);
Christopher Ferrisddc4f092014-01-07 14:31:07 -080086 delete cursor;
Christopher Ferris17e91d42013-10-21 13:30:52 -070087 return false;
88 }
89
Christopher Ferris46756822014-01-14 20:16:30 -080090 std::vector<backtrace_frame_data_t>* frames = GetFrames();
91 frames->reserve(MAX_BACKTRACE_FRAMES);
92 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070093 do {
94 unw_word_t pc;
95 ret = unw_get_reg(cursor, UNW_REG_IP, &pc);
96 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070097 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070098 break;
99 }
100 unw_word_t sp;
101 ret = unw_get_reg(cursor, UNW_REG_SP, &sp);
102 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700103 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700104 break;
105 }
106
107 if (num_ignore_frames == 0) {
Christopher Ferris46756822014-01-14 20:16:30 -0800108 frames->resize(num_frames+1);
109 backtrace_frame_data_t* frame = &frames->at(num_frames);
Christopher Ferris20303f82014-01-10 16:33:16 -0800110 frame->num = num_frames;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700111 frame->pc = static_cast<uintptr_t>(pc);
112 frame->sp = static_cast<uintptr_t>(sp);
113 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700114
115 if (num_frames > 0) {
116 // Set the stack size for the previous frame.
Christopher Ferris46756822014-01-14 20:16:30 -0800117 backtrace_frame_data_t* prev = &frames->at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700118 prev->stack_size = frame->sp - prev->sp;
119 }
120
121 if (resolve) {
Christopher Ferris46756822014-01-14 20:16:30 -0800122 frame->func_name = backtrace_obj_->GetFunctionName(frame->pc, &frame->func_offset);
123 frame->map = backtrace_obj_->FindMap(frame->pc);
124 } else {
125 frame->map = NULL;
126 frame->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700127 }
Christopher Ferris46756822014-01-14 20:16:30 -0800128 num_frames++;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700129 } else {
130 num_ignore_frames--;
131 }
132 ret = unw_step (cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800133 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700134
135 delete cursor;
136 return true;
137}
138
139void UnwindCurrent::ExtractContext(void* sigcontext) {
140 unw_tdep_context_t* context = reinterpret_cast<unw_tdep_context_t*>(&context_);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700141 const ucontext_t* uc = reinterpret_cast<const ucontext_t*>(sigcontext);
142
Christopher Ferris8ed46272013-10-29 15:44:25 -0700143#if defined(__arm__)
Christopher Ferris17e91d42013-10-21 13:30:52 -0700144 context->regs[0] = uc->uc_mcontext.arm_r0;
145 context->regs[1] = uc->uc_mcontext.arm_r1;
146 context->regs[2] = uc->uc_mcontext.arm_r2;
147 context->regs[3] = uc->uc_mcontext.arm_r3;
148 context->regs[4] = uc->uc_mcontext.arm_r4;
149 context->regs[5] = uc->uc_mcontext.arm_r5;
150 context->regs[6] = uc->uc_mcontext.arm_r6;
151 context->regs[7] = uc->uc_mcontext.arm_r7;
152 context->regs[8] = uc->uc_mcontext.arm_r8;
153 context->regs[9] = uc->uc_mcontext.arm_r9;
154 context->regs[10] = uc->uc_mcontext.arm_r10;
155 context->regs[11] = uc->uc_mcontext.arm_fp;
156 context->regs[12] = uc->uc_mcontext.arm_ip;
157 context->regs[13] = uc->uc_mcontext.arm_sp;
158 context->regs[14] = uc->uc_mcontext.arm_lr;
159 context->regs[15] = uc->uc_mcontext.arm_pc;
Christopher Ferrisddc4f092014-01-07 14:31:07 -0800160#elif defined(__mips__) || defined(__i386__)
161 context->uc_mcontext = uc->uc_mcontext;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700162#endif
163}
164
165//-------------------------------------------------------------------------
166// UnwindThread functions.
167//-------------------------------------------------------------------------
168UnwindThread::UnwindThread() {
169}
170
171UnwindThread::~UnwindThread() {
172}
173
174bool UnwindThread::Init() {
175 return true;
176}
177
178void UnwindThread::ThreadUnwind(
179 siginfo_t* /*siginfo*/, void* sigcontext, size_t num_ignore_frames) {
180 ExtractContext(sigcontext);
181 UnwindFromContext(num_ignore_frames, false);
182}
183
184//-------------------------------------------------------------------------
185// C++ object creation function.
186//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800187Backtrace* CreateCurrentObj(BacktraceMap* map) {
188 return new BacktraceCurrent(new UnwindCurrent(), map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700189}
190
Christopher Ferris46756822014-01-14 20:16:30 -0800191Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700192 UnwindThread* thread_obj = new UnwindThread();
Christopher Ferris46756822014-01-14 20:16:30 -0800193 return new BacktraceThread(thread_obj, thread_obj, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700194}