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 29class DynamicLoaderDarwinKernel : public lldb_private::DynamicLoader 30{ 31public: 32 //------------------------------------------------------------------ 33 // Static Functions 34 //------------------------------------------------------------------ 35 static void 36 Initialize(); 37 38 static void 39 Terminate(); 40 41 static lldb_private::ConstString 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 virtual 56 ~DynamicLoaderDarwinKernel (); 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 virtual void 65 DidAttach (); 66 67 virtual void 68 DidLaunch (); 69 70 virtual lldb::ThreadPlanSP 71 GetStepThroughTrampolinePlan (lldb_private::Thread &thread, 72 bool stop_others); 73 74 virtual lldb_private::Error 75 CanLoadImage (); 76 77 //------------------------------------------------------------------ 78 // PluginInterface protocol 79 //------------------------------------------------------------------ 80 virtual lldb_private::ConstString 81 GetPluginName(); 82 83 virtual uint32_t 84 GetPluginVersion(); 85 86protected: 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 270 struct OSKextLoadedKextSummaryHeader 271 { 272 uint32_t version; 273 uint32_t entry_size; 274 uint32_t entry_count; 275 lldb::addr_t image_infos_addr; 276 277 OSKextLoadedKextSummaryHeader() : 278 version (0), 279 entry_size (0), 280 entry_count (0), 281 image_infos_addr (LLDB_INVALID_ADDRESS) 282 { 283 } 284 285 uint32_t 286 GetSize() 287 { 288 switch (version) 289 { 290 case 0: return 0; // Can't know the size without a valid version 291 case 1: return 8; // Version 1 only had a version + entry_count 292 default: break; 293 } 294 // Version 2 and above has version, entry_size, entry_count, and reserved 295 return 16; 296 } 297 298 void 299 Clear() 300 { 301 version = 0; 302 entry_size = 0; 303 entry_count = 0; 304 image_infos_addr = LLDB_INVALID_ADDRESS; 305 } 306 307 bool 308 IsValid() const 309 { 310 return version >= 1 || version <= 2; 311 } 312 }; 313 314 void 315 RegisterNotificationCallbacks(); 316 317 void 318 UnregisterNotificationCallbacks(); 319 320 void 321 SetNotificationBreakpointIfNeeded (); 322 323 bool 324 ReadAllKextSummaries (); 325 326 bool 327 ReadKextSummaryHeader (); 328 329 bool 330 ParseKextSummaries (const lldb_private::Address &kext_summary_addr, 331 uint32_t count); 332 333 void 334 UpdateImageInfosHeaderAndLoadCommands(KextImageInfo::collection &image_infos, 335 uint32_t infos_count, 336 bool update_executable); 337 338 uint32_t 339 ReadKextSummaries (const lldb_private::Address &kext_summary_addr, 340 uint32_t image_infos_count, 341 KextImageInfo::collection &image_infos); 342 343 static lldb::addr_t 344 SearchForDarwinKernel (lldb_private::Process *process); 345 346 static lldb::addr_t 347 SearchForKernelAtSameLoadAddr (lldb_private::Process *process); 348 349 static lldb::addr_t 350 SearchForKernelWithDebugHints (lldb_private::Process *process); 351 352 static lldb::addr_t 353 SearchForKernelNearPC (lldb_private::Process *process); 354 355 static lldb::addr_t 356 SearchForKernelViaExhaustiveSearch (lldb_private::Process *process); 357 358 static lldb_private::UUID 359 CheckForKernelImageAtAddress (lldb::addr_t addr, lldb_private::Process *process); 360 361 lldb::addr_t m_kernel_load_address; 362 KextImageInfo m_kernel; // Info about the current kernel image being used 363 364 lldb_private::Address m_kext_summary_header_ptr_addr; 365 lldb_private::Address m_kext_summary_header_addr; 366 OSKextLoadedKextSummaryHeader m_kext_summary_header; 367 KextImageInfo::collection m_known_kexts; 368 mutable lldb_private::Mutex m_mutex; 369 lldb::user_id_t m_break_id; 370 371private: 372 DISALLOW_COPY_AND_ASSIGN (DynamicLoaderDarwinKernel); 373}; 374 375#endif // liblldb_DynamicLoaderDarwinKernel_h_ 376