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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (hide annotations)
Fri Jan 27 20:52:19 2006 UTC (8 years, 2 months ago) by rtoy
Branch: MAIN
CVS Tags: snapshot-2006-02
Changes since 1.33: +5 -2 lines
Signal an error if we try to trace flet/labels functions using
encapsulation.  We can't do that because there's nothing to wrap.
1 chiles 1.1 ;;; -*- Package: debug -*-
2     ;;;
3     ;;; **********************************************************************
4     ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;;
7     (ext:file-comment
8 rtoy 1.34 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/ntrace.lisp,v 1.34 2006/01/27 20:52:19 rtoy Exp $")
9 chiles 1.1 ;;;
10     ;;; **********************************************************************
11     ;;;
12 ram 1.8 ;;; This is a tracing facility based on breakpoints.
13 chiles 1.1 ;;;
14 ram 1.8 ;;; Written by Rob MacLachlan and Bill Chiles.
15 chiles 1.1 ;;;
16     ;;; **********************************************************************
17     ;;;
18 chiles 1.2 (in-package "LISP")
19    
20     (export '(trace untrace))
21    
22 chiles 1.1 (in-package "DEBUG")
23    
24 gerd 1.23 (export '(*trace-values* *max-trace-indentation* *trace-encapsulate-default*
25     *trace-encapsulate-package-names*))
26 chiles 1.1
27 gerd 1.25 (use-package :fwrappers)
28    
29 ram 1.7 (defvar *trace-values* nil
30     "This is bound to the returned values when evaluating :BREAK-AFTER and
31     :PRINT-AFTER forms.")
32    
33 ram 1.8 (defvar *max-trace-indentation* 40
34     "If the trace indentation exceeds this value, then indentation restarts at
35     0.")
36 chiles 1.2
37 rtoy 1.32 (defvar *trace-encapsulate-default* :default
38 ram 1.8 "The default value for the :ENCAPSULATE option to trace.")
39    
40 gerd 1.23 (defvar *trace-encapsulate-package-names*
41 gerd 1.27 '("LISP"
42     "COMMON-LISP"
43 gerd 1.23 "CONDITIONS"
44     "DEBUG"
45     "EXTENSIONS"
46     "FORMAT"
47     "KERNEL"
48     "LOOP"
49     "PRETTY-PRINT"
50     "SYSTEM"
51 gerd 1.26 "COMPILER"
52 gerd 1.23 "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 chiles 1.1
58 ram 1.8 ;;;; 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 chiles 1.1 ;;;
63 ram 1.8 (defvar *traced-functions* (make-hash-table :test #'eq))
64 chiles 1.2
65 ram 1.8 ;;; 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 chiles 1.1
118 ram 1.8 ;;; 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 ram 1.7
139 ram 1.8 ;;; 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 ram 1.7 ;;; TRACE-FDEFINITION -- Internal
149     ;;;
150 ram 1.8 ;;; 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 ram 1.7 ;;;
157     (defun trace-fdefinition (x)
158 gerd 1.22 (multiple-value-bind (res named-p)
159 ram 1.8 (typecase x
160     (symbol
161 ram 1.11 (cond ((special-operator-p x)
162 ram 1.8 (error "Can't trace special form ~S." x))
163     ((macro-function x))
164     (t
165     (values (fdefinition x) t))))
166     (function x)
167 rtoy 1.33 (list
168     ;; An extended function name for flets/labels. Should we
169     ;; make this check?
170 rtoy 1.34 (values x t))
171 ram 1.8 (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 ram 1.7
182    
183 ram 1.8 ;;; TRACE-REDEFINED-UPDATE -- Internal
184 chiles 1.1 ;;;
185 ram 1.8 ;;; When a function name is redefined, and we were tracing that name, then
186     ;;; untrace the old definition and trace the new one.
187 chiles 1.1 ;;;
188 ram 1.8 (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 chiles 1.1 ;;;
196 ram 1.8 (push #'trace-redefined-update ext:*setf-fdefinition-hook*)
197 chiles 1.1
198 ram 1.8
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 wlott 1.10 (format t "~@V,0T~D: "
244 ram 1.8 (+ (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 rtoy 1.30 (let ((frame-name (di:debug-function-name (di:frame-debug-function frame))))
256     (when (member frame-name names :test #'equal)
257     (return t)))))
258 ram 1.8
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 wlott 1.10 (kernel:*current-level* 0)
324 ram 1.8 (*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 cracauer 1.19 (print-frame-call frame :verbosity 1))
332 ram 1.8 (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 chiles 1.1 ;;;
349 ram 1.8 (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 wlott 1.10 (kernel:*current-level* 0)
364 ram 1.8 (*standard-output* *trace-output*)
365     (*in-trace* t))
366 wlott 1.10 (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 ram 1.8 (trace-maybe-break info (trace-info-break-after info)
378     "after" frame)))))
379    
380    
381 gerd 1.25 ;;; TRACE-FWRAPPER -- Internal
382 chiles 1.2 ;;;
383 ram 1.8 ;;; 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 chiles 1.2 ;;;
387 gerd 1.25 (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 gerd 1.23 (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 gerd 1.25 (let ((vals (multiple-value-list (call-next-function))))
402 gerd 1.23 (funcall (trace-end-breakpoint-fun info) frame nil vals nil)
403     (values-list vals))))))))
404 chiles 1.1
405 ram 1.8
406 chiles 1.2 ;;; TRACE-1 -- Internal.
407 chiles 1.1 ;;;
408 ram 1.8 ;;; 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 chiles 1.1 ;;;
412 ram 1.8 ;;; 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 gerd 1.22 (multiple-value-bind (fun named kind)
417 ram 1.8 (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 ram 1.7 (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 gerd 1.22 (encapsulated
427 ram 1.8 (if (eq (trace-info-encapsulated info) :default)
428 gerd 1.23 (let ((encapsulate-p
429 gerd 1.28 (or (not (di::can-set-function-end-breakpoint-p debug-fun))
430 gerd 1.23 (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 ram 1.8 (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 chiles 1.1
460 ram 1.8 (dolist (wherein (trace-info-wherein info))
461 rtoy 1.30 (multiple-value-bind (validp block-name)
462     (ext:valid-function-name-p wherein)
463     (unless (or (stringp block-name)
464     (fboundp block-name))
465 ram 1.8 (warn ":WHEREIN name is not a defined global function: ~S"
466 rtoy 1.30 wherein))))
467 ram 1.8
468     (cond
469     (encapsulated
470     (unless named
471     (error "Can't use encapsulation to trace anonymous function ~S."
472     fun))
473 rtoy 1.34 (when (listp fun)
474     (error "Can't use encapsulation to trace local flet/labels function ~S."
475     fun))
476 gerd 1.25 (fwrap function-or-name #'trace-fwrapper :type 'trace
477     :user-data info))
478 ram 1.8 (t
479     (multiple-value-bind
480     (start-fun cookie-fun)
481     (trace-start-breakpoint-fun info)
482     (let ((start (di:make-breakpoint start-fun debug-fun
483     :kind :function-start))
484     (end (di:make-breakpoint
485     (trace-end-breakpoint-fun info)
486     debug-fun :kind :function-end
487     :function-end-cookie cookie-fun)))
488     (setf (trace-info-start-breakpoint info) start)
489     (setf (trace-info-end-breakpoint info) end)
490     ;;
491     ;; The next two forms must be in the order in which they appear,
492     ;; since the start breakpoint must run before the function-end
493     ;; breakpoint's start helper (which calls the cookie function.)
494     ;; One reason is that cookie function requires that the CONDITIONP
495     ;; shared closure variable be initialized.
496     (di:activate-breakpoint start)
497     (di:activate-breakpoint end)))))
498    
499     (setf (gethash fun *traced-functions*) info)))
500    
501     function-or-name)
502 gerd 1.23
503     ;;;
504     ;;; Return true if FUNCTION-OR-NAME's package indicates that TRACE
505     ;;; should use encapsulation instead of function-end breakpoints.
506     ;;;
507     (defun encapsulate-by-package-p (function-or-name)
508     (multiple-value-bind (valid block)
509     (valid-function-name-p function-or-name)
510     (when (and valid (symbolp block))
511     (let* ((pkg (symbol-package block))
512     (pkg-name (and pkg (package-name pkg))))
513     (member pkg-name *trace-encapsulate-package-names* :test #'equal)))))
514 ram 1.8
515    
516     ;;;; The TRACE macro:
517    
518     ;;; PARSE-TRACE-OPTIONS -- Internal
519 chiles 1.1 ;;;
520 ram 1.8 ;;; Parse leading trace options off of Specs, modifying Info accordingly.
521     ;;; The remaining portion of the list is returned when we encounter a plausible
522     ;;; function name.
523 chiles 1.1 ;;;
524 ram 1.8 (defun parse-trace-options (specs info)
525     (let ((current specs))
526     (loop
527     (when (endp current) (return))
528     (let ((option (first current))
529     (value (cons (second current) nil)))
530     (case option
531     (:condition (setf (trace-info-condition info) value))
532     (:condition-after
533     (setf (trace-info-condition info) (cons nil nil))
534     (setf (trace-info-condition-after info) value))
535     (:condition-all
536     (setf (trace-info-condition info) value)
537     (setf (trace-info-condition-after info) value))
538     (:wherein
539 rtoy 1.30 (collect ((new-names))
540     (dolist (name (if (listp (car value)) (car value) value))
541     (cond ((and (consp name) (eq (car name) 'method)
542     (ext:valid-function-name-p name))
543     ;; This needs to be coordinated with how the
544     ;; debugger prints method names. So this is
545     ;; what this code does. Any method qualifiers
546     ;; appear as a list in the debugger. No
547     ;; qualifiers show up as NIL. We also take the
548     ;; method and add a pcl::fast-method in case the
549     ;; method wasn't compiled. (Do we need to do this?)
550     (let ((method (cond ((atom (third name))
551     `(,(second name) (,(third name)) ,@(cdddr name)))
552     (t
553     `(,(second name) nil ,@(cddr name))))))
554     (new-names `(pcl::fast-method ,@method))
555     (new-names `(method ,@method))))
556     (t
557     (new-names name))))
558     (setf (trace-info-wherein info) (new-names))))
559 ram 1.8 (:encapsulate
560     (setf (trace-info-encapsulated info) (car value)))
561     (:break (setf (trace-info-break info) value))
562     (:break-after (setf (trace-info-break-after info) value))
563     (:break-all
564     (setf (trace-info-break info) value)
565     (setf (trace-info-break-after info) value))
566     (:print
567     (setf (trace-info-print info)
568     (append (trace-info-print info) (list value))))
569     (:print-after
570     (setf (trace-info-print-after info)
571     (append (trace-info-print-after info) (list value))))
572     (:print-all
573     (setf (trace-info-print info)
574     (append (trace-info-print info) (list value)))
575     (setf (trace-info-print-after info)
576     (append (trace-info-print-after info) (list value))))
577     (t (return)))
578     (pop current)
579     (unless current
580     (error "Missing argument to ~S TRACE option." option))
581     (pop current)))
582     current))
583 chiles 1.1
584 ram 1.8
585     ;;; EXPAND-TRACE -- Internal
586 chiles 1.1 ;;;
587 ram 1.8 ;;; Compute the expansion of TRACE in the non-trivial case (arguments
588     ;;; specified.) If there are no :FUNCTION specs, then don't use a LET. This
589     ;;; allows TRACE to be used without the full interpreter.
590 chiles 1.1 ;;;
591 ram 1.8 (defun expand-trace (specs)
592     (collect ((binds)
593     (forms))
594     (let* ((global-options (make-trace-info))
595     (current (parse-trace-options specs global-options)))
596     (loop
597     (when (endp current) (return))
598     (let ((name (pop current))
599     (options (copy-trace-info global-options)))
600     (cond
601     ((eq name :function)
602     (let ((temp (gensym)))
603     (binds `(,temp ,(pop current)))
604     (forms `(trace-1 ,temp ',options))))
605 gerd 1.20 ;;
606     ;; Generic function -> trace all method functions.
607     ((eq name :methods)
608     (let ((tem (gensym)))
609     (binds `(,tem ,(pop current)))
610     (forms `(dolist (name (all-method-function-names ,tem))
611     (when (fboundp name)
612     (trace-1 name ',options))))))
613 ram 1.8 ((and (keywordp name)
614     (not (or (fboundp name) (macro-function name))))
615     (error "Unknown TRACE option: ~S" name))
616 gerd 1.20 ;;
617     ;; Method name -> trace method functions.
618     ((and (consp name) (eq (car name) 'method))
619     (when (fboundp name)
620     (forms `(trace-1 ',name ',options)))
621     (let ((name `(pcl::fast-method ,@(cdr name))))
622     (when (fboundp name)
623     (forms `(trace-1 ',name ',options)))))
624 ram 1.8 (t
625     (forms `(trace-1 ',name ',options))))
626     (setq current (parse-trace-options current options)))))
627 chiles 1.1
628 ram 1.8 (if (binds)
629     `(let ,(binds) (list ,@(forms)))
630     `(list ,@(forms)))))
631 chiles 1.1
632 ram 1.8
633     ;;; %LIST-TRACED-FUNCTIONS -- Internal
634 chiles 1.2 ;;;
635 ram 1.8 (defun %list-traced-functions ()
636     (loop for x being each hash-value in *traced-functions*
637     collect (trace-info-what x)))
638    
639    
640     ;;; TRACE -- Public.
641 chiles 1.2 ;;;
642 ram 1.8 (defmacro trace (&rest specs)
643     "TRACE {Option Global-Value}* {Name {Option Value}*}*
644     TRACE is a debugging tool that prints information when specified functions
645     are called. In its simplest form:
646     (trace Name-1 Name-2 ...)
647 chiles 1.1
648 gerd 1.20 CLOS methods can be traced by specifying a name of the form
649     (METHOD {Qualifier}* ({Specializer}*)).
650    
651 ram 1.8 TRACE causes a printout on *TRACE-OUTPUT* each time that one of the named
652     functions is entered or returns (the Names are not evaluated.) The output
653     is indented according to the number of pending traced calls, and this trace
654     depth is printed at the beginning of each line of output.
655 chiles 1.2
656 ram 1.8 Options allow modification of the default behavior. Each option is a pair
657     of an option keyword and a value form. Options may be interspersed with
658     function names. Options only affect tracing of the function whose name they
659     appear immediately after. Global options are specified before the first
660     name, and affect all functions traced by a given use of TRACE.
661    
662     The following options are defined:
663    
664     :CONDITION Form
665     :CONDITION-AFTER Form
666     :CONDITION-ALL Form
667     If :CONDITION is specified, then TRACE does nothing unless Form
668     evaluates to true at the time of the call. :CONDITION-AFTER is
669     similar, but suppresses the initial printout, and is tested when the
670     function returns. :CONDITION-ALL tries both before and after.
671 rtoy 1.29
672 ram 1.8 :WHEREIN Names
673     If specified, Names is a function name or list of names. TRACE does
674     nothing unless a call to one of those functions encloses the call to
675     this function (i.e. it would appear in a backtrace.) Anonymous
676     functions have string names like \"DEFUN FOO\".
677    
678     :BREAK Form
679     :BREAK-AFTER Form
680     :BREAK-ALL Form
681     If specified, and Form evaluates to true, then the debugger is invoked
682     at the start of the function, at the end of the function, or both,
683     according to the respective option.
684    
685     :PRINT Form
686     :PRINT-AFTER Form
687     :PRINT-ALL Form
688 toy 1.18 In addition to the usual printout, the result of evaluating FORM is
689 ram 1.8 printed at the start of the function, at the end of the function, or
690     both, according to the respective option. Multiple print options cause
691     multiple values to be printed.
692    
693     :FUNCTION Function-Form
694     This is a not really an option, but rather another way of specifying
695     what function to trace. The Function-Form is evaluated immediately,
696     and the resulting function is traced.
697    
698 gerd 1.20 :METHODS Function-Form
699     This is a not really an option, but rather a way of specifying
700 emarsden 1.21 that all methods of a generic functions should be traced. The
701 gerd 1.20 Function-Form is evaluated immediately, and the methods of the resulting
702     generic function are traced.
703    
704 ram 1.8 :ENCAPSULATE {:DEFAULT | T | NIL}
705     If T, the tracing is done via encapsulation (redefining the function
706     name) rather than by modifying the function. :DEFAULT is the default,
707     and means to use encapsulation for interpreted functions and funcallable
708     instances, breakpoints otherwise. When encapsulation is used, forms are
709     *not* evaluated in the function's lexical environment, but DEBUG:ARG can
710     still be used.
711    
712     :CONDITION, :BREAK and :PRINT forms are evaluated in the lexical environment
713     of the called function; DEBUG:VAR and DEBUG:ARG can be used. The -AFTER and
714     -ALL forms are evaluated in the null environment."
715     (if specs
716     (expand-trace specs)
717     '(%list-traced-functions)))
718    
719 chiles 1.1
720 ram 1.8 ;;;; Untracing:
721 chiles 1.1
722 ram 1.8 ;;; UNTRACE-1 -- Internal
723     ;;;
724     ;;; Untrace one function.
725     ;;;
726     (defun untrace-1 (function-or-name)
727     (let* ((fun (trace-fdefinition function-or-name))
728     (info (gethash fun *traced-functions*)))
729 gerd 1.24 (cond ((not info)
730     (warn "Function is not TRACE'd -- ~S." function-or-name))
731     (t
732     (cond ((trace-info-encapsulated info)
733 gerd 1.25 (funwrap (trace-info-what info) :type 'trace))
734 gerd 1.24 (t
735     (di:delete-breakpoint (trace-info-start-breakpoint info))
736     (di:delete-breakpoint (trace-info-end-breakpoint info))))
737     (setf (trace-info-untraced info) t)
738     (remhash fun *traced-functions*)))))
739 ram 1.8
740     ;;; UNTRACE-ALL -- Internal
741     ;;;
742     ;;; Untrace all traced functions.
743     ;;;
744     (defun untrace-all ()
745     (dolist (fun (%list-traced-functions))
746     (untrace-1 fun))
747     t)
748    
749 chiles 1.4 (defmacro untrace (&rest specs)
750     "Removes tracing from the specified functions. With no args, untraces all
751 chiles 1.1 functions."
752 ram 1.8 (if specs
753     (collect ((res))
754     (let ((current specs))
755     (loop
756     (unless current (return))
757     (let ((name (pop current)))
758 gerd 1.20 (cond ((eq name :function)
759     (res `(untrace-1 ,(pop current))))
760     ;;
761     ;; Method name -> untrace existing method functions.
762     ((and (consp name)
763     (eq (car name) 'method))
764     (when (fboundp name)
765     (res `(untrace-1 ',name)))
766     (let ((name `(pcl::fast-method ,@(cdr name))))
767     (when (fboundp name)
768     (res `(untrace-1 ',name)))))
769     ;;
770     ;; Generic function -> untrace all method functions.
771     ((eq name :methods)
772     (res
773     `(dolist (name (all-method-function-names ,(pop current)))
774     (when (fboundp name)
775     (untrace-1 name)))))
776     (t
777     (res `(untrace-1 ',name))))))
778 ram 1.8 `(progn ,@(res) t)))
779     '(untrace-all)))

  ViewVC Help
Powered by ViewVC 1.1.5