blob: 2155b8adcfbadcd3c7f08cc86043c145e8836e17 [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 Ferris17e91d42013-10-21 13:30:52 -070018#include <sys/types.h>
Christopher Ferrisc58287d2014-05-09 16:59:06 -070019#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070020
Christopher Ferris17e91d42013-10-21 13:30:52 -070021#include <libunwind.h>
22#include <libunwind-ptrace.h>
23
Christopher Ferris2c43cff2015-03-26 19:18:36 -070024#include <backtrace/Backtrace.h>
25#include <backtrace/BacktraceMap.h>
26
Christopher Ferrise2960912014-03-07 19:42:19 -080027#include "BacktraceLog.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080028#include "UnwindMap.h"
Christopher Ferris17e91d42013-10-21 13:30:52 -070029#include "UnwindPtrace.h"
30
Christopher Ferris2c43cff2015-03-26 19:18:36 -070031UnwindPtrace::UnwindPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
32 : BacktracePtrace(pid, tid, map), addr_space_(nullptr), upt_info_(nullptr) {
Christopher Ferris17e91d42013-10-21 13:30:52 -070033}
34
35UnwindPtrace::~UnwindPtrace() {
36 if (upt_info_) {
37 _UPT_destroy(upt_info_);
Christopher Ferris2c43cff2015-03-26 19:18:36 -070038 upt_info_ = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -070039 }
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070040
Christopher Ferris17e91d42013-10-21 13:30:52 -070041 if (addr_space_) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080042 // Remove the map from the address space before destroying it.
43 // It will be freed in the UnwindMap destructor.
Christopher Ferris2c43cff2015-03-26 19:18:36 -070044 unw_map_set(addr_space_, nullptr);
Christopher Ferrisdf290612014-01-22 19:21:07 -080045
Christopher Ferris17e91d42013-10-21 13:30:52 -070046 unw_destroy_addr_space(addr_space_);
Christopher Ferris2c43cff2015-03-26 19:18:36 -070047 addr_space_ = nullptr;
Christopher Ferris17e91d42013-10-21 13:30:52 -070048 }
49}
50
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070051bool UnwindPtrace::Init() {
52 if (upt_info_) {
53 return true;
Christopher Ferris30c942c2015-05-14 15:39:52 -070054 }
55
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070056 if (addr_space_) {
57 // If somehow the addr_space_ gets initialized but upt_info_ doesn't,
58 // then that indicates there is some kind of failure.
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070059 return false;
60 }
61
Christopher Ferris17e91d42013-10-21 13:30:52 -070062 addr_space_ = unw_create_addr_space(&_UPT_accessors, 0);
63 if (!addr_space_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070064 BACK_LOGW("unw_create_addr_space failed.");
Yabin Cuif8808282017-12-12 18:04:10 -080065 error_.error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
Christopher Ferris17e91d42013-10-21 13:30:52 -070066 return false;
67 }
68
Christopher Ferrisdf290612014-01-22 19:21:07 -080069 UnwindMap* map = static_cast<UnwindMap*>(GetMap());
70 unw_map_set(addr_space_, map->GetMapCursor());
71
72 upt_info_ = reinterpret_cast<struct UPT_info*>(_UPT_create(Tid()));
Christopher Ferris17e91d42013-10-21 13:30:52 -070073 if (!upt_info_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070074 BACK_LOGW("Failed to create upt info.");
Yabin Cuif8808282017-12-12 18:04:10 -080075 error_.error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
Christopher Ferris17e91d42013-10-21 13:30:52 -070076 return false;
77 }
78
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070079 return true;
80}
81
82bool UnwindPtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
83 if (GetMap() == nullptr) {
84 // Without a map object, we can't do anything.
Yabin Cuif8808282017-12-12 18:04:10 -080085 error_.error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070086 return false;
87 }
88
Yabin Cuif8808282017-12-12 18:04:10 -080089 error_.error_code = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070090
91 if (ucontext) {
92 BACK_LOGW("Unwinding from a specified context not supported yet.");
Yabin Cuif8808282017-12-12 18:04:10 -080093 error_.error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
Christopher Ferris82f3bbd2017-03-14 15:22:26 -070094 return false;
95 }
96
97 if (!Init()) {
98 return false;
99 }
100
Christopher Ferris17e91d42013-10-21 13:30:52 -0700101 unw_cursor_t cursor;
102 int ret = unw_init_remote(&cursor, addr_space_, upt_info_);
103 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700104 BACK_LOGW("unw_init_remote failed %d", ret);
Yabin Cuif8808282017-12-12 18:04:10 -0800105 error_.error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700106 return false;
107 }
108
Christopher Ferris46756822014-01-14 20:16:30 -0800109 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -0700110 do {
111 unw_word_t pc;
112 ret = unw_get_reg(&cursor, UNW_REG_IP, &pc);
113 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700114 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700115 break;
116 }
117 unw_word_t sp;
118 ret = unw_get_reg(&cursor, UNW_REG_SP, &sp);
119 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -0700120 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700121 break;
122 }
123
124 if (num_ignore_frames == 0) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700125 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) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700133 backtrace_frame_data_t* prev = &frames_.at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700134 prev->stack_size = frame->sp - prev->sp;
135 }
136
Christopher Ferris12385e32015-02-06 13:22:01 -0800137 FillInMap(frame->pc, &frame->map);
Christopher Ferris96722b02017-07-19 14:20:46 -0700138 if (BacktraceMap::IsValid(frame->map)) {
139 frame->rel_pc = frame->pc - frame->map.start + frame->map.load_bias;
140 } else {
141 frame->rel_pc = frame->pc;
142 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700143
Christopher Ferrisf5e568e2017-03-22 13:18:31 -0700144 frame->func_name = GetFunctionName(frame->pc, &frame->func_offset, &frame->map);
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700145
Christopher Ferris46756822014-01-14 20:16:30 -0800146 num_frames++;
Christopher Ferrisf5e568e2017-03-22 13:18:31 -0700147 // If the pc is in a device map, then don't try to step.
148 if (frame->map.flags & PROT_DEVICE_MAP) {
149 break;
150 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700151 } else {
Christopher Ferrisf5e568e2017-03-22 13:18:31 -0700152 // If the pc is in a device map, then don't try to step.
153 backtrace_map_t map;
154 FillInMap(pc, &map);
155 if (map.flags & PROT_DEVICE_MAP) {
156 break;
157 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700158 num_ignore_frames--;
159 }
Christopher Ferrisf5e568e2017-03-22 13:18:31 -0700160 // Verify the sp is not in a device map.
161 backtrace_map_t map;
162 FillInMap(sp, &map);
163 if (map.flags & PROT_DEVICE_MAP) {
164 break;
165 }
Christopher Ferris17e91d42013-10-21 13:30:52 -0700166 ret = unw_step (&cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800167 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700168
169 return true;
170}
171
172std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
Christopher Ferris82f3bbd2017-03-14 15:22:26 -0700173 if (!Init()) {
174 return "";
175 }
176
Christopher Ferris17e91d42013-10-21 13:30:52 -0700177 *offset = 0;
178 char buf[512];
179 unw_word_t value;
180 if (unw_get_proc_name_by_ip(addr_space_, pc, buf, sizeof(buf), &value,
181 upt_info_) >= 0 && buf[0] != '\0') {
182 *offset = static_cast<uintptr_t>(value);
183 return buf;
184 }
185 return "";
186}