Access a Swift REPL in Cocoa programs

落花浮王杯 提交于 2019-12-05 02:11:42

问题


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>

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

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.


回答1:


dlopen() to the rescue!

Download this script and save it e.g. to ~/.lldb/inject-swift-repl

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.




回答2:


For Xcode 10/Swift 4.2 I had to load the libs in this order

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


来源:https://stackoverflow.com/questions/24715891/access-a-swift-repl-in-cocoa-programs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!