Provide useful completer for "info registers"

Message ID 87h9xnqje8.fsf@br87z6lw.de.ibm.com
State New, archived
Headers

Commit Message

Andreas Arnez Nov. 25, 2014, 5:28 p.m. UTC
  Provide a new completion function for the argument of "info
registers", "info all-registers", and the "lr" command in dbx mode.
Without this patch the default symbol completer is used, which is more
confusing than helpful.

gdb/ChangeLog:

	* completer.c: Include "target.h" and "reggroups.h".
	(reg_or_group_completer): New.
	* completer.h (reg_or_group_completer): Declare.
	* infcmd.c (_initialize_infcmd): Set reg_or_group_completer for
	the "info registers" and "info all-registers" commands and the
	dbx-mode "lr" command.
---
 gdb/completer.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/completer.h |    3 +++
 gdb/infcmd.c    |   12 +++++++++---
 3 files changed, 63 insertions(+), 3 deletions(-)
  

Comments

Pedro Alves Dec. 4, 2014, 5:38 p.m. UTC | #1
Also, ...

On 11/25/2014 05:28 PM, Andreas Arnez wrote:
> +    for (i = 0; i < n_regs; i++)
> +      {
> +	const char *reg_name = gdbarch_register_name (gdbarch, i);
> +
> +	if (reg_name != NULL && strncmp (text, reg_name, len) == 0)
> +	  VEC_safe_push (char_ptr, result, xstrdup (reg_name));

Not sure under which conditions "len" could be zero here.  If it may,
note that some registers have empty names.  See default_print_registers_info:

      /* If the register name is empty, it is undefined for this
         processor, so don't display anything.  */
      if (gdbarch_register_name (gdbarch, i) == NULL
	  || *(gdbarch_register_name (gdbarch, i)) == '\0')
	continue;

> +      }
> +  }


Thanks,
Pedro Alves
  
Andreas Arnez Dec. 10, 2014, 5:48 p.m. UTC | #2
On Thu, Dec 04 2014, Pedro Alves wrote:

> Also, ...
>
> On 11/25/2014 05:28 PM, Andreas Arnez wrote:
>> +    for (i = 0; i < n_regs; i++)
>> +      {
>> +	const char *reg_name = gdbarch_register_name (gdbarch, i);
>> +
>> +	if (reg_name != NULL && strncmp (text, reg_name, len) == 0)
>> +	  VEC_safe_push (char_ptr, result, xstrdup (reg_name));
>
> Not sure under which conditions "len" could be zero here.  If it may,
> note that some registers have empty names.  See default_print_registers_info:
>
>       /* If the register name is empty, it is undefined for this
>          processor, so don't display anything.  */
>       if (gdbarch_register_name (gdbarch, i) == NULL
> 	  || *(gdbarch_register_name (gdbarch, i)) == '\0')
> 	continue;
>
>> +      }
>> +  }

Good point.  This is fixed in the new version; registers with empty
names are skipped now.
  

Patch

diff --git a/gdb/completer.c b/gdb/completer.c
index a0f3fa3..42188c0 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -23,6 +23,8 @@ 
 #include "filenames.h"		/* For DOSish file names.  */
 #include "language.h"
 #include "gdb_signals.h"
+#include "target.h"
+#include "reggroups.h"
 
 #include "cli/cli-decode.h"
 
@@ -836,6 +838,55 @@  signal_completer (struct cmd_list_element *ignore,
   return return_val;
 }
 
+/* Complete on a register or reggroup.  */
+
+VEC (char_ptr) *
+reg_or_group_completer (struct cmd_list_element *ignore,
+			const char *text, const char *word)
+{
+  VEC (char_ptr) *result = NULL;
+  size_t len = strlen (text);
+  struct frame_info *frame;
+  struct gdbarch *gdbarch;
+
+  if (!target_has_registers)
+    return result;
+
+  frame = get_selected_frame (NULL);
+  gdbarch = get_frame_arch (frame);
+
+  {
+    int i;
+    int n_regs = (gdbarch_num_regs (gdbarch)
+		  + gdbarch_num_pseudo_regs (gdbarch));
+
+    for (i = 0; i < n_regs; i++)
+      {
+	const char *reg_name = gdbarch_register_name (gdbarch, i);
+
+	if (reg_name != NULL && strncmp (text, reg_name, len) == 0)
+	  VEC_safe_push (char_ptr, result, xstrdup (reg_name));
+      }
+  }
+
+  {
+    struct reggroup *group;
+
+    for (group = reggroup_next (gdbarch, NULL);
+	 group != NULL;
+	 group = reggroup_next (gdbarch, group))
+      {
+	const char *group_name = reggroup_name (group);
+
+	if (strncmp (text, group_name, len) == 0)
+	  VEC_safe_push (char_ptr, result, xstrdup (group_name));
+      }
+  }
+
+  return result;
+}
+
+
 /* Get the list of chars that are considered as word breaks
    for the current command.  */
 
diff --git a/gdb/completer.h b/gdb/completer.h
index bc7ed96..5e91030 100644
--- a/gdb/completer.h
+++ b/gdb/completer.h
@@ -45,6 +45,9 @@  extern VEC (char_ptr) *command_completer (struct cmd_list_element *,
 extern VEC (char_ptr) *signal_completer (struct cmd_list_element *,
 					 const char *, const char *);
 
+extern VEC (char_ptr) *reg_or_group_completer (struct cmd_list_element *,
+					       const char *, const char *);
+
 extern char *get_gdb_completer_quote_characters (void);
 
 extern char *gdb_completion_word_break_characters (void);
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 4415b31..de0d24d 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -3235,18 +3235,24 @@  If non-stop mode is enabled, interrupt only the current thread,\n\
 otherwise all the threads in the program are stopped.  To \n\
 interrupt all running threads in non-stop mode, use the -a option."));
 
-  add_info ("registers", nofp_registers_info, _("\
+  c = add_info ("registers", nofp_registers_info, _("\
 List of integer registers and their contents, for selected stack frame.\n\
 Register name as argument means describe only that register."));
   add_info_alias ("r", "registers", 1);
+  set_cmd_completer (c, reg_or_group_completer);
 
   if (xdb_commands)
-    add_com ("lr", class_info, nofp_registers_info, _("\
+    {
+      c = add_com ("lr", class_info, nofp_registers_info, _("\
 List of integer registers and their contents, for selected stack frame.\n\
 Register name as argument means describe only that register."));
-  add_info ("all-registers", all_registers_info, _("\
+      set_cmd_completer (c, reg_or_group_completer);
+    }
+
+  c = add_info ("all-registers", all_registers_info, _("\
 List of all registers and their contents, for selected stack frame.\n\
 Register name as argument means describe only that register."));
+  set_cmd_completer (c, reg_or_group_completer);
 
   add_info ("program", program_info,
 	    _("Execution status of the program."));