Skip to content

[lldb] Add a per-CU API to read the SDK #119022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lldb/include/lldb/Target/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<XcodeSDK> GetSDKPathFromDebugInfo(CompileUnit &unit) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we abstract a base class for SDK and have XcodeSDK become a plug-in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd volunteer to do that work if we had another platform that also has a concept of an SDK. Otherwise it'd be dangerous that I design the base class to be way too close to Xcode's SDK properties.

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?
}
Expand Down
37 changes: 37 additions & 0 deletions lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
}
5 changes: 5 additions & 0 deletions lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down
Loading