I am trying to detect whether my process is being run in a debugger or not and, while in Windows there are many solutions and in Linux I use:
ptrace(PTRACE_ME,0
Here's a Swift version of the function from Apple Technical Q&A QA1361:
import Foundation
extension ProcessInfo {
/// - returns: true if the process is being debugged, else false.
public func isBeingDebugged() -> Bool {
// https://developer.apple.com/library/archive/qa/qa1361/_index.html
// Technical Q&A QA1361: Detecting the Debugger
var mib: [Int32] = [
CTL_KERN,
KERN_PROC,
KERN_PROC_PID,
processIdentifier
]
var info = kinfo_proc()
var size: size_t = MemoryLayout.size(ofValue: info)
let rc = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
assert(rc == 0)
return (info.kp_proc.p_flag & P_TRACED) != 0
}
}
Usage:
if ProcessInfo.processInfo.isBeingDebugged() {
print("running under the debugger")
}