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.
21 #ifndef LISP_FEATURE_WIN32
24 #include <sys/types.h>
25 #ifndef LISP_FEATURE_WIN32
31 #include <sys/param.h>
34 #ifndef LISP_FEATURE_WIN32
41 #if defined(SVR4) || defined(__linux__)
53 #include "interrupt.h"
62 #include "genesis/static-symbols.h"
63 #include "genesis/symbol.h"
72 #define SBCL_HOME SBCL_PREFIX"/lib/sbcl/"
75 #ifdef LISP_FEATURE_HPUX
76 extern void *return_from_lisp_stub;
77 #include "genesis/closure.h"
78 #include "genesis/simple-fun.h"
82 /* SIGINT handler that invokes the monitor (for when Lisp isn't up to it) */
84 sigint_handler(int signal, siginfo_t *info, os_context_t *context)
86 lose("\nSIGINT hit at 0x%08lX\n",
87 (unsigned long) *os_context_pc_addr(context));
90 /* (This is not static, because we want to be able to call it from
95 SHOW("entering sigint_init()");
96 install_handler(SIGINT, sigint_handler);
97 SHOW("leaving sigint_init()");
101 * helper functions for dealing with command line args
105 successful_malloc(size_t size)
107 void* result = malloc(size);
109 lose("malloc failure\n");
113 return (void *) NULL; /* dummy value: return something ... */
117 copied_string(char *string)
119 return strcpy(successful_malloc(1+strlen(string)), string);
123 copied_existing_filename_or_null(char *filename)
125 struct stat filename_stat;
126 if (stat(filename, &filename_stat)) { /* if failure */
129 return copied_string(filename);
133 #ifndef LISP_FEATURE_WIN32
135 copied_realpath(const char *pathname)
140 /* realpath() supposedly can't be counted on to always return
141 * an absolute path, so we prepend the cwd to relative paths */
143 if (pathname[0] != '/') {
144 messy = successful_malloc(PATH_MAX + 1);
145 if (getcwd(messy, PATH_MAX + 1) == NULL) {
150 snprintf(messy + len, PATH_MAX + 1 - len, "/%s", pathname);
153 tidy = successful_malloc(PATH_MAX + 1);
154 if (realpath((messy ? messy : pathname), tidy) == NULL) {
162 #endif /* LISP_FEATURE_WIN32 */
164 /* miscellaneous chattiness */
170 "Usage: sbcl [runtime-options] [toplevel-options] [user-options]\n\
171 Common runtime options:\n\
172 --help Print this message and exit.\n\
173 --version Print version information and exit.\n\
174 --core <filename> Use the specified core file instead of the default.\n\
175 --dynamic-space-size <MiB> Size of reserved dynamic space in megabytes.\n\
176 --control-stack-size <MiB> Size of reserved control stack in megabytes.\n\
178 Common toplevel options:\n\
179 --sysinit <filename> System-wide init-file to use instead of default.\n\
180 --userinit <filename> Per-user init-file to use instead of default.\n\
181 --no-sysinit Inhibit processing of any system-wide init-file.\n\
182 --no-userinit Inhibit processing of any per-user init-file.\n\
183 --disable-debugger Invoke sb-ext:disable-debugger.\n\
184 --noprint Run a Read-Eval Loop without printing results.\n\
185 --script [<filename>] Skip #! line, disable debugger, avoid verbosity.\n\
186 --quit Exit with code 0 after option processing.\n\
187 --non-interactive Sets both --quit and --disable-debugger.\n\
188 Common toplevel options that are processed in order:\n\
189 --eval <form> Form to eval when processing this option.\n\
190 --load <filename> File to load when processing this option.\n\
192 User options are not processed by SBCL. All runtime options must\n\
193 appear before toplevel options, and all toplevel options must\n\
194 appear before user options.\n\
196 For more information please refer to the SBCL User Manual, which\n\
197 should be installed along with SBCL, and is also available from the\n\
198 website <http://www.sbcl.org/>.\n");
204 printf("SBCL %s\n", SBCL_VERSION_STRING);
211 "This is SBCL %s, an implementation of ANSI Common Lisp.\n\
212 More information about SBCL is available at <http://www.sbcl.org/>.\n\
214 SBCL is free software, provided as is, with absolutely no warranty.\n\
215 It is mostly in the public domain; some portions are provided under\n\
216 BSD-style licenses. See the CREDITS and COPYING files in the\n\
217 distribution for more information.\n\
218 ", SBCL_VERSION_STRING);
221 /* Look for a core file to load, first in the directory named by the
222 * SBCL_HOME environment variable, then in a hardcoded default
223 * location. Returns a malloced copy of the core filename. */
227 char *sbcl_home = getenv("SBCL_HOME");
229 char *stem = "/sbcl.core";
232 if (!(sbcl_home && *sbcl_home)) sbcl_home = SBCL_HOME;
233 lookhere = (char *) calloc(strlen(sbcl_home) +
237 sprintf(lookhere, "%s%s", sbcl_home, stem);
238 core = copied_existing_filename_or_null(lookhere);
241 lose("can't find core file at %s\n", lookhere);
249 /* Try to find the path to an executable from argv[0], this is only
250 * used when os_get_runtime_executable_path() returns NULL */
251 #ifdef LISP_FEATURE_WIN32
253 search_for_executable(const char *argv0)
257 #else /* LISP_FEATURE_WIN32 */
259 search_for_executable(const char *argv0)
261 char *search, *start, *end, *buf;
263 /* If argv[0] contains a slash then it's probably an absolute path
264 * or relative to the current directory, so check if it exists. */
265 if (strchr(argv0, '/') != NULL && access(argv0, F_OK) == 0)
266 return copied_realpath(argv0);
268 /* Bail on an absolute path which doesn't exist */
272 /* Otherwise check if argv[0] exists relative to any directory in PATH */
273 search = getenv("PATH");
276 search = copied_string(search);
277 buf = successful_malloc(PATH_MAX + 1);
278 for (start = search; (end = strchr(start, ':')) != NULL; start = end + 1) {
280 snprintf(buf, PATH_MAX + 1, "%s/%s", start, argv0);
281 if (access(buf, F_OK) == 0) {
283 search = copied_realpath(buf);
293 #endif /* LISP_FEATURE_WIN32 */
295 unsigned long parse_size_arg(char *arg, char *arg_name)
297 char *tail, *power_name;
298 unsigned long power, res;
300 res = strtoul(arg, &tail, 0);
303 lose("%s argument is not a number: %s", arg_name, arg);
304 } else if (tail[0]) {
306 power_name = copied_string(tail);
307 size = strlen(power_name);
308 for (i=0; i<size; i++)
309 power_name[i] = toupper(power_name[i]);
315 if ((0==strcmp("KB", power_name)) ||
316 (0==strcmp("KIB", power_name))) {
318 } else if ((0==strcmp("MB", power_name)) ||
319 (0==strcmp("MIB", power_name))) {
321 } else if ((0==strcmp("GB", power_name)) ||
322 (0==strcmp("GIB", power_name))) {
325 lose("%s argument has an unknown suffix: %s", arg_name, tail);
330 (res > (ULONG_MAX >> power))) {
331 lose("%s argument is out of range: %s", arg_name, arg);
340 struct runtime_options *runtime_options;
342 char *saved_runtime_path = NULL;
345 main(int argc, char *argv[], char *envp[])
347 #ifdef LISP_FEATURE_WIN32
348 /* Exception handling support structure. Evil Win32 hack. */
349 struct lisp_exception_frame exception_frame;
352 /* the name of the core file we're to execute. Note that this is
353 * a malloc'ed string which should be freed eventually. */
355 char **sbcl_argv = 0;
356 os_vm_offset_t embedded_core_offset = 0;
357 char *runtime_path = 0;
359 /* other command line options */
360 boolean noinform = 0;
361 boolean end_runtime_options = 0;
362 boolean disable_lossage_handler_p = 0;
364 lispobj initial_function;
365 const char *sbcl_home = getenv("SBCL_HOME");
368 block_blockable_signals(0, 0);
370 setlocale(LC_ALL, "");
372 runtime_options = NULL;
374 /* Save the argv[0] derived runtime path in case
375 * os_get_runtime_executable_path(1) isn't able to get an
376 * externally-usable path later on. */
377 saved_runtime_path = search_for_executable(argv[0]);
379 /* Check early to see if this executable has an embedded core,
380 * which also populates runtime_options if the core has runtime
382 runtime_path = os_get_runtime_executable_path(0);
383 if (runtime_path || saved_runtime_path) {
384 os_vm_offset_t offset = search_for_embedded_core(
385 runtime_path ? runtime_path : saved_runtime_path);
387 embedded_core_offset = offset;
388 core = (runtime_path ? runtime_path :
389 copied_string(saved_runtime_path));
397 /* Parse our part of the command line (aka "runtime options"),
398 * stripping out those options that we handle. */
399 if (runtime_options != NULL) {
400 dynamic_space_size = runtime_options->dynamic_space_size;
401 thread_control_stack_size = runtime_options->thread_control_stack_size;
406 runtime_options = successful_malloc(sizeof(struct runtime_options));
408 while (argi < argc) {
409 char *arg = argv[argi];
410 if (0 == strcmp(arg, "--script")) {
411 /* This is both a runtime and a toplevel option. As a
412 * runtime option, it is equivalent to --noinform.
413 * This exits, and does not increment argi, so that
414 * TOPLEVEL-INIT sees the option. */
416 end_runtime_options = 1;
417 disable_lossage_handler_p = 1;
418 lose_on_corruption_p = 1;
420 } else if (0 == strcmp(arg, "--noinform")) {
423 } else if (0 == strcmp(arg, "--core")) {
425 lose("more than one core file specified\n");
429 lose("missing filename for --core argument\n");
431 core = copied_string(argv[argi]);
434 } else if (0 == strcmp(arg, "--help")) {
435 /* I think this is the (or a) usual convention: upon
436 * seeing "--help" we immediately print our help
437 * string and exit, ignoring everything else. */
440 } else if (0 == strcmp(arg, "--version")) {
441 /* As in "--help" case, I think this is expected. */
444 } else if (0 == strcmp(arg, "--dynamic-space-size")) {
447 lose("missing argument for --dynamic-space-size");
448 dynamic_space_size = parse_size_arg(argv[argi++], "--dynamic-space-size");
449 # ifdef MAX_DYNAMIC_SPACE_END
450 if (!((DYNAMIC_SPACE_START <
451 DYNAMIC_SPACE_START+dynamic_space_size) &&
452 (DYNAMIC_SPACE_START+dynamic_space_size <=
453 MAX_DYNAMIC_SPACE_END)))
454 lose("--dynamic-space-size argument %s is too large, max %lu",
455 argv[argi-1], MAX_DYNAMIC_SPACE_END-DYNAMIC_SPACE_START);
457 } else if (0 == strcmp(arg, "--control-stack-size")) {
460 lose("missing argument for --control-stack-size");
462 thread_control_stack_size = parse_size_arg(argv[argi++], "--control-stack-size");
463 } else if (0 == strcmp(arg, "--debug-environment")) {
465 printf("; Commandline arguments:\n");
467 printf("; %2d: \"%s\"\n", n, argv[n]);
471 printf(";\n; Environment:\n");
473 printf("; %2d: \"%s\"\n", n, ENVIRON[n]);
477 } else if (0 == strcmp(arg, "--disable-ldb")) {
478 disable_lossage_handler_p = 1;
480 } else if (0 == strcmp(arg, "--lose-on-corruption")) {
481 lose_on_corruption_p = 1;
483 } else if (0 == strcmp(arg, "--end-runtime-options")) {
484 end_runtime_options = 1;
487 } else if (0 == strcmp(arg, "--merge-core-pages")) {
489 merge_core_pages = 1;
490 } else if (0 == strcmp(arg, "--no-merge-core-pages")) {
492 merge_core_pages = 0;
493 } else if (0 == strcmp(arg, "--default-merge-core-pages")) {
495 merge_core_pages = -1;
497 /* This option was unrecognized as a runtime option,
498 * so it must be a toplevel option or a user option,
499 * so we must be past the end of the runtime option
504 /* This is where we strip out those options that we handle. We
505 * also take this opportunity to make sure that we don't find
506 * an out-of-place "--end-runtime-options" option. */
508 char *argi0 = argv[argi];
510 /* (argc - argi) for the arguments, one for the binary,
511 and one for the terminating NULL. */
512 sbcl_argv = successful_malloc((2 + argc - argi) * sizeof(char *));
513 sbcl_argv[0] = argv[0];
514 while (argi < argc) {
515 char *arg = argv[argi++];
516 /* If we encounter --end-runtime-options for the first
517 * time after the point where we had to give up on
518 * runtime options, then the point where we had to
519 * give up on runtime options must've been a user
521 if (!end_runtime_options &&
522 0 == strcmp(arg, "--end-runtime-options")) {
523 lose("bad runtime option \"%s\"\n", argi0);
525 sbcl_argv[argj++] = arg;
531 /* Align down to multiple of page_table page size, and to the appropriate
532 * stack alignment. */
533 dynamic_space_size &= ~(PAGE_BYTES-1);
534 #ifdef LISP_FEATURE_GENCGC
535 dynamic_space_size &= ~(GENCGC_CARD_BYTES-1);
537 thread_control_stack_size &= ~(CONTROL_STACK_ALIGNMENT_BYTES-1);
539 /* Preserve the runtime options for possible future core saving */
540 runtime_options->dynamic_space_size = dynamic_space_size;
541 runtime_options->thread_control_stack_size = thread_control_stack_size;
543 /* KLUDGE: os_vm_page_size is set by os_init(), and on some
544 * systems (e.g. Alpha) arch_init() needs need os_vm_page_size, so
545 * it must follow os_init(). -- WHN 2000-01-26 */
551 /* If no core file was specified, look for one. */
553 core = search_for_core();
556 /* Make sure that SBCL_HOME is set and not the empty string,
557 unless loading an embedded core. */
558 if (!(sbcl_home && *sbcl_home) && embedded_core_offset == 0) {
559 char *envstring, *copied_core, *dir;
560 char *stem = "SBCL_HOME=";
561 copied_core = copied_string(core);
562 dir = dirname(copied_core);
563 envstring = (char *) calloc(strlen(stem) +
567 sprintf(envstring, "%s%s", stem, dir);
572 if (!noinform && embedded_core_offset == 0) {
577 if (embedded_core_offset == 0) {
578 /* Here we make a last attempt at recognizing an embedded core,
579 * so that a file with an embedded core is a valid argument to
580 * --core. We take care that any decisions on special behaviour
581 * (suppressed banner, embedded options) have already been made
582 * before we reach this block, so that there is no observable
583 * difference between "embedded" and "bare" images given to
585 os_vm_offset_t offset = search_for_embedded_core(core);
587 embedded_core_offset = offset;
590 #if defined(SVR4) || defined(__linux__) || defined(__NetBSD__)
594 define_var("nil", NIL, 1);
595 define_var("t", T, 1);
597 if (!disable_lossage_handler_p)
598 enable_lossage_handler();
602 initial_function = load_core_file(core, embedded_core_offset);
603 if (initial_function == NIL) {
604 lose("couldn't find initial function\n");
606 #ifdef LISP_FEATURE_HPUX
607 /* -1 = CLOSURE_FUN_OFFSET, 23 = SIMPLE_FUN_CODE_OFFSET, we are
608 * not in LANGUAGE_ASSEMBLY so we cant reach them. */
609 return_from_lisp_stub = (void *) ((char *)*((unsigned long *)
610 ((char *)initial_function + -1)) + 23);
613 gc_initialize_pointers();
615 arch_install_interrupt_handlers();
616 #ifndef LISP_FEATURE_WIN32
617 os_install_interrupt_handlers();
619 /* wos_install_interrupt_handlers(handler); */
620 wos_install_interrupt_handlers(&exception_frame);
623 /* Pass core filename and the processed argv into Lisp. They'll
624 * need to be processed further there, to do locale conversion.
627 posix_argv = sbcl_argv;
629 FSHOW((stderr, "/funcalling initial_function=0x%lx\n",
630 (unsigned long)initial_function));
631 #ifdef LISP_FEATURE_WIN32
633 This is experimental prerelease support for the Windows platform: use\n\
634 at your own risk. \"Your Kitten of Death awaits!\"\n");
638 create_initial_thread(initial_function);
639 lose("CATS. CATS ARE NICE.\n");