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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.33 - (show annotations)
Mon Jan 23 14:11:02 2006 UTC (8 years, 2 months ago) by rtoy
Branch: MAIN
Changes since 1.32: +5 -1 lines
Implement tracing of flet/labels functions.  This probably needs more
work and could probably be implemented better.

With these changes (trace (labels foo bar)) will trace the labels
function FOO in the function BAR.  We only support encapsulate nil,
here.  No check is made for this.

code/ntrace.lisp:
o In TRACE-FDEFINITION, recognize a list as a valid function, and
  return the list as the value of TRACE-FDEFINITION.  This seems
  wrong, but I'm not sure if there's a real fdefinition for it, or if
  we could create a fake one.

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

  ViewVC Help
Powered by ViewVC 1.1.5