blob: a12862398f3efeb2fcd2c2793d33af953301b905 [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
132UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
Christopher Ferris5f118512017-09-01 11:17:16 -0700133 : BacktraceCurrent(pid, tid, map) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700134
Christopher Ferris7937a362018-01-18 11:15:49 -0800135std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700136 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700137}
138
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800139bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700140 std::unique_ptr<unwindstack::Regs> regs;
141 if (ucontext == nullptr) {
142 regs.reset(unwindstack::Regs::CreateFromLocal());
143 // Fill in the registers from this function. Do it here to avoid
144 // one extra function call appearing in the unwind.
145 unwindstack::RegsGetLocal(regs.get());
146 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800147 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700148 }
149
Christopher Ferrisc56a4992017-11-02 16:19:56 -0700150 std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
Christopher Ferris458f4e72018-03-23 12:51:43 -0700151 if (!skip_frames_) {
152 skip_names.clear();
153 }
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800154 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700155}
156
157UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
Casey Dahlin6b95a0e2019-03-12 17:50:52 -0700158 : BacktracePtrace(pid, tid, map), memory_(unwindstack::Memory::CreateProcessMemory(pid)) {}
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700159
Christopher Ferris7937a362018-01-18 11:15:49 -0800160std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
Josh Gao358de182017-09-06 22:16:09 -0700161 return GetMap()->GetFunctionName(pc, offset);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700162}
163
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800164bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700165 std::unique_ptr<unwindstack::Regs> regs;
166 if (context == nullptr) {
Josh Gao0953ecd2017-08-25 13:55:06 -0700167 regs.reset(unwindstack::Regs::RemoteGet(Tid()));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700168 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800169 regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700170 }
171
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800172 return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
Christopher Ferris6f3981c2017-07-27 09:29:18 -0700173}
Christopher Ferrise3286732017-12-07 17:41:18 -0800174
Christopher Ferris7937a362018-01-18 11:15:49 -0800175size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
Casey Dahlin6b95a0e2019-03-12 17:50:52 -0700176 return memory_->Read(addr, buffer, bytes);
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -0800177}