F-Script Injection in Lion

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.

Download “Inject-F-Script-into-application.zip”

Inject-F-Script-into-application.zip – Downloaded 2291 times – 63.58 KB

Installation is identical to the one that comes with F-Script, so follow their readme.

The automator workflow consists of two steps:

1. Run Applescript, with contents

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.

2. Run Shell Script (Shell: /bin/bash, Pass input: as arguments), with contents

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.

You may also like...

2 Responses

  1. Dennis says:

    Very glad to see this easy services injection functionality restored on Lion.
    Excellent!
    muchas gracias

  1. 14 August 2014

    […] However, the developer tools included with Xcode 5 swap out the gdb debugger for lldb, breaking the injection script I fixed for Lion again. After playing around with the automator action some more, here's what I came up with that […]

Leave a Reply to Dennis Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.