1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include "call-cmds.h"
30 #include <sys/types.h>
35 /* R_OK lives in either unistd.h or sys/file.h. */
43 /* Temporary variable for SET_TOP_LEVEL. */
44 static int top_level_val;
46 /* Do a setjmp on error_return and quit_return. catch_errors is
47 generally a cleaner way to do this, but main() would look pretty
48 ugly if it had to use catch_errors each time. */
50 #define SET_TOP_LEVEL() \
51 (((top_level_val = setjmp (error_return)) \
52 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \
55 extern void gdb_init PARAMS ((void));
66 /* Pointers to various arguments from command line. */
73 /* These are static so that we can take their address in an initializer. */
74 static int print_help;
75 static int print_version;
77 /* Pointers to all arguments of --command option. */
79 /* Allocated size of cmdarg. */
81 /* Number of elements of cmdarg used. */
84 /* Indices of all arguments of --directory option. */
88 /* Number of elements used. */
91 struct stat homebuf, cwdbuf;
92 char *homedir, *homeinit;
96 /* start-sanitize-mpw */
98 /* Drop into MacsBug, but only if the executable is specially named. */
99 if (strcmp(argv[0], "DEBUGGDB") == 0)
100 DebugStr("\pat start of GDB main");
101 /* Do all Mac-specific setup. */
104 /* end-sanitize-mpw */
105 /* This needs to happen before the first use of malloc. */
106 init_malloc ((PTR) NULL);
108 #if defined (ALIGN_STACK_ON_STARTUP)
109 i = (int) &count & 0x3;
114 /* If error() is called from initialization code, just exit */
115 if (SET_TOP_LEVEL ()) {
120 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
123 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
127 line = (char *) xmalloc (linesize);
128 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
131 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
132 current_directory = gdb_dirbuf;
134 /* Parse arguments and options. */
137 /* When var field is 0, use flag field to record the equivalent
138 short option (or arbitrary numbers starting at 10 for those
139 with no equivalent). */
140 static struct option long_options[] =
142 {"readnow", no_argument, &readnow_symbol_files, 1},
143 {"r", no_argument, &readnow_symbol_files, 1},
144 {"mapped", no_argument, &mapped_symbol_files, 1},
145 {"m", no_argument, &mapped_symbol_files, 1},
146 {"quiet", no_argument, &quiet, 1},
147 {"q", no_argument, &quiet, 1},
148 {"silent", no_argument, &quiet, 1},
149 {"nx", no_argument, &inhibit_gdbinit, 1},
150 {"n", no_argument, &inhibit_gdbinit, 1},
151 {"batch", no_argument, &batch, 1},
152 {"epoch", no_argument, &epoch_interface, 1},
154 /* This is a synonym for "--annotate=1". --annotate is now preferred,
155 but keep this here for a long time because people will be running
156 emacses which use --fullname. */
157 {"fullname", no_argument, 0, 'f'},
158 {"f", no_argument, 0, 'f'},
160 {"annotate", required_argument, 0, 12},
161 {"help", no_argument, &print_help, 1},
162 {"se", required_argument, 0, 10},
163 {"symbols", required_argument, 0, 's'},
164 {"s", required_argument, 0, 's'},
165 {"exec", required_argument, 0, 'e'},
166 {"e", required_argument, 0, 'e'},
167 {"core", required_argument, 0, 'c'},
168 {"c", required_argument, 0, 'c'},
169 {"command", required_argument, 0, 'x'},
170 {"version", no_argument, &print_version, 1},
171 {"x", required_argument, 0, 'x'},
172 {"directory", required_argument, 0, 'd'},
173 {"cd", required_argument, 0, 11},
174 {"tty", required_argument, 0, 't'},
175 {"baud", required_argument, 0, 'b'},
176 {"b", required_argument, 0, 'b'},
177 {"nw", no_argument, &no_windows, 1},
178 {"nowindows", no_argument, &no_windows, 1},
179 /* Allow machine descriptions to add more options... */
180 #ifdef ADDITIONAL_OPTIONS
183 {0, no_argument, 0, 0},
190 c = getopt_long_only (argc, argv, "",
191 long_options, &option_index);
195 /* Long option that takes an argument. */
196 if (c == 0 && long_options[option_index].flag == 0)
197 c = long_options[option_index].val;
202 /* Long option that just sets a flag. */
212 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
213 annotation_level = atoi (optarg);
216 annotation_level = 1;
228 cmdarg[ncmd++] = optarg;
232 cmdarg = (char **) xrealloc ((char *)cmdarg,
233 cmdsize * sizeof (*cmdarg));
237 dirarg[ndir++] = optarg;
241 dirarg = (char **) xrealloc ((char *)dirarg,
242 dirsize * sizeof (*dirarg));
256 i = strtol (optarg, &p, 0);
257 if (i == 0 && p == optarg)
259 /* Don't use *_filtered or warning() (which relies on
260 current_target) until after initialize_all_files(). */
264 "warning: could not set baud rate to `%s'.\n", optarg);
270 #ifdef ADDITIONAL_OPTION_CASES
271 ADDITIONAL_OPTION_CASES
274 fprintf_unfiltered (gdb_stderr,
275 "Use `%s --help' for a complete list of options.\n",
281 /* OK, that's all the options. The other arguments are filenames. */
283 for (; optind < argc; optind++)
287 symarg = argv[optind];
288 execarg = argv[optind];
291 corearg = argv[optind];
294 fprintf_unfiltered (gdb_stderr,
295 "Excess command line arguments ignored. (%s%s)\n",
296 argv[optind], (optind == argc - 1) ? "" : " ...");
305 /* Do these (and anything which might call wrap_here or *_filtered)
306 after initialize_all_files. */
309 print_gdb_version (gdb_stdout);
311 printf_filtered ("\n");
317 /* --version is intentionally not documented here, because we
318 are printing the version here, and the help is long enough
321 print_gdb_version (gdb_stdout);
322 /* Make sure the output gets printed. */
324 printf_filtered ("\n");
326 /* But don't use *_filtered here. We don't want to prompt for continue
327 no matter how small the screen or how much we're going to print. */
328 /* start-sanitize-mpw */
329 /* For reasons too ugly to describe... */
331 fputs_unfiltered ("This is the GNU debugger.\n", gdb_stdout);
333 /* end-sanitize-mpw */
335 This is the GNU debugger. Usage:\n\
336 gdb [options] [executable-file [core-file or process-id]]\n\
338 --help Print this message.\n\
339 --quiet Do not print version number on startup.\n\
340 --fullname Output information used by emacs-GDB interface.\n\
341 --epoch Output information used by epoch emacs-GDB interface.\n\
342 --batch Exit after processing options.\n\
343 --nx Do not read .gdbinit file.\n\
344 --tty=TTY Use TTY for input/output by the program being debugged.\n\
345 --cd=DIR Change current directory to DIR.\n\
346 --directory=DIR Search for source files in DIR.\n\
347 --command=FILE Execute GDB commands from FILE.\n\
348 --symbols=SYMFILE Read symbols from SYMFILE.\n\
349 --exec=EXECFILE Use EXECFILE as the executable.\n\
350 --se=FILE Use FILE as symbol file and executable file.\n\
351 --core=COREFILE Analyze the core dump COREFILE.\n\
352 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
353 --mapped Use mapped symbol files if supported on this system.\n\
354 --readnow Fully read symbol files on first access.\n\
355 --nw Do not use a window interface.\n\
357 /* start-sanitize-mpw */
359 /* end-sanitize-mpw */
360 #ifdef ADDITIONAL_OPTION_HELP
361 fputs_unfiltered (ADDITIONAL_OPTION_HELP, gdb_stdout);
363 fputs_unfiltered ("\n\
364 For more information, type \"help\" from within GDB, or consult the\n\
365 GDB manual (available as on-line info or a printed manual).\n", gdb_stdout);
371 /* Print all the junk at the top, with trailing "..." if we are about
372 to read a symbol file (possibly slowly). */
373 print_gnu_advertisement ();
374 print_gdb_version (gdb_stdout);
376 printf_filtered ("..");
378 gdb_flush (gdb_stdout); /* Force to screen during slow operations */
381 error_pre_print = "\n\n";
382 /* We may get more than one warning, don't double space all of them... */
383 warning_pre_print = "\nwarning: ";
385 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
386 *before* all the command line arguments are processed; it sets
387 global parameters, which are independent of what file you are
388 debugging or what directory you are in. */
389 homedir = getenv ("HOME");
392 homeinit = (char *) alloca (strlen (getenv ("HOME")) +
393 strlen (gdbinit) + 10);
394 strcpy (homeinit, getenv ("HOME"));
395 strcat (homeinit, "/");
396 strcat (homeinit, gdbinit);
397 if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
399 if (!SET_TOP_LEVEL ())
400 source_command (homeinit, 0);
402 do_cleanups (ALL_CLEANUPS);
404 /* Do stats; no need to do them elsewhere since we'll only
405 need them if homedir is set. Make sure that they are
406 zero in case one of them fails (this guarantees that they
407 won't match if either exists). */
409 memset (&homebuf, 0, sizeof (struct stat));
410 memset (&cwdbuf, 0, sizeof (struct stat));
412 stat (homeinit, &homebuf);
413 stat (gdbinit, &cwdbuf); /* We'll only need this if
417 /* Now perform all the actions indicated by the arguments. */
420 if (!SET_TOP_LEVEL ())
422 cd_command (cdarg, 0);
425 do_cleanups (ALL_CLEANUPS);
427 for (i = 0; i < ndir; i++)
428 if (!SET_TOP_LEVEL ())
429 directory_command (dirarg[i], 0);
431 do_cleanups (ALL_CLEANUPS);
435 && STREQ (execarg, symarg))
437 /* The exec file and the symbol-file are the same. If we can't open
438 it, better only print one error message. */
439 if (!SET_TOP_LEVEL ())
441 exec_file_command (execarg, !batch);
442 symbol_file_command (symarg, 0);
448 if (!SET_TOP_LEVEL ())
449 exec_file_command (execarg, !batch);
451 if (!SET_TOP_LEVEL ())
452 symbol_file_command (symarg, 0);
454 do_cleanups (ALL_CLEANUPS);
456 /* After the symbol file has been read, print a newline to get us
457 beyond the copyright line... But errors should still set off
458 the error message with a (single) blank line. */
460 printf_filtered ("\n");
461 error_pre_print = "\n";
462 warning_pre_print = "\nwarning: ";
465 if (!SET_TOP_LEVEL ())
466 core_file_command (corearg, !batch);
467 else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ())
468 attach_command (corearg, !batch);
469 do_cleanups (ALL_CLEANUPS);
472 if (!SET_TOP_LEVEL ())
473 tty_command (ttyarg, !batch);
474 do_cleanups (ALL_CLEANUPS);
476 #ifdef ADDITIONAL_OPTION_HANDLER
477 ADDITIONAL_OPTION_HANDLER;
480 /* Error messages should no longer be distinguished with extra output. */
482 warning_pre_print = "warning: ";
484 /* Read the .gdbinit file in the current directory, *if* it isn't
485 the same as the $HOME/.gdbinit file (it should exist, also). */
488 || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
489 if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
491 if (!SET_TOP_LEVEL ())
492 source_command (gdbinit, 0);
494 do_cleanups (ALL_CLEANUPS);
496 for (i = 0; i < ncmd; i++)
498 if (!SET_TOP_LEVEL ())
500 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
501 read_command_file (stdin);
503 source_command (cmdarg[i], !batch);
504 do_cleanups (ALL_CLEANUPS);
509 /* Read in the old history after all the command files have been read. */
514 /* We have hit the end of the batch file. */
518 /* Do any host- or target-specific hacks. This is used for i960 targets
519 to force the user to set a nindy target and spec its parameters. */
521 #ifdef BEFORE_MAIN_LOOP_HOOK
522 BEFORE_MAIN_LOOP_HOOK;
525 /* The command loop. */
529 if (!SET_TOP_LEVEL ())
531 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
532 /* GUIs generally have their own command loop, mainloop, or whatever.
533 This is a good place to gain control because many error
534 conditions will end up here via longjmp(). */
535 if (command_loop_hook)
536 command_loop_hook ();
539 quit_command ((char *)0, instream == stdin);
542 /* No exit -- exit is through quit_command. */
551 proc_wait (pid, status)
556 return wait (status);
561 proc_remove_foreign (pid)
567 fputs_unfiltered (linebuffer, stream)
568 const char *linebuffer;
571 if (fputs_unfiltered_hook)
573 fputs_unfiltered_hook (linebuffer);
577 fputs (linebuffer, stream);