Access a Swift REPL in Cocoa programs - cocoa

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.

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.

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

Related

Cannot launch MinGW-built program in WinDbg: "No runnable debuggees error in 'g'"

I'm trying to debug a program in WinDbg. However, WinDbg won't run it after I launch it; after loading the binary, when I attempt to continue the program I get this error:
0:002> g
^ No runnable debuggees error in 'g'
Some relevant facts:
I'm using Windows 10.
The program in question is written in C and was built with MinGW.
The program is loaded, as I see it in the Task Manager. It just won't go beyond ntdll!NtWaitForWorkViaWorkerFactory.
There is no error in the program at this point; I'm just trying to get the thing running before I move on to my real problem (which is beyond the scope of this post).
I can attach to a running instance of this program just fine, I just can't launch new ones.
I can launch notepad.exe with WinDbg just fine.

Run an exe file on a Mac with Wineskin (VseeFace Application)

I really hope that someone could help me out with my problem.
I want to start an exe File (VSeeFace - see https://www.vseeface.icu for more information) on my Mac with wineskin. VseeFace is an application for Vtubing.
The application starts normally via Wineskin, but as soon as I try to open my avatar (a .vrm file), the software stops and I don't know why. I have searched everywhere and found nothing about my problem, but perhaps that is also due to my limited knowledge of development skills.
Can anyone help me please?
My Mac:
MacOS Monterey Version 12.4
Radeon Pro 575X 4 GB
My Wineskin Setup:
Wrapper Version Wineskin 2.9.1.5
Engine WS11WineCX64Bit22.0.1-1
Config Utility - Windows-Version 8
VseeFace Software – https://www.vseeface.icu/#download
They also describe how it will work on Linux or maybe Mac,
https://www.vseeface.icu/#running-on-linux-and-maybe-mac
For the Facetracking I run OpenSeeFace (but that seemed to work, I could see the green light of the iMac camera, but I couldn't test it because of my software problem).
I mean I could run the application but on the most important Screen it stops working.
In the debug.log file I noticed the following messages:
0024:err:shell:HCR_GetFolderAttributes should be called for simple PIDL's only!
0024:fixme:exec:SHELL_execute flags ignored: 0x0000000c
0118:err:module:import_dll Library dsound.dll (which is needed by L"C:\windows\system32\devenum.dll") not found
0118:trace:loaddll:build_module Loaded L"C:\windows\system32\msdmo.dll" at 000000034ABC0000: builtin
0118:err:ole:apartment_add_dll couldn't load in-process dll L"C:\windows\system32\devenum.dll"
0118:err:ole:com_get_class_object no class object {62be5d10-60eb-11d0-bd3b-00a0c911ce86} could be created for context 0x1
0118:err:module:import_dll Library dsound.dll (which is needed by L"C:\windows\system32\devenum.dll") not found
0118:err:ole:apartment_add_dll couldn't load in-process dll L"C:\windows\system32\devenum.dll"
0118:err:ole:com_get_class_object no class object {62be5d10-60eb-11d0-bd3b-00a0c911ce86} could be created for context 0x1
0118:err:ole:apartment_add_dll couldn't find function DllGetClassObject in L"C:\windows\system32\devenum.dll"
0118:err:ole:com_get_class_object no class object {62be5d10-60eb-11d0-bd3b-00a0c911ce86} could be created for context 0x1
0118:err:ole:com_get_class_object no class object {62be5d10-60eb-11d0-bd3b-00a0c911ce86} could be created for context 0x1
0118:trace:loaddll:build_module Loaded L"C:\windows\system32\Avrt.dll" at 00000002F2930000: builtin
0118:trace:loaddll:build_module Loaded L"C:\VSeeFace\VSeeFace_Data\Plugins\x86_64\LeapCV4.dll" at 000000010A220000: native
0118:trace:loaddll:build_module Loaded L"C:\windows\system32\VCRUNTIME140_1.dll" at 00000002F7F20000: builtin
0118:err:module:import_dll Loading library MSVCP140_2.dll (which is needed by L"C:\VSeeFace\VSeeFace_Data\Plugins\x86_64\LeapCV5.dll") failed (error c000007b).
0118:trace:loaddll:build_module Loaded L"C:\VSeeFace\VSeeFace_Data\Plugins\x86_64\CONCRT140.dll" at 0000000104CA0000: native
0118:err:module:import_dll Loading library MSVCP140_2.dll (which is needed by L"C:\VSeeFace\VSeeFace_Data\Plugins\x86_64\LeapCV5.dll") failed (error c000007b).
[mvk-info] Created 2 swapchain images with initial size (1280, 720) and contents scale 1.0 for screen Integriertes Retina-Display.
[mvk-error] VK_ERROR_INITIALIZATION_FAILED: Render pipeline compile failed (Error code 2):
Vertex attribute v4(4) of type int4 cannot be read using MTLAttributeFormatUInt4.
0118:trace:loaddll:build_module Loaded L"C:\VSeeFace\UnityPlayer.dll" at 0000000180000000: native
0118:fixme:thread:get_thread_times not implemented on this platform
0118:trace:loaddll:build_module Loaded L"C:\windows\system32\mmdevapi.dll" at 0000000336850000: builtin
0118:trace:loaddll:build_module Loaded L"C:\windows\system32\winecoreaudio.drv" at 00000001C92D0000: builtin
0118:fixme:coreaudio:ca_channel_layout_to_channel_mask Unhandled channel 0xffffffff
0
01b0:fixme:kernelbase:AppPolicyGetThreadInitializationType FFFFFFFFFFFFFFFA, 0000000066D6FE10
01b0:trace:loaddll:build_module Loaded L"C:\windows\system32\avrt.dll" at 00000002F2930000: builtin
01b0:fixme:avrt:AvSetMmThreadCharacteristicsW (L"Audio",0000000066D6FDB8): stub
01b0:trace:loaddll:free_modref Unloaded module L"C:\windows\system32\avrt.dll" : builtin
***If needed I could post the complete log file. ***
I have installed the dlls and they are in all the folders. I have also tried the installation via Winetricks, where it displays the following message:
Executing mkdir -p /Applications/Wineskin/VSeeFace.app/Contents/SharedSupport
warning: You are using a 64-bit WINEPREFIX. Note that many verbs only install 32-bit versions of packages. If you encounter problems, please retest in a clean 32-bit WINEPREFIX before reporting a bug.
Using winetricks 20220411-next - sha256sum: 846f79cac49697fd818f06a6aebf36ca408661f89b97633e9872025b33bc0e7b with wine-7.7 (WineskinCX 22.0.1) and WINEARCH=win64
Unknown arg UnityPlayer.dll
Usage: Applications/Wineskin/VSeeFace.app/Wineskin.app/Contents/Resources/winetricks [options] [command|verb|path-to-verb] ...
Executes given verbs. Each verb installs an application or changes a setting.
Options:
--country=CC Set country code to CC and don't detect your IP address
-f, --force Don't check whether packages were already installed
--gui Show gui diagnostics even when driven by commandline
--gui=OPT Set OPT to kdialog or zenity to override GUI engine
--isolate Install each app or game in its own bottle (WINEPREFIX)
--self-update Update this application to the last version
--update-rollback Rollback the last self update
-k, --keep_isos Cache isos (allows later installation without disc)
--no-clean Don't delete temp directories (useful during debugging)
-q, --unattended Don't ask any questions, just install automatically
-r, --ddrescue Retry hard when caching scratched discs
-t --torify Run downloads under torify, if available
--verify Run (automated) GUI tests for verbs, if available
-v, --verbose Echo all commands as they are executed
-h, --help Display this message and exit
-V, --version Display version and exit
Commands:
list list categories
list-all list all categories and their verbs
apps list list verbs in category 'applications'
benchmarks list list verbs in category 'benchmarks'
dlls list list verbs in category 'dlls'
fonts list list verbs in category 'fonts'
games list list verbs in category 'games'
settings list list verbs in category 'settings'
list-cached list cached-and-ready-to-install verbs
list-download list verbs which download automatically
list-manual-download list verbs which download with some help from the user
list-installed list already-installed verbs
arch=32|64 create wineprefix with 32 or 64 bit, this option must be
given before prefix=foobar and will not work in case of
the default wineprefix.
prefix=foobar select WINEPREFIX=/Users/myusername/.local/share/wineprefixes/foobar
annihilate Delete ALL DATA AND APPLICATIONS INSIDE THIS WINEPREFIX
Winetricks Commands Finished!!

"Closing the internet connection" to another computer python 2.7

I need to program in py 2.7 a function that
when active, it closes all connections to the internet of a client (It is a server-client socket connection)
Because I cannot just make the client turn off the internet (Because then we won't be able to communicate with him)
I thought to run a thread. That when active (When the server sends to the client "close_internet") the thread runs a while true function that closes all the browsers (and thus "closing" its internet connection). I will have a list of known browsers (not all) that the thread will actively close.
This line of code closes any process by name:
import os
import threading
def close_internet():
while 1:
try:
os.system('taskkill /f /im MicrosoftEdge.exe')
except:
pass
threading.Thread(target=close_internet, args=()).start()
When I run this code, it does work, closing explorer when I open it (Explorer just for testing, you can change to chrome.exe or firefox.exe)
However, in the console it prints some scary errors, all the time:
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
What can I do? I only need this program to work in the background, without the user noticing with those errors.
The errors appear (not surprisingly) when I don't have explorer open.
When I do have it open, it works fine and prints:
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
SUCCESS: The process "MicrosoftEdge.exe" with PID 10808 has been terminated. # <-- this line
ERROR: The process "MicrosoftEdge.exe" not found.
The function is programmed on the client-side!
How can I fix it?
And secondly, is this the right way to close a process by its name? is it efficient? or is there a more efficient way to close a process (Maybe using its PID makes it faster...) I'd like to hear your advice.
Thanks!
Try using the subprocess module. I don't think you need Threading for this task.
Here is the code i use to open windows explorer and highlight a file.
subprocess.Popen(r'explorer /select, %s%s'%(self.cwd, ofile))
It should be easy to modify for your task. Also that code is assuming that Edge is open and the name is MicrosoftEdge.exe is the process name. Assume nothing and see if you can get a list of running processes using python, scan through that list and check if the application you want to close is already open. Here is a link with some information on getting the list of processes running on windows.

SBCL: building a standalone executable

How do I build a standalone executable in SBCL? I've tried
; SLIME 2.20
CL-USER> (defun hullo ()
(format t "hullo"))
HULLO
CL-USER> (sb-ext:save-lisp-and-die "hullo" :toplevel #'hullo :executable t)
but that just produces the following error.
Cannot save core with multiple threads running.
Interactive thread (of current session):
#<THREAD "main thread" RUNNING {10019563F3}>
Other threads:
#<THREAD "Swank Sentinel" RUNNING {100329E073}>,
#<THREAD "control-thread" RUNNING {1003423A13}>,
#<THREAD "reader-thread" RUNNING {1003428043}>,
#<THREAD "swank-indentation-cache-thread" RUNNING
{1003428153}>,
#<THREAD "auto-flush-thread" RUNNING {1004047DA3}>,
#<THREAD "repl-thread" RUNNING {1004047FA3}>
[Condition of type SB-IMPL::SAVE-WITH-MULTIPLE-THREADS-ERROR]
What am I doing wrong?
What you are doing wrong is trying to save an image while multiple threads are running. Unlike many errors in Lisp the error message explains exactly what the problem is.
If you look up the function in the sbcl manual here then you find that indeed one may not save an image with multiple threads running. The extra threads come from swank (the CL half of SLIME). The manual says that you may add functions to *save-hooks* which destroy excess threads and functions to *init-hooks* to restore threads.
One way around all this is to not save the image when it is running through slime but instead to start sbcl directly at a terminal (note: no readline support), load your program and save from there.
Working with slime is different. In theory there is a SWANK-BACKEND:SAVE-IMAGE function but I’m not sure if that works. Also as saving an image kills the process you may want to fork (SB-POSIX:FORK) first, unless you are on Windows. But forking causes problems due to not being well specified and file descriptor issues (i.e. if you try fork->close swank connection->save and die then you may find that the connection in the parent process is closed (or worse, corrupted by appearing open but being closed at some lower level)). One can read about such things online. Note that due to the way sbcl threads are implemented, forking clones only the thread that forks and the other threads are not cloned. Thus forking and then saving should work but may cause problems when running the executable due to partial slime state.
You may be interested in buildapp.
If you want to be able to use slime with your saved application you can load swank and start listening on a socket or port (maybe with some command line argument) and then in Emacs you may connect to that swank backend with slime.
You have to run save-lisp-and-die from a new sbcl, not from Slime. Dan Robertson explains more.
It is cumbersome the first time, but you can put it in a Makefile and re-use it. Don't forget to load your dependencies.
build:
sbcl --load cl-torrents.asd \
--eval '(ql:quickload :torrents)' \
--eval '(use-package :torrents)' \ # not mandatory
--eval "(sb-ext:save-lisp-and-die #p\"torrents\" :toplevel #'main :executable t)"
The quickload implies Quicklisp is already loaded, which may be the case if you installed Quicklisp on your machine, because then your ~/.sbclr contains quicklisp loading script ((load quicklisp-init)).
However sb-ext is not portable across implementations. asdf:make is the cross-platform equivalent. Add this in your .asd system definition:
:build-operation "program-op" ;; leave as is
:build-pathname "<binary-name>"
:entry-point "<my-package:main-function>"
and then call asdf:make to build the executable.
You can have a look at buildapp (mentioned above), a still popular app to do just that, for SBCL and CCL. It is in Debian. http://lisp-lang.org/wiki/article/buildapp An example usage looks like
buildapp --output myapp \
--asdf-path . \
--asdf-tree ~/quicklisp/dists \
--load-system my-app \
--entry my-app:main
But see also Roswell, a more general purpose tool, also supposed to build executables, but it is less documented. https://roswell.github.io/
If you want to build an executable on a CI system (like Gitlab CI), you may appreciate a lisp Docker image which has already SBCL, others lisps and Quicklisp installed, and if you want to parse command line arguments, see https://lispcookbook.github.io/cl-cookbook/testing.html#gitlab-ci and (my) tutorial: https://vindarel.github.io/cl-torrents/tutorial.html#org8567d07

select subset of debug info files in gdb session

On my fedora box I have installed a lot of separate debug infos.
sudo dnf debuginfo-install <list of packets>
Now, if I debug some simple code it needs very long until some symbol is displayed or some values are printed. It is quite clear that is absolutly needed to evaluate all the installed symbol files to get all information.
But if I have a problem, say on a lib like goocanvas I only want to have my local debug smbols generated with my own compiled code with -g option and the only the debug infos for goocanvas libs.
How can that kind of selection be achieved? Only by renaming the folder of debug info files and generate a copy of needed ones? Maybe as a symlink? Or is there a common selection option anywhere?
You can skip all debug info from shared libraries and only load goocanvas lib symbols. Here is a sample of how to do it in gdb session:
[ ~]$ gdb -q /your/binary
(gdb) set auto-solib-add off
(gdb) start
Temporary breakpoint 1, 0x000055555564edd0 in main ()
(gdb) sharedlibrary goocanvas
From gdb doc:
If your program uses lots of shared libraries with debug info that
takes large amounts of memory, you can decrease the gdb memory
footprint by preventing it from automatically loading the symbols from
shared libraries. To that end, type set auto-solib-add off before
running the inferior, then load each library whose debug symbols you
do need with sharedlibrary regexp, where regexp is a regular
expression that matches the libraries whose symbols you want to be
loaded.
See also this related question: How to prevent GDB from loading debugging symbol for a (large) library?

Resources