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