LLDB (Swift): Casting Raw Address into Usable Type
Solution 1
Under Xcode 8.2.1 and Swift 3, the lldb command po or p won't work with the typed variable. You will need to use the swift command print to examine the properties of the typed object instance. (Thanks to cbowns's answer!) E.g.:
expr -l Swift -- import UIKit
expr -l Swift -- let $pin = unsafeBitCast(0x7df67c50, to: MKPinAnnotationView.self)
expr -l Swift -- print($pin.alpha)
Solution 2
You can use Swift's unsafeBitCast
function to cast an address to an object instance:
(lldb) e let $pin = unsafeBitCast(0x7df67c50, MKPinAnnotationView.self)
(lldb) po $pin
Then you can work with $pin
as usual – access properties, call methods, etc.
Check out this article for more information: Swift Memory Dumping.
Solution 3
The lldb format for expression
seems to have changed in Xcode 7.3. The following got me started:
(lldb) expr -l Swift -- import UIKit
(lldb) expr -l Swift -- let $view = unsafeBitCast(0x7fb75d8349c0, UIView.self)
Solution 4
For Custom Classes you need to import your project
expr -l Swift -- import MyTestProject
expr -l Swift -- let $vc = unsafeBitCast(0x7fad22c066d0, ViewController.self)
expr -l Swift -- print($vc.view)
Solution 5
Objective-C version
po ((MKPinAnnotationView *)0x7df67c50).alpha
Related videos on Youtube
jarrodparkes
Jarrod Franklin Parkes I am software developer from Huntsville, AL.
Updated on July 08, 2022Comments
-
jarrodparkes almost 2 years
Is there an LLDB command that can cast a raw address into a usable Swift class?
For example:
(lldb) po 0x7df67c50 as MKPinAnnotationView
I know that this address points to a MKPinAnnotationView, but it is not in a frame that I can select. But, I want to cast the raw address into a MKPinAnnotationView so that I can examine its properties. Is this possible?
-
jarrodparkes about 9 yearsFor the first statement i think you forgot the 'expr' or 'expression'. Otherwise it is working great!
-
devios1 about 8 yearsI'm getting "error: use of undeclared identifier 'unsafeBitCast'" in Xcode 7.2.
-
carlos_ms about 8 yearsBesides that error (@devios) there's another error it shows in 7.3.1: "error: unknown type name 'let'"
-
Lucas van Dongen over 6 yearsThis really shouldn't be so hard
-
mfaani over 6 yearsThis was a little counter intuitive. I thought I didn't need to type the
(lldb)
in my console. But it didn't work without that. -
p0lAris about 6 yearsIs there a way to do this in objective-c?
-
Patrick Pijnappel over 5 yearsNote that depending on context you might need to switch lldb to Swift mode first using
(lldb) settings set target.language swift
. Also, in some cases (e.g. when breaking outside of your app's module while casting to a type from your app) you might need to follow that with ae import MyApp
-
Koen. over 5 yearsI keep getting back at this. I probably should create a lldb alias for
expr -l Swift --
.. -
Trev14 over 5 yearsThis worked perfectly for me. In my case I was in the
Debug View Hierarchy
view, right clicked on a view, then selectedPrint description of...
. That gave me a memory address and type I could drop into the code above. Nice to know that the visual debugger puts the console into an Obj-C frame. -
Alexander Stepanishin almost 5 yearsI'm getting error: no such module "MyProjectName". Any thoughts how to fix this?
-
Juanmi over 4 years@AlexanderStepanishin try setting the thread/stack path, Example: "MyApp > Thread 1 > 12 main"
-
tontonCD about 4 yearsfrom your link,
expression -l objc -O -- 0x76543210
is just the response for me, and it doesn't need to know the variable class from the adress! -
Max Desiatov over 2 yearsAs was mentioned in a different answer to this question, typing
settings set target.language swift
once is enough to avoidexpr -l Swift --
in subsequent commands. -
Bogdan Razvan over 2 years@AlexanderStepanishin you need to hit a breakpoint. It won't work if you interrupt the flow by pressing the Debug Memory Graph button in xCode.