GDB (API)
/home/stan/gdb/src/gdb/main.c
Go to the documentation of this file.
00001 /* Top level stuff for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "top.h"
00022 #include "target.h"
00023 #include "inferior.h"
00024 #include "symfile.h"
00025 #include "gdbcore.h"
00026 
00027 #include "exceptions.h"
00028 #include "getopt.h"
00029 
00030 #include <sys/types.h>
00031 #include "gdb_stat.h"
00032 #include <ctype.h>
00033 
00034 #include "gdb_string.h"
00035 #include "event-loop.h"
00036 #include "ui-out.h"
00037 
00038 #include "interps.h"
00039 #include "main.h"
00040 #include "source.h"
00041 #include "cli/cli-cmds.h"
00042 #include "python/python.h"
00043 #include "objfiles.h"
00044 #include "auto-load.h"
00045 #include "maint.h"
00046 
00047 #include "filenames.h"
00048 #include "filestuff.h"
00049 
00050 /* The selected interpreter.  This will be used as a set command
00051    variable, so it should always be malloc'ed - since
00052    do_setshow_command will free it.  */
00053 char *interpreter_p;
00054 
00055 /* Whether xdb commands will be handled.  */
00056 int xdb_commands = 0;
00057 
00058 /* Whether dbx commands will be handled.  */
00059 int dbx_commands = 0;
00060 
00061 /* System root path, used to find libraries etc.  */
00062 char *gdb_sysroot = 0;
00063 
00064 /* GDB datadir, used to store data files.  */
00065 char *gdb_datadir = 0;
00066 
00067 /* Non-zero if GDB_DATADIR was provided on the command line.
00068    This doesn't track whether data-directory is set later from the
00069    command line, but we don't reread system.gdbinit when that happens.  */
00070 static int gdb_datadir_provided = 0;
00071 
00072 /* If gdb was configured with --with-python=/path,
00073    the possibly relocated path to python's lib directory.  */
00074 char *python_libdir = 0;
00075 
00076 struct ui_file *gdb_stdout;
00077 struct ui_file *gdb_stderr;
00078 struct ui_file *gdb_stdlog;
00079 struct ui_file *gdb_stdin;
00080 /* Target IO streams.  */
00081 struct ui_file *gdb_stdtargin;
00082 struct ui_file *gdb_stdtarg;
00083 struct ui_file *gdb_stdtargerr;
00084 
00085 /* True if --batch or --batch-silent was seen.  */
00086 int batch_flag = 0;
00087 
00088 /* Support for the --batch-silent option.  */
00089 int batch_silent = 0;
00090 
00091 /* Support for --return-child-result option.
00092    Set the default to -1 to return error in the case
00093    that the program does not run or does not complete.  */
00094 int return_child_result = 0;
00095 int return_child_result_value = -1;
00096 
00097 
00098 /* GDB as it has been invoked from the command line (i.e. argv[0]).  */
00099 static char *gdb_program_name;
00100 
00101 /* Return read only pointer to GDB_PROGRAM_NAME.  */
00102 const char *
00103 get_gdb_program_name (void)
00104 {
00105   return gdb_program_name;
00106 }
00107 
00108 static void print_gdb_help (struct ui_file *);
00109 
00110 /* Relocate a file or directory.  PROGNAME is the name by which gdb
00111    was invoked (i.e., argv[0]).  INITIAL is the default value for the
00112    file or directory.  FLAG is true if the value is relocatable, false
00113    otherwise.  Returns a newly allocated string; this may return NULL
00114    under the same conditions as make_relative_prefix.  */
00115 
00116 static char *
00117 relocate_path (const char *progname, const char *initial, int flag)
00118 {
00119   if (flag)
00120     return make_relative_prefix (progname, BINDIR, initial);
00121   return xstrdup (initial);
00122 }
00123 
00124 /* Like relocate_path, but specifically checks for a directory.
00125    INITIAL is relocated according to the rules of relocate_path.  If
00126    the result is a directory, it is used; otherwise, INITIAL is used.
00127    The chosen directory is then canonicalized using lrealpath.  This
00128    function always returns a newly-allocated string.  */
00129 
00130 char *
00131 relocate_gdb_directory (const char *initial, int flag)
00132 {
00133   char *dir;
00134 
00135   dir = relocate_path (gdb_program_name, initial, flag);
00136   if (dir)
00137     {
00138       struct stat s;
00139 
00140       if (*dir == '\0' || stat (dir, &s) != 0 || !S_ISDIR (s.st_mode))
00141         {
00142           xfree (dir);
00143           dir = NULL;
00144         }
00145     }
00146   if (!dir)
00147     dir = xstrdup (initial);
00148 
00149   /* Canonicalize the directory.  */
00150   if (*dir)
00151     {
00152       char *canon_sysroot = lrealpath (dir);
00153 
00154       if (canon_sysroot)
00155         {
00156           xfree (dir);
00157           dir = canon_sysroot;
00158         }
00159     }
00160 
00161   return dir;
00162 }
00163 
00164 /* Compute the locations of init files that GDB should source and
00165    return them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT.  If
00166    there is no system gdbinit (resp. home gdbinit and local gdbinit)
00167    to be loaded, then SYSTEM_GDBINIT (resp. HOME_GDBINIT and
00168    LOCAL_GDBINIT) is set to NULL.  */
00169 static void
00170 get_init_files (const char **system_gdbinit,
00171                 const char **home_gdbinit,
00172                 const char **local_gdbinit)
00173 {
00174   static const char *sysgdbinit = NULL;
00175   static char *homeinit = NULL;
00176   static const char *localinit = NULL;
00177   static int initialized = 0;
00178 
00179   if (!initialized)
00180     {
00181       struct stat homebuf, cwdbuf, s;
00182       char *homedir;
00183 
00184       if (SYSTEM_GDBINIT[0])
00185         {
00186           int datadir_len = strlen (GDB_DATADIR);
00187           int sys_gdbinit_len = strlen (SYSTEM_GDBINIT);
00188           char *relocated_sysgdbinit;
00189 
00190           /* If SYSTEM_GDBINIT lives in data-directory, and data-directory
00191              has been provided, search for SYSTEM_GDBINIT there.  */
00192           if (gdb_datadir_provided
00193               && datadir_len < sys_gdbinit_len
00194               && filename_ncmp (SYSTEM_GDBINIT, GDB_DATADIR, datadir_len) == 0
00195               && IS_DIR_SEPARATOR (SYSTEM_GDBINIT[datadir_len]))
00196             {
00197               /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
00198                  to gdb_datadir.  */
00199               char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
00200               char *p;
00201 
00202               for (p = tmp_sys_gdbinit; IS_DIR_SEPARATOR (*p); ++p)
00203                 continue;
00204               relocated_sysgdbinit = concat (gdb_datadir, SLASH_STRING, p,
00205                                              NULL);
00206               xfree (tmp_sys_gdbinit);
00207             }
00208           else
00209             {
00210               relocated_sysgdbinit = relocate_path (gdb_program_name,
00211                                                     SYSTEM_GDBINIT,
00212                                                     SYSTEM_GDBINIT_RELOCATABLE);
00213             }
00214           if (relocated_sysgdbinit && stat (relocated_sysgdbinit, &s) == 0)
00215             sysgdbinit = relocated_sysgdbinit;
00216           else
00217             xfree (relocated_sysgdbinit);
00218         }
00219 
00220       homedir = getenv ("HOME");
00221 
00222       /* If the .gdbinit file in the current directory is the same as
00223          the $HOME/.gdbinit file, it should not be sourced.  homebuf
00224          and cwdbuf are used in that purpose.  Make sure that the stats
00225          are zero in case one of them fails (this guarantees that they
00226          won't match if either exists).  */
00227 
00228       memset (&homebuf, 0, sizeof (struct stat));
00229       memset (&cwdbuf, 0, sizeof (struct stat));
00230 
00231       if (homedir)
00232         {
00233           homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
00234           if (stat (homeinit, &homebuf) != 0)
00235             {
00236               xfree (homeinit);
00237               homeinit = NULL;
00238             }
00239         }
00240 
00241       if (stat (gdbinit, &cwdbuf) == 0)
00242         {
00243           if (!homeinit
00244               || memcmp ((char *) &homebuf, (char *) &cwdbuf,
00245                          sizeof (struct stat)))
00246             localinit = gdbinit;
00247         }
00248       
00249       initialized = 1;
00250     }
00251 
00252   *system_gdbinit = sysgdbinit;
00253   *home_gdbinit = homeinit;
00254   *local_gdbinit = localinit;
00255 }
00256 
00257 /* Call command_loop.  If it happens to return, pass that through as a
00258    non-zero return status.  */
00259 
00260 static int
00261 captured_command_loop (void *data)
00262 {
00263   /* Top-level execution commands can be run in the background from
00264      here on.  */
00265   interpreter_async = 1;
00266 
00267   current_interp_command_loop ();
00268   /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
00269      would clean things up (restoring the cleanup chain) to the state
00270      they were just prior to the call.  Technically, this means that
00271      the do_cleanups() below is redundant.  Unfortunately, many FUNCs
00272      are not that well behaved.  do_cleanups should either be replaced
00273      with a do_cleanups call (to cover the problem) or an assertion
00274      check to detect bad FUNCs code.  */
00275   do_cleanups (all_cleanups ());
00276   /* If the command_loop returned, normally (rather than threw an
00277      error) we try to quit.  If the quit is aborted, catch_errors()
00278      which called this catch the signal and restart the command
00279      loop.  */
00280   quit_command (NULL, instream == stdin);
00281   return 1;
00282 }
00283 
00284 /* Arguments of --command option and its counterpart.  */
00285 typedef struct cmdarg {
00286   /* Type of this option.  */
00287   enum {
00288     /* Option type -x.  */
00289     CMDARG_FILE,
00290 
00291     /* Option type -ex.  */
00292     CMDARG_COMMAND,
00293 
00294     /* Option type -ix.  */
00295     CMDARG_INIT_FILE,
00296     
00297     /* Option type -iex.  */
00298     CMDARG_INIT_COMMAND
00299   } type;
00300 
00301   /* Value of this option - filename or the GDB command itself.  String memory
00302      is not owned by this structure despite it is 'const'.  */
00303   char *string;
00304 } cmdarg_s;
00305 
00306 /* Define type VEC (cmdarg_s).  */
00307 DEF_VEC_O (cmdarg_s);
00308 
00309 static int
00310 captured_main (void *data)
00311 {
00312   struct captured_main_args *context = data;
00313   int argc = context->argc;
00314   char **argv = context->argv;
00315   static int quiet = 0;
00316   static int set_args = 0;
00317   static int inhibit_home_gdbinit = 0;
00318 
00319   /* Pointers to various arguments from command line.  */
00320   char *symarg = NULL;
00321   char *execarg = NULL;
00322   char *pidarg = NULL;
00323   char *corearg = NULL;
00324   char *pid_or_core_arg = NULL;
00325   char *cdarg = NULL;
00326   char *ttyarg = NULL;
00327 
00328   /* These are static so that we can take their address in an
00329      initializer.  */
00330   static int print_help;
00331   static int print_version;
00332   static int print_configuration;
00333 
00334   /* Pointers to all arguments of --command option.  */
00335   VEC (cmdarg_s) *cmdarg_vec = NULL;
00336   struct cmdarg *cmdarg_p;
00337 
00338   /* Indices of all arguments of --directory option.  */
00339   char **dirarg;
00340   /* Allocated size.  */
00341   int dirsize;
00342   /* Number of elements used.  */
00343   int ndir;
00344 
00345   /* gdb init files.  */
00346   const char *system_gdbinit;
00347   const char *home_gdbinit;
00348   const char *local_gdbinit;
00349 
00350   int i;
00351   int save_auto_load;
00352   struct objfile *objfile;
00353 
00354   struct cleanup *pre_stat_chain;
00355 
00356 #ifdef HAVE_SBRK
00357   /* Set this before calling make_command_stats_cleanup.  */
00358   lim_at_start = (char *) sbrk (0);
00359 #endif
00360 
00361   pre_stat_chain = make_command_stats_cleanup (0);
00362 
00363 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
00364   setlocale (LC_MESSAGES, "");
00365 #endif
00366 #if defined (HAVE_SETLOCALE)
00367   setlocale (LC_CTYPE, "");
00368 #endif
00369   bindtextdomain (PACKAGE, LOCALEDIR);
00370   textdomain (PACKAGE);
00371 
00372   bfd_init ();
00373   notice_open_fds ();
00374 
00375   make_cleanup (VEC_cleanup (cmdarg_s), &cmdarg_vec);
00376   dirsize = 1;
00377   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
00378   ndir = 0;
00379 
00380   clear_quit_flag ();
00381   saved_command_line = (char *) xmalloc (saved_command_line_size);
00382   saved_command_line[0] = '\0';
00383   instream = stdin;
00384 
00385 #ifdef __MINGW32__
00386   /* Ensure stderr is unbuffered.  A Cygwin pty or pipe is implemented
00387      as a Windows pipe, and Windows buffers on pipes.  */
00388   setvbuf (stderr, NULL, _IONBF, BUFSIZ);
00389 #endif
00390 
00391   gdb_stdout = stdio_fileopen (stdout);
00392   gdb_stderr = stderr_fileopen ();
00393 
00394   gdb_stdlog = gdb_stderr;      /* for moment */
00395   gdb_stdtarg = gdb_stderr;     /* for moment */
00396   gdb_stdin = stdio_fileopen (stdin);
00397   gdb_stdtargerr = gdb_stderr;  /* for moment */
00398   gdb_stdtargin = gdb_stdin;    /* for moment */
00399 
00400 #ifdef __MINGW32__
00401   /* On Windows, argv[0] is not necessarily set to absolute form when
00402      GDB is found along PATH, without which relocation doesn't work.  */
00403   gdb_program_name = windows_get_absolute_argv0 (argv[0]);
00404 #else
00405   gdb_program_name = xstrdup (argv[0]);
00406 #endif
00407 
00408   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
00409     /* Don't use *_filtered or warning() (which relies on
00410        current_target) until after initialize_all_files().  */
00411     fprintf_unfiltered (gdb_stderr,
00412                         _("%s: warning: error finding "
00413                           "working directory: %s\n"),
00414                         argv[0], safe_strerror (errno));
00415     
00416   current_directory = gdb_dirbuf;
00417 
00418   /* Set the sysroot path.  */
00419   gdb_sysroot = relocate_gdb_directory (TARGET_SYSTEM_ROOT,
00420                                         TARGET_SYSTEM_ROOT_RELOCATABLE);
00421 
00422   debug_file_directory = relocate_gdb_directory (DEBUGDIR,
00423                                                  DEBUGDIR_RELOCATABLE);
00424 
00425   gdb_datadir = relocate_gdb_directory (GDB_DATADIR,
00426                                         GDB_DATADIR_RELOCATABLE);
00427 
00428 #ifdef WITH_PYTHON_PATH
00429   {
00430     /* For later use in helping Python find itself.  */
00431     char *tmp = concat (WITH_PYTHON_PATH, SLASH_STRING, "lib", NULL);
00432 
00433     python_libdir = relocate_gdb_directory (tmp, PYTHON_PATH_RELOCATABLE);
00434     xfree (tmp);
00435   }
00436 #endif
00437 
00438 #ifdef RELOC_SRCDIR
00439   add_substitute_path_rule (RELOC_SRCDIR,
00440                             make_relative_prefix (gdb_program_name, BINDIR,
00441                                                   RELOC_SRCDIR));
00442 #endif
00443 
00444   /* There will always be an interpreter.  Either the one passed into
00445      this captured main, or one specified by the user at start up, or
00446      the console.  Initialize the interpreter to the one requested by 
00447      the application.  */
00448   interpreter_p = xstrdup (context->interpreter_p);
00449 
00450   /* Parse arguments and options.  */
00451   {
00452     int c;
00453     /* When var field is 0, use flag field to record the equivalent
00454        short option (or arbitrary numbers starting at 10 for those
00455        with no equivalent).  */
00456     enum {
00457       OPT_SE = 10,
00458       OPT_CD,
00459       OPT_ANNOTATE,
00460       OPT_STATISTICS,
00461       OPT_TUI,
00462       OPT_NOWINDOWS,
00463       OPT_WINDOWS,
00464       OPT_IX,
00465       OPT_IEX
00466     };
00467     static struct option long_options[] =
00468     {
00469       {"tui", no_argument, 0, OPT_TUI},
00470       {"xdb", no_argument, &xdb_commands, 1},
00471       {"dbx", no_argument, &dbx_commands, 1},
00472       {"readnow", no_argument, &readnow_symbol_files, 1},
00473       {"r", no_argument, &readnow_symbol_files, 1},
00474       {"quiet", no_argument, &quiet, 1},
00475       {"q", no_argument, &quiet, 1},
00476       {"silent", no_argument, &quiet, 1},
00477       {"nh", no_argument, &inhibit_home_gdbinit, 1},
00478       {"nx", no_argument, &inhibit_gdbinit, 1},
00479       {"n", no_argument, &inhibit_gdbinit, 1},
00480       {"batch-silent", no_argument, 0, 'B'},
00481       {"batch", no_argument, &batch_flag, 1},
00482 
00483     /* This is a synonym for "--annotate=1".  --annotate is now
00484        preferred, but keep this here for a long time because people
00485        will be running emacses which use --fullname.  */
00486       {"fullname", no_argument, 0, 'f'},
00487       {"f", no_argument, 0, 'f'},
00488 
00489       {"annotate", required_argument, 0, OPT_ANNOTATE},
00490       {"help", no_argument, &print_help, 1},
00491       {"se", required_argument, 0, OPT_SE},
00492       {"symbols", required_argument, 0, 's'},
00493       {"s", required_argument, 0, 's'},
00494       {"exec", required_argument, 0, 'e'},
00495       {"e", required_argument, 0, 'e'},
00496       {"core", required_argument, 0, 'c'},
00497       {"c", required_argument, 0, 'c'},
00498       {"pid", required_argument, 0, 'p'},
00499       {"p", required_argument, 0, 'p'},
00500       {"command", required_argument, 0, 'x'},
00501       {"eval-command", required_argument, 0, 'X'},
00502       {"version", no_argument, &print_version, 1},
00503       {"configuration", no_argument, &print_configuration, 1},
00504       {"x", required_argument, 0, 'x'},
00505       {"ex", required_argument, 0, 'X'},
00506       {"init-command", required_argument, 0, OPT_IX},
00507       {"init-eval-command", required_argument, 0, OPT_IEX},
00508       {"ix", required_argument, 0, OPT_IX},
00509       {"iex", required_argument, 0, OPT_IEX},
00510 #ifdef GDBTK
00511       {"tclcommand", required_argument, 0, 'z'},
00512       {"enable-external-editor", no_argument, 0, 'y'},
00513       {"editor-command", required_argument, 0, 'w'},
00514 #endif
00515       {"ui", required_argument, 0, 'i'},
00516       {"interpreter", required_argument, 0, 'i'},
00517       {"i", required_argument, 0, 'i'},
00518       {"directory", required_argument, 0, 'd'},
00519       {"d", required_argument, 0, 'd'},
00520       {"data-directory", required_argument, 0, 'D'},
00521       {"cd", required_argument, 0, OPT_CD},
00522       {"tty", required_argument, 0, 't'},
00523       {"baud", required_argument, 0, 'b'},
00524       {"b", required_argument, 0, 'b'},
00525       {"nw", no_argument, NULL, OPT_NOWINDOWS},
00526       {"nowindows", no_argument, NULL, OPT_NOWINDOWS},
00527       {"w", no_argument, NULL, OPT_WINDOWS},
00528       {"windows", no_argument, NULL, OPT_WINDOWS},
00529       {"statistics", no_argument, 0, OPT_STATISTICS},
00530       {"write", no_argument, &write_files, 1},
00531       {"args", no_argument, &set_args, 1},
00532       {"l", required_argument, 0, 'l'},
00533       {"return-child-result", no_argument, &return_child_result, 1},
00534       {0, no_argument, 0, 0}
00535     };
00536 
00537     while (1)
00538       {
00539         int option_index;
00540 
00541         c = getopt_long_only (argc, argv, "",
00542                               long_options, &option_index);
00543         if (c == EOF || set_args)
00544           break;
00545 
00546         /* Long option that takes an argument.  */
00547         if (c == 0 && long_options[option_index].flag == 0)
00548           c = long_options[option_index].val;
00549 
00550         switch (c)
00551           {
00552           case 0:
00553             /* Long option that just sets a flag.  */
00554             break;
00555           case OPT_SE:
00556             symarg = optarg;
00557             execarg = optarg;
00558             break;
00559           case OPT_CD:
00560             cdarg = optarg;
00561             break;
00562           case OPT_ANNOTATE:
00563             /* FIXME: what if the syntax is wrong (e.g. not digits)?  */
00564             annotation_level = atoi (optarg);
00565             break;
00566           case OPT_STATISTICS:
00567             /* Enable the display of both time and space usage.  */
00568             set_per_command_time (1);
00569             set_per_command_space (1);
00570             break;
00571           case OPT_TUI:
00572             /* --tui is equivalent to -i=tui.  */
00573 #ifdef TUI
00574             xfree (interpreter_p);
00575             interpreter_p = xstrdup (INTERP_TUI);
00576 #else
00577             fprintf_unfiltered (gdb_stderr,
00578                                 _("%s: TUI mode is not supported\n"),
00579                                 argv[0]);
00580             exit (1);
00581 #endif
00582             break;
00583           case OPT_WINDOWS:
00584             /* FIXME: cagney/2003-03-01: Not sure if this option is
00585                actually useful, and if it is, what it should do.  */
00586 #ifdef GDBTK
00587             /* --windows is equivalent to -i=insight.  */
00588             xfree (interpreter_p);
00589             interpreter_p = xstrdup (INTERP_INSIGHT);
00590 #endif
00591             use_windows = 1;
00592             break;
00593           case OPT_NOWINDOWS:
00594             /* -nw is equivalent to -i=console.  */
00595             xfree (interpreter_p);
00596             interpreter_p = xstrdup (INTERP_CONSOLE);
00597             use_windows = 0;
00598             break;
00599           case 'f':
00600             annotation_level = 1;
00601             /* We have probably been invoked from emacs.  Disable
00602                window interface.  */
00603             use_windows = 0;
00604             break;
00605           case 's':
00606             symarg = optarg;
00607             break;
00608           case 'e':
00609             execarg = optarg;
00610             break;
00611           case 'c':
00612             corearg = optarg;
00613             break;
00614           case 'p':
00615             pidarg = optarg;
00616             break;
00617           case 'x':
00618             {
00619               struct cmdarg cmdarg = { CMDARG_FILE, optarg };
00620 
00621               VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
00622             }
00623             break;
00624           case 'X':
00625             {
00626               struct cmdarg cmdarg = { CMDARG_COMMAND, optarg };
00627 
00628               VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
00629             }
00630             break;
00631           case OPT_IX:
00632             {
00633               struct cmdarg cmdarg = { CMDARG_INIT_FILE, optarg };
00634 
00635               VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
00636             }
00637             break;
00638           case OPT_IEX:
00639             {
00640               struct cmdarg cmdarg = { CMDARG_INIT_COMMAND, optarg };
00641 
00642               VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg);
00643             }
00644             break;
00645           case 'B':
00646             batch_flag = batch_silent = 1;
00647             gdb_stdout = ui_file_new();
00648             break;
00649           case 'D':
00650             xfree (gdb_datadir);
00651             gdb_datadir = xstrdup (optarg);
00652             gdb_datadir_provided = 1;
00653             break;
00654 #ifdef GDBTK
00655           case 'z':
00656             {
00657               extern int gdbtk_test (char *);
00658 
00659               if (!gdbtk_test (optarg))
00660                 {
00661                   fprintf_unfiltered (gdb_stderr,
00662                                       _("%s: unable to load "
00663                                         "tclcommand file \"%s\""),
00664                                       argv[0], optarg);
00665                   exit (1);
00666                 }
00667               break;
00668             }
00669           case 'y':
00670             /* Backwards compatibility only.  */
00671             break;
00672           case 'w':
00673             {
00674               /* Set the external editor commands when gdb is farming out files
00675                  to be edited by another program.  */
00676               extern char *external_editor_command;
00677 
00678               external_editor_command = xstrdup (optarg);
00679               break;
00680             }
00681 #endif /* GDBTK */
00682           case 'i':
00683             xfree (interpreter_p);
00684             interpreter_p = xstrdup (optarg);
00685             break;
00686           case 'd':
00687             dirarg[ndir++] = optarg;
00688             if (ndir >= dirsize)
00689               {
00690                 dirsize *= 2;
00691                 dirarg = (char **) xrealloc ((char *) dirarg,
00692                                              dirsize * sizeof (*dirarg));
00693               }
00694             break;
00695           case 't':
00696             ttyarg = optarg;
00697             break;
00698           case 'q':
00699             quiet = 1;
00700             break;
00701           case 'b':
00702             {
00703               int i;
00704               char *p;
00705 
00706               i = strtol (optarg, &p, 0);
00707               if (i == 0 && p == optarg)
00708 
00709                 /* Don't use *_filtered or warning() (which relies on
00710                    current_target) until after initialize_all_files().  */
00711 
00712                 fprintf_unfiltered
00713                   (gdb_stderr,
00714                    _("warning: could not set baud rate to `%s'.\n"), optarg);
00715               else
00716                 baud_rate = i;
00717             }
00718             break;
00719           case 'l':
00720             {
00721               int i;
00722               char *p;
00723 
00724               i = strtol (optarg, &p, 0);
00725               if (i == 0 && p == optarg)
00726 
00727                 /* Don't use *_filtered or warning() (which relies on
00728                    current_target) until after initialize_all_files().  */
00729 
00730                 fprintf_unfiltered (gdb_stderr,
00731                                     _("warning: could not set "
00732                                       "timeout limit to `%s'.\n"), optarg);
00733               else
00734                 remote_timeout = i;
00735             }
00736             break;
00737 
00738           case '?':
00739             fprintf_unfiltered (gdb_stderr,
00740                                 _("Use `%s --help' for a "
00741                                   "complete list of options.\n"),
00742                                 argv[0]);
00743             exit (1);
00744           }
00745       }
00746 
00747     /* If --help or --version or --configuration, disable window
00748        interface.  */
00749     if (print_help || print_version || print_configuration)
00750       {
00751         use_windows = 0;
00752       }
00753 
00754     if (batch_flag)
00755       quiet = 1;
00756   }
00757 
00758   /* Initialize all files.  Give the interpreter a chance to take
00759      control of the console via the deprecated_init_ui_hook ().  */
00760   gdb_init (gdb_program_name);
00761 
00762   /* Now that gdb_init has created the initial inferior, we're in
00763      position to set args for that inferior.  */
00764   if (set_args)
00765     {
00766       /* The remaining options are the command-line options for the
00767          inferior.  The first one is the sym/exec file, and the rest
00768          are arguments.  */
00769       if (optind >= argc)
00770         {
00771           fprintf_unfiltered (gdb_stderr,
00772                               _("%s: `--args' specified but "
00773                                 "no program specified\n"),
00774                               argv[0]);
00775           exit (1);
00776         }
00777       symarg = argv[optind];
00778       execarg = argv[optind];
00779       ++optind;
00780       set_inferior_args_vector (argc - optind, &argv[optind]);
00781     }
00782   else
00783     {
00784       /* OK, that's all the options.  */
00785 
00786       /* The first argument, if specified, is the name of the
00787          executable.  */
00788       if (optind < argc)
00789         {
00790           symarg = argv[optind];
00791           execarg = argv[optind];
00792           optind++;
00793         }
00794 
00795       /* If the user hasn't already specified a PID or the name of a
00796          core file, then a second optional argument is allowed.  If
00797          present, this argument should be interpreted as either a
00798          PID or a core file, whichever works.  */
00799       if (pidarg == NULL && corearg == NULL && optind < argc)
00800         {
00801           pid_or_core_arg = argv[optind];
00802           optind++;
00803         }
00804 
00805       /* Any argument left on the command line is unexpected and
00806          will be ignored.  Inform the user.  */
00807       if (optind < argc)
00808         fprintf_unfiltered (gdb_stderr,
00809                             _("Excess command line "
00810                               "arguments ignored. (%s%s)\n"),
00811                             argv[optind],
00812                             (optind == argc - 1) ? "" : " ...");
00813     }
00814 
00815   /* Lookup gdbinit files.  Note that the gdbinit file name may be
00816      overriden during file initialization, so get_init_files should be
00817      called after gdb_init.  */
00818   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
00819 
00820   /* Do these (and anything which might call wrap_here or *_filtered)
00821      after initialize_all_files() but before the interpreter has been
00822      installed.  Otherwize the help/version messages will be eaten by
00823      the interpreter's output handler.  */
00824 
00825   if (print_version)
00826     {
00827       print_gdb_version (gdb_stdout);
00828       wrap_here ("");
00829       printf_filtered ("\n");
00830       exit (0);
00831     }
00832 
00833   if (print_help)
00834     {
00835       print_gdb_help (gdb_stdout);
00836       fputs_unfiltered ("\n", gdb_stdout);
00837       exit (0);
00838     }
00839 
00840   if (print_configuration)
00841     {
00842       print_gdb_configuration (gdb_stdout);
00843       wrap_here ("");
00844       printf_filtered ("\n");
00845       exit (0);
00846     }
00847 
00848   /* FIXME: cagney/2003-02-03: The big hack (part 1 of 2) that lets
00849      GDB retain the old MI1 interpreter startup behavior.  Output the
00850      copyright message before the interpreter is installed.  That way
00851      it isn't encapsulated in MI output.  */
00852   if (!quiet && strcmp (interpreter_p, INTERP_MI1) == 0)
00853     {
00854       /* Print all the junk at the top, with trailing "..." if we are
00855          about to read a symbol file (possibly slowly).  */
00856       print_gdb_version (gdb_stdout);
00857       if (symarg)
00858         printf_filtered ("..");
00859       wrap_here ("");
00860       printf_filtered ("\n");
00861       gdb_flush (gdb_stdout);   /* Force to screen during slow
00862                                    operations.  */
00863     }
00864 
00865   /* Install the default UI.  All the interpreters should have had a
00866      look at things by now.  Initialize the default interpreter.  */
00867 
00868   {
00869     /* Find it.  */
00870     struct interp *interp = interp_lookup (interpreter_p);
00871 
00872     if (interp == NULL)
00873       error (_("Interpreter `%s' unrecognized"), interpreter_p);
00874     /* Install it.  */
00875     if (!interp_set (interp, 1))
00876       {
00877         fprintf_unfiltered (gdb_stderr,
00878                             "Interpreter `%s' failed to initialize.\n",
00879                             interpreter_p);
00880         exit (1);
00881       }
00882   }
00883 
00884   /* FIXME: cagney/2003-02-03: The big hack (part 2 of 2) that lets
00885      GDB retain the old MI1 interpreter startup behavior.  Output the
00886      copyright message after the interpreter is installed when it is
00887      any sane interpreter.  */
00888   if (!quiet && !current_interp_named_p (INTERP_MI1))
00889     {
00890       /* Print all the junk at the top, with trailing "..." if we are
00891          about to read a symbol file (possibly slowly).  */
00892       print_gdb_version (gdb_stdout);
00893       if (symarg)
00894         printf_filtered ("..");
00895       wrap_here ("");
00896       printf_filtered ("\n");
00897       gdb_flush (gdb_stdout);   /* Force to screen during slow
00898                                    operations.  */
00899     }
00900 
00901   /* Set off error and warning messages with a blank line.  */
00902   warning_pre_print = _("\nwarning: ");
00903 
00904   /* Read and execute the system-wide gdbinit file, if it exists.
00905      This is done *before* all the command line arguments are
00906      processed; it sets global parameters, which are independent of
00907      what file you are debugging or what directory you are in.  */
00908   if (system_gdbinit && !inhibit_gdbinit)
00909     catch_command_errors_const (source_script, system_gdbinit,
00910                                 0, RETURN_MASK_ALL);
00911 
00912   /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
00913      *before* all the command line arguments are processed; it sets
00914      global parameters, which are independent of what file you are
00915      debugging or what directory you are in.  */
00916 
00917   if (home_gdbinit && !inhibit_gdbinit && !inhibit_home_gdbinit)
00918     catch_command_errors_const (source_script,
00919                                 home_gdbinit, 0, RETURN_MASK_ALL);
00920 
00921   /* Process '-ix' and '-iex' options early.  */
00922   for (i = 0; VEC_iterate (cmdarg_s, cmdarg_vec, i, cmdarg_p); i++)
00923     switch (cmdarg_p->type)
00924     {
00925       case CMDARG_INIT_FILE:
00926         catch_command_errors_const (source_script, cmdarg_p->string,
00927                                     !batch_flag, RETURN_MASK_ALL);
00928         break;
00929       case CMDARG_INIT_COMMAND:
00930         catch_command_errors (execute_command, cmdarg_p->string,
00931                               !batch_flag, RETURN_MASK_ALL);
00932         break;
00933     }
00934 
00935   /* Now perform all the actions indicated by the arguments.  */
00936   if (cdarg != NULL)
00937     {
00938       catch_command_errors (cd_command, cdarg, 0, RETURN_MASK_ALL);
00939     }
00940 
00941   for (i = 0; i < ndir; i++)
00942     catch_command_errors (directory_switch, dirarg[i], 0, RETURN_MASK_ALL);
00943   xfree (dirarg);
00944 
00945   /* Skip auto-loading section-specified scripts until we've sourced
00946      local_gdbinit (which is often used to augment the source search
00947      path).  */
00948   save_auto_load = global_auto_load;
00949   global_auto_load = 0;
00950 
00951   if (execarg != NULL
00952       && symarg != NULL
00953       && strcmp (execarg, symarg) == 0)
00954     {
00955       /* The exec file and the symbol-file are the same.  If we can't
00956          open it, better only print one error message.
00957          catch_command_errors returns non-zero on success!  */
00958       if (catch_command_errors (exec_file_attach, execarg,
00959                                 !batch_flag, RETURN_MASK_ALL))
00960         catch_command_errors_const (symbol_file_add_main, symarg,
00961                                     !batch_flag, RETURN_MASK_ALL);
00962     }
00963   else
00964     {
00965       if (execarg != NULL)
00966         catch_command_errors (exec_file_attach, execarg,
00967                               !batch_flag, RETURN_MASK_ALL);
00968       if (symarg != NULL)
00969         catch_command_errors_const (symbol_file_add_main, symarg,
00970                                     !batch_flag, RETURN_MASK_ALL);
00971     }
00972 
00973   if (corearg && pidarg)
00974     error (_("Can't attach to process and specify "
00975              "a core file at the same time."));
00976 
00977   if (corearg != NULL)
00978     catch_command_errors (core_file_command, corearg,
00979                           !batch_flag, RETURN_MASK_ALL);
00980   else if (pidarg != NULL)
00981     catch_command_errors (attach_command, pidarg,
00982                           !batch_flag, RETURN_MASK_ALL);
00983   else if (pid_or_core_arg)
00984     {
00985       /* The user specified 'gdb program pid' or gdb program core'.
00986          If pid_or_core_arg's first character is a digit, try attach
00987          first and then corefile.  Otherwise try just corefile.  */
00988 
00989       if (isdigit (pid_or_core_arg[0]))
00990         {
00991           if (catch_command_errors (attach_command, pid_or_core_arg,
00992                                     !batch_flag, RETURN_MASK_ALL) == 0)
00993             catch_command_errors (core_file_command, pid_or_core_arg,
00994                                   !batch_flag, RETURN_MASK_ALL);
00995         }
00996       else /* Can't be a pid, better be a corefile.  */
00997         catch_command_errors (core_file_command, pid_or_core_arg,
00998                               !batch_flag, RETURN_MASK_ALL);
00999     }
01000 
01001   if (ttyarg != NULL)
01002     set_inferior_io_terminal (ttyarg);
01003 
01004   /* Error messages should no longer be distinguished with extra output.  */
01005   warning_pre_print = _("warning: ");
01006 
01007   /* Read the .gdbinit file in the current directory, *if* it isn't
01008      the same as the $HOME/.gdbinit file (it should exist, also).  */
01009   if (local_gdbinit)
01010     {
01011       auto_load_local_gdbinit_pathname = gdb_realpath (local_gdbinit);
01012 
01013       if (!inhibit_gdbinit && auto_load_local_gdbinit
01014           && file_is_auto_load_safe (local_gdbinit,
01015                                      _("auto-load: Loading .gdbinit "
01016                                        "file \"%s\".\n"),
01017                                      local_gdbinit))
01018         {
01019           auto_load_local_gdbinit_loaded = 1;
01020 
01021           catch_command_errors_const (source_script, local_gdbinit, 0,
01022                                       RETURN_MASK_ALL);
01023         }
01024     }
01025 
01026   /* Now that all .gdbinit's have been read and all -d options have been
01027      processed, we can read any scripts mentioned in SYMARG.
01028      We wait until now because it is common to add to the source search
01029      path in local_gdbinit.  */
01030   global_auto_load = save_auto_load;
01031   ALL_OBJFILES (objfile)
01032     load_auto_scripts_for_objfile (objfile);
01033 
01034   /* Process '-x' and '-ex' options.  */
01035   for (i = 0; VEC_iterate (cmdarg_s, cmdarg_vec, i, cmdarg_p); i++)
01036     switch (cmdarg_p->type)
01037     {
01038       case CMDARG_FILE:
01039         catch_command_errors_const (source_script, cmdarg_p->string,
01040                                     !batch_flag, RETURN_MASK_ALL);
01041         break;
01042       case CMDARG_COMMAND:
01043         catch_command_errors (execute_command, cmdarg_p->string,
01044                               !batch_flag, RETURN_MASK_ALL);
01045         break;
01046     }
01047 
01048   /* Read in the old history after all the command files have been
01049      read.  */
01050   init_history ();
01051 
01052   if (batch_flag)
01053     {
01054       /* We have hit the end of the batch file.  */
01055       quit_force (NULL, 0);
01056     }
01057 
01058   /* Show time and/or space usage.  */
01059   do_cleanups (pre_stat_chain);
01060 
01061   /* NOTE: cagney/1999-11-07: There is probably no reason for not
01062      moving this loop and the code found in captured_command_loop()
01063      into the command_loop() proper.  The main thing holding back that
01064      change - SET_TOP_LEVEL() - has been eliminated.  */
01065   while (1)
01066     {
01067       catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
01068     }
01069   /* No exit -- exit is through quit_command.  */
01070 }
01071 
01072 int
01073 gdb_main (struct captured_main_args *args)
01074 {
01075   use_windows = args->use_windows;
01076   catch_errors (captured_main, args, "", RETURN_MASK_ALL);
01077   /* The only way to end up here is by an error (normal exit is
01078      handled by quit_force()), hence always return an error status.  */
01079   return 1;
01080 }
01081 
01082 
01083 /* Don't use *_filtered for printing help.  We don't want to prompt
01084    for continue no matter how small the screen or how much we're going
01085    to print.  */
01086 
01087 static void
01088 print_gdb_help (struct ui_file *stream)
01089 {
01090   const char *system_gdbinit;
01091   const char *home_gdbinit;
01092   const char *local_gdbinit;
01093 
01094   get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
01095 
01096   /* Note: The options in the list below are only approximately sorted
01097      in the alphabetical order, so as to group closely related options
01098      together.  */
01099   fputs_unfiltered (_("\
01100 This is the GNU debugger.  Usage:\n\n\
01101     gdb [options] [executable-file [core-file or process-id]]\n\
01102     gdb [options] --args executable-file [inferior-arguments ...]\n\n\
01103 "), stream);
01104   fputs_unfiltered (_("\
01105 Selection of debuggee and its files:\n\n\
01106   --args             Arguments after executable-file are passed to inferior\n\
01107   --core=COREFILE    Analyze the core dump COREFILE.\n\
01108   --exec=EXECFILE    Use EXECFILE as the executable.\n\
01109   --pid=PID          Attach to running process PID.\n\
01110   --directory=DIR    Search for source files in DIR.\n\
01111   --se=FILE          Use FILE as symbol file and executable file.\n\
01112   --symbols=SYMFILE  Read symbols from SYMFILE.\n\
01113   --readnow          Fully read symbol files on first access.\n\
01114   --write            Set writing into executable and core files.\n\n\
01115 "), stream);
01116   fputs_unfiltered (_("\
01117 Initial commands and command files:\n\n\
01118   --command=FILE, -x Execute GDB commands from FILE.\n\
01119   --init-command=FILE, -ix\n\
01120                      Like -x but execute commands before loading inferior.\n\
01121   --eval-command=COMMAND, -ex\n\
01122                      Execute a single GDB command.\n\
01123                      May be used multiple times and in conjunction\n\
01124                      with --command.\n\
01125   --init-eval-command=COMMAND, -iex\n\
01126                      Like -ex but before loading inferior.\n\
01127   --nh               Do not read ~/.gdbinit.\n\
01128   --nx               Do not read any .gdbinit files in any directory.\n\n\
01129 "), stream);
01130   fputs_unfiltered (_("\
01131 Output and user interface control:\n\n\
01132   --fullname         Output information used by emacs-GDB interface.\n\
01133   --interpreter=INTERP\n\
01134                      Select a specific interpreter / user interface\n\
01135   --tty=TTY          Use TTY for input/output by the program being debugged.\n\
01136   -w                 Use the GUI interface.\n\
01137   --nw               Do not use the GUI interface.\n\
01138 "), stream);
01139 #if defined(TUI)
01140   fputs_unfiltered (_("\
01141   --tui              Use a terminal user interface.\n\
01142 "), stream);
01143 #endif
01144   fputs_unfiltered (_("\
01145   --dbx              DBX compatibility mode.\n\
01146   --xdb              XDB compatibility mode.\n\
01147   --quiet            Do not print version number on startup.\n\n\
01148 "), stream);
01149   fputs_unfiltered (_("\
01150 Operating modes:\n\n\
01151   --batch            Exit after processing options.\n\
01152   --batch-silent     Like --batch, but suppress all gdb stdout output.\n\
01153   --return-child-result\n\
01154                      GDB exit code will be the child's exit code.\n\
01155   --configuration    Print details about GDB configuration and then exit.\n\
01156   --help             Print this message and then exit.\n\
01157   --version          Print version information and then exit.\n\n\
01158 Remote debugging options:\n\n\
01159   -b BAUDRATE        Set serial port baud rate used for remote debugging.\n\
01160   -l TIMEOUT         Set timeout in seconds for remote debugging.\n\n\
01161 Other options:\n\n\
01162   --cd=DIR           Change current directory to DIR.\n\
01163 "), stream);
01164   fputs_unfiltered (_("\n\
01165 At startup, GDB reads the following init files and executes their commands:\n\
01166 "), stream);
01167   if (system_gdbinit)
01168     fprintf_unfiltered (stream, _("\
01169    * system-wide init file: %s\n\
01170 "), system_gdbinit);
01171   if (home_gdbinit)
01172     fprintf_unfiltered (stream, _("\
01173    * user-specific init file: %s\n\
01174 "), home_gdbinit);
01175   if (local_gdbinit)
01176     fprintf_unfiltered (stream, _("\
01177    * local init file (see also 'set auto-load local-gdbinit'): ./%s\n\
01178 "), local_gdbinit);
01179   fputs_unfiltered (_("\n\
01180 For more information, type \"help\" from within GDB, or consult the\n\
01181 GDB manual (available as on-line info or a printed manual).\n\
01182 "), stream);
01183   if (REPORT_BUGS_TO[0] && stream == gdb_stdout)
01184     fprintf_unfiltered (stream, _("\
01185 Report bugs to \"%s\".\n\
01186 "), REPORT_BUGS_TO);
01187 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines