blob: eaf867fd04637075c9678ce82d374ea0edefae65 [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
25#include <android-base/unique_fd.h>
Martin Stjernholmb49289b2018-12-14 15:26:32 +000026#include <art_api/dex_file_support.h>
Christopher Ferris0b06a592018-01-19 10:26:36 -080027
28#include <unwindstack/MapInfo.h>
29#include <unwindstack/Memory.h>
30
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080031#include "DexFile.h"
Christopher Ferris0b06a592018-01-19 10:26:36 -080032
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080033namespace unwindstack {
34
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000035std::unique_ptr<DexFile> DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory,
36 MapInfo* info) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080037 if (!info->name.empty()) {
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000038 std::unique_ptr<DexFile> dex_file =
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000039 DexFileFromFile::Create(dex_file_offset_in_memory - info->start + info->offset, info->name);
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000040 if (dex_file) {
41 return dex_file;
Christopher Ferris0b06a592018-01-19 10:26:36 -080042 }
43 }
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000044 return DexFileFromMemory::Create(dex_file_offset_in_memory, memory, info->name);
Christopher Ferris7747b602018-01-31 19:05:19 -080045}
46
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000047bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
48 uint64_t* method_offset) {
Martin Stjernholmb49289b2018-12-14 15:26:32 +000049 art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset, false);
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000050 if (method_info.offset == 0) {
Christopher Ferris7747b602018-01-31 19:05:19 -080051 return false;
52 }
David Srbeckyb9cc4fb2019-04-05 18:23:32 +000053 *method_name = method_info.name;
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000054 *method_offset = dex_offset - method_info.offset;
55 return true;
Christopher Ferris0b06a592018-01-19 10:26:36 -080056}
57
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000058std::unique_ptr<DexFileFromFile> DexFileFromFile::Create(uint64_t dex_file_offset_in_file,
59 const std::string& file) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080060 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
61 if (fd == -1) {
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000062 return nullptr;
Christopher Ferris0b06a592018-01-19 10:26:36 -080063 }
64
Christopher Ferris0b06a592018-01-19 10:26:36 -080065 std::string error_msg;
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000066 std::unique_ptr<art_api::dex::DexFile> art_dex_file =
67 OpenFromFd(fd, dex_file_offset_in_file, file, &error_msg);
68 if (art_dex_file == nullptr) {
69 return nullptr;
70 }
71
72 return std::unique_ptr<DexFileFromFile>(new DexFileFromFile(std::move(*art_dex_file.release())));
Christopher Ferris0b06a592018-01-19 10:26:36 -080073}
74
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000075std::unique_ptr<DexFileFromMemory> DexFileFromMemory::Create(uint64_t dex_file_offset_in_memory,
76 Memory* memory,
77 const std::string& name) {
78 std::vector<uint8_t> backing_memory;
Martin Stjernholmcacf5bf2018-12-19 00:09:41 +000079
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000080 for (size_t size = 0;;) {
81 std::string error_msg;
82 std::unique_ptr<art_api::dex::DexFile> art_dex_file =
83 OpenFromMemory(backing_memory.data(), &size, name, &error_msg);
84
85 if (art_dex_file != nullptr) {
86 return std::unique_ptr<DexFileFromMemory>(
87 new DexFileFromMemory(std::move(*art_dex_file.release()), std::move(backing_memory)));
Martin Stjernholmcacf5bf2018-12-19 00:09:41 +000088 }
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000089
90 if (!error_msg.empty()) {
91 return nullptr;
Martin Stjernholmcacf5bf2018-12-19 00:09:41 +000092 }
Martin Stjernholmbb4f2b42018-12-19 14:28:33 +000093
94 backing_memory.resize(size);
95 if (!memory->ReadFully(dex_file_offset_in_memory, backing_memory.data(),
96 backing_memory.size())) {
97 return nullptr;
98 }
Martin Stjernholmcacf5bf2018-12-19 00:09:41 +000099 }
Christopher Ferris0b06a592018-01-19 10:26:36 -0800100}
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800101
102} // namespace unwindstack