Mac Xcode - how to debug a non-Xcode program?

daihard

macrumors 6502a
Original poster
Feb 19, 2008
971
7
Seattle, WA
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

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
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

macrumors 6502a
Original poster
Feb 19, 2008
971
7
Seattle, WA
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

macrumors regular
Jun 4, 2009
159
26
Germany
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

macrumors 6502a
Original poster
Feb 19, 2008
971
7
Seattle, WA
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.
Code:
$ 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

macrumors 6502a
Sep 17, 2007
707
2
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

Moderator emeritus
Jun 15, 2000
7,958
5
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

macrumors newbie
Jun 11, 2010
3
0
Steps to use Xcode on non-Xcode program

Requirements: compile program with "gcc -g"
  1. Open Xcode
  2. Create an empty project
  3. Right click "Executables" under "Groups and Files" and select Add->New Custom Executable
  4. Set the path for your executable and click "Finish"
  5. 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.
  6. Right click your <Project Name> under "Groups and Files" and select Add->Existing Files
  7. 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
 

mannyglover

macrumors newbie
Jun 20, 2018
1
0
I use CMake, and if you type "cmake -G Xcode <source directory>", let's say in a directory called "build", you can just point Xcode to this build directory, and everything works.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.