blob: 034b73c24e2e11139690e495afc2cb62145d3dd6 [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 Ferris17e91d42013-10-21 13:30:52 -070027#include "UnwindCurrent.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080028#include "UnwindMap.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070029
Christopher Ferris17e91d42013-10-21 13:30:52 -070030//-------------------------------------------------------------------------
31// UnwindCurrent functions.
32//-------------------------------------------------------------------------
33UnwindCurrent::UnwindCurrent() {
34}
35
36UnwindCurrent::~UnwindCurrent() {
37}
38
39bool UnwindCurrent::Unwind(size_t num_ignore_frames) {
40 int ret = unw_getcontext(&context_);
41 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070042 BACK_LOGW("unw_getcontext failed %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070043 return false;
44 }
45 return UnwindFromContext(num_ignore_frames, true);
46}
47
48std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
49 *offset = 0;
50 char buf[512];
51 unw_word_t value;
52 if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
53 &value, &context_) >= 0 && buf[0] != '\0') {
54 *offset = static_cast<uintptr_t>(value);
55 return buf;
56 }
57 return "";
58}
59
60bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, bool resolve) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070061 // The cursor structure is pretty large, do not put it on the stack.
62 unw_cursor_t* cursor = new unw_cursor_t;
63 int ret = unw_init_local(cursor, &context_);
64 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070065 BACK_LOGW("unw_init_local failed %d", ret);
Christopher Ferrisddc4f092014-01-07 14:31:07 -080066 delete cursor;
Christopher Ferris17e91d42013-10-21 13:30:52 -070067 return false;
68 }
69
Christopher Ferris46756822014-01-14 20:16:30 -080070 std::vector<backtrace_frame_data_t>* frames = GetFrames();
71 frames->reserve(MAX_BACKTRACE_FRAMES);
72 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070073 do {
74 unw_word_t pc;
75 ret = unw_get_reg(cursor, UNW_REG_IP, &pc);
76 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070077 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070078 break;
79 }
80 unw_word_t sp;
81 ret = unw_get_reg(cursor, UNW_REG_SP, &sp);
82 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070083 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070084 break;
85 }
86
87 if (num_ignore_frames == 0) {
Christopher Ferris46756822014-01-14 20:16:30 -080088 frames->resize(num_frames+1);
89 backtrace_frame_data_t* frame = &frames->at(num_frames);
Christopher Ferris20303f82014-01-10 16:33:16 -080090 frame->num = num_frames;
Christopher Ferris17e91d42013-10-21 13:30:52 -070091 frame->pc = static_cast<uintptr_t>(pc);
92 frame->sp = static_cast<uintptr_t>(sp);
93 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070094
95 if (num_frames > 0) {
96 // Set the stack size for the previous frame.
Christopher Ferris46756822014-01-14 20:16:30 -080097 backtrace_frame_data_t* prev = &frames->at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -070098 prev->stack_size = frame->sp - prev->sp;
99 }
100
101 if (resolve) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800102 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
103 frame->map = FindMap(frame->pc);
Christopher Ferris46756822014-01-14 20:16:30 -0800104 } else {
105 frame->map = NULL;
106 frame->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700107 }
Christopher Ferris46756822014-01-14 20:16:30 -0800108 num_frames++;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700109 } else {
110 num_ignore_frames--;
111 }
112 ret = unw_step (cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800113 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700114
115 delete cursor;
116 return true;
117}
118
119void UnwindCurrent::ExtractContext(void* sigcontext) {
120 unw_tdep_context_t* context = reinterpret_cast<unw_tdep_context_t*>(&context_);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700121 const ucontext_t* uc = reinterpret_cast<const ucontext_t*>(sigcontext);
122
Christopher Ferris8ed46272013-10-29 15:44:25 -0700123#if defined(__arm__)
Christopher Ferris17e91d42013-10-21 13:30:52 -0700124 context->regs[0] = uc->uc_mcontext.arm_r0;
125 context->regs[1] = uc->uc_mcontext.arm_r1;
126 context->regs[2] = uc->uc_mcontext.arm_r2;
127 context->regs[3] = uc->uc_mcontext.arm_r3;
128 context->regs[4] = uc->uc_mcontext.arm_r4;
129 context->regs[5] = uc->uc_mcontext.arm_r5;
130 context->regs[6] = uc->uc_mcontext.arm_r6;
131 context->regs[7] = uc->uc_mcontext.arm_r7;
132 context->regs[8] = uc->uc_mcontext.arm_r8;
133 context->regs[9] = uc->uc_mcontext.arm_r9;
134 context->regs[10] = uc->uc_mcontext.arm_r10;
135 context->regs[11] = uc->uc_mcontext.arm_fp;
136 context->regs[12] = uc->uc_mcontext.arm_ip;
137 context->regs[13] = uc->uc_mcontext.arm_sp;
138 context->regs[14] = uc->uc_mcontext.arm_lr;
139 context->regs[15] = uc->uc_mcontext.arm_pc;
Christopher Ferris5c400192014-01-30 10:30:36 -0800140#else
Christopher Ferrisddc4f092014-01-07 14:31:07 -0800141 context->uc_mcontext = uc->uc_mcontext;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700142#endif
143}
144
145//-------------------------------------------------------------------------
146// UnwindThread functions.
147//-------------------------------------------------------------------------
148UnwindThread::UnwindThread() {
149}
150
151UnwindThread::~UnwindThread() {
152}
153
Christopher Ferris17e91d42013-10-21 13:30:52 -0700154void UnwindThread::ThreadUnwind(
155 siginfo_t* /*siginfo*/, void* sigcontext, size_t num_ignore_frames) {
156 ExtractContext(sigcontext);
157 UnwindFromContext(num_ignore_frames, false);
158}
159
160//-------------------------------------------------------------------------
161// C++ object creation function.
162//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800163Backtrace* CreateCurrentObj(BacktraceMap* map) {
164 return new BacktraceCurrent(new UnwindCurrent(), map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700165}
166
Christopher Ferris46756822014-01-14 20:16:30 -0800167Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700168 UnwindThread* thread_obj = new UnwindThread();
Christopher Ferris46756822014-01-14 20:16:30 -0800169 return new BacktraceThread(thread_obj, thread_obj, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700170}