Skip to content

Commit c8f133b

Browse files
committed
DoInitialImageFetch routine preloads modules explicitly
1 parent 64a4d69 commit c8f133b

File tree

4 files changed

+72
-49
lines changed

4 files changed

+72
-49
lines changed

lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp

+49-38
Original file line numberDiff line numberDiff line change
@@ -530,44 +530,43 @@ bool DynamicLoaderDarwin::JSONImageInformationIntoImageInfo(
530530
return true;
531531
}
532532

533-
void DynamicLoaderDarwin::UpdateSpecialBinariesFromNewImageInfos(
534-
ImageInfo::collection &image_infos) {
533+
void DynamicLoaderDarwin::UpdateSpecialBinariesFromPreloadedModules(
534+
std::vector<std::pair<ImageInfo, ModuleSP>> &images) {
535535
uint32_t exe_idx = UINT32_MAX;
536536
uint32_t dyld_idx = UINT32_MAX;
537537
Target &target = m_process->GetTarget();
538538
Log *log = GetLog(LLDBLog::DynamicLoader);
539539
ConstString g_dyld_sim_filename("dyld_sim");
540540

541541
ArchSpec target_arch = target.GetArchitecture();
542-
const size_t image_infos_size = image_infos.size();
543-
for (size_t i = 0; i < image_infos_size; i++) {
544-
if (image_infos[i].header.filetype == llvm::MachO::MH_DYLINKER) {
542+
const size_t images_size = images.size();
543+
for (size_t i = 0; i < images_size; i++) {
544+
const auto &image_info = images[i].first;
545+
if (image_info.header.filetype == llvm::MachO::MH_DYLINKER) {
545546
// In a "simulator" process we will have two dyld modules --
546547
// a "dyld" that we want to keep track of, and a "dyld_sim" which
547548
// we don't need to keep track of here. dyld_sim will have a non-macosx
548549
// OS.
549550
if (target_arch.GetTriple().getEnvironment() == llvm::Triple::Simulator &&
550-
image_infos[i].os_type != llvm::Triple::OSType::MacOSX) {
551+
image_info.os_type != llvm::Triple::OSType::MacOSX) {
551552
continue;
552553
}
553554

554555
dyld_idx = i;
555556
}
556-
if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) {
557+
if (image_info.header.filetype == llvm::MachO::MH_EXECUTE) {
557558
exe_idx = i;
558559
}
559560
}
560561

561562
// Set the target executable if we haven't found one so far.
562563
if (exe_idx != UINT32_MAX && !target.GetExecutableModule()) {
563-
const bool can_create = true;
564-
ModuleSP exe_module_sp(FindTargetModuleForImageInfo(image_infos[exe_idx],
565-
can_create, nullptr));
564+
ModuleSP exe_module_sp = images[exe_idx].second;
566565
if (exe_module_sp) {
567566
LLDB_LOGF(log, "Found executable module: %s",
568567
exe_module_sp->GetFileSpec().GetPath().c_str());
569568
target.GetImages().AppendIfNeeded(exe_module_sp);
570-
UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]);
569+
UpdateImageLoadAddress(exe_module_sp.get(), images[exe_idx].first);
571570
if (exe_module_sp.get() != target.GetExecutableModulePointer())
572571
target.SetExecutableModule(exe_module_sp, eLoadDependentsNo);
573572

@@ -594,14 +593,12 @@ void DynamicLoaderDarwin::UpdateSpecialBinariesFromNewImageInfos(
594593
}
595594

596595
if (dyld_idx != UINT32_MAX) {
597-
const bool can_create = true;
598-
ModuleSP dyld_sp = FindTargetModuleForImageInfo(image_infos[dyld_idx],
599-
can_create, nullptr);
596+
ModuleSP dyld_sp = images[dyld_idx].second;
600597
if (dyld_sp.get()) {
601598
LLDB_LOGF(log, "Found dyld module: %s",
602599
dyld_sp->GetFileSpec().GetPath().c_str());
603600
target.GetImages().AppendIfNeeded(dyld_sp);
604-
UpdateImageLoadAddress(dyld_sp.get(), image_infos[dyld_idx]);
601+
UpdateImageLoadAddress(dyld_sp.get(), images[dyld_idx].first);
605602
SetDYLDModule(dyld_sp);
606603
}
607604
}
@@ -690,37 +687,51 @@ map(InputIterator first, InputIterator last,
690687
return results;
691688
}
692689

690+
std::vector<std::pair<DynamicLoaderDarwin::ImageInfo, ModuleSP>>
691+
DynamicLoaderDarwin::PreloadModulesFromImageInfos(
692+
const ImageInfo::collection &image_infos) {
693+
auto ImageLoad = [this](const ImageInfo &image_info) {
694+
return std::make_pair(
695+
image_info, FindTargetModuleForImageInfo(image_info, true, nullptr));
696+
};
697+
bool is_parallel_load =
698+
DynamicLoaderDarwinProperties::GetGlobal().GetEnableParallelImageLoad();
699+
auto images = is_parallel_load
700+
? parallel_map<ImageInfo::collection::const_iterator,
701+
std::pair<ImageInfo, ModuleSP>>(
702+
Debugger::GetThreadPool(), image_infos.begin(),
703+
image_infos.end(), ImageLoad)
704+
: map<ImageInfo::collection::const_iterator,
705+
std::pair<ImageInfo, ModuleSP>>(
706+
image_infos.begin(), image_infos.end(), ImageLoad);
707+
return images;
708+
}
709+
693710
bool DynamicLoaderDarwin::AddModulesUsingImageInfos(
694711
ImageInfo::collection &image_infos) {
695712
std::lock_guard<std::recursive_mutex> guard(m_mutex);
713+
auto images = PreloadModulesFromImageInfos(image_infos);
714+
return AddModulesUsingPreloadedModules(images);
715+
}
716+
717+
bool DynamicLoaderDarwin::AddModulesUsingPreloadedModules(
718+
std::vector<std::pair<ImageInfo, ModuleSP>> &images) {
719+
std::lock_guard<std::recursive_mutex> guard(m_mutex);
696720
// Now add these images to the main list.
697721
ModuleList loaded_module_list;
698722
Log *log = GetLog(LLDBLog::DynamicLoader);
699723
Target &target = m_process->GetTarget();
700724
ModuleList &target_images = target.GetImages();
701725

702-
auto ImageLoad = [this, log](const ImageInfo &image_info) {
726+
for (uint32_t idx = 0; idx < images.size(); ++idx) {
727+
auto &image_info = images[idx].first;
728+
const auto &image_module_sp = images[idx].second;
703729
if (log) {
704730
LLDB_LOGF(log, "Adding new image at address=0x%16.16" PRIx64 ".",
705731
image_info.address);
706732
image_info.PutToLog(log);
707733
}
708-
return FindTargetModuleForImageInfo(image_info, true, nullptr);
709-
};
710-
bool is_parallel_load =
711-
DynamicLoaderDarwinProperties::GetGlobal().GetEnableParallelImageLoad();
712-
auto images =
713-
is_parallel_load
714-
? parallel_map<ImageInfo::collection::const_iterator, ModuleSP>(
715-
Debugger::GetThreadPool(), image_infos.begin(),
716-
image_infos.end(), ImageLoad)
717-
: map<ImageInfo::collection::const_iterator, ModuleSP>(
718-
image_infos.begin(), image_infos.end(), ImageLoad);
719-
720-
for (uint32_t idx = 0; idx < image_infos.size(); ++idx) {
721-
m_dyld_image_infos.push_back(image_infos[idx]);
722-
723-
ModuleSP image_module_sp = images[idx];
734+
m_dyld_image_infos.push_back(image_info);
724735

725736
if (image_module_sp) {
726737
ObjectFile *objfile = image_module_sp->GetObjectFile();
@@ -732,7 +743,7 @@ bool DynamicLoaderDarwin::AddModulesUsingImageInfos(
732743
sections->FindSectionByName(commpage_dbstr).get();
733744
if (commpage_section) {
734745
ModuleSpec module_spec(objfile->GetFileSpec(),
735-
image_infos[idx].GetArchitecture());
746+
image_info.GetArchitecture());
736747
module_spec.GetObjectName() = commpage_dbstr;
737748
ModuleSP commpage_image_module_sp(
738749
target_images.FindFirstModule(module_spec));
@@ -745,17 +756,17 @@ bool DynamicLoaderDarwin::AddModulesUsingImageInfos(
745756
if (!commpage_image_module_sp ||
746757
commpage_image_module_sp->GetObjectFile() == nullptr) {
747758
commpage_image_module_sp = m_process->ReadModuleFromMemory(
748-
image_infos[idx].file_spec, image_infos[idx].address);
759+
image_info.file_spec, image_info.address);
749760
// Always load a memory image right away in the target in case
750761
// we end up trying to read the symbol table from memory... The
751762
// __LINKEDIT will need to be mapped so we can figure out where
752763
// the symbol table bits are...
753764
bool changed = false;
754765
UpdateImageLoadAddress(commpage_image_module_sp.get(),
755-
image_infos[idx]);
766+
image_info);
756767
target.GetImages().Append(commpage_image_module_sp);
757768
if (changed) {
758-
image_infos[idx].load_stop_id = m_process->GetStopID();
769+
image_info.load_stop_id = m_process->GetStopID();
759770
loaded_module_list.AppendIfNeeded(commpage_image_module_sp);
760771
}
761772
}
@@ -768,14 +779,14 @@ bool DynamicLoaderDarwin::AddModulesUsingImageInfos(
768779
// address. We need to check this so we don't mention that all loaded
769780
// shared libraries are newly loaded each time we hit out dyld breakpoint
770781
// since dyld will list all shared libraries each time.
771-
if (UpdateImageLoadAddress(image_module_sp.get(), image_infos[idx])) {
782+
if (UpdateImageLoadAddress(image_module_sp.get(), image_info)) {
772783
target_images.AppendIfNeeded(image_module_sp);
773784
loaded_module_list.AppendIfNeeded(image_module_sp);
774785
}
775786

776787
// To support macCatalyst and legacy iOS simulator,
777788
// update the module's platform with the DYLD info.
778-
ArchSpec dyld_spec = image_infos[idx].GetArchitecture();
789+
ArchSpec dyld_spec = image_info.GetArchitecture();
779790
auto &dyld_triple = dyld_spec.GetTriple();
780791
if ((dyld_triple.getEnvironment() == llvm::Triple::MacABI &&
781792
dyld_triple.getOS() == llvm::Triple::IOS) ||

lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.h

+14-5
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,18 @@ class DynamicLoaderDarwin : public lldb_private::DynamicLoader {
203203
lldb_private::StructuredData::ObjectSP image_details,
204204
ImageInfo::collection &image_infos);
205205

206-
// If image_infos contains / may contain dyld or executable image, call this
207-
// method
208-
// to keep our internal record keeping of the special binaries up-to-date.
209-
void
210-
UpdateSpecialBinariesFromNewImageInfos(ImageInfo::collection &image_infos);
206+
// Finds/loads modules for a given `image_infos` and returns pairs
207+
// (ImageInfo, ModuleSP).
208+
// Prefer using this method rather than calling `FindTargetModuleForImageInfo`
209+
// directly as this method may load the modules in parallel.
210+
std::vector<std::pair<ImageInfo, lldb::ModuleSP>>
211+
PreloadModulesFromImageInfos(const ImageInfo::collection &image_infos);
212+
213+
// If `images` contains / may contain dyld or executable image, call this
214+
// method to keep our internal record keeping of the special binaries
215+
// up-to-date.
216+
void UpdateSpecialBinariesFromPreloadedModules(
217+
std::vector<std::pair<ImageInfo, lldb::ModuleSP>> &images);
211218

212219
// if image_info is a dyld binary, call this method
213220
bool UpdateDYLDImageInfoFromNewImageInfo(ImageInfo &image_info);
@@ -217,6 +224,8 @@ class DynamicLoaderDarwin : public lldb_private::DynamicLoader {
217224
void AddExecutableModuleIfInImageInfos(ImageInfo::collection &image_infos);
218225

219226
bool AddModulesUsingImageInfos(ImageInfo::collection &image_infos);
227+
bool AddModulesUsingPreloadedModules(
228+
std::vector<std::pair<ImageInfo, lldb::ModuleSP>> &images);
220229

221230
// Whether we should use the new dyld SPI to get shared library information,
222231
// or read

lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ void DynamicLoaderMacOS::DoInitialImageFetch() {
215215
LLDB_LOGF(log, "Initial module fetch: Adding %" PRId64 " modules.\n",
216216
(uint64_t)image_infos.size());
217217

218-
UpdateSpecialBinariesFromNewImageInfos(image_infos);
219-
AddModulesUsingImageInfos(image_infos);
218+
auto images = PreloadModulesFromImageInfos(image_infos);
219+
UpdateSpecialBinariesFromPreloadedModules(images);
220+
AddModulesUsingPreloadedModules(images);
220221
}
221222
}
222223

@@ -425,8 +426,9 @@ void DynamicLoaderMacOS::AddBinaries(
425426
->GetAsArray()
426427
->GetSize() == load_addresses.size()) {
427428
if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) {
428-
UpdateSpecialBinariesFromNewImageInfos(image_infos);
429-
AddModulesUsingImageInfos(image_infos);
429+
auto images = PreloadModulesFromImageInfos(image_infos);
430+
UpdateSpecialBinariesFromPreloadedModules(images);
431+
AddModulesUsingPreloadedModules(images);
430432
}
431433
m_dyld_image_infos_stop_id = m_process->GetStopID();
432434
}

lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,9 @@ bool DynamicLoaderMacOSXDYLD::AddModulesUsingImageInfosAddress(
572572
->GetSize() == image_infos_count) {
573573
bool return_value = false;
574574
if (JSONImageInformationIntoImageInfo(image_infos_json_sp, image_infos)) {
575-
UpdateSpecialBinariesFromNewImageInfos(image_infos);
576-
return_value = AddModulesUsingImageInfos(image_infos);
575+
auto images = PreloadModulesFromImageInfos(image_infos);
576+
UpdateSpecialBinariesFromPreloadedModules(images);
577+
return_value = AddModulesUsingPreloadedModules(images);
577578
}
578579
m_dyld_image_infos_stop_id = m_process->GetStopID();
579580
return return_value;

0 commit comments

Comments
 (0)