Detecting debugger on Mac OS X

后端 未结 3 1961
隐瞒了意图╮
隐瞒了意图╮ 2021-02-10 15:43

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         


        
3条回答
  •  深忆病人
    2021-02-10 16:26

    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")
    }
    

提交回复
热议问题