More robust undefined restarts.
[sbcl.git] / src / code / ntrace.lisp
blob8e0911b20094b999c21b6e24beae26a9c894f3ee
1 ;;;; a tracing facility
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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
16 ;;; prefixes..
18 (defvar *trace-indentation-step* 2
19 "the increase in trace indentation at each call level")
21 (defvar *max-trace-indentation* 40
22 "If the trace indentation exceeds this value, then indentation restarts at
23 0.")
25 (defvar *trace-encapsulate-default* t
26 "the default value for the :ENCAPSULATE option to TRACE")
28 ;;;; internal state
30 ;;; a hash table that maps each traced function to the TRACE-INFO. The
31 ;;; entry for a closure is the shared function entry object.
32 (defvar *traced-funs* (make-hash-table :test 'eq :synchronized t))
34 (deftype trace-report-type ()
35 '(member nil trace))
37 ;;; A TRACE-INFO object represents all the information we need to
38 ;;; trace a given function.
39 (defstruct (trace-info
40 (:print-object (lambda (x stream)
41 (print-unreadable-object (x stream :type t)
42 (prin1 (trace-info-what x) stream)))))
43 ;; the original representation of the thing traced
44 (what nil :type (or function cons symbol))
45 ;; Is WHAT a function name whose definition we should track?
46 (named nil)
47 ;; Is tracing to be done by encapsulation rather than breakpoints?
48 ;; T implies NAMED.
49 (encapsulated *trace-encapsulate-default*)
50 ;; Has this trace been untraced?
51 (untraced nil)
52 ;; breakpoints we set up to trigger tracing
53 (start-breakpoint nil :type (or sb-di:breakpoint null))
54 (end-breakpoint nil :type (or sb-di:breakpoint null))
55 ;; the list of function names for WHEREIN, or NIL if unspecified
56 (wherein nil :type list)
57 ;; should we trace methods given a generic function to trace?
58 (methods nil)
60 ;; The following slots represent the forms that we are supposed to
61 ;; evaluate on each iteration. Each form is represented by a cons
62 ;; (Form . Function), where the Function is the cached result of
63 ;; coercing Form to a function. Forms which use the current
64 ;; environment are converted with PREPROCESS-FOR-EVAL, which gives
65 ;; us a one-arg function. Null environment forms also have one-arg
66 ;; functions, but the argument is ignored. NIL means unspecified
67 ;; (the default.)
69 ;; report type
70 (report 'trace :type trace-report-type)
71 ;; current environment forms
72 (condition nil)
73 (break nil)
74 ;; List of current environment forms
75 (print () :type list)
76 ;; null environment forms
77 (condition-after nil)
78 (break-after nil)
79 ;; list of null environment forms
80 (print-after () :type list))
81 (!set-load-form-method trace-info (:target))
83 ;;; This is a list of conses (fun-end-cookie . condition-satisfied),
84 ;;; which we use to note distinct dynamic entries into functions. When
85 ;;; we enter a traced function, we add a entry to this list holding
86 ;;; the new end-cookie and whether the trace condition was satisfied.
87 ;;; We must save the trace condition so that the after breakpoint
88 ;;; knows whether to print. The length of this list tells us the
89 ;;; indentation to use for printing TRACE messages.
90 ;;;
91 ;;; This list also helps us synchronize the TRACE facility dynamically
92 ;;; for detecting non-local flow of control. Whenever execution hits a
93 ;;; :FUN-END breakpoint used for TRACE'ing, we look for the
94 ;;; FUN-END-COOKIE at the top of *TRACED-ENTRIES*. If it is not
95 ;;; there, we discard any entries that come before our cookie.
96 ;;;
97 ;;; When we trace using encapsulation, we bind this variable and add
98 ;;; (NIL . CONDITION-SATISFIED), so a NIL "cookie" marks an
99 ;;; encapsulated tracing.
100 (defvar *traced-entries* ())
101 (declaim (list *traced-entries*))
103 ;;; This variable is used to discourage infinite recursions when some
104 ;;; trace action invokes a function that is itself traced. In this
105 ;;; case, we quietly ignore the inner tracing.
106 (defvar *in-trace* nil)
108 ;;;; utilities
110 ;;; Given a function name, a function or a macro name, return the raw
111 ;;; definition and some information. "Raw" means that if the result is
112 ;;; a closure, we strip off the closure and return the bare code. The
113 ;;; second value is T if the argument was a function name. The third
114 ;;; value is one of :COMPILED, :COMPILED-CLOSURE, :INTERPRETED,
115 ;;; :INTERPRETED-CLOSURE and :FUNCALLABLE-INSTANCE.
116 (defun trace-fdefinition (x)
117 (flet ((get-def ()
118 (if (valid-function-name-p x)
119 (if (fboundp x)
120 (fdefinition x)
121 (warn "~/sb-impl::print-symbol-with-prefix/ is ~
122 undefined, not tracing." x))
123 (warn "~S is not a valid function name, not tracing." x))))
124 (multiple-value-bind (res named-p)
125 (typecase x
126 (symbol
127 (cond ((special-operator-p x)
128 (warn "~S is a special operator, not tracing." x))
129 ((macro-function x))
131 (values (get-def) t))))
132 (function
135 (values (get-def) t)))
136 (typecase res
137 (closure
138 (values (sb-kernel:%closure-fun res)
139 named-p
140 :compiled-closure))
141 (funcallable-instance
142 (values res named-p :funcallable-instance))
143 ;; FIXME: What about SB!EVAL:INTERPRETED-FUNCTION -- it gets picked off
144 ;; by the FIN above, is that right?
146 (values res named-p :compiled))))))
148 ;;; When a function name is redefined, and we were tracing that name,
149 ;;; then untrace the old definition and trace the new one.
150 (defun trace-redefined-update (fname new-value)
151 (when (fboundp fname)
152 (let* ((fun (trace-fdefinition fname))
153 (info (gethash fun *traced-funs*)))
154 (when (and info (trace-info-named info))
155 (untrace-1 fname)
156 (trace-1 fname info new-value)))))
157 (push #'trace-redefined-update *setf-fdefinition-hook*)
159 ;;; Annotate a FORM to evaluate with pre-converted functions. FORM is
160 ;;; really a cons (EXP . FUNCTION). LOC is the code location to use
161 ;;; for the lexical environment. If LOC is NIL, evaluate in the null
162 ;;; environment. If FORM is NIL, just return NIL.
163 (defun coerce-form (form loc)
164 (when form
165 (let ((exp (car form)))
166 (if (sb-di:code-location-p loc)
167 (let ((fun (sb-di:preprocess-for-eval exp loc)))
168 (declare (type function fun))
169 (cons exp
170 (lambda (frame &rest args)
171 (declare (ignore args))
172 (let ((*current-frame* frame))
173 (funcall fun frame)))))
174 (let* ((body `(locally (declare (disable-package-locks sb-debug:arg))
175 (flet ((sb-debug:arg (n)
176 (elt args n)))
177 (declare (ignorable #'sb-debug:arg)
178 (enable-package-locks sb-debug:arg))
179 ,exp)))
180 (fun (coerce `(lambda (&rest args) (declare (ignorable args))
181 ,body) 'function)))
182 (cons exp
183 (lambda (frame &rest args)
184 (declare (ignore frame))
185 (let ((*current-frame* nil))
186 (apply fun args)))))))))
188 (defun coerce-form-list (forms loc)
189 (mapcar (lambda (x) (coerce-form x loc)) forms))
191 ;;; Print indentation according to the number of trace entries.
192 ;;; Entries whose condition was false don't count.
193 (defun print-trace-indentation ()
194 (let* ((depth (count-if #'cdr *traced-entries*))
195 (step *trace-indentation-step*)
196 (max *max-trace-indentation*)
197 (indent (+ (mod (* depth step) (- max step)) step)))
198 (format t "~V,0@T~W: " indent depth)))
200 ;;; Return true if any of the NAMES appears on the stack below FRAME.
201 (defun trace-wherein-p (frame names)
202 (do ((frame (sb-di:frame-down frame) (sb-di:frame-down frame)))
203 ((not frame) nil)
204 (when (member (sb-di:debug-fun-name (sb-di:frame-debug-fun frame))
205 names
206 :test #'equal)
207 (return t))))
209 ;;; Handle PRINT and PRINT-AFTER options.
210 (defun trace-print (frame forms &rest args)
211 (dolist (ele forms)
212 (fresh-line)
213 (print-trace-indentation)
214 (format t "~@<~S ~_= ~:[; No values~;~:*~{~S~^, ~}~]~:>"
215 (car ele)
216 (multiple-value-list (apply (cdr ele) frame args)))
217 (terpri)))
219 ;;; Handle PRINT and PRINT-AFTER options when :REPORT style is NIL.
220 (defun trace-print-unadorned (frame forms &rest args)
221 (dolist (ele forms)
222 (let ((values (multiple-value-list (apply (cdr ele) frame args))))
223 (when values
224 (format t "~&~{~A~^, ~}~%" values)))))
226 ;;; Test a BREAK option, and if true, break.
227 (defun trace-maybe-break (info break where frame &rest args)
228 (when (and break (apply (cdr break) frame args))
229 (sb-di:flush-frames-above frame)
230 (let ((*stack-top-hint* frame))
231 (break "breaking ~A traced call to ~S:"
232 where
233 (trace-info-what info)))))
235 ;;; Discard any invalid cookies on our simulated stack. Encapsulated
236 ;;; entries are always valid, since we bind *TRACED-ENTRIES* in the
237 ;;; encapsulation.
238 (defun discard-invalid-entries (frame)
239 (loop
240 (when (or (null *traced-entries*)
241 (let ((cookie (caar *traced-entries*)))
242 (or (not cookie)
243 (sb-di:fun-end-cookie-valid-p frame cookie))))
244 (return))
245 (pop *traced-entries*)))
247 ;;;; hook functions
249 ;;; Return a closure that can be used for a function start breakpoint
250 ;;; hook function and a closure that can be used as the FUN-END-COOKIE
251 ;;; function. The first communicates the sense of the
252 ;;; TRACE-INFO-CONDITION to the second via a closure variable.
253 (defun trace-start-breakpoint-fun (info)
254 (let (conditionp)
255 (values
256 (lambda (frame bpt &rest args)
257 (declare (ignore bpt))
258 (discard-invalid-entries frame)
259 (let ((condition (trace-info-condition info))
260 (wherein (trace-info-wherein info)))
261 (setq conditionp
262 (and (not *in-trace*)
263 (or (not condition)
264 (apply (cdr condition) frame args))
265 (or (not wherein)
266 (trace-wherein-p frame wherein)))))
267 (when conditionp
268 (let ((sb-kernel:*current-level-in-print* 0)
269 (*standard-output* (make-string-output-stream))
270 (*in-trace* t))
271 (ecase (trace-info-report info)
272 (trace
273 (fresh-line)
274 (print-trace-indentation)
275 (if (trace-info-encapsulated info)
276 (prin1 `(,(trace-info-what info)
277 ,@(mapcar #'ensure-printable-object args)))
278 (print-frame-call frame *standard-output*))
279 (terpri)
280 (apply #'trace-print frame (trace-info-print info) args))
281 ((nil)
282 (apply #'trace-print-unadorned frame (trace-info-print info) args)))
283 (write-sequence (get-output-stream-string *standard-output*)
284 *trace-output*)
285 (finish-output *trace-output*))
286 (apply #'trace-maybe-break info (trace-info-break info) "before"
287 frame args)))
288 (lambda (frame cookie)
289 (declare (ignore frame))
290 (push (cons cookie conditionp) *traced-entries*)))))
292 ;;; This prints a representation of the return values delivered.
293 ;;; First, this checks to see that cookie is at the top of
294 ;;; *TRACED-ENTRIES*; if it is not, then we need to adjust this list
295 ;;; to determine the correct indentation for output. We then check to
296 ;;; see whether the function is still traced and that the condition
297 ;;; succeeded before printing anything.
298 (declaim (ftype (function (trace-info) function) trace-end-breakpoint-fun))
299 (defun trace-end-breakpoint-fun (info)
300 (lambda (frame bpt values cookie)
301 (declare (ignore bpt))
302 (unless (eq cookie (caar *traced-entries*))
303 (setf *traced-entries*
304 (member cookie *traced-entries* :key #'car)))
306 (let ((entry (pop *traced-entries*)))
307 (when (and (not (trace-info-untraced info))
308 (or (cdr entry)
309 (let ((cond (trace-info-condition-after info)))
310 (and cond (apply #'funcall (cdr cond) frame values)))))
311 (let ((sb-kernel:*current-level-in-print* 0)
312 (*standard-output* (make-string-output-stream))
313 (*in-trace* t))
314 (ecase (trace-info-report info)
315 (trace
316 (fresh-line)
317 (let ((*print-pretty* t))
318 (pprint-logical-block (*standard-output* nil)
319 (print-trace-indentation)
320 (pprint-indent :current 2)
321 (format t "~S returned" (trace-info-what info))
322 (dolist (v values)
323 (write-char #\space)
324 (pprint-newline :linear)
325 (prin1 (ensure-printable-object v))))
326 (terpri))
327 (apply #'trace-print frame (trace-info-print-after info) values))
328 ((nil)
329 (apply #'trace-print-unadorned frame (trace-info-print-after info) values)))
330 (write-sequence (get-output-stream-string *standard-output*)
331 *trace-output*)
332 (finish-output *trace-output*))
333 (apply #'trace-maybe-break info (trace-info-break-after info) "after"
334 frame values)))))
336 ;;; This function is called by the trace encapsulation. It calls the
337 ;;; breakpoint hook functions with NIL for the breakpoint and cookie,
338 ;;; which we have cleverly contrived to work for our hook functions.
339 (defun trace-call (info function &rest args)
340 (multiple-value-bind (start cookie) (trace-start-breakpoint-fun info)
341 (declare (type function start cookie))
342 (let ((frame (sb-di:frame-down (sb-di:top-frame))))
343 (apply #'funcall start frame nil args)
344 (let ((*traced-entries* *traced-entries*))
345 (funcall cookie frame nil)
346 (let ((vals (multiple-value-list (apply function args))))
347 (funcall (trace-end-breakpoint-fun info) frame nil vals nil)
348 (values-list vals))))))
350 ;;; Trace one function according to the specified options. We copy the
351 ;;; trace info (it was a quoted constant), fill in the functions, and
352 ;;; then install the breakpoints or encapsulation.
354 ;;; If non-null, DEFINITION is the new definition of a function that
355 ;;; we are automatically retracing.
356 (defun trace-1 (function-or-name info &optional definition)
357 (multiple-value-bind (fun named kind)
358 (if definition
359 (values definition t
360 (nth-value 2 (trace-fdefinition definition)))
361 (trace-fdefinition function-or-name))
362 (when fun
363 (when (gethash fun *traced-funs*)
364 (warn "~S is already TRACE'd, untracing it first." function-or-name)
365 (untrace-1 fun))
366 (let* ((debug-fun (sb-di:fun-debug-fun fun))
367 (encapsulated
368 (if (eq (trace-info-encapsulated info) :default)
369 (ecase kind
370 (:compiled nil)
371 (:compiled-closure
372 (unless (functionp function-or-name)
373 (warn "tracing shared code for ~S:~% ~S"
374 function-or-name
375 fun))
376 nil)
377 ((:interpreted :interpreted-closure :funcallable-instance)
379 (trace-info-encapsulated info)))
380 (loc (if encapsulated
381 :encapsulated
382 (sb-di:debug-fun-start-location debug-fun)))
383 (info (make-trace-info
384 :what function-or-name
385 :named named
386 :encapsulated encapsulated
387 :wherein (trace-info-wherein info)
388 :methods (trace-info-methods info)
389 :condition (coerce-form (trace-info-condition info) loc)
390 :break (coerce-form (trace-info-break info) loc)
391 :report (trace-info-report info)
392 :print (coerce-form-list (trace-info-print info) loc)
393 :break-after (coerce-form (trace-info-break-after info) nil)
394 :condition-after
395 (coerce-form (trace-info-condition-after info) nil)
396 :print-after
397 (coerce-form-list (trace-info-print-after info) nil))))
399 (dolist (wherein (trace-info-wherein info))
400 (unless (or (stringp wherein)
401 (fboundp wherein))
402 (warn ":WHEREIN name ~S is not a defined global function."
403 wherein)))
405 (cond
406 (encapsulated
407 (unless named
408 (error "can't use encapsulation to trace anonymous function ~S"
409 fun))
410 (encapsulate function-or-name 'trace
411 (lambda (function &rest args)
412 (apply #'trace-call info function args))))
414 (multiple-value-bind (start-fun cookie-fun)
415 (trace-start-breakpoint-fun info)
416 (let ((start (sb-di:make-breakpoint start-fun debug-fun
417 :kind :fun-start))
418 (end (sb-di:make-breakpoint
419 (trace-end-breakpoint-fun info)
420 debug-fun :kind :fun-end
421 :fun-end-cookie cookie-fun)))
422 (setf (trace-info-start-breakpoint info) start)
423 (setf (trace-info-end-breakpoint info) end)
424 ;; The next two forms must be in the order in which they
425 ;; appear, since the start breakpoint must run before the
426 ;; fun-end breakpoint's start helper (which calls the
427 ;; cookie function.) One reason is that cookie function
428 ;; requires that the CONDITIONP shared closure variable be
429 ;; initialized.
430 (sb-di:activate-breakpoint start)
431 (sb-di:activate-breakpoint end)))))
433 (setf (gethash fun *traced-funs*) info))
435 (when (and (typep fun 'generic-function)
436 (trace-info-methods info)
437 ;; we are going to trace the method functions directly.
438 (not (trace-info-encapsulated info)))
439 (dolist (method (sb-mop:generic-function-methods fun))
440 (let ((mf (sb-mop:method-function method)))
441 ;; NOTE: this direct style of tracing methods -- tracing the
442 ;; pcl-internal method functions -- is only one possible
443 ;; alternative. It fails (a) when encapulation is
444 ;; requested, because the function objects themselves are
445 ;; stored in the method object; (b) when the method in
446 ;; question is particularly simple, when the method
447 ;; functionality is in the dfun. See src/pcl/env.lisp for a
448 ;; stub implementation of encapsulating through a
449 ;; traced-method class.
450 (trace-1 mf info)
451 (when (typep mf 'sb-pcl::%method-function)
452 (trace-1 (sb-pcl::%method-function-fast-function mf) info)))))
454 function-or-name)))
456 ;;;; the TRACE macro
458 ;;; Parse leading trace options off of SPECS, modifying INFO
459 ;;; accordingly. The remaining portion of the list is returned when we
460 ;;; encounter a plausible function name.
461 (defun parse-trace-options (specs info)
462 (let ((current specs))
463 (loop
464 (when (endp current) (return))
465 (let ((option (first current))
466 (value (cons (second current) nil)))
467 (case option
468 (:report
469 (unless (typep (car value) 'trace-report-type)
470 (error "~S is not a valid ~A ~S type."
471 (car value) 'trace :report))
472 (setf (trace-info-report info) (car value)))
473 (:condition (setf (trace-info-condition info) value))
474 (:condition-after
475 (setf (trace-info-condition info) (cons nil nil))
476 (setf (trace-info-condition-after info) value))
477 (:condition-all
478 (setf (trace-info-condition info) value)
479 (setf (trace-info-condition-after info) value))
480 (:wherein
481 (setf (trace-info-wherein info)
482 (if (listp (car value)) (car value) value)))
483 (:encapsulate
484 (setf (trace-info-encapsulated info) (car value)))
485 (:methods
486 (setf (trace-info-methods info) (car value)))
487 (:break (setf (trace-info-break info) value))
488 (:break-after (setf (trace-info-break-after info) value))
489 (:break-all
490 (setf (trace-info-break info) value)
491 (setf (trace-info-break-after info) value))
492 (:print
493 (setf (trace-info-print info)
494 (append (trace-info-print info) (list value))))
495 (:print-after
496 (setf (trace-info-print-after info)
497 (append (trace-info-print-after info) (list value))))
498 (:print-all
499 (setf (trace-info-print info)
500 (append (trace-info-print info) (list value)))
501 (setf (trace-info-print-after info)
502 (append (trace-info-print-after info) (list value))))
503 (t (return)))
504 (pop current)
505 (unless current
506 (error "missing argument to ~S TRACE option" option))
507 (pop current)))
508 current))
510 ;;; Compute the expansion of TRACE in the non-trivial case (arguments
511 ;;; specified.)
512 (defun expand-trace (specs)
513 (collect ((binds)
514 (forms))
515 (let* ((global-options (make-trace-info))
516 (current (parse-trace-options specs global-options)))
517 (loop
518 (when (endp current) (return))
519 (let ((name (pop current))
520 (options (copy-trace-info global-options)))
521 (cond
522 ((eq name :function)
523 (let ((temp (gensym)))
524 (binds `(,temp ,(pop current)))
525 (forms `(trace-1 ,temp ',options))))
526 ((and (keywordp name)
527 (not (or (fboundp name) (macro-function name))))
528 (error "unknown TRACE option: ~S" name))
529 ((stringp name)
530 (let ((package (find-undeleted-package-or-lose name)))
531 (do-all-symbols (symbol (find-package name))
532 (when (eql package (symbol-package symbol))
533 (when (and (fboundp symbol)
534 (not (macro-function symbol))
535 (not (special-operator-p symbol)))
536 (forms `(trace-1 ',symbol ',options)))
537 (let ((setf-name `(setf ,symbol)))
538 (when (fboundp setf-name)
539 (forms `(trace-1 ',setf-name ',options))))))))
540 ;; special-case METHOD: it itself is not a general function
541 ;; name symbol, but it (at least here) designates one of a
542 ;; pair of such.
543 ((and (consp name) (eq (car name) 'method))
544 (when (fboundp (list* 'sb-pcl::slow-method (cdr name)))
545 (forms `(trace-1 ',(list* 'sb-pcl::slow-method (cdr name))
546 ',options)))
547 (when (fboundp (list* 'sb-pcl::fast-method (cdr name)))
548 (forms `(trace-1 ',(list* 'sb-pcl::fast-method (cdr name))
549 ',options))))
551 (forms `(trace-1 ',name ',options))))
552 (setq current (parse-trace-options current options)))))
554 `(let ,(binds)
555 (remove nil (list ,@(forms))))))
557 (defun %list-traced-funs ()
558 (loop for x being each hash-value in *traced-funs*
559 collect (trace-info-what x)))
561 (defmacro trace (&rest specs)
562 "TRACE {Option Global-Value}* {Name {Option Value}*}*
564 TRACE is a debugging tool that provides information when specified
565 functions are called. In its simplest form:
567 (TRACE NAME-1 NAME-2 ...)
569 The NAMEs are not evaluated. Each may be a symbol, denoting an
570 individual function, or a string, denoting all functions fbound to
571 symbols whose home package is the package with the given name.
573 Options allow modification of the default behavior. Each option is a
574 pair of an option keyword and a value form. Global options are
575 specified before the first name, and affect all functions traced by a
576 given use of TRACE. Options may also be interspersed with function
577 names, in which case they act as local options, only affecting tracing
578 of the immediately preceding function name. Local options override
579 global options.
581 By default, TRACE causes a printout on *TRACE-OUTPUT* each time that
582 one of the named functions is entered or returns. (This is the basic,
583 ANSI Common Lisp behavior of TRACE.)
585 The following options are defined:
587 :REPORT Report-Type
588 If Report-Type is TRACE (the default) then information is
589 reported by printing immediately. If Report-Type is NIL, then
590 the only effect of the trace is to execute other
591 options (e.g. PRINT or BREAK).
593 :CONDITION Form
594 :CONDITION-AFTER Form
595 :CONDITION-ALL Form
596 If :CONDITION is specified, then TRACE does nothing unless Form
597 evaluates to true at the time of the call. :CONDITION-AFTER is
598 similar, but suppresses the initial printout, and is tested when the
599 function returns. :CONDITION-ALL tries both before and after.
601 :BREAK Form
602 :BREAK-AFTER Form
603 :BREAK-ALL Form
604 If specified, and Form evaluates to true, then the debugger is invoked
605 at the start of the function, at the end of the function, or both,
606 according to the respective option.
608 :PRINT Form
609 :PRINT-AFTER Form
610 :PRINT-ALL Form
611 In addition to the usual printout, the result of evaluating Form is
612 printed at the start of the function, at the end of the function, or
613 both, according to the respective option. Multiple print options cause
614 multiple values to be printed.
616 :WHEREIN Names
617 If specified, Names is a function name or list of names. TRACE does
618 nothing unless a call to one of those functions encloses the call to
619 this function (i.e. it would appear in a backtrace.) Anonymous
620 functions have string names like \"DEFUN FOO\".
622 :ENCAPSULATE {:DEFAULT | T | NIL}
623 If T, the tracing is done via encapsulation (redefining the function
624 name) rather than by modifying the function. :DEFAULT is the default,
625 and means to use encapsulation for interpreted functions and funcallable
626 instances, breakpoints otherwise. When encapsulation is used, forms are
627 *not* evaluated in the function's lexical environment, but SB-DEBUG:ARG
628 can still be used.
630 :METHODS {T | NIL}
631 If T, any function argument naming a generic function will have its
632 methods traced in addition to the generic function itself.
634 :FUNCTION Function-Form
635 This is a not really an option, but rather another way of specifying
636 what function to trace. The Function-Form is evaluated immediately,
637 and the resulting function is traced.
639 :CONDITION, :BREAK and :PRINT forms are evaluated in a context which
640 mocks up the lexical environment of the called function, so that
641 SB-DEBUG:VAR and SB-DEBUG:ARG can be used.
642 The -AFTER and -ALL forms can use SB-DEBUG:ARG."
643 (if specs
644 (expand-trace specs)
645 '(%list-traced-funs)))
647 ;;;; untracing
649 ;;; Untrace one function.
650 (defun untrace-1 (function-or-name)
651 (let* ((fun (trace-fdefinition function-or-name))
652 (info (when fun (gethash fun *traced-funs*))))
653 (cond
654 ((and fun (not info))
655 (warn "Function is not TRACEd: ~S" function-or-name))
656 ((not fun)
657 ;; Someone has FMAKUNBOUND it.
658 (let ((table *traced-funs*))
659 (with-locked-system-table (table)
660 (maphash (lambda (fun info)
661 (when (equal function-or-name (trace-info-what info))
662 (remhash fun table)))
663 table))))
665 (cond
666 ((trace-info-encapsulated info)
667 (unencapsulate (trace-info-what info) 'trace))
669 (sb-di:delete-breakpoint (trace-info-start-breakpoint info))
670 (sb-di:delete-breakpoint (trace-info-end-breakpoint info))))
671 (setf (trace-info-untraced info) t)
672 (remhash fun *traced-funs*)))))
674 ;;; Untrace all traced functions.
675 (defun untrace-all ()
676 (dolist (fun (%list-traced-funs))
677 (untrace-1 fun))
680 (defun untrace-package (name)
681 (let ((package (find-package name)))
682 (when package
683 (dolist (fun (%list-traced-funs))
684 (cond ((and (symbolp fun) (eq package (symbol-package fun)))
685 (untrace-1 fun))
686 ((and (consp fun) (eq 'setf (car fun))
687 (symbolp (second fun))
688 (eq package (symbol-package (second fun))))
689 (untrace-1 fun)))))))
691 (defmacro untrace (&rest specs)
692 "Remove tracing from the specified functions. Untraces all
693 functions when called with no arguments."
694 (if specs
695 `(progn
696 ,@(loop while specs
697 for name = (pop specs)
698 collect (cond ((eq name :function)
699 `(untrace-1 ,(pop specs)))
700 ((stringp name)
701 `(untrace-package ,name))
703 `(untrace-1 ',name))))
705 '(untrace-all)))