Returns true if the current process is being debugged (either running under the debugger or has a debugger attached post facto).
| 10312 | // Returns true if the current process is being debugged (either |
| 10313 | // running under the debugger or has a debugger attached post facto). |
| 10314 | bool isDebuggerActive(){ |
| 10315 | int mib[4]; |
| 10316 | struct kinfo_proc info; |
| 10317 | std::size_t size; |
| 10318 | |
| 10319 | // Initialize the flags so that, if sysctl fails for some bizarre |
| 10320 | // reason, we get a predictable result. |
| 10321 | |
| 10322 | info.kp_proc.p_flag = 0; |
| 10323 | |
| 10324 | // Initialize mib, which tells sysctl the info we want, in this case |
| 10325 | // we're looking for information about a specific process ID. |
| 10326 | |
| 10327 | mib[0] = CTL_KERN; |
| 10328 | mib[1] = KERN_PROC; |
| 10329 | mib[2] = KERN_PROC_PID; |
| 10330 | mib[3] = getpid(); |
| 10331 | |
| 10332 | // Call sysctl. |
| 10333 | |
| 10334 | size = sizeof(info); |
| 10335 | if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0 ) { |
| 10336 | Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n" << std::endl; |
| 10337 | return false; |
| 10338 | } |
| 10339 | |
| 10340 | // We're being debugged if the P_TRACED flag is set. |
| 10341 | |
| 10342 | return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); |
| 10343 | } |
| 10344 | #else |
| 10345 | bool isDebuggerActive() { |
| 10346 | // We need to find another way to determine this for non-appleclang compilers on macOS |
no test coverage detected