1 //===-- DynamicLoaderDarwinKernel.h -----------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_DynamicLoaderDarwinKernel_h_ 11 #define liblldb_DynamicLoaderDarwinKernel_h_ 12 13 // C Includes 14 // C++ Includes 15 #include <mutex> 16 #include <string> 17 #include <vector> 18 19 // Other libraries and framework includes 20 // Project includes 21 #include "lldb/Core/UUID.h" 22 #include "lldb/Host/FileSpec.h" 23 #include "lldb/Host/TimeValue.h" 24 #include "lldb/Target/DynamicLoader.h" 25 #include "lldb/Target/Process.h" 26 27 class DynamicLoaderDarwinKernel : public lldb_private::DynamicLoader { 28 public: 29 DynamicLoaderDarwinKernel(lldb_private::Process *process, 30 lldb::addr_t kernel_addr); 31 32 ~DynamicLoaderDarwinKernel() override; 33 34 //------------------------------------------------------------------ 35 // Static Functions 36 //------------------------------------------------------------------ 37 static void Initialize(); 38 39 static void Terminate(); 40 41 static lldb_private::ConstString GetPluginNameStatic(); 42 43 static const char *GetPluginDescriptionStatic(); 44 45 static lldb_private::DynamicLoader * 46 CreateInstance(lldb_private::Process *process, bool force); 47 48 static void DebuggerInitialize(lldb_private::Debugger &debugger); 49 50 static lldb::addr_t SearchForDarwinKernel(lldb_private::Process *process); 51 52 //------------------------------------------------------------------ 53 /// Called after attaching a process. 54 /// 55 /// Allow DynamicLoader plug-ins to execute some code after 56 /// attaching to a process. 57 //------------------------------------------------------------------ 58 void DidAttach() override; 59 60 void DidLaunch() override; 61 62 lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, 63 bool stop_others) override; 64 65 lldb_private::Error CanLoadImage() override; 66 67 //------------------------------------------------------------------ 68 // PluginInterface protocol 69 //------------------------------------------------------------------ 70 lldb_private::ConstString GetPluginName() override; 71 72 uint32_t GetPluginVersion() override; 73 74 protected: 75 void PrivateInitialize(lldb_private::Process *process); 76 77 void PrivateProcessStateChanged(lldb_private::Process *process, 78 lldb::StateType state); 79 80 void UpdateIfNeeded(); 81 82 void LoadKernelModuleIfNeeded(); 83 84 void Clear(bool clear_process); 85 86 void PutToLog(lldb_private::Log *log) const; 87 88 static bool 89 BreakpointHitCallback(void *baton, 90 lldb_private::StoppointCallbackContext *context, 91 lldb::user_id_t break_id, lldb::user_id_t break_loc_id); 92 93 bool BreakpointHit(lldb_private::StoppointCallbackContext *context, 94 lldb::user_id_t break_id, lldb::user_id_t break_loc_id); 95 uint32_t GetAddrByteSize() { return m_kernel.GetAddressByteSize(); } 96 97 static lldb::ByteOrder GetByteOrderFromMagic(uint32_t magic); 98 99 enum { 100 KERNEL_MODULE_MAX_NAME = 64u, 101 // Versions less than 2 didn't have an entry size, 102 // they had a 64 bit name, 16 byte UUID, 8 byte addr, 103 // 8 byte size, 8 byte version, 4 byte load tag, and 104 // 4 byte flags 105 KERNEL_MODULE_ENTRY_SIZE_VERSION_1 = 64u + 16u + 8u + 8u + 8u + 4u + 4u 106 }; 107 108 // class KextImageInfo represents a single kext or kernel binary image. 109 // The class was designed to hold the information from the 110 // OSKextLoadedKextSummary 111 // structure (in libkern/libkern/OSKextLibPrivate.h from xnu). The kernel 112 // maintains 113 // a list of loded kexts in memory (the OSKextLoadedKextSummaryHeader 114 // structure, 115 // which points to an array of OSKextLoadedKextSummary's). 116 // 117 // A KextImageInfos may have - 118 // 119 // 1. The load address, name, UUID, and size of a kext/kernel binary in memory 120 // (read straight out of the kernel's list-of-kexts loaded) 121 // 2. A ModuleSP based on a MemoryModule read out of the kernel's memory 122 // (very unlikely to have any symbolic information) 123 // 3. A ModuleSP for an on-disk copy of the kext binary, possibly with debug 124 // info 125 // or a dSYM 126 // 127 // For performance reasons, the developer may prefer that lldb not load the 128 // kexts out 129 // of memory at the start of a kernel session. But we should build up / 130 // maintain a 131 // list of kexts that the kernel has told us about so we can relocate a kext 132 // module 133 // later if the user explicitly adds it to the target. 134 135 class KextImageInfo { 136 public: 137 KextImageInfo() 138 : m_name(), m_module_sp(), m_memory_module_sp(), 139 m_load_process_stop_id(UINT32_MAX), m_uuid(), 140 m_load_address(LLDB_INVALID_ADDRESS), m_size(0), 141 m_kernel_image(false) {} 142 143 void Clear() { 144 m_load_address = LLDB_INVALID_ADDRESS; 145 m_size = 0; 146 m_name.clear(); 147 m_uuid.Clear(); 148 m_module_sp.reset(); 149 m_memory_module_sp.reset(); 150 m_load_process_stop_id = UINT32_MAX; 151 } 152 153 bool LoadImageAtFileAddress(lldb_private::Process *process); 154 155 bool LoadImageUsingMemoryModule(lldb_private::Process *process); 156 157 bool IsLoaded() { return m_load_process_stop_id != UINT32_MAX; } 158 159 void SetLoadAddress( 160 lldb::addr_t load_addr); // Address of the Mach-O header for this binary 161 162 lldb::addr_t 163 GetLoadAddress() const; // Address of the Mach-O header for this binary 164 165 lldb_private::UUID GetUUID() const; 166 167 void SetUUID(const lldb_private::UUID &uuid); 168 169 void SetName(const char *); 170 171 std::string GetName() const; 172 173 void SetModule(lldb::ModuleSP module); 174 175 lldb::ModuleSP GetModule(); 176 177 // try to fill in m_memory_module_sp from memory based on the m_load_address 178 bool ReadMemoryModule(lldb_private::Process *process); 179 180 bool IsKernel() 181 const; // true if this is the mach_kernel; false if this is a kext 182 183 void SetIsKernel(bool is_kernel); 184 185 uint64_t GetSize() const; 186 187 void SetSize(uint64_t size); 188 189 uint32_t 190 GetProcessStopId() const; // the stop-id when this binary was first noticed 191 192 void SetProcessStopId(uint32_t stop_id); 193 194 bool operator==(const KextImageInfo &rhs); 195 196 uint32_t GetAddressByteSize(); // as determined by Mach-O header 197 198 lldb::ByteOrder GetByteOrder(); // as determined by Mach-O header 199 200 lldb_private::ArchSpec 201 GetArchitecture() const; // as determined by Mach-O header 202 203 void PutToLog(lldb_private::Log *log) const; 204 205 typedef std::vector<KextImageInfo> collection; 206 typedef collection::iterator iterator; 207 typedef collection::const_iterator const_iterator; 208 209 private: 210 std::string m_name; 211 lldb::ModuleSP m_module_sp; 212 lldb::ModuleSP m_memory_module_sp; 213 uint32_t m_load_process_stop_id; // the stop-id when this module was added 214 // to the Target 215 lldb_private::UUID 216 m_uuid; // UUID for this dylib if it has one, else all zeros 217 lldb::addr_t m_load_address; 218 uint64_t m_size; 219 bool m_kernel_image; // true if this is the kernel, false if this is a kext 220 }; 221 222 struct OSKextLoadedKextSummaryHeader { 223 uint32_t version; 224 uint32_t entry_size; 225 uint32_t entry_count; 226 lldb::addr_t image_infos_addr; 227 228 OSKextLoadedKextSummaryHeader() 229 : version(0), entry_size(0), entry_count(0), 230 image_infos_addr(LLDB_INVALID_ADDRESS) {} 231 232 uint32_t GetSize() { 233 switch (version) { 234 case 0: 235 return 0; // Can't know the size without a valid version 236 case 1: 237 return 8; // Version 1 only had a version + entry_count 238 default: 239 break; 240 } 241 // Version 2 and above has version, entry_size, entry_count, and reserved 242 return 16; 243 } 244 245 void Clear() { 246 version = 0; 247 entry_size = 0; 248 entry_count = 0; 249 image_infos_addr = LLDB_INVALID_ADDRESS; 250 } 251 252 bool IsValid() const { return version >= 1 || version <= 2; } 253 }; 254 255 void RegisterNotificationCallbacks(); 256 257 void UnregisterNotificationCallbacks(); 258 259 void SetNotificationBreakpointIfNeeded(); 260 261 bool ReadAllKextSummaries(); 262 263 bool ReadKextSummaryHeader(); 264 265 bool ParseKextSummaries(const lldb_private::Address &kext_summary_addr, 266 uint32_t count); 267 268 void 269 UpdateImageInfosHeaderAndLoadCommands(KextImageInfo::collection &image_infos, 270 uint32_t infos_count, 271 bool update_executable); 272 273 uint32_t ReadKextSummaries(const lldb_private::Address &kext_summary_addr, 274 uint32_t image_infos_count, 275 KextImageInfo::collection &image_infos); 276 277 static lldb::addr_t 278 SearchForKernelAtSameLoadAddr(lldb_private::Process *process); 279 280 static lldb::addr_t 281 SearchForKernelWithDebugHints(lldb_private::Process *process); 282 283 static lldb::addr_t SearchForKernelNearPC(lldb_private::Process *process); 284 285 static lldb::addr_t 286 SearchForKernelViaExhaustiveSearch(lldb_private::Process *process); 287 288 static lldb_private::UUID 289 CheckForKernelImageAtAddress(lldb::addr_t addr, 290 lldb_private::Process *process); 291 292 lldb::addr_t m_kernel_load_address; 293 KextImageInfo m_kernel; // Info about the current kernel image being used 294 295 lldb_private::Address m_kext_summary_header_ptr_addr; 296 lldb_private::Address m_kext_summary_header_addr; 297 OSKextLoadedKextSummaryHeader m_kext_summary_header; 298 KextImageInfo::collection m_known_kexts; 299 mutable std::recursive_mutex m_mutex; 300 lldb::user_id_t m_break_id; 301 302 private: 303 DISALLOW_COPY_AND_ASSIGN(DynamicLoaderDarwinKernel); 304 }; 305 306 #endif // liblldb_DynamicLoaderDarwinKernel_h_ 307