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

Contents of /src/code/ntrace.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (hide annotations)
Wed Jun 18 09:23:11 2003 UTC (10 years, 10 months ago) by gerd
Branch: MAIN
Changes since 1.26: +3 -2 lines
	Remove package nicknames USER from COMMON-LISP-USER.  Add a new
	package COMMON-LISP which LISP uses, so that COMMON-LISP no longer
	has the non-ANSI nickname LISP.

	To bootstrap, use boot13.lisp as target:bootstrap.lisp with pmai's
	build scripts, and do a full compile.

	* src/bootfiles/18e/boot13.lisp: Change for all the package
	changes.

	* src/code/exports.lisp: New package common-lisp,
	which lisp uses.

	* src/tools/worldload.lisp:
	* src/tools/setup.lisp: Use cl-user instead of user.
	Use lisp:: instead of cl::.

	* src/tools/worldcom.lisp:
	* src/tools/snapshot-update.lisp:
	* src/tools/pclcom.lisp:
	* src/tools/mk-lisp:
	* src/tools/hemcom.lisp:
	* src/tools/config.lisp:
	* src/tools/comcom.lisp:
	* src/tools/clxcom.lisp:
	* src/tools/clmcom.lisp:
	* src/pcl/defsys.lisp:
	* src/motif/lisp/initial.lisp:
	* src/interface/initial.lisp:
	* src/hemlock/lispmode.lisp (setup-lisp-mode):
	Use cl-user instead of user.

	* src/code/save.lisp (assert-user-package):
	* src/code/print.lisp (%with-standard-io-syntax): Find
	cl-user package instead of user.

	* src/code/package.lisp (package-locks-init): Add lisp.
	(package-init): Don't add user nickname to cl-user.

	* src/code/ntrace.lisp (*trace-encapsulate-package-names*):
	Add common-lisp.

	* src/code/hash.lisp (toplevel):
	* src/code/hash-new.lisp (toplevel): Use in-package :lisp
	instead of :common-lisp.

	* src/code/float-trap.lisp (sigfpe-handler): Don't
	qualify floating-point-inexact with ext:.

	* src/pcl/simple-streams/strategy.lisp (sc):
	* src/pcl/simple-streams/null.lisp (null-read-char):
	* src/pcl/simple-streams/internal.lisp (allocate-buffer)
	(free-buffer):
	* src/pcl/simple-streams/impl.lisp (%check, %read-line)
	(%peek-char, %read-byte):
	* src/pcl/simple-streams/file.lisp (open-file-stream)
	(device-close):
	* src/pcl/simple-streams/classes.lisp (simple-stream)
	(device-close):
	* src/pcl/macros.lisp (toplevel):
	* src/pcl/braid.lisp (lisp::sxhash-instance):
	* src/pcl/env.lisp (toplevel):
	* src/compiler/generic/objdef.lisp (symbol-hash):
	* src/code/stream.lisp (read-sequence, write-sequence):
	* src/code/macros.lisp (defmacro, deftype):
	* src/code/eval.lisp (interpreted-function):
	* src/code/defstruct.lisp (defstruct):
	* src/code/debug.lisp (debug-eval-print): Use lisp:: instead
	of cl::.
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.27 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/ntrace.lisp,v 1.27 2003/06/18 09:23:11 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 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     (when (member (di:debug-function-name (di:frame-debug-function frame))
252     names :test #'equal)
253     (return t))))
254    
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     (or (di::can-set-function-end-breakpoint-p debug-fun)
426     (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     (unless (or (stringp wherein)
458     (fboundp wherein))
459     (warn ":WHEREIN name is not a defined global function: ~S"
460     wherein)))
461    
462     (cond
463     (encapsulated
464     (unless named
465     (error "Can't use encapsulation to trace anonymous function ~S."
466     fun))
467 gerd 1.25 (fwrap function-or-name #'trace-fwrapper :type 'trace
468     :user-data info))
469 ram 1.8 (t
470     (multiple-value-bind
471     (start-fun cookie-fun)
472     (trace-start-breakpoint-fun info)
473     (let ((start (di:make-breakpoint start-fun debug-fun
474     :kind :function-start))
475     (end (di:make-breakpoint
476     (trace-end-breakpoint-fun info)
477     debug-fun :kind :function-end
478     :function-end-cookie cookie-fun)))
479     (setf (trace-info-start-breakpoint info) start)
480     (setf (trace-info-end-breakpoint info) end)
481     ;;
482     ;; The next two forms must be in the order in which they appear,
483     ;; since the start breakpoint must run before the function-end
484     ;; breakpoint's start helper (which calls the cookie function.)
485     ;; One reason is that cookie function requires that the CONDITIONP
486     ;; shared closure variable be initialized.
487     (di:activate-breakpoint start)
488     (di:activate-breakpoint end)))))
489    
490     (setf (gethash fun *traced-functions*) info)))
491    
492     function-or-name)
493 gerd 1.23
494     ;;;
495     ;;; Return true if FUNCTION-OR-NAME's package indicates that TRACE
496     ;;; should use encapsulation instead of function-end breakpoints.
497     ;;;
498     (defun encapsulate-by-package-p (function-or-name)
499     (multiple-value-bind (valid block)
500     (valid-function-name-p function-or-name)
501     (when (and valid (symbolp block))
502     (let* ((pkg (symbol-package block))
503     (pkg-name (and pkg (package-name pkg))))
504     (member pkg-name *trace-encapsulate-package-names* :test #'equal)))))
505 ram 1.8
506    
507     ;;;; The TRACE macro:
508    
509     ;;; PARSE-TRACE-OPTIONS -- Internal
510 chiles 1.1 ;;;
511 ram 1.8 ;;; Parse leading trace options off of Specs, modifying Info accordingly.
512     ;;; The remaining portion of the list is returned when we encounter a plausible
513     ;;; function name.
514 chiles 1.1 ;;;
515 ram 1.8 (defun parse-trace-options (specs info)
516     (let ((current specs))
517     (loop
518     (when (endp current) (return))
519     (let ((option (first current))
520     (value (cons (second current) nil)))
521     (case option
522     (:condition (setf (trace-info-condition info) value))
523     (:condition-after
524     (setf (trace-info-condition info) (cons nil nil))
525     (setf (trace-info-condition-after info) value))
526     (:condition-all
527     (setf (trace-info-condition info) value)
528     (setf (trace-info-condition-after info) value))
529     (:wherein
530     (setf (trace-info-wherein info)
531     (if (listp (car value)) (car value) value)))
532     (:encapsulate
533     (setf (trace-info-encapsulated info) (car value)))
534     (:break (setf (trace-info-break info) value))
535     (:break-after (setf (trace-info-break-after info) value))
536     (:break-all
537     (setf (trace-info-break info) value)
538     (setf (trace-info-break-after info) value))
539     (:print
540     (setf (trace-info-print info)
541     (append (trace-info-print info) (list value))))
542     (:print-after
543     (setf (trace-info-print-after info)
544     (append (trace-info-print-after info) (list value))))
545     (:print-all
546     (setf (trace-info-print info)
547     (append (trace-info-print info) (list value)))
548     (setf (trace-info-print-after info)
549     (append (trace-info-print-after info) (list value))))
550     (t (return)))
551     (pop current)
552     (unless current
553     (error "Missing argument to ~S TRACE option." option))
554     (pop current)))
555     current))
556 chiles 1.1
557 ram 1.8
558     ;;; EXPAND-TRACE -- Internal
559 chiles 1.1 ;;;
560 ram 1.8 ;;; Compute the expansion of TRACE in the non-trivial case (arguments
561     ;;; specified.) If there are no :FUNCTION specs, then don't use a LET. This
562     ;;; allows TRACE to be used without the full interpreter.
563 chiles 1.1 ;;;
564 ram 1.8 (defun expand-trace (specs)
565     (collect ((binds)
566     (forms))
567     (let* ((global-options (make-trace-info))
568     (current (parse-trace-options specs global-options)))
569     (loop
570     (when (endp current) (return))
571     (let ((name (pop current))
572     (options (copy-trace-info global-options)))
573     (cond
574     ((eq name :function)
575     (let ((temp (gensym)))
576     (binds `(,temp ,(pop current)))
577     (forms `(trace-1 ,temp ',options))))
578 gerd 1.20 ;;
579     ;; Generic function -> trace all method functions.
580     ((eq name :methods)
581     (let ((tem (gensym)))
582     (binds `(,tem ,(pop current)))
583     (forms `(dolist (name (all-method-function-names ,tem))
584     (when (fboundp name)
585     (trace-1 name ',options))))))
586 ram 1.8 ((and (keywordp name)
587     (not (or (fboundp name) (macro-function name))))
588     (error "Unknown TRACE option: ~S" name))
589 gerd 1.20 ;;
590     ;; Method name -> trace method functions.
591     ((and (consp name) (eq (car name) 'method))
592     (when (fboundp name)
593     (forms `(trace-1 ',name ',options)))
594     (let ((name `(pcl::fast-method ,@(cdr name))))
595     (when (fboundp name)
596     (forms `(trace-1 ',name ',options)))))
597 ram 1.8 (t
598     (forms `(trace-1 ',name ',options))))
599     (setq current (parse-trace-options current options)))))
600 chiles 1.1
601 ram 1.8 (if (binds)
602     `(let ,(binds) (list ,@(forms)))
603     `(list ,@(forms)))))
604 chiles 1.1
605 ram 1.8
606     ;;; %LIST-TRACED-FUNCTIONS -- Internal
607 chiles 1.2 ;;;
608 ram 1.8 (defun %list-traced-functions ()
609     (loop for x being each hash-value in *traced-functions*
610     collect (trace-info-what x)))
611    
612    
613     ;;; TRACE -- Public.
614 chiles 1.2 ;;;
615 ram 1.8 (defmacro trace (&rest specs)
616     "TRACE {Option Global-Value}* {Name {Option Value}*}*
617     TRACE is a debugging tool that prints information when specified functions
618     are called. In its simplest form:
619     (trace Name-1 Name-2 ...)
620 chiles 1.1
621 gerd 1.20 CLOS methods can be traced by specifying a name of the form
622     (METHOD {Qualifier}* ({Specializer}*)).
623    
624 ram 1.8 TRACE causes a printout on *TRACE-OUTPUT* each time that one of the named
625     functions is entered or returns (the Names are not evaluated.) The output
626     is indented according to the number of pending traced calls, and this trace
627     depth is printed at the beginning of each line of output.
628 chiles 1.2
629 ram 1.8 Options allow modification of the default behavior. Each option is a pair
630     of an option keyword and a value form. Options may be interspersed with
631     function names. Options only affect tracing of the function whose name they
632     appear immediately after. Global options are specified before the first
633     name, and affect all functions traced by a given use of TRACE.
634    
635     The following options are defined:
636    
637     :CONDITION Form
638     :CONDITION-AFTER Form
639     :CONDITION-ALL Form
640     If :CONDITION is specified, then TRACE does nothing unless Form
641     evaluates to true at the time of the call. :CONDITION-AFTER is
642     similar, but suppresses the initial printout, and is tested when the
643     function returns. :CONDITION-ALL tries both before and after.
644 gerd 1.25 p
645 ram 1.8 :WHEREIN Names
646     If specified, Names is a function name or list of names. TRACE does
647     nothing unless a call to one of those functions encloses the call to
648     this function (i.e. it would appear in a backtrace.) Anonymous
649     functions have string names like \"DEFUN FOO\".
650    
651     :BREAK Form
652     :BREAK-AFTER Form
653     :BREAK-ALL Form
654     If specified, and Form evaluates to true, then the debugger is invoked
655     at the start of the function, at the end of the function, or both,
656     according to the respective option.
657    
658     :PRINT Form
659     :PRINT-AFTER Form
660     :PRINT-ALL Form
661 toy 1.18 In addition to the usual printout, the result of evaluating FORM is
662 ram 1.8 printed at the start of the function, at the end of the function, or
663     both, according to the respective option. Multiple print options cause
664     multiple values to be printed.
665    
666     :FUNCTION Function-Form
667     This is a not really an option, but rather another way of specifying
668     what function to trace. The Function-Form is evaluated immediately,
669     and the resulting function is traced.
670    
671 gerd 1.20 :METHODS Function-Form
672     This is a not really an option, but rather a way of specifying
673 emarsden 1.21 that all methods of a generic functions should be traced. The
674 gerd 1.20 Function-Form is evaluated immediately, and the methods of the resulting
675     generic function are traced.
676    
677 ram 1.8 :ENCAPSULATE {:DEFAULT | T | NIL}
678     If T, the tracing is done via encapsulation (redefining the function
679     name) rather than by modifying the function. :DEFAULT is the default,
680     and means to use encapsulation for interpreted functions and funcallable
681     instances, breakpoints otherwise. When encapsulation is used, forms are
682     *not* evaluated in the function's lexical environment, but DEBUG:ARG can
683     still be used.
684    
685     :CONDITION, :BREAK and :PRINT forms are evaluated in the lexical environment
686     of the called function; DEBUG:VAR and DEBUG:ARG can be used. The -AFTER and
687     -ALL forms are evaluated in the null environment."
688     (if specs
689     (expand-trace specs)
690     '(%list-traced-functions)))
691    
692 chiles 1.1
693 ram 1.8 ;;;; Untracing:
694 chiles 1.1
695 ram 1.8 ;;; UNTRACE-1 -- Internal
696     ;;;
697     ;;; Untrace one function.
698     ;;;
699     (defun untrace-1 (function-or-name)
700     (let* ((fun (trace-fdefinition function-or-name))
701     (info (gethash fun *traced-functions*)))
702 gerd 1.24 (cond ((not info)
703     (warn "Function is not TRACE'd -- ~S." function-or-name))
704     (t
705     (cond ((trace-info-encapsulated info)
706 gerd 1.25 (funwrap (trace-info-what info) :type 'trace))
707 gerd 1.24 (t
708     (di:delete-breakpoint (trace-info-start-breakpoint info))
709     (di:delete-breakpoint (trace-info-end-breakpoint info))))
710     (setf (trace-info-untraced info) t)
711     (remhash fun *traced-functions*)))))
712 ram 1.8
713     ;;; UNTRACE-ALL -- Internal
714     ;;;
715     ;;; Untrace all traced functions.
716     ;;;
717     (defun untrace-all ()
718     (dolist (fun (%list-traced-functions))
719     (untrace-1 fun))
720     t)
721    
722 chiles 1.4 (defmacro untrace (&rest specs)
723     "Removes tracing from the specified functions. With no args, untraces all
724 chiles 1.1 functions."
725 ram 1.8 (if specs
726     (collect ((res))
727     (let ((current specs))
728     (loop
729     (unless current (return))
730     (let ((name (pop current)))
731 gerd 1.20 (cond ((eq name :function)
732     (res `(untrace-1 ,(pop current))))
733     ;;
734     ;; Method name -> untrace existing method functions.
735     ((and (consp name)
736     (eq (car name) 'method))
737     (when (fboundp name)
738     (res `(untrace-1 ',name)))
739     (let ((name `(pcl::fast-method ,@(cdr name))))
740     (when (fboundp name)
741     (res `(untrace-1 ',name)))))
742     ;;
743     ;; Generic function -> untrace all method functions.
744     ((eq name :methods)
745     (res
746     `(dolist (name (all-method-function-names ,(pop current)))
747     (when (fboundp name)
748     (untrace-1 name)))))
749     (t
750     (res `(untrace-1 ',name))))))
751 ram 1.8 `(progn ,@(res) t)))
752     '(untrace-all)))

  ViewVC Help
Powered by ViewVC 1.1.5