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 | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 39 | #include <unwindstack/Unwinder.h> |
| 40 | |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 41 | #include "BacktraceLog.h" |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 42 | #ifndef NO_LIBDEXFILE |
| 43 | #include "UnwindDexFile.h" |
| 44 | #endif |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 45 | #include "UnwindStack.h" |
| 46 | #include "UnwindStackMap.h" |
| 47 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 48 | static void FillInDexFrame(UnwindStackMap* stack_map, uint64_t dex_pc, |
| 49 | backtrace_frame_data_t* frame) { |
| 50 | // The DEX PC points into the .dex section within an ELF file. |
| 51 | // However, this is a BBS section manually mmaped to a .vdex file, |
| 52 | // so we need to get the following map to find the ELF data. |
| 53 | unwindstack::Maps* maps = stack_map->stack_maps(); |
| 54 | auto it = maps->begin(); |
| 55 | uint64_t rel_dex_pc; |
| 56 | unwindstack::MapInfo* info; |
| 57 | for (; it != maps->end(); ++it) { |
| 58 | auto entry = *it; |
| 59 | if (dex_pc >= entry->start && dex_pc < entry->end) { |
| 60 | info = entry; |
| 61 | rel_dex_pc = dex_pc - entry->start; |
| 62 | frame->map.start = entry->start; |
| 63 | frame->map.end = entry->end; |
| 64 | frame->map.offset = entry->offset; |
| 65 | frame->map.load_bias = entry->load_bias; |
| 66 | frame->map.flags = entry->flags; |
| 67 | frame->map.name = entry->name; |
| 68 | frame->rel_pc = rel_dex_pc; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | if (it == maps->end() || ++it == maps->end()) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | auto entry = *it; |
| 77 | auto process_memory = stack_map->process_memory(); |
| 78 | unwindstack::Elf* elf = entry->GetElf(process_memory, true); |
| 79 | if (!elf->valid()) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Adjust the relative dex by the offset. |
| 84 | rel_dex_pc += entry->elf_offset; |
| 85 | |
| 86 | uint64_t dex_offset; |
| 87 | if (!elf->GetFunctionName(rel_dex_pc, &frame->func_name, &dex_offset)) { |
| 88 | return; |
| 89 | } |
| 90 | frame->func_offset = dex_offset; |
| 91 | if (frame->func_name != "$dexfile") { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | #ifndef NO_LIBDEXFILE |
| 96 | UnwindDexFile* dex_file = stack_map->GetDexFile(dex_pc - dex_offset, info); |
| 97 | if (dex_file != nullptr) { |
| 98 | dex_file->GetMethodInformation(dex_offset, &frame->func_name, &frame->func_offset); |
| 99 | } |
| 100 | #endif |
| 101 | } |
| 102 | |
Josh Gao | 45c4a56 | 2017-09-06 14:35:35 -0700 | [diff] [blame] | 103 | bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map, |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 104 | std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames, |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 105 | std::vector<std::string>* skip_names, BacktraceUnwindError* error) { |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 106 | UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 107 | auto process_memory = stack_map->process_memory(); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 108 | unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(), |
| 109 | regs, stack_map->process_memory()); |
Christopher Ferris | 2486d5a | 2018-01-22 17:37:59 -0800 | [diff] [blame] | 110 | if (stack_map->GetJitDebug() != nullptr) { |
| 111 | unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch()); |
| 112 | } |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 113 | unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore()); |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 114 | if (error != nullptr) { |
| 115 | switch (unwinder.LastErrorCode()) { |
| 116 | case unwindstack::ERROR_NONE: |
| 117 | error->error_code = BACKTRACE_UNWIND_NO_ERROR; |
| 118 | break; |
| 119 | |
| 120 | case unwindstack::ERROR_MEMORY_INVALID: |
| 121 | error->error_code = BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED; |
| 122 | error->error_info.addr = unwinder.LastErrorAddress(); |
| 123 | break; |
| 124 | |
| 125 | case unwindstack::ERROR_UNWIND_INFO: |
| 126 | error->error_code = BACKTRACE_UNWIND_ERROR_UNWIND_INFO; |
| 127 | break; |
| 128 | |
| 129 | case unwindstack::ERROR_UNSUPPORTED: |
| 130 | error->error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION; |
| 131 | break; |
| 132 | |
| 133 | case unwindstack::ERROR_INVALID_MAP: |
| 134 | error->error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING; |
| 135 | break; |
| 136 | |
| 137 | case unwindstack::ERROR_MAX_FRAMES_EXCEEDED: |
| 138 | error->error_code = BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT; |
| 139 | break; |
| 140 | |
| 141 | case unwindstack::ERROR_REPEATED_FRAME: |
| 142 | error->error_code = BACKTRACE_UNWIND_ERROR_REPEATED_FRAME; |
| 143 | break; |
| 144 | } |
| 145 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 146 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 147 | if (num_ignore_frames >= unwinder.NumFrames()) { |
| 148 | frames->resize(0); |
| 149 | return true; |
| 150 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 151 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 152 | auto unwinder_frames = unwinder.frames(); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 153 | // Get the real number of frames we'll need. |
| 154 | size_t total_frames = 0; |
| 155 | for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++, total_frames++) { |
| 156 | if (unwinder_frames[i].dex_pc != 0) { |
| 157 | total_frames++; |
| 158 | } |
| 159 | } |
| 160 | frames->resize(total_frames); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 161 | size_t cur_frame = 0; |
David Srbecky | 645f8bb | 2018-01-25 12:21:59 +0000 | [diff] [blame] | 162 | for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) { |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 163 | auto frame = &unwinder_frames[i]; |
David Srbecky | 645f8bb | 2018-01-25 12:21:59 +0000 | [diff] [blame] | 164 | |
| 165 | // Inject extra 'virtual' frame that represents the dex pc data. |
| 166 | // The dex pc is magic register defined in the Mterp interpreter, |
| 167 | // and thus it will be restored/observed in the frame after it. |
| 168 | // Adding the dex frame first here will create something like: |
| 169 | // #7 pc 006b1ba1 libartd.so ExecuteMterpImpl+14625 |
| 170 | // #8 pc 0015fa20 core.vdex java.util.Arrays.binarySearch+8 |
| 171 | // #9 pc 0039a1ef libartd.so art::interpreter::Execute+719 |
| 172 | if (frame->dex_pc != 0) { |
Christopher Ferris | 8fe5836 | 2018-01-26 14:26:13 -0800 | [diff] [blame] | 173 | backtrace_frame_data_t* dex_frame = &frames->at(cur_frame); |
| 174 | dex_frame->num = cur_frame++; |
David Srbecky | 645f8bb | 2018-01-25 12:21:59 +0000 | [diff] [blame] | 175 | dex_frame->pc = frame->dex_pc; |
| 176 | dex_frame->rel_pc = frame->dex_pc; |
| 177 | dex_frame->sp = frame->sp; |
| 178 | dex_frame->stack_size = 0; |
| 179 | dex_frame->func_offset = 0; |
| 180 | FillInDexFrame(stack_map, frame->dex_pc, dex_frame); |
| 181 | } |
| 182 | |
Christopher Ferris | 8fe5836 | 2018-01-26 14:26:13 -0800 | [diff] [blame] | 183 | backtrace_frame_data_t* back_frame = &frames->at(cur_frame); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 184 | |
Christopher Ferris | 8fe5836 | 2018-01-26 14:26:13 -0800 | [diff] [blame] | 185 | back_frame->num = cur_frame++; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 186 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 187 | back_frame->rel_pc = frame->rel_pc; |
| 188 | back_frame->pc = frame->pc; |
| 189 | back_frame->sp = frame->sp; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 190 | |
Christopher Ferris | 9a6b3e3 | 2017-10-18 09:08:51 -0700 | [diff] [blame] | 191 | back_frame->func_name = demangle(frame->function_name.c_str()); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 192 | back_frame->func_offset = frame->function_offset; |
| 193 | |
| 194 | back_frame->map.name = frame->map_name; |
| 195 | back_frame->map.start = frame->map_start; |
| 196 | back_frame->map.end = frame->map_end; |
| 197 | back_frame->map.offset = frame->map_offset; |
| 198 | back_frame->map.load_bias = frame->map_load_bias; |
| 199 | back_frame->map.flags = frame->map_flags; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map) |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 206 | : BacktraceCurrent(pid, tid, map) {} |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 207 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 208 | std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 209 | return GetMap()->GetFunctionName(pc, offset); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 212 | bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 213 | std::unique_ptr<unwindstack::Regs> regs; |
| 214 | if (ucontext == nullptr) { |
| 215 | regs.reset(unwindstack::Regs::CreateFromLocal()); |
| 216 | // Fill in the registers from this function. Do it here to avoid |
| 217 | // one extra function call appearing in the unwind. |
| 218 | unwindstack::RegsGetLocal(regs.get()); |
| 219 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 220 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext)); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 223 | std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"}; |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 224 | 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] | 225 | } |
| 226 | |
| 227 | UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map) |
Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 228 | : BacktracePtrace(pid, tid, map), memory_(pid) {} |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 229 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 230 | std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 231 | return GetMap()->GetFunctionName(pc, offset); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 234 | bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 235 | std::unique_ptr<unwindstack::Regs> regs; |
| 236 | if (context == nullptr) { |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 237 | regs.reset(unwindstack::Regs::RemoteGet(Tid())); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 238 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 239 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context)); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 242 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 243 | } |
Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 244 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 245 | 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] | 246 | return memory_.Read(addr, buffer, bytes); |
| 247 | } |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 248 | |
| 249 | UnwindStackOffline::UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map, |
| 250 | bool map_shared) |
| 251 | : Backtrace(pid, tid, map), arch_(arch) { |
| 252 | map_shared_ = map_shared; |
| 253 | } |
| 254 | |
| 255 | bool UnwindStackOffline::Unwind(size_t num_ignore_frames, void* ucontext) { |
| 256 | if (ucontext == nullptr) { |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | unwindstack::ArchEnum arch; |
| 261 | switch (arch_) { |
| 262 | case ARCH_ARM: |
| 263 | arch = unwindstack::ARCH_ARM; |
| 264 | break; |
| 265 | case ARCH_ARM64: |
| 266 | arch = unwindstack::ARCH_ARM64; |
| 267 | break; |
| 268 | case ARCH_X86: |
| 269 | arch = unwindstack::ARCH_X86; |
| 270 | break; |
| 271 | case ARCH_X86_64: |
| 272 | arch = unwindstack::ARCH_X86_64; |
| 273 | break; |
| 274 | default: |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromUcontext(arch, ucontext)); |
| 279 | |
| 280 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_); |
| 281 | } |
| 282 | |
| 283 | std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) { |
| 284 | return ""; |
| 285 | } |
| 286 | |
| 287 | size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) { |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | bool UnwindStackOffline::ReadWord(uint64_t, word_t*) { |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, |
| 296 | const std::vector<backtrace_map_t>& maps, |
| 297 | const backtrace_stackinfo_t& stack) { |
| 298 | BacktraceMap* map = BacktraceMap::CreateOffline(pid, maps, stack); |
| 299 | if (map == nullptr) { |
| 300 | return nullptr; |
| 301 | } |
| 302 | |
| 303 | return new UnwindStackOffline(arch, pid, tid, map, false); |
| 304 | } |
| 305 | |
| 306 | Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) { |
| 307 | if (map == nullptr) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | return new UnwindStackOffline(arch, pid, tid, map, true); |
| 311 | } |
| 312 | |
| 313 | void Backtrace::SetGlobalElfCache(bool enable) { |
| 314 | unwindstack::Elf::SetCachingEnabled(enable); |
| 315 | } |