blob: dde33c390cd7e10bbe8a85523750ebf6705b9c28 [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)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000193 {
194 ModuleSpec module_spec (FileSpec(), target.GetArchitecture());
195 module_spec.GetUUID() = uuid;
196 module_sp = target.GetSharedModule (module_spec);
197 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000198 }
199 }
200
201
202 if (memory_module_sp)
203 {
204 // Someone already supplied a file, make sure it is the right one.
205 if (module_sp)
206 {
207 if (module_sp->GetUUID() == memory_module_sp->GetUUID())
208 {
209 ObjectFile *ondisk_object_file = module_sp->GetObjectFile();
210 ObjectFile *memory_object_file = memory_module_sp->GetObjectFile();
211 if (memory_object_file && ondisk_object_file)
212 {
213 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
214 SectionList *memory_section_list = memory_object_file->GetSectionList ();
215 if (memory_section_list && ondisk_section_list)
216 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000217 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000218 // There may be CTF sections in the memory image so we can't
219 // always just compare the number of sections (which are actually
220 // segments in mach-o parlance)
221 uint32_t sect_idx = 0;
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000222
223
224 // We now iterate through all sections in the file module
225 // and look to see if the memory module has a load address
226 // for that section.
227 uint32_t num_sections_loaded = 0;
228 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000229 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000230 const Section *ondisk_section = ondisk_section_list->GetSectionAtIndex(sect_idx).get();
231 if (ondisk_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000232 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000233 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section->GetName()).get();
234 if (memory_section)
235 {
236 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section, memory_section->GetFileAddress());
237 ++num_sections_loaded;
238 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000239 }
240 }
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000241 if (num_sections_loaded > 0)
242 load_process_stop_id = process->GetStopID();
243 else
244 module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000245 }
246 else
247 module_sp.reset(); // One or both section lists
248 }
249 else
250 module_sp.reset(); // One or both object files missing
251 }
252 else
253 module_sp.reset(); // UUID mismatch
254 }
255
256 // Use the memory module as the module if we didn't like the file
257 // module we either found or were supplied with
258 if (!module_sp)
259 {
260 module_sp = memory_module_sp;
261 // Load the memory image in the target as all adresses are already correct
262 bool changed = false;
263 target.GetImages().Append (memory_module_sp);
264 if (module_sp->SetLoadAddress (target, 0, changed))
265 load_process_stop_id = process->GetStopID();
266 }
267 }
268 bool is_loaded = IsLoaded();
269
270 if (so_address.IsValid())
271 {
272 if (is_loaded)
273 so_address.SetLoadAddress (address, &target);
274 else
275 target.GetImages().ResolveFileAddress (address, so_address);
276
277 }
278 return is_loaded;
279}
280
Greg Clayton7b242382011-07-08 00:48:09 +0000281//----------------------------------------------------------------------
282// Load the kernel module and initialize the "m_kernel" member. Return
283// true _only_ if the kernel is loaded the first time through (subsequent
284// calls to this function should return false after the kernel has been
285// already loaded).
286//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000287void
Greg Clayton944b8282011-08-22 22:30:57 +0000288DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000289{
Greg Claytond16e1e52011-07-12 17:06:17 +0000290 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000291 {
292 m_kernel.Clear(false);
293 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000294 strncpy(m_kernel.name, "mach_kernel", sizeof(m_kernel.name));
295 if (m_kernel.address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +0000296 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000297 m_kernel.address = m_process->GetImageInfoAddress ();
298 if (m_kernel.address == LLDB_INVALID_ADDRESS && m_kernel.module_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000299 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000300 // We didn't get a hint from the process, so we will
301 // try the kernel at the address that it exists at in
302 // the file if we have one
303 ObjectFile *kernel_object_file = m_kernel.module_sp->GetObjectFile();
304 if (kernel_object_file)
305 m_kernel.address = kernel_object_file->GetHeaderAddress().GetFileAddress();
Greg Clayton7b242382011-07-08 00:48:09 +0000306 }
307 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000308
309 if (m_kernel.address != LLDB_INVALID_ADDRESS)
310 m_kernel.LoadImageUsingMemoryModule (m_process);
Greg Clayton7b242382011-07-08 00:48:09 +0000311
Greg Claytonc859e2d2012-02-13 23:10:39 +0000312 if (m_kernel.IsLoaded())
Greg Clayton7b242382011-07-08 00:48:09 +0000313 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000314 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
315 const Symbol *symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
316 if (symbol)
317 {
Greg Claytone7612132012-03-07 21:03:09 +0000318 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000319 // Update all image infos
320 ReadAllKextSummaries ();
321 }
Greg Clayton7b242382011-07-08 00:48:09 +0000322 }
323 else
Greg Clayton7b242382011-07-08 00:48:09 +0000324 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000325 m_kernel.Clear(false);
Greg Clayton7b242382011-07-08 00:48:09 +0000326 }
327 }
Greg Clayton7b242382011-07-08 00:48:09 +0000328}
329
Greg Clayton7b242382011-07-08 00:48:09 +0000330//----------------------------------------------------------------------
331// Static callback function that gets called when our DYLD notification
332// breakpoint gets hit. We update all of our image infos and then
333// let our super class DynamicLoader class decide if we should stop
334// or not (based on global preference).
335//----------------------------------------------------------------------
336bool
Greg Clayton944b8282011-08-22 22:30:57 +0000337DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +0000338 StoppointCallbackContext *context,
339 user_id_t break_id,
340 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000341{
Greg Clayton944b8282011-08-22 22:30:57 +0000342 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000343}
344
345bool
Greg Clayton944b8282011-08-22 22:30:57 +0000346DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +0000347 user_id_t break_id,
348 user_id_t break_loc_id)
349{
Greg Claytond16e1e52011-07-12 17:06:17 +0000350 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
351 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000352 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +0000353
Greg Clayton374972e2011-07-09 17:15:55 +0000354 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000355
356 if (log)
357 PutToLog(log.get());
358
Greg Clayton374972e2011-07-09 17:15:55 +0000359 return GetStopWhenImagesChange();
360}
361
362
363bool
Greg Clayton944b8282011-08-22 22:30:57 +0000364DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +0000365{
366 Mutex::Locker locker(m_mutex);
367
368 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000369
370 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000371 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000372 {
373 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
374 const ByteOrder byte_order = m_kernel.GetByteOrder();
375 Error error;
376 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
377 // which is currenty 4 uint32_t and a pointer.
378 uint8_t buf[24];
379 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
380 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000381 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000382 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
383 prefer_file_cache,
384 error,
385 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000386 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000387 // We got a valid address for our kext summary header and make sure it isn't NULL
388 if (m_kext_summary_header_addr.IsValid() &&
389 m_kext_summary_header_addr.GetFileAddress() != 0)
390 {
391 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
392 if (bytes_read == count)
393 {
394 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000395 m_kext_summary_header.version = data.GetU32(&offset);
396 if (m_kext_summary_header.version >= 2)
397 {
398 m_kext_summary_header.entry_size = data.GetU32(&offset);
399 }
400 else
401 {
402 // Versions less than 2 didn't have an entry size, it was hard coded
403 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
404 }
405 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000406 return true;
407 }
408 }
Greg Clayton7b242382011-07-08 00:48:09 +0000409 }
410 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000411 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000412 return false;
413}
414
415
416bool
Greg Clayton944b8282011-08-22 22:30:57 +0000417DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000418 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000419{
420 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000421 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000422 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000423 log->Printf ("Adding %d modules.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000424
425 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000426
427 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
428 return false;
429
Greg Clayton07e66e32011-07-20 03:41:06 +0000430 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Greg Clayton7b242382011-07-08 00:48:09 +0000431 for (uint32_t i = 0; i < count; i++)
432 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000433 if (s)
434 {
435 const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
436 if (u)
437 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000438 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 +0000439 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
440 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
441 kext_summaries[i].address, kext_summaries[i].name);
442 }
443 else
444 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000445 s->Printf("0x%16.16llx \"%s\"...", kext_summaries[i].address, kext_summaries[i].name);
Greg Clayton07e66e32011-07-20 03:41:06 +0000446 }
447 }
448
Greg Claytonc859e2d2012-02-13 23:10:39 +0000449 kext_summaries[i].LoadImageUsingMemoryModule (m_process);
450
Greg Clayton5b882162011-07-21 01:12:01 +0000451 if (s)
452 {
453 if (kext_summaries[i].module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000454 {
455 if (kext_summaries[i].module_sp->GetFileSpec().GetDirectory())
456 s->Printf("\n found kext: %s/%s\n",
457 kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
458 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
459 else
460 s->Printf("\n found kext: %s\n",
461 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
462 }
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000463 else
464 s->Printf (" failed to locate/load.\n");
Greg Clayton5b882162011-07-21 01:12:01 +0000465 }
466
Greg Claytona63d08c2011-07-19 03:57:15 +0000467 if (log)
468 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000469 }
470 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000471 return return_value;
472}
473
474// Adds the modules in image_infos to m_kext_summaries.
475// NB don't call this passing in m_kext_summaries.
476
477bool
Greg Clayton944b8282011-08-22 22:30:57 +0000478DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +0000479{
480 // Now add these images to the main list.
481 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000482
483 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
484 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000485 OSKextLoadedKextSummary &image_info = image_infos[idx];
486 m_kext_summaries.push_back(image_info);
Greg Clayton7b242382011-07-08 00:48:09 +0000487
Greg Claytonc859e2d2012-02-13 23:10:39 +0000488 if (image_info.module_sp && m_process->GetStopID() == image_info.load_process_stop_id)
489 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
Greg Clayton7b242382011-07-08 00:48:09 +0000490 }
491
492 if (loaded_module_list.GetSize() > 0)
493 {
494 // FIXME: This should really be in the Runtime handlers class, which should get
495 // called by the target's ModulesDidLoad, but we're doing it all locally for now
496 // to save time.
497 // Also, I'm assuming there can be only one libobjc dylib loaded...
498
499 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
500 if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary())
501 {
502 size_t num_modules = loaded_module_list.GetSize();
503 for (int i = 0; i < num_modules; i++)
504 {
505 if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i)))
506 {
507 objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i));
508 break;
509 }
510 }
511 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000512// if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000513// loaded_module_list.LogUUIDAndPaths (log, "DynamicLoaderDarwinKernel::ModulesDidLoad");
Greg Clayton7b242382011-07-08 00:48:09 +0000514 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
515 }
516 return true;
517}
518
Greg Clayton7b242382011-07-08 00:48:09 +0000519
520uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000521DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000522 uint32_t image_infos_count,
523 OSKextLoadedKextSummary::collection &image_infos)
524{
525 const ByteOrder endian = m_kernel.GetByteOrder();
526 const uint32_t addr_size = m_kernel.GetAddressByteSize();
527
528 image_infos.resize(image_infos_count);
529 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
530 DataBufferHeap data(count, 0);
531 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000532
533 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
534
535 if (s)
536 s->Printf ("Reading %u kext summaries...\n", image_infos_count);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000537 const bool prefer_file_cache = false;
538 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
539 prefer_file_cache,
540 data.GetBytes(),
541 data.GetByteSize(),
542 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000543 if (bytes_read == count)
544 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000545
Greg Clayton7b242382011-07-08 00:48:09 +0000546 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
547 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000548 for (uint32_t kext_summary_offset = 0;
549 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
550 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000551 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000552 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000553 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
554 if (name_data == NULL)
555 break;
556 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
557 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
558 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000559 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
560 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000561 image_infos[i].size = extractor.GetU64(&offset);
562 image_infos[i].version = extractor.GetU64(&offset);
563 image_infos[i].load_tag = extractor.GetU32(&offset);
564 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000565 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
566 {
567 image_infos[i].reference_list = extractor.GetU64(&offset);
568 }
569 else
570 {
571 image_infos[i].reference_list = 0;
572 }
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000573// 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",
574// i,
575// KERNEL_MODULE_MAX_NAME, KERNEL_MODULE_MAX_NAME, (char *)name_data,
576// image_infos[i].address,
577// image_infos[i].size,
578// image_infos[i].version,
579// image_infos[i].load_tag,
580// image_infos[i].flags);
Greg Clayton7b242382011-07-08 00:48:09 +0000581 }
582 if (i < image_infos.size())
583 image_infos.resize(i);
584 }
585 else
586 {
587 image_infos.clear();
588 }
589 return image_infos.size();
590}
591
592bool
Greg Clayton944b8282011-08-22 22:30:57 +0000593DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000594{
Greg Clayton374972e2011-07-09 17:15:55 +0000595 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000596
597 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000598
Greg Clayton7b242382011-07-08 00:48:09 +0000599 if (ReadKextSummaryHeader ())
600 {
Greg Clayton374972e2011-07-09 17:15:55 +0000601 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000602 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000603 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000604 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000605 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000606 {
Greg Clayton7b242382011-07-08 00:48:09 +0000607 m_kext_summaries.clear();
608 }
609 return true;
610 }
611 }
612 return false;
613}
614
615//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000616// Dump an image info structure to the file handle provided.
617//----------------------------------------------------------------------
618void
Greg Clayton944b8282011-08-22 22:30:57 +0000619DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000620{
621 if (log == NULL)
622 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000623 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000624
625 if (address == LLDB_INVALID_ADDRESS)
626 {
627 if (u)
628 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000629 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 +0000630 u[ 0], u[ 1], u[ 2], u[ 3],
631 u[ 4], u[ 5], u[ 6], u[ 7],
632 u[ 8], u[ 9], u[10], u[11],
633 u[12], u[13], u[14], u[15],
634 name);
635 }
636 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000637 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000638 }
639 else
640 {
641 if (u)
642 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000643 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\"",
644 address, size, version, load_tag, flags, reference_list,
645 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
646 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000647 name);
648 }
649 else
650 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000651 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\"",
652 address, address+size, version, load_tag, flags, reference_list,
653 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000654 }
Greg Clayton7b242382011-07-08 00:48:09 +0000655 }
656}
657
658//----------------------------------------------------------------------
659// Dump the _dyld_all_image_infos members and all current image infos
660// that we have parsed to the file handle provided.
661//----------------------------------------------------------------------
662void
Greg Clayton944b8282011-08-22 22:30:57 +0000663DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000664{
665 if (log == NULL)
666 return;
667
668 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000669 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000670 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000671 m_kext_summary_header.version,
672 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000673 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000674
675 size_t i;
676 const size_t count = m_kext_summaries.size();
677 if (count > 0)
678 {
679 log->PutCString("Loaded:");
680 for (i = 0; i<count; i++)
681 m_kext_summaries[i].PutToLog(log);
682 }
683}
684
685void
Greg Clayton944b8282011-08-22 22:30:57 +0000686DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +0000687{
Greg Clayton944b8282011-08-22 22:30:57 +0000688 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +0000689 Clear(true);
690 m_process = process;
691 m_process->GetTarget().GetSectionLoadList().Clear();
692}
693
Greg Clayton374972e2011-07-09 17:15:55 +0000694void
Greg Clayton944b8282011-08-22 22:30:57 +0000695DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000696{
Greg Claytonc859e2d2012-02-13 23:10:39 +0000697 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.module_sp)
Greg Clayton374972e2011-07-09 17:15:55 +0000698 {
Greg Clayton944b8282011-08-22 22:30:57 +0000699 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +0000700
Greg Claytond16e1e52011-07-12 17:06:17 +0000701
Greg Clayton374972e2011-07-09 17:15:55 +0000702 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000703 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +0000704 FileSpecList module_spec_list;
705 module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
706 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +0000707 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +0000708 "OSKextLoadedKextSummariesUpdated",
709 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000710 internal_bp,
711 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000712
Greg Clayton944b8282011-08-22 22:30:57 +0000713 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +0000714 m_break_id = bp->GetID();
715 }
Greg Clayton7b242382011-07-08 00:48:09 +0000716}
717
718//----------------------------------------------------------------------
719// Member function that gets called when the process state changes.
720//----------------------------------------------------------------------
721void
Greg Clayton944b8282011-08-22 22:30:57 +0000722DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +0000723{
Greg Clayton944b8282011-08-22 22:30:57 +0000724 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +0000725 switch (state)
726 {
727 case eStateConnected:
728 case eStateAttaching:
729 case eStateLaunching:
730 case eStateInvalid:
731 case eStateUnloaded:
732 case eStateExited:
733 case eStateDetached:
734 Clear(false);
735 break;
736
737 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +0000738 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000739 break;
740
741 case eStateRunning:
742 case eStateStepping:
743 case eStateCrashed:
744 case eStateSuspended:
745 break;
746
747 default:
748 break;
749 }
750}
751
752ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +0000753DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +0000754{
755 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +0000756 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
757 if (log)
758 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +0000759 return thread_plan_sp;
760}
761
762Error
Greg Clayton944b8282011-08-22 22:30:57 +0000763DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +0000764{
765 Error error;
766 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
767 return error;
768}
769
770void
Greg Clayton944b8282011-08-22 22:30:57 +0000771DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +0000772{
773 PluginManager::RegisterPlugin (GetPluginNameStatic(),
774 GetPluginDescriptionStatic(),
775 CreateInstance);
776}
777
778void
Greg Clayton944b8282011-08-22 22:30:57 +0000779DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +0000780{
781 PluginManager::UnregisterPlugin (CreateInstance);
782}
783
784
785const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000786DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000787{
788 return "dynamic-loader.macosx-kernel";
789}
790
791const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000792DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000793{
794 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
795}
796
797
798//------------------------------------------------------------------
799// PluginInterface protocol
800//------------------------------------------------------------------
801const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000802DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000803{
Greg Clayton944b8282011-08-22 22:30:57 +0000804 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +0000805}
806
807const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000808DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000809{
810 return GetPluginNameStatic();
811}
812
813uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000814DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +0000815{
816 return 1;
817}
818