The Console & Basic Debugging

Xcode: The IDE Itself

Chapter 4 ยท The Console & Basic Debugging

Chapter 3 covered designing an interface. This chapter covers finding out why it, or any other code, does what it does. Xcode's debugging tools come down to three things you use together: the debug console, breakpoints, and the debugger underneath them, LLDB. The facts about LLDB are checked against a reference article. The workflow details come from tutorial sites that I read but could not confirm against Apple's own pages, and I have marked those.

LLDB: The Debugger Underneath

LLDB is a debugger created by Chris Lattner as part of the LLVM project, built as a set of reusable components that use existing LLVM libraries. It became the default debugger for Xcode 5 and later, replacing GDB. It handles C, Objective-C and C++, and the Swift community maintains a specialized version. It supports backtracing, breakpoints, disassembly, expression evaluation and remote debugging. Its license moved from the University of Illinois/NCSA license to Apache 2.0 with LLVM exceptions starting with version 9.0.0.

A Skill That Travels
The same reference says LLDB runs on macOS, Linux, FreeBSD, NetBSD and Windows, and is used in other tools including Android Studio and Visual Studio Code. This partly answers Chapter 1's question about what transfers: LLDB commands are a skill that exists outside Xcode. I have not checked how closely Xcode's console matches those other environments, so treat that as likely, not confirmed.

Breakpoints

A breakpoint pauses your running program at a chosen point so you can look around. The tutorials I read describe several kinds.

KindStops
Line breakpointWhen execution reaches that line of your code
ConditionalOnly if a condition you set is true, or after a number of hits
SymbolicAt a specific function or method, by name
ExceptionWhen an exception is thrown

The tutorials say a symbolic breakpoint is created through the Debug menu (Debug → Breakpoints → Create Symbolic), and that you can right-click a breakpoint and choose Edit to add a condition. I could not confirm the menu wording against Apple's documentation, so check your own version. Chapter 1's advice about confirming your Xcode version applies here.

The Console: Commands to Try

When execution is paused, the debug console accepts LLDB commands. The tutorials list these.

CommandWhat it does
pPrints a variable's value
poPrints the description of an object
eShort for expression; evaluates an expression
vAn alias for frame variable; reads what is currently in memory and prints an LLDB-formatted summary
btPrints a backtrace, the chain of calls that led here
c, n, s, finishContinue, step over, step into, step out
An Illustration, Not Real Output
The session below shows the shape of a debugging conversation. I wrote it to illustrate the commands, and I did not run it against a project, so the values are invented. Replace total with a variable in your own code.
(lldb) p total (lldb) po tasks (lldb) bt (lldb) n (lldb) c

A Debugging Loop

  1. Form a guess: which line first goes wrong?
  2. Place a breakpoint just before it, with a condition if the loop runs many times.
  3. Run the app until it pauses, then inspect the values with p, po or v.
  4. Step over with n or into a call with s, and check again.
  5. If the crash is an exception, add an exception breakpoint so execution stops where it is thrown, and use bt to see how you got there.
Where Performance Debugging Fits
Breakpoints answer "what is happening in this code." They do not answer "why is this slow" or "where is memory going." Those need Instruments, the subject of Chapter 5, and iOS Development — Architecture & Data 7 already introduced it at the workflow level.

Hands-On Exercises

Exercise 1

Explain in your own words what LLDB is, who created it, and what it replaced in Xcode. Then say what evidence in this chapter suggests LLDB skills are not limited to Xcode.

๐Ÿ“„ View solution
Exercise 2

A loop runs 1,000 times and a value goes wrong somewhere in it. Choose the right kind of breakpoint, say why a plain line breakpoint would be a poor choice, and list the console commands you would use once it stops.

๐Ÿ“„ View solution
Exercise 3

Your app crashes with a thrown exception and the console shows only a message. Describe how you would find where it was thrown, and name two details in this chapter you could not verify.

๐Ÿ“„ View solution

Chapter 4 Quick Reference

  • LLDB: created by Chris Lattner, part of LLVM; default in Xcode 5 and later, replacing GDB
  • Runs on: macOS, Linux, FreeBSD, NetBSD and Windows; also used in Android Studio and Visual Studio Code
  • Breakpoints: line, conditional, symbolic, exception
  • Commands: p, po, e, v, bt, c, n, s, finish
  • Unverified: exact Xcode menu wording, the console layout, and how closely Xcode's console matches other LLDB environments