Optionally be less noisy during build
[sbcl.git] / src / code / cold-init.lisp
blobdea61b59824d69a26bed2dcb1699d7e74943bffa
1 ;;;; cold initialization stuff, plus some other miscellaneous stuff
2 ;;;; that we don't have any better place for
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!IMPL")
15 ;;;; putting ourselves out of our misery when things become too much to bear
17 (declaim (ftype (function (simple-string) nil) !cold-lose))
18 (defun !cold-lose (msg)
19 (%primitive print msg)
20 (%primitive print "too early in cold init to recover from errors")
21 (%halt))
23 ;;; last-ditch error reporting for things which should never happen
24 ;;; and which, if they do happen, are sufficiently likely to torpedo
25 ;;; the normal error-handling system that we want to bypass it
26 (declaim (ftype (function (simple-string) nil) critically-unreachable))
27 (defun critically-unreachable (where)
28 (%primitive print "internal error: Control should never reach here, i.e.")
29 (%primitive print where)
30 (%halt))
32 ;;;; !COLD-INIT
34 ;;; a list of toplevel things set by GENESIS
35 (defvar *!cold-toplevels*) ; except for DEFUNs and SETF macros
36 (defvar *!cold-setf-macros*) ; just SETF macros
37 (defvar *!cold-defconstants*) ; just DEFCONSTANT-EQXs
38 (defvar *!cold-defuns*) ; just DEFUNs
40 ;;; a SIMPLE-VECTOR set by GENESIS
41 (defvar *!load-time-values*)
43 (eval-when (:compile-toplevel :execute)
44 ;; FIXME: Perhaps we should make SHOW-AND-CALL-AND-FMAKUNBOUND, too,
45 ;; and use it for most of the cold-init functions. (Just be careful
46 ;; not to use it for the COLD-INIT-OR-REINIT functions.)
47 (sb!xc:defmacro show-and-call (name)
48 `(progn
49 (/primitive-print ,(symbol-name name))
50 (,name))))
52 (defun !encapsulate-stuff-for-cold-init (&aux names)
53 (flet ((encapsulate-1 (name handler)
54 (encapsulate name '!cold-init handler)
55 (push name names)))
56 (encapsulate-1 '%failed-aver
57 (lambda (f expr)
58 ;; output the message before signaling error,
59 ;; as it may be this is too early in the cold init.
60 (fresh-line)
61 (write-line "failed AVER:")
62 (write expr)
63 (terpri)
64 (funcall f expr)))
66 (encapsulate-1
67 'find-package
68 (lambda (f designator)
69 (cond ((packagep designator) designator)
70 (t (funcall f (let ((s (string designator)))
71 (if (eql (mismatch s "SB!") 3)
72 (concatenate 'string "SB-" (subseq s 3))
73 s))))))))
74 names)
76 (defmacro !with-init-wrappers (&rest forms)
77 `(let ((wrapped-functions (!encapsulate-stuff-for-cold-init)))
78 ,@forms
79 (dolist (f wrapped-functions) (unencapsulate f '!cold-init))))
81 (defun !c-runtime-noinform-p () (/= (extern-alien "lisp_startup_options" char) 0))
83 ;;; called when a cold system starts up
84 (defun !cold-init (&aux real-choose-symbol-out-fun)
85 "Give the world a shove and hope it spins."
87 #!+sb-show
88 (sb!int::cannot-/show "Test of CANNOT-/SHOW [don't worry - this is expected]")
89 (/show0 "entering !COLD-INIT")
90 (setq *readtable* (make-readtable)
91 *print-length* 6 *print-level* 3)
92 (setq *error-output* (!make-cold-stderr-stream)
93 *standard-output* *error-output*
94 *trace-output* *error-output*)
95 (unless (!c-runtime-noinform-p)
96 (write-string "COLD-INIT... "))
98 ;; Assert that FBOUNDP doesn't choke when its answer is NIL.
99 ;; It was fine if T because in that case the legality of the arg is certain.
100 ;; And be extra paranoid - ensure that it really gets called.
101 (locally (declare (notinline fboundp)) (fboundp '(setf !zzzzzz)))
103 ;; Ensure that *CURRENT-THREAD* and *HANDLER-CLUSTERS* have sane values.
104 ;; create_thread_struct() assigned NIL/unbound-marker respectively.
105 (sb!thread::init-initial-thread)
106 (show-and-call sb!kernel::!target-error-cold-init)
108 ;; Putting data in a synchronized hashtable (*PACKAGE-NAMES*)
109 ;; requires that the main thread be properly initialized.
110 (show-and-call thread-init-or-reinit)
111 ;; Printing of symbols requires that packages be filled in, because
112 ;; OUTPUT-SYMBOL calls FIND-SYMBOL to determine accessibility.
113 (show-and-call !package-cold-init)
114 ;; Fill in the printer's character attribute tables now.
115 ;; If Genesis could write constant arrays into a target core,
116 ;; that would be nice, and would tidy up some other things too.
117 (show-and-call !printer-cold-init)
118 ;; Because L-T-V forms have not executed, CHOOSE-SYMBOL-OUT-FUN doesn't work.
119 (setf real-choose-symbol-out-fun #'choose-symbol-out-fun)
120 (setf (symbol-function 'choose-symbol-out-fun)
121 (lambda (&rest args) (declare (ignore args)) #'output-preserve-symbol))
123 ;; *RAW-SLOT-DATA* is essentially a compile-time constant
124 ;; but isn't dumpable as such because it has functions in it.
125 (show-and-call sb!kernel::!raw-slot-data-init)
127 ;; Anyone might call RANDOM to initialize a hash value or something;
128 ;; and there's nothing which needs to be initialized in order for
129 ;; this to be initialized, so we initialize it right away.
130 (show-and-call !random-cold-init)
132 ;; Must be done before any non-opencoded array references are made.
133 (show-and-call !hairy-data-vector-reffer-init)
135 (show-and-call !character-database-cold-init)
136 (show-and-call !character-name-database-cold-init)
137 (show-and-call sb!unicode::!unicode-properties-cold-init)
139 ;; All sorts of things need INFO and/or (SETF INFO).
140 (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
141 (show-and-call !globaldb-cold-init)
143 ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
144 ;; the basic type machinery needs to be initialized before toplevel
145 ;; forms run.
146 (show-and-call !type-class-cold-init)
147 ;; cold-init-wrappers are closures. Installing a closure as a
148 ;; named function requires consing immobile space code.
149 #!+immobile-code (setq sb!vm::*immobile-space-mutex*
150 (sb!thread:make-mutex :name "Immobile space"))
151 (!with-init-wrappers (show-and-call sb!kernel::!primordial-type-cold-init))
152 (show-and-call !world-lock-cold-init)
153 (show-and-call !classes-cold-init)
154 (show-and-call !early-type-cold-init)
155 (show-and-call !late-type-cold-init)
156 (show-and-call !alien-type-cold-init)
157 (show-and-call !target-type-cold-init)
158 ;; FIXME: It would be tidy to make sure that that these cold init
159 ;; functions are called in the same relative order as the toplevel
160 ;; forms of the corresponding source files.
162 (show-and-call !policy-cold-init-or-resanify)
163 (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
165 ;; Must be done before toplevel forms are invoked
166 ;; because a toplevel defstruct will need to add itself
167 ;; to the subclasses of STRUCTURE-OBJECT.
168 (show-and-call sb!kernel::!set-up-structure-object-class)
170 (dolist (x *!cold-defconstants*)
171 (destructuring-bind (name source-loc &optional docstring) x
172 (setf (info :variable :kind name) :constant)
173 (when source-loc (setf (info :source-location :constant name) source-loc))
174 (when docstring (setf (fdocumentation name 'variable) docstring))))
175 (!with-init-wrappers
176 (dolist (x *!cold-defuns*)
177 (destructuring-bind (name . inline-expansion) x
178 (%defun name (fdefinition name) nil inline-expansion))))
180 ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
181 ;; fixups be done separately? Wouldn't that be clearer and better?
182 ;; -- WHN 19991204
183 (/show0 "doing cold toplevel forms and fixups")
184 (unless (!c-runtime-noinform-p)
185 (write `("Length(TLFs)= " ,(length *!cold-toplevels*)))
186 (terpri))
187 ;; only the basic external formats are present at this point.
188 (setq sb!impl::*default-external-format* :latin-1)
190 (!with-init-wrappers
191 (loop for index-in-cold-toplevels from 0
192 for toplevel-thing in (prog1 *!cold-toplevels*
193 (makunbound '*!cold-toplevels*))
195 #!+sb-show
196 (when (zerop (mod index-in-cold-toplevels 1024))
197 (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
198 (/hexstr index-in-cold-toplevels))
199 (typecase toplevel-thing
200 (function
201 (funcall toplevel-thing))
202 ((cons (eql :load-time-value))
203 (setf (svref *!load-time-values* (third toplevel-thing))
204 (funcall (second toplevel-thing))))
205 ((cons (eql :load-time-value-fixup))
206 (setf (sap-ref-word (int-sap (get-lisp-obj-address (second toplevel-thing)))
207 (third toplevel-thing))
208 (get-lisp-obj-address
209 (svref *!load-time-values* (fourth toplevel-thing)))))
210 ((cons (eql defstruct))
211 (apply 'sb!kernel::%defstruct (cdr toplevel-thing)))
213 (!cold-lose "bogus operation in *!COLD-TOPLEVELS*")))))
214 (/show0 "done with loop over cold toplevel forms and fixups")
216 ;; Precise GC seems to think these symbols are live during the final GC
217 ;; which in turn enlivens a bunch of other "*!foo*" symbols.
218 ;; Setting them to NIL helps a little bit.
219 (setq *!cold-defuns* nil *!cold-defconstants* nil *!cold-toplevels* nil)
221 ;; Now that L-T-V forms have executed, the symbol output chooser works.
222 (setf (symbol-function 'choose-symbol-out-fun) real-choose-symbol-out-fun)
224 (show-and-call time-reinit)
226 ;; Set sane values again, so that the user sees sane values instead
227 ;; of whatever is left over from the last DECLAIM/PROCLAIM.
228 (show-and-call !policy-cold-init-or-resanify)
230 ;; Only do this after toplevel forms have run, 'cause that's where
231 ;; DEFTYPEs are.
232 (setf *type-system-initialized* t)
234 ;; now that the type system is definitely initialized, fixup UNKNOWN
235 ;; types that have crept in.
236 (show-and-call !fixup-type-cold-init)
237 ;; run the PROCLAIMs.
238 (show-and-call !late-proclaim-cold-init)
240 (show-and-call os-cold-init-or-reinit)
241 (show-and-call !pathname-cold-init)
243 (show-and-call stream-cold-init-or-reset)
244 (show-and-call !loader-cold-init)
245 (show-and-call !foreign-cold-init)
246 #!-(and win32 (not sb-thread))
247 (show-and-call signal-cold-init-or-reinit)
249 (show-and-call float-cold-init-or-reinit)
251 (show-and-call !class-finalize)
252 (show-and-call sb!disassem::!compile-inst-printers)
254 ;; Install closures as guards on some early PRINT-OBJECT methods so that
255 ;; THREAD and RESTART print nicely prior to the real methods being installed.
256 (dovector (method (cdr (assoc 'print-object sb!pcl::*!trivial-methods*)))
257 (unless (car method)
258 (let ((classoid (find-classoid (third method))))
259 (rplaca method
260 (lambda (x) (classoid-typep (layout-of x) classoid x))))))
262 ;; The reader and printer are initialized very late, so that they
263 ;; can do hairy things like invoking the compiler as part of their
264 ;; initialization.
265 (let ((*readtable* (make-readtable)))
266 (show-and-call !reader-cold-init)
267 (show-and-call !sharpm-cold-init)
268 (show-and-call !backq-cold-init)
269 ;; The *STANDARD-READTABLE* is assigned at last because the above
270 ;; functions would operate on the standard readtable otherwise---
271 ;; which would result in an error.
272 (setf *standard-readtable* *readtable*))
273 (setf *readtable* (copy-readtable *standard-readtable*))
274 (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
275 (sb!pretty:!pprint-cold-init)
276 (setq *print-level* nil *print-length* nil) ; restore defaults
278 ;; the ANSI-specified initial value of *PACKAGE*
279 (setf *package* (find-package "COMMON-LISP-USER"))
281 ;; Enable normal (post-cold-init) behavior of INFINITE-ERROR-PROTECT.
282 (setf sb!kernel::*maximum-error-depth* 10)
283 (/show0 "enabling internal errors")
284 (setf (extern-alien "internal_errors_enabled" int) 1)
286 ;; Toggle some readonly bits
287 (dovector (sc sb!c:*backend-sc-numbers*)
288 (when sc
289 (logically-readonlyize (sb!c::sc-move-funs sc))
290 (logically-readonlyize (sb!c::sc-load-costs sc))
291 (logically-readonlyize (sb!c::sc-move-vops sc))
292 (logically-readonlyize (sb!c::sc-move-costs sc))))
294 ; hppa heap is segmented, lisp and c uses a stub to call eachother
295 #!+hpux (%primitive sb!vm::setup-return-from-lisp-stub)
296 ;; The system is finally ready for GC.
297 (/show0 "enabling GC")
298 (setq *gc-inhibit* nil)
299 (/show0 "doing first GC")
300 (gc :full t)
301 (/show0 "back from first GC")
303 ;; The show is on.
304 (/show0 "going into toplevel loop")
305 (handling-end-of-the-world
306 (toplevel-init)
307 (critically-unreachable "after TOPLEVEL-INIT")))
309 (define-deprecated-function :early "1.0.56.55" quit (exit sb!thread:abort-thread)
310 (&key recklessly-p (unix-status 0))
311 (if (or recklessly-p (sb!thread:main-thread-p))
312 (exit :code unix-status :abort recklessly-p)
313 (sb!thread:abort-thread))
314 (critically-unreachable "after trying to die in QUIT"))
316 (declaim (ftype (sfunction (&key (:code (or null exit-code))
317 (:timeout (or null real))
318 (:abort t))
319 nil)
320 exit))
321 (defun exit (&key code abort (timeout *exit-timeout*))
322 "Terminates the process, causing SBCL to exit with CODE. CODE
323 defaults to 0 when ABORT is false, and 1 when it is true.
325 When ABORT is false (the default), current thread is first unwound,
326 *EXIT-HOOKS* are run, other threads are terminated, and standard
327 output streams are flushed before SBCL calls exit(3) -- at which point
328 atexit(3) functions will run. If multiple threads call EXIT with ABORT
329 being false, the first one to call it will complete the protocol.
331 When ABORT is true, SBCL exits immediately by calling _exit(2) without
332 unwinding stack, or calling exit hooks. Note that _exit(2) does not
333 call atexit(3) functions unlike exit(3).
335 Recursive calls to EXIT cause EXIT to behave as if ABORT was true.
337 TIMEOUT controls waiting for other threads to terminate when ABORT is
338 NIL. Once current thread has been unwound and *EXIT-HOOKS* have been
339 run, spawning new threads is prevented and all other threads are
340 terminated by calling TERMINATE-THREAD on them. The system then waits
341 for them to finish using JOIN-THREAD, waiting at most a total TIMEOUT
342 seconds for all threads to join. Those threads that do not finish
343 in time are simply ignored while the exit protocol continues. TIMEOUT
344 defaults to *EXIT-TIMEOUT*, which in turn defaults to 60. TIMEOUT NIL
345 means to wait indefinitely.
347 Note that TIMEOUT applies only to JOIN-THREAD, not *EXIT-HOOKS*. Since
348 TERMINATE-THREAD is asynchronous, getting multithreaded application
349 termination with complex cleanups right using it can be tricky. To
350 perform an orderly synchronous shutdown use an exit hook instead of
351 relying on implicit thread termination.
353 Consequences are unspecified if serious conditions occur during EXIT
354 excepting errors from *EXIT-HOOKS*, which cause warnings and stop
355 execution of the hook that signaled, but otherwise allow the exit
356 process to continue normally."
357 (if (or abort *exit-in-process*)
358 (os-exit (or code 1) :abort t)
359 (let ((code (or code 0)))
360 (with-deadline (:seconds nil :override t)
361 (sb!thread:grab-mutex *exit-lock*))
362 (setf *exit-in-process* code
363 *exit-timeout* timeout)
364 (throw '%end-of-the-world t)))
365 (critically-unreachable "After trying to die in EXIT."))
367 ;;;; initialization functions
369 (defun thread-init-or-reinit ()
370 (sb!thread::init-job-control)
371 (sb!thread::get-foreground))
373 (defun reinit ()
374 #!+win32
375 (setf sb!win32::*ansi-codepage* nil)
376 (setf *default-external-format* nil)
377 (setf sb!alien::*default-c-string-external-format* nil)
378 ;; WITHOUT-GCING implies WITHOUT-INTERRUPTS.
379 (without-gcing
380 ;; Create *CURRENT-THREAD* first, since initializing a stream calls
381 ;; ALLOC-BUFFER which calls FINALIZE which acquires **FINALIZER-STORE-LOCK**
382 ;; which needs a valid thread in order to grab a mutex.
383 (sb!thread::init-initial-thread)
384 ;; Initialize streams first, so that any errors can be printed later
385 (stream-reinit t)
386 (os-cold-init-or-reinit)
387 (thread-init-or-reinit)
388 #!-(and win32 (not sb-thread))
389 (signal-cold-init-or-reinit)
390 (setf (extern-alien "internal_errors_enabled" int) 1)
391 (float-cold-init-or-reinit))
392 (gc-reinit)
393 (foreign-reinit)
394 (time-reinit)
395 ;; If the debugger was disabled in the saved core, we need to
396 ;; re-disable ldb again.
397 (when (eq *invoke-debugger-hook* 'sb!debug::debugger-disabled-hook)
398 (sb!debug::disable-debugger))
399 (call-hooks "initialization" *init-hooks*))
401 ;;;; some support for any hapless wretches who end up debugging cold
402 ;;;; init code
404 ;;; Decode THING into hexadecimal notation using only machinery
405 ;;; available early in cold init.
406 #!+sb-show
407 (defun hexstr (thing)
408 (/noshow0 "entering HEXSTR")
409 (let* ((addr (get-lisp-obj-address thing))
410 (nchars (* sb!vm:n-word-bytes 2))
411 (str (make-string (+ nchars 2) :element-type 'base-char)))
412 (/noshow0 "ADDR and STR calculated")
413 (setf (char str 0) #\0
414 (char str 1) #\x)
415 (/noshow0 "CHARs 0 and 1 set")
416 (dotimes (i nchars)
417 (/noshow0 "at head of DOTIMES loop")
418 (let* ((nibble (ldb (byte 4 0) addr))
419 (chr (char "0123456789abcdef" nibble)))
420 (declare (type (unsigned-byte 4) nibble)
421 (base-char chr))
422 (/noshow0 "NIBBLE and CHR calculated")
423 (setf (char str (- (1+ nchars) i)) chr
424 addr (ash addr -4))))
425 str))
427 ;; But: you almost never need this. Just use WRITE in all its glory.
428 #!+sb-show
429 (defun cold-print (x)
430 (labels ((%cold-print (obj depthoid)
431 (if (> depthoid 4)
432 (%primitive print "...")
433 (typecase obj
434 (simple-string
435 (%primitive print obj))
436 (symbol
437 (%primitive print (symbol-name obj)))
438 (cons
439 (%primitive print "cons:")
440 (let ((d (1+ depthoid)))
441 (%cold-print (car obj) d)
442 (%cold-print (cdr obj) d)))
444 (%primitive print (hexstr obj)))))))
445 (%cold-print x 0))
446 (values))
448 (in-package "SB!INT")
449 (defun !unintern-symbols ()
450 ;; For some reason uninterning these:
451 ;; DEF!TYPE DEF!CONSTANT DEF!STRUCT
452 ;; does not work, they stick around as uninterned symbols.
453 ;; Some other macros must expand into them. Ugh.
454 '("SB-INT"
455 defenum defun-cached with-globaldb-name
457 #!+sb-show ()
458 #!-sb-show (/hexstr /nohexstr /noshow /noshow0 /noxhow
459 /primitive-print /show /show0 /xhow)))