LLDB mainline
DynamicLoaderPOSIXDYLD.cpp
Go to the documentation of this file.
1//===-- DynamicLoaderPOSIXDYLD.cpp ----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// Main header include
11
13#include "lldb/Core/Module.h"
16#include "lldb/Core/Section.h"
21#include "lldb/Target/Target.h"
22#include "lldb/Target/Thread.h"
25#include "lldb/Utility/Log.h"
27
28#include <memory>
29#include <optional>
30
31using namespace lldb;
32using namespace lldb_private;
33
35
36void DynamicLoaderPOSIXDYLD::Initialize() {
37 PluginManager::RegisterPlugin(GetPluginNameStatic(),
38 GetPluginDescriptionStatic(), CreateInstance);
39}
40
42
44 return "Dynamic loader plug-in that watches for shared library "
45 "loads/unloads in POSIX processes.";
46}
47
49 bool force) {
50 bool create = force;
51 if (!create) {
52 const llvm::Triple &triple_ref =
53 process->GetTarget().GetArchitecture().GetTriple();
54 if (triple_ref.getOS() == llvm::Triple::FreeBSD ||
55 triple_ref.getOS() == llvm::Triple::Linux ||
56 triple_ref.getOS() == llvm::Triple::NetBSD ||
57 triple_ref.getOS() == llvm::Triple::OpenBSD)
58 create = true;
59 }
60
61 if (create)
62 return new DynamicLoaderPOSIXDYLD(process);
63 return nullptr;
64}
65
67 : DynamicLoader(process), m_rendezvous(process),
68 m_load_offset(LLDB_INVALID_ADDRESS), m_entry_point(LLDB_INVALID_ADDRESS),
69 m_auxv(), m_dyld_bid(LLDB_INVALID_BREAK_ID),
70 m_vdso_base(LLDB_INVALID_ADDRESS),
71 m_interpreter_base(LLDB_INVALID_ADDRESS), m_initial_modules_added(false) {
72}
73
78 }
79}
80
82 Log *log = GetLog(LLDBLog::DynamicLoader);
83 LLDB_LOGF(log, "DynamicLoaderPOSIXDYLD::%s() pid %" PRIu64, __FUNCTION__,
85 m_auxv = std::make_unique<AuxVector>(m_process->GetAuxvData());
86
88 log, "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64 " reloaded auxv data",
90
91 ModuleSP executable_sp = GetTargetExecutable();
92 ResolveExecutableModule(executable_sp);
94
95 // find the main process load offset
96 addr_t load_offset = ComputeLoadOffset();
97 LLDB_LOGF(log,
98 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
99 " executable '%s', load_offset 0x%" PRIx64,
100 __FUNCTION__,
102 executable_sp ? executable_sp->GetFileSpec().GetPath().c_str()
103 : "<null executable>",
104 load_offset);
105
107
108 // if we dont have a load address we cant re-base
109 bool rebase_exec = load_offset != LLDB_INVALID_ADDRESS;
110
111 // if we have a valid executable
112 if (executable_sp.get()) {
113 lldb_private::ObjectFile *obj = executable_sp->GetObjectFile();
114 if (obj) {
115 // don't rebase if the module already has a load address
116 Target &target = m_process->GetTarget();
117 Address addr = obj->GetImageInfoAddress(&target);
118 if (addr.GetLoadAddress(&target) != LLDB_INVALID_ADDRESS)
119 rebase_exec = false;
120 }
121 } else {
122 // no executable, nothing to re-base
123 rebase_exec = false;
124 }
125
126 // if the target executable should be re-based
127 if (rebase_exec) {
128 ModuleList module_list;
129
130 module_list.Append(executable_sp);
131 LLDB_LOGF(log,
132 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
133 " added executable '%s' to module load list",
134 __FUNCTION__,
136 executable_sp->GetFileSpec().GetPath().c_str());
137
138 UpdateLoadedSections(executable_sp, LLDB_INVALID_ADDRESS, load_offset,
139 true);
140
142
143 m_process->GetTarget().ModulesDidLoad(module_list);
144 if (log) {
145 LLDB_LOGF(log,
146 "DynamicLoaderPOSIXDYLD::%s told the target about the "
147 "modules that loaded:",
148 __FUNCTION__);
149 for (auto module_sp : module_list.Modules()) {
150 LLDB_LOGF(log, "-- [module] %s (pid %" PRIu64 ")",
151 module_sp ? module_sp->GetFileSpec().GetPath().c_str()
152 : "<null>",
154 }
155 }
156 }
157
158 if (executable_sp.get()) {
160 // If we cannot establish rendezvous breakpoint right now we'll try again
161 // at entry point.
162 ProbeEntry();
163 }
164 }
165}
166
168 Log *log = GetLog(LLDBLog::DynamicLoader);
169 LLDB_LOGF(log, "DynamicLoaderPOSIXDYLD::%s()", __FUNCTION__);
170
171 ModuleSP executable;
172 addr_t load_offset;
173
174 m_auxv = std::make_unique<AuxVector>(m_process->GetAuxvData());
175
176 executable = GetTargetExecutable();
177 load_offset = ComputeLoadOffset();
179
180 if (executable.get() && load_offset != LLDB_INVALID_ADDRESS) {
181 ModuleList module_list;
182 module_list.Append(executable);
183 UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_offset, true);
184
185 LLDB_LOGF(log, "DynamicLoaderPOSIXDYLD::%s about to call ProbeEntry()",
186 __FUNCTION__);
187
189 // If we cannot establish rendezvous breakpoint right now we'll try again
190 // at entry point.
191 ProbeEntry();
192 }
193
194 LoadVDSO();
195 m_process->GetTarget().ModulesDidLoad(module_list);
196 }
197}
198
200
202 addr_t link_map_addr,
203 addr_t base_addr,
204 bool base_addr_is_offset) {
205 m_loaded_modules[module] = link_map_addr;
206 UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);
207}
208
210 m_loaded_modules.erase(module);
211
212 UnloadSectionsCommon(module);
213}
214
216 Log *log = GetLog(LLDBLog::DynamicLoader);
217
218 // If we have a core file, we don't need any breakpoints.
219 if (IsCoreFile())
220 return;
221
222 const addr_t entry = GetEntryPoint();
223 if (entry == LLDB_INVALID_ADDRESS) {
224 LLDB_LOGF(
225 log,
226 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
227 " GetEntryPoint() returned no address, not setting entry breakpoint",
228 __FUNCTION__, m_process ? m_process->GetID() : LLDB_INVALID_PROCESS_ID);
229 return;
230 }
231
232 LLDB_LOGF(log,
233 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
234 " GetEntryPoint() returned address 0x%" PRIx64
235 ", setting entry breakpoint",
236 __FUNCTION__,
238
239 if (m_process) {
240 Breakpoint *const entry_break =
241 m_process->GetTarget().CreateBreakpoint(entry, true, false).get();
242 entry_break->SetCallback(EntryBreakpointHit, this, true);
243 entry_break->SetBreakpointKind("shared-library-event");
244
245 // Shoudn't hit this more than once.
246 entry_break->SetOneShot(true);
247 }
248}
249
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
252// we 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
255// loaded modules (via dlopen).
257 void *baton, StoppointCallbackContext *context, user_id_t break_id,
258 user_id_t break_loc_id) {
259 assert(baton && "null baton");
260 if (!baton)
261 return false;
262
263 Log *log = GetLog(LLDBLog::DynamicLoader);
264 DynamicLoaderPOSIXDYLD *const dyld_instance =
265 static_cast<DynamicLoaderPOSIXDYLD *>(baton);
266 LLDB_LOGF(log, "DynamicLoaderPOSIXDYLD::%s called for pid %" PRIu64,
267 __FUNCTION__,
268 dyld_instance->m_process ? dyld_instance->m_process->GetID()
270
271 // Disable the breakpoint --- if a stop happens right after this, which we've
272 // seen on occasion, we don't want the breakpoint stepping thread-plan logic
273 // to show a breakpoint instruction at the disassembled entry point to the
274 // program. Disabling it prevents it. (One-shot is not enough - one-shot
275 // removal logic only happens after the breakpoint goes public, which wasn't
276 // happening in our scenario).
277 if (dyld_instance->m_process) {
278 BreakpointSP breakpoint_sp =
279 dyld_instance->m_process->GetTarget().GetBreakpointByID(break_id);
280 if (breakpoint_sp) {
281 LLDB_LOGF(log,
282 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
283 " disabling breakpoint id %" PRIu64,
284 __FUNCTION__, dyld_instance->m_process->GetID(), break_id);
285 breakpoint_sp->SetEnabled(false);
286 } else {
287 LLDB_LOGF(log,
288 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
289 " failed to find breakpoint for breakpoint id %" PRIu64,
290 __FUNCTION__, dyld_instance->m_process->GetID(), break_id);
291 }
292 } else {
293 LLDB_LOGF(log,
294 "DynamicLoaderPOSIXDYLD::%s breakpoint id %" PRIu64
295 " no Process instance! Cannot disable breakpoint",
296 __FUNCTION__, break_id);
297 }
298
299 dyld_instance->LoadAllCurrentModules();
300 dyld_instance->SetRendezvousBreakpoint();
301 return false; // Continue running.
302}
303
305 Log *log = GetLog(LLDBLog::DynamicLoader);
306
307 // If we have a core file, we don't need any breakpoints.
308 if (IsCoreFile())
309 return false;
310
312 LLDB_LOG(log,
313 "Rendezvous breakpoint breakpoint id {0} for pid {1}"
314 "is already set.",
317 return true;
318 }
319
320 addr_t break_addr;
321 Target &target = m_process->GetTarget();
322 BreakpointSP dyld_break;
324 break_addr = m_rendezvous.GetBreakAddress();
325 LLDB_LOG(log, "Setting rendezvous break address for pid {0} at {1:x}",
327 break_addr);
328 dyld_break = target.CreateBreakpoint(break_addr, true, false);
329 } else {
330 LLDB_LOG(log, "Rendezvous structure is not set up yet. "
331 "Trying to locate rendezvous breakpoint in the interpreter "
332 "by symbol name.");
333 // Function names from different dynamic loaders that are known to be
334 // used as rendezvous between the loader and debuggers.
335 static std::vector<std::string> DebugStateCandidates{
336 "_dl_debug_state", "rtld_db_dlactivity", "__dl_rtld_db_dlactivity",
337 "r_debug_state", "_r_debug_state", "_rtld_debug_state",
338 };
339
340 ModuleSP interpreter = LoadInterpreterModule();
341 FileSpecList containingModules;
342 if (interpreter)
343 containingModules.Append(interpreter->GetFileSpec());
344 else
345 containingModules.Append(
347
348 dyld_break = target.CreateBreakpoint(
349 &containingModules, /*containingSourceFiles=*/nullptr,
350 DebugStateCandidates, eFunctionNameTypeFull, eLanguageTypeC,
351 /*m_offset=*/0,
352 /*skip_prologue=*/eLazyBoolNo,
353 /*internal=*/true,
354 /*request_hardware=*/false);
355 }
356
357 if (dyld_break->GetNumResolvedLocations() != 1) {
358 LLDB_LOG(
359 log,
360 "Rendezvous breakpoint has abnormal number of"
361 " resolved locations ({0}) in pid {1}. It's supposed to be exactly 1.",
362 dyld_break->GetNumResolvedLocations(),
364
365 target.RemoveBreakpointByID(dyld_break->GetID());
366 return false;
367 }
368
369 BreakpointLocationSP location = dyld_break->GetLocationAtIndex(0);
370 LLDB_LOG(log,
371 "Successfully set rendezvous breakpoint at address {0:x} "
372 "for pid {1}",
373 location->GetLoadAddress(),
375
376 dyld_break->SetCallback(RendezvousBreakpointHit, this, true);
377 dyld_break->SetBreakpointKind("shared-library-event");
378 m_dyld_bid = dyld_break->GetID();
379 return true;
380}
381
383 void *baton, StoppointCallbackContext *context, user_id_t break_id,
384 user_id_t break_loc_id) {
385 assert(baton && "null baton");
386 if (!baton)
387 return false;
388
389 Log *log = GetLog(LLDBLog::DynamicLoader);
390 DynamicLoaderPOSIXDYLD *const dyld_instance =
391 static_cast<DynamicLoaderPOSIXDYLD *>(baton);
392 LLDB_LOGF(log, "DynamicLoaderPOSIXDYLD::%s called for pid %" PRIu64,
393 __FUNCTION__,
394 dyld_instance->m_process ? dyld_instance->m_process->GetID()
396
397 dyld_instance->RefreshModules();
398
399 // Return true to stop the target, false to just let the target run.
400 const bool stop_when_images_change = dyld_instance->GetStopWhenImagesChange();
401 LLDB_LOGF(log,
402 "DynamicLoaderPOSIXDYLD::%s pid %" PRIu64
403 " stop_when_images_change=%s",
404 __FUNCTION__,
405 dyld_instance->m_process ? dyld_instance->m_process->GetID()
407 stop_when_images_change ? "true" : "false");
408 return stop_when_images_change;
409}
410
412 if (!m_rendezvous.Resolve())
413 return;
414
415 // The rendezvous class doesn't enumerate the main module, so track that
416 // ourselves here.
417 ModuleSP executable = GetTargetExecutable();
419
422
423 ModuleList &loaded_modules = m_process->GetTarget().GetImages();
424
426 ModuleList new_modules;
427
428 // If this is the first time rendezvous breakpoint fires, we need
429 // to take care of adding all the initial modules reported by
430 // the loader. This is necessary to list ld-linux.so on Linux,
431 // and all DT_NEEDED entries on *BSD.
435 } else {
436 I = m_rendezvous.begin();
437 E = m_rendezvous.end();
439 }
440 for (; I != E; ++I) {
441 // Don't load a duplicate copy of ld.so if we have already loaded it
442 // earlier in LoadInterpreterModule. If we instead loaded then unloaded it
443 // later, the section information for ld.so would be removed. That
444 // information is required for placing breakpoints on Arm/Thumb systems.
445 if ((m_interpreter_module.lock() != nullptr) &&
446 (I->base_addr == m_interpreter_base))
447 continue;
448
449 ModuleSP module_sp =
450 LoadModuleAtAddress(I->file_spec, I->link_addr, I->base_addr, true);
451 if (!module_sp.get())
452 continue;
453
454 if (module_sp->GetObjectFile()->GetBaseAddress().GetLoadAddress(
456 ModuleSP interpreter_sp = m_interpreter_module.lock();
457 if (m_interpreter_module.lock() == nullptr) {
458 m_interpreter_module = module_sp;
459 } else if (module_sp == interpreter_sp) {
460 // Module already loaded.
461 continue;
462 }
463 }
464
465 loaded_modules.AppendIfNeeded(module_sp);
466 new_modules.Append(module_sp);
467 }
468 m_process->GetTarget().ModulesDidLoad(new_modules);
469 }
470
472 ModuleList old_modules;
473
475 for (I = m_rendezvous.unloaded_begin(); I != E; ++I) {
476 ModuleSpec module_spec{I->file_spec};
477 ModuleSP module_sp = loaded_modules.FindFirstModule(module_spec);
478
479 if (module_sp.get()) {
480 old_modules.Append(module_sp);
481 UnloadSections(module_sp);
482 }
483 }
484 loaded_modules.Remove(old_modules);
485 m_process->GetTarget().ModulesDidUnload(old_modules, false);
486 }
487}
488
491 bool stop) {
492 ThreadPlanSP thread_plan_sp;
493
494 StackFrame *frame = thread.GetStackFrameAtIndex(0).get();
495 const SymbolContext &context = frame->GetSymbolContext(eSymbolContextSymbol);
496 Symbol *sym = context.symbol;
497
498 if (sym == nullptr || !sym->IsTrampoline())
499 return thread_plan_sp;
500
502 if (!sym_name)
503 return thread_plan_sp;
504
505 SymbolContextList target_symbols;
506 Target &target = thread.GetProcess()->GetTarget();
507 const ModuleList &images = target.GetImages();
508
509 llvm::StringRef target_name = sym_name.GetStringRef();
510 // On AArch64, the trampoline name has a prefix (__AArch64ADRPThunk_ or
511 // __AArch64AbsLongThunk_) added to the function name. If we detect a
512 // trampoline with the prefix, we need to remove the prefix to find the
513 // function symbol.
514 if (target_name.consume_front("__AArch64ADRPThunk_") ||
515 target_name.consume_front("__AArch64AbsLongThunk_")) {
516 // An empty target name can happen for trampolines generated for
517 // section-referencing relocations.
518 if (!target_name.empty()) {
519 sym_name = ConstString(target_name);
520 }
521 }
522 images.FindSymbolsWithNameAndType(sym_name, eSymbolTypeCode, target_symbols);
523 if (!target_symbols.GetSize())
524 return thread_plan_sp;
525
526 typedef std::vector<lldb::addr_t> AddressVector;
527 AddressVector addrs;
528 for (const SymbolContext &context : target_symbols) {
529 AddressRange range;
530 context.GetAddressRange(eSymbolContextEverything, 0, false, range);
531 lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(&target);
532 if (addr != LLDB_INVALID_ADDRESS)
533 addrs.push_back(addr);
534 }
535
536 if (addrs.size() > 0) {
537 AddressVector::iterator start = addrs.begin();
538 AddressVector::iterator end = addrs.end();
539
540 llvm::sort(start, end);
541 addrs.erase(std::unique(start, end), end);
542 thread_plan_sp =
543 std::make_shared<ThreadPlanRunToAddress>(thread, addrs, stop);
544 }
545
546 return thread_plan_sp;
547}
548
551 return;
552
553 FileSpec file("[vdso]");
554
555 MemoryRegionInfo info;
557 if (status.Fail()) {
558 Log *log = GetLog(LLDBLog::DynamicLoader);
559 LLDB_LOG(log, "Failed to get vdso region info: {0}", status);
560 return;
561 }
562
563 if (ModuleSP module_sp = m_process->ReadModuleFromMemory(
564 file, m_vdso_base, info.GetRange().GetByteSize())) {
567 }
568}
569
572 return nullptr;
573
574 MemoryRegionInfo info;
575 Target &target = m_process->GetTarget();
577 if (status.Fail() || info.GetMapped() != MemoryRegionInfo::eYes ||
578 info.GetName().IsEmpty()) {
579 Log *log = GetLog(LLDBLog::DynamicLoader);
580 LLDB_LOG(log, "Failed to get interpreter region info: {0}", status);
581 return nullptr;
582 }
583
584 FileSpec file(info.GetName().GetCString());
585 ModuleSpec module_spec(file, target.GetArchitecture());
586
587 // Don't notify that module is added here because its loading section
588 // addresses are not updated yet. We manually notify it below.
589 if (ModuleSP module_sp =
590 target.GetOrCreateModule(module_spec, /*notify=*/false)) {
592 false);
593 // Manually notify that dynamic linker is loaded after updating load section
594 // addersses so that breakpoints can be resolved.
595 ModuleList module_list;
596 module_list.Append(module_sp);
597 target.ModulesDidLoad(module_list);
598 m_interpreter_module = module_sp;
599 return module_sp;
600 }
601 return nullptr;
602}
603
605 addr_t link_map_addr,
606 addr_t base_addr,
607 bool base_addr_is_offset) {
609 file, link_map_addr, base_addr, base_addr_is_offset))
610 return module_sp;
611
612 // This works around an dynamic linker "bug" on android <= 23, where the
613 // dynamic linker would report the application name
614 // (e.g. com.example.myapplication) instead of the main process binary
615 // (/system/bin/app_process(32)). The logic is not sound in general (it
616 // assumes base_addr is the real address, even though it actually is a load
617 // bias), but it happens to work on android because app_process has a file
618 // address of zero.
619 // This should be removed after we drop support for android-23.
620 if (m_process->GetTarget().GetArchitecture().GetTriple().isAndroid()) {
621 MemoryRegionInfo memory_info;
622 Status error = m_process->GetMemoryRegionInfo(base_addr, memory_info);
623 if (error.Success() && memory_info.GetMapped() &&
624 memory_info.GetRange().GetRangeBase() == base_addr &&
625 !(memory_info.GetName().IsEmpty())) {
627 FileSpec(memory_info.GetName().GetStringRef()), link_map_addr,
628 base_addr, base_addr_is_offset))
629 return module_sp;
630 }
631 }
632
633 return nullptr;
634}
635
639 ModuleList module_list;
640 Log *log = GetLog(LLDBLog::DynamicLoader);
641
642 LoadVDSO();
643
644 if (!m_rendezvous.Resolve()) {
645 LLDB_LOGF(log,
646 "DynamicLoaderPOSIXDYLD::%s unable to resolve POSIX DYLD "
647 "rendezvous address",
648 __FUNCTION__);
649 return;
650 }
651
652 // The rendezvous class doesn't enumerate the main module, so track that
653 // ourselves here.
654 ModuleSP executable = GetTargetExecutable();
656
657 std::vector<FileSpec> module_names;
658 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I)
659 module_names.push_back(I->file_spec);
661 module_names, m_process->GetTarget().GetArchitecture().GetTriple());
662
663 for (I = m_rendezvous.begin(), E = m_rendezvous.end(); I != E; ++I) {
664 ModuleSP module_sp =
665 LoadModuleAtAddress(I->file_spec, I->link_addr, I->base_addr, true);
666 if (module_sp.get()) {
667 LLDB_LOG(log, "LoadAllCurrentModules loading module: {0}",
668 I->file_spec.GetFilename());
669 module_list.Append(module_sp);
670 } else {
671 Log *log = GetLog(LLDBLog::DynamicLoader);
672 LLDB_LOGF(
673 log,
674 "DynamicLoaderPOSIXDYLD::%s failed loading module %s at 0x%" PRIx64,
675 __FUNCTION__, I->file_spec.GetPath().c_str(), I->base_addr);
676 }
677 }
678
679 m_process->GetTarget().ModulesDidLoad(module_list);
681}
682
684 addr_t virt_entry;
685
687 return m_load_offset;
688
689 if ((virt_entry = GetEntryPoint()) == LLDB_INVALID_ADDRESS)
691
693 if (!module)
695
696 ObjectFile *exe = module->GetObjectFile();
697 if (!exe)
699
700 Address file_entry = exe->GetEntryPointAddress();
701
702 if (!file_entry.IsValid())
704
705 m_load_offset = virt_entry - file_entry.GetFileAddress();
706 return m_load_offset;
707}
708
710 if (std::optional<uint64_t> vdso_base =
712 m_vdso_base = *vdso_base;
713
714 if (std::optional<uint64_t> interpreter_base =
715 m_auxv->GetAuxValue(AuxVector::AUXV_AT_BASE))
716 m_interpreter_base = *interpreter_base;
717}
718
721 return m_entry_point;
722
723 if (m_auxv == nullptr)
725
726 std::optional<uint64_t> entry_point =
727 m_auxv->GetAuxValue(AuxVector::AUXV_AT_ENTRY);
728 if (!entry_point)
730
731 m_entry_point = static_cast<addr_t>(*entry_point);
732
733 const ArchSpec &arch = m_process->GetTarget().GetArchitecture();
734
735 // On ppc64, the entry point is actually a descriptor. Dereference it.
736 if (arch.GetMachine() == llvm::Triple::ppc64)
738
739 return m_entry_point;
740}
741
744 const lldb::ThreadSP thread,
745 lldb::addr_t tls_file_addr) {
746 Log *log = GetLog(LLDBLog::DynamicLoader);
747 auto it = m_loaded_modules.find(module_sp);
748 if (it == m_loaded_modules.end()) {
749 LLDB_LOGF(
750 log, "GetThreadLocalData error: module(%s) not found in loaded modules",
751 module_sp->GetObjectName().AsCString());
753 }
754
755 addr_t link_map = it->second;
756 if (link_map == LLDB_INVALID_ADDRESS || link_map == 0) {
757 LLDB_LOGF(log,
758 "GetThreadLocalData error: invalid link map address=0x%" PRIx64,
759 link_map);
761 }
762
764 if (!metadata.valid) {
765 LLDB_LOGF(log,
766 "GetThreadLocalData error: fail to read thread info metadata");
768 }
769
770 LLDB_LOGF(log,
771 "GetThreadLocalData info: link_map=0x%" PRIx64
772 ", thread info metadata: "
773 "modid_offset=0x%" PRIx32 ", dtv_offset=0x%" PRIx32
774 ", tls_offset=0x%" PRIx32 ", dtv_slot_size=%" PRIx32 "\n",
775 link_map, metadata.modid_offset, metadata.dtv_offset,
776 metadata.tls_offset, metadata.dtv_slot_size);
777
778 // Get the thread pointer.
779 addr_t tp = thread->GetThreadPointer();
780 if (tp == LLDB_INVALID_ADDRESS) {
781 LLDB_LOGF(log, "GetThreadLocalData error: fail to read thread pointer");
783 }
784
785 // Find the module's modid.
786 int modid_size = 4; // FIXME(spucci): This isn't right for big-endian 64-bit
787 int64_t modid = ReadUnsignedIntWithSizeInBytes(
788 link_map + metadata.modid_offset, modid_size);
789 if (modid == -1) {
790 LLDB_LOGF(log, "GetThreadLocalData error: fail to read modid");
792 }
793
794 // Lookup the DTV structure for this thread.
795 addr_t dtv_ptr = tp + metadata.dtv_offset;
796 addr_t dtv = ReadPointer(dtv_ptr);
797 if (dtv == LLDB_INVALID_ADDRESS) {
798 LLDB_LOGF(log, "GetThreadLocalData error: fail to read dtv");
800 }
801
802 // Find the TLS block for this module.
803 addr_t dtv_slot = dtv + metadata.dtv_slot_size * modid;
804 addr_t tls_block = ReadPointer(dtv_slot + metadata.tls_offset);
805
806 LLDB_LOGF(log,
807 "DynamicLoaderPOSIXDYLD::Performed TLS lookup: "
808 "module=%s, link_map=0x%" PRIx64 ", tp=0x%" PRIx64
809 ", modid=%" PRId64 ", tls_block=0x%" PRIx64 "\n",
810 module_sp->GetObjectName().AsCString(""), link_map, tp,
811 (int64_t)modid, tls_block);
812
813 if (tls_block == LLDB_INVALID_ADDRESS) {
814 LLDB_LOGF(log, "GetThreadLocalData error: fail to read tls_block");
816 } else
817 return tls_block + tls_file_addr;
818}
819
821 lldb::ModuleSP &module_sp) {
822 Log *log = GetLog(LLDBLog::DynamicLoader);
823
824 if (m_process == nullptr)
825 return;
826
827 auto &target = m_process->GetTarget();
828 const auto platform_sp = target.GetPlatform();
829
830 ProcessInstanceInfo process_info;
831 if (!m_process->GetProcessInfo(process_info)) {
832 LLDB_LOGF(log,
833 "DynamicLoaderPOSIXDYLD::%s - failed to get process info for "
834 "pid %" PRIu64,
835 __FUNCTION__, m_process->GetID());
836 return;
837 }
838
839 LLDB_LOGF(
840 log, "DynamicLoaderPOSIXDYLD::%s - got executable by pid %" PRIu64 ": %s",
841 __FUNCTION__, m_process->GetID(),
842 process_info.GetExecutableFile().GetPath().c_str());
843
844 ModuleSpec module_spec(process_info.GetExecutableFile(),
845 process_info.GetArchitecture());
846 if (module_sp && module_sp->MatchesModuleSpec(module_spec))
847 return;
848
849 const auto executable_search_paths(Target::GetDefaultExecutableSearchPaths());
850 auto error = platform_sp->ResolveExecutable(
851 module_spec, module_sp,
852 !executable_search_paths.IsEmpty() ? &executable_search_paths : nullptr);
853 if (error.Fail()) {
854 StreamString stream;
855 module_spec.Dump(stream);
856
857 LLDB_LOGF(log,
858 "DynamicLoaderPOSIXDYLD::%s - failed to resolve executable "
859 "with module spec \"%s\": %s",
860 __FUNCTION__, stream.GetData(), error.AsCString());
861 return;
862 }
863
864 target.SetExecutableModule(module_sp, eLoadDependentsNo);
865}
866
869 ModuleSP module_sp;
870 if (sym_ctx.symbol)
871 module_sp = sym_ctx.symbol->GetAddressRef().GetModule();
872 if (!module_sp && sym_ctx.function)
873 module_sp =
875 if (!module_sp)
876 return false;
877
878 return module_sp->GetFileSpec().GetPath() == "[vdso]";
879}
880
882 return !m_process->IsLiveDebugSession();
883}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:359
#define LLDB_LOGF(log,...)
Definition: Log.h:366
#define LLDB_PLUGIN_DEFINE_ADV(ClassName, PluginName)
Definition: PluginManager.h:25
@ AUXV_AT_SYSINFO_EHDR
Definition: AuxVector.h:63
@ AUXV_AT_ENTRY
Program entry point.
Definition: AuxVector.h:36
@ AUXV_AT_BASE
Interpreter base address.
Definition: AuxVector.h:34
bool ModulesDidUnload() const
bool ModulesDidLoad() const
lldb::addr_t GetBreakAddress() const
A breakpoint should be set at this address and Resolve called on each hit.
void UpdateExecutablePath()
Update the cached executable path.
iterator loaded_begin() const
Iterators over all modules loaded into the inferior since the last call to Resolve().
lldb::addr_t GetLinkMapAddress() const
bool Resolve()
Update the internal snapshot of runtime linker rendezvous and recompute the currently loaded modules.
iterator unloaded_begin() const
Iterators over all modules unloaded from the inferior since the last call to Resolve().
iterator begin() const
Iterators over all currently loaded modules.
iterator unloaded_end() const
const ThreadInfo & GetThreadInfo()
iterator loaded_end() const
SOEntryList::const_iterator iterator
iterator end() const
static bool RendezvousBreakpointHit(void *baton, lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
Callback routine which updates the current list of loaded modules based on the information supplied b...
std::map< lldb::ModuleWP, lldb::addr_t, std::owner_less< lldb::ModuleWP > > m_loaded_modules
Loaded module list. (link map for each module)
static lldb_private::DynamicLoader * CreateInstance(lldb_private::Process *process, bool force)
bool m_initial_modules_added
Indicates whether the initial set of modules was reported added.
lldb::addr_t m_load_offset
Virtual load address of the inferior process.
lldb::break_id_t m_dyld_bid
Rendezvous breakpoint.
lldb::addr_t GetEntryPoint()
Computes a value for m_entry_point returning the computed address on success and LLDB_INVALID_ADDRESS...
std::unique_ptr< AuxVector > m_auxv
Auxiliary vector of the inferior process.
static llvm::StringRef GetPluginDescriptionStatic()
void EvalSpecialModulesStatus()
Evaluate if Aux vectors contain vDSO and LD information in case they do, read and assign the address ...
lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module, const lldb::ThreadSP thread, lldb::addr_t tls_file_addr) override
Retrieves the per-module TLS block for a given thread.
bool SetRendezvousBreakpoint()
If possible sets a breakpoint on a function called by the runtime linker each time a module is loaded...
lldb::ModuleSP LoadInterpreterModule()
void RefreshModules()
Helper method for RendezvousBreakpointHit.
bool AlwaysRelyOnEHUnwindInfo(lldb_private::SymbolContext &sym_ctx) override
Ask if the eh_frame information for the given SymbolContext should be relied on even when it's the fi...
lldb::addr_t m_interpreter_base
Contains AT_BASE, which means a dynamic loader has been mapped to the address space.
void ProbeEntry()
Resolves the entry point for the current inferior process and sets a breakpoint at that address.
lldb_private::Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
lldb::addr_t m_vdso_base
Contains AT_SYSINFO_EHDR, which means a vDSO has been mapped to the address space.
void UpdateLoadedSections(lldb::ModuleSP module, lldb::addr_t link_map_addr, lldb::addr_t base_addr, bool base_addr_is_offset) override
Updates the load address of every allocatable section in module.
lldb::addr_t ComputeLoadOffset()
Computes a value for m_load_offset returning the computed address on success and LLDB_INVALID_ADDRESS...
void UnloadSections(const lldb::ModuleSP module) override
Removes the loaded sections from the target in module.
DYLDRendezvous m_rendezvous
Runtime linker rendezvous structure.
void DidLaunch() override
Called after launching a process.
lldb::ModuleSP LoadModuleAtAddress(const lldb_private::FileSpec &file, lldb::addr_t link_map_addr, lldb::addr_t base_addr, bool base_addr_is_offset) override
Locates or creates a module given by file and updates/loads the resulting module at the virtual base ...
static bool EntryBreakpointHit(void *baton, lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
Callback routine invoked when we hit the breakpoint on process entry.
void DidAttach() override
Called after attaching a process.
lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, bool stop_others) override
Provides a plan to step through the dynamic loader trampoline for the current state of thread.
void ResolveExecutableModule(lldb::ModuleSP &module_sp)
Loads Module from inferior process.
lldb::addr_t m_entry_point
Virtual entry address of the inferior process.
virtual void LoadAllCurrentModules()
Helper for the entry breakpoint callback.
std::weak_ptr< lldb_private::Module > m_interpreter_module
Contains the pointer to the interpret module, if loaded.
bool IsCoreFile() const
Returns true if the process is for a core file.
DynamicLoaderPOSIXDYLD(lldb_private::Process *process)
A section + offset based address range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:211
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:313
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:285
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:293
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
An architecture specification class.
Definition: ArchSpec.h:31
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:450
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:683
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:81
void SetOneShot(bool one_shot)
If one_shot is true, breakpoint will be deleted on first hit.
Definition: Breakpoint.cpp:328
void SetBreakpointKind(const char *kind)
Set the "kind" description for a breakpoint.
Definition: Breakpoint.h:452
void SetCallback(BreakpointHitCallback callback, void *baton, bool is_synchronous=false)
Set the callback action invoked when the breakpoint is hit.
Definition: Breakpoint.cpp:408
A uniqued constant string class.
Definition: ConstString.h:40
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:304
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:197
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
A plug-in interface definition class for dynamic loaders.
Definition: DynamicLoader.h:52
int64_t ReadUnsignedIntWithSizeInBytes(lldb::addr_t addr, int size_in_bytes)
lldb::addr_t ReadPointer(lldb::addr_t addr)
Process * m_process
The process that this dynamic loader plug-in is tracking.
void UpdateLoadedSectionsCommon(lldb::ModuleSP module, lldb::addr_t base_addr, bool base_addr_is_offset)
lldb::ModuleSP GetTargetExecutable()
Checks to see if the target module has changed, updates the target accordingly and returns the target...
bool GetStopWhenImagesChange() const
Get whether the process should stop when images change.
virtual lldb::ModuleSP LoadModuleAtAddress(const lldb_private::FileSpec &file, lldb::addr_t link_map_addr, lldb::addr_t base_addr, bool base_addr_is_offset)
Locates or creates a module given by file and updates/loads the resulting module at the virtual base ...
void UnloadSectionsCommon(const lldb::ModuleSP module)
A file collection class.
Definition: FileSpecList.h:85
void Append(const FileSpec &file)
Append a FileSpec object to the list.
A file utility class.
Definition: FileSpec.h:56
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:367
const AddressRange & GetAddressRange()
Definition: Function.h:447
ConstString GetName(NamePreference preference=ePreferDemangled) const
Best name get accessor.
Definition: Mangled.cpp:335
OptionalBool GetMapped() const
A collection class for Module objects.
Definition: ModuleList.h:103
lldb::ModuleSP FindFirstModule(const ModuleSpec &module_spec) const
Definition: ModuleList.cpp:626
bool AppendIfNeeded(const lldb::ModuleSP &new_module, bool notify=true)
Append a module to the module list, if it is not already there.
Definition: ModuleList.cpp:280
void FindSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, SymbolContextList &sc_list) const
Definition: ModuleList.cpp:527
bool Remove(const lldb::ModuleSP &module_sp, bool notify=true)
Remove a module from the module list.
Definition: ModuleList.cpp:334
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.
Definition: ModuleList.cpp:247
ModuleIterable Modules() const
Definition: ModuleList.h:527
void Dump(Stream &strm) const
Definition: ModuleSpec.h:162
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition: Module.h:452
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
virtual lldb_private::Address GetImageInfoAddress(Target *target)
Similar to Process::GetImageInfoAddress().
Definition: ObjectFile.h:459
virtual lldb_private::Address GetEntryPointAddress()
Returns the address of the Entry Point in this object file - if the object file doesn't have an entry...
Definition: ObjectFile.h:469
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
FileSpec & GetExecutableFile()
Definition: ProcessInfo.h:42
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:61
A plug-in interface definition class for debugging a process.
Definition: Process.h:341
lldb::pid_t GetID() const
Returns the pid of the process or LLDB_INVALID_PROCESS_ID if there is no known pid.
Definition: Process.h:538
virtual DataExtractor GetAuxvData()
Definition: Process.cpp:2718
virtual void PrefetchModuleSpecs(llvm::ArrayRef< FileSpec > module_file_specs, const llvm::Triple &triple)
Definition: Process.h:2542
virtual bool GetProcessInfo(ProcessInstanceInfo &info)
Definition: Process.cpp:5937
virtual bool IsLiveDebugSession() const
Definition: Process.h:1519
Status GetMemoryRegionInfo(lldb::addr_t load_addr, MemoryRegionInfo &range_info)
Locate the memory region that contains load_addr.
Definition: Process.cpp:6044
lldb::ModuleSP ReadModuleFromMemory(const FileSpec &file_spec, lldb::addr_t header_addr, size_t size_to_read=512)
Definition: Process.cpp:2415
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1277
This base class provides an interface to stack frames.
Definition: StackFrame.h:43
const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
Definition: StackFrame.cpp:300
An error handling class.
Definition: Status.h:44
bool Fail() const
Test for error condition.
Definition: Status.cpp:180
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
const char * GetData() const
Definition: StreamString.h:43
Defines a list of symbol context objects.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
Function * function
The Function for a given query.
bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const
Get the address range contained within a symbol context.
Symbol * symbol
The Symbol for a given query.
Mangled & GetMangled()
Definition: Symbol.h:146
bool IsTrampoline() const
Definition: Symbol.cpp:221
Address & GetAddressRef()
Definition: Symbol.h:72
void ModulesDidLoad(ModuleList &module_list)
Definition: Target.cpp:1690
Module * GetExecutableModulePointer()
Definition: Target.cpp:1436
lldb::BreakpointSP GetBreakpointByID(lldb::break_id_t break_id)
Definition: Target.cpp:328
bool RemoveBreakpointByID(lldb::break_id_t break_id)
Definition: Target.cpp:1004
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
Definition: Target.cpp:2158
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1422
void ModulesDidUnload(ModuleList &module_list, bool delete_locations)
Definition: Target.cpp:1724
static FileSpecList GetDefaultExecutableSearchPaths()
Definition: Target.cpp:2597
lldb::PlatformSP GetPlatform()
Definition: Target.h:1444
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
Definition: Target.cpp:395
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:981
const ArchSpec & GetArchitecture() const
Definition: Target.h:1023
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:405
lldb::ProcessSP GetProcess() const
Definition: Thread.h:154
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:37
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:89
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:331
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
Definition: lldb-forward.h:444
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
Definition: lldb-forward.h:319
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:441
@ eLanguageTypeC
Non-standardized C, such as K&R.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:316
uint64_t user_id_t
Definition: lldb-types.h:82
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:368
BaseType GetRangeBase() const
Definition: RangeMap.h:45
SizeType GetByteSize() const
Definition: RangeMap.h:87