Add :application-type parameter for save-lisp-and-die on Windows.
[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   (prepend-runtime int)
24   (save-runtime-options int)
25   (compressed int)
26   (compression-level int)
27   (application-type int))
28
29 #!+gencgc
30 (define-alien-routine "gc_and_save" void
31   (file c-string)
32   (prepend-runtime int)
33   (save-runtime-options int)
34   (compressed int)
35   (compression-level int)
36   (application-type int))
37
38 #!+gencgc
39 (defvar sb!vm::*restart-lisp-function*)
40
41 (defun save-lisp-and-die (core-file-name &key
42                                          (toplevel #'toplevel-init)
43                                          (executable nil)
44                                          (save-runtime-options nil)
45                                          (purify t)
46                                          (root-structures ())
47                                          (environment-name "auxiliary")
48                                          (compression nil)
49                                          #!+win32
50                                          (application-type :console))
51   #!+sb-doc
52   "Save a \"core image\", i.e. enough information to restart a Lisp
53 process later in the same state, in the file of the specified name.
54 Only global state is preserved: the stack is unwound in the process.
55
56 The following &KEY arguments are defined:
57
58   :TOPLEVEL
59      The function to run when the created core file is resumed. The
60      default function handles command line toplevel option processing
61      and runs the top level read-eval-print loop. This function returning
62      is equivalent to (SB-EXT:EXIT :CODE 0) being called.
63
64      TOPLEVEL functions should always provide an ABORT restart: otherwise
65      code they call will run without one.
66
67   :EXECUTABLE
68      If true, arrange to combine the SBCL runtime and the core image
69      to create a standalone executable.  If false (the default), the
70      core image will not be executable on its own. Executable images
71      always behave as if they were passed the --noinform runtime option.
72
73   :SAVE-RUNTIME-OPTIONS
74      If true, values of runtime options --dynamic-space-size and
75      --control-stack-size that were used to start SBCL are stored in
76      the standalone executable, and restored when the executable is
77      run. This also inhibits normal runtime option processing, causing
78      all command line arguments to be passed to the toplevel.
79      Meaningless if :EXECUTABLE is NIL.
80
81   :PURIFY
82      If true (the default on cheneygc), do a purifying GC which moves all
83      dynamically allocated objects into static space. This takes
84      somewhat longer than the normal GC which is otherwise done, but
85      it's only done once, and subsequent GC's will be done less often
86      and will take less time in the resulting core file. See the PURIFY
87      function. This parameter has no effect on platforms using the
88      generational garbage collector.
89
90   :ROOT-STRUCTURES
91      This should be a list of the main entry points in any newly loaded
92      systems. This need not be supplied, but locality and/or GC performance
93      may be better if they are. Meaningless if :PURIFY is NIL. See the
94      PURIFY function.
95
96   :ENVIRONMENT-NAME
97      This is also passed to the PURIFY function when :PURIFY is T.
98      (rarely used)
99
100   :COMPRESSION
101      This is only meaningful if the runtime was built with the :SB-CORE-COMPRESSION
102      feature enabled. If NIL (the default), saves to uncompressed core files. If
103      :SB-CORE-COMPRESSION was enabled at build-time, the argument may also be
104      an integer from -1 to 9, corresponding to zlib compression levels, or T
105      (which is equivalent to the default compression level, -1).
106
107   :APPLICATION-TYPE
108      Present only on Windows and is meaningful only with :EXECUTABLE T.
109      Specifies the subsystem of the executable, :CONSOLE or :GUI.
110      The notable difference is that :GUI doesn't automatically create a console
111      window. The default is :CONSOLE.
112
113 The save/load process changes the values of some global variables:
114
115   *STANDARD-OUTPUT*, *DEBUG-IO*, etc.
116     Everything related to open streams is necessarily changed, since
117     the OS won't let us preserve a stream across save and load.
118
119   *DEFAULT-PATHNAME-DEFAULTS*
120     This is reinitialized to reflect the working directory where the
121     saved core is loaded.
122
123 SAVE-LISP-AND-DIE interacts with SB-ALIEN:LOAD-SHARED-OBJECT: see its
124 documentation for details.
125
126 On threaded platforms only a single thread may remain running after
127 SB-EXT:*SAVE-HOOKS* have run. Applications using multiple threads can
128 be SAVE-LISP-AND-DIE friendly by registering a save-hook that quits
129 any additional threads, and an init-hook that restarts them.
130
131 This implementation is not as polished and painless as you might like:
132   * It corrupts the current Lisp image enough that the current process
133     needs to be killed afterwards. This can be worked around by forking
134     another process that saves the core.
135   * There is absolutely no binary compatibility of core images between
136     different runtime support programs. Even runtimes built from the same
137     sources at different times are treated as incompatible for this
138     purpose.
139 This isn't because we like it this way, but just because there don't
140 seem to be good quick fixes for either limitation and no one has been
141 sufficiently motivated to do lengthy fixes."
142   #!+gencgc
143   (declare (ignore purify root-structures environment-name))
144   #!+sb-core-compression
145   (check-type compression (or boolean (integer -1 9)))
146   #!-sb-core-compression
147   (when compression
148     (error "Unable to save compressed core: this runtime was not built with zlib support"))
149   (when (eql t compression)
150     (setf compression -1))
151   (tune-hashtable-sizes-of-all-packages)
152   (deinit)
153   ;; FIXME: Would it be possible to unmix the PURIFY logic from this
154   ;; function, and just do a GC :FULL T here? (Then if the user wanted
155   ;; a PURIFYed image, he'd just run PURIFY immediately before calling
156   ;; SAVE-LISP-AND-DIE.)
157   (labels ((restart-lisp ()
158              (handling-end-of-the-world
159                (reinit)
160                #!+hpux (sb!sys:%primitive sb!vm::setup-return-from-lisp-stub)
161                (funcall toplevel)))
162            (foreign-bool (value)
163              (if value 1 0))
164            (save-core (gc)
165              (let ((name (native-namestring
166                           (physicalize-pathname core-file-name)
167                           :as-file t)))
168                (when gc
169                  #!-gencgc (gc)
170                  ;; Do a destructive non-conservative GC, and then save a core.
171                  ;; A normal GC will leave huge amounts of storage unreclaimed
172                  ;; (over 50% on x86). This needs to be done by a single function
173                  ;; since the GC will invalidate the stack.
174                  #!+gencgc (gc-and-save name
175                                         (foreign-bool executable)
176                                         (foreign-bool save-runtime-options)
177                                         (foreign-bool compression)
178                                         (or compression 0)
179                                         #!+win32
180                                         (ecase application-type
181                                           (:console 0)
182                                           (:gui 1))
183                                         #!-win32 0))
184                (without-gcing
185                  (save name
186                        (get-lisp-obj-address #'restart-lisp)
187                        (foreign-bool executable)
188                        (foreign-bool save-runtime-options)
189                        (foreign-bool compression)
190                        (or compression 0)
191                        #!+win32
192                        (ecase application-type
193                          (:console 0)
194                          (:gui 1))
195                        #!-win32 0)))))
196     ;; Save the restart function into a static symbol, to allow GC-AND-SAVE
197     ;; access to it even after the GC has moved it.
198     #!+gencgc
199     (setf sb!vm::*restart-lisp-function* #'restart-lisp)
200     (cond #!-gencgc
201           (purify
202            (purify :root-structures root-structures
203                    :environment-name environment-name)
204            (save-core nil))
205           (t
206            ;; Compact the environment even though we're skipping the
207            ;; other purification stages.
208            (sb!kernel::compact-environment-aux "Auxiliary" 200)
209            (save-core t)))
210     ;; Something went very wrong -- reinitialize to have a prayer
211     ;; of being able to report the error.
212     (reinit)
213     (error "Could not save core.")))
214
215 (defun deinit ()
216   (call-hooks "save" *save-hooks*)
217   #!+sb-wtimer
218   (itimer-emulation-deinit)
219   (when (rest (sb!thread:list-all-threads))
220     (error "Cannot save core with multiple threads running."))
221   (float-deinit)
222   (profile-deinit)
223   (foreign-deinit)
224   (stream-deinit)
225   (deinit-finalizers)
226   (drop-all-hash-caches))