Search code examples
swiftlldb

Write content of variable to file with LLDB


I am debugging some JSON in Swift and want to know if there is a way to write the content of a variable to file. Consider a trivial example:

let jsonString = "{\"name\": \"John\", \"age\": 21}"

How can I write jsonString to a local file for more detailed examination when pausing for debug with LLDB?


Solution

  • You could call write(toFile:atomically:encoding:). For example

    (lldb) call jsonString.write(toFile: "path/to/file", atomically: true, encoding: .utf8)
    

    You could make this more convenient using lldb's regex aliases. For example if you wanted a dump command, to run like this:

    (lldb) dump jsonString path/to/file
    

    Put the following into your ~/.lldbinit file to use this dump alias:

    command regex dump 's/(.+) (.+)/call %1.write(toFile: "%2", atomically: true, encoding: .utf8)/'