下面这个 C 函数可以用来检测你的程序是否正在被调试:
static bool debuggerRunning(void)
{
int junk;
int mib[4];
struct kinfo_proc info;
size_t size;
info.kp_proc.p_flag = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(junk == 0);
// If P_TRACED flag set, debugger running
return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
} |
你可以这样使用:
if (debuggerRunning())
NSLog(@"Debugger running");
else
NSLog(@"Debugger not running"); |