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. */
45 static int top_level_val
;
47 /* Do a setjmp on error_return and quit_return. catch_errors is
48 generally a cleaner way to do this, but main() would look pretty
49 ugly if it had to use catch_errors each time. */
51 #define SET_TOP_LEVEL() \
52 (((top_level_val = setjmp (error_return)) \
53 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \
56 /* If nonzero, display time usage both at startup and for each command. */
60 /* If nonzero, display space usage both at startup and for each command. */
64 extern void gdb_init
PARAMS ((void));
75 /* Pointers to various arguments from command line. */
82 /* These are static so that we can take their address in an initializer. */
83 static int print_help
;
84 static int print_version
;
86 /* Pointers to all arguments of --command option. */
88 /* Allocated size of cmdarg. */
90 /* Number of elements of cmdarg used. */
93 /* Indices of all arguments of --directory option. */
97 /* Number of elements used. */
100 struct stat homebuf
, cwdbuf
;
101 char *homedir
, *homeinit
;
105 long time_at_startup
= get_run_time ();
107 /* start-sanitize-mpw */
109 /* Drop into MacsBug, but only if the executable is specially named. */
110 if (strcmp(argv
[0], "DEBUGGDB") == 0)
111 DebugStr("\pat start of GDB main");
112 /* Do all Mac-specific setup. */
115 /* end-sanitize-mpw */
116 /* This needs to happen before the first use of malloc. */
117 init_malloc ((PTR
) NULL
);
119 #if defined (ALIGN_STACK_ON_STARTUP)
120 i
= (int) &count
& 0x3;
125 /* If error() is called from initialization code, just exit */
126 if (SET_TOP_LEVEL ()) {
131 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
134 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
138 line
= (char *) xmalloc (linesize
);
139 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
142 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
143 current_directory
= gdb_dirbuf
;
145 /* Parse arguments and options. */
148 /* When var field is 0, use flag field to record the equivalent
149 short option (or arbitrary numbers starting at 10 for those
150 with no equivalent). */
151 static struct option long_options
[] =
153 {"readnow", no_argument
, &readnow_symbol_files
, 1},
154 {"r", no_argument
, &readnow_symbol_files
, 1},
155 {"mapped", no_argument
, &mapped_symbol_files
, 1},
156 {"m", no_argument
, &mapped_symbol_files
, 1},
157 {"quiet", no_argument
, &quiet
, 1},
158 {"q", no_argument
, &quiet
, 1},
159 {"silent", no_argument
, &quiet
, 1},
160 {"nx", no_argument
, &inhibit_gdbinit
, 1},
161 {"n", no_argument
, &inhibit_gdbinit
, 1},
162 {"batch", no_argument
, &batch
, 1},
163 {"epoch", no_argument
, &epoch_interface
, 1},
165 /* This is a synonym for "--annotate=1". --annotate is now preferred,
166 but keep this here for a long time because people will be running
167 emacses which use --fullname. */
168 {"fullname", no_argument
, 0, 'f'},
169 {"f", no_argument
, 0, 'f'},
171 {"annotate", required_argument
, 0, 12},
172 {"help", no_argument
, &print_help
, 1},
173 {"se", required_argument
, 0, 10},
174 {"symbols", required_argument
, 0, 's'},
175 {"s", required_argument
, 0, 's'},
176 {"exec", required_argument
, 0, 'e'},
177 {"e", required_argument
, 0, 'e'},
178 {"core", required_argument
, 0, 'c'},
179 {"c", required_argument
, 0, 'c'},
180 {"command", required_argument
, 0, 'x'},
181 {"version", no_argument
, &print_version
, 1},
182 {"x", required_argument
, 0, 'x'},
183 {"directory", required_argument
, 0, 'd'},
184 {"cd", required_argument
, 0, 11},
185 {"tty", required_argument
, 0, 't'},
186 {"baud", required_argument
, 0, 'b'},
187 {"b", required_argument
, 0, 'b'},
188 {"nw", no_argument
, &use_windows
, 0},
189 {"nowindows", no_argument
, &use_windows
, 0},
190 {"w", no_argument
, &use_windows
, 1},
191 {"windows", no_argument
, &use_windows
, 1},
192 {"statistics", no_argument
, 0, 13},
193 /* Allow machine descriptions to add more options... */
194 #ifdef ADDITIONAL_OPTIONS
197 {0, no_argument
, 0, 0}
204 c
= getopt_long_only (argc
, argv
, "",
205 long_options
, &option_index
);
209 /* Long option that takes an argument. */
210 if (c
== 0 && long_options
[option_index
].flag
== 0)
211 c
= long_options
[option_index
].val
;
216 /* Long option that just sets a flag. */
226 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
227 annotation_level
= atoi (optarg
);
230 /* Enable the display of both time and space usage. */
235 annotation_level
= 1;
247 cmdarg
[ncmd
++] = optarg
;
251 cmdarg
= (char **) xrealloc ((char *)cmdarg
,
252 cmdsize
* sizeof (*cmdarg
));
256 dirarg
[ndir
++] = optarg
;
260 dirarg
= (char **) xrealloc ((char *)dirarg
,
261 dirsize
* sizeof (*dirarg
));
275 i
= strtol (optarg
, &p
, 0);
276 if (i
== 0 && p
== optarg
)
278 /* Don't use *_filtered or warning() (which relies on
279 current_target) until after initialize_all_files(). */
283 "warning: could not set baud rate to `%s'.\n", optarg
);
289 #ifdef ADDITIONAL_OPTION_CASES
290 ADDITIONAL_OPTION_CASES
293 fprintf_unfiltered (gdb_stderr
,
294 "Use `%s --help' for a complete list of options.\n",
300 /* OK, that's all the options. The other arguments are filenames. */
302 for (; optind
< argc
; optind
++)
306 symarg
= argv
[optind
];
307 execarg
= argv
[optind
];
310 corearg
= argv
[optind
];
313 fprintf_unfiltered (gdb_stderr
,
314 "Excess command line arguments ignored. (%s%s)\n",
315 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
324 /* Do these (and anything which might call wrap_here or *_filtered)
325 after initialize_all_files. */
328 print_gdb_version (gdb_stdout
);
330 printf_filtered ("\n");
336 /* --version is intentionally not documented here, because we
337 are printing the version here, and the help is long enough
340 print_gdb_version (gdb_stdout
);
341 /* Make sure the output gets printed. */
343 printf_filtered ("\n");
345 /* But don't use *_filtered here. We don't want to prompt for continue
346 no matter how small the screen or how much we're going to print. */
347 /* start-sanitize-mpw */
348 /* For reasons too ugly to describe... */
350 fputs_unfiltered ("This is the GNU debugger.\n", gdb_stdout
);
352 /* end-sanitize-mpw */
354 This is the GNU debugger. Usage:\n\
355 gdb [options] [executable-file [core-file or process-id]]\n\
357 --help Print this message.\n\
358 --quiet Do not print version number on startup.\n\
359 --fullname Output information used by emacs-GDB interface.\n\
360 --epoch Output information used by epoch emacs-GDB interface.\n\
361 --batch Exit after processing options.\n\
362 --nx Do not read .gdbinit file.\n\
363 --tty=TTY Use TTY for input/output by the program being debugged.\n\
364 --cd=DIR Change current directory to DIR.\n\
365 --directory=DIR Search for source files in DIR.\n\
366 --command=FILE Execute GDB commands from FILE.\n\
367 --symbols=SYMFILE Read symbols from SYMFILE.\n\
368 --exec=EXECFILE Use EXECFILE as the executable.\n\
369 --se=FILE Use FILE as symbol file and executable file.\n\
370 --core=COREFILE Analyze the core dump COREFILE.\n\
371 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
372 --mapped Use mapped symbol files if supported on this system.\n\
373 --readnow Fully read symbol files on first access.\n\
374 --nw Do not use a window interface.\n\
376 /* start-sanitize-mpw */
378 /* end-sanitize-mpw */
379 #ifdef ADDITIONAL_OPTION_HELP
380 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, gdb_stdout
);
382 fputs_unfiltered ("\n\
383 For more information, type \"help\" from within GDB, or consult the\n\
384 GDB manual (available as on-line info or a printed manual).\n", gdb_stdout
);
390 /* Print all the junk at the top, with trailing "..." if we are about
391 to read a symbol file (possibly slowly). */
392 print_gnu_advertisement ();
393 print_gdb_version (gdb_stdout
);
395 printf_filtered ("..");
397 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
400 error_pre_print
= "\n\n";
401 /* We may get more than one warning, don't double space all of them... */
402 warning_pre_print
= "\nwarning: ";
404 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
405 *before* all the command line arguments are processed; it sets
406 global parameters, which are independent of what file you are
407 debugging or what directory you are in. */
408 homedir
= getenv ("HOME");
411 homeinit
= (char *) alloca (strlen (getenv ("HOME")) +
412 strlen (gdbinit
) + 10);
413 strcpy (homeinit
, getenv ("HOME"));
414 strcat (homeinit
, "/");
415 strcat (homeinit
, gdbinit
);
416 if (!inhibit_gdbinit
&& access (homeinit
, R_OK
) == 0)
418 if (!SET_TOP_LEVEL ())
419 source_command (homeinit
, 0);
421 do_cleanups (ALL_CLEANUPS
);
423 /* Do stats; no need to do them elsewhere since we'll only
424 need them if homedir is set. Make sure that they are
425 zero in case one of them fails (this guarantees that they
426 won't match if either exists). */
428 memset (&homebuf
, 0, sizeof (struct stat
));
429 memset (&cwdbuf
, 0, sizeof (struct stat
));
431 stat (homeinit
, &homebuf
);
432 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
436 /* Now perform all the actions indicated by the arguments. */
439 if (!SET_TOP_LEVEL ())
441 cd_command (cdarg
, 0);
444 do_cleanups (ALL_CLEANUPS
);
446 for (i
= 0; i
< ndir
; i
++)
447 if (!SET_TOP_LEVEL ())
448 directory_command (dirarg
[i
], 0);
450 do_cleanups (ALL_CLEANUPS
);
454 && STREQ (execarg
, symarg
))
456 /* The exec file and the symbol-file are the same. If we can't open
457 it, better only print one error message. */
458 if (!SET_TOP_LEVEL ())
460 exec_file_command (execarg
, !batch
);
461 symbol_file_command (symarg
, 0);
467 if (!SET_TOP_LEVEL ())
468 exec_file_command (execarg
, !batch
);
470 if (!SET_TOP_LEVEL ())
471 symbol_file_command (symarg
, 0);
473 do_cleanups (ALL_CLEANUPS
);
475 /* After the symbol file has been read, print a newline to get us
476 beyond the copyright line... But errors should still set off
477 the error message with a (single) blank line. */
479 printf_filtered ("\n");
480 error_pre_print
= "\n";
481 warning_pre_print
= "\nwarning: ";
484 if (!SET_TOP_LEVEL ())
485 core_file_command (corearg
, !batch
);
486 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
487 attach_command (corearg
, !batch
);
488 do_cleanups (ALL_CLEANUPS
);
491 if (!SET_TOP_LEVEL ())
492 tty_command (ttyarg
, !batch
);
493 do_cleanups (ALL_CLEANUPS
);
495 #ifdef ADDITIONAL_OPTION_HANDLER
496 ADDITIONAL_OPTION_HANDLER
;
499 /* Error messages should no longer be distinguished with extra output. */
501 warning_pre_print
= "warning: ";
503 /* Read the .gdbinit file in the current directory, *if* it isn't
504 the same as the $HOME/.gdbinit file (it should exist, also). */
507 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
508 if (!inhibit_gdbinit
&& access (gdbinit
, R_OK
) == 0)
510 if (!SET_TOP_LEVEL ())
511 source_command (gdbinit
, 0);
513 do_cleanups (ALL_CLEANUPS
);
515 for (i
= 0; i
< ncmd
; i
++)
517 if (!SET_TOP_LEVEL ())
519 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
520 read_command_file (stdin
);
522 source_command (cmdarg
[i
], !batch
);
523 do_cleanups (ALL_CLEANUPS
);
528 /* Read in the old history after all the command files have been read. */
533 /* We have hit the end of the batch file. */
537 /* Do any host- or target-specific hacks. This is used for i960 targets
538 to force the user to set a nindy target and spec its parameters. */
540 #ifdef BEFORE_MAIN_LOOP_HOOK
541 BEFORE_MAIN_LOOP_HOOK
;
544 /* Show time and/or space usage. */
548 long init_time
= get_run_time () - time_at_startup
;
550 printf_unfiltered ("Startup time: %ld.%06ld\n",
551 init_time
/ 1000000, init_time
% 1000000);
556 extern char **environ
;
557 char *lim
= (char *) sbrk (0);
559 printf_unfiltered ("Startup size: data size %ld\n",
560 (long) (lim
- (char *) &environ
));
563 /* The command loop. */
567 if (!SET_TOP_LEVEL ())
569 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
570 /* GUIs generally have their own command loop, mainloop, or whatever.
571 This is a good place to gain control because many error
572 conditions will end up here via longjmp(). */
573 if (command_loop_hook
)
574 command_loop_hook ();
577 quit_command ((char *)0, instream
== stdin
);
580 /* No exit -- exit is through quit_command. */
589 proc_wait (pid
, status
)
594 return wait (status
);
599 proc_remove_foreign (pid
)
605 fputs_unfiltered (linebuffer
, stream
)
606 const char *linebuffer
;
609 if (fputs_unfiltered_hook
)
611 /* FIXME: I think we should only be doing this for stdout or stderr.
612 Either that or we should be passing stream to the hook so it can
613 deal with it. If that is cleaned up, this function can go back
614 into utils.c and the fputs_unfiltered_hook can replace the current
615 ability to avoid this function by not linking with main.c. */
616 fputs_unfiltered_hook (linebuffer
, stream
);
620 fputs (linebuffer
, stream
);