Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org, "Lluís Batlle i Rossell" <viric@viric.name>
Subject: Re: [PATCHv2] gdb: fixes and tests for the 'edit' command
Date: Fri, 08 Nov 2024 10:51:49 +0000	[thread overview]
Message-ID: <87ed3mq9q2.fsf@redhat.com> (raw)
In-Reply-To: <87ttcj13y4.fsf@tromey.com>

Tom Tromey <tom@tromey.com> writes:

> Andrew> I am instead proposing this alternative patch which I think fixes more
> Andrew> cases.  When GDB stops at a location then 'edit' with no arguments
> Andrew> should correctly edit the current line.  And using 'list XX' to list a
> Andrew> specific location, followed by 'edit' should also now edit the just
> Andrew> listed location.
>
> Looks good to me.  Thank you.
>
> I think this fixes
> https://sourceware.org/bugzilla/show_bug.cgi?id=17669
>
> I also wonder if it fixes
> https://sourceware.org/bugzilla/show_bug.cgi?id=8837
>
> Approved-By: Tom Tromey <tom@tromey.com>

Thanks Tom,

I updated the commit message to mention PR gdb/17669 as this patch does
fix that issue.

The other bug looks like it was fixed long ago, I added a comment to the
bug and closed it.

I did however update the tests in _this_ commit slightly to better cover
the case from the second bug (PR gdb/8837), though the GDB code didn't
change at all.  I've attached the updated patch below.

Thanks,
Andrew

---

commit 31ada87f91b4c5306d81c8a896df9764c32941f3
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Wed Nov 6 22:18:55 2024 +0000

    gdb: fixes and tests for the 'edit' command
    
    This commit was inspired by this mailing list post:
    
      https://inbox.sourceware.org/gdb-patches/osmtfvf5xe3yx4n7oirukidym4cik7lehhy4re5mxpset2qgwt@6qlboxhqiwgm
    
    When reviewing that patch, the first thing I wanted to do was add some
    tests for the 'edit' command because, as far as I can tell, there are
    no real tests right now.
    
    The approach I've taken for testing is to override the EDITOR
    environment variable, setting this to just 'echo'.  Now when the
    'edit' command is run, instead of entering an interactive editor, the
    shell instead echos back the arguments that GDB is trying to pass to
    the editor.  The output might look like this:
    
      (gdb) edit
      +22 /tmp/gdb/testsuite/gdb.base/edit-cmd.c
      (gdb)
    
    We can then test this like any other normal command.  I then wrote
    some basic tests covering a few situations like, using 'edit' before
    the inferior is started.  Using 'edit' without any arguments, and
    using 'edit' with a line number argument.
    
    There are plenty of cases that are still not tested, for example, the
    test program only has a single source file for example.  But we can
    always add more tests later.
    
    I then used these tests to validate the fix proposed in the above
    patch.
    
    The patch above does indeed fix some cases, specifically, when GDB
    stops at a location (e.g. a breakpoint location) and then the 'edit'
    command without any arguments is fixed.  But using the 'list' command
    to show some other location, and then 'edit' to edit the just listed
    location broken before and after the above patch.
    
    I am instead proposing this alternative patch which I think fixes more
    cases.  When GDB stops at a location then 'edit' with no arguments
    should correctly edit the current line.  And using 'list XX' to list a
    specific location, followed by 'edit' should also now edit the just
    listed location.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17669
    
    Co-Authored-By: Lluís Batlle i Rossell <viric@viric.name>
    Approved-By: Tom Tromey <tom@tromey.com>

diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 65ac7d6e7fb..225615f0210 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -973,7 +973,8 @@ edit_command (const char *arg, int from_tty)
     {
       if (sal.symtab == 0)
 	error (_("No default source file yet."));
-      sal.line += get_lines_to_list () / 2;
+      if (get_first_line_listed () != 0)
+	sal.line = get_first_line_listed () + get_lines_to_list () / 2;
     }
   else
     {
diff --git a/gdb/testsuite/gdb.base/basic-edit-cmd.c b/gdb/testsuite/gdb.base/basic-edit-cmd.c
new file mode 100644
index 00000000000..fb0f70db51c
--- /dev/null
+++ b/gdb/testsuite/gdb.base/basic-edit-cmd.c
@@ -0,0 +1,55 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2024 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/>.  */
+
+/* Used so we have some work to do.  */
+volatile int global_var = 0;
+
+int
+main (void)
+{			/* prologue location */
+  ++global_var;		/* first location */
+
+  /*
+   *
+   *
+   * This comment is here as filler.
+   *
+   *
+   */
+
+  ++global_var;		/* second location */
+
+  /*
+   *
+   *
+   * This comment is also here as filler.
+   *
+   *
+   */
+
+  ++global_var;		/* third location */
+
+  /*
+   *
+   *
+   * This is yet another filler comment.
+   *
+   *
+   */
+
+  return 0;		/* fourth location */
+}
diff --git a/gdb/testsuite/gdb.base/basic-edit-cmd.exp b/gdb/testsuite/gdb.base/basic-edit-cmd.exp
new file mode 100644
index 00000000000..116daf9e3b7
--- /dev/null
+++ b/gdb/testsuite/gdb.base/basic-edit-cmd.exp
@@ -0,0 +1,154 @@
+# Copyright 2024 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 'edit' command.
+
+# This relies on setting environment variables, so best to run on
+# non-remote hosts.
+require {!is_remote host}
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
+    return
+}
+
+# Check that 'echo' is available in the shell.
+gdb_test_multiple "shell echo test 1234 xyz" "check echo is available" {
+    -re -wrap "^test 1234 xyz" {
+    }
+
+    -re -wrap "" {
+	unsupported "shell cannot use echo command"
+	return
+    }
+}
+
+if {![runto_main]} {
+    return
+}
+
+# Are we using DWARF debug format?
+get_debug_format
+set non_dwarf [expr ! [test_debug_format "DWARF \[0-9\]"]]
+
+# Find line numbers for use in tests.
+set line_0 [gdb_get_line_number "prologue location"]
+set line_1 [gdb_get_line_number "first location"]
+set line_2 [gdb_get_line_number "second location"]
+set line_3 [gdb_get_line_number "third location"]
+set line_4 [gdb_get_line_number "fourth location"]
+
+# Regexp to match SRCFILE.
+set srcfile_re [string_to_regexp [file normalize $srcdir/$subdir]/$srcfile]
+set srcfile_re_simple "\[^\r\n\]+/[string_to_regexp $srcfile]"
+
+# Setup the EDITOR environment variable to run our helper script, and
+# then run the tests.
+
+save_vars { env(EDITOR) } {
+    set env(EDITOR) "echo"
+
+    # Start with no test binary loaded.
+    clean_restart
+    gdb_test "edit" \
+	"^No symbol table is loaded.  Use the \"file\" command\\." \
+	"try edit when no symbol file is loaded"
+
+    # Now start with a test binary.
+    clean_restart $binfile
+
+    with_test_prefix "before starting inferior" {
+
+	# We should be able to find the default location (of main)
+	# even for non-dwarf debug formats, but this currently fails
+	# with the stabs board.
+	if { $non_dwarf } { setup_xfail *-*-* }
+	gdb_test "edit" \
+	    "\r\n\\+$line_0 $srcfile_re" \
+	    "check edit of default location"
+
+	gdb_test "list $line_4" \
+	    "\r\n$line_4\\s+\[^\r\n\]+/\\* fourth location \\*/\r\n.*" \
+	    "list lines around the fourth location"
+
+	gdb_test "edit" \
+	    "\r\n\\+$line_4 $srcfile_re" \
+	    "check edit of fourth location after listing"
+
+	gdb_test "edit $line_2" \
+	    "\r\n\\+$line_2 $srcfile_re" \
+	    "check edit of second location"
+
+	gdb_test "edit xxx" \
+	    "^Function \"xxx\" not defined\\." \
+	    "try to edit an unknown function"
+    }
+
+    if {![runto_main]} {
+	return
+    }
+
+    set first_loc_pc [get_hexadecimal_valueof "\$pc" "*UNKNOWN*" \
+			  "get \$pc at first location"]
+
+    with_test_prefix "stopped at first location" {
+	gdb_test "edit" \
+	    "\r\n\\+$line_1 $srcfile_re" \
+	    "check edit of current location"
+    }
+
+    gdb_breakpoint $line_2
+    gdb_continue_to_breakpoint "stop at second location"
+
+    with_test_prefix "at second location" {
+	gdb_test "edit" \
+	    "\r\n\\+$line_2 $srcfile_re" \
+	    "check edit current location results"
+
+	gdb_test "edit $line_3" \
+	    "\r\n\\+$line_3 $srcfile_re" \
+	    "check edit third location results"
+    }
+
+    with_test_prefix "list first location" {
+	gdb_test "list $line_1" \
+	    "\r\n$line_1\\s+\[^\r\n\]+/\\* first location \\*/\r\n.*" \
+	    "list lines around the first location"
+
+	gdb_test "edit" \
+	    "\r\n\\+$line_1 $srcfile_re" \
+	    "check edit current location results"
+    }
+
+    gdb_breakpoint $line_4
+    gdb_continue_to_breakpoint "stop at fourth location"
+
+    with_test_prefix "at fourth location" {
+	gdb_test "edit" \
+	    "\r\n\\+$line_4 $srcfile_re" \
+	    "check edit current location results"
+
+	gdb_test "edit $line_1" \
+	    "\r\n\\+$line_1 $srcfile_re" \
+	    "check edit first location results"
+
+	gdb_test "edit *$first_loc_pc" \
+	    [multi_line \
+		 "[string_to_regexp $first_loc_pc] is in main \\($srcfile_re_simple:$line_1\\)\\." \
+		 "\\+$line_1 $srcfile_re"] \
+	    "check edit first location by address results"
+    }
+}


  reply	other threads:[~2024-11-08 10:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-05 22:23 [PATCH] Fix 'edit' command to go to right line Lluís Batlle i Rossell
2024-11-07 11:13 ` Andrew Burgess
2024-11-07 12:01   ` Lluís Batlle i Rossell
2024-11-07 13:22     ` Andrew Burgess
2024-11-07 13:17   ` [PATCHv2] gdb: fixes and tests for the 'edit' command Andrew Burgess
2024-11-07 15:03     ` Tom Tromey
2024-11-08 10:51       ` Andrew Burgess [this message]
2024-11-08 13:28         ` Lluís Batlle i Rossell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ed3mq9q2.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    --cc=viric@viric.name \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox