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 --quit Exit with code 0 after option processing.\n\
186 --non-interactive Sets both --quit and --disable-debugger.\n\
187 Common toplevel options that are processed in order:\n\
188 --eval <form> Form to eval when processing this option.\n\
189 --load <filename> File to load when processing this option.\n\
191 User options are not processed by SBCL. All runtime options must\n\
192 appear before toplevel options, and all toplevel options must\n\
193 appear before user options.\n\
195 For more information please refer to the SBCL User Manual, which\n\
196 should be installed along with SBCL, and is also available from the\n\
197 website <http://www.sbcl.org/>.\n");
203 printf("SBCL %s\n", SBCL_VERSION_STRING);
210 "This is SBCL %s, an implementation of ANSI Common Lisp.\n\
211 More information about SBCL is available at <http://www.sbcl.org/>.\n\
213 SBCL is free software, provided as is, with absolutely no warranty.\n\
214 It is mostly in the public domain; some portions are provided under\n\
215 BSD-style licenses. See the CREDITS and COPYING files in the\n\
216 distribution for more information.\n\
217 ", SBCL_VERSION_STRING);
220 /* Look for a core file to load, first in the directory named by the
221 * SBCL_HOME environment variable, then in a hardcoded default
222 * location. Returns a malloced copy of the core filename. */
226 char *sbcl_home = getenv("SBCL_HOME");
228 char *stem = "/sbcl.core";
231 if (!(sbcl_home && *sbcl_home)) sbcl_home = SBCL_HOME;
232 lookhere = (char *) calloc(strlen(sbcl_home) +
236 sprintf(lookhere, "%s%s", sbcl_home, stem);
237 core = copied_existing_filename_or_null(lookhere);
240 lose("can't find core file at %s\n", lookhere);
248 /* Try to find the path to an executable from argv[0], this is only
249 * used when os_get_runtime_executable_path() returns NULL */
250 #ifdef LISP_FEATURE_WIN32
252 search_for_executable(const char *argv0)
256 #else /* LISP_FEATURE_WIN32 */
258 search_for_executable(const char *argv0)
260 char *search, *start, *end, *buf;
262 /* If argv[0] contains a slash then it's probably an absolute path
263 * or relative to the current directory, so check if it exists. */
264 if (strchr(argv0, '/') != NULL && access(argv0, F_OK) == 0)
265 return copied_realpath(argv0);
267 /* Bail on an absolute path which doesn't exist */
271 /* Otherwise check if argv[0] exists relative to any directory in PATH */
272 search = getenv("PATH");
275 search = copied_string(search);
276 buf = successful_malloc(PATH_MAX + 1);
277 for (start = search; (end = strchr(start, ':')) != NULL; start = end + 1) {
279 snprintf(buf, PATH_MAX + 1, "%s/%s", start, argv0);
280 if (access(buf, F_OK) == 0) {
282 search = copied_realpath(buf);
292 #endif /* LISP_FEATURE_WIN32 */
297 struct runtime_options *runtime_options;
299 char *saved_runtime_path = NULL;
302 main(int argc, char *argv[], char *envp[])
304 #ifdef LISP_FEATURE_WIN32
305 /* Exception handling support structure. Evil Win32 hack. */
306 struct lisp_exception_frame exception_frame;
309 /* the name of the core file we're to execute. Note that this is
310 * a malloc'ed string which should be freed eventually. */
312 char **sbcl_argv = 0;
313 os_vm_offset_t embedded_core_offset = 0;
314 char *runtime_path = 0;
316 /* other command line options */
317 boolean noinform = 0;
318 boolean end_runtime_options = 0;
319 boolean disable_lossage_handler_p = 0;
321 lispobj initial_function;
322 const char *sbcl_home = getenv("SBCL_HOME");
325 block_blockable_signals(0, 0);
327 setlocale(LC_ALL, "");
329 runtime_options = NULL;
331 /* Save the argv[0] derived runtime path in case
332 * os_get_runtime_executable_path(1) isn't able to get an
333 * externally-usable path later on. */
334 saved_runtime_path = search_for_executable(argv[0]);
336 /* Check early to see if this executable has an embedded core,
337 * which also populates runtime_options if the core has runtime
339 runtime_path = os_get_runtime_executable_path(0);
340 if (runtime_path || saved_runtime_path) {
341 os_vm_offset_t offset = search_for_embedded_core(
342 runtime_path ? runtime_path : saved_runtime_path);
344 embedded_core_offset = offset;
345 core = (runtime_path ? runtime_path :
346 copied_string(saved_runtime_path));
354 /* Parse our part of the command line (aka "runtime options"),
355 * stripping out those options that we handle. */
356 if (runtime_options != NULL) {
357 dynamic_space_size = runtime_options->dynamic_space_size;
358 thread_control_stack_size = runtime_options->thread_control_stack_size;
363 runtime_options = successful_malloc(sizeof(struct runtime_options));
365 while (argi < argc) {
366 char *arg = argv[argi];
367 if (0 == strcmp(arg, "--script")) {
368 /* This is both a runtime and a toplevel option. As a
369 * runtime option, it is equivalent to --noinform.
370 * This exits, and does not increment argi, so that
371 * TOPLEVEL-INIT sees the option. */
373 end_runtime_options = 1;
374 disable_lossage_handler_p = 1;
375 lose_on_corruption_p = 1;
377 } else if (0 == strcmp(arg, "--noinform")) {
380 } else if (0 == strcmp(arg, "--core")) {
382 lose("more than one core file specified\n");
386 lose("missing filename for --core argument\n");
388 core = copied_string(argv[argi]);
391 } else if (0 == strcmp(arg, "--help")) {
392 /* I think this is the (or a) usual convention: upon
393 * seeing "--help" we immediately print our help
394 * string and exit, ignoring everything else. */
397 } else if (0 == strcmp(arg, "--version")) {
398 /* As in "--help" case, I think this is expected. */
401 } else if (0 == strcmp(arg, "--dynamic-space-size")) {
404 lose("missing argument for --dynamic-space-size");
407 long tmp = strtol(argv[argi++], &tail, 0);
409 lose("--dynamic-space-size argument is not a number");
411 (tmp >= (LONG_MAX >> 20))) {
412 lose("--dynamic-space-size argument is out of range");
414 dynamic_space_size = tmp << 20;
416 # ifdef MAX_DYNAMIC_SPACE_END
417 if (!((DYNAMIC_SPACE_START <
418 DYNAMIC_SPACE_START+dynamic_space_size) &&
419 (DYNAMIC_SPACE_START+dynamic_space_size <=
420 MAX_DYNAMIC_SPACE_END)))
421 lose("--dynamic-space-size argument is too large");
423 } else if (0 == strcmp(arg, "--control-stack-size")) {
426 lose("missing argument for --control-stack-size");
428 thread_control_stack_size = strtol(argv[argi++], 0, 0) << 20;
430 lose("argument to --control-stack-size is not a number");
431 } else if (0 == strcmp(arg, "--debug-environment")) {
433 printf("; Commandline arguments:\n");
435 printf("; %2d: \"%s\"\n", n, argv[n]);
439 printf(";\n; Environment:\n");
441 printf("; %2d: \"%s\"\n", n, ENVIRON[n]);
445 } else if (0 == strcmp(arg, "--disable-ldb")) {
446 disable_lossage_handler_p = 1;
448 } else if (0 == strcmp(arg, "--lose-on-corruption")) {
449 lose_on_corruption_p = 1;
451 } else if (0 == strcmp(arg, "--end-runtime-options")) {
452 end_runtime_options = 1;
456 /* This option was unrecognized as a runtime option,
457 * so it must be a toplevel option or a user option,
458 * so we must be past the end of the runtime option
463 /* This is where we strip out those options that we handle. We
464 * also take this opportunity to make sure that we don't find
465 * an out-of-place "--end-runtime-options" option. */
467 char *argi0 = argv[argi];
469 /* (argc - argi) for the arguments, one for the binary,
470 and one for the terminating NULL. */
471 sbcl_argv = successful_malloc((2 + argc - argi) * sizeof(char *));
472 sbcl_argv[0] = argv[0];
473 while (argi < argc) {
474 char *arg = argv[argi++];
475 /* If we encounter --end-runtime-options for the first
476 * time after the point where we had to give up on
477 * runtime options, then the point where we had to
478 * give up on runtime options must've been a user
480 if (!end_runtime_options &&
481 0 == strcmp(arg, "--end-runtime-options")) {
482 lose("bad runtime option \"%s\"\n", argi0);
484 sbcl_argv[argj++] = arg;
490 /* Align down to multiple of page_table page size, and to the appropriate
491 * stack alignment. */
492 dynamic_space_size &= ~(PAGE_BYTES-1);
493 #ifdef LISP_FEATURE_GENCGC
494 dynamic_space_size &= ~(GENCGC_CARD_BYTES-1);
496 thread_control_stack_size &= ~(CONTROL_STACK_ALIGNMENT_BYTES-1);
498 /* Preserve the runtime options for possible future core saving */
499 runtime_options->dynamic_space_size = dynamic_space_size;
500 runtime_options->thread_control_stack_size = thread_control_stack_size;
502 /* KLUDGE: os_vm_page_size is set by os_init(), and on some
503 * systems (e.g. Alpha) arch_init() needs need os_vm_page_size, so
504 * it must follow os_init(). -- WHN 2000-01-26 */
510 /* If no core file was specified, look for one. */
512 core = search_for_core();
515 /* Make sure that SBCL_HOME is set and not the empty string,
516 unless loading an embedded core. */
517 if (!(sbcl_home && *sbcl_home) && embedded_core_offset == 0) {
518 char *envstring, *copied_core, *dir;
519 char *stem = "SBCL_HOME=";
520 copied_core = copied_string(core);
521 dir = dirname(copied_core);
522 envstring = (char *) calloc(strlen(stem) +
526 sprintf(envstring, "%s%s", stem, dir);
531 if (!noinform && embedded_core_offset == 0) {
536 #if defined(SVR4) || defined(__linux__) || defined(__NetBSD__)
540 define_var("nil", NIL, 1);
541 define_var("t", T, 1);
543 if (!disable_lossage_handler_p)
544 enable_lossage_handler();
548 initial_function = load_core_file(core, embedded_core_offset);
549 if (initial_function == NIL) {
550 lose("couldn't find initial function\n");
552 #ifdef LISP_FEATURE_HPUX
553 /* -1 = CLOSURE_FUN_OFFSET, 23 = SIMPLE_FUN_CODE_OFFSET, we are
554 * not in LANGUAGE_ASSEMBLY so we cant reach them. */
555 return_from_lisp_stub = (void *) ((char *)*((unsigned long *)
556 ((char *)initial_function + -1)) + 23);
559 gc_initialize_pointers();
561 arch_install_interrupt_handlers();
562 #ifndef LISP_FEATURE_WIN32
563 os_install_interrupt_handlers();
565 /* wos_install_interrupt_handlers(handler); */
566 wos_install_interrupt_handlers(&exception_frame);
569 /* Pass core filename and the processed argv into Lisp. They'll
570 * need to be processed further there, to do locale conversion.
573 posix_argv = sbcl_argv;
575 FSHOW((stderr, "/funcalling initial_function=0x%lx\n",
576 (unsigned long)initial_function));
577 #ifdef LISP_FEATURE_WIN32
579 This is experimental prerelease support for the Windows platform: use\n\
580 at your own risk. \"Your Kitten of Death awaits!\"\n");
584 create_initial_thread(initial_function);
585 lose("CATS. CATS ARE NICE.\n");