Returns true if the current process is being debugged (either running under the debugger or has a debugger attached post facto).
| 8438 | // Returns true if the current process is being debugged (either |
| 8439 | // running under the debugger or has a debugger attached post facto). |
| 8440 | bool isDebuggerActive(){ |
| 8441 | int mib[4]; |
| 8442 | struct kinfo_proc info; |
| 8443 | std::size_t size; |
| 8444 | |
| 8445 | // Initialize the flags so that, if sysctl fails for some bizarre |
| 8446 | // reason, we get a predictable result. |
| 8447 | |
| 8448 | info.kp_proc.p_flag = 0; |
| 8449 | |
| 8450 | // Initialize mib, which tells sysctl the info we want, in this case |
| 8451 | // we're looking for information about a specific process ID. |
| 8452 | |
| 8453 | mib[0] = CTL_KERN; |
| 8454 | mib[1] = KERN_PROC; |
| 8455 | mib[2] = KERN_PROC_PID; |
| 8456 | mib[3] = getpid(); |
| 8457 | |
| 8458 | // Call sysctl. |
| 8459 | |
| 8460 | size = sizeof(info); |
| 8461 | if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0 ) { |
| 8462 | Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n" << std::endl; |
| 8463 | return false; |
| 8464 | } |
| 8465 | |
| 8466 | // We're being debugged if the P_TRACED flag is set. |
| 8467 | |
| 8468 | return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); |
| 8469 | } |
| 8470 | #else |
| 8471 | bool isDebuggerActive() { |
| 8472 | // We need to find another way to determine this for non-appleclang compilers on macOS |
no test coverage detected