1

I'm trying to create an NSTask that uses GDB to attach to a program, but my program just freezes after launching the task. Is this possible to do? Here is the code I'm using:

NSTask  *task = [NSTask new];
[task setLaunchPath:@"/usr/bin/gdb"];
NSArray *args = [NSArray arrayWithObjects:@"TestApp.app", nil];
[task setArguments:args];
[task launch];
NSLog(@"Launched.");

NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile];
NSLog(@"Read data.");

I'm certain "TestApp.app" is in the correct location because I don't get "No such file or directory" errors. The console only prints "Launched." and the spinning beachball just continues for over a minute until I kill the run. Any ideas what could make this work?

cdever
  • 41
  • 1
  • I suggest you read the following posts on Cocoa Dev Central: [Wrapping UNIX Commands](http://cocoadevcentral.com/articles/000025.php) and [Wrapping UNIX Commands Part II](http://cocoadevcentral.com/articles/000031.php). –  Apr 30 '11 at 02:58
  • Does your gdb NSTask detach immediately after executing the command or does it create a persistent session (like in terminal)? – Jared Aaron Loo Nov 02 '11 at 07:20

1 Answers1

1

Some things to consider:

  • It’s wise to set a pipe for standard input. There are some situations, particularly when NSLog() is called, that end up hanging NSTask.

  • If you send -readDataToEndOfFile to the standard output handle, your thread will pause until the task has finished executing. This is particularly bad if that code is running on the main thread — no user interface changes or application events will be processed, which most likely ends up beachballing the application. Use the …inBackground… methods instead.

  • You’re not sending data to standard input. If gdb doesn’t receive any input, it waits indefinitely until it receives a command.

Community
  • 1
  • 1