blob: 36640cdfaa19ed025475ae3f9a3b37060508c114 [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
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000035#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());
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000053 if (stack_map->GetJitDebug() != nullptr) {
54 unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch());
55 }
56#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;
Christopher Ferrisd11ed862019-04-11 19:45:35 -070092
93 case unwindstack::ERROR_INVALID_ELF:
94 error->error_code = BACKTRACE_UNWIND_ERROR_INVALID_ELF;
95 break;
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080096 }
97 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070098
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070099 if (num_ignore_frames >= unwinder.NumFrames()) {
100 frames->resize(0);
101 return true;
102 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700103
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700104 auto unwinder_frames = unwinder.frames();
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800105 frames->resize(unwinder.NumFrames() - num_ignore_frames);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700106 size_t cur_frame = 0;
David Srbecky645f8bb2018-01-25 12:21:59 +0000107 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700108 auto frame = &unwinder_frames[i];
David Srbecky645f8bb2018-01-25 12:21:59 +0000109
Christopher Ferris8fe58362018-01-26 14:26:13 -0800110 backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700111
Christopher Ferris8fe58362018-01-26 14:26:13 -0800112 back_frame->num = cur_frame++;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700113
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700114 back_frame->rel_pc = frame->rel_pc;
115 back_frame->pc = frame->pc;
116 back_frame->sp = frame->sp;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700117
Christopher Ferris9a6b3e32017-10-18 09:08:51 -0700118 back_frame->func_name = demangle(frame->function_name.c_str());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700119 back_frame->func_offset = frame->function_offset;
120
121 back_frame->map.name = frame->map_name;
122 back_frame->map.start = frame->map_start;
123 back_frame->map.end = frame->map_end;
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800124 back_frame->map.offset = frame->map_elf_start_offset;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700125 back_frame->map.load_bias = frame->map_load_bias;
126 back_frame->map.flags = frame->map_flags;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700127 }
128
129 return true;
130}
131
Christopher Ferris432981b2018-02-22 19:42:53 -0800132bool Backtrace::UnwindOffline(unwindstack::Regs* regs, BacktraceMap* back_map,
133 const backtrace_stackinfo_t& stack,
134 std::vector<backtrace_frame_data_t>* frames,
135 BacktraceUnwindError* error) {
136 UnwindStackOfflineMap* offline_map = reinterpret_cast<UnwindStackOfflineMap*>(back_map);
137 // Create the process memory from the stack data since this will almost
138 // always be different each unwind.
139 if (!offline_map->CreateProcessMemory(stack)) {
140 if (error != nullptr) {
141 error->error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
142 }
143 return false;
144 }
145 return Backtrace::Unwind(regs, back_map, frames, 0U, nullptr, error);
146}
147
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700148UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700149 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700150
Christopher Ferris7937a362018-01-18 11:15:49 -0800151std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700152 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700153}
154
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800155bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700156 std::unique_ptr<unwindstack::Regs> regs;
157 if (ucontext == nullptr) {
158 regs.reset(unwindstack::Regs::CreateFromLocal());
159 // Fill in the registers from this function. Do it here to avoid
160 // one extra function call appearing in the unwind.
161 unwindstack::RegsGetLocal(regs.get());
162 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800163 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700164 }
165
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700166 std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
Christopher Ferris458f4e72018-03-23 12:51:43 -0700167 if (!skip_frames_) {
168 skip_names.clear();
169 }
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800170 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700171}
172
173UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferrise3286732017-12-07 17:41:18 -0800174 : BacktracePtrace(pid, tid, map), memory_(pid) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700175
Christopher Ferris7937a362018-01-18 11:15:49 -0800176std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700177 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700178}
179
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800180bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700181 std::unique_ptr<unwindstack::Regs> regs;
182 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700183 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700184 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800185 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700186 }
187
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800188 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700189}
Christopher Ferrise3286732017-12-07 17:41:18 -0800190
Christopher Ferris7937a362018-01-18 11:15:49 -0800191size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
Christopher Ferrise3286732017-12-07 17:41:18 -0800192 return memory_.Read(addr, buffer, bytes);
193}
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800194
195UnwindStackOffline::UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map,
196 bool map_shared)
197 : Backtrace(pid, tid, map), arch_(arch) {
198 map_shared_ = map_shared;
199}
200
201bool UnwindStackOffline::Unwind(size_t num_ignore_frames, void* ucontext) {
202 if (ucontext == nullptr) {
203 return false;
204 }
205
206 unwindstack::ArchEnum arch;
207 switch (arch_) {
208 case ARCH_ARM:
209 arch = unwindstack::ARCH_ARM;
210 break;
211 case ARCH_ARM64:
212 arch = unwindstack::ARCH_ARM64;
213 break;
214 case ARCH_X86:
215 arch = unwindstack::ARCH_X86;
216 break;
217 case ARCH_X86_64:
218 arch = unwindstack::ARCH_X86_64;
219 break;
220 default:
221 return false;
222 }
223
224 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromUcontext(arch, ucontext));
225
226 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
227}
228
229std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) {
230 return "";
231}
232
233size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) {
234 return 0;
235}
236
237bool UnwindStackOffline::ReadWord(uint64_t, word_t*) {
238 return false;
239}
240
241Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid,
242 const std::vector<backtrace_map_t>& maps,
243 const backtrace_stackinfo_t& stack) {
Christopher Ferris432981b2018-02-22 19:42:53 -0800244 std::unique_ptr<UnwindStackOfflineMap> map(
245 reinterpret_cast<UnwindStackOfflineMap*>(BacktraceMap::CreateOffline(pid, maps)));
246 if (map.get() == nullptr || !map->CreateProcessMemory(stack)) {
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800247 return nullptr;
248 }
Christopher Ferris432981b2018-02-22 19:42:53 -0800249 return new UnwindStackOffline(arch, pid, tid, map.release(), false);
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800250}
251
252Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) {
253 if (map == nullptr) {
254 return nullptr;
255 }
256 return new UnwindStackOffline(arch, pid, tid, map, true);
257}
258
259void Backtrace::SetGlobalElfCache(bool enable) {
260 unwindstack::Elf::SetCachingEnabled(enable);
261}