Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 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 | #ifndef _LIBUNWINDSTACK_ELF_H |
| 18 | #define _LIBUNWINDSTACK_ELF_H |
| 19 | |
| 20 | #include <stddef.h> |
| 21 | |
| 22 | #include <memory> |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 23 | #include <mutex> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 24 | #include <string> |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 25 | #include <unordered_map> |
Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 26 | #include <utility> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 27 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 28 | #include <unwindstack/ElfInterface.h> |
| 29 | #include <unwindstack/Memory.h> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 30 | |
| 31 | #if !defined(EM_AARCH64) |
| 32 | #define EM_AARCH64 183 |
| 33 | #endif |
| 34 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 35 | namespace unwindstack { |
| 36 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 37 | // Forward declaration. |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 38 | struct MapInfo; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 39 | class Regs; |
| 40 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 41 | enum ArchEnum : uint8_t { |
| 42 | ARCH_UNKNOWN = 0, |
| 43 | ARCH_ARM, |
| 44 | ARCH_ARM64, |
| 45 | ARCH_X86, |
| 46 | ARCH_X86_64, |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 47 | ARCH_MIPS, |
| 48 | ARCH_MIPS64, |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 51 | class Elf { |
| 52 | public: |
| 53 | Elf(Memory* memory) : memory_(memory) {} |
| 54 | virtual ~Elf() = default; |
| 55 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 56 | bool Init(bool init_gnu_debugdata); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 57 | |
| 58 | void InitGnuDebugdata(); |
| 59 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 60 | bool GetSoname(std::string* name); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 61 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 62 | bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 63 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 64 | bool GetGlobalVariable(const std::string& name, uint64_t* memory_address); |
| 65 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 66 | uint64_t GetRelPc(uint64_t pc, const MapInfo* map_info); |
| 67 | |
Christopher Ferris | d299a7a | 2018-05-17 18:37:38 -0700 | [diff] [blame] | 68 | bool Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, Regs* regs, Memory* process_memory, |
| 69 | bool* finished); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 70 | |
| 71 | ElfInterface* CreateInterfaceFromMemory(Memory* memory); |
| 72 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 73 | uint64_t GetLoadBias() { return load_bias_; } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 74 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 75 | bool IsValidPc(uint64_t pc); |
| 76 | |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 77 | void GetLastError(ErrorData* data); |
| 78 | ErrorCode GetLastErrorCode(); |
| 79 | uint64_t GetLastErrorAddress(); |
| 80 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 81 | bool valid() { return valid_; } |
| 82 | |
| 83 | uint32_t machine_type() { return machine_type_; } |
| 84 | |
| 85 | uint8_t class_type() { return class_type_; } |
| 86 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 87 | ArchEnum arch() { return arch_; } |
| 88 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 89 | Memory* memory() { return memory_.get(); } |
| 90 | |
| 91 | ElfInterface* interface() { return interface_.get(); } |
| 92 | |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 93 | ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); } |
| 94 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 95 | static bool IsValidElf(Memory* memory); |
| 96 | |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 97 | static void GetInfo(Memory* memory, bool* valid, uint64_t* size); |
| 98 | |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 99 | static uint64_t GetLoadBias(Memory* memory); |
| 100 | |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 101 | static void SetCachingEnabled(bool enable); |
| 102 | static bool CachingEnabled() { return cache_enabled_; } |
| 103 | |
| 104 | static void CacheLock(); |
| 105 | static void CacheUnlock(); |
| 106 | static void CacheAdd(MapInfo* info); |
Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 107 | static bool CacheGet(MapInfo* info); |
| 108 | static bool CacheAfterCreateMemory(MapInfo* info); |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 109 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 110 | protected: |
| 111 | bool valid_ = false; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 112 | uint64_t load_bias_ = 0; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 113 | std::unique_ptr<ElfInterface> interface_; |
| 114 | std::unique_ptr<Memory> memory_; |
| 115 | uint32_t machine_type_; |
| 116 | uint8_t class_type_; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 117 | ArchEnum arch_; |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 118 | // Protect calls that can modify internal state of the interface object. |
| 119 | std::mutex lock_; |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 120 | |
| 121 | std::unique_ptr<Memory> gnu_debugdata_memory_; |
| 122 | std::unique_ptr<ElfInterface> gnu_debugdata_interface_; |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 123 | |
| 124 | static bool cache_enabled_; |
Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 125 | static std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>* cache_; |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 126 | static std::mutex* cache_lock_; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 129 | } // namespace unwindstack |
| 130 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 131 | #endif // _LIBUNWINDSTACK_ELF_H |