0.pre8.28
[sbcl/lichteblau.git] / src / code / toplevel.lisp
blob1fa44d91918a8911f1ac8010259550b06b82664e
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
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.
14 (in-package "SB!IMPL")
16 ;;;; magic specials initialized by GENESIS
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*))
25 ;;; specials initialized by !COLD-INIT
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*))
38 (defvar *cold-init-complete-p*)
40 ;;; counts of nested errors (with internal errors double-counted)
41 (defvar *maximum-error-depth*)
42 (defvar *current-error-depth*)
44 ;;;; miscellaneous utilities for working with with TOPLEVEL
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))))
59 ;;;; working with *CURRENT-ERROR-DEPTH* and *MAXIMUM-ERROR-DEPTH*
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 ;; arbitrary truncation
69 #!+sb-show (sb!debug:backtrace 8)
70 ,@forms)))
72 ;;; a helper function for INFINITE-ERROR-PROTECT
73 (defun infinite-error-protector ()
74 (/show0 "entering INFINITE-ERROR-PROTECTOR, *CURRENT-ERROR-DEPTH*=..")
75 (/hexstr *current-error-depth*)
76 (cond ((not *cold-init-complete-p*)
77 (%primitive print "Argh! error in cold init, halting")
78 (%primitive sb!c:halt))
79 ((or (not (boundp '*current-error-depth*))
80 (not (realp *current-error-depth*))
81 (not (boundp '*maximum-error-depth*))
82 (not (realp *maximum-error-depth*)))
83 (%primitive print "Argh! corrupted error depth, halting")
84 (%primitive sb!c:halt))
85 ((> *current-error-depth* *maximum-error-depth*)
86 (/show0 "*MAXIMUM-ERROR-DEPTH*=..")
87 (/hexstr *maximum-error-depth*)
88 (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
89 (error-error "Help! "
90 *current-error-depth*
91 " nested errors. "
92 "KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
95 (/show0 "returning normally from INFINITE-ERROR-PROTECTOR")
96 nil)))
98 ;;; FIXME: I had a badly broken version of INFINITE-ERROR-PROTECTOR at
99 ;;; one point (shown below), and SBCL cross-compiled it without
100 ;;; warning about FORMS being undefined. Check whether that problem
101 ;;; (missing warning) is repeatable in the final system and if so, fix
102 ;;; it.
104 (defun infinite-error-protector ()
105 `(cond ((not *cold-init-complete-p*)
106 (%primitive print "Argh! error in cold init, halting")
107 (%primitive sb!c:halt))
108 ((or (not (boundp '*current-error-depth*))
109 (not (realp *current-error-depth*))
110 (not (boundp '*maximum-error-depth*))
111 (not (realp *maximum-error-depth*)))
112 (%primitive print "Argh! corrupted error depth, halting")
113 (%primitive sb!c:halt))
114 ((> *current-error-depth* *maximum-error-depth*)
115 (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
116 (error-error "Help! "
117 *current-error-depth*
118 " nested errors. "
119 "KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
120 (progn ,@forms)
123 (/show0 "in INFINITE-ERROR-PROTECTOR, returning normally")
124 nil)))
127 ;;;; miscellaneous external functions
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)
149 ;;;; SCRUB-CONTROL-STACK
151 (defconstant bytes-per-scrub-unit 2048)
153 ;;; Zero the unused portion of the control stack so that old objects
154 ;;; are not kept alive because of uninitialized stack variables.
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
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
169 (defun scrub-control-stack ()
170 (declare (optimize (speed 3) (safety 0))
171 (values (unsigned-byte 20))) ; FIXME: DECLARE VALUES?
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))
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))
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)))
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))
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))))
243 ;;;; the default toplevel function
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")
258 (defun interactive-eval (form)
259 "Evaluate FORM, returning whatever it returns and adjusting ***, **, *,
260 +++, ++, +, ///, //, /, and -."
261 (setf - form)
262 (let ((results
263 (multiple-value-list
264 (eval-in-lexenv form
265 (make-null-interactive-lexenv)))))
266 (setf /// //
267 // /
268 / results
269 *** **
270 ** *
271 * (car results)))
272 (setf +++ ++
273 ++ +
274 + -)
275 (unless (boundp '*)
276 ;; The bogon returned an unbound marker.
277 ;; FIXME: It would be safer to check every one of the values in RESULTS,
278 ;; instead of just the first one.
279 (setf * nil)
280 (cerror "Go on with * set to NIL."
281 "EVAL returned an unbound marker."))
282 (values-list /))
284 ;;; Flush anything waiting on one of the ANSI Common Lisp standard
285 ;;; output streams before proceeding.
286 (defun flush-standard-output-streams ()
287 (dolist (name '(*debug-io*
288 *error-output*
289 *query-io*
290 *standard-output*
291 *trace-output*))
292 (finish-output (symbol-value name)))
293 (values))
295 ;;; the default system top level function
296 (defun toplevel-init ()
298 (/show0 "entering TOPLEVEL-INIT")
299 (setf sb!thread::*session-lock* (sb!thread:make-mutex :name "the terminal"))
300 (sb!thread::get-foreground)
301 (let ((sysinit nil) ; value of --sysinit option
302 (userinit nil) ; value of --userinit option
303 (reversed-evals nil) ; values of --eval options, in reverse order; and
304 ; also --load options, translated into --eval
305 (noprint nil) ; Has a --noprint option been seen?
306 (options (rest *posix-argv*))) ; skipping program name
308 (declare (type list options))
310 (/show0 "done with outer LET in TOPLEVEL-INIT")
312 ;; FIXME: There are lots of ways for errors to happen around here
313 ;; (e.g. bad command line syntax, or READ-ERROR while trying to
314 ;; READ an --eval string). Make sure that they're handled
315 ;; reasonably. Also, perhaps all errors while parsing the command
316 ;; line should cause the system to QUIT, instead of trying to go
317 ;; into the Lisp debugger, since trying to go into the debugger
318 ;; gets into various annoying issues of where we should go after
319 ;; the user tries to return from the debugger.
321 ;; Parse command line options.
322 (loop while options do
323 (/show0 "at head of LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
324 (let ((option (first options)))
325 (flet ((pop-option ()
326 (if options
327 (pop options)
328 (error "unexpected end of command line options"))))
329 (cond ((string= option "--sysinit")
330 (pop-option)
331 (if sysinit
332 (error "multiple --sysinit options")
333 (setf sysinit (pop-option))))
334 ((string= option "--userinit")
335 (pop-option)
336 (if userinit
337 (error "multiple --userinit options")
338 (setf userinit (pop-option))))
339 ((string= option "--eval")
340 (pop-option)
341 (let ((eval-as-string (pop-option)))
342 (with-input-from-string (eval-stream eval-as-string)
343 (let* ((eof-marker (cons :eof :eof))
344 (eval (read eval-stream nil eof-marker))
345 (eof (read eval-stream nil eof-marker)))
346 (cond ((eq eval eof-marker)
347 (error "unable to parse ~S"
348 eval-as-string))
349 ((not (eq eof eof-marker))
350 (error "more than one expression in ~S"
351 eval-as-string))
353 (push eval reversed-evals)))))))
354 ((string= option "--load")
355 (pop-option)
356 (push `(load ,(pop-option)) reversed-evals))
357 ((string= option "--noprint")
358 (pop-option)
359 (setf noprint t))
360 ;; FIXME: --noprogrammer was deprecated in 0.7.5, and
361 ;; in a year or so this backwards compatibility can
362 ;; go away.
363 ((string= option "--noprogrammer")
364 (warn "treating deprecated --noprogrammer as --disable-debugger")
365 (pop-option)
366 (push '(disable-debugger) reversed-evals))
367 ((string= option "--disable-debugger")
368 (pop-option)
369 (push '(disable-debugger) reversed-evals))
370 ((string= option "--end-toplevel-options")
371 (pop-option)
372 (return))
374 ;; Anything we don't recognize as a toplevel
375 ;; option must be the start of user-level
376 ;; options.. except that if we encounter
377 ;; "--end-toplevel-options" after we gave up
378 ;; because we didn't recognize an option as a
379 ;; toplevel option, then the option we gave up on
380 ;; must have been an error. (E.g. in
381 ;; "sbcl --eval '(a)' --eval'(b)' --end-toplevel-options"
382 ;; this test will let us detect that the string
383 ;; "--eval(b)" is an error.)
384 (if (find "--end-toplevel-options" options
385 :test #'string=)
386 (error "bad toplevel option: ~S" (first options))
387 (return)))))))
388 (/show0 "done with LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
390 ;; Excise all the options that we processed, so that only
391 ;; user-level options are left visible to user code.
392 (setf (rest *posix-argv*) options)
394 ;; Handle initialization files.
395 (/show0 "handling initialization files in TOPLEVEL-INIT")
396 (flet (;; If any of POSSIBLE-INIT-FILE-NAMES names a real file,
397 ;; return its truename.
398 (probe-init-files (&rest possible-init-file-names)
399 (declare (type list possible-init-file-names))
400 (/show0 "entering PROBE-INIT-FILES")
401 (prog1
402 (find-if (lambda (x)
403 (and (stringp x) (probe-file x)))
404 possible-init-file-names)
405 (/show0 "leaving PROBE-INIT-FILES"))))
406 (let* ((sbcl-home (posix-getenv "SBCL_HOME"))
407 (sysinit-truename (if sbcl-home
408 (probe-init-files sysinit
409 (concatenate 'string
410 sbcl-home
411 "/sbclrc"))
412 (probe-init-files sysinit
413 "/etc/sbclrc"
414 "/usr/local/etc/sbclrc")))
415 (user-home (or (posix-getenv "HOME")
416 (error "The HOME environment variable is unbound, ~
417 so user init file can't be found.")))
418 (userinit-truename (probe-init-files userinit
419 (concatenate 'string
420 user-home
421 "/.sbclrc"))))
423 ;; We wrap all the pre-REPL user/system customized startup code
424 ;; in a restart.
426 ;; (Why not wrap everything, even the stuff above, in this
427 ;; restart? Errors above here are basically command line or
428 ;; Unix environment errors, e.g. a missing file or a typo on
429 ;; the Unix command line, and you don't need to get into Lisp
430 ;; to debug them, you should just start over and do it right
431 ;; at the Unix level. Errors below here are generally errors
432 ;; in user Lisp code, and it might be helpful to let the user
433 ;; reach the REPL in order to help figure out what's going
434 ;; on.)
435 (restart-case
436 (progn
437 (flet ((process-init-file (truename)
438 (when truename
439 (unless (load truename)
440 (error "~S was not successfully loaded." truename))
441 (flush-standard-output-streams))))
442 (process-init-file sysinit-truename)
443 (process-init-file userinit-truename))
445 ;; Process --eval options.
446 (/show0 "handling --eval options in TOPLEVEL-INIT")
447 (dolist (eval (reverse reversed-evals))
448 (/show0 "handling one --eval option in TOPLEVEL-INIT")
449 (eval eval)
450 (flush-standard-output-streams)))
451 (continue ()
452 :report
453 "Continue anyway (skipping to toplevel read/eval/print loop)."
454 (/show0 "CONTINUEing from pre-REPL RESTART-CASE")
455 (values)) ; (no-op, just fall through)
456 (quit ()
457 :report "Quit SBCL (calling #'QUIT, killing the process)."
458 (/show0 "falling through to QUIT from pre-REPL RESTART-CASE")
459 (quit))))
461 ;; one more time for good measure, in case we fell out of the
462 ;; RESTART-CASE above before one of the flushes in the ordinary
463 ;; flow of control had a chance to operate
464 (flush-standard-output-streams)
466 (/show0 "falling into TOPLEVEL-REPL from TOPLEVEL-INIT")
467 (toplevel-repl noprint)
468 ;; (classic CMU CL error message: "You're certainly a clever child.":-)
469 (critically-unreachable "after TOPLEVEL-REPL"))))
471 ;;; halt-on-failures and prompt-on-failures modes, suitable for
472 ;;; noninteractive and interactive use respectively
473 (defun disable-debugger ()
474 (setf *debugger-hook* 'noprogrammer-debugger-hook-fun
475 *debug-io* *error-output*))
476 (defun enable-debugger ()
477 (setf *debugger-hook* nil
478 *debug-io* *query-io*))
480 ;;; read-eval-print loop for the default system toplevel
481 (defun toplevel-repl (noprint)
482 (/show0 "entering TOPLEVEL-REPL")
483 (let ((* nil) (** nil) (*** nil)
484 (- nil)
485 (+ nil) (++ nil) (+++ nil)
486 (/// nil) (// nil) (/ nil))
487 ;; WITH-SIMPLE-RESTART doesn't actually restart its body as some
488 ;; (like WHN for an embarrassingly long time ca. 2001-12-07) might
489 ;; think, but instead drops control back out at the end. So when a
490 ;; TOPLEVEL or outermost-ABORT restart happens, we need this outer
491 ;; LOOP wrapper to grab control and start over again. (And it also
492 ;; wraps CATCH 'TOPLEVEL-CATCHER for similar reasons.)
493 (loop
494 (/show0 "about to set up restarts in TOPLEVEL-REPL")
495 ;; There should only be one TOPLEVEL restart, and it's here, so
496 ;; restarting at TOPLEVEL always bounces you all the way out here.
497 (with-simple-restart (toplevel
498 "Restart at toplevel READ/EVAL/PRINT loop.")
499 ;; We add a new ABORT restart for every debugger level, so
500 ;; restarting at ABORT in a nested debugger gets you out to the
501 ;; innermost enclosing debugger, and only when you're in the
502 ;; outermost, unnested debugger level does restarting at ABORT
503 ;; get you out to here.
504 (with-simple-restart
505 (abort
506 "~@<Reduce debugger level (leaving debugger, returning to toplevel).~@:>")
507 (catch 'toplevel-catcher
508 #!-sunos (sb!unix:unix-sigsetmask 0) ; FIXME: What is this for?
509 ;; in the event of a control-stack-exhausted-error, we should
510 ;; have unwound enough stack by the time we get here that this
511 ;; is now possible
512 (sb!kernel::protect-control-stack-guard-page 1)
513 (repl noprint)
514 (critically-unreachable "after REPL")))))))
516 ;;; Our default REPL prompt is the minimal traditional one.
517 (defun repl-prompt-fun (stream)
518 (fresh-line stream)
519 (write-string "* " stream)) ; arbitrary but customary REPL prompt
521 ;;; Our default form reader does relatively little magic, but does
522 ;;; handle the Unix-style EOF-is-end-of-process convention.
523 (defun repl-read-form-fun (in out)
524 (declare (type stream in out) (ignore out))
525 (let* ((eof-marker (cons nil nil))
526 (form (read in nil eof-marker)))
527 (if (eq form eof-marker)
528 (quit)
529 form)))
531 ;;; hooks to support customized toplevels like ACL-style toplevel
532 ;;; from KMR on sbcl-devel 2002-12-21
533 (defvar *repl-read-form-fun* #'repl-read-form-fun
534 "a function of two stream arguments IN and OUT for the toplevel REPL to
535 call: Return the next Lisp form to evaluate (possibly handling other
536 magic -- like ACL-style keyword commands -- which precede the next
537 Lisp form). The OUT stream is there to support magic which requires
538 issuing new prompts.")
539 (defvar *repl-prompt-fun* #'repl-prompt-fun
540 "a function of one argument STREAM for the toplevel REPL to call: Prompt
541 the user for input.")
543 (defun repl (noprint)
544 (/show0 "entering REPL")
545 (let ((eof-marker (cons :eof nil)))
546 (loop
547 ;; (See comment preceding the definition of SCRUB-CONTROL-STACK.)
548 (scrub-control-stack)
549 (unless noprint
550 (funcall *repl-prompt-fun* *standard-output*)
551 ;; (Should *REPL-PROMPT-FUN* be responsible for doing its own
552 ;; FORCE-OUTPUT? I can't imagine a valid reason for it not to
553 ;; be done here, so leaving it up to *REPL-PROMPT-FUN* seems
554 ;; odd. But maybe there *is* a valid reason in some
555 ;; circumstances? perhaps some deadlock issue when being driven
556 ;; by another process or something...)
557 (force-output *standard-output*))
558 (let* ((form (funcall *repl-read-form-fun*
559 *standard-input*
560 *standard-output*))
561 (results (multiple-value-list (interactive-eval form))))
562 (unless noprint
563 (dolist (result results)
564 (fresh-line)
565 (prin1 result)))))))
567 ;;; suitable value for *DEBUGGER-HOOK* for a noninteractive Unix-y program
568 (defun noprogrammer-debugger-hook-fun (condition old-debugger-hook)
569 (declare (ignore old-debugger-hook))
570 (flet ((failure-quit (&key recklessly-p)
571 (/show0 "in FAILURE-QUIT (in --disable-debugger debugger hook)")
572 (quit :unix-status 1 :recklessly-p recklessly-p)))
573 ;; This HANDLER-CASE is here mostly to stop output immediately
574 ;; (and fall through to QUIT) when there's an I/O error. Thus,
575 ;; when we're run under a shell script or something, we can die
576 ;; cleanly when the script dies (and our pipes are cut), instead
577 ;; of falling into ldb or something messy like that.
578 (handler-case
579 (progn
580 (format *error-output*
581 "~&~@<unhandled condition (of type ~S): ~2I~_~A~:>~2%"
582 (type-of condition)
583 condition)
584 ;; Flush *ERROR-OUTPUT* even before the BACKTRACE, so that
585 ;; even if we hit an error within BACKTRACE (e.g. a bug in
586 ;; the debugger's own frame-walking code, or a bug in a user
587 ;; PRINT-OBJECT method) we'll at least have the CONDITION
588 ;; printed out before we die.
589 (finish-output *error-output*)
590 ;; (Where to truncate the BACKTRACE is of course arbitrary, but
591 ;; it seems as though we should at least truncate it somewhere.)
592 (sb!debug:backtrace 128 *error-output*)
593 (format
594 *error-output*
595 "~%unhandled condition in --disable-debugger mode, quitting~%")
596 (finish-output *error-output*)
597 (failure-quit))
598 (condition ()
599 ;; We IGNORE-ERRORS here because even %PRIMITIVE PRINT can
600 ;; fail when our output streams are blown away, as e.g. when
601 ;; we're running under a Unix shell script and it dies somehow
602 ;; (e.g. because of a SIGINT). In that case, we might as well
603 ;; just give it up for a bad job, and stop trying to notify
604 ;; the user of anything.
606 ;; Actually, the only way I've run across to exercise the
607 ;; problem is to have more than one layer of shell script.
608 ;; I have a shell script which does
609 ;; time nice -10 sh make.sh "$1" 2>&1 | tee make.tmp
610 ;; and the problem occurs when I interrupt this with Ctrl-C
611 ;; under Linux 2.2.14-5.0 and GNU bash, version 1.14.7(1).
612 ;; I haven't figured out whether it's bash, time, tee, Linux, or
613 ;; what that is responsible, but that it's possible at all
614 ;; means that we should IGNORE-ERRORS here. -- WHN 2001-04-24
615 (ignore-errors
616 (%primitive print
617 "Argh! error within --disable-debugger error handling"))
618 (failure-quit :recklessly-p t)))))
620 ;;; a convenient way to get into the assembly-level debugger
621 (defun %halt ()
622 (%primitive sb!c:halt))