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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5