为gdb创建NSTask
我正在尝试创建一个使用 GDB 附加到程序的 NSTask,但我的程序在启动任务后就冻结了。这可以吗?这是我正在使用的代码:
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.");
我确信“TestApp.app”位于正确的位置,因为我没有收到“没有这样的文件或目录”错误。控制台仅打印“已启动”。旋转的沙滩球持续了一分多钟,直到我停止奔跑。有什么想法可以让这项工作成功吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要考虑的一些事情:
明智的做法是 为标准输入设置管道。在某些情况下,特别是在调用
NSLog()
时,最终会挂起NSTask
。如果您将
-readDataToEndOfFile
发送到标准输出句柄,您的线程将暂停,直到任务执行完毕。如果该代码在主线程上运行,这尤其糟糕 - 不会处理用户界面更改或应用程序事件,这很可能最终导致应用程序瘫痪。请改用...inBackground...
方法。您没有将数据发送到标准输入。如果
gdb
没有收到任何输入,它会无限期地等待,直到收到命令。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.