A real stumper

I’ve run into a puzzling problem that’s kept me stumped for several days. I have a command-line tool that simply can’t be debugged. I’ve built it with optimizations turned off and debugging symbols enabled, but it won’t stop at any breakpoints, even if I put a breakpoint at the first line of main().

Someone suggested adding the command set start-with-shell off to ~/.gdbinit, but that didn’t help. I’ve also tried running gdb from the terminal and turning off ‘start executable after starting debugger’ in XCode’s preferences, but neither one helped. It will load into gdb without starting, but no source will be shown and it will run without stopping as soon as I start it.

I figured out a snippet of code that can identify whether or not it’s running in the debugger, and it properly recognizes that it’s running in the debugger, yet it won’t stop.

For anyone interested, here’s how you can tell if you’re running in a debugger:

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;
        sysctl(mib,4,&info,&size,NULL,0);
        return ((info.kp_proc.p_flag & P_TRACED) == P_TRACED);
}

Comments are closed.