3

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?

bot47
  • 1,458
  • 3
  • 18
  • 36
  • possible duplicate of [How can I pipe stdin from a file to the executable in Xcode 4?](http://stackoverflow.com/questions/7629886/how-can-i-pipe-stdin-from-a-file-to-the-executable-in-xcode-4) – Frodo Baggins Dec 02 '14 at 15:11
  • I have posted a new answer for this at https://stackoverflow.com/questions/7629886/how-can-i-pipe-stdin-from-a-file-to-the-executable-in-xcode-4/49669712#49669712 – Konchog Apr 05 '18 at 13:28

3 Answers3

4

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.

Community
  • 1
  • 1
user2067021
  • 4,399
  • 37
  • 44
  • My problem with this is that you have to open a Terminal session every time you run. You may as well just copy and paste the contents of `mydata` to the console area every time (which is what I was trying to avoid when I went looking for this post). – Michael Dorst Oct 01 '14 at 16:18
4

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

ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80
1

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.

Nestor
  • 2,753
  • 2
  • 29
  • 34
  • You can. user2067021's answer is correct. You DO need to set a breakpoint in the program in Xcode before clicking the run button and then launching your app. – msc Jul 17 '19 at 00:29
  • Ah, but could you seven years ago? That's the question everyone's desperate to know the answer to. – Nestor Jul 17 '19 at 12:59