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