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