From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id IWnHJwYG7mrb/CgAWB0awg (envelope-from ) for ; Fri, 06 Nov 2026 11:53:26 -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=S+YKAGti; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 9EE51378002; Fri, 06 Nov 2026 11:53:26 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.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 autolearn=unavailable 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 74B351E023 for ; Fri, 06 Nov 2026 11:53:25 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D06F14BA2E21 for ; Fri, 6 Nov 2026 16:53:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D06F14BA2E21 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=S+YKAGti Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id E16534BA2E0F for ; Fri, 6 Nov 2026 16:52:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E16534BA2E0F 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 E16534BA2E0F Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::42b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793983973; cv=none; b=BVeh+ooXGiu/Lqwd8R9Zy1pgkZtCgomQMiSXlUK5KA05RaWWzHmHjV2qIJ53Mp0q1FWcCw3Hs5VALFs0ptPJzK2CAIUkRDZt+66iQLZuNSpCjFzGSUdtzTOPitjOSpebFGe+fEx3VaNW2sRfuAGOZ8pIosbczcOprTN+ez7suaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793983973; c=relaxed/simple; bh=PIHNrKFh8ooi2UbTY+LHqOg/lrcUnxRXu4MpNkvxnTs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Hd2hdZT1k5AjB/d8R4TeoNRL/RTntuNOMxjpEtc6gSjT91z81DZxPIK609J6Y+mi/tFKRAqL9HvoGGhMVH1hL3dkqdmCrqQbL8qCQWjMOHeOoMmmOi+n3gnpHaKgoXaag264gL8t9h53iMVpLk9QnRMZsuIIXC+LkORGD/uiGZ0= 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=S+YKAGti DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E16534BA2E0F Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-490f38e9688so712204f8f.0 for ; Fri, 06 Nov 2026 08:52:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1793983972; x=1794588772; 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=5/2GylwbMo/um9eQrMB/lwMlMOOlsNY8dEfAzqdeCXw=; b=S+YKAGtiGDDf9SK32qN+QGrkE1Rq+7B9PyYmbgtztRbNRsj0JsTLDwlVnAk6EmboFn w+YtcOHNHUrsOe0cAqyNPwRHrmp9i6xs/JiEZGPWM+7j4N1nofhWityfzQypJ0UvbWDR wkFQhRlmI3820J/fpIIp+9dQGvX9+yVX8sxUfG5p1WAhT28YP4x6NR3dQAzPuPN84EPF hUIRpMvJ5ectG6g0BEWEOP/7WD2qwX6DKL6Swf/CtDMWxMKgUzl2uD3+idDs07FMbmhp HQeFj2BO/eGfXAGFULhrsG5+kZImKnPcZ3qgyUSzSDD5ncmAUVYy1z6GCkRHPag6EWi+ FfNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793983972; x=1794588772; 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=5/2GylwbMo/um9eQrMB/lwMlMOOlsNY8dEfAzqdeCXw=; b=cKLde03WZU6fvCFZyoDwcyiKy2N5ZkgcJ7LZlg4LDlOz+Pxh/fg1wUr1jYnSquyzOt /MIYECFwLiA1YNy/1SzjEOnCnGLjC6dIpQu25wWYjA/A0h/dA8mSsy0sfLJnHOCf1kA6 /IaGK6lQ3xvjxW4ZSDF0su5H2vYoxhCSL0HqOnFzHZBFbFiKp+xvc3FU9t2rU3qClMXJ HmW2vaT/Dk/5En/iHmvWSMJ8tE41oM+rbGmPgOMZhTWvDdCw8KtqgcQYUpsyzNau/n/l Cj4yZZJDJeaFcQesUoOK9hreNElO46g2jcoWrvuYo7R0P6Um+2bB6tUnYb+uYYSwEkOG R0Rg== X-Gm-Message-State: APuU4Z+bd1oMVchh2AEyPaPZir4ZW2RDQH/W2nGv1sQqPscYZTOWtYch vpZldvb1uNiF3voUy9eFv2fA7cCth/rLHxFdQroprS9Dj7kHnWYKhY1de26FxGy3qm1tVhInV8q 6TjCS9w== X-Gm-Gg: AYBFou124GYPDq72PH9x3ikj83zCFMVqHYnjFn5z4F8Zjji+Hc8h9Zts2f/MuOasNGn glNmDmPWhLta8BXcn3f64MYY/gsO45fqQfVc+WoRvoT7HJnwk4pRFL9t5Q6z02b2/IHro2anDDa hWm3VLyIWs3DClE8b4fzupO0iE6n/K7dxGJc0SsbHw/8yDpsmkXl/LeAu8ktd47Wi4aidtZbtXq DUmya0e/PfScODvFZE/0XOSOrHohFLUjtoKYFF/3pPe0++MzhoNFmy21I/AT/YtDyHQUcC2YoaE x+V1CMh3B+4nI+dZxkg/TXhq4Ci6j8riQTmu1kIAFd6NlVrkEA3QYvgNWNzY/ocDIUWH/SIX4hc W3VOXLDz4clZBloR6hHgx5kNPn1JtNYtgw97gkuESxrnVm/NGai1eQqpQgJM/M4qwZIJ6h6x/AM ELcf3tsyalfmBfGk6bl5MG2IpmJWVCe+6oRMnUspRscut09XxHNcXco/2zR4uO+OKedBQyAUYO1 YL0VCHz4IeE7YciJc82ljbrg9WzE8ofTMyPm9S+U55g/ksF3+gbSz1KDPWO4bgSInesnS1SQRV/ 6I3Qc5oqs+LSVO2ad0jn9x/r0KwDw4yp3f3qAA== X-Received: by 2002:a5d:5e91:0:b0:48e:6669:fefb with SMTP id ffacd0b85a97d-4927997bcb0mr6520339f8f.22.1793983971732; Fri, 06 Nov 2026 08:52:51 -0800 (PST) Received: from localhost.localdomain ([2.57.72.67]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-492d7cc5e23sm4895956f8f.45.2026.11.06.08.52.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 08:52:51 -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 v3 2/2] gdb: Fix CPU ID offset for Ravenscar Threads Date: Fri, 6 Nov 2026 17:51:57 +0100 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: <20230127165337.1832937-1-legouguec@adacore.com> 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 a9fa798a5c2..f8ce3520aa9 100644 --- a/gdb/ada-tasks.h +++ b/gdb/ada-tasks.h @@ -30,6 +30,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