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