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