strace’ing a Clojure process under lein

Datetime:2016-08-23 05:12:26          Topic: Clojure  Java           Share

Today I wanted to strace a JVM process to see if it was making network calls, and I discovered a minor roadblock: It was a Clojure program being run using the Leiningen build tool. lein run spawns a JVM subprocess and then exits, and I only wanted to trace that subprocess.

The solution is simple, but worth a post: Tell lein to run a different "java" command that actually wraps a call to java with strace. Here's how I did it:

  1. Create a shell script that straces a java call:
    #!/bin/bash
    
    strace -f -o lein-strace.out java "$@"
  2. Add a strace profile to ~/.lein/profiles.clj to call that instead of java directly. If you don't have any profiles already, the file might end up looking like this:
    {:strace {:java-cmd "/home/timmc/bin/strace-java.sh"}}
    Note that this changes the invocation for the subprocess; if you wanted to change the JVM used for running lein itself, you'd want to look into LEIN_JAVA_CMD and hope it's not quoted properly. :-)
  3. Run your lein command with the new profile overlaid: lein with-profiles +strace run
  4. Grovel through lein-strace.out in the current directory and see what turned up!

Thanks to ridcully in the #clojure IRC channel on freenode for reminding me to use the -f option (trace forked subprocesses too!)





About List