How do I describe a local function to (trace)?

2020-03-25 02:19发布

In common lisp, the function (trace name) can be used to see output about the calls to a function.

If my function is declared with local scope, how do I describe it to trace?

eg, how do I trace bar, below:

(defun foo (x)  
  (labels ((bar (y) (format t "bar: ~a~&" y)))  
    (bar x)))  

2条回答
地球回转人心会变
2楼-- · 2020-03-25 02:50

Tracing local functions with (TRACE ...) is not defined by ANSI Common Lisp.

Some implementations have extensions to do that. See for example CMU CL.

Other than that, you would need add some code to the definition of FOO. For example it might be useful to have a macro so that you can write the call to bar as (trace-it (bar x)) and the macro would expand into code that prints the entry and exit.

查看更多
做个烂人
3楼-- · 2020-03-25 02:55

As there is no standard way of tracing local functions, the way I'd go about the problem is by writing a tracing-labels macro that implements tracing, transforming the following:

(defun foo (x)  
  (tracing-labels ((bar (y) (format t "bar: ~a~&" y)))  
    (bar x)))

into something like this:

(defun foo (x)
  (labels ((bar (y)
             (format *trace-output* "~&ENTER: ~S" 'bar)  ;'
             (multiple-value-prog1
                 (progn (format t "bar: ~a~&" y))
               (format *trace-output* "~&LEAVE: ~S" 'bar))))  ;'
    (bar x)))
查看更多
登录 后发表回答