swiftdebugginglldb

How to print swift object in LLDB for given address?


In ObjC++, I just use po $x0 to print the first argument of a function. If it's an object, it will display it in detail.

However, in Swift when I do this for an swift object, I got an error:

po $x0
error: <EXPR>:6:1: cannot find '$x0' in scope
$x0
^~~

How can I print the detail of it?


Solution

  • Assuming you are trying to print the details of a class, you can do:

    p unsafeBitCast(UnsafeRawPointer(bitPattern: <address here>), to: AnyObject.self)
    

    Replace <address here> with your desired address.

    For reading things in registers, you can use register read as said by Jim Ingham. It is still possible to use the $-prefixed variables if you use expr -l objc. Example:

    (lldb) expr -l objc -- $arg1
    (unsigned long) $0 = 105553181632000
    (lldb) p unsafeBitCast(UnsafeRawPointer(bitPattern: 105553181632000), to: AnyObject.self)
    (Foo.SomeClass) 0x0000600003e56600 (property1 = "Property", property2 = "Property", property3 = "Property")
    

    I would also suggest using the dump global function, which pretty-prints the properties of the object.

    (lldb) p dump(unsafeBitCast(UnsafeRawPointer(bitPattern: 105553181632000), to: AnyObject.self))
    ▿ Foo.SomeClass #0
      - property1: "Property"
      - property2: "Property"
      - property3: "Property"
    (Foo.SomeClass) 0x0000600003e56600 (property1 = "Property", property2 = "Property", property3 = "Property")
    

    For structs, you will need to know the type of the struct in order to unsafeBitCast, since there are no metadata about its type stored in memory. Bigs structs also can get moved onto the heap, which further complicates things.