Returns true if the current process is being debugged (either running under the debugger or has a debugger attached post facto).
| 10294 | // Returns true if the current process is being debugged (either |
| 10295 | // running under the debugger or has a debugger attached post facto). |
| 10296 | bool isDebuggerActive(){ |
| 10297 | int mib[4]; |
| 10298 | struct kinfo_proc info; |
| 10299 | std::size_t size; |
| 10300 | |
| 10301 | // Initialize the flags so that, if sysctl fails for some bizarre |
| 10302 | // reason, we get a predictable result. |
| 10303 | |
| 10304 | info.kp_proc.p_flag = 0; |
| 10305 | |
| 10306 | // Initialize mib, which tells sysctl the info we want, in this case |
| 10307 | // we're looking for information about a specific process ID. |
| 10308 | |
| 10309 | mib[0] = CTL_KERN; |
| 10310 | mib[1] = KERN_PROC; |
| 10311 | mib[2] = KERN_PROC_PID; |
| 10312 | mib[3] = getpid(); |
| 10313 | |
| 10314 | // Call sysctl. |
| 10315 | |
| 10316 | size = sizeof(info); |
| 10317 | if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0 ) { |
| 10318 | Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n" << std::endl; |
| 10319 | return false; |
| 10320 | } |
| 10321 | |
| 10322 | // We're being debugged if the P_TRACED flag is set. |
| 10323 | |
| 10324 | return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); |
| 10325 | } |
| 10326 | #else |
| 10327 | bool isDebuggerActive() { |
| 10328 | // We need to find another way to determine this for non-appleclang compilers on macOS |
no outgoing calls
no test coverage detected