0.pre7.129:
[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 (sb!alien:define-alien-routine "save" (sb!alien:boolean)
21   (file sb!c-call:c-string)
22   (initial-fun (sb!alien: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   "Saves a CMU Common Lisp core image in the file of the specified name,
36   killing the current Lisp invocation in the process (unless it bails
37   out early because of some argument error or something).
38
39   The following &KEY args are defined:
40     :TOPLEVEL
41        The function to run when the created core file is resumed.
42        The default function handles command line toplevel option
43        processing and runs the top level read-eval-print loop. This
44        function should not return.
45     :PURIFY
46        If true (the default), do a purifying GC which moves all dynamically
47        allocated objects into static space so that they stay pure. This takes
48        somewhat longer than the normal GC which is otherwise done, but it's
49        only done once, and subsequent GC's will be done less often and will
50        take less time in the resulting core file. See PURIFY.
51     :ROOT-STRUCTURES
52        This should be a list of the main entry points in any newly loaded
53        systems. This need not be supplied, but locality and/or GC performance
54        may be better if they are. Meaningless if :PURIFY is NIL. See PURIFY.
55     :ENVIRONMENT-NAME
56        This is also passed to PURIFY when :PURIFY is T. (rarely used)
57
58   The save/load process changes the values of some global variables:
59     *STANDARD-OUTPUT*, *DEBUG-IO*, etc.
60       Everything related to open streams is necessarily changed, since
61       the OS won't let us preserve a stream across save and load.
62     *DEFAULT-PATHNAME-DEFAULTS*
63       This is reinitialized to reflect the working directory where the
64       saved core is loaded."
65
66   #!+mp (sb!mp::shutdown-multi-processing)
67   ;; FIXME: What is this for? Explain.
68   (when (fboundp 'cancel-finalization)
69     (cancel-finalization sb!sys:*tty*))
70   ;; FIXME: Would it be possible to unmix the PURIFY logic from this
71   ;; function, and just do a GC :FULL T here? (Then if the user wanted
72   ;; a PURIFYed image, he'd just run PURIFY immediately before calling
73   ;; SAVE-LISP-AND-DIE.)
74   (if purify
75       (purify :root-structures root-structures
76               :environment-name environment-name)
77       #!-gencgc (gc) #!+gencgc (gc :full t))
78   ;; FIXME: Wouldn't it be more correct to go through this list backwards
79   ;; instead of forwards?
80   (dolist (f *before-save-initializations*)
81     (funcall f))
82   (flet ((restart-lisp ()
83            (handling-end-of-the-world
84              (reinit)
85              (dolist (f *after-save-initializations*)
86                (funcall f))
87              (funcall toplevel))))
88     ;; FIXME: Perhaps WITHOUT-GCING should be wrapped around the
89     ;; LET as well, to avoid the off chance of an interrupt triggering
90     ;; GC and making our saved RESTART-LISP address invalid?
91     (without-gcing
92       (save (unix-namestring core-file-name nil)
93             (get-lisp-obj-address #'restart-lisp)))))