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"
36 /* Temporary variable for SET_TOP_LEVEL. */
38 static int top_level_val
;
40 /* Do a setjmp on error_return and quit_return. catch_errors is
41 generally a cleaner way to do this, but main() would look pretty
42 ugly if it had to use catch_errors each time. */
44 #define SET_TOP_LEVEL() \
45 (((top_level_val = SIGSETJMP (error_return)) \
46 ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (SIGJMP_BUF))) \
49 /* If nonzero, display time usage both at startup and for each command. */
53 /* If nonzero, display space usage both at startup and for each command. */
57 static void print_gdb_help
PARAMS ((GDB_FILE
*));
58 extern void gdb_init
PARAMS ((char *));
60 #include <windows.h> /* for MAX_PATH */
61 #include <sys/cygwin.h> /* for cygwin_conv_to_posix_path */
73 /* Pointers to various arguments from command line. */
80 /* These are static so that we can take their address in an initializer. */
81 static int print_help
;
82 static int print_version
;
84 /* Pointers to all arguments of --command option. */
86 /* Allocated size of cmdarg. */
88 /* Number of elements of cmdarg used. */
91 /* Indices of all arguments of --directory option. */
95 /* Number of elements used. */
98 struct stat homebuf
, cwdbuf
;
99 char *homedir
, *homeinit
;
103 long time_at_startup
= get_run_time ();
105 START_PROGRESS (argv
[0], 0);
108 /* Do all Mac-specific setup. */
112 /* This needs to happen before the first use of malloc. */
113 init_malloc ((PTR
) NULL
);
115 #if defined (ALIGN_STACK_ON_STARTUP)
116 i
= (int) &count
& 0x3;
121 /* If error() is called from initialization code, just exit */
122 if (SET_TOP_LEVEL ()) {
127 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
130 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
134 line
= (char *) xmalloc (linesize
);
135 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
138 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
139 current_directory
= gdb_dirbuf
;
141 /* Parse arguments and options. */
144 /* When var field is 0, use flag field to record the equivalent
145 short option (or arbitrary numbers starting at 10 for those
146 with no equivalent). */
147 static struct option long_options
[] =
149 {"readnow", no_argument
, &readnow_symbol_files
, 1},
150 {"r", no_argument
, &readnow_symbol_files
, 1},
151 {"mapped", no_argument
, &mapped_symbol_files
, 1},
152 {"m", no_argument
, &mapped_symbol_files
, 1},
153 {"quiet", no_argument
, &quiet
, 1},
154 {"q", no_argument
, &quiet
, 1},
155 {"silent", no_argument
, &quiet
, 1},
156 {"nx", no_argument
, &inhibit_gdbinit
, 1},
157 {"n", no_argument
, &inhibit_gdbinit
, 1},
158 {"batch", no_argument
, &batch
, 1},
159 {"epoch", no_argument
, &epoch_interface
, 1},
161 /* This is a synonym for "--annotate=1". --annotate is now preferred,
162 but keep this here for a long time because people will be running
163 emacses which use --fullname. */
164 {"fullname", no_argument
, 0, 'f'},
165 {"f", no_argument
, 0, 'f'},
167 {"annotate", required_argument
, 0, 12},
168 {"help", no_argument
, &print_help
, 1},
169 {"se", required_argument
, 0, 10},
170 {"symbols", required_argument
, 0, 's'},
171 {"s", required_argument
, 0, 's'},
172 {"exec", required_argument
, 0, 'e'},
173 {"e", required_argument
, 0, 'e'},
174 {"core", required_argument
, 0, 'c'},
175 {"c", required_argument
, 0, 'c'},
176 {"command", required_argument
, 0, 'x'},
177 {"version", no_argument
, &print_version
, 1},
178 {"x", required_argument
, 0, 'x'},
179 /* start-sanitize-gdbtk */
181 {"tclcommand", required_argument
, 0, 'z'},
183 /* end-sanitize-gdbtk */
184 {"directory", required_argument
, 0, 'd'},
185 {"cd", required_argument
, 0, 11},
186 {"tty", required_argument
, 0, 't'},
187 {"baud", required_argument
, 0, 'b'},
188 {"b", required_argument
, 0, 'b'},
189 {"nw", no_argument
, &use_windows
, 0},
190 {"nowindows", no_argument
, &use_windows
, 0},
191 {"w", no_argument
, &use_windows
, 1},
192 {"windows", no_argument
, &use_windows
, 1},
193 {"statistics", no_argument
, 0, 13},
194 /* Allow machine descriptions to add more options... */
195 #ifdef ADDITIONAL_OPTIONS
198 {0, no_argument
, 0, 0}
205 c
= getopt_long_only (argc
, argv
, "",
206 long_options
, &option_index
);
210 /* Long option that takes an argument. */
211 if (c
== 0 && long_options
[option_index
].flag
== 0)
212 c
= long_options
[option_index
].val
;
217 /* Long option that just sets a flag. */
227 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
228 annotation_level
= atoi (optarg
);
231 /* Enable the display of both time and space usage. */
236 annotation_level
= 1;
237 /* We have probably been invoked from emacs. Disable window interface. */
250 cmdarg
[ncmd
++] = optarg
;
254 cmdarg
= (char **) xrealloc ((char *)cmdarg
,
255 cmdsize
* sizeof (*cmdarg
));
258 /* start-sanitize-gdbtk */
262 extern int gdbtk_test
PARAMS ((char *));
263 if (!gdbtk_test (optarg
))
265 fprintf_unfiltered (gdb_stderr
, "%s: unable to load tclcommand file \"%s\"",
272 /* end-sanitize-gdbtk */
274 dirarg
[ndir
++] = optarg
;
278 dirarg
= (char **) xrealloc ((char *)dirarg
,
279 dirsize
* sizeof (*dirarg
));
293 i
= strtol (optarg
, &p
, 0);
294 if (i
== 0 && p
== optarg
)
296 /* Don't use *_filtered or warning() (which relies on
297 current_target) until after initialize_all_files(). */
301 "warning: could not set baud rate to `%s'.\n", optarg
);
310 i
= strtol (optarg
, &p
, 0);
311 if (i
== 0 && p
== optarg
)
313 /* Don't use *_filtered or warning() (which relies on
314 current_target) until after initialize_all_files(). */
318 "warning: could not set timeout limit to `%s'.\n", optarg
);
324 #ifdef ADDITIONAL_OPTION_CASES
325 ADDITIONAL_OPTION_CASES
328 fprintf_unfiltered (gdb_stderr
,
329 "Use `%s --help' for a complete list of options.\n",
335 /* If --help or --version, disable window interface. */
336 if (print_help
|| print_version
)
339 /* OK, that's all the options. The other arguments are filenames. */
341 for (; optind
< argc
; optind
++)
345 symarg
= argv
[optind
];
346 execarg
= argv
[optind
];
349 corearg
= argv
[optind
];
352 fprintf_unfiltered (gdb_stderr
,
353 "Excess command line arguments ignored. (%s%s)\n",
354 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
363 /* Do these (and anything which might call wrap_here or *_filtered)
364 after initialize_all_files. */
367 print_gdb_version (gdb_stdout
);
369 printf_filtered ("\n");
375 print_gdb_help (gdb_stdout
);
376 fputs_unfiltered ("\n", gdb_stdout
);
382 /* Print all the junk at the top, with trailing "..." if we are about
383 to read a symbol file (possibly slowly). */
384 print_gdb_version (gdb_stdout
);
386 printf_filtered ("..");
388 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
391 error_pre_print
= "\n\n";
392 quit_pre_print
= error_pre_print
;
394 /* We may get more than one warning, don't double space all of them... */
395 warning_pre_print
= "\nwarning: ";
397 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
398 *before* all the command line arguments are processed; it sets
399 global parameters, which are independent of what file you are
400 debugging or what directory you are in. */
403 char * tmp
= getenv ("HOME");
407 homedir
= (char *) alloca (MAX_PATH
+1);
408 cygwin_conv_to_posix_path (tmp
, homedir
);
414 homedir
= getenv ("HOME");
418 homeinit
= (char *) alloca (strlen (homedir
) +
419 strlen (gdbinit
) + 10);
420 strcpy (homeinit
, homedir
);
421 strcat (homeinit
, "/");
422 strcat (homeinit
, gdbinit
);
424 if (!inhibit_gdbinit
)
426 if (!SET_TOP_LEVEL ())
427 source_command (homeinit
, 0);
429 do_cleanups (ALL_CLEANUPS
);
431 /* Do stats; no need to do them elsewhere since we'll only
432 need them if homedir is set. Make sure that they are
433 zero in case one of them fails (this guarantees that they
434 won't match if either exists). */
436 memset (&homebuf
, 0, sizeof (struct stat
));
437 memset (&cwdbuf
, 0, sizeof (struct stat
));
439 stat (homeinit
, &homebuf
);
440 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
444 /* Now perform all the actions indicated by the arguments. */
447 if (!SET_TOP_LEVEL ())
449 cd_command (cdarg
, 0);
452 do_cleanups (ALL_CLEANUPS
);
454 for (i
= 0; i
< ndir
; i
++)
455 if (!SET_TOP_LEVEL ())
456 directory_command (dirarg
[i
], 0);
458 do_cleanups (ALL_CLEANUPS
);
462 && STREQ (execarg
, symarg
))
464 /* The exec file and the symbol-file are the same. If we can't open
465 it, better only print one error message. */
466 if (!SET_TOP_LEVEL ())
468 exec_file_command (execarg
, !batch
);
469 symbol_file_command (symarg
, 0);
475 if (!SET_TOP_LEVEL ())
476 exec_file_command (execarg
, !batch
);
478 if (!SET_TOP_LEVEL ())
479 symbol_file_command (symarg
, 0);
481 do_cleanups (ALL_CLEANUPS
);
483 /* After the symbol file has been read, print a newline to get us
484 beyond the copyright line... But errors should still set off
485 the error message with a (single) blank line. */
487 printf_filtered ("\n");
488 error_pre_print
= "\n";
489 quit_pre_print
= error_pre_print
;
490 warning_pre_print
= "\nwarning: ";
493 if (!SET_TOP_LEVEL ())
494 core_file_command (corearg
, !batch
);
495 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
496 attach_command (corearg
, !batch
);
497 do_cleanups (ALL_CLEANUPS
);
500 if (!SET_TOP_LEVEL ())
501 tty_command (ttyarg
, !batch
);
502 do_cleanups (ALL_CLEANUPS
);
504 #ifdef ADDITIONAL_OPTION_HANDLER
505 ADDITIONAL_OPTION_HANDLER
;
508 /* Error messages should no longer be distinguished with extra output. */
509 error_pre_print
= NULL
;
510 quit_pre_print
= NULL
;
511 warning_pre_print
= "warning: ";
513 /* Read the .gdbinit file in the current directory, *if* it isn't
514 the same as the $HOME/.gdbinit file (it should exist, also). */
517 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
518 if (!inhibit_gdbinit
)
520 if (!SET_TOP_LEVEL ())
521 source_command (gdbinit
, 0);
523 do_cleanups (ALL_CLEANUPS
);
525 for (i
= 0; i
< ncmd
; i
++)
527 if (!SET_TOP_LEVEL ())
529 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
530 read_command_file (stdin
);
532 source_command (cmdarg
[i
], !batch
);
533 do_cleanups (ALL_CLEANUPS
);
538 /* Read in the old history after all the command files have been read. */
543 /* We have hit the end of the batch file. */
547 /* Do any host- or target-specific hacks. This is used for i960 targets
548 to force the user to set a nindy target and spec its parameters. */
550 #ifdef BEFORE_MAIN_LOOP_HOOK
551 BEFORE_MAIN_LOOP_HOOK
;
554 END_PROGRESS (argv
[0]);
556 /* Show time and/or space usage. */
560 long init_time
= get_run_time () - time_at_startup
;
562 printf_unfiltered ("Startup time: %ld.%06ld\n",
563 init_time
/ 1000000, init_time
% 1000000);
569 extern char **environ
;
570 char *lim
= (char *) sbrk (0);
572 printf_unfiltered ("Startup size: data size %ld\n",
573 (long) (lim
- (char *) &environ
));
577 /* The default command loop.
578 The WIN32 Gui calls this main to set up gdb's state, and
579 has its own command loop. */
580 #if !defined _WIN32 || defined __GNUC__
583 if (!SET_TOP_LEVEL ())
585 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
586 /* GUIs generally have their own command loop, mainloop, or whatever.
587 This is a good place to gain control because many error
588 conditions will end up here via longjmp(). */
589 if (command_loop_hook
)
590 command_loop_hook ();
593 quit_command ((char *)0, instream
== stdin
);
597 /* No exit -- exit is through quit_command. */
602 /* Don't use *_filtered for printing help. We don't want to prompt
603 for continue no matter how small the screen or how much we're going
607 print_gdb_help (stream
)
611 This is the GNU debugger. Usage:\n\n\
612 gdb [options] [executable-file [core-file or process-id]]\n\n\
616 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
617 --batch Exit after processing options.\n\
618 --cd=DIR Change current directory to DIR.\n\
619 --command=FILE Execute GDB commands from FILE.\n\
620 --core=COREFILE Analyze the core dump COREFILE.\n\
623 --directory=DIR Search for source files in DIR.\n\
624 --epoch Output information used by epoch emacs-GDB interface.\n\
625 --exec=EXECFILE Use EXECFILE as the executable.\n\
626 --fullname Output information used by emacs-GDB interface.\n\
627 --help Print this message.\n\
630 --mapped Use mapped symbol files if supported on this system.\n\
631 --nw Do not use a window interface.\n\
632 --nx Do not read .gdbinit file.\n\
633 --quiet Do not print version number on startup.\n\
634 --readnow Fully read symbol files on first access.\n\
637 --se=FILE Use FILE as symbol file and executable file.\n\
638 --symbols=SYMFILE Read symbols from SYMFILE.\n\
639 --tty=TTY Use TTY for input/output by the program being debugged.\n\
640 --version Print version information and then exit.\n\
642 #ifdef ADDITIONAL_OPTION_HELP
643 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, stream
);
645 fputs_unfiltered ("\n\
646 For more information, type \"help\" from within GDB, or consult the\n\
647 GDB manual (available as on-line info or a printed manual).\n\
648 Report bugs to \"bug-gdb@prep.ai.mit.edu\".\
659 proc_remove_foreign (pid
)
664 /* All I/O sent to the *_filtered and *_unfiltered functions eventually ends up
665 here. The fputs_unfiltered_hook is primarily used by GUIs to collect all
666 output and send it to the GUI, instead of the controlling terminal. Only
667 output to gdb_stdout and gdb_stderr are sent to the hook. Everything else
668 is sent on to fputs to allow file I/O to be handled appropriately. */
671 fputs_unfiltered (linebuffer
, stream
)
672 const char *linebuffer
;
675 if (fputs_unfiltered_hook
676 && (stream
== gdb_stdout
677 || stream
== gdb_stderr
))
678 fputs_unfiltered_hook (linebuffer
, stream
);
680 fputs (linebuffer
, stream
);