From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id FQnZAQu+LGf1uigAWB0awg (envelope-from ) for ; Thu, 07 Nov 2024 08:18:03 -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=g4IwLAiH; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id DCE181E603; Thu, 7 Nov 2024 08:18:02 -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 88D231E601 for ; Thu, 7 Nov 2024 08:18:01 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 25AFC3858C60 for ; Thu, 7 Nov 2024 13:18:01 +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 857D83858D21 for ; Thu, 7 Nov 2024 13:17:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 857D83858D21 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 857D83858D21 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=1730985460; cv=none; b=Roj8ct4OJMl7OcJD6OeRZ5gs9S2k2COgHE989ufjXhE5yUOLr832pTfuT/YV2CSDczISrt2gH9arXvwAwc5P0w+b18AH9bPSjRLW1Kxk9Zl1xnzaFlPJPhRacNUogjZiETX4QCaofBavdcmriPadANE2hJ0NCUAJeDyq3Gj75z0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730985460; c=relaxed/simple; bh=1OSAdJuarCuQGingjOnTIJQJjo+Aqa8gXe6aoFQjBFI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=t1yKrZ/OqJRSF91pSpyygets4SY+VR9AaYbxXoW9TZhzf91v4S+g2d8OOrsWUJaocXJFSoH7KIbLRbP4YtbcbmDnfovWoHm1jWhLEDEplffAmzisBXoyPqMui+9z6GF7bOzX6IF4SLIsE3RMSL21nYCTpFtevNVjo4Tvkm+aths= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730985454; 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=0zvG+C8+DRkqbEVxu7o07OxHCYe+n1S/DOgDAVE+m4k=; b=g4IwLAiHTEBUnsuB3oeoD6AephAROJVRBBNqpOZSYX9dH0FHnB9sLgMKckvHAVeRMtY90c x2Jf2Z/Fd0QiQa9iWMkXnrixDyvQcOmEw3ZdBXvi+Pvtk4XYGvDeM9DiHurTguY44kVceh SHbWggiLS7zBOSZii8f+ByKWTEWZ3RU= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-672-tJvrX0NBN2WzVc5V--_TQw-1; Thu, 07 Nov 2024 08:17:33 -0500 X-MC-Unique: tJvrX0NBN2WzVc5V--_TQw-1 X-Mimecast-MFC-AGG-ID: tJvrX0NBN2WzVc5V--_TQw Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-37d5ca192b8so582854f8f.1 for ; Thu, 07 Nov 2024 05:17:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730985451; x=1731590251; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0zvG+C8+DRkqbEVxu7o07OxHCYe+n1S/DOgDAVE+m4k=; b=acVWvp0YDclW95CUQI0SOAAg1NX4dGRpMwp0u5m+8yPT6/rOG6bCtemUMBh4N7iQj/ 2LEWX0bHJJa2JCQJryH3CV7qSKsjy6tLX6pcbPXkyzBSc84YnwcbH2pCqaPV6aLTqaFW t3TM8trT1LsG86kV2O1gSC/V8+BfndlXq98exzQZpjVH8J65cEpEBhPs0Yrp6DEkoToN Tu19qHjpBBqbXyO3UqKFfmvziQKIz7etY/iHHmvtmkPC7L9xyqBSw6G61PDTSeJsOvH4 rTAJv1f64IxKfdi49K/CNnKbSL50mDG2Sk7ttTqnG4jcfpTNl9Gg19yatSpNoObdRnyr Kd1g== X-Gm-Message-State: AOJu0Yz4SIzpjcylblonrBLh8xVRsdOQW60xezmnT5WF9oDgHKGpDzup T83KzwkCQgLQ6UBVhuwrSRTE33Ai+jE9ysgj6CxMRyI/zCxImeN3WCeHpejBdGn/n3j2ikiVzro PWqnyl+9Ejct2VIH70gOIs3UeFzPDOv2HUliDE85sIYDJpMsWuC1VkL1DhJduvZM+PKkB0oQdbS dggDEhKZiV73yqRHZMeG2or0QwJTun8T3b1KMyYCjJH7c= X-Received: by 2002:a5d:6da8:0:b0:37e:d942:f4bf with SMTP id ffacd0b85a97d-381ef6dbf1fmr1198517f8f.12.1730985451222; Thu, 07 Nov 2024 05:17:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IHmjlb+pHCTfg9cK7MBSh56stJasmezSI3jEtz1Furj8CvvuG1bdpS/OowPxIIzYi33wc8vlw== X-Received: by 2002:a5d:6da8:0:b0:37e:d942:f4bf with SMTP id ffacd0b85a97d-381ef6dbf1fmr1198494f8f.12.1730985450717; Thu, 07 Nov 2024 05:17:30 -0800 (PST) Received: from localhost (197.209.200.146.dyn.plus.net. [146.200.209.197]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-381ed9707bdsm1750673f8f.4.2024.11.07.05.17.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2024 05:17:30 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , =?UTF-8?q?Llu=C3=ADs=20Batlle=20i=20Rossell?= Subject: [PATCHv2] gdb: fixes and tests for the 'edit' command Date: Thu, 7 Nov 2024 13:17:27 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: <87pln7qotz.fsf@redhat.com> References: <87pln7qotz.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: kCmwKNBkCuValNBxP1X3cQg9cfWxsLfKJlCTbt-BWmQ_1730985452 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 In v2: - Changes to the test so that 'make check-all-boards' runs clean. --- 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. Co-Authored-By: LluĂ­s Batlle i Rossell --- gdb/cli/cli-cmds.c | 3 +- gdb/testsuite/gdb.base/basic-edit-cmd.c | 55 ++++++++ gdb/testsuite/gdb.base/basic-edit-cmd.exp | 153 ++++++++++++++++++++++ 3 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.base/basic-edit-cmd.c create mode 100644 gdb/testsuite/gdb.base/basic-edit-cmd.exp 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 . */ + +/* 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..608ab1818a7 --- /dev/null +++ b/gdb/testsuite/gdb.base/basic-edit-cmd.exp @@ -0,0 +1,153 @@ +# 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 "" { + 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 "\[^\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:$line_1\\)\\." \ + "\\+$line_1 $srcfile_re"] \ + "check edit first location by address results" + } +} base-commit: ebc73070f4b85e4adee7d2f49cb32e2bd7b16324 -- 2.25.4