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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (hide annotations)
Fri May 23 13:34:04 2003 UTC (10 years, 10 months ago) by gerd
Branch: MAIN
Changes since 1.24: +17 -15 lines
	Add fwrappers.  Rewrite TRACE and PROFILE to use fwrappers.
	To bootstrap, use boot12.lisp with pmai's scripts.

	* src/bootfiles/18e/boot12.lisp: New file.

	* src/tools/worldload.lisp, src/tools/worldcom.lisp:
	* src/tools/worldbuild.lisp: Add code:fwrappers.

	* src/docs/cmu-user/extensions.tex (Function Wrappers):
	New section.

	* src/code/fwrappers.lisp: New file.

	* src-fw/pcl/defs.lisp (gdefinition): Don't check for profiled
	functions.

	* src-fw/code/exports.lisp: Add walker and fwrappers.

	* src-fw/code/fdefinition.lisp (fdefn-init): Set
	*valid-function-names* to nil.
	(encapsulation): Structure removed.
	(do-encapsulations, encapsulation, last-encapsulation)
	(push-encapsulation, encapsulate, unencapsulate, encapsulated-p):
	Removed.  There is a compatibility layer in fwrappers.lisp.
	(fdefinition, %set-fdefinition): Rewritten.

	* src/code/profile.lisp: Remove #+cmu and #-cmu.
	("PROFILE"): Use fwrappers.
	(*profile-info*): Removed.
	(profile-info): Redefined.
	(*existing-encapsulations*): Removed.
	(reset-profile-info, profile-info-profiling-values): New functions.
	(make-profile-encapsulation): Removed.
	(profile): New function name syntax.
	(make-profile-fwrapper-name, make-profile-fwrapper): New functions.
	(def-profile-fwrapper): Precompute some fwrappers.
	(ensure-profile-fwrapper, find-profile-fwrapper, pi-or-lose):
	New functions.
	(profile-1-function): Rewritten.
	(unprofile-1-function): Ditto.
	(re-profile-redefined-function): New function.
	(toplevel): Push it on *setf-fdefinition-hook*.

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

  ViewVC Help
Powered by ViewVC 1.1.5