[PATCH v1 04/13] script language API for GDB: auto-load changes

xdje42@gmail.com xdje42@gmail.com
Fri Dec 6 05:53:00 GMT 2013


This patch contains the changes to auto-load.c, auto-load.h, py-auto-load.c.
struct script_language has been moved to scripting-priv.h (and renamed to
script_language_defn).
Variable script_language_python has been moved to python.c.

2013-12-05  Doug Evans  <xdje42@gmail.com>

	* auto-load.c: #include "scripting.h".
	(GDB_AUTO_FILE_NAME): Delete.
	(auto_load_gdb_scripts_enabled): Make public.  New arg slang.
	(script_language_gdb): Delete, moved to scripting.c.
	(source_gdb_script_for_objfile): Delete.
	(auto_load_pspace_info): New member unsupported_script_warning_printed.
	(loaded_script): Change type of language member to
	struct script_language_defn *.
	(init_loaded_scripts_info): Initialize
	unsupported_script_warning_printed.
	(maybe_add_script): Make static.  Change type of language arg to
	struct script_language_defn *.
	(clear_section_scripts): Reset unsupported_script_warning_printed.
	(auto_load_objfile_script_1): Rewrite to use scripting language API.
	(auto_load_objfile_script): Make public.  Remove support-compiled-in
	and auto-load-enabled checks, moved to auto_load_scripts_for_objfile.
	(source_section_scripts): Rewrite to use scripting language API.
	(load_auto_scripts_for_objfile): Rewrite to use
	auto_load_scripts_for_objfile.
	(collect_matching_scripts_data): Change type of language member to
	struct script_language_defn *.
	(auto_load_info_scripts): Change type of language arg to
	struct script_language_defn *.
	(unsupported_script_warning_print): New function.
	(script_not_found_warning_print): Make static.
	(_initialize_auto_load): Rewrite construction of scripts-directory
	help.
	* auto-load.h (struct objfile): Add forward decl.
	(struct script_language): Delete.
	(struct auto_load_pspace_info): Add forward decl.
	(struct script_language_defn): Add forward decl.
	(maybe_add_script): Delete.
	(auto_load_objfile_script): Declare.
	(script_not_found_warning_print): Delete.
	(auto_load_info_scripts): Update prototype.
	(auto_load_gdb_scripts_enabled): Declare.
	* python/py-auto-load.c (gdbpy_auto_load_enabled): Renamed from
	auto_load_python_scripts_enabled and made public.
	(script_language_python): Delete, moved to python.c.
	(gdbpy_script_language_defn): Delete.

diff --git a/gdb/auto-load.c b/gdb/auto-load.c
index e516c6a..9677845 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -20,7 +20,6 @@
 #include "defs.h"
 #include "auto-load.h"
 #include "progspace.h"
-#include "python/python.h"
 #include "gdb_regex.h"
 #include "ui-out.h"
 #include "filenames.h"
@@ -40,6 +39,8 @@
 #include "fnmatch.h"
 #include "top.h"
 #include "filestuff.h"
+#include "scripting.h"
+#include "python/python.h"
 
 /* The section to look in for auto-loaded scripts (in file formats that
    support sections).
@@ -49,12 +50,14 @@
    followed by the path of a python script to load.  */
 #define AUTO_SECTION_NAME ".debug_gdb_scripts"
 
-/* The suffix of per-objfile scripts to auto-load as non-Python command files.
-   E.g. When the program loads libfoo.so, look for libfoo-gdb.gdb.  */
-#define GDB_AUTO_FILE_NAME "-gdb.gdb"
+static int maybe_add_script (struct auto_load_pspace_info *pspace_info,
+			     int loaded, const char *name,
+			     const char *full_path,
+			     const struct script_language_defn *language);
+
+static int unsupported_script_warning_print (struct auto_load_pspace_info *);
 
-static void source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
-					   const char *filename);
+static int script_not_found_warning_print (struct auto_load_pspace_info *);
 
 /* Value of the 'set debug auto-load' configuration variable.  */
 static int debug_auto_load = 0;
@@ -90,8 +93,8 @@ show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
 
 /* Return non-zero if auto-loading gdb scripts is enabled.  */
 
-static int
-auto_load_gdb_scripts_enabled (void)
+int
+auto_load_gdb_scripts_enabled (const struct script_language_defn *slang)
 {
   return auto_load_gdb_scripts;
 }
@@ -517,29 +520,6 @@ For more information about this security protection see the\n\
   return 0;
 }
 
-/* Definition of script language for GDB canned sequences of commands.  */
-
-static const struct script_language script_language_gdb =
-{
-  "gdb",
-  GDB_AUTO_FILE_NAME,
-  auto_load_gdb_scripts_enabled,
-  source_gdb_script_for_objfile
-};
-
-static void
-source_gdb_script_for_objfile (struct objfile *objfile, FILE *file,
-			       const char *filename)
-{
-  volatile struct gdb_exception e;
-
-  TRY_CATCH (e, RETURN_MASK_ALL)
-    {
-      script_from_file (file, filename);
-    }
-  exception_print (gdb_stderr, e);
-}
-
 /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
    the same script.  There's no point in loading the script multiple times,
    and there can be a lot of objfiles and scripts, so we keep track of scripts
@@ -551,6 +531,10 @@ struct auto_load_pspace_info
   struct htab *loaded_scripts;
 
   /* Non-zero if we've issued the warning about an auto-load script not being
+     supported.  We only want to issue this warning once.  */
+  int unsupported_script_warning_printed;
+
+  /* Non-zero if we've issued the warning about an auto-load script not being
      found.  We only want to issue this warning once.  */
   int script_not_found_warning_printed;
 };
@@ -569,7 +553,7 @@ struct loaded_script
   /* Non-zero if this script has been loaded.  */
   int loaded;
 
-  const struct script_language *language;
+  const struct script_language_defn *language;
 };
 
 /* Per-program-space data key.  */
@@ -639,6 +623,7 @@ init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
 					     eq_loaded_script_entry,
 					     xfree);
 
+  pspace_info->unsupported_script_warning_printed = FALSE;
   pspace_info->script_not_found_warning_printed = FALSE;
 }
 
@@ -662,10 +647,10 @@ get_auto_load_pspace_data_for_loading (struct program_space *pspace)
    been found).  FULL_PATH is NULL if the script wasn't found.  The result is
    true if the script was already in the hash table.  */
 
-int
+static int
 maybe_add_script (struct auto_load_pspace_info *pspace_info, int loaded,
 		  const char *name, const char *full_path,
-		  const struct script_language *language)
+		  const struct script_language_defn *language)
 {
   struct htab *htab = pspace_info->loaded_scripts;
   struct loaded_script **slot, entry;
@@ -717,6 +702,7 @@ clear_section_scripts (void)
     {
       htab_delete (info->loaded_scripts);
       info->loaded_scripts = NULL;
+      info->unsupported_script_warning_printed = FALSE;
       info->script_not_found_warning_printed = FALSE;
     }
 }
@@ -727,17 +713,18 @@ clear_section_scripts (void)
 
 static int
 auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
-			    const struct script_language *language)
+			    const struct script_language_defn *language)
 {
   char *filename, *debugfile;
   int len, retval;
   FILE *input;
   struct cleanup *cleanups;
+  const char *suffix = script_lang_auto_load_suffix (language);
 
   len = strlen (realname);
-  filename = xmalloc (len + strlen (language->suffix) + 1);
+  filename = xmalloc (len + strlen (suffix) + 1);
   memcpy (filename, realname, len);
-  strcpy (filename + len, language->suffix);
+  strcpy (filename + len, suffix);
 
   cleanups = make_cleanup (xfree, filename);
 
@@ -795,8 +782,8 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
 	= file_is_auto_load_safe (filename,
 				  _("auto-load: Loading %s script \"%s\""
 				    " by extension for objfile \"%s\".\n"),
-				  language->name, filename,
-				  objfile_name (objfile));
+				  script_lang_name (language),
+				  filename, objfile_name (objfile));
 
       /* Add this script to the hash table too so
 	 "info auto-load ${lang}-scripts" can print it.  */
@@ -810,7 +797,13 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
 	 and these scripts are required to be idempotent under multiple
 	 loads anyway.  */
       if (is_safe)
-	language->source_script_for_objfile (objfile, input, debugfile);
+	{
+	  objfile_script_sourcer_func *sourcer
+	    = script_lang_objfile_sourcer (language);
+
+	  gdb_assert (sourcer != NULL);
+	  sourcer (language, objfile, input, debugfile);
+	}
 
       retval = 1;
     }
@@ -824,21 +817,12 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
 /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
    it.  */
 
-static void
+void
 auto_load_objfile_script (struct objfile *objfile,
-			  const struct script_language *language)
+			  const struct script_language_defn *language)
 {
-  char *realname;
-  struct cleanup *cleanups;
-
-  /* Skip this script if support has not been compiled in or
-     auto-loading it has been disabled.  */
-  if (language == NULL
-      || !language->auto_load_enabled ())
-    return;
-
-  realname = gdb_realpath (objfile_name (objfile));
-  cleanups = make_cleanup (xfree, realname);
+  char *realname = gdb_realpath (objfile_name (objfile));
+  struct cleanup *cleanups = make_cleanup (xfree, realname);
 
   if (!auto_load_objfile_script_1 (objfile, realname, language))
     {
@@ -893,7 +877,8 @@ source_section_scripts (struct objfile *objfile, const char *section_name,
       struct cleanup *back_to;
       /* At the moment we only support python scripts in .debug_gdb_scripts,
 	 but that can change.  */
-      const struct script_language *language = gdbpy_script_language_defn ();
+      const struct script_language_defn *language = &script_language_python;
+      objfile_script_sourcer_func *sourcer;
 
       if (*p != 1)
 	{
@@ -923,10 +908,34 @@ source_section_scripts (struct objfile *objfile, const char *section_name,
 	  continue;
 	}
 
-      /* Skip this script if support has not been compiled in or
-	 auto-loading it has been disabled.  */
-      if (language == NULL
-	  || !language->auto_load_enabled ())
+      /* Until we support more types of records in .debug_gdb_scripts we do
+	 all the processing here.  The expectation is to add a new
+	 script_language_script_ops "method" that handles all the records
+	 for the language.  For now we can just use
+	 script_language_script_ops.objfile_script_sourcer.  */
+
+      /* Skip this script if support is not compiled in.  */
+      sourcer = script_lang_objfile_sourcer (language);
+      if (sourcer == NULL)
+	{
+	  /* We don't throw an error, the program is still debuggable.  */
+	  if (!unsupported_script_warning_print (pspace_info))
+	    {
+	      warning (_("Unsupported auto-load scripts referenced in"
+			 " %s section\n"
+			 "of file %s.\n"
+			 "Use `info auto-load %s-scripts [REGEXP]'"
+			 " to list them."),
+		       section_name, objfile_name (objfile),
+		       script_lang_name (language));
+	    }
+	  /* We *could* still try to open it, but there's no point.  */
+	  maybe_add_script (pspace_info, 0, file, NULL, language);
+	  continue;
+	}
+
+      /* Skip this script if auto-loading it has been disabled.  */
+      if (!script_lang_auto_load_enabled (language))
 	{
 	  /* No message is printed, just skip it.  */
 	  continue;
@@ -945,8 +954,8 @@ source_section_scripts (struct objfile *objfile, const char *section_name,
 				       _("auto-load: Loading %s script "
 					 "\"%s\" from section \"%s\" of "
 					 "objfile \"%s\".\n"),
-				       language->name, full_path, section_name,
-				       objfile_name (objfile)))
+				       script_lang_name (language), full_path,
+				       section_name, objfile_name (objfile)))
 	    opened = 0;
 	}
       else
@@ -965,7 +974,8 @@ source_section_scripts (struct objfile *objfile, const char *section_name,
 	    warning (_("Missing auto-load scripts referenced in section %s\n\
 of file %s\n\
 Use `info auto-load %s-scripts [REGEXP]' to list them."),
-		     section_name, objfile_name (objfile), language->name);
+		     section_name, objfile_name (objfile),
+		     script_lang_name (language));
 	}
 
       in_hash_table = maybe_add_script (pspace_info, opened, file, full_path,
@@ -973,10 +983,7 @@ Use `info auto-load %s-scripts [REGEXP]' to list them."),
 
       /* If this file is not currently loaded, load it.  */
       if (opened && !in_hash_table)
-	{
-	  gdb_assert (language->source_script_for_objfile != NULL);
-	  language->source_script_for_objfile (objfile, stream, full_path);
-	}
+	sourcer (language, objfile, stream, full_path);
 
       do_cleanups (back_to);
     }
@@ -1022,8 +1029,7 @@ load_auto_scripts_for_objfile (struct objfile *objfile)
     return;
 
   /* Load any scripts for this objfile. e.g. foo-gdb.gdb, foo-gdb.py.  */
-  auto_load_objfile_script (objfile, &script_language_gdb);
-  auto_load_objfile_script (objfile, gdbpy_script_language_defn ());
+  auto_load_scripts_for_objfile (objfile);
 
   /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts).  */
   auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
@@ -1057,7 +1063,7 @@ struct collect_matching_scripts_data
 {
   VEC (loaded_script_ptr) **scripts_p;
 
-  const struct script_language *language;
+  const struct script_language_defn *language;
 };
 
 /* Traversal function for htab_traverse.
@@ -1123,7 +1129,7 @@ char auto_load_info_scripts_pattern_nl[] = "";
 
 void
 auto_load_info_scripts (char *pattern, int from_tty,
-			const struct script_language *language)
+			const struct script_language_defn *language)
 {
   struct ui_out *uiout = current_uiout;
   struct auto_load_pspace_info *pspace_info;
@@ -1224,11 +1230,25 @@ info_auto_load_local_gdbinit (char *args, int from_tty)
 		     auto_load_local_gdbinit_pathname);
 }
 
+/* Return non-zero if UNSUPPORTED_SCRIPT_WARNING_PRINTED of PSPACE_INFO was
+   unset before calling this function.  Always set
+   UNSUPPORTED_SCRIPT_WARNING_PRINTED of PSPACE_INFO.  */
+
+static int
+unsupported_script_warning_print (struct auto_load_pspace_info *pspace_info)
+{
+  int retval = !pspace_info->unsupported_script_warning_printed;
+
+  pspace_info->unsupported_script_warning_printed = 1;
+
+  return retval;
+}
+
 /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
    before calling this function.  Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
    of PSPACE_INFO.  */
 
-int
+static int
 script_not_found_warning_print (struct auto_load_pspace_info *pspace_info)
 {
   int retval = !pspace_info->script_not_found_warning_printed;
@@ -1370,7 +1390,7 @@ void
 _initialize_auto_load (void)
 {
   struct cmd_list_element *cmd;
-  char *scripts_directory_help;
+  char *scripts_directory_help, *gdb_name_help, *python_name_help;
 
   auto_load_pspace_data
     = register_program_space_data_with_cleanup (NULL,
@@ -1414,27 +1434,34 @@ Usage: info auto-load local-gdbinit"),
 	   auto_load_info_cmdlist_get ());
 
   auto_load_dir = xstrdup (AUTO_LOAD_DIR);
-  scripts_directory_help = xstrprintf (
+
+  gdb_name_help
+    = xstrprintf (_("\
+GDB scripts:    OBJFILE%s\n"),
+		  script_lang_auto_load_suffix (&script_language_gdb));
+  python_name_help = NULL;
 #ifdef HAVE_PYTHON
-				       _("\
-Automatically loaded Python scripts (named OBJFILE%s) and GDB scripts\n\
-(named OBJFILE%s) are located in one of the directories listed by this\n\
-option.\n\
-%s"),
-				       GDBPY_AUTO_FILE_NAME,
-#else
-				       _("\
-Automatically loaded GDB scripts (named OBJFILE%s) are located in one\n\
-of the directories listed by this option.\n\
-%s"),
+  python_name_help
+    = xstrprintf (_("\
+Python scripts: OBJFILE%s\n"),
+		  script_lang_auto_load_suffix (&script_language_python));
 #endif
-				       GDB_AUTO_FILE_NAME,
-				       _("\
+  scripts_directory_help
+    = xstrprintf (_("\
+Automatically loaded scripts are located in one of the directories listed\n\
+by this option.\n\
+\n\
+Script names:\n\
+%s%s\
+\n\
 This option is ignored for the kinds of scripts \
 having 'set auto-load ... off'.\n\
 Directories listed here need to be present also \
 in the 'set auto-load safe-path'\n\
-option."));
+option."),
+		  gdb_name_help,
+		  python_name_help ? python_name_help : "");
+
   add_setshow_optional_filename_cmd ("scripts-directory", class_support,
 				     &auto_load_dir, _("\
 Set the list of directories from which to load auto-loaded scripts."), _("\
@@ -1444,6 +1471,8 @@ Show the list of directories from which to load auto-loaded scripts."),
 				     auto_load_set_cmdlist_get (),
 				     auto_load_show_cmdlist_get ());
   xfree (scripts_directory_help);
+  xfree (python_name_help);
+  xfree (gdb_name_help);
 
   auto_load_safe_path = xstrdup (AUTO_LOAD_SAFE_PATH);
   auto_load_safe_path_vec_update ();
diff --git a/gdb/auto-load.h b/gdb/auto-load.h
index 2ddebc5..df54dbe 100644
--- a/gdb/auto-load.h
+++ b/gdb/auto-load.h
@@ -20,25 +20,10 @@
 #ifndef AUTO_LOAD_H
 #define AUTO_LOAD_H 1
 
+struct objfile;
 struct program_space;
-
-struct script_language
-{
-  /* The name of the language, lowercase.  */
-  const char *name;
-
-  /* The suffix added to objfiles to get their auto-load script.
-     E.g., "-gdb.py".  */
-  const char *suffix;
-
-  /* Returns non-zero if auto-loading scripts in this language is enabled.  */
-  int (*auto_load_enabled) (void);
-
-  /* Worker routine to load the script.  It has already been opened and
-     deemed safe to load.  */
-  void (*source_script_for_objfile) (struct objfile *objfile, FILE *file,
-				     const char *filename);
-};
+struct auto_load_pspace_info;
+struct script_language_defn;
 
 extern int global_auto_load;
 
@@ -48,16 +33,12 @@ extern int auto_load_local_gdbinit_loaded;
 
 extern struct auto_load_pspace_info *
   get_auto_load_pspace_data_for_loading (struct program_space *pspace);
-extern int maybe_add_script (struct auto_load_pspace_info *pspace_info,
-			     int loaded, const char *name,
-			     const char *full_path,
-			     const struct script_language *language);
+extern void auto_load_objfile_script (struct objfile *objfile,
+				      const struct script_language_defn *);
 extern void load_auto_scripts_for_objfile (struct objfile *objfile);
-extern int
-  script_not_found_warning_print (struct auto_load_pspace_info *pspace_info);
 extern char auto_load_info_scripts_pattern_nl[];
 extern void auto_load_info_scripts (char *pattern, int from_tty,
-				    const struct script_language *language);
+				    const struct script_language_defn *);
 
 extern struct cmd_list_element **auto_load_set_cmdlist_get (void);
 extern struct cmd_list_element **auto_load_show_cmdlist_get (void);
@@ -66,4 +47,7 @@ extern struct cmd_list_element **auto_load_info_cmdlist_get (void);
 extern int file_is_auto_load_safe (const char *filename,
 				   const char *debug_fmt, ...);
 
+extern int auto_load_gdb_scripts_enabled
+  (const struct script_language_defn *slang);
+
 #endif /* AUTO_LOAD_H */
diff --git a/gdb/python/py-auto-load.c b/gdb/python/py-auto-load.c
index d5450b3..4ff4134 100644
--- a/gdb/python/py-auto-load.c
+++ b/gdb/python/py-auto-load.c
@@ -25,9 +25,6 @@
 #include "objfiles.h"
 #include "python.h"
 #include "auto-load.h"
-
-#ifdef HAVE_PYTHON
-
 #include "python-internal.h"
 
 /* User-settable option to enable/disable auto-loading of Python scripts:
@@ -45,32 +42,15 @@ show_auto_load_python_scripts (struct ui_file *file, int from_tty,
   fprintf_filtered (file, _("Auto-loading of Python scripts is %s.\n"), value);
 }
 
-/* Return non-zero if auto-loading Python scripts is enabled.  */
+/* Return non-zero if auto-loading Python scripts is enabled.
+   This is the script_lang_interface.auto_load_enabled "method".  */
 
-static int
-auto_load_python_scripts_enabled (void)
+int
+gdbpy_auto_load_enabled (const struct script_language_defn *slang)
 {
   return auto_load_python_scripts;
 }
 
-/* Definition of script language for Python scripts.  */
-
-static const struct script_language script_language_python =
-{
-  "python",
-  GDBPY_AUTO_FILE_NAME,
-  auto_load_python_scripts_enabled,
-  source_python_script_for_objfile
-};
-
-/* Return the Python script language definition.  */
-
-const struct script_language *
-gdbpy_script_language_defn (void)
-{
-  return &script_language_python;
-}
-
 /* Wrapper for "info auto-load python-scripts".  */
 
 static void
@@ -125,16 +105,3 @@ Print the list of automatically loaded Python scripts, deprecated."));
 
   return 0;
 }
-
-#else /* ! HAVE_PYTHON */
-
-/* Return the Python script language definition.
-   Since support isn't compiled in, return NULL.  */
-
-const struct script_language *
-gdbpy_script_language_defn (void)
-{
-  return NULL;
-}
-
-#endif /* ! HAVE_PYTHON */



More information about the Gdb-patches mailing list