• 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: Attaching to newly-exec'd process
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Attaching to newly-exec'd process


  • Subject: Re: Attaching to newly-exec'd process
  • From: Jim Ingham <email@hidden>
  • Date: Fri, 18 Jan 2008 14:29:40 -0800

"follow-fork child" does not work on Mac OS X. We need some kernel support that's not there yet.

The time honored trick is to put a spin loop like:

int wait = 1;

while (wait) {;}

in the main of your program (if you want to be fancy you can check an environment variable and only spin if that is set.) Then when B gets launched, you can attach with Xcode, set the "wait" variable to 0, set your breakpoints, and continue.

If you're working on Leopard, and if your process "A" is in the business of launching many workers, you could also have "A" conditionally start the child in a stopped state. This is particularly easy if you are using posix_spawn to start up your child processes, since then you can just set the POSIX_SPAWN_START_SUSPENDED flag attribute. Otherwise you have to use PT_TRACEME on the child side of the fork, and SIGSTOP, then PT_DETACH from it on the parent side. I don't know if there's any way to do this when using NSTask.

Note that this only works on Leopard, there was a kernel bug in Tiger & before that prevented restarting SIGSTOP'ed processes once the debugger attached to them.

Finally, in the Xcode 3.0 tools gdb now supports a "-waitfor" flag to the "attach" command. So you can say:

(gdb) attach -waitfor Foo

and gdb will wait for a process named "Foo" to be launched, and attach to it when it does. If you have a decently fast machine, gdb will usually catch your program before it gets out of the dyld loading code - so unless you need to debug shared library initializers, this should work for you.

There's no UI to do this in Xcode, but you can uncheck "Start Executable when starting debugger" in the Debugging tab of the Executable editor for the executable you are debugging, and then start the debugger and type the above. You might have to do a "Run->Sync with Debugger" after the debugger attaches to get Xcode to notice that you've attached, not sure about that.

Note that if you have multiple processes of the same name as the one you're trying to wait for, there's no way in this "-waitfor" method to pick a particular one. But if you have just one process with this name at a time, this is pretty convenient.

Jim

On Jan 18, 2008, at 1:55 PM, Peter O'Gorman wrote:

Scott Ribe wrote:
Given a process-per-connection server, process A gets the socket, vforks,
dups the socket, and execs a different executable image B--how to attach to
B and debug??? Specifically, I want to step main from the start, not attach
to the process after it's been running a while. Is there a way to do that,
besides sleeping main in the first line?



http://sourceware.org/gdb/current/onlinedocs/gdb_5.html#SEC28

Have not tried, but I assume it works as described for Mac OS X too.

Peter
--
Peter O'Gorman
http://pogma.com
_______________________________________________
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

_______________________________________________ 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
  • Follow-Ups:
    • Re: Attaching to newly-exec'd process
      • From: Steve Checkoway <email@hidden>
    • Re: Attaching to newly-exec'd process
      • From: Peter O'Gorman <email@hidden>
References: 
 >Attaching to newly-exec'd process (From: Scott Ribe <email@hidden>)
 >Re: Attaching to newly-exec'd process (From: Peter O'Gorman <email@hidden>)

  • Prev by Date: Re: Attaching to newly-exec'd process
  • Next by Date: Re: Attaching to newly-exec'd process
  • Previous by thread: Re: Attaching to newly-exec'd process
  • Next by thread: Re: Attaching to newly-exec'd process
  • Index(es):
    • Date
    • Thread