Skip to content

Instantly share code, notes, and snippets.

@Bunn
Last active October 25, 2021 18:53
Show Gist options
  • Save Bunn/1f7c04b62851152e2aece93e18c01245 to your computer and use it in GitHub Desktop.
Save Bunn/1f7c04b62851152e2aece93e18c01245 to your computer and use it in GitHub Desktop.
import lldb
def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand('command script add -f SwiftPrinter.printer pjson')
def printer(debugger, command, result, internal_dict):
debugger.HandleCommand('p print(String(data: try! JSONSerialization.data(withJSONObject:' + command + ', options: .prettyPrinted), encoding: .utf8 )!)')
@Bunn
Copy link
Author

Bunn commented Jun 25, 2021

To use the script you'll need to import it first:
command script import $PATH_TO_YOUR_PYTHON_FILE

For example:

(lldb) pjson a
error: 'pjson' is not a valid command.
(lldb) command script import ~/lldb/SwiftPrinter.py
(lldb) pjson a
{
  "bar" : 123,
  "foo" : "banana",
  "wow" : [
    1,
    2,
    3,
    4
  ],
  "bleh" : {
    "bla" : 333
  }
}

Or set it up in your .lldbinit file

@eneko
Copy link

eneko commented Jun 25, 2021

Nice! Never done this before, where is/should be .lldbinit located? In the home directory?

@Bunn
Copy link
Author

Bunn commented Jun 25, 2021

Nice! Never done this before, where is/should be .lldbinit located? In the home directory?

Yes, @eneko home directory, for example:

cat ~/.lldbinit 
command script import ~/lldb/SwiftPrinter.py

@eneko
Copy link

eneko commented Jun 25, 2021

Great, thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment