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 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/types.h> |
| 20 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 24 | #include <backtrace/BacktraceMap.h> |
| 25 | #include <unwindstack/Elf.h> |
| 26 | #include <unwindstack/MapInfo.h> |
| 27 | #include <unwindstack/Maps.h> |
| 28 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 29 | #include "UnwindDexFile.h" |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 30 | #include "UnwindStackMap.h" |
| 31 | |
| 32 | //------------------------------------------------------------------------- |
| 33 | UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {} |
| 34 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 35 | UnwindStackMap::~UnwindStackMap() { |
| 36 | #ifndef NO_LIBDEXFILE |
| 37 | for (auto& entry : dex_files_) { |
| 38 | delete entry.second; |
| 39 | } |
| 40 | #endif |
| 41 | } |
| 42 | |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 43 | bool UnwindStackMap::Build() { |
| 44 | if (pid_ == 0) { |
| 45 | pid_ = getpid(); |
| 46 | stack_maps_.reset(new unwindstack::LocalMaps); |
| 47 | } else { |
| 48 | stack_maps_.reset(new unwindstack::RemoteMaps(pid_)); |
| 49 | } |
| 50 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 51 | // Create the process memory object. |
| 52 | process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_); |
| 53 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 54 | // Create a JitDebug object for getting jit unwind information. |
| 55 | std::vector<std::string> search_libs_{"libart.so", "libartd.so"}; |
| 56 | jit_debug_.reset(new unwindstack::JitDebug(process_memory_, search_libs_)); |
| 57 | |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 58 | if (!stack_maps_->Parse()) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // Iterate through the maps and fill in the backtrace_map_t structure. |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 63 | for (auto* map_info : *stack_maps_) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 64 | backtrace_map_t map; |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 65 | map.start = map_info->start; |
| 66 | map.end = map_info->end; |
| 67 | map.offset = map_info->offset; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 68 | // Set to -1 so that it is demand loaded. |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 69 | map.load_bias = static_cast<uint64_t>(-1); |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 70 | map.flags = map_info->flags; |
| 71 | map.name = map_info->name; |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 72 | |
| 73 | maps_.push_back(map); |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 79 | void UnwindStackMap::FillIn(uint64_t addr, backtrace_map_t* map) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 80 | BacktraceMap::FillIn(addr, map); |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 81 | if (map->load_bias != static_cast<uint64_t>(-1)) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | // Fill in the load_bias. |
| 86 | unwindstack::MapInfo* map_info = stack_maps_->Find(addr); |
| 87 | if (map_info == nullptr) { |
| 88 | return; |
| 89 | } |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 90 | map->load_bias = map_info->GetLoadBias(process_memory_); |
| 91 | } |
| 92 | |
| 93 | uint64_t UnwindStackMap::GetLoadBias(size_t index) { |
| 94 | if (index >= stack_maps_->Total()) { |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | unwindstack::MapInfo* map_info = stack_maps_->Get(index); |
| 99 | if (map_info == nullptr) { |
| 100 | return 0; |
| 101 | } |
| 102 | return map_info->GetLoadBias(process_memory_); |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 105 | std::string UnwindStackMap::GetFunctionName(uint64_t pc, uint64_t* offset) { |
Josh Gao | 358de18 | 2017-09-06 22:16:09 -0700 | [diff] [blame] | 106 | *offset = 0; |
| 107 | unwindstack::Maps* maps = stack_maps(); |
| 108 | |
| 109 | // Get the map for this |
| 110 | unwindstack::MapInfo* map_info = maps->Find(pc); |
| 111 | if (map_info == nullptr || map_info->flags & PROT_DEVICE_MAP) { |
| 112 | return ""; |
| 113 | } |
| 114 | |
| 115 | unwindstack::Elf* elf = map_info->GetElf(process_memory(), true); |
| 116 | |
| 117 | std::string name; |
| 118 | uint64_t func_offset; |
| 119 | if (!elf->GetFunctionName(elf->GetRelPc(pc, map_info), &name, &func_offset)) { |
| 120 | return ""; |
| 121 | } |
| 122 | *offset = func_offset; |
| 123 | return name; |
| 124 | } |
| 125 | |
Josh Gao | ebea0ef | 2017-09-06 15:39:14 -0700 | [diff] [blame] | 126 | std::shared_ptr<unwindstack::Memory> UnwindStackMap::GetProcessMemory() { |
| 127 | return process_memory_; |
| 128 | } |
| 129 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 130 | #ifdef NO_LIBDEXFILE |
| 131 | UnwindDexFile* UnwindStackMap::GetDexFile(uint64_t, unwindstack::MapInfo*) { |
| 132 | return nullptr; |
| 133 | } |
| 134 | #else |
| 135 | UnwindDexFile* UnwindStackMap::GetDexFile(uint64_t dex_file_offset, unwindstack::MapInfo* info) { |
| 136 | // Lock while we get the data. |
| 137 | std::lock_guard<std::mutex> guard(dex_lock_); |
| 138 | UnwindDexFile* dex_file; |
| 139 | auto entry = dex_files_.find(dex_file_offset); |
| 140 | if (entry == dex_files_.end()) { |
| 141 | dex_file = UnwindDexFile::Create(dex_file_offset, process_memory_.get(), info); |
| 142 | dex_files_[dex_file_offset] = dex_file; |
| 143 | } else { |
| 144 | dex_file = entry->second; |
| 145 | } |
| 146 | return dex_file; |
| 147 | } |
| 148 | #endif |
| 149 | |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 150 | UnwindStackOfflineMap::UnwindStackOfflineMap(pid_t pid) : UnwindStackMap(pid) {} |
| 151 | |
| 152 | bool UnwindStackOfflineMap::Build() { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | bool UnwindStackOfflineMap::Build(const std::vector<backtrace_map_t>& backtrace_maps, |
| 157 | const backtrace_stackinfo_t& stack) { |
| 158 | if (stack.start >= stack.end) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | for (const backtrace_map_t& map : backtrace_maps) { |
| 163 | maps_.push_back(map); |
| 164 | } |
| 165 | |
| 166 | std::sort(maps_.begin(), maps_.end(), |
| 167 | [](const backtrace_map_t& a, const backtrace_map_t& b) { return a.start < b.start; }); |
| 168 | |
| 169 | unwindstack::Maps* maps = new unwindstack::Maps; |
| 170 | stack_maps_.reset(maps); |
| 171 | for (const backtrace_map_t& map : maps_) { |
| 172 | maps->Add(map.start, map.end, map.offset, map.flags, map.name, map.load_bias); |
| 173 | } |
| 174 | |
| 175 | // Create the process memory from the stack data. |
| 176 | uint64_t size = stack.end - stack.start; |
| 177 | unwindstack::MemoryBuffer* memory = new unwindstack::MemoryBuffer; |
| 178 | memory->Resize(size); |
| 179 | memcpy(memory->GetPtr(0), stack.data, size); |
| 180 | std::shared_ptr<unwindstack::Memory> shared_memory(memory); |
| 181 | |
| 182 | process_memory_.reset(new unwindstack::MemoryRange(shared_memory, 0, size, stack.start)); |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 187 | //------------------------------------------------------------------------- |
| 188 | // BacktraceMap create function. |
| 189 | //------------------------------------------------------------------------- |
Christopher Ferris | 086baf9 | 2017-10-17 14:12:52 -0700 | [diff] [blame] | 190 | BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) { |
Christopher Ferris | 6f3981c | 2017-07-27 09:29:18 -0700 | [diff] [blame] | 191 | BacktraceMap* map; |
| 192 | |
| 193 | if (uncached) { |
| 194 | // Force use of the base class to parse the maps when this call is made. |
| 195 | map = new BacktraceMap(pid); |
| 196 | } else if (pid == getpid()) { |
| 197 | map = new UnwindStackMap(0); |
| 198 | } else { |
| 199 | map = new UnwindStackMap(pid); |
| 200 | } |
| 201 | if (!map->Build()) { |
| 202 | delete map; |
| 203 | return nullptr; |
| 204 | } |
| 205 | return map; |
| 206 | } |
Christopher Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 207 | |
| 208 | //------------------------------------------------------------------------- |
| 209 | // BacktraceMap create offline function. |
| 210 | //------------------------------------------------------------------------- |
| 211 | BacktraceMap* BacktraceMap::CreateOffline(pid_t pid, const std::vector<backtrace_map_t>& maps, |
| 212 | const backtrace_stackinfo_t& stack) { |
| 213 | UnwindStackOfflineMap* map = new UnwindStackOfflineMap(pid); |
| 214 | if (!map->Build(maps, stack)) { |
| 215 | delete map; |
| 216 | return nullptr; |
| 217 | } |
| 218 | return map; |
| 219 | } |