Monday, November 23, 2009

Breakpoints not working in Xcode?

I decided to post this because I thought my breakpoints weren't working in Xcode. When I searched for the answer, I read several posts about people's breakpoints appearing not to work and what steps they took to solve the problem. But I did not find what I'm about to write. So here goes.

When I say a breakpoint doesn't work, I mean that execution doesn't pause there as expected. Be assured that Xcode is not to blame! In my case, it was operator malfunction. I was concentrating intently on so many complicated ideas at one time that I was pushing the wrong button and getting what I thought were unexpected results. Here's what happened.

I'm using version 3.1.3 of Xcode. As of this version and all prior ones of which I'm aware, there are two ways to start your program: Run or Debug. There's also a button called Go and another one called Build and Go. Either of these Go buttons will do either Run or Debug, depending on what you did last. I was pushing Go and getting Run when I thought I should be getting Debug.

The difference between Run and Debug is that Run just runs your program and Debug runs your program through gdb, the GNU Debugger. If you run your program without the help of the debugger, you cannot expect breakpoints to work!

How can you tell whether you're doing Run or Debug? If you look in the Xcode console, you'll see gdb's startup messages scroll by when your program starts in Debug. You will not see these messages when your program starts in Run. Also, your program will take a bit longer to load when running with the help of gdb. If you click and hold over the little downward-pointing triangle on the lower right corner of the Build and Go button in the toolbar, you'll get a menu where the first item will either say, "Build and Go (Run)" or "Build and Go (Debug)," depending on which you did last. If you go up into the Run menu in the menu bar, the first item will be "Go (Run)" or "Go (Debug)," the second will be "Run" and the third will be "Debug." There are other ways (hot keys, etc.) you can control which way your program is run.

How do you see the Xcode console? Look at the very top left of the Xcode window. Assuming you did not modify your toolbar, there should be two buttons. If you hover over the left one, you'll get a tooltip that reads "Project." This is where you edit source code. If you hover over the right one, the tooltip should read "Debug." Click on that and you should see the Xcode console at the bottom of the window.

I came to Xcode from another IDE that automatically ran the program through the debugger if you had any breakpoints. Xcode is more flexible in that you can define breakpoints and still run your program normally (without the debugger) if you so desire. You just have to pay attention to which way you're running it (Run or Debug). I hope this helps someone who is wondering why their breakpoints don't work.