Home | History | Annotate | Line # | Download | only in gdb
main.c revision 1.3
      1  1.1  christos /* Top level stuff for GDB, the GNU debugger.
      2  1.1  christos 
      3  1.3  christos    Copyright (C) 1986-2015 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "defs.h"
     21  1.1  christos #include "top.h"
     22  1.1  christos #include "target.h"
     23  1.1  christos #include "inferior.h"
     24  1.1  christos #include "symfile.h"
     25  1.1  christos #include "gdbcore.h"
     26  1.1  christos #include "getopt.h"
     27  1.1  christos 
     28  1.1  christos #include <sys/types.h>
     29  1.1  christos #include <sys/stat.h>
     30  1.1  christos #include <ctype.h>
     31  1.1  christos #include "event-loop.h"
     32  1.1  christos #include "ui-out.h"
     33  1.1  christos 
     34  1.1  christos #include "interps.h"
     35  1.1  christos #include "main.h"
     36  1.1  christos #include "source.h"
     37  1.1  christos #include "cli/cli-cmds.h"
     38  1.1  christos #include "objfiles.h"
     39  1.1  christos #include "auto-load.h"
     40  1.1  christos #include "maint.h"
     41  1.1  christos 
     42  1.1  christos #include "filenames.h"
     43  1.1  christos #include "filestuff.h"
     44  1.3  christos #include <signal.h>
     45  1.3  christos #include "event-top.h"
     46  1.3  christos #include "infrun.h"
     47  1.1  christos 
     48  1.1  christos /* The selected interpreter.  This will be used as a set command
     49  1.1  christos    variable, so it should always be malloc'ed - since
     50  1.1  christos    do_setshow_command will free it.  */
     51  1.1  christos char *interpreter_p;
     52  1.1  christos 
     53  1.1  christos /* Whether xdb commands will be handled.  */
     54  1.1  christos int xdb_commands = 0;
     55  1.1  christos 
     56  1.1  christos /* Whether dbx commands will be handled.  */
     57  1.1  christos int dbx_commands = 0;
     58  1.1  christos 
     59  1.1  christos /* System root path, used to find libraries etc.  */
     60  1.1  christos char *gdb_sysroot = 0;
     61  1.1  christos 
     62  1.1  christos /* GDB datadir, used to store data files.  */
     63  1.1  christos char *gdb_datadir = 0;
     64  1.1  christos 
     65  1.1  christos /* Non-zero if GDB_DATADIR was provided on the command line.
     66  1.1  christos    This doesn't track whether data-directory is set later from the
     67  1.1  christos    command line, but we don't reread system.gdbinit when that happens.  */
     68  1.1  christos static int gdb_datadir_provided = 0;
     69  1.1  christos 
     70  1.1  christos /* If gdb was configured with --with-python=/path,
     71  1.1  christos    the possibly relocated path to python's lib directory.  */
     72  1.1  christos char *python_libdir = 0;
     73  1.1  christos 
     74  1.1  christos struct ui_file *gdb_stdout;
     75  1.1  christos struct ui_file *gdb_stderr;
     76  1.1  christos struct ui_file *gdb_stdlog;
     77  1.1  christos struct ui_file *gdb_stdin;
     78  1.1  christos /* Target IO streams.  */
     79  1.1  christos struct ui_file *gdb_stdtargin;
     80  1.1  christos struct ui_file *gdb_stdtarg;
     81  1.1  christos struct ui_file *gdb_stdtargerr;
     82  1.1  christos 
     83  1.1  christos /* True if --batch or --batch-silent was seen.  */
     84  1.1  christos int batch_flag = 0;
     85  1.1  christos 
     86  1.1  christos /* Support for the --batch-silent option.  */
     87  1.1  christos int batch_silent = 0;
     88  1.1  christos 
     89  1.1  christos /* Support for --return-child-result option.
     90  1.1  christos    Set the default to -1 to return error in the case
     91  1.1  christos    that the program does not run or does not complete.  */
     92  1.1  christos int return_child_result = 0;
     93  1.1  christos int return_child_result_value = -1;
     94  1.1  christos 
     95  1.1  christos 
     96  1.1  christos /* GDB as it has been invoked from the command line (i.e. argv[0]).  */
     97  1.1  christos static char *gdb_program_name;
     98  1.1  christos 
     99  1.1  christos /* Return read only pointer to GDB_PROGRAM_NAME.  */
    100  1.1  christos const char *
    101  1.1  christos get_gdb_program_name (void)
    102  1.1  christos {
    103  1.1  christos   return gdb_program_name;
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos static void print_gdb_help (struct ui_file *);
    107  1.1  christos 
    108  1.3  christos /* Set the data-directory parameter to NEW_DATADIR.
    109  1.3  christos    If NEW_DATADIR is not a directory then a warning is printed.
    110  1.3  christos    We don't signal an error for backward compatibility.  */
    111  1.3  christos 
    112  1.3  christos void
    113  1.3  christos set_gdb_data_directory (const char *new_datadir)
    114  1.3  christos {
    115  1.3  christos   struct stat st;
    116  1.3  christos 
    117  1.3  christos   if (stat (new_datadir, &st) < 0)
    118  1.3  christos     {
    119  1.3  christos       int save_errno = errno;
    120  1.3  christos 
    121  1.3  christos       fprintf_unfiltered (gdb_stderr, "Warning: ");
    122  1.3  christos       print_sys_errmsg (new_datadir, save_errno);
    123  1.3  christos     }
    124  1.3  christos   else if (!S_ISDIR (st.st_mode))
    125  1.3  christos     warning (_("%s is not a directory."), new_datadir);
    126  1.3  christos 
    127  1.3  christos   xfree (gdb_datadir);
    128  1.3  christos   gdb_datadir = gdb_realpath (new_datadir);
    129  1.3  christos 
    130  1.3  christos   /* gdb_realpath won't return an absolute path if the path doesn't exist,
    131  1.3  christos      but we still want to record an absolute path here.  If the user entered
    132  1.3  christos      "../foo" and "../foo" doesn't exist then we'll record $(pwd)/../foo which
    133  1.3  christos      isn't canonical, but that's ok.  */
    134  1.3  christos   if (!IS_ABSOLUTE_PATH (gdb_datadir))
    135  1.3  christos     {
    136  1.3  christos       char *abs_datadir = gdb_abspath (gdb_datadir);
    137  1.3  christos 
    138  1.3  christos       xfree (gdb_datadir);
    139  1.3  christos       gdb_datadir = abs_datadir;
    140  1.3  christos     }
    141  1.3  christos }
    142  1.3  christos 
    143  1.1  christos /* Relocate a file or directory.  PROGNAME is the name by which gdb
    144  1.1  christos    was invoked (i.e., argv[0]).  INITIAL is the default value for the
    145  1.1  christos    file or directory.  FLAG is true if the value is relocatable, false
    146  1.1  christos    otherwise.  Returns a newly allocated string; this may return NULL
    147  1.1  christos    under the same conditions as make_relative_prefix.  */
    148  1.1  christos 
    149  1.1  christos static char *
    150  1.1  christos relocate_path (const char *progname, const char *initial, int flag)
    151  1.1  christos {
    152  1.1  christos   if (flag)
    153  1.1  christos     return make_relative_prefix (progname, BINDIR, initial);
    154  1.1  christos   return xstrdup (initial);
    155  1.1  christos }
    156  1.1  christos 
    157  1.1  christos /* Like relocate_path, but specifically checks for a directory.
    158  1.1  christos    INITIAL is relocated according to the rules of relocate_path.  If
    159  1.1  christos    the result is a directory, it is used; otherwise, INITIAL is used.
    160  1.1  christos    The chosen directory is then canonicalized using lrealpath.  This
    161  1.1  christos    function always returns a newly-allocated string.  */
    162  1.1  christos 
    163  1.1  christos char *
    164  1.1  christos relocate_gdb_directory (const char *initial, int flag)
    165  1.1  christos {
    166  1.1  christos   char *dir;
    167  1.1  christos 
    168  1.1  christos   dir = relocate_path (gdb_program_name, initial, flag);
    169  1.1  christos   if (dir)
    170  1.1  christos     {
    171  1.1  christos       struct stat s;
    172  1.1  christos 
    173  1.1  christos       if (*dir == '\0' || stat (dir, &s) != 0 || !S_ISDIR (s.st_mode))
    174  1.1  christos 	{
    175  1.1  christos 	  xfree (dir);
    176  1.1  christos 	  dir = NULL;
    177  1.1  christos 	}
    178  1.1  christos     }
    179  1.1  christos   if (!dir)
    180  1.1  christos     dir = xstrdup (initial);
    181  1.1  christos 
    182  1.1  christos   /* Canonicalize the directory.  */
    183  1.1  christos   if (*dir)
    184  1.1  christos     {
    185  1.1  christos       char *canon_sysroot = lrealpath (dir);
    186  1.1  christos 
    187  1.1  christos       if (canon_sysroot)
    188  1.1  christos 	{
    189  1.1  christos 	  xfree (dir);
    190  1.1  christos 	  dir = canon_sysroot;
    191  1.1  christos 	}
    192  1.1  christos     }
    193  1.1  christos 
    194  1.1  christos   return dir;
    195  1.1  christos }
    196  1.1  christos 
    197  1.1  christos /* Compute the locations of init files that GDB should source and
    198  1.1  christos    return them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT.  If
    199  1.1  christos    there is no system gdbinit (resp. home gdbinit and local gdbinit)
    200  1.1  christos    to be loaded, then SYSTEM_GDBINIT (resp. HOME_GDBINIT and
    201  1.1  christos    LOCAL_GDBINIT) is set to NULL.  */
    202  1.1  christos static void
    203  1.1  christos get_init_files (const char **system_gdbinit,
    204  1.1  christos 		const char **home_gdbinit,
    205  1.1  christos 		const char **local_gdbinit)
    206  1.1  christos {
    207  1.1  christos   static const char *sysgdbinit = NULL;
    208  1.1  christos   static char *homeinit = NULL;
    209  1.1  christos   static const char *localinit = NULL;
    210  1.1  christos   static int initialized = 0;
    211  1.1  christos 
    212  1.1  christos   if (!initialized)
    213  1.1  christos     {
    214  1.1  christos       struct stat homebuf, cwdbuf, s;
    215  1.1  christos       char *homedir;
    216  1.1  christos 
    217  1.1  christos       if (SYSTEM_GDBINIT[0])
    218  1.1  christos 	{
    219  1.1  christos 	  int datadir_len = strlen (GDB_DATADIR);
    220  1.1  christos 	  int sys_gdbinit_len = strlen (SYSTEM_GDBINIT);
    221  1.1  christos 	  char *relocated_sysgdbinit;
    222  1.1  christos 
    223  1.1  christos 	  /* If SYSTEM_GDBINIT lives in data-directory, and data-directory
    224  1.1  christos 	     has been provided, search for SYSTEM_GDBINIT there.  */
    225  1.1  christos 	  if (gdb_datadir_provided
    226  1.1  christos 	      && datadir_len < sys_gdbinit_len
    227  1.1  christos 	      && filename_ncmp (SYSTEM_GDBINIT, GDB_DATADIR, datadir_len) == 0
    228  1.1  christos 	      && IS_DIR_SEPARATOR (SYSTEM_GDBINIT[datadir_len]))
    229  1.1  christos 	    {
    230  1.1  christos 	      /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
    231  1.1  christos 		 to gdb_datadir.  */
    232  1.1  christos 	      char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
    233  1.1  christos 	      char *p;
    234  1.1  christos 
    235  1.1  christos 	      for (p = tmp_sys_gdbinit; IS_DIR_SEPARATOR (*p); ++p)
    236  1.1  christos 		continue;
    237  1.1  christos 	      relocated_sysgdbinit = concat (gdb_datadir, SLASH_STRING, p,
    238  1.1  christos 					     NULL);
    239  1.1  christos 	      xfree (tmp_sys_gdbinit);
    240  1.1  christos 	    }
    241  1.1  christos 	  else
    242  1.1  christos 	    {
    243  1.1  christos 	      relocated_sysgdbinit = relocate_path (gdb_program_name,
    244  1.1  christos 						    SYSTEM_GDBINIT,
    245  1.1  christos 						    SYSTEM_GDBINIT_RELOCATABLE);
    246  1.1  christos 	    }
    247  1.1  christos 	  if (relocated_sysgdbinit && stat (relocated_sysgdbinit, &s) == 0)
    248  1.1  christos 	    sysgdbinit = relocated_sysgdbinit;
    249  1.1  christos 	  else
    250  1.1  christos 	    xfree (relocated_sysgdbinit);
    251  1.1  christos 	}
    252  1.1  christos 
    253  1.1  christos       homedir = getenv ("HOME");
    254  1.1  christos 
    255  1.1  christos       /* If the .gdbinit file in the current directory is the same as
    256  1.1  christos 	 the $HOME/.gdbinit file, it should not be sourced.  homebuf
    257  1.1  christos 	 and cwdbuf are used in that purpose.  Make sure that the stats
    258  1.1  christos 	 are zero in case one of them fails (this guarantees that they
    259  1.1  christos 	 won't match if either exists).  */
    260  1.1  christos 
    261  1.1  christos       memset (&homebuf, 0, sizeof (struct stat));
    262  1.1  christos       memset (&cwdbuf, 0, sizeof (struct stat));
    263  1.1  christos 
    264  1.1  christos       if (homedir)
    265  1.1  christos 	{
    266  1.1  christos 	  homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
    267  1.1  christos 	  if (stat (homeinit, &homebuf) != 0)
    268  1.1  christos 	    {
    269  1.1  christos 	      xfree (homeinit);
    270  1.1  christos 	      homeinit = NULL;
    271  1.1  christos 	    }
    272  1.1  christos 	}
    273  1.1  christos 
    274  1.1  christos       if (stat (gdbinit, &cwdbuf) == 0)
    275  1.1  christos 	{
    276  1.1  christos 	  if (!homeinit
    277  1.1  christos 	      || memcmp ((char *) &homebuf, (char *) &cwdbuf,
    278  1.1  christos 			 sizeof (struct stat)))
    279  1.1  christos 	    localinit = gdbinit;
    280  1.1  christos 	}
    281  1.1  christos 
    282  1.1  christos       initialized = 1;
    283  1.1  christos     }
    284  1.1  christos 
    285  1.1  christos   *system_gdbinit = sysgdbinit;
    286  1.1  christos   *home_gdbinit = homeinit;
    287  1.1  christos   *local_gdbinit = localinit;
    288  1.1  christos }
    289  1.1  christos 
    290  1.3  christos /* Try to set up an alternate signal stack for SIGSEGV handlers.
    291  1.3  christos    This allows us to handle SIGSEGV signals generated when the
    292  1.3  christos    normal process stack is exhausted.  If this stack is not set
    293  1.3  christos    up (sigaltstack is unavailable or fails) and a SIGSEGV is
    294  1.3  christos    generated when the normal stack is exhausted then the program
    295  1.3  christos    will behave as though no SIGSEGV handler was installed.  */
    296  1.3  christos 
    297  1.3  christos static void
    298  1.3  christos setup_alternate_signal_stack (void)
    299  1.3  christos {
    300  1.3  christos #ifdef HAVE_SIGALTSTACK
    301  1.3  christos   stack_t ss;
    302  1.3  christos 
    303  1.3  christos   ss.ss_sp = xmalloc (SIGSTKSZ);
    304  1.3  christos   ss.ss_size = SIGSTKSZ;
    305  1.3  christos   ss.ss_flags = 0;
    306  1.3  christos 
    307  1.3  christos   sigaltstack(&ss, NULL);
    308  1.3  christos #endif
    309  1.3  christos }
    310  1.3  christos 
    311  1.1  christos /* Call command_loop.  If it happens to return, pass that through as a
    312  1.1  christos    non-zero return status.  */
    313  1.1  christos 
    314  1.1  christos static int
    315  1.1  christos captured_command_loop (void *data)
    316  1.1  christos {
    317  1.1  christos   /* Top-level execution commands can be run in the background from
    318  1.1  christos      here on.  */
    319  1.1  christos   interpreter_async = 1;
    320  1.1  christos 
    321  1.1  christos   current_interp_command_loop ();
    322  1.1  christos   /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
    323  1.1  christos      would clean things up (restoring the cleanup chain) to the state
    324  1.1  christos      they were just prior to the call.  Technically, this means that
    325  1.1  christos      the do_cleanups() below is redundant.  Unfortunately, many FUNCs
    326  1.1  christos      are not that well behaved.  do_cleanups should either be replaced
    327  1.1  christos      with a do_cleanups call (to cover the problem) or an assertion
    328  1.1  christos      check to detect bad FUNCs code.  */
    329  1.1  christos   do_cleanups (all_cleanups ());
    330  1.1  christos   /* If the command_loop returned, normally (rather than threw an
    331  1.1  christos      error) we try to quit.  If the quit is aborted, catch_errors()
    332  1.1  christos      which called this catch the signal and restart the command
    333  1.1  christos      loop.  */
    334  1.1  christos   quit_command (NULL, instream == stdin);
    335  1.1  christos   return 1;
    336  1.1  christos }
    337  1.1  christos 
    338  1.3  christos /* Handle command errors thrown from within
    339  1.3  christos    catch_command_errors/catch_command_errors_const.  */
    340  1.3  christos 
    341  1.3  christos static int
    342  1.3  christos handle_command_errors (volatile struct gdb_exception e)
    343  1.3  christos {
    344  1.3  christos   if (e.reason < 0)
    345  1.3  christos     {
    346  1.3  christos       exception_print (gdb_stderr, e);
    347  1.3  christos 
    348  1.3  christos       /* If any exception escaped to here, we better enable stdin.
    349  1.3  christos 	 Otherwise, any command that calls async_disable_stdin, and
    350  1.3  christos 	 then throws, will leave stdin inoperable.  */
    351  1.3  christos       async_enable_stdin ();
    352  1.3  christos       return 0;
    353  1.3  christos     }
    354  1.3  christos   return 1;
    355  1.3  christos }
    356  1.3  christos 
    357  1.3  christos /* Type of the command callback passed to catch_command_errors.  */
    358  1.3  christos 
    359  1.3  christos typedef void (catch_command_errors_ftype) (char *, int);
    360  1.3  christos 
    361  1.3  christos /* Wrap calls to commands run before the event loop is started.  */
    362  1.3  christos 
    363  1.3  christos static int
    364  1.3  christos catch_command_errors (catch_command_errors_ftype *command,
    365  1.3  christos 		      char *arg, int from_tty, return_mask mask)
    366  1.3  christos {
    367  1.3  christos   volatile struct gdb_exception e;
    368  1.3  christos 
    369  1.3  christos   TRY_CATCH (e, mask)
    370  1.3  christos     {
    371  1.3  christos       int was_sync = sync_execution;
    372  1.3  christos 
    373  1.3  christos       command (arg, from_tty);
    374  1.3  christos 
    375  1.3  christos       maybe_wait_sync_command_done (was_sync);
    376  1.3  christos     }
    377  1.3  christos   return handle_command_errors (e);
    378  1.3  christos }
    379  1.3  christos 
    380  1.3  christos /* Type of the command callback passed to catch_command_errors_const.  */
    381  1.3  christos 
    382  1.3  christos typedef void (catch_command_errors_const_ftype) (const char *, int);
    383  1.3  christos 
    384  1.3  christos /* Like catch_command_errors, but works with const command and args.  */
    385  1.3  christos 
    386  1.3  christos static int
    387  1.3  christos catch_command_errors_const (catch_command_errors_const_ftype *command,
    388  1.3  christos 			    const char *arg, int from_tty, return_mask mask)
    389  1.3  christos {
    390  1.3  christos   volatile struct gdb_exception e;
    391  1.3  christos 
    392  1.3  christos   TRY_CATCH (e, mask)
    393  1.3  christos     {
    394  1.3  christos       int was_sync = sync_execution;
    395  1.3  christos 
    396  1.3  christos       command (arg, from_tty);
    397  1.3  christos 
    398  1.3  christos       maybe_wait_sync_command_done (was_sync);
    399  1.3  christos     }
    400  1.3  christos   return handle_command_errors (e);
    401  1.3  christos }
    402  1.3  christos 
    403  1.1  christos /* Arguments of --command option and its counterpart.  */
    404  1.1  christos typedef struct cmdarg {
    405  1.1  christos   /* Type of this option.  */
    406  1.1  christos   enum {
    407  1.1  christos     /* Option type -x.  */
    408  1.1  christos     CMDARG_FILE,
    409  1.1  christos 
    410  1.1  christos     /* Option type -ex.  */
    411  1.1  christos     CMDARG_COMMAND,
    412  1.1  christos 
    413  1.1  christos     /* Option type -ix.  */
    414  1.1  christos     CMDARG_INIT_FILE,
    415  1.1  christos 
    416  1.1  christos     /* Option type -iex.  */
    417  1.1  christos     CMDARG_INIT_COMMAND
    418  1.1  christos   } type;
    419  1.1  christos 
    420  1.1  christos   /* Value of this option - filename or the GDB command itself.  String memory
    421  1.1  christos      is not owned by this structure despite it is 'const'.  */
    422  1.1  christos   char *string;
    423  1.1  christos } cmdarg_s;
    424  1.1  christos 
    425  1.1  christos /* Define type VEC (cmdarg_s).  */
    426  1.1  christos DEF_VEC_O (cmdarg_s);
    427  1.1  christos 
    428  1.1  christos static int
    429  1.1  christos captured_main (void *data)
    430  1.1  christos {
    431  1.1  christos   struct captured_main_args *context = data;
    432  1.1  christos   int argc = context->argc;
    433  1.1  christos   char **argv = context->argv;
    434  1.1  christos   static int quiet = 0;
    435  1.1  christos   static int set_args = 0;
    436  1.1  christos   static int inhibit_home_gdbinit = 0;
    437  1.1  christos 
    438  1.1  christos   /* Pointers to various arguments from command line.  */
    439  1.1  christos   char *symarg = NULL;
    440  1.1  christos   char *execarg = NULL;
    441  1.1  christos   char *pidarg = NULL;
    442  1.1  christos   char *corearg = NULL;
    443  1.1  christos   char *pid_or_core_arg = NULL;
    444  1.1  christos   char *cdarg = NULL;
    445  1.1  christos   char *ttyarg = NULL;
    446  1.1  christos 
    447  1.1  christos   /* These are static so that we can take their address in an
    448  1.1  christos      initializer.  */
    449  1.1  christos   static int print_help;
    450  1.1  christos   static int print_version;
    451  1.1  christos   static int print_configuration;
    452  1.1  christos 
    453  1.1  christos   /* Pointers to all arguments of --command option.  */
    454  1.1  christos   VEC (cmdarg_s) *cmdarg_vec = NULL;
    455  1.1  christos   struct cmdarg *cmdarg_p;
    456  1.1  christos 
    457  1.1  christos   /* Indices of all arguments of --directory option.  */
    458  1.1  christos   char **dirarg;
    459  1.1  christos   /* Allocated size.  */
    460  1.1  christos   int dirsize;
    461  1.1  christos   /* Number of elements used.  */
    462  1.1  christos   int ndir;
    463  1.1  christos 
    464  1.1  christos   /* gdb init files.  */
    465  1.1  christos   const char *system_gdbinit;
    466  1.1  christos   const char *home_gdbinit;
    467  1.1  christos   const char *local_gdbinit;
    468  1.1  christos 
    469  1.1  christos   int i;
    470  1.1  christos   int save_auto_load;
    471  1.1  christos   struct objfile *objfile;
    472  1.1  christos 
    473  1.1  christos   struct cleanup *pre_stat_chain;
    474  1.1  christos 
    475  1.1  christos #ifdef HAVE_SBRK
    476  1.1  christos   /* Set this before calling make_command_stats_cleanup.  */
    477  1.1  christos   lim_at_start = (char *) sbrk (0);
    478  1.1  christos #endif
    479  1.1  christos 
    480  1.1  christos   pre_stat_chain = make_command_stats_cleanup (0);
    481  1.1  christos 
    482  1.1  christos #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
    483  1.1  christos   setlocale (LC_MESSAGES, "");
    484  1.1  christos #endif
    485  1.1  christos #if defined (HAVE_SETLOCALE)
    486  1.1  christos   setlocale (LC_CTYPE, "");
    487  1.1  christos #endif
    488  1.1  christos   bindtextdomain (PACKAGE, LOCALEDIR);
    489  1.1  christos   textdomain (PACKAGE);
    490  1.1  christos 
    491  1.1  christos   bfd_init ();
    492  1.1  christos   notice_open_fds ();
    493  1.1  christos 
    494  1.1  christos   make_cleanup (VEC_cleanup (cmdarg_s), &cmdarg_vec);
    495  1.1  christos   dirsize = 1;
    496  1.1  christos   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
    497  1.1  christos   ndir = 0;
    498  1.1  christos 
    499  1.1  christos   clear_quit_flag ();
    500  1.1  christos   saved_command_line = (char *) xmalloc (saved_command_line_size);
    501  1.1  christos   saved_command_line[0] = '\0';
    502  1.1  christos   instream = stdin;
    503  1.1  christos 
    504  1.1  christos #ifdef __MINGW32__
    505  1.1  christos   /* Ensure stderr is unbuffered.  A Cygwin pty or pipe is implemented
    506  1.1  christos      as a Windows pipe, and Windows buffers on pipes.  */
    507  1.1  christos   setvbuf (stderr, NULL, _IONBF, BUFSIZ);
    508  1.1  christos #endif
    509  1.1  christos 
    510  1.1  christos   gdb_stdout = stdio_fileopen (stdout);
    511  1.1  christos   gdb_stderr = stderr_fileopen ();
    512  1.1  christos 
    513  1.1  christos   gdb_stdlog = gdb_stderr;	/* for moment */
    514  1.1  christos   gdb_stdtarg = gdb_stderr;	/* for moment */
    515  1.1  christos   gdb_stdin = stdio_fileopen (stdin);
    516  1.1  christos   gdb_stdtargerr = gdb_stderr;	/* for moment */
    517  1.1  christos   gdb_stdtargin = gdb_stdin;	/* for moment */
    518  1.1  christos 
    519  1.1  christos #ifdef __MINGW32__
    520  1.1  christos   /* On Windows, argv[0] is not necessarily set to absolute form when
    521  1.1  christos      GDB is found along PATH, without which relocation doesn't work.  */
    522  1.1  christos   gdb_program_name = windows_get_absolute_argv0 (argv[0]);
    523  1.1  christos #else
    524  1.1  christos   gdb_program_name = xstrdup (argv[0]);
    525  1.1  christos #endif
    526  1.1  christos 
    527  1.3  christos   /* Prefix warning messages with the command name.  */
    528  1.3  christos   warning_pre_print = xstrprintf ("%s: warning: ", gdb_program_name);
    529  1.3  christos 
    530  1.1  christos   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
    531  1.3  christos     perror_warning_with_name (_("error finding working directory"));
    532  1.3  christos 
    533  1.1  christos   current_directory = gdb_dirbuf;
    534  1.1  christos 
    535  1.1  christos   /* Set the sysroot path.  */
    536  1.1  christos   gdb_sysroot = relocate_gdb_directory (TARGET_SYSTEM_ROOT,
    537  1.1  christos 					TARGET_SYSTEM_ROOT_RELOCATABLE);
    538  1.1  christos 
    539  1.1  christos   debug_file_directory = relocate_gdb_directory (DEBUGDIR,
    540  1.1  christos 						 DEBUGDIR_RELOCATABLE);
    541  1.1  christos 
    542  1.1  christos   gdb_datadir = relocate_gdb_directory (GDB_DATADIR,
    543  1.1  christos 					GDB_DATADIR_RELOCATABLE);
    544  1.1  christos 
    545  1.1  christos #ifdef WITH_PYTHON_PATH
    546  1.1  christos   {
    547  1.1  christos     /* For later use in helping Python find itself.  */
    548  1.1  christos     char *tmp = concat (WITH_PYTHON_PATH, SLASH_STRING, "lib", NULL);
    549  1.1  christos 
    550  1.1  christos     python_libdir = relocate_gdb_directory (tmp, PYTHON_PATH_RELOCATABLE);
    551  1.1  christos     xfree (tmp);
    552  1.1  christos   }
    553  1.1  christos #endif
    554  1.1  christos 
    555  1.1  christos #ifdef RELOC_SRCDIR
    556  1.1  christos   add_substitute_path_rule (RELOC_SRCDIR,
    557  1.1  christos 			    make_relative_prefix (gdb_program_name, BINDIR,
    558  1.1  christos 						  RELOC_SRCDIR));
    559  1.1  christos #endif
    560  1.1  christos 
    561  1.1  christos   /* There will always be an interpreter.  Either the one passed into
    562  1.1  christos      this captured main, or one specified by the user at start up, or
    563  1.1  christos      the console.  Initialize the interpreter to the one requested by
    564  1.1  christos      the application.  */
    565  1.1  christos   interpreter_p = xstrdup (context->interpreter_p);
    566  1.1  christos 
    567  1.1  christos   /* Parse arguments and options.  */
    568  1.1  christos   {
    569  1.1  christos     int c;
    570  1.1  christos     /* When var field is 0, use flag field to record the equivalent
    571  1.1  christos        short option (or arbitrary numbers starting at 10 for those
    572  1.1  christos        with no equivalent).  */
    573  1.1  christos     enum {
    574  1.1  christos       OPT_SE = 10,
    575  1.1  christos       OPT_CD,
    576  1.1  christos       OPT_ANNOTATE,
    577  1.1  christos       OPT_STATISTICS,
    578  1.1  christos       OPT_TUI,
    579  1.1  christos       OPT_NOWINDOWS,
    580  1.1  christos       OPT_WINDOWS,
    581  1.1  christos       OPT_IX,
    582  1.1  christos       OPT_IEX
    583  1.1  christos     };
    584  1.1  christos     static struct option long_options[] =
    585  1.1  christos     {
    586  1.1  christos       {"tui", no_argument, 0, OPT_TUI},
    587  1.1  christos       {"xdb", no_argument, &xdb_commands, 1},
    588  1.1  christos       {"dbx", no_argument, &dbx_commands, 1},
    589  1.1  christos       {"readnow", no_argument, &readnow_symbol_files, 1},
    590  1.1  christos       {"r", no_argument, &readnow_symbol_files, 1},
    591  1.1  christos       {"quiet", no_argument, &quiet, 1},
    592  1.1  christos       {"q", no_argument, &quiet, 1},
    593  1.1  christos       {"silent", no_argument, &quiet, 1},
    594  1.1  christos       {"nh", no_argument, &inhibit_home_gdbinit, 1},
    595  1.1  christos       {"nx", no_argument, &inhibit_gdbinit, 1},
    596  1.1  christos       {"n", no_argument, &inhibit_gdbinit, 1},
    597  1.1  christos       {"batch-silent", no_argument, 0, 'B'},
    598  1.1  christos       {"batch", no_argument, &batch_flag, 1},
    599  1.1  christos 
    600  1.1  christos     /* This is a synonym for "--annotate=1".  --annotate is now
    601  1.1  christos        preferred, but keep this here for a long time because people
    602  1.1  christos        will be running emacses which use --fullname.  */
    603  1.1  christos       {"fullname", no_argument, 0, 'f'},
    604  1.1  christos       {"f", no_argument, 0, 'f'},
    605  1.1  christos 
    606  1.1  christos       {"annotate", required_argument, 0, OPT_ANNOTATE},
    607  1.1  christos       {"help", no_argument, &print_help, 1},
    608  1.1  christos       {"se", required_argument, 0, OPT_SE},
    609  1.1  christos       {"symbols", required_argument, 0, 's'},
    610  1.1  christos       {"s", required_argument, 0, 's'},
    611  1.1  christos       {"exec", required_argument, 0, 'e'},
    612  1.1  christos       {"e", required_argument, 0, 'e'},
    613  1.1  christos       {"core", required_argument, 0, 'c'},
    614  1.1  christos       {"c", required_argument, 0, 'c'},
    615  1.1  christos       {"pid", required_argument, 0, 'p'},
    616  1.1  christos       {"p", required_argument, 0, 'p'},
    617  1.1  christos       {"command", required_argument, 0, 'x'},
    618  1.1  christos       {"eval-command", required_argument, 0, 'X'},
    619  1.1  christos       {"version", no_argument, &print_version, 1},
    620  1.1  christos       {"configuration", no_argument, &print_configuration, 1},
    621  1.1  christos       {"x", required_argument, 0, 'x'},
    622  1.1  christos       {"ex", required_argument, 0, 'X'},
    623  1.1  christos       {"init-command", required_argument, 0, OPT_IX},
    624  1.1  christos       {"init-eval-command", required_argument, 0, OPT_IEX},
    625  1.1  christos       {"ix", required_argument, 0, OPT_IX},
    626  1.1  christos       {"iex", required_argument, 0, OPT_IEX},
    627  1.1  christos #ifdef GDBTK
    628  1.1  christos       {"tclcommand", required_argument, 0, 'z'},
    629  1.1  christos       {"enable-external-editor", no_argument, 0, 'y'},
    630  1.1  christos       {"editor-command", required_argument, 0, 'w'},
    631  1.1  christos #endif
    632  1.1  christos       {"ui", required_argument, 0, 'i'},
    633  1.1  christos       {"interpreter", required_argument, 0, 'i'},
    634  1.1  christos       {"i", required_argument, 0, 'i'},
    635  1.1  christos       {"directory", required_argument, 0, 'd'},
    636  1.1  christos       {"d", required_argument, 0, 'd'},
    637  1.1  christos       {"data-directory", required_argument, 0, 'D'},
    638  1.3  christos       {"D", required_argument, 0, 'D'},
    639  1.1  christos       {"cd", required_argument, 0, OPT_CD},
    640  1.1  christos       {"tty", required_argument, 0, 't'},
    641  1.1  christos       {"baud", required_argument, 0, 'b'},
    642  1.1  christos       {"b", required_argument, 0, 'b'},
    643  1.1  christos       {"nw", no_argument, NULL, OPT_NOWINDOWS},
    644  1.1  christos       {"nowindows", no_argument, NULL, OPT_NOWINDOWS},
    645  1.1  christos       {"w", no_argument, NULL, OPT_WINDOWS},
    646  1.1  christos       {"windows", no_argument, NULL, OPT_WINDOWS},
    647  1.1  christos       {"statistics", no_argument, 0, OPT_STATISTICS},
    648  1.1  christos       {"write", no_argument, &write_files, 1},
    649  1.1  christos       {"args", no_argument, &set_args, 1},
    650  1.1  christos       {"l", required_argument, 0, 'l'},
    651  1.1  christos       {"return-child-result", no_argument, &return_child_result, 1},
    652  1.1  christos       {0, no_argument, 0, 0}
    653  1.1  christos     };
    654  1.1  christos 
    655  1.1  christos     while (1)
    656  1.1  christos       {
    657  1.1  christos 	int option_index;
    658  1.1  christos 
    659  1.1  christos 	c = getopt_long_only (argc, argv, "",
    660  1.1  christos 			      long_options, &option_index);
    661  1.1  christos 	if (c == EOF || set_args)
    662  1.1  christos 	  break;
    663  1.1  christos 
    664  1.1  christos 	/* Long option that takes an argument.  */
    665  1.1  christos 	if (c == 0 && long_options[option_index].flag == 0)
    666  1.1  christos 	  c = long_options[option_index].val;
    667  1.1  christos 
    668  1.1  christos 	switch (c)
    669  1.1  christos 	  {
    670  1.1  christos 	  case 0:
    671  1.1  christos 	    /* Long option that just sets a flag.  */
    672  1.1  christos 	    break;
    673  1.1  christos 	  case OPT_SE:
    674  1.1  christos 	    symarg = optarg;
    675  1.1  christos 	    execarg = optarg;
    676  1.1  christos 	    break;
    677  1.1  christos 	  case OPT_CD:
    678  1.1  christos 	    cdarg = optarg;
    679  1.1  christos 	    break;
    680  1.1  christos 	  case OPT_ANNOTATE:
    681  1.1  christos 	    /* FIXME: what if the syntax is wrong (e.g. not digits)?  */
    682  1.1  christos 	    annotation_level = atoi (optarg);
    683  1.1  christos 	    break;
    684  1.1  christos 	  case OPT_STATISTICS:
    685  1.1  christos 	    /* Enable the display of both time and space usage.  */
    686  1.1  christos 	    set_per_command_time (1);
    687  1.1  christos 	    set_per_command_space (1);
    688  1.1  christos 	    break;
    689  1.1  christos 	  case OPT_TUI:
    690  1.1  christos 	    /* --tui is equivalent to -i=tui.  */
    691  1.1  christos #ifdef TUI
    692  1.1  christos 	    xfree (interpreter_p);
    693  1.1  christos 	    interpreter_p = xstrdup (INTERP_TUI);
    694  1.1  christos #else
    695  1.3  christos 	    error (_("%s: TUI mode is not supported"), gdb_program_name);
    696  1.1  christos #endif
    697  1.1  christos 	    break;
    698  1.1  christos 	  case OPT_WINDOWS:
    699  1.1  christos 	    /* FIXME: cagney/2003-03-01: Not sure if this option is
    700  1.1  christos                actually useful, and if it is, what it should do.  */
    701  1.1  christos #ifdef GDBTK
    702  1.1  christos 	    /* --windows is equivalent to -i=insight.  */
    703  1.1  christos 	    xfree (interpreter_p);
    704  1.1  christos 	    interpreter_p = xstrdup (INTERP_INSIGHT);
    705  1.1  christos #endif
    706  1.1  christos 	    break;
    707  1.1  christos 	  case OPT_NOWINDOWS:
    708  1.1  christos 	    /* -nw is equivalent to -i=console.  */
    709  1.1  christos 	    xfree (interpreter_p);
    710  1.1  christos 	    interpreter_p = xstrdup (INTERP_CONSOLE);
    711  1.1  christos 	    break;
    712  1.1  christos 	  case 'f':
    713  1.1  christos 	    annotation_level = 1;
    714  1.1  christos 	    break;
    715  1.1  christos 	  case 's':
    716  1.1  christos 	    symarg = optarg;
    717  1.1  christos 	    break;
    718  1.1  christos 	  case 'e':
    719  1.1  christos 	    execarg = optarg;
    720  1.1  christos 	    break;
    721  1.1  christos 	  case 'c':
    722  1.1  christos 	    corearg = optarg;
    723  1.1  christos 	    break;
    724  1.1  christos 	  case 'p':
    725  1.1  christos 	    pidarg = optarg;
    726  1.1  christos 	    break;
    727  1.1  christos 	  case 'x':
    728  1.1  christos 	    {
    729  1.1  christos 	      struct cmdarg cmdarg = { CMDARG_FILE, optarg };
    730  1.1  christos 
    731  1.1  christos 	      VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
    732  1.1  christos 	    }
    733  1.1  christos 	    break;
    734  1.1  christos 	  case 'X':
    735  1.1  christos 	    {
    736  1.1  christos 	      struct cmdarg cmdarg = { CMDARG_COMMAND, optarg };
    737  1.1  christos 
    738  1.1  christos 	      VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
    739  1.1  christos 	    }
    740  1.1  christos 	    break;
    741  1.1  christos 	  case OPT_IX:
    742  1.1  christos 	    {
    743  1.1  christos 	      struct cmdarg cmdarg = { CMDARG_INIT_FILE, optarg };
    744  1.1  christos 
    745  1.1  christos 	      VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
    746  1.1  christos 	    }
    747  1.1  christos 	    break;
    748  1.1  christos 	  case OPT_IEX:
    749  1.1  christos 	    {
    750  1.1  christos 	      struct cmdarg cmdarg = { CMDARG_INIT_COMMAND, optarg };
    751  1.1  christos 
    752  1.1  christos 	      VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
    753  1.1  christos 	    }
    754  1.1  christos 	    break;
    755  1.1  christos 	  case 'B':
    756  1.1  christos 	    batch_flag = batch_silent = 1;
    757  1.1  christos 	    gdb_stdout = ui_file_new();
    758  1.1  christos 	    break;
    759  1.1  christos 	  case 'D':
    760  1.3  christos 	    if (optarg[0] == '\0')
    761  1.3  christos 	      error (_("%s: empty path for `--data-directory'"),
    762  1.3  christos 		     gdb_program_name);
    763  1.3  christos 	    set_gdb_data_directory (optarg);
    764  1.1  christos 	    gdb_datadir_provided = 1;
    765  1.1  christos 	    break;
    766  1.1  christos #ifdef GDBTK
    767  1.1  christos 	  case 'z':
    768  1.1  christos 	    {
    769  1.1  christos 	      extern int gdbtk_test (char *);
    770  1.1  christos 
    771  1.1  christos 	      if (!gdbtk_test (optarg))
    772  1.3  christos 		error (_("%s: unable to load tclcommand file \"%s\""),
    773  1.3  christos 		       gdb_program_name, optarg);
    774  1.1  christos 	      break;
    775  1.1  christos 	    }
    776  1.1  christos 	  case 'y':
    777  1.1  christos 	    /* Backwards compatibility only.  */
    778  1.1  christos 	    break;
    779  1.1  christos 	  case 'w':
    780  1.1  christos 	    {
    781  1.1  christos 	      /* Set the external editor commands when gdb is farming out files
    782  1.1  christos 		 to be edited by another program.  */
    783  1.1  christos 	      extern char *external_editor_command;
    784  1.1  christos 
    785  1.1  christos 	      external_editor_command = xstrdup (optarg);
    786  1.1  christos 	      break;
    787  1.1  christos 	    }
    788  1.1  christos #endif /* GDBTK */
    789  1.1  christos 	  case 'i':
    790  1.1  christos 	    xfree (interpreter_p);
    791  1.1  christos 	    interpreter_p = xstrdup (optarg);
    792  1.1  christos 	    break;
    793  1.1  christos 	  case 'd':
    794  1.1  christos 	    dirarg[ndir++] = optarg;
    795  1.1  christos 	    if (ndir >= dirsize)
    796  1.1  christos 	      {
    797  1.1  christos 		dirsize *= 2;
    798  1.1  christos 		dirarg = (char **) xrealloc ((char *) dirarg,
    799  1.1  christos 					     dirsize * sizeof (*dirarg));
    800  1.1  christos 	      }
    801  1.1  christos 	    break;
    802  1.1  christos 	  case 't':
    803  1.1  christos 	    ttyarg = optarg;
    804  1.1  christos 	    break;
    805  1.1  christos 	  case 'q':
    806  1.1  christos 	    quiet = 1;
    807  1.1  christos 	    break;
    808  1.1  christos 	  case 'b':
    809  1.1  christos 	    {
    810  1.1  christos 	      int i;
    811  1.1  christos 	      char *p;
    812  1.1  christos 
    813  1.1  christos 	      i = strtol (optarg, &p, 0);
    814  1.1  christos 	      if (i == 0 && p == optarg)
    815  1.3  christos 		warning (_("could not set baud rate to `%s'."),
    816  1.3  christos 			 optarg);
    817  1.1  christos 	      else
    818  1.1  christos 		baud_rate = i;
    819  1.1  christos 	    }
    820  1.1  christos             break;
    821  1.1  christos 	  case 'l':
    822  1.1  christos 	    {
    823  1.1  christos 	      int i;
    824  1.1  christos 	      char *p;
    825  1.1  christos 
    826  1.1  christos 	      i = strtol (optarg, &p, 0);
    827  1.1  christos 	      if (i == 0 && p == optarg)
    828  1.3  christos 		warning (_("could not set timeout limit to `%s'."),
    829  1.3  christos 			 optarg);
    830  1.1  christos 	      else
    831  1.1  christos 		remote_timeout = i;
    832  1.1  christos 	    }
    833  1.1  christos 	    break;
    834  1.1  christos 
    835  1.1  christos 	  case '?':
    836  1.3  christos 	    error (_("Use `%s --help' for a complete list of options."),
    837  1.3  christos 		   gdb_program_name);
    838  1.1  christos 	  }
    839  1.1  christos       }
    840  1.1  christos 
    841  1.1  christos     if (batch_flag)
    842  1.1  christos       quiet = 1;
    843  1.1  christos   }
    844  1.1  christos 
    845  1.3  christos   /* Try to set up an alternate signal stack for SIGSEGV handlers.  */
    846  1.3  christos   setup_alternate_signal_stack ();
    847  1.3  christos 
    848  1.1  christos   /* Initialize all files.  Give the interpreter a chance to take
    849  1.1  christos      control of the console via the deprecated_init_ui_hook ().  */
    850  1.1  christos   gdb_init (gdb_program_name);
    851  1.1  christos 
    852  1.1  christos   /* Now that gdb_init has created the initial inferior, we're in
    853  1.1  christos      position to set args for that inferior.  */
    854  1.1  christos   if (set_args)
    855  1.1  christos     {
    856  1.1  christos       /* The remaining options are the command-line options for the
    857  1.1  christos 	 inferior.  The first one is the sym/exec file, and the rest
    858  1.1  christos 	 are arguments.  */
    859  1.1  christos       if (optind >= argc)
    860  1.3  christos 	error (_("%s: `--args' specified but no program specified"),
    861  1.3  christos 	       gdb_program_name);
    862  1.3  christos 
    863  1.1  christos       symarg = argv[optind];
    864  1.1  christos       execarg = argv[optind];
    865  1.1  christos       ++optind;
    866  1.1  christos       set_inferior_args_vector (argc - optind, &argv[optind]);
    867  1.1  christos     }
    868  1.1  christos   else
    869  1.1  christos     {
    870  1.1  christos       /* OK, that's all the options.  */
    871  1.1  christos 
    872  1.1  christos       /* The first argument, if specified, is the name of the
    873  1.1  christos 	 executable.  */
    874  1.1  christos       if (optind < argc)
    875  1.1  christos 	{
    876  1.1  christos 	  symarg = argv[optind];
    877  1.1  christos 	  execarg = argv[optind];
    878  1.1  christos 	  optind++;
    879  1.1  christos 	}
    880  1.1  christos 
    881  1.1  christos       /* If the user hasn't already specified a PID or the name of a
    882  1.1  christos 	 core file, then a second optional argument is allowed.  If
    883  1.1  christos 	 present, this argument should be interpreted as either a
    884  1.1  christos 	 PID or a core file, whichever works.  */
    885  1.1  christos       if (pidarg == NULL && corearg == NULL && optind < argc)
    886  1.1  christos 	{
    887  1.1  christos 	  pid_or_core_arg = argv[optind];
    888  1.1  christos 	  optind++;
    889  1.1  christos 	}
    890  1.1  christos 
    891  1.1  christos       /* Any argument left on the command line is unexpected and
    892  1.1  christos 	 will be ignored.  Inform the user.  */
    893  1.1  christos       if (optind < argc)
    894  1.1  christos 	fprintf_unfiltered (gdb_stderr,
    895  1.1  christos 			    _("Excess command line "
    896  1.1  christos 			      "arguments ignored. (%s%s)\n"),
    897  1.1  christos 			    argv[optind],
    898  1.1  christos 			    (optind == argc - 1) ? "" : " ...");
    899  1.1  christos     }
    900  1.1  christos 
    901  1.1  christos   /* Lookup gdbinit files.  Note that the gdbinit file name may be
    902  1.1  christos      overriden during file initialization, so get_init_files should be
    903  1.1  christos      called after gdb_init.  */
    904  1.1  christos   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
    905  1.1  christos 
    906  1.1  christos   /* Do these (and anything which might call wrap_here or *_filtered)
    907  1.1  christos      after initialize_all_files() but before the interpreter has been
    908  1.1  christos      installed.  Otherwize the help/version messages will be eaten by
    909  1.1  christos      the interpreter's output handler.  */
    910  1.1  christos 
    911  1.1  christos   if (print_version)
    912  1.1  christos     {
    913  1.1  christos       print_gdb_version (gdb_stdout);
    914  1.1  christos       wrap_here ("");
    915  1.1  christos       printf_filtered ("\n");
    916  1.1  christos       exit (0);
    917  1.1  christos     }
    918  1.1  christos 
    919  1.1  christos   if (print_help)
    920  1.1  christos     {
    921  1.1  christos       print_gdb_help (gdb_stdout);
    922  1.1  christos       fputs_unfiltered ("\n", gdb_stdout);
    923  1.1  christos       exit (0);
    924  1.1  christos     }
    925  1.1  christos 
    926  1.1  christos   if (print_configuration)
    927  1.1  christos     {
    928  1.1  christos       print_gdb_configuration (gdb_stdout);
    929  1.1  christos       wrap_here ("");
    930  1.1  christos       printf_filtered ("\n");
    931  1.1  christos       exit (0);
    932  1.1  christos     }
    933  1.1  christos 
    934  1.1  christos   /* FIXME: cagney/2003-02-03: The big hack (part 1 of 2) that lets
    935  1.1  christos      GDB retain the old MI1 interpreter startup behavior.  Output the
    936  1.1  christos      copyright message before the interpreter is installed.  That way
    937  1.1  christos      it isn't encapsulated in MI output.  */
    938  1.1  christos   if (!quiet && strcmp (interpreter_p, INTERP_MI1) == 0)
    939  1.1  christos     {
    940  1.1  christos       /* Print all the junk at the top, with trailing "..." if we are
    941  1.1  christos          about to read a symbol file (possibly slowly).  */
    942  1.1  christos       print_gdb_version (gdb_stdout);
    943  1.1  christos       if (symarg)
    944  1.1  christos 	printf_filtered ("..");
    945  1.1  christos       wrap_here ("");
    946  1.1  christos       printf_filtered ("\n");
    947  1.1  christos       gdb_flush (gdb_stdout);	/* Force to screen during slow
    948  1.1  christos 				   operations.  */
    949  1.1  christos     }
    950  1.1  christos 
    951  1.1  christos   /* Install the default UI.  All the interpreters should have had a
    952  1.1  christos      look at things by now.  Initialize the default interpreter.  */
    953  1.1  christos 
    954  1.1  christos   {
    955  1.1  christos     /* Find it.  */
    956  1.1  christos     struct interp *interp = interp_lookup (interpreter_p);
    957  1.1  christos 
    958  1.1  christos     if (interp == NULL)
    959  1.1  christos       error (_("Interpreter `%s' unrecognized"), interpreter_p);
    960  1.1  christos     /* Install it.  */
    961  1.1  christos     if (!interp_set (interp, 1))
    962  1.3  christos       error (_("Interpreter `%s' failed to initialize."), interpreter_p);
    963  1.1  christos   }
    964  1.1  christos 
    965  1.1  christos   /* FIXME: cagney/2003-02-03: The big hack (part 2 of 2) that lets
    966  1.1  christos      GDB retain the old MI1 interpreter startup behavior.  Output the
    967  1.1  christos      copyright message after the interpreter is installed when it is
    968  1.1  christos      any sane interpreter.  */
    969  1.1  christos   if (!quiet && !current_interp_named_p (INTERP_MI1))
    970  1.1  christos     {
    971  1.1  christos       /* Print all the junk at the top, with trailing "..." if we are
    972  1.1  christos          about to read a symbol file (possibly slowly).  */
    973  1.1  christos       print_gdb_version (gdb_stdout);
    974  1.1  christos       if (symarg)
    975  1.1  christos 	printf_filtered ("..");
    976  1.1  christos       wrap_here ("");
    977  1.1  christos       printf_filtered ("\n");
    978  1.1  christos       gdb_flush (gdb_stdout);	/* Force to screen during slow
    979  1.1  christos 				   operations.  */
    980  1.1  christos     }
    981  1.1  christos 
    982  1.1  christos   /* Set off error and warning messages with a blank line.  */
    983  1.3  christos   xfree (warning_pre_print);
    984  1.1  christos   warning_pre_print = _("\nwarning: ");
    985  1.1  christos 
    986  1.1  christos   /* Read and execute the system-wide gdbinit file, if it exists.
    987  1.1  christos      This is done *before* all the command line arguments are
    988  1.1  christos      processed; it sets global parameters, which are independent of
    989  1.1  christos      what file you are debugging or what directory you are in.  */
    990  1.1  christos   if (system_gdbinit && !inhibit_gdbinit)
    991  1.1  christos     catch_command_errors_const (source_script, system_gdbinit,
    992  1.1  christos 				0, RETURN_MASK_ALL);
    993  1.1  christos 
    994  1.1  christos   /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
    995  1.1  christos      *before* all the command line arguments are processed; it sets
    996  1.1  christos      global parameters, which are independent of what file you are
    997  1.1  christos      debugging or what directory you are in.  */
    998  1.1  christos 
    999  1.1  christos   if (home_gdbinit && !inhibit_gdbinit && !inhibit_home_gdbinit)
   1000  1.1  christos     catch_command_errors_const (source_script,
   1001  1.1  christos 				home_gdbinit, 0, RETURN_MASK_ALL);
   1002  1.1  christos 
   1003  1.1  christos   /* Process '-ix' and '-iex' options early.  */
   1004  1.1  christos   for (i = 0; VEC_iterate (cmdarg_s, cmdarg_vec, i, cmdarg_p); i++)
   1005  1.1  christos     switch (cmdarg_p->type)
   1006  1.1  christos     {
   1007  1.1  christos       case CMDARG_INIT_FILE:
   1008  1.1  christos         catch_command_errors_const (source_script, cmdarg_p->string,
   1009  1.1  christos 				    !batch_flag, RETURN_MASK_ALL);
   1010  1.1  christos 	break;
   1011  1.1  christos       case CMDARG_INIT_COMMAND:
   1012  1.1  christos         catch_command_errors (execute_command, cmdarg_p->string,
   1013  1.1  christos 			      !batch_flag, RETURN_MASK_ALL);
   1014  1.1  christos 	break;
   1015  1.1  christos     }
   1016  1.1  christos 
   1017  1.1  christos   /* Now perform all the actions indicated by the arguments.  */
   1018  1.1  christos   if (cdarg != NULL)
   1019  1.1  christos     {
   1020  1.1  christos       catch_command_errors (cd_command, cdarg, 0, RETURN_MASK_ALL);
   1021  1.1  christos     }
   1022  1.1  christos 
   1023  1.1  christos   for (i = 0; i < ndir; i++)
   1024  1.1  christos     catch_command_errors (directory_switch, dirarg[i], 0, RETURN_MASK_ALL);
   1025  1.1  christos   xfree (dirarg);
   1026  1.1  christos 
   1027  1.1  christos   /* Skip auto-loading section-specified scripts until we've sourced
   1028  1.1  christos      local_gdbinit (which is often used to augment the source search
   1029  1.1  christos      path).  */
   1030  1.1  christos   save_auto_load = global_auto_load;
   1031  1.1  christos   global_auto_load = 0;
   1032  1.1  christos 
   1033  1.1  christos   if (execarg != NULL
   1034  1.1  christos       && symarg != NULL
   1035  1.1  christos       && strcmp (execarg, symarg) == 0)
   1036  1.1  christos     {
   1037  1.1  christos       /* The exec file and the symbol-file are the same.  If we can't
   1038  1.1  christos          open it, better only print one error message.
   1039  1.1  christos          catch_command_errors returns non-zero on success!  */
   1040  1.3  christos       if (catch_command_errors_const (exec_file_attach, execarg,
   1041  1.3  christos 				      !batch_flag, RETURN_MASK_ALL))
   1042  1.1  christos 	catch_command_errors_const (symbol_file_add_main, symarg,
   1043  1.1  christos 				    !batch_flag, RETURN_MASK_ALL);
   1044  1.1  christos     }
   1045  1.1  christos   else
   1046  1.1  christos     {
   1047  1.1  christos       if (execarg != NULL)
   1048  1.3  christos 	catch_command_errors_const (exec_file_attach, execarg,
   1049  1.3  christos 				    !batch_flag, RETURN_MASK_ALL);
   1050  1.1  christos       if (symarg != NULL)
   1051  1.1  christos 	catch_command_errors_const (symbol_file_add_main, symarg,
   1052  1.1  christos 				    !batch_flag, RETURN_MASK_ALL);
   1053  1.1  christos     }
   1054  1.1  christos 
   1055  1.1  christos   if (corearg && pidarg)
   1056  1.1  christos     error (_("Can't attach to process and specify "
   1057  1.1  christos 	     "a core file at the same time."));
   1058  1.1  christos 
   1059  1.1  christos   if (corearg != NULL)
   1060  1.1  christos     catch_command_errors (core_file_command, corearg,
   1061  1.1  christos 			  !batch_flag, RETURN_MASK_ALL);
   1062  1.1  christos   else if (pidarg != NULL)
   1063  1.1  christos     catch_command_errors (attach_command, pidarg,
   1064  1.1  christos 			  !batch_flag, RETURN_MASK_ALL);
   1065  1.1  christos   else if (pid_or_core_arg)
   1066  1.1  christos     {
   1067  1.1  christos       /* The user specified 'gdb program pid' or gdb program core'.
   1068  1.1  christos 	 If pid_or_core_arg's first character is a digit, try attach
   1069  1.1  christos 	 first and then corefile.  Otherwise try just corefile.  */
   1070  1.1  christos 
   1071  1.1  christos       if (isdigit (pid_or_core_arg[0]))
   1072  1.1  christos 	{
   1073  1.1  christos 	  if (catch_command_errors (attach_command, pid_or_core_arg,
   1074  1.1  christos 				    !batch_flag, RETURN_MASK_ALL) == 0)
   1075  1.1  christos 	    catch_command_errors (core_file_command, pid_or_core_arg,
   1076  1.1  christos 				  !batch_flag, RETURN_MASK_ALL);
   1077  1.1  christos 	}
   1078  1.1  christos       else /* Can't be a pid, better be a corefile.  */
   1079  1.1  christos 	catch_command_errors (core_file_command, pid_or_core_arg,
   1080  1.1  christos 			      !batch_flag, RETURN_MASK_ALL);
   1081  1.1  christos     }
   1082  1.1  christos 
   1083  1.1  christos   if (ttyarg != NULL)
   1084  1.1  christos     set_inferior_io_terminal (ttyarg);
   1085  1.1  christos 
   1086  1.1  christos   /* Error messages should no longer be distinguished with extra output.  */
   1087  1.1  christos   warning_pre_print = _("warning: ");
   1088  1.1  christos 
   1089  1.1  christos   /* Read the .gdbinit file in the current directory, *if* it isn't
   1090  1.1  christos      the same as the $HOME/.gdbinit file (it should exist, also).  */
   1091  1.1  christos   if (local_gdbinit)
   1092  1.1  christos     {
   1093  1.1  christos       auto_load_local_gdbinit_pathname = gdb_realpath (local_gdbinit);
   1094  1.1  christos 
   1095  1.1  christos       if (!inhibit_gdbinit && auto_load_local_gdbinit
   1096  1.1  christos 	  && file_is_auto_load_safe (local_gdbinit,
   1097  1.1  christos 				     _("auto-load: Loading .gdbinit "
   1098  1.1  christos 				       "file \"%s\".\n"),
   1099  1.1  christos 				     local_gdbinit))
   1100  1.1  christos 	{
   1101  1.1  christos 	  auto_load_local_gdbinit_loaded = 1;
   1102  1.1  christos 
   1103  1.1  christos 	  catch_command_errors_const (source_script, local_gdbinit, 0,
   1104  1.1  christos 				      RETURN_MASK_ALL);
   1105  1.1  christos 	}
   1106  1.1  christos     }
   1107  1.1  christos 
   1108  1.1  christos   /* Now that all .gdbinit's have been read and all -d options have been
   1109  1.1  christos      processed, we can read any scripts mentioned in SYMARG.
   1110  1.1  christos      We wait until now because it is common to add to the source search
   1111  1.1  christos      path in local_gdbinit.  */
   1112  1.1  christos   global_auto_load = save_auto_load;
   1113  1.1  christos   ALL_OBJFILES (objfile)
   1114  1.1  christos     load_auto_scripts_for_objfile (objfile);
   1115  1.1  christos 
   1116  1.1  christos   /* Process '-x' and '-ex' options.  */
   1117  1.1  christos   for (i = 0; VEC_iterate (cmdarg_s, cmdarg_vec, i, cmdarg_p); i++)
   1118  1.1  christos     switch (cmdarg_p->type)
   1119  1.1  christos     {
   1120  1.1  christos       case CMDARG_FILE:
   1121  1.1  christos         catch_command_errors_const (source_script, cmdarg_p->string,
   1122  1.1  christos 				    !batch_flag, RETURN_MASK_ALL);
   1123  1.1  christos 	break;
   1124  1.1  christos       case CMDARG_COMMAND:
   1125  1.1  christos         catch_command_errors (execute_command, cmdarg_p->string,
   1126  1.1  christos 			      !batch_flag, RETURN_MASK_ALL);
   1127  1.1  christos 	break;
   1128  1.1  christos     }
   1129  1.1  christos 
   1130  1.1  christos   /* Read in the old history after all the command files have been
   1131  1.1  christos      read.  */
   1132  1.1  christos   init_history ();
   1133  1.1  christos 
   1134  1.1  christos   if (batch_flag)
   1135  1.1  christos     {
   1136  1.1  christos       /* We have hit the end of the batch file.  */
   1137  1.1  christos       quit_force (NULL, 0);
   1138  1.1  christos     }
   1139  1.1  christos 
   1140  1.1  christos   /* Show time and/or space usage.  */
   1141  1.1  christos   do_cleanups (pre_stat_chain);
   1142  1.1  christos 
   1143  1.1  christos   /* NOTE: cagney/1999-11-07: There is probably no reason for not
   1144  1.1  christos      moving this loop and the code found in captured_command_loop()
   1145  1.1  christos      into the command_loop() proper.  The main thing holding back that
   1146  1.1  christos      change - SET_TOP_LEVEL() - has been eliminated.  */
   1147  1.1  christos   while (1)
   1148  1.1  christos     {
   1149  1.1  christos       catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
   1150  1.1  christos     }
   1151  1.1  christos   /* No exit -- exit is through quit_command.  */
   1152  1.1  christos }
   1153  1.1  christos 
   1154  1.1  christos int
   1155  1.1  christos gdb_main (struct captured_main_args *args)
   1156  1.1  christos {
   1157  1.1  christos   catch_errors (captured_main, args, "", RETURN_MASK_ALL);
   1158  1.1  christos   /* The only way to end up here is by an error (normal exit is
   1159  1.1  christos      handled by quit_force()), hence always return an error status.  */
   1160  1.1  christos   return 1;
   1161  1.1  christos }
   1162  1.1  christos 
   1163  1.1  christos 
   1164  1.1  christos /* Don't use *_filtered for printing help.  We don't want to prompt
   1165  1.1  christos    for continue no matter how small the screen or how much we're going
   1166  1.1  christos    to print.  */
   1167  1.1  christos 
   1168  1.1  christos static void
   1169  1.1  christos print_gdb_help (struct ui_file *stream)
   1170  1.1  christos {
   1171  1.1  christos   const char *system_gdbinit;
   1172  1.1  christos   const char *home_gdbinit;
   1173  1.1  christos   const char *local_gdbinit;
   1174  1.1  christos 
   1175  1.1  christos   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
   1176  1.1  christos 
   1177  1.1  christos   /* Note: The options in the list below are only approximately sorted
   1178  1.1  christos      in the alphabetical order, so as to group closely related options
   1179  1.1  christos      together.  */
   1180  1.1  christos   fputs_unfiltered (_("\
   1181  1.1  christos This is the GNU debugger.  Usage:\n\n\
   1182  1.1  christos     gdb [options] [executable-file [core-file or process-id]]\n\
   1183  1.1  christos     gdb [options] --args executable-file [inferior-arguments ...]\n\n\
   1184  1.1  christos "), stream);
   1185  1.1  christos   fputs_unfiltered (_("\
   1186  1.1  christos Selection of debuggee and its files:\n\n\
   1187  1.1  christos   --args             Arguments after executable-file are passed to inferior\n\
   1188  1.1  christos   --core=COREFILE    Analyze the core dump COREFILE.\n\
   1189  1.1  christos   --exec=EXECFILE    Use EXECFILE as the executable.\n\
   1190  1.1  christos   --pid=PID          Attach to running process PID.\n\
   1191  1.1  christos   --directory=DIR    Search for source files in DIR.\n\
   1192  1.1  christos   --se=FILE          Use FILE as symbol file and executable file.\n\
   1193  1.1  christos   --symbols=SYMFILE  Read symbols from SYMFILE.\n\
   1194  1.1  christos   --readnow          Fully read symbol files on first access.\n\
   1195  1.1  christos   --write            Set writing into executable and core files.\n\n\
   1196  1.1  christos "), stream);
   1197  1.1  christos   fputs_unfiltered (_("\
   1198  1.1  christos Initial commands and command files:\n\n\
   1199  1.1  christos   --command=FILE, -x Execute GDB commands from FILE.\n\
   1200  1.1  christos   --init-command=FILE, -ix\n\
   1201  1.1  christos                      Like -x but execute commands before loading inferior.\n\
   1202  1.1  christos   --eval-command=COMMAND, -ex\n\
   1203  1.1  christos                      Execute a single GDB command.\n\
   1204  1.1  christos                      May be used multiple times and in conjunction\n\
   1205  1.1  christos                      with --command.\n\
   1206  1.1  christos   --init-eval-command=COMMAND, -iex\n\
   1207  1.1  christos                      Like -ex but before loading inferior.\n\
   1208  1.1  christos   --nh               Do not read ~/.gdbinit.\n\
   1209  1.1  christos   --nx               Do not read any .gdbinit files in any directory.\n\n\
   1210  1.1  christos "), stream);
   1211  1.1  christos   fputs_unfiltered (_("\
   1212  1.1  christos Output and user interface control:\n\n\
   1213  1.1  christos   --fullname         Output information used by emacs-GDB interface.\n\
   1214  1.1  christos   --interpreter=INTERP\n\
   1215  1.1  christos                      Select a specific interpreter / user interface\n\
   1216  1.1  christos   --tty=TTY          Use TTY for input/output by the program being debugged.\n\
   1217  1.1  christos   -w                 Use the GUI interface.\n\
   1218  1.1  christos   --nw               Do not use the GUI interface.\n\
   1219  1.1  christos "), stream);
   1220  1.1  christos #if defined(TUI)
   1221  1.1  christos   fputs_unfiltered (_("\
   1222  1.1  christos   --tui              Use a terminal user interface.\n\
   1223  1.1  christos "), stream);
   1224  1.1  christos #endif
   1225  1.1  christos   fputs_unfiltered (_("\
   1226  1.1  christos   --dbx              DBX compatibility mode.\n\
   1227  1.1  christos   --xdb              XDB compatibility mode.\n\
   1228  1.3  christos   -q, --quiet, --silent\n\
   1229  1.3  christos                      Do not print version number on startup.\n\n\
   1230  1.1  christos "), stream);
   1231  1.1  christos   fputs_unfiltered (_("\
   1232  1.1  christos Operating modes:\n\n\
   1233  1.1  christos   --batch            Exit after processing options.\n\
   1234  1.1  christos   --batch-silent     Like --batch, but suppress all gdb stdout output.\n\
   1235  1.1  christos   --return-child-result\n\
   1236  1.1  christos                      GDB exit code will be the child's exit code.\n\
   1237  1.1  christos   --configuration    Print details about GDB configuration and then exit.\n\
   1238  1.1  christos   --help             Print this message and then exit.\n\
   1239  1.1  christos   --version          Print version information and then exit.\n\n\
   1240  1.1  christos Remote debugging options:\n\n\
   1241  1.1  christos   -b BAUDRATE        Set serial port baud rate used for remote debugging.\n\
   1242  1.1  christos   -l TIMEOUT         Set timeout in seconds for remote debugging.\n\n\
   1243  1.1  christos Other options:\n\n\
   1244  1.1  christos   --cd=DIR           Change current directory to DIR.\n\
   1245  1.3  christos   --data-directory=DIR, -D\n\
   1246  1.3  christos                      Set GDB's data-directory to DIR.\n\
   1247  1.1  christos "), stream);
   1248  1.1  christos   fputs_unfiltered (_("\n\
   1249  1.1  christos At startup, GDB reads the following init files and executes their commands:\n\
   1250  1.1  christos "), stream);
   1251  1.1  christos   if (system_gdbinit)
   1252  1.1  christos     fprintf_unfiltered (stream, _("\
   1253  1.1  christos    * system-wide init file: %s\n\
   1254  1.1  christos "), system_gdbinit);
   1255  1.1  christos   if (home_gdbinit)
   1256  1.1  christos     fprintf_unfiltered (stream, _("\
   1257  1.1  christos    * user-specific init file: %s\n\
   1258  1.1  christos "), home_gdbinit);
   1259  1.1  christos   if (local_gdbinit)
   1260  1.1  christos     fprintf_unfiltered (stream, _("\
   1261  1.1  christos    * local init file (see also 'set auto-load local-gdbinit'): ./%s\n\
   1262  1.1  christos "), local_gdbinit);
   1263  1.1  christos   fputs_unfiltered (_("\n\
   1264  1.1  christos For more information, type \"help\" from within GDB, or consult the\n\
   1265  1.1  christos GDB manual (available as on-line info or a printed manual).\n\
   1266  1.1  christos "), stream);
   1267  1.1  christos   if (REPORT_BUGS_TO[0] && stream == gdb_stdout)
   1268  1.1  christos     fprintf_unfiltered (stream, _("\
   1269  1.1  christos Report bugs to \"%s\".\n\
   1270  1.1  christos "), REPORT_BUGS_TO);
   1271  1.1  christos }
   1272