| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [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 <sys/mman.h> | 
|  | 19 |  | 
|  | 20 | #include <memory> | 
|  | 21 | #include <vector> | 
|  | 22 |  | 
|  | 23 | #include <unwindstack/Elf.h> | 
|  | 24 | #include <unwindstack/JitDebug.h> | 
|  | 25 | #include <unwindstack/Maps.h> | 
|  | 26 | #include <unwindstack/Memory.h> | 
|  | 27 |  | 
|  | 28 | // This implements the JIT Compilation Interface. | 
|  | 29 | // See https://sourceware.org/gdb/onlinedocs/gdb/JIT-Interface.html | 
|  | 30 |  | 
|  | 31 | namespace unwindstack { | 
|  | 32 |  | 
|  | 33 | struct JITCodeEntry32Pack { | 
|  | 34 | uint32_t next; | 
|  | 35 | uint32_t prev; | 
|  | 36 | uint32_t symfile_addr; | 
|  | 37 | uint64_t symfile_size; | 
|  | 38 | } __attribute__((packed)); | 
|  | 39 |  | 
|  | 40 | struct JITCodeEntry32Pad { | 
|  | 41 | uint32_t next; | 
|  | 42 | uint32_t prev; | 
|  | 43 | uint32_t symfile_addr; | 
|  | 44 | uint32_t pad; | 
|  | 45 | uint64_t symfile_size; | 
|  | 46 | }; | 
|  | 47 |  | 
|  | 48 | struct JITCodeEntry64 { | 
|  | 49 | uint64_t next; | 
|  | 50 | uint64_t prev; | 
|  | 51 | uint64_t symfile_addr; | 
|  | 52 | uint64_t symfile_size; | 
|  | 53 | }; | 
|  | 54 |  | 
|  | 55 | struct JITDescriptorHeader { | 
|  | 56 | uint32_t version; | 
|  | 57 | uint32_t action_flag; | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | struct JITDescriptor32 { | 
|  | 61 | JITDescriptorHeader header; | 
|  | 62 | uint32_t relevant_entry; | 
|  | 63 | uint32_t first_entry; | 
|  | 64 | }; | 
|  | 65 |  | 
|  | 66 | struct JITDescriptor64 { | 
|  | 67 | JITDescriptorHeader header; | 
|  | 68 | uint64_t relevant_entry; | 
|  | 69 | uint64_t first_entry; | 
|  | 70 | }; | 
|  | 71 |  | 
|  | 72 | JitDebug::JitDebug(std::shared_ptr<Memory>& memory) : memory_(memory) {} | 
|  | 73 |  | 
|  | 74 | JitDebug::JitDebug(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs) | 
|  | 75 | : memory_(memory), search_libs_(search_libs) {} | 
|  | 76 |  | 
|  | 77 | JitDebug::~JitDebug() { | 
|  | 78 | for (auto* elf : elf_list_) { | 
|  | 79 | delete elf; | 
|  | 80 | } | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | uint64_t JitDebug::ReadDescriptor32(uint64_t addr) { | 
|  | 84 | JITDescriptor32 desc; | 
|  | 85 | if (!memory_->ReadFully(addr, &desc, sizeof(desc))) { | 
|  | 86 | return 0; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | if (desc.header.version != 1 || desc.first_entry == 0) { | 
|  | 90 | // Either unknown version, or no jit entries. | 
|  | 91 | return 0; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | return desc.first_entry; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | uint64_t JitDebug::ReadDescriptor64(uint64_t addr) { | 
|  | 98 | JITDescriptor64 desc; | 
|  | 99 | if (!memory_->ReadFully(addr, &desc, sizeof(desc))) { | 
|  | 100 | return 0; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | if (desc.header.version != 1 || desc.first_entry == 0) { | 
|  | 104 | // Either unknown version, or no jit entries. | 
|  | 105 | return 0; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | return desc.first_entry; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | uint64_t JitDebug::ReadEntry32Pack(uint64_t* start, uint64_t* size) { | 
|  | 112 | JITCodeEntry32Pack code; | 
|  | 113 | if (!memory_->ReadFully(entry_addr_, &code, sizeof(code))) { | 
|  | 114 | return 0; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | *start = code.symfile_addr; | 
|  | 118 | *size = code.symfile_size; | 
|  | 119 | return code.next; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | uint64_t JitDebug::ReadEntry32Pad(uint64_t* start, uint64_t* size) { | 
|  | 123 | JITCodeEntry32Pad code; | 
|  | 124 | if (!memory_->ReadFully(entry_addr_, &code, sizeof(code))) { | 
|  | 125 | return 0; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | *start = code.symfile_addr; | 
|  | 129 | *size = code.symfile_size; | 
|  | 130 | return code.next; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | uint64_t JitDebug::ReadEntry64(uint64_t* start, uint64_t* size) { | 
|  | 134 | JITCodeEntry64 code; | 
|  | 135 | if (!memory_->ReadFully(entry_addr_, &code, sizeof(code))) { | 
|  | 136 | return 0; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | *start = code.symfile_addr; | 
|  | 140 | *size = code.symfile_size; | 
|  | 141 | return code.next; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | void JitDebug::SetArch(ArchEnum arch) { | 
|  | 145 | switch (arch) { | 
|  | 146 | case ARCH_X86: | 
|  | 147 | read_descriptor_func_ = &JitDebug::ReadDescriptor32; | 
|  | 148 | read_entry_func_ = &JitDebug::ReadEntry32Pack; | 
|  | 149 | break; | 
|  | 150 |  | 
|  | 151 | case ARCH_ARM: | 
|  | 152 | case ARCH_MIPS: | 
|  | 153 | read_descriptor_func_ = &JitDebug::ReadDescriptor32; | 
|  | 154 | read_entry_func_ = &JitDebug::ReadEntry32Pad; | 
|  | 155 | break; | 
|  | 156 |  | 
|  | 157 | case ARCH_ARM64: | 
|  | 158 | case ARCH_X86_64: | 
|  | 159 | case ARCH_MIPS64: | 
|  | 160 | read_descriptor_func_ = &JitDebug::ReadDescriptor64; | 
|  | 161 | read_entry_func_ = &JitDebug::ReadEntry64; | 
|  | 162 | break; | 
|  | 163 | case ARCH_UNKNOWN: | 
|  | 164 | abort(); | 
|  | 165 | } | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | void JitDebug::Init(Maps* maps) { | 
|  | 169 | if (initialized_) { | 
|  | 170 | return; | 
|  | 171 | } | 
|  | 172 | // Regardless of what happens below, consider the init finished. | 
|  | 173 | initialized_ = true; | 
|  | 174 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 175 | const std::string descriptor_name("__jit_debug_descriptor"); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 176 | for (MapInfo* info : *maps) { | 
|  | 177 | if (!(info->flags & PROT_EXEC) || !(info->flags & PROT_READ) || info->offset != 0) { | 
|  | 178 | continue; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | if (!search_libs_.empty()) { | 
|  | 182 | bool found = false; | 
|  | 183 | const char* lib = basename(info->name.c_str()); | 
|  | 184 | for (std::string& name : search_libs_) { | 
|  | 185 | if (strcmp(name.c_str(), lib) == 0) { | 
|  | 186 | found = true; | 
|  | 187 | break; | 
|  | 188 | } | 
|  | 189 | } | 
|  | 190 | if (!found) { | 
|  | 191 | continue; | 
|  | 192 | } | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | Elf* elf = info->GetElf(memory_, true); | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 196 | uint64_t descriptor_addr; | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 197 | if (elf->GetGlobalVariable(descriptor_name, &descriptor_addr)) { | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 198 | // Search for the first non-zero entry. | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 199 | descriptor_addr += info->start; | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 200 | entry_addr_ = (this->*read_descriptor_func_)(descriptor_addr); | 
|  | 201 | if (entry_addr_ != 0) { | 
|  | 202 | break; | 
|  | 203 | } | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 204 | } | 
|  | 205 | } | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
|  | 208 | Elf* JitDebug::GetElf(Maps* maps, uint64_t pc) { | 
|  | 209 | // Use a single lock, this object should be used so infrequently that | 
|  | 210 | // a fine grain lock is unnecessary. | 
|  | 211 | std::lock_guard<std::mutex> guard(lock_); | 
|  | 212 | if (!initialized_) { | 
|  | 213 | Init(maps); | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | // Search the existing elf object first. | 
|  | 217 | for (Elf* elf : elf_list_) { | 
|  | 218 | if (elf->IsValidPc(pc)) { | 
|  | 219 | return elf; | 
|  | 220 | } | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | while (entry_addr_ != 0) { | 
|  | 224 | uint64_t start; | 
|  | 225 | uint64_t size; | 
|  | 226 | entry_addr_ = (this->*read_entry_func_)(&start, &size); | 
|  | 227 |  | 
|  | 228 | Elf* elf = new Elf(new MemoryRange(memory_, start, size, 0)); | 
|  | 229 | elf->Init(true); | 
|  | 230 | if (!elf->valid()) { | 
|  | 231 | // The data is not formatted in a way we understand, do not attempt | 
|  | 232 | // to process any other entries. | 
|  | 233 | entry_addr_ = 0; | 
|  | 234 | delete elf; | 
|  | 235 | return nullptr; | 
|  | 236 | } | 
|  | 237 | elf_list_.push_back(elf); | 
|  | 238 |  | 
|  | 239 | if (elf->IsValidPc(pc)) { | 
|  | 240 | return elf; | 
|  | 241 | } | 
|  | 242 | } | 
|  | 243 | return nullptr; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | }  // namespace unwindstack |