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 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 33 | struct JITCodeEntry32Pack { |
| 34 | uint32_t next; |
| 35 | uint32_t prev; |
| 36 | uint32_t symfile_addr; |
| 37 | uint64_t symfile_size; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 38 | } __attribute__((packed)); |
| 39 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 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; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 48 | struct JITCodeEntry64 { |
| 49 | uint64_t next; |
| 50 | uint64_t prev; |
| 51 | uint64_t symfile_addr; |
| 52 | uint64_t symfile_size; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 55 | struct JITDescriptorHeader { |
| 56 | uint32_t version; |
| 57 | uint32_t action_flag; |
| 58 | }; |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 59 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 60 | struct JITDescriptor32 { |
| 61 | JITDescriptorHeader header; |
| 62 | uint32_t relevant_entry; |
| 63 | uint32_t first_entry; |
| 64 | }; |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 65 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 66 | struct JITDescriptor64 { |
| 67 | JITDescriptorHeader header; |
| 68 | uint64_t relevant_entry; |
| 69 | uint64_t first_entry; |
| 70 | }; |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 71 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 72 | JitDebug::JitDebug(std::shared_ptr<Memory>& memory) : Global(memory) {} |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 73 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 74 | JitDebug::JitDebug(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs) |
| 75 | : Global(memory, 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; |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 87 | } |
| 88 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 89 | if (desc.header.version != 1 || desc.first_entry == 0) { |
| 90 | // Either unknown version, or no jit entries. |
| 91 | return 0; |
| 92 | } |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 93 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 94 | return desc.first_entry; |
| 95 | } |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 96 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 97 | uint64_t JitDebug::ReadDescriptor64(uint64_t addr) { |
| 98 | JITDescriptor64 desc; |
| 99 | if (!memory_->ReadFully(addr, &desc, sizeof(desc))) { |
| 100 | return 0; |
| 101 | } |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 102 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 103 | if (desc.header.version != 1 || desc.first_entry == 0) { |
| 104 | // Either unknown version, or no jit entries. |
| 105 | return 0; |
| 106 | } |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 107 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 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::ProcessArch() { |
| 145 | switch (arch()) { |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 146 | case ARCH_X86: |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 147 | read_descriptor_func_ = &JitDebug::ReadDescriptor32; |
| 148 | read_entry_func_ = &JitDebug::ReadEntry32Pack; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 149 | break; |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 150 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 151 | case ARCH_ARM: |
| 152 | case ARCH_MIPS: |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 153 | read_descriptor_func_ = &JitDebug::ReadDescriptor32; |
| 154 | read_entry_func_ = &JitDebug::ReadEntry32Pad; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 155 | break; |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 156 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 157 | case ARCH_ARM64: |
| 158 | case ARCH_X86_64: |
| 159 | case ARCH_MIPS64: |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 160 | read_descriptor_func_ = &JitDebug::ReadDescriptor64; |
| 161 | read_entry_func_ = &JitDebug::ReadEntry64; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 162 | break; |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 163 | case ARCH_UNKNOWN: |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 164 | abort(); |
| 165 | } |
| 166 | } |
| 167 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 168 | bool JitDebug::ReadVariableData(uint64_t ptr) { |
| 169 | entry_addr_ = (this->*read_descriptor_func_)(ptr); |
| 170 | return entry_addr_ != 0; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 171 | } |
| 172 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 173 | void JitDebug::Init(Maps* maps) { |
| 174 | if (initialized_) { |
| 175 | return; |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 176 | } |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 177 | // Regardless of what happens below, consider the init finished. |
| 178 | initialized_ = true; |
| 179 | |
| 180 | FindAndReadVariable(maps, "__jit_debug_descriptor"); |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 181 | } |
| 182 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 183 | Elf* JitDebug::GetElf(Maps* maps, uint64_t pc) { |
| 184 | // Use a single lock, this object should be used so infrequently that |
| 185 | // a fine grain lock is unnecessary. |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 186 | std::lock_guard<std::mutex> guard(lock_); |
| 187 | if (!initialized_) { |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 188 | Init(maps); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 189 | } |
| 190 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 191 | // Search the existing elf object first. |
| 192 | for (Elf* elf : elf_list_) { |
| 193 | if (elf->IsValidPc(pc)) { |
| 194 | return elf; |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 198 | while (entry_addr_ != 0) { |
| 199 | uint64_t start; |
| 200 | uint64_t size; |
| 201 | entry_addr_ = (this->*read_entry_func_)(&start, &size); |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 202 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 203 | Elf* elf = new Elf(new MemoryRange(memory_, start, size, 0)); |
| 204 | elf->Init(); |
| 205 | if (!elf->valid()) { |
| 206 | // The data is not formatted in a way we understand, do not attempt |
| 207 | // to process any other entries. |
| 208 | entry_addr_ = 0; |
| 209 | delete elf; |
| 210 | return nullptr; |
| 211 | } |
| 212 | elf_list_.push_back(elf); |
| 213 | |
| 214 | if (elf->IsValidPc(pc)) { |
| 215 | return elf; |
| 216 | } |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 217 | } |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame^] | 218 | return nullptr; |
David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 221 | } // namespace unwindstack |