1 /* 2 * Copyright (C) 2016 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 <stddef.h> 20 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <unordered_map> 25 #include <utility> 26 27 #include <unwindstack/Arch.h> 28 #include <unwindstack/ElfInterface.h> 29 #include <unwindstack/Memory.h> 30 #include <unwindstack/SharedString.h> 31 32 #if !defined(EM_AARCH64) 33 #define EM_AARCH64 183 34 #endif 35 36 #if !defined(EM_RISCV) 37 #define EM_RISCV 243 38 #endif 39 40 namespace unwindstack { 41 42 // Forward declaration. 43 class MapInfo; 44 class Regs; 45 46 class Elf { 47 public: Elf(Memory * memory)48 Elf(Memory* memory) : memory_(memory) {} 49 virtual ~Elf() = default; 50 51 bool Init(); 52 53 void InitGnuDebugdata(); 54 55 void Invalidate(); 56 57 std::string GetSoname(); 58 59 bool GetFunctionName(uint64_t addr, SharedString* name, uint64_t* func_offset); 60 61 bool GetGlobalVariableOffset(const std::string& name, uint64_t* memory_offset); 62 63 uint64_t GetRelPc(uint64_t pc, MapInfo* map_info); 64 65 bool StepIfSignalHandler(uint64_t rel_pc, Regs* regs, Memory* process_memory); 66 67 bool Step(uint64_t rel_pc, Regs* regs, Memory* process_memory, bool* finished, 68 bool* is_signal_frame); 69 70 ElfInterface* CreateInterfaceFromMemory(Memory* memory); 71 72 std::string GetBuildID(); 73 74 std::string GetPrintableBuildID(); 75 GetLoadBias()76 int64_t GetLoadBias() { return load_bias_; } 77 78 bool IsValidPc(uint64_t pc); 79 80 bool GetTextRange(uint64_t* addr, uint64_t* size); 81 82 void GetLastError(ErrorData* data); 83 ErrorCode GetLastErrorCode(); 84 uint64_t GetLastErrorAddress(); 85 valid()86 bool valid() { return valid_; } 87 machine_type()88 uint32_t machine_type() { return machine_type_; } 89 class_type()90 uint8_t class_type() { return class_type_; } 91 arch()92 ArchEnum arch() { return arch_; } 93 memory()94 Memory* memory() { return memory_.get(); } 95 interface()96 ElfInterface* interface() { return interface_.get(); } 97 gnu_debugdata_interface()98 ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); } 99 100 static bool IsValidElf(Memory* memory); 101 102 static bool GetInfo(Memory* memory, uint64_t* size); 103 104 static int64_t GetLoadBias(Memory* memory); 105 106 static std::string GetBuildID(Memory* memory); 107 108 // Caching cannot be enabled/disabled while unwinding. It is assumed 109 // that once enabled, it remains enabled while all unwinds are running. 110 // If the state of the caching changes while unwinding is occurring, 111 // it could cause crashes. 112 static void SetCachingEnabled(bool enable); 113 CachingEnabled()114 static bool CachingEnabled() { return cache_enabled_; } 115 116 static void CacheLock(); 117 static void CacheUnlock(); 118 static void CacheAdd(MapInfo* info); 119 static bool CacheGet(MapInfo* info); 120 121 static std::string GetPrintableBuildID(std::string& build_id); 122 123 protected: 124 bool valid_ = false; 125 int64_t load_bias_ = 0; 126 std::unique_ptr<ElfInterface> interface_; 127 std::unique_ptr<Memory> memory_; 128 uint32_t machine_type_; 129 uint8_t class_type_; 130 ArchEnum arch_; 131 // Protect calls that can modify internal state of the interface object. 132 std::mutex lock_; 133 134 std::unique_ptr<Memory> gnu_debugdata_memory_; 135 std::unique_ptr<ElfInterface> gnu_debugdata_interface_; 136 137 static bool cache_enabled_; 138 static std::unordered_map<std::string, std::unordered_map<uint64_t, std::shared_ptr<Elf>>>* 139 cache_; 140 static std::mutex* cache_lock_; 141 }; 142 143 } // namespace unwindstack 144