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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (hide annotations)
Sun May 11 08:57:13 2003 UTC (10 years, 11 months ago) by gerd
Branch: MAIN
Changes since 1.21: +9 -9 lines
	If encapsulation is :default, encapsulate if we know function-end
	breakpoints can't be used.

	* src/code/ntrace.lisp (trace-1): Use
	can-set-function-end-breakpoint-p.

	* src/code/debug-int.lisp (can-set-function-end-breakpoint-p):
	New function.
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 gerd 1.22 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/ntrace.lisp,v 1.22 2003/05/11 08:57:13 gerd 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 gerd 1.22 (multiple-value-bind (res named-p)
139 ram 1.8 (typecase x
140     (symbol
141 ram 1.11 (cond ((special-operator-p x)
142 ram 1.8 (error "Can't trace special form ~S." x))
143     ((macro-function x))
144     (t
145     (values (fdefinition x) t))))
146     (function x)
147     (t (values (fdefinition x) t)))
148     (if (eval:interpreted-function-p res)
149     (values res named-p (if (eval:interpreted-function-closure res)
150     :interpreted-closure :interpreted))
151     (case (kernel:get-type res)
152     (#.vm:closure-header-type
153     (values (kernel:%closure-function res) named-p :compiled-closure))
154     (#.vm:funcallable-instance-header-type
155     (values res named-p :funcallable-instance))
156     (t (values res named-p :compiled))))))
157 ram 1.7
158    
159 ram 1.8 ;;; TRACE-REDEFINED-UPDATE -- Internal
160 chiles 1.1 ;;;
161 ram 1.8 ;;; When a function name is redefined, and we were tracing that name, then
162     ;;; untrace the old definition and trace the new one.
163 chiles 1.1 ;;;
164 ram 1.8 (defun trace-redefined-update (fname new-value)
165     (when (fboundp fname)
166     (let* ((fun (trace-fdefinition fname))
167     (info (gethash fun *traced-functions*)))
168     (when (and info (trace-info-named info))
169     (untrace-1 fname)
170     (trace-1 fname info new-value)))))
171 chiles 1.1 ;;;
172 ram 1.8 (push #'trace-redefined-update ext:*setf-fdefinition-hook*)
173 chiles 1.1
174 ram 1.8
175     ;;; COERCE-FORM, COERCE-FORM-LIST -- Internal
176     ;;;
177     ;;; Annotate some forms to evaluate with pre-converted functions. Each form
178     ;;; is really a cons (exp . function). Loc is the code location to use for
179     ;;; the lexical environment. If Loc is NIL, evaluate in the null environment.
180     ;;; If Form is NIL, just return NIL.
181     ;;;
182     (defun coerce-form (form loc)
183     (when form
184     (let ((exp (car form)))
185     (if (di:code-location-p loc)
186     (let ((fun (di:preprocess-for-eval exp loc)))
187     (cons exp
188     #'(lambda (frame)
189     (let ((*current-frame* frame))
190     (funcall fun frame)))))
191     (let* ((bod (ecase loc
192     ((nil) exp)
193     (:encapsulated
194     `(flet ((debug:arg (n)
195     (declare (special argument-list))
196     (elt argument-list n)))
197     (declare (ignorable #'debug:arg))
198     ,exp))))
199     (fun (coerce `(lambda () ,bod) 'function)))
200     (cons exp
201     #'(lambda (frame)
202     (declare (ignore frame))
203     (let ((*current-frame* nil))
204     (funcall fun)))))))))
205     ;;;
206     (defun coerce-form-list (forms loc)
207     (mapcar #'(lambda (x) (coerce-form x loc)) forms))
208    
209    
210     ;;; PRINT-TRACE-INDENTATION -- Internal
211     ;;;
212     ;;; Print indentation according to the number of trace entries. Entries
213     ;;; whose condition was false don't count.
214     ;;;
215     (defun print-trace-indentation ()
216     (let ((depth 0))
217     (dolist (entry *traced-entries*)
218     (when (cdr entry) (incf depth)))
219 wlott 1.10 (format t "~@V,0T~D: "
220 ram 1.8 (+ (mod (* depth 2) (- *max-trace-indentation* 2)) 2)
221     depth)))
222    
223    
224     ;;; TRACE-WHEREIN-P -- Internal.
225     ;;;
226     ;;; Return true if one of the Names appears on the stack below Frame.
227     ;;;
228     (defun trace-wherein-p (frame names)
229     (do ((frame (di:frame-down frame) (di:frame-down frame)))
230     ((not frame) nil)
231     (when (member (di:debug-function-name (di:frame-debug-function frame))
232     names :test #'equal)
233     (return t))))
234    
235     ;;; TRACE-PRINT -- Internal
236     ;;;
237     ;;; Handle print and print-after options.
238     ;;;
239     (defun trace-print (frame forms)
240     (dolist (ele forms)
241     (fresh-line)
242     (print-trace-indentation)
243     (format t "~S = ~S" (car ele) (funcall (cdr ele) frame))))
244    
245     ;;; TRACE-MAYBE-BREAK -- Internal
246     ;;;
247     ;;; Test a break option, and break if true.
248     ;;;
249     (defun trace-maybe-break (info break where frame)
250     (when (and break (funcall (cdr break) frame))
251     (di:flush-frames-above frame)
252     (let ((*stack-top-hint* frame))
253     (break "Breaking ~A traced call to ~S:" where
254     (trace-info-what info)))))
255    
256     ;;; DISCARD-INVALID-ENTRIES -- Internal
257     ;;;
258     ;;; This function discards any invalid cookies on our simulated stack.
259     ;;; Encapsulated entries are always valid, since we bind *traced-entries* in
260     ;;; the encapsulation.
261     ;;;
262     (defun discard-invalid-entries (frame)
263     (loop
264     (when (or (null *traced-entries*)
265     (let ((cookie (caar *traced-entries*)))
266     (or (not cookie)
267     (di:function-end-cookie-valid-p frame cookie))))
268     (return))
269     (pop *traced-entries*)))
270    
271    
272     ;;;; Hook functions:
273    
274     ;;; TRACE-START-BREAKPOINT-FUN -- Internal.
275     ;;;
276     ;;; Return a closure that can be used for a function start breakpoint hook
277     ;;; function and a closure that can be used as the FUNCTION-END-COOKIE
278     ;;; function. The first communicates the sense of the Condition to the second
279     ;;; via a closure variable.
280     ;;;
281     (defun trace-start-breakpoint-fun (info)
282     (let (conditionp)
283     (values
284     #'(lambda (frame bpt)
285     (declare (ignore bpt))
286     (discard-invalid-entries frame)
287     (let ((condition (trace-info-condition info))
288     (wherein (trace-info-wherein info)))
289     (setq conditionp
290     (and (not *in-trace*)
291     (or (not condition)
292     (funcall (cdr condition) frame))
293     (or (not wherein)
294     (trace-wherein-p frame wherein)))))
295    
296     (when conditionp
297     (let ((*print-length* (or *debug-print-length* *print-length*))
298     (*print-level* (or *debug-print-level* *print-level*))
299 wlott 1.10 (kernel:*current-level* 0)
300 ram 1.8 (*standard-output* *trace-output*)
301     (*in-trace* t))
302     (fresh-line)
303     (print-trace-indentation)
304     (if (trace-info-encapsulated info)
305     (locally (declare (special basic-definition argument-list))
306     (prin1 `(,(trace-info-what info) ,@argument-list)))
307 cracauer 1.19 (print-frame-call frame :verbosity 1))
308 ram 1.8 (terpri)
309     (trace-print frame (trace-info-print info)))
310     (trace-maybe-break info (trace-info-break info) "before" frame)))
311    
312     #'(lambda (frame cookie)
313     (declare (ignore frame))
314     (push (cons cookie conditionp) *traced-entries*)))))
315    
316    
317     ;;; TRACE-END-BREAKPOINT-FUN -- Internal
318     ;;;
319     ;;; This prints a representation of the return values delivered. First,
320     ;;; this checks to see that cookie is at the top of *traced-entries*; if it is
321     ;;; not, then we need to adjust this list to determine the correct indentation
322     ;;; for output. We then check to see if the function is still traced and that
323     ;;; the condition succeeded before printing anything.
324 chiles 1.1 ;;;
325 ram 1.8 (defun trace-end-breakpoint-fun (info)
326     #'(lambda (frame bpt *trace-values* cookie)
327     (declare (ignore bpt))
328     (unless (eq cookie (caar *traced-entries*))
329     (setf *traced-entries*
330     (member cookie *traced-entries* :key #'car)))
331    
332     (let ((entry (pop *traced-entries*)))
333     (when (and (not (trace-info-untraced info))
334     (or (cdr entry)
335     (let ((cond (trace-info-condition-after info)))
336     (and cond (funcall (cdr cond) frame)))))
337     (let ((*print-length* (or *debug-print-length* *print-length*))
338     (*print-level* (or *debug-print-level* *print-level*))
339 wlott 1.10 (kernel:*current-level* 0)
340 ram 1.8 (*standard-output* *trace-output*)
341     (*in-trace* t))
342 wlott 1.10 (fresh-line)
343     (pprint-logical-block (*standard-output* nil)
344     (print-trace-indentation)
345     (pprint-indent :current 2)
346     (format t "~S returned" (trace-info-what info))
347     (dolist (v *trace-values*)
348     (write-char #\space)
349     (pprint-newline :linear)
350     (prin1 v)))
351     (terpri)
352     (trace-print frame (trace-info-print-after info)))
353 ram 1.8 (trace-maybe-break info (trace-info-break-after info)
354     "after" frame)))))
355    
356    
357     ;;; TRACE-CALL -- Internal
358 chiles 1.2 ;;;
359 ram 1.8 ;;; This function is called by the trace encapsulation. It calls the
360     ;;; breakpoint hook functions with NIL for the breakpoint and cookie, which
361     ;;; we have cleverly contrived to work for our hook functions.
362 chiles 1.2 ;;;
363 ram 1.8 (defun trace-call (info)
364     (multiple-value-bind
365     (start cookie)
366     (trace-start-breakpoint-fun info)
367     (let ((frame (di:frame-down (di:top-frame))))
368     (funcall start frame nil)
369     (let ((*traced-entries* *traced-entries*))
370     (declare (special basic-definition argument-list))
371     (funcall cookie frame nil)
372     (let ((vals
373     (multiple-value-list
374     (apply basic-definition argument-list))))
375     (funcall (trace-end-breakpoint-fun info) frame nil vals nil)
376     (values-list vals))))))
377 chiles 1.1
378 ram 1.8
379 chiles 1.2 ;;; TRACE-1 -- Internal.
380 chiles 1.1 ;;;
381 ram 1.8 ;;; Trace one function according to the specified options. We copy the
382     ;;; trace info (it was a quoted constant), fill in the functions, and then
383     ;;; install the breakpoints or encapsulation.
384 chiles 1.1 ;;;
385 ram 1.8 ;;; If non-null, Definition is the new definition of a function that we are
386     ;;; automatically retracing; this
387     ;;;
388     (defun trace-1 (function-or-name info &optional definition)
389 gerd 1.22 (multiple-value-bind (fun named kind)
390 ram 1.8 (if definition
391     (values definition t
392     (nth-value 2 (trace-fdefinition definition)))
393     (trace-fdefinition function-or-name))
394     (when (gethash fun *traced-functions*)
395 ram 1.7 (warn "Function ~S already TRACE'd, retracing it." function-or-name)
396     (untrace-1 fun))
397    
398     (let* ((debug-fun (di:function-debug-function fun))
399 gerd 1.22 (end-breakpoint-p (di::can-set-function-end-breakpoint-p debug-fun))
400     (encapsulated
401 ram 1.8 (if (eq (trace-info-encapsulated info) :default)
402     (ecase kind
403 gerd 1.22 (:compiled (not end-breakpoint-p))
404 ram 1.8 (:compiled-closure
405     (unless (functionp function-or-name)
406     (warn "Tracing shared code for ~S:~% ~S"
407     function-or-name fun))
408 gerd 1.22 (not end-breakpoint-p))
409     ((:interpreted :interpreted-closure
410     :funcallable-instance)
411 ram 1.8 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