在可可程序中访问Swift REPL [英] Access a Swift REPL in Cocoa programs
问题描述
我可以将LLDB附加到用Swift编写的程序中,并可以通过Xcode或运行以下命令来访问REPL:
I can attach LLDB to a program written in Swift and access the REPL, either from within Xcode or by running:
lldb -n ProcessName
(lldb) repl
1>
但是,如果我将LLDB连接到没有Swift运行时的进程,则REPL并不是很有用.例如:
However, if I attach LLDB to a process which doesn't have the Swift runtime, the REPL isn't very useful. For example:
lldb -n Finder
Process 218 stopped
Executable module set to "/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder".
Architecture set to: x86_64-apple-macosx.
(lldb) repl
1> import Cocoa
error: Couldn't lookup symbols:
__swift_FORCE_LOAD_$_swiftAppKit
__swift_FORCE_LOAD_$_swiftCoreGraphics
__swift_FORCE_LOAD_$_swiftObjectiveC
__swift_FORCE_LOAD_$_swiftDispatch
__swift_FORCE_LOAD_$_swiftDarwin
__swift_FORCE_LOAD_$_swiftFoundation
关于如何将Swift运行时加载到尚未与之链接的进程的任何想法吗?这可能是一个非常有用的替代方法,例如注入F脚本.
Any idea on how to load the Swift runtime into a process which isn't linked against it already? This could be a very useful alternative to e.g. injecting F-Script.
推荐答案
dlopen()进行救援!
dlopen() to the rescue!
下载此脚本并保存,例如到〜/.lldb/inject-swift-repl
Download this script and save it e.g. to ~/.lldb/inject-swift-repl
然后运行lldb -n Finder --source .lldb/inject-swift-repl
注入所有必需的库.请注意,您需要禁用SIP以使Finder混乱.还要注意,有时我必须退出LLDB并重新运行脚本,因为某些dlopen调用失败并返回0.
Then run lldb -n Finder --source .lldb/inject-swift-repl
to inject all necessary libraries. Note that you need to disable SIP to mess with the Finder. Also note that sometimes I had to exit LLDB and re-run the script, because some dlopen calls failed and returned 0.
这篇关于在可可程序中访问Swift REPL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!