Greg Clayton | 944b828 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 1 | //===-- DynamicLoaderDarwinKernel.h -----------------------------*- C++ -*-===// |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 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 | |
Greg Clayton | 944b828 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 10 | #ifndef liblldb_DynamicLoaderDarwinKernel_h_ |
| 11 | #define liblldb_DynamicLoaderDarwinKernel_h_ |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 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 | |
Greg Clayton | 944b828 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 29 | class DynamicLoaderDarwinKernel : public lldb_private::DynamicLoader |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 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 | |
Greg Clayton | e8cd0c9 | 2012-10-19 18:02:49 +0000 | [diff] [blame] | 50 | static void |
| 51 | DebuggerInitialize (lldb_private::Debugger &debugger); |
| 52 | |
Jason Molenda | 6ba6d3d | 2013-01-30 04:39:32 +0000 | [diff] [blame] | 53 | DynamicLoaderDarwinKernel (lldb_private::Process *process, lldb::addr_t kernel_addr); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 54 | |
| 55 | virtual |
Greg Clayton | 944b828 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 56 | ~DynamicLoaderDarwinKernel (); |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 57 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 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 const char * |
| 81 | GetPluginName(); |
| 82 | |
| 83 | virtual const char * |
| 84 | GetShortPluginName(); |
| 85 | |
| 86 | virtual uint32_t |
| 87 | GetPluginVersion(); |
| 88 | |
| 89 | protected: |
| 90 | void |
| 91 | PrivateInitialize (lldb_private::Process *process); |
| 92 | |
| 93 | void |
| 94 | PrivateProcessStateChanged (lldb_private::Process *process, |
| 95 | lldb::StateType state); |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 96 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 97 | void |
| 98 | UpdateIfNeeded(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 99 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 100 | void |
| 101 | LoadKernelModuleIfNeeded (); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 102 | |
| 103 | void |
| 104 | Clear (bool clear_process); |
| 105 | |
| 106 | void |
| 107 | PutToLog (lldb_private::Log *log) const; |
| 108 | |
| 109 | static bool |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 110 | BreakpointHitCallback (void *baton, |
| 111 | lldb_private::StoppointCallbackContext *context, |
| 112 | lldb::user_id_t break_id, |
| 113 | lldb::user_id_t break_loc_id); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 114 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 115 | bool |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 116 | BreakpointHit (lldb_private::StoppointCallbackContext *context, |
| 117 | lldb::user_id_t break_id, |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 118 | lldb::user_id_t break_loc_id); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 119 | uint32_t |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 120 | GetAddrByteSize() |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 121 | { |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 122 | return m_kernel.GetAddressByteSize(); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static lldb::ByteOrder |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 126 | GetByteOrderFromMagic (uint32_t magic); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 127 | |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 128 | enum |
| 129 | { |
| 130 | KERNEL_MODULE_MAX_NAME = 64u, |
| 131 | // Versions less than 2 didn't have an entry size, |
| 132 | // they had a 64 bit name, 16 byte UUID, 8 byte addr, |
| 133 | // 8 byte size, 8 byte version, 4 byte load tag, and |
| 134 | // 4 byte flags |
| 135 | KERNEL_MODULE_ENTRY_SIZE_VERSION_1 = 64u + 16u + 8u + 8u + 8u + 4u + 4u |
| 136 | }; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 137 | |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 138 | // class KextImageInfo represents a single kext or kernel binary image. |
| 139 | // The class was designed to hold the information from the OSKextLoadedKextSummary |
| 140 | // structure (in libkern/libkern/OSKextLibPrivate.h from xnu). The kernel maintains |
| 141 | // a list of loded kexts in memory (the OSKextLoadedKextSummaryHeader structure, |
| 142 | // which points to an array of OSKextLoadedKextSummary's). |
| 143 | // |
| 144 | // A KextImageInfos may have - |
| 145 | // |
| 146 | // 1. The load address, name, UUID, and size of a kext/kernel binary in memory |
| 147 | // (read straight out of the kernel's list-of-kexts loaded) |
| 148 | // 2. A ModuleSP based on a MemoryModule read out of the kernel's memory |
| 149 | // (very unlikely to have any symbolic information) |
| 150 | // 3. A ModuleSP for an on-disk copy of the kext binary, possibly with debug info |
| 151 | // or a dSYM |
| 152 | // |
| 153 | // For performance reasons, the developer may prefer that lldb not load the kexts out |
| 154 | // of memory at the start of a kernel session. But we should build up / maintain a |
| 155 | // list of kexts that the kernel has told us about so we can relocate a kext module |
| 156 | // later if the user explicitly adds it to the target. |
| 157 | |
| 158 | class KextImageInfo |
| 159 | { |
| 160 | public: |
| 161 | KextImageInfo () : |
| 162 | m_name (), |
| 163 | m_module_sp (), |
| 164 | m_memory_module_sp (), |
| 165 | m_load_process_stop_id (UINT32_MAX), |
| 166 | m_uuid (), |
| 167 | m_load_address (LLDB_INVALID_ADDRESS), |
| 168 | m_size (0), |
| 169 | m_kernel_image (false) |
| 170 | { } |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 171 | |
| 172 | void |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 173 | Clear () |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 174 | { |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 175 | m_load_address = LLDB_INVALID_ADDRESS; |
| 176 | m_size = 0; |
| 177 | m_name.clear (); |
| 178 | m_uuid.Clear(); |
| 179 | m_module_sp.reset(); |
| 180 | m_memory_module_sp.reset(); |
| 181 | m_load_process_stop_id = UINT32_MAX; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool |
Greg Clayton | 02c322c | 2012-03-21 22:50:54 +0000 | [diff] [blame] | 185 | LoadImageAtFileAddress (lldb_private::Process *process); |
| 186 | |
| 187 | bool |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 188 | LoadImageUsingMemoryModule (lldb_private::Process *process); |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 189 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 190 | bool |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 191 | IsLoaded () |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 192 | { |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 193 | return m_load_process_stop_id != UINT32_MAX; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 196 | void |
| 197 | SetLoadAddress (lldb::addr_t load_addr); // Address of the Mach-O header for this binary |
| 198 | |
| 199 | lldb::addr_t |
| 200 | GetLoadAddress () const; // Address of the Mach-O header for this binary |
| 201 | |
| 202 | lldb_private::UUID |
| 203 | GetUUID () const; |
| 204 | |
| 205 | void |
| 206 | SetUUID (const lldb_private::UUID &uuid); |
| 207 | |
| 208 | void |
| 209 | SetName (const char *); |
| 210 | |
| 211 | std::string |
| 212 | GetName () const; |
| 213 | |
| 214 | void |
| 215 | SetModule (lldb::ModuleSP module); |
| 216 | |
| 217 | lldb::ModuleSP |
| 218 | GetModule (); |
| 219 | |
| 220 | // try to fill in m_memory_module_sp from memory based on the m_load_address |
| 221 | bool |
| 222 | ReadMemoryModule (lldb_private::Process *process); |
| 223 | |
| 224 | bool |
| 225 | IsKernel () const; // true if this is the mach_kernel; false if this is a kext |
| 226 | |
| 227 | void |
| 228 | SetIsKernel (bool is_kernel); |
| 229 | |
| 230 | uint64_t |
| 231 | GetSize () const; |
| 232 | |
| 233 | void |
| 234 | SetSize (uint64_t size); |
| 235 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 236 | uint32_t |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 237 | GetProcessStopId () const; // the stop-id when this binary was first noticed |
| 238 | |
| 239 | void |
| 240 | SetProcessStopId (uint32_t stop_id); |
| 241 | |
| 242 | bool |
| 243 | operator== (const KextImageInfo &rhs); |
| 244 | |
| 245 | uint32_t |
| 246 | GetAddressByteSize (); // as determined by Mach-O header |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 247 | |
| 248 | lldb::ByteOrder |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 249 | GetByteOrder(); // as determined by Mach-O header |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 250 | |
| 251 | lldb_private::ArchSpec |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 252 | GetArchitecture () const; // as determined by Mach-O header |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 253 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 254 | void |
| 255 | PutToLog (lldb_private::Log *log) const; |
| 256 | |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 257 | typedef std::vector<KextImageInfo> collection; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 258 | typedef collection::iterator iterator; |
| 259 | typedef collection::const_iterator const_iterator; |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 260 | |
| 261 | private: |
| 262 | std::string m_name; |
| 263 | lldb::ModuleSP m_module_sp; |
| 264 | lldb::ModuleSP m_memory_module_sp; |
| 265 | uint32_t m_load_process_stop_id; // the stop-id when this module was added to the Target |
| 266 | lldb_private::UUID m_uuid; // UUID for this dylib if it has one, else all zeros |
| 267 | lldb::addr_t m_load_address; |
| 268 | uint64_t m_size; |
| 269 | bool m_kernel_image; // true if this is the kernel, false if this is a kext |
| 270 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | struct OSKextLoadedKextSummaryHeader |
| 274 | { |
| 275 | uint32_t version; |
| 276 | uint32_t entry_size; |
| 277 | uint32_t entry_count; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 278 | lldb::addr_t image_infos_addr; |
| 279 | |
| 280 | OSKextLoadedKextSummaryHeader() : |
| 281 | version (0), |
| 282 | entry_size (0), |
| 283 | entry_count (0), |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 284 | image_infos_addr (LLDB_INVALID_ADDRESS) |
| 285 | { |
| 286 | } |
| 287 | |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 288 | uint32_t |
| 289 | GetSize() |
| 290 | { |
| 291 | switch (version) |
| 292 | { |
| 293 | case 0: return 0; // Can't know the size without a valid version |
| 294 | case 1: return 8; // Version 1 only had a version + entry_count |
| 295 | default: break; |
| 296 | } |
| 297 | // Version 2 and above has version, entry_size, entry_count, and reserved |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 298 | return 16; |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 301 | void |
| 302 | Clear() |
| 303 | { |
| 304 | version = 0; |
| 305 | entry_size = 0; |
| 306 | entry_count = 0; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 307 | image_infos_addr = LLDB_INVALID_ADDRESS; |
| 308 | } |
| 309 | |
| 310 | bool |
| 311 | IsValid() const |
| 312 | { |
| 313 | return version >= 1 || version <= 2; |
| 314 | } |
| 315 | }; |
| 316 | |
| 317 | void |
| 318 | RegisterNotificationCallbacks(); |
| 319 | |
| 320 | void |
| 321 | UnregisterNotificationCallbacks(); |
| 322 | |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 323 | void |
| 324 | SetNotificationBreakpointIfNeeded (); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 325 | |
| 326 | bool |
Greg Clayton | 374972e | 2011-07-09 17:15:55 +0000 | [diff] [blame] | 327 | ReadAllKextSummaries (); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 328 | |
| 329 | bool |
| 330 | ReadKextSummaryHeader (); |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 331 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 332 | bool |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 333 | ParseKextSummaries (const lldb_private::Address &kext_summary_addr, |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 334 | uint32_t count); |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 335 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 336 | bool |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 337 | AddModulesUsingImageInfos (KextImageInfo::collection &image_infos); |
| 338 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 339 | void |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 340 | UpdateImageInfosHeaderAndLoadCommands(KextImageInfo::collection &image_infos, |
| 341 | uint32_t infos_count, |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 342 | bool update_executable); |
| 343 | |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 344 | uint32_t |
Greg Clayton | 0d9fc76 | 2011-07-08 03:21:57 +0000 | [diff] [blame] | 345 | ReadKextSummaries (const lldb_private::Address &kext_summary_addr, |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 346 | uint32_t image_infos_count, |
| 347 | KextImageInfo::collection &image_infos); |
| 348 | |
Jason Molenda | 6ba6d3d | 2013-01-30 04:39:32 +0000 | [diff] [blame] | 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 | |
Jason Molenda | 306bd0a | 2013-02-19 05:42:46 +0000 | [diff] [blame^] | 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; |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 373 | |
| 374 | private: |
Greg Clayton | 944b828 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 375 | DISALLOW_COPY_AND_ASSIGN (DynamicLoaderDarwinKernel); |
Greg Clayton | 7b24238 | 2011-07-08 00:48:09 +0000 | [diff] [blame] | 376 | }; |
| 377 | |
Greg Clayton | 944b828 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 378 | #endif // liblldb_DynamicLoaderDarwinKernel_h_ |