blob: 21bf70675ac150f42db04b57f439df447928e134 [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
158DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageUsingMemoryModule (Process *process)
159{
160 if (IsLoaded())
161 return true;
162
163 bool uuid_is_valid = uuid.IsValid();
164
165 Target &target = process->GetTarget();
166 ModuleSP memory_module_sp;
167 // Use the memory module as the module if we have one...
168 if (address != LLDB_INVALID_ADDRESS)
169 {
170 FileSpec file_spec;
171 if (module_sp)
172 file_spec = module_sp->GetFileSpec();
173 else
174 file_spec.SetFile (name, false);
175
176 memory_module_sp = process->ReadModuleFromMemory (file_spec, address, false, false);
177 if (memory_module_sp && !uuid_is_valid)
178 {
179 uuid = memory_module_sp->GetUUID();
180 uuid_is_valid = uuid.IsValid();
181 }
182 }
183
184 if (!module_sp)
185 {
186 bool uuid_is_valid = uuid.IsValid();
187 if (uuid_is_valid)
188 {
189 ModuleList &target_images = target.GetImages();
190 module_sp = target_images.FindModule(uuid);
191
192 if (!module_sp)
193 module_sp = target.GetSharedModule (FileSpec(), target.GetArchitecture(), &uuid);
194 }
195 }
196
197
198 if (memory_module_sp)
199 {
200 // Someone already supplied a file, make sure it is the right one.
201 if (module_sp)
202 {
203 if (module_sp->GetUUID() == memory_module_sp->GetUUID())
204 {
205 ObjectFile *ondisk_object_file = module_sp->GetObjectFile();
206 ObjectFile *memory_object_file = memory_module_sp->GetObjectFile();
207 if (memory_object_file && ondisk_object_file)
208 {
209 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
210 SectionList *memory_section_list = memory_object_file->GetSectionList ();
211 if (memory_section_list && ondisk_section_list)
212 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000213 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000214 // There may be CTF sections in the memory image so we can't
215 // always just compare the number of sections (which are actually
216 // segments in mach-o parlance)
217 uint32_t sect_idx = 0;
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000218
219
220 // We now iterate through all sections in the file module
221 // and look to see if the memory module has a load address
222 // for that section.
223 uint32_t num_sections_loaded = 0;
224 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000225 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000226 const Section *ondisk_section = ondisk_section_list->GetSectionAtIndex(sect_idx).get();
227 if (ondisk_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000228 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000229 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section->GetName()).get();
230 if (memory_section)
231 {
232 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section, memory_section->GetFileAddress());
233 ++num_sections_loaded;
234 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000235 }
236 }
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000237 if (num_sections_loaded > 0)
238 load_process_stop_id = process->GetStopID();
239 else
240 module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000241 }
242 else
243 module_sp.reset(); // One or both section lists
244 }
245 else
246 module_sp.reset(); // One or both object files missing
247 }
248 else
249 module_sp.reset(); // UUID mismatch
250 }
251
252 // Use the memory module as the module if we didn't like the file
253 // module we either found or were supplied with
254 if (!module_sp)
255 {
256 module_sp = memory_module_sp;
257 // Load the memory image in the target as all adresses are already correct
258 bool changed = false;
259 target.GetImages().Append (memory_module_sp);
260 if (module_sp->SetLoadAddress (target, 0, changed))
261 load_process_stop_id = process->GetStopID();
262 }
263 }
264 bool is_loaded = IsLoaded();
265
266 if (so_address.IsValid())
267 {
268 if (is_loaded)
269 so_address.SetLoadAddress (address, &target);
270 else
271 target.GetImages().ResolveFileAddress (address, so_address);
272
273 }
274 return is_loaded;
275}
276
Greg Clayton7b242382011-07-08 00:48:09 +0000277//----------------------------------------------------------------------
278// Load the kernel module and initialize the "m_kernel" member. Return
279// true _only_ if the kernel is loaded the first time through (subsequent
280// calls to this function should return false after the kernel has been
281// already loaded).
282//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000283void
Greg Clayton944b8282011-08-22 22:30:57 +0000284DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000285{
Greg Claytond16e1e52011-07-12 17:06:17 +0000286 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000287 {
288 m_kernel.Clear(false);
289 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000290 strncpy(m_kernel.name, "mach_kernel", sizeof(m_kernel.name));
291 if (m_kernel.address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +0000292 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000293 m_kernel.address = m_process->GetImageInfoAddress ();
294 if (m_kernel.address == LLDB_INVALID_ADDRESS && m_kernel.module_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000295 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000296 // We didn't get a hint from the process, so we will
297 // try the kernel at the address that it exists at in
298 // the file if we have one
299 ObjectFile *kernel_object_file = m_kernel.module_sp->GetObjectFile();
300 if (kernel_object_file)
301 m_kernel.address = kernel_object_file->GetHeaderAddress().GetFileAddress();
Greg Clayton7b242382011-07-08 00:48:09 +0000302 }
303 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000304
305 if (m_kernel.address != LLDB_INVALID_ADDRESS)
306 m_kernel.LoadImageUsingMemoryModule (m_process);
Greg Clayton7b242382011-07-08 00:48:09 +0000307
Greg Claytonc859e2d2012-02-13 23:10:39 +0000308 if (m_kernel.IsLoaded())
Greg Clayton7b242382011-07-08 00:48:09 +0000309 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000310 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
311 const Symbol *symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
312 if (symbol)
313 {
314 m_kext_summary_header_ptr_addr = symbol->GetValue();
315 // Update all image infos
316 ReadAllKextSummaries ();
317 }
Greg Clayton7b242382011-07-08 00:48:09 +0000318 }
319 else
Greg Clayton7b242382011-07-08 00:48:09 +0000320 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000321 m_kernel.Clear(false);
Greg Clayton7b242382011-07-08 00:48:09 +0000322 }
323 }
Greg Clayton7b242382011-07-08 00:48:09 +0000324}
325
Greg Clayton7b242382011-07-08 00:48:09 +0000326//----------------------------------------------------------------------
327// Static callback function that gets called when our DYLD notification
328// breakpoint gets hit. We update all of our image infos and then
329// let our super class DynamicLoader class decide if we should stop
330// or not (based on global preference).
331//----------------------------------------------------------------------
332bool
Greg Clayton944b8282011-08-22 22:30:57 +0000333DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +0000334 StoppointCallbackContext *context,
335 user_id_t break_id,
336 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000337{
Greg Clayton944b8282011-08-22 22:30:57 +0000338 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000339}
340
341bool
Greg Clayton944b8282011-08-22 22:30:57 +0000342DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +0000343 user_id_t break_id,
344 user_id_t break_loc_id)
345{
Greg Claytond16e1e52011-07-12 17:06:17 +0000346 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
347 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000348 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +0000349
Greg Clayton374972e2011-07-09 17:15:55 +0000350 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000351
352 if (log)
353 PutToLog(log.get());
354
Greg Clayton374972e2011-07-09 17:15:55 +0000355 return GetStopWhenImagesChange();
356}
357
358
359bool
Greg Clayton944b8282011-08-22 22:30:57 +0000360DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +0000361{
362 Mutex::Locker locker(m_mutex);
363
364 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000365
366 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000367 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000368 {
369 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
370 const ByteOrder byte_order = m_kernel.GetByteOrder();
371 Error error;
372 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
373 // which is currenty 4 uint32_t and a pointer.
374 uint8_t buf[24];
375 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
376 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000377 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000378 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
379 prefer_file_cache,
380 error,
381 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000382 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000383 // We got a valid address for our kext summary header and make sure it isn't NULL
384 if (m_kext_summary_header_addr.IsValid() &&
385 m_kext_summary_header_addr.GetFileAddress() != 0)
386 {
387 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
388 if (bytes_read == count)
389 {
390 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000391 m_kext_summary_header.version = data.GetU32(&offset);
392 if (m_kext_summary_header.version >= 2)
393 {
394 m_kext_summary_header.entry_size = data.GetU32(&offset);
395 }
396 else
397 {
398 // Versions less than 2 didn't have an entry size, it was hard coded
399 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
400 }
401 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000402 return true;
403 }
404 }
Greg Clayton7b242382011-07-08 00:48:09 +0000405 }
406 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000407 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000408 return false;
409}
410
411
412bool
Greg Clayton944b8282011-08-22 22:30:57 +0000413DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000414 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000415{
416 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000417 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000418 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000419 log->Printf ("Adding %d modules.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000420
421 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000422
423 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
424 return false;
425
Greg Clayton07e66e32011-07-20 03:41:06 +0000426 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Greg Clayton7b242382011-07-08 00:48:09 +0000427 for (uint32_t i = 0; i < count; i++)
428 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000429 if (s)
430 {
431 const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
432 if (u)
433 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000434 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 +0000435 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
436 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
437 kext_summaries[i].address, kext_summaries[i].name);
438 }
439 else
440 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000441 s->Printf("0x%16.16llx \"%s\"...", kext_summaries[i].address, kext_summaries[i].name);
Greg Clayton07e66e32011-07-20 03:41:06 +0000442 }
443 }
444
Greg Claytonc859e2d2012-02-13 23:10:39 +0000445 kext_summaries[i].LoadImageUsingMemoryModule (m_process);
446
Greg Clayton5b882162011-07-21 01:12:01 +0000447 if (s)
448 {
449 if (kext_summaries[i].module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000450 {
451 if (kext_summaries[i].module_sp->GetFileSpec().GetDirectory())
452 s->Printf("\n found kext: %s/%s\n",
453 kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
454 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
455 else
456 s->Printf("\n found kext: %s\n",
457 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
458 }
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000459 else
460 s->Printf (" failed to locate/load.\n");
Greg Clayton5b882162011-07-21 01:12:01 +0000461 }
462
Greg Claytona63d08c2011-07-19 03:57:15 +0000463 if (log)
464 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000465 }
466 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000467 return return_value;
468}
469
470// Adds the modules in image_infos to m_kext_summaries.
471// NB don't call this passing in m_kext_summaries.
472
473bool
Greg Clayton944b8282011-08-22 22:30:57 +0000474DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +0000475{
476 // Now add these images to the main list.
477 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000478
479 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
480 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000481 OSKextLoadedKextSummary &image_info = image_infos[idx];
482 m_kext_summaries.push_back(image_info);
Greg Clayton7b242382011-07-08 00:48:09 +0000483
Greg Claytonc859e2d2012-02-13 23:10:39 +0000484 if (image_info.module_sp && m_process->GetStopID() == image_info.load_process_stop_id)
485 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
Greg Clayton7b242382011-07-08 00:48:09 +0000486 }
487
488 if (loaded_module_list.GetSize() > 0)
489 {
490 // FIXME: This should really be in the Runtime handlers class, which should get
491 // called by the target's ModulesDidLoad, but we're doing it all locally for now
492 // to save time.
493 // Also, I'm assuming there can be only one libobjc dylib loaded...
494
495 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
496 if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary())
497 {
498 size_t num_modules = loaded_module_list.GetSize();
499 for (int i = 0; i < num_modules; i++)
500 {
501 if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i)))
502 {
503 objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i));
504 break;
505 }
506 }
507 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000508// if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000509// loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderDarwinKernel::ModulesDidLoad");
Greg Clayton7b242382011-07-08 00:48:09 +0000510 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
511 }
512 return true;
513}
514
Greg Clayton7b242382011-07-08 00:48:09 +0000515
516uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000517DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000518 uint32_t image_infos_count,
519 OSKextLoadedKextSummary::collection &image_infos)
520{
521 const ByteOrder endian = m_kernel.GetByteOrder();
522 const uint32_t addr_size = m_kernel.GetAddressByteSize();
523
524 image_infos.resize(image_infos_count);
525 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
526 DataBufferHeap data(count, 0);
527 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000528
529 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
530
531 if (s)
532 s->Printf ("Reading %u kext summaries...\n", image_infos_count);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000533 const bool prefer_file_cache = false;
534 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
535 prefer_file_cache,
536 data.GetBytes(),
537 data.GetByteSize(),
538 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000539 if (bytes_read == count)
540 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000541
Greg Clayton7b242382011-07-08 00:48:09 +0000542 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
543 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000544 for (uint32_t kext_summary_offset = 0;
545 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
546 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000547 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000548 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000549 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
550 if (name_data == NULL)
551 break;
552 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
553 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
554 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000555 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
556 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000557 image_infos[i].size = extractor.GetU64(&offset);
558 image_infos[i].version = extractor.GetU64(&offset);
559 image_infos[i].load_tag = extractor.GetU32(&offset);
560 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000561 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
562 {
563 image_infos[i].reference_list = extractor.GetU64(&offset);
564 }
565 else
566 {
567 image_infos[i].reference_list = 0;
568 }
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000569// 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",
570// i,
571// KERNEL_MODULE_MAX_NAME, KERNEL_MODULE_MAX_NAME, (char *)name_data,
572// image_infos[i].address,
573// image_infos[i].size,
574// image_infos[i].version,
575// image_infos[i].load_tag,
576// image_infos[i].flags);
Greg Clayton7b242382011-07-08 00:48:09 +0000577 }
578 if (i < image_infos.size())
579 image_infos.resize(i);
580 }
581 else
582 {
583 image_infos.clear();
584 }
585 return image_infos.size();
586}
587
588bool
Greg Clayton944b8282011-08-22 22:30:57 +0000589DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000590{
Greg Clayton374972e2011-07-09 17:15:55 +0000591 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000592
593 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000594
Greg Clayton7b242382011-07-08 00:48:09 +0000595 if (ReadKextSummaryHeader ())
596 {
Greg Clayton374972e2011-07-09 17:15:55 +0000597 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000598 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000599 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000600 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000601 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000602 {
Greg Clayton7b242382011-07-08 00:48:09 +0000603 m_kext_summaries.clear();
604 }
605 return true;
606 }
607 }
608 return false;
609}
610
611//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000612// Dump an image info structure to the file handle provided.
613//----------------------------------------------------------------------
614void
Greg Clayton944b8282011-08-22 22:30:57 +0000615DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000616{
617 if (log == NULL)
618 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000619 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000620
621 if (address == LLDB_INVALID_ADDRESS)
622 {
623 if (u)
624 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000625 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 +0000626 u[ 0], u[ 1], u[ 2], u[ 3],
627 u[ 4], u[ 5], u[ 6], u[ 7],
628 u[ 8], u[ 9], u[10], u[11],
629 u[12], u[13], u[14], u[15],
630 name);
631 }
632 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000633 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000634 }
635 else
636 {
637 if (u)
638 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000639 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\"",
640 address, size, version, load_tag, flags, reference_list,
641 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
642 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000643 name);
644 }
645 else
646 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000647 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\"",
648 address, address+size, version, load_tag, flags, reference_list,
649 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000650 }
Greg Clayton7b242382011-07-08 00:48:09 +0000651 }
652}
653
654//----------------------------------------------------------------------
655// Dump the _dyld_all_image_infos members and all current image infos
656// that we have parsed to the file handle provided.
657//----------------------------------------------------------------------
658void
Greg Clayton944b8282011-08-22 22:30:57 +0000659DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000660{
661 if (log == NULL)
662 return;
663
664 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000665 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000666 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000667 m_kext_summary_header.version,
668 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000669 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000670
671 size_t i;
672 const size_t count = m_kext_summaries.size();
673 if (count > 0)
674 {
675 log->PutCString("Loaded:");
676 for (i = 0; i<count; i++)
677 m_kext_summaries[i].PutToLog(log);
678 }
679}
680
681void
Greg Clayton944b8282011-08-22 22:30:57 +0000682DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +0000683{
Greg Clayton944b8282011-08-22 22:30:57 +0000684 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +0000685 Clear(true);
686 m_process = process;
687 m_process->GetTarget().GetSectionLoadList().Clear();
688}
689
Greg Clayton374972e2011-07-09 17:15:55 +0000690void
Greg Clayton944b8282011-08-22 22:30:57 +0000691DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000692{
Greg Claytonc859e2d2012-02-13 23:10:39 +0000693 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.module_sp)
Greg Clayton374972e2011-07-09 17:15:55 +0000694 {
Greg Clayton944b8282011-08-22 22:30:57 +0000695 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +0000696
Greg Claytond16e1e52011-07-12 17:06:17 +0000697
Greg Clayton374972e2011-07-09 17:15:55 +0000698 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000699 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +0000700 FileSpecList module_spec_list;
701 module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
702 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +0000703 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +0000704 "OSKextLoadedKextSummariesUpdated",
705 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000706 internal_bp,
707 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000708
Greg Clayton944b8282011-08-22 22:30:57 +0000709 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +0000710 m_break_id = bp->GetID();
711 }
Greg Clayton7b242382011-07-08 00:48:09 +0000712}
713
714//----------------------------------------------------------------------
715// Member function that gets called when the process state changes.
716//----------------------------------------------------------------------
717void
Greg Clayton944b8282011-08-22 22:30:57 +0000718DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +0000719{
Greg Clayton944b8282011-08-22 22:30:57 +0000720 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +0000721 switch (state)
722 {
723 case eStateConnected:
724 case eStateAttaching:
725 case eStateLaunching:
726 case eStateInvalid:
727 case eStateUnloaded:
728 case eStateExited:
729 case eStateDetached:
730 Clear(false);
731 break;
732
733 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +0000734 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000735 break;
736
737 case eStateRunning:
738 case eStateStepping:
739 case eStateCrashed:
740 case eStateSuspended:
741 break;
742
743 default:
744 break;
745 }
746}
747
748ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +0000749DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +0000750{
751 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +0000752 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
753 if (log)
754 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +0000755 return thread_plan_sp;
756}
757
758Error
Greg Clayton944b8282011-08-22 22:30:57 +0000759DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +0000760{
761 Error error;
762 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
763 return error;
764}
765
766void
Greg Clayton944b8282011-08-22 22:30:57 +0000767DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +0000768{
769 PluginManager::RegisterPlugin (GetPluginNameStatic(),
770 GetPluginDescriptionStatic(),
771 CreateInstance);
772}
773
774void
Greg Clayton944b8282011-08-22 22:30:57 +0000775DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +0000776{
777 PluginManager::UnregisterPlugin (CreateInstance);
778}
779
780
781const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000782DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000783{
784 return "dynamic-loader.macosx-kernel";
785}
786
787const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000788DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000789{
790 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
791}
792
793
794//------------------------------------------------------------------
795// PluginInterface protocol
796//------------------------------------------------------------------
797const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000798DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000799{
Greg Clayton944b8282011-08-22 22:30:57 +0000800 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +0000801}
802
803const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000804DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000805{
806 return GetPluginNameStatic();
807}
808
809uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000810DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +0000811{
812 return 1;
813}
814