This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Add support for --with-system-gdbinit


Eli Zaretskii (eliz@gnu.org):

> > Date: Mon, 19 Jan 2009 15:10:53 +0100
> > From: Jerome Guitton <guitton@adacore.com>
> > 
> > At startup, GDB reads the following init files and executes their commands:
> >   * system-wide init file: /etc/gdb/gdbinit
> >   * user-specific init file: /homes/guitton/.gdbinit
> >   * local init file: /homes/guitton/work/.gdbinit
> > 
> > For more information, type "help" from within GDB, or consult the
> > GDB manual (available as on-line info or a printed manual).
> > [...]
> > 
> > 
> > Would that make sense?
> 
> Yes, with changes suggested by Daniel.


Here is a patch that takes that into account and adds some
documentation about the relocation. Is it OK to apply?


2009-01-20  Daniel Jacobowitz  <dan@codesourcery.com>
	    Jerome Guitton  <guitton@adacore.com>

	* configure, config.in: Regenerated.
	* configure.ac: Add --with-system-gdbinit.
	* main.c (gdb_program_name): New static variable.
	(get_init_files): New function.
	(captured_main): Use get_init_files. Load system gdbinit before
	$HOME/.gdbinit.
	(print_gdb_help): Print location of init files.

2009-01-20  Daniel Jacobowitz  <dan@codesourcery.com>
	    Jerome Guitton  <guitton@adacore.com>

	* gdb.texinfo (Startup): Document --with-system-gdbinit.
	(System-wide configuration): New section.


Index: configure
===================================================================
RCS file: /cvs/src/src/gdb/configure,v
retrieving revision 1.264
diff -u -p -r1.264 configure
--- configure	12 Jan 2009 01:10:27 -0000	1.264
+++ configure	21 Jan 2009 10:51:58 -0000
@@ -895,6 +895,7 @@ Optional Packages:
                           on systems with version 2 of the GNU C library
                           (use with caution on other system)
   --with-sysroot=DIR Search for usr/lib et al within DIR.
+  --with-system-gdbinit=file       Automatically load a system-wide gdbinit file
   --with-tcl              directory containing tcl configuration (tclConfig.sh)
   --with-tk               directory containing tk configuration (tkConfig.sh)
   --with-x                use the X Window System
@@ -19482,6 +19483,49 @@ fi;
 
 
 
+system_gdbinit=
+
+# Check whether --with-system-gdbinit or --without-system-gdbinit was given.
+if test "${with_system_gdbinit+set}" = set; then
+  withval="$with_system_gdbinit"
+  system_gdbinit=${withval}
+fi;
+
+
+  test "x$prefix" = xNONE && prefix="$ac_default_prefix"
+  test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
+  ac_define_dir=`eval echo $system_gdbinit`
+  ac_define_dir=`eval echo $ac_define_dir`
+
+cat >>confdefs.h <<_ACEOF
+#define SYSTEM_GDBINIT "$ac_define_dir"
+_ACEOF
+
+
+
+if test "x$prefix" = xNONE; then
+  test_prefix=$ac_default_prefix
+else
+  test_prefix=$prefix
+fi
+if test "x$exec_prefix" = xNONE || test "x$exec_prefix" = 'x${prefix}'; then
+  test_exec_prefix=$test_prefix
+else
+  test_exec_prefix=$exec_prefix
+fi
+case ${system_gdbinit} in
+ "${test_prefix}"|"${test_prefix}/"*|\
+ "${test_exec_prefix}"|"${test_exec_prefix}/"*|\
+ '${prefix}'|'${prefix}/'*|\
+ '${exec_prefix}'|'${exec_prefix}/'*)
+
+cat >>confdefs.h <<\_ACEOF
+#define SYSTEM_GDBINIT_RELOCATABLE 1
+_ACEOF
+
+   ;;
+esac
+
 # Check whether --enable-werror or --disable-werror was given.
 if test "${enable_werror+set}" = set; then
   enableval="$enable_werror"
Index: config.in
===================================================================
RCS file: /cvs/src/src/gdb/config.in,v
retrieving revision 1.104
diff -u -p -r1.104 config.in
--- config.in	9 Dec 2008 17:18:29 -0000	1.104
+++ config.in	21 Jan 2009 10:51:58 -0000
@@ -655,6 +655,13 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* System-wide gdbinit file. */
+#undef SYSTEM_GDBINIT
+
+/* Define if the system-wide gdbinit file should be relocated when GDB is
+   moved. */
+#undef SYSTEM_GDBINIT_RELOCATABLE
+
 /* Define if <thread_db.h> has the TD_NOTALLOC error code. */
 #undef THREAD_DB_HAS_TD_NOTALLOC
 
Index: configure.ac
===================================================================
RCS file: /cvs/src/src/gdb/configure.ac,v
retrieving revision 1.84
diff -u -p -r1.84 configure.ac
--- configure.ac	12 Jan 2009 01:10:27 -0000	1.84
+++ configure.ac	21 Jan 2009 10:51:58 -0000
@@ -1489,6 +1489,34 @@ AC_ARG_WITH(sysroot,
 AC_SUBST(TARGET_SYSTEM_ROOT)
 AC_SUBST(TARGET_SYSTEM_ROOT_DEFINE)
 
+system_gdbinit=
+AC_ARG_WITH(system-gdbinit,
+[  --with-system-gdbinit=file       Automatically load a system-wide gdbinit file],
+[system_gdbinit=${withval}])
+
+AC_DEFINE_DIR(SYSTEM_GDBINIT, system_gdbinit,
+              [System-wide gdbinit file.])
+
+if test "x$prefix" = xNONE; then
+  test_prefix=$ac_default_prefix
+else
+  test_prefix=$prefix
+fi
+if test "x$exec_prefix" = xNONE || test "x$exec_prefix" = 'x${prefix}'; then
+  test_exec_prefix=$test_prefix
+else
+  test_exec_prefix=$exec_prefix
+fi
+case ${system_gdbinit} in
+ "${test_prefix}"|"${test_prefix}/"*|\
+ "${test_exec_prefix}"|"${test_exec_prefix}/"*|\
+ '${prefix}'|'${prefix}/'*|\
+ '${exec_prefix}'|'${exec_prefix}/'*)
+  AC_DEFINE(SYSTEM_GDBINIT_RELOCATABLE, 1,
+            [Define if the system-wide gdbinit file should be relocated when GDB is moved.])
+   ;;
+esac
+
 AC_ARG_ENABLE(werror,
   [  --enable-werror    treat compile warnings as errors],
   [case "${enableval}" in
Index: main.c
===================================================================
RCS file: /cvs/src/src/gdb/main.c,v
retrieving revision 1.72
diff -u -p -r1.72 main.c
--- main.c	6 Jan 2009 18:31:59 -0000	1.72
+++ main.c	21 Jan 2009 10:51:58 -0000
@@ -83,6 +83,9 @@ int return_child_result_value = -1;
 /* Whether to enable writing into executable and core files */
 extern int write_files;
 
+/* GDB as it has been invoked from the command line (i.e. argv[0]).  */
+static char *gdb_program_name;
+
 static void print_gdb_help (struct ui_file *);
 
 /* These two are used to set the external editor commands when gdb is farming
@@ -90,6 +93,78 @@ static void print_gdb_help (struct ui_fi
 
 extern char *external_editor_command;
 
+/* Compute the locations of init files that GDB should source and return
+   them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT.  If there is 
+   no system gdbinit (resp. home gdbinit and local gdbinit) to be loaded,
+   then SYSTEM_GDBINIT (resp. HOME_GDBINIT and LOCAL_GDBINIT) is set to
+   NULL.  */
+static void
+get_init_files (char **system_gdbinit,
+		char **home_gdbinit,
+		char **local_gdbinit)
+{
+  static char *sysgdbinit = NULL;
+  static char *homeinit = NULL;
+  static char *localinit = NULL;
+  static int initialized = 0;
+
+  if (!initialized)
+    {
+      struct stat homebuf, cwdbuf, s;
+      char *homedir, *relocated_sysgdbinit;
+
+      if (stat (SYSTEM_GDBINIT, &s) == 0)
+	sysgdbinit = SYSTEM_GDBINIT;
+
+#ifdef SYSTEM_GDBINIT_RELOCATABLE
+      relocated_sysgdbinit = make_relative_prefix (gdb_program_name, BINDIR,
+						   SYSTEM_GDBINIT);
+      if (relocated_sysgdbinit)
+	{
+	  struct stat s;
+	  int res = 0;
+
+	  if (stat (relocated_sysgdbinit, &s) == 0)
+	      sysgdbinit = relocated_sysgdbinit;
+	}
+#endif
+
+      homedir = getenv ("HOME");
+
+      /* If the .gdbinit file in the current directory is the same as
+	 the $HOME/.gdbinit file, it should not be sourced.  homebuf
+	 and cwdbuf are used in that purpose. Make sure that the stats
+	 are zero in case one of them fails (this guarantees that they
+	 won't match if either exists).  */
+
+      memset (&homebuf, 0, sizeof (struct stat));
+      memset (&cwdbuf, 0, sizeof (struct stat));
+
+      if (homedir)
+	{
+	  homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
+	  if (stat (homeinit, &homebuf) != 0)
+	    homeinit = NULL;
+
+	  stat (homeinit, &homebuf);
+	}
+
+      if (stat (gdbinit, &cwdbuf) == 0)
+	{
+	  if (!homeinit
+	      || memcmp ((char *) &homebuf, (char *) &cwdbuf,
+			 sizeof (struct stat)))
+	    localinit = gdbinit;
+	}
+      
+      initialized = 1;
+    }
+
+  *system_gdbinit = sysgdbinit;
+  *home_gdbinit = homeinit;
+  *local_gdbinit = localinit;
+}
+
 /* Call command_loop.  If it happens to return, pass that through as a
    non-zero return status. */
 
@@ -156,8 +231,10 @@ captured_main (void *data)
   /* Number of elements used.  */
   int ndir;
 
-  struct stat homebuf, cwdbuf;
-  char *homedir;
+  /* gdb init files.  */
+  char *system_gdbinit;
+  char *home_gdbinit;
+  char *local_gdbinit;
 
   int i;
 
@@ -196,6 +273,8 @@ captured_main (void *data)
   gdb_stdtargerr = gdb_stderr;	/* for moment */
   gdb_stdtargin = gdb_stdin;	/* for moment */
 
+  gdb_program_name = xstrdup (argv[0]);
+
   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
     /* Don't use *_filtered or warning() (which relies on
        current_target) until after initialize_all_files(). */
@@ -274,6 +353,8 @@ captured_main (void *data)
 	}
     }
 
+  get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
+
   /* There will always be an interpreter.  Either the one passed into
      this captured main, or one specified by the user at start up, or
      the console.  Initialize the interpreter to the one requested by 
@@ -685,33 +766,20 @@ Excess command line arguments ignored. (
   quit_pre_print = error_pre_print;
   warning_pre_print = _("\nwarning: ");
 
+  /* Read and execute the system-wide gdbinit file, if it exists.
+     This is done *before* all the command line arguments are
+     processed; it sets global parameters, which are independent of
+     what file you are debugging or what directory you are in.  */
+  if (system_gdbinit && !inhibit_gdbinit)
+    catch_command_errors (source_script, system_gdbinit, 0, RETURN_MASK_ALL);
+
   /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
      *before* all the command line arguments are processed; it sets
      global parameters, which are independent of what file you are
      debugging or what directory you are in.  */
-  homedir = getenv ("HOME");
-  if (homedir)
-    {
-      char *homeinit = xstrprintf ("%s/%s", homedir, gdbinit);
-
-      if (!inhibit_gdbinit)
-	{
-	  catch_command_errors (source_script, homeinit, 0, RETURN_MASK_ALL);
-	}
-
-      /* Do stats; no need to do them elsewhere since we'll only
-         need them if homedir is set.  Make sure that they are
-         zero in case one of them fails (this guarantees that they
-         won't match if either exists).  */
-
-      memset (&homebuf, 0, sizeof (struct stat));
-      memset (&cwdbuf, 0, sizeof (struct stat));
 
-      stat (homeinit, &homebuf);
-      stat (gdbinit, &cwdbuf);	/* We'll only need this if
-				   homedir was set.  */
-      xfree (homeinit);
-    }
+  if (home_gdbinit && !inhibit_gdbinit)
+    catch_command_errors (source_script, home_gdbinit, 0, RETURN_MASK_ALL);
 
   /* Now perform all the actions indicated by the arguments.  */
   if (cdarg != NULL)
@@ -779,13 +847,8 @@ Can't attach to process and specify a co
 
   /* Read the .gdbinit file in the current directory, *if* it isn't
      the same as the $HOME/.gdbinit file (it should exist, also).  */
-
-  if (!homedir
-      || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
-    if (!inhibit_gdbinit)
-      {
-	catch_command_errors (source_script, gdbinit, 0, RETURN_MASK_ALL);
-      }
+  if (local_gdbinit && !inhibit_gdbinit)
+    catch_command_errors (source_script, local_gdbinit, 0, RETURN_MASK_ALL);
 
   for (i = 0; i < ncmd; i++)
     {
@@ -857,6 +920,12 @@ gdb_main (struct captured_main_args *arg
 static void
 print_gdb_help (struct ui_file *stream)
 {
+  char *system_gdbinit;
+  char *home_gdbinit;
+  char *local_gdbinit;
+
+  get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
+
   fputs_unfiltered (_("\
 This is the GNU debugger.  Usage:\n\n\
     gdb [options] [executable-file [core-file or process-id]]\n\
@@ -919,6 +988,21 @@ Options:\n\n\
   --xdb              XDB compatibility mode.\n\
 "), stream);
   fputs_unfiltered (_("\n\
+At startup, GDB reads the following init files and executes their commands:\n\
+"), stream);
+  if (system_gdbinit)
+    fprintf_unfiltered (stream, _("\
+   * system-wide init file: %s\n\
+"), system_gdbinit);
+  if (home_gdbinit)
+    fprintf_unfiltered (stream, _("\
+   * user-specific init file: %s\n\
+"), home_gdbinit);
+  if (local_gdbinit)
+    fprintf_unfiltered (stream, _("\
+   * local init file: ./%s\n\
+"), local_gdbinit);
+  fputs_unfiltered (_("\n\
 For more information, type \"help\" from within GDB, or consult the\n\
 GDB manual (available as on-line info or a printed manual).\n\
 "), stream);
Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.545
diff -u -p -r1.545 gdb.texinfo
--- doc/gdb.texinfo	14 Jan 2009 11:47:07 -0000	1.545
+++ doc/gdb.texinfo	21 Jan 2009 10:52:02 -0000
@@ -1211,6 +1211,12 @@ Sets up the command interpreter as speci
 
 @item
 @cindex init file
+Reads the system-wide @dfn{init file} (if @option{--with-system-gdbinit} was
+used when building @value{GDBN}; @pxref{System-wide configuration,
+ ,System-wide configuration and settings}) and executes all the commands in
+that file.
+
+@item
 Reads the @dfn{init file} (if any) in your home directory@footnote{On
 DOS/Windows systems, the home directory is the one pointed to by the
 @code{HOME} environment variable.} and executes all the commands in
@@ -1244,6 +1250,9 @@ complaints}) that affect subsequent proc
 and operands.  Init files are not executed if you use the @samp{-nx}
 option (@pxref{Mode Options, ,Choosing Modes}).
 
+To display the list of init files loaded by gdb at startup, you
+can use @kbd{gdb --help}.
+
 @cindex init file name
 @cindex @file{.gdbinit}
 @cindex @file{gdb.ini}
@@ -24304,6 +24313,7 @@ Then give @file{gdb.dvi} to your @sc{dvi
 * Separate Objdir::             Compiling @value{GDBN} in another directory
 * Config Names::                Specifying names for hosts and targets
 * Configure Options::           Summary of options for configure
+* System-wide configuration::   Having a system-wide init file
 @end menu
 
 @node Requirements
@@ -24646,6 +24656,36 @@ There is no convenient way to generate a
 There are many other options available as well, but they are generally
 needed for special purposes only.
 
+@node System-wide configuration
+@section System-wide configuration and settings
+@cindex system-wide init file
+
+@value{GDBN} can be configured to have a system-wide @dfn{init file};
+this file will be read and executed at startup (@pxref{Startup, , What
+@value{GDBN} does during startup}).
+
+Here is the corresponding configure option:
+
+@table @code
+@item --with-system-gdbinit=@var{file}
+Specify that the default location of the system-wide @dfn{init file} is
+@var{file}.
+@end table
+
+If the default location of this init file contains the prefix,
+it will be subject to relocation. Suppose that @value{GDBN} has been
+configured with @kbd{--prefix=$prefix} and
+@kbd{--with-system-gdbinit=$prefix/etc/gdbinit}; if @value{GDBN} is
+installed somewhere else (say: in @file{$install/bin}), the system
+init file will be looked for relatively to this new location: in our
+case, it will be @file{$install/bin/../etc/gdbinit}.
+
+At the contrary, if the default location does not contain the prefix,
+it will not be relocated. E.g.@: if @value{GDBN} has been configured with
+@kbd{--prefix=/usr/local --with-system-gdbinit=/usr/share/gdb/gdbinit},
+then @value{GDBN} will always look for @kbd{/usr/share/gdb/gdbinit},
+wherever @value{GDBN} is installed.
+
 @node Maintenance Commands
 @appendix Maintenance Commands
 @cindex maintenance commands

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]