2 * main() entry point for a stand-alone SBCL image
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
20 #ifndef LISP_FEATURE_WIN32
23 #include <sys/types.h>
24 #ifndef LISP_FEATURE_WIN32
30 #include <sys/param.h>
33 #ifndef LISP_FEATURE_WIN32
40 #if defined(SVR4) || defined(__linux__)
52 #include "interrupt.h"
61 #include "genesis/static-symbols.h"
62 #include "genesis/symbol.h"
71 #define SBCL_HOME SBCL_PREFIX"/lib/sbcl/"
74 #ifdef LISP_FEATURE_HPUX
75 extern void *return_from_lisp_stub;
76 #include "genesis/closure.h"
77 #include "genesis/simple-fun.h"
81 /* SIGINT handler that invokes the monitor (for when Lisp isn't up to it) */
83 sigint_handler(int signal, siginfo_t *info, os_context_t *context)
85 lose("\nSIGINT hit at 0x%08lX\n",
86 (unsigned long) *os_context_pc_addr(context));
89 /* (This is not static, because we want to be able to call it from
94 SHOW("entering sigint_init()");
95 install_handler(SIGINT, sigint_handler);
96 SHOW("leaving sigint_init()");
100 * helper functions for dealing with command line args
104 successful_malloc(size_t size)
106 void* result = malloc(size);
108 lose("malloc failure\n");
112 return (void *) NULL; /* dummy value: return something ... */
116 copied_string(char *string)
118 return strcpy(successful_malloc(1+strlen(string)), string);
122 copied_existing_filename_or_null(char *filename)
124 struct stat filename_stat;
125 if (stat(filename, &filename_stat)) { /* if failure */
128 return copied_string(filename);
132 #ifndef LISP_FEATURE_WIN32
134 copied_realpath(const char *pathname)
139 /* realpath() supposedly can't be counted on to always return
140 * an absolute path, so we prepend the cwd to relative paths */
142 if (pathname[0] != '/') {
143 messy = successful_malloc(PATH_MAX + 1);
144 if (getcwd(messy, PATH_MAX + 1) == NULL) {
149 snprintf(messy + len, PATH_MAX + 1 - len, "/%s", pathname);
152 tidy = successful_malloc(PATH_MAX + 1);
153 if (realpath((messy ? messy : pathname), tidy) == NULL) {
161 #endif /* LISP_FEATURE_WIN32 */
163 /* miscellaneous chattiness */
169 "Usage: sbcl [runtime-options] [toplevel-options] [user-options]\n\
170 Common runtime options:\n\
171 --help Print this message and exit.\n\
172 --version Print version information and exit.\n\
173 --core <filename> Use the specified core file instead of the default.\n\
174 --dynamic-space-size <MiB> Size of reserved dynamic space in megabytes.\n\
175 --control-stack-size <MiB> Size of reserved control stack in megabytes.\n\
177 Common toplevel options:\n\
178 --sysinit <filename> System-wide init-file to use instead of default.\n\
179 --userinit <filename> Per-user init-file to use instead of default.\n\
180 --no-sysinit Inhibit processing of any system-wide init-file.\n\
181 --no-userinit Inhibit processing of any per-user init-file.\n\
182 --disable-debugger Invoke sb-ext:disable-debugger.\n\
183 --noprint Run a Read-Eval Loop without printing results.\n\
184 --script [<filename>] Skip #! line, disable debugger, avoid verbosity.\n\
185 Common toplevel options that are processed in order:\n\
186 --eval <form> Form to eval when processing this option.\n\
187 --load <filename> File to load when processing this option.\n\
189 User options are not processed by SBCL. All runtime options must\n\
190 appear before toplevel options, and all toplevel options must\n\
191 appear before user options.\n\
193 For more information please refer to the SBCL User Manual, which\n\
194 should be installed along with SBCL, and is also available from the\n\
195 website <http://www.sbcl.org/>.\n");
201 printf("SBCL %s\n", SBCL_VERSION_STRING);
208 "This is SBCL %s, an implementation of ANSI Common Lisp.\n\
209 More information about SBCL is available at <http://www.sbcl.org/>.\n\
211 SBCL is free software, provided as is, with absolutely no warranty.\n\
212 It is mostly in the public domain; some portions are provided under\n\
213 BSD-style licenses. See the CREDITS and COPYING files in the\n\
214 distribution for more information.\n\
215 ", SBCL_VERSION_STRING);
218 /* Look for a core file to load, first in the directory named by the
219 * SBCL_HOME environment variable, then in a hardcoded default
220 * location. Returns a malloced copy of the core filename. */
224 char *sbcl_home = getenv("SBCL_HOME");
226 char *stem = "/sbcl.core";
229 if (!(sbcl_home && *sbcl_home)) sbcl_home = SBCL_HOME;
230 lookhere = (char *) calloc(strlen(sbcl_home) +
234 sprintf(lookhere, "%s%s", sbcl_home, stem);
235 core = copied_existing_filename_or_null(lookhere);
238 lose("can't find core file at %s\n", lookhere);
246 /* Try to find the path to an executable from argv[0], this is only
247 * used when os_get_runtime_executable_path() returns NULL */
248 #ifdef LISP_FEATURE_WIN32
250 search_for_executable(const char *argv0)
254 #else /* LISP_FEATURE_WIN32 */
256 search_for_executable(const char *argv0)
258 char *search, *start, *end, *buf;
260 /* If argv[0] contains a slash then it's probably an absolute path
261 * or relative to the current directory, so check if it exists. */
262 if (strchr(argv0, '/') != NULL && access(argv0, F_OK) == 0)
263 return copied_realpath(argv0);
265 /* Bail on an absolute path which doesn't exist */
269 /* Otherwise check if argv[0] exists relative to any directory in PATH */
270 search = getenv("PATH");
273 search = copied_string(search);
274 buf = successful_malloc(PATH_MAX + 1);
275 for (start = search; (end = strchr(start, ':')) != NULL; start = end + 1) {
277 snprintf(buf, PATH_MAX + 1, "%s/%s", start, argv0);
278 if (access(buf, F_OK) == 0) {
280 search = copied_realpath(buf);
290 #endif /* LISP_FEATURE_WIN32 */
295 struct runtime_options *runtime_options;
297 char *saved_runtime_path = NULL;
300 main(int argc, char *argv[], char *envp[])
302 #ifdef LISP_FEATURE_WIN32
303 /* Exception handling support structure. Evil Win32 hack. */
304 struct lisp_exception_frame exception_frame;
307 /* the name of the core file we're to execute. Note that this is
308 * a malloc'ed string which should be freed eventually. */
310 char **sbcl_argv = 0;
311 os_vm_offset_t embedded_core_offset = 0;
312 char *runtime_path = 0;
314 /* other command line options */
315 boolean noinform = 0;
316 boolean end_runtime_options = 0;
317 boolean disable_lossage_handler_p = 0;
319 lispobj initial_function;
320 const char *sbcl_home = getenv("SBCL_HOME");
323 block_blockable_signals(0, 0);
325 setlocale(LC_ALL, "");
327 runtime_options = NULL;
329 /* Save the argv[0] derived runtime path in case
330 * os_get_runtime_executable_path(1) isn't able to get an
331 * externally-usable path later on. */
332 saved_runtime_path = search_for_executable(argv[0]);
334 /* Check early to see if this executable has an embedded core,
335 * which also populates runtime_options if the core has runtime
337 runtime_path = os_get_runtime_executable_path(0);
338 if (runtime_path || saved_runtime_path) {
339 os_vm_offset_t offset = search_for_embedded_core(
340 runtime_path ? runtime_path : saved_runtime_path);
342 embedded_core_offset = offset;
343 core = (runtime_path ? runtime_path :
344 copied_string(saved_runtime_path));
352 /* Parse our part of the command line (aka "runtime options"),
353 * stripping out those options that we handle. */
354 if (runtime_options != NULL) {
355 dynamic_space_size = runtime_options->dynamic_space_size;
356 thread_control_stack_size = runtime_options->thread_control_stack_size;
361 runtime_options = successful_malloc(sizeof(struct runtime_options));
363 while (argi < argc) {
364 char *arg = argv[argi];
365 if (0 == strcmp(arg, "--script")) {
366 /* This is both a runtime and a toplevel option. As a
367 * runtime option, it is equivalent to --noinform.
368 * This exits, and does not increment argi, so that
369 * TOPLEVEL-INIT sees the option. */
371 end_runtime_options = 1;
372 disable_lossage_handler_p = 1;
373 lose_on_corruption_p = 1;
375 } else if (0 == strcmp(arg, "--noinform")) {
378 } else if (0 == strcmp(arg, "--core")) {
380 lose("more than one core file specified\n");
384 lose("missing filename for --core argument\n");
386 core = copied_string(argv[argi]);
389 } else if (0 == strcmp(arg, "--help")) {
390 /* I think this is the (or a) usual convention: upon
391 * seeing "--help" we immediately print our help
392 * string and exit, ignoring everything else. */
395 } else if (0 == strcmp(arg, "--version")) {
396 /* As in "--help" case, I think this is expected. */
399 } else if (0 == strcmp(arg, "--dynamic-space-size")) {
402 lose("missing argument for --dynamic-space-size");
405 long tmp = strtol(argv[argi++], &tail, 0);
407 lose("--dynamic-space-size argument is not a number");
409 (tmp >= (LONG_MAX >> 20))) {
410 lose("--dynamic-space-size argument is out of range");
412 dynamic_space_size = tmp << 20;
414 # ifdef MAX_DYNAMIC_SPACE_END
415 if (!((DYNAMIC_SPACE_START <
416 DYNAMIC_SPACE_START+dynamic_space_size) &&
417 (DYNAMIC_SPACE_START+dynamic_space_size <=
418 MAX_DYNAMIC_SPACE_END)))
419 lose("--dynamic-space-size argument is too large");
421 } else if (0 == strcmp(arg, "--control-stack-size")) {
424 lose("missing argument for --control-stack-size");
426 thread_control_stack_size = strtol(argv[argi++], 0, 0) << 20;
428 lose("argument to --control-stack-size is not a number");
429 } else if (0 == strcmp(arg, "--debug-environment")) {
431 printf("; Commandline arguments:\n");
433 printf("; %2d: \"%s\"\n", n, argv[n]);
437 printf(";\n; Environment:\n");
439 printf("; %2d: \"%s\"\n", n, ENVIRON[n]);
443 } else if (0 == strcmp(arg, "--disable-ldb")) {
444 disable_lossage_handler_p = 1;
446 } else if (0 == strcmp(arg, "--lose-on-corruption")) {
447 lose_on_corruption_p = 1;
449 } else if (0 == strcmp(arg, "--end-runtime-options")) {
450 end_runtime_options = 1;
454 /* This option was unrecognized as a runtime option,
455 * so it must be a toplevel option or a user option,
456 * so we must be past the end of the runtime option
461 /* This is where we strip out those options that we handle. We
462 * also take this opportunity to make sure that we don't find
463 * an out-of-place "--end-runtime-options" option. */
465 char *argi0 = argv[argi];
467 /* (argc - argi) for the arguments, one for the binary,
468 and one for the terminating NULL. */
469 sbcl_argv = successful_malloc((2 + argc - argi) * sizeof(char *));
470 sbcl_argv[0] = argv[0];
471 while (argi < argc) {
472 char *arg = argv[argi++];
473 /* If we encounter --end-runtime-options for the first
474 * time after the point where we had to give up on
475 * runtime options, then the point where we had to
476 * give up on runtime options must've been a user
478 if (!end_runtime_options &&
479 0 == strcmp(arg, "--end-runtime-options")) {
480 lose("bad runtime option \"%s\"\n", argi0);
482 sbcl_argv[argj++] = arg;
488 /* Align down to multiple of page_table page size, and to the appropriate
489 * stack alignment. */
490 dynamic_space_size &= ~(PAGE_BYTES-1);
491 #ifdef LISP_FEATURE_GENCGC
492 dynamic_space_size &= ~(GENCGC_CARD_BYTES-1);
494 thread_control_stack_size &= ~(CONTROL_STACK_ALIGNMENT_BYTES-1);
496 /* Preserve the runtime options for possible future core saving */
497 runtime_options->dynamic_space_size = dynamic_space_size;
498 runtime_options->thread_control_stack_size = thread_control_stack_size;
500 /* KLUDGE: os_vm_page_size is set by os_init(), and on some
501 * systems (e.g. Alpha) arch_init() needs need os_vm_page_size, so
502 * it must follow os_init(). -- WHN 2000-01-26 */
508 /* If no core file was specified, look for one. */
510 core = search_for_core();
513 /* Make sure that SBCL_HOME is set and not the empty string,
514 unless loading an embedded core. */
515 if (!(sbcl_home && *sbcl_home) && embedded_core_offset == 0) {
516 char *envstring, *copied_core, *dir;
517 char *stem = "SBCL_HOME=";
518 copied_core = copied_string(core);
519 dir = dirname(copied_core);
520 envstring = (char *) calloc(strlen(stem) +
524 sprintf(envstring, "%s%s", stem, dir);
529 if (!noinform && embedded_core_offset == 0) {
534 #if defined(SVR4) || defined(__linux__) || defined(__NetBSD__)
538 define_var("nil", NIL, 1);
539 define_var("t", T, 1);
541 if (!disable_lossage_handler_p)
542 enable_lossage_handler();
546 initial_function = load_core_file(core, embedded_core_offset);
547 if (initial_function == NIL) {
548 lose("couldn't find initial function\n");
550 #ifdef LISP_FEATURE_HPUX
551 /* -1 = CLOSURE_FUN_OFFSET, 23 = SIMPLE_FUN_CODE_OFFSET, we are
552 * not in LANGUAGE_ASSEMBLY so we cant reach them. */
553 return_from_lisp_stub = (void *) ((char *)*((unsigned long *)
554 ((char *)initial_function + -1)) + 23);
557 gc_initialize_pointers();
559 arch_install_interrupt_handlers();
560 #ifndef LISP_FEATURE_WIN32
561 os_install_interrupt_handlers();
563 /* wos_install_interrupt_handlers(handler); */
564 wos_install_interrupt_handlers(&exception_frame);
567 /* Pass core filename and the processed argv into Lisp. They'll
568 * need to be processed further there, to do locale conversion.
571 posix_argv = sbcl_argv;
573 FSHOW((stderr, "/funcalling initial_function=0x%lx\n",
574 (unsigned long)initial_function));
575 #ifdef LISP_FEATURE_WIN32
577 This is experimental prerelease support for the Windows platform: use\n\
578 at your own risk. \"Your Kitten of Death awaits!\"\n");
582 create_initial_thread(initial_function);
583 lose("CATS. CATS ARE NICE.\n");