From: "Kévin Le Gouguec" <legouguec@adacore.com>
To: gdb-patches@sourceware.org
Cc: "Kévin Le Gouguec" <legouguec@adacore.com>,
"Simon Marchi" <simark@simark.ca>
Subject: [PATCH v4 2/2] gdb: Fix CPU ID offset for Ravenscar Threads
Date: Fri, 13 Nov 2026 17:40:39 +0100 [thread overview]
Message-ID: <4c600a461fb842813765398fe8f8bbcd1bd97abb.1794585020.git.legouguec@adacore.com> (raw)
In-Reply-To: <cover.1794585020.git.legouguec@adacore.com>
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<ULONGEST, int> m_cpu_map;
+ ada-tasks.c.
+ */
+ gdb::unordered_map<ULONGEST, cpu_id> 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
prev parent reply other threads:[~2026-11-13 16:42 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-12 17:10 [PATCH] Fix Ada tasking for baremetal targets using Ravenscar threads Kévin Le Gouguec via Gdb-patches
2023-01-12 18:19 ` Simon Marchi via Gdb-patches
2023-01-13 12:32 ` Kévin Le Gouguec via Gdb-patches
2023-01-13 16:29 ` Simon Marchi via Gdb-patches
2023-01-13 17:16 ` Kévin Le Gouguec via Gdb-patches
2023-01-27 16:53 ` [PATCH v2 0/3] " Kévin Le Gouguec via Gdb-patches
2023-01-27 16:53 ` [PATCH v2 1/3] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c Kévin Le Gouguec via Gdb-patches
2023-01-27 16:53 ` [PATCH v2 2/3] gdb/ada-tasks: Make the ada_tasks_pspace_data getter public Kévin Le Gouguec via Gdb-patches
2023-01-27 16:53 ` [PATCH v2 3/3] gdb: Fix Ada tasking for baremetal targets using Ravenscar threads Kévin Le Gouguec via Gdb-patches
2026-11-06 16:51 ` [PATCH v3 0/2] " Kévin Le Gouguec
2026-11-06 16:51 ` [PATCH v3 1/2] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c Kévin Le Gouguec
2026-11-06 19:43 ` Simon Marchi
2026-11-09 9:14 ` Kévin Le Gouguec
2026-11-09 15:02 ` Simon Marchi
2026-11-09 17:07 ` Kévin Le Gouguec
2026-11-06 16:51 ` [PATCH v3 2/2] gdb: Fix CPU ID offset for Ravenscar Threads Kévin Le Gouguec
2026-11-13 16:40 ` [PATCH v4 0/2] Fix Ada tasking for baremetal targets using Ravenscar threads Kévin Le Gouguec
2026-11-13 16:40 ` [PATCH v4 1/2] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c Kévin Le Gouguec
2026-11-13 16:40 ` Kévin Le Gouguec [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4c600a461fb842813765398fe8f8bbcd1bd97abb.1794585020.git.legouguec@adacore.com \
--to=legouguec@adacore.com \
--cc=gdb-patches@sourceware.org \
--cc=simark@simark.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox