119370Spst/* Top level stuff for GDB, the GNU debugger.
219370Spst
3130809Smarcel   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4130809Smarcel   1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
5130809Smarcel   Foundation, Inc.
6130809Smarcel
798948Sobrien   This file is part of GDB.
819370Spst
998948Sobrien   This program is free software; you can redistribute it and/or modify
1098948Sobrien   it under the terms of the GNU General Public License as published by
1198948Sobrien   the Free Software Foundation; either version 2 of the License, or
1298948Sobrien   (at your option) any later version.
1319370Spst
1498948Sobrien   This program is distributed in the hope that it will be useful,
1598948Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698948Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798948Sobrien   GNU General Public License for more details.
1819370Spst
1998948Sobrien   You should have received a copy of the GNU General Public License
2098948Sobrien   along with this program; if not, write to the Free Software
2198948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298948Sobrien   Boston, MA 02111-1307, USA.  */
2319370Spst
2419370Spst#include "defs.h"
2519370Spst#include "top.h"
2619370Spst#include "target.h"
2719370Spst#include "inferior.h"
2898948Sobrien#include "symfile.h"
2998948Sobrien#include "gdbcore.h"
3019370Spst
3119370Spst#include "getopt.h"
3219370Spst
3319370Spst#include <sys/types.h>
3419370Spst#include "gdb_stat.h"
3519370Spst#include <ctype.h>
3619370Spst
3719370Spst#include "gdb_string.h"
3898948Sobrien#include "event-loop.h"
3998948Sobrien#include "ui-out.h"
4019370Spst
41130809Smarcel#include "interps.h"
42130809Smarcel#include "main.h"
43130809Smarcel
4419370Spst/* If nonzero, display time usage both at startup and for each command.  */
4519370Spst
4619370Spstint display_time;
4719370Spst
4819370Spst/* If nonzero, display space usage both at startup and for each command.  */
4919370Spst
5019370Spstint display_space;
5119370Spst
5298948Sobrien/* Whether this is the async version or not.  The async version is
5398948Sobrien   invoked on the command line with the -nw --async options.  In this
5498948Sobrien   version, the usual command_loop is substituted by and event loop which
5598948Sobrien   processes UI events asynchronously. */
5698948Sobrienint event_loop_p = 1;
5798948Sobrien
58130809Smarcel/* The selected interpreter.  This will be used as a set command
59130809Smarcel   variable, so it should always be malloc'ed - since
60130809Smarcel   do_setshow_command will free it. */
6198948Sobrienchar *interpreter_p;
6298948Sobrien
6346283Sdfr/* Whether xdb commands will be handled */
6446283Sdfrint xdb_commands = 0;
6546283Sdfr
6646283Sdfr/* Whether dbx commands will be handled */
6746283Sdfrint dbx_commands = 0;
6846283Sdfr
69130809Smarcel/* System root path, used to find libraries etc.  */
70130809Smarcelchar *gdb_sysroot = 0;
71130809Smarcel
7298948Sobrienstruct ui_file *gdb_stdout;
7398948Sobrienstruct ui_file *gdb_stderr;
7498948Sobrienstruct ui_file *gdb_stdlog;
75130809Smarcelstruct ui_file *gdb_stdin;
76130809Smarcel/* target IO streams */
77130809Smarcelstruct ui_file *gdb_stdtargin;
7898948Sobrienstruct ui_file *gdb_stdtarg;
79130809Smarcelstruct ui_file *gdb_stdtargerr;
8046283Sdfr
8146283Sdfr/* Whether to enable writing into executable and core files */
8246283Sdfrextern int write_files;
8346283Sdfr
8498948Sobrienstatic void print_gdb_help (struct ui_file *);
8546283Sdfr
8646283Sdfr/* These two are used to set the external editor commands when gdb is farming
8746283Sdfr   out files to be edited by another program. */
8846283Sdfr
8998948Sobrienextern char *external_editor_command;
9046283Sdfr
9198948Sobrien/* Call command_loop.  If it happens to return, pass that through as a
9298948Sobrien   non-zero return status. */
9346283Sdfr
9498948Sobrienstatic int
9598948Sobriencaptured_command_loop (void *data)
9619370Spst{
97130809Smarcel  current_interp_command_loop ();
9898948Sobrien  /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
9998948Sobrien     would clean things up (restoring the cleanup chain) to the state
10098948Sobrien     they were just prior to the call.  Technically, this means that
10198948Sobrien     the do_cleanups() below is redundant.  Unfortunately, many FUNCs
10298948Sobrien     are not that well behaved.  do_cleanups should either be replaced
10398948Sobrien     with a do_cleanups call (to cover the problem) or an assertion
10498948Sobrien     check to detect bad FUNCs code. */
10598948Sobrien  do_cleanups (ALL_CLEANUPS);
10698948Sobrien  /* If the command_loop returned, normally (rather than threw an
10798948Sobrien     error) we try to quit. If the quit is aborted, catch_errors()
10898948Sobrien     which called this catch the signal and restart the command
10998948Sobrien     loop. */
11098948Sobrien  quit_command (NULL, instream == stdin);
11198948Sobrien  return 1;
11298948Sobrien}
11398948Sobrien
11498948Sobrienstatic int
11598948Sobriencaptured_main (void *data)
11698948Sobrien{
11798948Sobrien  struct captured_main_args *context = data;
11898948Sobrien  int argc = context->argc;
11998948Sobrien  char **argv = context->argv;
12019370Spst  int count;
12119370Spst  static int quiet = 0;
12219370Spst  static int batch = 0;
12398948Sobrien  static int set_args = 0;
12419370Spst
12519370Spst  /* Pointers to various arguments from command line.  */
12619370Spst  char *symarg = NULL;
12719370Spst  char *execarg = NULL;
12819370Spst  char *corearg = NULL;
12919370Spst  char *cdarg = NULL;
13019370Spst  char *ttyarg = NULL;
13119370Spst
13219370Spst  /* These are static so that we can take their address in an initializer.  */
13319370Spst  static int print_help;
13419370Spst  static int print_version;
13519370Spst
13619370Spst  /* Pointers to all arguments of --command option.  */
13719370Spst  char **cmdarg;
13819370Spst  /* Allocated size of cmdarg.  */
13919370Spst  int cmdsize;
14019370Spst  /* Number of elements of cmdarg used.  */
14119370Spst  int ncmd;
14219370Spst
14319370Spst  /* Indices of all arguments of --directory option.  */
14419370Spst  char **dirarg;
14519370Spst  /* Allocated size.  */
14619370Spst  int dirsize;
14719370Spst  /* Number of elements used.  */
14819370Spst  int ndir;
14998948Sobrien
15019370Spst  struct stat homebuf, cwdbuf;
15119370Spst  char *homedir, *homeinit;
15219370Spst
153130809Smarcel  int i;
15419370Spst
15519370Spst  long time_at_startup = get_run_time ();
15619370Spst
157130809Smarcel#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
158130809Smarcel  setlocale (LC_MESSAGES, "");
159130809Smarcel#endif
160130809Smarcel#if defined (HAVE_SETLOCALE)
161130809Smarcel  setlocale (LC_CTYPE, "");
162130809Smarcel#endif
163130809Smarcel  bindtextdomain (PACKAGE, LOCALEDIR);
164130809Smarcel  textdomain (PACKAGE);
16519370Spst
16619370Spst  /* This needs to happen before the first use of malloc.  */
167130809Smarcel  init_malloc (NULL);
16819370Spst
169130809Smarcel#ifdef HAVE_SBRK
170130809Smarcel  lim_at_start = (char *) sbrk (0);
171130809Smarcel#endif
172130809Smarcel
17319370Spst#if defined (ALIGN_STACK_ON_STARTUP)
17419370Spst  i = (int) &count & 0x3;
17519370Spst  if (i != 0)
17619370Spst    alloca (4 - i);
17719370Spst#endif
17819370Spst
17919370Spst  cmdsize = 1;
18019370Spst  cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
18119370Spst  ncmd = 0;
18219370Spst  dirsize = 1;
18319370Spst  dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
18419370Spst  ndir = 0;
18519370Spst
18619370Spst  quit_flag = 0;
18719370Spst  line = (char *) xmalloc (linesize);
18819370Spst  line[0] = '\0';		/* Terminate saved (now empty) cmd line */
18919370Spst  instream = stdin;
19019370Spst
19119370Spst  getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
19219370Spst  current_directory = gdb_dirbuf;
19319370Spst
19498948Sobrien  gdb_stdout = stdio_fileopen (stdout);
19598948Sobrien  gdb_stderr = stdio_fileopen (stderr);
19698948Sobrien  gdb_stdlog = gdb_stderr;	/* for moment */
19798948Sobrien  gdb_stdtarg = gdb_stderr;	/* for moment */
198130809Smarcel  gdb_stdin = stdio_fileopen (stdin);
199130809Smarcel  gdb_stdtargerr = gdb_stderr;	/* for moment */
200130809Smarcel  gdb_stdtargin = gdb_stdin;	/* for moment */
20146283Sdfr
20298948Sobrien  /* initialize error() */
20398948Sobrien  error_init ();
20446283Sdfr
205130809Smarcel  /* Set the sysroot path.  */
206130809Smarcel#ifdef TARGET_SYSTEM_ROOT_RELOCATABLE
207130809Smarcel  gdb_sysroot = make_relative_prefix (argv[0], BINDIR, TARGET_SYSTEM_ROOT);
208130809Smarcel  if (gdb_sysroot)
209130809Smarcel    {
210130809Smarcel      struct stat s;
211130809Smarcel      int res = 0;
212130809Smarcel
213130809Smarcel      if (stat (gdb_sysroot, &s) == 0)
214130809Smarcel	if (S_ISDIR (s.st_mode))
215130809Smarcel	  res = 1;
216130809Smarcel
217130809Smarcel      if (res == 0)
218130809Smarcel	{
219130809Smarcel	  xfree (gdb_sysroot);
220130809Smarcel	  gdb_sysroot = TARGET_SYSTEM_ROOT;
221130809Smarcel	}
222130809Smarcel    }
223130809Smarcel  else
224130809Smarcel    gdb_sysroot = TARGET_SYSTEM_ROOT;
225130809Smarcel#else
226130809Smarcel#if defined (TARGET_SYSTEM_ROOT)
227130809Smarcel  gdb_sysroot = TARGET_SYSTEM_ROOT;
228130809Smarcel#else
229130809Smarcel  gdb_sysroot = "";
230130809Smarcel#endif
231130809Smarcel#endif
232130809Smarcel
233130809Smarcel  /* There will always be an interpreter.  Either the one passed into
234130809Smarcel     this captured main, or one specified by the user at start up, or
235130809Smarcel     the console.  Initialize the interpreter to the one requested by
236130809Smarcel     the application.  */
237130809Smarcel  interpreter_p = xstrdup (context->interpreter_p);
238130809Smarcel
23919370Spst  /* Parse arguments and options.  */
24019370Spst  {
24119370Spst    int c;
24219370Spst    /* When var field is 0, use flag field to record the equivalent
24319370Spst       short option (or arbitrary numbers starting at 10 for those
24419370Spst       with no equivalent).  */
245130809Smarcel    enum {
246130809Smarcel      OPT_SE = 10,
247130809Smarcel      OPT_CD,
248130809Smarcel      OPT_ANNOTATE,
249130809Smarcel      OPT_STATISTICS,
250130809Smarcel      OPT_TUI,
251130809Smarcel      OPT_NOWINDOWS,
252130809Smarcel      OPT_WINDOWS
253130809Smarcel    };
25419370Spst    static struct option long_options[] =
25598948Sobrien    {
25698948Sobrien      {"async", no_argument, &event_loop_p, 1},
25798948Sobrien      {"noasync", no_argument, &event_loop_p, 0},
25846283Sdfr#if defined(TUI)
259130809Smarcel      {"tui", no_argument, 0, OPT_TUI},
26046283Sdfr#endif
26198948Sobrien      {"xdb", no_argument, &xdb_commands, 1},
26298948Sobrien      {"dbx", no_argument, &dbx_commands, 1},
26398948Sobrien      {"readnow", no_argument, &readnow_symbol_files, 1},
26498948Sobrien      {"r", no_argument, &readnow_symbol_files, 1},
26598948Sobrien      {"quiet", no_argument, &quiet, 1},
26698948Sobrien      {"q", no_argument, &quiet, 1},
26798948Sobrien      {"silent", no_argument, &quiet, 1},
26898948Sobrien      {"nx", no_argument, &inhibit_gdbinit, 1},
26998948Sobrien      {"n", no_argument, &inhibit_gdbinit, 1},
27098948Sobrien      {"batch", no_argument, &batch, 1},
27198948Sobrien      {"epoch", no_argument, &epoch_interface, 1},
27219370Spst
27398948Sobrien    /* This is a synonym for "--annotate=1".  --annotate is now preferred,
27498948Sobrien       but keep this here for a long time because people will be running
27598948Sobrien       emacses which use --fullname.  */
27698948Sobrien      {"fullname", no_argument, 0, 'f'},
27798948Sobrien      {"f", no_argument, 0, 'f'},
27819370Spst
279130809Smarcel      {"annotate", required_argument, 0, OPT_ANNOTATE},
28098948Sobrien      {"help", no_argument, &print_help, 1},
281130809Smarcel      {"se", required_argument, 0, OPT_SE},
28298948Sobrien      {"symbols", required_argument, 0, 's'},
28398948Sobrien      {"s", required_argument, 0, 's'},
28498948Sobrien      {"exec", required_argument, 0, 'e'},
28598948Sobrien      {"e", required_argument, 0, 'e'},
28698948Sobrien      {"core", required_argument, 0, 'c'},
28798948Sobrien      {"c", required_argument, 0, 'c'},
28898948Sobrien      {"pid", required_argument, 0, 'p'},
28998948Sobrien      {"p", required_argument, 0, 'p'},
29098948Sobrien      {"command", required_argument, 0, 'x'},
29198948Sobrien      {"version", no_argument, &print_version, 1},
29298948Sobrien      {"x", required_argument, 0, 'x'},
29398948Sobrien#ifdef GDBTK
29498948Sobrien      {"tclcommand", required_argument, 0, 'z'},
29598948Sobrien      {"enable-external-editor", no_argument, 0, 'y'},
29698948Sobrien      {"editor-command", required_argument, 0, 'w'},
29798948Sobrien#endif
29898948Sobrien      {"ui", required_argument, 0, 'i'},
29998948Sobrien      {"interpreter", required_argument, 0, 'i'},
30098948Sobrien      {"i", required_argument, 0, 'i'},
30198948Sobrien      {"directory", required_argument, 0, 'd'},
30298948Sobrien      {"d", required_argument, 0, 'd'},
303130809Smarcel      {"cd", required_argument, 0, OPT_CD},
30498948Sobrien      {"tty", required_argument, 0, 't'},
30598948Sobrien      {"baud", required_argument, 0, 'b'},
30698948Sobrien      {"b", required_argument, 0, 'b'},
307130809Smarcel      {"nw", no_argument, NULL, OPT_NOWINDOWS},
308130809Smarcel      {"nowindows", no_argument, NULL, OPT_NOWINDOWS},
309130809Smarcel      {"w", no_argument, NULL, OPT_WINDOWS},
310130809Smarcel      {"windows", no_argument, NULL, OPT_WINDOWS},
311130809Smarcel      {"statistics", no_argument, 0, OPT_STATISTICS},
31298948Sobrien      {"write", no_argument, &write_files, 1},
31398948Sobrien      {"args", no_argument, &set_args, 1},
31498948Sobrien      {0, no_argument, 0, 0}
31598948Sobrien    };
31619370Spst
31719370Spst    while (1)
31819370Spst      {
31919370Spst	int option_index;
32019370Spst
32119370Spst	c = getopt_long_only (argc, argv, "",
32219370Spst			      long_options, &option_index);
32398948Sobrien	if (c == EOF || set_args)
32419370Spst	  break;
32519370Spst
32619370Spst	/* Long option that takes an argument.  */
32719370Spst	if (c == 0 && long_options[option_index].flag == 0)
32819370Spst	  c = long_options[option_index].val;
32919370Spst
33019370Spst	switch (c)
33119370Spst	  {
33219370Spst	  case 0:
33319370Spst	    /* Long option that just sets a flag.  */
33419370Spst	    break;
335130809Smarcel	  case OPT_SE:
33619370Spst	    symarg = optarg;
33719370Spst	    execarg = optarg;
33819370Spst	    break;
339130809Smarcel	  case OPT_CD:
34019370Spst	    cdarg = optarg;
34119370Spst	    break;
342130809Smarcel	  case OPT_ANNOTATE:
34319370Spst	    /* FIXME: what if the syntax is wrong (e.g. not digits)?  */
34419370Spst	    annotation_level = atoi (optarg);
34519370Spst	    break;
346130809Smarcel	  case OPT_STATISTICS:
34719370Spst	    /* Enable the display of both time and space usage.  */
34819370Spst	    display_time = 1;
34919370Spst	    display_space = 1;
35019370Spst	    break;
351130809Smarcel	  case OPT_TUI:
352130809Smarcel	    /* --tui is equivalent to -i=tui.  */
353130809Smarcel	    xfree (interpreter_p);
354130809Smarcel	    interpreter_p = xstrdup ("tui");
355130809Smarcel	    break;
356130809Smarcel	  case OPT_WINDOWS:
357130809Smarcel	    /* FIXME: cagney/2003-03-01: Not sure if this option is
358130809Smarcel               actually useful, and if it is, what it should do.  */
359130809Smarcel	    use_windows = 1;
360130809Smarcel	    break;
361130809Smarcel	  case OPT_NOWINDOWS:
362130809Smarcel	    /* -nw is equivalent to -i=console.  */
363130809Smarcel	    xfree (interpreter_p);
364130809Smarcel	    interpreter_p = xstrdup (INTERP_CONSOLE);
365130809Smarcel	    use_windows = 0;
366130809Smarcel	    break;
36719370Spst	  case 'f':
36819370Spst	    annotation_level = 1;
36919370Spst/* We have probably been invoked from emacs.  Disable window interface.  */
37019370Spst	    use_windows = 0;
37119370Spst	    break;
37219370Spst	  case 's':
37319370Spst	    symarg = optarg;
37419370Spst	    break;
37519370Spst	  case 'e':
37619370Spst	    execarg = optarg;
37719370Spst	    break;
37819370Spst	  case 'c':
37919370Spst	    corearg = optarg;
38019370Spst	    break;
38198948Sobrien	  case 'p':
38298948Sobrien	    /* "corearg" is shared by "--core" and "--pid" */
38398948Sobrien	    corearg = optarg;
38498948Sobrien	    break;
38519370Spst	  case 'x':
38619370Spst	    cmdarg[ncmd++] = optarg;
38719370Spst	    if (ncmd >= cmdsize)
38819370Spst	      {
38919370Spst		cmdsize *= 2;
39098948Sobrien		cmdarg = (char **) xrealloc ((char *) cmdarg,
39119370Spst					     cmdsize * sizeof (*cmdarg));
39219370Spst	      }
39319370Spst	    break;
39498948Sobrien#ifdef GDBTK
39598948Sobrien	  case 'z':
39698948Sobrien	    {
39798948Sobrienextern int gdbtk_test (char *);
39898948Sobrien	      if (!gdbtk_test (optarg))
39998948Sobrien		{
400130809Smarcel		  fprintf_unfiltered (gdb_stderr, _("%s: unable to load tclcommand file \"%s\""),
40198948Sobrien				      argv[0], optarg);
40298948Sobrien		  exit (1);
40398948Sobrien		}
40498948Sobrien	      break;
40598948Sobrien	    }
40698948Sobrien	  case 'y':
40798948Sobrien	    /* Backwards compatibility only.  */
40898948Sobrien	    break;
40998948Sobrien	  case 'w':
41098948Sobrien	    {
41198948Sobrien	      external_editor_command = xstrdup (optarg);
41298948Sobrien	      break;
41398948Sobrien	    }
41498948Sobrien#endif /* GDBTK */
41598948Sobrien	  case 'i':
416130809Smarcel	    xfree (interpreter_p);
417130809Smarcel	    interpreter_p = xstrdup (optarg);
41898948Sobrien	    break;
41919370Spst	  case 'd':
42019370Spst	    dirarg[ndir++] = optarg;
42119370Spst	    if (ndir >= dirsize)
42219370Spst	      {
42319370Spst		dirsize *= 2;
42498948Sobrien		dirarg = (char **) xrealloc ((char *) dirarg,
42519370Spst					     dirsize * sizeof (*dirarg));
42619370Spst	      }
42719370Spst	    break;
42819370Spst	  case 't':
42919370Spst	    ttyarg = optarg;
43019370Spst	    break;
43119370Spst	  case 'q':
43219370Spst	    quiet = 1;
43319370Spst	    break;
43419370Spst	  case 'b':
43519370Spst	    {
43619370Spst	      int i;
43719370Spst	      char *p;
43819370Spst
43919370Spst	      i = strtol (optarg, &p, 0);
44019370Spst	      if (i == 0 && p == optarg)
44119370Spst
44219370Spst		/* Don't use *_filtered or warning() (which relies on
44398948Sobrien		   current_target) until after initialize_all_files(). */
44419370Spst
44519370Spst		fprintf_unfiltered
44619370Spst		  (gdb_stderr,
447130809Smarcel		   _("warning: could not set baud rate to `%s'.\n"), optarg);
44819370Spst	      else
44919370Spst		baud_rate = i;
45019370Spst	    }
451130809Smarcel            break;
45246283Sdfr	  case 'l':
45346283Sdfr	    {
45446283Sdfr	      int i;
45546283Sdfr	      char *p;
45646283Sdfr
45746283Sdfr	      i = strtol (optarg, &p, 0);
45846283Sdfr	      if (i == 0 && p == optarg)
45946283Sdfr
46046283Sdfr		/* Don't use *_filtered or warning() (which relies on
46198948Sobrien		   current_target) until after initialize_all_files(). */
46246283Sdfr
46346283Sdfr		fprintf_unfiltered
46446283Sdfr		  (gdb_stderr,
465130809Smarcel		 _("warning: could not set timeout limit to `%s'.\n"), optarg);
46646283Sdfr	      else
46746283Sdfr		remote_timeout = i;
46846283Sdfr	    }
46919370Spst	    break;
47019370Spst
47119370Spst	  case '?':
47219370Spst	    fprintf_unfiltered (gdb_stderr,
473130809Smarcel			_("Use `%s --help' for a complete list of options.\n"),
47498948Sobrien				argv[0]);
47519370Spst	    exit (1);
47619370Spst	  }
47719370Spst      }
47819370Spst
47919370Spst    /* If --help or --version, disable window interface.  */
48019370Spst    if (print_help || print_version)
48146283Sdfr      {
48246283Sdfr	use_windows = 0;
48346283Sdfr      }
48446283Sdfr
48598948Sobrien    if (set_args)
48698948Sobrien      {
48798948Sobrien	/* The remaining options are the command-line options for the
48898948Sobrien	   inferior.  The first one is the sym/exec file, and the rest
48998948Sobrien	   are arguments.  */
49098948Sobrien	if (optind >= argc)
49198948Sobrien	  {
49298948Sobrien	    fprintf_unfiltered (gdb_stderr,
493130809Smarcel				_("%s: `--args' specified but no program specified\n"),
49498948Sobrien				argv[0]);
49598948Sobrien	    exit (1);
49698948Sobrien	  }
49798948Sobrien	symarg = argv[optind];
49898948Sobrien	execarg = argv[optind];
49998948Sobrien	++optind;
50098948Sobrien	set_inferior_args_vector (argc - optind, &argv[optind]);
50198948Sobrien      }
50298948Sobrien    else
50398948Sobrien      {
50498948Sobrien	/* OK, that's all the options.  The other arguments are filenames.  */
50598948Sobrien	count = 0;
50698948Sobrien	for (; optind < argc; optind++)
50798948Sobrien	  switch (++count)
50898948Sobrien	    {
50998948Sobrien	    case 1:
51098948Sobrien	      symarg = argv[optind];
51198948Sobrien	      execarg = argv[optind];
51298948Sobrien	      break;
51398948Sobrien	    case 2:
51498948Sobrien	      /* The documentation says this can be a "ProcID" as well.
51598948Sobrien	         We will try it as both a corefile and a pid.  */
51698948Sobrien	      corearg = argv[optind];
51798948Sobrien	      break;
51898948Sobrien	    case 3:
51998948Sobrien	      fprintf_unfiltered (gdb_stderr,
520130809Smarcel				  _("Excess command line arguments ignored. (%s%s)\n"),
52198948Sobrien				  argv[optind], (optind == argc - 1) ? "" : " ...");
52298948Sobrien	      break;
52398948Sobrien	    }
52498948Sobrien      }
52519370Spst    if (batch)
52619370Spst      quiet = 1;
52719370Spst  }
52819370Spst
52998948Sobrien  /* Initialize all files.  Give the interpreter a chance to take
53098948Sobrien     control of the console via the init_ui_hook()) */
53146283Sdfr  gdb_init (argv[0]);
53219370Spst
53319370Spst  /* Do these (and anything which might call wrap_here or *_filtered)
534130809Smarcel     after initialize_all_files() but before the interpreter has been
535130809Smarcel     installed.  Otherwize the help/version messages will be eaten by
536130809Smarcel     the interpreter's output handler.  */
537130809Smarcel
53819370Spst  if (print_version)
53919370Spst    {
54019370Spst      print_gdb_version (gdb_stdout);
54119370Spst      wrap_here ("");
54219370Spst      printf_filtered ("\n");
54319370Spst      exit (0);
54419370Spst    }
54519370Spst
54619370Spst  if (print_help)
54719370Spst    {
54846283Sdfr      print_gdb_help (gdb_stdout);
54946283Sdfr      fputs_unfiltered ("\n", gdb_stdout);
55019370Spst      exit (0);
55119370Spst    }
55219370Spst
553130809Smarcel  /* FIXME: cagney/2003-02-03: The big hack (part 1 of 2) that lets
554130809Smarcel     GDB retain the old MI1 interpreter startup behavior.  Output the
555130809Smarcel     copyright message before the interpreter is installed.  That way
556130809Smarcel     it isn't encapsulated in MI output.  */
557130809Smarcel  if (!quiet && strcmp (interpreter_p, INTERP_MI1) == 0)
55819370Spst    {
55919370Spst      /* Print all the junk at the top, with trailing "..." if we are about
56098948Sobrien         to read a symbol file (possibly slowly).  */
56119370Spst      print_gdb_version (gdb_stdout);
56219370Spst      if (symarg)
56319370Spst	printf_filtered ("..");
56498948Sobrien      wrap_here ("");
56598948Sobrien      gdb_flush (gdb_stdout);	/* Force to screen during slow operations */
56619370Spst    }
56719370Spst
568130809Smarcel
569130809Smarcel  /* Install the default UI.  All the interpreters should have had a
570130809Smarcel     look at things by now.  Initialize the default interpreter. */
571130809Smarcel
572130809Smarcel  {
573130809Smarcel    /* Find it.  */
574130809Smarcel    struct interp *interp = interp_lookup (interpreter_p);
575130809Smarcel    if (interp == NULL)
576130809Smarcel      error ("Interpreter `%s' unrecognized", interpreter_p);
577130809Smarcel    /* Install it.  */
578130809Smarcel    if (!interp_set (interp))
579130809Smarcel      {
580130809Smarcel        fprintf_unfiltered (gdb_stderr,
581130809Smarcel			    "Interpreter `%s' failed to initialize.\n",
582130809Smarcel                            interpreter_p);
583130809Smarcel        exit (1);
584130809Smarcel      }
585130809Smarcel  }
586130809Smarcel
587130809Smarcel  /* FIXME: cagney/2003-02-03: The big hack (part 2 of 2) that lets
588130809Smarcel     GDB retain the old MI1 interpreter startup behavior.  Output the
589130809Smarcel     copyright message after the interpreter is installed when it is
590130809Smarcel     any sane interpreter.  */
591130809Smarcel  if (!quiet && !current_interp_named_p (INTERP_MI1))
592130809Smarcel    {
593130809Smarcel      /* Print all the junk at the top, with trailing "..." if we are about
594130809Smarcel         to read a symbol file (possibly slowly).  */
595130809Smarcel      print_gdb_version (gdb_stdout);
596130809Smarcel      if (symarg)
597130809Smarcel	printf_filtered ("..");
598130809Smarcel      wrap_here ("");
599130809Smarcel      gdb_flush (gdb_stdout);	/* Force to screen during slow operations */
600130809Smarcel    }
601130809Smarcel
60219370Spst  error_pre_print = "\n\n";
60319370Spst  quit_pre_print = error_pre_print;
60419370Spst
60519370Spst  /* We may get more than one warning, don't double space all of them... */
606130809Smarcel  warning_pre_print = _("\nwarning: ");
60719370Spst
60819370Spst  /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
60919370Spst     *before* all the command line arguments are processed; it sets
61019370Spst     global parameters, which are independent of what file you are
61119370Spst     debugging or what directory you are in.  */
61298948Sobrien  homedir = getenv ("HOME");
61319370Spst  if (homedir)
61419370Spst    {
61546283Sdfr      homeinit = (char *) alloca (strlen (homedir) +
61619370Spst				  strlen (gdbinit) + 10);
61746283Sdfr      strcpy (homeinit, homedir);
61819370Spst      strcat (homeinit, "/");
61919370Spst      strcat (homeinit, gdbinit);
62019370Spst
62146283Sdfr      if (!inhibit_gdbinit)
62219370Spst	{
62398948Sobrien	  catch_command_errors (source_command, homeinit, 0, RETURN_MASK_ALL);
62419370Spst	}
62519370Spst
62619370Spst      /* Do stats; no need to do them elsewhere since we'll only
62798948Sobrien         need them if homedir is set.  Make sure that they are
62898948Sobrien         zero in case one of them fails (this guarantees that they
62998948Sobrien         won't match if either exists).  */
63098948Sobrien
63119370Spst      memset (&homebuf, 0, sizeof (struct stat));
63219370Spst      memset (&cwdbuf, 0, sizeof (struct stat));
63398948Sobrien
63419370Spst      stat (homeinit, &homebuf);
63598948Sobrien      stat (gdbinit, &cwdbuf);	/* We'll only need this if
63698948Sobrien				   homedir was set.  */
63719370Spst    }
63819370Spst
63919370Spst  /* Now perform all the actions indicated by the arguments.  */
64019370Spst  if (cdarg != NULL)
64119370Spst    {
64298948Sobrien      catch_command_errors (cd_command, cdarg, 0, RETURN_MASK_ALL);
64319370Spst    }
64419370Spst
64519370Spst  for (i = 0; i < ndir; i++)
64698948Sobrien    catch_command_errors (directory_command, dirarg[i], 0, RETURN_MASK_ALL);
64798948Sobrien  xfree (dirarg);
64819370Spst
64919370Spst  if (execarg != NULL
65019370Spst      && symarg != NULL
651130809Smarcel      && strcmp (execarg, symarg) == 0)
65219370Spst    {
65398948Sobrien      /* The exec file and the symbol-file are the same.  If we can't
65498948Sobrien         open it, better only print one error message.
65598948Sobrien         catch_command_errors returns non-zero on success! */
65698948Sobrien      if (catch_command_errors (exec_file_attach, execarg, !batch, RETURN_MASK_ALL))
65798948Sobrien	catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL);
65819370Spst    }
65919370Spst  else
66019370Spst    {
66119370Spst      if (execarg != NULL)
66298948Sobrien	catch_command_errors (exec_file_attach, execarg, !batch, RETURN_MASK_ALL);
66319370Spst      if (symarg != NULL)
66498948Sobrien	catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL);
66519370Spst    }
66619370Spst
66719370Spst  /* After the symbol file has been read, print a newline to get us
66819370Spst     beyond the copyright line...  But errors should still set off
66919370Spst     the error message with a (single) blank line.  */
67019370Spst  if (!quiet)
67119370Spst    printf_filtered ("\n");
67219370Spst  error_pre_print = "\n";
67319370Spst  quit_pre_print = error_pre_print;
674130809Smarcel  warning_pre_print = _("\nwarning: ");
67519370Spst
67619370Spst  if (corearg != NULL)
67746283Sdfr    {
67898948Sobrien      /* corearg may be either a corefile or a pid.
67998948Sobrien	 If its first character is a digit, try attach first
68098948Sobrien	 and then corefile.  Otherwise try corefile first. */
68198948Sobrien
68298948Sobrien      if (isdigit (corearg[0]))
68398948Sobrien	{
68498948Sobrien	  if (catch_command_errors (attach_command, corearg,
68598948Sobrien				    !batch, RETURN_MASK_ALL) == 0)
68698948Sobrien	    catch_command_errors (core_file_command, corearg,
68798948Sobrien				  !batch, RETURN_MASK_ALL);
68898948Sobrien	}
68998948Sobrien      else /* Can't be a pid, better be a corefile. */
69098948Sobrien	catch_command_errors (core_file_command, corearg,
69198948Sobrien			      !batch, RETURN_MASK_ALL);
69246283Sdfr    }
69319370Spst
69419370Spst  if (ttyarg != NULL)
69598948Sobrien    catch_command_errors (tty_command, ttyarg, !batch, RETURN_MASK_ALL);
69619370Spst
69719370Spst  /* Error messages should no longer be distinguished with extra output. */
69819370Spst  error_pre_print = NULL;
69919370Spst  quit_pre_print = NULL;
700130809Smarcel  warning_pre_print = _("warning: ");
70119370Spst
70219370Spst  /* Read the .gdbinit file in the current directory, *if* it isn't
70319370Spst     the same as the $HOME/.gdbinit file (it should exist, also).  */
70498948Sobrien
70519370Spst  if (!homedir
70619370Spst      || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
70746283Sdfr    if (!inhibit_gdbinit)
70819370Spst      {
70998948Sobrien	catch_command_errors (source_command, gdbinit, 0, RETURN_MASK_ALL);
71019370Spst      }
71119370Spst
71219370Spst  for (i = 0; i < ncmd; i++)
71319370Spst    {
71498948Sobrien#if 0
71598948Sobrien      /* NOTE: cagney/1999-11-03: SET_TOP_LEVEL() was a macro that
71698948Sobrien         expanded into a call to setjmp().  */
71798948Sobrien      if (!SET_TOP_LEVEL ()) /* NB: This is #if 0'd out */
71819370Spst	{
71998948Sobrien	  /* NOTE: I am commenting this out, because it is not clear
72098948Sobrien	     where this feature is used. It is very old and
72198948Sobrien	     undocumented. ezannoni: 1999-05-04 */
72298948Sobrien#if 0
72319370Spst	  if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
72419370Spst	    read_command_file (stdin);
72519370Spst	  else
72698948Sobrien#endif
72719370Spst	    source_command (cmdarg[i], !batch);
72819370Spst	  do_cleanups (ALL_CLEANUPS);
72919370Spst	}
73098948Sobrien#endif
73198948Sobrien      catch_command_errors (source_command, cmdarg[i], !batch, RETURN_MASK_ALL);
73219370Spst    }
73398948Sobrien  xfree (cmdarg);
73419370Spst
73519370Spst  /* Read in the old history after all the command files have been read. */
73698948Sobrien  init_history ();
73719370Spst
73819370Spst  if (batch)
73919370Spst    {
74019370Spst      /* We have hit the end of the batch file.  */
74119370Spst      exit (0);
74219370Spst    }
74319370Spst
74419370Spst  /* Do any host- or target-specific hacks.  This is used for i960 targets
74519370Spst     to force the user to set a nindy target and spec its parameters.  */
74619370Spst
74719370Spst#ifdef BEFORE_MAIN_LOOP_HOOK
74819370Spst  BEFORE_MAIN_LOOP_HOOK;
74919370Spst#endif
75019370Spst
75119370Spst  /* Show time and/or space usage.  */
75219370Spst
75319370Spst  if (display_time)
75419370Spst    {
75519370Spst      long init_time = get_run_time () - time_at_startup;
75619370Spst
757130809Smarcel      printf_unfiltered (_("Startup time: %ld.%06ld\n"),
75819370Spst			 init_time / 1000000, init_time % 1000000);
75919370Spst    }
76019370Spst
76119370Spst  if (display_space)
76219370Spst    {
76319370Spst#ifdef HAVE_SBRK
76419370Spst      extern char **environ;
76519370Spst      char *lim = (char *) sbrk (0);
76619370Spst
767130809Smarcel      printf_unfiltered (_("Startup size: data size %ld\n"),
76819370Spst			 (long) (lim - (char *) &environ));
76919370Spst#endif
77019370Spst    }
77119370Spst
77298948Sobrien#if 0
77398948Sobrien  /* FIXME: cagney/1999-11-06: The original main loop was like: */
77419370Spst  while (1)
77519370Spst    {
77619370Spst      if (!SET_TOP_LEVEL ())
77719370Spst	{
77898948Sobrien	  do_cleanups (ALL_CLEANUPS);	/* Do complete cleanup */
77919370Spst	  /* GUIs generally have their own command loop, mainloop, or whatever.
78019370Spst	     This is a good place to gain control because many error
78119370Spst	     conditions will end up here via longjmp(). */
78219370Spst	  if (command_loop_hook)
78319370Spst	    command_loop_hook ();
78419370Spst	  else
78519370Spst	    command_loop ();
78698948Sobrien	  quit_command ((char *) 0, instream == stdin);
78719370Spst	}
78819370Spst    }
78998948Sobrien  /* NOTE: If the command_loop() returned normally, the loop would
79098948Sobrien     attempt to exit by calling the function quit_command().  That
79198948Sobrien     function would either call exit() or throw an error returning
79298948Sobrien     control to SET_TOP_LEVEL. */
79398948Sobrien  /* NOTE: The function do_cleanups() was called once each time round
79498948Sobrien     the loop.  The usefulness of the call isn't clear.  If an error
79598948Sobrien     was thrown, everything would have already been cleaned up.  If
79698948Sobrien     command_loop() returned normally and quit_command() was called,
79798948Sobrien     either exit() or error() (again cleaning up) would be called. */
79898948Sobrien#endif
79998948Sobrien  /* NOTE: cagney/1999-11-07: There is probably no reason for not
80098948Sobrien     moving this loop and the code found in captured_command_loop()
80198948Sobrien     into the command_loop() proper.  The main thing holding back that
80298948Sobrien     change - SET_TOP_LEVEL() - has been eliminated. */
80398948Sobrien  while (1)
80498948Sobrien    {
80598948Sobrien      catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
80698948Sobrien    }
80719370Spst  /* No exit -- exit is through quit_command.  */
80898948Sobrien}
80919370Spst
81098948Sobrienint
811130809Smarcelgdb_main (struct captured_main_args *args)
81298948Sobrien{
813130809Smarcel  use_windows = args->use_windows;
814130809Smarcel  catch_errors (captured_main, args, "", RETURN_MASK_ALL);
815130809Smarcel  /* The only way to end up here is by an error (normal exit is
816130809Smarcel     handled by quit_force()), hence always return an error status.  */
817130809Smarcel  return 1;
81819370Spst}
81946283Sdfr
82098948Sobrien
82146283Sdfr/* Don't use *_filtered for printing help.  We don't want to prompt
82246283Sdfr   for continue no matter how small the screen or how much we're going
82346283Sdfr   to print.  */
82446283Sdfr
82546283Sdfrstatic void
82698948Sobrienprint_gdb_help (struct ui_file *stream)
82746283Sdfr{
828130809Smarcel  fputs_unfiltered (_("\
82946283SdfrThis is the GNU debugger.  Usage:\n\n\
83098948Sobrien    gdb [options] [executable-file [core-file or process-id]]\n\
83198948Sobrien    gdb [options] --args executable-file [inferior-arguments ...]\n\n\
83246283SdfrOptions:\n\n\
833130809Smarcel"), stream);
834130809Smarcel  fputs_unfiltered (_("\
83598948Sobrien  --args             Arguments after executable-file are passed to inferior\n\
836130809Smarcel"), stream);
837130809Smarcel  fputs_unfiltered (_("\
83898948Sobrien  --[no]async        Enable (disable) asynchronous version of CLI\n\
839130809Smarcel"), stream);
840130809Smarcel  fputs_unfiltered (_("\
84146283Sdfr  -b BAUDRATE        Set serial port baud rate used for remote debugging.\n\
84246283Sdfr  --batch            Exit after processing options.\n\
84346283Sdfr  --cd=DIR           Change current directory to DIR.\n\
84446283Sdfr  --command=FILE     Execute GDB commands from FILE.\n\
84546283Sdfr  --core=COREFILE    Analyze the core dump COREFILE.\n\
84698948Sobrien  --pid=PID          Attach to running process PID.\n\
847130809Smarcel"), stream);
848130809Smarcel  fputs_unfiltered (_("\
84946283Sdfr  --dbx              DBX compatibility mode.\n\
85046283Sdfr  --directory=DIR    Search for source files in DIR.\n\
85146283Sdfr  --epoch            Output information used by epoch emacs-GDB interface.\n\
85246283Sdfr  --exec=EXECFILE    Use EXECFILE as the executable.\n\
85346283Sdfr  --fullname         Output information used by emacs-GDB interface.\n\
85446283Sdfr  --help             Print this message.\n\
855130809Smarcel"), stream);
856130809Smarcel  fputs_unfiltered (_("\
85798948Sobrien  --interpreter=INTERP\n\
85898948Sobrien                     Select a specific interpreter / user interface\n\
859130809Smarcel"), stream);
860130809Smarcel  fputs_unfiltered (_("\
86146283Sdfr  --mapped           Use mapped symbol files if supported on this system.\n\
86246283Sdfr  --nw		     Do not use a window interface.\n\
863130809Smarcel  --nx               Do not read "), stream);
86498948Sobrien  fputs_unfiltered (gdbinit, stream);
865130809Smarcel  fputs_unfiltered (_(" file.\n\
86646283Sdfr  --quiet            Do not print version number on startup.\n\
86746283Sdfr  --readnow          Fully read symbol files on first access.\n\
868130809Smarcel"), stream);
869130809Smarcel  fputs_unfiltered (_("\
87046283Sdfr  --se=FILE          Use FILE as symbol file and executable file.\n\
87146283Sdfr  --symbols=SYMFILE  Read symbols from SYMFILE.\n\
87246283Sdfr  --tty=TTY          Use TTY for input/output by the program being debugged.\n\
873130809Smarcel"), stream);
87446283Sdfr#if defined(TUI)
875130809Smarcel  fputs_unfiltered (_("\
87646283Sdfr  --tui              Use a terminal user interface.\n\
877130809Smarcel"), stream);
87846283Sdfr#endif
879130809Smarcel  fputs_unfiltered (_("\
88046283Sdfr  --version          Print version information and then exit.\n\
88146283Sdfr  -w                 Use a window interface.\n\
88246283Sdfr  --write            Set writing into executable and core files.\n\
88346283Sdfr  --xdb              XDB compatibility mode.\n\
884130809Smarcel"), stream);
885130809Smarcel  fputs_unfiltered (_("\n\
88646283SdfrFor more information, type \"help\" from within GDB, or consult the\n\
88746283SdfrGDB manual (available as on-line info or a printed manual).\n\
88898948SobrienReport bugs to \"bug-gdb@gnu.org\".\
889130809Smarcel"), stream);
89046283Sdfr}
891