| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -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 <android-base/unique_fd.h> | 
| Martin Stjernholm | b49289b | 2018-12-14 15:26:32 +0000 | [diff] [blame] | 26 | #include <art_api/dex_file_support.h> | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 27 |  | 
|  | 28 | #include <unwindstack/MapInfo.h> | 
|  | 29 | #include <unwindstack/Memory.h> | 
|  | 30 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 31 | #include "DexFile.h" | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 32 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 33 | namespace unwindstack { | 
|  | 34 |  | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 35 | std::unique_ptr<DexFile> DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory, | 
|  | 36 | MapInfo* info) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 37 | if (!info->name.empty()) { | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 38 | std::unique_ptr<DexFile> dex_file = | 
|  | 39 | DexFileFromFile::Create(dex_file_offset_in_memory - info->start + info->offset, info->name); | 
|  | 40 | if (dex_file) { | 
|  | 41 | return dex_file; | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 42 | } | 
|  | 43 | } | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 44 | return DexFileFromMemory::Create(dex_file_offset_in_memory, memory, info->name); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
|  | 47 | bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name, | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 48 | uint64_t* method_offset) { | 
| Martin Stjernholm | b49289b | 2018-12-14 15:26:32 +0000 | [diff] [blame] | 49 | art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset, false); | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 50 | if (method_info.offset == 0) { | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 51 | return false; | 
|  | 52 | } | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 53 | *method_name = method_info.name; | 
|  | 54 | *method_offset = dex_offset - method_info.offset; | 
|  | 55 | return true; | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 58 | std::unique_ptr<DexFileFromFile> DexFileFromFile::Create(uint64_t dex_file_offset_in_file, | 
|  | 59 | const std::string& file) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 60 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC))); | 
|  | 61 | if (fd == -1) { | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 62 | return nullptr; | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 65 | std::string error_msg; | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 66 | 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 Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 75 | std::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 Stjernholm | cacf5bf | 2018-12-19 00:09:41 +0000 | [diff] [blame] | 79 |  | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 80 | 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 Stjernholm | cacf5bf | 2018-12-19 00:09:41 +0000 | [diff] [blame] | 88 | } | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 89 |  | 
|  | 90 | if (!error_msg.empty()) { | 
|  | 91 | return nullptr; | 
| Martin Stjernholm | cacf5bf | 2018-12-19 00:09:41 +0000 | [diff] [blame] | 92 | } | 
| Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 93 |  | 
|  | 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 Stjernholm | cacf5bf | 2018-12-19 00:09:41 +0000 | [diff] [blame] | 99 | } | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 100 | } | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 101 |  | 
|  | 102 | }  // namespace unwindstack |