0

I would like to call dump method in the LLDB while using Debug View Hierarchy.
Select element (for instance View controller) -> open Object Inspector -> get the address and execute dump in lldb.
po 0x7fc1fd8301a0 works with object description as expected
e -l swift -O -- dump(0x7fc1fd8301a0) or with `` doesnt work
I have tried t o create and extension for my object:

@objc public class DumpTree: NSObject {
}
public extension DumpTree {
    @objc class func apple(_ value: Any?) {
        dump(value);
    }
}

po [DumpTree apple:0x7fc1fd8301a0] I get the following error:
error: expression failed to parse: error: Error [IRForTarget]: Couldn't resolve the class for an Objective-C static method call
Xcode 14.0.1
Thanks

Mike.R
  • 2,824
  • 2
  • 26
  • 34
  • 1
    `po` works because the expression is a literal, and then lldb does work after the fact to figure out that the literal (`0x7f...`) is really the memory for a swift object, and calls the debugger print routine for it. But the expression `dump(0x7f...)` would have to be a valid swift expression for `e` to work. It isn't, since swift is expecting some kind of object not a hex literal in the first slot. You could try using one of the swift Unsafe casts operators to make that address back to whatever type it is, though swift casting is even more tedious than C casting... – Jim Ingham Nov 29 '22 at 02:30
  • ok, what about using memory in objc context like I tried with `[DumpTree apple:0x7fc1fd8301a0]` shouldn't lldb be smart enough to understand the address is an object, for instance if I do `[0x7fc1fd8301a0 method]` it does understand me (or maybe I should make it pure objc) – Mike.R Nov 29 '22 at 15:20
  • ok I think l got it: `expr -l Swift -- let $var = unsafeBitCast(0x7df67c50, to: SomeClass.self)` `expr -l Swift -- dump($var)` I need to verify it – Mike.R Nov 29 '22 at 15:33
  • As a convenience, lldb will force cast hex literals to "id" if they are the first element of an ObjC message send, because they have to be that and it's a very common usage pattern. But it doesn't do that in any other position; the expression evaluator tries to be as close to "what the compiler would do for the code in this context" so we limit where it guesses about your intent. – Jim Ingham Nov 29 '22 at 19:18

1 Answers1

0

Thanks to @Jim Ingham for the comment so I have to cast the raw memory to the correct object and then it worked:

expr -l Swift -- import UIKit
expr -l Swift -- let $var = unsafeBitCast(0x7fe3f134bd70, to: UIView.self)
expr -l Swift -- dump($var)

although I had seen that answer before I didnt link between the two unfortunately

Mike.R
  • 2,824
  • 2
  • 26
  • 34