簡體   English   中英

如何使用地址(指針)從lldb調用swift函數?

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

我正在玩lldb而我正在嘗試調用我的快速靜態函數。 我能夠在圖像中找到它的細節,但是我不知道如何調用它並傳遞參數。

我的輸出:

(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

源代碼:

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

我知道我可以切換到Swift並調用這樣的東西:

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

但我想學習如何使用其地址或符號調用函數,而無需引用上面的特定類。

我找到了解決方案。

  1. 從范圍獲取地址: Symbol: id = {0x00002a71}, range = [0x000000010ac0c780-0x000000010ac0c830)這一個: 0x000000010ac0c780
  2. 導入UIKit (lldb) po import UIKit
  3. 創建Storyboard
 (lldb) p UIStoryboard(name: "Main", bundle: nil) (UIStoryboard) $R2 = 0x00006000006c8200 { ObjectiveC.NSObject = { isa = UIStoryboard } } 
  1. 將原始地址轉換為函數並使用創建的Storyboard變量$R2調用它:
    (lldb) po unsafeBitCast(0x000000010ac0c780, to: (@convention(c)(UIKit.UIStoryboard, Bool) -> ()).self)($R2, true)

在這里你還可以閱讀一個有趣的導入C函數的例子: Swift:如何調用從dylib加載的C函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM