views:

328

answers:

4

With common lisp and I am assuming the introspection properties. How can I add code to common lisp 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.

+2  A: 

Common lisp has a TRACE function that reports the function, arguments and resulting value of each call specified. Here is the doc page for Steel Bank's version, but you should find something similar in most implementations:

http://www.sbcl.org/manual/Function-Tracing.html

The system also includes a profiler:

http://www.sbcl.org/manual/Deterministic-Profiler.html

Godeke
+6  A: 

You can use (trace function) for a simple mechanism. For something more involved, here is a good discussion from comp.lang.lisp.

[CL_USER]>
(defun fac (n)
    "Naïve factorial implementation"
    (if (< 1 n)
        (* n (fac (- n 1)))
        1))
FAC
[CL_USER]> (trace fac)
;; Tracing function FAC.
(FAC)
[CL_USER]> (fac 5)
1. Trace: (FAC '5)
2. Trace: (FAC '4)
3. Trace: (FAC '3)
4. Trace: (FAC '2)
5. Trace: (FAC '1)
5. Trace: FAC ==> 1
4. Trace: FAC ==> 2
3. Trace: FAC ==> 6
2. Trace: FAC ==> 24
1. Trace: FAC ==> 120
120
[CL_USER]>
dsm
A: 

Yea, that was my question. Notice the usernames.

Berlin Brown
+3  A: 

If CLOS is an option, it has before, after, and around methods that run before, after and around other methods.

Tim Stewart