blob: 624711f797cfc1e226a0bd7ec6f98f6a7f5ec12f [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>
27#include <unwindstack/Elf.h>
28#include <unwindstack/MapInfo.h>
29#include <unwindstack/Maps.h>
30#include <unwindstack/Memory.h>
31#include <unwindstack/Regs.h>
32#include <unwindstack/RegsGetLocal.h>
33
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000034#if !defined(NO_LIBDEXFILE_SUPPORT)
35#include <unwindstack/DexFiles.h>
36#endif
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070037#include <unwindstack/Unwinder.h>
38
Christopher Ferris6f3981c2017-07-27 09:29:18 -070039#include "BacktraceLog.h"
40#include "UnwindStack.h"
41#include "UnwindStackMap.h"
42
Christopher Ferris4ec93a72019-07-18 14:11:07 -070043extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
44
Josh Gao45c4a562017-09-06 14:35:35 -070045bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
Christopher Ferrisc56a4992017-11-02 16:19:56 -070046 std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080047 std::vector<std::string>* skip_names, BacktraceUnwindError* error) {
Christopher Ferris5f118512017-09-01 11:17:16 -070048 UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070049 auto process_memory = stack_map->process_memory();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070050 unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
51 regs, stack_map->process_memory());
Christopher Ferrise4b3a6a2018-02-20 13:58:40 -080052 unwinder.SetResolveNames(stack_map->ResolveNames());
Christopher Ferris4568f4b2018-10-23 17:42:41 -070053 stack_map->SetArch(regs->Arch());
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000054 if (stack_map->GetJitDebug() != nullptr) {
55 unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch());
56 }
57#if !defined(NO_LIBDEXFILE_SUPPORT)
58 if (stack_map->GetDexFiles() != nullptr) {
59 unwinder.SetDexFiles(stack_map->GetDexFiles(), regs->Arch());
60 }
61#endif
Christopher Ferrisc56a4992017-11-02 16:19:56 -070062 unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore());
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080063 if (error != nullptr) {
64 switch (unwinder.LastErrorCode()) {
65 case unwindstack::ERROR_NONE:
66 error->error_code = BACKTRACE_UNWIND_NO_ERROR;
67 break;
68
69 case unwindstack::ERROR_MEMORY_INVALID:
70 error->error_code = BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED;
71 error->error_info.addr = unwinder.LastErrorAddress();
72 break;
73
74 case unwindstack::ERROR_UNWIND_INFO:
75 error->error_code = BACKTRACE_UNWIND_ERROR_UNWIND_INFO;
76 break;
77
78 case unwindstack::ERROR_UNSUPPORTED:
79 error->error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
80 break;
81
82 case unwindstack::ERROR_INVALID_MAP:
83 error->error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
84 break;
85
86 case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
87 error->error_code = BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT;
88 break;
89
90 case unwindstack::ERROR_REPEATED_FRAME:
91 error->error_code = BACKTRACE_UNWIND_ERROR_REPEATED_FRAME;
92 break;
Christopher Ferrisd11ed862019-04-11 19:45:35 -070093
94 case unwindstack::ERROR_INVALID_ELF:
95 error->error_code = BACKTRACE_UNWIND_ERROR_INVALID_ELF;
96 break;
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080097 }
98 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070099
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700100 if (num_ignore_frames >= unwinder.NumFrames()) {
101 frames->resize(0);
102 return true;
103 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700104
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700105 auto unwinder_frames = unwinder.frames();
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800106 frames->resize(unwinder.NumFrames() - num_ignore_frames);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700107 size_t cur_frame = 0;
David Srbecky645f8bb2018-01-25 12:21:59 +0000108 for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700109 auto frame = &unwinder_frames[i];
David Srbecky645f8bb2018-01-25 12:21:59 +0000110
Christopher Ferris8fe58362018-01-26 14:26:13 -0800111 backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700112
Christopher Ferris8fe58362018-01-26 14:26:13 -0800113 back_frame->num = cur_frame++;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700114
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700115 back_frame->rel_pc = frame->rel_pc;
116 back_frame->pc = frame->pc;
117 back_frame->sp = frame->sp;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700118
Christopher Ferris4ec93a72019-07-18 14:11:07 -0700119 char* demangled_name = __cxa_demangle(frame->function_name.c_str(), nullptr, nullptr, nullptr);
120 if (demangled_name != nullptr) {
121 back_frame->func_name = demangled_name;
122 free(demangled_name);
123 } else {
124 back_frame->func_name = frame->function_name;
125 }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700126 back_frame->func_offset = frame->function_offset;
127
128 back_frame->map.name = frame->map_name;
129 back_frame->map.start = frame->map_start;
130 back_frame->map.end = frame->map_end;
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800131 back_frame->map.offset = frame->map_elf_start_offset;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700132 back_frame->map.load_bias = frame->map_load_bias;
133 back_frame->map.flags = frame->map_flags;
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700134 }
135
136 return true;
137}
138
139UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700140 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700141
Christopher Ferris7937a362018-01-18 11:15:49 -0800142std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700143 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700144}
145
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800146bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700147 std::unique_ptr<unwindstack::Regs> regs;
148 if (ucontext == nullptr) {
149 regs.reset(unwindstack::Regs::CreateFromLocal());
150 // Fill in the registers from this function. Do it here to avoid
151 // one extra function call appearing in the unwind.
152 unwindstack::RegsGetLocal(regs.get());
153 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800154 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700155 }
156
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700157 std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
Christopher Ferris458f4e72018-03-23 12:51:43 -0700158 if (!skip_frames_) {
159 skip_names.clear();
160 }
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800161 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700162}
163
164UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Casey Dahlin6b95a0e2019-03-12 17:50:52 -0700165 : BacktracePtrace(pid, tid, map), memory_(unwindstack::Memory::CreateProcessMemory(pid)) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700166
Christopher Ferris7937a362018-01-18 11:15:49 -0800167std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700168 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700169}
170
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800171bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700172 std::unique_ptr<unwindstack::Regs> regs;
173 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700174 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700175 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800176 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700177 }
178
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800179 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700180}
Christopher Ferrise3286732017-12-07 17:41:18 -0800181
Christopher Ferris7937a362018-01-18 11:15:49 -0800182size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
Casey Dahlin6b95a0e2019-03-12 17:50:52 -0700183 return memory_->Read(addr, buffer, bytes);
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800184}