blob: f0477e8520894ba628f6e6dcec45f5e733b4b674 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- DynamicLoaderMacOSXDYLD.cpp -----------------------------*- C++ -*-===//
2//
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"
13#include "lldb/Core/Log.h"
14#include "lldb/Core/Module.h"
15#include "lldb/Core/PluginManager.h"
16#include "lldb/Core/State.h"
17#include "lldb/Symbol/ObjectFile.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000018#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Target/RegisterContext.h"
20#include "lldb/Target/Target.h"
21#include "lldb/Target/Thread.h"
22#include "lldb/Target/ThreadPlanRunToAddress.h"
23#include "lldb/Target/StackFrame.h"
24
25#include "DynamicLoaderMacOSXDYLD.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
27//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN
28#ifdef ENABLE_DEBUG_PRINTF
29#include <stdio.h>
30#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
31#else
32#define DEBUG_PRINTF(fmt, ...)
33#endif
34
35using namespace lldb;
36using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000037using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000038
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// Create an instance of this class. This function is filled into
45// the plugin info class that gets handed out by the plugin factory and
46// allows the lldb to instantiate an instance of this class.
47//----------------------------------------------------------------------
48DynamicLoader *
49DynamicLoaderMacOSXDYLD::CreateInstance (Process* process)
50{
51 return new DynamicLoaderMacOSXDYLD (process);
52}
53
54//----------------------------------------------------------------------
55// Constructor
56//----------------------------------------------------------------------
57DynamicLoaderMacOSXDYLD::DynamicLoaderMacOSXDYLD (Process* process) :
58 DynamicLoader(process),
59 m_dyld(),
60 m_dyld_all_image_infos_addr(LLDB_INVALID_ADDRESS),
61 m_dyld_all_image_infos(),
Greg Clayton20d338f2010-11-18 05:57:03 +000062 m_dyld_all_image_infos_stop_id (UINT32_MAX),
Chris Lattner24943d22010-06-08 16:52:24 +000063 m_break_id(LLDB_INVALID_BREAK_ID),
64 m_dyld_image_infos(),
Greg Clayton20d338f2010-11-18 05:57:03 +000065 m_dyld_image_infos_stop_id (UINT32_MAX),
Jim Inghamb66cd072010-09-28 01:25:32 +000066 m_mutex(Mutex::eMutexTypeRecursive)
Chris Lattner24943d22010-06-08 16:52:24 +000067{
68}
69
70//----------------------------------------------------------------------
71// Destructor
72//----------------------------------------------------------------------
73DynamicLoaderMacOSXDYLD::~DynamicLoaderMacOSXDYLD()
74{
75 Clear(true);
76}
77
78//------------------------------------------------------------------
79/// Called after attaching a process.
80///
81/// Allow DynamicLoader plug-ins to execute some code after
82/// attaching to a process.
83//------------------------------------------------------------------
84void
85DynamicLoaderMacOSXDYLD::DidAttach ()
86{
87 PrivateInitialize(m_process);
Greg Clayton20d338f2010-11-18 05:57:03 +000088 LocateDYLD ();
Chris Lattner24943d22010-06-08 16:52:24 +000089 SetNotificationBreakpoint ();
Chris Lattner24943d22010-06-08 16:52:24 +000090}
91
92//------------------------------------------------------------------
93/// Called after attaching a process.
94///
95/// Allow DynamicLoader plug-ins to execute some code after
96/// attaching to a process.
97//------------------------------------------------------------------
98void
99DynamicLoaderMacOSXDYLD::DidLaunch ()
100{
101 PrivateInitialize(m_process);
Greg Clayton20d338f2010-11-18 05:57:03 +0000102 LocateDYLD ();
Chris Lattner24943d22010-06-08 16:52:24 +0000103 SetNotificationBreakpoint ();
Chris Lattner24943d22010-06-08 16:52:24 +0000104}
105
106
107//----------------------------------------------------------------------
108// Clear out the state of this class.
109//----------------------------------------------------------------------
110void
111DynamicLoaderMacOSXDYLD::Clear (bool clear_process)
112{
113 Mutex::Locker locker(m_mutex);
114
115 if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id))
116 m_process->ClearBreakpointSiteByID(m_break_id);
117
118 if (clear_process)
119 m_process = NULL;
120 m_dyld.Clear(false);
121 m_dyld_all_image_infos_addr = LLDB_INVALID_ADDRESS;
122 m_dyld_all_image_infos.Clear();
123 m_break_id = LLDB_INVALID_BREAK_ID;
124 m_dyld_image_infos.clear();
125}
126
127//----------------------------------------------------------------------
128// Check if we have found DYLD yet
129//----------------------------------------------------------------------
130bool
131DynamicLoaderMacOSXDYLD::DidSetNotificationBreakpoint() const
132{
133 return LLDB_BREAK_ID_IS_VALID (m_break_id);
134}
135
136//----------------------------------------------------------------------
137// Try and figure out where dyld is by first asking the Process
138// if it knows (which currently calls down in the the lldb::Process
139// to get the DYLD info (available on SnowLeopard only). If that fails,
140// then check in the default addresses.
141//----------------------------------------------------------------------
142bool
143DynamicLoaderMacOSXDYLD::LocateDYLD()
144{
145 if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS)
146 m_dyld_all_image_infos_addr = m_process->GetImageInfoAddress ();
147
148 if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS)
149 {
150 if (ReadAllImageInfosStructure ())
151 {
152 if (m_dyld_all_image_infos.dyldImageLoadAddress != LLDB_INVALID_ADDRESS)
153 return ReadDYLDInfoFromMemoryAndSetNotificationCallback (m_dyld_all_image_infos.dyldImageLoadAddress);
154 else
155 return ReadDYLDInfoFromMemoryAndSetNotificationCallback (m_dyld_all_image_infos_addr & 0xfffffffffff00000ull);
156 }
157 }
158
159 // Check some default values
160 Module *executable = m_process->GetTarget().GetExecutableModule().get();
161
162 if (executable)
163 {
164 if (executable->GetArchitecture().GetAddressByteSize() == 8)
165 {
166 return ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x7fff5fc00000ull);
167 }
168#if defined (__arm__)
169 else
170 {
171 ArchSpec arm_arch("arm");
172 if (arm_arch == executable->Arch())
173 return ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x2fe00000);
174 }
175#endif
176 return ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x8fe00000);
177 }
178 return false;
179}
180
181//----------------------------------------------------------------------
182// Assume that dyld is in memory at ADDR and try to parse it's load
183// commands
184//----------------------------------------------------------------------
185bool
186DynamicLoaderMacOSXDYLD::ReadDYLDInfoFromMemoryAndSetNotificationCallback(lldb::addr_t addr)
187{
188 DataExtractor data; // Load command data
189 if (ReadMachHeader (addr, &m_dyld.header, &data))
190 {
Greg Clayton1674b122010-07-21 22:12:05 +0000191 if (m_dyld.header.filetype == HeaderFileTypeDynamicLinkEditor)
Chris Lattner24943d22010-06-08 16:52:24 +0000192 {
193 m_dyld.address = addr;
194 ModuleSP dyld_module_sp;
195 if (ParseLoadCommands (data, m_dyld, &m_dyld.file_spec))
196 {
197 if (m_dyld.file_spec)
198 {
Greg Claytoncf015052010-06-11 03:25:34 +0000199 ArchSpec dyld_arch(eArchTypeMachO, m_dyld.header.cputype, m_dyld.header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +0000200 dyld_module_sp = m_process->GetTarget().GetImages().FindFirstModuleForFileSpec (m_dyld.file_spec);
201
202 if (dyld_module_sp.get() == NULL || dyld_module_sp->GetArchitecture() != dyld_arch)
203 {
204 dyld_module_sp = m_process->GetTarget().GetSharedModule (m_dyld.file_spec,
205 dyld_arch,
206 &m_dyld.uuid);
207 }
208
209 UpdateImageLoadAddress(dyld_module_sp.get(), m_dyld);
210 }
211 }
212
213 if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS && dyld_module_sp.get())
214 {
215 static ConstString g_dyld_all_image_infos ("dyld_all_image_infos");
216 const Symbol *symbol = dyld_module_sp->FindFirstSymbolWithNameAndType (g_dyld_all_image_infos, eSymbolTypeData);
217 if (symbol)
Greg Claytoneea26402010-09-14 23:36:40 +0000218 m_dyld_all_image_infos_addr = symbol->GetValue().GetLoadAddress(&m_process->GetTarget());
Chris Lattner24943d22010-06-08 16:52:24 +0000219 }
220
221 // Update all image infos
222 UpdateAllImageInfos();
223
224 // If we didn't have an executable before, but now we do, then the
225 // dyld module shared pointer might be unique and we may need to add
226 // it again (since Target::SetExecutableModule() will clear the
227 // images). So append the dyld module back to the list if it is
228 /// unique!
229 if (m_process->GetTarget().GetImages().AppendInNeeded (dyld_module_sp))
230 UpdateImageLoadAddress(dyld_module_sp.get(), m_dyld);
231
232 return true;
233 }
234 }
235 return false;
236}
237
238bool
239DynamicLoaderMacOSXDYLD::NeedToLocateDYLD () const
240{
241 return m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS;
242}
243
244bool
245DynamicLoaderMacOSXDYLD::UpdateCommPageLoadAddress(Module *module)
246{
247 bool changed = false;
248 if (module)
249 {
250 ObjectFile *image_object_file = module->GetObjectFile();
251 if (image_object_file)
252 {
253 SectionList *section_list = image_object_file->GetSectionList ();
254 if (section_list)
255 {
256 uint32_t num_sections = section_list->GetSize();
257 for (uint32_t i=0; i<num_sections; ++i)
258 {
259 Section* section = section_list->GetSectionAtIndex (i).get();
260 if (section)
261 {
262 const addr_t new_section_load_addr = section->GetFileAddress ();
Greg Claytoneea26402010-09-14 23:36:40 +0000263 const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section);
Chris Lattner24943d22010-06-08 16:52:24 +0000264 if (old_section_load_addr == LLDB_INVALID_ADDRESS ||
265 old_section_load_addr != new_section_load_addr)
266 {
Greg Claytoneea26402010-09-14 23:36:40 +0000267 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress ()))
Chris Lattner24943d22010-06-08 16:52:24 +0000268 changed = true;
269 }
270 }
271 }
272 }
273 }
274 }
275 return changed;
276}
277
278//----------------------------------------------------------------------
279// Update the load addresses for all segments in MODULE using the
280// updated INFO that is passed in.
281//----------------------------------------------------------------------
282bool
283DynamicLoaderMacOSXDYLD::UpdateImageLoadAddress (Module *module, struct DYLDImageInfo& info)
284{
285 bool changed = false;
286 if (module)
287 {
288 ObjectFile *image_object_file = module->GetObjectFile();
289 if (image_object_file)
290 {
291 SectionList *section_list = image_object_file->GetSectionList ();
292 if (section_list)
293 {
294 // All sections listed in the dyld image info structure will all
295 // either be fixed up already, or they will all be off by a single
296 // slide amount that is determined by finding the first segment
297 // that is at file offset zero which also has bytes (a file size
298 // that is greater than zero) in the object file.
299
300 // Determine the slide amount (if any)
301 info.slide = 0;
302 const size_t num_sections = section_list->GetSize();
303 size_t sect_idx = 0;
304 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
305 {
306 // Iterate through the object file sections to find the
307 // first section that starts of file offset zero and that
308 // has bytes in the file...
309 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
310 if (section)
311 {
312 // Find the first section that begins at file offset zero
313 // a file size (skip page zero).
314 if (section->GetFileOffset() == 0 && section->GetFileSize() > 0)
315 {
316 // We have now found the section, lets match it up
317 // with the section in the dyld image info structure.
318 const Segment *dyld_segment = info.FindSegment (section->GetName());
319 if (dyld_segment)
320 info.slide = info.address - dyld_segment->addr;
321 // We have found the slide amount, so we can exit
322 // this for loop.
323 break;
324 }
325 }
326 }
327
328 // We now know the slide amount, so go through all sections
329 // and update the load addresses with the correct values.
330 uint32_t num_segments = info.segments.size();
331 for (uint32_t i=0; i<num_segments; ++i)
332 {
333 SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name));
334 assert (section_sp.get() != NULL);
335 const addr_t new_section_load_addr = info.segments[i].addr + info.slide;
Greg Claytoneea26402010-09-14 23:36:40 +0000336 const addr_t old_section_load_addr = m_process->GetTarget().GetSectionLoadList().GetSectionLoadAddress (section_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000337 if (old_section_load_addr == LLDB_INVALID_ADDRESS ||
338 old_section_load_addr != new_section_load_addr)
339 {
Greg Claytoneea26402010-09-14 23:36:40 +0000340 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), new_section_load_addr))
Chris Lattner24943d22010-06-08 16:52:24 +0000341 changed = true;
342 }
343 }
344 }
345 }
346 }
347 return changed;
348}
349
350//----------------------------------------------------------------------
351// Update the load addresses for all segments in MODULE using the
352// updated INFO that is passed in.
353//----------------------------------------------------------------------
354bool
355DynamicLoaderMacOSXDYLD::UnloadImageLoadAddress (Module *module, struct DYLDImageInfo& info)
356{
357 bool changed = false;
358 if (module)
359 {
360 ObjectFile *image_object_file = module->GetObjectFile();
361 if (image_object_file)
362 {
363 SectionList *section_list = image_object_file->GetSectionList ();
364 if (section_list)
365 {
366 uint32_t num_segments = info.segments.size();
367 for (uint32_t i=0; i<num_segments; ++i)
368 {
369 SectionSP section_sp(section_list->FindSectionByName(info.segments[i].name));
370 assert (section_sp.get() != NULL);
371 const addr_t old_section_load_addr = info.segments[i].addr + info.slide;
Greg Claytoneea26402010-09-14 23:36:40 +0000372 if (m_process->GetTarget().GetSectionLoadList().SetSectionUnloaded (section_sp.get(), old_section_load_addr))
Chris Lattner24943d22010-06-08 16:52:24 +0000373 changed = true;
374 }
375 }
376 }
377 }
378 return changed;
379}
380
381
382//----------------------------------------------------------------------
383// Static callback function that gets called when our DYLD notification
384// breakpoint gets hit. We update all of our image infos and then
385// let our super class DynamicLoader class decide if we should stop
386// or not (based on global preference).
387//----------------------------------------------------------------------
388bool
389DynamicLoaderMacOSXDYLD::NotifyBreakpointHit (void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
390{
391 // Let the event know that the images have changed
392 DynamicLoaderMacOSXDYLD* dyld_instance = (DynamicLoaderMacOSXDYLD*) baton;
393 dyld_instance->UpdateAllImageInfos();
394 // Return true to stop the target, false to just let the target run
395 return dyld_instance->GetStopWhenImagesChange();
396}
397
398bool
399DynamicLoaderMacOSXDYLD::ReadAllImageInfosStructure ()
400{
401 Mutex::Locker locker(m_mutex);
Greg Clayton20d338f2010-11-18 05:57:03 +0000402
403 // the all image infos is already valid for this process stop ID
404 if (m_process->GetStopID() == m_dyld_all_image_infos_stop_id)
405 return true;
406
Chris Lattner24943d22010-06-08 16:52:24 +0000407 m_dyld_all_image_infos.Clear();
408 if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS)
409 {
Greg Clayton20d338f2010-11-18 05:57:03 +0000410 ByteOrder byte_order = m_process->GetByteOrder();
411 uint32_t addr_size = 4;
412 if (m_dyld_all_image_infos_addr > UINT32_MAX)
413 addr_size = 8;
414
Chris Lattner24943d22010-06-08 16:52:24 +0000415 uint8_t buf[256];
Greg Clayton20d338f2010-11-18 05:57:03 +0000416 DataExtractor data (buf, sizeof(buf), byte_order, addr_size);
417 uint32_t offset = 0;
418
Jason Molenda1f3af542010-06-10 01:21:21 +0000419 const size_t count_v2 = sizeof (uint32_t) + // version
420 sizeof (uint32_t) + // infoArrayCount
421 addr_size + // infoArray
422 addr_size + // notification
423 addr_size + // processDetachedFromSharedRegion + libSystemInitialized + pad
424 addr_size; // dyldImageLoadAddress
425 const size_t count_v11 = count_v2 +
426 addr_size + // jitInfo
427 addr_size + // dyldVersion
428 addr_size + // errorMessage
429 addr_size + // terminationFlags
430 addr_size + // coreSymbolicationShmPage
431 addr_size + // systemOrderFlag
432 addr_size + // uuidArrayCount
433 addr_size + // uuidArray
434 addr_size + // dyldAllImageInfosAddress
435 addr_size + // initialImageCount
436 addr_size + // errorKind
437 addr_size + // errorClientOfDylibPath
438 addr_size + // errorTargetDylibPath
439 addr_size; // errorSymbol
440 assert (sizeof (buf) > count_v11);
441
442 int count;
Chris Lattner24943d22010-06-08 16:52:24 +0000443 Error error;
Jason Molenda1f3af542010-06-10 01:21:21 +0000444 if (m_process->ReadMemory (m_dyld_all_image_infos_addr, buf, 4, error) == 4)
445 {
Jason Molenda1f3af542010-06-10 01:21:21 +0000446 m_dyld_all_image_infos.version = data.GetU32(&offset);
Greg Clayton20d338f2010-11-18 05:57:03 +0000447 // If anything in the high byte is set, we probably got the byte
448 // order incorrect (the process might not have it set correctly
449 // yet due to attaching to a program without a specified file).
450 if (m_dyld_all_image_infos.version & 0xff000000)
451 {
452 // We have guessed the wrong byte order. Swap it and try
453 // reading the version again.
454 if (byte_order == eByteOrderLittle)
455 byte_order = eByteOrderBig;
456 else
457 byte_order = eByteOrderLittle;
458
459 data.SetByteOrder (byte_order);
460 offset = 0;
461 m_dyld_all_image_infos.version = data.GetU32(&offset);
462 }
Jason Molenda1f3af542010-06-10 01:21:21 +0000463 }
464 else
465 {
466 return false;
467 }
468
469 if (m_dyld_all_image_infos.version >= 11)
470 count = count_v11;
471 else
472 count = count_v2;
473
Chris Lattner24943d22010-06-08 16:52:24 +0000474 const size_t bytes_read = m_process->ReadMemory (m_dyld_all_image_infos_addr, buf, count, error);
475 if (bytes_read == count)
476 {
Greg Clayton20d338f2010-11-18 05:57:03 +0000477 offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000478 m_dyld_all_image_infos.version = data.GetU32(&offset);
479 m_dyld_all_image_infos.dylib_info_count = data.GetU32(&offset);
480 m_dyld_all_image_infos.dylib_info_addr = data.GetPointer(&offset);
481 m_dyld_all_image_infos.notification = data.GetPointer(&offset);
482 m_dyld_all_image_infos.processDetachedFromSharedRegion = data.GetU8(&offset);
Jason Molenda1f3af542010-06-10 01:21:21 +0000483 m_dyld_all_image_infos.libSystemInitialized = data.GetU8(&offset);
484 // Adjust for padding.
485 offset += addr_size - 2;
486 m_dyld_all_image_infos.dyldImageLoadAddress = data.GetPointer(&offset);
487 if (m_dyld_all_image_infos.version >= 11)
Chris Lattner24943d22010-06-08 16:52:24 +0000488 {
Jason Molenda1f3af542010-06-10 01:21:21 +0000489 offset += addr_size * 8;
490 uint64_t dyld_all_image_infos_addr = data.GetPointer(&offset);
491
492 // When we started, we were given the actual address of the all_image_infos
493 // struct (probably via TASK_DYLD_INFO) in memory - this address is stored in
494 // m_dyld_all_image_infos_addr and is the most accurate address we have.
495
496 // We read the dyld_all_image_infos struct from memory; it contains its own address.
497 // If the address in the struct does not match the actual address,
498 // the dyld we're looking at has been loaded at a different location (slid) from
499 // where it intended to load. The addresses in the dyld_all_image_infos struct
500 // are the original, non-slid addresses, and need to be adjusted. Most importantly
501 // the address of dyld and the notification address need to be adjusted.
502
503 if (dyld_all_image_infos_addr != m_dyld_all_image_infos_addr)
504 {
505 uint64_t image_infos_offset = dyld_all_image_infos_addr - m_dyld_all_image_infos.dyldImageLoadAddress;
506 uint64_t notification_offset = m_dyld_all_image_infos.notification - m_dyld_all_image_infos.dyldImageLoadAddress;
507 m_dyld_all_image_infos.dyldImageLoadAddress = m_dyld_all_image_infos_addr - image_infos_offset;
508 m_dyld_all_image_infos.notification = m_dyld_all_image_infos.dyldImageLoadAddress + notification_offset;
509 }
Chris Lattner24943d22010-06-08 16:52:24 +0000510 }
Greg Clayton20d338f2010-11-18 05:57:03 +0000511 m_dyld_all_image_infos_stop_id = m_process->GetStopID();
Chris Lattner24943d22010-06-08 16:52:24 +0000512 return true;
513 }
514 }
515 return false;
516}
517
518//----------------------------------------------------------------------
519// If we have found where the "_dyld_all_image_infos" lives in memory,
520// read the current info from it, and then update all image load
521// addresses (or lack thereof).
522//----------------------------------------------------------------------
523uint32_t
524DynamicLoaderMacOSXDYLD::UpdateAllImageInfos()
525{
Greg Claytone005f2c2010-11-06 01:53:30 +0000526 LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_DYNAMIC_LOADER));
Chris Lattner24943d22010-06-08 16:52:24 +0000527 if (ReadAllImageInfosStructure ())
528 {
529 Mutex::Locker locker(m_mutex);
Greg Clayton20d338f2010-11-18 05:57:03 +0000530 if (m_process->GetStopID() == m_dyld_image_infos_stop_id)
531 m_dyld_image_infos.size();
532
Chris Lattner24943d22010-06-08 16:52:24 +0000533 uint32_t idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000534 uint32_t i = 0;
535 DYLDImageInfo::collection old_dyld_all_image_infos;
536 old_dyld_all_image_infos.swap(m_dyld_image_infos);
537
538 // If we made it here, we are assuming that the all dylib info data should
539 // be valid, lets read the info array.
Greg Clayton20d338f2010-11-18 05:57:03 +0000540 const ByteOrder endian = m_dyld.GetByteOrder();
541 const uint32_t addr_size = m_dyld.GetAddressByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000542
543 if (m_dyld_all_image_infos.dylib_info_count > 0)
544 {
545 if (m_dyld_all_image_infos.dylib_info_addr == 0)
546 {
547 // DYLD is updating the images right now...
548 }
549 else
550 {
551 m_dyld_image_infos.resize(m_dyld_all_image_infos.dylib_info_count);
552 const size_t count = m_dyld_image_infos.size() * 3 * addr_size;
553 DataBufferHeap info_data(count, 0);
554 Error error;
555 const size_t bytes_read = m_process->ReadMemory (m_dyld_all_image_infos.dylib_info_addr,
556 info_data.GetBytes(),
557 info_data.GetByteSize(),
558 error);
559 if (bytes_read == count)
560 {
561 uint32_t info_data_offset = 0;
562 DataExtractor info_data_ref(info_data.GetBytes(), info_data.GetByteSize(), endian, addr_size);
563 for (i = 0; info_data_ref.ValidOffset(info_data_offset); i++)
564 {
565 assert (i < m_dyld_image_infos.size());
566 m_dyld_image_infos[i].address = info_data_ref.GetPointer(&info_data_offset);
567 lldb::addr_t path_addr = info_data_ref.GetPointer(&info_data_offset);
568 m_dyld_image_infos[i].mod_date = info_data_ref.GetPointer(&info_data_offset);
569
570 char raw_path[PATH_MAX];
571 m_process->ReadMemory (path_addr, raw_path, sizeof(raw_path), error);
Greg Clayton537a7a82010-10-20 20:54:39 +0000572 m_dyld_image_infos[i].file_spec.SetFile(raw_path, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000573 }
574 assert(i == m_dyld_all_image_infos.dylib_info_count);
575
576 UpdateAllImageInfosHeaderAndLoadCommands();
577 }
578 else
579 {
580 DEBUG_PRINTF( "unable to read all data for all_dylib_infos.");
581 m_dyld_image_infos.clear();
582 }
583 }
584 }
585 else
586 {
587 m_dyld_image_infos.clear();
588 }
589
590 // If our new list is smaller than our old list, we have unloaded
591 // some shared libraries
592 if (m_dyld_image_infos.size() < old_dyld_all_image_infos.size())
593 {
594 ModuleList unloaded_module_list;
Greg Clayton9d2993d2010-11-03 04:08:06 +0000595 uint32_t old_idx;
596 for (idx = 0; idx < old_dyld_all_image_infos.size(); ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000597 {
Greg Clayton9d2993d2010-11-03 04:08:06 +0000598 for (old_idx = idx; old_idx < old_dyld_all_image_infos.size(); ++old_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000599 {
Greg Clayton9d2993d2010-11-03 04:08:06 +0000600 if (m_dyld_image_infos[idx].file_spec == old_dyld_all_image_infos[old_idx].file_spec)
601 {
602 old_dyld_all_image_infos[old_idx].address = LLDB_INVALID_ADDRESS;
603 break;
604 }
Chris Lattner24943d22010-06-08 16:52:24 +0000605 }
606 }
Greg Clayton9d2993d2010-11-03 04:08:06 +0000607
608 if (log)
609 log->PutCString("Unloaded:");
610
611 for (old_idx = 0; old_idx < old_dyld_all_image_infos.size(); ++old_idx)
612 {
613 if (old_dyld_all_image_infos[old_idx].address != LLDB_INVALID_ADDRESS)
614 {
615 if (log)
Greg Claytone005f2c2010-11-06 01:53:30 +0000616 old_dyld_all_image_infos[old_idx].PutToLog (log.get());
Greg Clayton9d2993d2010-11-03 04:08:06 +0000617 ModuleSP unload_image_module_sp(m_process->GetTarget().GetImages().FindFirstModuleForFileSpec (old_dyld_all_image_infos[old_idx].file_spec));
618 if (unload_image_module_sp.get())
619 {
620 if (UnloadImageLoadAddress (unload_image_module_sp.get(), old_dyld_all_image_infos[old_idx]))
621 unloaded_module_list.AppendInNeeded (unload_image_module_sp);
622 }
623 }
624 }
625
Chris Lattner24943d22010-06-08 16:52:24 +0000626 if (unloaded_module_list.GetSize() > 0)
627 m_process->GetTarget().ModulesDidUnload (unloaded_module_list);
628 }
Greg Clayton9d2993d2010-11-03 04:08:06 +0000629 else
630 {
631 if (log)
Greg Claytone005f2c2010-11-06 01:53:30 +0000632 PutToLog(log.get());
Greg Clayton9d2993d2010-11-03 04:08:06 +0000633 }
Greg Clayton20d338f2010-11-18 05:57:03 +0000634 m_dyld_image_infos_stop_id = m_process->GetStopID();
Chris Lattner24943d22010-06-08 16:52:24 +0000635 }
636 else
637 {
638 m_dyld_image_infos.clear();
639 }
640
641 const uint32_t num_dylibs = m_dyld_image_infos.size();
642 if (num_dylibs > 0)
643 {
644 ModuleList loaded_module_list;
645 for (uint32_t idx = 0; idx<num_dylibs; ++idx)
646 {
Greg Claytoncf015052010-06-11 03:25:34 +0000647 ArchSpec arch_spec(eArchTypeMachO, m_dyld_image_infos[idx].header.cputype, m_dyld_image_infos[idx].header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +0000648 ModuleSP image_module_sp(m_process->GetTarget().GetImages().FindFirstModuleForFileSpec (m_dyld_image_infos[idx].file_spec));
649 if (image_module_sp.get() == NULL || image_module_sp->GetArchitecture() != arch_spec)
650 {
651 image_module_sp = m_process->GetTarget().GetSharedModule (m_dyld_image_infos[idx].file_spec,
652 arch_spec,
653 &m_dyld_image_infos[idx].uuid);
654 }
655
656 if (image_module_sp)
657 {
Greg Clayton236c1c72010-09-07 23:40:05 +0000658 if (m_dyld_image_infos[idx].header.filetype == HeaderFileTypeDynamicLinkEditor)
659 image_module_sp->SetIsDynamicLinkEditor (true);
660
Chris Lattner24943d22010-06-08 16:52:24 +0000661 ObjectFile *objfile = image_module_sp->GetObjectFile ();
662 if (objfile)
663 {
664 SectionList *sections = objfile->GetSectionList();
665 if (sections)
666 {
667 ConstString commpage_dbstr("__commpage");
668 Section *commpage_section = sections->FindSectionByName(commpage_dbstr).get();
669 if (commpage_section)
670 {
671 FileSpec objfile_file_spec(objfile->GetFileSpec());
672 ModuleSP commpage_image_module_sp(m_process->GetTarget().GetImages().FindFirstModuleForFileSpec (objfile_file_spec, &commpage_dbstr));
673 if (commpage_image_module_sp.get() == NULL)
674 {
675 commpage_image_module_sp = m_process->GetTarget().GetSharedModule (m_dyld_image_infos[idx].file_spec,
676 arch_spec,
677 &m_dyld_image_infos[idx].uuid,
678 &commpage_dbstr,
679 objfile->GetOffset() + commpage_section->GetOffset());
680 UpdateCommPageLoadAddress(commpage_image_module_sp.get());
681 }
682 }
683 }
684 }
685
686 // UpdateImageLoadAddress will return true if any segments
687 // change load address. We need to check this so we don't
688 // mention that all loaded shared libraries are newly loaded
689 // each time we hit out dyld breakpoint since dyld will list all
690 // shared libraries each time.
691 if (UpdateImageLoadAddress (image_module_sp.get(), m_dyld_image_infos[idx]))
692 {
693 loaded_module_list.AppendInNeeded (image_module_sp);
694 }
695 }
696 }
Chris Lattner24943d22010-06-08 16:52:24 +0000697 if (loaded_module_list.GetSize() > 0)
698 {
699 // FIXME: This should really be in the Runtime handlers class, which should get
700 // called by the target's ModulesDidLoad, but we're doing it all locally for now
701 // to save time.
702 // Also, I'm assuming there can be only one libobjc dylib loaded...
703
Jim Inghamb66cd072010-09-28 01:25:32 +0000704 ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime();
705 if (objc_runtime != NULL && !objc_runtime->HasReadObjCLibrary())
Chris Lattner24943d22010-06-08 16:52:24 +0000706 {
707 size_t num_modules = loaded_module_list.GetSize();
708 for (int i = 0; i < num_modules; i++)
709 {
Jim Inghamb66cd072010-09-28 01:25:32 +0000710 if (objc_runtime->IsModuleObjCLibrary (loaded_module_list.GetModuleAtIndex (i)))
Chris Lattner24943d22010-06-08 16:52:24 +0000711 {
Jim Inghamb66cd072010-09-28 01:25:32 +0000712 objc_runtime->ReadObjCLibrary (loaded_module_list.GetModuleAtIndex (i));
Chris Lattner24943d22010-06-08 16:52:24 +0000713 break;
714 }
715 }
716 }
717 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
718 }
719 }
720 return m_dyld_image_infos.size();
721}
722
723//----------------------------------------------------------------------
724// Read a mach_header at ADDR into HEADER, and also fill in the load
725// command data into LOAD_COMMAND_DATA if it is non-NULL.
726//
727// Returns true if we succeed, false if we fail for any reason.
728//----------------------------------------------------------------------
729bool
Greg Clayton1674b122010-07-21 22:12:05 +0000730DynamicLoaderMacOSXDYLD::ReadMachHeader (lldb::addr_t addr, mach_header *header, DataExtractor *load_command_data)
Chris Lattner24943d22010-06-08 16:52:24 +0000731{
Greg Clayton1674b122010-07-21 22:12:05 +0000732 DataBufferHeap header_bytes(sizeof(mach_header), 0);
Chris Lattner24943d22010-06-08 16:52:24 +0000733 Error error;
734 size_t bytes_read = m_process->ReadMemory (addr,
735 header_bytes.GetBytes(),
736 header_bytes.GetByteSize(),
737 error);
Greg Clayton1674b122010-07-21 22:12:05 +0000738 if (bytes_read == sizeof(mach_header))
Chris Lattner24943d22010-06-08 16:52:24 +0000739 {
740 uint32_t offset = 0;
741 ::memset (header, 0, sizeof(header));
742
743 // Get the magic byte unswapped so we can figure out what we are dealing with
744 DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), eByteOrderHost, 4);
745 header->magic = data.GetU32(&offset);
746 lldb::addr_t load_cmd_addr = addr;
747 data.SetByteOrder(DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic(header->magic));
748 switch (header->magic)
749 {
Greg Clayton1674b122010-07-21 22:12:05 +0000750 case llvm::MachO::HeaderMagic32:
751 case llvm::MachO::HeaderMagic32Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000752 data.SetAddressByteSize(4);
Greg Clayton1674b122010-07-21 22:12:05 +0000753 load_cmd_addr += sizeof(mach_header);
Chris Lattner24943d22010-06-08 16:52:24 +0000754 break;
755
Greg Clayton1674b122010-07-21 22:12:05 +0000756 case llvm::MachO::HeaderMagic64:
757 case llvm::MachO::HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000758 data.SetAddressByteSize(8);
Greg Clayton1674b122010-07-21 22:12:05 +0000759 load_cmd_addr += sizeof(mach_header_64);
Chris Lattner24943d22010-06-08 16:52:24 +0000760 break;
761
762 default:
763 return false;
764 }
765
766 // Read the rest of dyld's mach header
Greg Clayton1674b122010-07-21 22:12:05 +0000767 if (data.GetU32(&offset, &header->cputype, (sizeof(mach_header)/sizeof(uint32_t)) - 1))
Chris Lattner24943d22010-06-08 16:52:24 +0000768 {
769 if (load_command_data == NULL)
770 return true; // We were able to read the mach_header and weren't asked to read the load command bytes
771
772 DataBufferSP load_cmd_data_sp(new DataBufferHeap(header->sizeofcmds, 0));
773
774 size_t load_cmd_bytes_read = m_process->ReadMemory (load_cmd_addr,
775 load_cmd_data_sp->GetBytes(),
776 load_cmd_data_sp->GetByteSize(),
777 error);
778
779 if (load_cmd_bytes_read == header->sizeofcmds)
780 {
781 // Set the load command data and also set the correct endian
782 // swap settings and the correct address size
783 load_command_data->SetData(load_cmd_data_sp, 0, header->sizeofcmds);
784 load_command_data->SetByteOrder(data.GetByteOrder());
785 load_command_data->SetAddressByteSize(data.GetAddressByteSize());
786 return true; // We successfully read the mach_header and the load command data
787 }
788
789 return false; // We weren't able to read the load command data
790 }
791 }
792 return false; // We failed the read the mach_header
793}
794
795
796//----------------------------------------------------------------------
797// Parse the load commands for an image
798//----------------------------------------------------------------------
799uint32_t
800DynamicLoaderMacOSXDYLD::ParseLoadCommands (const DataExtractor& data, struct DYLDImageInfo& dylib_info, FileSpec *lc_id_dylinker)
801{
802 uint32_t offset = 0;
803 uint32_t cmd_idx;
804 Segment segment;
805 dylib_info.Clear (true);
806
807 for (cmd_idx = 0; cmd_idx < dylib_info.header.ncmds; cmd_idx++)
808 {
809 // Clear out any load command specific data from DYLIB_INFO since
810 // we are about to read it.
811
Greg Clayton1674b122010-07-21 22:12:05 +0000812 if (data.ValidOffsetForDataOfSize (offset, sizeof(load_command)))
Chris Lattner24943d22010-06-08 16:52:24 +0000813 {
Greg Clayton1674b122010-07-21 22:12:05 +0000814 load_command load_cmd;
Chris Lattner24943d22010-06-08 16:52:24 +0000815 uint32_t load_cmd_offset = offset;
816 load_cmd.cmd = data.GetU32 (&offset);
817 load_cmd.cmdsize = data.GetU32 (&offset);
818 switch (load_cmd.cmd)
819 {
Greg Clayton1674b122010-07-21 22:12:05 +0000820 case LoadCommandSegment32:
Chris Lattner24943d22010-06-08 16:52:24 +0000821 {
822 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
823 segment.addr = data.GetU32 (&offset);
824 segment.size = data.GetU32 (&offset);
825 dylib_info.segments.push_back (segment);
826 }
827 break;
828
Greg Clayton1674b122010-07-21 22:12:05 +0000829 case LoadCommandSegment64:
Chris Lattner24943d22010-06-08 16:52:24 +0000830 {
831 segment.name.SetTrimmedCStringWithLength ((const char *)data.GetData(&offset, 16), 16);
832 segment.addr = data.GetU64 (&offset);
833 segment.size = data.GetU64 (&offset);
834 dylib_info.segments.push_back (segment);
835 }
836 break;
837
Greg Clayton1674b122010-07-21 22:12:05 +0000838 case LoadCommandDynamicLinkerIdent:
Chris Lattner24943d22010-06-08 16:52:24 +0000839 if (lc_id_dylinker)
840 {
841 uint32_t name_offset = load_cmd_offset + data.GetU32 (&offset);
842 const char *path = data.PeekCStr (name_offset);
Greg Clayton537a7a82010-10-20 20:54:39 +0000843 lc_id_dylinker->SetFile (path, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000844 }
845 break;
846
Greg Clayton1674b122010-07-21 22:12:05 +0000847 case LoadCommandUUID:
Chris Lattner24943d22010-06-08 16:52:24 +0000848 dylib_info.uuid.SetBytes(data.GetData (&offset, 16));
849 break;
850
851 default:
852 break;
853 }
854 // Set offset to be the beginning of the next load command.
855 offset = load_cmd_offset + load_cmd.cmdsize;
856 }
857 }
858 return cmd_idx;
859}
860
861//----------------------------------------------------------------------
862// Read the mach_header and load commands for each image that the
863// _dyld_all_image_infos structure points to and cache the results.
864//----------------------------------------------------------------------
865void
866DynamicLoaderMacOSXDYLD::UpdateAllImageInfosHeaderAndLoadCommands()
867{
868 uint32_t exe_idx = UINT32_MAX;
869 // Read any UUID values that we can get
870 for (uint32_t i = 0; i < m_dyld_all_image_infos.dylib_info_count; i++)
871 {
872 if (!m_dyld_image_infos[i].UUIDValid())
873 {
874 DataExtractor data; // Load command data
875 if (!ReadMachHeader (m_dyld_image_infos[i].address, &m_dyld_image_infos[i].header, &data))
876 continue;
877
878 ParseLoadCommands (data, m_dyld_image_infos[i], NULL);
879
Greg Clayton1674b122010-07-21 22:12:05 +0000880 if (m_dyld_image_infos[i].header.filetype == HeaderFileTypeExecutable)
Chris Lattner24943d22010-06-08 16:52:24 +0000881 exe_idx = i;
882 }
883 }
884
885 if (exe_idx < m_dyld_image_infos.size())
886 {
887 bool set_executable = false;
Greg Claytoncf015052010-06-11 03:25:34 +0000888 ArchSpec dyld_exe_arch_spec(eArchTypeMachO, m_dyld_image_infos[exe_idx].header.cputype, m_dyld_image_infos[exe_idx].header.cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +0000889 ModuleSP exe_module_sp(m_process->GetTarget().GetExecutableModule());
890 if (exe_module_sp.get())
891 {
892 if (exe_module_sp->GetFileSpec() != m_dyld_image_infos[exe_idx].file_spec ||
893 exe_module_sp->GetArchitecture() != dyld_exe_arch_spec)
894 set_executable = true;
895 }
896 else
897 set_executable = true;
898
899 if (set_executable)
900 {
901 exe_module_sp = m_process->GetTarget().GetSharedModule (m_dyld_image_infos[exe_idx].file_spec,
902 dyld_exe_arch_spec,
903 &m_dyld_image_infos[exe_idx].uuid);
904 if (exe_module_sp.get())
905 {
906 // If we found the file where it purported to be, then it should
907 // be safe to load dependent images.
908 bool get_dependent_images = exe_module_sp->GetFileSpec() == m_dyld_image_infos[exe_idx].file_spec;
909
910 m_process->GetTarget().SetExecutableModule (exe_module_sp, get_dependent_images);
911 }
912 }
913 }
914}
915
916//----------------------------------------------------------------------
917// Dump a Segment to the file handle provided.
918//----------------------------------------------------------------------
919void
920DynamicLoaderMacOSXDYLD::Segment::PutToLog (Log *log, lldb::addr_t slide) const
921{
922 if (log)
923 log->Printf("\t\t%16s [0x%16.16llx - 0x%16.16llx)", name.AsCString(""), addr + slide, addr + slide + size);
924}
925
926const DynamicLoaderMacOSXDYLD::Segment *
927DynamicLoaderMacOSXDYLD::DYLDImageInfo::FindSegment (const ConstString &name) const
928{
929 const size_t num_segments = segments.size();
930 for (size_t i=0; i<num_segments; ++i)
931 {
932 if (segments[i].name == name)
933 return &segments[i];
934 }
935 return NULL;
936}
937
938
939//----------------------------------------------------------------------
940// Dump an image info structure to the file handle provided.
941//----------------------------------------------------------------------
942void
943DynamicLoaderMacOSXDYLD::DYLDImageInfo::PutToLog (Log *log) const
944{
945 if (log == NULL)
946 return;
947 uint8_t *u = (uint8_t *)uuid.GetBytes();
948
949 if (address == LLDB_INVALID_ADDRESS)
950 {
951 if (u)
952 {
953 log->Printf("\t modtime=0x%8.8llx 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 path='%s/%s' (UNLOADED)",
954 mod_date,
955 u[ 0], u[ 1], u[ 2], u[ 3],
956 u[ 4], u[ 5], u[ 6], u[ 7],
957 u[ 8], u[ 9], u[10], u[11],
958 u[12], u[13], u[14], u[15],
959 file_spec.GetDirectory().AsCString(),
960 file_spec.GetFilename().AsCString());
961 }
962 else
963 log->Printf("\t modtime=0x%8.8llx path='%s/%s' (UNLOADED)",
964 mod_date,
965 file_spec.GetDirectory().AsCString(),
966 file_spec.GetFilename().AsCString());
967 }
968 else
969 {
970 if (u)
971 {
972 log->Printf("\taddress=0x%16.16llx modtime=0x%8.8llx 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 path='%s/%s'",
973 address,
974 mod_date,
975 u[ 0], u[ 1], u[ 2], u[ 3],
976 u[ 4], u[ 5], u[ 6], u[ 7],
977 u[ 8], u[ 9], u[10], u[11],
978 u[12], u[13], u[14], u[15],
979 file_spec.GetDirectory().AsCString(),
980 file_spec.GetFilename().AsCString());
981 }
982 else
983 {
984 log->Printf("\taddress=0x%16.16llx modtime=0x%8.8llx path='%s/%s'",
985 address,
986 mod_date,
987 file_spec.GetDirectory().AsCString(),
988 file_spec.GetFilename().AsCString());
989
990 }
991 for (uint32_t i=0; i<segments.size(); ++i)
992 segments[i].PutToLog(log, slide);
993 }
994}
995
996//----------------------------------------------------------------------
997// Dump the _dyld_all_image_infos members and all current image infos
998// that we have parsed to the file handle provided.
999//----------------------------------------------------------------------
1000void
1001DynamicLoaderMacOSXDYLD::PutToLog(Log *log) const
1002{
1003 if (log == NULL)
1004 return;
1005
1006 Mutex::Locker locker(m_mutex);
1007 log->Printf("dyld_all_image_infos = { version=%d, count=%d, addr=0x%8.8llx, notify=0x%8.8llx }",
1008 m_dyld_all_image_infos.version,
1009 m_dyld_all_image_infos.dylib_info_count,
1010 (uint64_t)m_dyld_all_image_infos.dylib_info_addr,
1011 (uint64_t)m_dyld_all_image_infos.notification);
1012 size_t i;
1013 const size_t count = m_dyld_image_infos.size();
1014 if (count > 0)
1015 {
Greg Clayton9d2993d2010-11-03 04:08:06 +00001016 log->PutCString("Loaded:");
Chris Lattner24943d22010-06-08 16:52:24 +00001017 for (i = 0; i<count; i++)
1018 m_dyld_image_infos[i].PutToLog(log);
1019 }
1020}
1021
Chris Lattner24943d22010-06-08 16:52:24 +00001022void
1023DynamicLoaderMacOSXDYLD::PrivateInitialize(Process *process)
1024{
1025 DEBUG_PRINTF("DynamicLoaderMacOSXDYLD::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
1026 Clear(true);
1027 m_process = process;
Greg Clayton58e844b2010-12-08 05:08:21 +00001028 m_process->GetTarget().GetSectionLoadList().Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00001029}
1030
1031bool
1032DynamicLoaderMacOSXDYLD::SetNotificationBreakpoint ()
1033{
1034 DEBUG_PRINTF("DynamicLoaderMacOSXDYLD::%s() process state = %s\n", __FUNCTION__, StateAsCString(m_process->GetState()));
1035 if (m_break_id == LLDB_INVALID_BREAK_ID)
1036 {
1037 if (m_dyld_all_image_infos.notification != LLDB_INVALID_ADDRESS)
1038 {
1039 Address so_addr;
1040 // Set the notification breakpoint and install a breakpoint
1041 // callback function that will get called each time the
1042 // breakpoint gets hit. We will use this to track when shared
1043 // libraries get loaded/unloaded.
1044
Greg Claytoneea26402010-09-14 23:36:40 +00001045 if (m_process->GetTarget().GetSectionLoadList().ResolveLoadAddress(m_dyld_all_image_infos.notification, so_addr))
Chris Lattner24943d22010-06-08 16:52:24 +00001046 {
1047 Breakpoint *dyld_break = m_process->GetTarget().CreateBreakpoint (so_addr, true).get();
1048 dyld_break->SetCallback (DynamicLoaderMacOSXDYLD::NotifyBreakpointHit, this, true);
1049 m_break_id = dyld_break->GetID();
1050 }
1051 }
1052 }
1053 return m_break_id != LLDB_INVALID_BREAK_ID;
1054}
1055
Jim Ingham7508e732010-08-09 23:31:02 +00001056//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001057// Member function that gets called when the process state changes.
1058//----------------------------------------------------------------------
1059void
1060DynamicLoaderMacOSXDYLD::PrivateProcessStateChanged (Process *process, StateType state)
1061{
1062 DEBUG_PRINTF("DynamicLoaderMacOSXDYLD::%s(%s)\n", __FUNCTION__, StateAsCString(state));
1063 switch (state)
1064 {
1065 case eStateAttaching:
1066 case eStateLaunching:
1067 case eStateInvalid:
1068 case eStateUnloaded:
1069 case eStateExited:
1070 case eStateDetached:
1071 Clear(false);
1072 break;
1073
1074 case eStateStopped:
1075 // Keep trying find dyld and set our notification breakpoint each time
1076 // we stop until we succeed
1077 if (!DidSetNotificationBreakpoint () && m_process->IsAlive())
1078 {
1079 if (NeedToLocateDYLD ())
1080 LocateDYLD ();
1081
1082 SetNotificationBreakpoint ();
1083 }
1084 break;
1085
1086 case eStateRunning:
1087 case eStateStepping:
1088 case eStateCrashed:
1089 case eStateSuspended:
1090 break;
1091
1092 default:
1093 break;
1094 }
1095}
1096
1097ThreadPlanSP
1098DynamicLoaderMacOSXDYLD::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
1099{
1100 ThreadPlanSP thread_plan_sp;
1101 StackFrame *current_frame = thread.GetStackFrameAtIndex(0).get();
1102 const SymbolContext &current_context = current_frame->GetSymbolContext(eSymbolContextSymbol);
1103 Symbol *current_symbol = current_context.symbol;
Greg Claytone005f2c2010-11-06 01:53:30 +00001104 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +00001105
1106 if (current_symbol != NULL)
1107 {
1108 if (current_symbol->IsTrampoline())
1109 {
Jim Ingham17454cf2010-09-14 22:03:00 +00001110 const ConstString &trampoline_name = current_symbol->GetMangled().GetName(Mangled::ePreferMangled);
1111
Chris Lattner24943d22010-06-08 16:52:24 +00001112 if (trampoline_name)
1113 {
1114 SymbolContextList target_symbols;
1115 ModuleList &images = thread.GetProcess().GetTarget().GetImages();
1116 images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode, target_symbols);
1117 // FIXME - Make the Run to Address take multiple addresses, and
1118 // run to any of them.
Jim Ingham17454cf2010-09-14 22:03:00 +00001119 uint32_t num_symbols = target_symbols.GetSize();
1120 if (num_symbols == 1)
Chris Lattner24943d22010-06-08 16:52:24 +00001121 {
1122 SymbolContext context;
1123 AddressRange addr_range;
1124 if (target_symbols.GetContextAtIndex(0, context))
1125 {
1126 context.GetAddressRange (eSymbolContextEverything, addr_range);
1127 thread_plan_sp.reset (new ThreadPlanRunToAddress (thread, addr_range.GetBaseAddress(), stop_others));
1128 }
Jim Ingham17454cf2010-09-14 22:03:00 +00001129 else
Chris Lattner24943d22010-06-08 16:52:24 +00001130 {
Jim Ingham17454cf2010-09-14 22:03:00 +00001131 if (log)
1132 log->Printf ("Couldn't resolve the symbol context.");
1133 }
1134 }
1135 else if (num_symbols > 1)
1136 {
1137 std::vector<lldb::addr_t> addresses;
1138 addresses.resize (num_symbols);
1139 for (uint32_t i = 0; i < num_symbols; i++)
1140 {
1141 SymbolContext context;
1142 AddressRange addr_range;
1143 if (target_symbols.GetContextAtIndex(i, context))
1144 {
1145 context.GetAddressRange (eSymbolContextEverything, addr_range);
Greg Claytoneea26402010-09-14 23:36:40 +00001146 lldb::addr_t load_addr = addr_range.GetBaseAddress().GetLoadAddress(&thread.GetProcess().GetTarget());
Jim Ingham17454cf2010-09-14 22:03:00 +00001147 addresses[i] = load_addr;
1148 }
1149 }
1150 if (addresses.size() > 0)
1151 thread_plan_sp.reset (new ThreadPlanRunToAddress (thread, addresses, stop_others));
1152 else
1153 {
1154 if (log)
1155 log->Printf ("Couldn't resolve the symbol contexts.");
Chris Lattner24943d22010-06-08 16:52:24 +00001156 }
1157 }
1158 else
1159 {
Chris Lattner24943d22010-06-08 16:52:24 +00001160 if (log)
1161 {
1162 log->Printf ("Could not find symbol for trampoline target: \"%s\"", trampoline_name.AsCString());
1163 }
1164 }
1165 }
1166 }
1167 }
Jim Ingham17454cf2010-09-14 22:03:00 +00001168 else
1169 {
1170 if (log)
1171 log->Printf ("Could not find symbol for step through.");
1172 }
Chris Lattner24943d22010-06-08 16:52:24 +00001173
Chris Lattner24943d22010-06-08 16:52:24 +00001174 return thread_plan_sp;
1175}
1176
Greg Clayton0baa3942010-11-04 01:54:29 +00001177Error
1178DynamicLoaderMacOSXDYLD::CanLoadImage ()
1179{
1180 Error error;
1181 // In order for us to tell if we can load a shared library we verify that
1182 // the dylib_info_addr isn't zero (which means no shared libraries have
1183 // been set yet, or dyld is currently mucking with the shared library list).
1184 if (ReadAllImageInfosStructure ())
1185 {
1186 // TODO: also check the _dyld_global_lock_held variable in libSystem.B.dylib?
1187 // TODO: check the malloc lock?
1188 // TODO: check the objective C lock?
1189 if (m_dyld_all_image_infos.dylib_info_addr != 0)
1190 return error; // Success
1191 }
1192
1193 error.SetErrorString("unsafe to load or unload shared libraries");
1194 return error;
1195}
1196
Chris Lattner24943d22010-06-08 16:52:24 +00001197void
1198DynamicLoaderMacOSXDYLD::Initialize()
1199{
1200 PluginManager::RegisterPlugin (GetPluginNameStatic(),
1201 GetPluginDescriptionStatic(),
1202 CreateInstance);
1203}
1204
1205void
1206DynamicLoaderMacOSXDYLD::Terminate()
1207{
1208 PluginManager::UnregisterPlugin (CreateInstance);
1209}
1210
1211
1212const char *
1213DynamicLoaderMacOSXDYLD::GetPluginNameStatic()
1214{
1215 return "dynamic-loader.macosx-dyld";
1216}
1217
1218const char *
1219DynamicLoaderMacOSXDYLD::GetPluginDescriptionStatic()
1220{
1221 return "Dynamic loader plug-in that watches for shared library loads/unloads in MacOSX user processes.";
1222}
1223
1224
1225//------------------------------------------------------------------
1226// PluginInterface protocol
1227//------------------------------------------------------------------
1228const char *
1229DynamicLoaderMacOSXDYLD::GetPluginName()
1230{
1231 return "DynamicLoaderMacOSXDYLD";
1232}
1233
1234const char *
1235DynamicLoaderMacOSXDYLD::GetShortPluginName()
1236{
1237 return GetPluginNameStatic();
1238}
1239
1240uint32_t
1241DynamicLoaderMacOSXDYLD::GetPluginVersion()
1242{
1243 return 1;
1244}
1245
1246void
1247DynamicLoaderMacOSXDYLD::GetPluginCommandHelp (const char *command, Stream *strm)
1248{
1249}
1250
1251Error
1252DynamicLoaderMacOSXDYLD::ExecutePluginCommand (Args &command, Stream *strm)
1253{
1254 Error error;
1255 error.SetErrorString("No plug-in command are currently supported.");
1256 return error;
1257}
1258
1259Log *
1260DynamicLoaderMacOSXDYLD::EnablePluginLogging (Stream *strm, Args &command)
1261{
1262 return NULL;
1263}
1264
1265