/[cmucl]/src/code/ntrace.lisp
ViewVC logotype

Diff of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by ram, Fri May 15 18:31:05 1992 UTC revision 1.8 by ram, Wed May 27 01:09:51 1992 UTC
# Line 11  Line 11 
11  ;;;  ;;;
12  ;;; **********************************************************************  ;;; **********************************************************************
13  ;;;  ;;;
14  ;;; This is a tracing facility.  ;;; This is a tracing facility based on breakpoints.
15  ;;;  ;;;
16  ;;; THIS IS CURRENTLY UNDER DEVELOPMENT AND TESTING.  ;;; Written by Rob MacLachlan and Bill Chiles.
 ;;;  
 ;;; Written by Bill Chiles.  
17  ;;;  ;;;
18  ;;; **********************************************************************  ;;; **********************************************************************
19  ;;;  ;;;
   
20  (in-package "LISP")  (in-package "LISP")
21    
22  (export '(trace untrace))  (export '(trace untrace))
23    
   
24  (in-package "DEBUG")  (in-package "DEBUG")
25    
26  (export '(*trace-print-level* *trace-print-length* *traced-function-list*  (export '(*trace-values* *max-trace-indentation* *trace-encapsulate-default*))
           *trace-frame* *trace-values* *max-trace-indentation*))  
   
 (defvar *traced-function-list* nil  
   "A list of functions which are traced.")  
   
 (defvar *trace-print-length* nil  
   "Tracing output occurs with *print-length* bound to this value.")  
   
 (defvar *trace-print-level* nil  
   "Tracing output occurs with *print-level* bound to this value.")  
   
 (defvar *trace-frame* nil  
   "TRACE causes expressions for its switches to evaluate within a context  
    where this is bound to the appropriate control stack frame.")  
27    
28  (defvar *trace-values* nil  (defvar *trace-values* nil
29    "This is bound to the returned values when evaluating :BREAK-AFTER and    "This is bound to the returned values when evaluating :BREAK-AFTER and
30     :PRINT-AFTER forms.")     :PRINT-AFTER forms.")
31    
32  (defvar *max-trace-indentation* nil  (defvar *max-trace-indentation* 40
33    "This is currently unused.")    "If the trace indentation exceeds this value, then indentation restarts at
34       0.")
35    
36    (defvar *trace-encapsulate-default* :default
37  ;;; TRACE -- Public.    "The default value for the :ENCAPSULATE option to trace.")
 ;;;  
 (defmacro trace (&rest specs)  
   "Establishes tracing for specified functions and pushes them on  
    *traced-function-list*.  Each specification is one of the following:  
       function  
       the name of a function  
       a list form  
    If it is a list form, it has the following structure:  
       (function-or-name <trace-option> <value> <trace-option> <value> ...)  
    If you supply no specifications, TRACE returns the list of traced functions.  
    The following options are valid:  
       :condition  
          A form to EVAL to determine whether TRACE should display anything.  
       :break  
          A form to EVAL to determine whether to call BREAK before the call.  
       :break-after  
          Like :break, but takes effect after the call.  
       :break-all  
          Like :break, but takes effect before and after call.  
       :wherein  
          A function name or list of names in which TRACE will display a call.  
       :print  
          A list of forms for EVAL whose results TRACE will display in addition  
          to other information before the call.  
       :print-after  
          Like :print, but takes effect after the call.  
       :print-all  
          Like :print, but takes effect before and after the call.  
   
    While the provided expression evaluate, debug:*trace-frame* is bound to the  
    appropriate frame for accessing locals of the TRACE'd function.  Therefore,  
    you can use DI:PREPROCESS-FOR-EVAL (and DI:DEBUG-FUNCTION-START-LOCATION if  
    necessary) and with its resulting function, provide an expression including  
    a FUNCALL of the function and debug:*trace-frame*."  
   (cond  
    ((not specs) '*traced-function-list*)  
    (t  
     (let ((name-list nil)  
           (trace-1-forms nil))  
       (dolist (spec specs `(progn  
                              ,@trace-1-forms  
                              ',(reverse name-list)))  
         (multiple-value-bind  
             (name options)  
             (typecase spec  
               ((or symbol function)  
                (values spec nil))  
               (list  
                (let ((fun (car spec)))  
                  (cond ((eq fun 'quote)  
                         (error "Do NOT quote function names."))  
                        ((or (symbolp fun) (functionp fun))  
                         (values fun (cdr spec)))  
                        ((not (and (consp fun) (= (length fun) 2)))  
                         (error "Illegal function name:  ~S." fun))  
                        ((eq (car fun) 'setf)  
                         (values fun (cdr spec)))  
                        (t (error "Illegal function name:  ~S." fun)))))  
               (t (error "Illegal trace spec:  ~S." spec)))  
           (push name name-list)  
           (destructuring-bind (&key condition break break-after break-all  
                                     wherein print print-after print-all)  
                               options  
             (when break-all  
               (setf break (setf break-after break-all)))  
             (when print-all  
               (setf print (setf print-after print-all)))  
             ;; Wherein must be a list of symbols or nil.  
             (setf wherein  
                   (typecase wherein  
                     (null nil)  
                     (symbol (list wherein))  
                     (list (dolist (fun wherein wherein)  
                             (unless (or (symbolp fun)  
                                         (and (consp fun)  
                                              (= (length fun) 2)  
                                              (eq (car fun) 'setf)))  
                               (error "Illegal function name, ~S, in :wherein."  
                                      fun))))  
                     (t (error "Illegal :wherein option:  ~S." wherein))))  
             ;; Print and print-after must be lists.  
             (unless (listp print)  
               (error "Illegal form list, ~S, for :print." print))  
             (unless (listp print-after)  
               (error "Illegal form list, ~S, for :print-after." print-after))  
             (push `(trace-1 ',name ',condition ',break ',break-after  
                             ',wherein ',print ',print-after)  
                   trace-1-forms))))))))  
38    
39    
40    ;;;; Internal state:
41    
42  ;;; TRACE-FDEFINITION  --  Internal  ;;; A hash-table that maps each traced function to the TRACE-INFO.  The entry
43    ;;; for a closure is the shared function-entry object.
44  ;;;  ;;;
45  ;;;    Given a function or macro name, return the definition.  Error if a  (defvar *traced-functions* (make-hash-table :test #'eq))
 ;;; special form.  If already a function, just return it.  
 ;;;  
 (defun trace-fdefinition (x)  
   (typecase x  
     (symbol  
      (cond ((special-form-p x)  
             (error "Can't trace special form ~S." x))  
            ((macro-function x))  
            (t  
             (fdefinition x))))  
     (function x)  
     (t (fdefinition x))))  
   
46    
47  ;;; This is a list of function-end-cookies, which we use to note distinct  ;;; The TRACE-INFO structure represents all the information we need to trace a
48  ;;; dynamic entries into functions.  ;;; given function.
49  ;;;  ;;;
50    (defstruct (trace-info
51                (:print-function
52                 (lambda (s stream d)
53                   (declare (ignore d))
54                   (print-unreadable-object (s stream)
55                     (format stream "Trace-Info ~S" (trace-info-what s)))))
56                (:make-load-form-fun :just-dump-it-normally))
57      ;;
58      ;; The original representation of the thing traced.
59      (what nil :type (or function cons symbol))
60      ;;
61      ;; True if What is a function name whose definition we should track.
62      (named nil)
63      ;;
64      ;; True if tracing is to be done by encapsulation rather than breakpoints.
65      ;; T implies Named.
66      (encapsulated *trace-encapsulate-default*)
67      ;;
68      ;; True if this trace has been untraced.
69      (untraced nil)
70      ;;
71      ;; Breakpoints we set up to trigger tracing.
72      (start-breakpoint nil :type (or di:breakpoint null))
73      (end-breakpoint nil :type (or di:breakpoint null))
74      ;;
75      ;; The list of function names for wherein.  NIL means unspecified.
76      (wherein nil :type list)
77      ;;
78      ;; The following slots represent the forms that we are supposed to evaluate
79      ;; on each iteration.  Each form is represented by a cons (Form . Function),
80      ;; where the Function is the cached result of coercing Form to a function.
81      ;; Forms which use the current environment are converted with
82      ;; PREPROCESS-FOR-EVAL, which gives us a one-arg function.
83      ;; Null environment forms also have one-arg functions, but the argument is
84      ;; ignored.  NIL means unspecified (the default.)
85      ;;
86      ;; Current environment forms:
87      (condition nil)
88      (break nil)
89      ;;
90      ;; List of current environment forms:
91      (print () :type list)
92      ;;
93      ;; Null environment forms.
94      (condition-after nil)
95      (break-after nil)
96      ;;
97      ;; List of null environment forms
98      (print-after () :type list))
99    
100    ;;; This is a list of conses (function-end-cookie . condition-satisfied),
101    ;;; which we use to note distinct dynamic entries into functions.  When we
102    ;;; enter a traced function, we add a entry to this list holding the new
103    ;;; end-cookie and whether the trace condition was statisfied.  We must save
104    ;;; the trace condition so that the after breakpoint knows whether to print.
105  ;;; The length of this list tells us the indentation to use for printing TRACE  ;;; The length of this list tells us the indentation to use for printing TRACE
106  ;;; messages.  ;;; messages.
107  ;;;  ;;;
108  ;;; This list also helps us synchronize the TRACE facility dynamically for  ;;; This list also helps us synchronize the TRACE facility dynamically for
109  ;;; detecting non-local flow of control that affects TRACE'ing.  Whenever  ;;; detecting non-local flow of control.  Whenever execution hits a
110  ;;; execution hits a :function-end breakpoint used for TRACE'ing, we look for  ;;; :function-end breakpoint used for TRACE'ing, we look for the
111  ;;; the function-end-cookie at the top of *traced-entries*.  If it is not  ;;; function-end-cookie at the top of *traced-entries*.  If it is not there, we
112  ;;; there, we can adjust our indentation and the contents of the list  ;;; discard any entries that come before our cookie.
113  ;;; accordingly, printing a warning that some TRACE'd entries have been fouled.  ;;;
114    ;;; When we trace using encapsulation, we bind this variable and add
115    ;;; (nil . condition-satisfied), so a NIL "cookie" marks an encapsulated
116    ;;; tracing.
117    ;;;
118    (defvar *traced-entries* ())
119    (declaim (list *traced-entries*))
120    
121    ;;; This variable is used to discourage infinite recursions when some trace
122    ;;; action invokes a function that is itself traced.  In this case, we quietly
123    ;;; ignore the inner tracing.
124  ;;;  ;;;
125  (defvar *traced-entries* nil)  (defvar *in-trace* nil)
126    
127    
128    ;;;; Utilities:
129    
130  ;;; This maps functions to the two breakpoints created in TRACE-1, so we can  ;;; TRACE-FDEFINITION  --  Internal
131  ;;; get rid of them in UNTRACE-1.  ;;;
132    ;;;    Given a function name, a function or a macro name, return the raw
133    ;;; definition and some information.  "Raw"  means that if the result is a
134    ;;; closure, we strip off the closure and return the bare code.  The second
135    ;;; value is T if the argument was a function name.  The third value is one of
136    ;;; :COMPILED, :COMPILED-CLOSURE, :INTERPRETED, :INTERPRETED-CLOSURE and
137    ;;; :FUNCALLABLE-INSTANCE.
138    ;;;
139    (defun trace-fdefinition (x)
140      (multiple-value-bind
141          (res named-p)
142          (typecase x
143            (symbol
144             (cond ((special-form-p x)
145                    (error "Can't trace special form ~S." x))
146                   ((macro-function x))
147                   (t
148                    (values (fdefinition x) t))))
149            (function x)
150            (t (values (fdefinition x) t)))
151        (if (eval:interpreted-function-p res)
152            (values res named-p (if (eval:interpreted-function-closure res)
153                                    :interpreted-closure :interpreted))
154            (case (kernel:get-type res)
155              (#.vm:closure-header-type
156               (values (kernel:%closure-function res) named-p :compiled-closure))
157              (#.vm:funcallable-instance-header-type
158               (values res named-p :funcallable-instance))
159              (t (values res named-p :compiled))))))
160    
161    
162    ;;; TRACE-REDEFINED-UPDATE  --  Internal
163  ;;;  ;;;
164  (defvar *trace-breakpoints* (make-hash-table :test #'eq))  ;;;    When a function name is redefined, and we were tracing that name, then
165    ;;; untrace the old definition and trace the new one.
166  ;;;  ;;;
167  (defun clear-trace-breakpoint-record (fname new-value)  (defun trace-redefined-update (fname new-value)
   (declare (ignore new-value))  
168    (when (fboundp fname)    (when (fboundp fname)
169      (let* ((fun (trace-fdefinition fname))      (let* ((fun (trace-fdefinition fname))
170             (bpts (gethash fun *trace-breakpoints*)))             (info (gethash fun *traced-functions*)))
171        (when bpts        (when (and info (trace-info-named info))
172          ;; Free breakpoint bookkeeping data.          (untrace-1 fname)
173          (di:delete-breakpoint (car bpts))          (trace-1 fname info new-value)))))
174          (di:delete-breakpoint (cdr bpts))  ;;;
175          ;; Free TRACE bookkeeping data.  (push #'trace-redefined-update ext:*setf-fdefinition-hook*)
176          (setf (gethash fun *trace-breakpoints*) nil)))))  
177    
178    ;;; COERCE-FORM, COERCE-FORM-LIST  --  Internal
179    ;;;
180    ;;;    Annotate some forms to evaluate with pre-converted functions.  Each form
181    ;;; is really a cons (exp . function).  Loc is the code location to use for
182    ;;; the lexical environment.  If Loc is NIL, evaluate in the null environment.
183    ;;; If Form is NIL, just return NIL.
184    ;;;
185    (defun coerce-form (form loc)
186      (when form
187        (let ((exp (car form)))
188          (if (di:code-location-p loc)
189              (let ((fun (di:preprocess-for-eval exp loc)))
190                (cons exp
191                      #'(lambda (frame)
192                          (let ((*current-frame* frame))
193                            (funcall fun frame)))))
194              (let* ((bod (ecase loc
195                            ((nil) exp)
196                            (:encapsulated
197                             `(flet ((debug:arg (n)
198                                       (declare (special argument-list))
199                                       (elt argument-list n)))
200                                (declare (ignorable #'debug:arg))
201                                ,exp))))
202                     (fun (coerce `(lambda () ,bod) 'function)))
203                (cons exp
204                      #'(lambda (frame)
205                          (declare (ignore frame))
206                          (let ((*current-frame* nil))
207                            (funcall fun)))))))))
208  ;;;  ;;;
209  (push #'clear-trace-breakpoint-record ext:*setf-fdefinition-hook*)  (defun coerce-form-list (forms loc)
210      (mapcar #'(lambda (x) (coerce-form x loc)) forms))
211    
212  ;;; TRACE-1 -- Internal.  
213    ;;; PRINT-TRACE-INDENTATION  --  Internal
214  ;;;  ;;;
215  ;;; This establishes :function-start and :function-end breakpoints with  ;;;    Print indentation according to the number of trace entries.  Entries
216  ;;; appropriate hook functions to TRACE function-name as described by the user.  ;;; whose condition was false don't count.
217  ;;;  ;;;
 (defun trace-1 (function-or-name condition break break-after wherein print  
                  print-after)  
   (let ((fun (trace-fdefinition function-or-name)))  
     (when (member fun *traced-function-list*)  
       (warn "Function ~S already TRACE'd, retracing it." function-or-name)  
       (untrace-1 fun))  
   
     (when wherein  
       (dolist (f wherein)  
         (unless (fboundp f)  
           (error "Undefined :wherein name -- ~S." f))))  
     (let* ((debug-fun (di:function-debug-function fun))  
            ;; The start and end hooks use conditionp for communication.  
            (conditionp nil)  
            (start (di:make-breakpoint  
                    #'(lambda (frame bpt)  
                        (let ((*trace-frame* frame))  
                          (cond ((and (or (not condition) ;Save a call to EVAL  
                                          (eval condition))  
                                      (or (not wherein)  
                                          (trace-wherein-p frame wherein)))  
                                 (setf conditionp t)  
                                 (print-trace-start frame bpt print))  
                                (t (setf conditionp nil)))  
                          (when (and break (eval break))  
                            (di:flush-frames-above frame)  
                            (let ((*stack-top-hint* frame))  
                              (break "Breaking before TRACE'd call to ~S."  
                                     function-or-name)))))  
                    debug-fun :kind :function-start))  
            (end (di:make-breakpoint  
                  #'(lambda (frame bpt *trace-values* cookie)  
                      (if (member fun *traced-function-list*)  
                          (let ((*trace-frame* frame))  
                            (when conditionp  
                              (print-trace-end frame bpt *trace-values* cookie  
                                               print-after))  
                            (pop *traced-entries*)  
                            (when (and break-after (eval break-after))  
                              (di:flush-frames-above frame)  
                              (let ((*stack-top-hint* frame))  
                                (break "Breaking after TRACE'd call to ~S."  
                                       function-or-name))))  
                          (pop *traced-entries*)))  
                  debug-fun :kind :function-end  
                  :function-end-cookie  
                  #'(lambda (frame x)  
                      (when (and *traced-entries*  
                                 (not (di:function-end-cookie-valid-p  
                                       frame (car *traced-entries*))))  
                        (loop  
                          (pop *traced-entries*)  
                          (when (or (not *traced-entries*)  
                                    (di:function-end-cookie-valid-p  
                                     frame (car *traced-entries*)))  
                            (return))))  
                      (push x *traced-entries*)))))  
       (assert (not (gethash fun *trace-breakpoints*)))  
       (setf (gethash fun *trace-breakpoints*) (cons start end))  
       ;; The next two forms must be in the order in which they appear.  They  
       ;; rely on a documented property that later activated breakpoint hooks  
       ;; run first, and the end breakpoint establishes a starting helper bpt.  
       (di:activate-breakpoint start)  
       (di:activate-breakpoint end))  
     (push fun *traced-function-list*)))  
   
 ;;; PRINT-TRACE-START -- Internal.  
 ;;;  
 ;;; This prints a representation of the call establishing frame.  
 ;;;  
 (defun print-trace-start (frame bpt &optional print)  
   (declare (ignore bpt))  
   (let ((*print-length* (or *trace-print-length* *print-length*))  
         (*print-level* (or *trace-print-level* *print-level*)))  
     (fresh-line)  
     (print-trace-indentation)  
     (print-frame-call-1 frame nil)  
     (dolist (ele print)  
       (fresh-line)  
       (print-trace-indentation)  
       (prin1 (eval ele)))  
     (terpri)))  
   
 ;;; PRINT-TRACE-END -- Internal.  
 ;;;  
 ;;; This prints a representation of the return values delivered to frame by the  
 ;;; function for which bpt is a :function-end breakpoint.  First, this checks  
 ;;; to see that cookie is at the top of *traced-entries*; if it is not, then we  
 ;;; need to adjust this list to determine the correct indentation for output.  
 ;;;  
 (defun print-trace-end (frame bpt values cookie &optional print-after)  
   (declare (ignore frame bpt))  
   (unless (eq cookie (car *traced-entries*))  
     (setf *traced-entries* (member cookie *traced-entries*)))  
   (print-trace-indentation)  
   (write-string "returned ")  
   (dolist (v values)  
     (prin1 v)  
     (write-char #\space))  
   (dolist (ele print-after)  
     (terpri)  
     (print-trace-indentation)  
     (prin1 (eval ele)))  
   (terpri))  
   
218  (defun print-trace-indentation ()  (defun print-trace-indentation ()
219    (let ((len (length (the list *traced-entries*))))    (let ((depth 0))
220      (dotimes (i len) (write-string "  "))      (dolist (entry *traced-entries*)
221      (prin1 len)        (when (cdr entry) (incf depth)))
222      (write-string ": ")))      (format t "~&~VT~D: "
223                (+ (mod (* depth 2) (- *max-trace-indentation* 2)) 2)
224                depth)))
225    
226    
227  ;;; TRACE-WHEREIN-P -- Internal.  ;;; TRACE-WHEREIN-P -- Internal.
228  ;;;  ;;;
229  ;;; The TRACE hooks use this for the :wherein arg.  ;;;    Return true if one of the Names appears on the stack below Frame.
230  ;;;  ;;;
231  (defun trace-wherein-p (frame names)  (defun trace-wherein-p (frame names)
232    (do ((frame (di:frame-down frame) (di:frame-down frame)))    (do ((frame (di:frame-down frame) (di:frame-down frame)))
# Line 320  Line 235 
235                    names :test #'equal)                    names :test #'equal)
236        (return t))))        (return t))))
237    
238    ;;; TRACE-PRINT  --  Internal
239    ;;;
240    ;;;    Handle print and print-after options.
241    ;;;
242    (defun trace-print (frame forms)
243      (dolist (ele forms)
244        (fresh-line)
245        (print-trace-indentation)
246        (format t "~S = ~S" (car ele) (funcall (cdr ele) frame))))
247    
248    ;;; TRACE-MAYBE-BREAK  --  Internal
249    ;;;
250    ;;;    Test a break option, and break if true.
251    ;;;
252    (defun trace-maybe-break (info break where frame)
253      (when (and break (funcall (cdr break) frame))
254        (di:flush-frames-above frame)
255        (let ((*stack-top-hint* frame))
256          (break "Breaking ~A traced call to ~S:" where
257                 (trace-info-what info)))))
258    
259    ;;; DISCARD-INVALID-ENTRIES  --  Internal
260    ;;;
261    ;;;    This function discards any invalid cookies on our simulated stack.
262    ;;; Encapsulated entries are always valid, since we bind *traced-entries* in
263    ;;; the encapsulation.
264    ;;;
265    (defun discard-invalid-entries (frame)
266      (loop
267        (when (or (null *traced-entries*)
268                  (let ((cookie (caar *traced-entries*)))
269                    (or (not cookie)
270                        (di:function-end-cookie-valid-p frame cookie))))
271          (return))
272        (pop *traced-entries*)))
273    
274    
275    ;;;; Hook functions:
276    
277    ;;; TRACE-START-BREAKPOINT-FUN -- Internal.
278    ;;;
279    ;;;    Return a closure that can be used for a function start breakpoint hook
280    ;;; function and a closure that can be used as the FUNCTION-END-COOKIE
281    ;;; function.  The first communicates the sense of the Condition to the second
282    ;;; via a closure variable.
283    ;;;
284    (defun trace-start-breakpoint-fun (info)
285      (let (conditionp)
286        (values
287         #'(lambda (frame bpt)
288             (declare (ignore bpt))
289             (discard-invalid-entries frame)
290             (let ((condition (trace-info-condition info))
291                   (wherein (trace-info-wherein info)))
292               (setq conditionp
293                     (and (not *in-trace*)
294                          (or (not condition)
295                              (funcall (cdr condition) frame))
296                          (or (not wherein)
297                              (trace-wherein-p frame wherein)))))
298    
299             (when conditionp
300               (let ((*print-length* (or *debug-print-length* *print-length*))
301                     (*print-level* (or *debug-print-level* *print-level*))
302                     (*standard-output* *trace-output*)
303                     (*in-trace* t))
304                 (fresh-line)
305                 (print-trace-indentation)
306                 (if (trace-info-encapsulated info)
307                     (locally (declare (special basic-definition argument-list))
308                       (prin1 `(,(trace-info-what info) ,@argument-list)))
309                     (print-frame-call-1 frame nil))
310                 (terpri)
311                 (trace-print frame (trace-info-print info)))
312               (trace-maybe-break info (trace-info-break info) "before" frame)))
313    
314         #'(lambda (frame cookie)
315             (declare (ignore frame))
316             (push (cons cookie conditionp) *traced-entries*)))))
317    
318    
319    ;;; TRACE-END-BREAKPOINT-FUN  --  Internal
320    ;;;
321    ;;;    This prints a representation of the return values delivered.  First,
322    ;;; this checks to see that cookie is at the top of *traced-entries*; if it is
323    ;;; not, then we need to adjust this list to determine the correct indentation
324    ;;; for output.  We then check to see if the function is still traced and that
325    ;;; the condition succeeded before printing anything.
326    ;;;
327    (defun trace-end-breakpoint-fun (info)
328      #'(lambda (frame bpt *trace-values* cookie)
329          (declare (ignore bpt))
330          (unless (eq cookie (caar *traced-entries*))
331            (setf *traced-entries*
332                  (member cookie *traced-entries* :key #'car)))
333    
334          (let ((entry (pop *traced-entries*)))
335            (when (and (not (trace-info-untraced info))
336                       (or (cdr entry)
337                           (let ((cond (trace-info-condition-after info)))
338                             (and cond (funcall (cdr cond) frame)))))
339              (let ((*print-length* (or *debug-print-length* *print-length*))
340                    (*print-level* (or *debug-print-level* *print-level*))
341                    (*standard-output* *trace-output*)
342                    (*in-trace* t))
343                (print-trace-indentation)
344                (format t "~S returned " (trace-info-what info))
345                (dolist (v *trace-values*)
346                  (prin1 v)
347                  (write-char #\space))
348                (trace-print frame (trace-info-print-after info))
349                (terpri))
350              (trace-maybe-break info (trace-info-break-after info)
351                                 "after" frame)))))
352    
353    
354    ;;; TRACE-CALL  --  Internal
355    ;;;
356    ;;;    This function is called by the trace encapsulation.  It calls the
357    ;;; breakpoint hook functions with NIL for the breakpoint and cookie, which
358    ;;; we have cleverly contrived to work for our hook functions.
359    ;;;
360    (defun trace-call (info)
361      (multiple-value-bind
362          (start cookie)
363          (trace-start-breakpoint-fun info)
364        (let ((frame (di:frame-down (di:top-frame))))
365          (funcall start frame nil)
366          (let ((*traced-entries* *traced-entries*))
367            (declare (special basic-definition argument-list))
368            (funcall cookie frame nil)
369            (let ((vals
370                   (multiple-value-list
371                    (apply basic-definition argument-list))))
372              (funcall (trace-end-breakpoint-fun info) frame nil vals nil)
373              (values-list vals))))))
374    
375    
376    ;;; TRACE-1 -- Internal.
377    ;;;
378    ;;;    Trace one function according to the specified options.  We copy the
379    ;;; trace info (it was a quoted constant), fill in the functions, and then
380    ;;; install the breakpoints or encapsulation.
381    ;;;
382    ;;;    If non-null, Definition is the new definition of a function that we are
383    ;;; automatically retracing; this
384    ;;;
385    (defun trace-1 (function-or-name info &optional definition)
386      (multiple-value-bind
387          (fun named kind)
388          (if definition
389              (values definition t
390                      (nth-value 2 (trace-fdefinition definition)))
391              (trace-fdefinition function-or-name))
392        (when (gethash fun *traced-functions*)
393          (warn "Function ~S already TRACE'd, retracing it." function-or-name)
394          (untrace-1 fun))
395    
396        (let* ((debug-fun (di:function-debug-function fun))
397               (encapsulated
398                (if (eq (trace-info-encapsulated info) :default)
399                    (ecase kind
400                      (:compiled nil)
401                      (:compiled-closure
402                       (unless (functionp function-or-name)
403                         (warn "Tracing shared code for ~S:~%  ~S"
404                               function-or-name fun))
405                       nil)
406                      ((:interpreted :interpreted-closure :funcallable-instance)
407                       t))
408                    (trace-info-encapsulated info)))
409               (loc (if encapsulated
410                        :encapsulated
411                        (di:debug-function-start-location debug-fun)))
412               (info (make-trace-info
413                      :what function-or-name
414                      :named named
415                      :encapsulated encapsulated
416                      :wherein (trace-info-wherein info)
417                      :condition (coerce-form (trace-info-condition info) loc)
418                      :break (coerce-form (trace-info-break info) loc)
419                      :print (coerce-form-list (trace-info-print info) loc)
420                      :break-after (coerce-form (trace-info-break-after info) nil)
421                      :condition-after
422                      (coerce-form (trace-info-condition-after info) nil)
423                      :print-after
424                      (coerce-form-list (trace-info-print-after info) nil))))
425    
426          (dolist (wherein (trace-info-wherein info))
427            (unless (or (stringp wherein)
428                        (fboundp wherein))
429              (warn ":WHEREIN name is not a defined global function: ~S"
430                    wherein)))
431    
432          (cond
433           (encapsulated
434            (unless named
435              (error "Can't use encapsulation to trace anonymous function ~S."
436                     fun))
437            (ext:encapsulate function-or-name 'trace `(trace-call ',info)))
438           (t
439            (multiple-value-bind
440                (start-fun cookie-fun)
441                (trace-start-breakpoint-fun info)
442              (let ((start (di:make-breakpoint start-fun debug-fun
443                                               :kind :function-start))
444                    (end (di:make-breakpoint
445                          (trace-end-breakpoint-fun info)
446                          debug-fun :kind :function-end
447                          :function-end-cookie cookie-fun)))
448                (setf (trace-info-start-breakpoint info) start)
449                (setf (trace-info-end-breakpoint info) end)
450                ;;
451                ;; The next two forms must be in the order in which they appear,
452                ;; since the start breakpoint must run before the function-end
453                ;; breakpoint's start helper (which calls the cookie function.)
454                ;; One reason is that cookie function requires that the CONDITIONP
455                ;; shared closure variable be initialized.
456                (di:activate-breakpoint start)
457                (di:activate-breakpoint end)))))
458    
459          (setf (gethash fun *traced-functions*) info)))
460    
461      function-or-name)
462    
463    
464  ;;;; N-UNTRACE.  ;;;; The TRACE macro:
465    
466    ;;;  PARSE-TRACE-OPTIONS  --  Internal
467    ;;;
468    ;;;    Parse leading trace options off of Specs, modifying Info accordingly.
469    ;;; The remaining portion of the list is returned when we encounter a plausible
470    ;;; function name.
471    ;;;
472    (defun parse-trace-options (specs info)
473      (let ((current specs))
474        (loop
475          (when (endp current) (return))
476          (let ((option (first current))
477                (value (cons (second current) nil)))
478            (case option
479              (:condition (setf (trace-info-condition info) value))
480              (:condition-after
481               (setf (trace-info-condition info) (cons nil nil))
482               (setf (trace-info-condition-after info) value))
483              (:condition-all
484               (setf (trace-info-condition info) value)
485               (setf (trace-info-condition-after info) value))
486              (:wherein
487               (setf (trace-info-wherein info)
488                     (if (listp (car value)) (car value) value)))
489              (:encapsulate
490               (setf (trace-info-encapsulated info) (car value)))
491              (:break (setf (trace-info-break info) value))
492              (:break-after (setf (trace-info-break-after info) value))
493              (:break-all
494               (setf (trace-info-break info) value)
495               (setf (trace-info-break-after info) value))
496              (:print
497               (setf (trace-info-print info)
498                     (append (trace-info-print info) (list value))))
499              (:print-after
500               (setf (trace-info-print-after info)
501                     (append (trace-info-print-after info) (list value))))
502              (:print-all
503               (setf (trace-info-print info)
504                     (append (trace-info-print info) (list value)))
505               (setf (trace-info-print-after info)
506                     (append (trace-info-print-after info) (list value))))
507              (t (return)))
508            (pop current)
509            (unless current
510              (error "Missing argument to ~S TRACE option." option))
511            (pop current)))
512        current))
513    
514    
515    ;;; EXPAND-TRACE  --  Internal
516    ;;;
517    ;;;    Compute the expansion of TRACE in the non-trivial case (arguments
518    ;;; specified.)  If there are no :FUNCTION specs, then don't use a LET.  This
519    ;;; allows TRACE to be used without the full interpreter.
520    ;;;
521    (defun expand-trace (specs)
522      (collect ((binds)
523                (forms))
524        (let* ((global-options (make-trace-info))
525               (current (parse-trace-options specs global-options)))
526          (loop
527            (when (endp current) (return))
528            (let ((name (pop current))
529                  (options (copy-trace-info global-options)))
530              (cond
531               ((eq name :function)
532                (let ((temp (gensym)))
533                  (binds `(,temp ,(pop current)))
534                  (forms `(trace-1 ,temp ',options))))
535               ((and (keywordp name)
536                     (not (or (fboundp name) (macro-function name))))
537                (error "Unknown TRACE option: ~S" name))
538               (t
539                (forms `(trace-1 ',name ',options))))
540              (setq current (parse-trace-options current options)))))
541    
542        (if (binds)
543            `(let ,(binds) (list ,@(forms)))
544            `(list ,@(forms)))))
545    
546    
547    ;;; %LIST-TRACED-FUNCTIONS  --  Internal
548    ;;;
549    (defun %list-traced-functions ()
550      (loop for x being each hash-value in *traced-functions*
551            collect (trace-info-what x)))
552    
553    
554    ;;; TRACE -- Public.
555    ;;;
556    (defmacro trace (&rest specs)
557      "TRACE {Option Global-Value}* {Name {Option Value}*}*
558       TRACE is a debugging tool that prints information when specified functions
559       are called.  In its simplest form:
560           (trace Name-1 Name-2 ...)
561    
562       TRACE causes a printout on *TRACE-OUTPUT* each time that one of the named
563       functions is entered or returns (the Names are not evaluated.)  The output
564       is indented according to the number of pending traced calls, and this trace
565       depth is printed at the beginning of each line of output.
566    
567       Options allow modification of the default behavior.  Each option is a pair
568       of an option keyword and a value form.  Options may be interspersed with
569       function names.  Options only affect tracing of the function whose name they
570       appear immediately after.  Global options are specified before the first
571       name, and affect all functions traced by a given use of TRACE.
572    
573       The following options are defined:
574    
575       :CONDITION Form
576       :CONDITION-AFTER Form
577       :CONDITION-ALL Form
578           If :CONDITION is specified, then TRACE does nothing unless Form
579           evaluates to true at the time of the call.  :CONDITION-AFTER is
580           similar, but suppresses the initial printout, and is tested when the
581           function returns.  :CONDITION-ALL tries both before and after.
582    
583       :WHEREIN Names
584           If specified, Names is a function name or list of names.  TRACE does
585           nothing unless a call to one of those functions encloses the call to
586           this function (i.e. it would appear in a backtrace.)  Anonymous
587           functions have string names like \"DEFUN FOO\".
588    
589       :BREAK Form
590       :BREAK-AFTER Form
591       :BREAK-ALL Form
592           If specified, and Form evaluates to true, then the debugger is invoked
593           at the start of the function, at the end of the function, or both,
594           according to the respective option.
595    
596       :PRINT Form
597       :PRINT-AFTER Form
598       :PRINT-ALL Form
599           In addition to the usual prinout, he result of evaluating Form is
600           printed at the start of the function, at the end of the function, or
601           both, according to the respective option.  Multiple print options cause
602           multiple values to be printed.
603    
604       :FUNCTION Function-Form
605           This is a not really an option, but rather another way of specifying
606           what function to trace.  The Function-Form is evaluated immediately,
607           and the resulting function is traced.
608    
609       :ENCAPSULATE {:DEFAULT | T | NIL}
610           If T, the tracing is done via encapsulation (redefining the function
611           name) rather than by modifying the function.  :DEFAULT is the default,
612           and means to use encapsulation for interpreted functions and funcallable
613           instances, breakpoints otherwise.  When encapsulation is used, forms are
614           *not* evaluated in the function's lexical environment, but DEBUG:ARG can
615           still be used.
616    
617       :CONDITION, :BREAK and :PRINT forms are evaluated in the lexical environment
618       of the called function; DEBUG:VAR and DEBUG:ARG can be used.  The -AFTER and
619       -ALL forms are evaluated in the null environment."
620      (if specs
621          (expand-trace specs)
622          '(%list-traced-functions)))
623    
624    
625    ;;;; Untracing:
626    
627    ;;; UNTRACE-1  --  Internal
628    ;;;
629    ;;;    Untrace one function.
630    ;;;
631    (defun untrace-1 (function-or-name)
632      (let* ((fun (trace-fdefinition function-or-name))
633             (info (gethash fun *traced-functions*)))
634        (cond
635         ((not info) (warn "Function is not TRACE'd -- ~S." function-or-name))
636         ((trace-info-encapsulated info)
637          (ext:unencapsulate (trace-info-what info) 'trace))
638         (t
639          (di:delete-breakpoint (trace-info-start-breakpoint info))
640          (di:delete-breakpoint (trace-info-end-breakpoint info))))
641        (setf (trace-info-untraced info) t)
642        (remhash fun *traced-functions*)))
643    
644    ;;; UNTRACE-ALL  --  Internal
645    ;;;
646    ;;;    Untrace all traced functions.
647    ;;;
648    (defun untrace-all ()
649      (dolist (fun (%list-traced-functions))
650        (untrace-1 fun))
651      t)
652    
653  (defmacro untrace (&rest specs)  (defmacro untrace (&rest specs)
654    "Removes tracing from the specified functions.  With no args, untraces all    "Removes tracing from the specified functions.  With no args, untraces all
655     functions."     functions."
656    (let ((specs (or specs *traced-function-list*)))    (if specs
657      `(progn        (collect ((res))
658         ,@(mapcar #'(lambda (spec)          (let ((current specs))
659                       `(untrace-1 ',(if (consp spec) (car spec) spec)))            (loop
660                   specs)              (unless current (return))
661         t)))              (let ((name (pop current)))
662                  (res (if (eq name :function)
663  (defun untrace-1 (function-or-name)                         `(untrace-1 ,(pop current))
664    (let ((fun (trace-fdefinition function-or-name)))                         `(untrace-1 ',name)))))
665      (cond ((member fun *traced-function-list*)            `(progn ,@(res) t)))
666             (let ((breakpoints (gethash fun *trace-breakpoints*)))        '(untrace-all)))
              (di:delete-breakpoint (car breakpoints))  
              (di:delete-breakpoint (cdr breakpoints))  
              (setf (gethash fun *trace-breakpoints*) nil))  
            (setf *traced-function-list* (delete fun *traced-function-list*)))  
           (t (warn "Function is not TRACE'd -- ~S." fun)))))  

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

  ViewVC Help
Powered by ViewVC 1.1.5