Skip to content

Commit aadb454

Browse files
committed
[lldb] Add a per-CU API to read the SDK
1 parent 1d95825 commit aadb454

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

lldb/include/lldb/Target/Platform.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,38 @@ class Platform : public PluginInterface {
473473
LLVM_PRETTY_FUNCTION, GetName()));
474474
}
475475

476+
/// Search CU for the SDK paths the CUs was compiled against. In the
477+
/// presence of different SDKs, we try to pick the most appropriate
478+
/// one using \ref XcodeSDK::Merge.
479+
///
480+
/// \param[in] unit The CU
481+
///
482+
/// \returns If successful, returns a pair of a parsed XcodeSDK
483+
/// object and a boolean that is 'true' if we encountered
484+
/// a conflicting combination of SDKs when parsing the CUs
485+
/// (e.g., a public and internal SDK).
486+
virtual llvm::Expected<std::pair<XcodeSDK, bool>>
487+
GetSDKPathFromDebugInfo(CompileUnit &unit) {
488+
return llvm::createStringError(
489+
llvm::formatv("{0} not implemented for '{1}' platform.",
490+
LLVM_PRETTY_FUNCTION, GetName()));
491+
}
492+
493+
/// Returns the full path of the most appropriate SDK for the
494+
/// specified compile unit. This function gets this path by parsing
495+
/// debug-info (see \ref `GetSDKPathFromDebugInfo`).
496+
///
497+
/// \param[in] unit The CU to scan.
498+
///
499+
/// \returns If successful, returns the full path to an
500+
/// Xcode SDK.
501+
virtual llvm::Expected<std::string>
502+
ResolveSDKPathFromDebugInfo(CompileUnit &unit) {
503+
return llvm::createStringError(
504+
llvm::formatv("{0} not implemented for '{1}' platform.",
505+
LLVM_PRETTY_FUNCTION, GetName()));
506+
}
507+
476508
bool IsHost() const {
477509
return m_is_host; // Is this the default host platform?
478510
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "lldb/Interpreter/OptionValueProperties.h"
3030
#include "lldb/Interpreter/OptionValueString.h"
3131
#include "lldb/Interpreter/Options.h"
32+
#include "lldb/Symbol/CompileUnit.h"
3233
#include "lldb/Symbol/ObjectFile.h"
3334
#include "lldb/Symbol/SymbolFile.h"
3435
#include "lldb/Symbol/SymbolVendor.h"
@@ -1429,3 +1430,40 @@ PlatformDarwin::ResolveSDKPathFromDebugInfo(Module &module) {
14291430

14301431
return path_or_err->str();
14311432
}
1433+
1434+
llvm::Expected<std::pair<XcodeSDK, bool>>
1435+
PlatformDarwin::GetSDKPathFromDebugInfo(CompileUnit &unit) {
1436+
ModuleSP module_sp = unit.CalculateSymbolContextModule();
1437+
if (!module_sp)
1438+
return llvm::createStringError("compile unit has no module");
1439+
SymbolFile *sym_file = module_sp->GetSymbolFile();
1440+
if (!sym_file)
1441+
return llvm::createStringError(
1442+
llvm::formatv("No symbol file available for module '{0}'",
1443+
module_sp->GetFileSpec().GetFilename()));
1444+
1445+
const bool found_mismatch = false;
1446+
return std::pair{sym_file->ParseXcodeSDK(unit), found_mismatch};
1447+
}
1448+
1449+
llvm::Expected<std::string>
1450+
PlatformDarwin::ResolveSDKPathFromDebugInfo(CompileUnit &unit) {
1451+
auto sdk_or_err = GetSDKPathFromDebugInfo(unit);
1452+
if (!sdk_or_err)
1453+
return llvm::createStringError(
1454+
llvm::inconvertibleErrorCode(),
1455+
llvm::formatv("Failed to parse SDK path from debug-info: {0}",
1456+
llvm::toString(sdk_or_err.takeError())));
1457+
1458+
auto [sdk, _] = std::move(*sdk_or_err);
1459+
1460+
auto path_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk});
1461+
if (!path_or_err)
1462+
return llvm::createStringError(
1463+
llvm::inconvertibleErrorCode(),
1464+
llvm::formatv("Error while searching for SDK (XcodeSDK '{0}'): {1}",
1465+
sdk.GetString(),
1466+
llvm::toString(path_or_err.takeError())));
1467+
1468+
return path_or_err->str();
1469+
}

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ class PlatformDarwin : public PlatformPOSIX {
130130
llvm::Expected<std::string>
131131
ResolveSDKPathFromDebugInfo(Module &module) override;
132132

133+
llvm::Expected<std::pair<XcodeSDK, bool>>
134+
GetSDKPathFromDebugInfo(CompileUnit &unit) override;
135+
136+
llvm::Expected<std::string>
137+
ResolveSDKPathFromDebugInfo(CompileUnit &unit) override;
138+
133139
protected:
134140
static const char *GetCompatibleArch(ArchSpec::Core core, size_t idx);
135141

lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ TEST_P(SDKPathParsingMultiparamTests, TestSDKPathFromDebugInfo) {
268268
EXPECT_EQ(found_mismatch, expect_mismatch);
269269
EXPECT_EQ(sdk.IsAppleInternalSDK(), expect_internal_sdk);
270270
EXPECT_NE(sdk.GetString().find(expect_sdk_path_pattern), std::string::npos);
271+
272+
{
273+
auto sdk_or_err =
274+
platform_sp->GetSDKPathFromDebugInfo(*dwarf_cu->GetLLDBCompUnit());
275+
ASSERT_TRUE(static_cast<bool>(sdk_or_err));
276+
EXPECT_EQ(sdk.IsAppleInternalSDK(), expect_internal_sdk);
277+
}
271278
}
272279

273280
SDKPathParsingTestData sdkPathParsingTestCases[] = {

0 commit comments

Comments
 (0)