Lisp introspection? when the function is called and when it will exit

With generic lisp, and I assume introspection properties. How to add code to regular lisp code that will tell me when a function is called and when it finishes execution. I want to take any lisp code and this particular code modification. I believe this should be possible with lisp AST analysis.

+3
source share
3 answers

You can use (trace function)for a simple mechanism. For something more appealing, 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]> 
+6
source

CLOS , , , , , .

+3

Common lisp has a TRACE function that reports the function, arguments, and the resulting value of each specified call. Here is the document page for the Steel Bank 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

+2
source

Source: https://habr.com/ru/post/1698659/


All Articles