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 #pragma once 18 19 #include <stdint.h> 20 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <string> 25 #include <utility> 26 #include <vector> 27 28 #include <unwindstack/SharedString.h> 29 30 #include <art_api/dex_file_support.h> 31 32 namespace unwindstack { 33 34 class MapInfo; 35 class Memory; 36 37 class DexFile { 38 struct Info { 39 uint32_t offset; // Symbol start offset (relative to start of dex file). 40 SharedString name; 41 }; 42 43 public: IsValidPc(uint64_t dex_pc)44 bool IsValidPc(uint64_t dex_pc) { 45 return base_addr_ <= dex_pc && (dex_pc - base_addr_) < file_size_; 46 } 47 48 bool GetFunctionName(uint64_t dex_pc, SharedString* method_name, uint64_t* method_offset); 49 50 static std::shared_ptr<DexFile> Create(uint64_t base_addr, uint64_t file_size, Memory* memory, 51 MapInfo* info); 52 53 private: 54 // The underlying C API. It might be shared by multiple DexFiles (with different base_addr). 55 struct DexFileApi { 56 std::unique_ptr<art_api::dex::DexFile> dex_; 57 std::unique_ptr<Memory> memory_; // Keep alive the memory object backing the dex file data. 58 std::mutex lock_; // The C API is not thread-safe so we need to lock it. 59 }; 60 61 static std::shared_ptr<DexFile> CreateFromDisk(uint64_t addr, uint64_t size, MapInfo* map); 62 DexFile(uint64_t base_addr,uint64_t file_size,std::shared_ptr<DexFileApi> && dex_api)63 DexFile(uint64_t base_addr, uint64_t file_size, std::shared_ptr<DexFileApi>&& dex_api) 64 : base_addr_(base_addr), file_size_(file_size), dex_api_(std::move(dex_api)) {} 65 66 uint64_t base_addr_ = 0; // Absolute address where this DEX file is in memory. 67 uint64_t file_size_ = 0; // Total number of bytes in the dex file. 68 std::shared_ptr<DexFileApi> dex_api_; // Loaded underling dex object. 69 70 std::map<uint32_t, Info> symbols_; // Cache of read symbols (keyed by *end* offset). 71 72 // The same file can be mapped many times in system-wide profiling (once per process). 73 // Furthermore, the ART side of the API will create expensive PC lookup table for it. 74 // Therefore, we maintain cache to avoid loading the same file (sub-range) many times. 75 // The cache is weak: It will not keep DexFiles alive (the weak_ptr will become null). 76 using MappedFileKey = std::tuple<std::string, uint64_t, uint64_t>; // (path, offset, size). 77 static std::map<MappedFileKey, std::weak_ptr<DexFileApi>> g_mapped_dex_files; 78 static std::mutex g_lock; // Guards the static cache above. 79 }; 80 81 } // namespace unwindstack 82