0.9.5.79:
[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
24 #!+gencgc
25 (define-alien-routine "gc_and_save" void
26   (file c-string))
27
28 #!+gencgc
29 (defvar sb!vm::*restart-lisp-function*)
30
31 (defun save-lisp-and-die (core-file-name &key
32                                          (toplevel #'toplevel-init)
33                                          (purify #!+gencgc nil
34                                                  #!-gencgc t)
35                                          (root-structures ())
36                                          (environment-name "auxiliary"))
37   #!+sb-doc
38   "Save a \"core image\", i.e. enough information to restart a Lisp
39 process later in the same state, in the file of the specified name.
40 Only global state is preserved: the stack is unwound in the process.
41
42 The following &KEY arguments are defined:
43
44   :TOPLEVEL
45      The function to run when the created core file is resumed. The
46      default function handles command line toplevel option processing
47      and runs the top level read-eval-print loop. This function should
48      not return.
49
50   :PURIFY
51      If true (the default on cheneygc), do a purifying GC which moves all
52      dynamically allocated objects into static space. This takes
53      somewhat longer than the normal GC which is otherwise done, but
54      it's only done once, and subsequent GC's will be done less often
55      and will take less time in the resulting core file. See the PURIFY
56      function. For platforms that use the generational garbage collector
57      (x86 and x86-64) purification generally results in a loss of
58      performance.
59
60   :ROOT-STRUCTURES
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
64      PURIFY function.
65
66   :ENVIRONMENT-NAME
67      This is also passed to the PURIFY function when :PURIFY is T.
68      (rarely used)
69
70 The save/load process changes the values of some global variables:
71
72   *STANDARD-OUTPUT*, *DEBUG-IO*, etc.
73     Everything related to open streams is necessarily changed, since
74     the OS won't let us preserve a stream across save and load.
75
76   *DEFAULT-PATHNAME-DEFAULTS*
77     This is reinitialized to reflect the working directory where the
78     saved core is loaded.
79
80 Foreign objects loaded with SB-ALIEN:LOAD-SHARED-OBJECT are
81 automatically reloaded on startup, but references to foreign symbols
82 do not survive intact on all platforms: in this case a WARNING is
83 signalled when saving the core. If no warning is signalled, then the
84 foreign symbol references will remain intact. Platforms where this is
85 currently the case are x86/FreeBSD, x86/Linux, x86/NetBSD,
86 sparc/Linux, sparc/SunOS, and ppc/Darwin.
87
88 This implementation is not as polished and painless as you might like:
89   * It corrupts the current Lisp image enough that the current process
90     needs to be killed afterwards. This can be worked around by forking
91     another process that saves the core.
92   * It will not work if multiple threads are in use.
93   * There is absolutely no binary compatibility of core images between
94     different runtime support programs. Even runtimes built from the same
95     sources at different times are treated as incompatible for this
96     purpose.
97 This isn't because we like it this way, but just because there don't
98 seem to be good quick fixes for either limitation and no one has been
99 sufficiently motivated to do lengthy fixes."
100   (deinit)
101   ;; FIXME: Would it be possible to unmix the PURIFY logic from this
102   ;; function, and just do a GC :FULL T here? (Then if the user wanted
103   ;; a PURIFYed image, he'd just run PURIFY immediately before calling
104   ;; SAVE-LISP-AND-DIE.)
105   (labels ((restart-lisp ()
106              (handling-end-of-the-world
107                (reinit)
108                (funcall toplevel)))
109            (save-core (gc)
110              (when gc
111                #!-gencgc (gc)
112                ;; Do a destructive non-conservative GC, and then save a core.
113                ;; A normal GC will leave huge amounts of storage unreclaimed
114                ;; (over 50% on x86). This needs to be done by a single function
115                ;; since the GC will invalidate the stack.
116                #!+gencgc (gc-and-save (unix-namestring core-file-name nil)))
117              (without-gcing
118               (save (unix-namestring core-file-name nil)
119                     (get-lisp-obj-address #'restart-lisp)))))
120     ;; Save the restart function into a static symbol, to allow GC-AND-SAVE
121     ;; access to it even after the GC has moved it.
122     #!+gencgc
123     (setf sb!vm::*restart-lisp-function* #'restart-lisp)
124     (cond (purify
125            (purify :root-structures root-structures
126                    :environment-name environment-name)
127            (save-core nil))
128           (t
129            ;; Compact the environment even though we're skipping the
130            ;; other purification stages.
131            (sb!kernel::compact-environment-aux "Auxiliary" 200)
132            (save-core t)))))
133
134 (defun deinit ()
135   (dolist (hook *save-hooks*)
136     (with-simple-restart (continue "Skip this save hook.")
137       (funcall hook)))
138   (when (fboundp 'cancel-finalization)
139     (cancel-finalization sb!sys:*tty*))
140   (profile-deinit)
141   (debug-deinit)
142   (foreign-deinit))