blob: c5f8138ed9b41d67989e5a915a4d53e870e1ff43 [file] [log] [blame]
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -08001/*
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 Ferris7747b602018-01-31 19:05:19 -080027#include <unwindstack/Maps.h>
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080028#include <unwindstack/Memory.h>
29
30#include "DexFile.h"
31
32namespace unwindstack {
33
Christopher Ferris7747b602018-01-31 19:05:19 -080034struct DEXFileEntry32 {
35 uint32_t next;
36 uint32_t prev;
37 uint32_t dex_file;
38};
39
40struct DEXFileEntry64 {
41 uint64_t next;
42 uint64_t prev;
43 uint64_t dex_file;
44};
45
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080046DexFiles::DexFiles(std::shared_ptr<Memory>& memory) : memory_(memory) {}
47
Christopher Ferris7747b602018-01-31 19:05:19 -080048DexFiles::DexFiles(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs)
49 : memory_(memory), search_libs_(search_libs) {}
50
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080051DexFiles::~DexFiles() {
52 for (auto& entry : files_) {
53 delete entry.second;
54 }
55}
56
Christopher Ferris7747b602018-01-31 19:05:19 -080057void DexFiles::SetArch(ArchEnum arch) {
58 switch (arch) {
59 case ARCH_ARM:
60 case ARCH_MIPS:
61 case ARCH_X86:
62 read_entry_ptr_func_ = &DexFiles::ReadEntryPtr32;
63 read_entry_func_ = &DexFiles::ReadEntry32;
64 break;
65
66 case ARCH_ARM64:
67 case ARCH_MIPS64:
68 case ARCH_X86_64:
69 read_entry_ptr_func_ = &DexFiles::ReadEntryPtr64;
70 read_entry_func_ = &DexFiles::ReadEntry64;
71 break;
72
73 case ARCH_UNKNOWN:
74 abort();
75 }
76}
77
78uint64_t DexFiles::ReadEntryPtr32(uint64_t addr) {
79 uint32_t entry;
80 if (!memory_->ReadFully(addr, &entry, sizeof(entry))) {
81 return 0;
82 }
83 return entry;
84}
85
86uint64_t DexFiles::ReadEntryPtr64(uint64_t addr) {
87 uint64_t entry;
88 if (!memory_->ReadFully(addr, &entry, sizeof(entry))) {
89 return 0;
90 }
91 return entry;
92}
93
94bool DexFiles::ReadEntry32() {
95 DEXFileEntry32 entry;
96 if (!memory_->ReadFully(entry_addr_, &entry, sizeof(entry)) || entry.dex_file == 0) {
97 entry_addr_ = 0;
98 return false;
99 }
100
101 addrs_.push_back(entry.dex_file);
102 entry_addr_ = entry.next;
103 return true;
104}
105
106bool DexFiles::ReadEntry64() {
107 DEXFileEntry64 entry;
108 if (!memory_->ReadFully(entry_addr_, &entry, sizeof(entry)) || entry.dex_file == 0) {
109 entry_addr_ = 0;
110 return false;
111 }
112
113 addrs_.push_back(entry.dex_file);
114 entry_addr_ = entry.next;
115 return true;
116}
117
118void DexFiles::Init(Maps* maps) {
119 if (initialized_) {
120 return;
121 }
122 initialized_ = true;
123 entry_addr_ = 0;
124
125 const std::string dex_debug_name("__art_debug_dexfiles");
126 for (MapInfo* info : *maps) {
127 if (!(info->flags & PROT_EXEC) || !(info->flags & PROT_READ) || info->offset != 0) {
128 continue;
129 }
130
131 if (!search_libs_.empty()) {
132 bool found = false;
133 const char* lib = basename(info->name.c_str());
134 for (const std::string& name : search_libs_) {
135 if (name == lib) {
136 found = true;
137 break;
138 }
139 }
140 if (!found) {
141 continue;
142 }
143 }
144
145 Elf* elf = info->GetElf(memory_, true);
146 uint64_t ptr;
147 // Find first non-empty list (libart might be loaded multiple times).
148 if (elf->GetGlobalVariable(dex_debug_name, &ptr) && ptr != 0) {
149 entry_addr_ = (this->*read_entry_ptr_func_)(ptr + info->start);
150 if (entry_addr_ != 0) {
151 break;
152 }
153 }
154 }
155}
156
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800157DexFile* DexFiles::GetDexFile(uint64_t dex_file_offset, MapInfo* info) {
158 // Lock while processing the data.
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800159 DexFile* dex_file;
160 auto entry = files_.find(dex_file_offset);
161 if (entry == files_.end()) {
162 dex_file = DexFile::Create(dex_file_offset, memory_.get(), info);
163 files_[dex_file_offset] = dex_file;
164 } else {
165 dex_file = entry->second;
166 }
167 return dex_file;
168}
169
Christopher Ferris7747b602018-01-31 19:05:19 -0800170bool DexFiles::GetAddr(size_t index, uint64_t* addr) {
171 if (index < addrs_.size()) {
172 *addr = addrs_[index];
173 return true;
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800174 }
Christopher Ferris7747b602018-01-31 19:05:19 -0800175 if (entry_addr_ != 0 && (this->*read_entry_func_)()) {
176 *addr = addrs_.back();
177 return true;
178 }
179 return false;
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800180}
181
Christopher Ferris7747b602018-01-31 19:05:19 -0800182void DexFiles::GetMethodInformation(Maps* maps, MapInfo* info, uint64_t dex_pc,
183 std::string* method_name, uint64_t* method_offset) {
184 std::lock_guard<std::mutex> guard(lock_);
185 if (!initialized_) {
186 Init(maps);
187 }
188
189 size_t index = 0;
190 uint64_t addr;
191 while (GetAddr(index++, &addr)) {
192 if (addr < info->start || addr >= info->end) {
193 continue;
194 }
195
196 DexFile* dex_file = GetDexFile(addr, info);
197 if (dex_file != nullptr &&
198 dex_file->GetMethodInformation(dex_pc - addr, method_name, method_offset)) {
199 break;
200 }
201 }
202}
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800203
204} // namespace unwindstack