在Cocoa程序中访问Swift REPL

时间:2014-07-12 18:09:33

标签: 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/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。

2 个答案:

答案 0 :(得分:4)

dlopen()救援!

下载this script并保存,例如到〜/ .lldb / inject-swift-repl

然后运行lldb -n Finder --source .lldb/inject-swift-repl以注入所有必需的库。请注意,您需要禁用SIP以弄乱Finder。另请注意,有时我必须退出LLDB并重新运行脚本,因为某些dlopen调用失败并返回0。

答案 1 :(得分:1)

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

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