0.9.2.43:
[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 ;;; 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)
31                                          (purify t)
32                                          (root-structures ())
33                                          (environment-name "auxiliary"))
34   #!+sb-doc
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.
37 Only global state is preserved: the stack is unwound in the process.
38
39 The following &KEY arguments are defined:
40
41   :TOPLEVEL
42      The function to run when the created core file is resumed. The
43      default function handles command line toplevel option processing
44      and runs the top level read-eval-print loop. This function should
45      not return.
46
47   :PURIFY
48      If true (the default), do a purifying GC which moves all
49      dynamically allocated objects into static space. This takes
50      somewhat longer than the normal GC which is otherwise done, but
51      it's only done once, and subsequent GC's will be done less often
52      and will take less time in the resulting core file. See the PURIFY
53      function.
54
55   :ROOT-STRUCTURES
56      This should be a list of the main entry points in any newly loaded
57      systems. This need not be supplied, but locality and/or GC performance
58      may be better if they are. Meaningless if :PURIFY is NIL. See the
59      PURIFY function.
60
61   :ENVIRONMENT-NAME
62      This is also passed to the PURIFY function when :PURIFY is T.
63      (rarely used)
64
65 The save/load process changes the values of some global variables:
66
67   *STANDARD-OUTPUT*, *DEBUG-IO*, etc.
68     Everything related to open streams is necessarily changed, since
69     the OS won't let us preserve a stream across save and load.
70
71   *DEFAULT-PATHNAME-DEFAULTS*
72     This is reinitialized to reflect the working directory where the
73     saved core is loaded.
74
75 Foreign objects loaded with SB-ALIEN:LOAD-SHARED-OBJECT are
76 automatically reloaded on startup, but references to foreign symbols
77 do not survive intact on all platforms: in this case a WARNING is
78 signalled when saving the core. If no warning is signalled, then the
79 foreign symbol references will remain intact. Platforms where this is
80 currently the case are x86/FreeBSD, x86/Linux, x86/NetBSD,
81 sparc/Linux, sparc/SunOS, and ppc/Darwin.
82
83 This implementation is not as polished and painless as you might like:
84   * It corrupts the current Lisp image enough that the current process
85     needs to be killed afterwards. This can be worked around by forking
86     another process that saves the core.
87   * It will not work if multiple threads are in use.
88   * There is absolutely no binary compatibility of core images between
89     different runtime support programs. Even runtimes built from the same
90     sources at different times are treated as incompatible for this
91     purpose.
92 This isn't because we like it this way, but just because there don't
93 seem to be good quick fixes for either limitation and no one has been
94 sufficiently motivated to do lengthy fixes."
95   (deinit)
96   ;; FIXME: Would it be possible to unmix the PURIFY logic from this
97   ;; function, and just do a GC :FULL T here? (Then if the user wanted
98   ;; a PURIFYed image, he'd just run PURIFY immediately before calling
99   ;; SAVE-LISP-AND-DIE.)
100   (if purify
101       (purify :root-structures root-structures
102               :environment-name environment-name)
103       #-gencgc (gc) #+gencgc (gc :full t))
104   (flet ((restart-lisp ()
105            (handling-end-of-the-world
106              (reinit)
107              (funcall toplevel))))
108     ;; FIXME: Perhaps WITHOUT-GCING should be wrapped around the
109     ;; LET as well, to avoid the off chance of an interrupt triggering
110     ;; GC and making our saved RESTART-LISP address invalid?
111     (without-gcing
112      (save (unix-namestring core-file-name nil)
113            (get-lisp-obj-address #'restart-lisp)))))
114
115 (defun deinit ()
116   (dolist (hook *save-hooks*)
117     (with-simple-restart (continue "Skip this save hook.")
118       (funcall hook)))
119   (when (fboundp 'cancel-finalization)
120     (cancel-finalization sb!sys:*tty*))
121   (profile-deinit)
122   (debug-deinit)
123   (foreign-deinit))