Jan Kratochvil dcb8ac2
http://sourceware.org/ml/gdb-cvs/2011-05/msg00101.html
Jan Kratochvil dcb8ac2
Jan Kratochvil dcb8ac2
### src/gdb/ChangeLog	2011/05/13 22:07:56	1.13005
Jan Kratochvil dcb8ac2
### src/gdb/ChangeLog	2011/05/13 22:11:45	1.13006
Jan Kratochvil dcb8ac2
## -1,3 +1,24 @@
Jan Kratochvil dcb8ac2
+2011-05-13  Doug Evans  <dje@google.com>
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+	* NEWS: Mention "info auto-load-scripts".
Jan Kratochvil dcb8ac2
+	* python/py-auto-load.c (struct auto_load_pspace_info): New member
Jan Kratochvil dcb8ac2
+	script_not_found_warning_printed.
Jan Kratochvil dcb8ac2
+	(init_loaded_scripts_info): Renamed from create_loaded_scripts_hash,
Jan Kratochvil dcb8ac2
+	all callers updated.  Initialize script_not_found_warning_printed.
Jan Kratochvil dcb8ac2
+	(get_auto_load_pspace_data_for_loading): New function.
Jan Kratochvil dcb8ac2
+	(maybe_add_script): New function.
Jan Kratochvil dcb8ac2
+	(source_section_scripts): Simplify.  Only print one warning regardless
Jan Kratochvil dcb8ac2
+	of the number of auto-load scripts not found.
Jan Kratochvil dcb8ac2
+	(clear_section_scripts): Clear script_not_found_warning_printed.
Jan Kratochvil dcb8ac2
+	(auto_load_objfile_script): Record script in hash table.
Jan Kratochvil dcb8ac2
+	(count_matching_scripts): New function.
Jan Kratochvil dcb8ac2
+	(maybe_print_script): Renamed from maybe_print_section_script, all
Jan Kratochvil dcb8ac2
+	callers updated.  Rewrite to use ui_out_*.
Jan Kratochvil dcb8ac2
+	(info_auto_load_scripts): Renamed from
Jan Kratochvil dcb8ac2
+	maintenance_print_section_scripts, all callers updated.
Jan Kratochvil dcb8ac2
+	(gdbpy_initialize_auto_load): "maintenance print section-scripts"
Jan Kratochvil dcb8ac2
+	renamed as "info auto-load-scripts".
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
 2011-05-13  Tom Tromey  <tromey@redhat.com>
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 	* dwarf2expr.c (read_uleb128): Cast intermediate result.
Jan Kratochvil dcb8ac2
Index: gdb-7.3.1/gdb/NEWS
Jan Kratochvil dcb8ac2
===================================================================
Jan Kratochvil dcb8ac2
--- gdb-7.3.1.orig/gdb/NEWS	2012-04-20 22:33:54.000000000 +0200
Jan Kratochvil dcb8ac2
+++ gdb-7.3.1/gdb/NEWS	2012-04-20 22:34:14.280929212 +0200
Jan Kratochvil dcb8ac2
@@ -8,6 +8,10 @@
Jan Kratochvil dcb8ac2
   'data-directory'/python/gdb/function are now automatically loaded
Jan Kratochvil dcb8ac2
    on GDB start-up.
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
+info auto-load-scripts [REGEXP]
Jan Kratochvil dcb8ac2
+  This command was formerly named "maintenance print section-scripts".
Jan Kratochvil dcb8ac2
+  It is now generally useful and is no longer a maintenance-only command.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
 *** Changes in GDB 7.3.1
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 * The build failure for NetBSD and OpenBSD targets have now been fixed.
Jan Kratochvil dcb8ac2
Index: gdb-7.3.1/gdb/doc/gdb.texinfo
Jan Kratochvil dcb8ac2
===================================================================
Jan Kratochvil dcb8ac2
--- gdb-7.3.1.orig/gdb/doc/gdb.texinfo	2012-04-20 22:33:54.000000000 +0200
Jan Kratochvil dcb8ac2
+++ gdb-7.3.1/gdb/doc/gdb.texinfo	2012-04-20 22:34:03.663955649 +0200
Jan Kratochvil dcb8ac2
@@ -23673,7 +23673,8 @@ command, or because the inferior has loa
Jan Kratochvil dcb8ac2
 The auto-loading feature is useful for supplying application-specific
Jan Kratochvil dcb8ac2
 debugging commands and scripts.
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-Auto-loading can be enabled or disabled.
Jan Kratochvil dcb8ac2
+Auto-loading can be enabled or disabled,
Jan Kratochvil dcb8ac2
+and the list of auto-loaded scripts can be printed.
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 @table @code
Jan Kratochvil dcb8ac2
 @kindex set auto-load-scripts
Jan Kratochvil dcb8ac2
@@ -23683,6 +23684,19 @@ Enable or disable the auto-loading of Py
Jan Kratochvil dcb8ac2
 @kindex show auto-load-scripts
Jan Kratochvil dcb8ac2
 @item show auto-load-scripts
Jan Kratochvil dcb8ac2
 Show whether auto-loading of Python scripts is enabled or disabled.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+@kindex info auto-load-scripts
Jan Kratochvil dcb8ac2
+@cindex print list of auto-loaded scripts
Jan Kratochvil dcb8ac2
+@item info auto-load-scripts [@var{regexp}]
Jan Kratochvil dcb8ac2
+Print the list of all scripts that gdb auto-loaded, or tried to auto-load.
Jan Kratochvil dcb8ac2
+If @var{regexp} is supplied only scripts with matching names are printed.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+@smallexample
Jan Kratochvil dcb8ac2
+(gdb) info auto-load-scripts
Jan Kratochvil dcb8ac2
+Loaded Script                                                                 
Jan Kratochvil dcb8ac2
+  Yes  py-section-script.py
Jan Kratochvil dcb8ac2
+	full name: /tmp/py-section-script.py
Jan Kratochvil dcb8ac2
+@end smallexample
Jan Kratochvil dcb8ac2
 @end table
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 When reading an auto-loaded file, @value{GDBN} sets the
Jan Kratochvil dcb8ac2
Index: gdb-7.3.1/gdb/python/py-auto-load.c
Jan Kratochvil dcb8ac2
===================================================================
Jan Kratochvil dcb8ac2
--- gdb-7.3.1.orig/gdb/python/py-auto-load.c	2011-01-06 01:57:04.000000000 +0100
Jan Kratochvil dcb8ac2
+++ gdb-7.3.1/gdb/python/py-auto-load.c	2012-04-20 22:34:03.664955646 +0200
Jan Kratochvil dcb8ac2
@@ -18,6 +18,7 @@
Jan Kratochvil dcb8ac2
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 #include "defs.h"
Jan Kratochvil dcb8ac2
+#include "filenames.h"
Jan Kratochvil dcb8ac2
 #include "gdb_string.h"
Jan Kratochvil dcb8ac2
 #include "gdb_regex.h"
Jan Kratochvil dcb8ac2
 #include "top.h"
Jan Kratochvil dcb8ac2
@@ -68,11 +69,15 @@ struct auto_load_pspace_info
Jan Kratochvil dcb8ac2
 {
Jan Kratochvil dcb8ac2
   /* For each program space we keep track of loaded scripts.  */
Jan Kratochvil dcb8ac2
   struct htab *loaded_scripts;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  /* Non-zero if we've issued the warning about an auto-load script not being
Jan Kratochvil dcb8ac2
+     found.  We only want to issue this warning once.  */
Jan Kratochvil dcb8ac2
+  int script_not_found_warning_printed;
Jan Kratochvil dcb8ac2
 };
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 /* Objects of this type are stored in the loaded script hash table.  */
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-struct loaded_script_entry
Jan Kratochvil dcb8ac2
+struct loaded_script
Jan Kratochvil dcb8ac2
 {
Jan Kratochvil dcb8ac2
   /* Name as provided by the objfile.  */
Jan Kratochvil dcb8ac2
   const char *name;
Jan Kratochvil dcb8ac2
@@ -132,7 +137,7 @@ get_auto_load_pspace_data (struct progra
Jan Kratochvil dcb8ac2
 static hashval_t
Jan Kratochvil dcb8ac2
 hash_loaded_script_entry (const void *data)
Jan Kratochvil dcb8ac2
 {
Jan Kratochvil dcb8ac2
-  const struct loaded_script_entry *e = data;
Jan Kratochvil dcb8ac2
+  const struct loaded_script *e = data;
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
   return htab_hash_string (e->name);
Jan Kratochvil dcb8ac2
 }
Jan Kratochvil dcb8ac2
@@ -142,17 +147,17 @@ hash_loaded_script_entry (const void *da
Jan Kratochvil dcb8ac2
 static int
Jan Kratochvil dcb8ac2
 eq_loaded_script_entry (const void *a, const void *b)
Jan Kratochvil dcb8ac2
 {
Jan Kratochvil dcb8ac2
-  const struct loaded_script_entry *ea = a;
Jan Kratochvil dcb8ac2
-  const struct loaded_script_entry *eb = b;
Jan Kratochvil dcb8ac2
+  const struct loaded_script *ea = a;
Jan Kratochvil dcb8ac2
+  const struct loaded_script *eb = b;
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
   return strcmp (ea->name, eb->name) == 0;
Jan Kratochvil dcb8ac2
 }
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-/* Create the hash table used for loaded scripts.
Jan Kratochvil dcb8ac2
+/* Initialize the table to track loaded scripts.
Jan Kratochvil dcb8ac2
    Each entry is hashed by the full path name.  */
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 static void
Jan Kratochvil dcb8ac2
-create_loaded_scripts_hash (struct auto_load_pspace_info *pspace_info)
Jan Kratochvil dcb8ac2
+init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
Jan Kratochvil dcb8ac2
 {
Jan Kratochvil dcb8ac2
   /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
Jan Kratochvil dcb8ac2
      Space for each entry is obtained with one malloc so we can free them
Jan Kratochvil dcb8ac2
@@ -162,6 +167,64 @@ create_loaded_scripts_hash (struct auto_
Jan Kratochvil dcb8ac2
 					     hash_loaded_script_entry,
Jan Kratochvil dcb8ac2
 					     eq_loaded_script_entry,
Jan Kratochvil dcb8ac2
 					     xfree);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  pspace_info->script_not_found_warning_printed = FALSE;
Jan Kratochvil dcb8ac2
+}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+/* Wrapper on get_auto_load_pspace_data to also allocate the hash table
Jan Kratochvil dcb8ac2
+   for loading scripts.  */
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+static struct auto_load_pspace_info *
Jan Kratochvil dcb8ac2
+get_auto_load_pspace_data_for_loading (struct program_space *pspace)
Jan Kratochvil dcb8ac2
+{
Jan Kratochvil dcb8ac2
+  struct auto_load_pspace_info *info;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  info = get_auto_load_pspace_data (pspace);
Jan Kratochvil dcb8ac2
+  if (info->loaded_scripts == NULL)
Jan Kratochvil dcb8ac2
+    init_loaded_scripts_info (info);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  return info;
Jan Kratochvil dcb8ac2
+}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+/* Add script NAME to hash table HTAB.
Jan Kratochvil dcb8ac2
+   FULL_PATH is NULL if the script wasn't found.
Jan Kratochvil dcb8ac2
+   The result is true if the script was already in the hash table.  */
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+static int
Jan Kratochvil dcb8ac2
+maybe_add_script (struct htab *htab, const char *name, const char *full_path)
Jan Kratochvil dcb8ac2
+{
Jan Kratochvil dcb8ac2
+  struct loaded_script **slot, entry;
Jan Kratochvil dcb8ac2
+  int in_hash_table;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  entry.name = name;
Jan Kratochvil dcb8ac2
+  entry.full_path = full_path;
Jan Kratochvil dcb8ac2
+  slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
Jan Kratochvil dcb8ac2
+  in_hash_table = *slot != NULL;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  /* If this script is not in the hash table, add it.  */
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  if (! in_hash_table)
Jan Kratochvil dcb8ac2
+    {
Jan Kratochvil dcb8ac2
+      char *p;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+      /* Allocate all space in one chunk so it's easier to free.  */
Jan Kratochvil dcb8ac2
+      *slot = xmalloc (sizeof (**slot)
Jan Kratochvil dcb8ac2
+		       + strlen (name) + 1
Jan Kratochvil dcb8ac2
+		       + (full_path != NULL ? (strlen (full_path) + 1) : 0));
Jan Kratochvil dcb8ac2
+      p = ((char*) *slot) + sizeof (**slot);
Jan Kratochvil dcb8ac2
+      strcpy (p, name);
Jan Kratochvil dcb8ac2
+      (*slot)->name = p;
Jan Kratochvil dcb8ac2
+      if (full_path != NULL)
Jan Kratochvil dcb8ac2
+	{
Jan Kratochvil dcb8ac2
+	  p += strlen (p) + 1;
Jan Kratochvil dcb8ac2
+	  strcpy (p, full_path);
Jan Kratochvil dcb8ac2
+	  (*slot)->full_path = p;
Jan Kratochvil dcb8ac2
+	}
Jan Kratochvil dcb8ac2
+      else
Jan Kratochvil dcb8ac2
+	(*slot)->full_path = NULL;
Jan Kratochvil dcb8ac2
+    }
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  return in_hash_table;
Jan Kratochvil dcb8ac2
 }
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 /* Load scripts specified in OBJFILE.
Jan Kratochvil dcb8ac2
@@ -182,11 +245,8 @@ source_section_scripts (struct objfile *
Jan Kratochvil dcb8ac2
 {
Jan Kratochvil dcb8ac2
   const char *p;
Jan Kratochvil dcb8ac2
   struct auto_load_pspace_info *pspace_info;
Jan Kratochvil dcb8ac2
-  struct loaded_script_entry **slot, entry;
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-  pspace_info = get_auto_load_pspace_data (current_program_space);
Jan Kratochvil dcb8ac2
-  if (pspace_info->loaded_scripts == NULL)
Jan Kratochvil dcb8ac2
-    create_loaded_scripts_hash (pspace_info);
Jan Kratochvil dcb8ac2
+  pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
   for (p = start; p < end; ++p)
Jan Kratochvil dcb8ac2
     {
Jan Kratochvil dcb8ac2
@@ -226,51 +286,29 @@ source_section_scripts (struct objfile *
Jan Kratochvil dcb8ac2
       opened = find_and_open_script (file, 1 /*search_path*/,
Jan Kratochvil dcb8ac2
 				     &stream, &full_path);
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-      /* If the file is not found, we still record the file in the hash table,
Jan Kratochvil dcb8ac2
-	 we only want to print an error message once.
Jan Kratochvil dcb8ac2
-	 IWBN if complaints.c were more general-purpose.  */
Jan Kratochvil dcb8ac2
+      /* If one script isn't found it's not uncommon for more to not be
Jan Kratochvil dcb8ac2
+	 found either.  We don't want to print an error message for each
Jan Kratochvil dcb8ac2
+	 script, too much noise.  Instead, we print the warning once and tell
Jan Kratochvil dcb8ac2
+	 the user how to find the list of scripts that weren't loaded.
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-      entry.name = file;
Jan Kratochvil dcb8ac2
-      if (opened)
Jan Kratochvil dcb8ac2
-	entry.full_path = full_path;
Jan Kratochvil dcb8ac2
-      else
Jan Kratochvil dcb8ac2
-	entry.full_path = NULL;
Jan Kratochvil dcb8ac2
-      slot = ((struct loaded_script_entry **)
Jan Kratochvil dcb8ac2
-	      htab_find_slot (pspace_info->loaded_scripts,
Jan Kratochvil dcb8ac2
-			      &entry, INSERT));
Jan Kratochvil dcb8ac2
-      in_hash_table = *slot != NULL;
Jan Kratochvil dcb8ac2
-
Jan Kratochvil dcb8ac2
-      /* If this file is not in the hash table, add it.  */
Jan Kratochvil dcb8ac2
-      if (! in_hash_table)
Jan Kratochvil dcb8ac2
-	{
Jan Kratochvil dcb8ac2
-	  char *p;
Jan Kratochvil dcb8ac2
+	 IWBN if complaints.c were more general-purpose.  */
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-	  *slot = xmalloc (sizeof (**slot)
Jan Kratochvil dcb8ac2
-			   + strlen (file) + 1
Jan Kratochvil dcb8ac2
-			   + (opened ? (strlen (full_path) + 1) : 0));
Jan Kratochvil dcb8ac2
-	  p = ((char*) *slot) + sizeof (**slot);
Jan Kratochvil dcb8ac2
-	  strcpy (p, file);
Jan Kratochvil dcb8ac2
-	  (*slot)->name = p;
Jan Kratochvil dcb8ac2
-	  if (opened)
Jan Kratochvil dcb8ac2
-	    {
Jan Kratochvil dcb8ac2
-	      p += strlen (p) + 1;
Jan Kratochvil dcb8ac2
-	      strcpy (p, full_path);
Jan Kratochvil dcb8ac2
-	      (*slot)->full_path = p;
Jan Kratochvil dcb8ac2
-	    }
Jan Kratochvil dcb8ac2
-	  else
Jan Kratochvil dcb8ac2
-	    (*slot)->full_path = NULL;
Jan Kratochvil dcb8ac2
-	}
Jan Kratochvil dcb8ac2
+      in_hash_table = maybe_add_script (pspace_info->loaded_scripts, file,
Jan Kratochvil dcb8ac2
+					opened ? full_path : NULL);
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
       if (opened)
Jan Kratochvil dcb8ac2
 	free (full_path);
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
       if (! opened)
Jan Kratochvil dcb8ac2
 	{
Jan Kratochvil dcb8ac2
-	  /* We don't throw an error, the program is still debuggable.
Jan Kratochvil dcb8ac2
-	     Check in_hash_table to only print the warning once.  */
Jan Kratochvil dcb8ac2
-	  if (! in_hash_table)
Jan Kratochvil dcb8ac2
-	    warning (_("%s (referenced in %s): %s"),
Jan Kratochvil dcb8ac2
-		     file, GDBPY_AUTO_SECTION_NAME, safe_strerror (errno));
Jan Kratochvil dcb8ac2
+	  /* We don't throw an error, the program is still debuggable.  */
Jan Kratochvil dcb8ac2
+	  if (! pspace_info->script_not_found_warning_printed)
Jan Kratochvil dcb8ac2
+	    {
Jan Kratochvil dcb8ac2
+	      warning (_("Missing auto-load scripts referenced in %s.\n\
Jan Kratochvil dcb8ac2
+Use `info auto-load-scripts [REGEXP]' to list them."),
Jan Kratochvil dcb8ac2
+		       GDBPY_AUTO_SECTION_NAME);
Jan Kratochvil dcb8ac2
+	      pspace_info->script_not_found_warning_printed = TRUE;
Jan Kratochvil dcb8ac2
+	    }
Jan Kratochvil dcb8ac2
 	  continue;
Jan Kratochvil dcb8ac2
 	}
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
@@ -322,6 +360,7 @@ clear_section_scripts (void)
Jan Kratochvil dcb8ac2
     {
Jan Kratochvil dcb8ac2
       htab_delete (info->loaded_scripts);
Jan Kratochvil dcb8ac2
       info->loaded_scripts = NULL;
Jan Kratochvil dcb8ac2
+      info->script_not_found_warning_printed = FALSE;
Jan Kratochvil dcb8ac2
     }
Jan Kratochvil dcb8ac2
 }
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
@@ -378,6 +417,19 @@ auto_load_objfile_script (struct objfile
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
   if (input)
Jan Kratochvil dcb8ac2
     {
Jan Kratochvil dcb8ac2
+      struct auto_load_pspace_info *pspace_info;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+      /* Add this script to the hash table too so "info auto-load-scripts"
Jan Kratochvil dcb8ac2
+	 can print it.  */
Jan Kratochvil dcb8ac2
+      pspace_info =
Jan Kratochvil dcb8ac2
+	get_auto_load_pspace_data_for_loading (current_program_space);
Jan Kratochvil dcb8ac2
+      maybe_add_script (pspace_info->loaded_scripts, debugfile, debugfile);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+      /* To preserve existing behaviour we don't check for whether the
Jan Kratochvil dcb8ac2
+	 script was already in the table, and always load it.
Jan Kratochvil dcb8ac2
+	 It's highly unlikely that we'd ever load it twice,
Jan Kratochvil dcb8ac2
+	 and these scripts are required to be idempotent under multiple
Jan Kratochvil dcb8ac2
+	 loads anyway.  */
Jan Kratochvil dcb8ac2
       source_python_script_for_objfile (objfile, input, debugfile);
Jan Kratochvil dcb8ac2
       fclose (input);
Jan Kratochvil dcb8ac2
     }
Jan Kratochvil dcb8ac2
@@ -416,56 +468,133 @@ load_auto_scripts_for_objfile (struct ob
Jan Kratochvil dcb8ac2
     }
Jan Kratochvil dcb8ac2
 }
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
+/* Collect scripts to be printed in a vec.  */
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+typedef struct loaded_script *loaded_script_ptr;
Jan Kratochvil dcb8ac2
+DEF_VEC_P (loaded_script_ptr);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
 /* Traversal function for htab_traverse.
Jan Kratochvil dcb8ac2
-   Print the entry if specified in the regex.  */
Jan Kratochvil dcb8ac2
+   Collect the entry if it matches the regexp.  */
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 static int
Jan Kratochvil dcb8ac2
-maybe_print_section_script (void **slot, void *info)
Jan Kratochvil dcb8ac2
+collect_matching_scripts (void **slot, void *info)
Jan Kratochvil dcb8ac2
+{
Jan Kratochvil dcb8ac2
+  struct loaded_script *script = *slot;
Jan Kratochvil dcb8ac2
+  VEC (loaded_script_ptr) *scripts = info;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  if (re_exec (script->name))
Jan Kratochvil dcb8ac2
+    VEC_safe_push (loaded_script_ptr, scripts, script);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  return 1;
Jan Kratochvil dcb8ac2
+}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+/* Print SCRIPT.  */
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+static void
Jan Kratochvil dcb8ac2
+print_script (struct loaded_script *script)
Jan Kratochvil dcb8ac2
 {
Jan Kratochvil dcb8ac2
-  struct loaded_script_entry *entry = *slot;
Jan Kratochvil dcb8ac2
+  struct cleanup *chain;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-  if (re_exec (entry->name))
Jan Kratochvil dcb8ac2
+  ui_out_field_string (uiout, "loaded", script->full_path ? "Yes" : "No");
Jan Kratochvil dcb8ac2
+  ui_out_field_string (uiout, "script", script->name);
Jan Kratochvil dcb8ac2
+  ui_out_text (uiout, "\n");
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  /* If the name isn't the full path, print it too.  */
Jan Kratochvil dcb8ac2
+  if (script->full_path != NULL
Jan Kratochvil dcb8ac2
+      && strcmp (script->name, script->full_path) != 0)
Jan Kratochvil dcb8ac2
     {
Jan Kratochvil dcb8ac2
-      printf_filtered (_("Script name: %s\n"), entry->name);
Jan Kratochvil dcb8ac2
-      printf_filtered (_("  Full name: %s\n"),
Jan Kratochvil dcb8ac2
-		       entry->full_path ? entry->full_path : _("unknown"));
Jan Kratochvil dcb8ac2
+      ui_out_text (uiout, "\tfull name: ");
Jan Kratochvil dcb8ac2
+      ui_out_field_string (uiout, "full_path", script->full_path);
Jan Kratochvil dcb8ac2
+      ui_out_text (uiout, "\n");
Jan Kratochvil dcb8ac2
     }
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-  return 1;
Jan Kratochvil dcb8ac2
+  do_cleanups (chain);
Jan Kratochvil dcb8ac2
 }
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-/* "maint print section-scripts" command.  */
Jan Kratochvil dcb8ac2
+/* Helper for info_auto_load_scripts to sort the scripts by name.  */
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+static int
Jan Kratochvil dcb8ac2
+sort_scripts_by_name (const void *ap, const void *bp)
Jan Kratochvil dcb8ac2
+{
Jan Kratochvil dcb8ac2
+  const struct loaded_script *a = *(const struct loaded_script **) ap;
Jan Kratochvil dcb8ac2
+  const struct loaded_script *b = *(const struct loaded_script **) bp;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  return FILENAME_CMP (a->name, b->name);
Jan Kratochvil dcb8ac2
+}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+/* "info auto-load-scripts" command.  */
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 static void
Jan Kratochvil dcb8ac2
-maintenance_print_section_scripts (char *pattern, int from_tty)
Jan Kratochvil dcb8ac2
+info_auto_load_scripts (char *pattern, int from_tty)
Jan Kratochvil dcb8ac2
 {
Jan Kratochvil dcb8ac2
   struct auto_load_pspace_info *pspace_info;
Jan Kratochvil dcb8ac2
+  struct cleanup *script_chain;
Jan Kratochvil dcb8ac2
+  VEC (loaded_script_ptr) *scripts;
Jan Kratochvil dcb8ac2
+  int nr_scripts;
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
   dont_repeat ();
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
+  pspace_info = get_auto_load_pspace_data (current_program_space);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
   if (pattern && *pattern)
Jan Kratochvil dcb8ac2
     {
Jan Kratochvil dcb8ac2
       char *re_err = re_comp (pattern);
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
       if (re_err)
Jan Kratochvil dcb8ac2
 	error (_("Invalid regexp: %s"), re_err);
Jan Kratochvil dcb8ac2
-
Jan Kratochvil dcb8ac2
-      printf_filtered (_("Objfile scripts matching %s:\n"), pattern);
Jan Kratochvil dcb8ac2
     }
Jan Kratochvil dcb8ac2
   else
Jan Kratochvil dcb8ac2
     {
Jan Kratochvil dcb8ac2
       re_comp ("");
Jan Kratochvil dcb8ac2
-      printf_filtered (_("Objfile scripts:\n"));
Jan Kratochvil dcb8ac2
     }
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-  pspace_info = get_auto_load_pspace_data (current_program_space);
Jan Kratochvil dcb8ac2
-  if (pspace_info == NULL || pspace_info->loaded_scripts == NULL)
Jan Kratochvil dcb8ac2
-    return;
Jan Kratochvil dcb8ac2
+  /* We need to know the number of rows before we build the table.
Jan Kratochvil dcb8ac2
+     Plus we want to sort the scripts by name.
Jan Kratochvil dcb8ac2
+     So first traverse the hash table collecting the matching scripts.  */
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  scripts = VEC_alloc (loaded_script_ptr, 10);
Jan Kratochvil dcb8ac2
+  script_chain = make_cleanup (VEC_cleanup (loaded_script_ptr), &scripts);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  if (pspace_info != NULL && pspace_info->loaded_scripts != NULL)
Jan Kratochvil dcb8ac2
+    {
Jan Kratochvil dcb8ac2
+      immediate_quit++;
Jan Kratochvil dcb8ac2
+      htab_traverse_noresize (pspace_info->loaded_scripts,
Jan Kratochvil dcb8ac2
+			      collect_matching_scripts, scripts);
Jan Kratochvil dcb8ac2
+      immediate_quit--;
Jan Kratochvil dcb8ac2
+    }
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  nr_scripts = VEC_length (loaded_script_ptr, scripts);
Jan Kratochvil dcb8ac2
+  make_cleanup_ui_out_table_begin_end (uiout, 2, nr_scripts,
Jan Kratochvil dcb8ac2
+				       "AutoLoadedScriptsTable");
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-  immediate_quit++;
Jan Kratochvil dcb8ac2
-  htab_traverse_noresize (pspace_info->loaded_scripts,
Jan Kratochvil dcb8ac2
-			  maybe_print_section_script, NULL);
Jan Kratochvil dcb8ac2
-  immediate_quit--;
Jan Kratochvil dcb8ac2
+  ui_out_table_header (uiout, 6, ui_center, "loaded", "Loaded");
Jan Kratochvil dcb8ac2
+  ui_out_table_header (uiout, 70, ui_left, "script", "Script");
Jan Kratochvil dcb8ac2
+  ui_out_table_body (uiout);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  if (nr_scripts > 0)
Jan Kratochvil dcb8ac2
+    {
Jan Kratochvil dcb8ac2
+      int i;
Jan Kratochvil dcb8ac2
+      loaded_script_ptr script;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+      qsort (VEC_address (loaded_script_ptr, scripts),
Jan Kratochvil dcb8ac2
+	     VEC_length (loaded_script_ptr, scripts),
Jan Kratochvil dcb8ac2
+	     sizeof (loaded_script_ptr), sort_scripts_by_name);
Jan Kratochvil dcb8ac2
+      for (i = 0; VEC_iterate (loaded_script_ptr, scripts, i, script); ++i)
Jan Kratochvil dcb8ac2
+	print_script (script);
Jan Kratochvil dcb8ac2
+    }
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  do_cleanups (script_chain);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  if (nr_scripts == 0)
Jan Kratochvil dcb8ac2
+    {
Jan Kratochvil dcb8ac2
+      if (pattern && *pattern)
Jan Kratochvil dcb8ac2
+	ui_out_message (uiout, 0, "No auto-load scripts matching %s.\n",
Jan Kratochvil dcb8ac2
+			pattern);
Jan Kratochvil dcb8ac2
+      else
Jan Kratochvil dcb8ac2
+	ui_out_message (uiout, 0, "No auto-load scripts.\n");
Jan Kratochvil dcb8ac2
+    }
Jan Kratochvil dcb8ac2
 }
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 void
Jan Kratochvil dcb8ac2
@@ -486,10 +615,10 @@ an executable or shared library."),
Jan Kratochvil dcb8ac2
 			   &setlist,
Jan Kratochvil dcb8ac2
 			   &showlist);
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
-  add_cmd ("section-scripts", class_maintenance,
Jan Kratochvil dcb8ac2
-	   maintenance_print_section_scripts,
Jan Kratochvil dcb8ac2
-	   _("Print dump of auto-loaded section scripts matching REGEXP."),
Jan Kratochvil dcb8ac2
-	   &maintenanceprintlist);
Jan Kratochvil dcb8ac2
+  add_info ("auto-load-scripts",
Jan Kratochvil dcb8ac2
+	    info_auto_load_scripts,
Jan Kratochvil dcb8ac2
+	    _("Print the list of automatically loaded scripts.\n\
Jan Kratochvil dcb8ac2
+Usage: info auto-load-scripts [REGEXP]"));
Jan Kratochvil dcb8ac2
 }
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
 #else /* ! HAVE_PYTHON */
Jan Kratochvil dcb8ac2
Index: gdb-7.3.1/gdb/testsuite/gdb.python/py-objfile-script.c
Jan Kratochvil dcb8ac2
===================================================================
Jan Kratochvil dcb8ac2
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
Jan Kratochvil dcb8ac2
+++ gdb-7.3.1/gdb/testsuite/gdb.python/py-objfile-script.c	2012-04-20 22:34:03.664955646 +0200
Jan Kratochvil dcb8ac2
@@ -0,0 +1,39 @@
Jan Kratochvil dcb8ac2
+/* This testcase is part of GDB, the GNU debugger.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+   Copyright 2011 Free Software Foundation, Inc.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+   This program is free software; you can redistribute it and/or modify
Jan Kratochvil dcb8ac2
+   it under the terms of the GNU General Public License as published by
Jan Kratochvil dcb8ac2
+   the Free Software Foundation; either version 3 of the License, or
Jan Kratochvil dcb8ac2
+   (at your option) any later version.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+   This program is distributed in the hope that it will be useful,
Jan Kratochvil dcb8ac2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
Jan Kratochvil dcb8ac2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jan Kratochvil dcb8ac2
+   GNU General Public License for more details.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+   You should have received a copy of the GNU General Public License
Jan Kratochvil dcb8ac2
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+struct ss
Jan Kratochvil dcb8ac2
+{
Jan Kratochvil dcb8ac2
+  int a;
Jan Kratochvil dcb8ac2
+  int b;
Jan Kratochvil dcb8ac2
+};
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+void
Jan Kratochvil dcb8ac2
+init_ss (struct ss *s, int a, int b)
Jan Kratochvil dcb8ac2
+{
Jan Kratochvil dcb8ac2
+  s->a = a;
Jan Kratochvil dcb8ac2
+  s->b = b;
Jan Kratochvil dcb8ac2
+}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+int
Jan Kratochvil dcb8ac2
+main ()
Jan Kratochvil dcb8ac2
+{
Jan Kratochvil dcb8ac2
+  struct ss ss;
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  init_ss (&ss, 1, 2);
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+  return 0;      /* break to inspect struct and union */
Jan Kratochvil dcb8ac2
+}
Jan Kratochvil dcb8ac2
Index: gdb-7.3.1/gdb/testsuite/gdb.python/py-objfile-script.exp
Jan Kratochvil dcb8ac2
===================================================================
Jan Kratochvil dcb8ac2
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
Jan Kratochvil dcb8ac2
+++ gdb-7.3.1/gdb/testsuite/gdb.python/py-objfile-script.exp	2012-04-20 22:34:03.664955646 +0200
Jan Kratochvil dcb8ac2
@@ -0,0 +1,60 @@
Jan Kratochvil dcb8ac2
+# Copyright (C) 2011 Free Software Foundation, Inc.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+# This program is free software; you can redistribute it and/or modify
Jan Kratochvil dcb8ac2
+# it under the terms of the GNU General Public License as published by
Jan Kratochvil dcb8ac2
+# the Free Software Foundation; either version 3 of the License, or
Jan Kratochvil dcb8ac2
+# (at your option) any later version.
Jan Kratochvil dcb8ac2
+#
Jan Kratochvil dcb8ac2
+# This program is distributed in the hope that it will be useful,
Jan Kratochvil dcb8ac2
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
Jan Kratochvil dcb8ac2
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jan Kratochvil dcb8ac2
+# GNU General Public License for more details.
Jan Kratochvil dcb8ac2
+#
Jan Kratochvil dcb8ac2
+# You should have received a copy of the GNU General Public License
Jan Kratochvil dcb8ac2
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+# This file is part of the GDB testsuite.  It tests automagic loading of
Jan Kratochvil dcb8ac2
+# -gdb.py scripts.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+if $tracelevel then {
Jan Kratochvil dcb8ac2
+    strace $tracelevel
Jan Kratochvil dcb8ac2
+}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+set testfile "py-objfile-script"
Jan Kratochvil dcb8ac2
+set srcfile ${testfile}.c
Jan Kratochvil dcb8ac2
+set binfile ${objdir}/${subdir}/${testfile}
Jan Kratochvil dcb8ac2
+if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
Jan Kratochvil dcb8ac2
+    untested "Couldn't compile ${srcfile}"
Jan Kratochvil dcb8ac2
+    return -1
Jan Kratochvil dcb8ac2
+}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+# Start with a fresh gdb.
Jan Kratochvil dcb8ac2
+gdb_exit
Jan Kratochvil dcb8ac2
+gdb_start
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+# Skip all tests if Python scripting is not enabled.
Jan Kratochvil dcb8ac2
+if { [skip_python_tests] } { continue }
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+# Make the -gdb.py script available to gdb, it is automagically loaded by gdb.
Jan Kratochvil dcb8ac2
+# Care is taken to put it in the same directory as the binary so that
Jan Kratochvil dcb8ac2
+# gdb will find it.
Jan Kratochvil dcb8ac2
+set remote_python_file [remote_download host ${srcdir}/${subdir}/${testfile}-gdb.py ${subdir}/${testfile}-gdb.py]
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+gdb_reinitialize_dir $srcdir/$subdir
Jan Kratochvil dcb8ac2
+gdb_load ${binfile}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+# Verify gdb loaded the script.
Jan Kratochvil dcb8ac2
+gdb_test "info auto-load-scripts" "Yes.*/${testfile}-gdb.py.*"
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+if ![runto_main] {
Jan Kratochvil dcb8ac2
+    perror "couldn't run to main"
Jan Kratochvil dcb8ac2
+    return
Jan Kratochvil dcb8ac2
+}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+gdb_test "b [gdb_get_line_number {break to inspect} ${testfile}.c ]" \
Jan Kratochvil dcb8ac2
+    ".*Breakpoint.*"
Jan Kratochvil dcb8ac2
+gdb_test "continue" ".*Breakpoint.*"
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+gdb_test "print ss" " = a=<1> b=<2>"
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+remote_file host delete ${remote_python_file}
Jan Kratochvil dcb8ac2
Index: gdb-7.3.1/gdb/testsuite/gdb.python/py-section-script.exp
Jan Kratochvil dcb8ac2
===================================================================
Jan Kratochvil dcb8ac2
--- gdb-7.3.1.orig/gdb/testsuite/gdb.python/py-section-script.exp	2011-03-13 14:39:17.000000000 +0100
Jan Kratochvil dcb8ac2
+++ gdb-7.3.1/gdb/testsuite/gdb.python/py-section-script.exp	2012-04-20 22:34:03.664955646 +0200
Jan Kratochvil dcb8ac2
@@ -55,6 +55,14 @@ set remote_python_file [remote_download
Jan Kratochvil dcb8ac2
 gdb_reinitialize_dir $srcdir/$subdir
Jan Kratochvil dcb8ac2
 gdb_load ${binfile}
Jan Kratochvil dcb8ac2
 
Jan Kratochvil dcb8ac2
+# Verify gdb loaded the script.
Jan Kratochvil dcb8ac2
+gdb_test "info auto-load-scripts" "Yes.*${testfile}.py.*full name: .*/${testfile}.py.*"
Jan Kratochvil dcb8ac2
+# Again, with a regexp this time.
Jan Kratochvil dcb8ac2
+gdb_test "info auto-load-scripts ${testfile}" "Yes.*${testfile}.py.*full name: .*/${testfile}.py.*"
Jan Kratochvil dcb8ac2
+# Again, with a regexp that matches no scripts.
Jan Kratochvil dcb8ac2
+gdb_test "info auto-load-scripts no-script-matches-this" \
Jan Kratochvil dcb8ac2
+  "No auto-load scripts matching no-script-matches-this."
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
 if ![runto_main] {
Jan Kratochvil dcb8ac2
     perror "couldn't run to main"
Jan Kratochvil dcb8ac2
     return
Jan Kratochvil dcb8ac2
diff --git a/gdb/testsuite/gdb.python/py-objfile-script-gdb.py b/gdb/testsuite/gdb.python/py-objfile-script-gdb.py
Jan Kratochvil dcb8ac2
new file mode 100644
Jan Kratochvil dcb8ac2
index 0000000..c54e9ec
Jan Kratochvil dcb8ac2
--- /dev/null
Jan Kratochvil dcb8ac2
+++ b/gdb/testsuite/gdb.python/py-objfile-script-gdb.py
Jan Kratochvil dcb8ac2
@@ -0,0 +1,63 @@
Jan Kratochvil dcb8ac2
+# Copyright (C) 2011 Free Software Foundation, Inc.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+# This program is free software; you can redistribute it and/or modify
Jan Kratochvil dcb8ac2
+# it under the terms of the GNU General Public License as published by
Jan Kratochvil dcb8ac2
+# the Free Software Foundation; either version 3 of the License, or
Jan Kratochvil dcb8ac2
+# (at your option) any later version.
Jan Kratochvil dcb8ac2
+#
Jan Kratochvil dcb8ac2
+# This program is distributed in the hope that it will be useful,
Jan Kratochvil dcb8ac2
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
Jan Kratochvil dcb8ac2
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jan Kratochvil dcb8ac2
+# GNU General Public License for more details.
Jan Kratochvil dcb8ac2
+#
Jan Kratochvil dcb8ac2
+# You should have received a copy of the GNU General Public License
Jan Kratochvil dcb8ac2
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+# This file is part of the GDB testsuite.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+import re
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+class pp_ss:
Jan Kratochvil dcb8ac2
+    def __init__(self, val):
Jan Kratochvil dcb8ac2
+        self.val = val
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+    def to_string(self):
Jan Kratochvil dcb8ac2
+        return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+def lookup_function (val):
Jan Kratochvil dcb8ac2
+    "Look-up and return a pretty-printer that can print val."
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+    # Get the type.
Jan Kratochvil dcb8ac2
+    type = val.type
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+    # If it points to a reference, get the reference.
Jan Kratochvil dcb8ac2
+    if type.code == gdb.TYPE_CODE_REF:
Jan Kratochvil dcb8ac2
+        type = type.target ()
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+    # Get the unqualified type, stripped of typedefs.
Jan Kratochvil dcb8ac2
+    type = type.unqualified ().strip_typedefs ()
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+    # Get the type name.    
Jan Kratochvil dcb8ac2
+    typename = type.tag
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+    if typename == None:
Jan Kratochvil dcb8ac2
+        return None
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+    # Iterate over local dictionary of types to determine
Jan Kratochvil dcb8ac2
+    # if a printer is registered for that type.  Return an
Jan Kratochvil dcb8ac2
+    # instantiation of the printer if found.
Jan Kratochvil dcb8ac2
+    for function in pretty_printers_dict:
Jan Kratochvil dcb8ac2
+        if function.match (typename):
Jan Kratochvil dcb8ac2
+            return pretty_printers_dict[function] (val)
Jan Kratochvil dcb8ac2
+        
Jan Kratochvil dcb8ac2
+    # Cannot find a pretty printer.  Return None.
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+    return None
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+def register_pretty_printers ():
Jan Kratochvil dcb8ac2
+    pretty_printers_dict[re.compile ('^ss$')]  = pp_ss
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+pretty_printers_dict = {}
Jan Kratochvil dcb8ac2
+
Jan Kratochvil dcb8ac2
+register_pretty_printers ()
Jan Kratochvil dcb8ac2
+gdb.current_progspace().pretty_printers.append (lookup_function)