Dennis,
Insert the following code near the top of your main source file for the cmd line tool:
/* These 6 needed for _wait_for_debug */
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/sysctl.h>
__attribute__((used)) __attribute__((constructor))
static void _wait_for_debug(void)
{
for (;;) {
int mib[4];
size_t len;
struct kinfo_proc kp;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
len = sizeof(kp);
if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1) {
fprintf(stderr, "sysctl failed (%d): %s\n", errno, strerror(errno));
return;
}
if (kp.kp_proc.p_flag & P_TRACED) break;
pause(); /* If problems, use "sleep(1);" here instead */
}
}
The above code will cause the tool to wait for the debugger to attach before continuing.
It will enter the wait very early in its startup code before main is ever run which should
allow you to attach and debug the exception.
Kyle Mckay
On Apr 30, 2009, at 09:35:02 PDT, Dennis Christopher <
email@hidden> wrote:
We have a main application and cmd line tool--both targets in the same Xcode project. The former
calls the latter when it runs with a popen() call. I can symbolically debug the tool by
running the main app from the Finder, then, in Xcode with the tool target selected, doing
"Attach to process..". The tool then stops at any breakpoints set.
The problem I have is I want to debug an exception that occurs near
to the startup of the tool, and by the time I go through the Attach step back in XCode the tool
is already past that point. Perhaps there is a way with gdb to do this...? any suggestions would be
appreciated.
Xcode 3.1.1 on 10.5.6
Dennis Christopher