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.
Breakpoints
A breakpoint pauses your running program at a chosen point so you can look around. The tutorials I read describe several kinds.
| Kind | Stops |
|---|---|
| Line breakpoint | When execution reaches that line of your code |
| Conditional | Only if a condition you set is true, or after a number of hits |
| Symbolic | At a specific function or method, by name |
| Exception | When 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.
| Command | What it does |
|---|---|
p | Prints a variable's value |
po | Prints the description of an object |
e | Short for expression; evaluates an expression |
v | An alias for frame variable; reads what is currently in memory and prints an LLDB-formatted summary |
bt | Prints a backtrace, the chain of calls that led here |
c, n, s, finish | Continue, step over, step into, step out |
total with a variable in your own code.
A Debugging Loop
- Form a guess: which line first goes wrong?
- Place a breakpoint just before it, with a condition if the loop runs many times.
- Run the app until it pauses, then inspect the values with
p,poorv. - Step over with
nor into a call withs, and check again. - If the crash is an exception, add an exception breakpoint so execution stops where it is thrown, and use
btto see how you got there.
Hands-On Exercises
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 solutionA 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 solutionYour 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 solutionChapter 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