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