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 "read_dex_file.h"
18
19 #include <fcntl.h>
20
21 #include <algorithm>
22 #include <iterator>
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 #include <android-base/logging.h>
28 #include <android-base/mapped_file.h>
29 #include <android-base/unique_fd.h>
30 #include <art_api/dex_file_support.h>
31
32 #include "utils.h"
33
34 namespace simpleperf {
35
ReadSymbols(art_api::dex::DexFile & dex_file,uint64_t file_offset,const std::function<void (DexFileSymbol *)> & symbol_cb)36 static void ReadSymbols(art_api::dex::DexFile& dex_file, uint64_t file_offset,
37 const std::function<void(DexFileSymbol*)>& symbol_cb) {
38 auto callback = [&](const art_api::dex::DexFile::Method& method) {
39 size_t name_size, code_size;
40 const char* name = method.GetQualifiedName(/*with_params=*/false, &name_size);
41 size_t offset = method.GetCodeOffset(&code_size);
42 DexFileSymbol symbol{std::string_view(name, name_size), file_offset + offset, code_size};
43 symbol_cb(&symbol);
44 };
45 dex_file.ForEachMethod(callback);
46 }
47
ReadSymbolsFromDexFileInMemory(void * addr,uint64_t size,const std::string & debug_filename,const std::vector<uint64_t> & dex_file_offsets,const std::function<void (DexFileSymbol *)> & symbol_callback)48 bool ReadSymbolsFromDexFileInMemory(void* addr, uint64_t size, const std::string& debug_filename,
49 const std::vector<uint64_t>& dex_file_offsets,
50 const std::function<void(DexFileSymbol*)>& symbol_callback) {
51 for (uint64_t file_offset : dex_file_offsets) {
52 size_t max_file_size;
53 if (__builtin_sub_overflow(size, file_offset, &max_file_size)) {
54 LOG(WARNING) << "failed to read dex file symbols from " << debug_filename << "(offset "
55 << file_offset << ")";
56 return false;
57 }
58 uint8_t* file_addr = static_cast<uint8_t*>(addr) + file_offset;
59 std::unique_ptr<art_api::dex::DexFile> dex_file;
60 art_api::dex::DexFile::Error error_msg =
61 art_api::dex::DexFile::Create(file_addr, max_file_size, nullptr, "", &dex_file);
62 if (dex_file == nullptr) {
63 LOG(WARNING) << "failed to read dex file symbols from " << debug_filename << "(offset "
64 << file_offset << "): " << error_msg.ToString();
65 return false;
66 }
67 ReadSymbols(*dex_file, file_offset, symbol_callback);
68 }
69 return true;
70 }
71
ReadSymbolsFromDexFile(const std::string & file_path,const std::vector<uint64_t> & dex_file_offsets,const std::function<void (DexFileSymbol *)> & symbol_callback)72 bool ReadSymbolsFromDexFile(const std::string& file_path,
73 const std::vector<uint64_t>& dex_file_offsets,
74 const std::function<void(DexFileSymbol*)>& symbol_callback) {
75 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file_path.c_str(), O_RDONLY | O_CLOEXEC)));
76 if (fd == -1) {
77 return false;
78 }
79 size_t file_size = GetFileSize(file_path);
80 if (file_size == 0) {
81 return false;
82 }
83 std::unique_ptr<android::base::MappedFile> map;
84 map = android::base::MappedFile::FromFd(fd, 0, file_size, PROT_READ);
85 if (map == nullptr) {
86 return false;
87 }
88 return ReadSymbolsFromDexFileInMemory(map->data(), file_size, file_path, dex_file_offsets,
89 symbol_callback);
90 }
91
92 } // namespace simpleperf
93