One Quickie

Am I Being Debugged? (Debugging->General)
int AmIBeingDebugged(void)
        int                                        mib[4];
        struct kinfo_proc        info;
        size_t                                size;
        mib[0] = CTL_KERN;
        mib[1] = KERN_PROC;
        mib[2] = KERN_PROC_PID;
        mib[3] = getpid();
        size = sizeof(info);
        info.kp_proc.p_flag = 0;
        return ((info.kp_proc.p_flag & P_TRACED) == P_TRACED);

#define StopIfInDebugger()        __asm__ volatile ("twnei %0,0" : : "r" (AmIBeingDebugged()))
(courtesy of arwyn on #macdev. Useful uses: controlling voluminous console output when running in gdb, or if running non-debugged, an assert routine that generates stack trace and logs it when not-being debugged, but just traps into the debugger at the point of failure when being debugged)

borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book