2

Im trying to use NSTask to invoke GDB, the variable "resultStringID" is actually the corresponding process ID and "abc" is just an NSString. My code for invoking GDB is as follows

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];

Any help is very much appreciated! :)

The output of the task launch looks like this :

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
Jared Aaron Loo
  • 668
  • 1
  • 10
  • 18

2 Answers2

0

The line error detected on stdin indicates that there's no standard input to the process. You have set up an NSPipe for standard output, why not also set one up for standard input?

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • When I add a standard input NSPipe like this : NSPipe *pipe1; pipe1 = [NSPipe pipe]; [task setStandardInput:pipe1]; My simulator just hangs until I exit it – Jared Aaron Loo Aug 24 '11 at 02:30
  • That's because GDB is expecting input, just like it does when you use it at the command line. What do you want it to do? – jtbandes Aug 24 '11 at 02:32
  • I just want to attach it to the process in question, and place the output into a string variable. It must also await further instructions/commands – Jared Aaron Loo Aug 24 '11 at 04:26
  • So you have to set up a standard input pipe. If you want to pass input to it, send it on the standard input pipe. – jtbandes Aug 24 '11 at 04:29
  • So my way of setting the input pipe (as in my first comment) is correct? How would i then pass the input into it? – Jared Aaron Loo Aug 24 '11 at 04:35
  • Use [`-fileHandleForWriting`](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPipe_Class/Reference/Reference.html#//apple_ref/occ/instm/NSPipe/fileHandleForWriting) to write data to the pipe. – jtbandes Aug 24 '11 at 04:38
  • So after doing so like this : NSFileHandle *file1; file1 = [pipe1 fileHandleForWriting]; Do i do another readDataToEndOfFile like this? NSData *data1; data1 = [file1 readDataToEndOfFile]; – Jared Aaron Loo Aug 24 '11 at 04:48
0

Your code is behaving properly.

GDB is erring out because the arguments you provide it points to a nonexistent file. This line of output:

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

… comes from this line of code:

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

and everything after that line of output comes from the NSTask instance. The error detected on stdin is caused by you passing abc as the file name for gdb to run.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • Im trying to execute the command : "/usr/bin/gdb abc ". This command works properly when typed out and executed manually in the terminal. How am I suppossed to execute this command in my app properly? – Jared Aaron Loo Aug 25 '11 at 00:38
  • Im not concerned with "No such file or directory" errors. As long as gdb is able to attach to the process successfully and await further commands, I have achieved my goal. – Jared Aaron Loo Aug 25 '11 at 00:43
  • gdb will execute in the current working directory. If the file `abc` is not in the directory, it will not find the file. Are you trying to run this under iOS on device? – TechZen Aug 25 '11 at 13:51
  • Yes. When i try to run the exact same command in the terminal, it runs as desired. I was told that only the processID in the command matters. – Jared Aaron Loo Aug 26 '11 at 01:21