https://github.com/adrian-prantl updated https://github.com/llvm/llvm-project/pull/119022
>From 60ab35f50652e761d465cc1410b8d3352fa86b3f Mon Sep 17 00:00:00 2001 From: Adrian Prantl <apra...@apple.com> Date: Fri, 6 Dec 2024 12:09:27 -0800 Subject: [PATCH 1/2] [lldb] Add a per-CU API to read the SDK. This is needed by the Swift plugin. --- lldb/include/lldb/Target/Platform.h | 26 +++++++++++++ .../Platform/MacOSX/PlatformDarwin.cpp | 37 +++++++++++++++++++ .../Plugins/Platform/MacOSX/PlatformDarwin.h | 5 +++ .../SymbolFile/DWARF/XcodeSDKModuleTests.cpp | 7 ++++ 4 files changed, 75 insertions(+) diff --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h index f8a2cbf0d5d049..fc08e7c4096add 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 If successful, returns a parsed XcodeSDK object. + virtual llvm::Expected<XcodeSDK> 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<std::string> + 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 ed0c614cb3576b..8baf3a8d60c373 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<XcodeSDK> +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<std::string> +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 66a26d2f496776..e2d4cb6726d585 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<std::string> ResolveSDKPathFromDebugInfo(Module &module) override; + llvm::Expected<XcodeSDK> GetSDKPathFromDebugInfo(CompileUnit &unit) override; + + llvm::Expected<std::string> + 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 f1998a92f19b05..fc008ca7011e44 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<bool>(sdk_or_err)); + EXPECT_EQ(sdk.IsAppleInternalSDK(), expect_internal_sdk); + } } SDKPathParsingTestData sdkPathParsingTestCases[] = { >From 4facb5fc17280c27af99b014e621f7a573afb929 Mon Sep 17 00:00:00 2001 From: Adrian Prantl <adrian.pra...@gmail.com> Date: Fri, 6 Dec 2024 14:13:15 -0800 Subject: [PATCH 2/2] Update lldb/include/lldb/Target/Platform.h Co-authored-by: Jonas Devlieghere <jo...@devlieghere.com> --- lldb/include/lldb/Target/Platform.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h index fc08e7c4096add..a702abb540fd93 100644 --- a/lldb/include/lldb/Target/Platform.h +++ b/lldb/include/lldb/Target/Platform.h @@ -477,7 +477,7 @@ class Platform : public PluginInterface { /// /// \param[in] unit The CU /// - /// \returns If successful, returns a parsed XcodeSDK object. + /// \returns A parsed XcodeSDK object if successful, an Error otherwise. virtual llvm::Expected<XcodeSDK> GetSDKPathFromDebugInfo(CompileUnit &unit) { return llvm::createStringError( llvm::formatv("{0} not implemented for '{1}' platform.", _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits