Skip to content

Commit 96ae6db

Browse files
committed
fixup! if path at DW_AT_LLVM_sysroot exists, use it
1 parent 2665e74 commit 96ae6db

File tree

14 files changed

+54
-163
lines changed

14 files changed

+54
-163
lines changed

lldb/include/lldb/Host/FileSystem.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ class FileSystem {
183183
eEnumerateDirectoryResultQuit
184184
};
185185

186-
typedef std::function<EnumerateDirectoryResult(
187-
void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef)>
188-
EnumerateDirectoryCallbackType;
186+
typedef EnumerateDirectoryResult (*EnumerateDirectoryCallbackType)(
187+
void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef);
189188

190189
typedef std::function<EnumerateDirectoryResult(
191190
llvm::sys::fs::file_type file_type, llvm::StringRef)>

lldb/include/lldb/Symbol/SymbolFile.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <mutex>
3333
#include <optional>
3434
#include <unordered_map>
35+
#include <utility>
3536

3637
#if defined(LLDB_CONFIGURATION_DEBUG)
3738
#define ASSERT_MODULE_LOCK(expr) (expr->AssertModuleLock())
@@ -148,7 +149,10 @@ class SymbolFile : public PluginInterface {
148149

149150
virtual lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) = 0;
150151
/// Return the Xcode SDK comp_unit was compiled against.
151-
virtual XcodeSDK ParseXcodeSDK(CompileUnit &comp_unit) { return {}; }
152+
virtual std::pair<XcodeSDK, std::string>
153+
ParseXcodeSDK(CompileUnit &comp_unit) {
154+
return {};
155+
}
152156

153157
/// This function exists because SymbolFileDWARFDebugMap may extra compile
154158
/// units which aren't exposed as "real" compile units. In every other

lldb/include/lldb/Symbol/SymbolFileOnDemand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SymbolFileOnDemand : public lldb_private::SymbolFile {
6565
lldb::LanguageType
6666
ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
6767

68-
lldb_private::XcodeSDK
68+
std::pair<lldb_private::XcodeSDK, std::string>
6969
ParseXcodeSDK(lldb_private::CompileUnit &comp_unit) override;
7070

7171
void InitializeObject() override;

lldb/include/lldb/Target/Platform.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -438,25 +438,6 @@ class Platform : public PluginInterface {
438438
return lldb_private::ConstString();
439439
}
440440

441-
/// Search each CU associated with the specified 'module' for
442-
/// the SDK paths the CUs were compiled against. In the presence
443-
/// of different SDKs, we try to pick the most appropriate one
444-
/// using \ref XcodeSDK::Merge.
445-
///
446-
/// \param[in] module Module whose debug-info CUs to parse for
447-
/// which SDK they were compiled against.
448-
///
449-
/// \returns If successful, returns a pair of a parsed XcodeSDK
450-
/// object and a boolean that is 'true' if we encountered
451-
/// a conflicting combination of SDKs when parsing the CUs
452-
/// (e.g., a public and internal SDK).
453-
virtual llvm::Expected<std::pair<XcodeSDK, bool>>
454-
GetSDKPathFromDebugInfo(Module &module) {
455-
return llvm::createStringError(
456-
llvm::formatv("{0} not implemented for '{1}' platform.",
457-
LLVM_PRETTY_FUNCTION, GetName()));
458-
}
459-
460441
/// Returns the full path of the most appropriate SDK for the
461442
/// specified 'module'. This function gets this path by parsing
462443
/// debug-info (see \ref `GetSDKPathFromDebugInfo`).
@@ -477,8 +458,9 @@ class Platform : public PluginInterface {
477458
///
478459
/// \param[in] unit The CU
479460
///
480-
/// \returns A parsed XcodeSDK object if successful, an Error otherwise.
481-
virtual llvm::Expected<XcodeSDK> GetSDKPathFromDebugInfo(CompileUnit &unit) {
461+
/// \returns A parsed XcodeSDK object if successful, an Error otherwise.
462+
virtual llvm::Expected<std::pair<XcodeSDK, std::string>>
463+
GetSDKPathFromDebugInfo(CompileUnit &unit) {
482464
return llvm::createStringError(
483465
llvm::formatv("{0} not implemented for '{1}' platform.",
484466
LLVM_PRETTY_FUNCTION, GetName()));

lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
#include "lldb/Utility/Log.h"
1616
#include "lldb/Utility/Timer.h"
1717

18-
#include "clang/Basic/DarwinSDKInfo.h"
1918
#include "llvm/ADT/ScopeExit.h"
2019
#include "llvm/ADT/SmallString.h"
2120
#include "llvm/ADT/StringMap.h"
22-
#include "llvm/Support/Error.h"
2321
#include "llvm/Support/FileSystem.h"
2422
#include "llvm/Support/Path.h"
25-
#include "llvm/Support/VersionTuple.h"
2623
#include "llvm/Support/raw_ostream.h"
2724

2825
// C++ Includes
@@ -572,52 +569,10 @@ static bool ResolveAndVerifyCandidateSupportDir(FileSpec &path) {
572569
cache.insert({key, {error, true}});
573570
return llvm::createStringError(llvm::inconvertibleErrorCode(), error);
574571
}
575-
576-
if (path_or_err->empty())
577-
return llvm::createStringError("Empty path determined for '%s'",
578-
key.data());
579-
580572
auto it_new = cache.insert({key, {*path_or_err, false}});
581573
return it_new.first->second.str;
582574
}
583575

584-
static llvm::Expected<std::string>
585-
GetCommandLineToolsSDKRoot(llvm::VersionTuple version) {
586-
std::string clt_root_dir;
587-
FileSystem::Instance().EnumerateDirectory(
588-
"/Library/Developer/CommandLineTools/SDKs/", /*find_directories=*/true,
589-
/*find_files=*/false, /*find_other=*/false,
590-
[&](void *baton, llvm::sys::fs::file_type file_type,
591-
llvm::StringRef name) {
592-
assert(file_type == llvm::sys::fs::file_type::directory_file);
593-
594-
if (!name.ends_with(".sdk"))
595-
return FileSystem::eEnumerateDirectoryResultNext;
596-
597-
llvm::Expected<std::optional<clang::DarwinSDKInfo>> sdk_info =
598-
clang::parseDarwinSDKInfo(
599-
*FileSystem::Instance().GetVirtualFileSystem(), name);
600-
if (!sdk_info) {
601-
LLDB_LOG_ERROR(GetLog(LLDBLog::Expressions), sdk_info.takeError(),
602-
"Error while parsing {1}: {0}", name);
603-
return FileSystem::eEnumerateDirectoryResultNext;
604-
}
605-
606-
if (!*sdk_info)
607-
return FileSystem::eEnumerateDirectoryResultNext;
608-
609-
if (version == (*sdk_info)->getVersion()) {
610-
clt_root_dir = name;
611-
return FileSystem::eEnumerateDirectoryResultQuit;
612-
}
613-
614-
return FileSystem::eEnumerateDirectoryResultNext;
615-
},
616-
/*baton=*/nullptr);
617-
618-
return clt_root_dir;
619-
}
620-
621576
llvm::Expected<llvm::StringRef> HostInfoMacOSX::GetSDKRoot(SDKOptions options) {
622577
static llvm::StringMap<ErrorOrPath> g_sdk_path;
623578
static std::mutex g_sdk_path_mutex;
@@ -626,21 +581,6 @@ static bool ResolveAndVerifyCandidateSupportDir(FileSpec &path) {
626581
"XcodeSDK not specified");
627582
XcodeSDK sdk = *options.XcodeSDKSelection;
628583
auto key = sdk.GetString();
629-
630-
// xcrun doesn't search SDKs in the CommandLineTools (CLT) directory. So if
631-
// a program was compiled against a CLT SDK, but that SDK wasn't present in
632-
// any of the Xcode installations, then xcrun would fail to find the SDK
633-
// (which is expensive). To avoid this we first try to find the specified SDK
634-
// in the CLT directory.
635-
auto clt_root_dir = find_cached_path(g_sdk_path, g_sdk_path_mutex, key, [&] {
636-
return GetCommandLineToolsSDKRoot(sdk.GetVersion());
637-
});
638-
639-
if (clt_root_dir)
640-
return clt_root_dir;
641-
else
642-
llvm::consumeError(clt_root_dir.takeError());
643-
644584
return find_cached_path(g_sdk_path, g_sdk_path_mutex, key, [&](){
645585
return GetXcodeSDK(sdk);
646586
});

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,7 @@ sdkSupportsBuiltinModules(lldb_private::Target &target) {
336336
if (!platform_sp)
337337
return llvm::createStringError("No Platform plugin found on target.");
338338

339-
auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo(*module_sp);
340-
if (!sdk_or_err)
341-
return sdk_or_err.takeError();
342-
343-
// Use the SDK path from debug-info to find a local matching SDK directory.
344-
auto sdk_path_or_err =
345-
HostInfo::GetSDKRoot(HostInfo::SDKOptions{std::move(sdk_or_err->first)});
339+
auto sdk_path_or_err = platform_sp->ResolveSDKPathFromDebugInfo(*module_sp);
346340
if (!sdk_path_or_err)
347341
return sdk_path_or_err.takeError();
348342

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

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,57 +1386,18 @@ llvm::Triple::OSType PlatformDarwin::GetHostOSType() {
13861386
#endif // __APPLE__
13871387
}
13881388

1389-
llvm::Expected<std::pair<XcodeSDK, bool>>
1390-
PlatformDarwin::GetSDKPathFromDebugInfo(Module &module) {
1391-
SymbolFile *sym_file = module.GetSymbolFile();
1392-
if (!sym_file)
1393-
return llvm::createStringError(
1394-
llvm::inconvertibleErrorCode(),
1395-
llvm::formatv("No symbol file available for module '{0}'",
1396-
module.GetFileSpec().GetFilename().AsCString("")));
1397-
1398-
bool found_public_sdk = false;
1399-
bool found_internal_sdk = false;
1400-
XcodeSDK merged_sdk;
1401-
for (unsigned i = 0; i < sym_file->GetNumCompileUnits(); ++i) {
1402-
if (auto cu_sp = sym_file->GetCompileUnitAtIndex(i)) {
1403-
auto cu_sdk = sym_file->ParseXcodeSDK(*cu_sp);
1404-
bool is_internal_sdk = cu_sdk.IsAppleInternalSDK();
1405-
found_public_sdk |= !is_internal_sdk;
1406-
found_internal_sdk |= is_internal_sdk;
1407-
1408-
merged_sdk.Merge(cu_sdk);
1409-
}
1410-
}
1411-
1412-
const bool found_mismatch = found_internal_sdk && found_public_sdk;
1413-
1414-
return std::pair{std::move(merged_sdk), found_mismatch};
1415-
}
1416-
14171389
llvm::Expected<std::string>
14181390
PlatformDarwin::ResolveSDKPathFromDebugInfo(Module &module) {
1419-
auto sdk_or_err = GetSDKPathFromDebugInfo(module);
1420-
if (!sdk_or_err)
1391+
auto cu_sp = module.GetCompileUnitAtIndex(0);
1392+
if (!cu_sp)
14211393
return llvm::createStringError(
1422-
llvm::inconvertibleErrorCode(),
1423-
llvm::formatv("Failed to parse SDK path from debug-info: {0}",
1424-
llvm::toString(sdk_or_err.takeError())));
1425-
1426-
auto [sdk, _] = std::move(*sdk_or_err);
1427-
1428-
auto path_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk});
1429-
if (!path_or_err)
1430-
return llvm::createStringError(
1431-
llvm::inconvertibleErrorCode(),
1432-
llvm::formatv("Error while searching for SDK (XcodeSDK '{0}'): {1}",
1433-
sdk.GetString(),
1434-
llvm::toString(path_or_err.takeError())));
1394+
"Couldn't retrieve compile-unit for module '%s'.",
1395+
module.GetObjectName().AsCString("<null>"));
14351396

1436-
return path_or_err->str();
1397+
return ResolveSDKPathFromDebugInfo(*cu_sp);
14371398
}
14381399

1439-
llvm::Expected<XcodeSDK>
1400+
llvm::Expected<std::pair<XcodeSDK, std::string>>
14401401
PlatformDarwin::GetSDKPathFromDebugInfo(CompileUnit &unit) {
14411402
ModuleSP module_sp = unit.CalculateSymbolContextModule();
14421403
if (!module_sp)
@@ -1459,7 +1420,10 @@ PlatformDarwin::ResolveSDKPathFromDebugInfo(CompileUnit &unit) {
14591420
llvm::formatv("Failed to parse SDK path from debug-info: {0}",
14601421
llvm::toString(sdk_or_err.takeError())));
14611422

1462-
auto sdk = std::move(*sdk_or_err);
1423+
auto [sdk, sysroot] = std::move(*sdk_or_err);
1424+
1425+
if (FileSystem::Instance().Exists(sysroot))
1426+
return sysroot;
14631427

14641428
auto path_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk});
14651429
if (!path_or_err)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,11 @@ class PlatformDarwin : public PlatformPOSIX {
124124
/// located in.
125125
static FileSpec GetCurrentCommandLineToolsDirectory();
126126

127-
llvm::Expected<std::pair<XcodeSDK, bool>>
128-
GetSDKPathFromDebugInfo(Module &module) override;
129-
130127
llvm::Expected<std::string>
131128
ResolveSDKPathFromDebugInfo(Module &module) override;
132129

133-
llvm::Expected<XcodeSDK> GetSDKPathFromDebugInfo(CompileUnit &unit) override;
130+
llvm::Expected<std::pair<XcodeSDK, std::string>>
131+
GetSDKPathFromDebugInfo(CompileUnit &unit) override;
134132

135133
llvm::Expected<std::string>
136134
ResolveSDKPathFromDebugInfo(CompileUnit &unit) override;

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,8 @@ lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
982982
return eLanguageTypeUnknown;
983983
}
984984

985-
XcodeSDK SymbolFileDWARF::ParseXcodeSDK(CompileUnit &comp_unit) {
985+
std::pair<XcodeSDK, std::string>
986+
SymbolFileDWARF::ParseXcodeSDK(CompileUnit &comp_unit) {
986987
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
987988
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
988989
if (!dwarf_cu)
@@ -993,21 +994,26 @@ XcodeSDK SymbolFileDWARF::ParseXcodeSDK(CompileUnit &comp_unit) {
993994
const char *sdk = cu_die.GetAttributeValueAsString(DW_AT_APPLE_sdk, nullptr);
994995
if (!sdk)
995996
return {};
996-
const char *sysroot =
997+
std::string sysroot =
997998
cu_die.GetAttributeValueAsString(DW_AT_LLVM_sysroot, "");
998-
// Register the sysroot path remapping with the module belonging to
999-
// the CU as well as the one belonging to the symbol file. The two
1000-
// would be different if this is an OSO object and module is the
1001-
// corresponding debug map, in which case both should be updated.
1002-
ModuleSP module_sp = comp_unit.GetModule();
1003-
if (module_sp)
1004-
module_sp->RegisterXcodeSDK(sdk, sysroot);
1005999

1006-
ModuleSP local_module_sp = m_objfile_sp->GetModule();
1007-
if (local_module_sp && local_module_sp != module_sp)
1008-
local_module_sp->RegisterXcodeSDK(sdk, sysroot);
1000+
// RegisterXcodeSDK calls into xcrun which is not aware of CLT, which is
1001+
// expensive.
1002+
if (sysroot.find("/Library/Developer/CommandLineTools/SDKs") != 0) {
1003+
// Register the sysroot path remapping with the module belonging to
1004+
// the CU as well as the one belonging to the symbol file. The two
1005+
// would be different if this is an OSO object and module is the
1006+
// corresponding debug map, in which case both should be updated.
1007+
ModuleSP module_sp = comp_unit.GetModule();
1008+
if (module_sp)
1009+
module_sp->RegisterXcodeSDK(sdk, sysroot);
1010+
1011+
ModuleSP local_module_sp = m_objfile_sp->GetModule();
1012+
if (local_module_sp && local_module_sp != module_sp)
1013+
local_module_sp->RegisterXcodeSDK(sdk, sysroot);
1014+
}
10091015

1010-
return {sdk};
1016+
return std::pair{XcodeSDK{sdk}, std::move(sysroot)};
10111017
}
10121018

10131019
size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ class SymbolFileDWARF : public SymbolFileCommon {
110110

111111
lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) override;
112112

113-
XcodeSDK ParseXcodeSDK(CompileUnit &comp_unit) override;
113+
std::pair<XcodeSDK, std::string>
114+
ParseXcodeSDK(CompileUnit &comp_unit) override;
114115

115116
size_t ParseFunctions(CompileUnit &comp_unit) override;
116117

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,8 @@ SymbolFileDWARFDebugMap::ParseLanguage(CompileUnit &comp_unit) {
673673
return eLanguageTypeUnknown;
674674
}
675675

676-
XcodeSDK SymbolFileDWARFDebugMap::ParseXcodeSDK(CompileUnit &comp_unit) {
676+
std::pair<XcodeSDK, std::string>
677+
SymbolFileDWARFDebugMap::ParseXcodeSDK(CompileUnit &comp_unit) {
677678
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
678679
SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
679680
if (oso_dwarf)

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class SymbolFileDWARFDebugMap : public SymbolFileCommon {
6464

6565
// Compile Unit function calls
6666
lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) override;
67-
XcodeSDK ParseXcodeSDK(CompileUnit &comp_unit) override;
67+
std::pair<XcodeSDK, std::string>
68+
ParseXcodeSDK(CompileUnit &comp_unit) override;
6869
llvm::SmallSet<lldb::LanguageType, 4>
6970
ParseAllLanguages(CompileUnit &comp_unit) override;
7071
size_t ParseFunctions(CompileUnit &comp_unit) override;

lldb/source/Symbol/SymbolFileOnDemand.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,18 @@ lldb::LanguageType SymbolFileOnDemand::ParseLanguage(CompileUnit &comp_unit) {
5858
return m_sym_file_impl->ParseLanguage(comp_unit);
5959
}
6060

61-
XcodeSDK SymbolFileOnDemand::ParseXcodeSDK(CompileUnit &comp_unit) {
61+
std::pair<XcodeSDK, std::string>
62+
SymbolFileOnDemand::ParseXcodeSDK(CompileUnit &comp_unit) {
6263
if (!m_debug_info_enabled) {
6364
Log *log = GetLog();
6465
LLDB_LOG(log, "[{0}] {1} is skipped", GetSymbolFileName(), __FUNCTION__);
6566
XcodeSDK defaultValue{};
6667
if (log) {
67-
XcodeSDK sdk = m_sym_file_impl->ParseXcodeSDK(comp_unit);
68+
auto [sdk, sysroot] = m_sym_file_impl->ParseXcodeSDK(comp_unit);
6869
if (!(sdk == defaultValue))
6970
LLDB_LOG(log, "SDK {0} would return if hydrated.", sdk.GetString());
7071
}
71-
return defaultValue;
72+
return {defaultValue, {}};
7273
}
7374
return m_sym_file_impl->ParseXcodeSDK(comp_unit);
7475
}

lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ TEST_F(XcodeSDKModuleTests, TestModuleGetXcodeSDK) {
118118
ASSERT_TRUE(static_cast<bool>(comp_unit.get()));
119119
ModuleSP module = t.GetModule();
120120
ASSERT_EQ(module->GetSourceMappingList().GetSize(), 0u);
121-
XcodeSDK sdk = sym_file.ParseXcodeSDK(*comp_unit);
121+
auto [sdk, sysroot] = sym_file.ParseXcodeSDK(*comp_unit);
122122
ASSERT_EQ(sdk.GetType(), XcodeSDK::Type::MacOSX);
123123
ASSERT_EQ(module->GetSourceMappingList().GetSize(), 1u);
124124
}

0 commit comments

Comments
 (0)