From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id UZzdOts992ruEgkAWB0awg (envelope-from ) for ; Fri, 13 Nov 2026 11:42:03 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=QvniXWSO; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id ECE6B1E0BC; Fri, 13 Nov 2026 11:42:03 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=unavailable autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 B9A201E087 for ; Fri, 13 Nov 2026 11:42:02 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4928B4B1A2D4 for ; Fri, 13 Nov 2026 16:42:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4928B4B1A2D4 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=QvniXWSO Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 7197D4B7A1E6 for ; Fri, 13 Nov 2026 16:41:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7197D4B7A1E6 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7197D4B7A1E6 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794588091; cv=none; b=SOyG5k6UjbB2a5WgOeEZJIW4K4IWMDa39qdx5oMuCPKdpP02s+MGAe+ZgtYBqDoDobe7uN7h9G13OxKqwZNWipsUogGjhBpBAGjqUldRpak5t/DZxOrLYVBerbd7WRhKYBMao4m8xDiN06ssgsyXbZsnAUZoEOJ706hHXnySDko= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794588091; c=relaxed/simple; bh=LQ3mHgG936fcr/mlDUZVenFBfmVJ8yKFLpTmSja9y+A=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lhUxZ71DBzz6RZixSjPBJxHsESsYeuM60GX+LHnjhhyR9o9okKP2UpqXAHZad3m9IZKCoEPor1D1HsWMHChOLCVah4xTN5gkVdBy5Tptkuoy5oVlqCLzrJwxRf7etDMiydKJIETXXcDUBtjkD5OEs/pkcPGFwIeXKIJ28l7zRw0= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=QvniXWSO DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7197D4B7A1E6 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-4a214af2076so7671595e9.0 for ; Fri, 13 Nov 2026 08:41:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1794588090; x=1795192890; darn=sourceware.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=7wCBE4sOkoaRX+PGp02d7J6xGbQxmStGmeG0AmYOoz4=; b=QvniXWSOTt/eTmr1LRZ4WOa3RYamE/OaJqJjlhFuJ8zvnZOcWQoufSOin7I2FG2zhl /WfvCyP+VYRGbpz0oxJx2n1XGt8VBKYkth5PPHAaTy//jd9ByejXmgJ7YTsrMCGRbrGF qaAD1sCKpW4Dm8JEGxpkn33PPd87GIR3qO4+8WgeZUStw270Lzmu0hpUvK/1/p7v3Gh2 ONXzLVLhnkDHWtesv1e3v3hkABjLQhfkvYPvhoexP3fTpWyohVHwwK8mkwGqNXM1UlXR HD2Wq+plDqEHXBMg8daafCA2q1M5VboT3lxDhOjqNmp4/HYP5GO7yfZPMrwUZSRBVc+r NrMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794588090; x=1795192890; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=7wCBE4sOkoaRX+PGp02d7J6xGbQxmStGmeG0AmYOoz4=; b=cXkHMv7tv7ribel0vgErGu1GQbgUa/JaUPHm9w9cRS+GpvMYfygWsfkapjn4qF/R5b JywD/JiSkjAO8G9fS/oEwgSi3e3qW0jYQVgORx61Sa7X2IqL4/f5LWdS9UFQSigN5jZl +qEaVaExqvu1Ziml5LqPzsoBCME4nB5pMd+eevzgkmTpnUG1bh7zgh/XRp/VT7tGgXjm xRc9vWP46xA39gR/l63ADs0vshh7+m5Ma1BHKB3B1FAX8JABwD24ntsjbDrkaNWQJx5R RgUapxvfS9f6J52wcDalyvIFrEtWNnbfyLzSTx7DAz/mZTMiN5WdJNRCWANQZszLitkP T8pA== X-Gm-Message-State: APuU4Z9ovkJa8ObpTQdS8wdIKKXpIkf5H7sn4o24+UE7YRiBr/+LuWAe HHsbbWLyMZXyKTP2Xx8KTBS8ozwzdN5pENmsTTZTR41ukygEZk03P1SDYOBBdGMXNwR7Z6y0IQv qu4Eocw== X-Gm-Gg: AYBFou1VaiGdNsi2SDycUdM8cqhXoyBM6epgI+bLJYWzOxR2lGWRclv123HBbMgoYDL 7Q7NtSlWOZrbXWNQf1/xwbroyV+ItcGzTmRZelb98zbxODhFvjJcOAPZMfBf1rM7nVAAizYurUj AWVgzRkRv9IHFIvxuN93ePs0HBFrVru0uW6QW/RNZxk9E8VAmnJCrLjlFr3OjIl37l+ZQzs99TC sIio84jizeROGqpc02uBdWHrvtcU8LaC78qkhQ5EScVn4ZEbXdtng5vpsFDhMGWTSOczMK+WaEx 7ojtNKePfgFKJLxI87PqeWOR3372NKd0gYkh4EZhx8lSHBqsbLjVCgx61Sq0PyIQ1oC/zWGIJNM Oq7nehtbb9sTUvsU7tZmTePiN8VgMzQYITYoGOl77FJs5S81GrfjdSrfl7ygPdTXAvSqhxDdKIT 0RQ3ELGqd5+dvKuJpqA0TSm3sw+tFUf6FdGV7zmFNO1Bp+xPiHXalBvNygwlDhZEmljKsP9CxFo sqSv/2+ELR7RAzCdv4IgN0SDNB2rMC1Y94E9J70fJdqw6ePs/8MLHyXwH/jQXq2b9N0TaYl6h0R xyasdBmV6w0jDtljdIwkHFvodkxD13mnFOjbCA== X-Received: by 2002:a05:600c:470a:b0:4ac:b0b2:fa12 with SMTP id 5b1f17b1804b1-4bb559f5d44mr62716035e9.1.1794588090158; Fri, 13 Nov 2026 08:41:30 -0800 (PST) Received: from localhost.localdomain ([2.57.72.67]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-49c5d03b10fsm7059356f8f.1.2026.11.13.08.41.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Nov 2026 08:41:29 -0800 (PST) From: =?UTF-8?q?K=C3=A9vin=20Le=20Gouguec?= To: gdb-patches@sourceware.org Cc: =?UTF-8?q?K=C3=A9vin=20Le=20Gouguec?= , Simon Marchi Subject: [PATCH v4 2/2] gdb: Fix CPU ID offset for Ravenscar Threads Date: Fri, 13 Nov 2026 17:40:39 +0100 Message-ID: <4c600a461fb842813765398fe8f8bbcd1bd97abb.1794585020.git.legouguec@adacore.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 In a couple of spots, gdb/ravenscar-thread.c failed to correctly apply the CPU offset recorded in the Ada runtime, attempting to read the runtime's Running_Thread_Table with wrong indices, e.g. * using target CPU IDs directly (i.e. LWPs from remote PTIDs) without applying the offset, * applying the offset to IDs that were already in the right range. This led to GDB reading garbage instead of 'Thread_Id' values, then e.g. 'info tasks' (resp. 'info threads') failing to identify the current Ada task (resp. "Ravenscar Thread"). Observed on AdaCore's runtime for Microchip PolarFire, where the offset is defined as: package System.BB.Board_Parameters is -- […] GDB_First_CPU_Id : constant Interfaces.Unsigned_32 := 1; pragma Export (C, GDB_First_CPU_Id, "__gnat_gdb_cpu_first_id"); … because while the runtime counts the board's "application" CPUs from 1 to 4, QEMU counts them from 2 to 5, using 1 for the "monitor" CPU. In addition to fixing the bugs, this patch introduces a wrapper class to hopefully reduce the risk of ID confusion: 'cpu_id' provides (1) from_* "named constructors" (2) as_* accessors, making intent easier to understand if nothing else. --- gdb/ada-tasks.c | 13 ++++ gdb/ada-tasks.h | 5 ++ gdb/ravenscar-thread.c | 154 ++++++++++++++++++++++++++++++----------- 3 files changed, 131 insertions(+), 41 deletions(-) diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c index 503563de42a..50d424a64f6 100644 --- a/gdb/ada-tasks.c +++ b/gdb/ada-tasks.c @@ -336,6 +336,19 @@ ada_get_task_number (thread_info *thread) return 0; /* No matching task found. */ } +/* See ada-tasks.h. */ + +unsigned int +ada_get_cpu_first_id (struct program_space *pspace) +{ + const struct ada_tasks_pspace_data *pspace_data + = get_ada_tasks_pspace_data (pspace); + + gdb_assert (pspace_data->initialized_p); + + return pspace_data->cpu_id_offset; +} + /* Return the task number of the task running in inferior INF which matches TASK_ID , or zero if the task could not be found. */ diff --git a/gdb/ada-tasks.h b/gdb/ada-tasks.h index 66870ac323f..ea74a95fd35 100644 --- a/gdb/ada-tasks.h +++ b/gdb/ada-tasks.h @@ -24,6 +24,11 @@ extern bool valid_task_id (int task_num); +/* Return the offset between remote stub CPUs and ATCB Base_CPU + values; zero by default. */ + +extern unsigned int ada_get_cpu_first_id (struct program_space *pspace); + /* Search through the list of known tasks for the one whose ptid is PTID, and return it. Return NULL if the task was not found. */ diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c index bb3d7f9e847..e6aa6d14df1 100644 --- a/gdb/ravenscar-thread.c +++ b/gdb/ravenscar-thread.c @@ -76,10 +76,75 @@ static const target_info ravenscar_target_info = { N_("Ravenscar tasks support.") }; +/* Wrapper for CPU indices, with explicit constructors & accessors to prevent + confusion between: + + * CPU IDs coming from the Ada runtime: + * Base_CPU attribute from the ATCB. + * Used to offset System.BB tables. + + * CPU IDs coming from the remote protocol: + * CPU is the PTID's LWP. + * Expected to match ATCB CPUs, shifted by an optional offset exported + by the runtime, i.e. System.BB.Board_Parameters.GDB_First_CPU_Id. + + Relevant runtime excerpts: + + System.Multiprocessors: + type CPU_Range is range 0 .. System.BB.Parameters.Max_Number_Of_CPUs; + subtype CPU is CPU_Range range 1 .. CPU_Range'Last; + + System.BB.Threads: + type Thread_Descriptor is record + Base_CPU : System.Multiprocessors.CPU_Range; + + System.BB.Threads.Queues: + Running_Thread_Table : array (System.Multiprocessors.CPU) of Thread_Id; + + System.BB.CPU_Primitives: + type FPU_Context_Table is + array (System.Multiprocessors.CPU) of FPU_Context_Access; + + Note that while Base_CPU is allegedly a CPU_Range, with zero meaning + Not_A_Specific_CPU, in practice the tasking kernel sets ATCB Base_CPUs to + non-zero values. */ +class cpu_id +{ +public: + /* Constructor from a runtime array index, ranging from 1 to + Max_Number_Of_CPUs. */ + static cpu_id from_runtime_index (unsigned int array_index, + unsigned int cpu_offset) + { return cpu_id (array_index, cpu_offset); } + + /* Constructor from a thread ID sent by the remote stub, i.e. a CPU. */ + static cpu_id from_target_thread (unsigned int thread_id, + unsigned int cpu_offset) + { return cpu_id (thread_id - cpu_offset, cpu_offset); } + + /* Return an index for arrays indexed by System.Multiprocessors.CPU. */ + unsigned int as_runtime_index () const + { return m_runtime_id; } + + /* Return the remote thread ID for this CPU. */ + unsigned int as_target_cpu_id () const + { return m_runtime_id + m_cpu_offset; } + + /* Parameter-less constructor; required for unordered_map insertion. */ + cpu_id () = default; +private: + cpu_id (unsigned int runtime_id, unsigned int cpu_offset) + : m_runtime_id (runtime_id), m_cpu_offset (cpu_offset) + {} + unsigned int m_runtime_id; + unsigned int m_cpu_offset; +}; + struct ravenscar_thread_target final : public target_ops { - ravenscar_thread_target () - : m_base_ptid (inferior_ptid) + ravenscar_thread_target (unsigned int cpu_offset) + : m_base_ptid (inferior_ptid), + m_cpu_offset (cpu_offset) { } @@ -149,10 +214,18 @@ struct ravenscar_thread_target final : public target_ops the event, to make it the current task. */ ptid_t m_base_ptid; - ptid_t active_task (int cpu); + /* Offset between target CPUs (from remote stub, stored in PTIDs) and runtime + CPUs (from ATCB, typed as System.Multiprocessors.CPU_Range). + + Read from the runtime as System.BB.Board_Parameters.GDB_First_CPU_Id, aka + __gnat_gdb_cpu_first_id. */ + unsigned int m_cpu_offset; + + ptid_t active_task (cpu_id cpu); bool task_is_currently_active (ptid_t ptid); bool runtime_initialized (); - int get_thread_base_cpu (ptid_t ptid); + cpu_id get_task_cpu (struct ada_task_info *task); + cpu_id get_thread_cpu (ptid_t ptid); ptid_t get_base_thread_from_ravenscar_task (ptid_t ptid); void add_thread (struct ada_task_info *task); @@ -194,8 +267,9 @@ struct ravenscar_thread_target final : public target_ops /* This maps a TID to the CPU on which it was running. This is needed because sometimes the runtime will report an active task that hasn't yet been put on the list of tasks that is read by - ada-tasks.c. */ - gdb::unordered_map m_cpu_map; + ada-tasks.c. + */ + gdb::unordered_map m_cpu_map; }; /* Return true iff PTID corresponds to a ravenscar task. */ @@ -213,17 +287,24 @@ is_ravenscar_task (ptid_t ptid) return ptid.lwp () == 0 && ptid.tid () != 0; } +/* Return a cpu_id for TASK->base_cpu. */ + +cpu_id +ravenscar_thread_target::get_task_cpu (struct ada_task_info *task) +{ + /* read_atcb applies the offset, so expect a target CPU ID. */ + return cpu_id::from_target_thread (task->base_cpu, m_cpu_offset); +} + /* Given PTID, which can be either a ravenscar task or a CPU thread, return which CPU that ptid is running on. This assume that PTID is a valid ptid_t. Otherwise, a gdb_assert will be triggered. */ -int -ravenscar_thread_target::get_thread_base_cpu (ptid_t ptid) +cpu_id +ravenscar_thread_target::get_thread_cpu (ptid_t ptid) { - int base_cpu; - if (is_ravenscar_task (ptid)) { /* Prefer to not read inferior memory if possible, to avoid @@ -231,22 +312,15 @@ ravenscar_thread_target::get_thread_base_cpu (ptid_t ptid) auto iter = m_cpu_map.find (ptid.tid ()); if (iter != m_cpu_map.end ()) - base_cpu = iter->second; - else - { - struct ada_task_info *task_info = ada_get_task_info_from_ptid (ptid); + return iter->second; - gdb_assert (task_info != NULL); - base_cpu = task_info->base_cpu; - } - } - else - { - /* We assume that the LWP of the PTID is equal to the CPU number. */ - base_cpu = ptid.lwp (); + struct ada_task_info *task_info = ada_get_task_info_from_ptid (ptid); + gdb_assert (task_info != NULL); + return get_task_cpu (task_info); } - return base_cpu; + /* We assume that the LWP of the PTID is equal to the CPU number. */ + return cpu_id::from_target_thread (ptid.lwp (), m_cpu_offset); } /* Given a ravenscar task (identified by its ptid_t PTID), return true @@ -262,7 +336,7 @@ ravenscar_thread_target::get_thread_base_cpu (ptid_t ptid) bool ravenscar_thread_target::task_is_currently_active (ptid_t ptid) { - ptid_t active_task_ptid = active_task (get_thread_base_cpu (ptid)); + ptid_t active_task_ptid = active_task (get_thread_cpu (ptid)); return ptid == active_task_ptid; } @@ -276,13 +350,11 @@ ravenscar_thread_target::task_is_currently_active (ptid_t ptid) ptid_t ravenscar_thread_target::get_base_thread_from_ravenscar_task (ptid_t ptid) { - int base_cpu; - if (!is_ravenscar_task (ptid)) return ptid; - base_cpu = get_thread_base_cpu (ptid); - return ptid_t (ptid.pid (), base_cpu); + cpu_id cpu = get_thread_cpu (ptid); + return ptid_t (ptid.pid (), cpu.as_target_cpu_id ()); } /* Fetch the ravenscar running thread from target memory, make sure @@ -295,10 +367,8 @@ ravenscar_thread_target::add_active_thread () process_stratum_target *proc_target = as_process_stratum_target (this->beneath ()); - int base_cpu; - gdb_assert (!is_ravenscar_task (m_base_ptid)); - base_cpu = get_thread_base_cpu (m_base_ptid); + cpu_id cpu = get_thread_cpu (m_base_ptid); if (!runtime_initialized ()) return nullptr; @@ -306,7 +376,7 @@ ravenscar_thread_target::add_active_thread () /* It's possible for runtime_initialized to return true but for it not to be fully initialized. For example, this can happen for a breakpoint placed at the task's beginning. */ - ptid_t active_ptid = active_task (base_cpu); + ptid_t active_ptid = active_task (cpu); if (active_ptid == null_ptid) return nullptr; @@ -317,7 +387,7 @@ ravenscar_thread_target::add_active_thread () if (active_thr == nullptr) { active_thr = ::add_thread (proc_target, active_ptid); - m_cpu_map[active_ptid.tid ()] = base_cpu; + m_cpu_map[active_ptid.tid ()] = cpu; } return active_thr; } @@ -368,14 +438,15 @@ has_ravenscar_runtime () bool ravenscar_thread_target::runtime_initialized () { - return active_task (1) != null_ptid; + cpu_id first_cpu = cpu_id::from_runtime_index (1, m_cpu_offset); + return active_task (first_cpu) != null_ptid; } /* Return the ID of the thread that is currently running. Return 0 if the ID could not be determined. */ static CORE_ADDR -get_running_thread_id (int cpu) +get_running_thread_id (cpu_id cpu) { bound_minimal_symbol object_msym = get_running_thread_msymbol (); int object_size; @@ -390,7 +461,7 @@ get_running_thread_id (int cpu) object_size = builtin_type_void_data_ptr->length (); object_addr = (object_msym.value_address () - + (cpu - 1) * object_size); + + (cpu.as_runtime_index () - 1) * object_size); buf_size = object_size; buf = (gdb_byte *) alloca (buf_size); read_memory (object_addr, buf, buf_size); @@ -455,7 +526,7 @@ ravenscar_thread_target::add_thread (struct ada_task_info *task) if (current_inferior ()->find_thread (task->ptid) == NULL) { ::add_thread (current_inferior ()->process_target (), task->ptid); - m_cpu_map[task->ptid.tid ()] = task->base_cpu; + m_cpu_map[task->ptid.tid ()] = get_task_cpu (task); } } @@ -480,7 +551,7 @@ ravenscar_thread_target::update_thread_list () } ptid_t -ravenscar_thread_target::active_task (int cpu) +ravenscar_thread_target::active_task (cpu_id cpu) { CORE_ADDR tid = get_running_thread_id (cpu); @@ -654,10 +725,10 @@ ravenscar_thread_target::get_fpu_state (struct regcache *regcache, ptr_type = lookup_pointer_type (ptr_type); value *val = value_from_pointer (ptr_type, fpu_context.value_address ()); - int cpu = get_thread_base_cpu (regcache->ptid ()); + cpu_id cpu = get_thread_cpu (regcache->ptid ()); /* The array index type has a lower bound of 1 -- it is Ada code -- so subtract 1 here. */ - val = value_ptradd (val, cpu - 1); + val = value_ptradd (val, cpu.as_runtime_index () - 1); val = value_ind (val); CORE_ADDR fpu_task = value_as_long (val); @@ -887,7 +958,8 @@ ravenscar_inferior_created (inferior *inf) return; } - ravenscar_thread_target *rtarget = new ravenscar_thread_target (); + ravenscar_thread_target *rtarget + = new ravenscar_thread_target (ada_get_cpu_first_id (inf->pspace)); inf->push_target (target_ops_up (rtarget)); thread_info *thr = rtarget->add_active_thread (); if (thr != nullptr) -- 2.43.0