Search code examples
objective-cswiftdebugginglldb

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


I'm playing with lldb and I'm trying to call my swift static function. I was able to find its details in image, however I have no idea how to call it and pass arguments.

My output:

(lldb) image lookup -vs $S5project19ViewControllerUtilsC07setRootbC010storyboard13withAnimationySo12UIStoryboardC_SbtFZ
1 symbols match '$S5project19ViewControllerUtilsC07setRootbC010storyboard13withAnimationySo12UIStoryboardC_SbtFZ' in /Users/user/Library/Developer/CoreSimulator/Devices/DC7A5199-424E-4E38-A8A8-CB99C7D8CF82/data/Containers/Bundle/Application/BE7BB249-DB64-4228-B64E-EB430BCCE29E/project.app/project:
        Address: project[0x00000002000fc970] (project.__TEXT.__text + 1027840)
        Summary: project`static project.ViewControllerUtils.setRootViewController(storyboard: __C.UIStoryboard, withAnimation: Swift.Bool) -> () at ViewControllerUtils.swift:7
         Module: file = "/Users/user/Library/Developer/CoreSimulator/Devices/DC7A5199-424E-4E38-A8A8-CB99C7D8CF82/data/Containers/Bundle/Application/BE7BB249-DB64-4228-B64E-EB430BCCE29E/project.app/project", arch = "x86_64"
    CompileUnit: id = {0x00000000}, file = "/Users/user/mobile/company/iOS/project/project/Utils/ViewControllerUtils.swift", language = "swift"
       Function: id = {0x7700000075}, name = "static project.ViewControllerUtils.setRootViewController(storyboard: __C.UIStoryboard, withAnimation: Swift.Bool) -> ()", mangled = "$S5project19ViewControllerUtilsC07setRootbC010storyboard13withAnimationySo12UIStoryboardC_SbtFZ", range = [0x000000010bd0c780-0x000000010bd0c82e)
       FuncType: id = {0x7700000075}, byte-size = 8, decl = ViewControllerUtils.swift:7, compiler_type = "(UIKit.UIStoryboard, Swift.Bool) -> ()
"
         Blocks: id = {0x7700000075}, range = [0x10bd0c780-0x10bd0c82e)
      LineEntry: [0x000000010bd0c780-0x000000010bd0c79d): /Users/user/mobile/company/iOS/project/project/Utils/ViewControllerUtils.swift:7
         Symbol: id = {0x00002a71}, range = [0x000000010ac0c780-0x000000010ac0c830), name="static project.ViewControllerUtils.setRootViewController(storyboard: __C.UIStoryboard, withAnimation: Swift.Bool) -> ()", mangled="$S5project19ViewControllerUtilsC07setRootbC010storyboard13withAnimationySo12UIStoryboardC_SbtFZ"
       Variable: id = {0x6600000092}, name = "storyboard", type = "UIKit.UIStoryboard", location =  DW_OP_fbreg(-16), decl = ViewControllerUtils.swift:7
       Variable: id = {0x66000000a0}, name = "withAnimation", type = "Swift.Bool", location =  DW_OP_fbreg(-24), decl = ViewControllerUtils.swift:7
       Variable: id = {0x66000000ae}, name = "self", type = "@thick project.ViewControllerUtils.Type", location =  DW_OP_fbreg(-32), decl = ViewControllerUtils.swift:7

Source code:

final class ViewControllerUtils {
    static func setRootViewController(storyboard: UIStoryboard, withAnimation: Bool) {
        setRootViewController(window: UIApplication.shared.keyWindow, storyboard: storyboard, withAnimation: withAnimation)
    }
}

I know I could switch to Swift and invoke something like this:

expression -l swift -O -- ViewControllerUtils.setRootViewController(storyboard: UIKit.UIStoryboard(name: "Main", bundle: nil), withAnimation: true)

But I would like to learn how to invoke a function using its address or symbol without referencing to specific class like above.


Solution

  • 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