diff --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h index f8a2cbf0d5d04..a702abb540fd9 100644 --- a/lldb/include/lldb/Target/Platform.h +++ b/lldb/include/lldb/Target/Platform.h @@ -473,6 +473,32 @@ class Platform : public PluginInterface { LLVM_PRETTY_FUNCTION, GetName())); } + /// Search CU for the SDK path the CUs was compiled against. + /// + /// \param[in] unit The CU + /// + /// \returns A parsed XcodeSDK object if successful, an Error otherwise. + virtual llvm::Expected GetSDKPathFromDebugInfo(CompileUnit &unit) { + return llvm::createStringError( + llvm::formatv("{0} not implemented for '{1}' platform.", + LLVM_PRETTY_FUNCTION, GetName())); + } + + /// Returns the full path of the most appropriate SDK for the + /// specified compile unit. This function gets this path by parsing + /// debug-info (see \ref `GetSDKPathFromDebugInfo`). + /// + /// \param[in] unit The CU to scan. + /// + /// \returns If successful, returns the full path to an + /// Xcode SDK. + virtual llvm::Expected + ResolveSDKPathFromDebugInfo(CompileUnit &unit) { + return llvm::createStringError( + llvm::formatv("{0} not implemented for '{1}' platform.", + LLVM_PRETTY_FUNCTION, GetName())); + } + bool IsHost() const { return m_is_host; // Is this the default host platform? } diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp index ed0c614cb3576..8baf3a8d60c37 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp @@ -29,6 +29,7 @@ #include "lldb/Interpreter/OptionValueProperties.h" #include "lldb/Interpreter/OptionValueString.h" #include "lldb/Interpreter/Options.h" +#include "lldb/Symbol/CompileUnit.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Symbol/SymbolFile.h" #include "lldb/Symbol/SymbolVendor.h" @@ -1429,3 +1430,39 @@ PlatformDarwin::ResolveSDKPathFromDebugInfo(Module &module) { return path_or_err->str(); } + +llvm::Expected +PlatformDarwin::GetSDKPathFromDebugInfo(CompileUnit &unit) { + ModuleSP module_sp = unit.CalculateSymbolContextModule(); + if (!module_sp) + return llvm::createStringError("compile unit has no module"); + SymbolFile *sym_file = module_sp->GetSymbolFile(); + if (!sym_file) + return llvm::createStringError( + llvm::formatv("No symbol file available for module '{0}'", + module_sp->GetFileSpec().GetFilename())); + + return sym_file->ParseXcodeSDK(unit); +} + +llvm::Expected +PlatformDarwin::ResolveSDKPathFromDebugInfo(CompileUnit &unit) { + auto sdk_or_err = GetSDKPathFromDebugInfo(unit); + if (!sdk_or_err) + return llvm::createStringError( + llvm::inconvertibleErrorCode(), + llvm::formatv("Failed to parse SDK path from debug-info: {0}", + llvm::toString(sdk_or_err.takeError()))); + + auto sdk = std::move(*sdk_or_err); + + auto path_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk}); + if (!path_or_err) + return llvm::createStringError( + llvm::inconvertibleErrorCode(), + llvm::formatv("Error while searching for SDK (XcodeSDK '{0}'): {1}", + sdk.GetString(), + llvm::toString(path_or_err.takeError()))); + + return path_or_err->str(); +} diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h index 66a26d2f49677..e2d4cb6726d58 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h @@ -130,6 +130,11 @@ class PlatformDarwin : public PlatformPOSIX { llvm::Expected ResolveSDKPathFromDebugInfo(Module &module) override; + llvm::Expected GetSDKPathFromDebugInfo(CompileUnit &unit) override; + + llvm::Expected + ResolveSDKPathFromDebugInfo(CompileUnit &unit) override; + protected: static const char *GetCompatibleArch(ArchSpec::Core core, size_t idx); diff --git a/lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp b/lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp index f1998a92f19b0..fc008ca7011e4 100644 --- a/lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp +++ b/lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp @@ -268,6 +268,13 @@ TEST_P(SDKPathParsingMultiparamTests, TestSDKPathFromDebugInfo) { EXPECT_EQ(found_mismatch, expect_mismatch); EXPECT_EQ(sdk.IsAppleInternalSDK(), expect_internal_sdk); EXPECT_NE(sdk.GetString().find(expect_sdk_path_pattern), std::string::npos); + + { + auto sdk_or_err = + platform_sp->GetSDKPathFromDebugInfo(*dwarf_cu->GetLLDBCompUnit()); + ASSERT_TRUE(static_cast(sdk_or_err)); + EXPECT_EQ(sdk.IsAppleInternalSDK(), expect_internal_sdk); + } } SDKPathParsingTestData sdkPathParsingTestCases[] = {