1 ;;;; Dump the current Lisp image into a core file. Also contains
2 ;;;; various high-level initialization stuff: loading init files and
3 ;;;; parsing environment variables.
5 ;;;; (All the real work is done by C.)
7 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
16 (in-package "SB!IMPL")
18 ;;;; SAVE-LISP-AND-DIE itself
20 (sb!alien:define-alien-routine "save" (sb!alien:boolean)
21 (file sb!alien:c-string)
22 (initial-fun (sb!alien:unsigned #.sb!vm:n-word-bits)))
24 ;;; FIXME: When this is run without the PURIFY option,
25 ;;; it seems to save memory all the way up to the high-water mark,
26 ;;; not just what's currently used; and then after loading the
27 ;;; image to make a running Lisp, the memory never gets reclaimed.
28 ;;; (But with the PURIFY option it seems to work OK.)
29 (defun save-lisp-and-die (core-file-name &key
30 (toplevel #'toplevel-init)
33 (environment-name "auxiliary"))
35 "Save a \"core image\", i.e. enough information to restart a Lisp
36 process later in the same state, in the file of the specified name.
38 This implementation is not as polished and painless as you might like:
39 * It corrupts the current Lisp image enough that the current process
40 needs to be killed afterwards.
41 * There is absolutely no binary compatibility of core images between
42 different runtime support programs. Even runtimes built from the same
43 sources at different times are treated as incompatible for this purpose.
44 This isn't because we like it this way, but just because there don't
45 seem to be good quick fixes for either limitation and no one has been
46 sufficiently motivated to do lengthy fixes.
48 The following &KEY arguments are defined:
50 The function to run when the created core file is resumed.
51 The default function handles command line toplevel option
52 processing and runs the top level read-eval-print loop. This
53 function should not return.
55 If true (the default), do a purifying GC which moves all dynamically
56 allocated objects into static space so that they stay pure. This takes
57 somewhat longer than the normal GC which is otherwise done, but it's
58 only done once, and subsequent GC's will be done less often and will
59 take less time in the resulting core file. See the PURIFY function.
61 This should be a list of the main entry points in any newly loaded
62 systems. This need not be supplied, but locality and/or GC performance
63 may be better if they are. Meaningless if :PURIFY is NIL. See the
66 This is also passed to the PURIFY function when :PURIFY is T.
69 The save/load process changes the values of some global variables:
70 *STANDARD-OUTPUT*, *DEBUG-IO*, etc.
71 Everything related to open streams is necessarily changed, since
72 the OS won't let us preserve a stream across save and load.
73 *DEFAULT-PATHNAME-DEFAULTS*
74 This is reinitialized to reflect the working directory where the
75 saved core is loaded."
77 ;; FIXME (sb!mp::shutdown-multi-processing)
78 ;; FIXME: What is this for? Explain.
79 (when (fboundp 'cancel-finalization)
80 (cancel-finalization sb!sys:*tty*))
81 ;; FIXME: Would it be possible to unmix the PURIFY logic from this
82 ;; function, and just do a GC :FULL T here? (Then if the user wanted
83 ;; a PURIFYed image, he'd just run PURIFY immediately before calling
84 ;; SAVE-LISP-AND-DIE.)
86 (purify :root-structures root-structures
87 :environment-name environment-name)
88 #!-gencgc (gc) #!+gencgc (gc :full t))
89 ;; FIXME: Wouldn't it be more correct to go through this list backwards
90 ;; instead of forwards?
91 (dolist (f *before-save-initializations*)
93 (flet ((restart-lisp ()
94 (handling-end-of-the-world
96 (dolist (f *after-save-initializations*)
99 ;; FIXME: Perhaps WITHOUT-GCING should be wrapped around the
100 ;; LET as well, to avoid the off chance of an interrupt triggering
101 ;; GC and making our saved RESTART-LISP address invalid?
103 (save (unix-namestring core-file-name nil)
104 (get-lisp-obj-address #'restart-lisp)))))