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