Skip to content

Instantly share code, notes, and snippets.

@embassem
Created April 13, 2022 15:33
Show Gist options
  • Save embassem/26145adf555240332c0e49df417145a6 to your computer and use it in GitHub Desktop.
Save embassem/26145adf555240332c0e49df417145a6 to your computer and use it in GitHub Desktop.
/// Indicates whether the app is running from Xcode or
/// the debugger had been attached to a running app from Finder
static var isDebuggerAttached: Bool {
var debuggerIsAttached = false
var name: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var info = kinfo_proc()
var info_size = MemoryLayout<kinfo_proc>.size
let success = name.withUnsafeMutableBytes { (nameBytePtr: UnsafeMutableRawBufferPointer)
-> Bool in
guard let nameBytesBlindMemory = nameBytePtr
.bindMemory(to: Int32.self)
.baseAddress else { return false }
return sysctl(
nameBytesBlindMemory,
4,
&info /* UnsafeMutableRawPointer! */,
&info_size /* UnsafeMutablePointer<Int>! */,
nil,
0
) != -1
}
// The original HockeyApp code checks for this
if !success {
debuggerIsAttached = false
}
if !debuggerIsAttached, (info.kp_proc.p_flag & P_TRACED) != 0 {
debuggerIsAttached = true
}
return debuggerIsAttached
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment