bot47
bot47

Reputation: 1514

Piping Into Application Run Under Xcode

I'm trying to debug a C command line application using Xcode on OS X Lion. The application needs a lot input data via standard input. On the shell I just pipe a text file into this application.

How can I do this with Xcode?

Upvotes: 2

Views: 2382

Answers (3)

user149533
user149533

Reputation:

You can't, unfortunately. I'm having to modify my program to read from a file for debugging purposes, like this:

    NSFileHandle *input;

    if (AmIBeingDebugged())
        input = [NSFileHandle fileHandleForReadingAtPath:@"/Users/me/debug.input"];
    else
        input = [NSFileHandle fileHandleWithStandardInput];

The source for AmIBeingDebugged is here.

Upvotes: 1

ThinkBonobo
ThinkBonobo

Reputation: 16515

Just for better clarification of @Nestor's answer....

I couldn't figure out a way to pipe into x-code so I added the following code at the beginning of my script that piped from a test file called test file.

    // For debugging
if (argc == 2 && strcmp(argv[1], "debug") == 0 ) {
    printf("== [RUNNING IN DEBUG MODE]==\n\n");
    char test_file_path[] = "/Users/bonobos/are/amazing/test.txt";
    freopen(test_file_path, "r", stdin);
}

Note that I need the full file path because when xcode compiles it goes to some weird random folder and so if you don't want to bother packaging the text resources you're better off using an absolute path.

freopen sends the files contents into stdin.

also because I have that if statement I set up xcode to have the argument of debug. this beats doing it with comments and forgetting to uncomment when you compile for realsies.

to set up the xcode argument, go to:

Product > Scheme > edit scheme Edit Scheme

Then set up the argument 'debug' in the run scheme:

add argument debug

Upvotes: 4

user2067021
user2067021

Reputation: 4529

You can pipe data into the executable of an Xcode project for debugging.

See my answer to a similar Stackoverflow question here.

(copied for convenience):

In Xcode 4.5.1:

  1. Open the scheme editor (Product menu -> Edit Scheme...)
  2. Select the Run Debug scheme
  3. In the 'Info' tab panel, change the 'Launch' radio button selection from 'Automatically' to 'Wait for MyApp.app to launch'
  4. Close the scheme editor
  5. Press the Run button to build and run your target. (Xcode's status window in the toolbar will show 'Waiting for MyApp to launch')
  6. Launch Terminal and cd to your built application's folder. (It will be something like /Users/user/Library/Developer/Xcode/DerivedData/MyApp-dmzfrqdevydjuqbexdivolfeujsj/Build/Products/Debug/)
  7. Launch your app piping in whatever you want into standard input:

    echo mydata | ./MyApp.app/Contents/MacOs/MyApp

  8. Switch back to Xcode and the debugger will have detected your application's launch and attached to it.

Upvotes: 4

Related Questions