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 #include <sys/types.h>
26 #include <sys/param.h>
29 #ifdef LISP_FEATURE_SB_THREAD
30 #include <sys/ptrace.h>
35 #if defined(SVR4) || defined(__linux__)
46 #include "interrupt.h"
57 #include "genesis/static-symbols.h"
58 #include "genesis/symbol.h"
66 /* SIGINT handler that invokes the monitor (for when Lisp isn't up to it) */
68 sigint_handler(int signal, siginfo_t *info, void *void_context)
70 lose("\nSIGINT hit at 0x%08lX\n",
71 (unsigned long) *os_context_pc_addr(void_context));
74 /* (This is not static, because we want to be able to call it from
79 SHOW("entering sigint_init()");
80 install_handler(SIGINT, sigint_handler);
81 SHOW("leaving sigint_init()");
85 * helper functions for dealing with command line args
89 successful_malloc(size_t size)
91 void* result = malloc(size);
93 lose("malloc failure");
97 return (void *) NULL; /* dummy value: return something ... */
101 copied_string(char *string)
103 return strcpy(successful_malloc(1+strlen(string)), string);
107 copied_existing_filename_or_null(char *filename)
109 struct stat filename_stat;
110 if (stat(filename, &filename_stat)) { /* if failure */
113 return copied_string(filename);
117 /* Convert a null-terminated array of null-terminated strings (e.g.
118 * argv or envp) into a Lisp list of Lisp base-strings. */
120 alloc_base_string_list(char *array_ptr[])
123 return alloc_cons(alloc_base_string(*array_ptr),
124 alloc_base_string_list(1 + array_ptr));
130 /* miscellaneous chattiness */
136 "SBCL is a Common Lisp programming environment. Ordinarily you shouldn't\n\
137 need command line options when you invoke it interactively: you can just\n\
138 start it and work with the customary Lisp READ-EVAL-PRINT loop.\n\
140 One option idiom which is sometimes useful interactively (e.g. when\n\
141 exercising a test case for a bug report) is\n\
142 sbcl --sysinit /dev/null --userinit /dev/null\n\
143 to keep SBCL from reading any initialization files at startup. And some\n\
144 people like to suppress the default startup message:\n\
147 Other options can be useful when you're running SBCL noninteractively,\n\
148 e.g. from a script, or if you have a strange system configuration, so\n\
149 that SBCL can't by default find one of the files it needs. For\n\
150 information on such options, see the sbcl(1) man page.\n\
152 More information on SBCL can be found on its man page, or at\n\
153 <http://sbcl.sf.net/>.\n");
159 printf("SBCL %s\n", SBCL_VERSION_STRING);
166 "This is SBCL %s, an implementation of ANSI Common Lisp.\n\
168 SBCL is derived from the CMU CL system created at Carnegie Mellon University.\n\
169 Besides software and documentation originally created at Carnegie Mellon\n\
170 University, SBCL contains some software originally from the Massachusetts\n\
171 Institute of Technology, Symbolics Incorporated, and Xerox Corporation, and\n\
172 material contributed by volunteers since the release of CMU CL into the\n\
173 public domain. See the CREDITS file in the distribution for more information.\n\
175 SBCL is a free software system, provided as is, with absolutely no warranty.\n\
176 It is mostly in the public domain, but also includes some software copyrighted\n\
177 Massachusetts Institute of Technology, 1986;\n\
178 Symbolics, Inc., 1989, 1990, 1991, 1992; and\n\
179 Xerox Corporation, 1985, 1986, 1987, 1988, 1989, 1990\n\
180 used under BSD-style licenses allowing copying only under certain conditions.\n\
181 See the COPYING file in the distribution for more information.\n\
183 More information about SBCL is available at <http://sbcl.sourceforge.net/>.\n\
184 ", SBCL_VERSION_STRING);
192 main(int argc, char *argv[], char *envp[])
194 /* the name of the core file we're to execute. Note that this is
195 * a malloc'ed string which should be freed eventually. */
198 /* other command line options */
199 boolean noinform = 0;
200 boolean end_runtime_options = 0;
202 lispobj initial_function;
204 /* KLUDGE: os_vm_page_size is set by os_init(), and on some
205 * systems (e.g. Alpha) arch_init() needs need os_vm_page_size, so
206 * it must follow os_init(). -- WHN 2000-01-26 */
212 /* Parse our part of the command line (aka "runtime options"),
213 * stripping out those options that we handle. */
216 while (argi < argc) {
217 char *arg = argv[argi];
218 if (0 == strcmp(arg, "--noinform")) {
221 } else if (0 == strcmp(arg, "--core")) {
223 lose("more than one core file specified");
227 lose("missing filename for --core argument");
229 core = copied_string(argv[argi]);
232 } else if (0 == strcmp(arg, "--help")) {
233 /* I think this is the (or a) usual convention: upon
234 * seeing "--help" we immediately print our help
235 * string and exit, ignoring everything else. */
238 } else if (0 == strcmp(arg, "--version")) {
239 /* As in "--help" case, I think this is expected. */
242 } else if (0 == strcmp(arg, "--end-runtime-options")) {
243 end_runtime_options = 1;
247 /* This option was unrecognized as a runtime option,
248 * so it must be a toplevel option or a user option,
249 * so we must be past the end of the runtime option
254 /* This is where we strip out those options that we handle. We
255 * also take this opportunity to make sure that we don't find
256 * an out-of-place "--end-runtime-options" option. */
258 char *argi0 = argv[argi];
260 while (argi < argc) {
261 char *arg = argv[argi++];
262 /* If we encounter --end-runtime-options for the first
263 * time after the point where we had to give up on
264 * runtime options, then the point where we had to
265 * give up on runtime options must've been a user
267 if (!end_runtime_options &&
268 0 == strcmp(arg, "--end-runtime-options")) {
269 lose("bad runtime option \"%s\"", argi0);
278 /* If no core file was specified, look for one. */
280 char *sbcl_home = getenv("SBCL_HOME");
283 char *stem = "/sbcl.core";
284 lookhere = (char *) calloc(strlen(sbcl_home) +
288 sprintf(lookhere, "%s%s", sbcl_home, stem);
289 core = copied_existing_filename_or_null(lookhere);
292 putenv("SBCL_HOME=/usr/local/lib/sbcl/");
293 core = copied_existing_filename_or_null("/usr/local/lib/sbcl/sbcl.core");
295 putenv("SBCL_HOME=/usr/lib/sbcl/");
297 copied_existing_filename_or_null("/usr/lib/sbcl/sbcl.core");
301 lose("can't find core file");
304 /* If a core was specified and SBCL_HOME is unset, set it */
305 char *sbcl_home = getenv("SBCL_HOME");
307 char *envstring, *copied_core, *dir;
308 char *stem = "SBCL_HOME=";
309 copied_core = copied_string(core);
310 dir = dirname(copied_core);
311 envstring = (char *) calloc(strlen(stem) +
315 sprintf(envstring, "%s%s", stem, dir);
329 #if defined(SVR4) || defined(__linux__)
333 define_var("nil", NIL, 1);
334 define_var("t", T, 1);
336 set_lossage_handler(monitor_or_something);
340 initial_function = load_core_file(core);
341 if (initial_function == NIL) {
342 lose("couldn't find initial function");
344 SHOW("freeing core");
347 gc_initialize_pointers();
350 arch_install_interrupt_handlers();
351 os_install_interrupt_handlers();
353 /* Convert remaining argv values to something that Lisp can grok. */
354 SHOW("setting POSIX-ARGV symbol value");
355 SetSymbolValue(POSIX_ARGV, alloc_base_string_list(argv),0);
357 /* Install a handler to pick off SIGINT until the Lisp system gets
358 * far enough along to install its own handler. */
361 FSHOW((stderr, "/funcalling initial_function=0x%lx\n", initial_function));
362 create_thread(initial_function);
363 /* in a unithread build, create_thread never returns */
364 #ifdef LISP_FEATURE_SB_THREAD
365 gc_thread_pid=getpid();
370 static void parent_sighandler(int signum,siginfo_t *info, void *void_context)
373 os_context_t *context = (os_context_t*)void_context;
375 "parent thread got signal %d from %d, maybe_gc_pending=%d\n",
376 signum, info->si_pid,
381 #ifdef LISP_FEATURE_SB_THREAD
383 static void /* noreturn */ parent_loop(void)
390 sigemptyset(&sigset);
392 sigaddset(&sigset, SIGALRM);
393 sigaddset(&sigset, SIGCHLD);
394 sigprocmask(SIG_UNBLOCK,&sigset,0);
395 sa.sa_handler=parent_sighandler;
397 sa.sa_flags=SA_SIGINFO;
398 sigaction(SIGALRM, &sa, 0);
399 sigaction(SIGCHLD, &sa, 0);
401 sigemptyset(&sigset);
402 sa.sa_handler=SIG_IGN;
405 sigaction(SIGINT, &sa, 0);
407 while(!all_threads) {
410 while(all_threads && (pid=waitpid(-1,&status,__WALL|WUNTRACED))) {
412 int real_errno=errno;
414 if(real_errno == EINTR) {
417 if(real_errno == ECHILD) break;
418 fprintf(stderr,"waitpid: %s\n",strerror(real_errno));
421 th=find_thread_by_pid(pid);
423 if(WIFEXITED(status) || WIFSIGNALED(status)) {
424 fprintf(stderr,"waitpid : child %d %x exited \n", pid,th);
426 if(!all_threads) break;
429 exit(WEXITSTATUS(status));