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