0.6.7.22: removed CVS dollar-Header-dollar tags from sources
[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)
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 keyword args are defined:
52
53   :TOPLEVEL
54       The function to run when the created core file is resumed.
55   The default function handles command line toplevel option
56   processing and runs the top level read-eval-print loop. This
57   function should not return.
58
59   :PURIFY
60       If true (the default), do a purifying GC which moves all dynamically
61   allocated objects into static space so that they stay pure. This takes
62   somewhat longer than the normal GC which is otherwise done, but it's only
63   done once, and subsequent GC's will be done less often and will take less
64   time in the resulting core file. See PURIFY.
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 PURIFY.
70
71   :ENVIRONMENT-NAME
72       This is also passed to PURIFY when :PURIFY is T. (rarely used)"
73
74   #!+mp (sb!mp::shutdown-multi-processing)
75   (when (fboundp 'sb!eval:flush-interpreted-function-cache)
76     (sb!eval:flush-interpreted-function-cache))
77   ;; FIXME: What is this for? Explain.
78   (when (fboundp 'cancel-finalization)
79     (cancel-finalization sb!sys:*tty*))
80   ;; FIXME: Would it be possible to unmix the PURIFY logic from this
81   ;; function, and just do a GC :FULL T here? (Then if the user wanted
82   ;; a PURIFYed image, he'd just run PURIFY immediately before calling
83   ;; SAVE-LISP-AND-DIE.)
84   (if purify
85       (purify :root-structures root-structures
86               :environment-name environment-name)
87       #!-gencgc (gc) #!+gencgc (gc :full t))
88   ;; FIXME: Wouldn't it be more correct to go through this list backwards
89   ;; instead of forwards?
90   (dolist (f *before-save-initializations*)
91     (funcall f))
92   (flet ((restart-lisp ()
93            (handling-end-of-the-world
94              (reinit)
95              (dolist (f *after-save-initializations*)
96                (funcall f))
97              (funcall toplevel))))
98     ;; FIXME: Perhaps WITHOUT-GCING should be wrapped around the
99     ;; LET as well, to avoid the off chance of an interrupt triggering
100     ;; GC and making our saved RESTART-LISP address invalid?
101     (without-gcing
102       (save (unix-namestring core-file-name nil)
103             (get-lisp-obj-address #'restart-lisp)))))
104 \f
105 ;;;; functions used by worldload.lisp in CMU CL bootstrapping
106
107 ;;; If Name has been byte-compiled, and :RUNTIME is a feature, then load the
108 ;;; byte-compiled version, otherwise just do normal load.
109 #+nil ; no longer needed in SBCL.. I think.. -- WHN 19990814
110 (defun maybe-byte-load (name &optional (load-native t))
111   (let ((bname (make-pathname
112                 :defaults name
113                 :type #.(sb!c:backend-byte-fasl-file-type))))
114     (cond ((and (featurep :runtime)
115                 (probe-file bname))
116            (load bname))
117           (load-native
118            (load name)))))
119
120 ;;; Replace a cold-loaded native object file with a byte-compiled one, if it
121 ;;; exists.
122 #+nil ; no longer needed in SBCL.. I think.. -- WHN 19990814
123 (defun byte-load-over (name)
124   (load (make-pathname
125          :defaults name
126          :type #.(sb!c:backend-byte-fasl-file-type))
127         :if-does-not-exist nil))