blob: 461ed02cf2acdca24cdaaba9719454cfa87f2f6c [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 Ferris5c400192014-01-30 10:30:36 -080030#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070031
32//-------------------------------------------------------------------------
33// UnwindCurrent functions.
34//-------------------------------------------------------------------------
35UnwindCurrent::UnwindCurrent() {
36}
37
38UnwindCurrent::~UnwindCurrent() {
39}
40
41bool UnwindCurrent::Unwind(size_t num_ignore_frames) {
42 int ret = unw_getcontext(&context_);
43 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070044 BACK_LOGW("unw_getcontext failed %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070045 return false;
46 }
47 return UnwindFromContext(num_ignore_frames, true);
48}
49
50std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
51 *offset = 0;
52 char buf[512];
53 unw_word_t value;
54 if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
55 &value, &context_) >= 0 && buf[0] != '\0') {
56 *offset = static_cast<uintptr_t>(value);
57 return buf;
58 }
59 return "";
60}
61
62bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, bool resolve) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070063 // The cursor structure is pretty large, do not put it on the stack.
64 unw_cursor_t* cursor = new unw_cursor_t;
65 int ret = unw_init_local(cursor, &context_);
66 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070067 BACK_LOGW("unw_init_local failed %d", ret);
Christopher Ferrisddc4f092014-01-07 14:31:07 -080068 delete cursor;
Christopher Ferris17e91d42013-10-21 13:30:52 -070069 return false;
70 }
71
Christopher Ferris46756822014-01-14 20:16:30 -080072 std::vector<backtrace_frame_data_t>* frames = GetFrames();
73 frames->reserve(MAX_BACKTRACE_FRAMES);
74 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070075 do {
76 unw_word_t pc;
77 ret = unw_get_reg(cursor, UNW_REG_IP, &pc);
78 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070079 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070080 break;
81 }
82 unw_word_t sp;
83 ret = unw_get_reg(cursor, UNW_REG_SP, &sp);
84 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070085 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070086 break;
87 }
88
89 if (num_ignore_frames == 0) {
Christopher Ferris46756822014-01-14 20:16:30 -080090 frames->resize(num_frames+1);
91 backtrace_frame_data_t* frame = &frames->at(num_frames);
Christopher Ferris20303f82014-01-10 16:33:16 -080092 frame->num = num_frames;
Christopher Ferris17e91d42013-10-21 13:30:52 -070093 frame->pc = static_cast<uintptr_t>(pc);
94 frame->sp = static_cast<uintptr_t>(sp);
95 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070096
97 if (num_frames > 0) {
98 // Set the stack size for the previous frame.
Christopher Ferris46756822014-01-14 20:16:30 -080099 backtrace_frame_data_t* prev = &frames->at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700100 prev->stack_size = frame->sp - prev->sp;
101 }
102
103 if (resolve) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800104 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
105 frame->map = FindMap(frame->pc);
Christopher Ferris46756822014-01-14 20:16:30 -0800106 } else {
107 frame->map = NULL;
108 frame->func_offset = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700109 }
Christopher Ferris46756822014-01-14 20:16:30 -0800110 num_frames++;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700111 } else {
112 num_ignore_frames--;
113 }
114 ret = unw_step (cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800115 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700116
117 delete cursor;
118 return true;
119}
120
121void UnwindCurrent::ExtractContext(void* sigcontext) {
122 unw_tdep_context_t* context = reinterpret_cast<unw_tdep_context_t*>(&context_);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700123 const ucontext_t* uc = reinterpret_cast<const ucontext_t*>(sigcontext);
124
Christopher Ferris8ed46272013-10-29 15:44:25 -0700125#if defined(__arm__)
Christopher Ferris17e91d42013-10-21 13:30:52 -0700126 context->regs[0] = uc->uc_mcontext.arm_r0;
127 context->regs[1] = uc->uc_mcontext.arm_r1;
128 context->regs[2] = uc->uc_mcontext.arm_r2;
129 context->regs[3] = uc->uc_mcontext.arm_r3;
130 context->regs[4] = uc->uc_mcontext.arm_r4;
131 context->regs[5] = uc->uc_mcontext.arm_r5;
132 context->regs[6] = uc->uc_mcontext.arm_r6;
133 context->regs[7] = uc->uc_mcontext.arm_r7;
134 context->regs[8] = uc->uc_mcontext.arm_r8;
135 context->regs[9] = uc->uc_mcontext.arm_r9;
136 context->regs[10] = uc->uc_mcontext.arm_r10;
137 context->regs[11] = uc->uc_mcontext.arm_fp;
138 context->regs[12] = uc->uc_mcontext.arm_ip;
139 context->regs[13] = uc->uc_mcontext.arm_sp;
140 context->regs[14] = uc->uc_mcontext.arm_lr;
141 context->regs[15] = uc->uc_mcontext.arm_pc;
Christopher Ferris5c400192014-01-30 10:30:36 -0800142#else
Christopher Ferrisddc4f092014-01-07 14:31:07 -0800143 context->uc_mcontext = uc->uc_mcontext;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700144#endif
145}
146
147//-------------------------------------------------------------------------
148// UnwindThread functions.
149//-------------------------------------------------------------------------
150UnwindThread::UnwindThread() {
151}
152
153UnwindThread::~UnwindThread() {
154}
155
Christopher Ferris17e91d42013-10-21 13:30:52 -0700156void UnwindThread::ThreadUnwind(
157 siginfo_t* /*siginfo*/, void* sigcontext, size_t num_ignore_frames) {
158 ExtractContext(sigcontext);
159 UnwindFromContext(num_ignore_frames, false);
160}
161
162//-------------------------------------------------------------------------
163// C++ object creation function.
164//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800165Backtrace* CreateCurrentObj(BacktraceMap* map) {
166 return new BacktraceCurrent(new UnwindCurrent(), map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700167}
168
Christopher Ferris46756822014-01-14 20:16:30 -0800169Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
Christopher Ferris17e91d42013-10-21 13:30:52 -0700170 UnwindThread* thread_obj = new UnwindThread();
Christopher Ferris46756822014-01-14 20:16:30 -0800171 return new BacktraceThread(thread_obj, thread_obj, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700172}