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 /* Whether this is the command line version or not */
60 /* Whether xdb commands will be handled */
63 /* Whether dbx commands will be handled */
69 static void print_gdb_help
PARAMS ((GDB_FILE
*));
70 extern void gdb_init
PARAMS ((char *));
72 #include <windows.h> /* for MAX_PATH */
73 #include <sys/cygwin.h> /* for cygwin32_conv_to_posix_path */
85 /* Pointers to various arguments from command line. */
92 /* These are static so that we can take their address in an initializer. */
93 static int print_help
;
94 static int print_version
;
96 /* Pointers to all arguments of --command option. */
98 /* Allocated size of cmdarg. */
100 /* Number of elements of cmdarg used. */
103 /* Indices of all arguments of --directory option. */
105 /* Allocated size. */
107 /* Number of elements used. */
110 struct stat homebuf
, cwdbuf
;
111 char *homedir
, *homeinit
;
115 long time_at_startup
= get_run_time ();
119 START_PROGRESS (argv
[0], 0);
122 /* Do all Mac-specific setup. */
126 /* This needs to happen before the first use of malloc. */
127 init_malloc ((PTR
) NULL
);
129 #if defined (ALIGN_STACK_ON_STARTUP)
130 i
= (int) &count
& 0x3;
135 /* If error() is called from initialization code, just exit */
136 if (SET_TOP_LEVEL ()) {
141 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
144 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
148 line
= (char *) xmalloc (linesize
);
149 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
152 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
153 current_directory
= gdb_dirbuf
;
155 gdb_file_size
= sizeof(GDB_FILE
);
157 gdb_stdout
= (GDB_FILE
*)xmalloc (gdb_file_size
);
158 gdb_stdout
->ts_streamtype
= afile
;
159 gdb_stdout
->ts_filestream
= stdout
;
160 gdb_stdout
->ts_strbuf
= NULL
;
161 gdb_stdout
->ts_buflen
= 0;
163 gdb_stderr
= (GDB_FILE
*)xmalloc (gdb_file_size
);
164 gdb_stderr
->ts_streamtype
= afile
;
165 gdb_stderr
->ts_filestream
= stderr
;
166 gdb_stderr
->ts_strbuf
= NULL
;
167 gdb_stderr
->ts_buflen
= 0;
169 /* Parse arguments and options. */
172 /* When var field is 0, use flag field to record the equivalent
173 short option (or arbitrary numbers starting at 10 for those
174 with no equivalent). */
175 static struct option long_options
[] =
178 {"tui", no_argument
, &tui_version
, 1},
180 {"xdb", no_argument
, &xdb_commands
, 1},
181 {"dbx", no_argument
, &dbx_commands
, 1},
182 {"readnow", no_argument
, &readnow_symbol_files
, 1},
183 {"r", no_argument
, &readnow_symbol_files
, 1},
184 {"mapped", no_argument
, &mapped_symbol_files
, 1},
185 {"m", no_argument
, &mapped_symbol_files
, 1},
186 {"quiet", no_argument
, &quiet
, 1},
187 {"q", no_argument
, &quiet
, 1},
188 {"silent", no_argument
, &quiet
, 1},
189 {"nx", no_argument
, &inhibit_gdbinit
, 1},
190 {"n", no_argument
, &inhibit_gdbinit
, 1},
191 {"batch", no_argument
, &batch
, 1},
192 {"epoch", no_argument
, &epoch_interface
, 1},
194 /* This is a synonym for "--annotate=1". --annotate is now preferred,
195 but keep this here for a long time because people will be running
196 emacses which use --fullname. */
197 {"fullname", no_argument
, 0, 'f'},
198 {"f", no_argument
, 0, 'f'},
200 {"annotate", required_argument
, 0, 12},
201 {"help", no_argument
, &print_help
, 1},
202 {"se", required_argument
, 0, 10},
203 {"symbols", required_argument
, 0, 's'},
204 {"s", required_argument
, 0, 's'},
205 {"exec", required_argument
, 0, 'e'},
206 {"e", required_argument
, 0, 'e'},
207 {"core", required_argument
, 0, 'c'},
208 {"c", required_argument
, 0, 'c'},
209 {"command", required_argument
, 0, 'x'},
210 {"version", no_argument
, &print_version
, 1},
211 {"x", required_argument
, 0, 'x'},
212 /* start-sanitize-gdbtk */
214 {"tclcommand", required_argument
, 0, 'z'},
216 /* end-sanitize-gdbtk */
217 {"directory", required_argument
, 0, 'd'},
218 {"cd", required_argument
, 0, 11},
219 {"tty", required_argument
, 0, 't'},
220 {"baud", required_argument
, 0, 'b'},
221 {"b", required_argument
, 0, 'b'},
222 {"nw", no_argument
, &use_windows
, 0},
223 {"nowindows", no_argument
, &use_windows
, 0},
224 {"w", no_argument
, &use_windows
, 1},
225 {"windows", no_argument
, &use_windows
, 1},
226 {"statistics", no_argument
, 0, 13},
227 /* Allow machine descriptions to add more options... */
228 #ifdef ADDITIONAL_OPTIONS
231 {0, no_argument
, 0, 0}
238 c
= getopt_long_only (argc
, argv
, "",
239 long_options
, &option_index
);
243 /* Long option that takes an argument. */
244 if (c
== 0 && long_options
[option_index
].flag
== 0)
245 c
= long_options
[option_index
].val
;
250 /* Long option that just sets a flag. */
260 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
261 annotation_level
= atoi (optarg
);
264 /* Enable the display of both time and space usage. */
269 annotation_level
= 1;
270 /* We have probably been invoked from emacs. Disable window interface. */
283 cmdarg
[ncmd
++] = optarg
;
287 cmdarg
= (char **) xrealloc ((char *)cmdarg
,
288 cmdsize
* sizeof (*cmdarg
));
291 /* start-sanitize-gdbtk */
295 extern int gdbtk_test
PARAMS ((char *));
296 if (!gdbtk_test (optarg
))
298 fprintf_unfiltered (gdb_stderr
, "%s: unable to load tclcommand file \"%s\"",
305 /* end-sanitize-gdbtk */
307 dirarg
[ndir
++] = optarg
;
311 dirarg
= (char **) xrealloc ((char *)dirarg
,
312 dirsize
* sizeof (*dirarg
));
326 i
= strtol (optarg
, &p
, 0);
327 if (i
== 0 && p
== optarg
)
329 /* Don't use *_filtered or warning() (which relies on
330 current_target) until after initialize_all_files(). */
334 "warning: could not set baud rate to `%s'.\n", optarg
);
343 i
= strtol (optarg
, &p
, 0);
344 if (i
== 0 && p
== optarg
)
346 /* Don't use *_filtered or warning() (which relies on
347 current_target) until after initialize_all_files(). */
351 "warning: could not set timeout limit to `%s'.\n", optarg
);
357 #ifdef ADDITIONAL_OPTION_CASES
358 ADDITIONAL_OPTION_CASES
361 fprintf_unfiltered (gdb_stderr
,
362 "Use `%s --help' for a complete list of options.\n",
368 /* If --help or --version, disable window interface. */
369 if (print_help
|| print_version
)
373 /* Disable the TUI as well. */
379 /* An explicit --tui flag overrides the default UI, which is the
385 /* OK, that's all the options. The other arguments are filenames. */
387 for (; optind
< argc
; optind
++)
391 symarg
= argv
[optind
];
392 execarg
= argv
[optind
];
395 corearg
= argv
[optind
];
398 fprintf_unfiltered (gdb_stderr
,
399 "Excess command line arguments ignored. (%s%s)\n",
400 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
409 init_ui_hook
= tuiInit
;
413 /* Do these (and anything which might call wrap_here or *_filtered)
414 after initialize_all_files. */
417 print_gdb_version (gdb_stdout
);
419 printf_filtered ("\n");
425 print_gdb_help (gdb_stdout
);
426 fputs_unfiltered ("\n", gdb_stdout
);
432 /* Print all the junk at the top, with trailing "..." if we are about
433 to read a symbol file (possibly slowly). */
434 print_gdb_version (gdb_stdout
);
436 printf_filtered ("..");
438 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
441 error_pre_print
= "\n\n";
442 quit_pre_print
= error_pre_print
;
444 /* We may get more than one warning, don't double space all of them... */
445 warning_pre_print
= "\nwarning: ";
447 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
448 *before* all the command line arguments are processed; it sets
449 global parameters, which are independent of what file you are
450 debugging or what directory you are in. */
453 char * tmp
= getenv ("HOME");
457 homedir
= (char *) alloca (MAX_PATH
+1);
458 cygwin32_conv_to_posix_path (tmp
, homedir
);
464 homedir
= getenv ("HOME");
468 homeinit
= (char *) alloca (strlen (homedir
) +
469 strlen (gdbinit
) + 10);
470 strcpy (homeinit
, homedir
);
471 strcat (homeinit
, "/");
472 strcat (homeinit
, gdbinit
);
474 if (!inhibit_gdbinit
)
476 if (!SET_TOP_LEVEL ())
477 source_command (homeinit
, 0);
479 do_cleanups (ALL_CLEANUPS
);
481 /* Do stats; no need to do them elsewhere since we'll only
482 need them if homedir is set. Make sure that they are
483 zero in case one of them fails (this guarantees that they
484 won't match if either exists). */
486 memset (&homebuf
, 0, sizeof (struct stat
));
487 memset (&cwdbuf
, 0, sizeof (struct stat
));
489 stat (homeinit
, &homebuf
);
490 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
494 /* Now perform all the actions indicated by the arguments. */
497 if (!SET_TOP_LEVEL ())
499 cd_command (cdarg
, 0);
502 do_cleanups (ALL_CLEANUPS
);
504 for (i
= 0; i
< ndir
; i
++)
505 if (!SET_TOP_LEVEL ())
506 directory_command (dirarg
[i
], 0);
508 do_cleanups (ALL_CLEANUPS
);
512 && STREQ (execarg
, symarg
))
514 /* The exec file and the symbol-file are the same. If we can't open
515 it, better only print one error message. */
516 if (!SET_TOP_LEVEL ())
518 exec_file_command (execarg
, !batch
);
519 symbol_file_command (symarg
, 0);
525 if (!SET_TOP_LEVEL ())
526 exec_file_command (execarg
, !batch
);
528 if (!SET_TOP_LEVEL ())
529 symbol_file_command (symarg
, 0);
531 do_cleanups (ALL_CLEANUPS
);
533 /* After the symbol file has been read, print a newline to get us
534 beyond the copyright line... But errors should still set off
535 the error message with a (single) blank line. */
537 printf_filtered ("\n");
538 error_pre_print
= "\n";
539 quit_pre_print
= error_pre_print
;
540 warning_pre_print
= "\nwarning: ";
544 if (!SET_TOP_LEVEL ())
545 core_file_command (corearg
, !batch
);
546 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
547 attach_command (corearg
, !batch
);
549 do_cleanups (ALL_CLEANUPS
);
552 if (!SET_TOP_LEVEL ())
553 tty_command (ttyarg
, !batch
);
554 do_cleanups (ALL_CLEANUPS
);
556 #ifdef ADDITIONAL_OPTION_HANDLER
557 ADDITIONAL_OPTION_HANDLER
;
560 /* Error messages should no longer be distinguished with extra output. */
561 error_pre_print
= NULL
;
562 quit_pre_print
= NULL
;
563 warning_pre_print
= "warning: ";
565 /* Read the .gdbinit file in the current directory, *if* it isn't
566 the same as the $HOME/.gdbinit file (it should exist, also). */
569 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
570 if (!inhibit_gdbinit
)
572 if (!SET_TOP_LEVEL ())
573 source_command (gdbinit
, 0);
575 do_cleanups (ALL_CLEANUPS
);
577 for (i
= 0; i
< ncmd
; i
++)
579 if (!SET_TOP_LEVEL ())
581 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
582 read_command_file (stdin
);
584 source_command (cmdarg
[i
], !batch
);
585 do_cleanups (ALL_CLEANUPS
);
590 /* Read in the old history after all the command files have been read. */
595 /* We have hit the end of the batch file. */
599 /* Do any host- or target-specific hacks. This is used for i960 targets
600 to force the user to set a nindy target and spec its parameters. */
602 #ifdef BEFORE_MAIN_LOOP_HOOK
603 BEFORE_MAIN_LOOP_HOOK
;
606 END_PROGRESS (argv
[0]);
608 /* Show time and/or space usage. */
612 long init_time
= get_run_time () - time_at_startup
;
614 printf_unfiltered ("Startup time: %ld.%06ld\n",
615 init_time
/ 1000000, init_time
% 1000000);
621 extern char **environ
;
622 char *lim
= (char *) sbrk (0);
624 printf_unfiltered ("Startup size: data size %ld\n",
625 (long) (lim
- (char *) &environ
));
629 /* The default command loop.
630 The WIN32 Gui calls this main to set up gdb's state, and
631 has its own command loop. */
632 #if !defined _WIN32 || defined __GNUC__
635 if (!SET_TOP_LEVEL ())
637 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
638 /* GUIs generally have their own command loop, mainloop, or whatever.
639 This is a good place to gain control because many error
640 conditions will end up here via longjmp(). */
641 if (command_loop_hook
)
642 command_loop_hook ();
645 quit_command ((char *)0, instream
== stdin
);
649 /* No exit -- exit is through quit_command. */
654 /* Don't use *_filtered for printing help. We don't want to prompt
655 for continue no matter how small the screen or how much we're going
659 print_gdb_help (stream
)
663 This is the GNU debugger. Usage:\n\n\
664 gdb [options] [executable-file [core-file or process-id]]\n\n\
668 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
669 --batch Exit after processing options.\n\
670 --cd=DIR Change current directory to DIR.\n\
671 --command=FILE Execute GDB commands from FILE.\n\
672 --core=COREFILE Analyze the core dump COREFILE.\n\
675 --dbx DBX compatibility mode.\n\
676 --directory=DIR Search for source files in DIR.\n\
677 --epoch Output information used by epoch emacs-GDB interface.\n\
678 --exec=EXECFILE Use EXECFILE as the executable.\n\
679 --fullname Output information used by emacs-GDB interface.\n\
680 --help Print this message.\n\
683 --mapped Use mapped symbol files if supported on this system.\n\
684 --nw Do not use a window interface.\n\
685 --nx Do not read .gdbinit file.\n\
686 --quiet Do not print version number on startup.\n\
687 --readnow Fully read symbol files on first access.\n\
690 --se=FILE Use FILE as symbol file and executable file.\n\
691 --symbols=SYMFILE Read symbols from SYMFILE.\n\
692 --tty=TTY Use TTY for input/output by the program being debugged.\n\
696 --tui Use a terminal user interface.\n\
700 --version Print version information and then exit.\n\
701 --xdb XDB compatibility mode.\n\
703 #ifdef ADDITIONAL_OPTION_HELP
704 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, stream
);
706 fputs_unfiltered ("\n\
707 For more information, type \"help\" from within GDB, or consult the\n\
708 GDB manual (available as on-line info or a printed manual).\n\
709 Report bugs to \"bug-gdb@prep.ai.mit.edu\".\
720 proc_remove_foreign (pid
)
725 /* All I/O sent to the *_filtered and *_unfiltered functions eventually ends up
726 here. The fputs_unfiltered_hook is primarily used by GUIs to collect all
727 output and send it to the GUI, instead of the controlling terminal. Only
728 output to gdb_stdout and gdb_stderr are sent to the hook. Everything else
729 is sent on to fputs to allow file I/O to be handled appropriately. */
732 fputs_unfiltered (linebuffer
, stream
)
733 const char *linebuffer
;
737 extern int tui_owns_terminal
;
739 /* If anything (GUI, TUI) wants to capture GDB output, this is
740 * the place... the way to do it is to set up
741 * fputs_unfiltered_hook.
742 * Our TUI ("gdb -tui") used to hook output, but in the
743 * new (XDB style) scheme, we do not do that anymore... - RT
745 if (fputs_unfiltered_hook
746 && (stream
== gdb_stdout
747 || stream
== gdb_stderr
))
748 fputs_unfiltered_hook (linebuffer
, stream
);
752 if (tui_version
&& tui_owns_terminal
) {
753 /* If we get here somehow while updating the TUI (from
754 * within a tuiDo(), then we need to temporarily
755 * set up the terminal for GDB output. This probably just
756 * happens on error output.
759 if (stream
->ts_streamtype
== astring
) {
760 gdb_file_adjust_strbuf(strlen(linebuffer
), stream
);
761 strcat(stream
->ts_strbuf
, linebuffer
);
763 tuiTermUnsetup(0, (tui_version
) ? cmdWin
->detail
.commandInfo
.curch
: 0);
764 fputs (linebuffer
, stream
->ts_filestream
);
766 if (linebuffer
[strlen(linebuffer
) - 1] == '\n')
767 tuiClearCommandCharCount();
769 tuiIncrCommandCharCountBy(strlen(linebuffer
));
772 /* The normal case - just do a fputs() */
773 if (stream
->ts_streamtype
== astring
) {
774 gdb_file_adjust_strbuf(strlen(linebuffer
), stream
);
775 strcat(stream
->ts_strbuf
, linebuffer
);
776 } else fputs (linebuffer
, stream
->ts_filestream
);
781 if (stream
->ts_streamtype
== astring
) {
782 gdb_file_adjust_strbuf(strlen(linebuffer
), stream
);
783 strcat(stream
->ts_strbuf
, linebuffer
);
784 } else fputs (linebuffer
, stream
->ts_filestream
);