Add Backtrace capture feature (SE-0419) for programmatic stack inspection - #437
Merged
Merged
Conversation
…tion Integrate Swift 6.2 SE-0419 Backtrace API into DebugSwift for capturing and inspecting call stacks during normal execution. This is NOT a crash reporter — the Backtrace API is not async-signal-safe — it complements the existing crash/exception handling with on-demand programmatic backtrace capture. Library: - BacktraceManager: @unchecked Sendable singleton with NSLock-guarded storage (capped at 100, newest first), thread-safe callback/reload accessors, and a hybrid capture engine using `Backtrace.capture()` via `#if canImport(Runtime)` (macOS/Catalyst) with a `Thread.callStackSymbols` fallback for iOS. Internal frames (BacktraceCaptureEngine/BacktraceManager/DebugSwift.Performance.Backtrace) are filtered on both paths so frame 0 is the user's call site. - BacktraceEventsViewController: list + detail UI following the HangEventsViewController pattern. - Public API: `DebugSwift.Performance.Backtrace` enum with `capture(label:)`, `setOnCapture(_:)`, `clear()`, `backtraces`, and `backtracesOldestFirst`. - Performance.Controller: new `backtrace` section with "Capture Now" and "View Captures" rows. Example app: - BacktraceDemoView: iOS 13-safe demo (DateFormatter, plain Button) with capture/list/clear actions. - ContentView: Backtrace demo entry. Tests: - 13 BacktraceTests covering capture, ordering, capacity cap, frame filtering, unique IDs, and synchronous callback behavior. Closes #162. Co-authored-by: oh-my-pi <https://omp.sh>
Contributor
Author
CI failure analysisThe CI failure is Evidence:
This PR's changes do not touch StderrCapture or any file in the stderr capture path. |
Multiple test suites manipulate process-global state: - StderrCaptureTests: dup2 on fd 2 + ConsoleOutput.shared singleton - FeatureBaseTests: method swizzling on DebugSwift.shared.features - BacktraceTests: BacktraceManager.shared singleton With parallelizable=YES, parallel test workers race on this shared state, causing non-deterministic failures — a different single test fails each CI run (StderrCaptureTests one run, FeatureBaseTests.testDebugSwiftSwizzleFeature_allCases the next). Set parallelizable=NO so tests run serially in a single process, eliminating the race. Co-authored-by: oh-my-pi <https://omp.sh>
Root cause of the flaky CI failure in StderrCaptureTests.testSingleStderrWriteProducesOneConsoleEntry(): startCapturingInternal() set _isCapturing = true (line 97) BEFORE arming the readabilityHandler (line 105). Both run sequentially on the same serial captureQueue, but waitForCaptureReady() polls isCapturing from the test thread and returns the instant the flag flips — before the handler is attached. The test then writes the marker to the redirected pipe, but no handler is armed to read it. On a fast local machine the gap between lines 97 and 105 is ~0, so the handler is armed before the write. On CI runners the .utility- QoS captureQueue is starved under load, widening the gap beyond the 5s poll timeout — the marker sits unread in the pipe buffer and the test fails with "stderr marker was not captured." Fix: swap the order — arm the readabilityHandler FIRST, then set _isCapturing = true. Now isCapturing == true truthfully means "fd 2 is redirected AND the handler is armed." The test's short poll timeout is adequate because the handler fires on a private dispatch queue and the processingQueue does microseconds of work. Also bump captureQueue from .utility to .default QoS so it is not starved relative to the test thread under CI load. The previous commit (disable test parallelization) addresses a separate concern — test suites racing on process-global state (dup2, swizzling, singletons). Both fixes are needed. Co-authored-by: oh-my-pi <https://omp.sh>
Example.app: Coverage: 13.72
ExampleTests.xctest: Coverage: 97.34
Generated by 🚫 Danger Swift against e11ffd7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BacktraceManagersingleton with thread-safe (NSLock) storage capped at 100 captures (newest first), hybrid capture engine:Backtrace.capture().symbolicated()via#if canImport(Runtime)(macOS/Catalyst) withThread.callStackSymbolsfallback for iOS. Internal frames filtered on both paths so frame 0 is the user's call site.BacktraceEventsViewControllerlist + detail UI followingHangEventsViewControllerpattern.DebugSwift.Performance.Backtracewithcapture(label:),setOnCapture(_:),clear(),backtraces,backtracesOldestFirst.BacktraceDemoView, iOS 13-safe) and 13 unit tests.Closes #162.
Type of change
Test plan
Manual test steps
xcodebuild test -only-testing:ExampleTests/BacktraceTests→ 13 tests pass (0 failures in 0.862s)Checklist
Co-authored-by: oh-my-pi https://omp.sh