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];
NSLo开发者_运维百科g(@"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?
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 hangingNSTask
.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.
精彩评论