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