LLDB (Swift): Casting Raw Address into Usable Type

30,044

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
Share:
30,044

Related videos on Youtube

jarrodparkes
Author by

jarrodparkes

Jarrod Franklin Parkes I am software developer from Huntsville, AL.

Updated on July 08, 2022

Comments

  • jarrodparkes
    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
    jarrodparkes about 9 years
    For the first statement i think you forgot the 'expr' or 'expression'. Otherwise it is working great!
  • devios1
    devios1 about 8 years
    I'm getting "error: use of undeclared identifier 'unsafeBitCast'" in Xcode 7.2.
  • carlos_ms
    carlos_ms about 8 years
    Besides that error (@devios) there's another error it shows in 7.3.1: "error: unknown type name 'let'"
  • Lucas van Dongen
    Lucas van Dongen over 6 years
    This really shouldn't be so hard
  • mfaani
    mfaani over 6 years
    This 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
    p0lAris about 6 years
    Is there a way to do this in objective-c?
  • Patrick Pijnappel
    Patrick Pijnappel over 5 years
    Note 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 a e import MyApp
  • Koen.
    Koen. over 5 years
    I keep getting back at this. I probably should create a lldb alias for expr -l Swift -- ..
  • Trev14
    Trev14 over 5 years
    This worked perfectly for me. In my case I was in the Debug View Hierarchy view, right clicked on a view, then selected Print 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
    Alexander Stepanishin almost 5 years
    I'm getting error: no such module "MyProjectName". Any thoughts how to fix this?
  • Juanmi
    Juanmi over 4 years
    @AlexanderStepanishin try setting the thread/stack path, Example: "MyApp > Thread 1 > 12 main"
  • tontonCD
    tontonCD about 4 years
    from 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
    Max Desiatov over 2 years
    As was mentioned in a different answer to this question, typing settings set target.language swift once is enough to avoid expr -l Swift -- in subsequent commands.
  • Bogdan Razvan
    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.