2013-06-09 Sandra Loosemore <sandra@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / main.c
index 923a7fea04029297fc86f0af2b980c3c156da94d..3c1ef759462b24c23a7eb2cbb4930608af0d099f 100644 (file)
@@ -1,6 +1,6 @@
 /* Top level stuff for GDB, the GNU debugger.
 
-   Copyright (C) 1986-2005, 2007-2012 Free Software Foundation, Inc.
+   Copyright (C) 1986-2013 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "python/python.h"
 #include "objfiles.h"
 #include "auto-load.h"
+#include "maint.h"
+
+#include "filenames.h"
+#include "filestuff.h"
 
 /* The selected interpreter.  This will be used as a set command
    variable, so it should always be malloc'ed - since
@@ -60,6 +64,11 @@ char *gdb_sysroot = 0;
 /* GDB datadir, used to store data files.  */
 char *gdb_datadir = 0;
 
+/* Non-zero if GDB_DATADIR was provided on the command line.
+   This doesn't track whether data-directory is set later from the
+   command line, but we don't reread system.gdbinit when that happens.  */
+static int gdb_datadir_provided = 0;
+
 /* If gdb was configured with --with-python=/path,
    the possibly relocated path to python's lib directory.  */
 char *python_libdir = 0;
@@ -121,7 +130,7 @@ relocate_gdb_directory (const char *initial, int flag)
     {
       struct stat s;
 
-      if (stat (dir, &s) != 0 || !S_ISDIR (s.st_mode))
+      if (*dir == '\0' || stat (dir, &s) != 0 || !S_ISDIR (s.st_mode))
        {
          xfree (dir);
          dir = NULL;
@@ -163,13 +172,38 @@ get_init_files (char **system_gdbinit,
   if (!initialized)
     {
       struct stat homebuf, cwdbuf, s;
-      char *homedir, *relocated_sysgdbinit;
+      char *homedir;
 
       if (SYSTEM_GDBINIT[0])
        {
-         relocated_sysgdbinit = relocate_path (gdb_program_name,
-                                               SYSTEM_GDBINIT,
-                                               SYSTEM_GDBINIT_RELOCATABLE);
+         int datadir_len = strlen (GDB_DATADIR);
+         int sys_gdbinit_len = strlen (SYSTEM_GDBINIT);
+         char *relocated_sysgdbinit;
+
+         /* If SYSTEM_GDBINIT lives in data-directory, and data-directory
+            has been provided, search for SYSTEM_GDBINIT there.  */
+         if (gdb_datadir_provided
+             && datadir_len < sys_gdbinit_len
+             && filename_ncmp (SYSTEM_GDBINIT, GDB_DATADIR, datadir_len) == 0
+             && IS_DIR_SEPARATOR (SYSTEM_GDBINIT[datadir_len]))
+           {
+             /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
+                to gdb_datadir.  */
+             char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
+             char *p;
+
+             for (p = tmp_sys_gdbinit; IS_DIR_SEPARATOR (*p); ++p)
+               continue;
+             relocated_sysgdbinit = concat (gdb_datadir, SLASH_STRING, p,
+                                            NULL);
+             xfree (tmp_sys_gdbinit);
+           }
+         else
+           {
+             relocated_sysgdbinit = relocate_path (gdb_program_name,
+                                                   SYSTEM_GDBINIT,
+                                                   SYSTEM_GDBINIT_RELOCATABLE);
+           }
          if (relocated_sysgdbinit && stat (relocated_sysgdbinit, &s) == 0)
            sysgdbinit = relocated_sysgdbinit;
          else
@@ -288,6 +322,7 @@ captured_main (void *data)
      initializer.  */
   static int print_help;
   static int print_version;
+  static int print_configuration;
 
   /* Pointers to all arguments of --command option.  */
   VEC (cmdarg_s) *cmdarg_vec = NULL;
@@ -327,6 +362,9 @@ captured_main (void *data)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
+  bfd_init ();
+  notice_open_fds ();
+
   make_cleanup (VEC_cleanup (cmdarg_s), &cmdarg_vec);
   dirsize = 1;
   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
@@ -345,7 +383,13 @@ captured_main (void *data)
   gdb_stdtargerr = gdb_stderr; /* for moment */
   gdb_stdtargin = gdb_stdin;   /* for moment */
 
+#ifdef __MINGW32__
+  /* On Windows, argv[0] is not necessarily set to absolute form when
+     GDB is found along PATH, without which relocation doesn't work.  */
+  gdb_program_name = windows_get_absolute_argv0 (argv[0]);
+#else
   gdb_program_name = xstrdup (argv[0]);
+#endif
 
   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
     /* Don't use *_filtered or warning() (which relies on
@@ -379,7 +423,7 @@ captured_main (void *data)
 
 #ifdef RELOC_SRCDIR
   add_substitute_path_rule (RELOC_SRCDIR,
-                           make_relative_prefix (argv[0], BINDIR,
+                           make_relative_prefix (gdb_program_name, BINDIR,
                                                  RELOC_SRCDIR));
 #endif
 
@@ -421,7 +465,6 @@ captured_main (void *data)
       {"n", no_argument, &inhibit_gdbinit, 1},
       {"batch-silent", no_argument, 0, 'B'},
       {"batch", no_argument, &batch_flag, 1},
-      {"epoch", no_argument, &epoch_interface, 1},
 
     /* This is a synonym for "--annotate=1".  --annotate is now
        preferred, but keep this here for a long time because people
@@ -443,6 +486,7 @@ captured_main (void *data)
       {"command", required_argument, 0, 'x'},
       {"eval-command", required_argument, 0, 'X'},
       {"version", no_argument, &print_version, 1},
+      {"configuration", no_argument, &print_configuration, 1},
       {"x", required_argument, 0, 'x'},
       {"ex", required_argument, 0, 'X'},
       {"init-command", required_argument, 0, OPT_IX},
@@ -507,8 +551,8 @@ captured_main (void *data)
            break;
          case OPT_STATISTICS:
            /* Enable the display of both time and space usage.  */
-           set_display_time (1);
-           set_display_space (1);
+           set_per_command_time (1);
+           set_per_command_space (1);
            break;
          case OPT_TUI:
            /* --tui is equivalent to -i=tui.  */
@@ -591,6 +635,7 @@ captured_main (void *data)
          case 'D':
            xfree (gdb_datadir);
            gdb_datadir = xstrdup (optarg);
+           gdb_datadir_provided = 1;
            break;
 #ifdef GDBTK
          case 'z':
@@ -685,8 +730,9 @@ captured_main (void *data)
          }
       }
 
-    /* If --help or --version, disable window interface.  */
-    if (print_help || print_version)
+    /* If --help or --version or --configuration, disable window
+       interface.  */
+    if (print_help || print_version || print_configuration)
       {
        use_windows = 0;
       }
@@ -697,7 +743,7 @@ captured_main (void *data)
 
   /* Initialize all files.  Give the interpreter a chance to take
      control of the console via the deprecated_init_ui_hook ().  */
-  gdb_init (argv[0]);
+  gdb_init (gdb_program_name);
 
   /* Now that gdb_init has created the initial inferior, we're in
      position to set args for that inferior.  */
@@ -777,6 +823,14 @@ captured_main (void *data)
       exit (0);
     }
 
+  if (print_configuration)
+    {
+      print_gdb_configuration (gdb_stdout);
+      wrap_here ("");
+      printf_filtered ("\n");
+      exit (0);
+    }
+
   /* FIXME: cagney/2003-02-03: The big hack (part 1 of 2) that lets
      GDB retain the old MI1 interpreter startup behavior.  Output the
      copyright message before the interpreter is installed.  That way
@@ -1056,7 +1110,6 @@ Options:\n\n\
   fputs_unfiltered (_("\
   --dbx              DBX compatibility mode.\n\
   --directory=DIR    Search for source files in DIR.\n\
-  --epoch            Output information used by epoch emacs-GDB interface.\n\
   --exec=EXECFILE    Use EXECFILE as the executable.\n\
   --fullname         Output information used by emacs-GDB interface.\n\
   --help             Print this message.\n\
@@ -1089,6 +1142,7 @@ Options:\n\n\
 #endif
   fputs_unfiltered (_("\
   --version          Print version information and then exit.\n\
+  --configuration    Print details about GDB configuration and then exit.\n\
   -w                 Use a window interface.\n\
   --write            Set writing into executable and core files.\n\
   --xdb              XDB compatibility mode.\n\
This page took 0.027244 seconds and 4 git commands to generate.