Returns true if the current process is being debugged (either running under the debugger or has a debugger attached post facto).
| 3944 | // Returns true if the current process is being debugged (either |
| 3945 | // running under the debugger or has a debugger attached post facto). |
| 3946 | bool isDebuggerActive(){ |
| 3947 | int mib[4]; |
| 3948 | struct kinfo_proc info; |
| 3949 | std::size_t size; |
| 3950 | |
| 3951 | // Initialize the flags so that, if sysctl fails for some bizarre |
| 3952 | // reason, we get a predictable result. |
| 3953 | |
| 3954 | info.kp_proc.p_flag = 0; |
| 3955 | |
| 3956 | // Initialize mib, which tells sysctl the info we want, in this case |
| 3957 | // we're looking for information about a specific process ID. |
| 3958 | |
| 3959 | mib[0] = CTL_KERN; |
| 3960 | mib[1] = KERN_PROC; |
| 3961 | mib[2] = KERN_PROC_PID; |
| 3962 | mib[3] = getpid(); |
| 3963 | |
| 3964 | // Call sysctl. |
| 3965 | |
| 3966 | size = sizeof(info); |
| 3967 | if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0) != 0 ) { |
| 3968 | Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n\n" << std::flush; |
| 3969 | return false; |
| 3970 | } |
| 3971 | |
| 3972 | // We're being debugged if the P_TRACED flag is set. |
| 3973 | |
| 3974 | return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); |
| 3975 | } |
| 3976 | #else |
| 3977 | bool isDebuggerActive() { |
| 3978 | // We need to find another way to determine this for non-appleclang compilers on macOS |
no test coverage detected