This is an archive of the discontinued LLVM Phabricator instance.

[lldb][PlatformDarwin] Parse SDK path for module compilation from debug-info
ClosedPublic

Authored by Michael137 on Jul 22 2023, 3:23 AM.

Details

Summary

When we build the Clang module compilation command (e.g., when
a user requests import of a module via expression @import Foundation),
LLDB will try to determine which SDK directory to use as the sysroot.
However, it currently does so by simply enumerating the SDKs directory
and picking the last one that's appropriate for module compilation
(see PlatformDarwin::GetSDKDirectoryForModules). That means if we have
multiple platform SDKs installed (e.g., a public and internal one), we
may pick the wrong one by chance.

On Darwin platforms we emit the SDK path that a object
file was compiled against into DWARF (using DW_AT_LLVM_sysroot
and DW_AT_APPLE_sdk). For Swift debugging, we already parse the SDK
path from debug-info if we can.

This patch mimicks the Swift behaviour for non-Swift languages. I.e.,
if we can get the SDK path from debug-info, do so. Otherwise, fall back
to the old heuristic.

rdar://110407148

Diff Detail

Event Timeline

Michael137 created this revision.Jul 22 2023, 3:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 22 2023, 3:23 AM
Michael137 requested review of this revision.Jul 22 2023, 3:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 22 2023, 3:23 AM
Michael137 added inline comments.Jul 22 2023, 3:25 AM
lldb/include/lldb/Target/Platform.h
493

I'm open to suggestions on where to put this API. Platform seems a bit too generic. But the intention is to share it with the Swift plugin. I could put it into PlatformDarwin.

  • Remove redundant headers
Michael137 marked an inline comment as not done.
Michael137 added inline comments.Jul 22 2023, 3:33 AM
lldb/include/lldb/Target/Platform.h
493

See how I intend to use it from the Swift plugin here

aprantl added inline comments.Jul 24 2023, 10:41 AM
lldb/include/lldb/Target/Platform.h
480

These flags really only make sense in the context of an XcodeSDK, so why not just return an XcodeSDK or XcodeSDK::Info object here? Otherwise we'll probably introduce subtle bugs due to a lossy translation between the flags.

Michael137 added inline comments.Jul 24 2023, 10:53 AM
lldb/include/lldb/Target/Platform.h
480

Yup I think that'd be better. That'll also make it easier to use from the Swift plugin

  • Move into PlatformDarwin
  • Return XcodeSDK from GetSDKPathFromDebugInfo so it's easier to re-use from Swift plugin
  • Introduce ResolveSDKPathFromDebugInfo to be used from PlatformDarwin
  • Adjust tests
Michael137 marked an inline comment as done.Jul 25 2023, 1:56 AM
  • Remove redundant header
  • Remove more headers
Michael137 added inline comments.Jul 25 2023, 2:03 AM
lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
1363

Only remaining question is whether we want to limit this to just Objective-C and Swift. Going through each compile unit for C++ seems like a lot of work for something that we won't use

@aprantl

Michael137 marked 2 inline comments as not done.Jul 25 2023, 7:19 AM
Michael137 added inline comments.
lldb/include/lldb/Target/Platform.h
480

Actually on second look, the XcodeSDK and XcodeSDK::Info objects represent information about a single (possibly parsed) SDK path. Whereas what the intention here was is to let the caller know whether we encountered a public/internal SDK while scanning all the CUs. Since we only return a single XcodeSDK (not all the ones we looked at) in my opinion it isn't quite right to store that information in it.

This is all really only used to print a Swift health. Maybe we could instead just log this to LLDBLog::Types? Then we don't need to worry about returning any of this information. @aprantl

Michael137 marked an inline comment as not done.
  • Update unit-tests
  • Expand function docs
  • Parameterize tests
  • Return bool to indicate SDK mismatch
  • EXPECT -> ASSERT
aprantl accepted this revision.Jul 26 2023, 9:02 AM
aprantl added inline comments.
lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
1363

The work to detect the language and to get the SDK attribute is almost the same, both are stored in the top-level CU DIE.

This revision is now accepted and ready to land.Jul 26 2023, 9:02 AM