Lldb (Swift): Casting Raw Address into Usable Type

LLDB (Swift): Casting Raw Address into Usable Type

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)

How do I print property of object with given address in LLDB?

Cast it to the object type. Example:

po ((UIView *) 0x12345).layer

Evaluate Swift expressions in LLDB

Working with Swift code in LLDB /h3>

Most Swift code can be executed as part of LLDB if it's part of the stdlib with the right syntax. The key is to prefix type names with the symbol identifier $. I've used $ even for variable names here (new LLDB improvements make this unnecessary) because I prefer to distinguish LLDB definitions.

Extensions /h4>

With improvements to LLDB, you can actually copy-paste the Swift code directly after expression.

I've added an example for your extension with $ symbols for clarity:

(lldb) expression
extension Collection where Self.Element == Int {
var $elementsOver30: [Self.Element]? {
return self.filter { $0 > 30 }
}
}
(lldb) po [32,31,4].$elementsOver30
▿ Optional>
▿ some : 2 elements
- 0 : 32
- 1 : 31

Pressing Enter after expression prompts a multi-line evaluation where we can input the remaining Swift code.

Structs/Class definitions /h4>
(lldb) expression
struct $Person {
let name: String
}
(lldb) po let $pranav = $Person.init(name: "pranav")
(lldb) po $pranav
▿ $Person
- name : "pranav"

Reading a Swift instance from memory

Sometimes, we need to read Swift code using Objective-C in LLDB. For example, if we have a Swift file with a View Controller:

class LoginViewController: UIViewController {
...
}

We can print the dynamic type of the result of a memory address as follows:

(lldb) expr -O --language objc -- 0x14160c260

View Swift docs /h4>

Use type lookup to read a minified version of the type definitions:

(lldb) type lookup Equatable
protocol Equatable {
static func == (lhs: Self, rhs: Self) -> Swift.Bool
}
extension Swift.Equatable {
static func != (lhs: Self, rhs: Self) -> Swift.Bool
}

Xcode debugger (lldb) get object description from memory address

po works for addresses (In Objective-C/Mac context, at least)

e.g.:

(lldb) po [NSNotificationCenter defaultCenter]

NSWindowDidResizeNotification, 0x7fff9a0e98e0, 0x6100001246a0, 1400

(lldb) po 0x6100001246a0

ExpandOneView: 0x6100001246a0

LLDB Breakpoint on all methods in Swift class

The following syntax works for creating a Regex breakpoint for every symbol (properties and methods) in a Swift type, including structs and classes.

Assuming a type called Circle:

(lldb) break set -r "\.Circle\..*"

How to invoke swift function from lldb using address (pointer)?

I have found the solution.

  1. Take address from range: Symbol: id = {0x00002a71}, range = [0x000000010ac0c780-0x000000010ac0c830) this one: 0x000000010ac0c780.
  2. Import UIKit (lldb) po import UIKit
  3. Create Storyboard:
(lldb) p UIStoryboard(name: "Main", bundle: nil)  
(UIStoryboard) $R2 = 0x00006000006c8200 {
ObjectiveC.NSObject = {
isa = UIStoryboard
}
}

  1. Cast raw address to function and invoke it using created Storyboard variable $R2:

    (lldb) po unsafeBitCast(0x000000010ac0c780, to: (@convention(c)(UIKit.UIStoryboard, Bool) -> ()).self)($R2, true)

Here you can read also an interesting example of importing C function: Swift: How to call a C function loaded from a dylib

Swift generics in lldb - error: unable to find any types that match the raw type for full type

You shouldn't have to tell lldb the type. OTOH, lldb has to be able to figure out what the fully resolved type of this argument is at runtime, and that can be tricky.

If this is Xcode 7 or Swift 2.x, you might try with Xcode 8/Swift 3.0. lldb got a fair bit better at this task with some help from the Swift 3.0 compiler. If it still doesn't work with Swift 3.0, please file a bug with http://bugreporter.apple.com.



Related Topics



Leave a reply



Submit