Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 21 | |
| 22 | #include <memory> |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 23 | #include <set> |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| 26 | #if !defined(__ANDROID__) |
| 27 | #include <cutils/threads.h> |
| 28 | #endif |
| 29 | |
| 30 | #include <backtrace/Backtrace.h> |
Christopher Ferris | 04fdec0 | 2017-08-11 15:17:46 -0700 | [diff] [blame] | 31 | #include <demangle.h> |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 32 | #include <unwindstack/Elf.h> |
| 33 | #include <unwindstack/MapInfo.h> |
| 34 | #include <unwindstack/Maps.h> |
| 35 | #include <unwindstack/Memory.h> |
| 36 | #include <unwindstack/Regs.h> |
| 37 | #include <unwindstack/RegsGetLocal.h> |
| 38 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 39 | #if !defined(NO_LIBDEXFILE_SUPPORT) |
| 40 | #include <unwindstack/DexFiles.h> |
| 41 | #endif |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 42 | #include <unwindstack/Unwinder.h> |
| 43 | |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 44 | #include "BacktraceLog.h" |
| 45 | #include "UnwindStack.h" |
| 46 | #include "UnwindStackMap.h" |
| 47 | |
Josh Gao | 45c4a56 | 2017-09-06 14:35:35 -0700 | [diff] [blame] | 48 | bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map, |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 49 | std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames, |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 50 | std::vector<std::string>* skip_names, BacktraceUnwindError* error) { |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 51 | UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 52 | auto process_memory = stack_map->process_memory(); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 53 | unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(), |
| 54 | regs, stack_map->process_memory()); |
Christopher Ferris | 2486d5a | 2018-01-22 17:37:59 -0800 | [diff] [blame] | 55 | if (stack_map->GetJitDebug() != nullptr) { |
| 56 | unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch()); |
| 57 | } |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 58 | #if !defined(NO_LIBDEXFILE_SUPPORT) |
| 59 | if (stack_map->GetDexFiles() != nullptr) { |
| 60 | unwinder.SetDexFiles(stack_map->GetDexFiles(), regs->Arch()); |
| 61 | } |
| 62 | #endif |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 63 | unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore()); |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 64 | if (error != nullptr) { |
| 65 | switch (unwinder.LastErrorCode()) { |
| 66 | case unwindstack::ERROR_NONE: |
| 67 | error->error_code = BACKTRACE_UNWIND_NO_ERROR; |
| 68 | break; |
| 69 | |
| 70 | case unwindstack::ERROR_MEMORY_INVALID: |
| 71 | error->error_code = BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED; |
| 72 | error->error_info.addr = unwinder.LastErrorAddress(); |
| 73 | break; |
| 74 | |
| 75 | case unwindstack::ERROR_UNWIND_INFO: |
| 76 | error->error_code = BACKTRACE_UNWIND_ERROR_UNWIND_INFO; |
| 77 | break; |
| 78 | |
| 79 | case unwindstack::ERROR_UNSUPPORTED: |
| 80 | error->error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION; |
| 81 | break; |
| 82 | |
| 83 | case unwindstack::ERROR_INVALID_MAP: |
| 84 | error->error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING; |
| 85 | break; |
| 86 | |
| 87 | case unwindstack::ERROR_MAX_FRAMES_EXCEEDED: |
| 88 | error->error_code = BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT; |
| 89 | break; |
| 90 | |
| 91 | case unwindstack::ERROR_REPEATED_FRAME: |
| 92 | error->error_code = BACKTRACE_UNWIND_ERROR_REPEATED_FRAME; |
| 93 | break; |
| 94 | } |
| 95 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 96 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 97 | if (num_ignore_frames >= unwinder.NumFrames()) { |
| 98 | frames->resize(0); |
| 99 | return true; |
| 100 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 101 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 102 | auto unwinder_frames = unwinder.frames(); |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 103 | frames->resize(unwinder.NumFrames() - num_ignore_frames); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 104 | size_t cur_frame = 0; |
David Srbecky | 645f8bb | 2018-01-25 12:21:59 +0000 | [diff] [blame] | 105 | for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) { |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 106 | auto frame = &unwinder_frames[i]; |
David Srbecky | 645f8bb | 2018-01-25 12:21:59 +0000 | [diff] [blame] | 107 | |
Christopher Ferris | 8fe5836 | 2018-01-26 14:26:13 -0800 | [diff] [blame] | 108 | backtrace_frame_data_t* back_frame = &frames->at(cur_frame); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 109 | |
Christopher Ferris | 8fe5836 | 2018-01-26 14:26:13 -0800 | [diff] [blame] | 110 | back_frame->num = cur_frame++; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 111 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 112 | back_frame->rel_pc = frame->rel_pc; |
| 113 | back_frame->pc = frame->pc; |
| 114 | back_frame->sp = frame->sp; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 115 | |
Christopher Ferris | 9a6b3e3 | 2017-10-18 09:08:51 -0700 | [diff] [blame] | 116 | back_frame->func_name = demangle(frame->function_name.c_str()); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 117 | back_frame->func_offset = frame->function_offset; |
| 118 | |
| 119 | back_frame->map.name = frame->map_name; |
| 120 | back_frame->map.start = frame->map_start; |
| 121 | back_frame->map.end = frame->map_end; |
| 122 | back_frame->map.offset = frame->map_offset; |
| 123 | back_frame->map.load_bias = frame->map_load_bias; |
| 124 | back_frame->map.flags = frame->map_flags; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map) |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 131 | : BacktraceCurrent(pid, tid, map) {} |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 132 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 133 | std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 134 | return GetMap()->GetFunctionName(pc, offset); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 137 | bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 138 | std::unique_ptr<unwindstack::Regs> regs; |
| 139 | if (ucontext == nullptr) { |
| 140 | regs.reset(unwindstack::Regs::CreateFromLocal()); |
| 141 | // Fill in the registers from this function. Do it here to avoid |
| 142 | // one extra function call appearing in the unwind. |
| 143 | unwindstack::RegsGetLocal(regs.get()); |
| 144 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 145 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext)); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 148 | std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"}; |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 149 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map) |
Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 153 | : BacktracePtrace(pid, tid, map), memory_(pid) {} |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 154 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 155 | std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 156 | return GetMap()->GetFunctionName(pc, offset); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 159 | bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 160 | std::unique_ptr<unwindstack::Regs> regs; |
| 161 | if (context == nullptr) { |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 162 | regs.reset(unwindstack::Regs::RemoteGet(Tid())); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 163 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 164 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context)); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 167 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 168 | } |
Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 169 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 170 | size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) { |
Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 171 | return memory_.Read(addr, buffer, bytes); |
| 172 | } |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 173 | |
| 174 | UnwindStackOffline::UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map, |
| 175 | bool map_shared) |
| 176 | : Backtrace(pid, tid, map), arch_(arch) { |
| 177 | map_shared_ = map_shared; |
| 178 | } |
| 179 | |
| 180 | bool UnwindStackOffline::Unwind(size_t num_ignore_frames, void* ucontext) { |
| 181 | if (ucontext == nullptr) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | unwindstack::ArchEnum arch; |
| 186 | switch (arch_) { |
| 187 | case ARCH_ARM: |
| 188 | arch = unwindstack::ARCH_ARM; |
| 189 | break; |
| 190 | case ARCH_ARM64: |
| 191 | arch = unwindstack::ARCH_ARM64; |
| 192 | break; |
| 193 | case ARCH_X86: |
| 194 | arch = unwindstack::ARCH_X86; |
| 195 | break; |
| 196 | case ARCH_X86_64: |
| 197 | arch = unwindstack::ARCH_X86_64; |
| 198 | break; |
| 199 | default: |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromUcontext(arch, ucontext)); |
| 204 | |
| 205 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_); |
| 206 | } |
| 207 | |
| 208 | std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) { |
| 209 | return ""; |
| 210 | } |
| 211 | |
| 212 | size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) { |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | bool UnwindStackOffline::ReadWord(uint64_t, word_t*) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, |
| 221 | const std::vector<backtrace_map_t>& maps, |
| 222 | const backtrace_stackinfo_t& stack) { |
| 223 | BacktraceMap* map = BacktraceMap::CreateOffline(pid, maps, stack); |
| 224 | if (map == nullptr) { |
| 225 | return nullptr; |
| 226 | } |
| 227 | |
| 228 | return new UnwindStackOffline(arch, pid, tid, map, false); |
| 229 | } |
| 230 | |
| 231 | Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) { |
| 232 | if (map == nullptr) { |
| 233 | return nullptr; |
| 234 | } |
| 235 | return new UnwindStackOffline(arch, pid, tid, map, true); |
| 236 | } |
| 237 | |
| 238 | void Backtrace::SetGlobalElfCache(bool enable) { |
| 239 | unwindstack::Elf::SetCachingEnabled(enable); |
| 240 | } |