Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Debug Swift debugging with LLDB

CJ
August 17, 2022

Debug Swift debugging with LLDB

CJ

August 17, 2022
Tweet

More Decks by CJ

Other Decks in Programming

Transcript

  1. • Debug info in object file: For the debugger to

    map the address in the executable to the source code and line number
  2. DWARF • Debugging With Attribute Record Formats • A debugging

    file format used by many compilers and debuggers to support source-level debugging
  3. DWARF & dSYM • DWARF ◦ Keep debug symbols in

    the executable. • DWARF with dSYM File ◦ dSYM: debug SYMbol ◦ Strips debug symbols from the executable, and places them into a separate dSYM file. ◦ Reduce binary size, but slower
  4. Issues: Cannot debug framework source code • So how to

    debug framework? ◦ sample: TerminalInterface
  5. (lldb) image list • `image` is an abbreviation for `target

    modules` • List all the modules currently loaded ◦ executable ◦ dependent shared library • Could help to verify that dSYM is actually found • The UI framework is called TerminalInterface, and LLDB did find the framework and debug info.
  6. (lldb) image lookup -va {memory-address} • Look up information within

    executable and dependent shared library images.
  7. • The source file is pointed to build server, not

    local machine (lldb) image lookup -va {memory-address}
  8. Redirect source map • Since the source file is pointed

    to build server, not local machine • We can redirect the source map • (lldb) settings set target.source-map prefix new • (lldb) settings show target.source-map • Re-run the project, then the source code could be inspected ✅
  9. Redirect source map - LLDB Init File • Handy tips:

    Define per-project LLDB init file for commands
  10. Redirect source map - LLDB Init File • Handy tips:

    Define per-project LLDB init file for commands
  11. (lldb) v • but v works • Xcode variable view

    • = (lldb) frame variable • = (lldb) v
  12. If you are interested… • Type Metadata ◦ The Swift

    runtime keeps a metadata record for every type used in a program, including every instantiation of generic types. ◦ These metadata records can be used by reflection and debugger tools to discover information about types. • Reflection ◦ Reflection in Swift allows us to inspect and manipulate arbitrary values at runtime. • https://github.com/apple/swift/blob/main/docs/ABI/TypeMetadata.rst
  13. What if… cannot find dSYM? • Xcode Settings - Generate

    Debug Symbols ◦ Default: Yes for both Debug and Release
  14. What if… symbol not found? • Xcode Settings - Debug

    Information Level ◦ Toggles the amount of debug information emitted when debug symbols are enabled. ◦ Option - Compiler default ▪ Generate source level debug information ◦ Option - Generate line number tables only ▪ Allows to obtain stack traces with function names, file names and line numbers ▪ No other data (e.g. description of local variables, or function parameters).