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\
183 User options are not processed by SBCL. All runtime options must\n\
184 appear before toplevel options, and all toplevel options must\n\
185 appear before user options.\n\
187 For more information please refer to the SBCL User Manual, which\n\
188 should be installed along with SBCL, and is also available from the\n\
189 website <http://www.sbcl.org/>.\n");
195 printf("SBCL %s\n", SBCL_VERSION_STRING);
202 "This is SBCL %s, an implementation of ANSI Common Lisp.\n\
203 More information about SBCL is available at <http://www.sbcl.org/>.\n\
205 SBCL is free software, provided as is, with absolutely no warranty.\n\
206 It is mostly in the public domain; some portions are provided under\n\
207 BSD-style licenses. See the CREDITS and COPYING files in the\n\
208 distribution for more information.\n\
209 ", SBCL_VERSION_STRING);
212 /* Look for a core file to load, first in the directory named by the
213 * SBCL_HOME environment variable, then in a hardcoded default
214 * location. Returns a malloced copy of the core filename. */
218 char *sbcl_home = getenv("SBCL_HOME");
220 char *stem = "/sbcl.core";
223 if (!(sbcl_home && *sbcl_home)) sbcl_home = SBCL_HOME;
224 lookhere = (char *) calloc(strlen(sbcl_home) +
228 sprintf(lookhere, "%s%s", sbcl_home, stem);
229 core = copied_existing_filename_or_null(lookhere);
232 lose("can't find core file at %s\n", lookhere);
240 /* Try to find the path to an executable from argv[0], this is only
241 * used when os_get_runtime_executable_path() returns NULL */
242 #ifdef LISP_FEATURE_WIN32
244 search_for_executable(const char *argv0)
248 #else /* LISP_FEATURE_WIN32 */
250 search_for_executable(const char *argv0)
252 char *search, *start, *end, *buf;
254 /* If argv[0] contains a slash then it's probably an absolute path
255 * or relative to the current directory, so check if it exists. */
256 if (strchr(argv0, '/') != NULL && access(argv0, F_OK) == 0)
257 return copied_realpath(argv0);
259 /* Bail on an absolute path which doesn't exist */
263 /* Otherwise check if argv[0] exists relative to any directory in PATH */
264 search = getenv("PATH");
267 search = copied_string(search);
268 buf = successful_malloc(PATH_MAX + 1);
269 for (start = search; (end = strchr(start, ':')) != NULL; start = end + 1) {
271 snprintf(buf, PATH_MAX + 1, "%s/%s", start, argv0);
272 if (access(buf, F_OK) == 0) {
274 search = copied_realpath(buf);
284 #endif /* LISP_FEATURE_WIN32 */
289 struct runtime_options *runtime_options;
291 char *saved_runtime_path = NULL;
294 main(int argc, char *argv[], char *envp[])
296 #ifdef LISP_FEATURE_WIN32
297 /* Exception handling support structure. Evil Win32 hack. */
298 struct lisp_exception_frame exception_frame;
301 /* the name of the core file we're to execute. Note that this is
302 * a malloc'ed string which should be freed eventually. */
304 char **sbcl_argv = 0;
305 os_vm_offset_t embedded_core_offset = 0;
306 char *runtime_path = 0;
308 /* other command line options */
309 boolean noinform = 0;
310 boolean end_runtime_options = 0;
311 boolean disable_lossage_handler_p = 0;
313 lispobj initial_function;
314 const char *sbcl_home = getenv("SBCL_HOME");
317 block_blockable_signals(0, 0);
319 setlocale(LC_ALL, "");
321 runtime_options = NULL;
323 /* Save the argv[0] derived runtime path in case
324 * os_get_runtime_executable_path(1) isn't able to get an
325 * externally-usable path later on. */
326 saved_runtime_path = search_for_executable(argv[0]);
328 /* Check early to see if this executable has an embedded core,
329 * which also populates runtime_options if the core has runtime
331 runtime_path = os_get_runtime_executable_path(0);
332 if (runtime_path || saved_runtime_path) {
333 os_vm_offset_t offset = search_for_embedded_core(
334 runtime_path ? runtime_path : saved_runtime_path);
336 embedded_core_offset = offset;
337 core = (runtime_path ? runtime_path :
338 copied_string(saved_runtime_path));
346 /* Parse our part of the command line (aka "runtime options"),
347 * stripping out those options that we handle. */
348 if (runtime_options != NULL) {
349 dynamic_space_size = runtime_options->dynamic_space_size;
350 thread_control_stack_size = runtime_options->thread_control_stack_size;
355 runtime_options = successful_malloc(sizeof(struct runtime_options));
357 while (argi < argc) {
358 char *arg = argv[argi];
359 if (0 == strcmp(arg, "--script")) {
360 /* This is both a runtime and a toplevel option. As a
361 * runtime option, it is equivalent to --noinform.
362 * This exits, and does not increment argi, so that
363 * TOPLEVEL-INIT sees the option. */
365 end_runtime_options = 1;
366 disable_lossage_handler_p = 1;
367 lose_on_corruption_p = 1;
369 } else if (0 == strcmp(arg, "--noinform")) {
372 } else if (0 == strcmp(arg, "--core")) {
374 lose("more than one core file specified\n");
378 lose("missing filename for --core argument\n");
380 core = copied_string(argv[argi]);
383 } else if (0 == strcmp(arg, "--help")) {
384 /* I think this is the (or a) usual convention: upon
385 * seeing "--help" we immediately print our help
386 * string and exit, ignoring everything else. */
389 } else if (0 == strcmp(arg, "--version")) {
390 /* As in "--help" case, I think this is expected. */
393 } else if (0 == strcmp(arg, "--dynamic-space-size")) {
396 lose("missing argument for --dynamic-space-size");
399 long tmp = strtol(argv[argi++], &tail, 0);
401 lose("--dynamic-space-size argument is not a number");
403 (tmp >= (LONG_MAX >> 20))) {
404 lose("--dynamic-space-size argument is out of range");
406 dynamic_space_size = tmp << 20;
408 # ifdef MAX_DYNAMIC_SPACE_END
409 if (!((DYNAMIC_SPACE_START <
410 DYNAMIC_SPACE_START+dynamic_space_size) &&
411 (DYNAMIC_SPACE_START+dynamic_space_size <=
412 MAX_DYNAMIC_SPACE_END)))
413 lose("--dynamic-space-size argument is too large");
415 } else if (0 == strcmp(arg, "--control-stack-size")) {
418 lose("missing argument for --control-stack-size");
420 thread_control_stack_size = strtol(argv[argi++], 0, 0) << 20;
422 lose("argument to --control-stack-size is not a number");
423 } else if (0 == strcmp(arg, "--debug-environment")) {
425 printf("; Commandline arguments:\n");
427 printf("; %2d: \"%s\"\n", n, argv[n]);
431 printf(";\n; Environment:\n");
433 printf("; %2d: \"%s\"\n", n, ENVIRON[n]);
437 } else if (0 == strcmp(arg, "--disable-ldb")) {
438 disable_lossage_handler_p = 1;
440 } else if (0 == strcmp(arg, "--lose-on-corruption")) {
441 lose_on_corruption_p = 1;
443 } else if (0 == strcmp(arg, "--end-runtime-options")) {
444 end_runtime_options = 1;
448 /* This option was unrecognized as a runtime option,
449 * so it must be a toplevel option or a user option,
450 * so we must be past the end of the runtime option
455 /* This is where we strip out those options that we handle. We
456 * also take this opportunity to make sure that we don't find
457 * an out-of-place "--end-runtime-options" option. */
459 char *argi0 = argv[argi];
461 /* (argc - argi) for the arguments, one for the binary,
462 and one for the terminating NULL. */
463 sbcl_argv = successful_malloc((2 + argc - argi) * sizeof(char *));
464 sbcl_argv[0] = argv[0];
465 while (argi < argc) {
466 char *arg = argv[argi++];
467 /* If we encounter --end-runtime-options for the first
468 * time after the point where we had to give up on
469 * runtime options, then the point where we had to
470 * give up on runtime options must've been a user
472 if (!end_runtime_options &&
473 0 == strcmp(arg, "--end-runtime-options")) {
474 lose("bad runtime option \"%s\"\n", argi0);
476 sbcl_argv[argj++] = arg;
482 /* Align down to multiple of page_table page size, and to the appropriate
483 * stack alignment. */
484 dynamic_space_size &= ~(PAGE_BYTES-1);
485 thread_control_stack_size &= ~(CONTROL_STACK_ALIGNMENT_BYTES-1);
487 /* Preserve the runtime options for possible future core saving */
488 runtime_options->dynamic_space_size = dynamic_space_size;
489 runtime_options->thread_control_stack_size = thread_control_stack_size;
491 /* KLUDGE: os_vm_page_size is set by os_init(), and on some
492 * systems (e.g. Alpha) arch_init() needs need os_vm_page_size, so
493 * it must follow os_init(). -- WHN 2000-01-26 */
499 /* If no core file was specified, look for one. */
501 core = search_for_core();
504 /* Make sure that SBCL_HOME is set and not the empty string,
505 unless loading an embedded core. */
506 if (!(sbcl_home && *sbcl_home) && embedded_core_offset == 0) {
507 char *envstring, *copied_core, *dir;
508 char *stem = "SBCL_HOME=";
509 copied_core = copied_string(core);
510 dir = dirname(copied_core);
511 envstring = (char *) calloc(strlen(stem) +
515 sprintf(envstring, "%s%s", stem, dir);
520 if (!noinform && embedded_core_offset == 0) {
525 #if defined(SVR4) || defined(__linux__) || defined(__NetBSD__)
529 define_var("nil", NIL, 1);
530 define_var("t", T, 1);
532 if (!disable_lossage_handler_p)
533 enable_lossage_handler();
537 initial_function = load_core_file(core, embedded_core_offset);
538 if (initial_function == NIL) {
539 lose("couldn't find initial function\n");
541 #ifdef LISP_FEATURE_HPUX
542 /* -1 = CLOSURE_FUN_OFFSET, 23 = SIMPLE_FUN_CODE_OFFSET, we are
543 * not in LANGUAGE_ASSEMBLY so we cant reach them. */
544 return_from_lisp_stub = (void *) ((char *)*((unsigned long *)
545 ((char *)initial_function + -1)) + 23);
548 gc_initialize_pointers();
550 arch_install_interrupt_handlers();
551 #ifndef LISP_FEATURE_WIN32
552 os_install_interrupt_handlers();
554 /* wos_install_interrupt_handlers(handler); */
555 wos_install_interrupt_handlers(&exception_frame);
558 /* Pass core filename and the processed argv into Lisp. They'll
559 * need to be processed further there, to do locale conversion.
562 posix_argv = sbcl_argv;
564 FSHOW((stderr, "/funcalling initial_function=0x%lx\n",
565 (unsigned long)initial_function));
566 #ifdef LISP_FEATURE_WIN32
568 This is experimental prerelease support for the Windows platform: use\n\
569 at your own risk. \"Your Kitten of Death awaits!\"\n");
573 create_initial_thread(initial_function);
574 lose("CATS. CATS ARE NICE.\n");