@@ -475,61 +475,67 @@ void SwiftLanguageRuntimeImpl::SetupReflection() {
475475
476476 // The global ABI bit is read by the Swift runtime library.
477477 SetupABIBit ();
478-
479- std::lock_guard<std::recursive_mutex> lock (m_add_module_mutex);
480- if (m_initialized_reflection_ctx)
481- return ;
478+
479+ // A copy of the modules that should be added, to prevent mutating the list
480+ // while iterating over it.
481+ ModuleList modules_snapshot ;
482482
483483 auto &target = m_process.GetTarget ();
484484 auto exe_module = target.GetExecutableModule ();
485+ {
486+ std::lock_guard<std::recursive_mutex> lock (m_add_module_mutex);
487+ if (m_initialized_reflection_ctx)
488+ return ;
489+
490+ if (!exe_module) {
491+ LLDB_LOGF (GetLog (LLDBLog::Types), " %s: Failed to get executable module" ,
492+ LLVM_PRETTY_FUNCTION);
493+ m_initialized_reflection_ctx = false ;
494+ return ;
495+ }
485496
486- if (!exe_module) {
487- LLDB_LOGF (GetLog (LLDBLog::Types), " %s: Failed to get executable module" ,
488- LLVM_PRETTY_FUNCTION);
489- m_initialized_reflection_ctx = false ;
490- return ;
491- }
497+ bool objc_interop = (bool )findRuntime (m_process, RuntimeKind::ObjC);
498+ const char *objc_interop_msg =
499+ objc_interop ? " with Objective-C interopability" : " Swift only" ;
492500
493- bool objc_interop = (bool )findRuntime (m_process, RuntimeKind::ObjC);
494- const char *objc_interop_msg =
495- objc_interop ? " with Objective-C interopability" : " Swift only" ;
496-
497- auto &triple = exe_module->GetArchitecture ().GetTriple ();
498- if (triple.isArch64Bit ()) {
499- LLDB_LOGF (GetLog (LLDBLog::Types),
500- " Initializing a 64-bit reflection context (%s) for \" %s\" " ,
501- triple.str ().c_str (), objc_interop_msg);
502- m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext64 (
503- this ->GetMemoryReader (), objc_interop, GetSwiftMetadataCache ());
504- } else if (triple.isArch32Bit ()) {
505- LLDB_LOGF (GetLog (LLDBLog::Types),
506- " Initializing a 32-bit reflection context (%s) for \" %s\" " ,
507- triple.str ().c_str (), objc_interop_msg);
508- m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext32 (
509- this ->GetMemoryReader (), objc_interop, GetSwiftMetadataCache ());
510- } else {
511- LLDB_LOGF (GetLog (LLDBLog::Types),
512- " Could not initialize reflection context for \" %s\" " ,
513- triple.str ().c_str ());
514- }
501+ auto &triple = exe_module->GetArchitecture ().GetTriple ();
502+ if (triple.isArch64Bit ()) {
503+ LLDB_LOGF (GetLog (LLDBLog::Types),
504+ " Initializing a 64-bit reflection context (%s) for \" %s\" " ,
505+ triple.str ().c_str (), objc_interop_msg);
506+ m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext64 (
507+ this ->GetMemoryReader (), objc_interop, GetSwiftMetadataCache ());
508+ } else if (triple.isArch32Bit ()) {
509+ LLDB_LOGF (GetLog (LLDBLog::Types),
510+ " Initializing a 32-bit reflection context (%s) for \" %s\" " ,
511+ triple.str ().c_str (), objc_interop_msg);
512+ m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext32 (
513+ this ->GetMemoryReader (), objc_interop, GetSwiftMetadataCache ());
514+ } else {
515+ LLDB_LOGF (GetLog (LLDBLog::Types),
516+ " Could not initialize reflection context for \" %s\" " ,
517+ triple.str ().c_str ());
518+ }
515519
516- m_initialized_reflection_ctx = true ;
520+ modules_snapshot = std::move (m_modules_to_add);
521+ m_modules_to_add.Clear ();
522+ m_initialized_reflection_ctx = true ;
523+ }
517524
518525 Progress progress (
519526 llvm::formatv (" Setting up Swift reflection for '{0}'" ,
520527 exe_module->GetFileSpec ().GetFilename ().AsCString ()),
521- m_modules_to_add .GetSize ());
528+ modules_snapshot .GetSize ());
522529
523530 size_t completion = 0 ;
524531
525532 // Add all defered modules to reflection context that were added to
526533 // the target since this SwiftLanguageRuntime was created.
527- m_modules_to_add .ForEach ([&](const ModuleSP &module_sp) -> bool {
534+ modules_snapshot .ForEach ([&](const ModuleSP &module_sp) -> bool {
528535 AddModuleToReflectionContext (module_sp);
529536 progress.Increment (++completion);
530537 return true ;
531538 });
532- m_modules_to_add.Clear ();
533539}
534540
535541bool SwiftLanguageRuntimeImpl::IsABIStable () {
0 commit comments