1 ;;;; a tracing facility
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB-DEBUG") ; (SB-, not SB!, since we're built in warm load.)
14 ;;; FIXME: Why, oh why, doesn't the SB-DEBUG package use the SB-DI
15 ;;; package? That would let us get rid of a whole lot of stupid
18 (defvar *trace-values
* nil
20 "This is bound to the returned values when evaluating :BREAK-AFTER and
23 (defvar *trace-indentation-step
* 2
25 "the increase in trace indentation at each call level")
27 (defvar *max-trace-indentation
* 40
29 "If the trace indentation exceeds this value, then indentation restarts at
32 (defvar *trace-encapsulate-default
* t
34 "the default value for the :ENCAPSULATE option to TRACE")
38 ;;; a hash table that maps each traced function to the TRACE-INFO. The
39 ;;; entry for a closure is the shared function entry object.
40 (defvar *traced-funs
* (make-hash-table :test
'eq
))
42 ;;; A TRACE-INFO object represents all the information we need to
43 ;;; trace a given function.
44 (def!struct
(trace-info
45 (:make-load-form-fun sb-kernel
:just-dump-it-normally
)
46 (:print-object
(lambda (x stream
)
47 (print-unreadable-object (x stream
:type t
)
48 (prin1 (trace-info-what x
) stream
)))))
49 ;; the original representation of the thing traced
50 (what nil
:type
(or function cons symbol
))
51 ;; Is WHAT a function name whose definition we should track?
53 ;; Is tracing to be done by encapsulation rather than breakpoints?
55 (encapsulated *trace-encapsulate-default
*)
56 ;; Has this trace been untraced?
58 ;; breakpoints we set up to trigger tracing
59 (start-breakpoint nil
:type
(or sb-di
:breakpoint null
))
60 (end-breakpoint nil
:type
(or sb-di
:breakpoint null
))
61 ;; the list of function names for WHEREIN, or NIL if unspecified
62 (wherein nil
:type list
)
64 ;; The following slots represent the forms that we are supposed to
65 ;; evaluate on each iteration. Each form is represented by a cons
66 ;; (Form . Function), where the Function is the cached result of
67 ;; coercing Form to a function. Forms which use the current
68 ;; environment are converted with PREPROCESS-FOR-EVAL, which gives
69 ;; us a one-arg function. Null environment forms also have one-arg
70 ;; functions, but the argument is ignored. NIL means unspecified
73 ;; current environment forms
76 ;; List of current environment forms
78 ;; null environment forms
81 ;; list of null environment forms
82 (print-after () :type list
))
84 ;;; This is a list of conses (fun-end-cookie . condition-satisfied),
85 ;;; which we use to note distinct dynamic entries into functions. When
86 ;;; we enter a traced function, we add a entry to this list holding
87 ;;; the new end-cookie and whether the trace condition was satisfied.
88 ;;; We must save the trace condition so that the after breakpoint
89 ;;; knows whether to print. The length of this list tells us the
90 ;;; indentation to use for printing TRACE messages.
92 ;;; This list also helps us synchronize the TRACE facility dynamically
93 ;;; for detecting non-local flow of control. Whenever execution hits a
94 ;;; :FUN-END breakpoint used for TRACE'ing, we look for the
95 ;;; FUN-END-COOKIE at the top of *TRACED-ENTRIES*. If it is not
96 ;;; there, we discard any entries that come before our cookie.
98 ;;; When we trace using encapsulation, we bind this variable and add
99 ;;; (NIL . CONDITION-SATISFIED), so a NIL "cookie" marks an
100 ;;; encapsulated tracing.
101 (defvar *traced-entries
* ())
102 (declaim (list *traced-entries
*))
104 ;;; This variable is used to discourage infinite recursions when some
105 ;;; trace action invokes a function that is itself traced. In this
106 ;;; case, we quietly ignore the inner tracing.
107 (defvar *in-trace
* nil
)
111 ;;; Given a function name, a function or a macro name, return the raw
112 ;;; definition and some information. "Raw" means that if the result is
113 ;;; a closure, we strip off the closure and return the bare code. The
114 ;;; second value is T if the argument was a function name. The third
115 ;;; value is one of :COMPILED, :COMPILED-CLOSURE, :INTERPRETED,
116 ;;; :INTERPRETED-CLOSURE and :FUNCALLABLE-INSTANCE.
117 (defun trace-fdefinition (x)
118 (multiple-value-bind (res named-p
)
121 (cond ((special-operator-p x
)
122 (error "can't trace special form ~S" x
))
125 (values (fdefinition x
) t
))))
127 (t (values (fdefinition x
) t
)))
128 (case (sb-kernel:widetag-of res
)
129 (#.sb-vm
:closure-header-widetag
130 (values (sb-kernel:%closure-fun res
)
133 (#.sb-vm
:funcallable-instance-header-widetag
134 (values res named-p
:funcallable-instance
))
135 (t (values res named-p
:compiled
)))))
137 ;;; When a function name is redefined, and we were tracing that name,
138 ;;; then untrace the old definition and trace the new one.
139 (defun trace-redefined-update (fname new-value
)
140 (when (fboundp fname
)
141 (let* ((fun (trace-fdefinition fname
))
142 (info (gethash fun
*traced-funs
*)))
143 (when (and info
(trace-info-named info
))
145 (trace-1 fname info new-value
)))))
146 (push #'trace-redefined-update
*setf-fdefinition-hook
*)
148 ;;; Annotate a FORM to evaluate with pre-converted functions. FORM is
149 ;;; really a cons (EXP . FUNCTION). LOC is the code location to use
150 ;;; for the lexical environment. If LOC is NIL, evaluate in the null
151 ;;; environment. If FORM is NIL, just return NIL.
152 (defun coerce-form (form loc
)
154 (let ((exp (car form
)))
155 (if (sb-di:code-location-p loc
)
156 (let ((fun (sb-di:preprocess-for-eval exp loc
)))
157 (declare (type function fun
))
160 (let ((*current-frame
* frame
))
161 (funcall fun frame
)))))
162 (let* ((bod (ecase loc
165 `(flet ((sb-debug:arg
(n)
166 (declare (special arg-list
))
168 (declare (ignorable #'sb-debug
:arg
))
170 (fun (coerce `(lambda () ,bod
) 'function
)))
173 (declare (ignore frame
))
174 (let ((*current-frame
* nil
))
175 (funcall fun
)))))))))
177 (defun coerce-form-list (forms loc
)
178 (mapcar (lambda (x) (coerce-form x loc
)) forms
))
180 ;;; Print indentation according to the number of trace entries.
181 ;;; Entries whose condition was false don't count.
182 (defun print-trace-indentation ()
184 (dolist (entry *traced-entries
*)
185 (when (cdr entry
) (incf depth
)))
188 (+ (mod (* depth
*trace-indentation-step
*)
189 (- *max-trace-indentation
* *trace-indentation-step
*))
190 *trace-indentation-step
*)
193 ;;; Return true if any of the NAMES appears on the stack below FRAME.
194 (defun trace-wherein-p (frame names
)
195 (do ((frame (sb-di:frame-down frame
) (sb-di:frame-down frame
)))
197 (when (member (sb-di:debug-fun-name
(sb-di:frame-debug-fun frame
))
202 ;;; Handle PRINT and PRINT-AFTER options.
203 (defun trace-print (frame forms
)
206 (print-trace-indentation)
207 (format t
"~@<~S ~_= ~S~:>" (car ele
) (funcall (cdr ele
) frame
))
210 ;;; Test a BREAK option, and if true, break.
211 (defun trace-maybe-break (info break where frame
)
212 (when (and break
(funcall (cdr break
) frame
))
213 (sb-di:flush-frames-above frame
)
214 (let ((*stack-top-hint
* frame
))
215 (break "breaking ~A traced call to ~S:"
217 (trace-info-what info
)))))
219 ;;; Discard any invalid cookies on our simulated stack. Encapsulated
220 ;;; entries are always valid, since we bind *TRACED-ENTRIES* in the
222 (defun discard-invalid-entries (frame)
224 (when (or (null *traced-entries
*)
225 (let ((cookie (caar *traced-entries
*)))
227 (sb-di:fun-end-cookie-valid-p frame cookie
))))
229 (pop *traced-entries
*)))
233 ;;; Return a closure that can be used for a function start breakpoint
234 ;;; hook function and a closure that can be used as the FUN-END-COOKIE
235 ;;; function. The first communicates the sense of the
236 ;;; TRACE-INFO-CONDITION to the second via a closure variable.
237 (defun trace-start-breakpoint-fun (info)
242 (declare (ignore bpt
))
243 (discard-invalid-entries frame
)
244 (let ((condition (trace-info-condition info
))
245 (wherein (trace-info-wherein info
)))
247 (and (not *in-trace
*)
249 (funcall (cdr condition
) frame
))
251 (trace-wherein-p frame wherein
)))))
253 (let ((sb-kernel:*current-level-in-print
* 0)
254 (*standard-output
* (make-string-output-stream))
257 (print-trace-indentation)
258 (if (trace-info-encapsulated info
)
259 ;; FIXME: These special variables should be given
260 ;; *FOO*-style names, and probably declared globally
263 (declare (special basic-definition arg-list
))
264 (prin1 `(,(trace-info-what info
) ,@arg-list
)))
265 (print-frame-call frame
))
267 (trace-print frame
(trace-info-print info
))
268 (write-sequence (get-output-stream-string *standard-output
*)
270 (trace-maybe-break info
(trace-info-break info
) "before" frame
)))
272 (lambda (frame cookie
)
273 (declare (ignore frame
))
274 (push (cons cookie conditionp
) *traced-entries
*)))))
276 ;;; This prints a representation of the return values delivered.
277 ;;; First, this checks to see that cookie is at the top of
278 ;;; *TRACED-ENTRIES*; if it is not, then we need to adjust this list
279 ;;; to determine the correct indentation for output. We then check to
280 ;;; see whether the function is still traced and that the condition
281 ;;; succeeded before printing anything.
282 (declaim (ftype (function (trace-info) function
) trace-end-breakpoint-fun
))
283 (defun trace-end-breakpoint-fun (info)
284 (lambda (frame bpt
*trace-values
* cookie
)
285 (declare (ignore bpt
))
286 (unless (eq cookie
(caar *traced-entries
*))
287 (setf *traced-entries
*
288 (member cookie
*traced-entries
* :key
#'car
)))
290 (let ((entry (pop *traced-entries
*)))
291 (when (and (not (trace-info-untraced info
))
293 (let ((cond (trace-info-condition-after info
)))
294 (and cond
(funcall (cdr cond
) frame
)))))
295 (let ((sb-kernel:*current-level-in-print
* 0)
296 (*standard-output
* (make-string-output-stream))
299 (pprint-logical-block (*standard-output
* nil
)
300 (print-trace-indentation)
301 (pprint-indent :current
2)
302 (format t
"~S returned" (trace-info-what info
))
303 (dolist (v *trace-values
*)
305 (pprint-newline :linear
)
308 (trace-print frame
(trace-info-print-after info
))
309 (write-sequence (get-output-stream-string *standard-output
*)
311 (trace-maybe-break info
312 (trace-info-break-after info
)
316 ;;; This function is called by the trace encapsulation. It calls the
317 ;;; breakpoint hook functions with NIL for the breakpoint and cookie,
318 ;;; which we have cleverly contrived to work for our hook functions.
319 (defun trace-call (info)
320 (multiple-value-bind (start cookie
) (trace-start-breakpoint-fun info
)
321 (declare (type function start cookie
))
322 (let ((frame (sb-di:frame-down
(sb-di:top-frame
))))
323 (funcall start frame nil
)
324 (let ((*traced-entries
* *traced-entries
*))
325 (declare (special basic-definition arg-list
))
326 (funcall cookie frame nil
)
329 (apply basic-definition arg-list
))))
330 (funcall (trace-end-breakpoint-fun info
) frame nil vals nil
)
331 (values-list vals
))))))
333 ;;; Trace one function according to the specified options. We copy the
334 ;;; trace info (it was a quoted constant), fill in the functions, and
335 ;;; then install the breakpoints or encapsulation.
337 ;;; If non-null, DEFINITION is the new definition of a function that
338 ;;; we are automatically retracing.
339 (defun trace-1 (function-or-name info
&optional definition
)
340 (multiple-value-bind (fun named kind
)
343 (nth-value 2 (trace-fdefinition definition
)))
344 (trace-fdefinition function-or-name
))
345 (when (gethash fun
*traced-funs
*)
346 (warn "~S is already TRACE'd, untracing it first." function-or-name
)
349 (let* ((debug-fun (sb-di:fun-debug-fun fun
))
351 (if (eq (trace-info-encapsulated info
) :default
)
355 (unless (functionp function-or-name
)
356 (warn "tracing shared code for ~S:~% ~S"
360 ((:interpreted
:interpreted-closure
:funcallable-instance
)
362 (trace-info-encapsulated info
)))
363 (loc (if encapsulated
365 (sb-di:debug-fun-start-location debug-fun
)))
366 (info (make-trace-info
367 :what function-or-name
369 :encapsulated encapsulated
370 :wherein
(trace-info-wherein info
)
371 :condition
(coerce-form (trace-info-condition info
) loc
)
372 :break
(coerce-form (trace-info-break info
) loc
)
373 :print
(coerce-form-list (trace-info-print info
) loc
)
374 :break-after
(coerce-form (trace-info-break-after info
) nil
)
376 (coerce-form (trace-info-condition-after info
) nil
)
378 (coerce-form-list (trace-info-print-after info
) nil
))))
380 (dolist (wherein (trace-info-wherein info
))
381 (unless (or (stringp wherein
)
383 (warn ":WHEREIN name ~S is not a defined global function."
389 (error "can't use encapsulation to trace anonymous function ~S"
391 (encapsulate function-or-name
'trace
`(trace-call ',info
)))
393 (multiple-value-bind (start-fun cookie-fun
)
394 (trace-start-breakpoint-fun info
)
395 (let ((start (sb-di:make-breakpoint start-fun debug-fun
397 (end (sb-di:make-breakpoint
398 (trace-end-breakpoint-fun info
)
399 debug-fun
:kind
:fun-end
400 :fun-end-cookie cookie-fun
)))
401 (setf (trace-info-start-breakpoint info
) start
)
402 (setf (trace-info-end-breakpoint info
) end
)
403 ;; The next two forms must be in the order in which they
404 ;; appear, since the start breakpoint must run before the
405 ;; fun-end breakpoint's start helper (which calls the
406 ;; cookie function.) One reason is that cookie function
407 ;; requires that the CONDITIONP shared closure variable be
409 (sb-di:activate-breakpoint start
)
410 (sb-di:activate-breakpoint end
)))))
412 (setf (gethash fun
*traced-funs
*) info
)))
418 ;;; Parse leading trace options off of SPECS, modifying INFO
419 ;;; accordingly. The remaining portion of the list is returned when we
420 ;;; encounter a plausible function name.
421 (defun parse-trace-options (specs info
)
422 (let ((current specs
))
424 (when (endp current
) (return))
425 (let ((option (first current
))
426 (value (cons (second current
) nil
)))
428 (:report
(error "stub: The :REPORT option is not yet implemented."))
429 (:condition
(setf (trace-info-condition info
) value
))
431 (setf (trace-info-condition info
) (cons nil nil
))
432 (setf (trace-info-condition-after info
) value
))
434 (setf (trace-info-condition info
) value
)
435 (setf (trace-info-condition-after info
) value
))
437 (setf (trace-info-wherein info
)
438 (if (listp (car value
)) (car value
) value
)))
440 (setf (trace-info-encapsulated info
) (car value
)))
441 (:break
(setf (trace-info-break info
) value
))
442 (:break-after
(setf (trace-info-break-after info
) value
))
444 (setf (trace-info-break info
) value
)
445 (setf (trace-info-break-after info
) value
))
447 (setf (trace-info-print info
)
448 (append (trace-info-print info
) (list value
))))
450 (setf (trace-info-print-after info
)
451 (append (trace-info-print-after info
) (list value
))))
453 (setf (trace-info-print info
)
454 (append (trace-info-print info
) (list value
)))
455 (setf (trace-info-print-after info
)
456 (append (trace-info-print-after info
) (list value
))))
460 (error "missing argument to ~S TRACE option" option
))
464 ;;; Compute the expansion of TRACE in the non-trivial case (arguments
466 (defun expand-trace (specs)
469 (let* ((global-options (make-trace-info))
470 (current (parse-trace-options specs global-options
)))
472 (when (endp current
) (return))
473 (let ((name (pop current
))
474 (options (copy-trace-info global-options
)))
477 (let ((temp (gensym)))
478 (binds `(,temp
,(pop current
)))
479 (forms `(trace-1 ,temp
',options
))))
480 ((and (keywordp name
)
481 (not (or (fboundp name
) (macro-function name
))))
482 (error "unknown TRACE option: ~S" name
))
484 (let ((package (find-undeleted-package-or-lose name
)))
485 (do-all-symbols (symbol (find-package name
))
486 (when (and (eql package
(symbol-package symbol
))
488 (not (macro-function symbol
))
489 (not (special-operator-p symbol
)))
490 (forms `(trace-1 ',symbol
',options
))))))
492 (forms `(trace-1 ',name
',options
))))
493 (setq current
(parse-trace-options current options
)))))
498 (defun %list-traced-funs
()
499 (loop for x being each hash-value in
*traced-funs
*
500 collect
(trace-info-what x
)))
502 (defmacro trace
(&rest specs
)
504 "TRACE {Option Global-Value}* {Name {Option Value}*}*
505 TRACE is a debugging tool that provides information when specified functions
506 are called. In its simplest form:
507 (TRACE NAME-1 NAME-2 ...)
508 The NAMEs are not evaluated. Each may be a symbol, denoting an
509 individual function, or a string, denoting all functions fbound
510 to symbols whose home package is the package with the given name.
512 Options allow modification of the default behavior. Each option is a pair
513 of an option keyword and a value form. Global options are specified before
514 the first name, and affect all functions traced by a given use of TRACE.
515 Options may also be interspersed with function names, in which case they
516 act as local options, only affecting tracing of the immediately preceding
517 function name. Local options override global options.
519 By default, TRACE causes a printout on *TRACE-OUTPUT* each time that
520 one of the named functions is entered or returns. (This is the
521 basic, ANSI Common Lisp behavior of TRACE.) As an SBCL extension, the
522 :REPORT SB-EXT:PROFILE option can be used to instead cause information
523 to be silently recorded to be inspected later using the SB-EXT:PROFILE
526 The following options are defined:
529 If Report-Type is TRACE (the default) then information is reported
530 by printing immediately. If Report-Type is SB-EXT:PROFILE, information
531 is recorded for later summary by calls to SB-EXT:PROFILE. If
532 Report-Type is NIL, then the only effect of the trace is to execute
533 other options (e.g. PRINT or BREAK).
536 :CONDITION-AFTER Form
538 If :CONDITION is specified, then TRACE does nothing unless Form
539 evaluates to true at the time of the call. :CONDITION-AFTER is
540 similar, but suppresses the initial printout, and is tested when the
541 function returns. :CONDITION-ALL tries both before and after.
542 This option is not supported with :REPORT PROFILE.
547 If specified, and Form evaluates to true, then the debugger is invoked
548 at the start of the function, at the end of the function, or both,
549 according to the respective option.
554 In addition to the usual printout, the result of evaluating Form is
555 printed at the start of the function, at the end of the function, or
556 both, according to the respective option. Multiple print options cause
557 multiple values to be printed.
560 If specified, Names is a function name or list of names. TRACE does
561 nothing unless a call to one of those functions encloses the call to
562 this function (i.e. it would appear in a backtrace.) Anonymous
563 functions have string names like \"DEFUN FOO\". This option is not
564 supported with :REPORT PROFILE.
566 :ENCAPSULATE {:DEFAULT | T | NIL}
567 If T, the tracing is done via encapsulation (redefining the function
568 name) rather than by modifying the function. :DEFAULT is the default,
569 and means to use encapsulation for interpreted functions and funcallable
570 instances, breakpoints otherwise. When encapsulation is used, forms are
571 *not* evaluated in the function's lexical environment, but SB-DEBUG:ARG
574 :FUNCTION Function-Form
575 This is a not really an option, but rather another way of specifying
576 what function to trace. The Function-Form is evaluated immediately,
577 and the resulting function is instrumented, i.e. traced or profiled
578 as specified in REPORT.
580 :CONDITION, :BREAK and :PRINT forms are evaluated in a context which
581 mocks up the lexical environment of the called function, so that
582 SB-DEBUG:VAR and SB-DEBUG:ARG can be used. The -AFTER and -ALL forms
583 are evaluated in the null environment."
586 '(%list-traced-funs
)))
590 ;;; Untrace one function.
591 (defun untrace-1 (function-or-name)
592 (let* ((fun (trace-fdefinition function-or-name
))
593 (info (gethash fun
*traced-funs
*)))
596 (warn "Function is not TRACEd: ~S" function-or-name
))
599 ((trace-info-encapsulated info
)
600 (unencapsulate (trace-info-what info
) 'trace
))
602 (sb-di:delete-breakpoint
(trace-info-start-breakpoint info
))
603 (sb-di:delete-breakpoint
(trace-info-end-breakpoint info
))))
604 (setf (trace-info-untraced info
) t
)
605 (remhash fun
*traced-funs
*)))))
607 ;;; Untrace all traced functions.
608 (defun untrace-all ()
609 (dolist (fun (%list-traced-funs
))
613 (defmacro untrace
(&rest specs
)
615 "Remove tracing from the specified functions. With no args, untrace all
617 ;; KLUDGE: Since we now allow (TRACE FOO BAR "SB-EXT") to trace not
618 ;; only #'FOO and #'BAR but also all the functions in #<PACKAGE "SB-EXT">,
619 ;; it would be probably be best for consistency to do something similar
620 ;; with UNTRACE. (But I leave it to someone who uses and cares about
621 ;; UNTRACE-with-args more often than I do.) -- WHN 2003-12-17
624 (let ((current specs
))
626 (unless current
(return))
627 (let ((name (pop current
)))
628 (res (if (eq name
:function
)
629 `(untrace-1 ,(pop current
))
630 `(untrace-1 ',name
)))))