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. */
23 #include "event-loop.h"
26 #include "call-cmds.h"
33 #include <sys/types.h>
37 #include "gdb_string.h"
39 /* If nonzero, display time usage both at startup and for each command. */
43 /* If nonzero, display space usage both at startup and for each command. */
47 /* Whether this is the async version or not. The async version is
48 invoked on the command line with the -nw --async options. In this
49 version, the usual command_loop is substituted by and event loop which
50 processes UI events asynchronously. */
53 /* Whether this is the command line version or not */
56 /* Whether xdb commands will be handled */
59 /* Whether dbx commands will be handled */
65 /* Whether to enable writing into executable and core files */
66 extern int write_files
;
68 static void print_gdb_help
PARAMS ((GDB_FILE
*));
70 /* These two are used to set the external editor commands when gdb is farming
71 out files to be edited by another program. */
73 extern int enable_external_editor
;
74 extern char * external_editor_command
;
77 #include <windows.h> /* for MAX_PATH */
78 #include <sys/cygwin.h> /* for cygwin32_conv_to_posix_path */
90 /* Pointers to various arguments from command line. */
97 /* These are static so that we can take their address in an initializer. */
98 static int print_help
;
99 static int print_version
;
101 /* Pointers to all arguments of --command option. */
103 /* Allocated size of cmdarg. */
105 /* Number of elements of cmdarg used. */
108 /* Indices of all arguments of --directory option. */
110 /* Allocated size. */
112 /* Number of elements used. */
115 struct stat homebuf
, cwdbuf
;
116 char *homedir
, *homeinit
;
120 long time_at_startup
= get_run_time ();
124 START_PROGRESS (argv
[0], 0);
127 /* Do all Mac-specific setup. */
131 /* This needs to happen before the first use of malloc. */
132 init_malloc ((PTR
) NULL
);
134 #if defined (ALIGN_STACK_ON_STARTUP)
135 i
= (int) &count
& 0x3;
140 /* If error() is called from initialization code, just exit */
141 if (SET_TOP_LEVEL ()) {
146 cmdarg
= (char **) xmalloc (cmdsize
* sizeof (*cmdarg
));
149 dirarg
= (char **) xmalloc (dirsize
* sizeof (*dirarg
));
153 line
= (char *) xmalloc (linesize
);
154 line
[0] = '\0'; /* Terminate saved (now empty) cmd line */
157 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
158 current_directory
= gdb_dirbuf
;
162 gdb_stdout
= stdio_fileopen (stdout
);
163 gdb_stderr
= stdio_fileopen (stderr
);
165 gdb_stdout
= tui_fileopen (stdout
);
166 gdb_stderr
= tui_fileopen (stderr
);
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
[] =
177 {"async", no_argument
, &async
, 1},
179 {"tui", no_argument
, &tui_version
, 1},
181 {"xdb", no_argument
, &xdb_commands
, 1},
182 {"dbx", no_argument
, &dbx_commands
, 1},
183 {"readnow", no_argument
, &readnow_symbol_files
, 1},
184 {"r", no_argument
, &readnow_symbol_files
, 1},
185 {"mapped", no_argument
, &mapped_symbol_files
, 1},
186 {"m", no_argument
, &mapped_symbol_files
, 1},
187 {"quiet", no_argument
, &quiet
, 1},
188 {"q", no_argument
, &quiet
, 1},
189 {"silent", no_argument
, &quiet
, 1},
190 {"nx", no_argument
, &inhibit_gdbinit
, 1},
191 {"n", no_argument
, &inhibit_gdbinit
, 1},
192 {"batch", no_argument
, &batch
, 1},
193 {"epoch", no_argument
, &epoch_interface
, 1},
195 /* This is a synonym for "--annotate=1". --annotate is now preferred,
196 but keep this here for a long time because people will be running
197 emacses which use --fullname. */
198 {"fullname", no_argument
, 0, 'f'},
199 {"f", no_argument
, 0, 'f'},
201 {"annotate", required_argument
, 0, 12},
202 {"help", no_argument
, &print_help
, 1},
203 {"se", required_argument
, 0, 10},
204 {"symbols", required_argument
, 0, 's'},
205 {"s", required_argument
, 0, 's'},
206 {"exec", required_argument
, 0, 'e'},
207 {"e", required_argument
, 0, 'e'},
208 {"core", required_argument
, 0, 'c'},
209 {"c", required_argument
, 0, 'c'},
210 {"command", required_argument
, 0, 'x'},
211 {"version", no_argument
, &print_version
, 1},
212 {"x", required_argument
, 0, 'x'},
213 {"directory", required_argument
, 0, 'd'},
214 {"cd", required_argument
, 0, 11},
215 {"tty", required_argument
, 0, 't'},
216 {"baud", required_argument
, 0, 'b'},
217 {"b", required_argument
, 0, 'b'},
218 {"nw", no_argument
, &use_windows
, 0},
219 {"nowindows", no_argument
, &use_windows
, 0},
220 {"w", no_argument
, &use_windows
, 1},
221 {"windows", no_argument
, &use_windows
, 1},
222 {"statistics", no_argument
, 0, 13},
223 {"write", no_argument
, &write_files
, 1},
224 /* Allow machine descriptions to add more options... */
225 #ifdef ADDITIONAL_OPTIONS
228 {0, no_argument
, 0, 0}
235 c
= getopt_long_only (argc
, argv
, "",
236 long_options
, &option_index
);
240 /* Long option that takes an argument. */
241 if (c
== 0 && long_options
[option_index
].flag
== 0)
242 c
= long_options
[option_index
].val
;
247 /* Long option that just sets a flag. */
257 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
258 annotation_level
= atoi (optarg
);
261 /* Enable the display of both time and space usage. */
266 annotation_level
= 1;
267 /* We have probably been invoked from emacs. Disable window interface. */
280 cmdarg
[ncmd
++] = optarg
;
284 cmdarg
= (char **) xrealloc ((char *)cmdarg
,
285 cmdsize
* sizeof (*cmdarg
));
289 dirarg
[ndir
++] = optarg
;
293 dirarg
= (char **) xrealloc ((char *)dirarg
,
294 dirsize
* sizeof (*dirarg
));
308 i
= strtol (optarg
, &p
, 0);
309 if (i
== 0 && p
== optarg
)
311 /* Don't use *_filtered or warning() (which relies on
312 current_target) until after initialize_all_files(). */
316 "warning: could not set baud rate to `%s'.\n", optarg
);
325 i
= strtol (optarg
, &p
, 0);
326 if (i
== 0 && p
== optarg
)
328 /* Don't use *_filtered or warning() (which relies on
329 current_target) until after initialize_all_files(). */
333 "warning: could not set timeout limit to `%s'.\n", optarg
);
339 #ifdef ADDITIONAL_OPTION_CASES
340 ADDITIONAL_OPTION_CASES
343 fprintf_unfiltered (gdb_stderr
,
344 "Use `%s --help' for a complete list of options.\n",
350 /* If --help or --version, disable window interface. */
351 if (print_help
|| print_version
)
355 /* Disable the TUI as well. */
361 /* An explicit --tui flag overrides the default UI, which is the
367 /* OK, that's all the options. The other arguments are filenames. */
369 for (; optind
< argc
; optind
++)
373 symarg
= argv
[optind
];
374 execarg
= argv
[optind
];
377 corearg
= argv
[optind
];
380 fprintf_unfiltered (gdb_stderr
,
381 "Excess command line arguments ignored. (%s%s)\n",
382 argv
[optind
], (optind
== argc
- 1) ? "" : " ...");
389 /* Get ready to invoke the event loop instead of the
390 command_loop. See event-loop.h for more details.*/
392 async_hook
= setup_event_loop
;
395 init_ui_hook
= tuiInit
;
399 /* Do these (and anything which might call wrap_here or *_filtered)
400 after initialize_all_files. */
403 print_gdb_version (gdb_stdout
);
405 printf_filtered ("\n");
411 print_gdb_help (gdb_stdout
);
412 fputs_unfiltered ("\n", gdb_stdout
);
418 /* Print all the junk at the top, with trailing "..." if we are about
419 to read a symbol file (possibly slowly). */
420 print_gdb_version (gdb_stdout
);
422 printf_filtered ("..");
424 gdb_flush (gdb_stdout
); /* Force to screen during slow operations */
427 error_pre_print
= "\n\n";
428 quit_pre_print
= error_pre_print
;
430 /* We may get more than one warning, don't double space all of them... */
431 warning_pre_print
= "\nwarning: ";
433 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
434 *before* all the command line arguments are processed; it sets
435 global parameters, which are independent of what file you are
436 debugging or what directory you are in. */
439 char * tmp
= getenv ("HOME");
443 homedir
= (char *) alloca (MAX_PATH
+1);
444 cygwin32_conv_to_posix_path (tmp
, homedir
);
450 homedir
= getenv ("HOME");
454 homeinit
= (char *) alloca (strlen (homedir
) +
455 strlen (gdbinit
) + 10);
456 strcpy (homeinit
, homedir
);
457 strcat (homeinit
, "/");
458 strcat (homeinit
, gdbinit
);
460 if (!inhibit_gdbinit
)
462 if (!SET_TOP_LEVEL ())
463 source_command (homeinit
, 0);
465 do_cleanups (ALL_CLEANUPS
);
467 /* Do stats; no need to do them elsewhere since we'll only
468 need them if homedir is set. Make sure that they are
469 zero in case one of them fails (this guarantees that they
470 won't match if either exists). */
472 memset (&homebuf
, 0, sizeof (struct stat
));
473 memset (&cwdbuf
, 0, sizeof (struct stat
));
475 stat (homeinit
, &homebuf
);
476 stat (gdbinit
, &cwdbuf
); /* We'll only need this if
480 /* Now perform all the actions indicated by the arguments. */
483 if (!SET_TOP_LEVEL ())
485 cd_command (cdarg
, 0);
488 do_cleanups (ALL_CLEANUPS
);
490 for (i
= 0; i
< ndir
; i
++)
491 if (!SET_TOP_LEVEL ())
492 directory_command (dirarg
[i
], 0);
494 do_cleanups (ALL_CLEANUPS
);
498 && STREQ (execarg
, symarg
))
500 /* The exec file and the symbol-file are the same. If we can't open
501 it, better only print one error message. */
502 if (!SET_TOP_LEVEL ())
504 exec_file_command (execarg
, !batch
);
505 symbol_file_command (symarg
, 0);
511 if (!SET_TOP_LEVEL ())
512 exec_file_command (execarg
, !batch
);
514 if (!SET_TOP_LEVEL ())
515 symbol_file_command (symarg
, 0);
517 do_cleanups (ALL_CLEANUPS
);
519 /* After the symbol file has been read, print a newline to get us
520 beyond the copyright line... But errors should still set off
521 the error message with a (single) blank line. */
523 printf_filtered ("\n");
524 error_pre_print
= "\n";
525 quit_pre_print
= error_pre_print
;
526 warning_pre_print
= "\nwarning: ";
530 if (!SET_TOP_LEVEL ())
531 core_file_command (corearg
, !batch
);
532 else if (isdigit (corearg
[0]) && !SET_TOP_LEVEL ())
533 attach_command (corearg
, !batch
);
535 do_cleanups (ALL_CLEANUPS
);
538 if (!SET_TOP_LEVEL ())
539 tty_command (ttyarg
, !batch
);
540 do_cleanups (ALL_CLEANUPS
);
542 #ifdef ADDITIONAL_OPTION_HANDLER
543 ADDITIONAL_OPTION_HANDLER
;
546 /* Error messages should no longer be distinguished with extra output. */
547 error_pre_print
= NULL
;
548 quit_pre_print
= NULL
;
549 warning_pre_print
= "warning: ";
551 /* Read the .gdbinit file in the current directory, *if* it isn't
552 the same as the $HOME/.gdbinit file (it should exist, also). */
555 || memcmp ((char *) &homebuf
, (char *) &cwdbuf
, sizeof (struct stat
)))
556 if (!inhibit_gdbinit
)
558 if (!SET_TOP_LEVEL ())
559 source_command (gdbinit
, 0);
561 do_cleanups (ALL_CLEANUPS
);
563 for (i
= 0; i
< ncmd
; i
++)
565 if (!SET_TOP_LEVEL ())
567 /* NOTE: I am commenting this out, because it is not clear
568 where this feature is used. It is very old and
569 undocumented. ezannoni: 1999-05-04*/
571 if (cmdarg
[i
][0] == '-' && cmdarg
[i
][1] == '\0')
572 read_command_file (stdin
);
575 source_command (cmdarg
[i
], !batch
);
576 do_cleanups (ALL_CLEANUPS
);
581 /* Read in the old history after all the command files have been read. */
586 /* We have hit the end of the batch file. */
590 /* Do any host- or target-specific hacks. This is used for i960 targets
591 to force the user to set a nindy target and spec its parameters. */
593 #ifdef BEFORE_MAIN_LOOP_HOOK
594 BEFORE_MAIN_LOOP_HOOK
;
597 END_PROGRESS (argv
[0]);
599 /* Show time and/or space usage. */
603 long init_time
= get_run_time () - time_at_startup
;
605 printf_unfiltered ("Startup time: %ld.%06ld\n",
606 init_time
/ 1000000, init_time
% 1000000);
612 extern char **environ
;
613 char *lim
= (char *) sbrk (0);
615 printf_unfiltered ("Startup size: data size %ld\n",
616 (long) (lim
- (char *) &environ
));
620 /* Call the event loop, if gdb was invoked with the --async
621 option. Control will never get back to this file, if the event
622 loop is invoked. See the files event-*.[ch] for details. */
626 /* The default command loop.
627 The WIN32 Gui calls this main to set up gdb's state, and
628 has its own command loop. */
629 #if !defined _WIN32 || defined __GNUC__
632 if (!SET_TOP_LEVEL ())
634 do_cleanups (ALL_CLEANUPS
); /* Do complete cleanup */
635 /* GUIs generally have their own command loop, mainloop, or whatever.
636 This is a good place to gain control because many error
637 conditions will end up here via longjmp(). */
638 if (command_loop_hook
)
639 command_loop_hook ();
642 quit_command ((char *)0, instream
== stdin
);
646 /* No exit -- exit is through quit_command. */
651 /* Don't use *_filtered for printing help. We don't want to prompt
652 for continue no matter how small the screen or how much we're going
656 print_gdb_help (stream
)
660 This is the GNU debugger. Usage:\n\n\
661 gdb [options] [executable-file [core-file or process-id]]\n\n\
665 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
666 --batch Exit after processing options.\n\
667 --cd=DIR Change current directory to DIR.\n\
668 --command=FILE Execute GDB commands from FILE.\n\
669 --core=COREFILE Analyze the core dump COREFILE.\n\
672 --dbx DBX compatibility mode.\n\
673 --directory=DIR Search for source files in DIR.\n\
674 --epoch Output information used by epoch emacs-GDB interface.\n\
675 --exec=EXECFILE Use EXECFILE as the executable.\n\
676 --fullname Output information used by emacs-GDB interface.\n\
677 --help Print this message.\n\
680 --mapped Use mapped symbol files if supported on this system.\n\
681 --nw Do not use a window interface.\n\
682 --nx Do not read .gdbinit file.\n\
683 --quiet Do not print version number on startup.\n\
684 --readnow Fully read symbol files on first access.\n\
687 --se=FILE Use FILE as symbol file and executable file.\n\
688 --symbols=SYMFILE Read symbols from SYMFILE.\n\
689 --tty=TTY Use TTY for input/output by the program being debugged.\n\
693 --tui Use a terminal user interface.\n\
697 --version Print version information and then exit.\n\
698 -w Use a window interface.\n\
699 --write Set writing into executable and core files.\n\
700 --xdb XDB compatibility mode.\n\
702 #ifdef ADDITIONAL_OPTION_HELP
703 fputs_unfiltered (ADDITIONAL_OPTION_HELP
, stream
);
705 fputs_unfiltered ("\n\
706 For more information, type \"help\" from within GDB, or consult the\n\
707 GDB manual (available as on-line info or a printed manual).\n\
708 Report bugs to \"bug-gdb@prep.ai.mit.edu\".\
713 /* All TUI I/O sent to the *_filtered and *_unfiltered functions
714 eventually ends up here. The fputs_unfiltered_hook is primarily
715 used by GUIs to collect all output and send it to the GUI, instead
716 of the controlling terminal. Only output to gdb_stdout and
717 gdb_stderr are sent to the hook. Everything else is sent on to
718 fputs to allow file I/O to be handled appropriately. */
720 /* FIXME: Should be broken up and moved to a TUI specific file. */
723 tui_file_fputs (linebuffer
, file
)
724 const char *linebuffer
;
727 struct tui_stream
*stream
= gdb_file_data (file
);
729 extern int tui_owns_terminal
;
731 /* If anything (GUI, TUI) wants to capture GDB output, this is
732 * the place... the way to do it is to set up
733 * fputs_unfiltered_hook.
734 * Our TUI ("gdb -tui") used to hook output, but in the
735 * new (XDB style) scheme, we do not do that anymore... - RT
737 if (fputs_unfiltered_hook
738 && (file
== gdb_stdout
739 || file
== gdb_stderr
))
740 fputs_unfiltered_hook (linebuffer
, file
);
744 if (tui_version
&& tui_owns_terminal
) {
745 /* If we get here somehow while updating the TUI (from
746 * within a tuiDo(), then we need to temporarily
747 * set up the terminal for GDB output. This probably just
748 * happens on error output.
751 if (stream
->ts_streamtype
== astring
) {
752 gdb_file_adjust_strbuf(strlen(linebuffer
), stream
);
753 strcat(stream
->ts_strbuf
, linebuffer
);
755 tuiTermUnsetup(0, (tui_version
) ? cmdWin
->detail
.commandInfo
.curch
: 0);
756 fputs (linebuffer
, stream
->ts_filestream
);
758 if (linebuffer
[strlen(linebuffer
) - 1] == '\n')
759 tuiClearCommandCharCount();
761 tuiIncrCommandCharCountBy(strlen(linebuffer
));
764 /* The normal case - just do a fputs() */
765 if (stream
->ts_streamtype
== astring
) {
766 gdb_file_adjust_strbuf(strlen(linebuffer
), stream
);
767 strcat(stream
->ts_strbuf
, linebuffer
);
768 } else fputs (linebuffer
, stream
->ts_filestream
);
773 if (stream
->ts_streamtype
== astring
) {
774 gdb_file_adjust_strbuf(strlen(linebuffer
), file
);
775 strcat(stream
->ts_strbuf
, linebuffer
);
776 } else fputs (linebuffer
, stream
->ts_filestream
);