Returns true if the current process is being debugged (either running under the debugger or has a debugger attached post facto).
| 32 | // Returns true if the current process is being debugged (either |
| 33 | // running under the debugger or has a debugger attached post facto). |
| 34 | bool isDebuggerActive(){ |
| 35 | int mib[4]; |
| 36 | struct kinfo_proc info; |
| 37 | std::size_t size; |
| 38 | |
| 39 | // Initialize the flags so that, if sysctl fails for some bizarre |
| 40 | // reason, we get a predictable result. |
| 41 | |
| 42 | info.kp_proc.p_flag = 0; |
| 43 | |
| 44 | // Initialize mib, which tells sysctl the info we want, in this case |
| 45 | // we're looking for information about a specific process ID. |
| 46 | |
| 47 | mib[0] = CTL_KERN; |
| 48 | mib[1] = KERN_PROC; |
| 49 | mib[2] = KERN_PROC_PID; |
| 50 | mib[3] = getpid(); |
| 51 | |
| 52 | // Call sysctl. |
| 53 | |
| 54 | size = sizeof(info); |
| 55 | if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0 ) { |
| 56 | Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n\n" << std::flush; |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | // We're being debugged if the P_TRACED flag is set. |
| 61 | |
| 62 | return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); |
| 63 | } |
| 64 | #else |
| 65 | bool isDebuggerActive() { |
| 66 | // We need to find another way to determine this for non-appleclang compilers on macOS |
no test coverage detected