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