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 (define-alien-routine "save" (boolean)
22 (initial-fun (unsigned #.sb!vm:n-word-bits))
24 (save-runtime-options int))
27 (define-alien-routine "gc_and_save" void
30 (save-runtime-options int))
33 (defvar sb!vm::*restart-lisp-function*)
35 (defun save-lisp-and-die (core-file-name &key
36 (toplevel #'toplevel-init)
38 (save-runtime-options nil)
41 (environment-name "auxiliary"))
43 "Save a \"core image\", i.e. enough information to restart a Lisp
44 process later in the same state, in the file of the specified name.
45 Only global state is preserved: the stack is unwound in the process.
47 The following &KEY arguments are defined:
50 The function to run when the created core file is resumed. The
51 default function handles command line toplevel option processing
52 and runs the top level read-eval-print loop. This function should
56 If true, arrange to combine the SBCL runtime and the core image
57 to create a standalone executable. If false (the default), the
58 core image will not be executable on its own. Executable images
59 always behave as if they were passed the --noinform runtime option.
62 If true, values of runtime options --dynamic-space-size and
63 --control-stack-size that were used to start SBCL are stored in
64 the standalone executable, and restored when the executable is
65 run. This also inhibits normal runtime option processing, causing
66 all command line arguments to be passed to the toplevel.
67 Meaningless if :EXECUTABLE is NIL.
70 If true (the default on cheneygc), do a purifying GC which moves all
71 dynamically allocated objects into static space. This takes
72 somewhat longer than the normal GC which is otherwise done, but
73 it's only done once, and subsequent GC's will be done less often
74 and will take less time in the resulting core file. See the PURIFY
75 function. This parameter has no effect on platforms using the
76 generational garbage collector.
79 This should be a list of the main entry points in any newly loaded
80 systems. This need not be supplied, but locality and/or GC performance
81 may be better if they are. Meaningless if :PURIFY is NIL. See the
85 This is also passed to the PURIFY function when :PURIFY is T.
88 The save/load process changes the values of some global variables:
90 *STANDARD-OUTPUT*, *DEBUG-IO*, etc.
91 Everything related to open streams is necessarily changed, since
92 the OS won't let us preserve a stream across save and load.
94 *DEFAULT-PATHNAME-DEFAULTS*
95 This is reinitialized to reflect the working directory where the
98 SAVE-LISP-AND-DIE interacts with SB-ALIEN:LOAD-FOREIGN-OBJECT: see its
99 documentation for details.
101 On threaded platforms only a single thread may remain running after
102 SB-EXT:*SAVE-HOOKS* have run. Applications using multiple threads can
103 be SAVE-LISP-AND-DIE friendly by registering a save-hook that quits
104 any additional threads, and an init-hook that restarts them.
106 This implementation is not as polished and painless as you might like:
107 * It corrupts the current Lisp image enough that the current process
108 needs to be killed afterwards. This can be worked around by forking
109 another process that saves the core.
110 * There is absolutely no binary compatibility of core images between
111 different runtime support programs. Even runtimes built from the same
112 sources at different times are treated as incompatible for this
114 This isn't because we like it this way, but just because there don't
115 seem to be good quick fixes for either limitation and no one has been
116 sufficiently motivated to do lengthy fixes."
118 (declare (ignore purify root-structures environment-name))
119 (tune-hashtable-sizes-of-all-packages)
121 ;; FIXME: Would it be possible to unmix the PURIFY logic from this
122 ;; function, and just do a GC :FULL T here? (Then if the user wanted
123 ;; a PURIFYed image, he'd just run PURIFY immediately before calling
124 ;; SAVE-LISP-AND-DIE.)
125 (labels ((restart-lisp ()
126 (handling-end-of-the-world
129 (foreign-bool (value)
134 ;; Do a destructive non-conservative GC, and then save a core.
135 ;; A normal GC will leave huge amounts of storage unreclaimed
136 ;; (over 50% on x86). This needs to be done by a single function
137 ;; since the GC will invalidate the stack.
138 #!+gencgc (gc-and-save (unix-namestring core-file-name nil)
139 (foreign-bool executable)
140 (foreign-bool save-runtime-options))
142 (save (unix-namestring core-file-name nil)
143 (get-lisp-obj-address #'restart-lisp)
144 (foreign-bool executable)
145 (foreign-bool save-runtime-options))))))
146 ;; Save the restart function into a static symbol, to allow GC-AND-SAVE
147 ;; access to it even after the GC has moved it.
149 (setf sb!vm::*restart-lisp-function* #'restart-lisp)
152 (purify :root-structures root-structures
153 :environment-name environment-name)
156 ;; Compact the environment even though we're skipping the
157 ;; other purification stages.
158 (sb!kernel::compact-environment-aux "Auxiliary" 200)
162 (call-hooks "save" *save-hooks*)
163 (when (rest (sb!thread:list-all-threads))
164 (error "Cannot save core with multiple threads running."))