blob: dff7a8b3c119fd30dbae18c3496c64187c4fb174 [file] [log] [blame]
Christopher Ferris0b06a592018-01-19 10:26:36 -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
Martin Stjernholm9062bb42019-10-04 01:28:43 +010025#define LOG_TAG "unwind"
26#include <log/log.h>
27
Christopher Ferris0b06a592018-01-19 10:26:36 -080028#include <android-base/unique_fd.h>
Martin Stjernholmb49289b2018-12-14 15:26:32 +000029#include <art_api/dex_file_support.h>
Christopher Ferris0b06a592018-01-19 10:26:36 -080030
31#include <unwindstack/MapInfo.h>
32#include <unwindstack/Memory.h>
33
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080034#include "DexFile.h"
Christopher Ferris0b06a592018-01-19 10:26:36 -080035
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080036namespace unwindstack {
37
Martin Stjernholm9062bb42019-10-04 01:28:43 +010038static bool CheckDexSupport() {
39 if (std::string err_msg; !art_api::dex::TryLoadLibdexfileExternal(&err_msg)) {
40 ALOGW("Failed to initialize DEX file support: %s", err_msg.c_str());
41 return false;
42 }
43 return true;
44}
45
46static bool HasDexSupport() {
47 static bool has_dex_support = CheckDexSupport();
48 return has_dex_support;
49}
50
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000051std::unique_ptr<DexFile> DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory,
52 MapInfo* info) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080053 if (!info->name.empty()) {
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000054 std::unique_ptr<DexFile> dex_file =
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000055 DexFileFromFile::Create(dex_file_offset_in_memory - info->start + info->offset, info->name);
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000056 if (dex_file) {
57 return dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -080058 }
59 }
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000060 return DexFileFromMemory::Create(dex_file_offset_in_memory, memory, info->name);
Christopher Ferris7747b602018-01-31 19:05:19 -080061}
62
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000063bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
64 uint64_t* method_offset) {
Martin Stjernholmb49289b2018-12-14 15:26:32 +000065 art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset, false);
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000066 if (method_info.offset == 0) {
Christopher Ferris7747b602018-01-31 19:05:19 -080067 return false;
68 }
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000069 *method_name = method_info.name;
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000070 *method_offset = dex_offset - method_info.offset;
71 return true;
Christopher Ferris0b06a592018-01-19 10:26:36 -080072}
73
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000074std::unique_ptr<DexFileFromFile> DexFileFromFile::Create(uint64_t dex_file_offset_in_file,
75 const std::string& file) {
Martin Stjernholm9062bb42019-10-04 01:28:43 +010076 if (UNLIKELY(!HasDexSupport())) {
77 return nullptr;
78 }
79
Christopher Ferris0b06a592018-01-19 10:26:36 -080080 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
81 if (fd == -1) {
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000082 return nullptr;
Christopher Ferris0b06a592018-01-19 10:26:36 -080083 }
84
Christopher Ferris0b06a592018-01-19 10:26:36 -080085 std::string error_msg;
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000086 std::unique_ptr<art_api::dex::DexFile> art_dex_file =
87 OpenFromFd(fd, dex_file_offset_in_file, file, &error_msg);
88 if (art_dex_file == nullptr) {
89 return nullptr;
90 }
91
92 return std::unique_ptr<DexFileFromFile>(new DexFileFromFile(std::move(*art_dex_file.release())));
Christopher Ferris0b06a592018-01-19 10:26:36 -080093}
94
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000095std::unique_ptr<DexFileFromMemory> DexFileFromMemory::Create(uint64_t dex_file_offset_in_memory,
96 Memory* memory,
97 const std::string& name) {
Martin Stjernholm9062bb42019-10-04 01:28:43 +010098 if (UNLIKELY(!HasDexSupport())) {
99 return nullptr;
100 }
101
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +0000102 std::vector<uint8_t> backing_memory;
Martin Stjernholmcacf5bf2018-12-19 00:09:41 +0000103
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +0000104 for (size_t size = 0;;) {
105 std::string error_msg;
106 std::unique_ptr<art_api::dex::DexFile> art_dex_file =
107 OpenFromMemory(backing_memory.data(), &size, name, &error_msg);
108
109 if (art_dex_file != nullptr) {
110 return std::unique_ptr<DexFileFromMemory>(
111 new DexFileFromMemory(std::move(*art_dex_file.release()), std::move(backing_memory)));
Martin Stjernholmcacf5bf2018-12-19 00:09:41 +0000112 }
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +0000113
114 if (!error_msg.empty()) {
115 return nullptr;
Martin Stjernholmcacf5bf2018-12-19 00:09:41 +0000116 }
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +0000117
118 backing_memory.resize(size);
119 if (!memory->ReadFully(dex_file_offset_in_memory, backing_memory.data(),
120 backing_memory.size())) {
121 return nullptr;
122 }
Martin Stjernholmcacf5bf2018-12-19 00:09:41 +0000123 }
Christopher Ferris0b06a592018-01-19 10:26:36 -0800124}
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800125
126} // namespace unwindstack