Home | History | Annotate | Line # | Download | only in gdb
main.c revision 1.11
      1   1.1  christos /* Top level stuff for GDB, the GNU debugger.
      2   1.1  christos 
      3  1.11  christos    Copyright (C) 1986-2024 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.11  christos #include "annotate.h"
     21   1.1  christos #include "top.h"
     22  1.11  christos #include "ui.h"
     23   1.1  christos #include "target.h"
     24   1.1  christos #include "inferior.h"
     25   1.1  christos #include "symfile.h"
     26   1.1  christos #include "gdbcore.h"
     27   1.1  christos #include "getopt.h"
     28   1.1  christos 
     29   1.1  christos #include <sys/types.h>
     30   1.1  christos #include <sys/stat.h>
     31   1.1  christos #include <ctype.h>
     32   1.9  christos #include "gdbsupport/event-loop.h"
     33   1.1  christos #include "ui-out.h"
     34   1.1  christos 
     35   1.1  christos #include "interps.h"
     36   1.1  christos #include "main.h"
     37   1.1  christos #include "source.h"
     38   1.1  christos #include "cli/cli-cmds.h"
     39   1.1  christos #include "objfiles.h"
     40   1.1  christos #include "auto-load.h"
     41   1.1  christos #include "maint.h"
     42   1.1  christos 
     43   1.1  christos #include "filenames.h"
     44   1.9  christos #include "gdbsupport/filestuff.h"
     45   1.3  christos #include <signal.h>
     46   1.3  christos #include "event-top.h"
     47   1.3  christos #include "infrun.h"
     48   1.9  christos #include "gdbsupport/signals-state-save-restore.h"
     49   1.9  christos #include <algorithm>
     50   1.7  christos #include <vector>
     51   1.9  christos #include "gdbsupport/pathstuff.h"
     52   1.8  christos #include "cli/cli-style.h"
     53   1.9  christos #ifdef GDBTK
     54   1.9  christos #include "gdbtk/generic/gdbtk.h"
     55   1.9  christos #endif
     56   1.9  christos #include "gdbsupport/alt-stack.h"
     57   1.9  christos #include "observable.h"
     58  1.10  christos #include "serial.h"
     59  1.11  christos #include "cli-out.h"
     60   1.1  christos 
     61  1.10  christos /* The selected interpreter.  */
     62  1.10  christos std::string interpreter_p;
     63   1.1  christos 
     64   1.1  christos /* System root path, used to find libraries etc.  */
     65  1.10  christos std::string gdb_sysroot;
     66   1.1  christos 
     67   1.1  christos /* GDB datadir, used to store data files.  */
     68   1.9  christos std::string gdb_datadir;
     69   1.1  christos 
     70   1.1  christos /* Non-zero if GDB_DATADIR was provided on the command line.
     71   1.1  christos    This doesn't track whether data-directory is set later from the
     72   1.1  christos    command line, but we don't reread system.gdbinit when that happens.  */
     73   1.1  christos static int gdb_datadir_provided = 0;
     74   1.1  christos 
     75   1.1  christos /* If gdb was configured with --with-python=/path,
     76   1.1  christos    the possibly relocated path to python's lib directory.  */
     77   1.9  christos std::string python_libdir;
     78   1.1  christos 
     79   1.1  christos /* Target IO streams.  */
     80   1.1  christos struct ui_file *gdb_stdtargin;
     81   1.1  christos struct ui_file *gdb_stdtarg;
     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 static void print_gdb_help (struct ui_file *);
    100   1.1  christos 
    101   1.3  christos /* Set the data-directory parameter to NEW_DATADIR.
    102   1.3  christos    If NEW_DATADIR is not a directory then a warning is printed.
    103   1.3  christos    We don't signal an error for backward compatibility.  */
    104   1.3  christos 
    105   1.3  christos void
    106   1.3  christos set_gdb_data_directory (const char *new_datadir)
    107   1.3  christos {
    108   1.3  christos   struct stat st;
    109   1.3  christos 
    110   1.3  christos   if (stat (new_datadir, &st) < 0)
    111  1.11  christos     warning_filename_and_errno (new_datadir, errno);
    112   1.3  christos   else if (!S_ISDIR (st.st_mode))
    113   1.9  christos     warning (_("%ps is not a directory."),
    114   1.9  christos 	     styled_string (file_name_style.style (), new_datadir));
    115   1.3  christos 
    116   1.9  christos   gdb_datadir = gdb_realpath (new_datadir).get ();
    117   1.3  christos 
    118   1.3  christos   /* gdb_realpath won't return an absolute path if the path doesn't exist,
    119   1.3  christos      but we still want to record an absolute path here.  If the user entered
    120   1.3  christos      "../foo" and "../foo" doesn't exist then we'll record $(pwd)/../foo which
    121   1.3  christos      isn't canonical, but that's ok.  */
    122   1.9  christos   if (!IS_ABSOLUTE_PATH (gdb_datadir.c_str ()))
    123  1.10  christos     gdb_datadir = gdb_abspath (gdb_datadir.c_str ());
    124   1.3  christos }
    125   1.3  christos 
    126   1.1  christos /* Relocate a file or directory.  PROGNAME is the name by which gdb
    127   1.1  christos    was invoked (i.e., argv[0]).  INITIAL is the default value for the
    128   1.9  christos    file or directory.  RELOCATABLE is true if the value is relocatable,
    129   1.9  christos    false otherwise.  This may return an empty string under the same
    130   1.9  christos    conditions as make_relative_prefix returning NULL.  */
    131   1.1  christos 
    132   1.9  christos static std::string
    133   1.9  christos relocate_path (const char *progname, const char *initial, bool relocatable)
    134   1.1  christos {
    135   1.9  christos   if (relocatable)
    136   1.9  christos     {
    137   1.9  christos       gdb::unique_xmalloc_ptr<char> str (make_relative_prefix (progname,
    138   1.9  christos 							       BINDIR,
    139   1.9  christos 							       initial));
    140   1.9  christos       if (str != nullptr)
    141   1.9  christos 	return str.get ();
    142   1.9  christos       return std::string ();
    143   1.9  christos     }
    144   1.9  christos   return initial;
    145   1.1  christos }
    146   1.1  christos 
    147   1.1  christos /* Like relocate_path, but specifically checks for a directory.
    148   1.1  christos    INITIAL is relocated according to the rules of relocate_path.  If
    149   1.1  christos    the result is a directory, it is used; otherwise, INITIAL is used.
    150   1.9  christos    The chosen directory is then canonicalized using lrealpath.  */
    151   1.1  christos 
    152   1.9  christos std::string
    153   1.9  christos relocate_gdb_directory (const char *initial, bool relocatable)
    154   1.1  christos {
    155   1.9  christos   std::string dir = relocate_path (gdb_program_name, initial, relocatable);
    156   1.9  christos   if (!dir.empty ())
    157   1.1  christos     {
    158   1.1  christos       struct stat s;
    159   1.1  christos 
    160   1.9  christos       if (stat (dir.c_str (), &s) != 0 || !S_ISDIR (s.st_mode))
    161   1.1  christos 	{
    162   1.9  christos 	  dir.clear ();
    163   1.1  christos 	}
    164   1.1  christos     }
    165   1.9  christos   if (dir.empty ())
    166   1.9  christos     dir = initial;
    167   1.1  christos 
    168   1.1  christos   /* Canonicalize the directory.  */
    169   1.9  christos   if (!dir.empty ())
    170   1.1  christos     {
    171   1.9  christos       gdb::unique_xmalloc_ptr<char> canon_sysroot (lrealpath (dir.c_str ()));
    172   1.1  christos 
    173   1.1  christos       if (canon_sysroot)
    174   1.9  christos 	dir = canon_sysroot.get ();
    175   1.1  christos     }
    176   1.1  christos 
    177   1.1  christos   return dir;
    178   1.1  christos }
    179   1.1  christos 
    180   1.9  christos /* Given a gdbinit path in FILE, adjusts it according to the gdb_datadir
    181   1.9  christos    parameter if it is in the data dir, or passes it through relocate_path
    182   1.9  christos    otherwise.  */
    183   1.9  christos 
    184   1.9  christos static std::string
    185  1.10  christos relocate_file_path_maybe_in_datadir (const std::string &file,
    186  1.10  christos 				     bool relocatable)
    187   1.9  christos {
    188   1.9  christos   size_t datadir_len = strlen (GDB_DATADIR);
    189   1.9  christos 
    190   1.9  christos   std::string relocated_path;
    191   1.9  christos 
    192   1.9  christos   /* If SYSTEM_GDBINIT lives in data-directory, and data-directory
    193   1.9  christos      has been provided, search for SYSTEM_GDBINIT there.  */
    194   1.9  christos   if (gdb_datadir_provided
    195   1.9  christos       && datadir_len < file.length ()
    196   1.9  christos       && filename_ncmp (file.c_str (), GDB_DATADIR, datadir_len) == 0
    197   1.9  christos       && IS_DIR_SEPARATOR (file[datadir_len]))
    198   1.9  christos     {
    199   1.9  christos       /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
    200   1.9  christos 	 to gdb_datadir.  */
    201   1.9  christos 
    202   1.9  christos       size_t start = datadir_len;
    203   1.9  christos       for (; IS_DIR_SEPARATOR (file[start]); ++start)
    204   1.9  christos 	;
    205   1.9  christos       relocated_path = gdb_datadir + SLASH_STRING + file.substr (start);
    206   1.9  christos     }
    207   1.9  christos   else
    208   1.9  christos     {
    209   1.9  christos       relocated_path = relocate_path (gdb_program_name, file.c_str (),
    210   1.9  christos 				      relocatable);
    211   1.9  christos     }
    212   1.9  christos     return relocated_path;
    213   1.9  christos }
    214   1.9  christos 
    215  1.10  christos /* A class to wrap up the logic for finding the three different types of
    216  1.10  christos    initialisation files GDB uses, system wide, home directory, and current
    217  1.10  christos    working directory.  */
    218  1.10  christos 
    219  1.10  christos class gdb_initfile_finder
    220   1.1  christos {
    221  1.10  christos public:
    222  1.10  christos   /* Constructor.  Finds initialisation files named FILENAME in the home
    223  1.10  christos      directory or local (current working) directory.  System initialisation
    224  1.10  christos      files are found in both SYSTEM_FILENAME and SYSTEM_DIRNAME if these
    225  1.10  christos      are not nullptr (either or both can be).  The matching *_RELOCATABLE
    226  1.10  christos      flag is passed through to RELOCATE_FILE_PATH_MAYBE_IN_DATADIR.
    227  1.10  christos 
    228  1.10  christos      If FILENAME starts with a '.' then when looking in the home directory
    229  1.10  christos      this first '.' can be ignored in some cases.  */
    230  1.10  christos   explicit gdb_initfile_finder (const char *filename,
    231  1.10  christos 				const char *system_filename,
    232  1.10  christos 				bool system_filename_relocatable,
    233  1.10  christos 				const char *system_dirname,
    234  1.10  christos 				bool system_dirname_relocatable,
    235  1.10  christos 				bool lookup_local_file)
    236  1.10  christos   {
    237  1.10  christos     struct stat s;
    238   1.1  christos 
    239  1.10  christos     if (system_filename != nullptr && system_filename[0] != '\0')
    240  1.10  christos       {
    241  1.10  christos 	std::string relocated_filename
    242  1.10  christos 	  = relocate_file_path_maybe_in_datadir (system_filename,
    243  1.10  christos 						 system_filename_relocatable);
    244  1.10  christos 	if (!relocated_filename.empty ()
    245  1.10  christos 	    && stat (relocated_filename.c_str (), &s) == 0)
    246  1.10  christos 	  m_system_files.push_back (relocated_filename);
    247  1.10  christos       }
    248   1.1  christos 
    249  1.10  christos     if (system_dirname != nullptr && system_dirname[0] != '\0')
    250  1.10  christos       {
    251  1.10  christos 	std::string relocated_dirname
    252  1.10  christos 	  = relocate_file_path_maybe_in_datadir (system_dirname,
    253  1.10  christos 						 system_dirname_relocatable);
    254  1.10  christos 	if (!relocated_dirname.empty ())
    255  1.10  christos 	  {
    256  1.10  christos 	    gdb_dir_up dir (opendir (relocated_dirname.c_str ()));
    257   1.9  christos 	    if (dir != nullptr)
    258   1.9  christos 	      {
    259   1.9  christos 		std::vector<std::string> files;
    260  1.10  christos 		while (true)
    261   1.9  christos 		  {
    262   1.9  christos 		    struct dirent *ent = readdir (dir.get ());
    263   1.9  christos 		    if (ent == nullptr)
    264   1.9  christos 		      break;
    265   1.9  christos 		    std::string name (ent->d_name);
    266   1.9  christos 		    if (name == "." || name == "..")
    267   1.9  christos 		      continue;
    268  1.10  christos 		    /* ent->d_type is not available on all systems
    269  1.10  christos 		       (e.g. mingw, Solaris), so we have to call stat().  */
    270  1.10  christos 		    std::string tmp_filename
    271  1.10  christos 		      = relocated_dirname + SLASH_STRING + name;
    272  1.10  christos 		    if (stat (tmp_filename.c_str (), &s) != 0
    273   1.9  christos 			|| !S_ISREG (s.st_mode))
    274   1.9  christos 		      continue;
    275   1.9  christos 		    const struct extension_language_defn *extlang
    276  1.10  christos 		      = get_ext_lang_of_file (tmp_filename.c_str ());
    277   1.9  christos 		    /* We effectively don't support "set script-extension
    278  1.10  christos 		       off/soft", because we are loading system init files
    279  1.10  christos 		       here, so it does not really make sense to depend on
    280  1.10  christos 		       a setting.  */
    281   1.9  christos 		    if (extlang != nullptr && ext_lang_present_p (extlang))
    282  1.10  christos 		      files.push_back (std::move (tmp_filename));
    283   1.9  christos 		  }
    284   1.9  christos 		std::sort (files.begin (), files.end ());
    285  1.10  christos 		m_system_files.insert (m_system_files.end (),
    286  1.10  christos 				       files.begin (), files.end ());
    287   1.9  christos 	      }
    288   1.9  christos 	  }
    289  1.10  christos       }
    290   1.1  christos 
    291   1.1  christos       /* If the .gdbinit file in the current directory is the same as
    292   1.1  christos 	 the $HOME/.gdbinit file, it should not be sourced.  homebuf
    293   1.1  christos 	 and cwdbuf are used in that purpose.  Make sure that the stats
    294   1.1  christos 	 are zero in case one of them fails (this guarantees that they
    295   1.1  christos 	 won't match if either exists).  */
    296   1.1  christos 
    297  1.10  christos     struct stat homebuf, cwdbuf;
    298  1.10  christos     memset (&homebuf, 0, sizeof (struct stat));
    299  1.10  christos     memset (&cwdbuf, 0, sizeof (struct stat));
    300  1.10  christos 
    301  1.10  christos     m_home_file = find_gdb_home_config_file (filename, &homebuf);
    302   1.1  christos 
    303  1.10  christos     if (lookup_local_file && stat (filename, &cwdbuf) == 0)
    304  1.10  christos       {
    305  1.10  christos 	if (m_home_file.empty ()
    306  1.10  christos 	    || memcmp ((char *) &homebuf, (char *) &cwdbuf,
    307  1.10  christos 		       sizeof (struct stat)))
    308  1.10  christos 	  m_local_file = filename;
    309  1.10  christos       }
    310  1.10  christos   }
    311  1.10  christos 
    312  1.10  christos   DISABLE_COPY_AND_ASSIGN (gdb_initfile_finder);
    313  1.10  christos 
    314  1.10  christos   /* Return a list of system initialisation files.  The list could be
    315  1.10  christos      empty.  */
    316  1.10  christos   const std::vector<std::string> &system_files () const
    317  1.10  christos   { return m_system_files; }
    318  1.10  christos 
    319  1.10  christos   /* Return the path to the home initialisation file.  The string can be
    320  1.10  christos      empty if there is no such file.  */
    321  1.10  christos   const std::string &home_file () const
    322  1.10  christos   { return m_home_file; }
    323  1.10  christos 
    324  1.10  christos   /* Return the path to the local initialisation file.  The string can be
    325  1.10  christos      empty if there is no such file.  */
    326  1.10  christos   const std::string &local_file () const
    327  1.10  christos   { return m_local_file; }
    328  1.10  christos 
    329  1.10  christos private:
    330  1.10  christos 
    331  1.10  christos   /* Vector of all system init files in the order they should be processed.
    332  1.10  christos      Could be empty.  */
    333  1.10  christos   std::vector<std::string> m_system_files;
    334  1.10  christos 
    335  1.10  christos   /* Initialization file from the home directory.  Could be the empty
    336  1.10  christos      string if there is no such file found.  */
    337  1.10  christos   std::string m_home_file;
    338  1.10  christos 
    339  1.10  christos   /* Initialization file from the current working directory.  Could be the
    340  1.10  christos      empty string if there is no such file found.  */
    341  1.10  christos   std::string m_local_file;
    342  1.10  christos };
    343  1.10  christos 
    344  1.10  christos /* Compute the locations of init files that GDB should source and return
    345  1.10  christos    them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT.  The SYSTEM_GDBINIT
    346  1.10  christos    can be returned as an empty vector, and HOME_GDBINIT and LOCAL_GDBINIT
    347  1.10  christos    can be returned as empty strings if there is no init file of that
    348  1.10  christos    type.  */
    349  1.10  christos 
    350  1.10  christos static void
    351  1.10  christos get_init_files (std::vector<std::string> *system_gdbinit,
    352  1.10  christos 		std::string *home_gdbinit,
    353  1.10  christos 		std::string *local_gdbinit)
    354  1.10  christos {
    355  1.10  christos   /* Cache the file lookup object so we only actually search for the files
    356  1.10  christos      once.  */
    357  1.11  christos   static std::optional<gdb_initfile_finder> init_files;
    358  1.10  christos   if (!init_files.has_value ())
    359  1.10  christos     init_files.emplace (GDBINIT, SYSTEM_GDBINIT, SYSTEM_GDBINIT_RELOCATABLE,
    360  1.10  christos 			SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE,
    361  1.10  christos 			true);
    362  1.10  christos 
    363  1.10  christos   *system_gdbinit = init_files->system_files ();
    364  1.10  christos   *home_gdbinit = init_files->home_file ();
    365  1.10  christos   *local_gdbinit = init_files->local_file ();
    366  1.10  christos }
    367  1.10  christos 
    368  1.10  christos /* Compute the location of the early init file GDB should source and return
    369  1.10  christos    it in HOME_GDBEARLYINIT.  HOME_GDBEARLYINIT could be returned as an
    370  1.10  christos    empty string if there is no early init file found.  */
    371   1.1  christos 
    372  1.10  christos static void
    373  1.10  christos get_earlyinit_files (std::string *home_gdbearlyinit)
    374  1.10  christos {
    375  1.10  christos   /* Cache the file lookup object so we only actually search for the files
    376  1.10  christos      once.  */
    377  1.11  christos   static std::optional<gdb_initfile_finder> init_files;
    378  1.10  christos   if (!init_files.has_value ())
    379  1.10  christos     init_files.emplace (GDBEARLYINIT, nullptr, false, nullptr, false, false);
    380   1.1  christos 
    381  1.10  christos   *home_gdbearlyinit = init_files->home_file ();
    382   1.1  christos }
    383   1.1  christos 
    384   1.9  christos /* Start up the event loop.  This is the entry point to the event loop
    385   1.9  christos    from the command loop.  */
    386   1.3  christos 
    387   1.3  christos static void
    388   1.9  christos start_event_loop ()
    389   1.3  christos {
    390   1.9  christos   /* Loop until there is nothing to do.  This is the entry point to
    391   1.9  christos      the event loop engine.  gdb_do_one_event will process one event
    392   1.9  christos      for each invocation.  It blocks waiting for an event and then
    393   1.9  christos      processes it.  */
    394   1.9  christos   while (1)
    395   1.9  christos     {
    396   1.9  christos       int result = 0;
    397   1.3  christos 
    398   1.9  christos       try
    399   1.9  christos 	{
    400   1.9  christos 	  result = gdb_do_one_event ();
    401   1.9  christos 	}
    402  1.11  christos       catch (const gdb_exception_forced_quit &ex)
    403  1.11  christos 	{
    404  1.11  christos 	  throw;
    405  1.11  christos 	}
    406   1.9  christos       catch (const gdb_exception &ex)
    407   1.9  christos 	{
    408   1.9  christos 	  exception_print (gdb_stderr, ex);
    409   1.3  christos 
    410   1.9  christos 	  /* If any exception escaped to here, we better enable
    411   1.9  christos 	     stdin.  Otherwise, any command that calls async_disable_stdin,
    412   1.9  christos 	     and then throws, will leave stdin inoperable.  */
    413   1.9  christos 	  SWITCH_THRU_ALL_UIS ()
    414   1.9  christos 	    {
    415   1.9  christos 	      async_enable_stdin ();
    416   1.9  christos 	    }
    417   1.9  christos 	  /* If we long-jumped out of do_one_event, we probably didn't
    418   1.9  christos 	     get around to resetting the prompt, which leaves readline
    419   1.9  christos 	     in a messed-up state.  Reset it here.  */
    420   1.9  christos 	  current_ui->prompt_state = PROMPT_NEEDED;
    421  1.11  christos 	  top_level_interpreter ()->on_command_error ();
    422   1.9  christos 	  /* This call looks bizarre, but it is required.  If the user
    423   1.9  christos 	     entered a command that caused an error,
    424   1.9  christos 	     after_char_processing_hook won't be called from
    425   1.9  christos 	     rl_callback_read_char_wrapper.  Using a cleanup there
    426   1.9  christos 	     won't work, since we want this function to be called
    427   1.9  christos 	     after a new prompt is printed.  */
    428   1.9  christos 	  if (after_char_processing_hook)
    429   1.9  christos 	    (*after_char_processing_hook) ();
    430   1.9  christos 	  /* Maybe better to set a flag to be checked somewhere as to
    431   1.9  christos 	     whether display the prompt or not.  */
    432   1.9  christos 	}
    433   1.9  christos 
    434   1.9  christos       if (result < 0)
    435   1.9  christos 	break;
    436   1.9  christos     }
    437   1.9  christos 
    438   1.9  christos   /* We are done with the event loop.  There are no more event sources
    439   1.9  christos      to listen to.  So we exit GDB.  */
    440   1.9  christos   return;
    441   1.3  christos }
    442   1.3  christos 
    443   1.8  christos /* Call command_loop.  */
    444   1.1  christos 
    445   1.8  christos /* Prevent inlining this function for the benefit of GDB's selftests
    446   1.8  christos    in the testsuite.  Those tests want to run GDB under GDB and stop
    447   1.8  christos    here.  */
    448   1.8  christos static void captured_command_loop () __attribute__((noinline));
    449   1.8  christos 
    450   1.8  christos static void
    451   1.8  christos captured_command_loop ()
    452   1.1  christos {
    453   1.6  christos   struct ui *ui = current_ui;
    454   1.6  christos 
    455   1.1  christos   /* Top-level execution commands can be run in the background from
    456   1.1  christos      here on.  */
    457   1.6  christos   current_ui->async = 1;
    458   1.6  christos 
    459   1.6  christos   /* Give the interpreter a chance to print a prompt, if necessary  */
    460   1.6  christos   if (ui->prompt_state != PROMPT_BLOCKED)
    461  1.11  christos     top_level_interpreter ()->pre_command_loop ();
    462   1.6  christos 
    463   1.6  christos   /* Now it's time to start the event loop.  */
    464   1.6  christos   start_event_loop ();
    465   1.1  christos 
    466   1.1  christos   /* If the command_loop returned, normally (rather than threw an
    467   1.8  christos      error) we try to quit.  If the quit is aborted, our caller
    468   1.8  christos      catches the signal and restarts the command loop.  */
    469   1.6  christos   quit_command (NULL, ui->instream == ui->stdin_stream);
    470   1.1  christos }
    471   1.1  christos 
    472   1.8  christos /* Handle command errors thrown from within catch_command_errors.  */
    473   1.3  christos 
    474   1.3  christos static int
    475   1.9  christos handle_command_errors (const struct gdb_exception &e)
    476   1.3  christos {
    477   1.3  christos   if (e.reason < 0)
    478   1.3  christos     {
    479   1.3  christos       exception_print (gdb_stderr, e);
    480   1.3  christos 
    481   1.3  christos       /* If any exception escaped to here, we better enable stdin.
    482   1.3  christos 	 Otherwise, any command that calls async_disable_stdin, and
    483   1.3  christos 	 then throws, will leave stdin inoperable.  */
    484   1.3  christos       async_enable_stdin ();
    485   1.3  christos       return 0;
    486   1.3  christos     }
    487   1.3  christos   return 1;
    488   1.3  christos }
    489   1.3  christos 
    490   1.8  christos /* Type of the command callback passed to the const
    491   1.8  christos    catch_command_errors.  */
    492   1.3  christos 
    493   1.8  christos typedef void (catch_command_errors_const_ftype) (const char *, int);
    494   1.3  christos 
    495   1.3  christos /* Wrap calls to commands run before the event loop is started.  */
    496   1.3  christos 
    497   1.3  christos static int
    498   1.8  christos catch_command_errors (catch_command_errors_const_ftype command,
    499  1.10  christos 		      const char *arg, int from_tty,
    500  1.10  christos 		      bool do_bp_actions = false)
    501   1.3  christos {
    502   1.9  christos   try
    503   1.3  christos     {
    504   1.6  christos       int was_sync = current_ui->prompt_state == PROMPT_BLOCKED;
    505   1.3  christos 
    506   1.3  christos       command (arg, from_tty);
    507   1.3  christos 
    508   1.3  christos       maybe_wait_sync_command_done (was_sync);
    509  1.10  christos 
    510  1.10  christos       /* Do any commands attached to breakpoint we stopped at.  */
    511  1.10  christos       if (do_bp_actions)
    512  1.10  christos 	bpstat_do_actions ();
    513   1.3  christos     }
    514  1.11  christos   catch (const gdb_exception_forced_quit &e)
    515  1.11  christos     {
    516  1.11  christos       quit_force (NULL, 0);
    517  1.11  christos     }
    518   1.9  christos   catch (const gdb_exception &e)
    519   1.5  christos     {
    520   1.5  christos       return handle_command_errors (e);
    521   1.5  christos     }
    522   1.5  christos 
    523   1.5  christos   return 1;
    524   1.3  christos }
    525   1.3  christos 
    526   1.7  christos /* Adapter for symbol_file_add_main that translates 'from_tty' to a
    527   1.7  christos    symfile_add_flags.  */
    528   1.7  christos 
    529   1.7  christos static void
    530   1.7  christos symbol_file_add_main_adapter (const char *arg, int from_tty)
    531   1.7  christos {
    532   1.7  christos   symfile_add_flags add_flags = 0;
    533   1.7  christos 
    534   1.7  christos   if (from_tty)
    535   1.7  christos     add_flags |= SYMFILE_VERBOSE;
    536   1.7  christos 
    537   1.7  christos   symbol_file_add_main (arg, add_flags);
    538   1.7  christos }
    539   1.7  christos 
    540   1.8  christos /* Perform validation of the '--readnow' and '--readnever' flags.  */
    541   1.8  christos 
    542   1.8  christos static void
    543   1.8  christos validate_readnow_readnever ()
    544   1.8  christos {
    545   1.8  christos   if (readnever_symbol_files && readnow_symbol_files)
    546   1.8  christos     {
    547   1.8  christos       error (_("%s: '--readnow' and '--readnever' cannot be "
    548   1.8  christos 	       "specified simultaneously"),
    549   1.8  christos 	     gdb_program_name);
    550   1.8  christos     }
    551   1.8  christos }
    552   1.8  christos 
    553   1.5  christos /* Type of this option.  */
    554   1.5  christos enum cmdarg_kind
    555   1.5  christos {
    556   1.5  christos   /* Option type -x.  */
    557   1.5  christos   CMDARG_FILE,
    558   1.5  christos 
    559   1.5  christos   /* Option type -ex.  */
    560   1.5  christos   CMDARG_COMMAND,
    561   1.5  christos 
    562   1.5  christos   /* Option type -ix.  */
    563   1.5  christos   CMDARG_INIT_FILE,
    564  1.10  christos 
    565   1.5  christos   /* Option type -iex.  */
    566  1.10  christos   CMDARG_INIT_COMMAND,
    567  1.10  christos 
    568  1.10  christos   /* Option type -eix.  */
    569  1.10  christos   CMDARG_EARLYINIT_FILE,
    570  1.10  christos 
    571  1.10  christos   /* Option type -eiex.  */
    572  1.10  christos   CMDARG_EARLYINIT_COMMAND
    573   1.5  christos };
    574   1.5  christos 
    575   1.1  christos /* Arguments of --command option and its counterpart.  */
    576   1.7  christos struct cmdarg
    577   1.7  christos {
    578   1.7  christos   cmdarg (cmdarg_kind type_, char *string_)
    579   1.7  christos     : type (type_), string (string_)
    580   1.7  christos   {}
    581   1.7  christos 
    582   1.1  christos   /* Type of this option.  */
    583   1.5  christos   enum cmdarg_kind type;
    584   1.1  christos 
    585   1.1  christos   /* Value of this option - filename or the GDB command itself.  String memory
    586   1.1  christos      is not owned by this structure despite it is 'const'.  */
    587   1.1  christos   char *string;
    588   1.7  christos };
    589   1.1  christos 
    590  1.10  christos /* From CMDARG_VEC execute command files (matching FILE_TYPE) or commands
    591  1.10  christos    (matching CMD_TYPE).  Update the value in *RET if and scripts or
    592  1.10  christos    commands are executed.  */
    593  1.10  christos 
    594  1.10  christos static void
    595  1.10  christos execute_cmdargs (const std::vector<struct cmdarg> *cmdarg_vec,
    596  1.10  christos 		 cmdarg_kind file_type, cmdarg_kind cmd_type,
    597  1.10  christos 		 int *ret)
    598  1.10  christos {
    599  1.10  christos   for (const auto &cmdarg_p : *cmdarg_vec)
    600  1.10  christos     {
    601  1.10  christos       if (cmdarg_p.type == file_type)
    602  1.10  christos 	*ret = catch_command_errors (source_script, cmdarg_p.string,
    603  1.10  christos 				     !batch_flag);
    604  1.10  christos       else if (cmdarg_p.type == cmd_type)
    605  1.10  christos 	*ret = catch_command_errors (execute_command, cmdarg_p.string,
    606  1.10  christos 				     !batch_flag, true);
    607  1.10  christos     }
    608  1.10  christos }
    609  1.10  christos 
    610   1.7  christos static void
    611   1.7  christos captured_main_1 (struct captured_main_args *context)
    612   1.1  christos {
    613   1.1  christos   int argc = context->argc;
    614   1.1  christos   char **argv = context->argv;
    615   1.7  christos 
    616   1.1  christos   static int quiet = 0;
    617   1.1  christos   static int set_args = 0;
    618   1.1  christos   static int inhibit_home_gdbinit = 0;
    619   1.1  christos 
    620   1.1  christos   /* Pointers to various arguments from command line.  */
    621   1.1  christos   char *symarg = NULL;
    622   1.1  christos   char *execarg = NULL;
    623   1.1  christos   char *pidarg = NULL;
    624   1.1  christos   char *corearg = NULL;
    625   1.1  christos   char *pid_or_core_arg = NULL;
    626   1.1  christos   char *cdarg = NULL;
    627   1.1  christos   char *ttyarg = NULL;
    628   1.1  christos 
    629   1.1  christos   /* These are static so that we can take their address in an
    630   1.1  christos      initializer.  */
    631   1.1  christos   static int print_help;
    632   1.1  christos   static int print_version;
    633   1.1  christos   static int print_configuration;
    634   1.1  christos 
    635   1.1  christos   /* Pointers to all arguments of --command option.  */
    636   1.7  christos   std::vector<struct cmdarg> cmdarg_vec;
    637   1.1  christos 
    638   1.7  christos   /* All arguments of --directory option.  */
    639   1.7  christos   std::vector<char *> dirarg;
    640   1.1  christos 
    641   1.1  christos   int i;
    642   1.1  christos   int save_auto_load;
    643   1.8  christos   int ret = 1;
    644   1.1  christos 
    645  1.11  christos   const char *no_color = getenv ("NO_COLOR");
    646  1.11  christos   if (no_color != nullptr && *no_color != '\0')
    647  1.11  christos     cli_styling = false;
    648  1.11  christos 
    649   1.8  christos #ifdef HAVE_USEFUL_SBRK
    650   1.7  christos   /* Set this before constructing scoped_command_stats.  */
    651   1.1  christos   lim_at_start = (char *) sbrk (0);
    652   1.1  christos #endif
    653   1.1  christos 
    654   1.7  christos   scoped_command_stats stat_reporter (false);
    655   1.1  christos 
    656   1.1  christos #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
    657   1.1  christos   setlocale (LC_MESSAGES, "");
    658   1.1  christos #endif
    659   1.1  christos #if defined (HAVE_SETLOCALE)
    660   1.1  christos   setlocale (LC_CTYPE, "");
    661   1.1  christos #endif
    662   1.7  christos #ifdef ENABLE_NLS
    663   1.1  christos   bindtextdomain (PACKAGE, LOCALEDIR);
    664   1.1  christos   textdomain (PACKAGE);
    665   1.7  christos #endif
    666   1.1  christos 
    667   1.1  christos   notice_open_fds ();
    668   1.1  christos 
    669   1.1  christos #ifdef __MINGW32__
    670   1.1  christos   /* Ensure stderr is unbuffered.  A Cygwin pty or pipe is implemented
    671   1.1  christos      as a Windows pipe, and Windows buffers on pipes.  */
    672   1.1  christos   setvbuf (stderr, NULL, _IONBF, BUFSIZ);
    673   1.1  christos #endif
    674   1.1  christos 
    675   1.8  christos   /* Note: `error' cannot be called before this point, because the
    676   1.8  christos      caller will crash when trying to print the exception.  */
    677   1.8  christos   main_ui = new ui (stdin, stdout, stderr);
    678   1.6  christos   current_ui = main_ui;
    679   1.1  christos 
    680  1.10  christos   gdb_stdtarg = gdb_stderr;
    681  1.10  christos   gdb_stdtargin = gdb_stdin;
    682   1.1  christos 
    683  1.11  christos   /* Put a CLI based uiout in place early.  If the early initialization
    684  1.11  christos      files trigger any I/O then it isn't hard to reach parts of GDB that
    685  1.11  christos      assume current_uiout is not nullptr.  Maybe we should just install the
    686  1.11  christos      CLI interpreter initially, then switch to the application requested
    687  1.11  christos      interpreter later?  But that would (potentially) result in an
    688  1.11  christos      interpreter being instantiated "just in case".  For now this feels
    689  1.11  christos      like the least effort way to protect GDB from crashing.  */
    690  1.11  christos   auto temp_uiout = std::make_unique<cli_ui_out> (gdb_stdout);
    691  1.11  christos   current_uiout = temp_uiout.get ();
    692  1.11  christos 
    693  1.11  christos   gdb_bfd_init ();
    694   1.8  christos 
    695   1.1  christos #ifdef __MINGW32__
    696   1.1  christos   /* On Windows, argv[0] is not necessarily set to absolute form when
    697   1.1  christos      GDB is found along PATH, without which relocation doesn't work.  */
    698   1.1  christos   gdb_program_name = windows_get_absolute_argv0 (argv[0]);
    699   1.1  christos #else
    700   1.1  christos   gdb_program_name = xstrdup (argv[0]);
    701   1.1  christos #endif
    702   1.1  christos 
    703   1.3  christos   /* Prefix warning messages with the command name.  */
    704   1.7  christos   gdb::unique_xmalloc_ptr<char> tmp_warn_preprint
    705  1.10  christos     = xstrprintf ("%s: warning: ", gdb_program_name);
    706   1.7  christos   warning_pre_print = tmp_warn_preprint.get ();
    707   1.3  christos 
    708   1.8  christos   current_directory = getcwd (NULL, 0);
    709   1.8  christos   if (current_directory == NULL)
    710   1.3  christos     perror_warning_with_name (_("error finding working directory"));
    711   1.3  christos 
    712   1.1  christos   /* Set the sysroot path.  */
    713  1.10  christos   gdb_sysroot = relocate_gdb_directory (TARGET_SYSTEM_ROOT,
    714  1.10  christos 					TARGET_SYSTEM_ROOT_RELOCATABLE);
    715   1.1  christos 
    716  1.10  christos   if (gdb_sysroot.empty ())
    717  1.10  christos     gdb_sysroot = TARGET_SYSROOT_PREFIX;
    718   1.5  christos 
    719   1.9  christos   debug_file_directory
    720  1.10  christos     = relocate_gdb_directory (DEBUGDIR, DEBUGDIR_RELOCATABLE);
    721   1.1  christos 
    722  1.11  christos #ifdef ADDITIONAL_DEBUG_DIRS
    723  1.11  christos   debug_file_directory = (debug_file_directory + DIRNAME_SEPARATOR
    724  1.11  christos 			  + ADDITIONAL_DEBUG_DIRS);
    725  1.11  christos #endif
    726  1.11  christos 
    727   1.1  christos   gdb_datadir = relocate_gdb_directory (GDB_DATADIR,
    728   1.1  christos 					GDB_DATADIR_RELOCATABLE);
    729   1.1  christos 
    730   1.9  christos #ifdef WITH_PYTHON_LIBDIR
    731   1.9  christos   python_libdir = relocate_gdb_directory (WITH_PYTHON_LIBDIR,
    732   1.9  christos 					  PYTHON_LIBDIR_RELOCATABLE);
    733   1.1  christos #endif
    734   1.1  christos 
    735   1.1  christos #ifdef RELOC_SRCDIR
    736   1.1  christos   add_substitute_path_rule (RELOC_SRCDIR,
    737   1.1  christos 			    make_relative_prefix (gdb_program_name, BINDIR,
    738   1.1  christos 						  RELOC_SRCDIR));
    739   1.1  christos #endif
    740   1.1  christos 
    741   1.1  christos   /* There will always be an interpreter.  Either the one passed into
    742   1.1  christos      this captured main, or one specified by the user at start up, or
    743   1.1  christos      the console.  Initialize the interpreter to the one requested by
    744   1.1  christos      the application.  */
    745  1.10  christos   interpreter_p = context->interpreter_p;
    746   1.1  christos 
    747   1.1  christos   /* Parse arguments and options.  */
    748   1.1  christos   {
    749   1.1  christos     int c;
    750   1.1  christos     /* When var field is 0, use flag field to record the equivalent
    751   1.1  christos        short option (or arbitrary numbers starting at 10 for those
    752   1.1  christos        with no equivalent).  */
    753   1.1  christos     enum {
    754   1.1  christos       OPT_SE = 10,
    755   1.1  christos       OPT_CD,
    756   1.1  christos       OPT_ANNOTATE,
    757   1.1  christos       OPT_STATISTICS,
    758   1.1  christos       OPT_TUI,
    759   1.1  christos       OPT_NOWINDOWS,
    760   1.1  christos       OPT_WINDOWS,
    761   1.1  christos       OPT_IX,
    762   1.8  christos       OPT_IEX,
    763  1.10  christos       OPT_EIX,
    764  1.10  christos       OPT_EIEX,
    765   1.8  christos       OPT_READNOW,
    766   1.8  christos       OPT_READNEVER
    767   1.1  christos     };
    768   1.9  christos     /* This struct requires int* in the struct, but write_files is a bool.
    769   1.9  christos        So use this temporary int that we write back after argument parsing.  */
    770   1.9  christos     int write_files_1 = 0;
    771   1.1  christos     static struct option long_options[] =
    772   1.1  christos     {
    773   1.1  christos       {"tui", no_argument, 0, OPT_TUI},
    774   1.8  christos       {"readnow", no_argument, NULL, OPT_READNOW},
    775   1.8  christos       {"readnever", no_argument, NULL, OPT_READNEVER},
    776   1.8  christos       {"r", no_argument, NULL, OPT_READNOW},
    777   1.1  christos       {"quiet", no_argument, &quiet, 1},
    778   1.1  christos       {"q", no_argument, &quiet, 1},
    779   1.1  christos       {"silent", no_argument, &quiet, 1},
    780   1.1  christos       {"nh", no_argument, &inhibit_home_gdbinit, 1},
    781   1.1  christos       {"nx", no_argument, &inhibit_gdbinit, 1},
    782   1.1  christos       {"n", no_argument, &inhibit_gdbinit, 1},
    783   1.1  christos       {"batch-silent", no_argument, 0, 'B'},
    784   1.1  christos       {"batch", no_argument, &batch_flag, 1},
    785   1.1  christos 
    786   1.1  christos     /* This is a synonym for "--annotate=1".  --annotate is now
    787   1.1  christos        preferred, but keep this here for a long time because people
    788   1.1  christos        will be running emacses which use --fullname.  */
    789   1.1  christos       {"fullname", no_argument, 0, 'f'},
    790   1.1  christos       {"f", no_argument, 0, 'f'},
    791   1.1  christos 
    792   1.1  christos       {"annotate", required_argument, 0, OPT_ANNOTATE},
    793   1.1  christos       {"help", no_argument, &print_help, 1},
    794   1.1  christos       {"se", required_argument, 0, OPT_SE},
    795   1.1  christos       {"symbols", required_argument, 0, 's'},
    796   1.1  christos       {"s", required_argument, 0, 's'},
    797   1.1  christos       {"exec", required_argument, 0, 'e'},
    798   1.1  christos       {"e", required_argument, 0, 'e'},
    799   1.1  christos       {"core", required_argument, 0, 'c'},
    800   1.1  christos       {"c", required_argument, 0, 'c'},
    801   1.1  christos       {"pid", required_argument, 0, 'p'},
    802   1.1  christos       {"p", required_argument, 0, 'p'},
    803   1.1  christos       {"command", required_argument, 0, 'x'},
    804   1.1  christos       {"eval-command", required_argument, 0, 'X'},
    805   1.1  christos       {"version", no_argument, &print_version, 1},
    806   1.1  christos       {"configuration", no_argument, &print_configuration, 1},
    807   1.1  christos       {"x", required_argument, 0, 'x'},
    808   1.1  christos       {"ex", required_argument, 0, 'X'},
    809   1.1  christos       {"init-command", required_argument, 0, OPT_IX},
    810   1.1  christos       {"init-eval-command", required_argument, 0, OPT_IEX},
    811   1.1  christos       {"ix", required_argument, 0, OPT_IX},
    812   1.1  christos       {"iex", required_argument, 0, OPT_IEX},
    813  1.10  christos       {"early-init-command", required_argument, 0, OPT_EIX},
    814  1.10  christos       {"early-init-eval-command", required_argument, 0, OPT_EIEX},
    815  1.10  christos       {"eix", required_argument, 0, OPT_EIX},
    816  1.10  christos       {"eiex", required_argument, 0, OPT_EIEX},
    817   1.1  christos #ifdef GDBTK
    818   1.1  christos       {"tclcommand", required_argument, 0, 'z'},
    819   1.1  christos       {"enable-external-editor", no_argument, 0, 'y'},
    820   1.1  christos       {"editor-command", required_argument, 0, 'w'},
    821   1.1  christos #endif
    822   1.1  christos       {"ui", required_argument, 0, 'i'},
    823   1.1  christos       {"interpreter", required_argument, 0, 'i'},
    824   1.1  christos       {"i", required_argument, 0, 'i'},
    825   1.1  christos       {"directory", required_argument, 0, 'd'},
    826   1.1  christos       {"d", required_argument, 0, 'd'},
    827   1.1  christos       {"data-directory", required_argument, 0, 'D'},
    828   1.3  christos       {"D", required_argument, 0, 'D'},
    829   1.1  christos       {"cd", required_argument, 0, OPT_CD},
    830   1.1  christos       {"tty", required_argument, 0, 't'},
    831   1.1  christos       {"baud", required_argument, 0, 'b'},
    832   1.1  christos       {"b", required_argument, 0, 'b'},
    833   1.1  christos       {"nw", no_argument, NULL, OPT_NOWINDOWS},
    834   1.1  christos       {"nowindows", no_argument, NULL, OPT_NOWINDOWS},
    835   1.1  christos       {"w", no_argument, NULL, OPT_WINDOWS},
    836   1.1  christos       {"windows", no_argument, NULL, OPT_WINDOWS},
    837   1.1  christos       {"statistics", no_argument, 0, OPT_STATISTICS},
    838   1.9  christos       {"write", no_argument, &write_files_1, 1},
    839   1.1  christos       {"args", no_argument, &set_args, 1},
    840   1.1  christos       {"l", required_argument, 0, 'l'},
    841   1.1  christos       {"return-child-result", no_argument, &return_child_result, 1},
    842   1.1  christos       {0, no_argument, 0, 0}
    843   1.1  christos     };
    844   1.1  christos 
    845   1.1  christos     while (1)
    846   1.1  christos       {
    847   1.1  christos 	int option_index;
    848   1.1  christos 
    849   1.1  christos 	c = getopt_long_only (argc, argv, "",
    850   1.1  christos 			      long_options, &option_index);
    851   1.1  christos 	if (c == EOF || set_args)
    852   1.1  christos 	  break;
    853   1.1  christos 
    854   1.1  christos 	/* Long option that takes an argument.  */
    855   1.1  christos 	if (c == 0 && long_options[option_index].flag == 0)
    856   1.1  christos 	  c = long_options[option_index].val;
    857   1.1  christos 
    858   1.1  christos 	switch (c)
    859   1.1  christos 	  {
    860   1.1  christos 	  case 0:
    861   1.1  christos 	    /* Long option that just sets a flag.  */
    862   1.1  christos 	    break;
    863   1.1  christos 	  case OPT_SE:
    864   1.1  christos 	    symarg = optarg;
    865   1.1  christos 	    execarg = optarg;
    866   1.1  christos 	    break;
    867   1.1  christos 	  case OPT_CD:
    868   1.1  christos 	    cdarg = optarg;
    869   1.1  christos 	    break;
    870   1.1  christos 	  case OPT_ANNOTATE:
    871   1.1  christos 	    /* FIXME: what if the syntax is wrong (e.g. not digits)?  */
    872   1.1  christos 	    annotation_level = atoi (optarg);
    873   1.1  christos 	    break;
    874   1.1  christos 	  case OPT_STATISTICS:
    875   1.1  christos 	    /* Enable the display of both time and space usage.  */
    876   1.1  christos 	    set_per_command_time (1);
    877   1.1  christos 	    set_per_command_space (1);
    878   1.1  christos 	    break;
    879   1.1  christos 	  case OPT_TUI:
    880   1.1  christos 	    /* --tui is equivalent to -i=tui.  */
    881   1.1  christos #ifdef TUI
    882  1.10  christos 	    interpreter_p = INTERP_TUI;
    883   1.1  christos #else
    884   1.3  christos 	    error (_("%s: TUI mode is not supported"), gdb_program_name);
    885   1.1  christos #endif
    886   1.1  christos 	    break;
    887   1.1  christos 	  case OPT_WINDOWS:
    888   1.1  christos 	    /* FIXME: cagney/2003-03-01: Not sure if this option is
    889  1.10  christos 	       actually useful, and if it is, what it should do.  */
    890   1.1  christos #ifdef GDBTK
    891   1.1  christos 	    /* --windows is equivalent to -i=insight.  */
    892  1.10  christos 	    interpreter_p = INTERP_INSIGHT;
    893   1.1  christos #endif
    894   1.1  christos 	    break;
    895   1.1  christos 	  case OPT_NOWINDOWS:
    896   1.1  christos 	    /* -nw is equivalent to -i=console.  */
    897  1.10  christos 	    interpreter_p = INTERP_CONSOLE;
    898   1.1  christos 	    break;
    899   1.1  christos 	  case 'f':
    900   1.1  christos 	    annotation_level = 1;
    901   1.1  christos 	    break;
    902   1.1  christos 	  case 's':
    903   1.1  christos 	    symarg = optarg;
    904   1.1  christos 	    break;
    905   1.1  christos 	  case 'e':
    906   1.1  christos 	    execarg = optarg;
    907   1.1  christos 	    break;
    908   1.1  christos 	  case 'c':
    909   1.1  christos 	    corearg = optarg;
    910   1.1  christos 	    break;
    911   1.1  christos 	  case 'p':
    912   1.1  christos 	    pidarg = optarg;
    913   1.1  christos 	    break;
    914   1.1  christos 	  case 'x':
    915   1.7  christos 	    cmdarg_vec.emplace_back (CMDARG_FILE, optarg);
    916   1.1  christos 	    break;
    917   1.1  christos 	  case 'X':
    918   1.7  christos 	    cmdarg_vec.emplace_back (CMDARG_COMMAND, optarg);
    919   1.1  christos 	    break;
    920   1.1  christos 	  case OPT_IX:
    921   1.7  christos 	    cmdarg_vec.emplace_back (CMDARG_INIT_FILE, optarg);
    922   1.1  christos 	    break;
    923   1.1  christos 	  case OPT_IEX:
    924   1.7  christos 	    cmdarg_vec.emplace_back (CMDARG_INIT_COMMAND, optarg);
    925   1.1  christos 	    break;
    926  1.10  christos 	  case OPT_EIX:
    927  1.10  christos 	    cmdarg_vec.emplace_back (CMDARG_EARLYINIT_FILE, optarg);
    928  1.10  christos 	    break;
    929  1.10  christos 	  case OPT_EIEX:
    930  1.10  christos 	    cmdarg_vec.emplace_back (CMDARG_EARLYINIT_COMMAND, optarg);
    931  1.10  christos 	    break;
    932   1.1  christos 	  case 'B':
    933   1.1  christos 	    batch_flag = batch_silent = 1;
    934   1.7  christos 	    gdb_stdout = new null_file ();
    935   1.1  christos 	    break;
    936   1.1  christos 	  case 'D':
    937   1.3  christos 	    if (optarg[0] == '\0')
    938   1.3  christos 	      error (_("%s: empty path for `--data-directory'"),
    939   1.3  christos 		     gdb_program_name);
    940   1.3  christos 	    set_gdb_data_directory (optarg);
    941   1.1  christos 	    gdb_datadir_provided = 1;
    942   1.1  christos 	    break;
    943   1.1  christos #ifdef GDBTK
    944   1.1  christos 	  case 'z':
    945   1.1  christos 	    {
    946   1.1  christos 	      if (!gdbtk_test (optarg))
    947   1.3  christos 		error (_("%s: unable to load tclcommand file \"%s\""),
    948   1.3  christos 		       gdb_program_name, optarg);
    949   1.1  christos 	      break;
    950   1.1  christos 	    }
    951   1.1  christos 	  case 'y':
    952   1.1  christos 	    /* Backwards compatibility only.  */
    953   1.1  christos 	    break;
    954   1.1  christos 	  case 'w':
    955   1.1  christos 	    {
    956   1.1  christos 	      /* Set the external editor commands when gdb is farming out files
    957   1.1  christos 		 to be edited by another program.  */
    958   1.1  christos 	      external_editor_command = xstrdup (optarg);
    959   1.1  christos 	      break;
    960   1.1  christos 	    }
    961   1.1  christos #endif /* GDBTK */
    962   1.1  christos 	  case 'i':
    963  1.10  christos 	    interpreter_p = optarg;
    964   1.1  christos 	    break;
    965   1.1  christos 	  case 'd':
    966   1.7  christos 	    dirarg.push_back (optarg);
    967   1.1  christos 	    break;
    968   1.1  christos 	  case 't':
    969   1.1  christos 	    ttyarg = optarg;
    970   1.1  christos 	    break;
    971   1.1  christos 	  case 'q':
    972   1.1  christos 	    quiet = 1;
    973   1.1  christos 	    break;
    974   1.1  christos 	  case 'b':
    975   1.1  christos 	    {
    976   1.8  christos 	      int rate;
    977   1.1  christos 	      char *p;
    978   1.1  christos 
    979   1.8  christos 	      rate = strtol (optarg, &p, 0);
    980   1.8  christos 	      if (rate == 0 && p == optarg)
    981   1.3  christos 		warning (_("could not set baud rate to `%s'."),
    982   1.3  christos 			 optarg);
    983   1.1  christos 	      else
    984   1.8  christos 		baud_rate = rate;
    985   1.1  christos 	    }
    986  1.10  christos 	    break;
    987   1.1  christos 	  case 'l':
    988   1.1  christos 	    {
    989   1.8  christos 	      int timeout;
    990   1.1  christos 	      char *p;
    991   1.1  christos 
    992   1.8  christos 	      timeout = strtol (optarg, &p, 0);
    993   1.8  christos 	      if (timeout == 0 && p == optarg)
    994   1.3  christos 		warning (_("could not set timeout limit to `%s'."),
    995   1.3  christos 			 optarg);
    996   1.1  christos 	      else
    997   1.8  christos 		remote_timeout = timeout;
    998   1.8  christos 	    }
    999   1.8  christos 	    break;
   1000   1.8  christos 
   1001   1.8  christos 	  case OPT_READNOW:
   1002   1.8  christos 	    {
   1003   1.8  christos 	      readnow_symbol_files = 1;
   1004   1.8  christos 	      validate_readnow_readnever ();
   1005   1.8  christos 	    }
   1006   1.8  christos 	    break;
   1007   1.8  christos 
   1008   1.8  christos 	  case OPT_READNEVER:
   1009   1.8  christos 	    {
   1010   1.8  christos 	      readnever_symbol_files = 1;
   1011   1.8  christos 	      validate_readnow_readnever ();
   1012   1.1  christos 	    }
   1013   1.1  christos 	    break;
   1014   1.1  christos 
   1015   1.1  christos 	  case '?':
   1016   1.3  christos 	    error (_("Use `%s --help' for a complete list of options."),
   1017   1.3  christos 		   gdb_program_name);
   1018   1.1  christos 	  }
   1019   1.1  christos       }
   1020   1.9  christos     write_files = (write_files_1 != 0);
   1021   1.1  christos 
   1022   1.1  christos     if (batch_flag)
   1023   1.8  christos       {
   1024   1.8  christos 	quiet = 1;
   1025   1.8  christos 
   1026   1.8  christos 	/* Disable all output styling when running in batch mode.  */
   1027   1.8  christos 	cli_styling = 0;
   1028   1.8  christos       }
   1029   1.1  christos   }
   1030   1.1  christos 
   1031   1.8  christos   save_original_signals_state (quiet);
   1032   1.8  christos 
   1033   1.3  christos   /* Try to set up an alternate signal stack for SIGSEGV handlers.  */
   1034   1.9  christos   gdb::alternate_signal_stack signal_stack;
   1035   1.3  christos 
   1036   1.5  christos   /* Initialize all files.  */
   1037  1.10  christos   gdb_init ();
   1038  1.10  christos 
   1039  1.10  christos   /* Process early init files and early init options from the command line.  */
   1040  1.10  christos   if (!inhibit_gdbinit)
   1041  1.10  christos     {
   1042  1.10  christos       std::string home_gdbearlyinit;
   1043  1.10  christos       get_earlyinit_files (&home_gdbearlyinit);
   1044  1.10  christos       if (!home_gdbearlyinit.empty () && !inhibit_home_gdbinit)
   1045  1.10  christos 	ret = catch_command_errors (source_script,
   1046  1.10  christos 				    home_gdbearlyinit.c_str (), 0);
   1047  1.10  christos     }
   1048  1.10  christos   execute_cmdargs (&cmdarg_vec, CMDARG_EARLYINIT_FILE,
   1049  1.10  christos 		   CMDARG_EARLYINIT_COMMAND, &ret);
   1050  1.10  christos 
   1051  1.11  christos   /* Set the thread pool size here, so the size can be influenced by the
   1052  1.11  christos      early initialization commands.  */
   1053  1.11  christos   update_thread_pool_size ();
   1054  1.11  christos 
   1055  1.10  christos   /* Initialize the extension languages.  */
   1056  1.10  christos   ext_lang_initialization ();
   1057  1.10  christos 
   1058  1.10  christos   /* Recheck if we're starting up quietly after processing the startup
   1059  1.10  christos      scripts and commands.  */
   1060  1.10  christos   if (!quiet)
   1061  1.10  christos     quiet = check_quiet_mode ();
   1062   1.1  christos 
   1063   1.1  christos   /* Now that gdb_init has created the initial inferior, we're in
   1064   1.1  christos      position to set args for that inferior.  */
   1065   1.1  christos   if (set_args)
   1066   1.1  christos     {
   1067   1.1  christos       /* The remaining options are the command-line options for the
   1068   1.1  christos 	 inferior.  The first one is the sym/exec file, and the rest
   1069   1.1  christos 	 are arguments.  */
   1070   1.1  christos       if (optind >= argc)
   1071   1.3  christos 	error (_("%s: `--args' specified but no program specified"),
   1072   1.3  christos 	       gdb_program_name);
   1073   1.3  christos 
   1074   1.1  christos       symarg = argv[optind];
   1075   1.1  christos       execarg = argv[optind];
   1076   1.1  christos       ++optind;
   1077  1.11  christos       current_inferior ()->set_args
   1078  1.11  christos 	(gdb::array_view<char * const> (&argv[optind], argc - optind));
   1079   1.1  christos     }
   1080   1.1  christos   else
   1081   1.1  christos     {
   1082   1.1  christos       /* OK, that's all the options.  */
   1083   1.1  christos 
   1084   1.1  christos       /* The first argument, if specified, is the name of the
   1085   1.1  christos 	 executable.  */
   1086   1.1  christos       if (optind < argc)
   1087   1.1  christos 	{
   1088   1.1  christos 	  symarg = argv[optind];
   1089   1.1  christos 	  execarg = argv[optind];
   1090   1.1  christos 	  optind++;
   1091   1.1  christos 	}
   1092   1.1  christos 
   1093   1.1  christos       /* If the user hasn't already specified a PID or the name of a
   1094   1.1  christos 	 core file, then a second optional argument is allowed.  If
   1095   1.1  christos 	 present, this argument should be interpreted as either a
   1096   1.1  christos 	 PID or a core file, whichever works.  */
   1097   1.1  christos       if (pidarg == NULL && corearg == NULL && optind < argc)
   1098   1.1  christos 	{
   1099   1.1  christos 	  pid_or_core_arg = argv[optind];
   1100   1.1  christos 	  optind++;
   1101   1.1  christos 	}
   1102   1.1  christos 
   1103   1.1  christos       /* Any argument left on the command line is unexpected and
   1104   1.1  christos 	 will be ignored.  Inform the user.  */
   1105   1.1  christos       if (optind < argc)
   1106  1.10  christos 	gdb_printf (gdb_stderr,
   1107  1.10  christos 		    _("Excess command line "
   1108  1.10  christos 		      "arguments ignored. (%s%s)\n"),
   1109  1.10  christos 		    argv[optind],
   1110  1.10  christos 		    (optind == argc - 1) ? "" : " ...");
   1111   1.1  christos     }
   1112   1.1  christos 
   1113   1.1  christos   /* Lookup gdbinit files.  Note that the gdbinit file name may be
   1114   1.9  christos      overridden during file initialization, so get_init_files should be
   1115   1.1  christos      called after gdb_init.  */
   1116   1.9  christos   std::vector<std::string> system_gdbinit;
   1117   1.9  christos   std::string home_gdbinit;
   1118   1.9  christos   std::string local_gdbinit;
   1119   1.1  christos   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
   1120   1.1  christos 
   1121   1.1  christos   /* Do these (and anything which might call wrap_here or *_filtered)
   1122   1.1  christos      after initialize_all_files() but before the interpreter has been
   1123   1.1  christos      installed.  Otherwize the help/version messages will be eaten by
   1124   1.1  christos      the interpreter's output handler.  */
   1125   1.1  christos 
   1126   1.1  christos   if (print_version)
   1127   1.1  christos     {
   1128   1.8  christos       print_gdb_version (gdb_stdout, false);
   1129  1.10  christos       gdb_printf ("\n");
   1130   1.1  christos       exit (0);
   1131   1.1  christos     }
   1132   1.1  christos 
   1133   1.1  christos   if (print_help)
   1134   1.1  christos     {
   1135   1.1  christos       print_gdb_help (gdb_stdout);
   1136   1.1  christos       exit (0);
   1137   1.1  christos     }
   1138   1.1  christos 
   1139   1.1  christos   if (print_configuration)
   1140   1.1  christos     {
   1141   1.1  christos       print_gdb_configuration (gdb_stdout);
   1142  1.10  christos       gdb_printf ("\n");
   1143   1.1  christos       exit (0);
   1144   1.1  christos     }
   1145   1.1  christos 
   1146   1.1  christos   /* Install the default UI.  All the interpreters should have had a
   1147   1.1  christos      look at things by now.  Initialize the default interpreter.  */
   1148  1.11  christos   set_top_level_interpreter (interpreter_p.c_str (), false);
   1149  1.11  christos 
   1150  1.11  christos   /* The interpreter should have installed the real uiout by now.  */
   1151  1.11  christos   gdb_assert (current_uiout != temp_uiout.get ());
   1152  1.11  christos   temp_uiout = nullptr;
   1153   1.1  christos 
   1154  1.11  christos   if (!quiet)
   1155   1.1  christos     {
   1156   1.1  christos       /* Print all the junk at the top, with trailing "..." if we are
   1157  1.10  christos 	 about to read a symbol file (possibly slowly).  */
   1158   1.8  christos       print_gdb_version (gdb_stdout, true);
   1159   1.1  christos       if (symarg)
   1160  1.10  christos 	gdb_printf ("..");
   1161  1.10  christos       gdb_printf ("\n");
   1162   1.1  christos       gdb_flush (gdb_stdout);	/* Force to screen during slow
   1163   1.1  christos 				   operations.  */
   1164   1.1  christos     }
   1165   1.1  christos 
   1166   1.1  christos   /* Set off error and warning messages with a blank line.  */
   1167   1.7  christos   tmp_warn_preprint.reset ();
   1168   1.1  christos   warning_pre_print = _("\nwarning: ");
   1169   1.1  christos 
   1170   1.1  christos   /* Read and execute the system-wide gdbinit file, if it exists.
   1171   1.1  christos      This is done *before* all the command line arguments are
   1172   1.1  christos      processed; it sets global parameters, which are independent of
   1173   1.1  christos      what file you are debugging or what directory you are in.  */
   1174   1.9  christos   if (!system_gdbinit.empty () && !inhibit_gdbinit)
   1175   1.9  christos     {
   1176   1.9  christos       for (const std::string &file : system_gdbinit)
   1177   1.9  christos 	ret = catch_command_errors (source_script, file.c_str (), 0);
   1178   1.9  christos     }
   1179   1.1  christos 
   1180   1.1  christos   /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
   1181   1.1  christos      *before* all the command line arguments are processed; it sets
   1182   1.1  christos      global parameters, which are independent of what file you are
   1183   1.1  christos      debugging or what directory you are in.  */
   1184   1.1  christos 
   1185   1.9  christos   if (!home_gdbinit.empty () && !inhibit_gdbinit && !inhibit_home_gdbinit)
   1186   1.9  christos     ret = catch_command_errors (source_script, home_gdbinit.c_str (), 0);
   1187   1.1  christos 
   1188   1.1  christos   /* Process '-ix' and '-iex' options early.  */
   1189  1.10  christos   execute_cmdargs (&cmdarg_vec, CMDARG_INIT_FILE, CMDARG_INIT_COMMAND, &ret);
   1190   1.1  christos 
   1191   1.1  christos   /* Now perform all the actions indicated by the arguments.  */
   1192   1.1  christos   if (cdarg != NULL)
   1193   1.1  christos     {
   1194   1.8  christos       ret = catch_command_errors (cd_command, cdarg, 0);
   1195   1.1  christos     }
   1196   1.1  christos 
   1197   1.7  christos   for (i = 0; i < dirarg.size (); i++)
   1198   1.8  christos     ret = catch_command_errors (directory_switch, dirarg[i], 0);
   1199   1.1  christos 
   1200   1.1  christos   /* Skip auto-loading section-specified scripts until we've sourced
   1201   1.1  christos      local_gdbinit (which is often used to augment the source search
   1202   1.1  christos      path).  */
   1203   1.1  christos   save_auto_load = global_auto_load;
   1204   1.1  christos   global_auto_load = 0;
   1205   1.1  christos 
   1206   1.1  christos   if (execarg != NULL
   1207   1.1  christos       && symarg != NULL
   1208   1.1  christos       && strcmp (execarg, symarg) == 0)
   1209   1.1  christos     {
   1210   1.1  christos       /* The exec file and the symbol-file are the same.  If we can't
   1211  1.10  christos 	 open it, better only print one error message.
   1212  1.10  christos 	 catch_command_errors returns non-zero on success!  */
   1213   1.8  christos       ret = catch_command_errors (exec_file_attach, execarg,
   1214   1.8  christos 				  !batch_flag);
   1215   1.8  christos       if (ret != 0)
   1216   1.8  christos 	ret = catch_command_errors (symbol_file_add_main_adapter,
   1217   1.8  christos 				    symarg, !batch_flag);
   1218   1.1  christos     }
   1219   1.1  christos   else
   1220   1.1  christos     {
   1221   1.1  christos       if (execarg != NULL)
   1222   1.8  christos 	ret = catch_command_errors (exec_file_attach, execarg,
   1223   1.5  christos 				    !batch_flag);
   1224   1.1  christos       if (symarg != NULL)
   1225   1.8  christos 	ret = catch_command_errors (symbol_file_add_main_adapter,
   1226   1.8  christos 				    symarg, !batch_flag);
   1227   1.1  christos     }
   1228   1.1  christos 
   1229   1.1  christos   if (corearg && pidarg)
   1230   1.1  christos     error (_("Can't attach to process and specify "
   1231   1.1  christos 	     "a core file at the same time."));
   1232   1.1  christos 
   1233   1.1  christos   if (corearg != NULL)
   1234   1.8  christos     {
   1235   1.8  christos       ret = catch_command_errors (core_file_command, corearg,
   1236   1.8  christos 				  !batch_flag);
   1237   1.8  christos     }
   1238   1.1  christos   else if (pidarg != NULL)
   1239   1.8  christos     {
   1240   1.8  christos       ret = catch_command_errors (attach_command, pidarg, !batch_flag);
   1241   1.8  christos     }
   1242   1.1  christos   else if (pid_or_core_arg)
   1243   1.1  christos     {
   1244   1.1  christos       /* The user specified 'gdb program pid' or gdb program core'.
   1245   1.1  christos 	 If pid_or_core_arg's first character is a digit, try attach
   1246   1.1  christos 	 first and then corefile.  Otherwise try just corefile.  */
   1247   1.1  christos 
   1248  1.11  christos       if (isdigit ((unsigned char)pid_or_core_arg[0]))
   1249   1.1  christos 	{
   1250   1.8  christos 	  ret = catch_command_errors (attach_command, pid_or_core_arg,
   1251   1.8  christos 				      !batch_flag);
   1252   1.8  christos 	  if (ret == 0)
   1253   1.8  christos 	    ret = catch_command_errors (core_file_command,
   1254   1.8  christos 					pid_or_core_arg,
   1255   1.8  christos 					!batch_flag);
   1256   1.8  christos 	}
   1257   1.8  christos       else
   1258   1.8  christos 	{
   1259   1.8  christos 	  /* Can't be a pid, better be a corefile.  */
   1260   1.8  christos 	  ret = catch_command_errors (core_file_command,
   1261   1.8  christos 				      pid_or_core_arg,
   1262   1.8  christos 				      !batch_flag);
   1263   1.1  christos 	}
   1264   1.1  christos     }
   1265   1.1  christos 
   1266   1.1  christos   if (ttyarg != NULL)
   1267   1.9  christos     current_inferior ()->set_tty (ttyarg);
   1268   1.1  christos 
   1269   1.1  christos   /* Error messages should no longer be distinguished with extra output.  */
   1270   1.1  christos   warning_pre_print = _("warning: ");
   1271   1.1  christos 
   1272   1.1  christos   /* Read the .gdbinit file in the current directory, *if* it isn't
   1273   1.1  christos      the same as the $HOME/.gdbinit file (it should exist, also).  */
   1274   1.9  christos   if (!local_gdbinit.empty ())
   1275   1.1  christos     {
   1276   1.8  christos       auto_load_local_gdbinit_pathname
   1277   1.9  christos 	= gdb_realpath (local_gdbinit.c_str ()).release ();
   1278   1.1  christos 
   1279  1.10  christos       if (!inhibit_gdbinit && auto_load_local_gdbinit)
   1280   1.1  christos 	{
   1281  1.10  christos 	  auto_load_debug_printf ("Loading .gdbinit file \"%s\".",
   1282  1.10  christos 				  local_gdbinit.c_str ());
   1283  1.10  christos 
   1284  1.10  christos 	  if (file_is_auto_load_safe (local_gdbinit.c_str ()))
   1285  1.10  christos 	    {
   1286  1.10  christos 	      auto_load_local_gdbinit_loaded = 1;
   1287   1.1  christos 
   1288  1.10  christos 	      ret = catch_command_errors (source_script, local_gdbinit.c_str (), 0);
   1289  1.10  christos 	    }
   1290   1.1  christos 	}
   1291   1.1  christos     }
   1292   1.1  christos 
   1293   1.1  christos   /* Now that all .gdbinit's have been read and all -d options have been
   1294   1.1  christos      processed, we can read any scripts mentioned in SYMARG.
   1295   1.1  christos      We wait until now because it is common to add to the source search
   1296   1.1  christos      path in local_gdbinit.  */
   1297   1.1  christos   global_auto_load = save_auto_load;
   1298   1.8  christos   for (objfile *objfile : current_program_space->objfiles ())
   1299   1.1  christos     load_auto_scripts_for_objfile (objfile);
   1300   1.1  christos 
   1301   1.1  christos   /* Process '-x' and '-ex' options.  */
   1302  1.10  christos   execute_cmdargs (&cmdarg_vec, CMDARG_FILE, CMDARG_COMMAND, &ret);
   1303   1.1  christos 
   1304   1.1  christos   if (batch_flag)
   1305   1.1  christos     {
   1306   1.8  christos       int error_status = EXIT_FAILURE;
   1307   1.8  christos       int *exit_arg = ret == 0 ? &error_status : NULL;
   1308   1.8  christos 
   1309   1.1  christos       /* We have hit the end of the batch file.  */
   1310   1.8  christos       quit_force (exit_arg, 0);
   1311   1.1  christos     }
   1312  1.11  christos 
   1313  1.11  christos   /* We are starting an interactive session.  */
   1314  1.11  christos 
   1315  1.11  christos   /* Read in the history.  This is after all the command files have been read,
   1316  1.11  christos      so that the user can change the history file via a .gdbinit file.  This
   1317  1.11  christos      is also after the batch_flag check, because we don't need the history in
   1318  1.11  christos      batch mode.  */
   1319  1.11  christos   init_history ();
   1320   1.7  christos }
   1321   1.7  christos 
   1322   1.7  christos static void
   1323   1.7  christos captured_main (void *data)
   1324   1.7  christos {
   1325   1.7  christos   struct captured_main_args *context = (struct captured_main_args *) data;
   1326   1.1  christos 
   1327   1.7  christos   captured_main_1 (context);
   1328   1.1  christos 
   1329   1.1  christos   /* NOTE: cagney/1999-11-07: There is probably no reason for not
   1330   1.1  christos      moving this loop and the code found in captured_command_loop()
   1331   1.1  christos      into the command_loop() proper.  The main thing holding back that
   1332   1.1  christos      change - SET_TOP_LEVEL() - has been eliminated.  */
   1333   1.1  christos   while (1)
   1334   1.1  christos     {
   1335   1.9  christos       try
   1336   1.8  christos 	{
   1337   1.8  christos 	  captured_command_loop ();
   1338   1.8  christos 	}
   1339  1.11  christos       catch (const gdb_exception_forced_quit &ex)
   1340  1.11  christos 	{
   1341  1.11  christos 	  quit_force (NULL, 0);
   1342  1.11  christos 	}
   1343   1.9  christos       catch (const gdb_exception &ex)
   1344   1.8  christos 	{
   1345   1.8  christos 	  exception_print (gdb_stderr, ex);
   1346   1.8  christos 	}
   1347   1.1  christos     }
   1348   1.1  christos   /* No exit -- exit is through quit_command.  */
   1349   1.1  christos }
   1350   1.1  christos 
   1351   1.1  christos int
   1352   1.1  christos gdb_main (struct captured_main_args *args)
   1353   1.1  christos {
   1354   1.9  christos   try
   1355   1.6  christos     {
   1356   1.6  christos       captured_main (args);
   1357   1.6  christos     }
   1358   1.9  christos   catch (const gdb_exception &ex)
   1359   1.6  christos     {
   1360   1.6  christos       exception_print (gdb_stderr, ex);
   1361   1.6  christos     }
   1362   1.6  christos 
   1363   1.1  christos   /* The only way to end up here is by an error (normal exit is
   1364   1.1  christos      handled by quit_force()), hence always return an error status.  */
   1365   1.1  christos   return 1;
   1366   1.1  christos }
   1367   1.1  christos 
   1368   1.1  christos 
   1369   1.1  christos /* Don't use *_filtered for printing help.  We don't want to prompt
   1370   1.1  christos    for continue no matter how small the screen or how much we're going
   1371   1.1  christos    to print.  */
   1372   1.1  christos 
   1373   1.1  christos static void
   1374   1.1  christos print_gdb_help (struct ui_file *stream)
   1375   1.1  christos {
   1376   1.9  christos   std::vector<std::string> system_gdbinit;
   1377   1.9  christos   std::string home_gdbinit;
   1378   1.9  christos   std::string local_gdbinit;
   1379  1.10  christos   std::string home_gdbearlyinit;
   1380   1.1  christos 
   1381   1.1  christos   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
   1382  1.10  christos   get_earlyinit_files (&home_gdbearlyinit);
   1383   1.1  christos 
   1384   1.1  christos   /* Note: The options in the list below are only approximately sorted
   1385   1.1  christos      in the alphabetical order, so as to group closely related options
   1386   1.1  christos      together.  */
   1387  1.10  christos   gdb_puts (_("\
   1388   1.1  christos This is the GNU debugger.  Usage:\n\n\
   1389   1.1  christos     gdb [options] [executable-file [core-file or process-id]]\n\
   1390   1.1  christos     gdb [options] --args executable-file [inferior-arguments ...]\n\n\
   1391   1.1  christos "), stream);
   1392  1.10  christos   gdb_puts (_("\
   1393   1.1  christos Selection of debuggee and its files:\n\n\
   1394  1.10  christos   --args             Arguments after executable-file are passed to inferior.\n\
   1395   1.1  christos   --core=COREFILE    Analyze the core dump COREFILE.\n\
   1396   1.1  christos   --exec=EXECFILE    Use EXECFILE as the executable.\n\
   1397   1.1  christos   --pid=PID          Attach to running process PID.\n\
   1398   1.1  christos   --directory=DIR    Search for source files in DIR.\n\
   1399   1.1  christos   --se=FILE          Use FILE as symbol file and executable file.\n\
   1400   1.1  christos   --symbols=SYMFILE  Read symbols from SYMFILE.\n\
   1401   1.1  christos   --readnow          Fully read symbol files on first access.\n\
   1402   1.8  christos   --readnever        Do not read symbol files.\n\
   1403   1.1  christos   --write            Set writing into executable and core files.\n\n\
   1404   1.1  christos "), stream);
   1405  1.10  christos   gdb_puts (_("\
   1406   1.1  christos Initial commands and command files:\n\n\
   1407   1.1  christos   --command=FILE, -x Execute GDB commands from FILE.\n\
   1408   1.1  christos   --init-command=FILE, -ix\n\
   1409  1.10  christos 		     Like -x but execute commands before loading inferior.\n\
   1410   1.1  christos   --eval-command=COMMAND, -ex\n\
   1411  1.10  christos 		     Execute a single GDB command.\n\
   1412  1.10  christos 		     May be used multiple times and in conjunction\n\
   1413  1.10  christos 		     with --command.\n\
   1414   1.1  christos   --init-eval-command=COMMAND, -iex\n\
   1415  1.10  christos 		     Like -ex but before loading inferior.\n\
   1416   1.1  christos   --nh               Do not read ~/.gdbinit.\n\
   1417   1.1  christos   --nx               Do not read any .gdbinit files in any directory.\n\n\
   1418   1.1  christos "), stream);
   1419  1.10  christos   gdb_puts (_("\
   1420   1.1  christos Output and user interface control:\n\n\
   1421   1.1  christos   --fullname         Output information used by emacs-GDB interface.\n\
   1422   1.1  christos   --interpreter=INTERP\n\
   1423  1.10  christos 		     Select a specific interpreter / user interface.\n\
   1424   1.1  christos   --tty=TTY          Use TTY for input/output by the program being debugged.\n\
   1425   1.1  christos   -w                 Use the GUI interface.\n\
   1426   1.1  christos   --nw               Do not use the GUI interface.\n\
   1427   1.1  christos "), stream);
   1428   1.1  christos #if defined(TUI)
   1429  1.10  christos   gdb_puts (_("\
   1430   1.1  christos   --tui              Use a terminal user interface.\n\
   1431   1.1  christos "), stream);
   1432   1.1  christos #endif
   1433  1.10  christos   gdb_puts (_("\
   1434   1.3  christos   -q, --quiet, --silent\n\
   1435  1.10  christos 		     Do not print version number on startup.\n\n\
   1436   1.1  christos "), stream);
   1437  1.10  christos   gdb_puts (_("\
   1438   1.1  christos Operating modes:\n\n\
   1439   1.1  christos   --batch            Exit after processing options.\n\
   1440   1.1  christos   --batch-silent     Like --batch, but suppress all gdb stdout output.\n\
   1441   1.1  christos   --return-child-result\n\
   1442  1.10  christos 		     GDB exit code will be the child's exit code.\n\
   1443   1.1  christos   --configuration    Print details about GDB configuration and then exit.\n\
   1444   1.1  christos   --help             Print this message and then exit.\n\
   1445   1.1  christos   --version          Print version information and then exit.\n\n\
   1446   1.1  christos Remote debugging options:\n\n\
   1447   1.1  christos   -b BAUDRATE        Set serial port baud rate used for remote debugging.\n\
   1448   1.1  christos   -l TIMEOUT         Set timeout in seconds for remote debugging.\n\n\
   1449   1.1  christos Other options:\n\n\
   1450   1.1  christos   --cd=DIR           Change current directory to DIR.\n\
   1451   1.3  christos   --data-directory=DIR, -D\n\
   1452  1.10  christos 		     Set GDB's data-directory to DIR.\n\
   1453   1.1  christos "), stream);
   1454  1.10  christos   gdb_puts (_("\n\
   1455  1.10  christos At startup, GDB reads the following early init files and executes their\n\
   1456  1.10  christos commands:\n\
   1457  1.10  christos "), stream);
   1458  1.10  christos   if (!home_gdbearlyinit.empty ())
   1459  1.10  christos     gdb_printf (stream, _("\
   1460  1.10  christos    * user-specific early init file: %s\n\
   1461  1.10  christos "), home_gdbearlyinit.c_str ());
   1462  1.10  christos   if (home_gdbearlyinit.empty ())
   1463  1.10  christos     gdb_printf (stream, _("\
   1464  1.10  christos    None found.\n"));
   1465  1.10  christos   gdb_puts (_("\n\
   1466   1.1  christos At startup, GDB reads the following init files and executes their commands:\n\
   1467   1.1  christos "), stream);
   1468   1.9  christos   if (!system_gdbinit.empty ())
   1469   1.9  christos     {
   1470   1.9  christos       std::string output;
   1471   1.9  christos       for (size_t idx = 0; idx < system_gdbinit.size (); ++idx)
   1472  1.10  christos 	{
   1473   1.9  christos 	  output += system_gdbinit[idx];
   1474   1.9  christos 	  if (idx < system_gdbinit.size () - 1)
   1475   1.9  christos 	    output += ", ";
   1476   1.9  christos 	}
   1477  1.10  christos       gdb_printf (stream, _("\
   1478   1.9  christos    * system-wide init files: %s\n\
   1479   1.9  christos "), output.c_str ());
   1480   1.9  christos     }
   1481   1.9  christos   if (!home_gdbinit.empty ())
   1482  1.10  christos     gdb_printf (stream, _("\
   1483   1.1  christos    * user-specific init file: %s\n\
   1484   1.9  christos "), home_gdbinit.c_str ());
   1485   1.9  christos   if (!local_gdbinit.empty ())
   1486  1.10  christos     gdb_printf (stream, _("\
   1487   1.1  christos    * local init file (see also 'set auto-load local-gdbinit'): ./%s\n\
   1488   1.9  christos "), local_gdbinit.c_str ());
   1489  1.10  christos   if (system_gdbinit.empty () && home_gdbinit.empty ()
   1490  1.10  christos       && local_gdbinit.empty ())
   1491  1.10  christos     gdb_printf (stream, _("\
   1492  1.10  christos    None found.\n"));
   1493  1.10  christos   gdb_puts (_("\n\
   1494   1.1  christos For more information, type \"help\" from within GDB, or consult the\n\
   1495   1.1  christos GDB manual (available as on-line info or a printed manual).\n\
   1496   1.1  christos "), stream);
   1497   1.1  christos   if (REPORT_BUGS_TO[0] && stream == gdb_stdout)
   1498  1.10  christos     gdb_printf (stream, _("\n\
   1499  1.10  christos Report bugs to %ps.\n\
   1500  1.10  christos "), styled_string (file_name_style.style (), REPORT_BUGS_TO));
   1501   1.9  christos   if (stream == gdb_stdout)
   1502  1.10  christos     gdb_printf (stream, _("\n\
   1503   1.9  christos You can ask GDB-related questions on the GDB users mailing list\n\
   1504  1.11  christos (gdb (at) sourceware.org) or on GDB's IRC channel (#gdb on Libera.Chat).\n"));
   1505   1.1  christos }
   1506