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. */
25 #include "call-cmds.h"
32 #include <sys/types.h>
36 #include "gdb_string.h"
38 /* If nonzero, display time usage both at startup and for each command. */
42 /* If nonzero, display space usage both at startup and for each command. */
46 /* Whether this is the async version or not. The async version is
47 invoked on the command line with the -nw --async options. In this
48 version, the usual command_loop is substituted by and event loop which
49 processes UI events asynchronously. */
52 /* Whether this is the command line version or not */
55 /* Whether xdb commands will be handled */
58 /* Whether dbx commands will be handled */
64 /* Whether to enable writing into executable and core files */
65 extern int write_files;
67 static void print_gdb_help PARAMS ((GDB_FILE *));
68 extern void gdb_init PARAMS ((char *));
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;
160 gdb_file_size = sizeof(GDB_FILE);
162 gdb_stdout = (GDB_FILE *)xmalloc (gdb_file_size);
163 gdb_stdout->ts_streamtype = afile;
164 gdb_stdout->ts_filestream = stdout;
165 gdb_stdout->ts_strbuf = NULL;
166 gdb_stdout->ts_buflen = 0;
168 gdb_stderr = (GDB_FILE *)xmalloc (gdb_file_size);
169 gdb_stderr->ts_streamtype = afile;
170 gdb_stderr->ts_filestream = stderr;
171 gdb_stderr->ts_strbuf = NULL;
172 gdb_stderr->ts_buflen = 0;
174 /* Parse arguments and options. */
177 /* When var field is 0, use flag field to record the equivalent
178 short option (or arbitrary numbers starting at 10 for those
179 with no equivalent). */
180 static struct option long_options[] =
182 {"async", no_argument, &async, 1},
184 {"tui", no_argument, &tui_version, 1},
186 {"xdb", no_argument, &xdb_commands, 1},
187 {"dbx", no_argument, &dbx_commands, 1},
188 {"readnow", no_argument, &readnow_symbol_files, 1},
189 {"r", no_argument, &readnow_symbol_files, 1},
190 {"mapped", no_argument, &mapped_symbol_files, 1},
191 {"m", no_argument, &mapped_symbol_files, 1},
192 {"quiet", no_argument, &quiet, 1},
193 {"q", no_argument, &quiet, 1},
194 {"silent", no_argument, &quiet, 1},
195 {"nx", no_argument, &inhibit_gdbinit, 1},
196 {"n", no_argument, &inhibit_gdbinit, 1},
197 {"batch", no_argument, &batch, 1},
198 {"epoch", no_argument, &epoch_interface, 1},
200 /* This is a synonym for "--annotate=1". --annotate is now preferred,
201 but keep this here for a long time because people will be running
202 emacses which use --fullname. */
203 {"fullname", no_argument, 0, 'f'},
204 {"f", no_argument, 0, 'f'},
206 {"annotate", required_argument, 0, 12},
207 {"help", no_argument, &print_help, 1},
208 {"se", required_argument, 0, 10},
209 {"symbols", required_argument, 0, 's'},
210 {"s", required_argument, 0, 's'},
211 {"exec", required_argument, 0, 'e'},
212 {"e", required_argument, 0, 'e'},
213 {"core", required_argument, 0, 'c'},
214 {"c", required_argument, 0, 'c'},
215 {"command", required_argument, 0, 'x'},
216 {"version", no_argument, &print_version, 1},
217 {"x", required_argument, 0, 'x'},
218 {"directory", required_argument, 0, 'd'},
219 {"cd", required_argument, 0, 11},
220 {"tty", required_argument, 0, 't'},
221 {"baud", required_argument, 0, 'b'},
222 {"b", required_argument, 0, 'b'},
223 {"nw", no_argument, &use_windows, 0},
224 {"nowindows", no_argument, &use_windows, 0},
225 {"w", no_argument, &use_windows, 1},
226 {"windows", no_argument, &use_windows, 1},
227 {"statistics", no_argument, 0, 13},
228 {"write", no_argument, &write_files, 1},
229 /* Allow machine descriptions to add more options... */
230 #ifdef ADDITIONAL_OPTIONS
233 {0, no_argument, 0, 0}
240 c = getopt_long_only (argc, argv, "",
241 long_options, &option_index);
245 /* Long option that takes an argument. */
246 if (c == 0 && long_options[option_index].flag == 0)
247 c = long_options[option_index].val;
252 /* Long option that just sets a flag. */
262 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
263 annotation_level = atoi (optarg);
266 /* Enable the display of both time and space usage. */
271 annotation_level = 1;
272 /* We have probably been invoked from emacs. Disable window interface. */
285 cmdarg[ncmd++] = optarg;
289 cmdarg = (char **) xrealloc ((char *)cmdarg,
290 cmdsize * sizeof (*cmdarg));
294 dirarg[ndir++] = optarg;
298 dirarg = (char **) xrealloc ((char *)dirarg,
299 dirsize * sizeof (*dirarg));
313 i = strtol (optarg, &p, 0);
314 if (i == 0 && p == optarg)
316 /* Don't use *_filtered or warning() (which relies on
317 current_target) until after initialize_all_files(). */
321 "warning: could not set baud rate to `%s'.\n", optarg);
330 i = strtol (optarg, &p, 0);
331 if (i == 0 && p == optarg)
333 /* Don't use *_filtered or warning() (which relies on
334 current_target) until after initialize_all_files(). */
338 "warning: could not set timeout limit to `%s'.\n", optarg);
344 #ifdef ADDITIONAL_OPTION_CASES
345 ADDITIONAL_OPTION_CASES
348 fprintf_unfiltered (gdb_stderr,
349 "Use `%s --help' for a complete list of options.\n",
355 /* If --help or --version, disable window interface. */
356 if (print_help || print_version)
360 /* Disable the TUI as well. */
366 /* An explicit --tui flag overrides the default UI, which is the
372 /* OK, that's all the options. The other arguments are filenames. */
374 for (; optind < argc; optind++)
378 symarg = argv[optind];
379 execarg = argv[optind];
382 corearg = argv[optind];
385 fprintf_unfiltered (gdb_stderr,
386 "Excess command line arguments ignored. (%s%s)\n",
387 argv[optind], (optind == argc - 1) ? "" : " ...");
394 /* Get ready to invoke the event loop instead of the
395 command_loop. See event-loop.h for more details.*/
397 async_hook = setup_event_loop;
400 init_ui_hook = tuiInit;
404 /* Do these (and anything which might call wrap_here or *_filtered)
405 after initialize_all_files. */
408 print_gdb_version (gdb_stdout);
410 printf_filtered ("\n");
416 print_gdb_help (gdb_stdout);
417 fputs_unfiltered ("\n", gdb_stdout);
423 /* Print all the junk at the top, with trailing "..." if we are about
424 to read a symbol file (possibly slowly). */
425 print_gdb_version (gdb_stdout);
427 printf_filtered ("..");
429 gdb_flush (gdb_stdout); /* Force to screen during slow operations */
432 error_pre_print = "\n\n";
433 quit_pre_print = error_pre_print;
435 /* We may get more than one warning, don't double space all of them... */
436 warning_pre_print = "\nwarning: ";
438 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
439 *before* all the command line arguments are processed; it sets
440 global parameters, which are independent of what file you are
441 debugging or what directory you are in. */
444 char * tmp = getenv ("HOME");
448 homedir = (char *) alloca (MAX_PATH+1);
449 cygwin32_conv_to_posix_path (tmp, homedir);
455 homedir = getenv ("HOME");
459 homeinit = (char *) alloca (strlen (homedir) +
460 strlen (gdbinit) + 10);
461 strcpy (homeinit, homedir);
462 strcat (homeinit, "/");
463 strcat (homeinit, gdbinit);
465 if (!inhibit_gdbinit)
467 if (!SET_TOP_LEVEL ())
468 source_command (homeinit, 0);
470 do_cleanups (ALL_CLEANUPS);
472 /* Do stats; no need to do them elsewhere since we'll only
473 need them if homedir is set. Make sure that they are
474 zero in case one of them fails (this guarantees that they
475 won't match if either exists). */
477 memset (&homebuf, 0, sizeof (struct stat));
478 memset (&cwdbuf, 0, sizeof (struct stat));
480 stat (homeinit, &homebuf);
481 stat (gdbinit, &cwdbuf); /* We'll only need this if
485 /* Now perform all the actions indicated by the arguments. */
488 if (!SET_TOP_LEVEL ())
490 cd_command (cdarg, 0);
493 do_cleanups (ALL_CLEANUPS);
495 for (i = 0; i < ndir; i++)
496 if (!SET_TOP_LEVEL ())
497 directory_command (dirarg[i], 0);
499 do_cleanups (ALL_CLEANUPS);
503 && STREQ (execarg, symarg))
505 /* The exec file and the symbol-file are the same. If we can't open
506 it, better only print one error message. */
507 if (!SET_TOP_LEVEL ())
509 exec_file_command (execarg, !batch);
510 symbol_file_command (symarg, 0);
516 if (!SET_TOP_LEVEL ())
517 exec_file_command (execarg, !batch);
519 if (!SET_TOP_LEVEL ())
520 symbol_file_command (symarg, 0);
522 do_cleanups (ALL_CLEANUPS);
524 /* After the symbol file has been read, print a newline to get us
525 beyond the copyright line... But errors should still set off
526 the error message with a (single) blank line. */
528 printf_filtered ("\n");
529 error_pre_print = "\n";
530 quit_pre_print = error_pre_print;
531 warning_pre_print = "\nwarning: ";
535 if (!SET_TOP_LEVEL ())
536 core_file_command (corearg, !batch);
537 else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ())
538 attach_command (corearg, !batch);
540 do_cleanups (ALL_CLEANUPS);
543 if (!SET_TOP_LEVEL ())
544 tty_command (ttyarg, !batch);
545 do_cleanups (ALL_CLEANUPS);
547 #ifdef ADDITIONAL_OPTION_HANDLER
548 ADDITIONAL_OPTION_HANDLER;
551 /* Error messages should no longer be distinguished with extra output. */
552 error_pre_print = NULL;
553 quit_pre_print = NULL;
554 warning_pre_print = "warning: ";
556 /* Read the .gdbinit file in the current directory, *if* it isn't
557 the same as the $HOME/.gdbinit file (it should exist, also). */
560 || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
561 if (!inhibit_gdbinit)
563 if (!SET_TOP_LEVEL ())
564 source_command (gdbinit, 0);
566 do_cleanups (ALL_CLEANUPS);
568 for (i = 0; i < ncmd; i++)
570 if (!SET_TOP_LEVEL ())
572 /* NOTE: I am commenting this out, because it is not clear
573 where this feature is used. It is very old and
574 undocumented. ezannoni: 5/4/99*/
576 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
577 read_command_file (stdin);
580 source_command (cmdarg[i], !batch);
581 do_cleanups (ALL_CLEANUPS);
586 /* Read in the old history after all the command files have been read. */
591 /* We have hit the end of the batch file. */
595 /* Do any host- or target-specific hacks. This is used for i960 targets
596 to force the user to set a nindy target and spec its parameters. */
598 #ifdef BEFORE_MAIN_LOOP_HOOK
599 BEFORE_MAIN_LOOP_HOOK;
602 END_PROGRESS (argv[0]);
604 /* Show time and/or space usage. */
608 long init_time = get_run_time () - time_at_startup;
610 printf_unfiltered ("Startup time: %ld.%06ld\n",
611 init_time / 1000000, init_time % 1000000);
617 extern char **environ;
618 char *lim = (char *) sbrk (0);
620 printf_unfiltered ("Startup size: data size %ld\n",
621 (long) (lim - (char *) &environ));
625 /* Call the event loop, if gdb was invoked with the --async
626 option. Control will never get back to this file, if the event
627 loop is invoked. See the files event-*.[ch] for details. */
631 /* The default command loop.
632 The WIN32 Gui calls this main to set up gdb's state, and
633 has its own command loop. */
634 #if !defined _WIN32 || defined __GNUC__
637 if (!SET_TOP_LEVEL ())
639 do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */
640 /* GUIs generally have their own command loop, mainloop, or whatever.
641 This is a good place to gain control because many error
642 conditions will end up here via longjmp(). */
643 if (command_loop_hook)
644 command_loop_hook ();
647 quit_command ((char *)0, instream == stdin);
651 /* No exit -- exit is through quit_command. */
656 /* Don't use *_filtered for printing help. We don't want to prompt
657 for continue no matter how small the screen or how much we're going
661 print_gdb_help (stream)
665 This is the GNU debugger. Usage:\n\n\
666 gdb [options] [executable-file [core-file or process-id]]\n\n\
670 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
671 --batch Exit after processing options.\n\
672 --cd=DIR Change current directory to DIR.\n\
673 --command=FILE Execute GDB commands from FILE.\n\
674 --core=COREFILE Analyze the core dump COREFILE.\n\
677 --dbx DBX compatibility mode.\n\
678 --directory=DIR Search for source files in DIR.\n\
679 --epoch Output information used by epoch emacs-GDB interface.\n\
680 --exec=EXECFILE Use EXECFILE as the executable.\n\
681 --fullname Output information used by emacs-GDB interface.\n\
682 --help Print this message.\n\
685 --mapped Use mapped symbol files if supported on this system.\n\
686 --nw Do not use a window interface.\n\
687 --nx Do not read .gdbinit file.\n\
688 --quiet Do not print version number on startup.\n\
689 --readnow Fully read symbol files on first access.\n\
692 --se=FILE Use FILE as symbol file and executable file.\n\
693 --symbols=SYMFILE Read symbols from SYMFILE.\n\
694 --tty=TTY Use TTY for input/output by the program being debugged.\n\
698 --tui Use a terminal user interface.\n\
702 --version Print version information and then exit.\n\
703 -w Use a window interface.\n\
704 --write Set writing into executable and core files.\n\
705 --xdb XDB compatibility mode.\n\
707 #ifdef ADDITIONAL_OPTION_HELP
708 fputs_unfiltered (ADDITIONAL_OPTION_HELP, stream);
710 fputs_unfiltered ("\n\
711 For more information, type \"help\" from within GDB, or consult the\n\
712 GDB manual (available as on-line info or a printed manual).\n\
724 proc_remove_foreign (pid)
729 /* All I/O sent to the *_filtered and *_unfiltered functions eventually ends up
730 here. The fputs_unfiltered_hook is primarily used by GUIs to collect all
731 output and send it to the GUI, instead of the controlling terminal. Only
732 output to gdb_stdout and gdb_stderr are sent to the hook. Everything else
733 is sent on to fputs to allow file I/O to be handled appropriately. */
736 fputs_unfiltered (linebuffer, stream)
737 const char *linebuffer;
741 extern int tui_owns_terminal;
743 /* If anything (GUI, TUI) wants to capture GDB output, this is
744 * the place... the way to do it is to set up
745 * fputs_unfiltered_hook.
746 * Our TUI ("gdb -tui") used to hook output, but in the
747 * new (XDB style) scheme, we do not do that anymore... - RT
749 if (fputs_unfiltered_hook
750 && (stream == gdb_stdout
751 || stream == gdb_stderr))
752 fputs_unfiltered_hook (linebuffer, stream);
756 if (tui_version && tui_owns_terminal) {
757 /* If we get here somehow while updating the TUI (from
758 * within a tuiDo(), then we need to temporarily
759 * set up the terminal for GDB output. This probably just
760 * happens on error output.
763 if (stream->ts_streamtype == astring) {
764 gdb_file_adjust_strbuf(strlen(linebuffer), stream);
765 strcat(stream->ts_strbuf, linebuffer);
767 tuiTermUnsetup(0, (tui_version) ? cmdWin->detail.commandInfo.curch : 0);
768 fputs (linebuffer, stream->ts_filestream);
770 if (linebuffer[strlen(linebuffer) - 1] == '\n')
771 tuiClearCommandCharCount();
773 tuiIncrCommandCharCountBy(strlen(linebuffer));
776 /* The normal case - just do a fputs() */
777 if (stream->ts_streamtype == astring) {
778 gdb_file_adjust_strbuf(strlen(linebuffer), stream);
779 strcat(stream->ts_strbuf, linebuffer);
780 } else fputs (linebuffer, stream->ts_filestream);
785 if (stream->ts_streamtype == astring) {
786 gdb_file_adjust_strbuf(strlen(linebuffer), stream);
787 strcat(stream->ts_strbuf, linebuffer);
788 } else fputs (linebuffer, stream->ts_filestream);