0.8.12.7: Merge package locks, AKA "what can go wrong with a 3783 line patch?"
[sbcl.git] / src / code / debug.lisp
index b3876e8..b7c1306 100644 (file)
 ;;;         to satisfy lambda list
 ;;;           #:
 ;;;         exactly 2 expected, but 5 found
+;;;
+;;; FIXME: These variables were deprecated in late February 2004, and
+;;; can probably be removed in about a year.
 (defvar *debug-print-level* 5
   #!+sb-doc
-  "*PRINT-LEVEL* for the debugger")
+  "(This is deprecated in favor of *DEBUG-PRINT-VARIABLE-ALIST*.)
+
+*PRINT-LEVEL* for the debugger")
 (defvar *debug-print-length* 7
   #!+sb-doc
-  "*PRINT-LENGTH* for the debugger")
+  "(This is deprecated in favor of *DEBUG-PRINT-VARIABLE-ALIST*.)
+
+*PRINT-LENGTH* for the debugger")
+
+(defvar *debug-print-variable-alist* nil
+  #!+sb-doc
+  "an association list describing new bindings for special variables
+(typically *PRINT-FOO* variables) to be used within the debugger, e.g.
+((*PRINT-LENGTH* . 10) (*PRINT-LEVEL* . 6) (*PRINT-PRETTY* . NIL))")
 
 (defvar *debug-readtable*
   ;; KLUDGE: This can't be initialized in a cold toplevel form,
@@ -639,44 +652,29 @@ Other commands:
    of this variable to the function because it binds *DEBUGGER-HOOK* to NIL
    around the invocation.")
 
+(defvar *invoke-debugger-hook* nil
+  #!+sb-doc
+  "This is either NIL or a designator for a function of two arguments,
+   to be run when the debugger is about to be entered.  The function is
+   run with *INVOKE-DEBUGGER-HOOK* bound to NIL to minimize recursive
+   errors, and receives as arguments the condition that triggered 
+   debugger entry and the previous value of *INVOKE-DEBUGGER-HOOK*   
+
+   This mechanism is an SBCL extension similar to the standard *DEBUGGER-HOOK*.
+   In contrast to *DEBUGGER-HOOK*, it is observed by INVOKE-DEBUGGER even when
+   called by BREAK.")
+
 ;;; These are bound on each invocation of INVOKE-DEBUGGER.
 (defvar *debug-restarts*)
 (defvar *debug-condition*)
 (defvar *nested-debug-condition*)
 
-;;; the ordinary ANSI case of INVOKE-DEBUGGER, when not suppressed by
-;;; command-line --disable-debugger option
-(defun invoke-debugger/enabled (condition)
-  #!+sb-doc
-  "Enter the debugger."
-  (let ((old-hook *debugger-hook*))
-    (when old-hook
-      (let ((*debugger-hook* nil))
-       (funcall old-hook condition old-hook))))
-
-  ;; If we're a background thread and *background-threads-wait-for-debugger*
-  ;; is NIL, this will invoke a restart
-
-  ;; Note: CMU CL had (SB-UNIX:UNIX-SIGSETMASK 0) here. I deleted it
-  ;; around sbcl-0.7.8.5 (by which time it had mutated to have a
-  ;; #!-SUNOS prefix and a FIXME note observing that it wasn't needed
-  ;; on SunOS and no one knew why it was needed anywhere else either).
-  ;; So if something mysteriously breaks that has worked since the CMU
-  ;; CL days, that might be why. -- WHN 2002-09-28
-
-  ;; We definitely want *PACKAGE* to be of valid type.
-  ;;
-  ;; Elsewhere in the system, we use the SANE-PACKAGE function for
-  ;; this, but here causing an exception just as we're trying to handle
-  ;; an exception would be confusing, so instead we use a special hack.
-  (unless (and (packagep *package*)
-              (package-name *package*))
-    (setf *package* (find-package :cl-user))
-    (format *error-output*
-           "The value of ~S was not an undeleted PACKAGE. It has been
-reset to ~S."
-           '*package* *package*))
-
+;;; Oh, what a tangled web we weave when we preserve backwards
+;;; compatibility with 1968-style use of global variables to control
+;;; per-stream i/o properties; there's really no way to get this
+;;; quite right, but we do what we can.
+(defun funcall-with-debug-io-syntax (fun &rest rest)
+  (declare (type function fun))
   ;; Try to force the other special variables into a useful state.
   (let (;; Protect from WITH-STANDARD-IO-SYNTAX some variables where
        ;; any default we might use is less useful than just reusing
@@ -684,10 +682,7 @@ reset to ~S."
        (original-package *package*)
        (original-print-pretty *print-pretty*))
     (with-standard-io-syntax
-     (let ((*debug-condition* condition)
-          (*debug-restarts* (compute-restarts condition))
-          (*nested-debug-condition* nil)
-          ;; We want the printer and reader to be in a useful state,
+     (let (;; We want the printer and reader to be in a useful state,
           ;; regardless of where the debugger was invoked in the
           ;; program. WITH-STANDARD-IO-SYNTAX did much of what we
           ;; want, but
@@ -705,92 +700,165 @@ reset to ~S."
           ;; We try to address all these issues with explicit
           ;; rebindings here.
           (sb!kernel:*current-level-in-print* 0)
+          (*package* original-package)
+          (*print-pretty* original-print-pretty)
+          (*print-readably* nil)
+          ;; Clear the circularity machinery to try to to reduce the
+          ;; pain from sharing the circularity table across all
+          ;; streams; if these are not rebound here, then setting
+          ;; *PRINT-CIRCLE* within the debugger when debugging in a
+          ;; state where something circular was being printed (e.g.,
+          ;; because the debugger was entered on an error in a
+          ;; PRINT-OBJECT method) makes a hopeless mess. Binding them
+          ;; here does seem somewhat ugly because it makes it more
+          ;; difficult to debug the printing-of-circularities code
+          ;; itself; however, as far as I (WHN, 2004-05-29) can see,
+          ;; that's almost entirely academic as long as there's one
+          ;; shared *C-H-T* for all streams (i.e., it's already
+          ;; unreasonably difficult to debug print-circle machinery
+          ;; given the buggy crosstalk between the debugger streams
+          ;; and the stream you're trying to watch), and any fix for
+          ;; that buggy arrangement will likely let this hack go away
+          ;; naturally.
+          (sb!impl::*circularity-hash-table* . nil)
+          (sb!impl::*circularity-counter* . nil)
+          ;; These rebindings are now (as of early 2004) deprecated,
+          ;; with the new *PRINT-VAR-ALIST* mechanism preferred.
           (*print-length* *debug-print-length*)
           (*print-level* *debug-print-level*)
-          (*readtable* *debug-readtable*)
-          (*print-readably* nil)
-          (*package* original-package)
-          (background-p nil)
-          (*print-pretty* original-print-pretty))
-
-       ;; Before we start our own output, finish any pending output.
-       ;; Otherwise, if the user tried to track the progress of his
-       ;; program using PRINT statements, he'd tend to lose the last
-       ;; line of output or so, which'd be confusing.
-       (flush-standard-output-streams)
-
-       ;; (The initial output here goes to *ERROR-OUTPUT*, because the
-       ;; initial output is not interactive, just an error message,
-       ;; and when people redirect *ERROR-OUTPUT*, they could
-       ;; reasonably expect to see error messages logged there,
-       ;; regardless of what the debugger does afterwards.)
-       (handler-case
-          (format *error-output*
-                  "~2&~@<debugger invoked on condition of type ~S: ~
+          (*readtable* *debug-readtable*))
+       (progv
+          ;; (Why NREVERSE? PROGV makes the later entries have
+          ;; precedence over the earlier entries.
+          ;; *DEBUG-PRINT-VARIABLE-ALIST* is called an alist, so it's
+          ;; expected that its earlier entries have precedence. And
+          ;; the earlier-has-precedence behavior is mostly more
+          ;; convenient, so that programmers can use PUSH or LIST* to
+          ;; customize *DEBUG-PRINT-VARIABLE-ALIST*.)
+          (nreverse (mapcar #'car *debug-print-variable-alist*))
+          (nreverse (mapcar #'cdr *debug-print-variable-alist*))
+        (apply fun rest))))))
+
+;;; the ordinary ANSI case of INVOKE-DEBUGGER, when not suppressed by
+;;; command-line --disable-debugger option
+(defun invoke-debugger (condition)
+  #!+sb-doc
+  "Enter the debugger."
+
+  (let ((old-hook *debugger-hook*))
+    (when old-hook
+      (let ((*debugger-hook* nil))
+       (funcall old-hook condition old-hook))))
+  (let ((old-hook *invoke-debugger-hook*))
+    (when old-hook
+      (let ((*invoke-debugger-hook* nil))
+       (funcall old-hook condition old-hook))))
+
+  ;; Note: CMU CL had (SB-UNIX:UNIX-SIGSETMASK 0) here, to reset the
+  ;; signal state in the case that we wind up in the debugger as a
+  ;; result of something done by a signal handler.  It's not
+  ;; altogether obvious that this is necessary, and indeed SBCL has
+  ;; not been doing it since 0.7.8.5.  But nobody seems altogether
+  ;; convinced yet
+  ;; -- dan 2003.11.11, based on earlier comment of WHN 2002-09-28
+
+  ;; We definitely want *PACKAGE* to be of valid type.
+  ;;
+  ;; Elsewhere in the system, we use the SANE-PACKAGE function for
+  ;; this, but here causing an exception just as we're trying to handle
+  ;; an exception would be confusing, so instead we use a special hack.
+  (unless (and (packagep *package*)
+              (package-name *package*))
+    (setf *package* (find-package :cl-user))
+    (format *error-output*
+           "The value of ~S was not an undeleted PACKAGE. It has been
+reset to ~S."
+           '*package* *package*))
+
+  ;; Before we start our own output, finish any pending output.
+  ;; Otherwise, if the user tried to track the progress of his program
+  ;; using PRINT statements, he'd tend to lose the last line of output
+  ;; or so, which'd be confusing.
+  (flush-standard-output-streams)
+
+  (funcall-with-debug-io-syntax #'%invoke-debugger condition))
+
+(defun %invoke-debugger (condition)
+  
+  (let ((*debug-condition* condition)
+       (*debug-restarts* (compute-restarts condition))
+       (*nested-debug-condition* nil))
+    (handler-case
+       ;; (The initial output here goes to *ERROR-OUTPUT*, because the
+       ;; initial output is not interactive, just an error message, and
+       ;; when people redirect *ERROR-OUTPUT*, they could reasonably
+       ;; expect to see error messages logged there, regardless of what
+       ;; the debugger does afterwards.)
+       (format *error-output*
+               "~2&~@<debugger invoked on a ~S in thread ~A: ~
                     ~2I~_~A~:>~%"
-                  (type-of *debug-condition*)
-                  *debug-condition*)
-        (error (condition)
-           (setf *nested-debug-condition* condition)
-          (let ((ndc-type (type-of *nested-debug-condition*)))
-            (format *error-output*
-                    "~&~@<(A ~S was caught when trying to print ~S when ~
+               (type-of *debug-condition*)
+               (sb!thread:current-thread-id)
+               *debug-condition*)
+      (error (condition)
+       (setf *nested-debug-condition* condition)
+       (let ((ndc-type (type-of *nested-debug-condition*)))
+         (format *error-output*
+                 "~&~@<(A ~S was caught when trying to print ~S when ~
                       entering the debugger. Printing was aborted and the ~
                       ~S was stored in ~S.)~@:>~%"
-                    ndc-type
-                    '*debug-condition*
-                    ndc-type
-                    '*nested-debug-condition*))
-          (when (typep condition 'cell-error)
-            ;; what we really want to know when it's e.g. an UNBOUND-VARIABLE:
-            (format *error-output*
-                    "~&(CELL-ERROR-NAME ~S) = ~S~%"
-                    '*debug-condition*
-                    (cell-error-name *debug-condition*)))))
-
-       ;; After the initial error/condition/whatever announcement to
-       ;; *ERROR-OUTPUT*, we become interactive, and should talk on
-       ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
-       ;; statement, not a description of reality.:-| There's a lot of
-       ;; older debugger code which was written to do i/o on whatever
-       ;; stream was in fashion at the time, and not all of it has
-       ;; been converted to behave this way. -- WHN 2000-11-16)
-
-       (setf background-p
-            (sb!thread::debugger-wait-until-foreground-thread *debug-io*))
-       (unwind-protect
-       (let (;; FIXME: Rebinding *STANDARD-OUTPUT* here seems wrong,
-            ;; violating the principle of least surprise, and making
-            ;; it impossible for the user to do reasonable things
-            ;; like using PRINT at the debugger prompt to send output
-            ;; to the program's ordinary (possibly
-            ;; redirected-to-a-file) *STANDARD-OUTPUT*. (CMU CL
-            ;; used to rebind *STANDARD-INPUT* here too, but that's
-            ;; been fixed already.)
-            (*standard-output* *debug-io*)
-            ;; This seems reasonable: e.g. if the user has redirected
-            ;; *ERROR-OUTPUT* to some log file, it's probably wrong
-            ;; to send errors which occur in interactive debugging to
-            ;; that file, and right to send them to *DEBUG-IO*.
-            (*error-output* *debug-io*))
-        (unless (typep condition 'step-condition)
-          (when *debug-beginner-help-p*
-            (format *debug-io*
-                    "~%~@<Within the debugger, you can type HELP for help. ~
-                      At any command prompt (within the debugger or not) you ~
-                      can type (SB-EXT:QUIT) to terminate the SBCL ~
-                      executable. The condition which caused the debugger to ~
-                      be entered is bound to ~S. You can suppress this ~
-                      message by clearing ~S.~:@>~2%"
-                    '*debug-condition*
-                    '*debug-beginner-help-p*))
-          (show-restarts *debug-restarts* *debug-io*))
-             (internal-debug))
-        (when background-p (sb!thread::release-foreground)))))))
-
-;;; the degenerate case of INVOKE-DEBUGGER, when ordinary ANSI behavior
-;;; has been suppressed by command-line --disable-debugger option
-(defun invoke-debugger/disabled (condition)
+                 ndc-type
+                 '*debug-condition*
+                 ndc-type
+                 '*nested-debug-condition*))
+       (when (typep condition 'cell-error)
+         ;; what we really want to know when it's e.g. an UNBOUND-VARIABLE:
+         (format *error-output*
+                 "~&(CELL-ERROR-NAME ~S) = ~S~%"
+                 '*debug-condition*
+                 (cell-error-name *debug-condition*)))))
+
+    (let ((background-p (sb!thread::debugger-wait-until-foreground-thread
+                        *debug-io*)))
+
+      ;; After the initial error/condition/whatever announcement to
+      ;; *ERROR-OUTPUT*, we become interactive, and should talk on
+      ;; *DEBUG-IO* from now on. (KLUDGE: This is a normative
+      ;; statement, not a description of reality.:-| There's a lot of
+      ;; older debugger code which was written to do i/o on whatever
+      ;; stream was in fashion at the time, and not all of it has
+      ;; been converted to behave this way. -- WHN 2000-11-16)
+
+      (unwind-protect
+          (let (;; FIXME: Rebinding *STANDARD-OUTPUT* here seems wrong,
+                ;; violating the principle of least surprise, and making
+                ;; it impossible for the user to do reasonable things
+                ;; like using PRINT at the debugger prompt to send output
+                ;; to the program's ordinary (possibly
+                ;; redirected-to-a-file) *STANDARD-OUTPUT*. (CMU CL
+                ;; used to rebind *STANDARD-INPUT* here too, but that's
+                ;; been fixed already.)
+                (*standard-output* *debug-io*)
+                ;; This seems reasonable: e.g. if the user has redirected
+                ;; *ERROR-OUTPUT* to some log file, it's probably wrong
+                ;; to send errors which occur in interactive debugging to
+                ;; that file, and right to send them to *DEBUG-IO*.
+                (*error-output* *debug-io*))
+            (unless (typep condition 'step-condition)
+              (when *debug-beginner-help-p*
+                (format *debug-io*
+                        "~%~@<You can type HELP for debugger help, or ~
+                               (SB-EXT:QUIT) to exit from SBCL.~:@>~2%"))
+              (show-restarts *debug-restarts* *debug-io*))
+            (internal-debug))
+       (when background-p
+         (sb!thread::release-foreground))))))
+
+;;; this function is for use in *INVOKE-DEBUGGER-HOOK* when ordinary
+;;; ANSI behavior has been suppressed by the "--disable-debugger"
+;;; command-line option
+(defun debugger-disabled-hook (condition me)
+  (declare (ignore me))
   ;; There is no one there to interact with, so report the
   ;; condition and terminate the program.
   (flet ((failure-quit (&key recklessly-p)
@@ -848,13 +916,15 @@ reset to ~S."
 ;;; halt-on-failures and prompt-on-failures modes, suitable for
 ;;; noninteractive and interactive use respectively
 (defun disable-debugger ()
-  (setf (fdefinition 'invoke-debugger) #'invoke-debugger/disabled
-       *debug-io* *error-output*))
+  (when (eql *invoke-debugger-hook* nil)
+    (setf *debug-io* *error-output*
+         *invoke-debugger-hook* 'debugger-disabled-hook)))
+
 (defun enable-debugger ()
-  (setf (fdefinition 'invoke-debugger) #'invoke-debugger/enabled
-       *debug-io* *query-io*))
-;;; The enabled mode is the ANSI default.
-(enable-debugger)
+  (when (eql *invoke-debugger-hook* 'debugger-disabled-hook)
+    (setf *invoke-debugger-hook* nil)))
+
+(setf *debug-io* *query-io*)
 
 (defun show-restarts (restarts s)
   (cond ((null restarts)
@@ -885,6 +955,9 @@ reset to ~S."
                      (push name names-used))))
             (incf count))))))
 
+(defvar *debug-loop-fun* #'debug-loop-fun
+  "a function taking no parameters that starts the low-level debug loop")
+
 ;;; This calls DEBUG-LOOP, performing some simple initializations
 ;;; before doing so. INVOKE-DEBUGGER calls this to actually get into
 ;;; the debugger. SB!KERNEL::ERROR-ERROR calls this in emergencies
@@ -955,9 +1028,6 @@ reset to ~S."
                        (t
                         (funcall cmd-fun))))))))))))
 
-(defvar *debug-loop-fun* #'debug-loop-fun
-  "a function taking no parameters that starts the low-level debug loop")
-
 ;;; FIXME: We could probably use INTERACTIVE-EVAL for much of this logic.
 (defun debug-eval-print (expr)
   (/noshow "entering DEBUG-EVAL-PRINT" expr)