1*eb293b8fSAndroid Build Coastguard Worker /* 2*eb293b8fSAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project 3*eb293b8fSAndroid Build Coastguard Worker * 4*eb293b8fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*eb293b8fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*eb293b8fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*eb293b8fSAndroid Build Coastguard Worker * 8*eb293b8fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*eb293b8fSAndroid Build Coastguard Worker * 10*eb293b8fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*eb293b8fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*eb293b8fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*eb293b8fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*eb293b8fSAndroid Build Coastguard Worker * limitations under the License. 15*eb293b8fSAndroid Build Coastguard Worker */ 16*eb293b8fSAndroid Build Coastguard Worker 17*eb293b8fSAndroid Build Coastguard Worker #pragma once 18*eb293b8fSAndroid Build Coastguard Worker 19*eb293b8fSAndroid Build Coastguard Worker #include <stdint.h> 20*eb293b8fSAndroid Build Coastguard Worker 21*eb293b8fSAndroid Build Coastguard Worker #include <map> 22*eb293b8fSAndroid Build Coastguard Worker #include <memory> 23*eb293b8fSAndroid Build Coastguard Worker #include <mutex> 24*eb293b8fSAndroid Build Coastguard Worker #include <string> 25*eb293b8fSAndroid Build Coastguard Worker #include <utility> 26*eb293b8fSAndroid Build Coastguard Worker #include <vector> 27*eb293b8fSAndroid Build Coastguard Worker 28*eb293b8fSAndroid Build Coastguard Worker #include <unwindstack/SharedString.h> 29*eb293b8fSAndroid Build Coastguard Worker 30*eb293b8fSAndroid Build Coastguard Worker #include <art_api/dex_file_support.h> 31*eb293b8fSAndroid Build Coastguard Worker 32*eb293b8fSAndroid Build Coastguard Worker namespace unwindstack { 33*eb293b8fSAndroid Build Coastguard Worker 34*eb293b8fSAndroid Build Coastguard Worker class MapInfo; 35*eb293b8fSAndroid Build Coastguard Worker class Memory; 36*eb293b8fSAndroid Build Coastguard Worker 37*eb293b8fSAndroid Build Coastguard Worker class DexFile { 38*eb293b8fSAndroid Build Coastguard Worker struct Info { 39*eb293b8fSAndroid Build Coastguard Worker uint32_t offset; // Symbol start offset (relative to start of dex file). 40*eb293b8fSAndroid Build Coastguard Worker SharedString name; 41*eb293b8fSAndroid Build Coastguard Worker }; 42*eb293b8fSAndroid Build Coastguard Worker 43*eb293b8fSAndroid Build Coastguard Worker public: IsValidPc(uint64_t dex_pc)44*eb293b8fSAndroid Build Coastguard Worker bool IsValidPc(uint64_t dex_pc) { 45*eb293b8fSAndroid Build Coastguard Worker return base_addr_ <= dex_pc && (dex_pc - base_addr_) < file_size_; 46*eb293b8fSAndroid Build Coastguard Worker } 47*eb293b8fSAndroid Build Coastguard Worker 48*eb293b8fSAndroid Build Coastguard Worker bool GetFunctionName(uint64_t dex_pc, SharedString* method_name, uint64_t* method_offset); 49*eb293b8fSAndroid Build Coastguard Worker 50*eb293b8fSAndroid Build Coastguard Worker static std::shared_ptr<DexFile> Create(uint64_t base_addr, uint64_t file_size, Memory* memory, 51*eb293b8fSAndroid Build Coastguard Worker MapInfo* info); 52*eb293b8fSAndroid Build Coastguard Worker 53*eb293b8fSAndroid Build Coastguard Worker private: 54*eb293b8fSAndroid Build Coastguard Worker // The underlying C API. It might be shared by multiple DexFiles (with different base_addr). 55*eb293b8fSAndroid Build Coastguard Worker struct DexFileApi { 56*eb293b8fSAndroid Build Coastguard Worker std::unique_ptr<art_api::dex::DexFile> dex_; 57*eb293b8fSAndroid Build Coastguard Worker std::shared_ptr<Memory> memory_; // Keep alive the memory object backing the dex file data. 58*eb293b8fSAndroid Build Coastguard Worker std::mutex lock_; // The C API is not thread-safe so we need to lock it. 59*eb293b8fSAndroid Build Coastguard Worker }; 60*eb293b8fSAndroid Build Coastguard Worker 61*eb293b8fSAndroid Build Coastguard Worker static std::shared_ptr<DexFile> CreateFromDisk(uint64_t addr, uint64_t size, MapInfo* map); 62*eb293b8fSAndroid Build Coastguard Worker DexFile(uint64_t base_addr,uint64_t file_size,std::shared_ptr<DexFileApi> && dex_api)63*eb293b8fSAndroid Build Coastguard Worker DexFile(uint64_t base_addr, uint64_t file_size, std::shared_ptr<DexFileApi>&& dex_api) 64*eb293b8fSAndroid Build Coastguard Worker : base_addr_(base_addr), file_size_(file_size), dex_api_(std::move(dex_api)) {} 65*eb293b8fSAndroid Build Coastguard Worker 66*eb293b8fSAndroid Build Coastguard Worker uint64_t base_addr_ = 0; // Absolute address where this DEX file is in memory. 67*eb293b8fSAndroid Build Coastguard Worker uint64_t file_size_ = 0; // Total number of bytes in the dex file. 68*eb293b8fSAndroid Build Coastguard Worker std::shared_ptr<DexFileApi> dex_api_; // Loaded underling dex object. 69*eb293b8fSAndroid Build Coastguard Worker 70*eb293b8fSAndroid Build Coastguard Worker std::map<uint32_t, Info> symbols_; // Cache of read symbols (keyed by *end* offset). 71*eb293b8fSAndroid Build Coastguard Worker 72*eb293b8fSAndroid Build Coastguard Worker // The same file can be mapped many times in system-wide profiling (once per process). 73*eb293b8fSAndroid Build Coastguard Worker // Furthermore, the ART side of the API will create expensive PC lookup table for it. 74*eb293b8fSAndroid Build Coastguard Worker // Therefore, we maintain cache to avoid loading the same file (sub-range) many times. 75*eb293b8fSAndroid Build Coastguard Worker // The cache is weak: It will not keep DexFiles alive (the weak_ptr will become null). 76*eb293b8fSAndroid Build Coastguard Worker using MappedFileKey = std::tuple<std::string, uint64_t, uint64_t>; // (path, offset, size). 77*eb293b8fSAndroid Build Coastguard Worker static std::map<MappedFileKey, std::weak_ptr<DexFileApi>> g_mapped_dex_files; 78*eb293b8fSAndroid Build Coastguard Worker static std::mutex g_lock; // Guards the static cache above. 79*eb293b8fSAndroid Build Coastguard Worker }; 80*eb293b8fSAndroid Build Coastguard Worker 81*eb293b8fSAndroid Build Coastguard Worker } // namespace unwindstack 82