1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 1999
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,
20 Boston, MA 02111-1307, USA. */
26 #include "call-cmds.h"
33 #include <sys/types.h>
37 #include "gdb_string.h"
38 #include "event-loop.h"
40 /* If nonzero, display time usage both at startup and for each command. */
44 /* If nonzero, display space usage both at startup and for each command. */
48 /* Whether this is the async version or not. The async version is
49 invoked on the command line with the -nw --async options. In this
50 version, the usual command_loop is substituted by and event loop which
51 processes UI events asynchronously. */
54 /* Whether this is the command line version or not */
57 /* Whether xdb commands will be handled */
60 /* Whether dbx commands will be handled */
66 GDB_FILE *gdb_stdtarg;
68 /* Whether to enable writing into executable and core files */
69 extern int write_files;
71 static void print_gdb_help PARAMS ((GDB_FILE *));
73 /* These two are used to set the external editor commands when gdb is farming
74 out files to be edited by another program. */
76 extern int enable_external_editor;
77 extern char *external_editor_command;
80 #include <windows.h> /* for MAX_PATH */
81 #include <sys/cygwin.h> /* for cygwin32_conv_to_posix_path */
93 /* Pointers to various arguments from command line. */
100 /* These are static so that we can take their address in an initializer. */
101 static int print_help;
102 static int print_version;
104 /* Pointers to all arguments of --command option. */
106 /* Allocated size of cmdarg. */
108 /* Number of elements of cmdarg used. */
111 /* Indices of all arguments of --directory option. */
113 /* Allocated size. */
115 /* Number of elements used. */
118 struct stat homebuf, cwdbuf;
119 char *homedir, *homeinit;
123 long time_at_startup = get_run_time ();
127 START_PROGRESS (argv[0], 0);
130 /* Do all Mac-specific setup. */
134 /* This needs to happen before the first use of malloc. */
135 init_malloc ((PTR) NULL);
137 #if defined (ALIGN_STACK_ON_STARTUP)
138 i = (int) &count & 0x3;
143 /* If error() is called from initialization code, just exit */
144 if (SET_TOP_LEVEL ())
150 cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
153 dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
157 line = (char *) xmalloc (linesize);
158 line[0] = '\0'; /* Terminate saved (now empty) cmd line */
161 getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
162 current_directory = gdb_dirbuf;
166 gdb_stdout = stdio_fileopen (stdout);
167 gdb_stderr = stdio_fileopen (stderr);
168 gdb_stdlog = gdb_stderr; /* for moment */
169 gdb_stdtarg = gdb_stderr; /* for moment */
171 gdb_stdout = tui_fileopen (stdout);
172 gdb_stderr = tui_fileopen (stderr);
173 gdb_stdlog = gdb_stdout; /* for moment */
174 gdb_stdtarg = gdb_stderr; /* for moment */
177 /* Parse arguments and options. */
180 /* When var field is 0, use flag field to record the equivalent
181 short option (or arbitrary numbers starting at 10 for those
182 with no equivalent). */
183 static struct option long_options[] =
185 {"async", no_argument, &async_p, 1},
186 {"noasync", no_argument, &async_p, 0},
188 {"tui", no_argument, &tui_version, 1},
190 {"xdb", no_argument, &xdb_commands, 1},
191 {"dbx", no_argument, &dbx_commands, 1},
192 {"readnow", no_argument, &readnow_symbol_files, 1},
193 {"r", no_argument, &readnow_symbol_files, 1},
194 {"mapped", no_argument, &mapped_symbol_files, 1},
195 {"m", no_argument, &mapped_symbol_files, 1},
196 {"quiet", no_argument, &quiet, 1},
197 {"q", no_argument, &quiet, 1},
198 {"silent", no_argument, &quiet, 1},
199 {"nx", no_argument, &inhibit_gdbinit, 1},
200 {"n", no_argument, &inhibit_gdbinit, 1},
201 {"batch", no_argument, &batch, 1},
202 {"epoch", no_argument, &epoch_interface, 1},
204 /* This is a synonym for "--annotate=1". --annotate is now preferred,
205 but keep this here for a long time because people will be running
206 emacses which use --fullname. */
207 {"fullname", no_argument, 0, 'f'},
208 {"f", no_argument, 0, 'f'},
210 {"annotate", required_argument, 0, 12},
211 {"help", no_argument, &print_help, 1},
212 {"se", required_argument, 0, 10},
213 {"symbols", required_argument, 0, 's'},
214 {"s", required_argument, 0, 's'},
215 {"exec", required_argument, 0, 'e'},
216 {"e", required_argument, 0, 'e'},
217 {"core", required_argument, 0, 'c'},
218 {"c", required_argument, 0, 'c'},
219 {"command", required_argument, 0, 'x'},
220 {"version", no_argument, &print_version, 1},
221 {"x", required_argument, 0, 'x'},
222 {"directory", required_argument, 0, 'd'},
223 {"cd", required_argument, 0, 11},
224 {"tty", required_argument, 0, 't'},
225 {"baud", required_argument, 0, 'b'},
226 {"b", required_argument, 0, 'b'},
227 {"nw", no_argument, &use_windows, 0},
228 {"nowindows", no_argument, &use_windows, 0},
229 {"w", no_argument, &use_windows, 1},
230 {"windows", no_argument, &use_windows, 1},
231 {"statistics", no_argument, 0, 13},
232 {"write", no_argument, &write_files, 1},
233 /* Allow machine descriptions to add more options... */
234 #ifdef ADDITIONAL_OPTIONS
237 {0, no_argument, 0, 0}
244 c = getopt_long_only (argc, argv, "",
245 long_options, &option_index);
249 /* Long option that takes an argument. */
250 if (c == 0 && long_options[option_index].flag == 0)
251 c = long_options[option_index].val;
256 /* Long option that just sets a flag. */
266 /* FIXME: what if the syntax is wrong (e.g. not digits)? */
267 annotation_level = atoi (optarg);
270 /* Enable the display of both time and space usage. */
275 annotation_level = 1;
276 /* We have probably been invoked from emacs. Disable window interface. */
289 cmdarg[ncmd++] = optarg;
293 cmdarg = (char **) xrealloc ((char *) cmdarg,
294 cmdsize * sizeof (*cmdarg));
298 dirarg[ndir++] = optarg;
302 dirarg = (char **) xrealloc ((char *) dirarg,
303 dirsize * sizeof (*dirarg));
317 i = strtol (optarg, &p, 0);
318 if (i == 0 && p == optarg)
320 /* Don't use *_filtered or warning() (which relies on
321 current_target) until after initialize_all_files(). */
325 "warning: could not set baud rate to `%s'.\n", optarg);
334 i = strtol (optarg, &p, 0);
335 if (i == 0 && p == optarg)
337 /* Don't use *_filtered or warning() (which relies on
338 current_target) until after initialize_all_files(). */
342 "warning: could not set timeout limit to `%s'.\n", optarg);
348 #ifdef ADDITIONAL_OPTION_CASES
349 ADDITIONAL_OPTION_CASES
352 fprintf_unfiltered (gdb_stderr,
353 "Use `%s --help' for a complete list of options.\n",
359 /* If --help or --version, disable window interface. */
360 if (print_help || print_version)
364 /* Disable the TUI as well. */
370 /* An explicit --tui flag overrides the default UI, which is the
376 /* OK, that's all the options. The other arguments are filenames. */
378 for (; optind < argc; optind++)
382 symarg = argv[optind];
383 execarg = argv[optind];
386 corearg = argv[optind];
389 fprintf_unfiltered (gdb_stderr,
390 "Excess command line arguments ignored. (%s%s)\n",
391 argv[optind], (optind == argc - 1) ? "" : " ...");
399 /* Should this be moved to tui-top.c:_initialize_tui()? */
401 init_ui_hook = tuiInit;
404 /* Initialize all files. Give the interpreter a chance to take
405 control of the console via the init_ui_hook()) */
408 /* Do these (and anything which might call wrap_here or *_filtered)
409 after initialize_all_files. */
412 print_gdb_version (gdb_stdout);
414 printf_filtered ("\n");
420 print_gdb_help (gdb_stdout);
421 fputs_unfiltered ("\n", gdb_stdout);
427 /* Print all the junk at the top, with trailing "..." if we are about
428 to read a symbol file (possibly slowly). */
429 print_gdb_version (gdb_stdout);
431 printf_filtered ("..");
433 gdb_flush (gdb_stdout); /* Force to screen during slow operations */
436 error_pre_print = "\n\n";
437 quit_pre_print = error_pre_print;
439 /* We may get more than one warning, don't double space all of them... */
440 warning_pre_print = "\nwarning: ";
442 /* Read and execute $HOME/.gdbinit file, if it exists. This is done
443 *before* all the command line arguments are processed; it sets
444 global parameters, which are independent of what file you are
445 debugging or what directory you are in. */
448 char *tmp = getenv ("HOME");
452 homedir = (char *) alloca (MAX_PATH + 1);
453 cygwin32_conv_to_posix_path (tmp, homedir);
459 homedir = getenv ("HOME");
463 homeinit = (char *) alloca (strlen (homedir) +
464 strlen (gdbinit) + 10);
465 strcpy (homeinit, homedir);
466 strcat (homeinit, "/");
467 strcat (homeinit, gdbinit);
469 if (!inhibit_gdbinit)
471 if (!SET_TOP_LEVEL ())
472 source_command (homeinit, 0);
474 do_cleanups (ALL_CLEANUPS);
476 /* Do stats; no need to do them elsewhere since we'll only
477 need them if homedir is set. Make sure that they are
478 zero in case one of them fails (this guarantees that they
479 won't match if either exists). */
481 memset (&homebuf, 0, sizeof (struct stat));
482 memset (&cwdbuf, 0, sizeof (struct stat));
484 stat (homeinit, &homebuf);
485 stat (gdbinit, &cwdbuf); /* We'll only need this if
489 /* Now perform all the actions indicated by the arguments. */
492 if (!SET_TOP_LEVEL ())
494 cd_command (cdarg, 0);
497 do_cleanups (ALL_CLEANUPS);
499 for (i = 0; i < ndir; i++)
500 if (!SET_TOP_LEVEL ())
501 directory_command (dirarg[i], 0);
503 do_cleanups (ALL_CLEANUPS);
507 && STREQ (execarg, symarg))
509 /* The exec file and the symbol-file are the same. If we can't open
510 it, better only print one error message. */
511 if (!SET_TOP_LEVEL ())
513 exec_file_command (execarg, !batch);
514 symbol_file_command (symarg, 0);
520 if (!SET_TOP_LEVEL ())
521 exec_file_command (execarg, !batch);
523 if (!SET_TOP_LEVEL ())
524 symbol_file_command (symarg, 0);
526 do_cleanups (ALL_CLEANUPS);
528 /* After the symbol file has been read, print a newline to get us
529 beyond the copyright line... But errors should still set off
530 the error message with a (single) blank line. */
532 printf_filtered ("\n");
533 error_pre_print = "\n";
534 quit_pre_print = error_pre_print;
535 warning_pre_print = "\nwarning: ";
539 if (!SET_TOP_LEVEL ())
540 core_file_command (corearg, !batch);
541 else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ())
542 attach_command (corearg, !batch);
544 do_cleanups (ALL_CLEANUPS);
547 if (!SET_TOP_LEVEL ())
548 tty_command (ttyarg, !batch);
549 do_cleanups (ALL_CLEANUPS);
551 #ifdef ADDITIONAL_OPTION_HANDLER
552 ADDITIONAL_OPTION_HANDLER;
555 /* Error messages should no longer be distinguished with extra output. */
556 error_pre_print = NULL;
557 quit_pre_print = NULL;
558 warning_pre_print = "warning: ";
560 /* Read the .gdbinit file in the current directory, *if* it isn't
561 the same as the $HOME/.gdbinit file (it should exist, also). */
564 || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
565 if (!inhibit_gdbinit)
567 if (!SET_TOP_LEVEL ())
568 source_command (gdbinit, 0);
570 do_cleanups (ALL_CLEANUPS);
572 for (i = 0; i < ncmd; i++)
574 if (!SET_TOP_LEVEL ())
576 /* NOTE: I am commenting this out, because it is not clear
577 where this feature is used. It is very old and
578 undocumented. ezannoni: 1999-05-04 */
580 if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
581 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);
648 /* No exit -- exit is through quit_command. */
653 /* Don't use *_filtered for printing help. We don't want to prompt
654 for continue no matter how small the screen or how much we're going
658 print_gdb_help (stream)
662 This is the GNU debugger. Usage:\n\n\
663 gdb [options] [executable-file [core-file or process-id]]\n\n\
667 --[no]async Enable (disable) asynchronous version of CLI\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\
718 /* All TUI I/O sent to the *_filtered and *_unfiltered functions
719 eventually ends up here. The fputs_unfiltered_hook is primarily
720 used by GUIs to collect all output and send it to the GUI, instead
721 of the controlling terminal. Only output to gdb_stdout and
722 gdb_stderr are sent to the hook. Everything else is sent on to
723 fputs to allow file I/O to be handled appropriately. */
725 /* FIXME: Should be broken up and moved to a TUI specific file. */
728 tui_file_fputs (linebuffer, file)
729 const char *linebuffer;
732 struct tui_stream *stream = gdb_file_data (file);
734 extern int tui_owns_terminal;
736 /* If anything (GUI, TUI) wants to capture GDB output, this is
737 * the place... the way to do it is to set up
738 * fputs_unfiltered_hook.
739 * Our TUI ("gdb -tui") used to hook output, but in the
740 * new (XDB style) scheme, we do not do that anymore... - RT
742 if (fputs_unfiltered_hook
743 && (file == gdb_stdout
744 || file == gdb_stderr))
745 fputs_unfiltered_hook (linebuffer, file);
749 if (tui_version && tui_owns_terminal)
751 /* If we get here somehow while updating the TUI (from
752 * within a tuiDo(), then we need to temporarily
753 * set up the terminal for GDB output. This probably just
754 * happens on error output.
757 if (stream->ts_streamtype == astring)
759 gdb_file_adjust_strbuf (strlen (linebuffer), stream);
760 strcat (stream->ts_strbuf, linebuffer);
764 tuiTermUnsetup (0, (tui_version) ? cmdWin->detail.commandInfo.curch : 0);
765 fputs (linebuffer, stream->ts_filestream);
767 if (linebuffer[strlen (linebuffer) - 1] == '\n')
768 tuiClearCommandCharCount ();
770 tuiIncrCommandCharCountBy (strlen (linebuffer));
775 /* The normal case - just do a fputs() */
776 if (stream->ts_streamtype == astring)
778 gdb_file_adjust_strbuf (strlen (linebuffer), stream);
779 strcat (stream->ts_strbuf, linebuffer);
782 fputs (linebuffer, stream->ts_filestream);
787 if (stream->ts_streamtype == astring)
789 gdb_file_adjust_strbuf (strlen (linebuffer), file);
790 strcat (stream->ts_strbuf, linebuffer);
793 fputs (linebuffer, stream->ts_filestream);