Debugging in LLDB with command regex

Overview

Add the following to ~/.lldbinit and examine Swift objects in LLDB with “mp myobject” for both iOS and macOS, including those in NSView for macOS.

command regex mp 's/(.+)/expr Swift.print(%1)/'

Read on for more info

Goodbye po

I wanted to share a quick tip I discovered that allowed me to more easily examine objects.

I had gotten used to examining objects in LLDB using the command “po”.   This worked fine in the past when I was dealing with Objective-C, but not so well now with Swift.

Here are the options I have used:

  • po obj   This is an abbreviation for “expression -O –“.  Per the documentation: Displays any returned value  with formatting controlled by the type’s author.
  • p obj   This is an abbreviation for “expression –“.  Displays any returned value with LLDB’s default formatting.
  • e print(obj)   Here “e” is an abbreviation for “expression” and “print” is a built-in Swift method.

Here is an example of displaying a Property object named “property” with each of these options.

swift_object_inspection

Using “e print(property)” provides the most concise output.  It is outputting the result obtained from the computed property “description” that I implemented for the Property structure.   The command “po property” provides the output of “description”, but also includes an alternate display of the information.   The third option of “p property” is the least useful way to display the object.

Hello command regex

I prefer using “e print(object)” since I can get a concise representation of the object, particularly when used in combination with the computed property “description”.   It is a little tedious to type all this though.   I found a way to shorten this considerably in the answer to the post I can’t get this simple LLDB alias to work.  Using this answer as an inspiration, I came up with:

command regex mp 's/(.+)/expr Swift.print(%1)/'

Here “mp” is an alias that represents “my print”.

Why Swift.print?  I wanted to be able to use this alias with both iOS and macOS.  With NSView (macOS), the default print method is used with a physical printer device!  By specifying the Swift module, we can work around this.

Other References of Interest

You can find documentation on LLDB on their homepage.

I ran across a nice summary of what you can do with LLDB in the article Debugging Swift code with LLDB.

On a side note

I ran across an interesting tip when doing some research.  I haven’t had an opportunity to try this yet, so feel free to comment with your experiences here.

The article Xcode: One Weird Debugging Trick That Will Save Your Life describes how you can take the mystery out of objc_exception_throw with “po $arg1” to give a human-readable version of the error. The author mentions that you can add this to the Exception Breakpoint so it’s automatic.

In the comments someone also mentions the following additional commands:

  • “po $eax” (for 32 bit simulator)
  • “po $rax” (64 bit simulator).
  • “po $r0” (if you’re debugging on a device).

Is this officially documented somewhere?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s