From patchwork Wed Sep 27 14:22:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 76768 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C2B99387103C for ; Wed, 27 Sep 2023 14:23:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C2B99387103C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1695824582; bh=veFP/L2Tw1GxHWcyLm61FadByn6i0GWosFeRJ7w3E7c=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=hDwSRiGzQETep1YumZjhH6VukFdI7zz/+PQ+BOh+Tzrr1NOeQYQZA2UxCXHXgdJMZ 89y4Ojm6Ybz/miivmoKrG1aK+tWDQ0nb3GtMGxPWzOeosjsO0htrmGFmRG/UshcUB5 9tTWdvOfsKXTf8bSB2Qbb0CwXJCjbWJfFML9F9YM= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id EF2B9385559E for ; Wed, 27 Sep 2023 14:22:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EF2B9385559E Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-EZIykRoMN7mQUGMDHLxq5A-1; Wed, 27 Sep 2023 10:22:20 -0400 X-MC-Unique: EZIykRoMN7mQUGMDHLxq5A-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-774269fffe7so846087485a.0 for ; Wed, 27 Sep 2023 07:22:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695824540; x=1696429340; 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=veFP/L2Tw1GxHWcyLm61FadByn6i0GWosFeRJ7w3E7c=; b=Nw+9q3HYfDA+HEd3gntBDNyBEcq6+dDQFex6mTFJEAx9QJKvu/1Zf/nDSWbm1eVhb1 2/rmLBm0Sv5+7V/XK+15i1vGmJ/fIeNdAREMRYSsSZUKZVL4DA92nW8VlPkoLJo6xsKF dTypSZEAuhbEr+NfyPmQufmji4MNe9bwARQemg4+hDsl/LtxbabuYF1A2h596OiK0vJm /A5AgOUzXYgU4iq7vmqTsNZpDAujyVCRbKtVfuO6qEi5w1tYUbtaBJejnVtLIE1G1bAg f1XkSCRK4czV2P7eE+AA1347jD76QSZvi8YUbUZ9uVJq1agOydsfLjAjP/7kHTXHz1GP fp8A== X-Gm-Message-State: AOJu0YzqX90+hd6TnEo4Vocx1TnQVwnm0h3IdslUEWyUA1+A4tMoipIf SIaYayOc8Y4ayhbVTmuVyllXYPwfEyppNZMXSzQ6yNYaa0xs1u3Qov6UoLfLiCEnRBVzTemNCur Uj5AOV/Q5FeagCNbc4mky5RAc86TqUxZ+qZAmtaWXKLblOXRlhQjmyopDNmPFgpwkY39OhIsvnn m6Pd4o2Q== X-Received: by 2002:a05:620a:44c9:b0:76e:f686:cad8 with SMTP id y9-20020a05620a44c900b0076ef686cad8mr6884866qkp.13.1695824539657; Wed, 27 Sep 2023 07:22:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEX9JURgMbIOZ1ERuFTtSiho9Hg4kfQ0A9S5m1ZZ1zqEEmz+2v49Y9h4ML99fWs0q38qIyrWA== X-Received: by 2002:a05:620a:44c9:b0:76e:f686:cad8 with SMTP id y9-20020a05620a44c900b0076ef686cad8mr6884835qkp.13.1695824539183; Wed, 27 Sep 2023 07:22:19 -0700 (PDT) Received: from localhost ([31.111.84.209]) by smtp.gmail.com with ESMTPSA id e15-20020a05620a12cf00b007756d233fbdsm968794qkl.37.2023.09.27.07.22.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 07:22:18 -0700 (PDT) To: gdb-patches@sourceware.org Subject: [PATCH 5/5] gdb: fix reread_symbols when an objfile has target: prefix Date: Wed, 27 Sep 2023 15:22:05 +0100 Message-Id: <70ba713f7bbc6ac07de7dbdbfa186d6d3c37df5d.1695824275.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" When using a remote target, it is possible to tell GDB that the executable to be debugged is located on the remote machine, like this: (gdb) target extended-remote :54321 ... snip ... (gdb) file target:/tmp/hello.x Reading /tmp/hello.x from remote target... warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead. Reading /tmp/hello.x from remote target... Reading symbols from target:/tmp/hello.x... (gdb) So far so good. However, when we try to start the inferior we run into a small problem: (gdb) set remote exec-file /tmp/hello.x (gdb) start `target:/tmp/hello.x' has disappeared; keeping its symbols. Temporary breakpoint 1 at 0x401198: file /tmp/hello.c, line 18. Starting program: target:/tmp/hello.x ... snip ... Temporary breakpoint 1, main () at /tmp/hello.c:18 18 printf ("Hello World\n"); (gdb) Notice this line: `target:/tmp/hello.x' has disappeared; keeping its symbols. That's wrong, the executable hasn't been removed, GDB just doesn't know how to check if the remote file has changed, and so falls back to assuming that the file has been removed. In this commit I add support to reread_symbols for stat-ing remote files. With this in place GDB no longer complains when using a remote executable file. --- gdb/symfile.c | 19 +- gdb/target.c | 15 ++ gdb/target.h | 39 ++++ gdb/testsuite/gdb.server/target-exec-file.c | 22 +++ gdb/testsuite/gdb.server/target-exec-file.exp | 174 ++++++++++++++++++ 5 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.server/target-exec-file.c create mode 100644 gdb/testsuite/gdb.server/target-exec-file.exp diff --git a/gdb/symfile.c b/gdb/symfile.c index 10074e281bd..b21c8ead225 100644 --- a/gdb/symfile.c +++ b/gdb/symfile.c @@ -2480,7 +2480,24 @@ reread_symbols (int from_tty) else filename = objfile_name (objfile); - int res = stat (filename, &new_statbuf); + int res; + if (is_target_filename (filename)) + { + fileio_error target_errno; + + scoped_target_fileio_open fd (nullptr, + (filename + + strlen (TARGET_SYSROOT_PREFIX)), + FILEIO_O_RDONLY, 0, false, + &target_errno); + if (fd.get () == -1) + res = -1; + else + res = target_fileio_fstat (fd.get (), &new_statbuf, + &target_errno); + } + else + res = stat (filename, &new_statbuf); if (res != 0) { warning (_("`%ps' has disappeared; keeping its symbols."), diff --git a/gdb/target.c b/gdb/target.c index 8cb4fa1736d..0d1608bb5fe 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -54,6 +54,7 @@ #include "target-connection.h" #include "valprint.h" #include "cli/cli-decode.h" +#include "cli/cli-style.h" static void generic_tls_error (void) ATTRIBUTE_NORETURN; @@ -3577,6 +3578,20 @@ target_fileio_read_stralloc (struct inferior *inf, const char *filename) return gdb::unique_xmalloc_ptr (bufstr); } +/* See target.h. */ + +scoped_target_fileio_open::~scoped_target_fileio_open () +{ + if (m_fd != -1) + { + fileio_error target_errno; + if (target_fileio_close (m_fd, &target_errno) != 0) + warning (_("failed to close %ps: %s"), + styled_string (file_name_style.style (), + m_filename.c_str ()), + safe_strerror (fileio_error_to_host (target_errno))); + } +} static int default_region_ok_for_hw_watchpoint (struct target_ops *self, diff --git a/gdb/target.h b/gdb/target.h index 936ae79219c..5712adc367d 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -2235,6 +2235,45 @@ extern gdb::unique_xmalloc_ptr target_fileio_read_stralloc with EIO. */ extern void fileio_handles_invalidate_target (target_ops *targ); +/* A class that performs a target_fileio_open within a scope. On leaving + the scope target_fileio_close is called. + + If the close fails then a warning is issued. */ +struct scoped_target_fileio_open +{ + /* Call target_fileio_open passing all the arguments through. See + target_fileio_open for the meaning of all arguments. */ + scoped_target_fileio_open (struct inferior *inf, + const char *filename, int flags, + int mode, bool warn_if_slow, + fileio_error *target_errno) + : m_filename (filename) + { + m_fd = target_fileio_open (inf, filename, flags, mode, warn_if_slow, + target_errno); + } + + /* Close the file that was opened in the constructor, issue a warning if + the close fails. */ + ~scoped_target_fileio_open (); + + /* Return the file descriptor for the opened file. This can only be used + with target_fileio_* calls. This will return -1 if the open failed. */ + int get () const + { + return m_fd; + } + +private: + /* The filename that we opened. Stored so we can give a warning if the + close fails for any reason. */ + std::string m_filename; + + /* The target file descriptor for the opened file, or -1 if the open + failed for any reason. */ + int m_fd = -1; +}; + /* Tracepoint-related operations. */ extern void target_trace_init (); diff --git a/gdb/testsuite/gdb.server/target-exec-file.c b/gdb/testsuite/gdb.server/target-exec-file.c new file mode 100644 index 00000000000..3a264f239ed --- /dev/null +++ b/gdb/testsuite/gdb.server/target-exec-file.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 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 . */ + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.server/target-exec-file.exp b/gdb/testsuite/gdb.server/target-exec-file.exp new file mode 100644 index 00000000000..9260df8b88d --- /dev/null +++ b/gdb/testsuite/gdb.server/target-exec-file.exp @@ -0,0 +1,174 @@ +# This testcase is part of GDB, the GNU debugger. + +# Copyright 2023 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 GDB's handling of using a file with a 'target:' prefix as the +# executable file. This test includes checking what happens when the +# file on the target system changes and GDB needs to reload it. + +load_lib gdbserver-support.exp + +require allow_gdbserver_tests !use_gdb_stub + +standard_testfile + +if { [build_executable "failed to prepare" $testfile $srcfile debug] } { + return -1 +} + +clean_restart + +# Some boards specifically set the sysroot to the empty string to +# avoid copying files from the target. But for this test we do want +# to copy files from the target, so set the sysroot back to 'target:'. +# +# This is fine so long as we're not using a board file that sets the +# sysroot to something else -- but none of the standard boards do +# this, and plenty of other tests mess with the sysroot, so I guess we +# don't worry about that too much. +gdb_test "set sysroot target:" ".*" + +# Make sure we're disconnected, in case we're testing with an +# extended-remote board, therefore already connected. +gdb_test "disconnect" ".*" + +# Ensure the executable is on the target. +set target_exec [gdb_remote_download target $binfile] + +# We're going to be restarting the inferior. Lets ask GDB not to +# prompt us if this is the right thing to do. +gdb_test_no_output "set confirm off" + +# Start gdbserver, but always in extended-remote mode, and then +# connect to it from GDB. +set res [gdbserver_start "--multi" $target_exec] +set gdbserver_protocol "extended-remote" +set gdbserver_gdbport [lindex $res 1] +gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport + +# Issue a 'file' command and parse the output. We look for a couple +# of specific things to ensure that we are correctly reading the exec +# from the remote target. +set saw_read_of_remote_exec false +set saw_read_of_syms_from_exec false +gdb_test_multiple "file target:$target_exec" "run file command" { + -re "^file target:\[^\r\n\]+\r\n" { + exp_continue + } + + -re "^Reading (\[^\r\n\]+) from remote target\\.\\.\\.\r\n" { + set filename $expect_out(1,string) + if { $filename eq $target_exec } { + set saw_read_of_remote_exec true + } + exp_continue + } + + -re "^warning: File transfers from remote targets can be slow\[^\r\n\]+\r\n" { + exp_continue + } + + -re "^Reading symbols from target:(\[^\r\n\]+)\\.\\.\\.\r\n" { + set filename $expect_out(1,string) + if { $filename eq $target_exec } { + set saw_read_of_syms_from_exec true + } + exp_continue + } + + -re "^Expanding full symbols from \[^\r\n\]+\r\n" { + exp_continue + } + + -re "^$gdb_prompt $" { + pass $gdb_test_name + } +} + +gdb_assert { $saw_read_of_remote_exec } \ + "exec was read from the remote target" + +gdb_assert { $saw_read_of_syms_from_exec } \ + "symbols were read from remote exec file" + +# Start the inferior (with the 'start' command), use TESTNAME for any +# pass/fail calls. EXPECT_REREAD should be true or false and +# indicates if we expect to too a line like: +# +# `FILE' has changed; re-reading symbols. +proc start_inferior { testname expect_reread } { + with_test_prefix $testname { + if { [gdb_start_cmd] < 0 } { + fail "start command" + return -1 + } + + set saw_reread false + gdb_test_multiple "" "stopped at main" { + -re "^start\\s*\r\n" { + exp_continue + } + -re "^`\[^\r\n\]+' has changed; re-reading symbols\\.\r\n" { + set saw_reread true + exp_continue + } + -re "^Reading \[^\r\n\]+ from remote target\\.\\.\\.\r\n" { + exp_continue + } + -re "^Expanding full symbols from \[^\r\n\]+\\.\\.\\.\r\n" { + exp_continue + } + -re "^Temporary breakpoint $::decimal at $::hex: \[^\r\n\]+\r\n" { + exp_continue + } + -re "^Starting program: \[^\r\n\]+\r\n" { + exp_continue + } + -re "^\\s*\r\n" { + exp_continue + } + -re "^Temporary breakpoint $::decimal, main \\(\\) at .*$::gdb_prompt $" { + pass $testname + } + } + + gdb_assert { $expect_reread == $saw_reread } \ + "check symbol re-read behaviour" + } +} + +# Start the inferior for the first time. The symbols were already +# read from the file when the 'file' command was used, we should not +# see the symbols re-read now. +start_inferior "start inferior the first time" false + +# Re-start the inferior. The executable is unchanged so we should not +# see the symbol file being re-read. +start_inferior "start inferior a second time" false + +# Delay for a short while so, when we touch the exec, we know the +# timestamp will change. +sleep 1 +set res [remote_exec target "touch $target_exec"] +set status [lindex $res 0] +if { $status != 0 } { + fail "touching executable on target" + return -1 +} + +# Start the inferior again, we expect to see the symbols being re-read +# from the remote file. +start_inferior "start inferior a third time" true