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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5