From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 98FE0385773F for ; Wed, 25 Oct 2023 15:08:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 98FE0385773F 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 98FE0385773F 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=1698246514; cv=none; b=R5SQ8Y0Le6hvDiwLEMTIt76q7Osw5wPCOk73hwwFoY3b+uONYOBbdUO4RCTk5qDDk73NGNuOkL/UW5Fp+9LXiHdumjIfc+wHHPXDTw9xNIQjALhLo2+8jQQ6t91lEIr81m3i8AP2OmqJe1GuFR2sYE9DuxmE1N3/dqT6ugeSlhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698246514; c=relaxed/simple; bh=5BhmjfP0joW8VOZAIPYIUw3COVsrm/VMgNn1sU2rb1Q=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=YGBYB3t6gvok4B8AsynKnF8hpxWpA2fjHUA7/Mfxq9VQEA+JkFaRmfOvzVsmVfBYtBLGDwgxrxysfsrSro9px8yIvf8A2JGws1nBwzI15mOs/SMAJS8t84thIUTMWWiNEkgDGIAYpc1IJZRgitg7jxKOb03m6hznp54gybwqX4c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698246512; 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=CH/p+SjpPSjCBRymgtp3rbHb0pIiO8yl7ZQQisZMxlQ=; b=e2FDPVuz6rHzK7a+ouccilDSK5i9o8lUcZR8RGrn8S4JinzsH4BogRslj+BaY9ym9rOy8M vMPhAxG10Xn9DW8/6lC7jeOhwetTzM2Ozz9utLCEVD+FGCbtCIlOuxLUnZshOfVNnLQ14V aHVi7NxTvbokDLRfGCuqCRhczh66qfk= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-452-RciMAnNfOAavkFZnN-jDbA-1; Wed, 25 Oct 2023 11:08:27 -0400 X-MC-Unique: RciMAnNfOAavkFZnN-jDbA-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-9c7558b838aso361881666b.2 for ; Wed, 25 Oct 2023 08:08:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698246506; x=1698851306; 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=KCcVpc0AIZvlL2AxZMoLHttUphvcGVvlq0izth6Nsng=; b=wBMqfoX+Lx618wPHr9CqO7X1G078hz358GCh91QwVk5fqI3kfprAL2JfL1J7UXi4LO ryjQZr4Ct5hOQLNJve2VfFEbMATGplDOHKptJ/fjTSZkb9HP918Hu8lQreh3z3jd5f9K 3HUQ0BUNbR3EBW1FmUsUaRMjULOKgC1cFT0mu44+G2KufaXlpGtKCSo23Q2FmccXJR4J 1f859mCmALM5KsGV/QDLSEdn6vbwEomUFeWigrmqWrDBJ9NLL9zzwsacvxQOcSBVKHB1 qFKxpzHeZKbABCQsJSX8+BJGCuzHHnK3kkODHQEGk3zZpRch+Wz5f0Z7g/D8LwnjI87A MMuw== X-Gm-Message-State: AOJu0YzQhzmvd3dB0244PsjG/OPxPhwtwNYrygBAmMcIxZWitxCOQq60 DslYlA8BubOqa7ak5XnfZND7b6APzV9vUmRX99nlz3dSz+fiqkUihqDorjPir1T78h46nLE1YAr p6nZglZLugnb7O2VWSmAC9Jx039e4HB37VJ5iBuNMPxEZBKEkaCXJ9Au0+Fzf4Q1AO9MhVHU5AZ w/XGItXg== X-Received: by 2002:a17:906:4fc4:b0:9be:e6d4:5751 with SMTP id i4-20020a1709064fc400b009bee6d45751mr13401528ejw.55.1698246505997; Wed, 25 Oct 2023 08:08:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEwGX3xUdMLAHEDQmJQRil3rU2VIudx0Pw4H7s4vE6NJr/czR2KWBGt7cwuSh40YirwopOUsg== X-Received: by 2002:a17:906:4fc4:b0:9be:e6d4:5751 with SMTP id i4-20020a1709064fc400b009bee6d45751mr13401510ejw.55.1698246505470; Wed, 25 Oct 2023 08:08:25 -0700 (PDT) Received: from localhost ([31.111.84.209]) by smtp.gmail.com with ESMTPSA id n13-20020a17090673cd00b009b65a834dd6sm10111671ejl.215.2023.10.25.08.08.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 08:08:24 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/2] gdb: fix reopen_exec_file for files with target: prefix Date: Wed, 25 Oct 2023 16:08:18 +0100 Message-Id: <8f5ded0b6bac82dcc59fc028aad239012f08bf1a.1698246342.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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIM_INVALID,DKIM_SIGNED,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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 List-Id: Following on from this commit: commit f2c4f78c813a9cef38b7e9c9ad18822fb9e19345 Date: Thu Sep 21 16:35:30 2023 +0100 gdb: fix reread_symbols when an objfile has target: prefix In this commit I update reopen_exec_file to correctly handle executables with a target: prefix. Before this commit we used the system 'stat' call, which obviously isn't going to work for files with a target: prefix (files located on a possibly remote target machine). By switching to bfd_stat we will use remote fileio to stat the remote files, which means we should now correctly detect changes in a remote executable. The program_space::ebfd_mtime variable, with which we compare the result of bfd_stat is set with a call to bfd_get_mtime, which in turn calls bfd_stat, so comparing to the result of calling bfd_stat makes sense (I think). As I discussed in the commit f2c4f78c813a, if a BFD is an in-memory BFD, then calling bfd_stat will always return 0, while bfd_get_mtime will always return the time at which the BFD was created. As a result comparing the results will always show the file having changed. I don't believe that GDB can set the main executable to an in-memory BFD object, so, in this commit, I simply assert that the executable is not in-memory. If this ever changes then we would need to decide how to handle this case -- always reload, or never reload. The assert doesn't appear to trigger for our current test suite. --- gdb/corefile.c | 17 +++-- gdb/testsuite/gdb.server/target-exec-file.exp | 70 +++++++++++++++++++ 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/gdb/corefile.c b/gdb/corefile.c index 19a96bc6f86..cfbb852b3c2 100644 --- a/gdb/corefile.c +++ b/gdb/corefile.c @@ -105,21 +105,24 @@ specify_exec_file_hook (void (*hook) (const char *)) void reopen_exec_file (void) { - int res; - struct stat st; - /* Don't do anything if there isn't an exec file. */ if (current_program_space->exec_bfd () == NULL) return; + bfd *exec_bfd = current_program_space->exec_bfd (); + + /* The main executable can't be an in-memory BFD object. If it was then + the use of bfd_stat below would not work as expected. */ + gdb_assert ((exec_bfd->flags & BFD_IN_MEMORY) == 0); + /* If the timestamp of the exec file has changed, reopen it. */ - std::string filename = bfd_get_filename (current_program_space->exec_bfd ()); - res = stat (filename.c_str (), &st); + struct stat st; + int res = bfd_stat (exec_bfd, &st); if (res == 0 - && current_program_space->ebfd_mtime + && current_program_space->ebfd_mtime != 0 && current_program_space->ebfd_mtime != st.st_mtime) - exec_file_attach (filename.c_str (), 0); + exec_file_attach (bfd_get_filename (exec_bfd), 0); } /* If we have both a core file and an exec file, diff --git a/gdb/testsuite/gdb.server/target-exec-file.exp b/gdb/testsuite/gdb.server/target-exec-file.exp index 9260df8b88d..40863538785 100644 --- a/gdb/testsuite/gdb.server/target-exec-file.exp +++ b/gdb/testsuite/gdb.server/target-exec-file.exp @@ -52,6 +52,19 @@ set target_exec [gdb_remote_download target $binfile] # prompt us if this is the right thing to do. gdb_test_no_output "set confirm off" +if { [allow_python_tests] } { + # Register an event handler for the executable changed event. + # This handler just copies the event into a global Python object. + gdb_test_multiline "Add connection_removed event" \ + "python" "" \ + "global_exec_changed_event = None" "" \ + "def executable_changed(event):" "" \ + " global global_exec_changed_event" "" \ + " global_exec_changed_event = event" "" \ + "gdb.events.executable_changed.connect (executable_changed)" "" \ + "end" "" +} + # Start gdbserver, but always in extended-remote mode, and then # connect to it from GDB. set res [gdbserver_start "--multi" $target_exec] @@ -59,6 +72,22 @@ set gdbserver_protocol "extended-remote" set gdbserver_gdbport [lindex $res 1] gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport +if { [allow_python_tests] } { + # When connecting to a remote target, if the user has not told GDB + # which executable to use, then GDB will figure out an executable + # from the remote target. + # + # As a result we expect to have seen an executable changed event. + with_test_prefix "after connecting" { + gdb_test "python print(global_exec_changed_event)" \ + "" + gdb_test "python print(global_exec_changed_event.progspace.executable_filename)" \ + [string_to_regexp target:$target_exec] + gdb_test "python print(global_exec_changed_event.reload)" "False" + gdb_test_no_output "python global_exec_changed_event = None" + } +} + # 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. @@ -104,6 +133,20 @@ gdb_assert { $saw_read_of_remote_exec } \ gdb_assert { $saw_read_of_syms_from_exec } \ "symbols were read from remote exec file" +if { [allow_python_tests] } { + # The 'file' command forces GDB to always load the executable, + # even if the same filename is used. In this case, as the + # filename is the same, this will show as a reload event. + with_test_prefix "after 'file' command" { + gdb_test "python print(global_exec_changed_event)" \ + "" + gdb_test "python print(global_exec_changed_event.progspace.executable_filename)" \ + [string_to_regexp target:$target_exec] + gdb_test "python print(global_exec_changed_event.reload)" "True" + gdb_test_no_output "python global_exec_changed_event = None" + } +} + # 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: @@ -155,10 +198,24 @@ proc start_inferior { testname expect_reread } { # see the symbols re-read now. start_inferior "start inferior the first time" false +if { [allow_python_tests] } { + # The executable hasn't changed. + with_test_prefix "after starting inferior for the first time" { + gdb_test "python print(global_exec_changed_event)" "None" + } +} + # 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 +if { [allow_python_tests] } { + # The executable still hasn't changed. + with_test_prefix "after starting inferior for the second time" { + gdb_test "python print(global_exec_changed_event)" "None" + } +} + # Delay for a short while so, when we touch the exec, we know the # timestamp will change. sleep 1 @@ -172,3 +229,16 @@ if { $status != 0 } { # 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 + +if { [allow_python_tests] } { + # The executable has now changed on disk. This will be a reload + # event. + with_test_prefix "after starting inferior for the third time" { + gdb_test "python print(global_exec_changed_event)" \ + "" + gdb_test "python print(global_exec_changed_event.progspace.executable_filename)" \ + [string_to_regexp target:$target_exec] + gdb_test "python print(global_exec_changed_event.reload)" "True" + gdb_test_no_output "python global_exec_changed_event = None" + } +} -- 2.25.4