Skip to content

🍒 [lldb][HostInfoMacOSX] Try to use DW_AT_LLVM_sysroot instead of xcrun when looking up SDK #10157

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

Conversation

Michael137
Copy link

GetSDKRoot uses xcrun to find an SDK root path for a given SDK version string. But if the SDK doesn't exist in the Xcode installations, but instead lives in the CommandLineTools, xcrun will fail to find it. Negative searches for an SDK path cost a lot (a few seconds) each time xcrun is invoked. We do cache negative results in find_cached_path inside LLDB, but we would still pay the price on every new debug session the first time we evaluate an expression. This doesn't only cause a noticable delay in running the expression, but also generates following error:

error: Error while searching for Xcode SDK: timed out waiting for shell command to complete
(int) $0 = 42

In this patch we avoid these possibly expensive calls to xcrun by checking the DW_AT_LLVM_sysroot, and if it exists, using that as the SDK path. We need an explicit check for the CommandLineTools path before we call RegisterXcodeSDK, because that will try to call xcrun. This won't prevent other uses of GetSDKRoot popping up that cause us to make expensive xcrun calls, but for now this addresses the regression in the expression evaluator. We also had to adjust the XcodeSDK::Merge logic to update the sysroot. There is one case for which this wouldn't make sense: if a CU was compiled with CommandLineTools and a different one with an older internal SDK, in that case we would update the CommandLineTools sysroot with a .Internal.sdk prefix, which won't possibly exist for CommandLineTools. I added a unit-test for this. Not sure if we want to explicitly detect and disallow this, given it's quite a niche scenario.

rdar://113619904
rdar://113619723

(cherry picked from commit 77a8770)

…ping checks

This assertion was added to check that `RegisterXcodeSDK` will correctly update the source mappings of the module. However, the source mapping will only get updated if the command-line invocation of `xcrun` succeeded. Even if `xcrun` failed to find an SDK, the source mappings would get an entry (see https://github.com/llvm/llvm-project/blob/f6212c1cd3d8b827c7d7e2f6cf54b135c27eacc6/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm#L424-L444). But if the shell invocation itself failed, then the mappings are not updated. This means depending on how slow `xcrun` is on a given host, this test may fail. On my machine this happens consistently in debug and release builds.

This patch removes this flakey assertion. We unfortunately lost some
test coverage here but I'm not sure there's great alternatives unless we
either:
1. Mock the `xcrun` call somehow (we could maybe pass a callable around
   which defaults to `xcrun` in non-test code?)
2. Make a `xcrun` time-out not an error either?

(cherry picked from commit 338c9a4)
…of xcrun when looking up SDK" (llvm#129621)"

This reverts commit 6041c74.

Relands the original patch with the test-case data fixed. Weirldy the PR CI
didn't seem to run the unit-tests? In any case, the problem was an
incorrect expectation in the test-case data. Since we have both public
and internal SDK in that test-case, we should `expect_mismatch` to be
`true`.

(cherry picked from commit 77a8770)
@Michael137 Michael137 requested a review from adrian-prantl March 4, 2025 12:28
@Michael137 Michael137 requested a review from a team as a code owner March 4, 2025 12:28
@Michael137
Copy link
Author

@swift-ci test

@Michael137 Michael137 merged commit 8f75a79 into stable/20240723 Mar 5, 2025
3 checks passed
@Michael137 Michael137 deleted the lldb/command-line-tools-search-to-20240723 branch March 5, 2025 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant