objective-c - 如何使用地址(指针)从 lldb 调用 swift 函数?
问题描述
我正在玩,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)
但我想学习如何使用函数的地址或符号调用函数,而不像上面那样引用特定的类。
解决方案
我找到了解决方案。
- 从范围中获取地址:
Symbol: id = {0x00002a71}, range = [0x000000010ac0c780-0x000000010ac0c830)
这个:0x000000010ac0c780
。 - 导入 UIKit
(lldb) po import UIKit
- 创建
Storyboard
:
(lldb) p UIStoryboard(name: "Main", bundle: nil) (UIStoryboard) $R2 = 0x00006000006c8200 { ObjectiveC.NSObject = { isa = UIStoryboard } }
- 将原始地址转换为函数并使用创建的
Storyboard
变量调用它$R2
:
(lldb) po unsafeBitCast(0x000000010ac0c780, to: (@convention(c)(UIKit.UIStoryboard, Bool) -> ()).self)($R2, true)
在这里您还可以阅读导入 C 函数的有趣示例:Swift: How to call a C function loaded from a dylib
推荐阅读
- yocto - poky里面有什么操作系统分布?
- javascript - 如何动态地将侦听器添加到小部件列
- php - spl_autoload_register - 从数组中只加载一个类
- node.js - 有多个页面时如何使用axios获取所有响应数据?
- swift - 我希望系统报告不符合协议,但事实并非如此!为什么?
- h2 - 如何查看配置值
- javascript - 隐藏inapp浏览器上的地址栏反应原生而不使用webview
- ios - ios:如何检查应用程序与扩展程序上下文
- c# - 如何在没有按钮的情况下打开内容对话框单击从 c# 代码到 UWp 应用程序的 xaml 代码
- python - 如何将 EXE 的图标替换为“默认”图标?