blob: e5f7c3deecce47fe185012c523a56a526e9c9202 [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
Pavel Chupinc6c194c2013-11-21 23:17:20 +040017#include <sys/ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070018#include <sys/types.h>
19
Christopher Ferris46756822014-01-14 20:16:30 -080020#include <backtrace/Backtrace.h>
21#include <backtrace/BacktraceMap.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070022
23#define UNW_LOCAL_ONLY
24#include <libunwind.h>
25
Christopher Ferrise2960912014-03-07 19:42:19 -080026#include "BacktraceLog.h"
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070027#include "BacktraceThread.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070028#include "UnwindCurrent.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080029#include "UnwindMap.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070030
Christopher Ferris17e91d42013-10-21 13:30:52 -070031//-------------------------------------------------------------------------
32// UnwindCurrent functions.
33//-------------------------------------------------------------------------
34UnwindCurrent::UnwindCurrent() {
35}
36
37UnwindCurrent::~UnwindCurrent() {
38}
39
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070040bool UnwindCurrent::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
41 if (!ucontext) {
42 int ret = unw_getcontext(&context_);
43 if (ret < 0) {
44 BACK_LOGW("unw_getcontext failed %d", ret);
45 return false;
46 }
47 }
48 else {
49 GetUnwContextFromUcontext(ucontext);
Christopher Ferris17e91d42013-10-21 13:30:52 -070050 }
Christopher Ferris17224d52014-04-11 13:05:07 -070051 return UnwindFromContext(num_ignore_frames, false);
Christopher Ferris17e91d42013-10-21 13:30:52 -070052}
53
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070054void UnwindCurrent::GetUnwContextFromUcontext(const ucontext_t* ucontext) {
55 unw_tdep_context_t* unw_context = reinterpret_cast<unw_tdep_context_t*>(&context_);
56
57#if defined(__arm__)
58 unw_context->regs[0] = ucontext->uc_mcontext.arm_r0;
59 unw_context->regs[1] = ucontext->uc_mcontext.arm_r1;
60 unw_context->regs[2] = ucontext->uc_mcontext.arm_r2;
61 unw_context->regs[3] = ucontext->uc_mcontext.arm_r3;
62 unw_context->regs[4] = ucontext->uc_mcontext.arm_r4;
63 unw_context->regs[5] = ucontext->uc_mcontext.arm_r5;
64 unw_context->regs[6] = ucontext->uc_mcontext.arm_r6;
65 unw_context->regs[7] = ucontext->uc_mcontext.arm_r7;
66 unw_context->regs[8] = ucontext->uc_mcontext.arm_r8;
67 unw_context->regs[9] = ucontext->uc_mcontext.arm_r9;
68 unw_context->regs[10] = ucontext->uc_mcontext.arm_r10;
69 unw_context->regs[11] = ucontext->uc_mcontext.arm_fp;
70 unw_context->regs[12] = ucontext->uc_mcontext.arm_ip;
71 unw_context->regs[13] = ucontext->uc_mcontext.arm_sp;
72 unw_context->regs[14] = ucontext->uc_mcontext.arm_lr;
73 unw_context->regs[15] = ucontext->uc_mcontext.arm_pc;
74#else
75 unw_context->uc_mcontext = ucontext->uc_mcontext;
76#endif
77}
78
Christopher Ferris17e91d42013-10-21 13:30:52 -070079std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
80 *offset = 0;
81 char buf[512];
82 unw_word_t value;
83 if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
84 &value, &context_) >= 0 && buf[0] != '\0') {
85 *offset = static_cast<uintptr_t>(value);
86 return buf;
87 }
88 return "";
89}
90
Christopher Ferris17224d52014-04-11 13:05:07 -070091bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, bool within_handler) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070092 // The cursor structure is pretty large, do not put it on the stack.
93 unw_cursor_t* cursor = new unw_cursor_t;
94 int ret = unw_init_local(cursor, &context_);
95 if (ret < 0) {
Christopher Ferris17224d52014-04-11 13:05:07 -070096 if (!within_handler) {
97 BACK_LOGW("unw_init_local failed %d", ret);
98 }
Christopher Ferrisddc4f092014-01-07 14:31:07 -080099 delete cursor;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700100 return false;
101 }
102
Christopher Ferris46756822014-01-14 20:16:30 -0800103 std::vector<backtrace_frame_data_t>* frames = GetFrames();
104 frames->reserve(MAX_BACKTRACE_FRAMES);
105 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700106 do {
107 unw_word_t pc;
108 ret = unw_get_reg(cursor, UNW_REG_IP, &pc);
109 if (ret < 0) {
Christopher Ferris17224d52014-04-11 13:05:07 -0700110 if (!within_handler) {
111 BACK_LOGW("Failed to read IP %d", ret);
112 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700113 break;
114 }
115 unw_word_t sp;
116 ret = unw_get_reg(cursor, UNW_REG_SP, &sp);
117 if (ret < 0) {
Christopher Ferris17224d52014-04-11 13:05:07 -0700118 if (!within_handler) {
119 BACK_LOGW("Failed to read SP %d", ret);
120 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700121 break;
122 }
123
124 if (num_ignore_frames == 0) {
Christopher Ferris46756822014-01-14 20:16:30 -0800125 frames->resize(num_frames+1);
126 backtrace_frame_data_t* frame = &frames->at(num_frames);
Christopher Ferris20303f82014-01-10 16:33:16 -0800127 frame->num = num_frames;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700128 frame->pc = static_cast<uintptr_t>(pc);
129 frame->sp = static_cast<uintptr_t>(sp);
130 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700131
132 if (num_frames > 0) {
133 // Set the stack size for the previous frame.
Christopher Ferris46756822014-01-14 20:16:30 -0800134 backtrace_frame_data_t* prev = &frames->at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700135 prev->stack_size = frame->sp - prev->sp;
136 }
137
Christopher Ferris17224d52014-04-11 13:05:07 -0700138 if (!within_handler) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800139 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
140 frame->map = FindMap(frame->pc);
Christopher Ferris46756822014-01-14 20:16:30 -0800141 } else {
142 frame->map = NULL;
143 frame->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700144 }
Christopher Ferris46756822014-01-14 20:16:30 -0800145 num_frames++;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700146 } else {
147 num_ignore_frames--;
148 }
149 ret = unw_step (cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800150 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700151
152 delete cursor;
153 return true;
154}
155
Christopher Ferris17e91d42013-10-21 13:30:52 -0700156//-------------------------------------------------------------------------
157// C++ object creation function.
158//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800159Backtrace* CreateCurrentObj(BacktraceMap* map) {
160 return new BacktraceCurrent(new UnwindCurrent(), map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700161}
162
Christopher Ferris46756822014-01-14 20:16:30 -0800163Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -0700164 return new BacktraceThread(new UnwindCurrent(), tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700165}