| 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 |  | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 26 | #include <backtrace/Backtrace.h> | 
| Christopher Ferris | 04fdec0 | 2017-08-11 15:17:46 -0700 | [diff] [blame] | 27 | #include <demangle.h> | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 28 | #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 Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 35 | #if !defined(NO_LIBDEXFILE_SUPPORT) | 
|  | 36 | #include <unwindstack/DexFiles.h> | 
|  | 37 | #endif | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 38 | #include <unwindstack/Unwinder.h> | 
|  | 39 |  | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 40 | #include "BacktraceLog.h" | 
|  | 41 | #include "UnwindStack.h" | 
|  | 42 | #include "UnwindStackMap.h" | 
|  | 43 |  | 
| Josh Gao | 45c4a56 | 2017-09-06 14:35:35 -0700 | [diff] [blame] | 44 | bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map, | 
| Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 45 | std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames, | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 46 | std::vector<std::string>* skip_names, BacktraceUnwindError* error) { | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 47 | UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map); | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 48 | auto process_memory = stack_map->process_memory(); | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 49 | unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(), | 
|  | 50 | regs, stack_map->process_memory()); | 
| Christopher Ferris | e4b3a6a | 2018-02-20 13:58:40 -0800 | [diff] [blame] | 51 | unwinder.SetResolveNames(stack_map->ResolveNames()); | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 52 | stack_map->SetArch(regs->Arch()); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 53 | 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 Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 61 | unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore()); | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 62 | 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 Ferris | d11ed86 | 2019-04-11 19:45:35 -0700 | [diff] [blame] | 92 |  | 
|  | 93 | case unwindstack::ERROR_INVALID_ELF: | 
|  | 94 | error->error_code = BACKTRACE_UNWIND_ERROR_INVALID_ELF; | 
|  | 95 | break; | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 96 | } | 
|  | 97 | } | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 98 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 99 | if (num_ignore_frames >= unwinder.NumFrames()) { | 
|  | 100 | frames->resize(0); | 
|  | 101 | return true; | 
|  | 102 | } | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 103 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 104 | auto unwinder_frames = unwinder.frames(); | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 105 | frames->resize(unwinder.NumFrames() - num_ignore_frames); | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 106 | size_t cur_frame = 0; | 
| David Srbecky | 645f8bb | 2018-01-25 12:21:59 +0000 | [diff] [blame] | 107 | for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) { | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 108 | auto frame = &unwinder_frames[i]; | 
| David Srbecky | 645f8bb | 2018-01-25 12:21:59 +0000 | [diff] [blame] | 109 |  | 
| Christopher Ferris | 8fe5836 | 2018-01-26 14:26:13 -0800 | [diff] [blame] | 110 | backtrace_frame_data_t* back_frame = &frames->at(cur_frame); | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 111 |  | 
| Christopher Ferris | 8fe5836 | 2018-01-26 14:26:13 -0800 | [diff] [blame] | 112 | back_frame->num = cur_frame++; | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 113 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 114 | back_frame->rel_pc = frame->rel_pc; | 
|  | 115 | back_frame->pc = frame->pc; | 
|  | 116 | back_frame->sp = frame->sp; | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 117 |  | 
| Christopher Ferris | 9a6b3e3 | 2017-10-18 09:08:51 -0700 | [diff] [blame] | 118 | back_frame->func_name = demangle(frame->function_name.c_str()); | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 119 | 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 Ferris | a09c4a6 | 2018-12-13 16:08:50 -0800 | [diff] [blame] | 124 | back_frame->map.offset = frame->map_elf_start_offset; | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 125 | back_frame->map.load_bias = frame->map_load_bias; | 
|  | 126 | back_frame->map.flags = frame->map_flags; | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
|  | 129 | return true; | 
|  | 130 | } | 
|  | 131 |  | 
| Christopher Ferris | 432981b | 2018-02-22 19:42:53 -0800 | [diff] [blame] | 132 | bool 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 Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 148 | UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map) | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 149 | : BacktraceCurrent(pid, tid, map) {} | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 150 |  | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 151 | std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { | 
| Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 152 | return GetMap()->GetFunctionName(pc, offset); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 155 | bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) { | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 156 | 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 Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 163 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext)); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 166 | std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"}; | 
| Christopher Ferris | 458f4e7 | 2018-03-23 12:51:43 -0700 | [diff] [blame] | 167 | if (!skip_frames_) { | 
|  | 168 | skip_names.clear(); | 
|  | 169 | } | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 170 | 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] | 171 | } | 
|  | 172 |  | 
|  | 173 | UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map) | 
| Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 174 | : BacktracePtrace(pid, tid, map), memory_(pid) {} | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 175 |  | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 176 | std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { | 
| Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 177 | return GetMap()->GetFunctionName(pc, offset); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 180 | bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) { | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 181 | std::unique_ptr<unwindstack::Regs> regs; | 
|  | 182 | if (context == nullptr) { | 
| Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 183 | regs.reset(unwindstack::Regs::RemoteGet(Tid())); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 184 | } else { | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 185 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context)); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 188 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 189 | } | 
| Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 190 |  | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 191 | 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] | 192 | return memory_.Read(addr, buffer, bytes); | 
|  | 193 | } | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 194 |  | 
|  | 195 | UnwindStackOffline::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 |  | 
|  | 201 | bool 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 |  | 
|  | 229 | std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) { | 
|  | 230 | return ""; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) { | 
|  | 234 | return 0; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | bool UnwindStackOffline::ReadWord(uint64_t, word_t*) { | 
|  | 238 | return false; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | Backtrace* 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 Ferris | 432981b | 2018-02-22 19:42:53 -0800 | [diff] [blame] | 244 | std::unique_ptr<UnwindStackOfflineMap> map( | 
|  | 245 | reinterpret_cast<UnwindStackOfflineMap*>(BacktraceMap::CreateOffline(pid, maps))); | 
|  | 246 | if (map.get() == nullptr || !map->CreateProcessMemory(stack)) { | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 247 | return nullptr; | 
|  | 248 | } | 
| Christopher Ferris | 432981b | 2018-02-22 19:42:53 -0800 | [diff] [blame] | 249 | return new UnwindStackOffline(arch, pid, tid, map.release(), false); | 
| Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 250 | } | 
|  | 251 |  | 
|  | 252 | Backtrace* 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 |  | 
|  | 259 | void Backtrace::SetGlobalElfCache(bool enable) { | 
|  | 260 | unwindstack::Elf::SetCachingEnabled(enable); | 
|  | 261 | } |