• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: need way to debug tool called by popen--at startup
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: need way to debug tool called by popen--at startup


  • Subject: Re: need way to debug tool called by popen--at startup
  • From: Kyle McKay <email@hidden>
  • Date: Fri, 1 May 2009 17:44:20 -0700

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

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Can't set breakpoint in library
  • Next by Date: Re: Bug in Debugger?
  • Previous by thread: Re: Can't set breakpoint in library
  • Next by thread: Re: Bug in Debugger?
  • Index(es):
    • Date
    • Thread