blob: 47fba086a4a9fefcfa80e4f182598f283eab462e [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
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000015#include <mutex>
Greg Clayton7b242382011-07-08 00:48:09 +000016#include <string>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000017#include <vector>
Greg Clayton7b242382011-07-08 00:48:09 +000018
19// Other libraries and framework includes
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000020// Project includes
Greg Clayton7b242382011-07-08 00:48:09 +000021#include "lldb/Target/DynamicLoader.h"
22#include "lldb/Host/FileSpec.h"
23#include "lldb/Host/TimeValue.h"
24#include "lldb/Core/UUID.h"
Greg Clayton7b242382011-07-08 00:48:09 +000025#include "lldb/Target/Process.h"
26
Greg Clayton944b8282011-08-22 22:30:57 +000027class DynamicLoaderDarwinKernel : public lldb_private::DynamicLoader
Greg Clayton7b242382011-07-08 00:48:09 +000028{
29public:
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000030 DynamicLoaderDarwinKernel(lldb_private::Process *process, lldb::addr_t kernel_addr);
31
32 ~DynamicLoaderDarwinKernel() override;
33
Greg Clayton7b242382011-07-08 00:48:09 +000034 //------------------------------------------------------------------
35 // Static Functions
36 //------------------------------------------------------------------
37 static void
38 Initialize();
39
40 static void
41 Terminate();
42
Greg Clayton57abc5d2013-05-10 21:47:16 +000043 static lldb_private::ConstString
Greg Clayton7b242382011-07-08 00:48:09 +000044 GetPluginNameStatic();
45
46 static const char *
47 GetPluginDescriptionStatic();
48
49 static lldb_private::DynamicLoader *
50 CreateInstance (lldb_private::Process *process, bool force);
51
Greg Claytone8cd0c92012-10-19 18:02:49 +000052 static void
53 DebuggerInitialize (lldb_private::Debugger &debugger);
54
Jason Molenda880988a2016-02-06 04:55:26 +000055 static lldb::addr_t
56 SearchForDarwinKernel (lldb_private::Process *process);
57
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 //------------------------------------------------------------------
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000064 void
65 DidAttach() override;
Greg Clayton7b242382011-07-08 00:48:09 +000066
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000067 void
68 DidLaunch() override;
Greg Clayton7b242382011-07-08 00:48:09 +000069
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000070 lldb::ThreadPlanSP
71 GetStepThroughTrampolinePlan(lldb_private::Thread &thread,
72 bool stop_others) override;
Greg Clayton7b242382011-07-08 00:48:09 +000073
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000074 lldb_private::Error
75 CanLoadImage() override;
Greg Clayton7b242382011-07-08 00:48:09 +000076
77 //------------------------------------------------------------------
78 // PluginInterface protocol
79 //------------------------------------------------------------------
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000080 lldb_private::ConstString
81 GetPluginName() override;
Greg Clayton7b242382011-07-08 00:48:09 +000082
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000083 uint32_t
84 GetPluginVersion() override;
Greg Clayton7b242382011-07-08 00:48:09 +000085
86protected:
87 void
88 PrivateInitialize (lldb_private::Process *process);
89
90 void
91 PrivateProcessStateChanged (lldb_private::Process *process,
92 lldb::StateType state);
Jason Molenda306bd0a2013-02-19 05:42:46 +000093
Greg Clayton374972e2011-07-09 17:15:55 +000094 void
95 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +000096
Greg Clayton374972e2011-07-09 17:15:55 +000097 void
98 LoadKernelModuleIfNeeded ();
Greg Clayton7b242382011-07-08 00:48:09 +000099
100 void
101 Clear (bool clear_process);
102
103 void
104 PutToLog (lldb_private::Log *log) const;
105
106 static bool
Greg Clayton374972e2011-07-09 17:15:55 +0000107 BreakpointHitCallback (void *baton,
108 lldb_private::StoppointCallbackContext *context,
109 lldb::user_id_t break_id,
110 lldb::user_id_t break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000111
Greg Clayton374972e2011-07-09 17:15:55 +0000112 bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000113 BreakpointHit (lldb_private::StoppointCallbackContext *context,
114 lldb::user_id_t break_id,
Greg Clayton374972e2011-07-09 17:15:55 +0000115 lldb::user_id_t break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000116 uint32_t
Greg Claytonc859e2d2012-02-13 23:10:39 +0000117 GetAddrByteSize()
Greg Clayton7b242382011-07-08 00:48:09 +0000118 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000119 return m_kernel.GetAddressByteSize();
Greg Clayton7b242382011-07-08 00:48:09 +0000120 }
121
122 static lldb::ByteOrder
Greg Clayton1f746072012-08-29 21:13:06 +0000123 GetByteOrderFromMagic (uint32_t magic);
Greg Clayton7b242382011-07-08 00:48:09 +0000124
Greg Claytona63d08c2011-07-19 03:57:15 +0000125 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 };
Greg Clayton7b242382011-07-08 00:48:09 +0000134
Jason Molenda306bd0a2013-02-19 05:42:46 +0000135 // 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 { }
Greg Clayton7b242382011-07-08 00:48:09 +0000168
169 void
Jason Molenda306bd0a2013-02-19 05:42:46 +0000170 Clear ()
Greg Clayton7b242382011-07-08 00:48:09 +0000171 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000172 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;
Greg Clayton7b242382011-07-08 00:48:09 +0000179 }
180
181 bool
Greg Clayton02c322c2012-03-21 22:50:54 +0000182 LoadImageAtFileAddress (lldb_private::Process *process);
183
184 bool
Greg Claytonc859e2d2012-02-13 23:10:39 +0000185 LoadImageUsingMemoryModule (lldb_private::Process *process);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000186
Greg Clayton7b242382011-07-08 00:48:09 +0000187 bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000188 IsLoaded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000189 {
Jason Molenda306bd0a2013-02-19 05:42:46 +0000190 return m_load_process_stop_id != UINT32_MAX;
Greg Clayton7b242382011-07-08 00:48:09 +0000191 }
192
Jason Molenda306bd0a2013-02-19 05:42:46 +0000193 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
Greg Clayton7b242382011-07-08 00:48:09 +0000233 uint32_t
Jason Molenda306bd0a2013-02-19 05:42:46 +0000234 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
Greg Clayton7b242382011-07-08 00:48:09 +0000244
245 lldb::ByteOrder
Jason Molenda306bd0a2013-02-19 05:42:46 +0000246 GetByteOrder(); // as determined by Mach-O header
Greg Clayton7b242382011-07-08 00:48:09 +0000247
248 lldb_private::ArchSpec
Jason Molenda306bd0a2013-02-19 05:42:46 +0000249 GetArchitecture () const; // as determined by Mach-O header
Greg Clayton7b242382011-07-08 00:48:09 +0000250
Greg Clayton7b242382011-07-08 00:48:09 +0000251 void
252 PutToLog (lldb_private::Log *log) const;
253
Jason Molenda306bd0a2013-02-19 05:42:46 +0000254 typedef std::vector<KextImageInfo> collection;
Greg Clayton7b242382011-07-08 00:48:09 +0000255 typedef collection::iterator iterator;
256 typedef collection::const_iterator const_iterator;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000257
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
Greg Clayton7b242382011-07-08 00:48:09 +0000267 };
268
269 struct OSKextLoadedKextSummaryHeader
270 {
271 uint32_t version;
272 uint32_t entry_size;
273 uint32_t entry_count;
Greg Clayton7b242382011-07-08 00:48:09 +0000274 lldb::addr_t image_infos_addr;
275
276 OSKextLoadedKextSummaryHeader() :
277 version (0),
278 entry_size (0),
279 entry_count (0),
Greg Clayton7b242382011-07-08 00:48:09 +0000280 image_infos_addr (LLDB_INVALID_ADDRESS)
281 {
282 }
283
Greg Claytona63d08c2011-07-19 03:57:15 +0000284 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
Jason Molenda306bd0a2013-02-19 05:42:46 +0000294 return 16;
Greg Claytona63d08c2011-07-19 03:57:15 +0000295 }
296
Greg Clayton7b242382011-07-08 00:48:09 +0000297 void
298 Clear()
299 {
300 version = 0;
301 entry_size = 0;
302 entry_count = 0;
Greg Clayton7b242382011-07-08 00:48:09 +0000303 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
Greg Clayton374972e2011-07-09 17:15:55 +0000319 void
320 SetNotificationBreakpointIfNeeded ();
Greg Clayton7b242382011-07-08 00:48:09 +0000321
322 bool
Greg Clayton374972e2011-07-09 17:15:55 +0000323 ReadAllKextSummaries ();
Greg Clayton7b242382011-07-08 00:48:09 +0000324
325 bool
326 ReadKextSummaryHeader ();
Jason Molenda306bd0a2013-02-19 05:42:46 +0000327
Greg Clayton7b242382011-07-08 00:48:09 +0000328 bool
Jason Molenda306bd0a2013-02-19 05:42:46 +0000329 ParseKextSummaries (const lldb_private::Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000330 uint32_t count);
Jason Molenda306bd0a2013-02-19 05:42:46 +0000331
Greg Clayton7b242382011-07-08 00:48:09 +0000332 void
Jason Molenda306bd0a2013-02-19 05:42:46 +0000333 UpdateImageInfosHeaderAndLoadCommands(KextImageInfo::collection &image_infos,
334 uint32_t infos_count,
Greg Clayton7b242382011-07-08 00:48:09 +0000335 bool update_executable);
336
Greg Clayton7b242382011-07-08 00:48:09 +0000337 uint32_t
Greg Clayton0d9fc762011-07-08 03:21:57 +0000338 ReadKextSummaries (const lldb_private::Address &kext_summary_addr,
Jason Molenda306bd0a2013-02-19 05:42:46 +0000339 uint32_t image_infos_count,
340 KextImageInfo::collection &image_infos);
341
Jason Molenda6ba6d3d2013-01-30 04:39:32 +0000342 static lldb::addr_t
343 SearchForKernelAtSameLoadAddr (lldb_private::Process *process);
344
345 static lldb::addr_t
346 SearchForKernelWithDebugHints (lldb_private::Process *process);
347
348 static lldb::addr_t
349 SearchForKernelNearPC (lldb_private::Process *process);
350
351 static lldb::addr_t
352 SearchForKernelViaExhaustiveSearch (lldb_private::Process *process);
353
354 static lldb_private::UUID
355 CheckForKernelImageAtAddress (lldb::addr_t addr, lldb_private::Process *process);
356
Jason Molenda306bd0a2013-02-19 05:42:46 +0000357 lldb::addr_t m_kernel_load_address;
358 KextImageInfo m_kernel; // Info about the current kernel image being used
359
360 lldb_private::Address m_kext_summary_header_ptr_addr;
361 lldb_private::Address m_kext_summary_header_addr;
362 OSKextLoadedKextSummaryHeader m_kext_summary_header;
363 KextImageInfo::collection m_known_kexts;
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000364 mutable std::recursive_mutex m_mutex;
Jason Molenda306bd0a2013-02-19 05:42:46 +0000365 lldb::user_id_t m_break_id;
Greg Clayton7b242382011-07-08 00:48:09 +0000366
367private:
Greg Clayton944b8282011-08-22 22:30:57 +0000368 DISALLOW_COPY_AND_ASSIGN (DynamicLoaderDarwinKernel);
Greg Clayton7b242382011-07-08 00:48:09 +0000369};
370
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +0000371#endif // liblldb_DynamicLoaderDarwinKernel_h_