Clojure and introspection/reflection analysis?

153 views
Skip to first unread message

BerlinBrown

unread,
Oct 23, 2008, 6:42:18 PM10/23/08
to Clojure
I asked this on common lisp thread but I want to work with clojure as
well:

With clojure and I am assuming the introspection properties. How
can I add code to clojure code that will tell me when a function
is called and when has finished executing. I want to take any lisp
code and this particular modification to the code. I figure with
lisp's AST analysis, this should be possible.

For example, pseudo code in common lisp, hello_world.lisp:

(defun hello-world ()
(format t "Hello World"))

(hello-world)

---- And then I have a utility to load hello_world.lisp and execute
the hello-world call.

At the command line:
#Inspect: hello-world function was called
#Hello World
#Inspect: hello-world has finished executing.

BerlinBrown

unread,
Oct 23, 2008, 8:03:30 PM10/23/08
to Clojure
Another similar question (second question):

If you have used ASM, I can do a lot of this "trace" type
functionality with the bytecode manipulator ASM (and obviously
reflection) but I was also looking for the clojure way. http://asm.objectweb.org/).
Or maybe even reflection. Is there something in clojure that might
allow me to see when a function is called.

For example, with ASM, I can get the following information of a
method:

protected <init>(Ljava/lang/String;)V
ALOAD 0
INVOKESPECIAL java/lang/Object.<init>()V
ALOAD 0
ALOAD 1
PUTFIELD org/objectweb/asm/Attribute.type : Ljava/lang/String;
RETURN
MAXSTACK = 2
MAXLOCALS = 2

----
What is put on the stack and when methods are invoked.

Does something similar exist for ASM?

Chouser

unread,
Oct 23, 2008, 10:02:19 PM10/23/08
to clo...@googlegroups.com
On Thu, Oct 23, 2008 at 6:42 PM, BerlinBrown <berlin...@gmail.com> wrote:
>
> For example, pseudo code in common lisp, hello_world.lisp:
>
> (defun hello-world ()
> (format t "Hello World"))
>
> (hello-world)
>
> ---- And then I have a utility to load hello_world.lisp and execute
> the hello-world call.
>
> At the command line:
> #Inspect: hello-world function was called
> #Hello World
> #Inspect: hello-world has finished executing.

This was asked in IRC, and a couple suggestions were posted:

http://paste.lisp.org/display/69085
http://paste.lisp.org/display/69086

--Chouser

Parth Malwankar

unread,
Oct 23, 2008, 10:30:35 PM10/23/08
to Clojure
This message also points to one way of doing this:
http://groups.google.com/group/clojure/msg/91b028c63ec440d6

Parth

mb

unread,
Oct 24, 2008, 2:08:38 AM10/24/08
to Clojure
Hi,

On 24 Okt., 00:42, BerlinBrown <berlin.br...@gmail.com> wrote:
> ---- And then I have a utility to load hello_world.lisp and execute
> the hello-world call.
>
> At the command line:
> #Inspect: hello-world function was called
> #Hello World
> #Inspect: hello-world has finished executing.

There is also clojure.contrib.trace.

Sincerely
Meikel

mritun

unread,
Oct 24, 2008, 2:34:38 AM10/24/08
to Clojure
Hi

Take a look at btrace. It is DTrace like dynamic instrumentation tool
for JVM (DTrace : System :: btrace : JVM, though DTrace is superset of
btrace).

It allows you to instrument JVM at bytecode level with no changes in
code.

It might also help going forward to enhance clojure to insert btrace
probes. These probes can help expose higher level details to
instrumentation tools like btrace and would help professional
developers immensely.

- Akhilesh

BerlinBrown

unread,
Oct 24, 2008, 5:36:42 PM10/24/08
to Clojure
http://clojure.org/other_libraries

Zipper may also be something that I could use.

BerlinBrown

unread,
Oct 24, 2008, 5:39:58 PM10/24/08
to Clojure
Reply all
Reply to author
Forward
0 new messages