1 //===-- DynamicLoaderPOSIX.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
13 #include "lldb/Core/PluginManager.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Target/Thread.h"
22 #include "lldb/Target/ThreadPlanRunToAddress.h"
23 #include "lldb/Breakpoint/BreakpointLocation.h"
25 #include "AuxVector.h"
26 #include "DynamicLoaderPOSIXDYLD.h"
29 using namespace lldb_private;
32 DynamicLoaderPOSIXDYLD::Initialize()
34 PluginManager::RegisterPlugin(GetPluginNameStatic(),
35 GetPluginDescriptionStatic(),
40 DynamicLoaderPOSIXDYLD::Terminate()
44 lldb_private::ConstString
45 DynamicLoaderPOSIXDYLD::GetPluginName()
47 return GetPluginNameStatic();
50 lldb_private::ConstString
51 DynamicLoaderPOSIXDYLD::GetPluginNameStatic()
53 static ConstString g_name("linux-dyld");
58 DynamicLoaderPOSIXDYLD::GetPluginDescriptionStatic()
60 return "Dynamic loader plug-in that watches for shared library "
61 "loads/unloads in POSIX processes.";
65 DynamicLoaderPOSIXDYLD::GetPluginCommandHelp(const char *command, Stream *strm)
70 DynamicLoaderPOSIXDYLD::GetPluginVersion()
76 DynamicLoaderPOSIXDYLD::CreateInstance(Process *process, bool force)
81 const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
82 if (triple_ref.getOS() == llvm::Triple::Linux ||
83 triple_ref.getOS() == llvm::Triple::FreeBSD)
88 return new DynamicLoaderPOSIXDYLD (process);
92 DynamicLoaderPOSIXDYLD::DynamicLoaderPOSIXDYLD(Process *process)
93 : DynamicLoader(process),
94 m_rendezvous(process),
95 m_load_offset(LLDB_INVALID_ADDRESS),
96 m_entry_point(LLDB_INVALID_ADDRESS),
98 m_dyld_bid(LLDB_INVALID_BREAK_ID)
102 DynamicLoaderPOSIXDYLD::~DynamicLoaderPOSIXDYLD()
104 if (m_dyld_bid != LLDB_INVALID_BREAK_ID)
106 m_process->GetTarget().RemoveBreakpointByID (m_dyld_bid);
107 m_dyld_bid = LLDB_INVALID_BREAK_ID;
112 DynamicLoaderPOSIXDYLD::DidAttach()
117 m_auxv.reset(new AuxVector(m_process));
119 executable = GetTargetExecutable();
120 load_offset = ComputeLoadOffset();
122 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS)
124 ModuleList module_list;
125 module_list.Append(executable);
126 UpdateLoadedSections(executable, load_offset);
127 LoadAllCurrentModules();
128 m_process->GetTarget().ModulesDidLoad(module_list);
133 DynamicLoaderPOSIXDYLD::DidLaunch()
138 m_auxv.reset(new AuxVector(m_process));
140 executable = GetTargetExecutable();
141 load_offset = ComputeLoadOffset();
143 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS)
145 ModuleList module_list;
146 module_list.Append(executable);
147 UpdateLoadedSections(executable, load_offset);
149 m_process->GetTarget().ModulesDidLoad(module_list);
154 DynamicLoaderPOSIXDYLD::GetTargetExecutable()
156 Target &target = m_process->GetTarget();
157 ModuleSP executable = target.GetExecutableModule();
159 if (executable.get())
161 if (executable->GetFileSpec().Exists())
163 ModuleSpec module_spec (executable->GetFileSpec(), executable->GetArchitecture());
164 ModuleSP module_sp (new Module (module_spec));
166 // Check if the executable has changed and set it to the target executable if they differ.
167 if (module_sp.get() && module_sp->GetUUID().IsValid() && executable->GetUUID().IsValid())
169 if (module_sp->GetUUID() != executable->GetUUID())
172 else if (executable->FileHasChanged())
177 if (!executable.get())
179 executable = target.GetSharedModule(module_spec);
180 if (executable.get() != target.GetExecutableModulePointer())
182 // Don't load dependent images since we are in dyld where we will know
183 // and find out about all images that are loaded
184 const bool get_dependent_images = false;
185 target.SetExecutableModule(executable, get_dependent_images);
194 DynamicLoaderPOSIXDYLD::ExecutePluginCommand(Args &command, Stream *strm)
200 DynamicLoaderPOSIXDYLD::EnablePluginLogging(Stream *strm, Args &command)
206 DynamicLoaderPOSIXDYLD::CanLoadImage()
212 DynamicLoaderPOSIXDYLD::UpdateLoadedSections(ModuleSP module, addr_t base_addr)
214 ObjectFile *obj_file = module->GetObjectFile();
215 SectionList *sections = obj_file->GetSectionList();
216 SectionLoadList &load_list = m_process->GetTarget().GetSectionLoadList();
217 const size_t num_sections = sections->GetSize();
219 for (unsigned i = 0; i < num_sections; ++i)
221 SectionSP section_sp (sections->GetSectionAtIndex(i));
222 lldb::addr_t new_load_addr = section_sp->GetFileAddress() + base_addr;
223 lldb::addr_t old_load_addr = load_list.GetSectionLoadAddress(section_sp);
225 // If the file address of the section is zero then this is not an
226 // allocatable/loadable section (property of ELF sh_addr). Skip it.
227 if (new_load_addr == base_addr)
230 if (old_load_addr == LLDB_INVALID_ADDRESS ||
231 old_load_addr != new_load_addr)
232 load_list.SetSectionLoadAddress(section_sp, new_load_addr);
237 DynamicLoaderPOSIXDYLD::ProbeEntry()
239 Breakpoint *entry_break;
242 if ((entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS)
245 entry_break = m_process->GetTarget().CreateBreakpoint(entry, true).get();
246 entry_break->SetCallback(EntryBreakpointHit, this, true);
247 entry_break->SetBreakpointKind("shared-library-event");
250 // The runtime linker has run and initialized the rendezvous structure once the
251 // process has hit its entry point. When we hit the corresponding breakpoint we
252 // interrogate the rendezvous structure to get the load addresses of all
253 // dependent modules for the process. Similarly, we can discover the runtime
254 // linker function and setup a breakpoint to notify us of any dynamically loaded
255 // modules (via dlopen).
257 DynamicLoaderPOSIXDYLD::EntryBreakpointHit(void *baton,
258 StoppointCallbackContext *context,
260 user_id_t break_loc_id)
262 DynamicLoaderPOSIXDYLD* dyld_instance;
264 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton);
265 dyld_instance->LoadAllCurrentModules();
266 dyld_instance->SetRendezvousBreakpoint();
267 return false; // Continue running.
271 DynamicLoaderPOSIXDYLD::SetRendezvousBreakpoint()
273 addr_t break_addr = m_rendezvous.GetBreakAddress();
274 Target &target = m_process->GetTarget();
276 if (m_dyld_bid == LLDB_INVALID_BREAK_ID)
278 Breakpoint *dyld_break = target.CreateBreakpoint (break_addr, true).get();
279 dyld_break->SetCallback(RendezvousBreakpointHit, this, true);
280 dyld_break->SetBreakpointKind ("shared-library-event");
281 m_dyld_bid = dyld_break->GetID();
284 // Make sure our breakpoint is at the right address.
285 assert (target.GetBreakpointByID(m_dyld_bid)->FindLocationByAddress(break_addr)->GetBreakpoint().GetID() == m_dyld_bid);
289 DynamicLoaderPOSIXDYLD::RendezvousBreakpointHit(void *baton,
290 StoppointCallbackContext *context,
292 user_id_t break_loc_id)
294 DynamicLoaderPOSIXDYLD* dyld_instance;
296 dyld_instance = static_cast<DynamicLoaderPOSIXDYLD*>(baton);
297 dyld_instance->RefreshModules();
299 // Return true to stop the target, false to just let the target run.
300 return dyld_instance->GetStopWhenImagesChange();
304 DynamicLoaderPOSIXDYLD::RefreshModules()
306 if (!m_rendezvous.Resolve())
309 DYLDRendezvous::iterator I;
310 DYLDRendezvous::iterator E;
312 ModuleList &loaded_modules = m_process->GetTarget().GetImages();
314 if (m_rendezvous.ModulesDidLoad())
316 ModuleList new_modules;
318 E = m_rendezvous.loaded_end();
319 for (I = m_rendezvous.loaded_begin(); I != E; ++I)
321 FileSpec file(I->path.c_str(), true);
322 ModuleSP module_sp = LoadModuleAtAddress(file, I->base_addr);
324 loaded_modules.AppendIfNeeded(module_sp);
328 if (m_rendezvous.ModulesDidUnload())
330 ModuleList old_modules;
332 E = m_rendezvous.unloaded_end();
333 for (I = m_rendezvous.unloaded_begin(); I != E; ++I)
335 FileSpec file(I->path.c_str(), true);
336 ModuleSpec module_spec (file);
338 loaded_modules.FindFirstModule (module_spec);
340 old_modules.Append(module_sp);
342 loaded_modules.Remove(old_modules);
347 DynamicLoaderPOSIXDYLD::GetStepThroughTrampolinePlan(Thread &thread, bool stop)
349 ThreadPlanSP thread_plan_sp;
351 StackFrame *frame = thread.GetStackFrameAtIndex(0).get();
352 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol);
353 Symbol *sym = context.symbol;
355 if (sym == NULL || !sym->IsTrampoline())
356 return thread_plan_sp;
358 const ConstString &sym_name = sym->GetMangled().GetName(Mangled::ePreferMangled);
360 return thread_plan_sp;
362 SymbolContextList target_symbols;
363 Target &target = thread.GetProcess()->GetTarget();
364 const ModuleList &images = target.GetImages();
366 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols);
367 size_t num_targets = target_symbols.GetSize();
369 return thread_plan_sp;
371 typedef std::vector<lldb::addr_t> AddressVector;
373 for (size_t i = 0; i < num_targets; ++i)
375 SymbolContext context;
377 if (target_symbols.GetContextAtIndex(i, context))
379 context.GetAddressRange(eSymbolContextEverything, 0, false, range);
380 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
381 if (addr != LLDB_INVALID_ADDRESS)
382 addrs.push_back(addr);
386 if (addrs.size() > 0)
388 AddressVector::iterator start = addrs.begin();
389 AddressVector::iterator end = addrs.end();
391 std::sort(start, end);
392 addrs.erase(std::unique(start, end), end);
393 thread_plan_sp.reset(new ThreadPlanRunToAddress(thread, addrs, stop));
396 return thread_plan_sp;
400 DynamicLoaderPOSIXDYLD::LoadAllCurrentModules()
402 DYLDRendezvous::iterator I;
403 DYLDRendezvous::iterator E;
404 ModuleList module_list;
406 if (!m_rendezvous.Resolve())
409 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I)
411 FileSpec file(I->path.c_str(), false);
412 ModuleSP module_sp = LoadModuleAtAddress(file, I->base_addr);
414 module_list.Append(module_sp);
417 m_process->GetTarget().ModulesDidLoad(module_list);
421 DynamicLoaderPOSIXDYLD::LoadModuleAtAddress(const FileSpec &file, addr_t base_addr)
423 Target &target = m_process->GetTarget();
424 ModuleList &modules = target.GetImages();
427 ModuleSpec module_spec (file, target.GetArchitecture());
428 if ((module_sp = modules.FindFirstModule (module_spec)))
430 UpdateLoadedSections(module_sp, base_addr);
432 else if ((module_sp = target.GetSharedModule(module_spec)))
434 UpdateLoadedSections(module_sp, base_addr);
441 DynamicLoaderPOSIXDYLD::ComputeLoadOffset()
445 if (m_load_offset != LLDB_INVALID_ADDRESS)
446 return m_load_offset;
448 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS)
449 return LLDB_INVALID_ADDRESS;
451 ModuleSP module = m_process->GetTarget().GetExecutableModule();
453 return LLDB_INVALID_ADDRESS;
455 ObjectFile *exe = module->GetObjectFile();
456 Address file_entry = exe->GetEntryPointAddress();
458 if (!file_entry.IsValid())
459 return LLDB_INVALID_ADDRESS;
461 m_load_offset = virt_entry - file_entry.GetFileAddress();
462 return m_load_offset;
466 DynamicLoaderPOSIXDYLD::GetEntryPoint()
468 if (m_entry_point != LLDB_INVALID_ADDRESS)
469 return m_entry_point;
471 if (m_auxv.get() == NULL)
472 return LLDB_INVALID_ADDRESS;
474 AuxVector::iterator I = m_auxv->FindEntry(AuxVector::AT_ENTRY);
476 if (I == m_auxv->end())
477 return LLDB_INVALID_ADDRESS;
479 m_entry_point = static_cast<addr_t>(I->value);
480 return m_entry_point;