Rog
Rog

Reputation: 17170

Any way to tell if my iPhone app is running under the debugger at runtime?

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.

Upvotes: 4

Views: 931

Answers (2)

epatel
epatel

Reputation: 46041

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";
}

Upvotes: 5

sfossen
sfossen

Reputation: 4778

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.

Upvotes: 1

Related Questions