15

Suppose I know, based on earlier console output, that at some memory location is an object of interest:

<MySpecialObject:0x7a5125a0 This is a description of my special object>

In the ObjC debugger I could do something like po [0x7a5125a0 myMethod:arg1 arg2:arg2] to interact with this object in the debugger.

I could also do this:

(lldb) expr MySpecialObject *$foo = 0x7a5125a0
(lldb) po [foo myMethod:arg1 arg2:arg2]

What is the way to accomplish this effect (interact with object in lldb given its memory address) when debugging a Swift program?

1
  • 1
    Debugging in swift is broken at this point. Commented Aug 24, 2014 at 4:14

4 Answers 4

24

One thing you could try is the following:

(lldb) expr -l objc++ -O -- [(id)0xmyFancyAddressGoesHere selector]

Your mileage may vary but essentially this is the glorified version of what you would do in ObjC (except that now you're in Swift-land so you have to force the expression evaluator in ObjC mode (-l objc++), and you can't rely on the "po" alias, so you need to explicitly ask for "object description behavior" (-O)

Of course, if you find yourself doing this often, you can make your own alias for "expr -l objc++ -O --"

Sign up to request clarification or add additional context in comments.

Comments

10

In Swift you use the unsafeBitcast function to cast a memory address to a variable in lldb.

expr $mv = unsafeBitCast(0x7a66cdb0, MapView.self)

This would cast the memory address to a MapVie object. When you're casting your own objects, you may find that you have to import your project's module into lldb.

Update for Swift 4: it appears that the syntax changes slightly in Xcode 10.1 so the unsafeBitCast part now needs to name the second parameter and we add a let (or maybe var depending). So, the example above now becomes

expr let $mv = unsafeBitCast(0x7a66cdb0, to: MapView.self)

4 Comments

This seems to be the Swift equivalent of what I've been doing for decades— (MapView *)0x7a66cdb0. I like.
Yes, this seems to be the Swift equivalent. There is an article about using it by Mike Ash [at his website] (mikeash.com/pyblog/…) and the people over at realm.io wrote about it recently as well
"<EXPR>:3:1: error: use of unresolved identifier '$mv'" Not sure if I'm doing it right...
@surfrider I just did some fiddling around in the console in Xcode 10.1 and I got the same error. It appears that we now have to give our variable a type. So expr int $mv = 2 is fine but expr $mv = 2 throws the error you're seeing. I'll dig around and see if I can update my answer.
4

There is no such way. Debugging Swift is pretty much a non-starter. Evaluation of expressions and inspection of variables while paused is totally broken. You're better off using println or NSLog for now.

EDIT That was a year ago. LLDB is greatly improved for use with Swift now!

1 Comment

greatly improved.. so is there a way to reference a memory address now? :/
3

Version 6.1.1 (6A2008a) I am debugging the view hierarchy. I have my own subclass of UIImage view. Here it is how it works for me :

(lldb) po 0x7fc6ecb55e30

<Lesson_1_Quartz_Composer.Layer: 0x7fc6ecb55e30; baseClass = UIImageView; frame = (0 0; 320 49.5); opaque = NO; gestureRecognizers = <NSArray: 0x7fc6ecb6df20>; layer = <CALayer: 0x7fc6ecb55f30>>

(lldb) po [0x7fc6ecb55e30 isUserInteractionEnabled]
0x0000000182002001

(lldb) po (bool)[0x7fc6ecb55e30 isUserInteractionEnabled]
true

(lldb)  expr -l objc++ -O -- [(id)0x7fc6ecb55e30 isUserInteractionEnabled]
0x0000000182002001

Obviously it is a UIView subclass, so it objective-c, but it mixes with Swift.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.