Slide 1

Slide 1 text

Debug Swift debugging with LLDB JJ Lin

Slide 2

Slide 2 text

Issues: Cannot debug framework source code

Slide 3

Slide 3 text

Issues: Cannot debug framework source code

Slide 4

Slide 4 text

● When the compiler compiles a function, it generates machine code.

Slide 5

Slide 5 text

● Debug info in object file: For the debugger to map the address in the executable to the source code and line number

Slide 6

Slide 6 text

● For archiving distribution, debug info will be put into .dSYM bundles by dsymutil.

Slide 7

Slide 7 text

DWARF & dSYM ● For debug at run-time ● Crash log symbolication

Slide 8

Slide 8 text

DWARF ● Debugging With Attribute Record Formats ● A debugging file format used by many compilers and debuggers to support source-level debugging

Slide 9

Slide 9 text

$ dwarfdump Framework1.framework.dSYM/Contents/Resources/DWARF/Framework1

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Issues: Cannot debug framework source code ● So how to debug framework? ○ sample: TerminalInterface

Slide 12

Slide 12 text

(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.

Slide 13

Slide 13 text

What if… dSYM is not loaded? ● (lldb) add-dsym {dSYM}

Slide 14

Slide 14 text

(lldb) image lookup -va {memory-address} ● Look up information within executable and dependent shared library images.

Slide 15

Slide 15 text

● The source file is pointed to build server, not local machine (lldb) image lookup -va {memory-address}

Slide 16

Slide 16 text

(lldb) image lookup -r -n {symbol name}

Slide 17

Slide 17 text

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 ✅

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

● (lldb) po //failed Issues: Cannot debug framework source code

Slide 21

Slide 21 text

(lldb) v ● but v works ● Xcode variable view ● = (lldb) frame variable ● = (lldb) v

Slide 22

Slide 22 text

LLDB is debugger, and also a compiler

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

● Solution: Archive the framework again, with the missing module

Slide 27

Slide 27 text

Some Tips

Slide 28

Slide 28 text

What if… cannot find dSYM? ● Xcode Settings - Generate Debug Symbols ○ Default: Yes for both Debug and Release

Slide 29

Slide 29 text

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).

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Reference ● https://developer.apple.com/videos/play/wwdc2022/110370/ ● https://developer.apple.com/videos/play/wwdc2021/10211 ● https://developer.apple.com/documentation/xcode/build-settings-reference ● https://clang.llvm.org/docs/UsersManual.html#controlling-size-of-debug-information ● https://github.com/apple/swift/blob/main/docs/ABI/TypeMetadata.rst

Slide 34

Slide 34 text

Thank you