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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5