PDA

View Full Version : Xcode - how to debug a non-Xcode program?




daihard
Oct 21, 2009, 11:13 PM
Hi.

I've been looking all over the place, to no avail. Hopefully I need to look no further than here...

I have a Mac OS X console application built with GCC that came with Snow Leopard. I specified "-g" as a compiler option to include debug symbols. The executable runs fine.

Now, how do I debug it using Xcode? The Apple documentation says I can invoke Xcode, go to Run --> Attach to Process in order to attach the Xcode debugger to a running process. However, everything under "Attach to Process" is greyed out, including the "Attach to Process ID" option. I did run "top" and made sure my executable was listed there with a valid PID.

What am I doing wrong? Any help would be much appreciated.



lee1210
Oct 21, 2009, 11:22 PM
You could just build an XCode project with your source, then the debugger is integrated.

Otherwise, you can just invoke the same debugger that XCode uses, GDB, from terminal. Just run:
gdb ./myprog

use "set args" to set the command line arguments, then "run" to start your program. Google for a gdb cheat sheet for other commands.

-Lee

daihard
Oct 21, 2009, 11:30 PM
You could just build an XCode project with your source, then the debugger is integrated.

Otherwise, you can just invoke the same debugger that XCode uses, GDB, from terminal. Just run:
gdb ./myprog

use "set args" to set the command line arguments, then "run" to start your program. Google for a gdb cheat sheet for other commands.


Thanks for the quick reply. I do know how to do command-line debugging using gdb. I'd just like to be able to use a graphical debugger because it makes debugging a lot easier (IMO).

I could try and build my application using Xcode, but I'd like to avoid that unless absolutely necessary. It is an application I develop at work. It consists of several shared libraries and an executable that uses those libraries. I don't want to have to rebuild every single one of those components using Xcode.

On Windows (ugh!), you can run any executable with Visual Studio by running "devenv /debugexe <executable_name>.exe" from the command line. That will bring up the debugger. I am hoping that something similar can be done with Xcode. After all, Mac should be more user-friendly than Windows. :)

cqexbesd
Oct 22, 2009, 10:50 AM
However, everything under "Attach to Process" is greyed out, including the "Attach to Process ID" option. I did run "top" and made sure my executable was listed there with a valid PID.

XCode wants a valid project open so it can find the source to the process you are debugging. Could this be your issue?

HTH,

Andrew

daihard
Oct 22, 2009, 01:27 PM
XCode wants a valid project open so it can find the source to the process you are debugging. Could this be your issue?

Mind showing me how to create a valid project from a debug executable? Like the subject line says, I would like to debug an application built and managed outside Xcode.

To make it very simple, let's say I build a test application by doing the following.
$ gcc -g -o mytest mytest.c
This will create an executable, mytest, with debugging symbols embedded. How do I open this executable with Xcode so I can debug it?

On Windows, Visual Studio lets you open such an executable. As soon as the executable is loaded, VS will prompt you to save a new project file (mytest.sln, for instance). From then on, I can simply open the newly created VS project to run/debug the executable. Can something similar be done with Xcode?

Thanks!

mslide
Oct 22, 2009, 01:51 PM
This will create an executable, mytest, with debugging symbols embedded. How do I open this executable with Xcode so I can debug it?

I'm certainly no XCode expert, as I rarely use it, but I've tried several times in the past to do this. I also don't use xcode to build my work stuff (and building it with xcode is not an option) and I would love to easily be able to use xcode to debug already built executables.

What I've tried in the past was creating an empty project (File -> New -> Project -> other ->Empty Project). I then add a custom executable to it (Project -> New Custom Executable). From there I've been able to run it in their debugger. I thought I got source level debugging to work in the past, but I just tried and couldn't figure it out. I guess I could add the sources to my project but I'd rather not go through all that considering all I want to do with xcode is use it as a GUI to gdb.

I gave up and now I just use gdb.

kainjow
Oct 22, 2009, 02:05 PM
I was just about to post what mslide said. It worked for me at least. Just set the path to your executable and run. I tested with a public beta game and obviously only got assembly, and tested with a debug build from a personal project and got source code when I paused.

jzwolak
Sep 28, 2010, 05:37 PM
Requirements: compile program with "gcc -g"

Open Xcode
Create an empty project
Right click "Executables" under "Groups and Files" and select Add->New Custom Executable
Set the path for your executable and click "Finish"
Optional: in the "Executable Info" window (accessible from right click then "Get Info" on the executable in "Groups and Files"), set the working directory to the path to your executable, this was necessary for me, but may not be for your project.
Right click your <Project Name> under "Groups and Files" and select Add->Existing Files
Select your source file.


After that you're all set. You probably want to click your source file in "Groups and Files" set a break point and start the debugger with break points on. You'll see all the graphical debugger goodness!!

NOTE:
This won't copy your executable or source code to the project directory. If you save this project, close Xcode, modify your source code, recompile, re-open your project in Xcode - all your new code and new executable will be right there ready for your debugging pleasure.

Cheers,
Jason

mstauff
Feb 11, 2011, 03:32 PM
Thanks jzwolak! Works like a charm.

-M