1 ;;;; stuff related to the toplevel read-eval-print loop, plus some
2 ;;;; other miscellaneous functions that we don't have any better place
5 ;;;; This software is part of the SBCL system. See the README file for
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.
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*
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")
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
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)
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")
92 "KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
95 (/show0 "returning normally from INFINITE-ERROR-PROTECTOR")
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
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*
119 "KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
123 (/show0 "in INFINITE-ERROR-PROTECTOR, returning normally")
127 ;;;; miscellaneous external functions
129 #!-mp ; The multi-processing version is defined in multi-proc.lisp.
132 "This function causes execution to be suspended for N seconds. N may
133 be any non-negative, non-complex number."
134 (when (or (not (realp n))
136 (error 'simple-type-error
137 :format-control "invalid argument to SLEEP: ~S"
138 :format-arguments (list n)
140 :expected-type '(real 0)))
141 (multiple-value-bind (sec usec)
144 (multiple-value-bind (sec frac)
146 (values sec (truncate frac 1e-6))))
147 (sb!unix:unix-select 0 0 0 0 sec usec))
150 ;;;; SCRUB-CONTROL-STACK
152 (defconstant bytes-per-scrub-unit 2048)
154 ;;; Zero the unused portion of the control stack so that old objects
155 ;;; are not kept alive because of uninitialized stack variables.
157 ;;; "To summarize the problem, since not all allocated stack frame
158 ;;; slots are guaranteed to be written by the time you call an another
159 ;;; function or GC, there may be garbage pointers retained in your
160 ;;; dead stack locations. The stack scrubbing only affects the part
161 ;;; of the stack from the SP to the end of the allocated stack."
162 ;;; - ram, on cmucl-imp, Tue, 25 Sep 2001
164 ;;; So, as an (admittedly lame) workaround, from time to time we call
165 ;;; scrub-control-stack to zero out all the unused portion. This is
166 ;;; supposed to happen when the stack is mostly empty, so that we have
167 ;;; a chance of clearing more of it: callers are currently (2002.07.18)
170 (defun scrub-control-stack ()
171 (declare (optimize (speed 3) (safety 0))
172 (values (unsigned-byte 20))) ; FIXME: DECLARE VALUES?
174 #!-stack-grows-downward-not-upward
175 (let* ((csp (sap-int (sb!c::control-stack-pointer-sap)))
176 (initial-offset (logand csp (1- bytes-per-scrub-unit)))
178 (- sb!vm:control-stack-end sb!c:*backend-page-size*)))
180 ((scrub (ptr offset count)
181 (declare (type system-area-pointer ptr)
182 (type (unsigned-byte 16) offset)
183 (type (unsigned-byte 20) count)
184 (values (unsigned-byte 20)))
185 (cond ((>= (sap-int ptr) end-of-stack) 0)
186 ((= offset bytes-per-scrub-unit)
187 (look (sap+ ptr bytes-per-scrub-unit) 0 count))
189 (setf (sap-ref-32 ptr offset) 0)
190 (scrub ptr (+ offset sb!vm:n-word-bytes) count))))
191 (look (ptr offset count)
192 (declare (type system-area-pointer ptr)
193 (type (unsigned-byte 16) offset)
194 (type (unsigned-byte 20) count)
195 (values (unsigned-byte 20)))
196 (cond ((>= (sap-int ptr) end-of-stack) 0)
197 ((= offset bytes-per-scrub-unit)
199 ((zerop (sap-ref-32 ptr offset))
200 (look ptr (+ offset sb!vm:n-word-bytes) count))
202 (scrub ptr offset (+ count sb!vm:n-word-bytes))))))
203 (declare (type (unsigned-byte 32) csp))
204 (scrub (int-sap (- csp initial-offset))
205 (* (floor initial-offset sb!vm:n-word-bytes) sb!vm:n-word-bytes)
208 #!+stack-grows-downward-not-upward
209 (let* ((csp (sap-int (sb!c::control-stack-pointer-sap)))
210 (end-of-stack (+ sb!vm:control-stack-start sb!c:*backend-page-size*))
211 (initial-offset (logand csp (1- bytes-per-scrub-unit))))
213 ((scrub (ptr offset count)
214 (declare (type system-area-pointer ptr)
215 (type (unsigned-byte 16) offset)
216 (type (unsigned-byte 20) count)
217 (values (unsigned-byte 20)))
218 (let ((loc (int-sap (- (sap-int ptr) (+ offset sb!vm:n-word-bytes)))))
219 (cond ((< (sap-int loc) end-of-stack) 0)
220 ((= offset bytes-per-scrub-unit)
221 (look (int-sap (- (sap-int ptr) bytes-per-scrub-unit))
223 (t ;; need to fix bug in %SET-STACK-REF
224 (setf (sap-ref-32 loc 0) 0)
225 (scrub ptr (+ offset sb!vm:n-word-bytes) count)))))
226 (look (ptr offset count)
227 (declare (type system-area-pointer ptr)
228 (type (unsigned-byte 16) offset)
229 (type (unsigned-byte 20) count)
230 (values (unsigned-byte 20)))
231 (let ((loc (int-sap (- (sap-int ptr) offset))))
232 (cond ((< (sap-int loc) end-of-stack) 0)
233 ((= offset bytes-per-scrub-unit)
235 ((zerop (sb!kernel::get-lisp-obj-address (stack-ref loc 0)))
236 (look ptr (+ offset sb!vm:n-word-bytes) count))
238 (scrub ptr offset (+ count sb!vm:n-word-bytes)))))))
239 (declare (type (unsigned-byte 32) csp))
240 (scrub (int-sap (+ csp initial-offset))
241 (* (floor initial-offset sb!vm:n-word-bytes) sb!vm:n-word-bytes)
244 ;;;; the default toplevel function
248 "a list of all the values returned by the most recent top level EVAL")
249 (defvar // nil #!+sb-doc "the previous value of /")
250 (defvar /// nil #!+sb-doc "the previous value of //")
251 (defvar * nil #!+sb-doc "the value of the most recent top level EVAL")
252 (defvar ** nil #!+sb-doc "the previous value of *")
253 (defvar *** nil #!+sb-doc "the previous value of **")
254 (defvar + nil #!+sb-doc "the value of the most recent top level READ")
255 (defvar ++ nil #!+sb-doc "the previous value of +")
256 (defvar +++ nil #!+sb-doc "the previous value of ++")
257 (defvar - nil #!+sb-doc "the form currently being evaluated")
259 (defun interactive-eval (form)
260 "Evaluate FORM, returning whatever it returns and adjusting ***, **, *,
261 +++, ++, +, ///, //, /, and -."
263 (let ((results (multiple-value-list (eval form))))
274 ;; The bogon returned an unbound marker.
275 ;; FIXME: It would be safer to check every one of the values in RESULTS,
276 ;; instead of just the first one.
278 (cerror "Go on with * set to NIL."
279 "EVAL returned an unbound marker."))
282 ;;; Flush anything waiting on one of the ANSI Common Lisp standard
283 ;;; output streams before proceeding.
284 (defun flush-standard-output-streams ()
285 (dolist (name '(*debug-io*
290 (finish-output (symbol-value name)))
293 ;;; the default system top level function
294 (defun toplevel-init ()
296 (/show0 "entering TOPLEVEL-INIT")
298 (let ((sysinit nil) ; value of --sysinit option
299 (userinit nil) ; value of --userinit option
300 (reversed-evals nil) ; values of --eval options, in reverse order; and
301 ; also --load options, translated into --eval
302 (noprint nil) ; Has a --noprint option been seen?
303 (options (rest *posix-argv*))) ; skipping program name
305 (/show0 "done with outer LET in TOPLEVEL-INIT")
307 ;; FIXME: There are lots of ways for errors to happen around here
308 ;; (e.g. bad command line syntax, or READ-ERROR while trying to
309 ;; READ an --eval string). Make sure that they're handled
310 ;; reasonably. Also, perhaps all errors while parsing the command
311 ;; line should cause the system to QUIT, instead of trying to go
312 ;; into the Lisp debugger, since trying to go into the debugger
313 ;; gets into various annoying issues of where we should go after
314 ;; the user tries to return from the debugger.
316 ;; Parse command line options.
317 (loop while options do
318 (/show0 "at head of LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
319 (let ((option (first options)))
320 (flet ((pop-option ()
323 (error "unexpected end of command line options"))))
324 (cond ((string= option "--sysinit")
327 (error "multiple --sysinit options")
328 (setf sysinit (pop-option))))
329 ((string= option "--userinit")
332 (error "multiple --userinit options")
333 (setf userinit (pop-option))))
334 ((string= option "--eval")
336 (let ((eval-as-string (pop-option)))
337 (with-input-from-string (eval-stream eval-as-string)
338 (let* ((eof-marker (cons :eof :eof))
339 (eval (read eval-stream nil eof-marker))
340 (eof (read eval-stream nil eof-marker)))
341 (cond ((eq eval eof-marker)
342 (error "unable to parse ~S"
344 ((not (eq eof eof-marker))
345 (error "more than one expression in ~S"
348 (push eval reversed-evals)))))))
349 ((string= option "--load")
351 (push `(load ,(pop-option)) reversed-evals))
352 ((string= option "--noprint")
355 ;; FIXME: --noprogrammer was deprecated in 0.7.5, and
356 ;; in a year or so this backwards compatibility can
358 ((string= option "--noprogrammer")
359 (warn "treating deprecated --noprogrammer as --disable-debugger")
361 (push '(disable-debugger) reversed-evals))
362 ((string= option "--disable-debugger")
364 (push '(disable-debugger) reversed-evals))
365 ((string= option "--end-toplevel-options")
369 ;; Anything we don't recognize as a toplevel
370 ;; option must be the start of user-level
371 ;; options.. except that if we encounter
372 ;; "--end-toplevel-options" after we gave up
373 ;; because we didn't recognize an option as a
374 ;; toplevel option, then the option we gave up on
375 ;; must have been an error. (E.g. in
376 ;; "sbcl --eval '(a)' --eval'(b)' --end-toplevel-options"
377 ;; this test will let us detect that the string
378 ;; "--eval(b)" is an error.)
379 (if (find "--end-toplevel-options" options
381 (error "bad toplevel option: ~S" (first options))
383 (/show0 "done with LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
385 ;; Excise all the options that we processed, so that only
386 ;; user-level options are left visible to user code.
387 (setf (rest *posix-argv*) options)
389 ;; Handle initialization files.
390 (/show0 "handling initialization files in TOPLEVEL-INIT")
391 (flet (;; If any of POSSIBLE-INIT-FILE-NAMES names a real file,
392 ;; return its truename.
393 (probe-init-files (&rest possible-init-file-names)
394 (/show0 "entering PROBE-INIT-FILES")
397 (and (stringp x) (probe-file x)))
398 possible-init-file-names)
399 (/show0 "leaving PROBE-INIT-FILES"))))
400 (let* ((sbcl-home (posix-getenv "SBCL_HOME"))
401 (sysinit-truename (if sbcl-home
402 (probe-init-files sysinit
406 (probe-init-files sysinit
408 "/usr/local/etc/sbclrc")))
409 (user-home (or (posix-getenv "HOME")
410 (error "The HOME environment variable is unbound, ~
411 so user init file can't be found.")))
412 (userinit-truename (probe-init-files userinit
417 ;; We wrap all the pre-REPL user/system customized startup code
420 ;; (Why not wrap everything, even the stuff above, in this
421 ;; restart? Errors above here are basically command line or
422 ;; Unix environment errors, e.g. a missing file or a typo on
423 ;; the Unix command line, and you don't need to get into Lisp
424 ;; to debug them, you should just start over and do it right
425 ;; at the Unix level. Errors below here are generally errors
426 ;; in user Lisp code, and it might be helpful to let the user
427 ;; reach the REPL in order to help figure out what's going
431 (flet ((process-init-file (truename)
433 (unless (load truename)
434 (error "~S was not successfully loaded." truename))
435 (flush-standard-output-streams))))
436 (process-init-file sysinit-truename)
437 (process-init-file userinit-truename))
439 ;; Process --eval options.
440 (/show0 "handling --eval options in TOPLEVEL-INIT")
441 (dolist (eval (reverse reversed-evals))
442 (/show0 "handling one --eval option in TOPLEVEL-INIT")
444 (flush-standard-output-streams)))
447 "Continue anyway (skipping to toplevel read/eval/print loop)."
448 (/show0 "CONTINUEing from pre-REPL RESTART-CASE")
449 (values)) ; (no-op, just fall through)
451 :report "Quit SBCL (calling #'QUIT, killing the process)."
452 (/show0 "falling through to QUIT from pre-REPL RESTART-CASE")
455 ;; one more time for good measure, in case we fell out of the
456 ;; RESTART-CASE above before one of the flushes in the ordinary
457 ;; flow of control had a chance to operate
458 (flush-standard-output-streams)
460 (/show0 "falling into TOPLEVEL-REPL from TOPLEVEL-INIT")
461 (toplevel-repl noprint)
462 ;; (classic CMU CL error message: "You're certainly a clever child.":-)
463 (critically-unreachable "after TOPLEVEL-REPL"))))
465 ;;; halt-on-failures and prompt-on-failures modes, suitable for
466 ;;; noninteractive and interactive use respectively
467 (defun disable-debugger ()
468 (setf *debugger-hook* 'noprogrammer-debugger-hook-fun
469 *debug-io* *error-output*))
470 (defun enable-debugger ()
471 (setf *debugger-hook* nil
472 *debug-io* *query-io*))
474 ;;; read-eval-print loop for the default system toplevel
475 (defun toplevel-repl (noprint)
476 (/show0 "entering TOPLEVEL-REPL")
477 (let ((* nil) (** nil) (*** nil)
479 (+ nil) (++ nil) (+++ nil)
480 (/// nil) (// nil) (/ nil))
481 ;; WITH-SIMPLE-RESTART doesn't actually restart its body as some
482 ;; (like WHN for an embarrassingly long time ca. 2001-12-07) might
483 ;; think, but instead drops control back out at the end. So when a
484 ;; TOPLEVEL or outermost-ABORT restart happens, we need this outer
485 ;; LOOP wrapper to grab control and start over again. (And it also
486 ;; wraps CATCH 'TOPLEVEL-CATCHER for similar reasons.)
488 (/show0 "about to set up restarts in TOPLEVEL-REPL")
489 ;; There should only be one TOPLEVEL restart, and it's here, so
490 ;; restarting at TOPLEVEL always bounces you all the way out here.
491 (with-simple-restart (toplevel
492 "Restart at toplevel READ/EVAL/PRINT loop.")
493 ;; We add a new ABORT restart for every debugger level, so
494 ;; restarting at ABORT in a nested debugger gets you out to the
495 ;; innermost enclosing debugger, and only when you're in the
496 ;; outermost, unnested debugger level does restarting at ABORT
497 ;; get you out to here.
500 "Reduce debugger level (leaving debugger, returning to toplevel).")
501 (catch 'toplevel-catcher
502 #!-sunos (sb!unix:unix-sigsetmask 0) ; FIXME: What is this for?
503 ;; in the event of a control-stack-exhausted-error, we should
504 ;; have unwound enough stack by the time we get here that this
506 (sb!kernel::protect-control-stack-guard-page 1)
508 (critically-unreachable "after REPL")))))))
510 (defun repl (noprint)
511 (/show0 "entering REPL")
512 (let ((eof-marker (cons :eof nil)))
514 ;; see comment preceding definition of SCRUB-CONTROL-STACK
515 (scrub-control-stack)
518 (write-string "* ") ; arbitrary but customary REPL prompt
519 (flush-standard-output-streams))
520 (let ((form (read *standard-input* nil eof-marker)))
521 (cond ((eq form eof-marker)
522 (/show0 "doing QUIT for EOF in REPL")
525 (let ((results (multiple-value-list (interactive-eval form))))
527 (dolist (result results)
529 (prin1 result))))))))))
531 ;;; suitable value for *DEBUGGER-HOOK* for a noninteractive Unix-y program
532 (defun noprogrammer-debugger-hook-fun (condition old-debugger-hook)
533 (declare (ignore old-debugger-hook))
534 (flet ((failure-quit (&key recklessly-p)
535 (/show0 "in FAILURE-QUIT (in --disable-debugger debugger hook)")
536 (quit :unix-status 1 :recklessly-p recklessly-p)))
537 ;; This HANDLER-CASE is here mostly to stop output immediately
538 ;; (and fall through to QUIT) when there's an I/O error. Thus,
539 ;; when we're run under a shell script or something, we can die
540 ;; cleanly when the script dies (and our pipes are cut), instead
541 ;; of falling into ldb or something messy like that.
544 (format *error-output*
545 "~&~@<unhandled condition (of type ~S): ~2I~_~A~:>~2%"
548 ;; Flush *ERROR-OUTPUT* even before the BACKTRACE, so that
549 ;; even if we hit an error within BACKTRACE (e.g. a bug in
550 ;; the debugger's own frame-walking code, or a bug in a user
551 ;; PRINT-OBJECT method) we'll at least have the CONDITION
552 ;; printed out before we die.
553 (finish-output *error-output*)
554 ;; (Where to truncate the BACKTRACE is of course arbitrary, but
555 ;; it seems as though we should at least truncate it somewhere.)
556 (sb!debug:backtrace 128 *error-output*)
559 "~%unhandled condition in --disable-debugger mode, quitting~%")
560 (finish-output *error-output*)
563 ;; We IGNORE-ERRORS here because even %PRIMITIVE PRINT can
564 ;; fail when our output streams are blown away, as e.g. when
565 ;; we're running under a Unix shell script and it dies somehow
566 ;; (e.g. because of a SIGINT). In that case, we might as well
567 ;; just give it up for a bad job, and stop trying to notify
568 ;; the user of anything.
570 ;; Actually, the only way I've run across to exercise the
571 ;; problem is to have more than one layer of shell script.
572 ;; I have a shell script which does
573 ;; time nice -10 sh make.sh "$1" 2>&1 | tee make.tmp
574 ;; and the problem occurs when I interrupt this with Ctrl-C
575 ;; under Linux 2.2.14-5.0 and GNU bash, version 1.14.7(1).
576 ;; I haven't figured out whether it's bash, time, tee, Linux, or
577 ;; what that is responsible, but that it's possible at all
578 ;; means that we should IGNORE-ERRORS here. -- WHN 2001-04-24
581 "Argh! error within --disable-debugger error handling"))
582 (failure-quit :recklessly-p t)))))
584 ;;; a convenient way to get into the assembly-level debugger
586 (%primitive sb!c:halt))