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