#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>
static bool Amibeingdebugged (void)
Returns true if the current process is being debugged (either
Running under the debugger or has a debugger attached post facto).
{
int junk;
int mib[4];
struct KINFO_PROC info;
size_t size;
Initialize the flags so, if sysctl fails for some bizarre
Reason, we get a predictable result.
Info.kp_proc.p_flag = 0;
Initialize MiB, which tells Sysctl the info we want, in this case
We ' re looking for information about a specific process ID.
Mib[0] = Ctl_kern;
MIB[1] = Kern_proc;
MIB[2] = kern_proc_pid;
MIB[3] = Getpid ();
Call Sysctl.
size = sizeof (info);
Junk = sysctl (MIB, sizeof (MIB)/sizeof (*MIB), &info, &size, NULL, 0);
ASSERT (junk = = 0);
We ' re being debugged if the p_traced flag is set.
Return ((Info.kp_proc.p_flag & p_traced)! = 0);
}
- Important:because the definition of the KINFO_PROC structure (in <sys/sysctl.h>) was conditionalized by __apple_api_ UNSTABLE, you should restrict use of the above code to the debug build of your program.
How does I determine if I ' m being run under the debugger?