10

Bottom-line question: Is it possible to have Xcode wait for an application to launch, then launch the application from outside of Xcode, then have Xcode stop at a breakpoint?

--detailed description below--

I would like to debug (step through line-by-line) a simple C program that requires text to be piped in to stdin using Xcode 4.6.3 on 10.8.5.

In order to do-so I need to launch the program like this $ cat in.txt | ./mysimpleprogram > out.txt and have the Xcode debugger stop at a breakpoint.

I followed the instructions that I found in this answer https://stackoverflow.com/a/13006633/2779792 but Xcode does not stop at the breakpoints.

To reproduce the issue

  1. Open Xcode 4.6.3

  2. In the "Welcome to Xcode" screen choose "Create a new Xcode project"

  3. In the "Choose a template for your new project" screen choose Mac OS X -> Application -> Command Line Tool then click Next

  4. In the "Choose options for your new project:" screen Product Name: HelloWorld, Type: C -> Next, Use Automatic Reference Counting = False

  5. Choose a location for the project.

Note: In Xcode preferences -> Locations -> Derived data, I have selected "Relative"

  1. Go to Product menu -> Scheme -> Edit Scheme

  2. On the left side of the Scheme Editing window, selected by default is "Run HelloWorld Debug". On the right side of the Scheme Editing window I changed the Launch Automatically radio button to "Wait for HelloWorld to launch". Debugger = LLDB, Debug Process As = me

  3. Set one or more breakpoints in the code.

  4. Select Product -> Run (or click the Play button). Now the main Xcode window says "Waiting for HelloWorld to launch".

  5. Open a terminal window and navigate to the HelloWorld executable, in my case .../HelloWorld/DerivedData/HelloWorld/Build/Products/Debug/

  6. run the program with the command ./HelloWord

The program runs and prints Hello, World! as expected, Xcode says "Finished running HelloWorld : HelloWorld". It did not stop at any of the breakpoints.

Desired behavior, Xcode should stop at the breakpoints and allow me to step through the code.

Thank you for any help or hints.

Community
  • 1
  • 1
Matthew James Briggs
  • 2,085
  • 2
  • 28
  • 58
  • Have you tried `Debugger->Attach to Process`? – BergQuester Sep 30 '13 at 00:17
  • Per your suggestion I tried a few variations of opening a terminal and selecting Product -> Attach to Process -> Terminal. Still it does not stop at the breakpoints. Is "Terminal" the correct process to attach to? Of course HelloWorld is not an available process in the list, and HelloWorld would only exist in the list for a microsecond once it's started. – Matthew James Briggs Sep 30 '13 at 01:06
  • See my recent answer to a related question 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:31

1 Answers1

13

In the time it takes the debugger to notice and attach to your process execution of the process has proceeded past the breakpoints. To get deterministic behavior from the debugger in this setup you need to pause execution of your process prior to the first breakpoint and wait for the debugger to attach. A simple way to do this is by adding code to send the STOP signal:

raise(SIGSTOP);

Execution will stop at this point and the debugger will automatically continue when it attaches.

Matt Stevens
  • 13,093
  • 3
  • 33
  • 27