1.0.19.7: refactor stack allocation decisions
[sbcl.git] / src / code / step.lisp
1 ;;;; single stepper for SBCL
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 ;;;; Single stepping works by having compiler insert STEP-CONDITION
13 ;;;; signalling forms into code compiled at high debug settings, and
14 ;;;; having a handler for them at the toplevel.
15
16 (in-package "SB-IMPL") ; in warm SBCL
17
18 (defun step-form (form args)
19   (restart-case
20       (signal 'step-form-condition
21               :form form
22               :args args)
23     (step-continue ()
24       :report "Resume normal execution"
25       (disable-stepping)
26       (setf *step-out* nil))
27     (step-out ()
28       :report "Resume stepping after returning from this function"
29       (ecase *step-out*
30         ((nil)
31          (error "Can't STEP-OUT: No STEP-IN on the call-stack"))
32         ((t :maybe)
33          (disable-stepping)
34          (setf *step-out* t)))
35       nil)
36     (step-next ()
37       :report "Step over call"
38       nil)
39     (step-into ()
40       :report "Step into call"
41       t)))
42
43 (defun step-values (form &rest values)
44   (declare (truly-dynamic-extent values))
45   (signal 'step-values-condition :form form :result values)
46   (values-list values))
47
48 (defun step-finished ()
49   (restart-case
50       (signal 'step-finished-condition)
51     (continue ())))
52
53 (defgeneric single-step (condition))
54
55 (defmethod single-step ((condition step-values-condition))
56   (let ((values (step-condition-result condition)))
57     (format *debug-io* "; ~A => ~:[#<no value>~;~{~S~^, ~}~]~%"
58             (step-condition-form condition)
59             values values)))
60
61 (defmethod single-step ((condition step-form-condition))
62   (let ((form (step-condition-form condition))
63         (args (step-condition-args condition)))
64     (let ((*print-circle* t)
65           (*print-pretty* t)
66           (*print-readably* nil))
67       (format *debug-io*
68               "; Evaluating call:~%~<; ~@;  ~A~:>~%~
69                ; ~:[With arguments:~%~<; ~@;~{  ~S~^~%~}~:>~;With unknown arguments~]~%"
70               (list form)
71               (eq args :unknown)
72               (list args)))
73     (finish-output *debug-io*)
74     (let ((*stack-top-hint* (sb-di::find-stepped-frame)))
75       (invoke-debugger condition))))
76
77 ;;; In the TTY debugger we're not interested in STEP returning
78 (defmethod single-step ((condition step-finished-condition))
79   (values))
80
81 (defvar *stepper-hook* 'single-step
82   #+sb-doc "Customization hook for alternative single-steppers.
83 *STEPPER-HOOK* is bound to NIL prior to calling the bound function
84 with the STEP-CONDITION as argument.")
85
86 (defun invoke-stepper (condition)
87   (when (and (stepping-enabled-p)
88              *stepper-hook*)
89     (with-stepping-disabled
90       (let ((hook *stepper-hook*)
91             (*stepper-hook* nil))
92         (funcall hook condition)))))
93
94 (defmacro step (form)
95   #+sb-doc
96   "The form is evaluated with single stepping enabled. Function calls
97 outside the lexical scope of the form can be stepped into only if the
98 functions in question have been compiled with sufficient DEBUG policy
99 to be at least partially steppable."
100   `(locally
101     (declare (optimize debug (sb-c:insert-step-conditions 0)))
102      ;; Allow stepping out of the STEP form.
103      (let ((*step-out* :maybe))
104        (unwind-protect
105             (with-stepping-enabled
106               (multiple-value-prog1
107                   (locally (declare (optimize (sb-c:insert-step-conditions 3)))
108                     ,form)
109                 (step-finished)))))))
110