1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "call-cmds.h"
30 #include <sys/types.h>
34 #include "gdb_string.h"
42 /* 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 /* If nonzero, display time usage both at startup and for each command. */
59 /* If nonzero, display space usage both at startup and for each command. */
63 extern void gdb_init
PARAMS ((void));
74 /* Pointers to various arguments from command line. */
81 /* These are static so that we can take their address in an initializer. */
82 static int print_help
;
83 static int print_version
;
85 /* Pointers to all arguments of --command option. */
87 /* Allocated size of cmdarg. */
89 /* Number of elements of cmdarg used. */
92 /* Indices of all arguments of --directory option. */
96 /* Number of elements used. */
99 struct stat homebuf
, cwdbuf
;
100 char *homedir
, *homeinit
;
104 long time_at_startup
= get_run_time ();
106 START_PROGRESS (argv
[0], 0);
109 /* Do all Mac-specific setup. */
113 /* This needs to happen before the first use of malloc. */
114 init_malloc ((PTR
) NULL
);
116 #if defined (ALIGN_STACK_ON_STARTUP)
117 i
= (int) &count
& 0x3;
122 /* If error() is called from initialization code, just exit */
123 if (SET_TOP_LEVEL ()) {
128 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
131 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
135 line
= (char *) xmalloc (linesize
);
136 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
139 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
140 current_directory
= gdb_dirbuf
;
142 /* Parse arguments and options. */
146 /* When var field is 0, use flag field to record the equivalent
147 short option (or arbitrary numbers starting at 10 for those
148 with no equivalent). */
149 static struct option long_options
[] =
151 {"readnow", no_argument
, &readnow_symbol_files
, 1},
152 {"r", no_argument
, &readnow_symbol_files
, 1},
153 {"mapped", no_argument
, &mapped_symbol_files
, 1},
154 {"m", no_argument
, &mapped_symbol_files
, 1},
155 {"quiet", no_argument
, &quiet
, 1},
156 {"q", no_argument
, &quiet
, 1},
157 {"silent", no_argument
, &quiet
, 1},
158 {"nx", no_argument
, &inhibit_gdbinit
, 1},
159 {"n", no_argument
, &inhibit_gdbinit
, 1},
160 {"batch", no_argument
, &batch
, 1},
161 {"epoch", no_argument
, &epoch_interface
, 1},
163 /* This is a synonym for "--annotate=1". --annotate is now preferred,
164 but keep this here for a long time because people will be running
165 emacses which use --fullname. */
166 {"fullname", no_argument
, 0, 'f'},
167 {"f", no_argument
, 0, 'f'},
169 {"annotate", required_argument
, 0, 12},
170 {"help", no_argument
, &print_help
, 1},
171 {"se", required_argument
, 0, 10},
172 {"symbols", required_argument
, 0, 's'},
173 {"s", required_argument
, 0, 's'},
174 {"exec", required_argument
, 0, 'e'},
175 {"e", required_argument
, 0, 'e'},
176 {"core", required_argument
, 0, 'c'},
177 {"c", required_argument
, 0, 'c'},
178 {"command", required_argument
, 0, 'x'},
179 {"version", no_argument
, &print_version
, 1},
180 {"x", required_argument
, 0, 'x'},
181 {"directory", required_argument
, 0, 'd'},
182 {"cd", required_argument
, 0, 11},
183 {"tty", required_argument
, 0, 't'},
184 {"baud", required_argument
, 0, 'b'},
185 {"b", required_argument
, 0, 'b'},
186 {"nw", no_argument
, &use_windows
, 0},
187 {"nowindows", no_argument
, &use_windows
, 0},
188 {"w", no_argument
, &use_windows
, 1},
189 {"windows", no_argument
, &use_windows
, 1},
190 {"statistics", no_argument
, 0, 13},
191 /* Allow machine descriptions to add more options... */
192 #ifdef ADDITIONAL_OPTIONS
195 {0, no_argument
, 0, 0}
202 c
= getopt_long_only (argc
, argv
, "",
203 long_options
, &option_index
);
207 /* Long option that takes an argument. */
208 if (c
== 0 && long_options
[option_index
].flag
== 0)
209 c
= long_options
[option_index
].val
;
214 /* Long option that just sets a flag. */
224 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
225 annotation_level
= atoi (optarg
);
228 /* Enable the display of both time and space usage. */
233 annotation_level
= 1;
234 /* We have probably been invoked from emacs. Disable window interface. */
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 /* If --help or --version, disable window interface. */
301 if (print_help
|| print_version
)
304 /* OK, that's all the options. The other arguments are filenames. */
306 for (; optind
< argc
; optind
++)
310 symarg
= argv
[optind
];
311 execarg
= argv
[optind
];
314 corearg
= argv
[optind
];
317 fprintf_unfiltered (gdb_stderr
,
318 "Excess command line arguments ignored. (%s%s)\n",
319 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
329 /* Do these (and anything which might call wrap_here or *_filtered)
330 after initialize_all_files. */
333 print_gdb_version (gdb_stdout
);
335 printf_filtered ("\n");
341 /* --version is intentionally not documented here, because we
342 are printing the version here, and the help is long enough
345 print_gdb_version (gdb_stdout
);
346 /* Make sure the output gets printed. */
348 printf_filtered ("\n");
350 /* But don't use *_filtered here. We don't want to prompt for continue
351 no matter how small the screen or how much we're going to print. */
353 This is the GNU debugger. Usage:\n\
354 gdb [options] [executable-file [core-file or process-id]]\n\
356 --help Print this message.\n\
357 --quiet Do not print version number on startup.\n\
358 --fullname Output information used by emacs-GDB interface.\n\
359 --epoch Output information used by epoch emacs-GDB interface.\n\
362 --batch Exit after processing options.\n\
363 --nx Do not read .gdbinit file.\n\
364 --tty=TTY Use TTY for input/output by the program being debugged.\n\
365 --cd=DIR Change current directory to DIR.\n\
366 --directory=DIR Search for source files in DIR.\n\
369 --command=FILE Execute GDB commands from FILE.\n\
370 --symbols=SYMFILE Read symbols from SYMFILE.\n\
371 --exec=EXECFILE Use EXECFILE as the executable.\n\
372 --se=FILE Use FILE as symbol file and executable file.\n\
375 --core=COREFILE Analyze the core dump COREFILE.\n\
376 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
377 --mapped Use mapped symbol files if supported on this system.\n\
378 --readnow Fully read symbol files on first access.\n\
379 --nw Do not use a window interface.\n\
381 #ifdef ADDITIONAL_OPTION_HELP
382 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, gdb_stdout
);
384 fputs_unfiltered ("\n\
385 For more information, type \"help\" from within GDB, or consult the\n\
386 GDB manual (available as on-line info or a printed manual).\n", gdb_stdout
);
392 /* Print all the junk at the top, with trailing "..." if we are about
393 to read a symbol file (possibly slowly). */
394 print_gnu_advertisement ();
395 print_gdb_version (gdb_stdout
);
397 printf_filtered ("..");
399 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
402 error_pre_print
= "\n\n";
403 quit_pre_print
= error_pre_print
;
405 /* We may get more than one warning, don't double space all of them... */
406 warning_pre_print
= "\nwarning: ";
408 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
409 *before* all the command line arguments are processed; it sets
410 global parameters, which are independent of what file you are
411 debugging or what directory you are in. */
412 homedir
= getenv ("HOME");
415 homeinit
= (char *) alloca (strlen (getenv ("HOME")) +
416 strlen (gdbinit
) + 10);
417 strcpy (homeinit
, getenv ("HOME"));
418 strcat (homeinit
, "/");
419 strcat (homeinit
, gdbinit
);
421 if (!inhibit_gdbinit
&& access (homeinit
, R_OK
) == 0)
423 if (!SET_TOP_LEVEL ())
424 source_command (homeinit
, 0);
426 do_cleanups (ALL_CLEANUPS
);
428 /* Do stats; no need to do them elsewhere since we'll only
429 need them if homedir is set. Make sure that they are
430 zero in case one of them fails (this guarantees that they
431 won't match if either exists). */
433 memset (&homebuf
, 0, sizeof (struct stat
));
434 memset (&cwdbuf
, 0, sizeof (struct stat
));
436 stat (homeinit
, &homebuf
);
437 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
441 /* Now perform all the actions indicated by the arguments. */
444 if (!SET_TOP_LEVEL ())
446 cd_command (cdarg
, 0);
449 do_cleanups (ALL_CLEANUPS
);
451 for (i
= 0; i
< ndir
; i
++)
452 if (!SET_TOP_LEVEL ())
453 directory_command (dirarg
[i
], 0);
455 do_cleanups (ALL_CLEANUPS
);
459 && STREQ (execarg
, symarg
))
461 /* The exec file and the symbol-file are the same. If we can't open
462 it, better only print one error message. */
463 if (!SET_TOP_LEVEL ())
465 exec_file_command (execarg
, !batch
);
466 symbol_file_command (symarg
, 0);
472 if (!SET_TOP_LEVEL ())
473 exec_file_command (execarg
, !batch
);
475 if (!SET_TOP_LEVEL ())
476 symbol_file_command (symarg
, 0);
478 do_cleanups (ALL_CLEANUPS
);
480 /* After the symbol file has been read, print a newline to get us
481 beyond the copyright line... But errors should still set off
482 the error message with a (single) blank line. */
484 printf_filtered ("\n");
485 error_pre_print
= "\n";
486 quit_pre_print
= error_pre_print
;
487 warning_pre_print
= "\nwarning: ";
490 if (!SET_TOP_LEVEL ())
491 core_file_command (corearg
, !batch
);
492 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
493 attach_command (corearg
, !batch
);
494 do_cleanups (ALL_CLEANUPS
);
497 if (!SET_TOP_LEVEL ())
498 tty_command (ttyarg
, !batch
);
499 do_cleanups (ALL_CLEANUPS
);
501 #ifdef ADDITIONAL_OPTION_HANDLER
502 ADDITIONAL_OPTION_HANDLER
;
505 /* Error messages should no longer be distinguished with extra output. */
506 error_pre_print
= NULL
;
507 quit_pre_print
= NULL
;
508 warning_pre_print
= "warning: ";
510 /* Read the .gdbinit file in the current directory, *if* it isn't
511 the same as the $HOME/.gdbinit file (it should exist, also). */
514 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
515 if (!inhibit_gdbinit
&& access (gdbinit
, R_OK
) == 0)
517 if (!SET_TOP_LEVEL ())
518 source_command (gdbinit
, 0);
520 do_cleanups (ALL_CLEANUPS
);
522 for (i
= 0; i
< ncmd
; i
++)
524 if (!SET_TOP_LEVEL ())
526 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
527 read_command_file (stdin
);
529 source_command (cmdarg
[i
], !batch
);
530 do_cleanups (ALL_CLEANUPS
);
535 /* Read in the old history after all the command files have been read. */
540 /* We have hit the end of the batch file. */
544 /* Do any host- or target-specific hacks. This is used for i960 targets
545 to force the user to set a nindy target and spec its parameters. */
547 #ifdef BEFORE_MAIN_LOOP_HOOK
548 BEFORE_MAIN_LOOP_HOOK
;
551 END_PROGRESS (argv
[0]);
553 /* Show time and/or space usage. */
557 long init_time
= get_run_time () - time_at_startup
;
559 printf_unfiltered ("Startup time: %ld.%06ld\n",
560 init_time
/ 1000000, init_time
% 1000000);
565 extern char **environ
;
566 char *lim
= (char *) sbrk (0);
568 printf_unfiltered ("Startup size: data size %ld\n",
569 (long) (lim
- (char *) &environ
));
572 /* The default command loop.
573 The WIN32 Gui calls this main to set up gdb's state, and
574 has its own command loop. */
575 #if !defined (WINGDB)
578 if (!SET_TOP_LEVEL ())
580 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
581 /* GUIs generally have their own command loop, mainloop, or whatever.
582 This is a good place to gain control because many error
583 conditions will end up here via longjmp(). */
584 if (command_loop_hook
)
585 command_loop_hook ();
588 quit_command ((char *)0, instream
== stdin
);
592 /* No exit -- exit is through quit_command. */
603 proc_remove_foreign (pid
)
609 fputs_unfiltered (linebuffer
, stream
)
610 const char *linebuffer
;
613 if (fputs_unfiltered_hook
)
615 /* FIXME: I think we should only be doing this for stdout or stderr.
616 Either that or we should be passing stream to the hook so it can
617 deal with it. If that is cleaned up, this function can go back
618 into utils.c and the fputs_unfiltered_hook can replace the current
619 ability to avoid this function by not linking with main.c. */
620 fputs_unfiltered_hook (linebuffer
, stream
);
624 fputs (linebuffer
, stream
);