From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 3JRPGWbtLWfT0ykAWB0awg (envelope-from ) for ; Fri, 08 Nov 2024 05:52:22 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gh8ofGmF; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 513BA1E613; Fri, 8 Nov 2024 05:52:22 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.0 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 03D551E611 for ; Fri, 8 Nov 2024 05:52:20 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 919FD385841C for ; Fri, 8 Nov 2024 10:52:20 +0000 (GMT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 2053C3858C3A for ; Fri, 8 Nov 2024 10:51:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2053C3858C3A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2053C3858C3A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731063116; cv=none; b=p76j/YoIC0cUf8XGLW571avWziqc23AhenkX9ZNAHs98nU0q4mEcPDylIGzQet44U1AvtRx6tbgQyl5g0hNYocskeX19GWOIjxI5O2obR+oZSdtyeati6YBztvhnl/h7EDhOvOW7GYOeW5wDtaIOnjYr6BLVRTCARk0VWlEyYYI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731063116; c=relaxed/simple; bh=B2b4h6g+/nlLvGo0IdDD1vuN483YoSMJ9fvN4Gb50lk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LbJaKLDfpS3HYiz7ZYl90VnFPzk3MQTrEiU2Kg4Of89htlmVU4lkoU2N0BvzAVNDWMCUqIcdvHyOmWIJuSyG5V//tZi3v4gSBaRuYj6sy8c58WYdKpPquqGJjuiCC9YhpaueWiDwlWP+8s4wVrchTGY9ve9Naz7ehDXp8RzMAbs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731063113; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9FB/bk2YIbafv4eBlQIYDKJOilnfVDiDDeICg2deOP0=; b=gh8ofGmFr6t/uCuFEHfRCrz7Ch5S08GpvpPhCpGYL7lNP4qDA7Zw9yip/vnwuawVEK0MV5 +FUX9VTXbLu7kOuYo6KcephaTmDSmCb37h7in2juTDBjGo+aEYRruy7PA8/PUNrlrDDl2z 4vYuFhiOjuI1uBTSCnGJ3JUmNInyQqo= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-478-xyJwP6zxORaQskeSeidGUw-1; Fri, 08 Nov 2024 05:51:52 -0500 X-MC-Unique: xyJwP6zxORaQskeSeidGUw-1 X-Mimecast-MFC-AGG-ID: xyJwP6zxORaQskeSeidGUw Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4314c6ca114so14208515e9.1 for ; Fri, 08 Nov 2024 02:51:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731063111; x=1731667911; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=D6OSsGm+ZXGxeVanNItjXM52T2/O/OUu3F5AE0jxIDc=; b=S34Upnt3mMJbX62IQtSzWTHe+0nmuc4Cfcujd1Osp/s0yD15CbsbZAmD/GU8rAQkyE rPe5y0bQEXbgfqnC8aiDa9JZhZ/lpYfJ7yDjCJ1SSjg61h5Fm7HhnOWF0EpJ704E9f+3 YrWUpgJGf3Z8gYUUgYRyb/tdA/yYwtHmeU+lIxEH38bMDXDmcgjVcWj838a8gZP6yaPj rMOePB9AX8s37H2d+YWpBFuBY3CklX94xvKzikSanyaYbwEMue/kzKFobUdojkDpHgo7 npEJFXowhk9kt1uTpqsdaOzGrWXqncpg3AdGz73kOlLOZ91XDV9lVkwO9wEdQVEaCU7l YUjA== X-Gm-Message-State: AOJu0YwhXtMWBN9jFN0z4ueWvPgxkag45p39XZvNIeJOkyAOeCVtW/2a lStJN8w+WPM+6+07KPCOOCteWkizNNC6e6vUFI4kIvMWSIKGCp9rxZLddX/hXQBLX3+IOjDSahS Qbye7uUJYdi6+1pcTOnVDR7UqkbcmixlgtZcZLZscbVHiM9AxrarhPuuuY6imWjMexxQ= X-Received: by 2002:a05:600c:3ca0:b0:431:5021:32 with SMTP id 5b1f17b1804b1-432b74fd9afmr16659575e9.6.1731063111114; Fri, 08 Nov 2024 02:51:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IGY4YBy1mmKULe06zeNOJhXilJsC5LecdK5upcIF9NiVzlym/daS/RVGPfMmLptW3QE4ZEUkg== X-Received: by 2002:a05:600c:3ca0:b0:431:5021:32 with SMTP id 5b1f17b1804b1-432b74fd9afmr16659385e9.6.1731063110576; Fri, 08 Nov 2024 02:51:50 -0800 (PST) Received: from localhost (197.209.200.146.dyn.plus.net. [146.200.209.197]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432aa74bab3sm94706865e9.43.2024.11.08.02.51.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 02:51:50 -0800 (PST) From: Andrew Burgess To: Tom Tromey Cc: gdb-patches@sourceware.org, =?utf-8?Q?Llu=C3=ADs?= Batlle i Rossell Subject: Re: [PATCHv2] gdb: fixes and tests for the 'edit' command In-Reply-To: <87ttcj13y4.fsf@tromey.com> References: <87pln7qotz.fsf@redhat.com> <87ttcj13y4.fsf@tromey.com> Date: Fri, 08 Nov 2024 10:51:49 +0000 Message-ID: <87ed3mq9q2.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: zATN6L3zqtbY2LrUqy3NDMhVJUOs3egaoYY4thkhWSw_1731063111 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Tom Tromey 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 argument= s > Andrew> should correctly edit the current line. And using 'list XX' to l= ist a > Andrew> specific location, followed by 'edit' should also now edit the ju= st > Andrew> listed location. > > Looks good to me. Thank you. > > I think this fixes > https://sourceware.org/bugzilla/show_bug.cgi?id=3D17669 > > I also wonder if it fixes > https://sourceware.org/bugzilla/show_bug.cgi?id=3D8837 > > Approved-By: Tom Tromey 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 Date: Wed Nov 6 22:18:55 2024 +0000 gdb: fixes and tests for the 'edit' command =20 This commit was inspired by this mailing list post: =20 https://inbox.sourceware.org/gdb-patches/osmtfvf5xe3yx4n7oirukidym4ci= k7lehhy4re5mxpset2qgwt@6qlboxhqiwgm =20 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. =20 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: =20 (gdb) edit +22 /tmp/gdb/testsuite/gdb.base/edit-cmd.c (gdb) =20 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. =20 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. =20 I then used these tests to validate the fix proposed in the above patch. =20 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. =20 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. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D17669 =20 Co-Authored-By: Llu=C3=ADs Batlle i Rossell Approved-By: Tom Tromey 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 =3D=3D 0) =09error (_("No default source file yet.")); - sal.line +=3D get_lines_to_list () / 2; + if (get_first_line_listed () !=3D 0) +=09sal.line =3D 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.ba= se/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 . = */ + +/* Used so we have some work to do. */ +volatile int global_var =3D 0; + +int +main (void) +{=09=09=09/* prologue location */ + ++global_var;=09=09/* first location */ + + /* + * + * + * This comment is here as filler. + * + * + */ + + ++global_var;=09=09/* second location */ + + /* + * + * + * This comment is also here as filler. + * + * + */ + + ++global_var;=09=09/* third location */ + + /* + * + * + * This is yet another filler comment. + * + * + */ + + return 0;=09=09/* 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 . + +# 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 "" { +=09unsupported "shell cannot use echo command" +=09return + } +} + +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" \ +=09"^No symbol table is loaded. Use the \"file\" command\\." \ +=09"try edit when no symbol file is loaded" + + # Now start with a test binary. + clean_restart $binfile + + with_test_prefix "before starting inferior" { + +=09# We should be able to find the default location (of main) +=09# even for non-dwarf debug formats, but this currently fails +=09# with the stabs board. +=09if { $non_dwarf } { setup_xfail *-*-* } +=09gdb_test "edit" \ +=09 "\r\n\\+$line_0 $srcfile_re" \ +=09 "check edit of default location" + +=09gdb_test "list $line_4" \ +=09 "\r\n$line_4\\s+\[^\r\n\]+/\\* fourth location \\*/\r\n.*" \ +=09 "list lines around the fourth location" + +=09gdb_test "edit" \ +=09 "\r\n\\+$line_4 $srcfile_re" \ +=09 "check edit of fourth location after listing" + +=09gdb_test "edit $line_2" \ +=09 "\r\n\\+$line_2 $srcfile_re" \ +=09 "check edit of second location" + +=09gdb_test "edit xxx" \ +=09 "^Function \"xxx\" not defined\\." \ +=09 "try to edit an unknown function" + } + + if {![runto_main]} { +=09return + } + + set first_loc_pc [get_hexadecimal_valueof "\$pc" "*UNKNOWN*" \ +=09=09=09 "get \$pc at first location"] + + with_test_prefix "stopped at first location" { +=09gdb_test "edit" \ +=09 "\r\n\\+$line_1 $srcfile_re" \ +=09 "check edit of current location" + } + + gdb_breakpoint $line_2 + gdb_continue_to_breakpoint "stop at second location" + + with_test_prefix "at second location" { +=09gdb_test "edit" \ +=09 "\r\n\\+$line_2 $srcfile_re" \ +=09 "check edit current location results" + +=09gdb_test "edit $line_3" \ +=09 "\r\n\\+$line_3 $srcfile_re" \ +=09 "check edit third location results" + } + + with_test_prefix "list first location" { +=09gdb_test "list $line_1" \ +=09 "\r\n$line_1\\s+\[^\r\n\]+/\\* first location \\*/\r\n.*" \ +=09 "list lines around the first location" + +=09gdb_test "edit" \ +=09 "\r\n\\+$line_1 $srcfile_re" \ +=09 "check edit current location results" + } + + gdb_breakpoint $line_4 + gdb_continue_to_breakpoint "stop at fourth location" + + with_test_prefix "at fourth location" { +=09gdb_test "edit" \ +=09 "\r\n\\+$line_4 $srcfile_re" \ +=09 "check edit current location results" + +=09gdb_test "edit $line_1" \ +=09 "\r\n\\+$line_1 $srcfile_re" \ +=09 "check edit first location results" + +=09gdb_test "edit *$first_loc_pc" \ +=09 [multi_line \ +=09=09 "[string_to_regexp $first_loc_pc] is in main \\($srcfile_re_simple:= $line_1\\)\\." \ +=09=09 "\\+$line_1 $srcfile_re"] \ +=09 "check edit first location by address results" + } +}