/[cmucl]/src/code/save.lisp
ViewVC logotype

Contents of /src/code/save.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.72 - (show annotations)
Wed Oct 13 18:00:44 2010 UTC (3 years, 6 months ago) by rtoy
Branch: MAIN
CVS Tags: snapshot-2010-11
Changes since 1.71: +3 -3 lines
Some changes to replace calls to gettext with _"" or _N"" for things
compiled with and without Unicode.  This is needed so that the pot
files have the same content for both unicode and non-unicode builds.
(The _"" and _N"" are handled by the reader, so things that are
conditionalized out still get processed, unlike using gettext.)
1 ;;; -*- Mode: Lisp; Package: Lisp; Log: code.log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/save.lisp,v 1.72 2010/10/13 18:00:44 rtoy Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Dump the current lisp image into a core file. All the real work is done
13 ;;; be C. Also contains various high-level initialization stuff: loading init
14 ;;; files and parsing environment variables.
15 ;;;
16 ;;; Written by William Lott.
17 ;;;
18 ;;;
19 (in-package "LISP")
20
21 (in-package "EXTENSIONS")
22 (intl:textdomain "cmucl")
23
24 (export '(print-herald *herald-items* save-lisp *before-save-initializations*
25 *after-save-initializations* *environment-list* *editor-lisp-p*))
26 (in-package "LISP")
27
28 (defvar *before-save-initializations* nil
29 "This is a list of functions which are called before creating a saved core
30 image. These functions are executed in the child process which has no ports,
31 so they cannot do anything that tries to talk to the outside world.")
32
33 (defvar *after-save-initializations* nil
34 "This is a list of functions which are called when a saved core image starts
35 up. The system itself should be initialized at this point, but applications
36 might not be.")
37
38 (defvar *environment-list* nil
39 "An alist mapping environment variables (as keywords) to either values")
40
41 (defvar *environment-list-initialized* nil
42 "Non-NIL if environment-init has been called")
43
44 (defvar *editor-lisp-p* nil
45 "This is true if and only if the lisp was started with the -edit switch.")
46
47
48
49 ;;; Filled in by the startup code.
50 (defvar lisp-environment-list)
51 (defvar *cmucl-lib*) ; Essentially the envvar CMUCLLIB, if available
52
53 #+:executable
54 ;; A dumped executable image won't have any reasonable default
55 ;; library: search list. Save the one from when it was dumped.
56 (defvar *old-cmucl-library-search-list*)
57
58 (defvar *cmucl-core-path*) ; Path to where the Lisp core file was found.
59
60 ;;; Filled in by the save code.
61 (defvar *cmucl-core-dump-time*) ; Time the core was dumped
62 (defvar *cmucl-core-dump-host*) ; machine-instance the core was dumped on
63
64
65 ;;; PARSE-UNIX-SEARCH-LIST -- Internal
66 ;;;
67 ;;; Returns a list of the directories that are in the specified Unix
68 ;;; environment variable. Return NIL if the variable is undefined.
69 ;;;
70 (defun parse-unix-search-path (path)
71 (do* ((i 0 (1+ p))
72 (p (position #\: path :start i)
73 (position #\: path :start i))
74 (pl ()))
75 ((null p)
76 (let ((s (subseq path i)))
77 (if (string= s "")
78 (push "default:" pl)
79 (push (concatenate 'simple-string s "/") pl)))
80 (nreverse pl))
81 (let ((s (subseq path i p)))
82 (if (string= s "")
83 (push "default:" pl)
84 (push (concatenate 'simple-string s "/") pl)))))
85
86 (defun parse-unix-search-list (var)
87 (let ((path (cdr (assoc var ext::*environment-list*))))
88 (when path
89 (parse-unix-search-path path))))
90
91
92 ;;; ENVIRONMENT-INIT -- Internal
93 ;;;
94 ;;; Parse the LISP-ENVIRONMENT-LIST into a keyword alist. Set up default
95 ;;; search lists.
96 ;;;
97 (defun environment-init ()
98 (setq *environment-list* ())
99 (dolist (ele lisp-environment-list)
100 (let ((=pos (position #\= (the simple-string ele))))
101 (when =pos
102 (push (cons (intern (subseq ele 0 =pos)
103 *keyword-package*)
104 (subseq ele (1+ =pos)))
105 *environment-list*))))
106 (setf (search-list "default:") (list (default-directory)))
107 (setf (search-list "path:") (parse-unix-search-list :path))
108 (setf (search-list "home:")
109 (or (parse-unix-search-list :home)
110 (list (default-directory))))
111
112 (setf (search-list "library:")
113 (if (and (boundp '*cmucl-lib*)
114 #+:executable
115 (not (boundp '*old-cmucl-library-search-list*)))
116 (parse-unix-search-path *cmucl-lib*)
117 (or
118 #+:executable
119 *old-cmucl-library-search-list*
120 '("/usr/local/lib/cmucl/lib/"))))
121 (setf (search-list "modules:")
122 '("library:contrib/" "library:subsystems/" "target:contrib/"))
123 (setf (search-list "ld-library-path:")
124 (parse-unix-search-list :ld_library_path))
125 (setf (search-list "ext-formats:")
126 '("library:ext-formats/"
127 "target:i18n/"
128 "target:pcl/simple-streams/external-formats/"))
129 (setq *environment-list-initialized* t))
130
131
132 ;;;; SAVE-LISP itself.
133
134 (alien:def-alien-routine "save" (alien:boolean)
135 (file c-call:c-string)
136 (initial-function (alien:unsigned #.vm:word-bits))
137 (sse2-mode c-call:int))
138
139 #+:executable
140 (alien:def-alien-routine "save_executable" (alien:boolean)
141 (file c-call:c-string)
142 (initial-function (alien:unsigned #.vm:word-bits)))
143
144 (defun save-lisp (core-file-name &key
145 (purify t)
146 (root-structures ())
147 (environment-name "Auxiliary")
148 (init-function #'%top-level)
149 (load-init-file t)
150 (site-init "library:site-init")
151 (print-herald t)
152 (process-command-line t)
153 #+:executable
154 (executable nil)
155 (batch-mode nil))
156 "Saves a CMU Common Lisp core image in the file of the specified name. The
157 following keywords are defined:
158
159 :purify
160 If true (the default), do a purifying GC which moves all dynamically
161 allocated objects into static space so that they stay pure. This takes
162 somewhat longer than the normal GC which is otherwise done, but GC's will
163 be done less often and take less time in the resulting core file. See
164 EXT:PURIFY.
165
166 :root-structures
167 This should be a list of the main entry points in any newly loaded
168 systems. This need not be supplied, but locality and/or GC performance
169 will be better if they are. Meaningless if :purify is NIL. See EXT:PURIFY.
170
171 :environment-name
172 Also passed to EXT:PURIFY when :PURIFY is T. Rarely used.
173
174 :init-function
175 This is the function that starts running when the created core file is
176 resumed. The default function simply invokes the top level
177 read-eval-print loop. If the function returns the lisp will exit.
178
179 :load-init-file
180 If true, then look for an init.lisp or init.fasl file when the core
181 file is resumed.
182
183 :site-init
184 If true, then the name of the site init file to load. The default is
185 library:site-init. No error if this does not exist.
186
187 :print-herald
188 If true (the default), print out the lisp system herald when starting.
189
190 :process-command-line
191 If true (the default), process command-line switches via the normal
192 mechanisms, otherwise ignore all switches (except those processed by the
193 C startup code).
194
195 :executable
196 If nil (the default), save-lisp will save using the traditional
197 core-file format. If true, save-lisp will create an executable
198 file that contains the lisp image built in.
199 (Not all architectures support this yet.)
200
201 :batch-mode
202 If nil (the default), then the presence of the -batch command-line
203 switch will invoke batch-mode processing. If true, the produced core
204 will always be in batch-mode, regardless of any command-line switches."
205
206 (unless (probe-file (directory-namestring core-file-name))
207 (error 'simple-file-error
208 :format-control (intl:gettext "Directory ~S does not exist")
209 :format-arguments (list (directory-namestring core-file-name))))
210
211 #+mp (mp::shutdown-multi-processing)
212 (when (fboundp 'eval:flush-interpreted-function-cache)
213 (eval:flush-interpreted-function-cache))
214 (when (fboundp 'cancel-finalization)
215 (cancel-finalization sys:*tty*))
216
217 #+:executable
218 (when executable
219 ;; Only do this when dumping an executable Lisp. Otherwise
220 ;; worldload will make us lose because it clears the search lists.
221 ;; If we are dumping an executable lisp image, we want to keep
222 ;; track of the library search list across dumps because the
223 ;; normal way for figuring out the library paths from arg[0] is
224 ;; almost guaranteed to be wrong for executables.
225 (setf *old-cmucl-library-search-list* (search-list "library:")))
226
227 (if purify
228 (purify :root-structures root-structures
229 :environment-name environment-name)
230 #-gencgc (gc) #+gencgc (gc :full t))
231 (dolist (f *before-save-initializations*) (funcall f))
232 (setq ext:*batch-mode* (if batch-mode t nil))
233 (labels
234 ((%restart-lisp ()
235 (with-simple-restart (abort (intl:gettext "Skip remaining initializations."))
236 (catch 'top-level-catcher
237 (reinit)
238 (environment-init)
239 (dolist (f *after-save-initializations*) (funcall f))
240 (intl::setlocale)
241 (when process-command-line
242 (ext::process-command-strings))
243 (setf *editor-lisp-p* nil)
244 (macrolet ((find-switch (name)
245 `(find ,name *command-line-switches*
246 :key #'cmd-switch-name
247 :test #'(lambda (x y)
248 (declare (simple-string x y))
249 (string-equal x y)))))
250 (when (and process-command-line (find-switch "quiet"))
251 (setq *load-verbose* nil
252 *compile-verbose* nil
253 *compile-print* nil
254 *compile-progress* nil
255 *require-verbose* nil
256 *gc-verbose* nil
257 *herald-items* nil))
258 (when (and process-command-line
259 (or (find-switch "help")
260 (find-switch "-help")))
261 ;; Don't load any init files if -help or --help is given
262 (setf site-init nil)
263 (setf load-init-file nil))
264 (when (and site-init
265 (not (and process-command-line
266 (find-switch "nositeinit"))))
267 (load site-init :if-does-not-exist nil :verbose nil))
268 (when (and process-command-line (find-switch "edit"))
269 (setf *editor-lisp-p* t))
270 (when (and load-init-file
271 (not (and process-command-line
272 (find-switch "noinit"))))
273 (let* ((cl-switch (find-switch "init"))
274 (name (and cl-switch
275 (or (cmd-switch-value cl-switch)
276 (car (cmd-switch-words cl-switch))))))
277 (if name
278 (load (merge-pathnames name #p"home:")
279 :if-does-not-exist nil)
280 (or (load "home:init" :if-does-not-exist nil)
281 (load "home:.cmucl-init"
282 :if-does-not-exist nil))))))
283 (when process-command-line
284 (ext::invoke-switch-demons *command-line-switches*
285 *command-switch-demons*))
286 (when print-herald
287 (print-herald))))
288 (funcall init-function))
289 (restart-lisp ()
290 (unix:unix-exit
291 (catch '%end-of-the-world
292 (unwind-protect
293 (if *batch-mode*
294 (handler-case
295 (%restart-lisp)
296 (error (cond)
297 (format *error-output* (intl:gettext "Error in batch processing:~%~A~%")
298 cond)
299 (throw '%end-of-the-world 1)))
300 (%restart-lisp))
301 (finish-standard-output-streams))))))
302
303 ;; Record dump time and host
304 (setq *cmucl-core-dump-time* (get-universal-time))
305 (setq *cmucl-core-dump-host* (machine-instance))
306
307 (let ((initial-function (get-lisp-obj-address #'restart-lisp))
308 (core-name (unix-namestring core-file-name nil)))
309 (without-gcing
310 #+:executable
311 (if executable
312 (save-executable core-name initial-function)
313 (save core-name initial-function #+sse2 1 #-sse2 0))
314 #-:executable
315 (save core-name initial-function #+sse2 1 #-sse2 0))))
316 nil)
317
318
319
320 ;;;; PRINT-HERALD support.
321
322 (defvar *herald-items* ()
323 "Determines what PRINT-HERALD prints (the system startup banner.) This is a
324 database which can be augmented by each loaded system. The format is a
325 property list which maps from subsystem names to the banner information for
326 that system. This list can be manipulated with GETF -- entries are printed
327 in, reverse order, so the newest entry is printed last. Usually the system
328 feature keyword is used as the system name. A given banner is a list of
329 strings and functions (or function names). Strings are printed, and
330 functions are called with an output stream argument.")
331
332 (setf (getf *herald-items* :common-lisp)
333 `("CMU Common Lisp "
334 ,#'(lambda (stream)
335 (write-string (lisp-implementation-version) stream))
336 ,#'(lambda (stream)
337 (write-string (intl:gettext ", running on ") stream))
338 ,#'(lambda (stream) (write-string (machine-instance) stream))
339 terpri
340 ,#'(lambda (stream)
341 (let ((core (if (boundp '*cmucl-core-path*)
342 (truename *cmucl-core-path*)
343 nil))
344 (dump-time (if (boundp '*cmucl-core-dump-time*)
345 *cmucl-core-dump-time*
346 nil)))
347 (when core
348 (write-string (intl:gettext "With core: ") stream)
349 (write-line (namestring core) stream))
350 (when dump-time
351 (write-string (intl:gettext "Dumped on: ") stream)
352 (ext:format-universal-time stream dump-time :style :iso8601)
353 (write-string (intl:gettext " on ") stream)
354 (write-line *cmucl-core-dump-host* stream))))
355 ))
356
357 (setf (getf *herald-items* :bugs)
358 `(,#'(lambda (stream)
359 (write-string (intl:gettext "See <http://www.cons.org/cmucl/> for support information.") stream))
360 terpri
361 ,#'(lambda (stream)
362 (write-string (intl:gettext "Loaded subsystems:") stream))))
363
364 #+unicode
365 (setf (getf *herald-items* :unicode)
366 `(,#'(lambda (stream)
367 (write-string _" Unicode " stream))
368 ,(if (and (boundp 'lisp::*unidata-version*)
369 (>= (length lisp::*unidata-version*) 11))
370 (subseq lisp::*unidata-version* 11
371 (1- (length lisp::*unidata-version*)))
372 " ")
373 ,#'(lambda (stream)
374 (write-string _"with Unicode version " stream))
375 ,#'(lambda (stream)
376 (princ lisp::+unicode-major-version+ stream)
377 (write-char #\. stream)
378 (princ lisp::+unicode-minor-version+ stream)
379 (write-char #\. stream)
380 (princ lisp::+unicode-update-version+ stream))
381 terpri))
382
383 ;;; PRINT-HERALD -- Public
384 ;;;
385 (defun print-herald (&optional (stream *standard-output*))
386 "Print some descriptive information about the Lisp system version and
387 configuration."
388 (let ((res ()))
389 (do ((item *herald-items* (cddr item)))
390 ((null item))
391 (push (second item) res))
392
393 (fresh-line stream)
394 (dolist (item res)
395 (dolist (thing item)
396 (typecase thing
397 (string
398 (write-string thing stream))
399 (function (funcall thing stream))
400 ((or symbol cons)
401 (funcall (fdefinition thing) stream))
402 (t
403 (error (intl:gettext "Unrecognized *HERALD-ITEMS* entry: ~S.") thing))))
404 (fresh-line stream)))
405
406 (values))
407
408
409 ;;;; Random functions used by worldload.
410
411 (defun assert-user-package ()
412 (unless (eq *package* (find-package "CL-USER"))
413 (error (intl:gettext "Change *PACKAGE* to the USER package and try again."))))
414
415 ;;; MAYBE-BYTE-LOAD -- Interface
416 ;;;
417 ;;; If Name has been byte-compiled, and :runtime is a feature, then load the
418 ;;; byte-compiled version, otherwise just do normal load.
419 ;;;
420 (defun maybe-byte-load (name &optional (load-native t))
421 (let ((bname (make-pathname
422 :defaults name
423 :type #.(c:backend-byte-fasl-file-type c:*target-backend*))))
424 (cond ((and (featurep :runtime)
425 (probe-file bname))
426 (load bname))
427 (load-native
428 (load name)))))
429
430
431 ;;; BYTE-LOAD-OVER -- Interface
432 ;;;
433 ;;; Replace a cold-loaded native object file with a byte-compiled one, if it
434 ;;; exists.
435 ;;;
436 (defun byte-load-over (name)
437 (load (make-pathname
438 :defaults name
439 :type #.(c:backend-byte-fasl-file-type c:*target-backend*))
440 :if-does-not-exist nil))

  ViewVC Help
Powered by ViewVC 1.1.5