Skip to content

Commit 04e10ab

Browse files
committed
Clean up PlatformDarwinKernel::GetSharedModule, document (llvm#78652)
PlatformDarwinKernel::GetSharedModule, which can find a kernel or kext from a local filesystem scan, needed a little cleanup. The method which finds kernels was (1) not looking for the SymbolFileSpec when creating a Module, and (2) adding that newly created Module to a Target, which GetSharedModule should not be doing - after auditing many other subclass implementations of this method, I haven't found any others doing it. Platform::GetSharedModule didn't have a headerdoc so it took a little work to piece together the intended behaviors. This is addressing a bug where PlatformDarwinKernel::GetSharedModuleKernel would find the ObjectFile for a kernel, create a Module, and add it to the Target. Then up in DynamicLoaderDarwinKernel, it would check if the Module had a SymbolFile FileSpec, and because it did not, it would do its own search for a binary & dSYM, find them, and then add that to the Target. Now we have two copies of the Module in the Target, one with a dSYM and the other without, and only one of them has its load addresses set. GetSharedModule should not be adding binaries to the Target, and it should set the SymbolFile FileSpec when it is creating the Module. rdar://120895951 (cherry picked from commit 430e145)
1 parent 7056313 commit 04e10ab

File tree

2 files changed

+49
-43
lines changed

2 files changed

+49
-43
lines changed

lldb/include/lldb/Target/Platform.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,32 @@ class Platform : public PluginInterface {
288288
LocateExecutableScriptingResources(Target *target, Module &module,
289289
Stream &feedback_stream);
290290

291+
/// \param[in] module_spec
292+
/// The ModuleSpec of a binary to find.
293+
///
294+
/// \param[in] process
295+
/// A Process.
296+
///
297+
/// \param[out] module_sp
298+
/// A Module that matches the ModuleSpec, if one is found.
299+
///
300+
/// \param[in] module_search_paths_ptr
301+
/// Locations to possibly look for a binary that matches the ModuleSpec.
302+
///
303+
/// \param[out] old_modules
304+
/// Existing Modules in the Process' Target image list which match
305+
/// the FileSpec.
306+
///
307+
/// \param[out] did_create_ptr
308+
/// Optional boolean, nullptr may be passed for this argument.
309+
/// If this method is returning a *new* ModuleSP, this
310+
/// will be set to true.
311+
/// If this method is returning a ModuleSP that is already in the
312+
/// Target's image list, it will be false.
313+
///
314+
/// \return
315+
/// The Status object for any errors found while searching for
316+
/// the binary.
291317
virtual Status GetSharedModule(
292318
const ModuleSpec &module_spec, Process *process,
293319
lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr,

lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,10 @@ Status PlatformDarwinKernel::GetSharedModuleKernel(
791791
const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp,
792792
const FileSpecList *module_search_paths_ptr,
793793
llvm::SmallVectorImpl<ModuleSP> *old_modules, bool *did_create_ptr) {
794-
Status error;
795-
module_sp.reset();
794+
assert(module_sp.get() == nullptr);
796795
UpdateKextandKernelsLocalScan();
796+
if (did_create_ptr)
797+
*did_create_ptr = false;
797798

798799
// First try all kernel binaries that have a dSYM next to them
799800
for (auto possible_kernel : m_kernel_binaries_with_dsyms) {
@@ -803,22 +804,19 @@ Status PlatformDarwinKernel::GetSharedModuleKernel(
803804
module_sp.reset(new Module(kern_spec));
804805
if (module_sp && module_sp->GetObjectFile() &&
805806
module_sp->MatchesModuleSpec(kern_spec)) {
806-
// module_sp is an actual kernel binary we want to add.
807-
if (process) {
808-
const bool notify = false;
809-
process->GetTarget().GetImages().AppendIfNeeded(module_sp, notify);
810-
error.Clear();
811-
return error;
812-
} else {
813-
error = ModuleList::GetSharedModule(kern_spec, module_sp, nullptr,
814-
nullptr, nullptr);
815-
if (module_sp && module_sp->GetObjectFile() &&
816-
module_sp->GetObjectFile()->GetType() !=
817-
ObjectFile::Type::eTypeCoreFile) {
818-
return error;
819-
}
820-
module_sp.reset();
821-
}
807+
// The dSYM is next to the binary (that's the only
808+
// way it ends up in the index), but it might be a
809+
// .dSYM.yaa that needs to be expanded, don't just
810+
// append ".dSYM" to the filename for the SymbolFile.
811+
FileSpecList search_paths =
812+
process->GetTarget().GetDebugFileSearchPaths();
813+
FileSpec dsym_fspec =
814+
PluginManager::LocateExecutableSymbolFile(kern_spec, search_paths);
815+
if (FileSystem::Instance().Exists(dsym_fspec))
816+
module_sp->SetSymbolFileFileSpec(dsym_fspec);
817+
if (did_create_ptr)
818+
*did_create_ptr = true;
819+
return {};
822820
}
823821
}
824822
}
@@ -836,36 +834,18 @@ Status PlatformDarwinKernel::GetSharedModuleKernel(
836834
module_sp.reset(new Module(kern_spec));
837835
if (module_sp && module_sp->GetObjectFile() &&
838836
module_sp->MatchesModuleSpec(kern_spec)) {
839-
// module_sp is an actual kernel binary we want to add.
840-
if (process) {
841-
const bool notify = false;
842-
process->GetTarget().GetImages().AppendIfNeeded(module_sp, notify);
843-
error.Clear();
844-
return error;
845-
} else {
846-
error = ModuleList::GetSharedModule(kern_spec, module_sp, nullptr,
847-
nullptr, nullptr);
848-
if (module_sp && module_sp->GetObjectFile() &&
849-
module_sp->GetObjectFile()->GetType() !=
850-
ObjectFile::Type::eTypeCoreFile) {
851-
return error;
852-
}
853-
module_sp.reset();
854-
}
837+
if (did_create_ptr)
838+
*did_create_ptr = true;
839+
return {};
855840
}
856841
}
857842
}
858843

859-
// Give the generic methods, including possibly calling into DebugSymbols
844+
// Give the generic methods, including possibly calling into DebugSymbols
860845
// framework on macOS systems, a chance.
861-
error = PlatformDarwin::GetSharedModule(module_spec, process, module_sp,
862-
module_search_paths_ptr, old_modules,
863-
did_create_ptr);
864-
if (error.Success() && module_sp.get()) {
865-
return error;
866-
}
867-
868-
return error;
846+
return PlatformDarwin::GetSharedModule(module_spec, process, module_sp,
847+
module_search_paths_ptr, old_modules,
848+
did_create_ptr);
869849
}
870850

871851
std::vector<lldb_private::FileSpec>

0 commit comments

Comments
 (0)