xref: /aosp_15_r20/system/extras/simpleperf/read_dex_file.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include "read_dex_file.h"
18*288bf522SAndroid Build Coastguard Worker 
19*288bf522SAndroid Build Coastguard Worker #include <fcntl.h>
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker #include <algorithm>
22*288bf522SAndroid Build Coastguard Worker #include <functional>
23*288bf522SAndroid Build Coastguard Worker #include <iterator>
24*288bf522SAndroid Build Coastguard Worker #include <string>
25*288bf522SAndroid Build Coastguard Worker #include <utility>
26*288bf522SAndroid Build Coastguard Worker #include <vector>
27*288bf522SAndroid Build Coastguard Worker 
28*288bf522SAndroid Build Coastguard Worker #include <android-base/logging.h>
29*288bf522SAndroid Build Coastguard Worker #include <android-base/mapped_file.h>
30*288bf522SAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
31*288bf522SAndroid Build Coastguard Worker #include <art_api/dex_file_support.h>
32*288bf522SAndroid Build Coastguard Worker 
33*288bf522SAndroid Build Coastguard Worker #include "utils.h"
34*288bf522SAndroid Build Coastguard Worker 
35*288bf522SAndroid Build Coastguard Worker namespace simpleperf {
36*288bf522SAndroid Build Coastguard Worker 
ReadSymbols(art_api::dex::DexFile & dex_file,uint64_t file_offset,const std::function<void (DexFileSymbol *)> & symbol_cb)37*288bf522SAndroid Build Coastguard Worker static void ReadSymbols(art_api::dex::DexFile& dex_file, uint64_t file_offset,
38*288bf522SAndroid Build Coastguard Worker                         const std::function<void(DexFileSymbol*)>& symbol_cb) {
39*288bf522SAndroid Build Coastguard Worker   auto callback = [&](const art_api::dex::DexFile::Method& method) {
40*288bf522SAndroid Build Coastguard Worker     size_t name_size, code_size;
41*288bf522SAndroid Build Coastguard Worker     const char* name = method.GetQualifiedName(/*with_params=*/false, &name_size);
42*288bf522SAndroid Build Coastguard Worker     size_t offset = method.GetCodeOffset(&code_size);
43*288bf522SAndroid Build Coastguard Worker     DexFileSymbol symbol{std::string_view(name, name_size), file_offset + offset, code_size};
44*288bf522SAndroid Build Coastguard Worker     symbol_cb(&symbol);
45*288bf522SAndroid Build Coastguard Worker   };
46*288bf522SAndroid Build Coastguard Worker   dex_file.ForEachMethod(callback);
47*288bf522SAndroid Build Coastguard Worker }
48*288bf522SAndroid Build Coastguard Worker 
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*288bf522SAndroid Build Coastguard Worker bool ReadSymbolsFromDexFileInMemory(void* addr, uint64_t size, const std::string& debug_filename,
50*288bf522SAndroid Build Coastguard Worker                                     const std::vector<uint64_t>& dex_file_offsets,
51*288bf522SAndroid Build Coastguard Worker                                     const std::function<void(DexFileSymbol*)>& symbol_callback) {
52*288bf522SAndroid Build Coastguard Worker   for (uint64_t file_offset : dex_file_offsets) {
53*288bf522SAndroid Build Coastguard Worker     size_t max_file_size;
54*288bf522SAndroid Build Coastguard Worker     if (__builtin_sub_overflow(size, file_offset, &max_file_size)) {
55*288bf522SAndroid Build Coastguard Worker       LOG(WARNING) << "failed to read dex file symbols from " << debug_filename << "(offset "
56*288bf522SAndroid Build Coastguard Worker                    << file_offset << ")";
57*288bf522SAndroid Build Coastguard Worker       return false;
58*288bf522SAndroid Build Coastguard Worker     }
59*288bf522SAndroid Build Coastguard Worker     uint8_t* file_addr = static_cast<uint8_t*>(addr) + file_offset;
60*288bf522SAndroid Build Coastguard Worker     std::unique_ptr<art_api::dex::DexFile> dex_file;
61*288bf522SAndroid Build Coastguard Worker     art_api::dex::DexFile::Error error_msg =
62*288bf522SAndroid Build Coastguard Worker         art_api::dex::DexFile::Create(file_addr, max_file_size, nullptr, "", &dex_file);
63*288bf522SAndroid Build Coastguard Worker     if (dex_file == nullptr) {
64*288bf522SAndroid Build Coastguard Worker       LOG(WARNING) << "failed to read dex file symbols from " << debug_filename << "(offset "
65*288bf522SAndroid Build Coastguard Worker                    << file_offset << "): " << error_msg.ToString();
66*288bf522SAndroid Build Coastguard Worker       return false;
67*288bf522SAndroid Build Coastguard Worker     }
68*288bf522SAndroid Build Coastguard Worker     ReadSymbols(*dex_file, file_offset, symbol_callback);
69*288bf522SAndroid Build Coastguard Worker   }
70*288bf522SAndroid Build Coastguard Worker   return true;
71*288bf522SAndroid Build Coastguard Worker }
72*288bf522SAndroid Build Coastguard Worker 
ReadSymbolsFromDexFile(const std::string & file_path,const std::vector<uint64_t> & dex_file_offsets,const std::function<void (DexFileSymbol *)> & symbol_callback)73*288bf522SAndroid Build Coastguard Worker bool ReadSymbolsFromDexFile(const std::string& file_path,
74*288bf522SAndroid Build Coastguard Worker                             const std::vector<uint64_t>& dex_file_offsets,
75*288bf522SAndroid Build Coastguard Worker                             const std::function<void(DexFileSymbol*)>& symbol_callback) {
76*288bf522SAndroid Build Coastguard Worker   android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file_path.c_str(), O_RDONLY | O_CLOEXEC)));
77*288bf522SAndroid Build Coastguard Worker   if (fd == -1) {
78*288bf522SAndroid Build Coastguard Worker     return false;
79*288bf522SAndroid Build Coastguard Worker   }
80*288bf522SAndroid Build Coastguard Worker   size_t file_size = GetFileSize(file_path);
81*288bf522SAndroid Build Coastguard Worker   if (file_size == 0) {
82*288bf522SAndroid Build Coastguard Worker     return false;
83*288bf522SAndroid Build Coastguard Worker   }
84*288bf522SAndroid Build Coastguard Worker   std::unique_ptr<android::base::MappedFile> map;
85*288bf522SAndroid Build Coastguard Worker   map = android::base::MappedFile::FromFd(fd, 0, file_size, PROT_READ);
86*288bf522SAndroid Build Coastguard Worker   if (map == nullptr) {
87*288bf522SAndroid Build Coastguard Worker     return false;
88*288bf522SAndroid Build Coastguard Worker   }
89*288bf522SAndroid Build Coastguard Worker   return ReadSymbolsFromDexFileInMemory(map->data(), file_size, file_path, dex_file_offsets,
90*288bf522SAndroid Build Coastguard Worker                                         symbol_callback);
91*288bf522SAndroid Build Coastguard Worker }
92*288bf522SAndroid Build Coastguard Worker 
93*288bf522SAndroid Build Coastguard Worker }  // namespace simpleperf
94