From: jimka
Subject: tracing a function without knowing its name
Date: 
Message-ID: <1177785100.633306.177960@y80g2000hsf.googlegroups.com>
Does anyone know how (or at least how with sbcl) to trace a function
object without knowing its name?

for example: here is a macro which is creating a function.
I'd like to trace it as well while I'm debugging.

(defmacro def-type (type-name lambda-list &rest body)
  `(new-type ',type-name (lambda ,lambda-list ,@body)))

-jim

From: Duane Rettig
Subject: Re: tracing a function without knowing its name
Date: 
Message-ID: <o03b2k1fgl.fsf@gemini.franz.com>
jimka <·····@rdrop.com> writes:

> Does anyone know how (or at least how with sbcl) to trace a function
> object without knowing its name?
>
> for example: here is a macro which is creating a function.
> I'd like to trace it as well while I'm debugging.
>
> (defmacro def-type (type-name lambda-list &rest body)
>   `(new-type ',type-name (lambda ,lambda-list ,@body)))

Don't know about sbcl, but Allegro CL has a function called ftrace,
with which you can do just that:

http://www.franz.com/support/documentation/8.0/doc/operators/excl/ftrace.htm

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Pascal Bourguignon
Subject: Re: tracing a function without knowing its name
Date: 
Message-ID: <87d51ol170.fsf@thalassa.lan.informatimago.com>
jimka <·····@rdrop.com> writes:

> Does anyone know how (or at least how with sbcl) to trace a function
> object without knowing its name?
>
> for example: here is a macro which is creating a function.
> I'd like to trace it as well while I'm debugging.
>
> (defmacro def-type (type-name lambda-list &rest body)
>   `(new-type ',type-name (lambda ,lambda-list ,@body)))

Portably, you'd have to replace the function you want to trace
everywhere it's stored.

In the happy case where it's used only from one place, like in this
macro, you can of course easily do it.

For example:

;; Not tested:

(shadow 'lambda)
(use-package :com.informatimago.common-lisp.source-form)
(defmacro lambda (arguments &body body)
  (let ((declarations (extract-declarations body)))
     (if (find-if (lambda (decl) (member 'trace (rest decl)))
                   declarations)
        ;; Let's trace this lambda:
        `(cl:lambda ,arguments
            ,@declarations
            ,@(let ((doc (extract-documentation body)))
                 (when doc (list doc)))
            (print ,(format nil "~&Entering function ~S~%" 
                      `(lambda ,arguments ,@body)) *trace-output*)
            (force-output *trace-output*)
            (unwind-protect
                 (progn ,@(extract-body body))
              (print ,(format nil "~&Exiting function ~S~%" 
                      `(lambda ,arguments ,@body)) *trace-output*)
              (force-output *trace-output*)))
        ;; Not tracing this function:
        `(cl:lambda ,arguments ,@body))))

So you can now write:

(defmacro def-type (type-name lambda-list &rest body)
  `(new-type ',type-name (lambda ,lambda-list (declare trace) ,@body)))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

In a World without Walls and Fences, 
who needs Windows and Gates?
From: Juho Snellman
Subject: Re: tracing a function without knowing its name
Date: 
Message-ID: <slrnf37vbt.ph5.jsnell@sbz-30.cs.Helsinki.FI>
jimka <·····@rdrop.com> wrote:
> Does anyone know how (or at least how with sbcl) to trace a function
> object without knowing its name?

CL-USER> (defparameter *a* (lambda () 1))
*A*
CL-USER> (trace #.*a* :encapsulate nil)
(#<FUNCTION (LAMBDA #) {1003AC0109}>)
CL-USER> (funcall *a*)
  0: ((LAMBDA ()))
  0: #<FUNCTION (LAMBDA #) {1003AC0109}> returned 1
1

(Though beware that TRACE with :ENCAPSULATE NIL doesn't work on all
platforms that sbcl supports).

-- 
Juho Snellman