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] Clarify "list" output when specified lines are ambiguous


Currently, with "list LINESPEC1,LINESPEC2", if one of the linespecs is
ambiguous, i.e., if it expands to multiple locations, you get this
seemingly odd output:

 (gdb) list foo,bar
 file: "file0.c", line number: 26
 file: "file1.c", line number: 29

Since "ambiguous" expands to multiple locations, the specified range
is indeterminate, and above, GDB is trying to be helpful by showing
you what was ambiguous.  It looks confusing to me, though.  I think
it'd be much more user friendly if GDB actually told you that, like
this:

 (gdb) list foo,bar
 Specified first line 'foo' is ambiguous:
 file: "file0.c", line number: 26
 file: "file1.c", line number: 29

 (gdb) list bar,foo
 Specified last line 'foo' is ambiguous:
 file: "file0.c", line number: 26
 file: "file1.c", line number: 29

Note, I'm using "first" and "last" in the output because that's what
the manual uses:

 ~~~
 list first,last

     Print lines from first to last. [...]
 ~~~

Tested on x86-64 GNU/Linux.

Comments?

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* cli/cli-cmds.c (edit_command): Pass message to
	ambiguous_line_spec.
	(list_command): Pass message to ambiguous_line_spec.  Say
	"first"/"last" instead of "start" and "end" to be consistent with
	the manual.
	(ambiguous_line_spec): Add 'format' and vararg parameters.  Use
	them to print formatted message.

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* gdb.base/list-ambiguous.exp: New file.
	* gdb.base/list-ambiguous0.c: New file.
	* gdb.base/list-ambiguous1.c: New file.
	* gdb.base/list.exp (test_list_range): Adjust expected output.
---
 gdb/cli/cli-cmds.c                        | 38 +++++++++++++---
 gdb/testsuite/gdb.base/list-ambiguous.exp | 74 +++++++++++++++++++++++++++++++
 gdb/testsuite/gdb.base/list-ambiguous0.c  | 41 +++++++++++++++++
 gdb/testsuite/gdb.base/list-ambiguous1.c  | 40 +++++++++++++++++
 gdb/testsuite/gdb.base/list.exp           |  2 +-
 5 files changed, 187 insertions(+), 8 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/list-ambiguous.exp
 create mode 100644 gdb/testsuite/gdb.base/list-ambiguous0.c
 create mode 100644 gdb/testsuite/gdb.base/list-ambiguous1.c

diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index d4dc539..e1b88dd 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -90,7 +90,9 @@ static void list_command (char *, int);
 
 /* Prototypes for local utility functions */
 
-static void ambiguous_line_spec (struct symtabs_and_lines *);
+static void ambiguous_line_spec (struct symtabs_and_lines *,
+				 const char *format, ...)
+  ATTRIBUTE_PRINTF (2, 3);
 
 static void filter_sals (struct symtabs_and_lines *);
 
@@ -827,7 +829,8 @@ edit_command (char *arg, int from_tty)
 	}
       if (sals.nelts > 1)
 	{
-	  ambiguous_line_spec (&sals);
+	  ambiguous_line_spec (&sals,
+			       _("Specified line is ambiguous:\n"));
 	  xfree (sals.sals);
 	  return;
 	}
@@ -982,6 +985,11 @@ list_command (char *arg, int from_tty)
   for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
   linenum_beg = (p == arg1);
 
+  /* Save the range of the first argument, in case we need to let the
+     user know it was ambiguous.  */
+  const char *beg = arg;
+  size_t beg_len = arg1 - beg;
+
   while (*arg1 == ' ' || *arg1 == '\t')
     arg1++;
   if (*arg1 == ',')
@@ -989,7 +997,9 @@ list_command (char *arg, int from_tty)
       no_end = 0;
       if (sals.nelts > 1)
 	{
-	  ambiguous_line_spec (&sals);
+	  ambiguous_line_spec (&sals,
+			       _("Specified first line '%.*s' is ambiguous:\n"),
+			       (int) beg_len, beg);
 	  xfree (sals.sals);
 	  return;
 	}
@@ -1000,6 +1010,10 @@ list_command (char *arg, int from_tty)
 	dummy_end = 1;
       else
 	{
+	  /* Save the last argument, in case we need to let the user
+	     know it was ambiguous.  */
+	  const char *end_arg = arg1;
+
 	  event_location_up location
 	    = string_to_event_location (&arg1, current_language);
 	  if (dummy_beg)
@@ -1017,7 +1031,9 @@ list_command (char *arg, int from_tty)
 	    }
 	  if (sals_end.nelts > 1)
 	    {
-	      ambiguous_line_spec (&sals_end);
+	      ambiguous_line_spec (&sals_end,
+				   _("Specified last line '%s' is ambiguous:\n"),
+				   end_arg);
 	      xfree (sals_end.sals);
 	      xfree (sals.sals);
 	      return;
@@ -1032,7 +1048,7 @@ list_command (char *arg, int from_tty)
 
   if (!no_end && !dummy_beg && !dummy_end
       && sal.symtab != sal_end.symtab)
-    error (_("Specified start and end are in different files."));
+    error (_("Specified first and last lines are in different files."));
   if (dummy_beg && dummy_end)
     error (_("Two empty args do not say what lines to list."));
 
@@ -1517,17 +1533,25 @@ alias_command (char *args, int from_tty)
 /* Print a list of files and line numbers which a user may choose from
    in order to list a function which was specified ambiguously (as
    with `list classname::overloadedfuncname', for example).  The
-   vector in SALS provides the filenames and line numbers.  */
+   vector in SALS provides the filenames and line numbers.  FORMAT is
+   a printf-style format string used to tell the user what was
+   ambiguous.  */
 
 static void
-ambiguous_line_spec (struct symtabs_and_lines *sals)
+ambiguous_line_spec (struct symtabs_and_lines *sals, const char *format, ...)
 {
   int i;
 
+  va_list ap;
+  va_start (ap, format);
+
+  vprintf_filtered (format, ap);
   for (i = 0; i < sals->nelts; ++i)
     printf_filtered (_("file: \"%s\", line number: %d\n"),
 		     symtab_to_filename_for_display (sals->sals[i].symtab),
 		     sals->sals[i].line);
+
+  va_end (ap);
 }
 
 /* Sort function for filter_sals.  */
diff --git a/gdb/testsuite/gdb.base/list-ambiguous.exp b/gdb/testsuite/gdb.base/list-ambiguous.exp
new file mode 100644
index 0000000..db9d028
--- /dev/null
+++ b/gdb/testsuite/gdb.base/list-ambiguous.exp
@@ -0,0 +1,74 @@
+# Copyright 2017 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test the "list" command with linespecs that expand to multiple
+# locations.
+
+standard_testfile list-ambiguous0.c list-ambiguous1.c
+
+if {[prepare_for_testing "failed to prepare" $testfile [list $srcfile $srcfile2] \
+	 {debug}]} {
+    return -1
+}
+
+# Build source listing pattern based on an inclusive line range.
+
+proc line_range_pattern { range_start range_end } {
+    global line_re
+
+    for {set i $range_start} {$i <= $range_end} {incr i} {
+	append pattern "\r\n$i\[ \t\]\[^\r\n\]*"
+    }
+
+    verbose -log "pattern $pattern"
+    return $pattern
+}
+
+# Test the "list" command with linespecs that expand to multiple
+# locations.
+
+proc test_list_ambiguous_function {} {
+    global srcfile srcfile2
+
+    set lineno0 [gdb_get_line_number "ambiguous (void)" $srcfile]
+    set lineno1 [gdb_get_line_number "ambiguous (void)" $srcfile2]
+    set lines0_re [line_range_pattern [expr $lineno0 - 5] [expr $lineno0 + 4]]
+    set lines1_re [line_range_pattern [expr $lineno1 - 5] [expr $lineno1 + 4]]
+
+    set any "\[^\r\n\]*"
+    set h0_re "file: \"${any}list-ambiguous0.c\", line number: $lineno0"
+    set h1_re "file: \"${any}list-ambiguous1.c\", line number: $lineno1"
+    gdb_test "list ambiguous" "${h0_re}${lines0_re}\r\n${h1_re}${lines1_re}"
+
+    gdb_test "list main,ambiguous" \
+	"Specified last line 'ambiguous' is ambiguous:\r\n${h0_re}\r\n${h1_re}"
+    gdb_test "list ,ambiguous" \
+	"Specified last line 'ambiguous' is ambiguous:\r\n${h0_re}\r\n${h1_re}"
+    gdb_test "list ambiguous,main" \
+	"Specified first line 'ambiguous' is ambiguous:\r\n${h0_re}\r\n${h1_re}"
+    gdb_test "list ambiguous,ambiguous" \
+	"Specified first line 'ambiguous' is ambiguous:\r\n${h0_re}\r\n${h1_re}"
+    gdb_test "list ambiguous," \
+	"Specified first line 'ambiguous' is ambiguous:\r\n${h0_re}\r\n${h1_re}"
+
+    # While at it, test the "edit" command as well, since it shares
+    # code with "list".
+    gdb_test "edit ambiguous" \
+	"Specified line is ambiguous:\r\n${h0_re}\r\n${h1_re}"
+}
+
+gdb_test_no_output "set listsize 10"
+
+test_list_ambiguous_function
diff --git a/gdb/testsuite/gdb.base/list-ambiguous0.c b/gdb/testsuite/gdb.base/list-ambiguous0.c
new file mode 100644
index 0000000..91f7fe9
--- /dev/null
+++ b/gdb/testsuite/gdb.base/list-ambiguous0.c
@@ -0,0 +1,41 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2017 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+
+
+
+
+/* This function is defined in both
+   list-ambiguous0.c/list-ambiguous1.c files, in order to test
+   "list"'s behavior with ambiguous linespecs.  */
+
+static void __attribute__ ((used)) ambiguous (void) {}
+
+
+
+
+
+
+
+
+
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/list-ambiguous1.c b/gdb/testsuite/gdb.base/list-ambiguous1.c
new file mode 100644
index 0000000..024299a
--- /dev/null
+++ b/gdb/testsuite/gdb.base/list-ambiguous1.c
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2017 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+
+
+
+
+
+
+
+
+/* This function is defined in both
+   list-ambiguous0.c/list-ambiguous1.c files, in order to test
+   "list"'s behavior with ambiguous linespecs.  */
+static void __attribute__ ((used)) ambiguous (void) {}
+
+
+
+
+
+
+
+
+
+
+/* last line */
diff --git a/gdb/testsuite/gdb.base/list.exp b/gdb/testsuite/gdb.base/list.exp
index 5377571..d554c9e 100644
--- a/gdb/testsuite/gdb.base/list.exp
+++ b/gdb/testsuite/gdb.base/list.exp
@@ -361,7 +361,7 @@ proc test_list_range {} {
 
     gdb_test "list ${past_end},${much_past_end}" "Line number ${past_end} out of range; .*list0.c has ${last_line} lines." "list range; both bounds past EOF"
 
-    gdb_test "list list0.c:2,list1.c:17" "Specified start and end are in different files." "list range, must be same files"
+    gdb_test "list list0.c:2,list1.c:17" "Specified first and last lines are in different files." "list range, must be same files"
 }
 
 #
-- 
2.5.5


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