From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id EfqpGb+z42oHLA8AWB0awg (envelope-from ) for ; Thu, 29 Oct 2026 16:59:43 -0400 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=QsxMbtJY; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 639351E06B; Thu, 29 Oct 2026 16:59:43 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.1 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,RCVD_IN_SBL_CSS autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (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 CECF71E007 for ; Thu, 29 Oct 2026 16:59:41 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 11D744BA798A for ; Thu, 29 Oct 2026 20:59:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 11D744BA798A Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QsxMbtJY Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id F07E04BA900E for ; Thu, 29 Oct 2026 20:58:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F07E04BA900E Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 F07E04BA900E Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793307513; cv=none; b=v1GHaEORC0fO1GCFGluJI4JX9lz8KK+xTYuOYqYxhSkIF8DRbZXRDI7sEJ/n0ZmNSER0puT97JkjDL1rn/C4Bx6Kv2VfFM1vZgSpAnh7NH+qAb8n4bchhpo/Efbo8LgxxIRtgfe9Kn7Y50aj0vizB4d0g4AY3863NjIQgCbvABI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793307513; c=relaxed/simple; bh=z1JBbleeHb/gcu0TzkInBLRkbykqpce14Oug4arh+tA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WioWFXX4eO2rkp/6hR+ssIVrETA4xWn3tplNt1N2hmlaF2k05qU/SvkwzWmclKPTMk1zfExZ+EzEC4O9c9BjjK7I7BkuDI8N3/d0CnZ0zhLLslfDALn577qH4q4YuslnZRD8Ft60DNoB/SGqJEWDC8Vf1XIq1YfqetRJA7iyLWQ= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QsxMbtJY DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F07E04BA900E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793307512; 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=k0MVOlMeBQvzs7xaQEv0rY5+EHctpz2rP0wO/rJsQyo=; b=QsxMbtJYWONJCtYhc6mE5ZcWyNiryXNrHz8Z2ahMkwMA0wUONlFJ+gzFHdaMuwoLAAPEMP SStCeUiCIZwIO+4iwuduPGJyVqsn0xE9ZXw6wVNaaCAdB9ujozH7/B99xiL8bJQ0kBJgSj GL2+ImvlHrAwILxSQVUh32zihs5fGpQ= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-435-u6kMyb6iOYeKikAstYAPFA-1; Thu, 29 Oct 2026 16:58:28 -0400 X-MC-Unique: u6kMyb6iOYeKikAstYAPFA-1 X-Mimecast-MFC-AGG-ID: u6kMyb6iOYeKikAstYAPFA_1793307507 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 99FF51955F2F for ; Thu, 29 Oct 2026 20:58:27 +0000 (UTC) Received: from glarsen-thinkpadp16vgen1.rmtbr.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9F236433; Thu, 29 Oct 2026 20:58:26 +0000 (UTC) From: Guinevere Larsen To: gdb-patches@sourceware.org Cc: Guinevere Larsen Subject: [PATCH 4/7] gdb: make regcache save a thread pointer, not ptid Date: Thu, 29 Oct 2026 17:57:40 -0300 Message-ID: <20261029205743.1917668-5-guinevere@redhat.com> In-Reply-To: <20261029205743.1917668-1-guinevere@redhat.com> References: <20261029205743.1917668-1-guinevere@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: eE77k3fAP8JiSWwwLTX-ikJxXueHBIm_P-Hm1XQdIaM_1793307507 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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 Regcaches save a PTID as a way to for GDB to identify the thread that the cache belongs to. This didn't use to be an issue because, until now, it was only reasonable to touch regcaches of live threads. However, when record-full is capable of replaying already-exited threads, there will be times when we will need to find the thread_info pointer for an exited thread, and for that case, inferior::find_thread won't work. This commit changes regcache so that it now saves a thread pointer. One simplification that this change makes is that, when a thread's ptid changes, we no longer need to update the regcache, so the observer function and selftest can be simply removed. Another future simplification that can be done is that callers of regcache::ptid that only want to find a thread pointer can now call regcache::thread, but this is left for the future to minimize accidental impact of this specific commit. This change caused a regression to the get_thread_arch_regcache selftest, because we now need a real thread in the mock inferior for each PTID. To fix this, the mock context can now add new threads with the desired PTIDs. This mostly works, however, properly deleting the memory for the threads caused the inferior destructor to de-reference a null pointer as it tried to unlink the threads from the intrusive list. Finally, as a sidenote, ravenscar_thread sometimes needs to temporarily change the ptid of a regcache for some operations that I haven't quite understood. However, considering that the process for getting the PTID will return the new one, I believe the change should be trivial. This is just some internal change, no user-visible changes are expected. --- gdb/ravenscar-thread.c | 41 ++++++++++----- gdb/regcache.c | 105 ++++---------------------------------- gdb/regcache.h | 17 +++--- gdb/scoped-mock-context.h | 18 +++++++ 4 files changed, 67 insertions(+), 114 deletions(-) diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c index b5e0979a7f1..a5324be19c6 100644 --- a/gdb/ravenscar-thread.c +++ b/gdb/ravenscar-thread.c @@ -602,32 +602,33 @@ ravenscar_arch_ops::store_register (struct regcache *regcache, stack_address); } -/* Temporarily set the ptid of a regcache to some other value. When - this object is destroyed, the regcache's original ptid is +/* Temporarily set the thread of a regcache to some other value. When + this object is destroyed, the regcache's original thread is restored. */ -class temporarily_change_regcache_ptid +class temporarily_change_regcache_thread { public: - temporarily_change_regcache_ptid (struct regcache *regcache, ptid_t new_ptid) + temporarily_change_regcache_thread (struct regcache *regcache, + thread_info *new_thread) : m_regcache (regcache), - m_save_ptid (regcache->ptid ()) + m_save_thread (regcache->thread ()) { - m_regcache->set_ptid (new_ptid); + m_regcache->set_thread (new_thread); } - ~temporarily_change_regcache_ptid () + ~temporarily_change_regcache_thread () { - m_regcache->set_ptid (m_save_ptid); + m_regcache->set_thread (m_save_thread); } private: /* The regcache. */ struct regcache *m_regcache; - /* The saved ptid. */ - ptid_t m_save_ptid; + /* The saved thread. */ + thread_info *m_save_thread; }; ravenscar_thread_target::fpu_state @@ -709,7 +710,12 @@ ravenscar_thread_target::fetch_registers (struct regcache *regcache, if (use_beneath) { - temporarily_change_regcache_ptid changer (regcache, base); + process_stratum_target *proc_target + = as_process_stratum_target (this->beneath ()); + thread_info *base_thread = proc_target->find_thread (base); + + temporarily_change_regcache_thread changer (regcache, + base_thread); beneath ()->fetch_registers (regcache, i); } else @@ -755,7 +761,12 @@ ravenscar_thread_target::store_registers (struct regcache *regcache, if (use_beneath) { - temporarily_change_regcache_ptid changer (regcache, base); + process_stratum_target *proc_target + = as_process_stratum_target (this->beneath ()); + thread_info *base_thread = proc_target->find_thread (base); + + temporarily_change_regcache_thread changer (regcache, + base_thread); beneath ()->store_registers (regcache, i); } else @@ -776,7 +787,11 @@ ravenscar_thread_target::prepare_to_store (struct regcache *regcache) if (task_is_currently_active (ptid)) { ptid_t base = get_base_thread_from_ravenscar_task (ptid); - temporarily_change_regcache_ptid changer (regcache, base); + process_stratum_target *proc_target + = as_process_stratum_target (this->beneath ()); + thread_info *tp = proc_target->find_thread (base); + + temporarily_change_regcache_thread changer (regcache, tp); beneath ()->prepare_to_store (regcache); } else diff --git a/gdb/regcache.c b/gdb/regcache.c index 773e8ac37d0..755b92d48ee 100644 --- a/gdb/regcache.c +++ b/gdb/regcache.c @@ -214,7 +214,7 @@ regcache::regcache (inferior *inf_for_target_calls, gdbarch *gdbarch) : detached_regcache (gdbarch, false), m_inf_for_target_calls (inf_for_target_calls) { - m_ptid = minus_one_ptid; + m_thread = nullptr; } readonly_detached_regcache::readonly_detached_regcache (regcache &src) @@ -390,7 +390,7 @@ get_thread_arch_regcache (inferior *inf_for_target_calls, ptid_t ptid, /* It does not exist, create it. */ regcache *new_regcache = new regcache (inf_for_target_calls, arch); - new_regcache->set_ptid (ptid); + new_regcache->set_thread (inf_for_target_calls->find_thread (ptid)); /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up constructor explicitly instead of implicitly. */ ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache))); @@ -455,37 +455,6 @@ regcache_observer_target_changed (struct target_ops *target) registers_changed (); } -/* Update regcaches related to OLD_PTID to now use NEW_PTID. */ -static void -regcache_thread_ptid_changed (process_stratum_target *target, - ptid_t old_ptid, ptid_t new_ptid) -{ - /* Look up map for target. */ - auto pid_ptid_regc_map_it = regcaches.find (target); - if (pid_ptid_regc_map_it == regcaches.end ()) - return; - - /* Look up map for pid. */ - pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second; - auto ptid_regc_map_it = pid_ptid_regc_map.find (old_ptid.pid ()); - if (ptid_regc_map_it == pid_ptid_regc_map.end ()) - return; - - /* Update all regcaches belonging to old_ptid. */ - ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second; - auto range = ptid_regc_map.equal_range (old_ptid); - for (auto it = range.first; it != range.second;) - { - regcache_up rc = std::move (it->second); - rc->set_ptid (new_ptid); - - /* Remove old before inserting new, to avoid rehashing, - which would invalidate iterators. */ - it = ptid_regc_map.erase (it); - ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc))); - } -} - /* Low level examining and depositing of registers. The caller is responsible for making sure that the inferior is @@ -1703,6 +1672,12 @@ struct regcache_test_data : test_ctx_1 (current_inferior ()->arch ()), test_ctx_2 (current_inferior ()->arch ()) { + /* Ensure we have all the threads we need to run the regcache tests. */ + std::vector ptids + = {ptid_t (1,2), ptid_t (1,3), + ptid_t (2,1), ptid_t (2,2), ptid_t (2, 3)}; + test_ctx_1.create_additional_threads (ptids.data (), ptids.size ()); + test_ctx_2.create_additional_threads (ptids.data (), ptids.size ()); /* Ensure the regcaches container is empty at the start. */ registers_changed (); } @@ -1945,7 +1920,7 @@ cooked_read_test (struct gdbarch *gdbarch) &mockctx.mock_inferior, gdbarch))) ->second; - readwrite.set_ptid (mockctx.mock_ptid); + readwrite.set_thread (&mockctx.mock_thread); gdb::byte_vector buf (register_size (gdbarch, nonzero_regnum)); readwrite.raw_read (nonzero_regnum, buf); @@ -2072,7 +2047,7 @@ cooked_write_test (struct gdbarch *gdbarch) &ctx.mock_inferior, gdbarch))) ->second; - readwrite.set_ptid (ctx.mock_ptid); + readwrite.set_thread (&ctx.mock_thread); const int num_regs = gdbarch_num_cooked_regs (gdbarch); for (auto regnum = 0; regnum < num_regs; regnum++) @@ -2155,62 +2130,6 @@ cooked_write_test (struct gdbarch *gdbarch) regcaches.erase (&ctx.mock_target); } - -/* Verify that when two threads with the same ptid exist (from two different - targets) and one of them changes ptid, we only update the appropriate - regcaches. */ - -static void -regcache_thread_ptid_changed () -{ - /* This test relies on the global regcache list to initially be empty. */ - registers_changed (); - - /* Any arch will do. */ - gdbarch *arch = current_inferior ()->arch (); - - /* Prepare two targets with one thread each, with the same ptid. */ - scoped_mock_context target1 (arch); - scoped_mock_context target2 (arch); - - ptid_t old_ptid (111, 222); - ptid_t new_ptid (111, 333); - - target1.mock_inferior.pid = old_ptid.pid (); - target1.mock_thread.ptid = old_ptid; - target1.mock_inferior.ptid_thread_map.clear (); - target1.mock_inferior.ptid_thread_map[old_ptid] = &target1.mock_thread; - - target2.mock_inferior.pid = old_ptid.pid (); - target2.mock_thread.ptid = old_ptid; - target2.mock_inferior.ptid_thread_map.clear (); - target2.mock_inferior.ptid_thread_map[old_ptid] = &target2.mock_thread; - - gdb_assert (regcaches.empty ()); - - /* Populate the regcaches container. */ - get_thread_arch_regcache (&target1.mock_inferior, old_ptid, arch); - get_thread_arch_regcache (&target2.mock_inferior, old_ptid, arch); - - gdb_assert (regcaches.size () == 2); - gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1); - gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0); - gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1); - gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0); - - thread_change_ptid (&target1.mock_target, old_ptid, new_ptid); - - gdb_assert (regcaches.size () == 2); - gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0); - gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1); - gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1); - gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0); - - /* Leave the regcache list empty. */ - registers_changed (); - gdb_assert (regcaches.empty ()); -} - } /* namespace selftests */ #endif /* GDB_SELF_TEST */ @@ -2220,8 +2139,6 @@ INIT_GDB_FILE (regcache) gdb::observers::target_changed.attach (regcache_observer_target_changed, "regcache"); - gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed, - "regcache"); cmd_list_element *maintenance_flush_register_cache_cmd = add_cmd ("register-cache", class_maintenance, reg_flush_command, @@ -2249,7 +2166,5 @@ INIT_GDB_FILE (regcache) selftests::cooked_read_test); selftests::register_test_foreach_arch ("regcache::cooked_write_test", selftests::cooked_write_test); - selftests::register_test ("regcache_thread_ptid_changed", - selftests::regcache_thread_ptid_changed); #endif } diff --git a/gdb/regcache.h b/gdb/regcache.h index 66e78a7c2fb..a69ed8a8401 100644 --- a/gdb/regcache.h +++ b/gdb/regcache.h @@ -24,11 +24,11 @@ #include "gdbsupport/common-regcache.h" #include "gdbsupport/function-view.h" #include "gdbsupport/traits.h" +#include "gdbthread.h" struct regcache; struct regset; struct gdbarch; -class thread_info; struct process_stratum_target; struct inferior; class ui_out; @@ -449,14 +449,19 @@ class regcache : public detached_regcache ptid_t ptid () const { - gdb_assert (m_ptid != minus_one_ptid); + return thread ()->ptid; + } + + thread_info *thread () const + { + gdb_assert (m_thread != nullptr); - return m_ptid; + return m_thread; } - void set_ptid (const ptid_t ptid) + void set_thread (thread_info *tp) { - this->m_ptid = ptid; + this->m_thread = tp; } /* Return a string with the contents of a register, suitable for debug output. */ @@ -497,7 +502,7 @@ class regcache : public detached_regcache /* If this is a read-write cache, which thread's registers is it connected to? */ - ptid_t m_ptid; + thread_info *m_thread; friend regcache *get_thread_arch_regcache (inferior *inf_for_target_calls, ptid_t ptid, gdbarch *gdbarch); diff --git a/gdb/scoped-mock-context.h b/gdb/scoped-mock-context.h index 165abef5063..ce98f51dceb 100644 --- a/gdb/scoped-mock-context.h +++ b/gdb/scoped-mock-context.h @@ -41,6 +41,8 @@ struct scoped_mock_context program_space mock_pspace {new_address_space ()}; inferior mock_inferior {mock_ptid.pid ()}; thread_info mock_thread {&mock_inferior, mock_ptid}; + thread_info **additional_threads = nullptr; + int num_additional_threads = 0; scoped_restore_current_pspace_and_thread restore_pspace_thread; @@ -72,6 +74,22 @@ struct scoped_mock_context { inferior_list.erase (inferior_list.iterator_to (mock_inferior)); mock_inferior.pop_all_targets_at_and_above (process_stratum); + /* Unfortunately, here, we may leak some memory from additional_threads + If we delete them now, we will get a segmentation fault when deleting + the inferior. Since this is only used when executing some selftests + it shouldn't be a big deal. */ + } + + void create_additional_threads (ptid_t *ptids, int num) + { + num_additional_threads = num; + additional_threads = new thread_info *[num]; + for (int i = 0; i < num; i++) + { + additional_threads[i] = new thread_info (&mock_inferior, ptids[i]); + mock_inferior.thread_list.push_back (*additional_threads[i]); + mock_inferior.ptid_thread_map[ptids[i]] = additional_threads[i]; + } } }; -- 2.56.0