Swift.org Blog Adrian Prantl Sep 28, 2023

What's New in Swift 5.9 Debugging

Article Summary

Adrian Prantl, Augusto Noronha, and Dave Lee from Apple's Debugger Compiler Integration team just made debugging Swift apps significantly faster. If you're still using the old `p` and `po` commands the same way, you're leaving performance on the table.

Swift 5.9 shipped with three major debugging improvements that change how developers inspect variables and troubleshoot code in LLDB. The updates focus on speed, generic type support, and more accurate variable scoping during debug sessions.

Key Takeaways

Critical Insight

Swift 5.9's debugging improvements make variable inspection faster and more accurate by replacing heavyweight expression evaluation with lightweight alternatives and adding generic type support.

The article reveals a simple ~/.lldbinit configuration that lets you customize the new behavior if you actually need those persistent result variables.

About This Article

Problem

Apple's Debugger Compiler Integration team found that LLDB's `p` and `po` commands were creating persistent result variables like `$R0`. These variables kept objects in memory and caused unexpected side effects while the program ran.

Solution

The team changed `p` and `po` to use the new `dwim-print` command instead. This command uses `frame variable` rather than the full expression evaluator, making it lighter weight. For `po`, the command now automatically evaluates `unsafeBitCast()` so developers can print Swift objects directly from raw memory addresses.

Impact

Swift 5.9 removed the overhead from persistent result variables. Developers can now inspect objects straight from raw addresses. A raw hex address like `0x00006000025c43d0` becomes a readable object description like `<MyApp.AppDelegate: 0x6000025c43d0>`.