Skip to content

[lldb] Read the precise SDK for the current CU first. #9704

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 9, 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 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()));
}

const std::string &GetRemoteURL() const { return m_remote_url; }

bool IsHost() const {
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
24 changes: 22 additions & 2 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ static std::string GetSDKPathFromDebugInfo(std::string m_description,
return {};
auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo(module);
if (!sdk_or_err) {
Debugger::ReportError("Error while parsing SDK path from debug-info: " +
Debugger::ReportError("Error while parsing SDK path from debug info: " +
toString(sdk_or_err.takeError()));
return {};
}
Expand Down Expand Up @@ -2825,9 +2825,29 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
FileSpec target_sdk_spec = target_sp ? target_sp->GetSDKPath() : FileSpec();
if (target_sdk_spec && FileSystem::Instance().Exists(target_sdk_spec)) {
swift_ast_sp->SetPlatformSDKPath(target_sdk_spec.GetPath());
LOG_PRINTF(GetLog(LLDBLog::Types), "Using target SDK override: %s",
target_sdk_spec.GetPath().c_str());
handled_sdk_path = true;
}

// Get the precise SDK from the symbol context.
if (cu)
if (auto platform_sp = Platform::GetHostPlatform()) {
auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo(*cu);
if (!sdk_or_err)
Debugger::ReportError("Error while parsing SDK path from debug info: " +
toString(sdk_or_err.takeError()));
else {
std::string sdk_path = GetSDKPath(m_description, *sdk_or_err);
if (!sdk_path.empty()) {
swift_ast_sp->SetPlatformSDKPath(sdk_path);
handled_sdk_path = true;
LOG_PRINTF(GetLog(LLDBLog::Types), "Using precise SDK: %s",
sdk_path.c_str());
}
}
}

if (!handled_sdk_path) {
for (size_t mi = 0; mi != num_images; ++mi) {
ModuleSP module_sp = modules.GetModuleAtIndex(mi);
Expand All @@ -2839,8 +2859,8 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
if (sdk_path.empty())
continue;

handled_sdk_path = true;
swift_ast_sp->SetPlatformSDKPath(sdk_path);
handled_sdk_path = true;
break;
}
}
Expand Down
24 changes: 17 additions & 7 deletions lldb/test/API/lang/swift/xcode_sdk/TestSwiftXcodeSDK.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ class TestSwiftXcodeSDK(lldbtest.TestBase):
mydir = lldbtest.TestBase.compute_mydir(__file__)
NO_DEBUG_INFO_TESTCASE = True

def check_log(self, log, expected_path):
def check_log(self, log, expected_path, expect_precise):
import io
logfile = io.open(log, "r", encoding='utf-8')
in_expr_log = 0
found = 0
precise = False
for line in logfile:
if (line.startswith(' SwiftASTContextForExpressions(module: "a", cu: "main.swift")::LogConfiguration()') and
"SDK path" in line and expected_path in line):
if not line.startswith(' SwiftASTContextForExpressions(module: "a", cu: "main.swift")'):
continue
if "Using precise SDK" in line:
precise = True
continue
if "Override target.sdk-path" in line:
precise = False
continue
if not '::LogConfiguration()' in line:
continue
if "SDK path" in line and expected_path in line:
found += 1
self.assertEqual(found, 1)
self.assertEqual(precise, expect_precise)

@swiftTest
@skipUnlessDarwin
Expand All @@ -33,7 +43,7 @@ def test_decode(self):
lldbutil.run_to_name_breakpoint(self, 'main')

self.expect("expression 1")
self.check_log(log, "MacOSX")
self.check_log(log, "MacOSX", True)

@swiftTest
@skipUnlessDarwin
Expand All @@ -52,7 +62,7 @@ def test_decode_sim(self):
lldbutil.run_to_name_breakpoint(self, 'main')

self.expect("expression 1")
self.check_log(log, "iPhoneSimulator")
self.check_log(log, "iPhoneSimulator", True)

@swiftTest
@skipUnlessDarwin
Expand All @@ -72,4 +82,4 @@ def test_override(self):
lldbutil.run_to_name_breakpoint(self, 'main')

self.expect("expression 1")
self.check_log(log, ios_sdk)
self.check_log(log, ios_sdk, False)
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