blob: f985f528cce224845a8df573e4d465e83d6ea76d [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
Christopher Ferris2c43cff2015-03-26 19:18:36 -070017#include <stdint.h>
Christopher Ferrisc58287d2014-05-09 16:59:06 -070018#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070019
Christopher Ferris2c43cff2015-03-26 19:18:36 -070020#include <memory>
21#include <string>
Christopher Ferris17e91d42013-10-21 13:30:52 -070022
23#define UNW_LOCAL_ONLY
24#include <libunwind.h>
25
Christopher Ferris4265ed72016-03-01 17:56:00 -080026#include <android-base/strings.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070027#include <backtrace/Backtrace.h>
28
Christopher Ferrise2960912014-03-07 19:42:19 -080029#include "BacktraceLog.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070030#include "UnwindCurrent.h"
31
Christopher Ferris2c43cff2015-03-26 19:18:36 -070032std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
33 *offset = 0;
34 char buf[512];
35 unw_word_t value;
36 if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
37 &value, &context_) >= 0 && buf[0] != '\0') {
38 *offset = static_cast<uintptr_t>(value);
39 return buf;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070040 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -070041 return "";
Christopher Ferris17e91d42013-10-21 13:30:52 -070042}
43
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070044void UnwindCurrent::GetUnwContextFromUcontext(const ucontext_t* ucontext) {
45 unw_tdep_context_t* unw_context = reinterpret_cast<unw_tdep_context_t*>(&context_);
46
47#if defined(__arm__)
48 unw_context->regs[0] = ucontext->uc_mcontext.arm_r0;
49 unw_context->regs[1] = ucontext->uc_mcontext.arm_r1;
50 unw_context->regs[2] = ucontext->uc_mcontext.arm_r2;
51 unw_context->regs[3] = ucontext->uc_mcontext.arm_r3;
52 unw_context->regs[4] = ucontext->uc_mcontext.arm_r4;
53 unw_context->regs[5] = ucontext->uc_mcontext.arm_r5;
54 unw_context->regs[6] = ucontext->uc_mcontext.arm_r6;
55 unw_context->regs[7] = ucontext->uc_mcontext.arm_r7;
56 unw_context->regs[8] = ucontext->uc_mcontext.arm_r8;
57 unw_context->regs[9] = ucontext->uc_mcontext.arm_r9;
58 unw_context->regs[10] = ucontext->uc_mcontext.arm_r10;
59 unw_context->regs[11] = ucontext->uc_mcontext.arm_fp;
60 unw_context->regs[12] = ucontext->uc_mcontext.arm_ip;
61 unw_context->regs[13] = ucontext->uc_mcontext.arm_sp;
62 unw_context->regs[14] = ucontext->uc_mcontext.arm_lr;
63 unw_context->regs[15] = ucontext->uc_mcontext.arm_pc;
64#else
65 unw_context->uc_mcontext = ucontext->uc_mcontext;
66#endif
67}
68
Christopher Ferris2c43cff2015-03-26 19:18:36 -070069bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) {
70 if (ucontext == nullptr) {
71 int ret = unw_getcontext(&context_);
72 if (ret < 0) {
73 BACK_LOGW("unw_getcontext failed %d", ret);
74 return false;
Christopher Ferris17224d52014-04-11 13:05:07 -070075 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -070076 } else {
77 GetUnwContextFromUcontext(ucontext);
78 }
79
80 // The cursor structure is pretty large, do not put it on the stack.
81 std::unique_ptr<unw_cursor_t> cursor(new unw_cursor_t);
82 int ret = unw_init_local(cursor.get(), &context_);
83 if (ret < 0) {
84 BACK_LOGW("unw_init_local failed %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070085 return false;
86 }
87
Christopher Ferris46756822014-01-14 20:16:30 -080088 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070089 do {
90 unw_word_t pc;
Christopher Ferris2c43cff2015-03-26 19:18:36 -070091 ret = unw_get_reg(cursor.get(), UNW_REG_IP, &pc);
Christopher Ferris17e91d42013-10-21 13:30:52 -070092 if (ret < 0) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -070093 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070094 break;
95 }
96 unw_word_t sp;
Christopher Ferris2c43cff2015-03-26 19:18:36 -070097 ret = unw_get_reg(cursor.get(), UNW_REG_SP, &sp);
Christopher Ferris17e91d42013-10-21 13:30:52 -070098 if (ret < 0) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -070099 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700100 break;
101 }
102
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700103 frames_.resize(num_frames+1);
104 backtrace_frame_data_t* frame = &frames_.at(num_frames);
105 frame->num = num_frames;
106 frame->pc = static_cast<uintptr_t>(pc);
107 frame->sp = static_cast<uintptr_t>(sp);
108 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700109
Christopher Ferrisca09ce92015-03-31 17:28:22 -0700110 FillInMap(frame->pc, &frame->map);
111 // Check to see if we should skip this frame because it's coming
112 // from within the library, and we are doing a local unwind.
113 if (ucontext != nullptr || num_frames != 0 || !DiscardFrame(*frame)) {
114 if (num_ignore_frames == 0) {
115 // GetFunctionName is an expensive call, only do it if we are
116 // keeping the frame.
117 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
118 if (num_frames > 0) {
119 // Set the stack size for the previous frame.
120 backtrace_frame_data_t* prev = &frames_.at(num_frames-1);
121 prev->stack_size = frame->sp - prev->sp;
122 }
123 num_frames++;
124 } else {
125 num_ignore_frames--;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700126 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700127 }
Christopher Ferris4265ed72016-03-01 17:56:00 -0800128
129 // For now, do not attempt to do local unwinds through .dex, or .oat
130 // maps. We can only unwind through these if there is a compressed
131 // section available, almost all local unwinds are done by ART
132 // which will dump the Java frames separately.
133 // TODO: Come up with a flag to control this.
134 if (android::base::EndsWith(frame->map.name, ".dex")
135 || android::base::EndsWith(frame->map.name, ".oat")) {
136 break;
137 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700138 ret = unw_step (cursor.get());
Christopher Ferris46756822014-01-14 20:16:30 -0800139 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700140
Christopher Ferris17e91d42013-10-21 13:30:52 -0700141 return true;
142}