Book 13: Getting to Know Your Code

Previous | Next

Debugging a program

Xcode also comes with a debugger that lets you watch your program execute, live. To use it, simply, open your main.c source file and click in the grey “gutter” at the left of the first command of your main() function. In the case of our simple calculator program from chapter 4, that would be the vFinished = false; line. A blue “pointing thing” will show up. This is what’s called a “breakpoint”, or “debug checkpoint” and will cause the debugger to stop before executing that line. To remove a breakpoint, simply drag it out the left of the window. Now choose Run from the Product menu.

Screen Shot 2012 08 15 at 6 33 15 PM

Your application will launch, and then stop at the breakpoint, opening your main.c file with the left column switched to the Debug Navigator. It will mark the current line with a green highlight and a green arrow to its left. You can now point at any variable with your mouse to get a tool tip with its value, and even follow pointers and look into arrays by hovering the mouse over the little triangles next to them.

Xcode code sense tooltip opened a few levels deep

If the debugger doesn’t stop at your breakpoint, make sure you haven’t turned off breakpoints by accident using the Breakpoints button button at the top of Xcode’s window. (In Xcode 8 and later, that is now an “Activate Breakpoints” icon in the bottom half of the window: Debug bar with Activate Breakpoints icon)

The Debug Navigator in the left (in this screen shot saying GetArgument, main and start) shows your what function called this function, and which one called that, and so on. In this example, the debugger is stopped in the GetArgument() function called by the main() function, and I just used that list to select the main() function and look at the variables at the time it called GetArgument in the Debug Area at the bottom of the window:

Screen shot of the Debug Area containing the variables in main() while GetArgument() is running.

If you were to click the little “Continue Execution” icon at the top of the debug area now (it’s marked with a red circle in the screenshot above), your program would just keep running, finish and quit. But if you want to follow your program along as it runs, you can use the “Step Over” (arrow jumping over a box) and “Step Into” buttons (arrow pointing down at a box) to step through your program, line-wise, where “Step Into” will go into any functions you call and let you step through them line-wise, too, while “Step Over” will see a function call as just one command.

Of course you can still use the three little icons to show/hide the console while you are debugging, but be aware that, while the program is paused in the debugger, the console behaves specially, in that it lets you talk directly to the Debugger (aka lldb) that Xcode uses under the hood (just like it uses llvm). But you’ll probably not need to talk to lldb directly anytime soon, so you can safely ignore that, as long as you remember to click “Continue” before you start typing something into the console.

I encourage you to have some fun now: Take the project for one of our more complex sample programs, set a breakpoint in its main() function at the first line, and step through it. There’s no better way to get a feeling for how your computer works than actually watching it do something. Also, don’t forget to follow some pointers using the little triangles in the Code Sense tool tip for a variable. Addresses are usually not shown in decimal, but in hexadecimal, and prefixed with 0x. You can use Apple’s Calculator application in Programmer mode to convert between decimal and hexadecimal.

Navigating around your Code Efficiently

Whenever you have source code open in Xcode, you can click any word with the command key held down to be brought to its definition. I.e. if you’re calling a function and want to know what it does, or what its parameters were again, command-double-click and you’re there. This also works for system commands, and sometimes their headers even contain useful comments. If you do the same on a definition, you will be brought to its declaration instead, which is handy when you need to keep declaration and definition in sync after changing one of them.

A help popup that you get using option-double-click

There’s another neat trick for system commands: Hold down the option key while clicking instead of the command key, and your cursor will turn into a question mark and Xcode will open a small documentation popup reminding you of what this command, function or constant is for. Click the link next to the Reference heading to bring up more information in the Documentation Organizer (available from the Help menu under Documentation and API Refeence). And click the name of the header next to Declared to show the file containing this item.

Sometimes, the header here is not the same as you would include. That is because a header can include other headers. In the screenshot above, printf(), which I told you lived in stdio.h is said to be in libkern.h. This simply means that the Apple developers thought it would be a bit tidier to break out printf into its own header. However, stdio.h is the official spot where it must be available (either directly or indirectly) on every computer that understands C, so you should not rely on it being in libkern.h. They might even move printf back, or rename libkern.h to printfstuff.h or whatever.

After navigating through code like this for a while, you will probably find yourself looking at Apple’s headers a lot, and you’ll likely even remember what commands were in what header. In that case, you can use the “Open Quickly” menu item to quickly jump to a particular header, either by selecting the header file’s name in a #include statement, or by selecting nothing and then typing in the header name.

Another nice feature is the search field in the lower left of the project window, which lets you search for a particular file in the Project Navigator (or also in whatever Navigator you currently have showing in the left of your window). This is handy especially for huge, collaborative projects where you may not remember the exact file name.

What else Xcode can do for You

Xcode also has contextual menus with nice features. For example, you can hide the types of variables in the Debug Area’s variable list, click in the text and show where the current file is in the Project Navigator’s list of files, or show a file in the Finder that you have in your project. You can also tell Xcode to split its window into the main text editor and a second Assistant Editor (just option-click on the second file), and even set that editor up so it shows the header file that goes with the source file you’re currently editing, or even split the editor into more than two editors.

And finally, Xcode has a “Preferences…” menu item where you can choose some more tasteful colours for syntax coloring (if you ask me, comments should stand out, so make them red!), and activate lots of other nice features.

Previous | Next

This entry was posted in C Tutorial. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*