【问题标题】:GDB throws cryptic error when invoked from NSTask从 NSTask 调用时 GDB 抛出神秘错误
【发布时间】:2011-08-23 10:11:39
【问题描述】:

我尝试使用 NSTask 调用 GDB,变量“resultStringID”实际上是对应的进程 ID,“abc”只是一个 NSString。我调用GDB的代码如下

NSMutableString *resultStringID = [NSMutableString stringWithCapacity:processSelected.length];

        NSScanner *scanner = [NSScanner scannerWithString:processSelected];  
        //define the allowed characters, here only all numbers are allowed 
        NSCharacterSet *allowedChars = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"]; 

        while ([scanner isAtEnd] == NO) {  
            NSString *buffer;  
            if ([scanner scanCharactersFromSet:allowedChars intoString:&buffer]) {  
                [resultStringID appendString:buffer];       
            } else {  
                [scanner setScanLocation:([scanner scanLocation] + 1)];  
            }  
        }  

        //Trim off the last 3 characters if the string is larger than 4 characters long
        if ( [resultStringID length] > 4 )
            resultStringID = [resultStringID substringToIndex:[resultStringID length] - 3];


        NSInteger pid1 = [resultStringID intValue];

        //NSLog(@"pid 1 is :%@", pid1);

        NSTask *task = [[NSTask alloc] init];
        [task setLaunchPath: @"/usr/bin/gdb"];

        NSString * abc = @"abc";



        //NSLog(@"Process with extension %@", fullString);

        NSArray *arguments;
        arguments = [NSArray arrayWithObjects: abc, resultStringID, nil];
        [task setArguments: arguments];

        NSPipe *pipe;
        pipe = [NSPipe pipe];
        [task setStandardOutput: pipe];

        NSFileHandle *file;
        file = [pipe fileHandleForReading];

        [task launch];

        NSData *data;
        data = [file readDataToEndOfFile];

        NSString *string;
        string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
        NSLog (@"GDB Output:\n%@", string);

        /*
        GDBViewController *bController = [[GDBViewController alloc] init];
        [self.navigationController pushViewController:bController animated:YES];
        [bController release];
        */

        TableViewController_09AppDelegate *appDelegate = (TableViewController_09AppDelegate*)[[UIApplication sharedApplication] delegate];


        [string release];
        [task release];

非常感谢任何帮助! :)

任务启动的输出如下所示:

RE:Notice: Launching: com.apple.gdb
abc: No such file or directory 
//763: No such file or directory
Unable to access task for process-id 763: (os/kern) failure.
2011-08-24 08:53:06.002 TableViewController_09[764:507] GDB Output:
GNU gdb 6.3.50.20050815-cvs (Fri Mar 18 17:42:37 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "--host=arm-apple-darwin9 --target="...
(gdb) Exception condition detected on fd 0
error detected on stdin

【问题讨论】:

  • 你得到了什么错误,哪行代码抛出了它?如果我们不知道问题是什么,我们就无法解决问题。
  • 对不起,我把这个问题贴在了一个短暂的 o.o 中
  • 我已经发布了 GDB 在 iPhone 上部署时提供的输出
  • 如果您不给 GDB 标准输入,您期望会发生什么?
  • 我的主要目的是在视图中显示 GDB 命令的输出。当我像这样添加标准输入 NSPipe 时: NSPipe *pipe1; pipe1 = [NSPipe 管道]; [任务集标准输入:管道1];我的模拟器一直挂起,直到我退出它

标签: iphone objective-c gdb nstask


【解决方案1】:

error detected on stdin 行表示该进程没有标准输入。你已经为标准输出设置了NSPipe,为什么不也为标准输入设置一个?

【讨论】:

  • 当我像这样添加标准输入 NSPipe 时: NSPipe *pipe1; pipe1 = [NSPipe 管道]; [任务集标准输入:管道1];我的模拟器一直挂起,直到我退出它
  • 这是因为 GDB 需要输入,就像在命令行中使用它一样。你想让它做什么?
  • 我只想将它附加到有问题的进程中,并将输出放入字符串变量中。它还必须等待进一步的指令/命令
  • 所以你必须设置一个标准输入管道。如果您想向它传递输入,请在标准输入管道上发送它。
  • 所以我设置输入管道的方式(如我的第一条评论)是否正确?然后我将如何将输入传递给它?
【解决方案2】:

您的代码运行正常。

GDB 出错了,因为您提供给它的参数指向一个不存在的文件。这行输出:

2011-08-24 08:53:06.002 TableViewController_09[764:507] GDB Output:

…来自这行代码:

NSLog (@"GDB Output:\n%@", string);

并且该行输出之后的所有内容都来自 NSTask 实例。 error detected on stdin 是由您传递 abc 作为 gdb 运行的文件名引起的。

【讨论】:

  • 我正在尝试执行命令:“/usr/bin/gdb abc ”。此命令在终端中手动输入和执行时可以正常工作。我应该如何在我的应用程序中正确执行此命令?
  • 我不关心“没有这样的文件或目录”错误。只要 gdb 能够成功附加到进程并等待进一步的命令,我就实现了我的目标。
  • gdb 将在当前工作目录中执行。如果文件abc 不在目录中,它将找不到该文件。您是否尝试在设备上的 iOS 下运行它?
  • 是的。当我尝试在终端中运行完全相同的命令时,它会根据需要运行。有人告诉我,只有命令中的 processID 很重要。
猜你喜欢
  • 2012-01-24
  • 2019-07-05
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
相关资源
最近更新 更多