UPDATE: This workflow no longer works on Mavericks due to a debugger change. See this post for details on a new version.
I wanted to use the very handy F-Script environment to snoop around inside an application. F-Script can be injected into running applications by using gdb, which of course works fine, but they also provide a services-menu item for performing the injection. Due to several changes in OS X 10.7 Lion, the automator workflow that came with F-Script to perform this did not work.
I reworked the injector service so it works on Lion and doesn't leave behind (or even create) any temp files.
Click to download Inject-F-Script-into-application.zip. (63.6 KB - 413 downloads)
Installation is identical to the one that comes with F-Script, so follow their readme.
The automator workflow consists of two steps.
- Run Applescript
- Run Shell Script (Shell: /bin/bash, Pass input: as arguments)
tell application "System Events" set pid to unix id of the first process whose frontmost is true end tell return "" & pid
This gets the PID of the active application and returns it as a string.
cat << EOF | sudo -u`whoami` gdb -n -q attach '$1' p (char)[[NSBundle bundleWithPath:@"/Library/Frameworks/FScript.framework"] load] p (void)[FScriptMenuItem insertInMainMenu] detach quit EOF
Using the "here-document" feature of bash, lines 2-6 are sent as STDIN to GDB without using a temp file. The PID to attach is passed in as an argument from the previous block. Invoking GDB directly (without sudo) doesn't allow attaching to a process, presumably due to services sandboxing. Sudoing as the current user works fine and allows the attach to happen.