Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode debugging breakpoints when app is launched from outside of Xcode

Tags:

c

xcode

xcode4

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.

like image 841
Matthew James Briggs Avatar asked Sep 29 '13 22:09

Matthew James Briggs


Video Answer


1 Answers

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.

like image 120
Matt Stevens Avatar answered Sep 27 '22 19:09

Matt Stevens