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" |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 43 | #ifndef NO_LIBDEXFILE |
| 44 | #include "UnwindDexFile.h" |
| 45 | #endif |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 46 | #include "UnwindStack.h" |
| 47 | #include "UnwindStackMap.h" |
| 48 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 49 | static void FillInDexFrame(UnwindStackMap* stack_map, uint64_t dex_pc, |
| 50 | backtrace_frame_data_t* frame) { |
| 51 | // The DEX PC points into the .dex section within an ELF file. |
| 52 | // However, this is a BBS section manually mmaped to a .vdex file, |
| 53 | // so we need to get the following map to find the ELF data. |
| 54 | unwindstack::Maps* maps = stack_map->stack_maps(); |
| 55 | auto it = maps->begin(); |
| 56 | uint64_t rel_dex_pc; |
| 57 | unwindstack::MapInfo* info; |
| 58 | for (; it != maps->end(); ++it) { |
| 59 | auto entry = *it; |
| 60 | if (dex_pc >= entry->start && dex_pc < entry->end) { |
| 61 | info = entry; |
| 62 | rel_dex_pc = dex_pc - entry->start; |
| 63 | frame->map.start = entry->start; |
| 64 | frame->map.end = entry->end; |
| 65 | frame->map.offset = entry->offset; |
| 66 | frame->map.load_bias = entry->load_bias; |
| 67 | frame->map.flags = entry->flags; |
| 68 | frame->map.name = entry->name; |
| 69 | frame->rel_pc = rel_dex_pc; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | if (it == maps->end() || ++it == maps->end()) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | auto entry = *it; |
| 78 | auto process_memory = stack_map->process_memory(); |
| 79 | unwindstack::Elf* elf = entry->GetElf(process_memory, true); |
| 80 | if (!elf->valid()) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Adjust the relative dex by the offset. |
| 85 | rel_dex_pc += entry->elf_offset; |
| 86 | |
| 87 | uint64_t dex_offset; |
| 88 | if (!elf->GetFunctionName(rel_dex_pc, &frame->func_name, &dex_offset)) { |
| 89 | return; |
| 90 | } |
| 91 | frame->func_offset = dex_offset; |
| 92 | if (frame->func_name != "$dexfile") { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | #ifndef NO_LIBDEXFILE |
| 97 | UnwindDexFile* dex_file = stack_map->GetDexFile(dex_pc - dex_offset, info); |
| 98 | if (dex_file != nullptr) { |
| 99 | dex_file->GetMethodInformation(dex_offset, &frame->func_name, &frame->func_offset); |
| 100 | } |
| 101 | #endif |
| 102 | } |
| 103 | |
Josh Gao | 45c4a56 | 2017-09-06 14:35:35 -0700 | [diff] [blame] | 104 | bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map, |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 105 | std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames, |
| 106 | std::vector<std::string>* skip_names) { |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 107 | UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 108 | auto process_memory = stack_map->process_memory(); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 109 | unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(), |
| 110 | regs, stack_map->process_memory()); |
Christopher Ferris | 2486d5a | 2018-01-22 17:37:59 -0800 | [diff] [blame] | 111 | if (stack_map->GetJitDebug() != nullptr) { |
| 112 | unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch()); |
| 113 | } |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 114 | unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore()); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 115 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 116 | if (num_ignore_frames >= unwinder.NumFrames()) { |
| 117 | frames->resize(0); |
| 118 | return true; |
| 119 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 120 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 121 | auto unwinder_frames = unwinder.frames(); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 122 | // Get the real number of frames we'll need. |
| 123 | size_t total_frames = 0; |
| 124 | for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++, total_frames++) { |
| 125 | if (unwinder_frames[i].dex_pc != 0) { |
| 126 | total_frames++; |
| 127 | } |
| 128 | } |
| 129 | frames->resize(total_frames); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 130 | size_t cur_frame = 0; |
David Srbecky | 645f8bb | 2018-01-25 12:21:59 +0000 | [diff] [blame^] | 131 | for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) { |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 132 | auto frame = &unwinder_frames[i]; |
David Srbecky | 645f8bb | 2018-01-25 12:21:59 +0000 | [diff] [blame^] | 133 | |
| 134 | // Inject extra 'virtual' frame that represents the dex pc data. |
| 135 | // The dex pc is magic register defined in the Mterp interpreter, |
| 136 | // and thus it will be restored/observed in the frame after it. |
| 137 | // Adding the dex frame first here will create something like: |
| 138 | // #7 pc 006b1ba1 libartd.so ExecuteMterpImpl+14625 |
| 139 | // #8 pc 0015fa20 core.vdex java.util.Arrays.binarySearch+8 |
| 140 | // #9 pc 0039a1ef libartd.so art::interpreter::Execute+719 |
| 141 | if (frame->dex_pc != 0) { |
| 142 | backtrace_frame_data_t* dex_frame = &frames->at(cur_frame++); |
| 143 | dex_frame->num = cur_frame; |
| 144 | dex_frame->pc = frame->dex_pc; |
| 145 | dex_frame->rel_pc = frame->dex_pc; |
| 146 | dex_frame->sp = frame->sp; |
| 147 | dex_frame->stack_size = 0; |
| 148 | dex_frame->func_offset = 0; |
| 149 | FillInDexFrame(stack_map, frame->dex_pc, dex_frame); |
| 150 | } |
| 151 | |
| 152 | backtrace_frame_data_t* back_frame = &frames->at(cur_frame++); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 153 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 154 | back_frame->num = cur_frame; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 155 | |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 156 | back_frame->rel_pc = frame->rel_pc; |
| 157 | back_frame->pc = frame->pc; |
| 158 | back_frame->sp = frame->sp; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 159 | |
Christopher Ferris | 9a6b3e3 | 2017-10-18 09:08:51 -0700 | [diff] [blame] | 160 | back_frame->func_name = demangle(frame->function_name.c_str()); |
Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 161 | back_frame->func_offset = frame->function_offset; |
| 162 | |
| 163 | back_frame->map.name = frame->map_name; |
| 164 | back_frame->map.start = frame->map_start; |
| 165 | back_frame->map.end = frame->map_end; |
| 166 | back_frame->map.offset = frame->map_offset; |
| 167 | back_frame->map.load_bias = frame->map_load_bias; |
| 168 | back_frame->map.flags = frame->map_flags; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map) |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 175 | : BacktraceCurrent(pid, tid, map) {} |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 176 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 177 | std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 178 | return GetMap()->GetFunctionName(pc, offset); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) { |
| 182 | std::unique_ptr<unwindstack::Regs> regs; |
| 183 | if (ucontext == nullptr) { |
| 184 | regs.reset(unwindstack::Regs::CreateFromLocal()); |
| 185 | // Fill in the registers from this function. Do it here to avoid |
| 186 | // one extra function call appearing in the unwind. |
| 187 | unwindstack::RegsGetLocal(regs.get()); |
| 188 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 189 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext)); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 192 | error_.error_code = BACKTRACE_UNWIND_NO_ERROR; |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 193 | std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"}; |
| 194 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map) |
Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 198 | : BacktracePtrace(pid, tid, map), memory_(pid) {} |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 199 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 200 | std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) { |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 201 | return GetMap()->GetFunctionName(pc, offset); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) { |
| 205 | std::unique_ptr<unwindstack::Regs> regs; |
| 206 | if (context == nullptr) { |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 207 | regs.reset(unwindstack::Regs::RemoteGet(Tid())); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 208 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 209 | regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context)); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 212 | error_.error_code = BACKTRACE_UNWIND_NO_ERROR; |
Christopher Ferris | c56a499 | 2017-11-02 16:19:56 -0700 | [diff] [blame] | 213 | return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 214 | } |
Christopher Ferris | e328673 | 2017-12-07 17:41:18 -0800 | [diff] [blame] | 215 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 216 | 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] | 217 | return memory_.Read(addr, buffer, bytes); |
| 218 | } |