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> |
| 21 | #include <ucontext.h> |
| 22 | |
| 23 | #include <memory> |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 24 | #include <set> |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | |
| 27 | #if !defined(__ANDROID__) |
| 28 | #include <cutils/threads.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <backtrace/Backtrace.h> |
Christopher Ferris | 04fdec0 | 2017-08-11 15:17:46 -0700 | [diff] [blame] | 32 | #include <demangle.h> |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 33 | #include <unwindstack/Elf.h> |
| 34 | #include <unwindstack/MapInfo.h> |
| 35 | #include <unwindstack/Maps.h> |
| 36 | #include <unwindstack/Memory.h> |
| 37 | #include <unwindstack/Regs.h> |
| 38 | #include <unwindstack/RegsGetLocal.h> |
| 39 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 40 | #include <unwindstack/Unwinder.h> |
| 41 | |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 42 | #include "BacktraceLog.h" |
| 43 | #include "UnwindStack.h" |
| 44 | #include "UnwindStackMap.h" |
| 45 | |
Josh Gao | 45c4a56 | 2017-09-06 14:35:35 -0700 | [diff] [blame] | 46 | bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map, |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 47 | std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames, |
| 48 | std::vector<std::string>* skip_names) { |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 49 | UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 50 | auto process_memory = stack_map->process_memory(); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 51 | unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(), |
| 52 | regs, stack_map->process_memory()); |
Christopher Ferris | 2486d5a | 2018-01-22 17:37:59 -0800 | [diff] [blame^] | 53 | if (stack_map->GetJitDebug() != nullptr) { |
| 54 | unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch()); |
| 55 | } |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 56 | unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore()); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 57 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 58 | if (num_ignore_frames >= unwinder.NumFrames()) { |
| 59 | frames->resize(0); |
| 60 | return true; |
| 61 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 62 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 63 | frames->resize(unwinder.NumFrames() - num_ignore_frames); |
| 64 | auto unwinder_frames = unwinder.frames(); |
| 65 | size_t cur_frame = 0; |
| 66 | for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++, cur_frame++) { |
| 67 | auto frame = &unwinder_frames[i]; |
| 68 | backtrace_frame_data_t* back_frame = &frames->at(cur_frame); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 69 | |
Josh Gao | cd546c1 | 2017-10-25 15:21:45 -0700 | [diff] [blame] | 70 | back_frame->num = frame->num - num_ignore_frames; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 71 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 72 | back_frame->rel_pc = frame->rel_pc; |
| 73 | back_frame->pc = frame->pc; |
| 74 | back_frame->sp = frame->sp; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 75 | back_frame->dex_pc = frame->dex_pc; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 76 | |
Christopher Ferris | 9a6b3e3 | 2017-10-18 09:08:51 -0700 | [diff] [blame] | 77 | back_frame->func_name = demangle(frame->function_name.c_str()); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 78 | back_frame->func_offset = frame->function_offset; |
| 79 | |
| 80 | back_frame->map.name = frame->map_name; |
| 81 | back_frame->map.start = frame->map_start; |
| 82 | back_frame->map.end = frame->map_end; |
| 83 | back_frame->map.offset = frame->map_offset; |
| 84 | back_frame->map.load_bias = frame->map_load_bias; |
| 85 | back_frame->map.flags = frame->map_flags; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map) |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 92 | : BacktraceCurrent(pid, tid, map) {} |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 93 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 94 | std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 95 | return GetMap()->GetFunctionName(pc, offset); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) { |
| 99 | std::unique_ptr<unwindstack::Regs> regs; |
| 100 | if (ucontext == nullptr) { |
| 101 | regs.reset(unwindstack::Regs::CreateFromLocal()); |
| 102 | // Fill in the registers from this function. Do it here to avoid |
| 103 | // one extra function call appearing in the unwind. |
| 104 | unwindstack::RegsGetLocal(regs.get()); |
| 105 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 106 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext)); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 109 | error_.error_code = BACKTRACE_UNWIND_NO_ERROR; |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 110 | std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"}; |
| 111 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map) |
Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 115 | : BacktracePtrace(pid, tid, map), memory_(pid) {} |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 116 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 117 | std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 118 | return GetMap()->GetFunctionName(pc, offset); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) { |
| 122 | std::unique_ptr<unwindstack::Regs> regs; |
| 123 | if (context == nullptr) { |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 124 | regs.reset(unwindstack::Regs::RemoteGet(Tid())); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 125 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 126 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context)); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 129 | error_.error_code = BACKTRACE_UNWIND_NO_ERROR; |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 130 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 131 | } |
Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 132 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 133 | 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] | 134 | return memory_.Read(addr, buffer, bytes); |
| 135 | } |