xref: /dflybsd-src/contrib/gdb-7/gdb/main.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Top level stuff for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
45796c8dcSSimon Schubert    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
5*cf7f2e2dSJohn Marino    2009, 2010 Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "top.h"
245796c8dcSSimon Schubert #include "target.h"
255796c8dcSSimon Schubert #include "inferior.h"
265796c8dcSSimon Schubert #include "symfile.h"
275796c8dcSSimon Schubert #include "gdbcore.h"
285796c8dcSSimon Schubert 
295796c8dcSSimon Schubert #include "exceptions.h"
305796c8dcSSimon Schubert #include "getopt.h"
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert #include <sys/types.h>
335796c8dcSSimon Schubert #include "gdb_stat.h"
345796c8dcSSimon Schubert #include <ctype.h>
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert #include "gdb_string.h"
375796c8dcSSimon Schubert #include "event-loop.h"
385796c8dcSSimon Schubert #include "ui-out.h"
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert #include "interps.h"
415796c8dcSSimon Schubert #include "main.h"
425796c8dcSSimon Schubert #include "source.h"
43*cf7f2e2dSJohn Marino #include "cli/cli-cmds.h"
44*cf7f2e2dSJohn Marino #include "python/python.h"
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert /* The selected interpreter.  This will be used as a set command
475796c8dcSSimon Schubert    variable, so it should always be malloc'ed - since
485796c8dcSSimon Schubert    do_setshow_command will free it. */
495796c8dcSSimon Schubert char *interpreter_p;
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert /* Whether xdb commands will be handled */
525796c8dcSSimon Schubert int xdb_commands = 0;
535796c8dcSSimon Schubert 
545796c8dcSSimon Schubert /* Whether dbx commands will be handled */
555796c8dcSSimon Schubert int dbx_commands = 0;
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert /* System root path, used to find libraries etc.  */
585796c8dcSSimon Schubert char *gdb_sysroot = 0;
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert /* GDB datadir, used to store data files.  */
615796c8dcSSimon Schubert char *gdb_datadir = 0;
625796c8dcSSimon Schubert 
63*cf7f2e2dSJohn Marino /* If gdb was configured with --with-python=/path,
64*cf7f2e2dSJohn Marino    the possibly relocated path to python's lib directory.  */
65*cf7f2e2dSJohn Marino char *python_libdir = 0;
66*cf7f2e2dSJohn Marino 
675796c8dcSSimon Schubert struct ui_file *gdb_stdout;
685796c8dcSSimon Schubert struct ui_file *gdb_stderr;
695796c8dcSSimon Schubert struct ui_file *gdb_stdlog;
705796c8dcSSimon Schubert struct ui_file *gdb_stdin;
715796c8dcSSimon Schubert /* target IO streams */
725796c8dcSSimon Schubert struct ui_file *gdb_stdtargin;
735796c8dcSSimon Schubert struct ui_file *gdb_stdtarg;
745796c8dcSSimon Schubert struct ui_file *gdb_stdtargerr;
755796c8dcSSimon Schubert 
76*cf7f2e2dSJohn Marino /* True if --batch or --batch-silent was seen.  */
77*cf7f2e2dSJohn Marino int batch_flag = 0;
78*cf7f2e2dSJohn Marino 
795796c8dcSSimon Schubert /* Support for the --batch-silent option.  */
805796c8dcSSimon Schubert int batch_silent = 0;
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert /* Support for --return-child-result option.
835796c8dcSSimon Schubert    Set the default to -1 to return error in the case
845796c8dcSSimon Schubert    that the program does not run or does not complete.  */
855796c8dcSSimon Schubert int return_child_result = 0;
865796c8dcSSimon Schubert int return_child_result_value = -1;
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert /* Whether to enable writing into executable and core files */
895796c8dcSSimon Schubert extern int write_files;
905796c8dcSSimon Schubert 
915796c8dcSSimon Schubert /* GDB as it has been invoked from the command line (i.e. argv[0]).  */
925796c8dcSSimon Schubert static char *gdb_program_name;
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert static void print_gdb_help (struct ui_file *);
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert /* These two are used to set the external editor commands when gdb is farming
975796c8dcSSimon Schubert    out files to be edited by another program. */
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert extern char *external_editor_command;
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert /* Relocate a file or directory.  PROGNAME is the name by which gdb
1025796c8dcSSimon Schubert    was invoked (i.e., argv[0]).  INITIAL is the default value for the
1035796c8dcSSimon Schubert    file or directory.  FLAG is true if the value is relocatable, false
1045796c8dcSSimon Schubert    otherwise.  Returns a newly allocated string; this may return NULL
1055796c8dcSSimon Schubert    under the same conditions as make_relative_prefix.  */
1065796c8dcSSimon Schubert static char *
1075796c8dcSSimon Schubert relocate_path (const char *progname, const char *initial, int flag)
1085796c8dcSSimon Schubert {
1095796c8dcSSimon Schubert   if (flag)
1105796c8dcSSimon Schubert     return make_relative_prefix (progname, BINDIR, initial);
1115796c8dcSSimon Schubert   return xstrdup (initial);
1125796c8dcSSimon Schubert }
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert /* Like relocate_path, but specifically checks for a directory.
1155796c8dcSSimon Schubert    INITIAL is relocated according to the rules of relocate_path.  If
1165796c8dcSSimon Schubert    the result is a directory, it is used; otherwise, INITIAL is used.
1175796c8dcSSimon Schubert    The chosen directory is then canonicalized using lrealpath.  This
1185796c8dcSSimon Schubert    function always returns a newly-allocated string.  */
1195796c8dcSSimon Schubert static char *
1205796c8dcSSimon Schubert relocate_directory (const char *progname, const char *initial, int flag)
1215796c8dcSSimon Schubert {
1225796c8dcSSimon Schubert   char *dir;
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert   dir = relocate_path (progname, initial, flag);
1255796c8dcSSimon Schubert   if (dir)
1265796c8dcSSimon Schubert     {
1275796c8dcSSimon Schubert       struct stat s;
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert       if (stat (dir, &s) != 0 || !S_ISDIR (s.st_mode))
1305796c8dcSSimon Schubert 	{
1315796c8dcSSimon Schubert 	  xfree (dir);
1325796c8dcSSimon Schubert 	  dir = NULL;
1335796c8dcSSimon Schubert 	}
1345796c8dcSSimon Schubert     }
1355796c8dcSSimon Schubert   if (!dir)
1365796c8dcSSimon Schubert     dir = xstrdup (initial);
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert   /* Canonicalize the directory.  */
1395796c8dcSSimon Schubert   if (*dir)
1405796c8dcSSimon Schubert     {
1415796c8dcSSimon Schubert       char *canon_sysroot = lrealpath (dir);
142*cf7f2e2dSJohn Marino 
1435796c8dcSSimon Schubert       if (canon_sysroot)
1445796c8dcSSimon Schubert 	{
1455796c8dcSSimon Schubert 	  xfree (dir);
1465796c8dcSSimon Schubert 	  dir = canon_sysroot;
1475796c8dcSSimon Schubert 	}
1485796c8dcSSimon Schubert     }
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   return dir;
1515796c8dcSSimon Schubert }
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert /* Compute the locations of init files that GDB should source and return
1545796c8dcSSimon Schubert    them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT.  If there is
1555796c8dcSSimon Schubert    no system gdbinit (resp. home gdbinit and local gdbinit) to be loaded,
1565796c8dcSSimon Schubert    then SYSTEM_GDBINIT (resp. HOME_GDBINIT and LOCAL_GDBINIT) is set to
1575796c8dcSSimon Schubert    NULL.  */
1585796c8dcSSimon Schubert static void
1595796c8dcSSimon Schubert get_init_files (char **system_gdbinit,
1605796c8dcSSimon Schubert 		char **home_gdbinit,
1615796c8dcSSimon Schubert 		char **local_gdbinit)
1625796c8dcSSimon Schubert {
1635796c8dcSSimon Schubert   static char *sysgdbinit = NULL;
1645796c8dcSSimon Schubert   static char *homeinit = NULL;
1655796c8dcSSimon Schubert   static char *localinit = NULL;
1665796c8dcSSimon Schubert   static int initialized = 0;
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert   if (!initialized)
1695796c8dcSSimon Schubert     {
1705796c8dcSSimon Schubert       struct stat homebuf, cwdbuf, s;
1715796c8dcSSimon Schubert       char *homedir, *relocated_sysgdbinit;
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert       if (SYSTEM_GDBINIT[0])
1745796c8dcSSimon Schubert 	{
1755796c8dcSSimon Schubert 	  relocated_sysgdbinit = relocate_path (gdb_program_name,
1765796c8dcSSimon Schubert 						SYSTEM_GDBINIT,
1775796c8dcSSimon Schubert 						SYSTEM_GDBINIT_RELOCATABLE);
1785796c8dcSSimon Schubert 	  if (relocated_sysgdbinit && stat (relocated_sysgdbinit, &s) == 0)
1795796c8dcSSimon Schubert 	    sysgdbinit = relocated_sysgdbinit;
1805796c8dcSSimon Schubert 	  else
1815796c8dcSSimon Schubert 	    xfree (relocated_sysgdbinit);
1825796c8dcSSimon Schubert 	}
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert       homedir = getenv ("HOME");
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert       /* If the .gdbinit file in the current directory is the same as
1875796c8dcSSimon Schubert 	 the $HOME/.gdbinit file, it should not be sourced.  homebuf
1885796c8dcSSimon Schubert 	 and cwdbuf are used in that purpose. Make sure that the stats
1895796c8dcSSimon Schubert 	 are zero in case one of them fails (this guarantees that they
1905796c8dcSSimon Schubert 	 won't match if either exists).  */
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert       memset (&homebuf, 0, sizeof (struct stat));
1935796c8dcSSimon Schubert       memset (&cwdbuf, 0, sizeof (struct stat));
1945796c8dcSSimon Schubert 
1955796c8dcSSimon Schubert       if (homedir)
1965796c8dcSSimon Schubert 	{
1975796c8dcSSimon Schubert 	  homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
1985796c8dcSSimon Schubert 	  if (stat (homeinit, &homebuf) != 0)
1995796c8dcSSimon Schubert 	    {
2005796c8dcSSimon Schubert 	      xfree (homeinit);
2015796c8dcSSimon Schubert 	      homeinit = NULL;
2025796c8dcSSimon Schubert 	    }
2035796c8dcSSimon Schubert 	}
2045796c8dcSSimon Schubert 
2055796c8dcSSimon Schubert       if (stat (gdbinit, &cwdbuf) == 0)
2065796c8dcSSimon Schubert 	{
2075796c8dcSSimon Schubert 	  if (!homeinit
2085796c8dcSSimon Schubert 	      || memcmp ((char *) &homebuf, (char *) &cwdbuf,
2095796c8dcSSimon Schubert 			 sizeof (struct stat)))
2105796c8dcSSimon Schubert 	    localinit = gdbinit;
2115796c8dcSSimon Schubert 	}
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert       initialized = 1;
2145796c8dcSSimon Schubert     }
2155796c8dcSSimon Schubert 
2165796c8dcSSimon Schubert   *system_gdbinit = sysgdbinit;
2175796c8dcSSimon Schubert   *home_gdbinit = homeinit;
2185796c8dcSSimon Schubert   *local_gdbinit = localinit;
2195796c8dcSSimon Schubert }
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert /* Call command_loop.  If it happens to return, pass that through as a
2225796c8dcSSimon Schubert    non-zero return status. */
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert static int
2255796c8dcSSimon Schubert captured_command_loop (void *data)
2265796c8dcSSimon Schubert {
2275796c8dcSSimon Schubert   current_interp_command_loop ();
2285796c8dcSSimon Schubert   /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
2295796c8dcSSimon Schubert      would clean things up (restoring the cleanup chain) to the state
2305796c8dcSSimon Schubert      they were just prior to the call.  Technically, this means that
2315796c8dcSSimon Schubert      the do_cleanups() below is redundant.  Unfortunately, many FUNCs
2325796c8dcSSimon Schubert      are not that well behaved.  do_cleanups should either be replaced
2335796c8dcSSimon Schubert      with a do_cleanups call (to cover the problem) or an assertion
2345796c8dcSSimon Schubert      check to detect bad FUNCs code. */
2355796c8dcSSimon Schubert   do_cleanups (ALL_CLEANUPS);
2365796c8dcSSimon Schubert   /* If the command_loop returned, normally (rather than threw an
2375796c8dcSSimon Schubert      error) we try to quit. If the quit is aborted, catch_errors()
2385796c8dcSSimon Schubert      which called this catch the signal and restart the command
2395796c8dcSSimon Schubert      loop. */
2405796c8dcSSimon Schubert   quit_command (NULL, instream == stdin);
2415796c8dcSSimon Schubert   return 1;
2425796c8dcSSimon Schubert }
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert static int
2455796c8dcSSimon Schubert captured_main (void *data)
2465796c8dcSSimon Schubert {
2475796c8dcSSimon Schubert   struct captured_main_args *context = data;
2485796c8dcSSimon Schubert   int argc = context->argc;
2495796c8dcSSimon Schubert   char **argv = context->argv;
2505796c8dcSSimon Schubert   static int quiet = 0;
2515796c8dcSSimon Schubert   static int set_args = 0;
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert   /* Pointers to various arguments from command line.  */
2545796c8dcSSimon Schubert   char *symarg = NULL;
2555796c8dcSSimon Schubert   char *execarg = NULL;
2565796c8dcSSimon Schubert   char *pidarg = NULL;
2575796c8dcSSimon Schubert   char *corearg = NULL;
2585796c8dcSSimon Schubert   char *pid_or_core_arg = NULL;
2595796c8dcSSimon Schubert   char *cdarg = NULL;
2605796c8dcSSimon Schubert   char *ttyarg = NULL;
2615796c8dcSSimon Schubert 
2625796c8dcSSimon Schubert   /* These are static so that we can take their address in an initializer.  */
2635796c8dcSSimon Schubert   static int print_help;
2645796c8dcSSimon Schubert   static int print_version;
2655796c8dcSSimon Schubert 
2665796c8dcSSimon Schubert   /* Pointers to all arguments of --command option.  */
2675796c8dcSSimon Schubert   struct cmdarg {
2685796c8dcSSimon Schubert     enum {
2695796c8dcSSimon Schubert       CMDARG_FILE,
2705796c8dcSSimon Schubert       CMDARG_COMMAND
2715796c8dcSSimon Schubert     } type;
2725796c8dcSSimon Schubert     char *string;
2735796c8dcSSimon Schubert   } *cmdarg;
2745796c8dcSSimon Schubert   /* Allocated size of cmdarg.  */
2755796c8dcSSimon Schubert   int cmdsize;
2765796c8dcSSimon Schubert   /* Number of elements of cmdarg used.  */
2775796c8dcSSimon Schubert   int ncmd;
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert   /* Indices of all arguments of --directory option.  */
2805796c8dcSSimon Schubert   char **dirarg;
2815796c8dcSSimon Schubert   /* Allocated size.  */
2825796c8dcSSimon Schubert   int dirsize;
2835796c8dcSSimon Schubert   /* Number of elements used.  */
2845796c8dcSSimon Schubert   int ndir;
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert   /* gdb init files.  */
2875796c8dcSSimon Schubert   char *system_gdbinit;
2885796c8dcSSimon Schubert   char *home_gdbinit;
2895796c8dcSSimon Schubert   char *local_gdbinit;
2905796c8dcSSimon Schubert 
2915796c8dcSSimon Schubert   int i;
292*cf7f2e2dSJohn Marino   int save_auto_load;
2935796c8dcSSimon Schubert 
294*cf7f2e2dSJohn Marino   struct cleanup *pre_stat_chain = make_command_stats_cleanup (0);
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2975796c8dcSSimon Schubert   setlocale (LC_MESSAGES, "");
2985796c8dcSSimon Schubert #endif
2995796c8dcSSimon Schubert #if defined (HAVE_SETLOCALE)
3005796c8dcSSimon Schubert   setlocale (LC_CTYPE, "");
3015796c8dcSSimon Schubert #endif
3025796c8dcSSimon Schubert   bindtextdomain (PACKAGE, LOCALEDIR);
3035796c8dcSSimon Schubert   textdomain (PACKAGE);
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert #ifdef HAVE_SBRK
3065796c8dcSSimon Schubert   lim_at_start = (char *) sbrk (0);
3075796c8dcSSimon Schubert #endif
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert   cmdsize = 1;
3105796c8dcSSimon Schubert   cmdarg = (struct cmdarg *) xmalloc (cmdsize * sizeof (*cmdarg));
3115796c8dcSSimon Schubert   ncmd = 0;
3125796c8dcSSimon Schubert   dirsize = 1;
3135796c8dcSSimon Schubert   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
3145796c8dcSSimon Schubert   ndir = 0;
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert   quit_flag = 0;
3175796c8dcSSimon Schubert   line = (char *) xmalloc (linesize);
3185796c8dcSSimon Schubert   line[0] = '\0';		/* Terminate saved (now empty) cmd line */
3195796c8dcSSimon Schubert   instream = stdin;
3205796c8dcSSimon Schubert 
3215796c8dcSSimon Schubert   gdb_stdout = stdio_fileopen (stdout);
3225796c8dcSSimon Schubert   gdb_stderr = stdio_fileopen (stderr);
3235796c8dcSSimon Schubert   gdb_stdlog = gdb_stderr;	/* for moment */
3245796c8dcSSimon Schubert   gdb_stdtarg = gdb_stderr;	/* for moment */
3255796c8dcSSimon Schubert   gdb_stdin = stdio_fileopen (stdin);
3265796c8dcSSimon Schubert   gdb_stdtargerr = gdb_stderr;	/* for moment */
3275796c8dcSSimon Schubert   gdb_stdtargin = gdb_stdin;	/* for moment */
3285796c8dcSSimon Schubert 
3295796c8dcSSimon Schubert   gdb_program_name = xstrdup (argv[0]);
3305796c8dcSSimon Schubert 
3315796c8dcSSimon Schubert   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
3325796c8dcSSimon Schubert     /* Don't use *_filtered or warning() (which relies on
3335796c8dcSSimon Schubert        current_target) until after initialize_all_files(). */
3345796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stderr,
3355796c8dcSSimon Schubert                         _("%s: warning: error finding working directory: %s\n"),
3365796c8dcSSimon Schubert                         argv[0], safe_strerror (errno));
3375796c8dcSSimon Schubert 
3385796c8dcSSimon Schubert   current_directory = gdb_dirbuf;
3395796c8dcSSimon Schubert 
3405796c8dcSSimon Schubert   /* Set the sysroot path.  */
3415796c8dcSSimon Schubert   gdb_sysroot = relocate_directory (argv[0], TARGET_SYSTEM_ROOT,
3425796c8dcSSimon Schubert 				    TARGET_SYSTEM_ROOT_RELOCATABLE);
3435796c8dcSSimon Schubert 
3445796c8dcSSimon Schubert   debug_file_directory = relocate_directory (argv[0], DEBUGDIR,
3455796c8dcSSimon Schubert 					     DEBUGDIR_RELOCATABLE);
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert   gdb_datadir = relocate_directory (argv[0], GDB_DATADIR,
3485796c8dcSSimon Schubert 				    GDB_DATADIR_RELOCATABLE);
3495796c8dcSSimon Schubert 
350*cf7f2e2dSJohn Marino #ifdef WITH_PYTHON_PATH
351*cf7f2e2dSJohn Marino   /* For later use in helping Python find itself.  */
352*cf7f2e2dSJohn Marino   python_libdir = relocate_directory (argv[0],
353*cf7f2e2dSJohn Marino 				      concat (WITH_PYTHON_PATH,
354*cf7f2e2dSJohn Marino 					      SLASH_STRING, "lib", NULL),
355*cf7f2e2dSJohn Marino 				      PYTHON_PATH_RELOCATABLE);
356*cf7f2e2dSJohn Marino #endif
357*cf7f2e2dSJohn Marino 
3585796c8dcSSimon Schubert #ifdef RELOC_SRCDIR
3595796c8dcSSimon Schubert   add_substitute_path_rule (RELOC_SRCDIR,
3605796c8dcSSimon Schubert 			    make_relative_prefix (argv[0], BINDIR,
3615796c8dcSSimon Schubert 						  RELOC_SRCDIR));
3625796c8dcSSimon Schubert #endif
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert   /* There will always be an interpreter.  Either the one passed into
3655796c8dcSSimon Schubert      this captured main, or one specified by the user at start up, or
3665796c8dcSSimon Schubert      the console.  Initialize the interpreter to the one requested by
3675796c8dcSSimon Schubert      the application.  */
3685796c8dcSSimon Schubert   interpreter_p = xstrdup (context->interpreter_p);
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert   /* Parse arguments and options.  */
3715796c8dcSSimon Schubert   {
3725796c8dcSSimon Schubert     int c;
3735796c8dcSSimon Schubert     /* When var field is 0, use flag field to record the equivalent
3745796c8dcSSimon Schubert        short option (or arbitrary numbers starting at 10 for those
3755796c8dcSSimon Schubert        with no equivalent).  */
3765796c8dcSSimon Schubert     enum {
3775796c8dcSSimon Schubert       OPT_SE = 10,
3785796c8dcSSimon Schubert       OPT_CD,
3795796c8dcSSimon Schubert       OPT_ANNOTATE,
3805796c8dcSSimon Schubert       OPT_STATISTICS,
3815796c8dcSSimon Schubert       OPT_TUI,
3825796c8dcSSimon Schubert       OPT_NOWINDOWS,
3835796c8dcSSimon Schubert       OPT_WINDOWS
3845796c8dcSSimon Schubert     };
3855796c8dcSSimon Schubert     static struct option long_options[] =
3865796c8dcSSimon Schubert     {
3875796c8dcSSimon Schubert       {"tui", no_argument, 0, OPT_TUI},
3885796c8dcSSimon Schubert       {"xdb", no_argument, &xdb_commands, 1},
3895796c8dcSSimon Schubert       {"dbx", no_argument, &dbx_commands, 1},
3905796c8dcSSimon Schubert       {"readnow", no_argument, &readnow_symbol_files, 1},
3915796c8dcSSimon Schubert       {"r", no_argument, &readnow_symbol_files, 1},
3925796c8dcSSimon Schubert       {"quiet", no_argument, &quiet, 1},
3935796c8dcSSimon Schubert       {"q", no_argument, &quiet, 1},
3945796c8dcSSimon Schubert       {"silent", no_argument, &quiet, 1},
3955796c8dcSSimon Schubert       {"nx", no_argument, &inhibit_gdbinit, 1},
3965796c8dcSSimon Schubert       {"n", no_argument, &inhibit_gdbinit, 1},
3975796c8dcSSimon Schubert       {"batch-silent", no_argument, 0, 'B'},
398*cf7f2e2dSJohn Marino       {"batch", no_argument, &batch_flag, 1},
3995796c8dcSSimon Schubert       {"epoch", no_argument, &epoch_interface, 1},
4005796c8dcSSimon Schubert 
4015796c8dcSSimon Schubert     /* This is a synonym for "--annotate=1".  --annotate is now preferred,
4025796c8dcSSimon Schubert        but keep this here for a long time because people will be running
4035796c8dcSSimon Schubert        emacses which use --fullname.  */
4045796c8dcSSimon Schubert       {"fullname", no_argument, 0, 'f'},
4055796c8dcSSimon Schubert       {"f", no_argument, 0, 'f'},
4065796c8dcSSimon Schubert 
4075796c8dcSSimon Schubert       {"annotate", required_argument, 0, OPT_ANNOTATE},
4085796c8dcSSimon Schubert       {"help", no_argument, &print_help, 1},
4095796c8dcSSimon Schubert       {"se", required_argument, 0, OPT_SE},
4105796c8dcSSimon Schubert       {"symbols", required_argument, 0, 's'},
4115796c8dcSSimon Schubert       {"s", required_argument, 0, 's'},
4125796c8dcSSimon Schubert       {"exec", required_argument, 0, 'e'},
4135796c8dcSSimon Schubert       {"e", required_argument, 0, 'e'},
4145796c8dcSSimon Schubert       {"core", required_argument, 0, 'c'},
4155796c8dcSSimon Schubert       {"c", required_argument, 0, 'c'},
4165796c8dcSSimon Schubert       {"pid", required_argument, 0, 'p'},
4175796c8dcSSimon Schubert       {"p", required_argument, 0, 'p'},
4185796c8dcSSimon Schubert       {"command", required_argument, 0, 'x'},
4195796c8dcSSimon Schubert       {"eval-command", required_argument, 0, 'X'},
4205796c8dcSSimon Schubert       {"version", no_argument, &print_version, 1},
4215796c8dcSSimon Schubert       {"x", required_argument, 0, 'x'},
4225796c8dcSSimon Schubert       {"ex", required_argument, 0, 'X'},
4235796c8dcSSimon Schubert #ifdef GDBTK
4245796c8dcSSimon Schubert       {"tclcommand", required_argument, 0, 'z'},
4255796c8dcSSimon Schubert       {"enable-external-editor", no_argument, 0, 'y'},
4265796c8dcSSimon Schubert       {"editor-command", required_argument, 0, 'w'},
4275796c8dcSSimon Schubert #endif
4285796c8dcSSimon Schubert       {"ui", required_argument, 0, 'i'},
4295796c8dcSSimon Schubert       {"interpreter", required_argument, 0, 'i'},
4305796c8dcSSimon Schubert       {"i", required_argument, 0, 'i'},
4315796c8dcSSimon Schubert       {"directory", required_argument, 0, 'd'},
4325796c8dcSSimon Schubert       {"d", required_argument, 0, 'd'},
4335796c8dcSSimon Schubert       {"cd", required_argument, 0, OPT_CD},
4345796c8dcSSimon Schubert       {"tty", required_argument, 0, 't'},
4355796c8dcSSimon Schubert       {"baud", required_argument, 0, 'b'},
4365796c8dcSSimon Schubert       {"b", required_argument, 0, 'b'},
4375796c8dcSSimon Schubert       {"nw", no_argument, NULL, OPT_NOWINDOWS},
4385796c8dcSSimon Schubert       {"nowindows", no_argument, NULL, OPT_NOWINDOWS},
4395796c8dcSSimon Schubert       {"w", no_argument, NULL, OPT_WINDOWS},
4405796c8dcSSimon Schubert       {"windows", no_argument, NULL, OPT_WINDOWS},
4415796c8dcSSimon Schubert       {"statistics", no_argument, 0, OPT_STATISTICS},
4425796c8dcSSimon Schubert       {"write", no_argument, &write_files, 1},
4435796c8dcSSimon Schubert       {"args", no_argument, &set_args, 1},
4445796c8dcSSimon Schubert       {"l", required_argument, 0, 'l'},
4455796c8dcSSimon Schubert       {"return-child-result", no_argument, &return_child_result, 1},
4465796c8dcSSimon Schubert       {0, no_argument, 0, 0}
4475796c8dcSSimon Schubert     };
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert     while (1)
4505796c8dcSSimon Schubert       {
4515796c8dcSSimon Schubert 	int option_index;
4525796c8dcSSimon Schubert 
4535796c8dcSSimon Schubert 	c = getopt_long_only (argc, argv, "",
4545796c8dcSSimon Schubert 			      long_options, &option_index);
4555796c8dcSSimon Schubert 	if (c == EOF || set_args)
4565796c8dcSSimon Schubert 	  break;
4575796c8dcSSimon Schubert 
4585796c8dcSSimon Schubert 	/* Long option that takes an argument.  */
4595796c8dcSSimon Schubert 	if (c == 0 && long_options[option_index].flag == 0)
4605796c8dcSSimon Schubert 	  c = long_options[option_index].val;
4615796c8dcSSimon Schubert 
4625796c8dcSSimon Schubert 	switch (c)
4635796c8dcSSimon Schubert 	  {
4645796c8dcSSimon Schubert 	  case 0:
4655796c8dcSSimon Schubert 	    /* Long option that just sets a flag.  */
4665796c8dcSSimon Schubert 	    break;
4675796c8dcSSimon Schubert 	  case OPT_SE:
4685796c8dcSSimon Schubert 	    symarg = optarg;
4695796c8dcSSimon Schubert 	    execarg = optarg;
4705796c8dcSSimon Schubert 	    break;
4715796c8dcSSimon Schubert 	  case OPT_CD:
4725796c8dcSSimon Schubert 	    cdarg = optarg;
4735796c8dcSSimon Schubert 	    break;
4745796c8dcSSimon Schubert 	  case OPT_ANNOTATE:
4755796c8dcSSimon Schubert 	    /* FIXME: what if the syntax is wrong (e.g. not digits)?  */
4765796c8dcSSimon Schubert 	    annotation_level = atoi (optarg);
4775796c8dcSSimon Schubert 	    break;
4785796c8dcSSimon Schubert 	  case OPT_STATISTICS:
4795796c8dcSSimon Schubert 	    /* Enable the display of both time and space usage.  */
480*cf7f2e2dSJohn Marino 	    set_display_time (1);
481*cf7f2e2dSJohn Marino 	    set_display_space (1);
4825796c8dcSSimon Schubert 	    break;
4835796c8dcSSimon Schubert 	  case OPT_TUI:
4845796c8dcSSimon Schubert 	    /* --tui is equivalent to -i=tui.  */
4855796c8dcSSimon Schubert #ifdef TUI
4865796c8dcSSimon Schubert 	    xfree (interpreter_p);
4875796c8dcSSimon Schubert 	    interpreter_p = xstrdup (INTERP_TUI);
4885796c8dcSSimon Schubert #else
4895796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stderr,
4905796c8dcSSimon Schubert 				_("%s: TUI mode is not supported\n"),
4915796c8dcSSimon Schubert 				argv[0]);
4925796c8dcSSimon Schubert 	    exit (1);
4935796c8dcSSimon Schubert #endif
4945796c8dcSSimon Schubert 	    break;
4955796c8dcSSimon Schubert 	  case OPT_WINDOWS:
4965796c8dcSSimon Schubert 	    /* FIXME: cagney/2003-03-01: Not sure if this option is
4975796c8dcSSimon Schubert                actually useful, and if it is, what it should do.  */
4985796c8dcSSimon Schubert #ifdef GDBTK
4995796c8dcSSimon Schubert 	    /* --windows is equivalent to -i=insight.  */
5005796c8dcSSimon Schubert 	    xfree (interpreter_p);
5015796c8dcSSimon Schubert 	    interpreter_p = xstrdup (INTERP_INSIGHT);
5025796c8dcSSimon Schubert #endif
5035796c8dcSSimon Schubert 	    use_windows = 1;
5045796c8dcSSimon Schubert 	    break;
5055796c8dcSSimon Schubert 	  case OPT_NOWINDOWS:
5065796c8dcSSimon Schubert 	    /* -nw is equivalent to -i=console.  */
5075796c8dcSSimon Schubert 	    xfree (interpreter_p);
5085796c8dcSSimon Schubert 	    interpreter_p = xstrdup (INTERP_CONSOLE);
5095796c8dcSSimon Schubert 	    use_windows = 0;
5105796c8dcSSimon Schubert 	    break;
5115796c8dcSSimon Schubert 	  case 'f':
5125796c8dcSSimon Schubert 	    annotation_level = 1;
5135796c8dcSSimon Schubert /* We have probably been invoked from emacs.  Disable window interface.  */
5145796c8dcSSimon Schubert 	    use_windows = 0;
5155796c8dcSSimon Schubert 	    break;
5165796c8dcSSimon Schubert 	  case 's':
5175796c8dcSSimon Schubert 	    symarg = optarg;
5185796c8dcSSimon Schubert 	    break;
5195796c8dcSSimon Schubert 	  case 'e':
5205796c8dcSSimon Schubert 	    execarg = optarg;
5215796c8dcSSimon Schubert 	    break;
5225796c8dcSSimon Schubert 	  case 'c':
5235796c8dcSSimon Schubert 	    corearg = optarg;
5245796c8dcSSimon Schubert 	    break;
5255796c8dcSSimon Schubert 	  case 'p':
5265796c8dcSSimon Schubert 	    pidarg = optarg;
5275796c8dcSSimon Schubert 	    break;
5285796c8dcSSimon Schubert 	  case 'x':
5295796c8dcSSimon Schubert 	    cmdarg[ncmd].type = CMDARG_FILE;
5305796c8dcSSimon Schubert 	    cmdarg[ncmd++].string = optarg;
5315796c8dcSSimon Schubert 	    if (ncmd >= cmdsize)
5325796c8dcSSimon Schubert 	      {
5335796c8dcSSimon Schubert 		cmdsize *= 2;
5345796c8dcSSimon Schubert 		cmdarg = xrealloc ((char *) cmdarg,
5355796c8dcSSimon Schubert 				   cmdsize * sizeof (*cmdarg));
5365796c8dcSSimon Schubert 	      }
5375796c8dcSSimon Schubert 	    break;
5385796c8dcSSimon Schubert 	  case 'X':
5395796c8dcSSimon Schubert 	    cmdarg[ncmd].type = CMDARG_COMMAND;
5405796c8dcSSimon Schubert 	    cmdarg[ncmd++].string = optarg;
5415796c8dcSSimon Schubert 	    if (ncmd >= cmdsize)
5425796c8dcSSimon Schubert 	      {
5435796c8dcSSimon Schubert 		cmdsize *= 2;
5445796c8dcSSimon Schubert 		cmdarg = xrealloc ((char *) cmdarg,
5455796c8dcSSimon Schubert 				   cmdsize * sizeof (*cmdarg));
5465796c8dcSSimon Schubert 	      }
5475796c8dcSSimon Schubert 	    break;
5485796c8dcSSimon Schubert 	  case 'B':
549*cf7f2e2dSJohn Marino 	    batch_flag = batch_silent = 1;
5505796c8dcSSimon Schubert 	    gdb_stdout = ui_file_new();
5515796c8dcSSimon Schubert 	    break;
5525796c8dcSSimon Schubert #ifdef GDBTK
5535796c8dcSSimon Schubert 	  case 'z':
5545796c8dcSSimon Schubert 	    {
5555796c8dcSSimon Schubert extern int gdbtk_test (char *);
5565796c8dcSSimon Schubert 	      if (!gdbtk_test (optarg))
5575796c8dcSSimon Schubert 		{
5585796c8dcSSimon Schubert 		  fprintf_unfiltered (gdb_stderr, _("%s: unable to load tclcommand file \"%s\""),
5595796c8dcSSimon Schubert 				      argv[0], optarg);
5605796c8dcSSimon Schubert 		  exit (1);
5615796c8dcSSimon Schubert 		}
5625796c8dcSSimon Schubert 	      break;
5635796c8dcSSimon Schubert 	    }
5645796c8dcSSimon Schubert 	  case 'y':
5655796c8dcSSimon Schubert 	    /* Backwards compatibility only.  */
5665796c8dcSSimon Schubert 	    break;
5675796c8dcSSimon Schubert 	  case 'w':
5685796c8dcSSimon Schubert 	    {
5695796c8dcSSimon Schubert 	      external_editor_command = xstrdup (optarg);
5705796c8dcSSimon Schubert 	      break;
5715796c8dcSSimon Schubert 	    }
5725796c8dcSSimon Schubert #endif /* GDBTK */
5735796c8dcSSimon Schubert 	  case 'i':
5745796c8dcSSimon Schubert 	    xfree (interpreter_p);
5755796c8dcSSimon Schubert 	    interpreter_p = xstrdup (optarg);
5765796c8dcSSimon Schubert 	    break;
5775796c8dcSSimon Schubert 	  case 'd':
5785796c8dcSSimon Schubert 	    dirarg[ndir++] = optarg;
5795796c8dcSSimon Schubert 	    if (ndir >= dirsize)
5805796c8dcSSimon Schubert 	      {
5815796c8dcSSimon Schubert 		dirsize *= 2;
5825796c8dcSSimon Schubert 		dirarg = (char **) xrealloc ((char *) dirarg,
5835796c8dcSSimon Schubert 					     dirsize * sizeof (*dirarg));
5845796c8dcSSimon Schubert 	      }
5855796c8dcSSimon Schubert 	    break;
5865796c8dcSSimon Schubert 	  case 't':
5875796c8dcSSimon Schubert 	    ttyarg = optarg;
5885796c8dcSSimon Schubert 	    break;
5895796c8dcSSimon Schubert 	  case 'q':
5905796c8dcSSimon Schubert 	    quiet = 1;
5915796c8dcSSimon Schubert 	    break;
5925796c8dcSSimon Schubert 	  case 'b':
5935796c8dcSSimon Schubert 	    {
5945796c8dcSSimon Schubert 	      int i;
5955796c8dcSSimon Schubert 	      char *p;
5965796c8dcSSimon Schubert 
5975796c8dcSSimon Schubert 	      i = strtol (optarg, &p, 0);
5985796c8dcSSimon Schubert 	      if (i == 0 && p == optarg)
5995796c8dcSSimon Schubert 
6005796c8dcSSimon Schubert 		/* Don't use *_filtered or warning() (which relies on
6015796c8dcSSimon Schubert 		   current_target) until after initialize_all_files(). */
6025796c8dcSSimon Schubert 
6035796c8dcSSimon Schubert 		fprintf_unfiltered
6045796c8dcSSimon Schubert 		  (gdb_stderr,
6055796c8dcSSimon Schubert 		   _("warning: could not set baud rate to `%s'.\n"), optarg);
6065796c8dcSSimon Schubert 	      else
6075796c8dcSSimon Schubert 		baud_rate = i;
6085796c8dcSSimon Schubert 	    }
6095796c8dcSSimon Schubert             break;
6105796c8dcSSimon Schubert 	  case 'l':
6115796c8dcSSimon Schubert 	    {
6125796c8dcSSimon Schubert 	      int i;
6135796c8dcSSimon Schubert 	      char *p;
6145796c8dcSSimon Schubert 
6155796c8dcSSimon Schubert 	      i = strtol (optarg, &p, 0);
6165796c8dcSSimon Schubert 	      if (i == 0 && p == optarg)
6175796c8dcSSimon Schubert 
6185796c8dcSSimon Schubert 		/* Don't use *_filtered or warning() (which relies on
6195796c8dcSSimon Schubert 		   current_target) until after initialize_all_files(). */
6205796c8dcSSimon Schubert 
6215796c8dcSSimon Schubert 		fprintf_unfiltered
6225796c8dcSSimon Schubert 		  (gdb_stderr,
6235796c8dcSSimon Schubert 		 _("warning: could not set timeout limit to `%s'.\n"), optarg);
6245796c8dcSSimon Schubert 	      else
6255796c8dcSSimon Schubert 		remote_timeout = i;
6265796c8dcSSimon Schubert 	    }
6275796c8dcSSimon Schubert 	    break;
6285796c8dcSSimon Schubert 
6295796c8dcSSimon Schubert 	  case '?':
6305796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stderr,
6315796c8dcSSimon Schubert 			_("Use `%s --help' for a complete list of options.\n"),
6325796c8dcSSimon Schubert 				argv[0]);
6335796c8dcSSimon Schubert 	    exit (1);
6345796c8dcSSimon Schubert 	  }
6355796c8dcSSimon Schubert       }
6365796c8dcSSimon Schubert 
6375796c8dcSSimon Schubert     /* If --help or --version, disable window interface.  */
6385796c8dcSSimon Schubert     if (print_help || print_version)
6395796c8dcSSimon Schubert       {
6405796c8dcSSimon Schubert 	use_windows = 0;
6415796c8dcSSimon Schubert       }
6425796c8dcSSimon Schubert 
643*cf7f2e2dSJohn Marino     if (batch_flag)
644*cf7f2e2dSJohn Marino       quiet = 1;
645*cf7f2e2dSJohn Marino   }
646*cf7f2e2dSJohn Marino 
647*cf7f2e2dSJohn Marino   /* Initialize all files.  Give the interpreter a chance to take
648*cf7f2e2dSJohn Marino      control of the console via the deprecated_init_ui_hook ().  */
649*cf7f2e2dSJohn Marino   gdb_init (argv[0]);
650*cf7f2e2dSJohn Marino 
651*cf7f2e2dSJohn Marino   /* Now that gdb_init has created the initial inferior, we're in position
652*cf7f2e2dSJohn Marino      to set args for that inferior.  */
6535796c8dcSSimon Schubert   if (set_args)
6545796c8dcSSimon Schubert     {
6555796c8dcSSimon Schubert       /* The remaining options are the command-line options for the
6565796c8dcSSimon Schubert 	 inferior.  The first one is the sym/exec file, and the rest
6575796c8dcSSimon Schubert 	 are arguments.  */
6585796c8dcSSimon Schubert       if (optind >= argc)
6595796c8dcSSimon Schubert 	{
6605796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stderr,
6615796c8dcSSimon Schubert 			      _("%s: `--args' specified but no program specified\n"),
6625796c8dcSSimon Schubert 			      argv[0]);
6635796c8dcSSimon Schubert 	  exit (1);
6645796c8dcSSimon Schubert 	}
6655796c8dcSSimon Schubert       symarg = argv[optind];
6665796c8dcSSimon Schubert       execarg = argv[optind];
6675796c8dcSSimon Schubert       ++optind;
6685796c8dcSSimon Schubert       set_inferior_args_vector (argc - optind, &argv[optind]);
6695796c8dcSSimon Schubert     }
6705796c8dcSSimon Schubert   else
6715796c8dcSSimon Schubert     {
6725796c8dcSSimon Schubert       /* OK, that's all the options.  */
6735796c8dcSSimon Schubert 
6745796c8dcSSimon Schubert       /* The first argument, if specified, is the name of the
6755796c8dcSSimon Schubert 	 executable.  */
6765796c8dcSSimon Schubert       if (optind < argc)
6775796c8dcSSimon Schubert 	{
6785796c8dcSSimon Schubert 	  symarg = argv[optind];
6795796c8dcSSimon Schubert 	  execarg = argv[optind];
6805796c8dcSSimon Schubert 	  optind++;
6815796c8dcSSimon Schubert 	}
6825796c8dcSSimon Schubert 
6835796c8dcSSimon Schubert       /* If the user hasn't already specified a PID or the name of a
6845796c8dcSSimon Schubert 	 core file, then a second optional argument is allowed.  If
6855796c8dcSSimon Schubert 	 present, this argument should be interpreted as either a
6865796c8dcSSimon Schubert 	 PID or a core file, whichever works.  */
6875796c8dcSSimon Schubert       if (pidarg == NULL && corearg == NULL && optind < argc)
6885796c8dcSSimon Schubert 	{
6895796c8dcSSimon Schubert 	  pid_or_core_arg = argv[optind];
6905796c8dcSSimon Schubert 	  optind++;
6915796c8dcSSimon Schubert 	}
6925796c8dcSSimon Schubert 
6935796c8dcSSimon Schubert       /* Any argument left on the command line is unexpected and
6945796c8dcSSimon Schubert 	 will be ignored.  Inform the user.  */
6955796c8dcSSimon Schubert       if (optind < argc)
6965796c8dcSSimon Schubert 	fprintf_unfiltered (gdb_stderr, _("\
6975796c8dcSSimon Schubert Excess command line arguments ignored. (%s%s)\n"),
6985796c8dcSSimon Schubert 			    argv[optind],
6995796c8dcSSimon Schubert 			    (optind == argc - 1) ? "" : " ...");
7005796c8dcSSimon Schubert     }
7015796c8dcSSimon Schubert 
7025796c8dcSSimon Schubert   /* Lookup gdbinit files. Note that the gdbinit file name may be overriden
7035796c8dcSSimon Schubert      during file initialization, so get_init_files should be called after
7045796c8dcSSimon Schubert      gdb_init.  */
7055796c8dcSSimon Schubert   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
7065796c8dcSSimon Schubert 
7075796c8dcSSimon Schubert   /* Do these (and anything which might call wrap_here or *_filtered)
7085796c8dcSSimon Schubert      after initialize_all_files() but before the interpreter has been
7095796c8dcSSimon Schubert      installed.  Otherwize the help/version messages will be eaten by
7105796c8dcSSimon Schubert      the interpreter's output handler.  */
7115796c8dcSSimon Schubert 
7125796c8dcSSimon Schubert   if (print_version)
7135796c8dcSSimon Schubert     {
7145796c8dcSSimon Schubert       print_gdb_version (gdb_stdout);
7155796c8dcSSimon Schubert       wrap_here ("");
7165796c8dcSSimon Schubert       printf_filtered ("\n");
7175796c8dcSSimon Schubert       exit (0);
7185796c8dcSSimon Schubert     }
7195796c8dcSSimon Schubert 
7205796c8dcSSimon Schubert   if (print_help)
7215796c8dcSSimon Schubert     {
7225796c8dcSSimon Schubert       print_gdb_help (gdb_stdout);
7235796c8dcSSimon Schubert       fputs_unfiltered ("\n", gdb_stdout);
7245796c8dcSSimon Schubert       exit (0);
7255796c8dcSSimon Schubert     }
7265796c8dcSSimon Schubert 
7275796c8dcSSimon Schubert   /* FIXME: cagney/2003-02-03: The big hack (part 1 of 2) that lets
7285796c8dcSSimon Schubert      GDB retain the old MI1 interpreter startup behavior.  Output the
7295796c8dcSSimon Schubert      copyright message before the interpreter is installed.  That way
7305796c8dcSSimon Schubert      it isn't encapsulated in MI output.  */
7315796c8dcSSimon Schubert   if (!quiet && strcmp (interpreter_p, INTERP_MI1) == 0)
7325796c8dcSSimon Schubert     {
7335796c8dcSSimon Schubert       /* Print all the junk at the top, with trailing "..." if we are about
7345796c8dcSSimon Schubert          to read a symbol file (possibly slowly).  */
7355796c8dcSSimon Schubert       print_gdb_version (gdb_stdout);
7365796c8dcSSimon Schubert       if (symarg)
7375796c8dcSSimon Schubert 	printf_filtered ("..");
7385796c8dcSSimon Schubert       wrap_here ("");
7395796c8dcSSimon Schubert       printf_filtered ("\n");
7405796c8dcSSimon Schubert       gdb_flush (gdb_stdout);	/* Force to screen during slow operations */
7415796c8dcSSimon Schubert     }
7425796c8dcSSimon Schubert 
7435796c8dcSSimon Schubert 
7445796c8dcSSimon Schubert   /* Install the default UI.  All the interpreters should have had a
7455796c8dcSSimon Schubert      look at things by now.  Initialize the default interpreter. */
7465796c8dcSSimon Schubert 
7475796c8dcSSimon Schubert   {
7485796c8dcSSimon Schubert     /* Find it.  */
7495796c8dcSSimon Schubert     struct interp *interp = interp_lookup (interpreter_p);
750*cf7f2e2dSJohn Marino 
7515796c8dcSSimon Schubert     if (interp == NULL)
7525796c8dcSSimon Schubert       error (_("Interpreter `%s' unrecognized"), interpreter_p);
7535796c8dcSSimon Schubert     /* Install it.  */
7545796c8dcSSimon Schubert     if (!interp_set (interp, 1))
7555796c8dcSSimon Schubert       {
7565796c8dcSSimon Schubert         fprintf_unfiltered (gdb_stderr,
7575796c8dcSSimon Schubert 			    "Interpreter `%s' failed to initialize.\n",
7585796c8dcSSimon Schubert                             interpreter_p);
7595796c8dcSSimon Schubert         exit (1);
7605796c8dcSSimon Schubert       }
7615796c8dcSSimon Schubert   }
7625796c8dcSSimon Schubert 
7635796c8dcSSimon Schubert   /* FIXME: cagney/2003-02-03: The big hack (part 2 of 2) that lets
7645796c8dcSSimon Schubert      GDB retain the old MI1 interpreter startup behavior.  Output the
7655796c8dcSSimon Schubert      copyright message after the interpreter is installed when it is
7665796c8dcSSimon Schubert      any sane interpreter.  */
7675796c8dcSSimon Schubert   if (!quiet && !current_interp_named_p (INTERP_MI1))
7685796c8dcSSimon Schubert     {
7695796c8dcSSimon Schubert       /* Print all the junk at the top, with trailing "..." if we are about
7705796c8dcSSimon Schubert          to read a symbol file (possibly slowly).  */
7715796c8dcSSimon Schubert       print_gdb_version (gdb_stdout);
7725796c8dcSSimon Schubert       if (symarg)
7735796c8dcSSimon Schubert 	printf_filtered ("..");
7745796c8dcSSimon Schubert       wrap_here ("");
7755796c8dcSSimon Schubert       printf_filtered ("\n");
7765796c8dcSSimon Schubert       gdb_flush (gdb_stdout);	/* Force to screen during slow operations */
7775796c8dcSSimon Schubert     }
7785796c8dcSSimon Schubert 
7795796c8dcSSimon Schubert   /* Set off error and warning messages with a blank line.  */
7805796c8dcSSimon Schubert   error_pre_print = "\n";
7815796c8dcSSimon Schubert   quit_pre_print = error_pre_print;
7825796c8dcSSimon Schubert   warning_pre_print = _("\nwarning: ");
7835796c8dcSSimon Schubert 
7845796c8dcSSimon Schubert   /* Read and execute the system-wide gdbinit file, if it exists.
7855796c8dcSSimon Schubert      This is done *before* all the command line arguments are
7865796c8dcSSimon Schubert      processed; it sets global parameters, which are independent of
7875796c8dcSSimon Schubert      what file you are debugging or what directory you are in.  */
7885796c8dcSSimon Schubert   if (system_gdbinit && !inhibit_gdbinit)
7895796c8dcSSimon Schubert     catch_command_errors (source_script, system_gdbinit, 0, RETURN_MASK_ALL);
7905796c8dcSSimon Schubert 
7915796c8dcSSimon Schubert   /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
7925796c8dcSSimon Schubert      *before* all the command line arguments are processed; it sets
7935796c8dcSSimon Schubert      global parameters, which are independent of what file you are
7945796c8dcSSimon Schubert      debugging or what directory you are in.  */
7955796c8dcSSimon Schubert 
7965796c8dcSSimon Schubert   if (home_gdbinit && !inhibit_gdbinit)
7975796c8dcSSimon Schubert     catch_command_errors (source_script, home_gdbinit, 0, RETURN_MASK_ALL);
7985796c8dcSSimon Schubert 
7995796c8dcSSimon Schubert   /* Now perform all the actions indicated by the arguments.  */
8005796c8dcSSimon Schubert   if (cdarg != NULL)
8015796c8dcSSimon Schubert     {
8025796c8dcSSimon Schubert       catch_command_errors (cd_command, cdarg, 0, RETURN_MASK_ALL);
8035796c8dcSSimon Schubert     }
8045796c8dcSSimon Schubert 
8055796c8dcSSimon Schubert   for (i = 0; i < ndir; i++)
8065796c8dcSSimon Schubert     catch_command_errors (directory_switch, dirarg[i], 0, RETURN_MASK_ALL);
8075796c8dcSSimon Schubert   xfree (dirarg);
8085796c8dcSSimon Schubert 
809*cf7f2e2dSJohn Marino   /* Skip auto-loading section-specified scripts until we've sourced
810*cf7f2e2dSJohn Marino      local_gdbinit (which is often used to augment the source search path).  */
811*cf7f2e2dSJohn Marino   save_auto_load = gdbpy_global_auto_load;
812*cf7f2e2dSJohn Marino   gdbpy_global_auto_load = 0;
813*cf7f2e2dSJohn Marino 
8145796c8dcSSimon Schubert   if (execarg != NULL
8155796c8dcSSimon Schubert       && symarg != NULL
8165796c8dcSSimon Schubert       && strcmp (execarg, symarg) == 0)
8175796c8dcSSimon Schubert     {
8185796c8dcSSimon Schubert       /* The exec file and the symbol-file are the same.  If we can't
8195796c8dcSSimon Schubert          open it, better only print one error message.
8205796c8dcSSimon Schubert          catch_command_errors returns non-zero on success! */
821*cf7f2e2dSJohn Marino       if (catch_command_errors (exec_file_attach, execarg, !batch_flag, RETURN_MASK_ALL))
822*cf7f2e2dSJohn Marino 	catch_command_errors (symbol_file_add_main, symarg, !batch_flag, RETURN_MASK_ALL);
8235796c8dcSSimon Schubert     }
8245796c8dcSSimon Schubert   else
8255796c8dcSSimon Schubert     {
8265796c8dcSSimon Schubert       if (execarg != NULL)
827*cf7f2e2dSJohn Marino 	catch_command_errors (exec_file_attach, execarg, !batch_flag, RETURN_MASK_ALL);
8285796c8dcSSimon Schubert       if (symarg != NULL)
829*cf7f2e2dSJohn Marino 	catch_command_errors (symbol_file_add_main, symarg, !batch_flag, RETURN_MASK_ALL);
8305796c8dcSSimon Schubert     }
8315796c8dcSSimon Schubert 
8325796c8dcSSimon Schubert   if (corearg && pidarg)
8335796c8dcSSimon Schubert     error (_("\
8345796c8dcSSimon Schubert Can't attach to process and specify a core file at the same time."));
8355796c8dcSSimon Schubert 
8365796c8dcSSimon Schubert   if (corearg != NULL)
8375796c8dcSSimon Schubert     catch_command_errors (core_file_command, corearg,
838*cf7f2e2dSJohn Marino 			  !batch_flag, RETURN_MASK_ALL);
8395796c8dcSSimon Schubert   else if (pidarg != NULL)
8405796c8dcSSimon Schubert     catch_command_errors (attach_command, pidarg,
841*cf7f2e2dSJohn Marino 			  !batch_flag, RETURN_MASK_ALL);
8425796c8dcSSimon Schubert   else if (pid_or_core_arg)
8435796c8dcSSimon Schubert     {
8445796c8dcSSimon Schubert       /* The user specified 'gdb program pid' or gdb program core'.
8455796c8dcSSimon Schubert 	 If pid_or_core_arg's first character is a digit, try attach
8465796c8dcSSimon Schubert 	 first and then corefile.  Otherwise try just corefile.  */
8475796c8dcSSimon Schubert 
8485796c8dcSSimon Schubert       if (isdigit (pid_or_core_arg[0]))
8495796c8dcSSimon Schubert 	{
8505796c8dcSSimon Schubert 	  if (catch_command_errors (attach_command, pid_or_core_arg,
851*cf7f2e2dSJohn Marino 				    !batch_flag, RETURN_MASK_ALL) == 0)
8525796c8dcSSimon Schubert 	    catch_command_errors (core_file_command, pid_or_core_arg,
853*cf7f2e2dSJohn Marino 				  !batch_flag, RETURN_MASK_ALL);
8545796c8dcSSimon Schubert 	}
8555796c8dcSSimon Schubert       else /* Can't be a pid, better be a corefile.  */
8565796c8dcSSimon Schubert 	catch_command_errors (core_file_command, pid_or_core_arg,
857*cf7f2e2dSJohn Marino 			      !batch_flag, RETURN_MASK_ALL);
8585796c8dcSSimon Schubert     }
8595796c8dcSSimon Schubert 
8605796c8dcSSimon Schubert   if (ttyarg != NULL)
861*cf7f2e2dSJohn Marino     set_inferior_io_terminal (ttyarg);
8625796c8dcSSimon Schubert 
8635796c8dcSSimon Schubert   /* Error messages should no longer be distinguished with extra output. */
8645796c8dcSSimon Schubert   error_pre_print = NULL;
8655796c8dcSSimon Schubert   quit_pre_print = NULL;
8665796c8dcSSimon Schubert   warning_pre_print = _("warning: ");
8675796c8dcSSimon Schubert 
8685796c8dcSSimon Schubert   /* Read the .gdbinit file in the current directory, *if* it isn't
8695796c8dcSSimon Schubert      the same as the $HOME/.gdbinit file (it should exist, also).  */
8705796c8dcSSimon Schubert   if (local_gdbinit && !inhibit_gdbinit)
8715796c8dcSSimon Schubert     catch_command_errors (source_script, local_gdbinit, 0, RETURN_MASK_ALL);
8725796c8dcSSimon Schubert 
873*cf7f2e2dSJohn Marino   /* Now that all .gdbinit's have been read and all -d options have been
874*cf7f2e2dSJohn Marino      processed, we can read any scripts mentioned in SYMARG.
875*cf7f2e2dSJohn Marino      We wait until now because it is common to add to the source search
876*cf7f2e2dSJohn Marino      path in local_gdbinit.  */
877*cf7f2e2dSJohn Marino   gdbpy_global_auto_load = save_auto_load;
878*cf7f2e2dSJohn Marino   if (symfile_objfile != NULL)
879*cf7f2e2dSJohn Marino     load_auto_scripts_for_objfile (symfile_objfile);
880*cf7f2e2dSJohn Marino 
8815796c8dcSSimon Schubert   for (i = 0; i < ncmd; i++)
8825796c8dcSSimon Schubert     {
8835796c8dcSSimon Schubert       if (cmdarg[i].type == CMDARG_FILE)
8845796c8dcSSimon Schubert         catch_command_errors (source_script, cmdarg[i].string,
885*cf7f2e2dSJohn Marino 			      !batch_flag, RETURN_MASK_ALL);
8865796c8dcSSimon Schubert       else  /* cmdarg[i].type == CMDARG_COMMAND */
8875796c8dcSSimon Schubert         catch_command_errors (execute_command, cmdarg[i].string,
888*cf7f2e2dSJohn Marino 			      !batch_flag, RETURN_MASK_ALL);
8895796c8dcSSimon Schubert     }
8905796c8dcSSimon Schubert   xfree (cmdarg);
8915796c8dcSSimon Schubert 
8925796c8dcSSimon Schubert   /* Read in the old history after all the command files have been read. */
8935796c8dcSSimon Schubert   init_history ();
8945796c8dcSSimon Schubert 
895*cf7f2e2dSJohn Marino   if (batch_flag)
8965796c8dcSSimon Schubert     {
8975796c8dcSSimon Schubert       /* We have hit the end of the batch file.  */
8985796c8dcSSimon Schubert       quit_force (NULL, 0);
8995796c8dcSSimon Schubert     }
9005796c8dcSSimon Schubert 
9015796c8dcSSimon Schubert   /* Show time and/or space usage.  */
902*cf7f2e2dSJohn Marino   do_cleanups (pre_stat_chain);
9035796c8dcSSimon Schubert 
9045796c8dcSSimon Schubert   /* NOTE: cagney/1999-11-07: There is probably no reason for not
9055796c8dcSSimon Schubert      moving this loop and the code found in captured_command_loop()
9065796c8dcSSimon Schubert      into the command_loop() proper.  The main thing holding back that
9075796c8dcSSimon Schubert      change - SET_TOP_LEVEL() - has been eliminated. */
9085796c8dcSSimon Schubert   while (1)
9095796c8dcSSimon Schubert     {
9105796c8dcSSimon Schubert       catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
9115796c8dcSSimon Schubert     }
9125796c8dcSSimon Schubert   /* No exit -- exit is through quit_command.  */
9135796c8dcSSimon Schubert }
9145796c8dcSSimon Schubert 
9155796c8dcSSimon Schubert int
9165796c8dcSSimon Schubert gdb_main (struct captured_main_args *args)
9175796c8dcSSimon Schubert {
9185796c8dcSSimon Schubert   use_windows = args->use_windows;
9195796c8dcSSimon Schubert   catch_errors (captured_main, args, "", RETURN_MASK_ALL);
9205796c8dcSSimon Schubert   /* The only way to end up here is by an error (normal exit is
9215796c8dcSSimon Schubert      handled by quit_force()), hence always return an error status.  */
9225796c8dcSSimon Schubert   return 1;
9235796c8dcSSimon Schubert }
9245796c8dcSSimon Schubert 
9255796c8dcSSimon Schubert 
9265796c8dcSSimon Schubert /* Don't use *_filtered for printing help.  We don't want to prompt
9275796c8dcSSimon Schubert    for continue no matter how small the screen or how much we're going
9285796c8dcSSimon Schubert    to print.  */
9295796c8dcSSimon Schubert 
9305796c8dcSSimon Schubert static void
9315796c8dcSSimon Schubert print_gdb_help (struct ui_file *stream)
9325796c8dcSSimon Schubert {
9335796c8dcSSimon Schubert   char *system_gdbinit;
9345796c8dcSSimon Schubert   char *home_gdbinit;
9355796c8dcSSimon Schubert   char *local_gdbinit;
9365796c8dcSSimon Schubert 
9375796c8dcSSimon Schubert   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert   fputs_unfiltered (_("\
9405796c8dcSSimon Schubert This is the GNU debugger.  Usage:\n\n\
9415796c8dcSSimon Schubert     gdb [options] [executable-file [core-file or process-id]]\n\
9425796c8dcSSimon Schubert     gdb [options] --args executable-file [inferior-arguments ...]\n\n\
9435796c8dcSSimon Schubert Options:\n\n\
9445796c8dcSSimon Schubert "), stream);
9455796c8dcSSimon Schubert   fputs_unfiltered (_("\
9465796c8dcSSimon Schubert   --args             Arguments after executable-file are passed to inferior\n\
9475796c8dcSSimon Schubert "), stream);
9485796c8dcSSimon Schubert   fputs_unfiltered (_("\
9495796c8dcSSimon Schubert   -b BAUDRATE        Set serial port baud rate used for remote debugging.\n\
9505796c8dcSSimon Schubert   --batch            Exit after processing options.\n\
9515796c8dcSSimon Schubert   --batch-silent     As for --batch, but suppress all gdb stdout output.\n\
9525796c8dcSSimon Schubert   --return-child-result\n\
9535796c8dcSSimon Schubert                      GDB exit code will be the child's exit code.\n\
9545796c8dcSSimon Schubert   --cd=DIR           Change current directory to DIR.\n\
9555796c8dcSSimon Schubert   --command=FILE, -x Execute GDB commands from FILE.\n\
9565796c8dcSSimon Schubert   --eval-command=COMMAND, -ex\n\
9575796c8dcSSimon Schubert                      Execute a single GDB command.\n\
9585796c8dcSSimon Schubert                      May be used multiple times and in conjunction\n\
9595796c8dcSSimon Schubert                      with --command.\n\
9605796c8dcSSimon Schubert   --core=COREFILE    Analyze the core dump COREFILE.\n\
9615796c8dcSSimon Schubert   --pid=PID          Attach to running process PID.\n\
9625796c8dcSSimon Schubert "), stream);
9635796c8dcSSimon Schubert   fputs_unfiltered (_("\
9645796c8dcSSimon Schubert   --dbx              DBX compatibility mode.\n\
9655796c8dcSSimon Schubert   --directory=DIR    Search for source files in DIR.\n\
9665796c8dcSSimon Schubert   --epoch            Output information used by epoch emacs-GDB interface.\n\
9675796c8dcSSimon Schubert   --exec=EXECFILE    Use EXECFILE as the executable.\n\
9685796c8dcSSimon Schubert   --fullname         Output information used by emacs-GDB interface.\n\
9695796c8dcSSimon Schubert   --help             Print this message.\n\
9705796c8dcSSimon Schubert "), stream);
9715796c8dcSSimon Schubert   fputs_unfiltered (_("\
9725796c8dcSSimon Schubert   --interpreter=INTERP\n\
9735796c8dcSSimon Schubert                      Select a specific interpreter / user interface\n\
9745796c8dcSSimon Schubert "), stream);
9755796c8dcSSimon Schubert   fputs_unfiltered (_("\
9765796c8dcSSimon Schubert   -l TIMEOUT         Set timeout in seconds for remote debugging.\n\
9775796c8dcSSimon Schubert   --nw		     Do not use a window interface.\n\
9785796c8dcSSimon Schubert   --nx               Do not read "), stream);
9795796c8dcSSimon Schubert   fputs_unfiltered (gdbinit, stream);
9805796c8dcSSimon Schubert   fputs_unfiltered (_(" file.\n\
9815796c8dcSSimon Schubert   --quiet            Do not print version number on startup.\n\
9825796c8dcSSimon Schubert   --readnow          Fully read symbol files on first access.\n\
9835796c8dcSSimon Schubert "), stream);
9845796c8dcSSimon Schubert   fputs_unfiltered (_("\
9855796c8dcSSimon Schubert   --se=FILE          Use FILE as symbol file and executable file.\n\
9865796c8dcSSimon Schubert   --symbols=SYMFILE  Read symbols from SYMFILE.\n\
9875796c8dcSSimon Schubert   --tty=TTY          Use TTY for input/output by the program being debugged.\n\
9885796c8dcSSimon Schubert "), stream);
9895796c8dcSSimon Schubert #if defined(TUI)
9905796c8dcSSimon Schubert   fputs_unfiltered (_("\
9915796c8dcSSimon Schubert   --tui              Use a terminal user interface.\n\
9925796c8dcSSimon Schubert "), stream);
9935796c8dcSSimon Schubert #endif
9945796c8dcSSimon Schubert   fputs_unfiltered (_("\
9955796c8dcSSimon Schubert   --version          Print version information and then exit.\n\
9965796c8dcSSimon Schubert   -w                 Use a window interface.\n\
9975796c8dcSSimon Schubert   --write            Set writing into executable and core files.\n\
9985796c8dcSSimon Schubert   --xdb              XDB compatibility mode.\n\
9995796c8dcSSimon Schubert "), stream);
10005796c8dcSSimon Schubert   fputs_unfiltered (_("\n\
10015796c8dcSSimon Schubert At startup, GDB reads the following init files and executes their commands:\n\
10025796c8dcSSimon Schubert "), stream);
10035796c8dcSSimon Schubert   if (system_gdbinit)
10045796c8dcSSimon Schubert     fprintf_unfiltered (stream, _("\
10055796c8dcSSimon Schubert    * system-wide init file: %s\n\
10065796c8dcSSimon Schubert "), system_gdbinit);
10075796c8dcSSimon Schubert   if (home_gdbinit)
10085796c8dcSSimon Schubert     fprintf_unfiltered (stream, _("\
10095796c8dcSSimon Schubert    * user-specific init file: %s\n\
10105796c8dcSSimon Schubert "), home_gdbinit);
10115796c8dcSSimon Schubert   if (local_gdbinit)
10125796c8dcSSimon Schubert     fprintf_unfiltered (stream, _("\
10135796c8dcSSimon Schubert    * local init file: ./%s\n\
10145796c8dcSSimon Schubert "), local_gdbinit);
10155796c8dcSSimon Schubert   fputs_unfiltered (_("\n\
10165796c8dcSSimon Schubert For more information, type \"help\" from within GDB, or consult the\n\
10175796c8dcSSimon Schubert GDB manual (available as on-line info or a printed manual).\n\
10185796c8dcSSimon Schubert "), stream);
10195796c8dcSSimon Schubert   if (REPORT_BUGS_TO[0] && stream == gdb_stdout)
10205796c8dcSSimon Schubert     fprintf_unfiltered (stream, _("\
10215796c8dcSSimon Schubert Report bugs to \"%s\".\n\
10225796c8dcSSimon Schubert "), REPORT_BUGS_TO);
10235796c8dcSSimon Schubert }
1024