That’s the way you do it

I’ve been meaning to write lots more about Clojure, and will do at some point soon; as I delve deeper into the language, it gets more and more interesting. There is one thing that stopped me making progress for a while, so I’d better mention that.
I wanted to step through the Clojure repl (read-eval-print-loop), particularly the compiler, at the instruction level to get a feel for what is really going on under the covers. I dutifully downloaded the 1.0.0 zip, unpacked it onto my desktop and used ant to build it, but couldn’t figure out how to get the jdb debugger to show me the source as I stepped through it.
It turns out (and I expect you knew this), that the jvm can be started in a mode where it allows connections from exterior debuggers. To get this to work you’d run the repl using:
 "c:Program FilesJavajdk1.6.0_14binjava.exe"  -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=4571 -classpath clojure.jar  clojure.lang.Repl
and then connect jdb to it using:
    "c:Program FilesJavajdk1.6.0_14binjdb.exe" -connect com.sun.jdi.SocketAttach:hostname=localhost,port=4571
You can then set a breakpoint at entry into the repl:
main[1] stop in clojure.lang.Repl.main
main[1] run
You can then use the "use" command to set the path for the source finding
main[1] use /users/clive/desktop/clojure-1.0.0/src/jvm
main[1] list
17    public class Repl {
19    public static void main(String[] args) throws Exception{
20 =>     main.legacy_repl(args);
21    }
22    }
The cool thing is that the Clojure code is compiled with source location information
main[1] stop in clojure.core$apply__3243.doInvoke
main[1] cont
For such locations, we need to set the source search path elsewhere
main[1] use /users/clive/desktop/clojure-1.0.0/src/clj
main[1] list
386    (defn apply
387      "Applies fn f to the argument list formed by prepending args to argseq.
388      {:arglists ‘([f args* argseq])}
389      [#^clojure.lang.IFn f & args]
390 =>     (. f (applyTo (spread args))))
392    (defn vary-meta
393     "Returns an object of the same type and value as obj, with
394      (apply f (meta obj) args) as its metadata."
395     [obj f & args]
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s