blob: c719654998ca5c9e67c2532e957f57ee6894a464 [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 {
213 const uint32_t num_sections = ondisk_section_list->GetSize();
214 // 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;
218 const Section *memory_section;
219 const Section *ondisk_section;
220 // Always use the number of sections from the on disk file
221 // in case there are extra sections added to the memory image.
222 for (sect_idx=0; sect_idx<num_sections; ++sect_idx)
223 {
224 memory_section = memory_section_list->GetSectionAtIndex(sect_idx).get();
225 ondisk_section = ondisk_section_list->GetSectionAtIndex(sect_idx).get();
226 if (memory_section->GetName() != ondisk_section->GetName())
227 {
228 // Section count was the same, but the sections themselves do not match
229 module_sp.reset();
230 break;
231 }
232 }
233 if (module_sp)
234 {
235 for (sect_idx=0; sect_idx<num_sections; ++sect_idx)
236 {
237 memory_section = memory_section_list->GetSectionAtIndex(sect_idx).get();
238 ondisk_section = ondisk_section_list->GetSectionAtIndex(sect_idx).get();
239 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section, memory_section->GetFileAddress());
240 }
241 if (num_sections > 0)
242 load_process_stop_id = process->GetStopID();
243 }
244 }
245 else
246 module_sp.reset(); // One or both section lists
247 }
248 else
249 module_sp.reset(); // One or both object files missing
250 }
251 else
252 module_sp.reset(); // UUID mismatch
253 }
254
255 // Use the memory module as the module if we didn't like the file
256 // module we either found or were supplied with
257 if (!module_sp)
258 {
259 module_sp = memory_module_sp;
260 // Load the memory image in the target as all adresses are already correct
261 bool changed = false;
262 target.GetImages().Append (memory_module_sp);
263 if (module_sp->SetLoadAddress (target, 0, changed))
264 load_process_stop_id = process->GetStopID();
265 }
266 }
267 bool is_loaded = IsLoaded();
268
269 if (so_address.IsValid())
270 {
271 if (is_loaded)
272 so_address.SetLoadAddress (address, &target);
273 else
274 target.GetImages().ResolveFileAddress (address, so_address);
275
276 }
277 return is_loaded;
278}
279
Greg Clayton7b242382011-07-08 00:48:09 +0000280//----------------------------------------------------------------------
281// Load the kernel module and initialize the "m_kernel" member. Return
282// true _only_ if the kernel is loaded the first time through (subsequent
283// calls to this function should return false after the kernel has been
284// already loaded).
285//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000286void
Greg Clayton944b8282011-08-22 22:30:57 +0000287DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000288{
Greg Claytond16e1e52011-07-12 17:06:17 +0000289 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000290 {
291 m_kernel.Clear(false);
292 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000293 strncpy(m_kernel.name, "mach_kernel", sizeof(m_kernel.name));
294 if (m_kernel.address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +0000295 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000296 m_kernel.address = m_process->GetImageInfoAddress ();
297 if (m_kernel.address == LLDB_INVALID_ADDRESS && m_kernel.module_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000298 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000299 // We didn't get a hint from the process, so we will
300 // try the kernel at the address that it exists at in
301 // the file if we have one
302 ObjectFile *kernel_object_file = m_kernel.module_sp->GetObjectFile();
303 if (kernel_object_file)
304 m_kernel.address = kernel_object_file->GetHeaderAddress().GetFileAddress();
Greg Clayton7b242382011-07-08 00:48:09 +0000305 }
306 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000307
308 if (m_kernel.address != LLDB_INVALID_ADDRESS)
309 m_kernel.LoadImageUsingMemoryModule (m_process);
Greg Clayton7b242382011-07-08 00:48:09 +0000310
Greg Claytonc859e2d2012-02-13 23:10:39 +0000311 if (m_kernel.IsLoaded())
Greg Clayton7b242382011-07-08 00:48:09 +0000312 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000313 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
314 const Symbol *symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
315 if (symbol)
316 {
317 m_kext_summary_header_ptr_addr = symbol->GetValue();
318 // Update all image infos
319 ReadAllKextSummaries ();
320 }
Greg Clayton7b242382011-07-08 00:48:09 +0000321 }
322 else
Greg Clayton7b242382011-07-08 00:48:09 +0000323 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000324 m_kernel.Clear(false);
Greg Clayton7b242382011-07-08 00:48:09 +0000325 }
326 }
Greg Clayton7b242382011-07-08 00:48:09 +0000327}
328
Greg Clayton7b242382011-07-08 00:48:09 +0000329//----------------------------------------------------------------------
330// Static callback function that gets called when our DYLD notification
331// breakpoint gets hit. We update all of our image infos and then
332// let our super class DynamicLoader class decide if we should stop
333// or not (based on global preference).
334//----------------------------------------------------------------------
335bool
Greg Clayton944b8282011-08-22 22:30:57 +0000336DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +0000337 StoppointCallbackContext *context,
338 user_id_t break_id,
339 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000340{
Greg Clayton944b8282011-08-22 22:30:57 +0000341 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000342}
343
344bool
Greg Clayton944b8282011-08-22 22:30:57 +0000345DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +0000346 user_id_t break_id,
347 user_id_t break_loc_id)
348{
Greg Claytond16e1e52011-07-12 17:06:17 +0000349 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
350 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000351 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +0000352
Greg Clayton374972e2011-07-09 17:15:55 +0000353 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000354
355 if (log)
356 PutToLog(log.get());
357
Greg Clayton374972e2011-07-09 17:15:55 +0000358 return GetStopWhenImagesChange();
359}
360
361
362bool
Greg Clayton944b8282011-08-22 22:30:57 +0000363DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +0000364{
365 Mutex::Locker locker(m_mutex);
366
367 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000368
369 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000370 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000371 {
372 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
373 const ByteOrder byte_order = m_kernel.GetByteOrder();
374 Error error;
375 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
376 // which is currenty 4 uint32_t and a pointer.
377 uint8_t buf[24];
378 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
379 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000380 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000381 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
382 prefer_file_cache,
383 error,
384 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000385 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000386 // We got a valid address for our kext summary header and make sure it isn't NULL
387 if (m_kext_summary_header_addr.IsValid() &&
388 m_kext_summary_header_addr.GetFileAddress() != 0)
389 {
390 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
391 if (bytes_read == count)
392 {
393 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000394 m_kext_summary_header.version = data.GetU32(&offset);
395 if (m_kext_summary_header.version >= 2)
396 {
397 m_kext_summary_header.entry_size = data.GetU32(&offset);
398 }
399 else
400 {
401 // Versions less than 2 didn't have an entry size, it was hard coded
402 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
403 }
404 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000405 return true;
406 }
407 }
Greg Clayton7b242382011-07-08 00:48:09 +0000408 }
409 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000410 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000411 return false;
412}
413
414
415bool
Greg Clayton944b8282011-08-22 22:30:57 +0000416DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000417 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000418{
419 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000420 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000421 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000422 log->Printf ("Adding %d modules.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000423
424 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000425
426 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
427 return false;
428
Greg Clayton07e66e32011-07-20 03:41:06 +0000429 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Greg Clayton7b242382011-07-08 00:48:09 +0000430 for (uint32_t i = 0; i < count; i++)
431 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000432 if (s)
433 {
434 const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
435 if (u)
436 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000437 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 +0000438 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
439 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
440 kext_summaries[i].address, kext_summaries[i].name);
441 }
442 else
443 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000444 s->Printf("0x%16.16llx \"%s\"...", kext_summaries[i].address, kext_summaries[i].name);
Greg Clayton07e66e32011-07-20 03:41:06 +0000445 }
446 }
447
Greg Claytonc859e2d2012-02-13 23:10:39 +0000448 kext_summaries[i].LoadImageUsingMemoryModule (m_process);
449
Greg Clayton5b882162011-07-21 01:12:01 +0000450 if (s)
451 {
452 if (kext_summaries[i].module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000453 {
454 if (kext_summaries[i].module_sp->GetFileSpec().GetDirectory())
455 s->Printf("\n found kext: %s/%s\n",
456 kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
457 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
458 else
459 s->Printf("\n found kext: %s\n",
460 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
461 }
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000462 else
463 s->Printf (" failed to locate/load.\n");
Greg Clayton5b882162011-07-21 01:12:01 +0000464 }
465
Greg Claytona63d08c2011-07-19 03:57:15 +0000466 if (log)
467 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000468 }
469 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000470 return return_value;
471}
472
473// Adds the modules in image_infos to m_kext_summaries.
474// NB don't call this passing in m_kext_summaries.
475
476bool
Greg Clayton944b8282011-08-22 22:30:57 +0000477DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +0000478{
479 // Now add these images to the main list.
480 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000481
482 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
483 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000484 OSKextLoadedKextSummary &image_info = image_infos[idx];
485 m_kext_summaries.push_back(image_info);
Greg Clayton7b242382011-07-08 00:48:09 +0000486
Greg Claytonc859e2d2012-02-13 23:10:39 +0000487 if (image_info.module_sp && m_process->GetStopID() == image_info.load_process_stop_id)
488 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
Greg Clayton7b242382011-07-08 00:48:09 +0000489 }
490
491 if (loaded_module_list.GetSize() > 0)
492 {
493 // FIXME: This should really be in the Runtime handlers class, which should get
494 // called by the target's ModulesDidLoad, but we're doing it all locally for now
495 // to save time.
496 // Also, I'm assuming there can be only one libobjc dylib loaded...
497
498 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
499 if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary())
500 {
501 size_t num_modules = loaded_module_list.GetSize();
502 for (int i = 0; i < num_modules; i++)
503 {
504 if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i)))
505 {
506 objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i));
507 break;
508 }
509 }
510 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000511// if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000512// loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderDarwinKernel::ModulesDidLoad");
Greg Clayton7b242382011-07-08 00:48:09 +0000513 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
514 }
515 return true;
516}
517
Greg Clayton7b242382011-07-08 00:48:09 +0000518
519uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000520DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000521 uint32_t image_infos_count,
522 OSKextLoadedKextSummary::collection &image_infos)
523{
524 const ByteOrder endian = m_kernel.GetByteOrder();
525 const uint32_t addr_size = m_kernel.GetAddressByteSize();
526
527 image_infos.resize(image_infos_count);
528 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
529 DataBufferHeap data(count, 0);
530 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000531
532 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
533
534 if (s)
535 s->Printf ("Reading %u kext summaries...\n", image_infos_count);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000536 const bool prefer_file_cache = false;
537 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
538 prefer_file_cache,
539 data.GetBytes(),
540 data.GetByteSize(),
541 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000542 if (bytes_read == count)
543 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000544
Greg Clayton7b242382011-07-08 00:48:09 +0000545 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
546 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000547 for (uint32_t kext_summary_offset = 0;
548 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
549 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000550 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000551 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000552 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
553 if (name_data == NULL)
554 break;
555 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
556 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
557 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000558 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
559 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000560 image_infos[i].size = extractor.GetU64(&offset);
561 image_infos[i].version = extractor.GetU64(&offset);
562 image_infos[i].load_tag = extractor.GetU32(&offset);
563 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000564 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
565 {
566 image_infos[i].reference_list = extractor.GetU64(&offset);
567 }
568 else
569 {
570 image_infos[i].reference_list = 0;
571 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000572 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",
573 i,
574 KERNEL_MODULE_MAX_NAME, KERNEL_MODULE_MAX_NAME, (char *)name_data,
575 image_infos[i].address,
576 image_infos[i].size,
577 image_infos[i].version,
578 image_infos[i].load_tag,
579 image_infos[i].flags);
Greg Clayton7b242382011-07-08 00:48:09 +0000580 }
581 if (i < image_infos.size())
582 image_infos.resize(i);
583 }
584 else
585 {
586 image_infos.clear();
587 }
588 return image_infos.size();
589}
590
591bool
Greg Clayton944b8282011-08-22 22:30:57 +0000592DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000593{
Greg Clayton374972e2011-07-09 17:15:55 +0000594 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000595
596 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000597
Greg Clayton7b242382011-07-08 00:48:09 +0000598 if (ReadKextSummaryHeader ())
599 {
Greg Clayton374972e2011-07-09 17:15:55 +0000600 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000601 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000602 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000603 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000604 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000605 {
Greg Clayton7b242382011-07-08 00:48:09 +0000606 m_kext_summaries.clear();
607 }
608 return true;
609 }
610 }
611 return false;
612}
613
614//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000615// Dump an image info structure to the file handle provided.
616//----------------------------------------------------------------------
617void
Greg Clayton944b8282011-08-22 22:30:57 +0000618DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000619{
620 if (log == NULL)
621 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000622 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000623
624 if (address == LLDB_INVALID_ADDRESS)
625 {
626 if (u)
627 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000628 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 +0000629 u[ 0], u[ 1], u[ 2], u[ 3],
630 u[ 4], u[ 5], u[ 6], u[ 7],
631 u[ 8], u[ 9], u[10], u[11],
632 u[12], u[13], u[14], u[15],
633 name);
634 }
635 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000636 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000637 }
638 else
639 {
640 if (u)
641 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000642 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\"",
643 address, size, version, load_tag, flags, reference_list,
644 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
645 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000646 name);
647 }
648 else
649 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000650 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\"",
651 address, address+size, version, load_tag, flags, reference_list,
652 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000653 }
Greg Clayton7b242382011-07-08 00:48:09 +0000654 }
655}
656
657//----------------------------------------------------------------------
658// Dump the _dyld_all_image_infos members and all current image infos
659// that we have parsed to the file handle provided.
660//----------------------------------------------------------------------
661void
Greg Clayton944b8282011-08-22 22:30:57 +0000662DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000663{
664 if (log == NULL)
665 return;
666
667 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000668 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000669 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000670 m_kext_summary_header.version,
671 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000672 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000673
674 size_t i;
675 const size_t count = m_kext_summaries.size();
676 if (count > 0)
677 {
678 log->PutCString("Loaded:");
679 for (i = 0; i<count; i++)
680 m_kext_summaries[i].PutToLog(log);
681 }
682}
683
684void
Greg Clayton944b8282011-08-22 22:30:57 +0000685DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +0000686{
Greg Clayton944b8282011-08-22 22:30:57 +0000687 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +0000688 Clear(true);
689 m_process = process;
690 m_process->GetTarget().GetSectionLoadList().Clear();
691}
692
Greg Clayton374972e2011-07-09 17:15:55 +0000693void
Greg Clayton944b8282011-08-22 22:30:57 +0000694DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000695{
Greg Claytonc859e2d2012-02-13 23:10:39 +0000696 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.module_sp)
Greg Clayton374972e2011-07-09 17:15:55 +0000697 {
Greg Clayton944b8282011-08-22 22:30:57 +0000698 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +0000699
Greg Claytond16e1e52011-07-12 17:06:17 +0000700
Greg Clayton374972e2011-07-09 17:15:55 +0000701 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000702 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +0000703 FileSpecList module_spec_list;
704 module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
705 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +0000706 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +0000707 "OSKextLoadedKextSummariesUpdated",
708 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000709 internal_bp,
710 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000711
Greg Clayton944b8282011-08-22 22:30:57 +0000712 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +0000713 m_break_id = bp->GetID();
714 }
Greg Clayton7b242382011-07-08 00:48:09 +0000715}
716
717//----------------------------------------------------------------------
718// Member function that gets called when the process state changes.
719//----------------------------------------------------------------------
720void
Greg Clayton944b8282011-08-22 22:30:57 +0000721DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +0000722{
Greg Clayton944b8282011-08-22 22:30:57 +0000723 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +0000724 switch (state)
725 {
726 case eStateConnected:
727 case eStateAttaching:
728 case eStateLaunching:
729 case eStateInvalid:
730 case eStateUnloaded:
731 case eStateExited:
732 case eStateDetached:
733 Clear(false);
734 break;
735
736 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +0000737 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000738 break;
739
740 case eStateRunning:
741 case eStateStepping:
742 case eStateCrashed:
743 case eStateSuspended:
744 break;
745
746 default:
747 break;
748 }
749}
750
751ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +0000752DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +0000753{
754 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +0000755 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
756 if (log)
757 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +0000758 return thread_plan_sp;
759}
760
761Error
Greg Clayton944b8282011-08-22 22:30:57 +0000762DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +0000763{
764 Error error;
765 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
766 return error;
767}
768
769void
Greg Clayton944b8282011-08-22 22:30:57 +0000770DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +0000771{
772 PluginManager::RegisterPlugin (GetPluginNameStatic(),
773 GetPluginDescriptionStatic(),
774 CreateInstance);
775}
776
777void
Greg Clayton944b8282011-08-22 22:30:57 +0000778DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +0000779{
780 PluginManager::UnregisterPlugin (CreateInstance);
781}
782
783
784const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000785DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000786{
787 return "dynamic-loader.macosx-kernel";
788}
789
790const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000791DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000792{
793 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
794}
795
796
797//------------------------------------------------------------------
798// PluginInterface protocol
799//------------------------------------------------------------------
800const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000801DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000802{
Greg Clayton944b8282011-08-22 22:30:57 +0000803 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +0000804}
805
806const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000807DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000808{
809 return GetPluginNameStatic();
810}
811
812uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000813DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +0000814{
815 return 1;
816}
817