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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24 - (hide annotations)
Thu May 15 12:28:56 2003 UTC (10 years, 11 months ago) by gerd
Branch: MAIN
Changes since 1.23: +14 -13 lines
	Let TRACE and PROFILE ignore package locks.

	* src/code/ntrace.lisp (trace-1, untrace-1): Use
	without-package-locks.

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

  ViewVC Help
Powered by ViewVC 1.1.5