blob: 93b8b9497798c2e87efe9e794f673f66459d8bd3 [file] [log] [blame]
Greg Clayton944b8282011-08-22 22:30:57 +00001//===-- DynamicLoaderDarwinKernel.cpp -----------------------------*- 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
10#include "lldb/Breakpoint/StoppointCallbackContext.h"
11#include "lldb/Core/DataBuffer.h"
12#include "lldb/Core/DataBufferHeap.h"
Greg Clayton07e66e32011-07-20 03:41:06 +000013#include "lldb/Core/Debugger.h"
Greg Clayton7b242382011-07-08 00:48:09 +000014#include "lldb/Core/Log.h"
15#include "lldb/Core/Module.h"
16#include "lldb/Core/PluginManager.h"
17#include "lldb/Core/State.h"
18#include "lldb/Symbol/ObjectFile.h"
19#include "lldb/Target/ObjCLanguageRuntime.h"
20#include "lldb/Target/RegisterContext.h"
21#include "lldb/Target/Target.h"
22#include "lldb/Target/Thread.h"
23#include "lldb/Target/ThreadPlanRunToAddress.h"
24#include "lldb/Target/StackFrame.h"
25
Greg Clayton944b8282011-08-22 22:30:57 +000026#include "DynamicLoaderDarwinKernel.h"
Greg Clayton7b242382011-07-08 00:48:09 +000027
28//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
29#ifdef ENABLE_DEBUG_PRINTF
30#include <stdio.h>
31#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
32#else
33#define DEBUG_PRINTF(fmt, ...)
34#endif
35
36using namespace lldb;
37using namespace lldb_private;
38
39/// FIXME - The ObjC Runtime trampoline handler doesn't really belong here.
40/// I am putting it here so I can invoke it in the Trampoline code here, but
41/// it should be moved to the ObjC Runtime support when it is set up.
42
43
44//----------------------------------------------------------------------
45// Create an instance of this class. This function is filled into
46// the plugin info class that gets handed out by the plugin factory and
47// allows the lldb to instantiate an instance of this class.
48//----------------------------------------------------------------------
49DynamicLoader *
Greg Clayton944b8282011-08-22 22:30:57 +000050DynamicLoaderDarwinKernel::CreateInstance (Process* process, bool force)
Greg Clayton7b242382011-07-08 00:48:09 +000051{
52 bool create = force;
53 if (!create)
54 {
Greg Claytonaa149cb2011-08-11 02:48:45 +000055 Module* exe_module = process->GetTarget().GetExecutableModulePointer();
Greg Claytondf0b7d52011-07-08 04:11:42 +000056 if (exe_module)
57 {
58 ObjectFile *object_file = exe_module->GetObjectFile();
59 if (object_file)
60 {
Sean Callanan49bce8e2012-02-10 20:22:35 +000061 create = (object_file->GetStrata() == ObjectFile::eStrataKernel);
Greg Claytondf0b7d52011-07-08 04:11:42 +000062 }
63 }
64
65 if (create)
66 {
67 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
68 create = triple_ref.getOS() == llvm::Triple::Darwin && triple_ref.getVendor() == llvm::Triple::Apple;
69 }
Greg Clayton7b242382011-07-08 00:48:09 +000070 }
71
72 if (create)
Sean Callanan90539452011-09-20 23:01:51 +000073 {
74 process->SetCanJIT(false);
Greg Clayton944b8282011-08-22 22:30:57 +000075 return new DynamicLoaderDarwinKernel (process);
Sean Callanan90539452011-09-20 23:01:51 +000076 }
Greg Clayton7b242382011-07-08 00:48:09 +000077 return NULL;
78}
79
80//----------------------------------------------------------------------
81// Constructor
82//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +000083DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process) :
Greg Clayton7b242382011-07-08 00:48:09 +000084 DynamicLoader(process),
85 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +000086 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +000087 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +000088 m_kext_summary_header (),
Greg Clayton7b242382011-07-08 00:48:09 +000089 m_kext_summaries(),
Daniel Dunbara08823f2011-10-31 22:50:49 +000090 m_mutex(Mutex::eMutexTypeRecursive),
91 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +000092{
93}
94
95//----------------------------------------------------------------------
96// Destructor
97//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +000098DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +000099{
100 Clear(true);
101}
102
Greg Clayton374972e2011-07-09 17:15:55 +0000103void
Greg Clayton944b8282011-08-22 22:30:57 +0000104DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000105{
106 LoadKernelModuleIfNeeded();
107 SetNotificationBreakpointIfNeeded ();
108}
Greg Clayton7b242382011-07-08 00:48:09 +0000109//------------------------------------------------------------------
110/// Called after attaching a process.
111///
112/// Allow DynamicLoader plug-ins to execute some code after
113/// attaching to a process.
114//------------------------------------------------------------------
115void
Greg Clayton944b8282011-08-22 22:30:57 +0000116DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000117{
118 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000119 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000120}
121
122//------------------------------------------------------------------
123/// Called after attaching a process.
124///
125/// Allow DynamicLoader plug-ins to execute some code after
126/// attaching to a process.
127//------------------------------------------------------------------
128void
Greg Clayton944b8282011-08-22 22:30:57 +0000129DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000130{
131 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000132 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000133}
134
135
136//----------------------------------------------------------------------
137// Clear out the state of this class.
138//----------------------------------------------------------------------
139void
Greg Clayton944b8282011-08-22 22:30:57 +0000140DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000141{
142 Mutex::Locker locker(m_mutex);
143
144 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
145 m_process->ClearBreakpointSiteByID(m_break_id);
146
147 if (clear_process)
148 m_process = NULL;
149 m_kernel.Clear(false);
Greg Claytond16e1e52011-07-12 17:06:17 +0000150 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000151 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000152 m_kext_summaries.clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000153 m_break_id = LLDB_INVALID_BREAK_ID;
154}
155
Greg Clayton7b242382011-07-08 00:48:09 +0000156
Greg Claytonc859e2d2012-02-13 23:10:39 +0000157bool
Greg Clayton2af282a2012-03-21 04:25:00 +0000158DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageAtFileAddress (Process *process)
159{
160 if (IsLoaded())
161 return true;
162
163 if (module_sp)
164 {
165 bool changed = false;
166 if (module_sp->SetLoadAddress (process->GetTarget(), 0, changed))
167 load_process_stop_id = process->GetStopID();
168 }
169 return false;
170}
171
172bool
Greg Claytonc859e2d2012-02-13 23:10:39 +0000173DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageUsingMemoryModule (Process *process)
174{
175 if (IsLoaded())
176 return true;
177
178 bool uuid_is_valid = uuid.IsValid();
179
180 Target &target = process->GetTarget();
181 ModuleSP memory_module_sp;
182 // Use the memory module as the module if we have one...
183 if (address != LLDB_INVALID_ADDRESS)
184 {
185 FileSpec file_spec;
186 if (module_sp)
187 file_spec = module_sp->GetFileSpec();
188 else
189 file_spec.SetFile (name, false);
190
191 memory_module_sp = process->ReadModuleFromMemory (file_spec, address, false, false);
192 if (memory_module_sp && !uuid_is_valid)
193 {
194 uuid = memory_module_sp->GetUUID();
195 uuid_is_valid = uuid.IsValid();
196 }
197 }
198
199 if (!module_sp)
200 {
201 bool uuid_is_valid = uuid.IsValid();
202 if (uuid_is_valid)
203 {
204 ModuleList &target_images = target.GetImages();
205 module_sp = target_images.FindModule(uuid);
206
207 if (!module_sp)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000208 {
Greg Clayton2af282a2012-03-21 04:25:00 +0000209 ModuleSpec module_spec;
Greg Claytonb9a01b32012-02-26 05:51:37 +0000210 module_spec.GetUUID() = uuid;
211 module_sp = target.GetSharedModule (module_spec);
212 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000213 }
214 }
215
216
217 if (memory_module_sp)
218 {
219 // Someone already supplied a file, make sure it is the right one.
220 if (module_sp)
221 {
222 if (module_sp->GetUUID() == memory_module_sp->GetUUID())
223 {
224 ObjectFile *ondisk_object_file = module_sp->GetObjectFile();
225 ObjectFile *memory_object_file = memory_module_sp->GetObjectFile();
226 if (memory_object_file && ondisk_object_file)
227 {
228 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
229 SectionList *memory_section_list = memory_object_file->GetSectionList ();
230 if (memory_section_list && ondisk_section_list)
231 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000232 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000233 // There may be CTF sections in the memory image so we can't
234 // always just compare the number of sections (which are actually
235 // segments in mach-o parlance)
236 uint32_t sect_idx = 0;
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000237
238
239 // We now iterate through all sections in the file module
240 // and look to see if the memory module has a load address
241 // for that section.
242 uint32_t num_sections_loaded = 0;
243 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000244 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000245 const Section *ondisk_section = ondisk_section_list->GetSectionAtIndex(sect_idx).get();
246 if (ondisk_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000247 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000248 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section->GetName()).get();
249 if (memory_section)
250 {
251 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section, memory_section->GetFileAddress());
252 ++num_sections_loaded;
253 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000254 }
255 }
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000256 if (num_sections_loaded > 0)
257 load_process_stop_id = process->GetStopID();
258 else
259 module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000260 }
261 else
262 module_sp.reset(); // One or both section lists
263 }
264 else
265 module_sp.reset(); // One or both object files missing
266 }
267 else
268 module_sp.reset(); // UUID mismatch
269 }
270
271 // Use the memory module as the module if we didn't like the file
272 // module we either found or were supplied with
273 if (!module_sp)
274 {
275 module_sp = memory_module_sp;
276 // Load the memory image in the target as all adresses are already correct
277 bool changed = false;
278 target.GetImages().Append (memory_module_sp);
279 if (module_sp->SetLoadAddress (target, 0, changed))
280 load_process_stop_id = process->GetStopID();
281 }
282 }
283 bool is_loaded = IsLoaded();
284
285 if (so_address.IsValid())
286 {
287 if (is_loaded)
288 so_address.SetLoadAddress (address, &target);
289 else
290 target.GetImages().ResolveFileAddress (address, so_address);
291
292 }
293 return is_loaded;
294}
295
Greg Clayton7b242382011-07-08 00:48:09 +0000296//----------------------------------------------------------------------
297// Load the kernel module and initialize the "m_kernel" member. Return
298// true _only_ if the kernel is loaded the first time through (subsequent
299// calls to this function should return false after the kernel has been
300// already loaded).
301//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000302void
Greg Clayton944b8282011-08-22 22:30:57 +0000303DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000304{
Greg Claytond16e1e52011-07-12 17:06:17 +0000305 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000306 {
307 m_kernel.Clear(false);
308 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000309 strncpy(m_kernel.name, "mach_kernel", sizeof(m_kernel.name));
310 if (m_kernel.address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +0000311 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000312 m_kernel.address = m_process->GetImageInfoAddress ();
313 if (m_kernel.address == LLDB_INVALID_ADDRESS && m_kernel.module_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000314 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000315 // We didn't get a hint from the process, so we will
316 // try the kernel at the address that it exists at in
317 // the file if we have one
318 ObjectFile *kernel_object_file = m_kernel.module_sp->GetObjectFile();
319 if (kernel_object_file)
320 m_kernel.address = kernel_object_file->GetHeaderAddress().GetFileAddress();
Greg Clayton7b242382011-07-08 00:48:09 +0000321 }
322 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000323
324 if (m_kernel.address != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +0000325 {
326 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
327 {
328 m_kernel.LoadImageAtFileAddress (m_process);
329 }
330 }
Greg Clayton7b242382011-07-08 00:48:09 +0000331
Greg Claytonc859e2d2012-02-13 23:10:39 +0000332 if (m_kernel.IsLoaded())
Greg Clayton7b242382011-07-08 00:48:09 +0000333 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000334 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
335 const Symbol *symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
336 if (symbol)
337 {
Greg Claytone7612132012-03-07 21:03:09 +0000338 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000339 // Update all image infos
340 ReadAllKextSummaries ();
341 }
Greg Clayton7b242382011-07-08 00:48:09 +0000342 }
343 else
Greg Clayton7b242382011-07-08 00:48:09 +0000344 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000345 m_kernel.Clear(false);
Greg Clayton7b242382011-07-08 00:48:09 +0000346 }
347 }
Greg Clayton7b242382011-07-08 00:48:09 +0000348}
349
Greg Clayton7b242382011-07-08 00:48:09 +0000350//----------------------------------------------------------------------
351// Static callback function that gets called when our DYLD notification
352// breakpoint gets hit. We update all of our image infos and then
353// let our super class DynamicLoader class decide if we should stop
354// or not (based on global preference).
355//----------------------------------------------------------------------
356bool
Greg Clayton944b8282011-08-22 22:30:57 +0000357DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +0000358 StoppointCallbackContext *context,
359 user_id_t break_id,
360 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000361{
Greg Clayton944b8282011-08-22 22:30:57 +0000362 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000363}
364
365bool
Greg Clayton944b8282011-08-22 22:30:57 +0000366DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +0000367 user_id_t break_id,
368 user_id_t break_loc_id)
369{
Greg Claytond16e1e52011-07-12 17:06:17 +0000370 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
371 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000372 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +0000373
Greg Clayton374972e2011-07-09 17:15:55 +0000374 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000375
376 if (log)
377 PutToLog(log.get());
378
Greg Clayton374972e2011-07-09 17:15:55 +0000379 return GetStopWhenImagesChange();
380}
381
382
383bool
Greg Clayton944b8282011-08-22 22:30:57 +0000384DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +0000385{
386 Mutex::Locker locker(m_mutex);
387
388 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000389
390 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000391 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000392 {
393 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
394 const ByteOrder byte_order = m_kernel.GetByteOrder();
395 Error error;
396 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
397 // which is currenty 4 uint32_t and a pointer.
398 uint8_t buf[24];
399 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
400 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000401 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000402 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
403 prefer_file_cache,
404 error,
405 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000406 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000407 // We got a valid address for our kext summary header and make sure it isn't NULL
408 if (m_kext_summary_header_addr.IsValid() &&
409 m_kext_summary_header_addr.GetFileAddress() != 0)
410 {
411 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
412 if (bytes_read == count)
413 {
414 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000415 m_kext_summary_header.version = data.GetU32(&offset);
416 if (m_kext_summary_header.version >= 2)
417 {
418 m_kext_summary_header.entry_size = data.GetU32(&offset);
419 }
420 else
421 {
422 // Versions less than 2 didn't have an entry size, it was hard coded
423 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
424 }
425 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000426 return true;
427 }
428 }
Greg Clayton7b242382011-07-08 00:48:09 +0000429 }
430 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000431 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000432 return false;
433}
434
435
436bool
Greg Clayton944b8282011-08-22 22:30:57 +0000437DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000438 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000439{
440 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000441 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000442 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000443 log->Printf ("Adding %d modules.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000444
445 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000446
447 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
448 return false;
449
Greg Clayton07e66e32011-07-20 03:41:06 +0000450 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Greg Clayton7b242382011-07-08 00:48:09 +0000451 for (uint32_t i = 0; i < count; i++)
452 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000453 if (s)
454 {
455 const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
456 if (u)
457 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000458 s->Printf("Loading kext: %2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X 0x%16.16llx \"%s\"...",
Greg Clayton07e66e32011-07-20 03:41:06 +0000459 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
460 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
461 kext_summaries[i].address, kext_summaries[i].name);
462 }
463 else
464 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000465 s->Printf("0x%16.16llx \"%s\"...", kext_summaries[i].address, kext_summaries[i].name);
Greg Clayton07e66e32011-07-20 03:41:06 +0000466 }
467 }
468
Greg Clayton2af282a2012-03-21 04:25:00 +0000469 if (!kext_summaries[i].LoadImageUsingMemoryModule (m_process))
470 kext_summaries[i].LoadImageAtFileAddress (m_process);
Greg Claytonc859e2d2012-02-13 23:10:39 +0000471
Greg Clayton5b882162011-07-21 01:12:01 +0000472 if (s)
473 {
474 if (kext_summaries[i].module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000475 {
476 if (kext_summaries[i].module_sp->GetFileSpec().GetDirectory())
477 s->Printf("\n found kext: %s/%s\n",
478 kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
479 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
480 else
481 s->Printf("\n found kext: %s\n",
482 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
483 }
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000484 else
485 s->Printf (" failed to locate/load.\n");
Greg Clayton5b882162011-07-21 01:12:01 +0000486 }
487
Greg Claytona63d08c2011-07-19 03:57:15 +0000488 if (log)
489 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000490 }
491 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000492 return return_value;
493}
494
495// Adds the modules in image_infos to m_kext_summaries.
496// NB don't call this passing in m_kext_summaries.
497
498bool
Greg Clayton944b8282011-08-22 22:30:57 +0000499DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +0000500{
501 // Now add these images to the main list.
502 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000503
504 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
505 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000506 OSKextLoadedKextSummary &image_info = image_infos[idx];
507 m_kext_summaries.push_back(image_info);
Greg Clayton7b242382011-07-08 00:48:09 +0000508
Greg Claytonc859e2d2012-02-13 23:10:39 +0000509 if (image_info.module_sp && m_process->GetStopID() == image_info.load_process_stop_id)
510 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
Greg Clayton7b242382011-07-08 00:48:09 +0000511 }
512
513 if (loaded_module_list.GetSize() > 0)
514 {
Greg Clayton7b242382011-07-08 00:48:09 +0000515 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
516 }
517 return true;
518}
519
Greg Clayton7b242382011-07-08 00:48:09 +0000520
521uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000522DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000523 uint32_t image_infos_count,
524 OSKextLoadedKextSummary::collection &image_infos)
525{
526 const ByteOrder endian = m_kernel.GetByteOrder();
527 const uint32_t addr_size = m_kernel.GetAddressByteSize();
528
529 image_infos.resize(image_infos_count);
530 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
531 DataBufferHeap data(count, 0);
532 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000533
534 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
535
536 if (s)
537 s->Printf ("Reading %u kext summaries...\n", image_infos_count);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000538 const bool prefer_file_cache = false;
539 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
540 prefer_file_cache,
541 data.GetBytes(),
542 data.GetByteSize(),
543 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000544 if (bytes_read == count)
545 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000546
Greg Clayton7b242382011-07-08 00:48:09 +0000547 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
548 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000549 for (uint32_t kext_summary_offset = 0;
550 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
551 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000552 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000553 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000554 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
555 if (name_data == NULL)
556 break;
557 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
558 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
559 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000560 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
561 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000562 image_infos[i].size = extractor.GetU64(&offset);
563 image_infos[i].version = extractor.GetU64(&offset);
564 image_infos[i].load_tag = extractor.GetU32(&offset);
565 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000566 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
567 {
568 image_infos[i].reference_list = extractor.GetU64(&offset);
569 }
570 else
571 {
572 image_infos[i].reference_list = 0;
573 }
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000574// printf ("[%3u] %*.*s: address=0x%16.16llx, size=0x%16.16llx, version=0x%16.16llx, load_tag=0x%8.8x, flags=0x%8.8x\n",
575// i,
576// KERNEL_MODULE_MAX_NAME, KERNEL_MODULE_MAX_NAME, (char *)name_data,
577// image_infos[i].address,
578// image_infos[i].size,
579// image_infos[i].version,
580// image_infos[i].load_tag,
581// image_infos[i].flags);
Greg Clayton7b242382011-07-08 00:48:09 +0000582 }
583 if (i < image_infos.size())
584 image_infos.resize(i);
585 }
586 else
587 {
588 image_infos.clear();
589 }
590 return image_infos.size();
591}
592
593bool
Greg Clayton944b8282011-08-22 22:30:57 +0000594DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000595{
Greg Clayton374972e2011-07-09 17:15:55 +0000596 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000597
598 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000599
Greg Clayton7b242382011-07-08 00:48:09 +0000600 if (ReadKextSummaryHeader ())
601 {
Greg Clayton374972e2011-07-09 17:15:55 +0000602 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000603 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000604 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000605 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000606 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000607 {
Greg Clayton7b242382011-07-08 00:48:09 +0000608 m_kext_summaries.clear();
609 }
610 return true;
611 }
612 }
613 return false;
614}
615
616//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000617// Dump an image info structure to the file handle provided.
618//----------------------------------------------------------------------
619void
Greg Clayton944b8282011-08-22 22:30:57 +0000620DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000621{
622 if (log == NULL)
623 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000624 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000625
626 if (address == LLDB_INVALID_ADDRESS)
627 {
628 if (u)
629 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000630 log->Printf("\tuuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\" (UNLOADED)",
Greg Clayton7b242382011-07-08 00:48:09 +0000631 u[ 0], u[ 1], u[ 2], u[ 3],
632 u[ 4], u[ 5], u[ 6], u[ 7],
633 u[ 8], u[ 9], u[10], u[11],
634 u[12], u[13], u[14], u[15],
635 name);
636 }
637 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000638 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000639 }
640 else
641 {
642 if (u)
643 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000644 log->Printf("\taddr=0x%16.16llx size=0x%16.16llx version=0x%16.16llx load-tag=0x%8.8x flags=0x%8.8x ref-list=0x%16.16llx uuid=%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X name=\"%s\"",
645 address, size, version, load_tag, flags, reference_list,
646 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
647 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000648 name);
649 }
650 else
651 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000652 log->Printf("\t[0x%16.16llx - 0x%16.16llx) version=0x%16.16llx load-tag=0x%8.8x flags=0x%8.8x ref-list=0x%16.16llx name=\"%s\"",
653 address, address+size, version, load_tag, flags, reference_list,
654 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000655 }
Greg Clayton7b242382011-07-08 00:48:09 +0000656 }
657}
658
659//----------------------------------------------------------------------
660// Dump the _dyld_all_image_infos members and all current image infos
661// that we have parsed to the file handle provided.
662//----------------------------------------------------------------------
663void
Greg Clayton944b8282011-08-22 22:30:57 +0000664DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000665{
666 if (log == NULL)
667 return;
668
669 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000670 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000671 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000672 m_kext_summary_header.version,
673 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000674 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000675
676 size_t i;
677 const size_t count = m_kext_summaries.size();
678 if (count > 0)
679 {
680 log->PutCString("Loaded:");
681 for (i = 0; i<count; i++)
682 m_kext_summaries[i].PutToLog(log);
683 }
684}
685
686void
Greg Clayton944b8282011-08-22 22:30:57 +0000687DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +0000688{
Greg Clayton944b8282011-08-22 22:30:57 +0000689 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +0000690 Clear(true);
691 m_process = process;
692 m_process->GetTarget().GetSectionLoadList().Clear();
693}
694
Greg Clayton374972e2011-07-09 17:15:55 +0000695void
Greg Clayton944b8282011-08-22 22:30:57 +0000696DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000697{
Greg Claytonc859e2d2012-02-13 23:10:39 +0000698 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.module_sp)
Greg Clayton374972e2011-07-09 17:15:55 +0000699 {
Greg Clayton944b8282011-08-22 22:30:57 +0000700 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +0000701
Greg Claytond16e1e52011-07-12 17:06:17 +0000702
Greg Clayton374972e2011-07-09 17:15:55 +0000703 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000704 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +0000705 FileSpecList module_spec_list;
706 module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
707 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +0000708 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +0000709 "OSKextLoadedKextSummariesUpdated",
710 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000711 internal_bp,
712 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000713
Greg Clayton944b8282011-08-22 22:30:57 +0000714 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +0000715 m_break_id = bp->GetID();
716 }
Greg Clayton7b242382011-07-08 00:48:09 +0000717}
718
719//----------------------------------------------------------------------
720// Member function that gets called when the process state changes.
721//----------------------------------------------------------------------
722void
Greg Clayton944b8282011-08-22 22:30:57 +0000723DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +0000724{
Greg Clayton944b8282011-08-22 22:30:57 +0000725 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +0000726 switch (state)
727 {
728 case eStateConnected:
729 case eStateAttaching:
730 case eStateLaunching:
731 case eStateInvalid:
732 case eStateUnloaded:
733 case eStateExited:
734 case eStateDetached:
735 Clear(false);
736 break;
737
738 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +0000739 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000740 break;
741
742 case eStateRunning:
743 case eStateStepping:
744 case eStateCrashed:
745 case eStateSuspended:
746 break;
747
748 default:
749 break;
750 }
751}
752
753ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +0000754DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +0000755{
756 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +0000757 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
758 if (log)
759 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +0000760 return thread_plan_sp;
761}
762
763Error
Greg Clayton944b8282011-08-22 22:30:57 +0000764DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +0000765{
766 Error error;
767 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
768 return error;
769}
770
771void
Greg Clayton944b8282011-08-22 22:30:57 +0000772DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +0000773{
774 PluginManager::RegisterPlugin (GetPluginNameStatic(),
775 GetPluginDescriptionStatic(),
776 CreateInstance);
777}
778
779void
Greg Clayton944b8282011-08-22 22:30:57 +0000780DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +0000781{
782 PluginManager::UnregisterPlugin (CreateInstance);
783}
784
785
786const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000787DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000788{
789 return "dynamic-loader.macosx-kernel";
790}
791
792const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000793DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000794{
795 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
796}
797
798
799//------------------------------------------------------------------
800// PluginInterface protocol
801//------------------------------------------------------------------
802const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000803DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000804{
Greg Clayton944b8282011-08-22 22:30:57 +0000805 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +0000806}
807
808const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000809DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000810{
811 return GetPluginNameStatic();
812}
813
814uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000815DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +0000816{
817 return 1;
818}
819