Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [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 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/types.h> |
| 20 | |
Christopher Ferris | e903059 | 2018-01-12 23:55:29 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 24 | #include <backtrace/BacktraceMap.h> |
| 25 | #include <unwindstack/Elf.h> |
| 26 | #include <unwindstack/MapInfo.h> |
| 27 | #include <unwindstack/Maps.h> |
Christopher Ferris | 8fa3160 | 2018-10-30 16:25:13 | [diff] [blame] | 28 | #include <unwindstack/Regs.h> |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 29 | |
| 30 | #include "UnwindStackMap.h" |
| 31 | |
| 32 | //------------------------------------------------------------------------- |
| 33 | UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {} |
| 34 | |
| 35 | bool UnwindStackMap::Build() { |
| 36 | if (pid_ == 0) { |
| 37 | pid_ = getpid(); |
| 38 | stack_maps_.reset(new unwindstack::LocalMaps); |
| 39 | } else { |
| 40 | stack_maps_.reset(new unwindstack::RemoteMaps(pid_)); |
| 41 | } |
| 42 | |
Christopher Ferris | 23292b3 | 2017-09-07 00:16:42 | [diff] [blame] | 43 | // Create the process memory object. |
| 44 | process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_); |
| 45 | |
Christopher Ferris | e903059 | 2018-01-12 23:55:29 | [diff] [blame] | 46 | // Create a JitDebug object for getting jit unwind information. |
| 47 | std::vector<std::string> search_libs_{"libart.so", "libartd.so"}; |
| 48 | jit_debug_.reset(new unwindstack::JitDebug(process_memory_, search_libs_)); |
Christopher Ferris | 142d8d7 | 2018-02-01 00:36:22 | [diff] [blame] | 49 | #if !defined(NO_LIBDEXFILE_SUPPORT) |
David Srbecky | bca1c74 | 2018-02-03 11:35:21 | [diff] [blame] | 50 | dex_files_.reset(new unwindstack::DexFiles(process_memory_, search_libs_)); |
Christopher Ferris | 142d8d7 | 2018-02-01 00:36:22 | [diff] [blame] | 51 | #endif |
Christopher Ferris | e903059 | 2018-01-12 23:55:29 | [diff] [blame] | 52 | |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 53 | if (!stack_maps_->Parse()) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | // Iterate through the maps and fill in the backtrace_map_t structure. |
Christopher Ferris | 219eb8d | 2017-11-29 03:07:08 | [diff] [blame] | 58 | for (auto* map_info : *stack_maps_) { |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 59 | backtrace_map_t map; |
Christopher Ferris | 219eb8d | 2017-11-29 03:07:08 | [diff] [blame] | 60 | map.start = map_info->start; |
| 61 | map.end = map_info->end; |
| 62 | map.offset = map_info->offset; |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 63 | // Set to -1 so that it is demand loaded. |
Christopher Ferris | 103f457 | 2018-01-19 23:38:38 | [diff] [blame] | 64 | map.load_bias = static_cast<uint64_t>(-1); |
Christopher Ferris | 219eb8d | 2017-11-29 03:07:08 | [diff] [blame] | 65 | map.flags = map_info->flags; |
| 66 | map.name = map_info->name; |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 67 | |
| 68 | maps_.push_back(map); |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
Christopher Ferris | 103f457 | 2018-01-19 23:38:38 | [diff] [blame] | 74 | void UnwindStackMap::FillIn(uint64_t addr, backtrace_map_t* map) { |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 75 | BacktraceMap::FillIn(addr, map); |
Christopher Ferris | 103f457 | 2018-01-19 23:38:38 | [diff] [blame] | 76 | if (map->load_bias != static_cast<uint64_t>(-1)) { |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
| 80 | // Fill in the load_bias. |
| 81 | unwindstack::MapInfo* map_info = stack_maps_->Find(addr); |
| 82 | if (map_info == nullptr) { |
| 83 | return; |
| 84 | } |
Christopher Ferris | 4102ba0 | 2017-12-05 23:43:27 | [diff] [blame] | 85 | map->load_bias = map_info->GetLoadBias(process_memory_); |
| 86 | } |
| 87 | |
| 88 | uint64_t UnwindStackMap::GetLoadBias(size_t index) { |
| 89 | if (index >= stack_maps_->Total()) { |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | unwindstack::MapInfo* map_info = stack_maps_->Get(index); |
| 94 | if (map_info == nullptr) { |
| 95 | return 0; |
| 96 | } |
| 97 | return map_info->GetLoadBias(process_memory_); |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 98 | } |
| 99 | |
Christopher Ferris | 103f457 | 2018-01-19 23:38:38 | [diff] [blame] | 100 | std::string UnwindStackMap::GetFunctionName(uint64_t pc, uint64_t* offset) { |
Josh Gao | 3cc149e | 2017-10-24 04:08:01 | [diff] [blame] | 101 | *offset = 0; |
| 102 | unwindstack::Maps* maps = stack_maps(); |
| 103 | |
| 104 | // Get the map for this |
| 105 | unwindstack::MapInfo* map_info = maps->Find(pc); |
| 106 | if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) { |
| 107 | return ""; |
| 108 | } |
| 109 | |
Christopher Ferris | 8fa3160 | 2018-10-30 16:25:13 | [diff] [blame] | 110 | if (arch_ == unwindstack::ARCH_UNKNOWN) { |
| 111 | if (pid_ == getpid()) { |
| 112 | arch_ = unwindstack::Regs::CurrentArch(); |
| 113 | } else { |
| 114 | // Create a remote regs, to figure out the architecture. |
| 115 | std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::RemoteGet(pid_)); |
| 116 | arch_ = regs->Arch(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | unwindstack::Elf* elf = map_info->GetElf(process_memory(), arch_); |
Josh Gao | 3cc149e | 2017-10-24 04:08:01 | [diff] [blame] | 121 | |
| 122 | std::string name; |
| 123 | uint64_t func_offset; |
| 124 | if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) { |
| 125 | return ""; |
| 126 | } |
| 127 | *offset = func_offset; |
| 128 | return name; |
| 129 | } |
| 130 | |
| 131 | std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() { |
| 132 | return process_memory_; |
| 133 | } |
| 134 | |
Christopher Ferris | 703a27f | 2018-01-30 15:44:36 | [diff] [blame] | 135 | UnwindStackOfflineMap::UnwindStackOfflineMap(pid_t pid) : UnwindStackMap(pid) {} |
| 136 | |
| 137 | bool UnwindStackOfflineMap::Build() { |
| 138 | return false; |
| 139 | } |
| 140 | |
David Srbecky | 70ed650 | 2018-02-23 19:42:42 | [diff] [blame] | 141 | bool UnwindStackOfflineMap::Build(const std::vector<backtrace_map_t>& backtrace_maps) { |
Christopher Ferris | 703a27f | 2018-01-30 15:44:36 | [diff] [blame] | 142 | for (const backtrace_map_t& map : backtrace_maps) { |
| 143 | maps_.push_back(map); |
| 144 | } |
| 145 | |
| 146 | std::sort(maps_.begin(), maps_.end(), |
| 147 | [](const backtrace_map_t& a, const backtrace_map_t& b) { return a.start < b.start; }); |
| 148 | |
| 149 | unwindstack::Maps* maps = new unwindstack::Maps; |
| 150 | stack_maps_.reset(maps); |
| 151 | for (const backtrace_map_t& map : maps_) { |
| 152 | maps->Add(map.start, map.end, map.offset, map.flags, map.name, map.load_bias); |
| 153 | } |
David Srbecky | 70ed650 | 2018-02-23 19:42:42 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool UnwindStackOfflineMap::CreateProcessMemory(const backtrace_stackinfo_t& stack) { |
| 158 | if (stack.start >= stack.end) { |
| 159 | return false; |
| 160 | } |
Christopher Ferris | 703a27f | 2018-01-30 15:44:36 | [diff] [blame] | 161 | |
| 162 | // Create the process memory from the stack data. |
Christopher Ferris | 29d42cc | 2018-04-04 17:48:12 | [diff] [blame] | 163 | if (memory_ == nullptr) { |
| 164 | memory_ = new unwindstack::MemoryOfflineBuffer(stack.data, stack.start, stack.end); |
| 165 | process_memory_.reset(memory_); |
| 166 | } else { |
| 167 | memory_->Reset(stack.data, stack.start, stack.end); |
| 168 | } |
Christopher Ferris | 703a27f | 2018-01-30 15:44:36 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 172 | //------------------------------------------------------------------------- |
| 173 | // BacktraceMap create function. |
| 174 | //------------------------------------------------------------------------- |
Christopher Ferris | b0d1e7e | 2017-10-31 00:48:51 | [diff] [blame] | 175 | BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) { |
Christopher Ferris | 27e2460 | 2017-08-03 20:48:45 | [diff] [blame] | 176 | BacktraceMap* map; |
| 177 | |
| 178 | if (uncached) { |
| 179 | // Force use of the base class to parse the maps when this call is made. |
| 180 | map = new BacktraceMap(pid); |
| 181 | } else if (pid == getpid()) { |
| 182 | map = new UnwindStackMap(0); |
| 183 | } else { |
| 184 | map = new UnwindStackMap(pid); |
| 185 | } |
| 186 | if (!map->Build()) { |
| 187 | delete map; |
| 188 | return nullptr; |
| 189 | } |
| 190 | return map; |
| 191 | } |
Christopher Ferris | 703a27f | 2018-01-30 15:44:36 | [diff] [blame] | 192 | |
| 193 | //------------------------------------------------------------------------- |
| 194 | // BacktraceMap create offline function. |
| 195 | //------------------------------------------------------------------------- |
David Srbecky | 70ed650 | 2018-02-23 19:42:42 | [diff] [blame] | 196 | BacktraceMap* BacktraceMap::CreateOffline(pid_t pid, const std::vector<backtrace_map_t>& maps) { |
Christopher Ferris | 703a27f | 2018-01-30 15:44:36 | [diff] [blame] | 197 | UnwindStackOfflineMap* map = new UnwindStackOfflineMap(pid); |
David Srbecky | 70ed650 | 2018-02-23 19:42:42 | [diff] [blame] | 198 | if (!map->Build(maps)) { |
Christopher Ferris | 703a27f | 2018-01-30 15:44:36 | [diff] [blame] | 199 | delete map; |
| 200 | return nullptr; |
| 201 | } |
| 202 | return map; |
| 203 | } |