blob: 2057fad1911632f30a7c85ac797c68e8873034ec [file] [log] [blame]
David Srbeckyb9cc4fb2019-04-05 18:23:32 +00001/*
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>
27#include <unwindstack/Maps.h>
28#include <unwindstack/Memory.h>
29
Christopher Ferris456df692019-11-20 14:46:10 -080030#if defined(DEXFILE_SUPPORT)
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000031#include "DexFile.h"
Christopher Ferris456df692019-11-20 14:46:10 -080032#endif
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000033
34namespace unwindstack {
35
Christopher Ferris456df692019-11-20 14:46:10 -080036#if !defined(DEXFILE_SUPPORT)
37// Empty class definition.
38class DexFile {
39 public:
40 DexFile() = default;
41 virtual ~DexFile() = default;
42};
43#endif
44
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000045struct DEXFileEntry32 {
46 uint32_t next;
47 uint32_t prev;
48 uint32_t dex_file;
49};
50
51struct DEXFileEntry64 {
52 uint64_t next;
53 uint64_t prev;
54 uint64_t dex_file;
55};
56
57DexFiles::DexFiles(std::shared_ptr<Memory>& memory) : Global(memory) {}
58
59DexFiles::DexFiles(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs)
60 : Global(memory, search_libs) {}
61
62DexFiles::~DexFiles() {}
63
64void DexFiles::ProcessArch() {
65 switch (arch()) {
66 case ARCH_ARM:
67 case ARCH_MIPS:
68 case ARCH_X86:
69 read_entry_ptr_func_ = &DexFiles::ReadEntryPtr32;
70 read_entry_func_ = &DexFiles::ReadEntry32;
71 break;
72
73 case ARCH_ARM64:
74 case ARCH_MIPS64:
75 case ARCH_X86_64:
76 read_entry_ptr_func_ = &DexFiles::ReadEntryPtr64;
77 read_entry_func_ = &DexFiles::ReadEntry64;
78 break;
79
80 case ARCH_UNKNOWN:
81 abort();
82 }
83}
84
85uint64_t DexFiles::ReadEntryPtr32(uint64_t addr) {
86 uint32_t entry;
87 const uint32_t field_offset = 12; // offset of first_entry_ in the descriptor struct.
88 if (!memory_->ReadFully(addr + field_offset, &entry, sizeof(entry))) {
89 return 0;
90 }
91 return entry;
92}
93
94uint64_t DexFiles::ReadEntryPtr64(uint64_t addr) {
95 uint64_t entry;
96 const uint32_t field_offset = 16; // offset of first_entry_ in the descriptor struct.
97 if (!memory_->ReadFully(addr + field_offset, &entry, sizeof(entry))) {
98 return 0;
99 }
100 return entry;
101}
102
103bool DexFiles::ReadEntry32() {
104 DEXFileEntry32 entry;
105 if (!memory_->ReadFully(entry_addr_, &entry, sizeof(entry)) || entry.dex_file == 0) {
106 entry_addr_ = 0;
107 return false;
108 }
109
110 addrs_.push_back(entry.dex_file);
111 entry_addr_ = entry.next;
112 return true;
113}
114
115bool DexFiles::ReadEntry64() {
116 DEXFileEntry64 entry;
117 if (!memory_->ReadFully(entry_addr_, &entry, sizeof(entry)) || entry.dex_file == 0) {
118 entry_addr_ = 0;
119 return false;
120 }
121
122 addrs_.push_back(entry.dex_file);
123 entry_addr_ = entry.next;
124 return true;
125}
126
127bool DexFiles::ReadVariableData(uint64_t ptr_offset) {
128 entry_addr_ = (this->*read_entry_ptr_func_)(ptr_offset);
129 return entry_addr_ != 0;
130}
131
132void DexFiles::Init(Maps* maps) {
133 if (initialized_) {
134 return;
135 }
136 initialized_ = true;
137 entry_addr_ = 0;
138
139 FindAndReadVariable(maps, "__dex_debug_descriptor");
140}
141
Christopher Ferris456df692019-11-20 14:46:10 -0800142#if defined(DEXFILE_SUPPORT)
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000143DexFile* DexFiles::GetDexFile(uint64_t dex_file_offset, MapInfo* info) {
144 // Lock while processing the data.
145 DexFile* dex_file;
146 auto entry = files_.find(dex_file_offset);
147 if (entry == files_.end()) {
148 std::unique_ptr<DexFile> new_dex_file = DexFile::Create(dex_file_offset, memory_.get(), info);
149 dex_file = new_dex_file.get();
150 files_[dex_file_offset] = std::move(new_dex_file);
151 } else {
152 dex_file = entry->second.get();
153 }
154 return dex_file;
155}
Christopher Ferris456df692019-11-20 14:46:10 -0800156#else
157DexFile* DexFiles::GetDexFile(uint64_t, MapInfo*) {
158 return nullptr;
159}
160#endif
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000161
162bool DexFiles::GetAddr(size_t index, uint64_t* addr) {
163 if (index < addrs_.size()) {
164 *addr = addrs_[index];
165 return true;
166 }
167 if (entry_addr_ != 0 && (this->*read_entry_func_)()) {
168 *addr = addrs_.back();
169 return true;
170 }
171 return false;
172}
173
Christopher Ferris456df692019-11-20 14:46:10 -0800174#if defined(DEXFILE_SUPPORT)
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000175void DexFiles::GetMethodInformation(Maps* maps, MapInfo* info, uint64_t dex_pc,
176 std::string* method_name, uint64_t* method_offset) {
177 std::lock_guard<std::mutex> guard(lock_);
178 if (!initialized_) {
179 Init(maps);
180 }
181
182 size_t index = 0;
183 uint64_t addr;
184 while (GetAddr(index++, &addr)) {
185 if (addr < info->start || addr >= info->end) {
186 continue;
187 }
188
189 DexFile* dex_file = GetDexFile(addr, info);
190 if (dex_file != nullptr &&
191 dex_file->GetMethodInformation(dex_pc - addr, method_name, method_offset)) {
192 break;
193 }
194 }
195}
Christopher Ferris456df692019-11-20 14:46:10 -0800196#else
197void DexFiles::GetMethodInformation(Maps*, MapInfo*, uint64_t, std::string*, uint64_t*) {}
198#endif
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000199
200} // namespace unwindstack