Debugging with breakpoints in Xcode

Debugging is inherently linked to development, regardless of the programming language or the platform for which the software is implemented. A good knowledge of the debugging techniques and of the development environment debugging features makes the process of finding issues and tracing the program flow much more efficient.

Xcode integrates several advanced debugging features relying on the powerful LLDB debugger. One of these features is the advanced breakpoint, probably the debugging tool that comes first to the mind when trying to analyze a program at runtime.

Breakpoints are interruptions in the execution of a program allowing to inspect the program state at that specific points. As explained later, breakpoints can pause the program or simply perform predefined actions without actually stopping the execution.

Breakpoint navigator

The easiest way to create a breakpoint is to click on the code editor gutter and the blue breakpoint symbol appears. When the program is run, the execution stops when it reaches the line of code related to the breakpoint. To disable a breakpoint, click on its symbol; its color changes to light blue. To delete a breakpoint, drag it out of the gutter. These actions are also available by right-clicking on the breakpoint symbol.

The list of all the breakpoints in the project is visible in the breakpoint navigator:

xcode breakpoint navigator

Breakpoint scope

Breakpoints are persistent. They are vailable each time the project is open in Xcode until they are manually deleted.

A breakpoint can be defined at one of the three scope levels:

  • project level: the default scope for a new breakpoint. It is available in the current project and for the current user / developer.
  • shared level: the breakpoint is shared between all the developers working on the same project
  • user level: the breakpoint is available in all the projects of the same developer.

To change the scope, right click on the breakpoint in the navigator and click on Share breakpoint or choose one of the options from the Move Breakpoint To submenu.

Step by step debugging

When the execution of the program stops on a breakpoint, it can be resumed using the buttons available on the debug toolbar which should be displayed at the bottom of the Xcode window when the program is running:

xcode debug toolbar

If the debugger console is not automatically showing at runtime, the bottom pane is probably hidden; to display it, you should click on the corresponding option at the top right of the Xcode toolbar:

show the xcode debugger console

The debug toolbar buttons perform the same actions as the commands in the Debug menu:

  • Continue program execution: resumes the program execution, which will advance until the next breakpoint is reached or until the current action finishes its execution. The same can be performed by typing continue or c in the LLDB console and pressing the Enter key.
  • Step over: executes the current instruction (line of code) and passes to the next. The same can be performed by typing next or n in the LLDB console and pressing the Enter key.
  • Step into: enters inside the method that is invoked by the current line of code. The same can be performed by typing step or s in the LLDB console and pressing the Enter key.
  • Step out: exits the current method after executing it until its last line of code. The same can be performed by typing finish in the LLDB console and pressing the Enter key.

execute debbuger commands in the console

Breakpoint types

The breakpoints set by clicking on the editor pane gutter are the most basic ones; they simply stop the program just before the line of code they are set on is executed. They are named method breakpoints and appear in the breakpoint navigator with the M symbol.

Other types of breakpoints can be created from the breakpoint navigator, by clicking on the + button at the bottom. The most common are:

  • Exception breakpoint: stops the program execution when an exception occurs. The program can be interrupted before the exception is raised (Break = On throw) or after the exception is raised (Break = On catch). These breakpoints are identified by the Ex symbol:

exception breakpoint

  • Symbolic breakpoint: stops the program execution when a specific message is send (a method is called). The execution cursor is set on the first line of the invoked method.

symbolic breakpoint

Conditions and actions

Breakpoints can be triggered depending on specific runtime conditions, by evaluating boolean expression on property values or method return values .

The condition can be specified by right-clicking on a breakpoint and choosing the Edit Breakpoint command.

When the breakpoint is triggered, one or multiple custom actions can be performed. For instance, a message can be written and the value of a property can printed in the debugger console. If the Automatically continue after evaluating the actions checkbox is selected, the breakpoint executes the actions without interrupting the program:

breakpoint conditions and actions

Conclusion

I hope this article helps to better understand how breakpoints can be used in Xcode and how to configure their different options to get the most out of them in any situation.

In the next article I will explain how to interact with the runtime while the program is interrupted by a breakpoint.

 

Catalin Rosioru