views:

399

answers:

2

I would like to have my error handling code behave differently if it is running under the debugger. Specifically, if I am running on a handset, not attached to a debugger and fail an assertion I want to send the error to my server. When I am under gdb, I want to break into the debugger.

Although I can imagine how Apple would write the code, I can't find any documentation of a runtime way to test for the presence of the debugger.

+1  A: 

Why not redefine assert to do what you want, when not compiled for debug?

Another option, is to create your own assert function, where you can then add a breakpoint on loading into GDB.

The assert prototype is

void assert(int expression);

void assert(int expression)
{
    if( !expression )
    {
       // enable break point here
       // log to server
    }
}

or add the break point into the log to server code.

sfossen
I would still want it to behave differently under the debugger - it's not just debug builds, I would like to behave differently when gdb is attached.
Roger Nolan
Are you planning on distributing with debug symbols? Otherwise it makes little sense to not have a debug/release build. If you are trying to protect the application from a debugger, that is a whole other thing :P.
sfossen
+5  A: 

The method described here worked fine for me

I tested by placing it in -(void)viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    int mib[4];
    size_t bufSize = 0;
    int local_error = 0;
    struct kinfo_proc kp;

    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID;
    mib[3] = getpid();

    bufSize = sizeof (kp);
    if ((local_error = sysctl(mib, 4, &kp, &bufSize, NULL, 0)) < 0) {
        label.text = @"Failure calling sysctl";
        return;
    }
    if (kp.kp_proc.p_flag & P_TRACED)
        label.text = @"I am traced";
    else
        label.text = @"I am not traced";
}
epatel