Tolerate non-simple strings when checking arguments to CERROR.
[sbcl.git] / src / code / ntrace.lisp
blob9d052e6edea2c726add13623789853dd9f2b9b23
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-ext: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 (%closure-fun res) named-p :compiled-closure))
139 (funcallable-instance
140 (values res named-p :funcallable-instance))
141 ;; FIXME: What about SB!EVAL:INTERPRETED-FUNCTION -- it gets picked off
142 ;; by the FIN above, is that right?
144 (values res named-p :compiled))))))
146 ;;; When a function name is redefined, and we were tracing that name,
147 ;;; then untrace the old definition and trace the new one.
148 (defun trace-redefined-update (fname new-value)
149 (when (fboundp fname)
150 (let* ((fun (trace-fdefinition fname))
151 (info (gethash fun *traced-funs*)))
152 (when (and info (trace-info-named info))
153 (untrace-1 fname)
154 (trace-1 fname info new-value)))))
155 (push #'trace-redefined-update *setf-fdefinition-hook*)
157 ;;; Annotate a FORM to evaluate with pre-converted functions. FORM is
158 ;;; really a cons (EXP . FUNCTION). LOC is the code location to use
159 ;;; for the lexical environment. If LOC is NIL, evaluate in the null
160 ;;; environment. If FORM is NIL, just return NIL.
161 (defun coerce-form (form loc)
162 (when form
163 (let ((exp (car form)))
164 (if (sb-di:code-location-p loc)
165 (let ((fun (sb-di:preprocess-for-eval exp loc)))
166 (declare (type function fun))
167 (cons exp
168 (lambda (frame &rest args)
169 (declare (ignore args))
170 (let ((*current-frame* frame))
171 (funcall fun frame)))))
172 (let* ((body `(locally (declare (disable-package-locks sb-debug:arg))
173 (flet ((sb-debug:arg (n)
174 (elt args n)))
175 (declare (ignorable #'sb-debug:arg)
176 (enable-package-locks sb-debug:arg))
177 ,exp)))
178 (fun (coerce `(lambda (&rest args) (declare (ignorable args))
179 ,body) 'function)))
180 (cons exp
181 (lambda (frame &rest args)
182 (declare (ignore frame))
183 (let ((*current-frame* nil))
184 (apply fun args)))))))))
186 (defun coerce-form-list (forms loc)
187 (mapcar (lambda (x) (coerce-form x loc)) forms))
189 ;;; Print indentation according to the number of trace entries.
190 ;;; Entries whose condition was false don't count.
191 (defun print-trace-indentation ()
192 (let* ((depth (count-if #'cdr *traced-entries*))
193 (step *trace-indentation-step*)
194 (max *max-trace-indentation*)
195 (indent (+ (mod (* depth step) (- max step)) step)))
196 (format t "~V,0@T~W: " indent depth)))
198 ;;; Return true if any of the NAMES appears on the stack below FRAME.
199 (defun trace-wherein-p (frame names)
200 (do ((frame (sb-di:frame-down frame) (sb-di:frame-down frame)))
201 ((not frame) nil)
202 (when (member (sb-di:debug-fun-name (sb-di:frame-debug-fun frame))
203 names
204 :test #'equal)
205 (return t))))
207 ;;; Handle PRINT and PRINT-AFTER options.
208 (defun trace-print (frame forms &rest args)
209 (dolist (ele forms)
210 (fresh-line)
211 (print-trace-indentation)
212 (format t "~@<~S ~_= ~:[; No values~;~:*~{~S~^, ~}~]~:>"
213 (car ele)
214 (multiple-value-list (apply (cdr ele) frame args)))
215 (terpri)))
217 ;;; Handle PRINT and PRINT-AFTER options when :REPORT style is NIL.
218 (defun trace-print-unadorned (frame forms &rest args)
219 (dolist (ele forms)
220 (let ((values (multiple-value-list (apply (cdr ele) frame args))))
221 (when values
222 (format t "~&~{~A~^, ~}~%" values)))))
224 ;;; Test a BREAK option, and if true, break.
225 (defun trace-maybe-break (info break where frame &rest args)
226 (when (and break (apply (cdr break) frame args))
227 (sb-di:flush-frames-above frame)
228 (let ((*stack-top-hint* frame))
229 (break "breaking ~A traced call to ~S:"
230 where
231 (trace-info-what info)))))
233 ;;; Discard any invalid cookies on our simulated stack. Encapsulated
234 ;;; entries are always valid, since we bind *TRACED-ENTRIES* in the
235 ;;; encapsulation.
236 (defun discard-invalid-entries (frame)
237 (loop
238 (when (or (null *traced-entries*)
239 (let ((cookie (caar *traced-entries*)))
240 (or (not cookie)
241 (sb-di:fun-end-cookie-valid-p frame cookie))))
242 (return))
243 (pop *traced-entries*)))
245 ;;;; hook functions
247 ;;; Return a closure that can be used for a function start breakpoint
248 ;;; hook function and a closure that can be used as the FUN-END-COOKIE
249 ;;; function. The first communicates the sense of the
250 ;;; TRACE-INFO-CONDITION to the second via a closure variable.
251 (defun trace-start-breakpoint-fun (info)
252 (let (conditionp)
253 (values
254 (lambda (frame bpt &rest args)
255 (declare (ignore bpt))
256 (discard-invalid-entries frame)
257 (let ((condition (trace-info-condition info))
258 (wherein (trace-info-wherein info)))
259 (setq conditionp
260 (and (not *in-trace*)
261 (or (not condition)
262 (apply (cdr condition) frame args))
263 (or (not wherein)
264 (trace-wherein-p frame wherein)))))
265 (when conditionp
266 (let ((*current-level-in-print* 0)
267 (*standard-output* (make-string-output-stream))
268 (*in-trace* t))
269 (ecase (trace-info-report info)
270 (trace
271 (fresh-line)
272 (print-trace-indentation)
273 (if (trace-info-encapsulated info)
274 (prin1 `(,(trace-info-what info)
275 ,@(mapcar #'ensure-printable-object args)))
276 (print-frame-call frame *standard-output*))
277 (terpri)
278 (apply #'trace-print frame (trace-info-print info) args))
279 ((nil)
280 (apply #'trace-print-unadorned frame (trace-info-print info) args)))
281 (write-sequence (get-output-stream-string *standard-output*)
282 *trace-output*)
283 (finish-output *trace-output*))
284 (apply #'trace-maybe-break info (trace-info-break info) "before"
285 frame args)))
286 (lambda (frame cookie)
287 (declare (ignore frame))
288 (push (cons cookie conditionp) *traced-entries*)))))
290 ;;; This prints a representation of the return values delivered.
291 ;;; First, this checks to see that cookie is at the top of
292 ;;; *TRACED-ENTRIES*; if it is not, then we need to adjust this list
293 ;;; to determine the correct indentation for output. We then check to
294 ;;; see whether the function is still traced and that the condition
295 ;;; succeeded before printing anything.
296 (declaim (ftype (function (trace-info) function) trace-end-breakpoint-fun))
297 (defun trace-end-breakpoint-fun (info)
298 (lambda (frame bpt values cookie)
299 (declare (ignore bpt))
300 (unless (eq cookie (caar *traced-entries*))
301 (setf *traced-entries*
302 (member cookie *traced-entries* :key #'car)))
304 (let ((entry (pop *traced-entries*)))
305 (when (and (not (trace-info-untraced info))
306 (or (cdr entry)
307 (let ((cond (trace-info-condition-after info)))
308 (and cond (apply #'funcall (cdr cond) frame values)))))
309 (let ((*current-level-in-print* 0)
310 (*standard-output* (make-string-output-stream))
311 (*in-trace* t))
312 (ecase (trace-info-report info)
313 (trace
314 (fresh-line)
315 (let ((*print-pretty* t))
316 (pprint-logical-block (*standard-output* nil)
317 (print-trace-indentation)
318 (pprint-indent :current 2)
319 (format t "~S returned" (trace-info-what info))
320 (dolist (v values)
321 (write-char #\space)
322 (pprint-newline :linear)
323 (prin1 (ensure-printable-object v))))
324 (terpri))
325 (apply #'trace-print frame (trace-info-print-after info) values))
326 ((nil)
327 (apply #'trace-print-unadorned frame (trace-info-print-after info) values)))
328 (write-sequence (get-output-stream-string *standard-output*)
329 *trace-output*)
330 (finish-output *trace-output*))
331 (apply #'trace-maybe-break info (trace-info-break-after info) "after"
332 frame values)))))
334 ;;; This function is called by the trace encapsulation. It calls the
335 ;;; breakpoint hook functions with NIL for the breakpoint and cookie,
336 ;;; which we have cleverly contrived to work for our hook functions.
337 (defun trace-call (info function &rest args)
338 (multiple-value-bind (start cookie) (trace-start-breakpoint-fun info)
339 (declare (type function start cookie))
340 (let ((frame (sb-di:frame-down (sb-di:top-frame))))
341 (apply #'funcall start frame nil args)
342 (let ((*traced-entries* *traced-entries*))
343 (funcall cookie frame nil)
344 (let ((vals (multiple-value-list (apply function args))))
345 (funcall (trace-end-breakpoint-fun info) frame nil vals nil)
346 (values-list vals))))))
348 ;;; Trace one function according to the specified options. We copy the
349 ;;; trace info (it was a quoted constant), fill in the functions, and
350 ;;; then install the breakpoints or encapsulation.
352 ;;; If non-null, DEFINITION is the new definition of a function that
353 ;;; we are automatically retracing.
354 (defun trace-1 (function-or-name info &optional definition)
355 (multiple-value-bind (fun named kind)
356 (if definition
357 (values definition t
358 (nth-value 2 (trace-fdefinition definition)))
359 (trace-fdefinition function-or-name))
360 (when fun
361 (when (gethash fun *traced-funs*)
362 (warn "~S is already TRACE'd, untracing it first." function-or-name)
363 (untrace-1 fun))
364 (let* ((debug-fun (sb-di:fun-debug-fun fun))
365 (encapsulated
366 (if (eq (trace-info-encapsulated info) :default)
367 (ecase kind
368 (:compiled nil)
369 (:compiled-closure
370 (unless (functionp function-or-name)
371 (warn "tracing shared code for ~S:~% ~S"
372 function-or-name
373 fun))
374 nil)
375 ((:interpreted :interpreted-closure :funcallable-instance)
377 (trace-info-encapsulated info)))
378 (loc (if encapsulated
379 :encapsulated
380 (sb-di:debug-fun-start-location debug-fun)))
381 (info (make-trace-info
382 :what function-or-name
383 :named named
384 :encapsulated encapsulated
385 :wherein (trace-info-wherein info)
386 :methods (trace-info-methods info)
387 :condition (coerce-form (trace-info-condition info) loc)
388 :break (coerce-form (trace-info-break info) loc)
389 :report (trace-info-report info)
390 :print (coerce-form-list (trace-info-print info) loc)
391 :break-after (coerce-form (trace-info-break-after info) nil)
392 :condition-after
393 (coerce-form (trace-info-condition-after info) nil)
394 :print-after
395 (coerce-form-list (trace-info-print-after info) nil))))
397 (dolist (wherein (trace-info-wherein info))
398 (unless (or (stringp wherein)
399 (fboundp wherein))
400 (warn ":WHEREIN name ~S is not a defined global function."
401 wherein)))
403 (cond
404 (encapsulated
405 (unless named
406 (error "can't use encapsulation to trace anonymous function ~S"
407 fun))
408 (encapsulate function-or-name 'trace
409 (lambda (function &rest args)
410 (apply #'trace-call info function args))))
412 (multiple-value-bind (start-fun cookie-fun)
413 (trace-start-breakpoint-fun info)
414 (let ((start (sb-di:make-breakpoint start-fun debug-fun
415 :kind :fun-start))
416 (end (sb-di:make-breakpoint
417 (trace-end-breakpoint-fun info)
418 debug-fun :kind :fun-end
419 :fun-end-cookie cookie-fun)))
420 (setf (trace-info-start-breakpoint info) start)
421 (setf (trace-info-end-breakpoint info) end)
422 ;; The next two forms must be in the order in which they
423 ;; appear, since the start breakpoint must run before the
424 ;; fun-end breakpoint's start helper (which calls the
425 ;; cookie function.) One reason is that cookie function
426 ;; requires that the CONDITIONP shared closure variable be
427 ;; initialized.
428 (sb-di:activate-breakpoint start)
429 (sb-di:activate-breakpoint end)))))
431 (setf (gethash fun *traced-funs*) info))
433 (when (and (typep fun 'generic-function)
434 (trace-info-methods info)
435 ;; we are going to trace the method functions directly.
436 (not (trace-info-encapsulated info)))
437 (dolist (method (sb-mop:generic-function-methods fun))
438 (let ((mf (sb-mop:method-function method)))
439 ;; NOTE: this direct style of tracing methods -- tracing the
440 ;; pcl-internal method functions -- is only one possible
441 ;; alternative. It fails (a) when encapulation is
442 ;; requested, because the function objects themselves are
443 ;; stored in the method object; (b) when the method in
444 ;; question is particularly simple, when the method
445 ;; functionality is in the dfun. See src/pcl/env.lisp for a
446 ;; stub implementation of encapsulating through a
447 ;; traced-method class.
448 (trace-1 mf info)
449 (when (typep mf 'sb-pcl::%method-function)
450 (trace-1 (sb-pcl::%method-function-fast-function mf) info)))))
452 function-or-name)))
454 ;;;; the TRACE macro
456 ;;; Parse leading trace options off of SPECS, modifying INFO
457 ;;; accordingly. The remaining portion of the list is returned when we
458 ;;; encounter a plausible function name.
459 (defun parse-trace-options (specs info)
460 (let ((current specs))
461 (loop
462 (when (endp current) (return))
463 (let ((option (first current))
464 (value (cons (second current) nil)))
465 (case option
466 (:report
467 (unless (typep (car value) 'trace-report-type)
468 (error "~S is not a valid ~A ~S type."
469 (car value) 'trace :report))
470 (setf (trace-info-report info) (car value)))
471 (:condition (setf (trace-info-condition info) value))
472 (:condition-after
473 (setf (trace-info-condition info) (cons nil nil))
474 (setf (trace-info-condition-after info) value))
475 (:condition-all
476 (setf (trace-info-condition info) value)
477 (setf (trace-info-condition-after info) value))
478 (:wherein
479 (setf (trace-info-wherein info)
480 (if (listp (car value)) (car value) value)))
481 (:encapsulate
482 (setf (trace-info-encapsulated info) (car value)))
483 (:methods
484 (setf (trace-info-methods info) (car value)))
485 (:break (setf (trace-info-break info) value))
486 (:break-after (setf (trace-info-break-after info) value))
487 (:break-all
488 (setf (trace-info-break info) value)
489 (setf (trace-info-break-after info) value))
490 (:print
491 (setf (trace-info-print info)
492 (append (trace-info-print info) (list value))))
493 (:print-after
494 (setf (trace-info-print-after info)
495 (append (trace-info-print-after info) (list value))))
496 (:print-all
497 (setf (trace-info-print info)
498 (append (trace-info-print info) (list value)))
499 (setf (trace-info-print-after info)
500 (append (trace-info-print-after info) (list value))))
501 (t (return)))
502 (pop current)
503 (unless current
504 (error "missing argument to ~S TRACE option" option))
505 (pop current)))
506 current))
508 ;;; Compute the expansion of TRACE in the non-trivial case (arguments
509 ;;; specified.)
510 (defun expand-trace (specs)
511 (collect ((binds)
512 (forms))
513 (let* ((global-options (make-trace-info))
514 (current (parse-trace-options specs global-options)))
515 (loop
516 (when (endp current) (return))
517 (let ((name (pop current))
518 (options (copy-trace-info global-options)))
519 (cond
520 ((eq name :function)
521 (let ((temp (gensym)))
522 (binds `(,temp ,(pop current)))
523 (forms `(trace-1 ,temp ',options))))
524 ((and (keywordp name)
525 (not (or (fboundp name) (macro-function name))))
526 (error "unknown TRACE option: ~S" name))
527 ((stringp name)
528 (let ((package (find-undeleted-package-or-lose name)))
529 (do-all-symbols (symbol (find-package name))
530 (when (eql package (symbol-package symbol))
531 (when (and (fboundp symbol)
532 (not (macro-function symbol))
533 (not (special-operator-p symbol)))
534 (forms `(trace-1 ',symbol ',options)))
535 (let ((setf-name `(setf ,symbol)))
536 (when (fboundp setf-name)
537 (forms `(trace-1 ',setf-name ',options))))))))
538 ;; special-case METHOD: it itself is not a general function
539 ;; name symbol, but it (at least here) designates one of a
540 ;; pair of such.
541 ((and (consp name) (eq (car name) 'method))
542 (when (fboundp (list* 'sb-pcl::slow-method (cdr name)))
543 (forms `(trace-1 ',(list* 'sb-pcl::slow-method (cdr name))
544 ',options)))
545 (when (fboundp (list* 'sb-pcl::fast-method (cdr name)))
546 (forms `(trace-1 ',(list* 'sb-pcl::fast-method (cdr name))
547 ',options))))
549 (forms `(trace-1 ',name ',options))))
550 (setq current (parse-trace-options current options)))))
552 `(let ,(binds)
553 (remove nil (list ,@(forms))))))
555 (defun %list-traced-funs ()
556 (loop for x being each hash-value in *traced-funs*
557 collect (trace-info-what x)))
559 (defmacro trace (&rest specs)
560 "TRACE {Option Global-Value}* {Name {Option Value}*}*
562 TRACE is a debugging tool that provides information when specified
563 functions are called. In its simplest form:
565 (TRACE NAME-1 NAME-2 ...)
567 The NAMEs are not evaluated. Each may be a symbol, denoting an
568 individual function, or a string, denoting all functions fbound to
569 symbols whose home package is the package with the given name.
571 Options allow modification of the default behavior. Each option is a
572 pair of an option keyword and a value form. Global options are
573 specified before the first name, and affect all functions traced by a
574 given use of TRACE. Options may also be interspersed with function
575 names, in which case they act as local options, only affecting tracing
576 of the immediately preceding function name. Local options override
577 global options.
579 By default, TRACE causes a printout on *TRACE-OUTPUT* each time that
580 one of the named functions is entered or returns. (This is the basic,
581 ANSI Common Lisp behavior of TRACE.)
583 The following options are defined:
585 :REPORT Report-Type
586 If Report-Type is TRACE (the default) then information is
587 reported by printing immediately. If Report-Type is NIL, then
588 the only effect of the trace is to execute other
589 options (e.g. PRINT or BREAK).
591 :CONDITION Form
592 :CONDITION-AFTER Form
593 :CONDITION-ALL Form
594 If :CONDITION is specified, then TRACE does nothing unless Form
595 evaluates to true at the time of the call. :CONDITION-AFTER is
596 similar, but suppresses the initial printout, and is tested when the
597 function returns. :CONDITION-ALL tries both before and after.
599 :BREAK Form
600 :BREAK-AFTER Form
601 :BREAK-ALL Form
602 If specified, and Form evaluates to true, then the debugger is invoked
603 at the start of the function, at the end of the function, or both,
604 according to the respective option.
606 :PRINT Form
607 :PRINT-AFTER Form
608 :PRINT-ALL Form
609 In addition to the usual printout, the result of evaluating Form is
610 printed at the start of the function, at the end of the function, or
611 both, according to the respective option. Multiple print options cause
612 multiple values to be printed.
614 :WHEREIN Names
615 If specified, Names is a function name or list of names. TRACE does
616 nothing unless a call to one of those functions encloses the call to
617 this function (i.e. it would appear in a backtrace.) Anonymous
618 functions have string names like \"DEFUN FOO\".
620 :ENCAPSULATE {:DEFAULT | T | NIL}
621 If T, the tracing is done via encapsulation (redefining the function
622 name) rather than by modifying the function. :DEFAULT is the default,
623 and means to use encapsulation for interpreted functions and funcallable
624 instances, breakpoints otherwise. When encapsulation is used, forms are
625 *not* evaluated in the function's lexical environment, but SB-DEBUG:ARG
626 can still be used.
628 :METHODS {T | NIL}
629 If T, any function argument naming a generic function will have its
630 methods traced in addition to the generic function itself.
632 :FUNCTION Function-Form
633 This is a not really an option, but rather another way of specifying
634 what function to trace. The Function-Form is evaluated immediately,
635 and the resulting function is traced.
637 :CONDITION, :BREAK and :PRINT forms are evaluated in a context which
638 mocks up the lexical environment of the called function, so that
639 SB-DEBUG:VAR and SB-DEBUG:ARG can be used.
640 The -AFTER and -ALL forms can use SB-DEBUG:ARG."
641 (if specs
642 (expand-trace specs)
643 '(%list-traced-funs)))
645 ;;;; untracing
647 ;;; Untrace one function.
648 (defun untrace-1 (function-or-name)
649 (let* ((fun (trace-fdefinition function-or-name))
650 (info (when fun (gethash fun *traced-funs*))))
651 (cond
652 ((and fun (not info))
653 (warn "Function is not TRACEd: ~S" function-or-name))
654 ((not fun)
655 ;; Someone has FMAKUNBOUND it.
656 (let ((table *traced-funs*))
657 (with-locked-system-table (table)
658 (maphash (lambda (fun info)
659 (when (equal function-or-name (trace-info-what info))
660 (remhash fun table)))
661 table))))
663 (cond
664 ((trace-info-encapsulated info)
665 (unencapsulate (trace-info-what info) 'trace))
667 (sb-di:delete-breakpoint (trace-info-start-breakpoint info))
668 (sb-di:delete-breakpoint (trace-info-end-breakpoint info))))
669 (setf (trace-info-untraced info) t)
670 (remhash fun *traced-funs*)))))
672 ;;; Untrace all traced functions.
673 (defun untrace-all ()
674 (dolist (fun (%list-traced-funs))
675 (untrace-1 fun))
678 (defun untrace-package (name)
679 (let ((package (find-package name)))
680 (when package
681 (dolist (fun (%list-traced-funs))
682 (cond ((and (symbolp fun) (eq package (symbol-package fun)))
683 (untrace-1 fun))
684 ((and (consp fun) (eq 'setf (car fun))
685 (symbolp (second fun))
686 (eq package (symbol-package (second fun))))
687 (untrace-1 fun)))))))
689 (defmacro untrace (&rest specs)
690 "Remove tracing from the specified functions. Untraces all
691 functions when called with no arguments."
692 (if specs
693 `(progn
694 ,@(loop while specs
695 for name = (pop specs)
696 collect (cond ((eq name :function)
697 `(untrace-1 ,(pop specs)))
698 ((stringp name)
699 `(untrace-package ,name))
701 `(untrace-1 ',name))))
703 '(untrace-all)))