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* *need-to-collect-garbage*
29 *before-gc-hooks* *after-gc-hooks*
30 #!+x86 *pseudo-atomic-atomic*
31 #!+x86 *pseudo-atomic-interrupted*
32 sb!unix::*interrupts-enabled*
33 sb!unix::*interrupt-pending*
34 *type-system-initialized*))
36 (defvar *cold-init-complete-p*)
38 ;;; counts of nested errors (with internal errors double-counted)
39 (defvar *maximum-error-depth*)
40 (defvar *current-error-depth*)
42 ;;;; miscellaneous utilities for working with with TOPLEVEL
44 ;;; Execute BODY in a context where any %END-OF-THE-WORLD (thrown e.g.
45 ;;; by QUIT) is caught and any final processing and return codes are
46 ;;; handled appropriately.
47 (defmacro handling-end-of-the-world (&body body)
48 (with-unique-names (caught)
49 `(let ((,caught (catch '%end-of-the-world
50 (/show0 "inside CATCH '%END-OF-THE-WORLD")
52 (/show0 "back from CATCH '%END-OF-THE-WORLD, flushing output")
53 (flush-standard-output-streams)
54 (/show0 "calling UNIX-EXIT")
55 (sb!unix:unix-exit ,caught))))
57 ;;;; working with *CURRENT-ERROR-DEPTH* and *MAXIMUM-ERROR-DEPTH*
59 ;;; INFINITE-ERROR-PROTECT is used by ERROR and friends to keep us out
61 (defmacro infinite-error-protect (&rest forms)
62 `(unless (infinite-error-protector)
63 (/show0 "back from INFINITE-ERROR-PROTECTOR")
64 (let ((*current-error-depth* (1+ *current-error-depth*)))
65 (/show0 "in INFINITE-ERROR-PROTECT, incremented error depth")
66 ;; arbitrary truncation
67 #!+sb-show (sb!debug:backtrace 8)
70 ;;; a helper function for INFINITE-ERROR-PROTECT
71 (defun infinite-error-protector ()
72 (/show0 "entering INFINITE-ERROR-PROTECTOR, *CURRENT-ERROR-DEPTH*=..")
73 (/hexstr *current-error-depth*)
74 (cond ((not *cold-init-complete-p*)
75 (%primitive print "Argh! error in cold init, halting")
76 (%primitive sb!c:halt))
77 ((or (not (boundp '*current-error-depth*))
78 (not (realp *current-error-depth*))
79 (not (boundp '*maximum-error-depth*))
80 (not (realp *maximum-error-depth*)))
81 (%primitive print "Argh! corrupted error depth, halting")
82 (%primitive sb!c:halt))
83 ((> *current-error-depth* *maximum-error-depth*)
84 (/show0 "*MAXIMUM-ERROR-DEPTH*=..")
85 (/hexstr *maximum-error-depth*)
86 (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
90 "SB-KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
93 (/show0 "returning normally from INFINITE-ERROR-PROTECTOR")
96 ;;; FIXME: I had a badly broken version of INFINITE-ERROR-PROTECTOR at
97 ;;; one point (shown below), and SBCL cross-compiled it without
98 ;;; warning about FORMS being undefined. Check whether that problem
99 ;;; (missing warning) is repeatable in the final system and if so, fix
102 (defun infinite-error-protector ()
103 `(cond ((not *cold-init-complete-p*)
104 (%primitive print "Argh! error in cold init, halting")
105 (%primitive sb!c:halt))
106 ((or (not (boundp '*current-error-depth*))
107 (not (realp *current-error-depth*))
108 (not (boundp '*maximum-error-depth*))
109 (not (realp *maximum-error-depth*)))
110 (%primitive print "Argh! corrupted error depth, halting")
111 (%primitive sb!c:halt))
112 ((> *current-error-depth* *maximum-error-depth*)
113 (/show0 "in INFINITE-ERROR-PROTECTOR, calling ERROR-ERROR")
114 (error-error "Help! "
115 *current-error-depth*
117 "SB-KERNEL:*MAXIMUM-ERROR-DEPTH* exceeded.")
121 (/show0 "in INFINITE-ERROR-PROTECTOR, returning normally")
125 ;;;; miscellaneous external functions
129 "This function causes execution to be suspended for N seconds. N may
130 be any non-negative, non-complex number."
131 (when (or (not (realp n))
133 (error 'simple-type-error
134 :format-control "invalid argument to SLEEP: ~S"
135 :format-arguments (list n)
137 :expected-type '(real 0)))
138 (multiple-value-bind (sec usec)
141 (multiple-value-bind (sec frac)
143 (values sec (truncate frac 1e-6))))
144 (sb!unix:unix-select 0 0 0 0 sec usec))
147 ;;;; SCRUB-CONTROL-STACK
149 (defconstant bytes-per-scrub-unit 2048)
151 ;;; Zero the unused portion of the control stack so that old objects
152 ;;; are not kept alive because of uninitialized stack variables.
154 ;;; "To summarize the problem, since not all allocated stack frame
155 ;;; slots are guaranteed to be written by the time you call an another
156 ;;; function or GC, there may be garbage pointers retained in your
157 ;;; dead stack locations. The stack scrubbing only affects the part
158 ;;; of the stack from the SP to the end of the allocated stack."
159 ;;; - ram, on cmucl-imp, Tue, 25 Sep 2001
161 ;;; So, as an (admittedly lame) workaround, from time to time we call
162 ;;; scrub-control-stack to zero out all the unused portion. This is
163 ;;; supposed to happen when the stack is mostly empty, so that we have
164 ;;; a chance of clearing more of it: callers are currently (2002.07.18)
167 (defun scrub-control-stack ()
168 (declare (optimize (speed 3) (safety 0))
169 (values (unsigned-byte 20))) ; FIXME: DECLARE VALUES?
171 #!-stack-grows-downward-not-upward
172 (let* ((csp (sap-int (sb!c::control-stack-pointer-sap)))
173 (initial-offset (logand csp (1- bytes-per-scrub-unit)))
175 (- (sb!vm:fixnumize sb!vm:*control-stack-end*)
176 sb!c:*backend-page-size*)))
178 ((scrub (ptr offset count)
179 (declare (type system-area-pointer ptr)
180 (type (unsigned-byte 16) offset)
181 (type (unsigned-byte 20) count)
182 (values (unsigned-byte 20)))
183 (cond ((>= (sap-int ptr) end-of-stack) 0)
184 ((= offset bytes-per-scrub-unit)
185 (look (sap+ ptr bytes-per-scrub-unit) 0 count))
187 (setf (sap-ref-32 ptr offset) 0)
188 (scrub ptr (+ offset sb!vm:n-word-bytes) count))))
189 (look (ptr offset count)
190 (declare (type system-area-pointer ptr)
191 (type (unsigned-byte 16) offset)
192 (type (unsigned-byte 20) count)
193 (values (unsigned-byte 20)))
194 (cond ((>= (sap-int ptr) end-of-stack) 0)
195 ((= offset bytes-per-scrub-unit)
197 ((zerop (sap-ref-32 ptr offset))
198 (look ptr (+ offset sb!vm:n-word-bytes) count))
200 (scrub ptr offset (+ count sb!vm:n-word-bytes))))))
201 (declare (type (unsigned-byte 32) csp))
202 (scrub (int-sap (- csp initial-offset))
203 (* (floor initial-offset sb!vm:n-word-bytes) sb!vm:n-word-bytes)
206 #!+stack-grows-downward-not-upward
207 (let* ((csp (sap-int (sb!c::control-stack-pointer-sap)))
208 (end-of-stack (+ (sb!vm:fixnumize sb!vm:*control-stack-start*)
209 sb!c:*backend-page-size*))
210 (initial-offset (logand csp (1- bytes-per-scrub-unit))))
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))
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)
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)
243 ;;;; the default toplevel function
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 -."
265 (make-null-interactive-lexenv)))))
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.
280 (cerror "Go on with * set to NIL."
281 "EVAL returned an unbound marker."))
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*
292 (finish-output (symbol-value name)))
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 (;; value of --sysinit option
303 ;; value of --userinit option
305 ;; values of --eval options, in reverse order; and also any
306 ;; other options (like --load) which're translated into --eval
308 ;; The values are stored as strings, so that they can be
309 ;; passed to READ only after their predecessors have been
310 ;; EVALed, so that things work when e.g. REQUIRE in one EVAL
311 ;; form creates a package referred to in the next EVAL form.
313 ;; Has a --noprint option been seen?
315 ;; everything in *POSIX-ARGV* except for argv[0]=programname
316 (options (rest *posix-argv*)))
318 (declare (type list options))
320 (/show0 "done with outer LET in TOPLEVEL-INIT")
322 ;; FIXME: There are lots of ways for errors to happen around here
323 ;; (e.g. bad command line syntax, or READ-ERROR while trying to
324 ;; READ an --eval string). Make sure that they're handled
325 ;; reasonably. Also, perhaps all errors while parsing the command
326 ;; line should cause the system to QUIT, instead of trying to go
327 ;; into the Lisp debugger, since trying to go into the debugger
328 ;; gets into various annoying issues of where we should go after
329 ;; the user tries to return from the debugger.
331 ;; Parse command line options.
332 (loop while options do
333 (/show0 "at head of LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
334 (let ((option (first options)))
335 (flet ((pop-option ()
338 (error "unexpected end of command line options"))))
339 (cond ((string= option "--sysinit")
342 (error "multiple --sysinit options")
343 (setf sysinit (pop-option))))
344 ((string= option "--userinit")
347 (error "multiple --userinit options")
348 (setf userinit (pop-option))))
349 ((string= option "--eval")
351 (push (pop-option) reversed-evals))
352 ((string= option "--load")
355 ;; FIXME: see BUG 296
356 (concatenate 'string "(|LOAD| \"" (pop-option) "\")")
358 ((string= option "--noprint")
361 ;; FIXME: --noprogrammer was deprecated in 0.7.5, and
362 ;; in a year or so this backwards compatibility can
364 ((string= option "--noprogrammer")
365 (warn "treating deprecated --noprogrammer as --disable-debugger")
367 (push "(|DISABLE-DEBUGGER|)" reversed-evals))
368 ((string= option "--disable-debugger")
370 (push "(|DISABLE-DEBUGGER|)" reversed-evals))
371 ((string= option "--end-toplevel-options")
375 ;; Anything we don't recognize as a toplevel
376 ;; option must be the start of user-level
377 ;; options.. except that if we encounter
378 ;; "--end-toplevel-options" after we gave up
379 ;; because we didn't recognize an option as a
380 ;; toplevel option, then the option we gave up on
381 ;; must have been an error. (E.g. in
382 ;; "sbcl --eval '(a)' --eval'(b)' --end-toplevel-options"
383 ;; this test will let us detect that the string
384 ;; "--eval(b)" is an error.)
385 (if (find "--end-toplevel-options" options
387 (error "bad toplevel option: ~S" (first options))
389 (/show0 "done with LOOP WHILE OPTIONS DO in TOPLEVEL-INIT")
391 ;; Delete all the options that we processed, so that only
392 ;; user-level options are left visible to user code.
393 (setf (rest *posix-argv*) options)
395 ;; Handle initialization files.
396 (/show0 "handling initialization files in TOPLEVEL-INIT")
397 (flet (;; If any of POSSIBLE-INIT-FILE-NAMES names a real file,
398 ;; return its truename.
399 (probe-init-files (&rest possible-init-file-names)
400 (declare (type list possible-init-file-names))
401 (/show0 "entering PROBE-INIT-FILES")
404 (and (stringp x) (probe-file x)))
405 possible-init-file-names)
406 (/show0 "leaving PROBE-INIT-FILES"))))
407 (let* ((sbcl-home (posix-getenv "SBCL_HOME"))
409 (probe-init-files sysinit
410 (concatenate 'string sbcl-home "/sbclrc")
412 (user-home (or (posix-getenv "HOME")
413 (error "The HOME environment variable is unbound, ~
414 so user init file can't be found.")))
415 (userinit-truename (probe-init-files userinit
420 ;; We wrap all the pre-REPL user/system customized startup code
423 ;; (Why not wrap everything, even the stuff above, in this
424 ;; restart? Errors above here are basically command line or
425 ;; Unix environment errors, e.g. a missing file or a typo on
426 ;; the Unix command line, and you don't need to get into Lisp
427 ;; to debug them, you should just start over and do it right
428 ;; at the Unix level. Errors below here are generally errors
429 ;; in user Lisp code, and it might be helpful to let the user
430 ;; reach the REPL in order to help figure out what's going
434 (flet ((process-init-file (truename)
436 (unless (load truename)
437 (error "~S was not successfully loaded." truename))
438 (flush-standard-output-streams))))
439 (process-init-file sysinit-truename)
440 (process-init-file userinit-truename))
442 ;; Process --eval options.
443 (/show0 "handling --eval options in TOPLEVEL-INIT")
444 (dolist (expr-as-string (reverse reversed-evals))
445 (/show0 "handling one --eval option in TOPLEVEL-INIT")
446 (let ((expr (with-input-from-string (eval-stream
448 (let* ((eof-marker (cons :eof :eof))
449 (result (read eval-stream nil eof-marker))
450 (eof (read eval-stream nil eof-marker)))
451 (cond ((eq result eof-marker)
452 (error "unable to parse ~S"
454 ((not (eq eof eof-marker))
455 (error "more than one expression in ~S"
460 (flush-standard-output-streams))))
463 "Continue anyway (skipping to toplevel read/eval/print loop)."
464 (/show0 "CONTINUEing from pre-REPL RESTART-CASE")
465 (values)) ; (no-op, just fall through)
467 :report "Quit SBCL (calling #'QUIT, killing the process)."
468 (/show0 "falling through to QUIT from pre-REPL RESTART-CASE")
471 ;; one more time for good measure, in case we fell out of the
472 ;; RESTART-CASE above before one of the flushes in the ordinary
473 ;; flow of control had a chance to operate
474 (flush-standard-output-streams)
476 (/show0 "falling into TOPLEVEL-REPL from TOPLEVEL-INIT")
477 (toplevel-repl noprint)
478 ;; (classic CMU CL error message: "You're certainly a clever child.":-)
479 (critically-unreachable "after TOPLEVEL-REPL"))))
481 ;;; read-eval-print loop for the default system toplevel
482 (defun toplevel-repl (noprint)
483 (/show0 "entering TOPLEVEL-REPL")
484 (let ((* nil) (** nil) (*** nil)
486 (+ nil) (++ nil) (+++ nil)
487 (/// nil) (// nil) (/ nil))
488 ;; WITH-SIMPLE-RESTART doesn't actually restart its body as some
489 ;; (like WHN for an embarrassingly long time ca. 2001-12-07) might
490 ;; think, but instead drops control back out at the end. So when a
491 ;; TOPLEVEL or outermost-ABORT restart happens, we need this outer
492 ;; LOOP wrapper to grab control and start over again. (And it also
493 ;; wraps CATCH 'TOPLEVEL-CATCHER for similar reasons.)
495 (/show0 "about to set up restarts in TOPLEVEL-REPL")
496 ;; There should only be one TOPLEVEL restart, and it's here, so
497 ;; restarting at TOPLEVEL always bounces you all the way out here.
498 (with-simple-restart (toplevel
499 "Restart at toplevel READ/EVAL/PRINT loop.")
500 ;; We add a new ABORT restart for every debugger level, so
501 ;; restarting at ABORT in a nested debugger gets you out to the
502 ;; innermost enclosing debugger, and only when you're in the
503 ;; outermost, unnested debugger level does restarting at ABORT
504 ;; get you out to here.
507 "~@<Reduce debugger level (leaving debugger, returning to toplevel).~@:>")
508 (catch 'toplevel-catcher
509 (sb!unix::warn-when-signals-masked)
510 ;; in the event of a control-stack-exhausted-error, we should
511 ;; have unwound enough stack by the time we get here that this
513 (sb!kernel::protect-control-stack-guard-page 1)
514 (funcall *repl-fun* noprint)
515 (critically-unreachable "after REPL")))))))
517 ;;; Our default REPL prompt is the minimal traditional one.
518 (defun repl-prompt-fun (stream)
520 (write-string "* " stream)) ; arbitrary but customary REPL prompt
522 ;;; Our default form reader does relatively little magic, but does
523 ;;; handle the Unix-style EOF-is-end-of-process convention.
524 (defun repl-read-form-fun (in out)
525 (declare (type stream in out) (ignore out))
526 (let* ((eof-marker (cons nil nil))
527 (form (read in nil eof-marker)))
528 (if (eq form eof-marker)
532 ;;; hooks to support customized toplevels like ACL-style toplevel
533 ;;; from KMR on sbcl-devel 2002-12-21
534 (defvar *repl-read-form-fun* #'repl-read-form-fun
535 "a function of two stream arguments IN and OUT for the toplevel REPL to
536 call: Return the next Lisp form to evaluate (possibly handling other
537 magic -- like ACL-style keyword commands -- which precede the next
538 Lisp form). The OUT stream is there to support magic which requires
539 issuing new prompts.")
540 (defvar *repl-prompt-fun* #'repl-prompt-fun
541 "a function of one argument STREAM for the toplevel REPL to call: Prompt
542 the user for input.")
543 (defvar *repl-fun* #'repl-fun
544 "a function of one argument NOPRINT that provides the REPL for the system.
545 Assumes that *standard-input* and *standard-output* are setup.")
547 (defun repl-fun (noprint)
548 (/show0 "entering REPL")
550 ;; (See comment preceding the definition of SCRUB-CONTROL-STACK.)
551 (scrub-control-stack)
552 (sb!thread::get-foreground)
554 (funcall *repl-prompt-fun* *standard-output*)
555 ;; (Should *REPL-PROMPT-FUN* be responsible for doing its own
556 ;; FORCE-OUTPUT? I can't imagine a valid reason for it not to
557 ;; be done here, so leaving it up to *REPL-PROMPT-FUN* seems
558 ;; odd. But maybe there *is* a valid reason in some
559 ;; circumstances? perhaps some deadlock issue when being driven
560 ;; by another process or something...)
561 (force-output *standard-output*))
562 (let* ((form (funcall *repl-read-form-fun*
565 (results (multiple-value-list (interactive-eval form))))
567 (dolist (result results)
571 ;;; a convenient way to get into the assembly-level debugger
573 (%primitive sb!c:halt))