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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5