0.6.12.18:
[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 (defvar *before-save-initializations* nil
19   #!+sb-doc
20   "This is a list of functions which are called before creating a saved core
21   image. These functions are executed in the child process which has no ports,
22   so they cannot do anything that tries to talk to the outside world.")
23
24 (defvar *after-save-initializations* nil
25   #!+sb-doc
26   "This is a list of functions which are called when a saved core image starts
27   up. The system itself should be initialized at this point, but applications
28   might not be.")
29 \f
30 ;;;; SAVE-LISP-AND-DIE itself
31
32 (sb!alien:def-alien-routine "save" (sb!alien:boolean)
33   (file sb!c-call:c-string)
34   (initial-function (sb!alien:unsigned #.sb!vm:word-bits)))
35
36 ;;; FIXME: When this is run without the PURIFY option,
37 ;;; it seems to save memory all the way up to the high-water mark,
38 ;;; not just what's currently used; and then after loading the
39 ;;; image to make a running Lisp, the memory never gets reclaimed.
40 ;;; (But with the PURIFY option it seems to work OK.)
41 (defun save-lisp-and-die (core-file-name &key
42                                          (toplevel #'toplevel-init)
43                                          (purify nil)
44                                          (root-structures ())
45                                          (environment-name "auxiliary"))
46   #!+sb-doc
47   "Saves a CMU Common Lisp core image in the file of the specified name,
48   killing the current Lisp invocation in the process (unless it bails
49   out early because of some argument error or something).
50
51   The following &KEY args are defined:
52     :TOPLEVEL
53        The function to run when the created core file is resumed.
54        The default function handles command line toplevel option
55        processing and runs the top level read-eval-print loop. This
56        function should not return.
57     :PURIFY
58        If true (the default), do a purifying GC which moves all dynamically
59        allocated objects into static space so that they stay pure. This takes
60        somewhat longer than the normal GC which is otherwise done, but it's
61        only done once, and subsequent GC's will be done less often and will
62        take less time in the resulting core file. See PURIFY.
63     :ROOT-STRUCTURES
64        This should be a list of the main entry points in any newly loaded
65        systems. This need not be supplied, but locality and/or GC performance
66        may be better if they are. Meaningless if :PURIFY is NIL. See PURIFY.
67     :ENVIRONMENT-NAME
68        This is also passed to PURIFY when :PURIFY is T. (rarely used)
69
70   The save/load process changes the values of some global variables:
71     *STANDARD-OUTPUT*, *DEBUG-IO*, etc.
72       Everything related to open streams is necessarily changed, since
73       the OS won't let us preserve a stream across save and load.
74     *DEFAULT-PATHNAME-DEFAULTS*
75       This is reinitialized to reflect the working directory where the
76       saved core is loaded."
77
78   #!+mp (sb!mp::shutdown-multi-processing)
79   (when (fboundp 'sb!eval:flush-interpreted-function-cache)
80     (sb!eval:flush-interpreted-function-cache))
81   ;; FIXME: What is this for? Explain.
82   (when (fboundp 'cancel-finalization)
83     (cancel-finalization sb!sys:*tty*))
84   ;; FIXME: Would it be possible to unmix the PURIFY logic from this
85   ;; function, and just do a GC :FULL T here? (Then if the user wanted
86   ;; a PURIFYed image, he'd just run PURIFY immediately before calling
87   ;; SAVE-LISP-AND-DIE.)
88   (if purify
89       (purify :root-structures root-structures
90               :environment-name environment-name)
91       #!-gencgc (gc) #!+gencgc (gc :full t))
92   ;; FIXME: Wouldn't it be more correct to go through this list backwards
93   ;; instead of forwards?
94   (dolist (f *before-save-initializations*)
95     (funcall f))
96   (flet ((restart-lisp ()
97            (handling-end-of-the-world
98              (reinit)
99              (dolist (f *after-save-initializations*)
100                (funcall f))
101              (funcall toplevel))))
102     ;; FIXME: Perhaps WITHOUT-GCING should be wrapped around the
103     ;; LET as well, to avoid the off chance of an interrupt triggering
104     ;; GC and making our saved RESTART-LISP address invalid?
105     (without-gcing
106       (save (unix-namestring core-file-name nil)
107             (get-lisp-obj-address #'restart-lisp)))))
108 \f
109 ;;;; functions used by worldload.lisp in CMU CL bootstrapping
110
111 ;;; If NAME has been byte-compiled, and :RUNTIME is a feature, then
112 ;;; load the byte-compiled version, otherwise just do normal load.
113 #+nil ; no longer needed in SBCL.. I think.. -- WHN 19990814
114 (defun maybe-byte-load (name &optional (load-native t))
115   (let ((bname (make-pathname
116                 :defaults name
117                 :type #.(sb!c:backend-byte-fasl-file-type))))
118     (cond ((and (featurep :runtime)
119                 (probe-file bname))
120            (load bname))
121           (load-native
122            (load name)))))
123
124 ;;; Replace a cold-loaded native object file with a byte-compiled one, if it
125 ;;; exists.
126 #+nil ; no longer needed in SBCL.. I think.. -- WHN 19990814
127 (defun byte-load-over (name)
128   (load (make-pathname
129          :defaults name
130          :type #.(sb!c:backend-byte-fasl-file-type))
131         :if-does-not-exist nil))