Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Sources/OpenSwiftUICore/Util/Debugger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Debugger.swift
// OpenSwiftUICore
//
// Audited for 6.5.4
// Status: Complete

#if canImport(Darwin)
import Darwin
import os

package let isDebuggerAttached: Bool = {
var info = kinfo_proc()
var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var size = MemoryLayout.stride(ofValue: info)

let result = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)

guard result == 0 else {
os_log(.error, "sysctl(3) failed: %{errno}d", errno)
return false
}
return (info.kp_proc.p_flag & P_TRACED) != 0
}()
#else
import Foundation

package let isDebuggerAttached: Bool = {
guard let statusData = try? Data(contentsOf: URL(fileURLWithPath: "/proc/self/status")),
let statusString = String(data: statusData, encoding: .utf8) else {
return false
}

for line in statusString.components(separatedBy: .newlines) {
if line.hasPrefix("TracerPid:") {
let components = line.components(separatedBy: .whitespaces)
if components.count >= 2,
let tracerPid = Int(components[1]),
tracerPid != 0 {
return true
}
break
}
}

return false
}()
#endif
17 changes: 17 additions & 0 deletions Tests/OpenSwiftUICoreTests/Util/DebuggerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// DebuggerTests.swift
// OpenSwiftUICoreTests
//
// Audited for 6.5.4
// Status: Complete
//

import Testing
@testable import OpenSwiftUICore

struct DebuggerTests {
@Test
func attached() {
#expect(isDebuggerAttached == false)
}
}