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]

[patch#2 3/6] set auto-load local-gdbinit warn-and-*


Hi,

it does not change any default behavior (that's done in [patch 6/6]) but it
gives an option to warn on .gdbinit files (which become deprecated).


Thanks,
Jan


gdb/
2012-03-20  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* NEWS (set auto-load local-gdbinit): Add warn-and-on and warn-and-off
	parameters.
	* auto-load.c (auto_load_local_gdbinit_yes, auto_load_local_gdbinit_no)
	(auto_load_local_gdbinit_warn_and_yes)
	(auto_load_local_gdbinit_warn_and_no, auto_load_local_gdbinit_enum):
	New.
	(auto_load_local_gdbinit): Change it to string.
	(_initialize_auto_load): Extend the parameters
	for "set auto-load local-gdbinit".
	* auto-load.h (auto_load_local_gdbinit_yes, auto_load_local_gdbinit_no)
	(auto_load_local_gdbinit_warn_and_yes) 
	(auto_load_local_gdbinit_warn_and_no): New prototypes.
	(auto_load_local_gdbinit): Change the prototype to string.
	* main.c: Include readline/tilde.h.
	(get_init_files): Add parameter local_gdbinit_stat, describe it in the
	comment.  New variable localinit_stat.  Remove variable cwdbuf.
	(captured_main): New variable local_gdbinit_stat.  Scan CMDARG_VEC and
	print warnings according to AUTO_LOAD_LOCAL_GDBINIT.
	(print_gdb_help): Update the get_init_files caller.

gdb/doc/
2012-03-29  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb.texinfo (Startup): Mention also warn-and-yes for local gdbinit,
	deprecate the feature.
	(Auto-loading): Update the example from on to yes.
	(Current Directory Init File): Mention also warn-and-yes and
	warn-and-no options of "set auto-load local-gdbinit and the warnings for
	"show auto-load local-gdbinit".

--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -141,7 +141,7 @@ set auto-load python-scripts on|off
 show auto-load python-scripts
   Control auto-loading of Python script files.
 
-set auto-load local-gdbinit on|off
+set auto-load local-gdbinit on|off|warn-and-on|warn-and-off
 show auto-load local-gdbinit
   Control loading of init file (.gdbinit) from current directory.
 
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -70,8 +70,31 @@ show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
    used to find the scripts.  */
 int global_auto_load = 1;
 
+/* Load current directory .gdbinit file automatically.  */
+const char auto_load_local_gdbinit_yes[] = "yes";
+
+/* Never load current directory .gdbinit file.  */
+const char auto_load_local_gdbinit_no[] = "no";
+
+/* Give deprecation warning and load current directory .gdbinit file
+   automatically.  */
+const char auto_load_local_gdbinit_warn_and_yes[] = "warn-and-yes";
+
+/* Give deprecation warning and do not load current directory .gdbinit file.  */
+const char auto_load_local_gdbinit_warn_and_no[] = "warn-and-no";
+
+/* Options for auto_load_local_gdbinit.  */
+static const char *const auto_load_local_gdbinit_enum[] =
+{
+  auto_load_local_gdbinit_yes,
+  auto_load_local_gdbinit_no,
+  auto_load_local_gdbinit_warn_and_yes,
+  auto_load_local_gdbinit_warn_and_no,
+  NULL
+};
+
 /* Auto-load .gdbinit file from the current directory?  */
-int auto_load_local_gdbinit = 1;
+const char *auto_load_local_gdbinit = auto_load_local_gdbinit_yes;
 
 /* Absolute pathname to the current directory .gdbinit, if it exists.  */
 char *auto_load_local_gdbinit_pathname = NULL;
@@ -705,18 +728,20 @@ This options has security implications for untrusted inferiors."),
 Usage: info auto-load gdb-scripts [REGEXP]"),
 	   auto_load_info_cmdlist_get ());
 
-  add_setshow_boolean_cmd ("local-gdbinit", class_support,
-			   &auto_load_local_gdbinit, _("\
+  add_setshow_enum_cmd ("local-gdbinit", class_support,
+			auto_load_local_gdbinit_enum,
+			&auto_load_local_gdbinit, _("\
 Enable or disable auto-loading of .gdbinit script in current directory."), _("\
 Show whether auto-loading .gdbinit script in current directory is enabled."),
 			   _("\
 If enabled, canned sequences of commands are loaded when debugger starts\n\
 from .gdbinit file in current directory.  Such files are deprecated,\n\
 use script associated with inferior executable file instead.\n\
+You can optionally display a warning when such file is found.\n\
 This options has security implications for untrusted inferiors."),
-			   NULL, show_auto_load_local_gdbinit,
-			   auto_load_set_cmdlist_get (),
-			   auto_load_show_cmdlist_get ());
+			NULL, show_auto_load_local_gdbinit,
+			auto_load_set_cmdlist_get (),
+			auto_load_show_cmdlist_get ());
 
   add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
 	   _("Print whether current directory .gdbinit file has been loaded.\n\
--- a/gdb/auto-load.h
+++ b/gdb/auto-load.h
@@ -32,7 +32,11 @@ struct script_language
 
 extern int global_auto_load;
 
-extern int auto_load_local_gdbinit;
+extern const char auto_load_local_gdbinit_yes[];
+extern const char auto_load_local_gdbinit_no[];
+extern const char auto_load_local_gdbinit_warn_and_yes[];
+extern const char auto_load_local_gdbinit_warn_and_no[];
+extern const char *auto_load_local_gdbinit;
 extern char *auto_load_local_gdbinit_pathname;
 extern int auto_load_local_gdbinit_loaded;
 
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -1287,13 +1287,16 @@ Processes command line options and operands.
 @item
 Reads and executes the commands from init file (if any) in the current
 working directory as long as @samp{set auto-load local-gdbinit} is set to
-@samp{yes} (@pxref{Current Directory Init File}).
+@samp{yes} or @samp{warn-and-yes} (@pxref{Current Directory Init File}).
 This is only done if the current directory is
 different from your home directory.  Thus, you can have more than one
 init file, one generic in your home directory, and another, specific
 to the program you are debugging, in the directory where you invoke
 @value{GDBN}.
 
+This feature is deprecated, please use @ref{objfile-gdb.rc file} instead;
+it will be file-bound, no longer directory-bound.
+
 @item
 If the command line specified a program to debug, or a process to
 attach to, or a core file, @value{GDBN} loads any auto-loaded
@@ -20728,7 +20731,7 @@ is enabled or disabled.
 (gdb) show auto-load
 gdb-scripts:  Canned sequences of commands auto-loading is on.
 libthread-db:  Inferior specific libthread_db auto-loading is on.
-local-gdbinit:  Current directory .gdbinit script auto-loading is on.
+local-gdbinit:  Current directory .gdbinit script auto-loading is yes.
 python-scripts:  Python scripts auto-loading is on.
 @end smallexample
 
@@ -20816,15 +20819,20 @@ see @ref{Current Directory Init File during Startup}.
 @table @code
 @anchor{set auto-load local-gdbinit}
 @kindex set auto-load local-gdbinit
-@item set auto-load local-gdbinit [yes|no]
+@item set auto-load local-gdbinit [yes|no|warn-and-yes|warn-and-no]
 Enable or disable the auto-loading of canned sequences of commands
 (@pxref{Sequences}) found in init file in the current directory.
+The options @samp{warn-and-X} give warning before loading
+(@samp{warn-and-yes}) or when declining to load (@samp{warn-and-no}) the file
+as the @samp{local-gdbinit} feature is deprecated in the favor
+of @xref{objfile-gdb.rc file}.
 
 @anchor{show auto-load local-gdbinit}
 @kindex show auto-load local-gdbinit
 @item show auto-load local-gdbinit
 Show whether auto-loading of canned sequences of commands from init file in the
-current directory is enabled or disabled.
+current directory is enabled or disabled and whether warnings are printed
+during its load.
 
 @anchor{info auto-load local-gdbinit}
 @kindex info auto-load local-gdbinit
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -42,6 +42,7 @@
 #include "python/python.h"
 #include "objfiles.h"
 #include "auto-load.h"
+#include "readline/tilde.h"
 
 /* The selected interpreter.  This will be used as a set command
    variable, so it should always be malloc'ed - since
@@ -146,23 +147,26 @@ relocate_gdb_directory (const char *initial, int flag)
 }
 
 /* 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.  */
+   return them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT and
+   LOCAL_GDBINIT_STAT.  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, LOCAL_GDBINIT_STAT is
+   zeroed.  */
+
 static void
 get_init_files (char **system_gdbinit,
 		char **home_gdbinit,
-		char **local_gdbinit)
+		char **local_gdbinit, struct stat *local_gdbinit_stat)
 {
   static char *sysgdbinit = NULL;
   static char *homeinit = NULL;
   static char *localinit = NULL;
+  static struct stat localinit_stat;
   static int initialized = 0;
 
   if (!initialized)
     {
-      struct stat homebuf, cwdbuf, s;
+      struct stat homebuf, s;
       char *homedir, *relocated_sysgdbinit;
 
       if (SYSTEM_GDBINIT[0])
@@ -180,12 +184,12 @@ get_init_files (char **system_gdbinit,
 
       /* 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).  */
+	 and localinit_stat 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));
+      memset (&localinit_stat, 0, sizeof (struct stat));
 
       if (homedir)
 	{
@@ -197,11 +201,10 @@ get_init_files (char **system_gdbinit,
 	    }
 	}
 
-      if (stat (gdbinit, &cwdbuf) == 0)
+      if (stat (gdbinit, &localinit_stat) == 0)
 	{
 	  if (!homeinit
-	      || memcmp ((char *) &homebuf, (char *) &cwdbuf,
-			 sizeof (struct stat)))
+	      || memcmp (&homebuf, &localinit_stat, sizeof (struct stat)))
 	    localinit = gdbinit;
 	}
       
@@ -211,6 +214,8 @@ get_init_files (char **system_gdbinit,
   *system_gdbinit = sysgdbinit;
   *home_gdbinit = homeinit;
   *local_gdbinit = localinit;
+  if (local_gdbinit_stat)
+    *local_gdbinit_stat = localinit_stat;
 }
 
 /* Call command_loop.  If it happens to return, pass that through as a
@@ -303,6 +308,7 @@ captured_main (void *data)
   char *system_gdbinit;
   char *home_gdbinit;
   char *local_gdbinit;
+  struct stat local_gdbinit_stat;
 
   int i;
   int save_auto_load;
@@ -755,7 +761,8 @@ captured_main (void *data)
   /* Lookup gdbinit files.  Note that the gdbinit file name may be
      overriden during file initialization, so get_init_files should be
      called after gdb_init.  */
-  get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
+  get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit,
+		  &local_gdbinit_stat);
 
   /* Do these (and anything which might call wrap_here or *_filtered)
      after initialize_all_files() but before the interpreter has been
@@ -944,12 +951,64 @@ captured_main (void *data)
     {
       auto_load_local_gdbinit_pathname = gdb_realpath (local_gdbinit);
 
-      if (!inhibit_gdbinit && auto_load_local_gdbinit)
+      if (!inhibit_gdbinit
+	  && auto_load_local_gdbinit != auto_load_local_gdbinit_no)
 	{
-	  auto_load_local_gdbinit_loaded = 1;
+	  /* Verify whether user has already specified `-x ./.gdbinit'.  */
+	  for (i = 0; VEC_iterate (cmdarg_s, cmdarg_vec, i, cmdarg_p); i++)
+	    if (cmdarg_p->type == CMDARG_FILE)
+	      {
+		struct cleanup *old_cleanups;
+		char *file;
+		int fd;
+
+		file = tilde_expand (cmdarg_p->string);
+		old_cleanups = make_cleanup (xfree, file);
+
+		fd = openp (source_path, OPF_TRY_CWD_FIRST, file, O_RDONLY,
+			    NULL);
+
+		do_cleanups (old_cleanups);
+
+		if (fd != -1)
+		  {
+		    struct stat statbuf;
+
+		    if (fstat (fd, &statbuf) == 0
+			&& memcmp (&statbuf, &local_gdbinit_stat,
+				   sizeof (statbuf)) == 0)
+		      local_gdbinit = NULL;
 
-	  catch_command_errors (source_script, local_gdbinit, 0,
-				RETURN_MASK_ALL);
+		    close (fd);
+		    if (local_gdbinit == NULL)
+		      break;
+		  }
+	      }
+
+	  if (local_gdbinit)
+	    {
+	      const char adv[] = N_("Use script associated with inferior "
+				    "executable file instead.  "
+				    "See also 'set auto-load local-gdbinit'.  "
+				    "You may also use 'gdb -x .gdbinit ...'.");
+	      if (auto_load_local_gdbinit
+		  == auto_load_local_gdbinit_warn_and_yes)
+		warning (_("Reading file .gdbinit in current directory but it "
+			   "has been deprecated and the reading will be "
+			   "removed.  %s"),
+			 _(adv));
+	      if (auto_load_local_gdbinit
+		  == auto_load_local_gdbinit_warn_and_no)
+		warning (_("Ignoring file .gdbinit in current directory as it "
+			   "has been deprecated.  %s"),
+			 _(adv));
+	      else
+		{
+		  auto_load_local_gdbinit_loaded = 1;
+		  catch_command_errors (source_script, local_gdbinit, 0,
+					RETURN_MASK_ALL);
+		}
+	    }
 	}
     }
 
@@ -1021,7 +1080,7 @@ print_gdb_help (struct ui_file *stream)
   char *home_gdbinit;
   char *local_gdbinit;
 
-  get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
+  get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit, NULL);
 
   fputs_unfiltered (_("\
 This is the GNU debugger.  Usage:\n\n\


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