| 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 | 
|  | 18 | #include <assert.h> | 
|  | 19 | #include <stdint.h> | 
|  | 20 | #include <stdlib.h> | 
|  | 21 | #include <string.h> | 
|  | 22 | #include <ucontext.h> | 
|  | 23 |  | 
|  | 24 | #include <memory> | 
|  | 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 |  | 
|  | 40 | #include "BacktraceLog.h" | 
|  | 41 | #include "UnwindStack.h" | 
|  | 42 | #include "UnwindStackMap.h" | 
|  | 43 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 44 | static std::string GetFunctionName(BacktraceMap* back_map, uintptr_t pc, uintptr_t* offset) { | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 45 | *offset = 0; | 
|  | 46 | unwindstack::Maps* maps = reinterpret_cast<UnwindStackMap*>(back_map)->stack_maps(); | 
|  | 47 |  | 
|  | 48 | // Get the map for this | 
|  | 49 | unwindstack::MapInfo* map_info = maps->Find(pc); | 
|  | 50 | if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) { | 
|  | 51 | return ""; | 
|  | 52 | } | 
|  | 53 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 54 | UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map); | 
|  | 55 | unwindstack::Elf* elf = map_info->GetElf(stack_map->process_memory(), true); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 56 |  | 
|  | 57 | std::string name; | 
|  | 58 | uint64_t func_offset; | 
|  | 59 | if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) { | 
|  | 60 | return ""; | 
|  | 61 | } | 
|  | 62 | *offset = func_offset; | 
|  | 63 | return name; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | static bool IsUnwindLibrary(const std::string& map_name) { | 
|  | 67 | const std::string library(basename(map_name.c_str())); | 
|  | 68 | return library == "libunwindstack.so" || library == "libbacktrace.so"; | 
|  | 69 | } | 
|  | 70 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 71 | static bool Unwind(unwindstack::Regs* regs, BacktraceMap* back_map, | 
|  | 72 | std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames) { | 
|  | 73 | UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map); | 
|  | 74 | unwindstack::Maps* maps = stack_map->stack_maps(); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 75 | bool adjust_rel_pc = false; | 
|  | 76 | size_t num_frames = 0; | 
|  | 77 | frames->clear(); | 
|  | 78 | while (num_frames < MAX_BACKTRACE_FRAMES) { | 
|  | 79 | if (regs->pc() == 0) { | 
|  | 80 | break; | 
|  | 81 | } | 
|  | 82 | unwindstack::MapInfo* map_info = maps->Find(regs->pc()); | 
|  | 83 | if (map_info == nullptr) { | 
|  | 84 | break; | 
|  | 85 | } | 
|  | 86 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 87 | unwindstack::Elf* elf = map_info->GetElf(stack_map->process_memory(), true); | 
| Christopher Ferris | 3b4b075 | 2017-08-09 11:16:03 -0700 | [diff] [blame] | 88 | uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 89 |  | 
|  | 90 | bool skip_frame = num_frames == 0 && IsUnwindLibrary(map_info->name); | 
|  | 91 | if (num_ignore_frames == 0 && !skip_frame) { | 
|  | 92 | uint64_t adjusted_rel_pc = rel_pc; | 
| Christopher Ferris | 3b4b075 | 2017-08-09 11:16:03 -0700 | [diff] [blame] | 93 | if (adjust_rel_pc) { | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 94 | adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf); | 
|  | 95 | } | 
|  | 96 | frames->resize(num_frames + 1); | 
|  | 97 | backtrace_frame_data_t* frame = &frames->at(num_frames); | 
|  | 98 | frame->num = num_frames; | 
| Christopher Ferris | 3b4b075 | 2017-08-09 11:16:03 -0700 | [diff] [blame] | 99 | // This will point to the adjusted absolute pc. regs->pc() is | 
|  | 100 | // unaltered. | 
|  | 101 | frame->pc = map_info->start + adjusted_rel_pc; | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 102 | frame->sp = regs->sp(); | 
|  | 103 | frame->rel_pc = adjusted_rel_pc; | 
|  | 104 | frame->stack_size = 0; | 
|  | 105 |  | 
|  | 106 | frame->map.start = map_info->start; | 
|  | 107 | frame->map.end = map_info->end; | 
|  | 108 | frame->map.offset = map_info->offset; | 
|  | 109 | frame->map.load_bias = elf->GetLoadBias(); | 
|  | 110 | frame->map.flags = map_info->flags; | 
|  | 111 | frame->map.name = map_info->name; | 
|  | 112 |  | 
|  | 113 | uint64_t func_offset = 0; | 
| Christopher Ferris | 04fdec0 | 2017-08-11 15:17:46 -0700 | [diff] [blame] | 114 | if (elf->GetFunctionName(adjusted_rel_pc, &frame->func_name, &func_offset)) { | 
|  | 115 | frame->func_name = demangle(frame->func_name.c_str()); | 
|  | 116 | } else { | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 117 | frame->func_name = ""; | 
|  | 118 | } | 
|  | 119 | frame->func_offset = func_offset; | 
|  | 120 | if (num_frames > 0) { | 
|  | 121 | // Set the stack size for the previous frame. | 
|  | 122 | backtrace_frame_data_t* prev = &frames->at(num_frames - 1); | 
|  | 123 | prev->stack_size = frame->sp - prev->sp; | 
|  | 124 | } | 
|  | 125 | num_frames++; | 
|  | 126 | } else if (!skip_frame && num_ignore_frames > 0) { | 
|  | 127 | num_ignore_frames--; | 
|  | 128 | } | 
|  | 129 | adjust_rel_pc = true; | 
|  | 130 |  | 
|  | 131 | // Do not unwind through a device map. | 
|  | 132 | if (map_info->flags & PROT_DEVICE_MAP) { | 
|  | 133 | break; | 
|  | 134 | } | 
|  | 135 | unwindstack::MapInfo* sp_info = maps->Find(regs->sp()); | 
|  | 136 | if (sp_info->flags & PROT_DEVICE_MAP) { | 
|  | 137 | break; | 
|  | 138 | } | 
|  | 139 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 140 | if (!elf->Step(rel_pc + map_info->elf_offset, regs, stack_map->process_memory().get())) { | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 141 | break; | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | return true; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 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 |  | 
|  | 151 | std::string UnwindStackCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) { | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 152 | return ::GetFunctionName(GetMap(), pc, offset); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
|  | 155 | bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) { | 
|  | 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 { | 
| Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 163 | regs.reset( | 
|  | 164 | unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), ucontext)); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
|  | 167 | error_ = BACKTRACE_UNWIND_NO_ERROR; | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 168 | return ::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
|  | 171 | UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map) | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 172 | : BacktracePtrace(pid, tid, map) {} | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 173 |  | 
|  | 174 | std::string UnwindStackPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) { | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 175 | return ::GetFunctionName(GetMap(), pc, offset); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
|  | 178 | bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, ucontext_t* context) { | 
|  | 179 | std::unique_ptr<unwindstack::Regs> regs; | 
|  | 180 | if (context == nullptr) { | 
| Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 181 | regs.reset(unwindstack::Regs::RemoteGet(Tid())); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 182 | } else { | 
| Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 183 | regs.reset( | 
|  | 184 | unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentMachineType(), context)); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
|  | 187 | error_ = BACKTRACE_UNWIND_NO_ERROR; | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 188 | return ::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames); | 
| Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
|  | 191 | Backtrace* Backtrace::CreateNew(pid_t pid, pid_t tid, BacktraceMap* map) { | 
|  | 192 | if (pid == BACKTRACE_CURRENT_PROCESS) { | 
|  | 193 | pid = getpid(); | 
|  | 194 | if (tid == BACKTRACE_CURRENT_THREAD) { | 
|  | 195 | tid = gettid(); | 
|  | 196 | } | 
|  | 197 | } else if (tid == BACKTRACE_CURRENT_THREAD) { | 
|  | 198 | tid = pid; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | if (map == nullptr) { | 
|  | 202 | // This would cause the wrong type of map object to be created, so disallow. | 
|  | 203 | #if defined(__ANDROID__) | 
|  | 204 | __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, | 
|  | 205 | "Backtrace::CreateNew() must be called with a real map pointer."); | 
|  | 206 | #else | 
|  | 207 | BACK_LOGE("Backtrace::CreateNew() must be called with a real map pointer."); | 
|  | 208 | abort(); | 
|  | 209 | #endif | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | if (pid == getpid()) { | 
|  | 213 | return new UnwindStackCurrent(pid, tid, map); | 
|  | 214 | } else { | 
|  | 215 | return new UnwindStackPtrace(pid, tid, map); | 
|  | 216 | } | 
|  | 217 | } |