2

我尝试使用 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
4

2 回答 2

0

您的代码运行正常。

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 运行的文件名引起的。

于 2011-08-24T12:39:36.023 回答
0

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

于 2011-08-24T02:00:16.403 回答