blob: 4e7f761ff613d38d6265ed458c659f4ef7b58a49 [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 Ferris2486d5a2018-01-22 17:37:59 -080052 if (stack_map->GetJitDebug() != nullptr) {
53 unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch());
54 }
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080055#if !defined(NO_LIBDEXFILE_SUPPORT)
56 if (stack_map->GetDexFiles() != nullptr) {
57 unwinder.SetDexFiles(stack_map->GetDexFiles(), regs->Arch());
58 }
59#endif
Christopher Ferrisc56a4992017-11-02 16:19:56 -070060 unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore());
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080061 if (error != nullptr) {
62 switch (unwinder.LastErrorCode()) {
63 case unwindstack::ERROR_NONE:
64 error->error_code = BACKTRACE_UNWIND_NO_ERROR;
65 break;
66
67 case unwindstack::ERROR_MEMORY_INVALID:
68 error->error_code = BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED;
69 error->error_info.addr = unwinder.LastErrorAddress();
70 break;
71
72 case unwindstack::ERROR_UNWIND_INFO:
73 error->error_code = BACKTRACE_UNWIND_ERROR_UNWIND_INFO;
74 break;
75
76 case unwindstack::ERROR_UNSUPPORTED:
77 error->error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
78 break;
79
80 case unwindstack::ERROR_INVALID_MAP:
81 error->error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
82 break;
83
84 case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
85 error->error_code = BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT;
86 break;
87
88 case unwindstack::ERROR_REPEATED_FRAME:
89 error->error_code = BACKTRACE_UNWIND_ERROR_REPEATED_FRAME;
90 break;
91 }
92 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070093
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070094 if (num_ignore_frames >= unwinder.NumFrames()) {
95 frames->resize(0);
96 return true;
97 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070098
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070099 auto unwinder_frames = unwinder.frames();
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800100 frames->resize(unwinder.NumFrames() - num_ignore_frames);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700101 size_t cur_frame = 0;
David Srbecky645f8bb2018-01-25 12:21:59 +0000102 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700103 auto frame = &unwinder_frames[i];
David Srbecky645f8bb2018-01-25 12:21:59 +0000104
Christopher Ferris8fe58362018-01-26 14:26:13 -0800105 backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700106
Christopher Ferris8fe58362018-01-26 14:26:13 -0800107 back_frame->num = cur_frame++;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700108
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700109 back_frame->rel_pc = frame->rel_pc;
110 back_frame->pc = frame->pc;
111 back_frame->sp = frame->sp;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700112
Christopher Ferris9a6b3e32017-10-18 09:08:51 -0700113 back_frame->func_name = demangle(frame->function_name.c_str());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700114 back_frame->func_offset = frame->function_offset;
115
116 back_frame->map.name = frame->map_name;
117 back_frame->map.start = frame->map_start;
118 back_frame->map.end = frame->map_end;
119 back_frame->map.offset = frame->map_offset;
120 back_frame->map.load_bias = frame->map_load_bias;
121 back_frame->map.flags = frame->map_flags;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700122 }
123
124 return true;
125}
126
Christopher Ferris432981b2018-02-22 19:42:53 -0800127bool Backtrace::UnwindOffline(unwindstack::Regs* regs, BacktraceMap* back_map,
128 const backtrace_stackinfo_t& stack,
129 std::vector<backtrace_frame_data_t>* frames,
130 BacktraceUnwindError* error) {
131 UnwindStackOfflineMap* offline_map = reinterpret_cast<UnwindStackOfflineMap*>(back_map);
132 // Create the process memory from the stack data since this will almost
133 // always be different each unwind.
134 if (!offline_map->CreateProcessMemory(stack)) {
135 if (error != nullptr) {
136 error->error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
137 }
138 return false;
139 }
140 return Backtrace::Unwind(regs, back_map, frames, 0U, nullptr, error);
141}
142
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700143UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700144 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700145
Christopher Ferris7937a362018-01-18 11:15:49 -0800146std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700147 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700148}
149
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800150bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700151 std::unique_ptr<unwindstack::Regs> regs;
152 if (ucontext == nullptr) {
153 regs.reset(unwindstack::Regs::CreateFromLocal());
154 // Fill in the registers from this function. Do it here to avoid
155 // one extra function call appearing in the unwind.
156 unwindstack::RegsGetLocal(regs.get());
157 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800158 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700159 }
160
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700161 std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
Christopher Ferris458f4e72018-03-23 12:51:43 -0700162 if (!skip_frames_) {
163 skip_names.clear();
164 }
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800165 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700166}
167
168UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferrise3286732017-12-07 17:41:18 -0800169 : BacktracePtrace(pid, tid, map), memory_(pid) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700170
Christopher Ferris7937a362018-01-18 11:15:49 -0800171std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700172 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700173}
174
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800175bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700176 std::unique_ptr<unwindstack::Regs> regs;
177 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700178 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700179 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800180 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700181 }
182
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800183 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700184}
Christopher Ferrise3286732017-12-07 17:41:18 -0800185
Christopher Ferris7937a362018-01-18 11:15:49 -0800186size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
Christopher Ferrise3286732017-12-07 17:41:18 -0800187 return memory_.Read(addr, buffer, bytes);
188}
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800189
190UnwindStackOffline::UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map,
191 bool map_shared)
192 : Backtrace(pid, tid, map), arch_(arch) {
193 map_shared_ = map_shared;
194}
195
196bool UnwindStackOffline::Unwind(size_t num_ignore_frames, void* ucontext) {
197 if (ucontext == nullptr) {
198 return false;
199 }
200
201 unwindstack::ArchEnum arch;
202 switch (arch_) {
203 case ARCH_ARM:
204 arch = unwindstack::ARCH_ARM;
205 break;
206 case ARCH_ARM64:
207 arch = unwindstack::ARCH_ARM64;
208 break;
209 case ARCH_X86:
210 arch = unwindstack::ARCH_X86;
211 break;
212 case ARCH_X86_64:
213 arch = unwindstack::ARCH_X86_64;
214 break;
215 default:
216 return false;
217 }
218
219 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromUcontext(arch, ucontext));
220
221 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
222}
223
224std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) {
225 return "";
226}
227
228size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) {
229 return 0;
230}
231
232bool UnwindStackOffline::ReadWord(uint64_t, word_t*) {
233 return false;
234}
235
236Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid,
237 const std::vector<backtrace_map_t>& maps,
238 const backtrace_stackinfo_t& stack) {
Christopher Ferris432981b2018-02-22 19:42:53 -0800239 std::unique_ptr<UnwindStackOfflineMap> map(
240 reinterpret_cast<UnwindStackOfflineMap*>(BacktraceMap::CreateOffline(pid, maps)));
241 if (map.get() == nullptr || !map->CreateProcessMemory(stack)) {
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800242 return nullptr;
243 }
Christopher Ferris432981b2018-02-22 19:42:53 -0800244 return new UnwindStackOffline(arch, pid, tid, map.release(), false);
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800245}
246
247Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) {
248 if (map == nullptr) {
249 return nullptr;
250 }
251 return new UnwindStackOffline(arch, pid, tid, map, true);
252}
253
254void Backtrace::SetGlobalElfCache(bool enable) {
255 unwindstack::Elf::SetCachingEnabled(enable);
256}