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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5