在Cocoa程序中访问Swift REPL

在Cocoa程序中访问Swift REPL,cocoa,swift,read-eval-print-loop,lldb,f-script,Cocoa,Swift,Read Eval Print Loop,Lldb,F Script,我可以将LLDB连接到用Swift编写的程序,并从Xcode中或通过运行以下命令访问REPL: lldb -n ProcessName (lldb) repl 1> 但是,如果我将LLDB附加到一个没有Swift运行时的进程,REPL就不是很有用。例如: lldb -n Finder Process 218 stopped Executable module set to "/System/Library/CoreServices/Finder.app/Contents/MacOS/

我可以将LLDB连接到用Swift编写的程序,并从Xcode中或通过运行以下命令访问REPL:

lldb -n ProcessName
(lldb) repl
  1>
但是,如果我将LLDB附加到一个没有Swift运行时的进程,REPL就不是很有用。例如:

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-Script。

dlopen()以进行救援

下载并保存到~/.lldb/inject swift repl


然后运行
lldb-nfinder--source.lldb/inject swift repl
注入所有必要的库。请注意,您需要禁用SIP以干扰查找程序。还请注意,有时我必须退出LLDB并重新运行脚本,因为一些dlopen调用失败并返回0。

对于Xcode 10/Swift 4.2,我必须按此顺序加载libs

process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftCore.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftDarwin.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftObjectiveC.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftRemoteMirror.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftSwiftOnoneSupport.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftsimd.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftDispatch.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftCoreFoundation.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftCoreGraphics.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftFoundation.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftCoreData.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftAssetsLibrary.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftModelIO.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftos.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftMetal.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftNetwork.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftCoreImage.dylib
process load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftQuartzCore.dylib

有人能帮我把这个变成剧本吗?我想把它放到我的lldb init文件中,这样我就可以运行“injectswift”…对于Xcode 6 Beta 4,libswiftAppKit.dylib需要libswiftQuartzCore.dylib。我已经相应地更新了。我发现有用:
find/Applications/Xcode.app/Contents/Developer/-name“libswift*”
,因为我没有在完全相同的路径中找到动态库,所以我更新了10.12和Xcode 8.3.1的答案