blob: ebdf4dbdea2710e1184f13bbba832c3956b10a94 [file] [log] [blame]
Greg Clayton944b8282011-08-22 22:30:57 +00001//===-- DynamicLoaderDarwinKernel.h -----------------------------*- C++ -*-===//
Greg Clayton7b242382011-07-08 00:48:09 +00002//
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 Clayton944b8282011-08-22 22:30:57 +000010#ifndef liblldb_DynamicLoaderDarwinKernel_h_
11#define liblldb_DynamicLoaderDarwinKernel_h_
Greg Clayton7b242382011-07-08 00:48:09 +000012
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 Clayton944b8282011-08-22 22:30:57 +000029class DynamicLoaderDarwinKernel : public lldb_private::DynamicLoader
Greg Clayton7b242382011-07-08 00:48:09 +000030{
31public:
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 Claytone8cd0c92012-10-19 18:02:49 +000050 static void
51 DebuggerInitialize (lldb_private::Debugger &debugger);
52
Jason Molenda6ba6d3d2013-01-30 04:39:32 +000053 DynamicLoaderDarwinKernel (lldb_private::Process *process, lldb::addr_t kernel_addr);
Greg Clayton7b242382011-07-08 00:48:09 +000054
55 virtual
Greg Clayton944b8282011-08-22 22:30:57 +000056 ~DynamicLoaderDarwinKernel ();
Jason Molenda306bd0a2013-02-19 05:42:46 +000057
Greg Clayton7b242382011-07-08 00:48:09 +000058 //------------------------------------------------------------------
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
89protected:
90 void
91 PrivateInitialize (lldb_private::Process *process);
92
93 void
94 PrivateProcessStateChanged (lldb_private::Process *process,
95 lldb::StateType state);
Jason Molenda306bd0a2013-02-19 05:42:46 +000096
Greg Clayton374972e2011-07-09 17:15:55 +000097 void
98 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +000099
Greg Clayton374972e2011-07-09 17:15:55 +0000100 void
101 LoadKernelModuleIfNeeded ();
Greg Clayton7b242382011-07-08 00:48:09 +0000102
103 void
104 Clear (bool clear_process);
105
106 void
107 PutToLog (lldb_private::Log *log) const;
108
109 static bool
Greg Clayton374972e2011-07-09 17:15:55 +0000110 BreakpointHitCallback (void *baton,
111 lldb_private::StoppointCallbackContext *context,
112 lldb::user_id_t break_id,
113 lldb::user_id_t break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000114
Greg Clayton374972e2011-07-09 17:15:55 +0000115 bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000116 BreakpointHit (lldb_private::StoppointCallbackContext *context,
117 lldb::user_id_t break_id,
Greg Clayton374972e2011-07-09 17:15:55 +0000118 lldb::user_id_t break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000119 uint32_t
Greg Claytonc859e2d2012-02-13 23:10:39 +0000120 GetAddrByteSize()
Greg Clayton7b242382011-07-08 00:48:09 +0000121 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000122 return m_kernel.GetAddressByteSize();
Greg Clayton7b242382011-07-08 00:48:09 +0000123 }
124
125 static lldb::ByteOrder
Greg Clayton1f746072012-08-29 21:13:06 +0000126 GetByteOrderFromMagic (uint32_t magic);
Greg Clayton7b242382011-07-08 00:48:09 +0000127
Greg Claytona63d08c2011-07-19 03:57:15 +0000128 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 Clayton7b242382011-07-08 00:48:09 +0000137
Jason Molenda306bd0a2013-02-19 05:42:46 +0000138 // 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 Clayton7b242382011-07-08 00:48:09 +0000171
172 void
Jason Molenda306bd0a2013-02-19 05:42:46 +0000173 Clear ()
Greg Clayton7b242382011-07-08 00:48:09 +0000174 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000175 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 Clayton7b242382011-07-08 00:48:09 +0000182 }
183
184 bool
Greg Clayton02c322c2012-03-21 22:50:54 +0000185 LoadImageAtFileAddress (lldb_private::Process *process);
186
187 bool
Greg Claytonc859e2d2012-02-13 23:10:39 +0000188 LoadImageUsingMemoryModule (lldb_private::Process *process);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000189
Greg Clayton7b242382011-07-08 00:48:09 +0000190 bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000191 IsLoaded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000192 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000193 return m_load_process_stop_id != UINT32_MAX;
Greg Clayton7b242382011-07-08 00:48:09 +0000194 }
195
Jason Molenda306bd0a2013-02-19 05:42:46 +0000196 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 Clayton7b242382011-07-08 00:48:09 +0000236 uint32_t
Jason Molenda306bd0a2013-02-19 05:42:46 +0000237 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 Clayton7b242382011-07-08 00:48:09 +0000247
248 lldb::ByteOrder
Jason Molenda306bd0a2013-02-19 05:42:46 +0000249 GetByteOrder(); // as determined by Mach-O header
Greg Clayton7b242382011-07-08 00:48:09 +0000250
251 lldb_private::ArchSpec
Jason Molenda306bd0a2013-02-19 05:42:46 +0000252 GetArchitecture () const; // as determined by Mach-O header
Greg Clayton7b242382011-07-08 00:48:09 +0000253
Greg Clayton7b242382011-07-08 00:48:09 +0000254 void
255 PutToLog (lldb_private::Log *log) const;
256
Jason Molenda306bd0a2013-02-19 05:42:46 +0000257 typedef std::vector<KextImageInfo> collection;
Greg Clayton7b242382011-07-08 00:48:09 +0000258 typedef collection::iterator iterator;
259 typedef collection::const_iterator const_iterator;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000260
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 Clayton7b242382011-07-08 00:48:09 +0000271 };
272
273 struct OSKextLoadedKextSummaryHeader
274 {
275 uint32_t version;
276 uint32_t entry_size;
277 uint32_t entry_count;
Greg Clayton7b242382011-07-08 00:48:09 +0000278 lldb::addr_t image_infos_addr;
279
280 OSKextLoadedKextSummaryHeader() :
281 version (0),
282 entry_size (0),
283 entry_count (0),
Greg Clayton7b242382011-07-08 00:48:09 +0000284 image_infos_addr (LLDB_INVALID_ADDRESS)
285 {
286 }
287
Greg Claytona63d08c2011-07-19 03:57:15 +0000288 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 Molenda306bd0a2013-02-19 05:42:46 +0000298 return 16;
Greg Claytona63d08c2011-07-19 03:57:15 +0000299 }
300
Greg Clayton7b242382011-07-08 00:48:09 +0000301 void
302 Clear()
303 {
304 version = 0;
305 entry_size = 0;
306 entry_count = 0;
Greg Clayton7b242382011-07-08 00:48:09 +0000307 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 Clayton374972e2011-07-09 17:15:55 +0000323 void
324 SetNotificationBreakpointIfNeeded ();
Greg Clayton7b242382011-07-08 00:48:09 +0000325
326 bool
Greg Clayton374972e2011-07-09 17:15:55 +0000327 ReadAllKextSummaries ();
Greg Clayton7b242382011-07-08 00:48:09 +0000328
329 bool
330 ReadKextSummaryHeader ();
Jason Molenda306bd0a2013-02-19 05:42:46 +0000331
Greg Clayton7b242382011-07-08 00:48:09 +0000332 bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000333 ParseKextSummaries (const lldb_private::Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000334 uint32_t count);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000335
Greg Clayton7b242382011-07-08 00:48:09 +0000336 bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000337 AddModulesUsingImageInfos (KextImageInfo::collection &image_infos);
338
Greg Clayton7b242382011-07-08 00:48:09 +0000339 void
Jason Molenda306bd0a2013-02-19 05:42:46 +0000340 UpdateImageInfosHeaderAndLoadCommands(KextImageInfo::collection &image_infos,
341 uint32_t infos_count,
Greg Clayton7b242382011-07-08 00:48:09 +0000342 bool update_executable);
343
Greg Clayton7b242382011-07-08 00:48:09 +0000344 uint32_t
Greg Clayton0d9fc762011-07-08 03:21:57 +0000345 ReadKextSummaries (const lldb_private::Address &kext_summary_addr,
Jason Molenda306bd0a2013-02-19 05:42:46 +0000346 uint32_t image_infos_count,
347 KextImageInfo::collection &image_infos);
348
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000349 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 Molenda306bd0a2013-02-19 05:42:46 +0000364 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 Clayton7b242382011-07-08 00:48:09 +0000373
374private:
Greg Clayton944b8282011-08-22 22:30:57 +0000375 DISALLOW_COPY_AND_ASSIGN (DynamicLoaderDarwinKernel);
Greg Clayton7b242382011-07-08 00:48:09 +0000376};
377
Greg Clayton944b8282011-08-22 22:30:57 +0000378#endif // liblldb_DynamicLoaderDarwinKernel_h_