Optional support for zlib-based in-memory deflate/inflate for core files
[sbcl.git] / src / code / save.lisp
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.
4 ;;;;
5 ;;;; (All the real work is done by C.)
6
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
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.
15
16 (in-package "SB!IMPL")
17 \f
18 ;;;; SAVE-LISP-AND-DIE itself
19
20 (define-alien-routine "save" (boolean)
21   (file c-string)
22   (initial-fun (unsigned #.sb!vm:n-word-bits))
23   (prepend-runtime int)
24   (save-runtime-options int)
25   (compressed int)
26   (compression-level int))
27
28 #!+gencgc
29 (define-alien-routine "gc_and_save" void
30   (file c-string)
31   (prepend-runtime int)
32   (save-runtime-options int)
33   (compressed int)
34   (compression-level int))
35
36 #!+gencgc
37 (defvar sb!vm::*restart-lisp-function*)
38
39 (defun save-lisp-and-die (core-file-name &key
40                                          (toplevel #'toplevel-init)
41                                          (executable nil)
42                                          (save-runtime-options nil)
43                                          (purify t)
44                                          (root-structures ())
45                                          (environment-name "auxiliary")
46                                          (compression nil))
47   #!+sb-doc
48   "Save a \"core image\", i.e. enough information to restart a Lisp
49 process later in the same state, in the file of the specified name.
50 Only global state is preserved: the stack is unwound in the process.
51
52 The following &KEY arguments are defined:
53
54   :TOPLEVEL
55      The function to run when the created core file is resumed. The
56      default function handles command line toplevel option processing
57      and runs the top level read-eval-print loop. This function returning
58      is equivalent to (SB-EXT:QUIT :UNIX-STATUS 0) being called.
59
60   :EXECUTABLE
61      If true, arrange to combine the SBCL runtime and the core image
62      to create a standalone executable.  If false (the default), the
63      core image will not be executable on its own. Executable images
64      always behave as if they were passed the --noinform runtime option.
65
66   :SAVE-RUNTIME-OPTIONS
67      If true, values of runtime options --dynamic-space-size and
68      --control-stack-size that were used to start SBCL are stored in
69      the standalone executable, and restored when the executable is
70      run. This also inhibits normal runtime option processing, causing
71      all command line arguments to be passed to the toplevel.
72      Meaningless if :EXECUTABLE is NIL.
73
74   :PURIFY
75      If true (the default on cheneygc), do a purifying GC which moves all
76      dynamically allocated objects into static space. This takes
77      somewhat longer than the normal GC which is otherwise done, but
78      it's only done once, and subsequent GC's will be done less often
79      and will take less time in the resulting core file. See the PURIFY
80      function. This parameter has no effect on platforms using the
81      generational garbage collector.
82
83   :ROOT-STRUCTURES
84      This should be a list of the main entry points in any newly loaded
85      systems. This need not be supplied, but locality and/or GC performance
86      may be better if they are. Meaningless if :PURIFY is NIL. See the
87      PURIFY function.
88
89   :ENVIRONMENT-NAME
90      This is also passed to the PURIFY function when :PURIFY is T.
91      (rarely used)
92
93   :COMPRESSION
94      This is only meaningful if the runtime was built with the :SB-CORE-COMPRESSION
95      feature enabled. If NIL (the default), saves to uncompressed core files. If
96      :SB-CORE-COMPRESSION was enabled at build-time, the argument may also be
97      an integer from -1 to 9, corresponding to zlib compression levels, or T
98      (which is equivalent to the default compression level, -1).
99
100 The save/load process changes the values of some global variables:
101
102   *STANDARD-OUTPUT*, *DEBUG-IO*, etc.
103     Everything related to open streams is necessarily changed, since
104     the OS won't let us preserve a stream across save and load.
105
106   *DEFAULT-PATHNAME-DEFAULTS*
107     This is reinitialized to reflect the working directory where the
108     saved core is loaded.
109
110 SAVE-LISP-AND-DIE interacts with SB-ALIEN:LOAD-SHARED-OBJECT: see its
111 documentation for details.
112
113 On threaded platforms only a single thread may remain running after
114 SB-EXT:*SAVE-HOOKS* have run. Applications using multiple threads can
115 be SAVE-LISP-AND-DIE friendly by registering a save-hook that quits
116 any additional threads, and an init-hook that restarts them.
117
118 This implementation is not as polished and painless as you might like:
119   * It corrupts the current Lisp image enough that the current process
120     needs to be killed afterwards. This can be worked around by forking
121     another process that saves the core.
122   * There is absolutely no binary compatibility of core images between
123     different runtime support programs. Even runtimes built from the same
124     sources at different times are treated as incompatible for this
125     purpose.
126 This isn't because we like it this way, but just because there don't
127 seem to be good quick fixes for either limitation and no one has been
128 sufficiently motivated to do lengthy fixes."
129   #!+gencgc
130   (declare (ignore purify root-structures environment-name))
131   #!+sb-core-compression
132   (check-type compression (or boolean (integer -1 9)))
133   #!-sb-core-compression
134   (when compression
135     (error "Unable to save compressed core: this runtime was not built with zlib support"))
136   (when (eql t compression)
137     (setf compression -1))
138   (tune-hashtable-sizes-of-all-packages)
139   (deinit)
140   ;; FIXME: Would it be possible to unmix the PURIFY logic from this
141   ;; function, and just do a GC :FULL T here? (Then if the user wanted
142   ;; a PURIFYed image, he'd just run PURIFY immediately before calling
143   ;; SAVE-LISP-AND-DIE.)
144   (labels ((restart-lisp ()
145              (handling-end-of-the-world
146                (reinit)
147                #!+hpux (sb!sys:%primitive sb!vm::setup-return-from-lisp-stub)
148                (funcall toplevel)))
149            (foreign-bool (value)
150              (if value 1 0))
151            (save-core (gc)
152              (let ((name (native-namestring
153                           (physicalize-pathname core-file-name)
154                           :as-file t)))
155                (when gc
156                  #!-gencgc (gc)
157                  ;; Do a destructive non-conservative GC, and then save a core.
158                  ;; A normal GC will leave huge amounts of storage unreclaimed
159                  ;; (over 50% on x86). This needs to be done by a single function
160                  ;; since the GC will invalidate the stack.
161                  #!+gencgc (gc-and-save name
162                                         (foreign-bool executable)
163                                         (foreign-bool save-runtime-options)
164                                         (foreign-bool compression)
165                                         (or compression 0)))
166                (without-gcing
167                  (save name
168                        (get-lisp-obj-address #'restart-lisp)
169                        (foreign-bool executable)
170                        (foreign-bool save-runtime-options)
171                        (foreign-bool compression)
172                        (or compression 0))))))
173     ;; Save the restart function into a static symbol, to allow GC-AND-SAVE
174     ;; access to it even after the GC has moved it.
175     #!+gencgc
176     (setf sb!vm::*restart-lisp-function* #'restart-lisp)
177     (cond #!-gencgc
178           (purify
179            (purify :root-structures root-structures
180                    :environment-name environment-name)
181            (save-core nil))
182           (t
183            ;; Compact the environment even though we're skipping the
184            ;; other purification stages.
185            (sb!kernel::compact-environment-aux "Auxiliary" 200)
186            (save-core t)))
187     ;; Something went very wrong -- reinitialize to have a prayer
188     ;; of being able to report the error.
189     (reinit)
190     (error "Could not save core.")))
191
192 (defun deinit ()
193   (call-hooks "save" *save-hooks*)
194   (when (rest (sb!thread:list-all-threads))
195     (error "Cannot save core with multiple threads running."))
196   (float-deinit)
197   (profile-deinit)
198   (debug-deinit)
199   (foreign-deinit)
200   (stream-deinit)
201   (deinit-finalizers)
202   (drop-all-hash-caches))