diff mbox

[v2] Provide useful completer for "info registers"

Message ID 87r3wnp4a8.fsf@br87z6lw.de.ibm.com
State New
Headers show

Commit Message

Andreas Arnez Nov. 28, 2014, 6:29 p.m. UTC
First version here:

  https://sourceware.org/ml/gdb-patches/2014-11/msg00626.html

New in second version:

* Changed all uses of 'text' in reg_or_group_completer to 'word', based
  on Sergio's comment.

* Made reg_or_group_completer more compact by dissolving sub-blocks.

* Added test case.

OK to apply?

-- >8 --
Subject: Provide useful completer for "info registers"

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/testsuite/ChangeLog:

	* gdb.base/completion.exp: Add test for completion of "info
	registers ".
---
 gdb/completer.c                       | 42 +++++++++++++++++++++++++++++++++++
 gdb/completer.h                       |  3 +++
 gdb/infcmd.c                          | 12 +++++++---
 gdb/testsuite/gdb.base/completion.exp | 24 ++++++++++++++++++++
 4 files changed, 78 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/gdb/completer.c b/gdb/completer.c
index a0f3fa3..8bc62d9 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,46 @@  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 (word);
+  struct gdbarch *gdbarch;
+  struct reggroup *group;
+  int i, n_regs;
+
+  if (!target_has_registers)
+    return result;
+
+  gdbarch = get_frame_arch (get_selected_frame (NULL));
+  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 (word, reg_name, len) == 0)
+	VEC_safe_push (char_ptr, result, xstrdup (reg_name));
+    }
+
+  for (group = reggroup_next (gdbarch, NULL);
+       group != NULL;
+       group = reggroup_next (gdbarch, group))
+    {
+      const char *group_name = reggroup_name (group);
+
+      if (strncmp (word, 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."));
diff --git a/gdb/testsuite/gdb.base/completion.exp b/gdb/testsuite/gdb.base/completion.exp
index c633a51..ae0019b 100644
--- a/gdb/testsuite/gdb.base/completion.exp
+++ b/gdb/testsuite/gdb.base/completion.exp
@@ -137,6 +137,30 @@  gdb_test "complete set listsize unl" "set listsize unlimited"
 gdb_test "complete set trace-buffer-size " "set trace-buffer-size unlimited"
 gdb_test "complete set trace-buffer-size unl" "set trace-buffer-size unlimited"
 
+# Test "info registers" completion: First determine this
+# architecture's registers and reggroups...
+
+set regs_output [capture_command_output "mt print registers" \
+		     ".*Name.*Nr.*Rel.*Offset.*Size.*Type.\[^\n\]*\n"]
+append regs_output "\n"
+append regs_output [capture_command_output "mt print reggroups" \
+			".*Group.*Type\[^\n]*\n"]
+set all_regs {}
+foreach {-> reg} [regexp -all -inline -line {^\s+(\w+\S*)} $regs_output] {
+    lappend all_regs $reg
+}
+set all_regs [join [lsort $all_regs]]
+
+# ... and compare them to the completion of "info registers"
+
+set regs_output [capture_command_output "complete info registers " ""]
+set completed_regs {}
+foreach {-> reg} [regexp -all -inline -line {^info registers (\w+\S*)} $regs_output] {
+    lappend completed_regs $reg
+}
+set completed_regs [join [lsort $completed_regs]]
+gdb_assert {{$all_regs eq $completed_regs}} "complete 'info registers '"
+
 # Tests below are about tab-completion, which doesn't work if readline
 # library isn't used.  Check it first.