From patchwork Mon Jul 10 17:01:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72436 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 5D0DB3857C44 for ; Mon, 10 Jul 2023 17:01:49 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from gproxy3-pub.mail.unifiedlayer.com (gproxy3-pub.mail.unifiedlayer.com [69.89.30.42]) by sourceware.org (Postfix) with ESMTPS id 826303858D32 for ; Mon, 10 Jul 2023 17:01:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 826303858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw13.mail.unifiedlayer.com (unknown [10.0.90.128]) by progateway5.mail.pro1.eigbox.com (Postfix) with ESMTP id D82C3100406DB for ; Mon, 10 Jul 2023 17:01:26 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id IuGMqIjCMNX2aIuGMqkKK6; Mon, 10 Jul 2023 17:01:26 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=NMAQR22g c=1 sm=1 tr=0 ts=64ac3966 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=OWjo9vPv0XrRhIrVQ50Ab3nP57M=:19 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=ws7JD89P4LkA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=A_ZZtpe6_zYDRwHpDnAA:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=BnBx4fll8y7jMhJSD/6jcB5a42W4dM6Ost5a5Vq0/64=; b=NnFomCF/kInl7JTtd4g9B3e8cD /HueVevQ+5uT5TBaNtfouK4904/k4x/Fz/h/Zdc+ICotm245hpH06IY+PFa2fTHZD/bHR9HF+XKUF 4PaiMyAObILSLzhUw7h70e6t5; Received: from 75-166-135-140.hlrn.qwest.net ([75.166.135.140]:41754 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1qIuGM-001I1a-1m; Mon, 10 Jul 2023 11:01:26 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Remove target_close Date: Mon, 10 Jul 2023 11:01:17 -0600 Message-ID: <20230710170117.1574727-1-tom@tromey.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.135.140 X-Source-L: No X-Exim-ID: 1qIuGM-001I1a-1m X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-135-140.hlrn.qwest.net (localhost.localdomain) [75.166.135.140]:41754 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3025.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" I noticed that target_close is only called in two places: solib-svr4.c, and target_ops_ref_policy::decref. This patch fixes the former by changing target_bfd_reopen to return a target_ops_up and then fixing the sole caller. Then it removes target_close by inlining its body into the decref method. The advantage of this approach is that targets are now automatically managed. Regression tested on x86-64 Fedora 38. --- gdb/bfd-target.c | 4 ++-- gdb/bfd-target.h | 2 +- gdb/solib-svr4.c | 16 ++++++---------- gdb/target.c | 25 ++++++++++--------------- gdb/target.h | 9 --------- 5 files changed, 19 insertions(+), 37 deletions(-) diff --git a/gdb/bfd-target.c b/gdb/bfd-target.c index 6b8a839b286..9e1b980957c 100644 --- a/gdb/bfd-target.c +++ b/gdb/bfd-target.c @@ -94,10 +94,10 @@ target_bfd::target_bfd (const gdb_bfd_ref_ptr &abfd) { } -target_ops * +target_ops_up target_bfd_reopen (const gdb_bfd_ref_ptr &abfd) { - return new target_bfd (abfd); + return target_ops_up (new target_bfd (abfd)); } void diff --git a/gdb/bfd-target.h b/gdb/bfd-target.h index d77f0367fd5..eb33374c6ea 100644 --- a/gdb/bfd-target.h +++ b/gdb/bfd-target.h @@ -25,6 +25,6 @@ struct target_ops; /* Given an existing BFD, re-open it as a "struct target_ops". */ -struct target_ops *target_bfd_reopen (const gdb_bfd_ref_ptr &bfd); +target_ops_up target_bfd_reopen (const gdb_bfd_ref_ptr &bfd); #endif diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c index 0ef83726d62..f1fa437e31a 100644 --- a/gdb/solib-svr4.c +++ b/gdb/solib-svr4.c @@ -2425,7 +2425,7 @@ enable_break (struct svr4_info *info, int from_tty) CORE_ADDR load_addr = 0; int load_addr_found = 0; int loader_found_in_list = 0; - struct target_ops *tmp_bfd_target; + target_ops_up tmp_bfd_target; sym_addr = 0; @@ -2482,8 +2482,8 @@ enable_break (struct svr4_info *info, int from_tty) if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) { CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit; - CORE_ADDR tmp_entry_point = exec_entry_point (tmp_bfd.get (), - tmp_bfd_target); + CORE_ADDR tmp_entry_point + = exec_entry_point (tmp_bfd.get (), tmp_bfd_target.get ()); gdb_assert (load_addr < space_size); @@ -2512,7 +2512,8 @@ enable_break (struct svr4_info *info, int from_tty) inferior_ptid, target_gdbarch ()); load_addr = (regcache_read_pc (regcache) - - exec_entry_point (tmp_bfd.get (), tmp_bfd_target)); + - exec_entry_point (tmp_bfd.get (), + tmp_bfd_target.get ())); } if (!loader_found_in_list) @@ -2564,12 +2565,7 @@ enable_break (struct svr4_info *info, int from_tty) target, this will always produce an unrelocated value. */ sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (), sym_addr, - tmp_bfd_target); - - /* We're done with both the temporary bfd and target. Closing - the target closes the underlying bfd, because it holds the - only remaining reference. */ - target_close (tmp_bfd_target); + tmp_bfd_target.get ()); if (sym_addr != 0) { diff --git a/gdb/target.c b/gdb/target.c index fecbc89d3ca..16f43d072cd 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -1173,7 +1173,16 @@ target_ops_ref_policy::decref (target_ops *t) { if (t->stratum () == process_stratum) connection_list_remove (as_process_stratum_target (t)); - target_close (t); + + for (inferior *inf : all_inferiors ()) + gdb_assert (!inf->target_is_pushed (t)); + + fileio_handles_invalidate_target (t); + + t->close (); + + if (targetdebug) + gdb_printf (gdb_stdlog, "closing target\n"); } } @@ -3752,20 +3761,6 @@ debug_target::info () const -void -target_close (struct target_ops *targ) -{ - for (inferior *inf : all_inferiors ()) - gdb_assert (!inf->target_is_pushed (targ)); - - fileio_handles_invalidate_target (targ); - - targ->close (); - - if (targetdebug) - gdb_printf (gdb_stdlog, "target_close ()\n"); -} - int target_thread_alive (ptid_t ptid) { diff --git a/gdb/target.h b/gdb/target.h index 5f9a2a2d97a..6ae400e2cc2 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -1410,15 +1410,6 @@ extern target_ops *get_dummy_target (); extern const char *target_shortname (); -/* Does whatever cleanup is required for a target that we are no - longer going to be calling. This routine is automatically always - called after popping the target off the target stack - the target's - own methods are no longer available through the target vector. - Closing file descriptors and freeing all memory allocated memory are - typical things it should do. */ - -void target_close (struct target_ops *targ); - /* Find the correct target to use for "attach". If a target on the current stack supports attaching, then it is returned. Otherwise, the default run target is returned. */