From patchwork Fri Nov 18 16:42:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 60846 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 AD6E83839D8D for ; Fri, 18 Nov 2022 16:46:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD6E83839D8D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668789997; bh=gmM9wWh1uSCpmrBLG8Lj2WZdjlA1uHfDKArrCa8bCeY=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=yyVUDficF6c0H+Qp7Ynon+A5z2AIFUvSArlNGoTe8INyquliegsLMgLQetPfy/wCJ 8b1x0/a3LLDnSEpBC2mX0N486CueuLC4ldG3Wq4eaVWswWppKBd/QUBHh2TKy3L8gr F7ViajYH/X7aCVDNZt38vTNR8728I0Zst8QCiawU= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id BF4D2384F6FB for ; Fri, 18 Nov 2022 16:43:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BF4D2384F6FB Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-483-eul2Si6XOjq3aNbVILj7Nw-1; Fri, 18 Nov 2022 11:43:09 -0500 X-MC-Unique: eul2Si6XOjq3aNbVILj7Nw-1 Received: by mail-wm1-f70.google.com with SMTP id o5-20020a05600c510500b003cfca1a327fso2488682wms.8 for ; Fri, 18 Nov 2022 08:43:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=gmM9wWh1uSCpmrBLG8Lj2WZdjlA1uHfDKArrCa8bCeY=; b=KsMwoqJN1wZgCrfFvyHNOPt+k99GIit5tl4OETxuhWyR+Gd3lVfAQ9AZkfcW1Oomc+ zwFWJnnjd1Y7U4Yqjk4b+Mu67P84ysNMB5M7c+9+sGTJe8P1tPjZ3QxstK32iBDLngRv GzpywLWsMxZS6OIzjoyHLed8+ORs7kZpsA3NQb6/8qlchTfS8LIBeKC3O0/mOWcxrOLu vdgCo8FEakSTCP5keVt7VkurYrxTEQjVeALpKsNiHz3cPyxsZam2D/IMf7mgWIvbRYCA VlBYosvgVH9FpssYnKG7ql1vuqazXFJK2K40NK0qgSHh6Y8JwU6JOJXNrHhd2PVdfDCq ZDLA== X-Gm-Message-State: ANoB5pn/CMcn3tkB00sKKYUD1d74Y85R5D5uXATQpdEwwawKVtHzSv8/ w7Pze5ERnBt4fSOqMU2uU1vW8acTjtH+uB3TXlOsy2JGoWAwKJ66kgB9EICNC1/bGvSra8VZwTX lJ4WX86kHszGFvMT5K2cZFTCoH27bNZTLkpq5yMQ7krDHqvGgREg+UafwIN5ckR1X+/DO2puw9A == X-Received: by 2002:a05:600c:15d6:b0:3cf:54a5:b7ee with SMTP id v22-20020a05600c15d600b003cf54a5b7eemr8965044wmf.106.1668789788128; Fri, 18 Nov 2022 08:43:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf4jeCBiCJRu8oDlJT1HgosouHeiZ7btOYM9lQcF6Vef67uUBVhgOptbq4HMDUsLB2nw1ta0Ew== X-Received: by 2002:a05:600c:15d6:b0:3cf:54a5:b7ee with SMTP id v22-20020a05600c15d600b003cf54a5b7eemr8965016wmf.106.1668789787763; Fri, 18 Nov 2022 08:43:07 -0800 (PST) Received: from localhost ([31.111.84.238]) by smtp.gmail.com with ESMTPSA id t23-20020adfa2d7000000b0023662d97130sm4095994wra.20.2022.11.18.08.43.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 08:43:07 -0800 (PST) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv3 4/7] gdb: remove the pop_all_targets (and friends) global functions Date: Fri, 18 Nov 2022 16:42:55 +0000 Message-Id: <4b85a8b4e635dafa1ca18e3602ba04669876b6b4.1668789658.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=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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.29 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" This commit removes the global functions pop_all_targets, pop_all_targets_above, and pop_all_targets_at_and_above, and makes them methods on the inferior class. As the pop_all_targets functions will unpush each target, which decrements the targets reference count, it is possible that the target might be closed. Right now, closing a target, in some cases, depends on the current inferior being set correctly, that is, to the inferior from which the target was popped. To facilitate this I have used switch_to_inferior_no_thread within the new methods. Previously it was the responsibility of the caller to ensure that the correct inferior was selected. In a couple of places (event-top.c and top.c) I have been able to remove a previous switch_to_inferior_no_thread call. In remote_unpush_target (remote.c) I have left the switch_to_inferior_no_thread call as it is required for the generic_mourn_inferior call. --- gdb/event-top.c | 3 +-- gdb/inferior.c | 39 ++++++++++++++++++++++++++++++++++++++ gdb/inferior.h | 20 ++++++++++++++++++++ gdb/remote.c | 2 +- gdb/scoped-mock-context.h | 2 +- gdb/target.c | 40 +-------------------------------------- gdb/target.h | 11 ----------- gdb/top.c | 3 +-- 8 files changed, 64 insertions(+), 56 deletions(-) diff --git a/gdb/event-top.c b/gdb/event-top.c index 0e371194ee3..d858104c1e8 100644 --- a/gdb/event-top.c +++ b/gdb/event-top.c @@ -1293,10 +1293,9 @@ async_disconnect (gdb_client_data arg) for (inferior *inf : all_inferiors ()) { - switch_to_inferior_no_thread (inf); try { - pop_all_targets (); + inf->pop_all_targets (); } catch (const gdb_exception &exception) { diff --git a/gdb/inferior.c b/gdb/inferior.c index 7eb2bd97907..4bad2198af7 100644 --- a/gdb/inferior.c +++ b/gdb/inferior.c @@ -103,6 +103,45 @@ inferior::unpush_target (struct target_ops *t) return m_target_stack.unpush (t); } +/* See inferior.h. */ + +void inferior::unpush_target_and_assert (struct target_ops *target) +{ + gdb_assert (current_inferior () == this); + + if (!unpush_target (target)) + internal_error ("pop_all_targets couldn't find target %s\n", + target->shortname ()); +} + +/* See inferior.h. */ + +void inferior::pop_all_targets_above (enum strata stratum) +{ + /* Unpushing a target might cause it to close. Some targets currently + rely on the current_inferior being set for their ::close method, so we + temporarily switch inferior now. */ + scoped_restore_current_pspace_and_thread restore_pspace_and_thread; + switch_to_inferior_no_thread (this); + + while (top_target ()->stratum () > stratum) + unpush_target_and_assert (top_target ()); +} + +/* See inferior.h. */ + +void inferior::pop_all_targets_at_and_above (enum strata stratum) +{ + /* Unpushing a target might cause it to close. Some targets currently + rely on the current_inferior being set for their ::close method, so we + temporarily switch inferior now. */ + scoped_restore_current_pspace_and_thread restore_pspace_and_thread; + switch_to_inferior_no_thread (this); + + while (top_target ()->stratum () >= stratum) + unpush_target_and_assert (top_target ()); +} + void inferior::set_tty (std::string terminal_name) { diff --git a/gdb/inferior.h b/gdb/inferior.h index 69525a2e053..2917f42e624 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -377,6 +377,22 @@ class inferior : public refcounted_object, target_ops *top_target () { return m_target_stack.top (); } + /* Unpush all targets except the dummy target from m_target_stack. As + targets are removed from m_target_stack their reference count is + decremented, which may cause a target to close. */ + void pop_all_targets () + { pop_all_targets_above (dummy_stratum); } + + /* Unpush all targets above STRATUM from m_target_stack. As targets are + removed from m_target_stack their reference count is decremented, + which may cause a target to close. */ + void pop_all_targets_above (enum strata stratum); + + /* Unpush all targets at and above STRATUM from m_target_stack. As + targets are removed from m_target_stack their reference count is + decremented, which may cause a target to close. */ + void pop_all_targets_at_and_above (enum strata stratum); + /* Return the target at process_stratum level in this inferior's target stack. */ struct process_stratum_target *process_target () @@ -595,6 +611,10 @@ class inferior : public refcounted_object, registry registry_fields; private: + + /* Unpush TARGET and assert that it worked. */ + void unpush_target_and_assert (struct target_ops *target); + /* The inferior's target stack. */ target_stack m_target_stack; diff --git a/gdb/remote.c b/gdb/remote.c index 24da3d544d5..4db5fd18b14 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -5717,7 +5717,7 @@ remote_unpush_target (remote_target *target) for (inferior *inf : all_inferiors (target)) { switch_to_inferior_no_thread (inf); - pop_all_targets_at_and_above (process_stratum); + inf->pop_all_targets_at_and_above (process_stratum); generic_mourn_inferior (); } diff --git a/gdb/scoped-mock-context.h b/gdb/scoped-mock-context.h index a9895303015..87c1df0d206 100644 --- a/gdb/scoped-mock-context.h +++ b/gdb/scoped-mock-context.h @@ -71,7 +71,7 @@ struct scoped_mock_context ~scoped_mock_context () { inferior_list.erase (inferior_list.iterator_to (mock_inferior)); - pop_all_targets_at_and_above (process_stratum); + mock_inferior.pop_all_targets_at_and_above (process_stratum); } }; diff --git a/gdb/target.c b/gdb/target.c index 75bc6c680d8..b2af51551ba 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -1236,44 +1236,6 @@ target_stack::unpush (target_ops *t) return true; } -/* Unpush TARGET and assert that it worked. */ - -static void -unpush_target_and_assert (struct target_ops *target) -{ - if (!current_inferior ()->unpush_target (target)) - { - gdb_printf (gdb_stderr, - "pop_all_targets couldn't find target %s\n", - target->shortname ()); - internal_error (_("failed internal consistency check")); - } -} - -void -pop_all_targets_above (enum strata above_stratum) -{ - while ((int) (current_inferior ()->top_target ()->stratum ()) - > (int) above_stratum) - unpush_target_and_assert (current_inferior ()->top_target ()); -} - -/* See target.h. */ - -void -pop_all_targets_at_and_above (enum strata stratum) -{ - while ((int) (current_inferior ()->top_target ()->stratum ()) - >= (int) stratum) - unpush_target_and_assert (current_inferior ()->top_target ()); -} - -void -pop_all_targets (void) -{ - pop_all_targets_above (dummy_stratum); -} - void target_unpusher::operator() (struct target_ops *ops) const { @@ -2530,7 +2492,7 @@ target_preopen (int from_tty) it doesn't (which seems like a win for UDI), remove it now. */ /* Leave the exec target, though. The user may be switching from a live process to a core of the same program. */ - pop_all_targets_above (file_stratum); + current_inferior ()->pop_all_targets_above (file_stratum); target_pre_inferior (from_tty); } diff --git a/gdb/target.h b/gdb/target.h index 68446a39c1b..547ee8a3bbd 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -2389,17 +2389,6 @@ extern void target_pre_inferior (int); extern void target_preopen (int); -/* Does whatever cleanup is required to get rid of all pushed targets. */ -extern void pop_all_targets (void); - -/* Like pop_all_targets, but pops only targets whose stratum is at or - above STRATUM. */ -extern void pop_all_targets_at_and_above (enum strata stratum); - -/* Like pop_all_targets, but pops only targets whose stratum is - strictly above ABOVE_STRATUM. */ -extern void pop_all_targets_above (enum strata above_stratum); - extern CORE_ADDR target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset); diff --git a/gdb/top.c b/gdb/top.c index e9794184f07..f5fc7276ac7 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -1845,10 +1845,9 @@ quit_force (int *exit_arg, int from_tty) them all out. */ for (inferior *inf : all_inferiors ()) { - switch_to_inferior_no_thread (inf); try { - pop_all_targets (); + inf->pop_all_targets (); } catch (const gdb_exception &ex) {