Debugging Essentials

Xcode lets you analyze your code line by line to view your program’s state at a particular stage of execution. This process is called debugging. To debug a program, you run it under the control of a debugger, which lets you pause programs and examine their state.

Debugging Environments

You can perform debugging operations in several ways:

Debugging Workflow

When you debug a product, Xcode opens a debugging session with a debugger and your product’s binary. Before you can debug a program, however, it should contain as much debugging information as possible, so that Xcode can provide you with accurate and useful information during your debugging. See Building for Debugging for information about how to build a program to debug.

You may also debug a running process (not launched by Xcode). This operation is known as attaching to a process. You can attach to a program running under Xcode or to any process for which you have a process ID. To attach to a running program use Run > Attach to Process.

This menu lists currently running programs launched from Xcode, identified by the program name and the name of the corresponding Xcode project. (This menu also lists other programs running on the computer.) You can use the menu’s Process ID option to attach to any process using its process ID, which you obtain using UNIX utilities such as top.

Building a Product with Debugging Symbols

Before you can take advantage of the source-level debugger, the compiler must collect debugging information. To generate debugging symbols for a product, turn on the Generate Debug Symbols (see GCC_GENERATE_DEBUGGING_SYMBOLS (Generate Debug Symbols)) build setting and build the product. This setting is turned on by default in the Debug build configuration. If you use this build configuration as the active build configuration when you build your target, the compiler generates the necessary debugging information. For more information about build configurations, see Build Configurations.

To view the build settings that are set in the Debug build configuration:

  1. In the project window, select the target or targets you want to build and bring up the target editor.

  2. Click Build to bring up the Build pane.

  3. Choose Debug (or Development) from the Configuration pop-up menu at the top of the pane. You should see the Generate Debug Symbols setting in the table of build settings. Make sure that this setting is turned on; if it is, a checkmark appears in the Value column for this setting. Otherwise, turn on the setting by clicking the checkbox in the Value column.

For more information about building and running programs, see Xcode Project Management Guide.