blob: fe28eba4101ec0c5014cbc338463c4e9b2e0bbe0 [file] [log] [blame]
Christopher Ferris6f3981c2017-07-27 09:29:18 -07001/*
2 * Copyright (C) 2017 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 _GNU_SOURCE 1
Christopher Ferris6f3981c2017-07-27 09:29:18 -070018#include <stdint.h>
19#include <stdlib.h>
20#include <string.h>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070021
22#include <memory>
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070023#include <set>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070024#include <string>
25
Christopher Ferris6f3981c2017-07-27 09:29:18 -070026#include <backtrace/Backtrace.h>
Christopher Ferris04fdec02017-08-11 15:17:46 -070027#include <demangle.h>
Christopher Ferris6f3981c2017-07-27 09:29:18 -070028#include <unwindstack/Elf.h>
29#include <unwindstack/MapInfo.h>
30#include <unwindstack/Maps.h>
31#include <unwindstack/Memory.h>
32#include <unwindstack/Regs.h>
33#include <unwindstack/RegsGetLocal.h>
34
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080035#if !defined(NO_LIBDEXFILE_SUPPORT)
36#include <unwindstack/DexFiles.h>
37#endif
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070038#include <unwindstack/Unwinder.h>
39
Christopher Ferris6f3981c2017-07-27 09:29:18 -070040#include "BacktraceLog.h"
41#include "UnwindStack.h"
42#include "UnwindStackMap.h"
43
Josh Gao45c4a562017-09-06 14:35:35 -070044bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
Christopher Ferrisc56a4992017-11-02 16:19:56 -070045 std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080046 std::vector<std::string>* skip_names, BacktraceUnwindError* error) {
Christopher Ferris5f118512017-09-01 11:17:16 -070047 UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070048 auto process_memory = stack_map->process_memory();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070049 unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
50 regs, stack_map->process_memory());
Christopher Ferrise4b3a6a2018-02-20 13:58:40 -080051 unwinder.SetResolveNames(stack_map->ResolveNames());
Christopher Ferris4568f4b2018-10-23 17:42:41 -070052 stack_map->SetArch(regs->Arch());
Christopher Ferris2486d5a2018-01-22 17:37:59 -080053 if (stack_map->GetJitDebug() != nullptr) {
54 unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch());
55 }
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080056#if !defined(NO_LIBDEXFILE_SUPPORT)
57 if (stack_map->GetDexFiles() != nullptr) {
58 unwinder.SetDexFiles(stack_map->GetDexFiles(), regs->Arch());
59 }
60#endif
Christopher Ferrisc56a4992017-11-02 16:19:56 -070061 unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore());
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080062 if (error != nullptr) {
63 switch (unwinder.LastErrorCode()) {
64 case unwindstack::ERROR_NONE:
65 error->error_code = BACKTRACE_UNWIND_NO_ERROR;
66 break;
67
68 case unwindstack::ERROR_MEMORY_INVALID:
69 error->error_code = BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED;
70 error->error_info.addr = unwinder.LastErrorAddress();
71 break;
72
73 case unwindstack::ERROR_UNWIND_INFO:
74 error->error_code = BACKTRACE_UNWIND_ERROR_UNWIND_INFO;
75 break;
76
77 case unwindstack::ERROR_UNSUPPORTED:
78 error->error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
79 break;
80
81 case unwindstack::ERROR_INVALID_MAP:
82 error->error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
83 break;
84
85 case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
86 error->error_code = BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT;
87 break;
88
89 case unwindstack::ERROR_REPEATED_FRAME:
90 error->error_code = BACKTRACE_UNWIND_ERROR_REPEATED_FRAME;
91 break;
92 }
93 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070094
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070095 if (num_ignore_frames >= unwinder.NumFrames()) {
96 frames->resize(0);
97 return true;
98 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070099
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700100 auto unwinder_frames = unwinder.frames();
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800101 frames->resize(unwinder.NumFrames() - num_ignore_frames);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700102 size_t cur_frame = 0;
David Srbecky645f8bb2018-01-25 12:21:59 +0000103 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700104 auto frame = &unwinder_frames[i];
David Srbecky645f8bb2018-01-25 12:21:59 +0000105
Christopher Ferris8fe58362018-01-26 14:26:13 -0800106 backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700107
Christopher Ferris8fe58362018-01-26 14:26:13 -0800108 back_frame->num = cur_frame++;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700109
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700110 back_frame->rel_pc = frame->rel_pc;
111 back_frame->pc = frame->pc;
112 back_frame->sp = frame->sp;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700113
Christopher Ferris9a6b3e32017-10-18 09:08:51 -0700114 back_frame->func_name = demangle(frame->function_name.c_str());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700115 back_frame->func_offset = frame->function_offset;
116
117 back_frame->map.name = frame->map_name;
118 back_frame->map.start = frame->map_start;
119 back_frame->map.end = frame->map_end;
120 back_frame->map.offset = frame->map_offset;
121 back_frame->map.load_bias = frame->map_load_bias;
122 back_frame->map.flags = frame->map_flags;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700123 }
124
125 return true;
126}
127
Christopher Ferris432981b2018-02-22 19:42:53 -0800128bool Backtrace::UnwindOffline(unwindstack::Regs* regs, BacktraceMap* back_map,
129 const backtrace_stackinfo_t& stack,
130 std::vector<backtrace_frame_data_t>* frames,
131 BacktraceUnwindError* error) {
132 UnwindStackOfflineMap* offline_map = reinterpret_cast<UnwindStackOfflineMap*>(back_map);
133 // Create the process memory from the stack data since this will almost
134 // always be different each unwind.
135 if (!offline_map->CreateProcessMemory(stack)) {
136 if (error != nullptr) {
137 error->error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
138 }
139 return false;
140 }
141 return Backtrace::Unwind(regs, back_map, frames, 0U, nullptr, error);
142}
143
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700144UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700145 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700146
Christopher Ferris7937a362018-01-18 11:15:49 -0800147std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700148 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700149}
150
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800151bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700152 std::unique_ptr<unwindstack::Regs> regs;
153 if (ucontext == nullptr) {
154 regs.reset(unwindstack::Regs::CreateFromLocal());
155 // Fill in the registers from this function. Do it here to avoid
156 // one extra function call appearing in the unwind.
157 unwindstack::RegsGetLocal(regs.get());
158 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800159 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700160 }
161
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700162 std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
Christopher Ferris458f4e72018-03-23 12:51:43 -0700163 if (!skip_frames_) {
164 skip_names.clear();
165 }
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800166 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700167}
168
169UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferrise3286732017-12-07 17:41:18 -0800170 : BacktracePtrace(pid, tid, map), memory_(pid) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700171
Christopher Ferris7937a362018-01-18 11:15:49 -0800172std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700173 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700174}
175
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800176bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700177 std::unique_ptr<unwindstack::Regs> regs;
178 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700179 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700180 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800181 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700182 }
183
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800184 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700185}
Christopher Ferrise3286732017-12-07 17:41:18 -0800186
Christopher Ferris7937a362018-01-18 11:15:49 -0800187size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
Christopher Ferrise3286732017-12-07 17:41:18 -0800188 return memory_.Read(addr, buffer, bytes);
189}
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800190
191UnwindStackOffline::UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map,
192 bool map_shared)
193 : Backtrace(pid, tid, map), arch_(arch) {
194 map_shared_ = map_shared;
195}
196
197bool UnwindStackOffline::Unwind(size_t num_ignore_frames, void* ucontext) {
198 if (ucontext == nullptr) {
199 return false;
200 }
201
202 unwindstack::ArchEnum arch;
203 switch (arch_) {
204 case ARCH_ARM:
205 arch = unwindstack::ARCH_ARM;
206 break;
207 case ARCH_ARM64:
208 arch = unwindstack::ARCH_ARM64;
209 break;
210 case ARCH_X86:
211 arch = unwindstack::ARCH_X86;
212 break;
213 case ARCH_X86_64:
214 arch = unwindstack::ARCH_X86_64;
215 break;
216 default:
217 return false;
218 }
219
220 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromUcontext(arch, ucontext));
221
222 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
223}
224
225std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) {
226 return "";
227}
228
229size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) {
230 return 0;
231}
232
233bool UnwindStackOffline::ReadWord(uint64_t, word_t*) {
234 return false;
235}
236
237Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid,
238 const std::vector<backtrace_map_t>& maps,
239 const backtrace_stackinfo_t& stack) {
Christopher Ferris432981b2018-02-22 19:42:53 -0800240 std::unique_ptr<UnwindStackOfflineMap> map(
241 reinterpret_cast<UnwindStackOfflineMap*>(BacktraceMap::CreateOffline(pid, maps)));
242 if (map.get() == nullptr || !map->CreateProcessMemory(stack)) {
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800243 return nullptr;
244 }
Christopher Ferris432981b2018-02-22 19:42:53 -0800245 return new UnwindStackOffline(arch, pid, tid, map.release(), false);
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800246}
247
248Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) {
249 if (map == nullptr) {
250 return nullptr;
251 }
252 return new UnwindStackOffline(arch, pid, tid, map, true);
253}
254
255void Backtrace::SetGlobalElfCache(bool enable) {
256 unwindstack::Elf::SetCachingEnabled(enable);
257}