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