1 ;;;; a tracing facility based on breakpoints
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))))
209 ;;; Test a BREAK option, and break if true.
210 (defun trace-maybe-break (info break where frame)
211 (when (and break (funcall (cdr break) frame))
212 (sb-di:flush-frames-above frame)
213 (let ((*stack-top-hint* frame))
214 (break "breaking ~A traced call to ~S:"
216 (trace-info-what info)))))
218 ;;; Discard any invalid cookies on our simulated stack. Encapsulated
219 ;;; entries are always valid, since we bind *TRACED-ENTRIES* in the
221 (defun discard-invalid-entries (frame)
223 (when (or (null *traced-entries*)
224 (let ((cookie (caar *traced-entries*)))
226 (sb-di:fun-end-cookie-valid-p frame cookie))))
228 (pop *traced-entries*)))
232 ;;; Return a closure that can be used for a function start breakpoint
233 ;;; hook function and a closure that can be used as the
234 ;;; FUN-END-COOKIE function. The first communicates the sense of
235 ;;; the Condition to the second via a closure variable.
236 (defun trace-start-breakpoint-fun (info)
241 (declare (ignore bpt))
242 (discard-invalid-entries frame)
243 (let ((condition (trace-info-condition info))
244 (wherein (trace-info-wherein info)))
246 (and (not *in-trace*)
248 (funcall (cdr condition) frame))
250 (trace-wherein-p frame wherein)))))
252 (let ((sb-kernel:*current-level-in-print* 0)
253 (*standard-output* *trace-output*)
256 (print-trace-indentation)
257 (if (trace-info-encapsulated info)
258 ;; FIXME: These special variables should be given
259 ;; *FOO*-style names, and probably declared globally
262 (declare (special basic-definition arg-list))
263 (prin1 `(,(trace-info-what info) ,@arg-list)))
264 (print-frame-call frame))
266 (trace-print frame (trace-info-print info)))
267 (trace-maybe-break info (trace-info-break info) "before" frame)))
269 (lambda (frame cookie)
270 (declare (ignore frame))
271 (push (cons cookie conditionp) *traced-entries*)))))
273 ;;; This prints a representation of the return values delivered.
274 ;;; First, this checks to see that cookie is at the top of
275 ;;; *TRACED-ENTRIES*; if it is not, then we need to adjust this list
276 ;;; to determine the correct indentation for output. We then check to
277 ;;; see whether the function is still traced and that the condition
278 ;;; succeeded before printing anything.
279 (declaim (ftype (function (trace-info) function) trace-end-breakpoint-fun))
280 (defun trace-end-breakpoint-fun (info)
281 (lambda (frame bpt *trace-values* cookie)
282 (declare (ignore bpt))
283 (unless (eq cookie (caar *traced-entries*))
284 (setf *traced-entries*
285 (member cookie *traced-entries* :key #'car)))
287 (let ((entry (pop *traced-entries*)))
288 (when (and (not (trace-info-untraced info))
290 (let ((cond (trace-info-condition-after info)))
291 (and cond (funcall (cdr cond) frame)))))
292 (let ((sb-kernel:*current-level-in-print* 0)
293 (*standard-output* *trace-output*)
296 (pprint-logical-block (*standard-output* nil)
297 (print-trace-indentation)
298 (pprint-indent :current 2)
299 (format t "~S returned" (trace-info-what info))
300 (dolist (v *trace-values*)
302 (pprint-newline :linear)
305 (trace-print frame (trace-info-print-after info)))
306 (trace-maybe-break info
307 (trace-info-break-after info)
311 ;;; This function is called by the trace encapsulation. It calls the
312 ;;; breakpoint hook functions with NIL for the breakpoint and cookie,
313 ;;; which we have cleverly contrived to work for our hook functions.
314 (defun trace-call (info)
315 (multiple-value-bind (start cookie) (trace-start-breakpoint-fun info)
316 (declare (type function start cookie))
317 (let ((frame (sb-di:frame-down (sb-di:top-frame))))
318 (funcall start frame nil)
319 (let ((*traced-entries* *traced-entries*))
320 (declare (special basic-definition arg-list))
321 (funcall cookie frame nil)
324 (apply basic-definition arg-list))))
325 (funcall (trace-end-breakpoint-fun info) frame nil vals nil)
326 (values-list vals))))))
328 ;;; Trace one function according to the specified options. We copy the
329 ;;; trace info (it was a quoted constant), fill in the functions, and
330 ;;; then install the breakpoints or encapsulation.
332 ;;; If non-null, DEFINITION is the new definition of a function that
333 ;;; we are automatically retracing.
334 (defun trace-1 (function-or-name info &optional definition)
335 (multiple-value-bind (fun named kind)
338 (nth-value 2 (trace-fdefinition definition)))
339 (trace-fdefinition function-or-name))
340 (when (gethash fun *traced-funs*)
341 (warn "~S is already TRACE'd, untracing it." function-or-name)
344 (let* ((debug-fun (sb-di:fun-debug-fun fun))
346 (if (eq (trace-info-encapsulated info) :default)
350 (unless (functionp function-or-name)
351 (warn "tracing shared code for ~S:~% ~S"
355 ((:interpreted :interpreted-closure :funcallable-instance)
357 (trace-info-encapsulated info)))
358 (loc (if encapsulated
360 (sb-di:debug-fun-start-location debug-fun)))
361 (info (make-trace-info
362 :what function-or-name
364 :encapsulated encapsulated
365 :wherein (trace-info-wherein info)
366 :condition (coerce-form (trace-info-condition info) loc)
367 :break (coerce-form (trace-info-break info) loc)
368 :print (coerce-form-list (trace-info-print info) loc)
369 :break-after (coerce-form (trace-info-break-after info) nil)
371 (coerce-form (trace-info-condition-after info) nil)
373 (coerce-form-list (trace-info-print-after info) nil))))
375 (dolist (wherein (trace-info-wherein info))
376 (unless (or (stringp wherein)
378 (warn ":WHEREIN name ~S is not a defined global function."
384 (error "can't use encapsulation to trace anonymous function ~S"
386 (encapsulate function-or-name 'trace `(trace-call ',info)))
388 (multiple-value-bind (start-fun cookie-fun)
389 (trace-start-breakpoint-fun info)
390 (let ((start (sb-di:make-breakpoint start-fun debug-fun
392 (end (sb-di:make-breakpoint
393 (trace-end-breakpoint-fun info)
394 debug-fun :kind :fun-end
395 :fun-end-cookie cookie-fun)))
396 (setf (trace-info-start-breakpoint info) start)
397 (setf (trace-info-end-breakpoint info) end)
398 ;; The next two forms must be in the order in which they
399 ;; appear, since the start breakpoint must run before the
400 ;; fun-end breakpoint's start helper (which calls the
401 ;; cookie function.) One reason is that cookie function
402 ;; requires that the CONDITIONP shared closure variable be
404 (sb-di:activate-breakpoint start)
405 (sb-di:activate-breakpoint end)))))
407 (setf (gethash fun *traced-funs*) info)))
413 ;;; Parse leading trace options off of SPECS, modifying INFO
414 ;;; accordingly. The remaining portion of the list is returned when we
415 ;;; encounter a plausible function name.
416 (defun parse-trace-options (specs info)
417 (let ((current specs))
419 (when (endp current) (return))
420 (let ((option (first current))
421 (value (cons (second current) nil)))
423 (:report (error "stub: The :REPORT option is not yet implemented."))
424 (:condition (setf (trace-info-condition info) value))
426 (setf (trace-info-condition info) (cons nil nil))
427 (setf (trace-info-condition-after info) value))
429 (setf (trace-info-condition info) value)
430 (setf (trace-info-condition-after info) value))
432 (setf (trace-info-wherein info)
433 (if (listp (car value)) (car value) value)))
435 (setf (trace-info-encapsulated info) (car value)))
436 (:break (setf (trace-info-break info) value))
437 (:break-after (setf (trace-info-break-after info) value))
439 (setf (trace-info-break info) value)
440 (setf (trace-info-break-after info) value))
442 (setf (trace-info-print info)
443 (append (trace-info-print info) (list value))))
445 (setf (trace-info-print-after info)
446 (append (trace-info-print-after info) (list value))))
448 (setf (trace-info-print info)
449 (append (trace-info-print info) (list value)))
450 (setf (trace-info-print-after info)
451 (append (trace-info-print-after info) (list value))))
455 (error "missing argument to ~S TRACE option" option))
459 ;;; Compute the expansion of TRACE in the non-trivial case (arguments
460 ;;; specified.) If there are no :FUNCTION specs, then don't use a LET.
461 ;;; This allows TRACE to be used without the full interpreter.
462 (defun expand-trace (specs)
465 (let* ((global-options (make-trace-info))
466 (current (parse-trace-options specs global-options)))
468 (when (endp current) (return))
469 (let ((name (pop current))
470 (options (copy-trace-info global-options)))
473 (let ((temp (gensym)))
474 (binds `(,temp ,(pop current)))
475 (forms `(trace-1 ,temp ',options))))
476 ((and (keywordp name)
477 (not (or (fboundp name) (macro-function name))))
478 (error "unknown TRACE option: ~S" name))
480 (forms `(trace-1 ',name ',options))))
481 (setq current (parse-trace-options current options)))))
484 `(let ,(binds) (list ,@(forms)))
487 (defun %list-traced-funs ()
488 (loop for x being each hash-value in *traced-funs*
489 collect (trace-info-what x)))
491 (defmacro trace (&rest specs)
493 "TRACE {Option Global-Value}* {Name {Option Value}*}*
494 TRACE is a debugging tool that provides information when specified functions
495 are called. In its simplest form:
496 (trace Name-1 Name-2 ...)
497 (The Names are not evaluated.)
499 Options allow modification of the default behavior. Each option is a pair
500 of an option keyword and a value form. Global options are specified before
501 the first name, and affect all functions traced by a given use of TRACE.
502 Options may also be interspersed with function names, in which case they
503 act as local options, only affecting tracing of the immediately preceding
504 function name. Local options override global options.
506 By default, TRACE causes a printout on *TRACE-OUTPUT* each time that
507 one of the named functions is entered or returns. (This is the
508 basic, ANSI Common Lisp behavior of TRACE.) As an SBCL extension, the
509 :REPORT SB-EXT:PROFILE option can be used to instead cause information
510 to be silently recorded to be inspected later using the SB-EXT:PROFILE
513 The following options are defined:
516 If Report-Type is TRACE (the default) then information is reported
517 by printing immediately. If Report-Type is SB-EXT:PROFILE, information
518 is recorded for later summary by calls to SB-EXT:PROFILE. If
519 Report-Type is NIL, then the only effect of the trace is to execute
520 other options (e.g. PRINT or BREAK).
523 :CONDITION-AFTER Form
525 If :CONDITION is specified, then TRACE does nothing unless Form
526 evaluates to true at the time of the call. :CONDITION-AFTER is
527 similar, but suppresses the initial printout, and is tested when the
528 function returns. :CONDITION-ALL tries both before and after.
533 If specified, and Form evaluates to true, then the debugger is invoked
534 at the start of the function, at the end of the function, or both,
535 according to the respective option.
540 In addition to the usual printout, the result of evaluating Form is
541 printed at the start of the function, at the end of the function, or
542 both, according to the respective option. Multiple print options cause
543 multiple values to be printed.
546 If specified, Names is a function name or list of names. TRACE does
547 nothing unless a call to one of those functions encloses the call to
548 this function (i.e. it would appear in a backtrace.) Anonymous
549 functions have string names like \"DEFUN FOO\".
551 :ENCAPSULATE {:DEFAULT | T | NIL}
552 If T, the tracing is done via encapsulation (redefining the function
553 name) rather than by modifying the function. :DEFAULT is the default,
554 and means to use encapsulation for interpreted functions and funcallable
555 instances, breakpoints otherwise. When encapsulation is used, forms are
556 *not* evaluated in the function's lexical environment, but SB-DEBUG:ARG
559 :FUNCTION Function-Form
560 This is a not really an option, but rather another way of specifying
561 what function to trace. The Function-Form is evaluated immediately,
562 and the resulting function is traced.
564 :CONDITION, :BREAK and :PRINT forms are evaluated in the lexical environment
565 of the called function; SB-DEBUG:VAR and SB-DEBUG:ARG can be used. The
566 -AFTER and -ALL forms are evaluated in the null environment."
569 '(%list-traced-funs)))
573 ;;; Untrace one function.
574 (defun untrace-1 (function-or-name)
575 (let* ((fun (trace-fdefinition function-or-name))
576 (info (gethash fun *traced-funs*)))
579 (warn "Function is not TRACEd: ~S" function-or-name))
582 ((trace-info-encapsulated info)
583 (unencapsulate (trace-info-what info) 'trace))
585 (sb-di:delete-breakpoint (trace-info-start-breakpoint info))
586 (sb-di:delete-breakpoint (trace-info-end-breakpoint info))))
587 (setf (trace-info-untraced info) t)
588 (remhash fun *traced-funs*)))))
590 ;;; Untrace all traced functions.
591 (defun untrace-all ()
592 (dolist (fun (%list-traced-funs))
596 (defmacro untrace (&rest specs)
598 "Remove tracing from the specified functions. With no args, untrace all
602 (let ((current specs))
604 (unless current (return))
605 (let ((name (pop current)))
606 (res (if (eq name :function)
607 `(untrace-1 ,(pop current))
608 `(untrace-1 ',name)))))