| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2018 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 | #include <sys/stat.h> | 
|  | 20 | #include <sys/types.h> | 
|  | 21 | #include <unistd.h> | 
|  | 22 |  | 
|  | 23 | #include <memory> | 
|  | 24 |  | 
|  | 25 | #include <unwindstack/DexFiles.h> | 
|  | 26 | #include <unwindstack/MapInfo.h> | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 27 | #include <unwindstack/Maps.h> | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 28 | #include <unwindstack/Memory.h> | 
|  | 29 |  | 
|  | 30 | #include "DexFile.h" | 
|  | 31 |  | 
|  | 32 | namespace unwindstack { | 
|  | 33 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 34 | struct DEXFileEntry32 { | 
|  | 35 | uint32_t next; | 
|  | 36 | uint32_t prev; | 
|  | 37 | uint32_t dex_file; | 
|  | 38 | }; | 
|  | 39 |  | 
|  | 40 | struct DEXFileEntry64 { | 
|  | 41 | uint64_t next; | 
|  | 42 | uint64_t prev; | 
|  | 43 | uint64_t dex_file; | 
|  | 44 | }; | 
|  | 45 |  | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 46 | DexFiles::DexFiles(std::shared_ptr<Memory>& memory) : Global(memory) {} | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 47 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 48 | DexFiles::DexFiles(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs) | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 49 | : Global(memory, search_libs) {} | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 50 |  | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 51 | DexFiles::~DexFiles() {} | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 52 |  | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 53 | void DexFiles::ProcessArch() { | 
|  | 54 | switch (arch()) { | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 55 | case ARCH_ARM: | 
|  | 56 | case ARCH_MIPS: | 
|  | 57 | case ARCH_X86: | 
|  | 58 | read_entry_ptr_func_ = &DexFiles::ReadEntryPtr32; | 
|  | 59 | read_entry_func_ = &DexFiles::ReadEntry32; | 
|  | 60 | break; | 
|  | 61 |  | 
|  | 62 | case ARCH_ARM64: | 
|  | 63 | case ARCH_MIPS64: | 
|  | 64 | case ARCH_X86_64: | 
|  | 65 | read_entry_ptr_func_ = &DexFiles::ReadEntryPtr64; | 
|  | 66 | read_entry_func_ = &DexFiles::ReadEntry64; | 
|  | 67 | break; | 
|  | 68 |  | 
|  | 69 | case ARCH_UNKNOWN: | 
|  | 70 | abort(); | 
|  | 71 | } | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | uint64_t DexFiles::ReadEntryPtr32(uint64_t addr) { | 
|  | 75 | uint32_t entry; | 
| David Srbecky | 4015ef4 | 2018-02-15 17:57:16 +0000 | [diff] [blame] | 76 | const uint32_t field_offset = 12;  // offset of first_entry_ in the descriptor struct. | 
|  | 77 | if (!memory_->ReadFully(addr + field_offset, &entry, sizeof(entry))) { | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 78 | return 0; | 
|  | 79 | } | 
|  | 80 | return entry; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | uint64_t DexFiles::ReadEntryPtr64(uint64_t addr) { | 
|  | 84 | uint64_t entry; | 
| David Srbecky | 4015ef4 | 2018-02-15 17:57:16 +0000 | [diff] [blame] | 85 | const uint32_t field_offset = 16;  // offset of first_entry_ in the descriptor struct. | 
|  | 86 | if (!memory_->ReadFully(addr + field_offset, &entry, sizeof(entry))) { | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 87 | return 0; | 
|  | 88 | } | 
|  | 89 | return entry; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | bool DexFiles::ReadEntry32() { | 
|  | 93 | DEXFileEntry32 entry; | 
|  | 94 | if (!memory_->ReadFully(entry_addr_, &entry, sizeof(entry)) || entry.dex_file == 0) { | 
|  | 95 | entry_addr_ = 0; | 
|  | 96 | return false; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | addrs_.push_back(entry.dex_file); | 
|  | 100 | entry_addr_ = entry.next; | 
|  | 101 | return true; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | bool DexFiles::ReadEntry64() { | 
|  | 105 | DEXFileEntry64 entry; | 
|  | 106 | if (!memory_->ReadFully(entry_addr_, &entry, sizeof(entry)) || entry.dex_file == 0) { | 
|  | 107 | entry_addr_ = 0; | 
|  | 108 | return false; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | addrs_.push_back(entry.dex_file); | 
|  | 112 | entry_addr_ = entry.next; | 
|  | 113 | return true; | 
|  | 114 | } | 
|  | 115 |  | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 116 | bool DexFiles::ReadVariableData(uint64_t ptr_offset) { | 
|  | 117 | entry_addr_ = (this->*read_entry_ptr_func_)(ptr_offset); | 
|  | 118 | return entry_addr_ != 0; | 
|  | 119 | } | 
|  | 120 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 121 | void DexFiles::Init(Maps* maps) { | 
|  | 122 | if (initialized_) { | 
|  | 123 | return; | 
|  | 124 | } | 
|  | 125 | initialized_ = true; | 
|  | 126 | entry_addr_ = 0; | 
|  | 127 |  | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 128 | FindAndReadVariable(maps, "__dex_debug_descriptor"); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 131 | DexFile* DexFiles::GetDexFile(uint64_t dex_file_offset, MapInfo* info) { | 
|  | 132 | // Lock while processing the data. | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 133 | DexFile* dex_file; | 
|  | 134 | auto entry = files_.find(dex_file_offset); | 
|  | 135 | if (entry == files_.end()) { | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 136 | std::unique_ptr<DexFile> new_dex_file = DexFile::Create(dex_file_offset, memory_.get(), info); | 
|  | 137 | dex_file = new_dex_file.get(); | 
|  | 138 | files_[dex_file_offset] = std::move(new_dex_file); | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 139 | } else { | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 140 | dex_file = entry->second.get(); | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 141 | } | 
|  | 142 | return dex_file; | 
|  | 143 | } | 
|  | 144 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 145 | bool DexFiles::GetAddr(size_t index, uint64_t* addr) { | 
|  | 146 | if (index < addrs_.size()) { | 
|  | 147 | *addr = addrs_[index]; | 
|  | 148 | return true; | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 149 | } | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 150 | if (entry_addr_ != 0 && (this->*read_entry_func_)()) { | 
|  | 151 | *addr = addrs_.back(); | 
|  | 152 | return true; | 
|  | 153 | } | 
|  | 154 | return false; | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 157 | void DexFiles::GetMethodInformation(Maps* maps, MapInfo* info, uint64_t dex_pc, | 
|  | 158 | std::string* method_name, uint64_t* method_offset) { | 
|  | 159 | std::lock_guard<std::mutex> guard(lock_); | 
|  | 160 | if (!initialized_) { | 
|  | 161 | Init(maps); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | size_t index = 0; | 
|  | 165 | uint64_t addr; | 
|  | 166 | while (GetAddr(index++, &addr)) { | 
|  | 167 | if (addr < info->start || addr >= info->end) { | 
|  | 168 | continue; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | DexFile* dex_file = GetDexFile(addr, info); | 
|  | 172 | if (dex_file != nullptr && | 
|  | 173 | dex_file->GetMethodInformation(dex_pc - addr, method_name, method_offset)) { | 
|  | 174 | break; | 
|  | 175 | } | 
|  | 176 | } | 
|  | 177 | } | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 178 |  | 
|  | 179 | }  // namespace unwindstack |