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. */
28 #include <sys/types.h>
32 /* R_OK lives in either unistd.h or sys/file.h. */
40 /* Temporary variable for SET_TOP_LEVEL. */
41 static int top_level_val
;
43 /* Do a setjmp on error_return and quit_return. catch_errors is
44 generally a cleaner way to do this, but main() would look pretty
45 ugly if it had to use catch_errors each time. */
47 #define SET_TOP_LEVEL() \
48 (((top_level_val = setjmp (error_return)) \
49 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \
61 /* Pointers to various arguments from command line. */
68 /* These are static so that we can take their address in an initializer. */
69 static int print_help
;
70 static int print_version
;
72 /* Pointers to all arguments of --command option. */
74 /* Allocated size of cmdarg. */
76 /* Number of elements of cmdarg used. */
79 /* Indices of all arguments of --directory option. */
83 /* Number of elements used. */
86 struct stat homebuf
, cwdbuf
;
87 char *homedir
, *homeinit
;
91 /* start-sanitize-mpw */
93 /* Drop into MacsBug, but only if the executable is specially named. */
94 if (strcmp(argv
[0], "DEBUGGDB") == 0)
95 DebugStr("\pat start of GDB main");
96 /* Do all Mac-specific setup. */
99 /* end-sanitize-mpw */
100 /* This needs to happen before the first use of malloc. */
101 init_malloc ((PTR
) NULL
);
103 #if defined (ALIGN_STACK_ON_STARTUP)
104 i
= (int) &count
& 0x3;
109 /* If error() is called from initialization code, just exit */
110 if (SET_TOP_LEVEL ()) {
115 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
118 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
122 line
= (char *) xmalloc (linesize
);
123 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
126 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
127 current_directory
= gdb_dirbuf
;
129 /* Parse arguments and options. */
132 /* When var field is 0, use flag field to record the equivalent
133 short option (or arbitrary numbers starting at 10 for those
134 with no equivalent). */
135 static struct option long_options
[] =
137 {"readnow", no_argument
, &readnow_symbol_files
, 1},
138 {"r", no_argument
, &readnow_symbol_files
, 1},
139 {"mapped", no_argument
, &mapped_symbol_files
, 1},
140 {"m", no_argument
, &mapped_symbol_files
, 1},
141 {"quiet", no_argument
, &quiet
, 1},
142 {"q", no_argument
, &quiet
, 1},
143 {"silent", no_argument
, &quiet
, 1},
144 {"nx", no_argument
, &inhibit_gdbinit
, 1},
145 {"n", no_argument
, &inhibit_gdbinit
, 1},
146 {"batch", no_argument
, &batch
, 1},
147 {"epoch", no_argument
, &epoch_interface
, 1},
149 /* This is a synonym for "--annotate=1". --annotate is now preferred,
150 but keep this here for a long time because people will be running
151 emacses which use --fullname. */
152 {"fullname", no_argument
, 0, 'f'},
153 {"f", no_argument
, 0, 'f'},
155 {"annotate", required_argument
, 0, 12},
156 {"help", no_argument
, &print_help
, 1},
157 {"se", required_argument
, 0, 10},
158 {"symbols", required_argument
, 0, 's'},
159 {"s", required_argument
, 0, 's'},
160 {"exec", required_argument
, 0, 'e'},
161 {"e", required_argument
, 0, 'e'},
162 {"core", required_argument
, 0, 'c'},
163 {"c", required_argument
, 0, 'c'},
164 {"command", required_argument
, 0, 'x'},
165 {"version", no_argument
, &print_version
, 1},
166 {"x", required_argument
, 0, 'x'},
167 {"directory", required_argument
, 0, 'd'},
168 {"cd", required_argument
, 0, 11},
169 {"tty", required_argument
, 0, 't'},
170 {"baud", required_argument
, 0, 'b'},
171 {"b", required_argument
, 0, 'b'},
172 /* Allow machine descriptions to add more options... */
173 #ifdef ADDITIONAL_OPTIONS
176 {0, no_argument
, 0, 0},
183 c
= getopt_long_only (argc
, argv
, "",
184 long_options
, &option_index
);
188 /* Long option that takes an argument. */
189 if (c
== 0 && long_options
[option_index
].flag
== 0)
190 c
= long_options
[option_index
].val
;
195 /* Long option that just sets a flag. */
205 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
206 annotation_level
= atoi (optarg
);
209 annotation_level
= 1;
221 cmdarg
[ncmd
++] = optarg
;
225 cmdarg
= (char **) xrealloc ((char *)cmdarg
,
226 cmdsize
* sizeof (*cmdarg
));
230 dirarg
[ndir
++] = optarg
;
234 dirarg
= (char **) xrealloc ((char *)dirarg
,
235 dirsize
* sizeof (*dirarg
));
249 i
= strtol (optarg
, &p
, 0);
250 if (i
== 0 && p
== optarg
)
252 /* Don't use *_filtered or warning() (which relies on
253 current_target) until after initialize_all_files(). */
257 "warning: could not set baud rate to `%s'.\n", optarg
);
263 #ifdef ADDITIONAL_OPTION_CASES
264 ADDITIONAL_OPTION_CASES
267 fprintf_unfiltered (gdb_stderr
,
268 "Use `%s --help' for a complete list of options.\n",
274 /* OK, that's all the options. The other arguments are filenames. */
276 for (; optind
< argc
; optind
++)
280 symarg
= argv
[optind
];
281 execarg
= argv
[optind
];
284 corearg
= argv
[optind
];
287 fprintf_unfiltered (gdb_stderr
,
288 "Excess command line arguments ignored. (%s%s)\n",
289 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
298 /* Do these (and anything which might call wrap_here or *_filtered)
299 after initialize_all_files. */
302 print_gdb_version (gdb_stdout
);
304 printf_filtered ("\n");
310 /* --version is intentionally not documented here, because we
311 are printing the version here, and the help is long enough
314 print_gdb_version (gdb_stdout
);
315 /* Make sure the output gets printed. */
317 printf_filtered ("\n");
319 /* But don't use *_filtered here. We don't want to prompt for continue
320 no matter how small the screen or how much we're going to print. */
321 /* start-sanitize-mpw */
322 /* For reasons too ugly to describe... */
324 fputs_unfiltered ("This is the GNU debugger.\n", gdb_stdout
);
326 /* end-sanitize-mpw */
328 This is the GNU debugger. Usage:\n\
329 gdb [options] [executable-file [core-file or process-id]]\n\
331 --help Print this message.\n\
332 --quiet Do not print version number on startup.\n\
333 --fullname Output information used by emacs-GDB interface.\n\
334 --epoch Output information used by epoch emacs-GDB interface.\n\
335 --batch Exit after processing options.\n\
336 --nx Do not read .gdbinit file.\n\
337 --tty=TTY Use TTY for input/output by the program being debugged.\n\
338 --cd=DIR Change current directory to DIR.\n\
339 --directory=DIR Search for source files in DIR.\n\
340 --command=FILE Execute GDB commands from FILE.\n\
341 --symbols=SYMFILE Read symbols from SYMFILE.\n\
342 --exec=EXECFILE Use EXECFILE as the executable.\n\
343 --se=FILE Use FILE as symbol file and executable file.\n\
344 --core=COREFILE Analyze the core dump COREFILE.\n\
345 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
346 --mapped Use mapped symbol files if supported on this system.\n\
347 --readnow Fully read symbol files on first access.\n\
349 /* start-sanitize-mpw */
351 /* end-sanitize-mpw */
352 #ifdef ADDITIONAL_OPTION_HELP
353 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, gdb_stdout
);
355 fputs_unfiltered ("\n\
356 For more information, type \"help\" from within GDB, or consult the\n\
357 GDB manual (available as on-line info or a printed manual).\n", gdb_stdout
);
363 /* Print all the junk at the top, with trailing "..." if we are about
364 to read a symbol file (possibly slowly). */
365 print_gnu_advertisement ();
366 print_gdb_version (gdb_stdout
);
368 printf_filtered ("..");
370 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
373 error_pre_print
= "\n\n";
374 /* We may get more than one warning, don't double space all of them... */
375 warning_pre_print
= "\nwarning: ";
377 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
378 *before* all the command line arguments are processed; it sets
379 global parameters, which are independent of what file you are
380 debugging or what directory you are in. */
381 homedir
= getenv ("HOME");
384 homeinit
= (char *) alloca (strlen (getenv ("HOME")) +
385 strlen (gdbinit
) + 10);
386 strcpy (homeinit
, getenv ("HOME"));
387 strcat (homeinit
, "/");
388 strcat (homeinit
, gdbinit
);
389 if (!inhibit_gdbinit
&& access (homeinit
, R_OK
) == 0)
391 if (!SET_TOP_LEVEL ())
392 source_command (homeinit
, 0);
394 do_cleanups (ALL_CLEANUPS
);
396 /* Do stats; no need to do them elsewhere since we'll only
397 need them if homedir is set. Make sure that they are
398 zero in case one of them fails (this guarantees that they
399 won't match if either exists). */
401 memset (&homebuf
, 0, sizeof (struct stat
));
402 memset (&cwdbuf
, 0, sizeof (struct stat
));
404 stat (homeinit
, &homebuf
);
405 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
409 /* Now perform all the actions indicated by the arguments. */
412 if (!SET_TOP_LEVEL ())
414 cd_command (cdarg
, 0);
417 do_cleanups (ALL_CLEANUPS
);
419 for (i
= 0; i
< ndir
; i
++)
420 if (!SET_TOP_LEVEL ())
421 directory_command (dirarg
[i
], 0);
423 do_cleanups (ALL_CLEANUPS
);
427 && STREQ (execarg
, symarg
))
429 /* The exec file and the symbol-file are the same. If we can't open
430 it, better only print one error message. */
431 if (!SET_TOP_LEVEL ())
433 exec_file_command (execarg
, !batch
);
434 symbol_file_command (symarg
, 0);
440 if (!SET_TOP_LEVEL ())
441 exec_file_command (execarg
, !batch
);
443 if (!SET_TOP_LEVEL ())
444 symbol_file_command (symarg
, 0);
446 do_cleanups (ALL_CLEANUPS
);
448 /* After the symbol file has been read, print a newline to get us
449 beyond the copyright line... But errors should still set off
450 the error message with a (single) blank line. */
452 printf_filtered ("\n");
453 error_pre_print
= "\n";
454 warning_pre_print
= "\nwarning: ";
457 if (!SET_TOP_LEVEL ())
458 core_file_command (corearg
, !batch
);
459 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
460 attach_command (corearg
, !batch
);
461 do_cleanups (ALL_CLEANUPS
);
464 if (!SET_TOP_LEVEL ())
465 tty_command (ttyarg
, !batch
);
466 do_cleanups (ALL_CLEANUPS
);
468 #ifdef ADDITIONAL_OPTION_HANDLER
469 ADDITIONAL_OPTION_HANDLER
;
472 /* Error messages should no longer be distinguished with extra output. */
474 warning_pre_print
= "warning: ";
476 /* Read the .gdbinit file in the current directory, *if* it isn't
477 the same as the $HOME/.gdbinit file (it should exist, also). */
480 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
481 if (!inhibit_gdbinit
&& access (gdbinit
, R_OK
) == 0)
483 if (!SET_TOP_LEVEL ())
484 source_command (gdbinit
, 0);
486 do_cleanups (ALL_CLEANUPS
);
488 for (i
= 0; i
< ncmd
; i
++)
490 if (!SET_TOP_LEVEL ())
492 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
493 read_command_file (stdin
);
495 source_command (cmdarg
[i
], !batch
);
496 do_cleanups (ALL_CLEANUPS
);
501 /* Read in the old history after all the command files have been read. */
506 /* We have hit the end of the batch file. */
510 /* Do any host- or target-specific hacks. This is used for i960 targets
511 to force the user to set a nindy target and spec its parameters. */
513 #ifdef BEFORE_MAIN_LOOP_HOOK
514 BEFORE_MAIN_LOOP_HOOK
;
517 /* The command loop. */
521 if (!SET_TOP_LEVEL ())
523 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
524 /* start-sanitize-mpw */
526 /* If we're being a Mac application, go into a Mac-specific
527 event-handling loop instead. We still want to be inside
528 the outer loop, because that will catch longjmps resulting
529 from some command executions. */
534 /* end-sanitize-mpw */
536 quit_command ((char *)0, instream
== stdin
);
539 /* No exit -- exit is through quit_command. */
548 proc_wait (pid
, status
)
552 return wait (status
);
556 proc_remove_foreign (pid
)
562 fputs_unfiltered (linebuffer
, stream
)
563 const char *linebuffer
;
566 fputs (linebuffer
, stream
);