From patchwork Tue Aug 22 19:47:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 22322 Received: (qmail 78063 invoked by alias); 22 Aug 2017 19:47:57 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 78037 invoked by uid 89); 22 Aug 2017 19:47:56 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LINEPADDING, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 22 Aug 2017 19:47:53 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 242332027A; Tue, 22 Aug 2017 19:47:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 242332027A Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=palves@redhat.com Received: from cascais.lan (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 385557E4CE; Tue, 22 Aug 2017 19:47:51 +0000 (UTC) From: Pedro Alves To: gdb-patches@sourceware.org Cc: Zhouyi Zhou Subject: [PATCH] Clarify "list" output when specified lines are ambiguous Date: Tue, 22 Aug 2017 20:47:50 +0100 Message-Id: <1503431270-19557-1-git-send-email-palves@redhat.com> 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 * 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 * 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 . + +# 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 . */ + + + + + +/* 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 . */ + + + + + + + + + +/* 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" } #