blob: b2df3105453533a514be30f81f33bfa4130be7af [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();
Greg Clayton70512312012-05-08 01:45:38 +000068 switch (triple_ref.getOS())
69 {
70 case llvm::Triple::Darwin:
71 case llvm::Triple::MacOSX:
72 case llvm::Triple::IOS:
73 create = triple_ref.getVendor() == llvm::Triple::Apple;
74 break;
75 default:
76 create = false;
77 break;
78 }
Greg Claytondf0b7d52011-07-08 04:11:42 +000079 }
Greg Clayton7b242382011-07-08 00:48:09 +000080 }
81
82 if (create)
Sean Callanan90539452011-09-20 23:01:51 +000083 {
84 process->SetCanJIT(false);
Greg Clayton944b8282011-08-22 22:30:57 +000085 return new DynamicLoaderDarwinKernel (process);
Sean Callanan90539452011-09-20 23:01:51 +000086 }
Greg Clayton7b242382011-07-08 00:48:09 +000087 return NULL;
88}
89
90//----------------------------------------------------------------------
91// Constructor
92//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +000093DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel (Process* process) :
Greg Clayton7b242382011-07-08 00:48:09 +000094 DynamicLoader(process),
95 m_kernel(),
Greg Claytond16e1e52011-07-12 17:06:17 +000096 m_kext_summary_header_ptr_addr (),
Greg Clayton0d9fc762011-07-08 03:21:57 +000097 m_kext_summary_header_addr (),
Greg Clayton7b242382011-07-08 00:48:09 +000098 m_kext_summary_header (),
Greg Clayton7b242382011-07-08 00:48:09 +000099 m_kext_summaries(),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000100 m_mutex(Mutex::eMutexTypeRecursive),
101 m_break_id (LLDB_INVALID_BREAK_ID)
Greg Clayton7b242382011-07-08 00:48:09 +0000102{
103}
104
105//----------------------------------------------------------------------
106// Destructor
107//----------------------------------------------------------------------
Greg Clayton944b8282011-08-22 22:30:57 +0000108DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel()
Greg Clayton7b242382011-07-08 00:48:09 +0000109{
110 Clear(true);
111}
112
Greg Clayton374972e2011-07-09 17:15:55 +0000113void
Greg Clayton944b8282011-08-22 22:30:57 +0000114DynamicLoaderDarwinKernel::UpdateIfNeeded()
Greg Clayton374972e2011-07-09 17:15:55 +0000115{
116 LoadKernelModuleIfNeeded();
117 SetNotificationBreakpointIfNeeded ();
118}
Greg Clayton7b242382011-07-08 00:48:09 +0000119//------------------------------------------------------------------
120/// Called after attaching a process.
121///
122/// Allow DynamicLoader plug-ins to execute some code after
123/// attaching to a process.
124//------------------------------------------------------------------
125void
Greg Clayton944b8282011-08-22 22:30:57 +0000126DynamicLoaderDarwinKernel::DidAttach ()
Greg Clayton7b242382011-07-08 00:48:09 +0000127{
128 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000129 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000130}
131
132//------------------------------------------------------------------
133/// Called after attaching a process.
134///
135/// Allow DynamicLoader plug-ins to execute some code after
136/// attaching to a process.
137//------------------------------------------------------------------
138void
Greg Clayton944b8282011-08-22 22:30:57 +0000139DynamicLoaderDarwinKernel::DidLaunch ()
Greg Clayton7b242382011-07-08 00:48:09 +0000140{
141 PrivateInitialize(m_process);
Greg Clayton374972e2011-07-09 17:15:55 +0000142 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000143}
144
145
146//----------------------------------------------------------------------
147// Clear out the state of this class.
148//----------------------------------------------------------------------
149void
Greg Clayton944b8282011-08-22 22:30:57 +0000150DynamicLoaderDarwinKernel::Clear (bool clear_process)
Greg Clayton7b242382011-07-08 00:48:09 +0000151{
152 Mutex::Locker locker(m_mutex);
153
154 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
155 m_process->ClearBreakpointSiteByID(m_break_id);
156
157 if (clear_process)
158 m_process = NULL;
159 m_kernel.Clear(false);
Greg Claytond16e1e52011-07-12 17:06:17 +0000160 m_kext_summary_header_ptr_addr.Clear();
Greg Clayton0d9fc762011-07-08 03:21:57 +0000161 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000162 m_kext_summaries.clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000163 m_break_id = LLDB_INVALID_BREAK_ID;
164}
165
Greg Clayton7b242382011-07-08 00:48:09 +0000166
Greg Claytonc859e2d2012-02-13 23:10:39 +0000167bool
Greg Clayton2af282a2012-03-21 04:25:00 +0000168DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageAtFileAddress (Process *process)
169{
170 if (IsLoaded())
171 return true;
172
173 if (module_sp)
174 {
175 bool changed = false;
176 if (module_sp->SetLoadAddress (process->GetTarget(), 0, changed))
177 load_process_stop_id = process->GetStopID();
178 }
179 return false;
180}
181
182bool
Greg Claytonc859e2d2012-02-13 23:10:39 +0000183DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::LoadImageUsingMemoryModule (Process *process)
184{
185 if (IsLoaded())
186 return true;
187
188 bool uuid_is_valid = uuid.IsValid();
189
190 Target &target = process->GetTarget();
191 ModuleSP memory_module_sp;
192 // Use the memory module as the module if we have one...
193 if (address != LLDB_INVALID_ADDRESS)
194 {
195 FileSpec file_spec;
196 if (module_sp)
197 file_spec = module_sp->GetFileSpec();
198 else
199 file_spec.SetFile (name, false);
200
201 memory_module_sp = process->ReadModuleFromMemory (file_spec, address, false, false);
202 if (memory_module_sp && !uuid_is_valid)
203 {
204 uuid = memory_module_sp->GetUUID();
205 uuid_is_valid = uuid.IsValid();
206 }
207 }
208
209 if (!module_sp)
210 {
211 bool uuid_is_valid = uuid.IsValid();
212 if (uuid_is_valid)
213 {
214 ModuleList &target_images = target.GetImages();
215 module_sp = target_images.FindModule(uuid);
216
217 if (!module_sp)
Greg Claytonb9a01b32012-02-26 05:51:37 +0000218 {
Greg Clayton2af282a2012-03-21 04:25:00 +0000219 ModuleSpec module_spec;
Greg Claytonb9a01b32012-02-26 05:51:37 +0000220 module_spec.GetUUID() = uuid;
221 module_sp = target.GetSharedModule (module_spec);
222 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000223 }
224 }
225
226
227 if (memory_module_sp)
228 {
229 // Someone already supplied a file, make sure it is the right one.
230 if (module_sp)
231 {
232 if (module_sp->GetUUID() == memory_module_sp->GetUUID())
233 {
234 ObjectFile *ondisk_object_file = module_sp->GetObjectFile();
235 ObjectFile *memory_object_file = memory_module_sp->GetObjectFile();
236 if (memory_object_file && ondisk_object_file)
237 {
238 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList ();
239 SectionList *memory_section_list = memory_object_file->GetSectionList ();
240 if (memory_section_list && ondisk_section_list)
241 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000242 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000243 // There may be CTF sections in the memory image so we can't
244 // always just compare the number of sections (which are actually
245 // segments in mach-o parlance)
246 uint32_t sect_idx = 0;
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000247
248
249 // We now iterate through all sections in the file module
250 // and look to see if the memory module has a load address
251 // for that section.
252 uint32_t num_sections_loaded = 0;
253 for (sect_idx=0; sect_idx<num_ondisk_sections; ++sect_idx)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000254 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000255 const Section *ondisk_section = ondisk_section_list->GetSectionAtIndex(sect_idx).get();
256 if (ondisk_section)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000257 {
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000258 const Section *memory_section = memory_section_list->FindSectionByName(ondisk_section->GetName()).get();
259 if (memory_section)
260 {
261 target.GetSectionLoadList().SetSectionLoadAddress (ondisk_section, memory_section->GetFileAddress());
262 ++num_sections_loaded;
263 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000264 }
265 }
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000266 if (num_sections_loaded > 0)
267 load_process_stop_id = process->GetStopID();
268 else
269 module_sp.reset(); // No sections were loaded
Greg Claytonc859e2d2012-02-13 23:10:39 +0000270 }
271 else
272 module_sp.reset(); // One or both section lists
273 }
274 else
275 module_sp.reset(); // One or both object files missing
276 }
277 else
278 module_sp.reset(); // UUID mismatch
279 }
280
281 // Use the memory module as the module if we didn't like the file
282 // module we either found or were supplied with
283 if (!module_sp)
284 {
285 module_sp = memory_module_sp;
286 // Load the memory image in the target as all adresses are already correct
287 bool changed = false;
288 target.GetImages().Append (memory_module_sp);
289 if (module_sp->SetLoadAddress (target, 0, changed))
290 load_process_stop_id = process->GetStopID();
291 }
292 }
293 bool is_loaded = IsLoaded();
294
295 if (so_address.IsValid())
296 {
297 if (is_loaded)
298 so_address.SetLoadAddress (address, &target);
299 else
300 target.GetImages().ResolveFileAddress (address, so_address);
301
302 }
303 return is_loaded;
304}
305
Greg Clayton7b242382011-07-08 00:48:09 +0000306//----------------------------------------------------------------------
307// Load the kernel module and initialize the "m_kernel" member. Return
308// true _only_ if the kernel is loaded the first time through (subsequent
309// calls to this function should return false after the kernel has been
310// already loaded).
311//----------------------------------------------------------------------
Greg Clayton374972e2011-07-09 17:15:55 +0000312void
Greg Clayton944b8282011-08-22 22:30:57 +0000313DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded()
Greg Clayton7b242382011-07-08 00:48:09 +0000314{
Greg Claytond16e1e52011-07-12 17:06:17 +0000315 if (!m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000316 {
317 m_kernel.Clear(false);
318 m_kernel.module_sp = m_process->GetTarget().GetExecutableModule();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000319 strncpy(m_kernel.name, "mach_kernel", sizeof(m_kernel.name));
320 if (m_kernel.address == LLDB_INVALID_ADDRESS)
Greg Clayton7b242382011-07-08 00:48:09 +0000321 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000322 m_kernel.address = m_process->GetImageInfoAddress ();
323 if (m_kernel.address == LLDB_INVALID_ADDRESS && m_kernel.module_sp)
Greg Clayton7b242382011-07-08 00:48:09 +0000324 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000325 // We didn't get a hint from the process, so we will
326 // try the kernel at the address that it exists at in
327 // the file if we have one
328 ObjectFile *kernel_object_file = m_kernel.module_sp->GetObjectFile();
329 if (kernel_object_file)
330 m_kernel.address = kernel_object_file->GetHeaderAddress().GetFileAddress();
Greg Clayton7b242382011-07-08 00:48:09 +0000331 }
332 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000333
334 if (m_kernel.address != LLDB_INVALID_ADDRESS)
Greg Clayton2af282a2012-03-21 04:25:00 +0000335 {
336 if (!m_kernel.LoadImageUsingMemoryModule (m_process))
337 {
338 m_kernel.LoadImageAtFileAddress (m_process);
339 }
340 }
Greg Clayton7b242382011-07-08 00:48:09 +0000341
Greg Claytonc859e2d2012-02-13 23:10:39 +0000342 if (m_kernel.IsLoaded())
Greg Clayton7b242382011-07-08 00:48:09 +0000343 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000344 static ConstString kext_summary_symbol ("gLoadedKextSummaries");
345 const Symbol *symbol = m_kernel.module_sp->FindFirstSymbolWithNameAndType (kext_summary_symbol, eSymbolTypeData);
346 if (symbol)
347 {
Greg Claytone7612132012-03-07 21:03:09 +0000348 m_kext_summary_header_ptr_addr = symbol->GetAddress();
Greg Claytonc859e2d2012-02-13 23:10:39 +0000349 // Update all image infos
350 ReadAllKextSummaries ();
351 }
Greg Clayton7b242382011-07-08 00:48:09 +0000352 }
353 else
Greg Clayton7b242382011-07-08 00:48:09 +0000354 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000355 m_kernel.Clear(false);
Greg Clayton7b242382011-07-08 00:48:09 +0000356 }
357 }
Greg Clayton7b242382011-07-08 00:48:09 +0000358}
359
Greg Clayton7b242382011-07-08 00:48:09 +0000360//----------------------------------------------------------------------
361// Static callback function that gets called when our DYLD notification
362// breakpoint gets hit. We update all of our image infos and then
363// let our super class DynamicLoader class decide if we should stop
364// or not (based on global preference).
365//----------------------------------------------------------------------
366bool
Greg Clayton944b8282011-08-22 22:30:57 +0000367DynamicLoaderDarwinKernel::BreakpointHitCallback (void *baton,
Greg Clayton374972e2011-07-09 17:15:55 +0000368 StoppointCallbackContext *context,
369 user_id_t break_id,
370 user_id_t break_loc_id)
Greg Clayton0d9fc762011-07-08 03:21:57 +0000371{
Greg Clayton944b8282011-08-22 22:30:57 +0000372 return static_cast<DynamicLoaderDarwinKernel*>(baton)->BreakpointHit (context, break_id, break_loc_id);
Greg Clayton7b242382011-07-08 00:48:09 +0000373}
374
375bool
Greg Clayton944b8282011-08-22 22:30:57 +0000376DynamicLoaderDarwinKernel::BreakpointHit (StoppointCallbackContext *context,
Greg Clayton374972e2011-07-09 17:15:55 +0000377 user_id_t break_id,
378 user_id_t break_loc_id)
379{
Greg Claytond16e1e52011-07-12 17:06:17 +0000380 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
381 if (log)
Greg Clayton944b8282011-08-22 22:30:57 +0000382 log->Printf ("DynamicLoaderDarwinKernel::BreakpointHit (...)\n");
Greg Claytond16e1e52011-07-12 17:06:17 +0000383
Greg Clayton374972e2011-07-09 17:15:55 +0000384 ReadAllKextSummaries ();
Greg Claytond16e1e52011-07-12 17:06:17 +0000385
386 if (log)
387 PutToLog(log.get());
388
Greg Clayton374972e2011-07-09 17:15:55 +0000389 return GetStopWhenImagesChange();
390}
391
392
393bool
Greg Clayton944b8282011-08-22 22:30:57 +0000394DynamicLoaderDarwinKernel::ReadKextSummaryHeader ()
Greg Clayton7b242382011-07-08 00:48:09 +0000395{
396 Mutex::Locker locker(m_mutex);
397
398 // the all image infos is already valid for this process stop ID
Greg Clayton7b242382011-07-08 00:48:09 +0000399
400 m_kext_summaries.clear();
Greg Claytond16e1e52011-07-12 17:06:17 +0000401 if (m_kext_summary_header_ptr_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000402 {
403 const uint32_t addr_size = m_kernel.GetAddressByteSize ();
404 const ByteOrder byte_order = m_kernel.GetByteOrder();
405 Error error;
406 // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure
407 // which is currenty 4 uint32_t and a pointer.
408 uint8_t buf[24];
409 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
410 const size_t count = 4 * sizeof(uint32_t) + addr_size;
Greg Clayton0d9fc762011-07-08 03:21:57 +0000411 const bool prefer_file_cache = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000412 if (m_process->GetTarget().ReadPointerFromMemory (m_kext_summary_header_ptr_addr,
413 prefer_file_cache,
414 error,
415 m_kext_summary_header_addr))
Greg Clayton7b242382011-07-08 00:48:09 +0000416 {
Greg Claytond16e1e52011-07-12 17:06:17 +0000417 // We got a valid address for our kext summary header and make sure it isn't NULL
418 if (m_kext_summary_header_addr.IsValid() &&
419 m_kext_summary_header_addr.GetFileAddress() != 0)
420 {
421 const size_t bytes_read = m_process->GetTarget().ReadMemory (m_kext_summary_header_addr, prefer_file_cache, buf, count, error);
422 if (bytes_read == count)
423 {
424 uint32_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000425 m_kext_summary_header.version = data.GetU32(&offset);
426 if (m_kext_summary_header.version >= 2)
427 {
428 m_kext_summary_header.entry_size = data.GetU32(&offset);
429 }
430 else
431 {
432 // Versions less than 2 didn't have an entry size, it was hard coded
433 m_kext_summary_header.entry_size = KERNEL_MODULE_ENTRY_SIZE_VERSION_1;
434 }
435 m_kext_summary_header.entry_count = data.GetU32(&offset);
Greg Claytond16e1e52011-07-12 17:06:17 +0000436 return true;
437 }
438 }
Greg Clayton7b242382011-07-08 00:48:09 +0000439 }
440 }
Greg Claytond16e1e52011-07-12 17:06:17 +0000441 m_kext_summary_header_addr.Clear();
Greg Clayton7b242382011-07-08 00:48:09 +0000442 return false;
443}
444
445
446bool
Greg Clayton944b8282011-08-22 22:30:57 +0000447DynamicLoaderDarwinKernel::ParseKextSummaries (const Address &kext_summary_addr,
Greg Clayton0d9fc762011-07-08 03:21:57 +0000448 uint32_t count)
Greg Clayton7b242382011-07-08 00:48:09 +0000449{
450 OSKextLoadedKextSummary::collection kext_summaries;
Greg Clayton374972e2011-07-09 17:15:55 +0000451 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000452 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000453 log->Printf ("Adding %d modules.\n", count);
Greg Clayton7b242382011-07-08 00:48:09 +0000454
455 Mutex::Locker locker(m_mutex);
Greg Clayton7b242382011-07-08 00:48:09 +0000456
457 if (!ReadKextSummaries (kext_summary_addr, count, kext_summaries))
458 return false;
459
Greg Clayton07e66e32011-07-20 03:41:06 +0000460 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
Greg Clayton7b242382011-07-08 00:48:09 +0000461 for (uint32_t i = 0; i < count; i++)
462 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000463 if (s)
464 {
465 const uint8_t *u = (const uint8_t *)kext_summaries[i].uuid.GetBytes();
466 if (u)
467 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000468 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 +0000469 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
470 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
471 kext_summaries[i].address, kext_summaries[i].name);
472 }
473 else
474 {
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000475 s->Printf("0x%16.16llx \"%s\"...", kext_summaries[i].address, kext_summaries[i].name);
Greg Clayton07e66e32011-07-20 03:41:06 +0000476 }
477 }
478
Greg Clayton2af282a2012-03-21 04:25:00 +0000479 if (!kext_summaries[i].LoadImageUsingMemoryModule (m_process))
480 kext_summaries[i].LoadImageAtFileAddress (m_process);
Greg Claytonc859e2d2012-02-13 23:10:39 +0000481
Greg Clayton5b882162011-07-21 01:12:01 +0000482 if (s)
483 {
484 if (kext_summaries[i].module_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000485 {
486 if (kext_summaries[i].module_sp->GetFileSpec().GetDirectory())
487 s->Printf("\n found kext: %s/%s\n",
488 kext_summaries[i].module_sp->GetFileSpec().GetDirectory().AsCString(),
489 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
490 else
491 s->Printf("\n found kext: %s\n",
492 kext_summaries[i].module_sp->GetFileSpec().GetFilename().AsCString());
493 }
Jason Molenda0ca4f8b2011-09-24 02:47:39 +0000494 else
495 s->Printf (" failed to locate/load.\n");
Greg Clayton5b882162011-07-21 01:12:01 +0000496 }
497
Greg Claytona63d08c2011-07-19 03:57:15 +0000498 if (log)
499 kext_summaries[i].PutToLog (log.get());
Greg Clayton7b242382011-07-08 00:48:09 +0000500 }
501 bool return_value = AddModulesUsingImageInfos (kext_summaries);
Greg Clayton7b242382011-07-08 00:48:09 +0000502 return return_value;
503}
504
505// Adds the modules in image_infos to m_kext_summaries.
506// NB don't call this passing in m_kext_summaries.
507
508bool
Greg Clayton944b8282011-08-22 22:30:57 +0000509DynamicLoaderDarwinKernel::AddModulesUsingImageInfos (OSKextLoadedKextSummary::collection &image_infos)
Greg Clayton7b242382011-07-08 00:48:09 +0000510{
511 // Now add these images to the main list.
512 ModuleList loaded_module_list;
Greg Clayton7b242382011-07-08 00:48:09 +0000513
514 for (uint32_t idx = 0; idx < image_infos.size(); ++idx)
515 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000516 OSKextLoadedKextSummary &image_info = image_infos[idx];
517 m_kext_summaries.push_back(image_info);
Greg Clayton7b242382011-07-08 00:48:09 +0000518
Greg Claytonc859e2d2012-02-13 23:10:39 +0000519 if (image_info.module_sp && m_process->GetStopID() == image_info.load_process_stop_id)
520 loaded_module_list.AppendIfNeeded (image_infos[idx].module_sp);
Greg Clayton7b242382011-07-08 00:48:09 +0000521 }
522
523 if (loaded_module_list.GetSize() > 0)
524 {
Greg Clayton7b242382011-07-08 00:48:09 +0000525 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
526 }
527 return true;
528}
529
Greg Clayton7b242382011-07-08 00:48:09 +0000530
531uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000532DynamicLoaderDarwinKernel::ReadKextSummaries (const Address &kext_summary_addr,
Greg Clayton7b242382011-07-08 00:48:09 +0000533 uint32_t image_infos_count,
534 OSKextLoadedKextSummary::collection &image_infos)
535{
536 const ByteOrder endian = m_kernel.GetByteOrder();
537 const uint32_t addr_size = m_kernel.GetAddressByteSize();
538
539 image_infos.resize(image_infos_count);
540 const size_t count = image_infos.size() * m_kext_summary_header.entry_size;
541 DataBufferHeap data(count, 0);
542 Error error;
Greg Clayton5b882162011-07-21 01:12:01 +0000543
544 Stream *s = &m_process->GetTarget().GetDebugger().GetOutputStream();
545
546 if (s)
547 s->Printf ("Reading %u kext summaries...\n", image_infos_count);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000548 const bool prefer_file_cache = false;
549 const size_t bytes_read = m_process->GetTarget().ReadMemory (kext_summary_addr,
550 prefer_file_cache,
551 data.GetBytes(),
552 data.GetByteSize(),
553 error);
Greg Clayton7b242382011-07-08 00:48:09 +0000554 if (bytes_read == count)
555 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000556
Greg Clayton7b242382011-07-08 00:48:09 +0000557 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), endian, addr_size);
558 uint32_t i=0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000559 for (uint32_t kext_summary_offset = 0;
560 i < image_infos.size() && extractor.ValidOffsetForDataOfSize(kext_summary_offset, m_kext_summary_header.entry_size);
561 ++i, kext_summary_offset += m_kext_summary_header.entry_size)
Greg Clayton7b242382011-07-08 00:48:09 +0000562 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000563 uint32_t offset = kext_summary_offset;
Greg Clayton7b242382011-07-08 00:48:09 +0000564 const void *name_data = extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME);
565 if (name_data == NULL)
566 break;
567 memcpy (image_infos[i].name, name_data, KERNEL_MODULE_MAX_NAME);
568 image_infos[i].uuid.SetBytes(extractor.GetData (&offset, 16));
569 image_infos[i].address = extractor.GetU64(&offset);
Greg Clayton0d9fc762011-07-08 03:21:57 +0000570 if (!image_infos[i].so_address.SetLoadAddress (image_infos[i].address, &m_process->GetTarget()))
571 m_process->GetTarget().GetImages().ResolveFileAddress (image_infos[i].address, image_infos[i].so_address);
Greg Clayton7b242382011-07-08 00:48:09 +0000572 image_infos[i].size = extractor.GetU64(&offset);
573 image_infos[i].version = extractor.GetU64(&offset);
574 image_infos[i].load_tag = extractor.GetU32(&offset);
575 image_infos[i].flags = extractor.GetU32(&offset);
Greg Claytona63d08c2011-07-19 03:57:15 +0000576 if ((offset - kext_summary_offset) < m_kext_summary_header.entry_size)
577 {
578 image_infos[i].reference_list = extractor.GetU64(&offset);
579 }
580 else
581 {
582 image_infos[i].reference_list = 0;
583 }
Greg Claytonbae2f2f2012-02-14 00:14:24 +0000584// 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",
585// i,
586// KERNEL_MODULE_MAX_NAME, KERNEL_MODULE_MAX_NAME, (char *)name_data,
587// image_infos[i].address,
588// image_infos[i].size,
589// image_infos[i].version,
590// image_infos[i].load_tag,
591// image_infos[i].flags);
Greg Clayton7b242382011-07-08 00:48:09 +0000592 }
593 if (i < image_infos.size())
594 image_infos.resize(i);
595 }
596 else
597 {
598 image_infos.clear();
599 }
600 return image_infos.size();
601}
602
603bool
Greg Clayton944b8282011-08-22 22:30:57 +0000604DynamicLoaderDarwinKernel::ReadAllKextSummaries ()
Greg Clayton7b242382011-07-08 00:48:09 +0000605{
Greg Clayton374972e2011-07-09 17:15:55 +0000606 LogSP log(GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Greg Clayton7b242382011-07-08 00:48:09 +0000607
608 Mutex::Locker locker(m_mutex);
Greg Clayton374972e2011-07-09 17:15:55 +0000609
Greg Clayton7b242382011-07-08 00:48:09 +0000610 if (ReadKextSummaryHeader ())
611 {
Greg Clayton374972e2011-07-09 17:15:55 +0000612 if (m_kext_summary_header.entry_count > 0 && m_kext_summary_header_addr.IsValid())
Greg Clayton7b242382011-07-08 00:48:09 +0000613 {
Greg Clayton0d9fc762011-07-08 03:21:57 +0000614 Address summary_addr (m_kext_summary_header_addr);
Greg Claytona63d08c2011-07-19 03:57:15 +0000615 summary_addr.Slide(m_kext_summary_header.GetSize());
Greg Clayton0d9fc762011-07-08 03:21:57 +0000616 if (!ParseKextSummaries (summary_addr, m_kext_summary_header.entry_count))
Greg Clayton7b242382011-07-08 00:48:09 +0000617 {
Greg Clayton7b242382011-07-08 00:48:09 +0000618 m_kext_summaries.clear();
619 }
620 return true;
621 }
622 }
623 return false;
624}
625
626//----------------------------------------------------------------------
Greg Clayton7b242382011-07-08 00:48:09 +0000627// Dump an image info structure to the file handle provided.
628//----------------------------------------------------------------------
629void
Greg Clayton944b8282011-08-22 22:30:57 +0000630DynamicLoaderDarwinKernel::OSKextLoadedKextSummary::PutToLog (Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000631{
632 if (log == NULL)
633 return;
Greg Clayton07e66e32011-07-20 03:41:06 +0000634 const uint8_t *u = (uint8_t *)uuid.GetBytes();
Greg Clayton7b242382011-07-08 00:48:09 +0000635
636 if (address == LLDB_INVALID_ADDRESS)
637 {
638 if (u)
639 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000640 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 +0000641 u[ 0], u[ 1], u[ 2], u[ 3],
642 u[ 4], u[ 5], u[ 6], u[ 7],
643 u[ 8], u[ 9], u[10], u[11],
644 u[12], u[13], u[14], u[15],
645 name);
646 }
647 else
Greg Claytona63d08c2011-07-19 03:57:15 +0000648 log->Printf("\tname=\"%s\" (UNLOADED)", name);
Greg Clayton7b242382011-07-08 00:48:09 +0000649 }
650 else
651 {
652 if (u)
653 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000654 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\"",
655 address, size, version, load_tag, flags, reference_list,
656 u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
657 u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15],
Greg Clayton7b242382011-07-08 00:48:09 +0000658 name);
659 }
660 else
661 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000662 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\"",
663 address, address+size, version, load_tag, flags, reference_list,
664 name);
Greg Clayton7b242382011-07-08 00:48:09 +0000665 }
Greg Clayton7b242382011-07-08 00:48:09 +0000666 }
667}
668
669//----------------------------------------------------------------------
670// Dump the _dyld_all_image_infos members and all current image infos
671// that we have parsed to the file handle provided.
672//----------------------------------------------------------------------
673void
Greg Clayton944b8282011-08-22 22:30:57 +0000674DynamicLoaderDarwinKernel::PutToLog(Log *log) const
Greg Clayton7b242382011-07-08 00:48:09 +0000675{
676 if (log == NULL)
677 return;
678
679 Mutex::Locker locker(m_mutex);
Greg Claytona63d08c2011-07-19 03:57:15 +0000680 log->Printf("gLoadedKextSummaries = 0x%16.16llx { version=%u, entry_size=%u, entry_count=%u }",
Greg Clayton0d9fc762011-07-08 03:21:57 +0000681 m_kext_summary_header_addr.GetFileAddress(),
Greg Clayton7b242382011-07-08 00:48:09 +0000682 m_kext_summary_header.version,
683 m_kext_summary_header.entry_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000684 m_kext_summary_header.entry_count);
Greg Clayton7b242382011-07-08 00:48:09 +0000685
686 size_t i;
687 const size_t count = m_kext_summaries.size();
688 if (count > 0)
689 {
690 log->PutCString("Loaded:");
691 for (i = 0; i<count; i++)
692 m_kext_summaries[i].PutToLog(log);
693 }
694}
695
696void
Greg Clayton944b8282011-08-22 22:30:57 +0000697DynamicLoaderDarwinKernel::PrivateInitialize(Process *process)
Greg Clayton7b242382011-07-08 00:48:09 +0000698{
Greg Clayton944b8282011-08-22 22:30:57 +0000699 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton7b242382011-07-08 00:48:09 +0000700 Clear(true);
701 m_process = process;
702 m_process->GetTarget().GetSectionLoadList().Clear();
703}
704
Greg Clayton374972e2011-07-09 17:15:55 +0000705void
Greg Clayton944b8282011-08-22 22:30:57 +0000706DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded ()
Greg Clayton7b242382011-07-08 00:48:09 +0000707{
Greg Claytonc859e2d2012-02-13 23:10:39 +0000708 if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.module_sp)
Greg Clayton374972e2011-07-09 17:15:55 +0000709 {
Greg Clayton944b8282011-08-22 22:30:57 +0000710 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
Greg Clayton374972e2011-07-09 17:15:55 +0000711
Greg Claytond16e1e52011-07-12 17:06:17 +0000712
Greg Clayton374972e2011-07-09 17:15:55 +0000713 const bool internal_bp = false;
Greg Claytond16e1e52011-07-12 17:06:17 +0000714 const LazyBool skip_prologue = eLazyBoolNo;
Jim Ingham969795f2011-09-21 01:17:13 +0000715 FileSpecList module_spec_list;
716 module_spec_list.Append (m_kernel.module_sp->GetFileSpec());
717 Breakpoint *bp = m_process->GetTarget().CreateBreakpoint (&module_spec_list,
Jim Ingham87df91b2011-09-23 00:54:11 +0000718 NULL,
Greg Clayton374972e2011-07-09 17:15:55 +0000719 "OSKextLoadedKextSummariesUpdated",
720 eFunctionNameTypeFull,
Greg Claytond16e1e52011-07-12 17:06:17 +0000721 internal_bp,
722 skip_prologue).get();
Greg Clayton374972e2011-07-09 17:15:55 +0000723
Greg Clayton944b8282011-08-22 22:30:57 +0000724 bp->SetCallback (DynamicLoaderDarwinKernel::BreakpointHitCallback, this, true);
Greg Clayton374972e2011-07-09 17:15:55 +0000725 m_break_id = bp->GetID();
726 }
Greg Clayton7b242382011-07-08 00:48:09 +0000727}
728
729//----------------------------------------------------------------------
730// Member function that gets called when the process state changes.
731//----------------------------------------------------------------------
732void
Greg Clayton944b8282011-08-22 22:30:57 +0000733DynamicLoaderDarwinKernel::PrivateProcessStateChanged (Process *process, StateType state)
Greg Clayton7b242382011-07-08 00:48:09 +0000734{
Greg Clayton944b8282011-08-22 22:30:57 +0000735 DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, StateAsCString(state));
Greg Clayton7b242382011-07-08 00:48:09 +0000736 switch (state)
737 {
738 case eStateConnected:
739 case eStateAttaching:
740 case eStateLaunching:
741 case eStateInvalid:
742 case eStateUnloaded:
743 case eStateExited:
744 case eStateDetached:
745 Clear(false);
746 break;
747
748 case eStateStopped:
Greg Clayton374972e2011-07-09 17:15:55 +0000749 UpdateIfNeeded();
Greg Clayton7b242382011-07-08 00:48:09 +0000750 break;
751
752 case eStateRunning:
753 case eStateStepping:
754 case eStateCrashed:
755 case eStateSuspended:
756 break;
757
758 default:
759 break;
760 }
761}
762
763ThreadPlanSP
Greg Clayton944b8282011-08-22 22:30:57 +0000764DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
Greg Clayton7b242382011-07-08 00:48:09 +0000765{
766 ThreadPlanSP thread_plan_sp;
Greg Clayton374972e2011-07-09 17:15:55 +0000767 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
768 if (log)
769 log->Printf ("Could not find symbol for step through.");
Greg Clayton7b242382011-07-08 00:48:09 +0000770 return thread_plan_sp;
771}
772
773Error
Greg Clayton944b8282011-08-22 22:30:57 +0000774DynamicLoaderDarwinKernel::CanLoadImage ()
Greg Clayton7b242382011-07-08 00:48:09 +0000775{
776 Error error;
777 error.SetErrorString("always unsafe to load or unload shared libraries in the darwin kernel");
778 return error;
779}
780
781void
Greg Clayton944b8282011-08-22 22:30:57 +0000782DynamicLoaderDarwinKernel::Initialize()
Greg Clayton7b242382011-07-08 00:48:09 +0000783{
784 PluginManager::RegisterPlugin (GetPluginNameStatic(),
785 GetPluginDescriptionStatic(),
786 CreateInstance);
787}
788
789void
Greg Clayton944b8282011-08-22 22:30:57 +0000790DynamicLoaderDarwinKernel::Terminate()
Greg Clayton7b242382011-07-08 00:48:09 +0000791{
792 PluginManager::UnregisterPlugin (CreateInstance);
793}
794
795
796const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000797DynamicLoaderDarwinKernel::GetPluginNameStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000798{
799 return "dynamic-loader.macosx-kernel";
800}
801
802const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000803DynamicLoaderDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton7b242382011-07-08 00:48:09 +0000804{
805 return "Dynamic loader plug-in that watches for shared library loads/unloads in the MacOSX kernel.";
806}
807
808
809//------------------------------------------------------------------
810// PluginInterface protocol
811//------------------------------------------------------------------
812const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000813DynamicLoaderDarwinKernel::GetPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000814{
Greg Clayton944b8282011-08-22 22:30:57 +0000815 return "DynamicLoaderDarwinKernel";
Greg Clayton7b242382011-07-08 00:48:09 +0000816}
817
818const char *
Greg Clayton944b8282011-08-22 22:30:57 +0000819DynamicLoaderDarwinKernel::GetShortPluginName()
Greg Clayton7b242382011-07-08 00:48:09 +0000820{
821 return GetPluginNameStatic();
822}
823
824uint32_t
Greg Clayton944b8282011-08-22 22:30:57 +0000825DynamicLoaderDarwinKernel::GetPluginVersion()
Greg Clayton7b242382011-07-08 00:48:09 +0000826{
827 return 1;
828}
829