0.7.6.1:
[sbcl.git] / src / code / toplevel.lisp
1 ;;;; stuff related to the toplevel read-eval-print loop, plus some
2 ;;;; other miscellaneous functions that we don't have any better place
3 ;;;; for
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!IMPL")
15 \f
16 ;;;; magic specials initialized by GENESIS
17
18 ;;; FIXME: The DEFVAR here is redundant with the (DECLAIM (SPECIAL ..))
19 ;;; of all static symbols in early-impl.lisp.
20 (progn
21   (defvar *current-catch-block*)
22   (defvar *current-unwind-protect-block*)
23   (defvar *free-interrupt-context-index*))
24 \f
25 ;;; specials initialized by !COLD-INIT
26
27 ;;; FIXME: These could be converted to DEFVARs.
28 (declaim (special *gc-inhibit* *already-maybe-gcing*
29                   *need-to-collect-garbage*
30                   *gc-notify-stream*
31                   *before-gc-hooks* *after-gc-hooks*
32                   #!+x86 *pseudo-atomic-atomic*
33                   #!+x86 *pseudo-atomic-interrupted*
34                   sb!unix::*interrupts-enabled*
35                   sb!unix::*interrupt-pending*
36                   *type-system-initialized*))
37
38 (defvar *cold-init-complete-p*)
39
40 ;;; counts of nested errors (with internal errors double-counted)
41 (defvar *maximum-error-depth*)
42 (defvar *current-error-depth*)
43 \f
44 ;;;; miscellaneous utilities for working with with TOPLEVEL
45
46 ;;; Execute BODY in a context where any %END-OF-THE-WORLD (thrown e.g.
47 ;;; by QUIT) is caught and any final processing and return codes are
48 ;;; handled appropriately.
49 (defmacro handling-end-of-the-world (&body body)
50   (let ((caught (gensym "CAUGHT")))
51     `(let ((,caught (catch '%end-of-the-world
52                       (/show0 "inside CATCH '%END-OF-THE-WORLD")
53                       ,@body)))
54        (/show0 "back from CATCH '%END-OF-THE-WORLD, flushing output")
55        (flush-standard-output-streams)
56        (/show0 "calling UNIX-EXIT")
57        (sb!unix:unix-exit ,caught))))
58 \f
59 ;;;; working with *CURRENT-ERROR-DEPTH* and *MAXIMUM-ERROR-DEPTH*
60
61 ;;; INFINITE-ERROR-PROTECT is used by ERROR and friends to keep us out
62 ;;; of hyperspace.
63 (defmacro infinite-error-protect (&rest forms)
64   `(unless (infinite-error-protector)
65      (/show0 "back from INFINITE-ERROR-PROTECTOR")
66      (let ((*current-error-depth* (1+ *current-error-depth*)))
67        (/show0 "in INFINITE-ERROR-PROTECT, incremented error depth")
68        #+sb-show (sb-debug:backtrace)
69        ,@forms)))
70
71 ;;; a helper function for INFINITE-ERROR-PROTECT
72 (defun infinite-error-protector ()
73   (/show0 "entering INFINITE-ERROR-PROTECTOR, *CURRENT-ERROR-DEPTH*=..")
74   (/hexstr *current-error-depth*)
75   (cond ((not *cold-init-complete-p*)
76          (%primitive print "Argh! error in cold init, halting")
77          (%primitive sb!c:halt))
78         ((or (not (boundp '*current-error-depth*))
79              (not (realp   *current-error-depth*))
80              (not (boundp '*maximum-error-depth*))
81              (not (realp   *maximum-error-depth*)))
82          (%primitive print "Argh! corrupted error depth, halting")
83          (%primitive sb!c:halt))
84         ((> *current-error-depth* *maximum-error-depth*)
85          (/show0 "*MAXIMUM-ERROR-DEPTH*=..")
86          (/hexstr *maximum-error-depth*)
87          (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
88          (error-error "Help! "
89                       *current-error-depth*
90                       " nested errors. "
91                       "KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
92          t)
93         (t
94          (/show0 "returning normally from INFINITE-ERROR-PROTECTOR")
95          nil)))
96
97 ;;; FIXME: I had a badly broken version of INFINITE-ERROR-PROTECTOR at
98 ;;; one point (shown below), and SBCL cross-compiled it without
99 ;;; warning about FORMS being undefined. Check whether that problem
100 ;;; (missing warning) is repeatable in the final system and if so, fix
101 ;;; it.
102 #|
103 (defun infinite-error-protector ()
104   `(cond ((not *cold-init-complete-p*)
105           (%primitive print "Argh! error in cold init, halting")
106           (%primitive sb!c:halt))
107          ((or (not (boundp '*current-error-depth*))
108               (not (realp   *current-error-depth*))
109               (not (boundp '*maximum-error-depth*))
110               (not (realp   *maximum-error-depth*)))
111           (%primitive print "Argh! corrupted error depth, halting")
112           (%primitive sb!c:halt))
113          ((> *current-error-depth* *maximum-error-depth*)
114           (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
115           (error-error "Help! "
116                        *current-error-depth*
117                        " nested errors. "
118                        "KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
119           (progn ,@forms)
120           t)
121          (t
122           (/show0 "in INFINITE-ERROR-PROTECTOR, returning normally")
123           nil)))
124 |#
125 \f
126 ;;;; miscellaneous external functions
127
128 #!-mp ; The multi-processing version is defined in multi-proc.lisp.
129 (defun sleep (n)
130   #!+sb-doc
131   "This function causes execution to be suspended for N seconds. N may
132   be any non-negative, non-complex number."
133   (when (or (not (realp n))
134             (minusp n))
135     (error 'simple-type-error
136            :format-control "invalid argument to SLEEP: ~S"
137            :format-arguments (list n)
138            :datum n
139            :expected-type '(real 0)))
140   (multiple-value-bind (sec usec)
141       (if (integerp n)
142           (values n 0)
143           (multiple-value-bind (sec frac)
144               (truncate n)
145             (values sec (truncate frac 1e-6))))
146     (sb!unix:unix-select 0 0 0 0 sec usec))
147   nil)
148 \f
149 ;;;; SCRUB-CONTROL-STACK
150
151 (defconstant bytes-per-scrub-unit 2048)
152
153 ;;; Zero the unused portion of the control stack so that old objects
154 ;;; are not kept alive because of uninitialized stack variables.
155
156 ;;; "To summarize the problem, since not all allocated stack frame
157 ;;; slots are guaranteed to be written by the time you call an another
158 ;;; function or GC, there may be garbage pointers retained in your
159 ;;; dead stack locations.  The stack scrubbing only affects the part
160 ;;; of the stack from the SP to the end of the allocated stack."
161 ;;; - ram, on cmucl-imp, Tue, 25 Sep 2001
162
163 ;;; So, as an (admittedly lame) workaround, from time to time we call
164 ;;; scrub-control-stack to zero out all the unused portion.  This is
165 ;;; supposed to happen when the stack is mostly empty, so that we have
166 ;;; a chance of clearing more of it: callers are currently (2002.07.18)
167 ;;; REPL and SUB-GC
168
169 (defun scrub-control-stack ()
170   (declare (optimize (speed 3) (safety 0))
171            (values (unsigned-byte 20))) ; FIXME: DECLARE VALUES?
172
173   #!-stack-grows-downward-not-upward
174   (let* ((csp (sap-int (sb!c::control-stack-pointer-sap)))
175          (initial-offset (logand csp (1- bytes-per-scrub-unit)))
176          (end-of-stack
177           (- sb!vm:control-stack-end sb!c:*backend-page-size*)))
178     (labels
179         ((scrub (ptr offset count)
180            (declare (type system-area-pointer ptr)
181                     (type (unsigned-byte 16) offset)
182                     (type (unsigned-byte 20) count)
183                     (values (unsigned-byte 20)))
184            (cond ((>= (sap-int ptr) end-of-stack) 0)
185                  ((= offset bytes-per-scrub-unit)
186                   (look (sap+ ptr bytes-per-scrub-unit) 0 count))
187                  (t
188                   (setf (sap-ref-32 ptr offset) 0)
189                   (scrub ptr (+ offset sb!vm:n-word-bytes) count))))
190          (look (ptr offset count)
191            (declare (type system-area-pointer ptr)
192                     (type (unsigned-byte 16) offset)
193                     (type (unsigned-byte 20) count)
194                     (values (unsigned-byte 20)))
195            (cond ((>= (sap-int ptr) end-of-stack) 0)
196                  ((= offset bytes-per-scrub-unit)
197                   count)
198                  ((zerop (sap-ref-32 ptr offset))
199                   (look ptr (+ offset sb!vm:n-word-bytes) count))
200                  (t
201                   (scrub ptr offset (+ count sb!vm:n-word-bytes))))))
202       (declare (type (unsigned-byte 32) csp))
203       (scrub (int-sap (- csp initial-offset))
204              (* (floor initial-offset sb!vm:n-word-bytes) sb!vm:n-word-bytes)
205              0)))
206
207   #!+stack-grows-downward-not-upward
208   (let* ((csp (sap-int (sb!c::control-stack-pointer-sap)))
209          (end-of-stack (+ sb!vm:control-stack-start sb!c:*backend-page-size*))
210          (initial-offset (logand csp (1- bytes-per-scrub-unit))))
211     (labels
212         ((scrub (ptr offset count)
213            (declare (type system-area-pointer ptr)
214                     (type (unsigned-byte 16) offset)
215                     (type (unsigned-byte 20) count)
216                     (values (unsigned-byte 20)))
217            (let ((loc (int-sap (- (sap-int ptr) (+ offset sb!vm:n-word-bytes)))))
218              (cond ((<= (sap-int loc) end-of-stack) 0)
219                    ((= offset bytes-per-scrub-unit)
220                     (look (int-sap (- (sap-int ptr) bytes-per-scrub-unit))
221                           0 count))
222                    (t ;; need to fix bug in %SET-STACK-REF
223                     (setf (sap-ref-32 loc 0) 0)
224                     (scrub ptr (+ offset sb!vm:n-word-bytes) count)))))
225          (look (ptr offset count)
226            (declare (type system-area-pointer ptr)
227                     (type (unsigned-byte 16) offset)
228                     (type (unsigned-byte 20) count)
229                     (values (unsigned-byte 20)))
230            (let ((loc (int-sap (- (sap-int ptr) offset))))
231              (cond ((<= (sap-int loc) end-of-stack) 0)
232                    ((= offset bytes-per-scrub-unit)
233                     count)
234                    ((zerop (sb!kernel::get-lisp-obj-address (stack-ref loc 0)))
235                     (look ptr (+ offset sb!vm:n-word-bytes) count))
236                    (t
237                     (scrub ptr offset (+ count sb!vm:n-word-bytes)))))))
238       (declare (type (unsigned-byte 32) csp))
239       (scrub (int-sap (+ csp initial-offset))
240              (* (floor initial-offset sb!vm:n-word-bytes) sb!vm:n-word-bytes)
241              0))))
242 \f
243 ;;;; the default toplevel function
244
245 (defvar / nil
246   #!+sb-doc
247   "a list of all the values returned by the most recent top level EVAL")
248 (defvar //  nil #!+sb-doc "the previous value of /")
249 (defvar /// nil #!+sb-doc "the previous value of //")
250 (defvar *   nil #!+sb-doc "the value of the most recent top level EVAL")
251 (defvar **  nil #!+sb-doc "the previous value of *")
252 (defvar *** nil #!+sb-doc "the previous value of **")
253 (defvar +   nil #!+sb-doc "the value of the most recent top level READ")
254 (defvar ++  nil #!+sb-doc "the previous value of +")
255 (defvar +++ nil #!+sb-doc "the previous value of ++")
256 (defvar -   nil #!+sb-doc "the form currently being evaluated")
257
258 (defun interactive-eval (form)
259   "Evaluate FORM, returning whatever it returns and adjusting ***, **, *,
260    +++, ++, +, ///, //, /, and -."
261   (setf - form)
262   (let ((results (multiple-value-list (eval form))))
263     (setf /// //
264           // /
265           / results
266           *** **
267           ** *
268           * (car results)))
269   (setf +++ ++
270         ++ +
271         + -)
272   (unless (boundp '*)
273     ;; The bogon returned an unbound marker.
274     ;; FIXME: It would be safer to check every one of the values in RESULTS,
275     ;; instead of just the first one.
276     (setf * nil)
277     (cerror "Go on with * set to NIL."
278             "EVAL returned an unbound marker."))
279   (values-list /))
280
281 ;;; Flush anything waiting on one of the ANSI Common Lisp standard
282 ;;; output streams before proceeding.
283 (defun flush-standard-output-streams ()
284   (dolist (name '(*debug-io*
285                   *error-output*
286                   *query-io*
287                   *standard-output*
288                   *trace-output*))
289     (finish-output (symbol-value name)))
290   (values))
291
292 ;;; the default system top level function
293 (defun toplevel-init ()
294
295   (/show0 "entering TOPLEVEL-INIT")
296   
297   (let ((sysinit nil)        ; value of --sysinit option
298         (userinit nil)       ; value of --userinit option
299         (reversed-evals nil) ; values of --eval options, in reverse order; and
300                              ; also --load options, translated into --eval
301         (noprint nil)        ; Has a --noprint option been seen?
302         (options (rest *posix-argv*))) ; skipping program name
303
304     (/show0 "done with outer LET in TOPLEVEL-INIT")
305   
306     ;; FIXME: There are lots of ways for errors to happen around here
307     ;; (e.g. bad command line syntax, or READ-ERROR while trying to
308     ;; READ an --eval string). Make sure that they're handled
309     ;; reasonably. Also, perhaps all errors while parsing the command
310     ;; line should cause the system to QUIT, instead of trying to go
311     ;; into the Lisp debugger, since trying to go into the debugger
312     ;; gets into various annoying issues of where we should go after
313     ;; the user tries to return from the debugger.
314     
315     ;; Parse command line options.
316     (loop while options do
317           (/show0 "at head of LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
318           (let ((option (first options)))
319             (flet ((pop-option ()
320                      (if options
321                          (pop options)
322                          (error "unexpected end of command line options"))))
323               (cond ((string= option "--sysinit")
324                      (pop-option)
325                      (if sysinit
326                          (error "multiple --sysinit options")
327                          (setf sysinit (pop-option))))
328                     ((string= option "--userinit")
329                      (pop-option)
330                      (if userinit
331                          (error "multiple --userinit options")
332                          (setf userinit (pop-option))))
333                     ((string= option "--eval")
334                      (pop-option)
335                      (let ((eval-as-string (pop-option)))
336                        (with-input-from-string (eval-stream eval-as-string)
337                          (let* ((eof-marker (cons :eof :eof))
338                                 (eval (read eval-stream nil eof-marker))
339                                 (eof (read eval-stream nil eof-marker)))
340                            (cond ((eq eval eof-marker)
341                                   (error "unable to parse ~S"
342                                          eval-as-string))
343                                  ((not (eq eof eof-marker))
344                                   (error "more than one expression in ~S"
345                                          eval-as-string))
346                                  (t
347                                   (push eval reversed-evals)))))))
348                     ((string= option "--load")
349                      (pop-option)
350                      (push `(load ,(pop-option)) reversed-evals))
351                     ((string= option "--noprint")
352                      (pop-option)
353                      (setf noprint t))
354                     ;; FIXME: --noprogrammer was deprecated in 0.7.5, and
355                     ;; in a year or so this backwards compatibility can
356                     ;; go away.
357                     ((string= option "--noprogrammer")
358                      (warn "treating deprecated --noprogrammer as --disable-debugger")
359                      (pop-option)
360                      (push '(disable-debugger) reversed-evals))
361                     ((string= option "--disable-debugger")
362                      (pop-option)
363                      (push '(disable-debugger) reversed-evals))
364                     ((string= option "--end-toplevel-options")
365                      (pop-option)
366                      (return))
367                     (t
368                      ;; Anything we don't recognize as a toplevel
369                      ;; option must be the start of user-level
370                      ;; options.. except that if we encounter
371                      ;; "--end-toplevel-options" after we gave up
372                      ;; because we didn't recognize an option as a
373                      ;; toplevel option, then the option we gave up on
374                      ;; must have been an error. (E.g. in
375                      ;;  "sbcl --eval '(a)' --eval'(b)' --end-toplevel-options"
376                      ;; this test will let us detect that the string
377                      ;; "--eval(b)" is an error.)
378                      (if (find "--end-toplevel-options" options
379                                :test #'string=)
380                          (error "bad toplevel option: ~S" (first options))
381                          (return)))))))
382     (/show0 "done with LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
383
384     ;; Excise all the options that we processed, so that only
385     ;; user-level options are left visible to user code.
386     (setf (rest *posix-argv*) options)
387
388     ;; Handle initialization files.
389     (/show0 "handling initialization files in TOPLEVEL-INIT")
390     (flet (;; If any of POSSIBLE-INIT-FILE-NAMES names a real file,
391            ;; return its truename.
392            (probe-init-files (&rest possible-init-file-names)
393              (/show0 "entering PROBE-INIT-FILES")
394              (prog1
395                  (find-if (lambda (x)
396                             (and (stringp x) (probe-file x)))
397                           possible-init-file-names)
398                (/show0 "leaving PROBE-INIT-FILES"))))
399       (let* ((sbcl-home (posix-getenv "SBCL_HOME"))
400              (sysinit-truename (if sbcl-home
401                                    (probe-init-files sysinit
402                                                      (concatenate 'string
403                                                                   sbcl-home
404                                                                   "/sbclrc"))
405                                    (probe-init-files sysinit
406                                                      "/etc/sbclrc"
407                                                      "/usr/local/etc/sbclrc")))
408              (user-home (or (posix-getenv "HOME")
409                             (error "The HOME environment variable is unbound, ~
410                                     so user init file can't be found.")))
411              (userinit-truename (probe-init-files userinit
412                                                   (concatenate 'string
413                                                                user-home
414                                                                "/.sbclrc"))))
415
416         ;; We wrap all the pre-REPL user/system customized startup code 
417         ;; in a restart.
418         ;;
419         ;; (Why not wrap everything, even the stuff above, in this
420         ;; restart? Errors above here are basically command line or
421         ;; Unix environment errors, e.g. a missing file or a typo on
422         ;; the Unix command line, and you don't need to get into Lisp
423         ;; to debug them, you should just start over and do it right
424         ;; at the Unix level. Errors below here are generally errors
425         ;; in user Lisp code, and it might be helpful to let the user
426         ;; reach the REPL in order to help figure out what's going
427         ;; on.)
428         (restart-case
429             (progn
430               (flet ((process-init-file (truename)
431                        (when truename
432                          (unless (load truename)
433                            (error "~S was not successfully loaded." truename))
434                          (flush-standard-output-streams))))
435                 (process-init-file sysinit-truename)
436                 (process-init-file userinit-truename))
437
438               ;; Process --eval options.
439               (/show0 "handling --eval options in TOPLEVEL-INIT")
440               (dolist (eval (reverse reversed-evals))
441                 (/show0 "handling one --eval option in TOPLEVEL-INIT")
442                 (eval eval)
443                 (flush-standard-output-streams)))
444           (continue ()
445             :report
446             "Continue anyway (skipping to toplevel read/eval/print loop)."
447             (/show0 "CONTINUEing from pre-REPL RESTART-CASE")
448             (values)) ; (no-op, just fall through)
449           (quit ()
450             :report "Quit SBCL (calling #'QUIT, killing the process)."
451             (/show0 "falling through to QUIT from pre-REPL RESTART-CASE")
452             (quit))))
453
454       ;; one more time for good measure, in case we fell out of the
455       ;; RESTART-CASE above before one of the flushes in the ordinary
456       ;; flow of control had a chance to operate
457       (flush-standard-output-streams)
458
459       (/show0 "falling into TOPLEVEL-REPL from TOPLEVEL-INIT")
460       (toplevel-repl noprint)
461       ;; (classic CMU CL error message: "You're certainly a clever child.":-)
462       (critically-unreachable "after TOPLEVEL-REPL"))))
463
464 ;;; halt-on-failures and prompt-on-failures modes, suitable for
465 ;;; noninteractive and interactive use respectively
466 (defun disable-debugger ()
467   (setf *debugger-hook* 'noprogrammer-debugger-hook-fun
468         *debug-io* *error-output*))
469 (defun enable-debugger ()
470   (setf *debugger-hook* nil
471         *debug-io* *query-io*))
472
473 ;;; read-eval-print loop for the default system toplevel
474 (defun toplevel-repl (noprint)
475   (/show0 "entering TOPLEVEL-REPL")
476   (let ((* nil) (** nil) (*** nil)
477         (- nil)
478         (+ nil) (++ nil) (+++ nil)
479         (/// nil) (// nil) (/ nil))
480     ;; WITH-SIMPLE-RESTART doesn't actually restart its body as some
481     ;; (like WHN for an embarrassingly long time ca. 2001-12-07) might
482     ;; think, but instead drops control back out at the end. So when a
483     ;; TOPLEVEL or outermost-ABORT restart happens, we need this outer
484     ;; LOOP wrapper to grab control and start over again. (And it also
485     ;; wraps CATCH 'TOPLEVEL-CATCHER for similar reasons.)
486     (loop
487      (/show0 "about to set up restarts in TOPLEVEL-REPL")
488      ;; There should only be one TOPLEVEL restart, and it's here, so
489      ;; restarting at TOPLEVEL always bounces you all the way out here.
490      (with-simple-restart (toplevel
491                            "Restart at toplevel READ/EVAL/PRINT loop.")
492        ;; We add a new ABORT restart for every debugger level, so 
493        ;; restarting at ABORT in a nested debugger gets you out to the
494        ;; innermost enclosing debugger, and only when you're in the
495        ;; outermost, unnested debugger level does restarting at ABORT 
496        ;; get you out to here.
497        (with-simple-restart
498            (abort
499             "Reduce debugger level (leaving debugger, returning to toplevel).")
500          (catch 'toplevel-catcher
501            #!-sunos (sb!unix:unix-sigsetmask 0) ; FIXME: What is this for?
502            ;; in the event of a control-stack-exhausted-error, we should
503            ;; have unwound enough stack by the time we get here that this
504            ;; is now possible
505            (sb!kernel::protect-control-stack-guard-page 1)
506            (repl noprint)
507            (critically-unreachable "after REPL")))))))
508
509 (defun repl (noprint)
510   (/show0 "entering REPL")
511   (let ((eof-marker (cons :eof nil)))
512     (loop
513      ;; see comment preceding definition of SCRUB-CONTROL-STACK
514      (scrub-control-stack)
515      (unless noprint
516        (fresh-line)
517        (write-string "* ") ; arbitrary but customary REPL prompt
518        (flush-standard-output-streams))
519      (let ((form (read *standard-input* nil eof-marker)))
520        (cond ((eq form eof-marker)
521               (/show0 "doing QUIT for EOF in REPL")
522               (quit))
523              (t
524               (let ((results (multiple-value-list (interactive-eval form))))
525                 (unless noprint
526                   (dolist (result results)
527                     (fresh-line)
528                     (prin1 result))))))))))
529
530 ;;; suitable value for *DEBUGGER-HOOK* for a noninteractive Unix-y program
531 (defun noprogrammer-debugger-hook-fun (condition old-debugger-hook)
532   (declare (ignore old-debugger-hook))
533   (flet ((failure-quit (&key recklessly-p)
534            (/show0 "in FAILURE-QUIT (in --disable-debugger debugger hook)")
535            (quit :unix-status 1 :recklessly-p recklessly-p)))
536     ;; This HANDLER-CASE is here mostly to stop output immediately
537     ;; (and fall through to QUIT) when there's an I/O error. Thus,
538     ;; when we're run under a shell script or something, we can die
539     ;; cleanly when the script dies (and our pipes are cut), instead
540     ;; of falling into ldb or something messy like that.
541     (handler-case
542         (progn
543           (format *error-output*
544                   "~&~@<unhandled condition (of type ~S): ~2I~_~A~:>~2%"
545                   (type-of condition)
546                   condition)
547           ;; Flush *ERROR-OUTPUT* even before the BACKTRACE, so that
548           ;; even if we hit an error within BACKTRACE (e.g. a bug in
549           ;; the debugger's own frame-walking code, or a bug in a user
550           ;; PRINT-OBJECT method) we'll at least have the CONDITION
551           ;; printed out before we die.
552           (finish-output *error-output*)
553           ;; (Where to truncate the BACKTRACE is of course arbitrary, but
554           ;; it seems as though we should at least truncate it somewhere.)
555           (sb!debug:backtrace 128 *error-output*)
556           (format
557            *error-output*
558            "~%unhandled condition in --disable-debugger mode, quitting~%")
559           (finish-output *error-output*)
560           (failure-quit))
561       (condition ()
562         ;; We IGNORE-ERRORS here because even %PRIMITIVE PRINT can
563         ;; fail when our output streams are blown away, as e.g. when
564         ;; we're running under a Unix shell script and it dies somehow
565         ;; (e.g. because of a SIGINT). In that case, we might as well
566         ;; just give it up for a bad job, and stop trying to notify
567         ;; the user of anything.
568         ;;
569         ;; Actually, the only way I've run across to exercise the
570         ;; problem is to have more than one layer of shell script.
571         ;; I have a shell script which does
572         ;;   time nice -10 sh make.sh "$1" 2>&1 | tee make.tmp
573         ;; and the problem occurs when I interrupt this with Ctrl-C
574         ;; under Linux 2.2.14-5.0 and GNU bash, version 1.14.7(1).
575         ;; I haven't figured out whether it's bash, time, tee, Linux, or
576         ;; what that is responsible, but that it's possible at all
577         ;; means that we should IGNORE-ERRORS here. -- WHN 2001-04-24
578         (ignore-errors
579          (%primitive print
580                      "Argh! error within --disable-debugger error handling"))
581         (failure-quit :recklessly-p t)))))
582 \f
583 ;;; a convenient way to get into the assembly-level debugger
584 (defun %halt ()
585   (%primitive sb!c:halt))