Skip to content

Conversation

@Kyle-Ye
Copy link
Collaborator

@Kyle-Ye Kyle-Ye commented Sep 20, 2025

Summary

  • Implement cross-platform debugger detection functionality based on SwiftUI's isDebuggerAttached
  • Add Darwin-specific implementation using sysctl() with kinfo_proc and P_TRACED flag
  • Add Linux implementation using /proc/self/status TracerPid field
  • Include comprehensive test case to verify functionality

Implementation Details

Darwin (macOS/iOS)

  • Uses sysctl() system call with KERN_PROC_PID to get process information
  • Checks the P_TRACED flag in kp_proc.p_flag to detect debugger attachment
  • Includes error handling with os_log for failed system calls

Linux

  • Reads /proc/self/status file to get process status information
  • Parses the TracerPid field - non-zero value indicates debugger attachment
  • Gracefully handles file reading errors by returning false

Cross-platform Support

  • Uses conditional compilation with #if canImport(Darwin) for platform-specific code
  • Maintains consistent API across platforms with package let isDebuggerAttached: Bool
  • Falls back to safe defaults (false) when detection fails

Test Coverage

  • Added DebuggerTests.swift with test case ensuring isDebuggerAttached == false during normal execution
  • Follows project testing conventions using swift-testing framework
  • Test verifies both implementations work correctly in non-debugged environment

Compatibility

  • Compatible with existing OpenSwiftUI build system and environment variables
  • No breaking changes to public API
  • Maintains SwiftUI API compatibility goals

Provide a platform-level utility to detect whether a debugger is attached by adding Debugger.swift. This implements a package-scoped isDebuggerAttached Bool computed at initialization that calls sysctl(3) to query the current process (KERN_PROC_PID) and checks the P_TRACED flag. The change logs an error via os_log if sysctl fails and returns false in that case. This fixes crashes related to missing debugger-detection logic and enables tests that depend on safe debugger-state handling.
Wrap Darwin imports and the kinfo_proc-based debugger check in #if canImport(Darwin) so Linux builds (which lack kinfo_proc, CTL_KERN, KERN_PROC, etc.) no longer fail to compile. Provide a fallback constant (isDebuggerAttached = false) for non-Darwin platforms.
Add a Linux-specific implementation to detect if the current process is being debugged by reading /proc/self/status and checking the TracerPid field. This was needed because the previous default returned false on non-Apple platforms; the new code parses the status file and returns true when TracerPid is non-zero to correctly detect attached debuggers on Linux.
Add a unit test (DebuggerTests.testIsDebuggerAttachedIsFalse) that asserts isDebuggerAttached == false. This verifies the Debugger utility returns false in the test environment and prevents regressions. The change also cleans up conditional import placement in Debugger.swift to ensure compilation on Darwin platforms.
@codecov
Copy link

codecov bot commented Sep 20, 2025

Codecov Report

❌ Patch coverage is 85.71429% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 24.45%. Comparing base (9a7abef) to head (c099fd4).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
Sources/OpenSwiftUICore/Util/Debugger.swift 85.71% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #509      +/-   ##
==========================================
+ Coverage   24.38%   24.45%   +0.07%     
==========================================
  Files         483      484       +1     
  Lines       27893    27921      +28     
==========================================
+ Hits         6802     6829      +27     
- Misses      21091    21092       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Kyle-Ye Kyle-Ye merged commit 01e187a into main Sep 20, 2025
8 checks passed
@Kyle-Ye Kyle-Ye deleted the tooling/debugger-detection branch September 20, 2025 04: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.

2 participants