Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Fix Ada tasking for baremetal targets using Ravenscar threads
@ 2023-01-12 17:10 Kévin Le Gouguec via Gdb-patches
  2023-01-12 18:19 ` Simon Marchi via Gdb-patches
  0 siblings, 1 reply; 19+ messages in thread
From: Kévin Le Gouguec via Gdb-patches @ 2023-01-12 17:10 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kévin Le Gouguec

For some boards, the Ada tasking runtime defines a __gnat_gdb_cpu_first_id
symbol to let GDB map the ATCB's "Base_CPU" indices (ranging from 1 to
System.Multiprocessors.CPU) onto the CPU numbers that we read from the
remote stub and record in our ptids.

In other words, this symbol lets GDB translate "Ada CPUs" into "target
CPUs".  For example, for the Microchip PolarFire board, the runtime defines
this offset 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");

This is because on this board, CPU#1 is the "monitor" CPU; CPUs #2-5 are the
"application" CPUs that run user code.  So while the ATCB shows Base_CPUs
equal to 1, QEMU reports that our code is running on CPU #2.

We convert the ATCB's Base_CPUs into "target" CPUs before recording them in
struct ada_task_info.base_cpu; this is what we need in most of
ravenscar-thread.c, except in one specific spot: when reading the active
thread from __gnat_running_thread_table, which is defined as:

  package System.BB.Threads.Queues is
     -- [...]
     Running_Thread_Table : array (System.Multiprocessors.CPU) of Thread_Id

On baremetal targets, System.Multiprocessors.CPU is defined as:

  package System.Multiprocessors is
     -- [...]
     type CPU_Range is range 0 .. System.BB.Parameters.Max_Number_Of_CPUs;

     subtype CPU is CPU_Range range 1 .. CPU_Range'Last;

     Not_A_Specific_CPU : constant CPU_Range := 0;

Thus __gnat_running_thread_table has Max_Number_Of_CPUs elements; for the
Microchip PolarFire board, the runtime define this as:

  package System.BB.Parameters is
     -- [...]
     Max_Number_Of_CPUs : constant := 1;

So the table has just one element, but ravenscar-thread.c attempts to index
it using the "target" CPU ID, i.e. 2.

This remained undetected because as luck would have it, with the specific
compiler we were using, *(__gnat_running_thread_table+8) happened to contain
exactly the same content as *__gnat_running_thread_table.  After bumping the
compiler, the layout of the tasking runtime changed, and so did the content
of *(__gnat_running_thread_table+8).

This commit introduces a new function to let GDB convert a "target" CPU back
to an "Ada" CPU.

Tested on x86_64-linux and riscv64-elf with AdaCore's internal testsuite,
which has more extensive coverage of Ada tasking and "Ravenscar thread"
features.
---
 gdb/ada-lang.h         |  8 +++++++-
 gdb/ada-tasks.c        | 14 ++++++++++++++
 gdb/ravenscar-thread.c |  8 +++++++-
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
index 9fb7ac7f384..bca0deea67f 100644
--- a/gdb/ada-lang.h
+++ b/gdb/ada-lang.h
@@ -145,7 +145,11 @@ struct ada_task_info
   /* The CPU on which the task is running.  This is dependent on
      the runtime actually providing that info, which is not always
      the case.  Normally, we should be able to count on it on
-     bare-metal targets.  */
+     bare-metal targets.
+
+     NB: This CPU number has been normalized to match the IDs reported by the
+     target, as recorded in the LWP field of PTIDs.  It may not map directly to
+     the Base_CPU recorded in the ATCB; see ada_get_runtime_cpu_index.  */
   int base_cpu;
 };
 
@@ -374,6 +378,8 @@ extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
 
 extern int ada_get_task_number (thread_info *thread);
 
+extern int ada_get_runtime_cpu_index (int target_cpu);
+
 typedef gdb::function_view<void (struct ada_task_info *task)>
   ada_task_list_iterator_ftype;
 extern void iterate_over_live_ada_tasks
diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index a3a28063daa..1e5605f9b62 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -346,6 +346,20 @@ ada_get_task_number (thread_info *thread)
   return 0;  /* No matching task found.  */
 }
 
+/* Translate a "target" CPU index into a "runtime" index suitable for addressing
+   arrays dimensioned with System.Multiprocessors.CPU.  */
+
+int
+ada_get_runtime_cpu_index (int target_cpu)
+{
+  const struct ada_tasks_pspace_data *pspace_data
+    = get_ada_tasks_pspace_data (current_program_space);
+
+  gdb_assert (pspace_data->initialized_p);
+
+  return target_cpu - 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/ravenscar-thread.c b/gdb/ravenscar-thread.c
index 22fbdbe9662..52908eb2bb5 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -193,7 +193,11 @@ 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.  */
+     ada-tasks.c.
+
+     NB: These CPU numbers correspond to those reported by the target,
+     which may differ from the numbers recorded in the ATCB.  See
+     ada_get_runtime_cpu_index.  */
   std::unordered_map<ULONGEST, int> m_cpu_map;
 };
 
@@ -377,6 +381,8 @@ ravenscar_thread_target::runtime_initialized ()
 static CORE_ADDR
 get_running_thread_id (int cpu)
 {
+  cpu = ada_get_runtime_cpu_index (cpu);
+
   struct bound_minimal_symbol object_msym = get_running_thread_msymbol ();
   int object_size;
   int buf_size;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] Fix Ada tasking for baremetal targets using Ravenscar threads
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Simon Marchi via Gdb-patches @ 2023-01-12 18:19 UTC (permalink / raw)
  To: Kévin Le Gouguec, gdb-patches



On 1/12/23 12:10, Kévin Le Gouguec via Gdb-patches wrote:
> For some boards, the Ada tasking runtime defines a __gnat_gdb_cpu_first_id
> symbol to let GDB map the ATCB's "Base_CPU" indices (ranging from 1 to
> System.Multiprocessors.CPU) onto the CPU numbers that we read from the
> remote stub and record in our ptids.
> 
> In other words, this symbol lets GDB translate "Ada CPUs" into "target
> CPUs".  For example, for the Microchip PolarFire board, the runtime defines
> this offset 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");
> 
> This is because on this board, CPU#1 is the "monitor" CPU; CPUs #2-5 are the
> "application" CPUs that run user code.  So while the ATCB shows Base_CPUs
> equal to 1, QEMU reports that our code is running on CPU #2.
> 
> We convert the ATCB's Base_CPUs into "target" CPUs before recording them in
> struct ada_task_info.base_cpu; this is what we need in most of
> ravenscar-thread.c, except in one specific spot: when reading the active
> thread from __gnat_running_thread_table, which is defined as:
> 
>   package System.BB.Threads.Queues is
>      -- [...]
>      Running_Thread_Table : array (System.Multiprocessors.CPU) of Thread_Id
> 
> On baremetal targets, System.Multiprocessors.CPU is defined as:
> 
>   package System.Multiprocessors is
>      -- [...]
>      type CPU_Range is range 0 .. System.BB.Parameters.Max_Number_Of_CPUs;
> 
>      subtype CPU is CPU_Range range 1 .. CPU_Range'Last;
> 
>      Not_A_Specific_CPU : constant CPU_Range := 0;
> 
> Thus __gnat_running_thread_table has Max_Number_Of_CPUs elements; for the
> Microchip PolarFire board, the runtime define this as:
> 
>   package System.BB.Parameters is
>      -- [...]
>      Max_Number_Of_CPUs : constant := 1;
> 
> So the table has just one element, but ravenscar-thread.c attempts to index
> it using the "target" CPU ID, i.e. 2.
> 
> This remained undetected because as luck would have it, with the specific
> compiler we were using, *(__gnat_running_thread_table+8) happened to contain
> exactly the same content as *__gnat_running_thread_table.  After bumping the
> compiler, the layout of the tasking runtime changed, and so did the content
> of *(__gnat_running_thread_table+8).
> 
> This commit introduces a new function to let GDB convert a "target" CPU back
> to an "Ada" CPU.
> 
> Tested on x86_64-linux and riscv64-elf with AdaCore's internal testsuite,
> which has more extensive coverage of Ada tasking and "Ravenscar thread"
> features.

Hi Kevin,

I can't speak about the Ada-specific bits, but at least the commit
message makes it like you know what you're doing, so that's convincing
:).

> ---
>  gdb/ada-lang.h         |  8 +++++++-
>  gdb/ada-tasks.c        | 14 ++++++++++++++
>  gdb/ravenscar-thread.c |  8 +++++++-
>  3 files changed, 28 insertions(+), 2 deletions(-)
> 
> diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
> index 9fb7ac7f384..bca0deea67f 100644
> --- a/gdb/ada-lang.h
> +++ b/gdb/ada-lang.h
> @@ -145,7 +145,11 @@ struct ada_task_info
>    /* The CPU on which the task is running.  This is dependent on
>       the runtime actually providing that info, which is not always
>       the case.  Normally, we should be able to count on it on
> -     bare-metal targets.  */
> +     bare-metal targets.
> +
> +     NB: This CPU number has been normalized to match the IDs reported by the
> +     target, as recorded in the LWP field of PTIDs.  It may not map directly to
> +     the Base_CPU recorded in the ATCB; see ada_get_runtime_cpu_index.  */
>    int base_cpu;
>  };
>  
> @@ -374,6 +378,8 @@ extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
>  
>  extern int ada_get_task_number (thread_info *thread);
>  
> +extern int ada_get_runtime_cpu_index (int target_cpu);

To conform with the current standards, please move the function doc
here.

> +
>  typedef gdb::function_view<void (struct ada_task_info *task)>
>    ada_task_list_iterator_ftype;
>  extern void iterate_over_live_ada_tasks
> diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
> index a3a28063daa..1e5605f9b62 100644
> --- a/gdb/ada-tasks.c
> +++ b/gdb/ada-tasks.c
> @@ -346,6 +346,20 @@ ada_get_task_number (thread_info *thread)
>    return 0;  /* No matching task found.  */
>  }
>  
> +/* Translate a "target" CPU index into a "runtime" index suitable for addressing
> +   arrays dimensioned with System.Multiprocessors.CPU.  */


And here, put:

/* See ada-lang.h.  */

(Ideally there would be an ada-tasks.h to match ada-tasks.c.)

> +
> +int
> +ada_get_runtime_cpu_index (int target_cpu)
> +{
> +  const struct ada_tasks_pspace_data *pspace_data
> +    = get_ada_tasks_pspace_data (current_program_space);

I would prefer if you added either a `program_space *` or
`ada_tasks_pspace_data *` to this function, and pushed the reference to
the global state to the caller.  My life goal is to reduce the number of
places that reference the global state in GDB.

Or, maybe, make this a method of ada_tasks_pspace_data?  The caller
would then to:

  const ada_tasks_pspace_data *pspace_data
    = get_ada_tasks_pspace_data (current_program_space);
  cpu = pspace_data->get_runtime_cpu_index (cpu);

Simon

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] Fix Ada tasking for baremetal targets using Ravenscar threads
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Kévin Le Gouguec via Gdb-patches @ 2023-01-13 12:32 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Hey Simon, thanks for the review!

Simon Marchi <simark@simark.ca> writes:

> I can't speak about the Ada-specific bits, but at least the commit
> message makes it like you know what you're doing, so that's convincing
> :).

(I've slowly come to believe I know what I am doing, but if not,
hopefully this commit message will be enough to trigger Cunningham's
Law)

>> diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
>> index 9fb7ac7f384..bca0deea67f 100644
>> --- a/gdb/ada-lang.h
>> +++ b/gdb/ada-lang.h
>> @@ -145,7 +145,11 @@ struct ada_task_info
>>    /* The CPU on which the task is running.  This is dependent on
>>       the runtime actually providing that info, which is not always
>>       the case.  Normally, we should be able to count on it on
>> -     bare-metal targets.  */
>> +     bare-metal targets.
>> +
>> +     NB: This CPU number has been normalized to match the IDs reported by the
>> +     target, as recorded in the LWP field of PTIDs.  It may not map directly to
>> +     the Base_CPU recorded in the ATCB; see ada_get_runtime_cpu_index.  */
>>    int base_cpu;
>>  };
>>  
>> @@ -374,6 +378,8 @@ extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
>>  
>>  extern int ada_get_task_number (thread_info *thread);
>>  
>> +extern int ada_get_runtime_cpu_index (int target_cpu);
>
> To conform with the current standards, please move the function doc
> here.

ACK, traced back to

https://sourceware.org/gdb/wiki/Internals GDB-C-Coding-Standards#Document_Every_Subprogram

Will give this a re-read before submitting v2.

>> +
>>  typedef gdb::function_view<void (struct ada_task_info *task)>
>>    ada_task_list_iterator_ftype;
>>  extern void iterate_over_live_ada_tasks
>> diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
>> index a3a28063daa..1e5605f9b62 100644
>> --- a/gdb/ada-tasks.c
>> +++ b/gdb/ada-tasks.c
>> @@ -346,6 +346,20 @@ ada_get_task_number (thread_info *thread)
>>    return 0;  /* No matching task found.  */
>>  }
>>  
>> +/* Translate a "target" CPU index into a "runtime" index suitable for addressing
>> +   arrays dimensioned with System.Multiprocessors.CPU.  */
>
>
> And here, put:
>
> /* See ada-lang.h.  */
>
> (Ideally there would be an ada-tasks.h to match ada-tasks.c.)

Agreed, might explore that in a preliminary commit for v2.

>> +
>> +int
>> +ada_get_runtime_cpu_index (int target_cpu)
>> +{
>> +  const struct ada_tasks_pspace_data *pspace_data
>> +    = get_ada_tasks_pspace_data (current_program_space);
>
> I would prefer if you added either a `program_space *` or
> `ada_tasks_pspace_data *` to this function, and pushed the reference to
> the global state to the caller.  My life goal is to reduce the number of
> places that reference the global state in GDB.
>
> Or, maybe, make this a method of ada_tasks_pspace_data?  The caller
> would then to:
>
>   const ada_tasks_pspace_data *pspace_data
>     = get_ada_tasks_pspace_data (current_program_space);
>   cpu = pspace_data->get_runtime_cpu_index (cpu);

I like the idea, although right now ada_tasks_pspace_data is completely
internal to ada-tasks.c.  ravenscar-thread.c only has access to
ada_task_info instances; we could stick the CPU offset in there, but
conceptually it's not really a property of the task.

AFAICT this patch follows the current pattern of ravenscar-thread.c
letting ada-tasks.c access globals: ada-tasks.c currently has 18
accesses to current_\(program_space\|inferior\), vs 3 calls to
current_inferior in ravenscar-thread.c, which does not even include
progspace.h.

So by my reckoning:

(a) this v1 is no worse than the status quo in terms of accessing
globals,

(b) naively pushing current_program_space to the caller would mean
making ravenscar-thread.c aware of current_program_space, so a net loss
according to the "reduce the number of references to global state in
GDB" metric,

(c) publishing struct ada_tasks_pspace_data and
get_ada_tasks_pspace_data wouldn't be much better (ravenscar-thread.c
would still need to learn about current_program_space), but it does feel
like a step in the right direction?  I.e. it would then "just" be a
matter of…

(d) … somehow adding a struct ada_tasks_pspace_data member to
ravenscar_thread_target.  That would allow
ravenscar_thread_target::active_task to call
m_pspace_data->get_runtime_cpu_index (cpu).

So, with utmost consideration for your life goal, it seems to me that
the solutions, ranked by decreasing order of preference, would be

  (c)+(d) > (a) > (b)

Does that make sense?  If so, I'd like to advocate for (a) (+ the doc
fixes you noted), and write "figure out how to pass an
ada_tasks_pspace_data instance to ravenscar_thread_target" on a post-it
and stick it to my monitor, in order to unblock (d).


Let me know if you'd rather I go with (b), or if I've misread the
situation.  Again, thanks for the review!

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] Fix Ada tasking for baremetal targets using Ravenscar threads
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Simon Marchi via Gdb-patches @ 2023-01-13 16:29 UTC (permalink / raw)
  To: Kévin Le Gouguec; +Cc: gdb-patches



On 1/13/23 07:32, Kévin Le Gouguec wrote:
> Hey Simon, thanks for the review!
> 
> Simon Marchi <simark@simark.ca> writes:
> 
>> I can't speak about the Ada-specific bits, but at least the commit
>> message makes it like you know what you're doing, so that's convincing
>> :).
> 
> (I've slowly come to believe I know what I am doing, but if not,
> hopefully this commit message will be enough to trigger Cunningham's
> Law)
> 
>>> diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
>>> index 9fb7ac7f384..bca0deea67f 100644
>>> --- a/gdb/ada-lang.h
>>> +++ b/gdb/ada-lang.h
>>> @@ -145,7 +145,11 @@ struct ada_task_info
>>>    /* The CPU on which the task is running.  This is dependent on
>>>       the runtime actually providing that info, which is not always
>>>       the case.  Normally, we should be able to count on it on
>>> -     bare-metal targets.  */
>>> +     bare-metal targets.
>>> +
>>> +     NB: This CPU number has been normalized to match the IDs reported by the
>>> +     target, as recorded in the LWP field of PTIDs.  It may not map directly to
>>> +     the Base_CPU recorded in the ATCB; see ada_get_runtime_cpu_index.  */
>>>    int base_cpu;
>>>  };
>>>  
>>> @@ -374,6 +378,8 @@ extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
>>>  
>>>  extern int ada_get_task_number (thread_info *thread);
>>>  
>>> +extern int ada_get_runtime_cpu_index (int target_cpu);
>>
>> To conform with the current standards, please move the function doc
>> here.
> 
> ACK, traced back to
> 
> https://sourceware.org/gdb/wiki/Internals GDB-C-Coding-Standards#Document_Every_Subprogram
> 
> Will give this a re-read before submitting v2.
> 
>>> +
>>>  typedef gdb::function_view<void (struct ada_task_info *task)>
>>>    ada_task_list_iterator_ftype;
>>>  extern void iterate_over_live_ada_tasks
>>> diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
>>> index a3a28063daa..1e5605f9b62 100644
>>> --- a/gdb/ada-tasks.c
>>> +++ b/gdb/ada-tasks.c
>>> @@ -346,6 +346,20 @@ ada_get_task_number (thread_info *thread)
>>>    return 0;  /* No matching task found.  */
>>>  }
>>>  
>>> +/* Translate a "target" CPU index into a "runtime" index suitable for addressing
>>> +   arrays dimensioned with System.Multiprocessors.CPU.  */
>>
>>
>> And here, put:
>>
>> /* See ada-lang.h.  */
>>
>> (Ideally there would be an ada-tasks.h to match ada-tasks.c.)
> 
> Agreed, might explore that in a preliminary commit for v2.
> 
>>> +
>>> +int
>>> +ada_get_runtime_cpu_index (int target_cpu)
>>> +{
>>> +  const struct ada_tasks_pspace_data *pspace_data
>>> +    = get_ada_tasks_pspace_data (current_program_space);
>>
>> I would prefer if you added either a `program_space *` or
>> `ada_tasks_pspace_data *` to this function, and pushed the reference to
>> the global state to the caller.  My life goal is to reduce the number of
>> places that reference the global state in GDB.
>>
>> Or, maybe, make this a method of ada_tasks_pspace_data?  The caller
>> would then to:
>>
>>   const ada_tasks_pspace_data *pspace_data
>>     = get_ada_tasks_pspace_data (current_program_space);
>>   cpu = pspace_data->get_runtime_cpu_index (cpu);
> 
> I like the idea, although right now ada_tasks_pspace_data is completely
> internal to ada-tasks.c.  ravenscar-thread.c only has access to
> ada_task_info instances; we could stick the CPU offset in there, but
> conceptually it's not really a property of the task.

Indeed.

> AFAICT this patch follows the current pattern of ravenscar-thread.c
> letting ada-tasks.c access globals: ada-tasks.c currently has 18
> accesses to current_\(program_space\|inferior\), vs 3 calls to
> current_inferior in ravenscar-thread.c, which does not even include
> progspace.h.
> 
> So by my reckoning:
> 
> (a) this v1 is no worse than the status quo in terms of accessing
> globals,

That's true.

> (b) naively pushing current_program_space to the caller would mean
> making ravenscar-thread.c aware of current_program_space, so a net loss
> according to the "reduce the number of references to global state in
> GDB" metric,

ravenscar-thread.c is already implicitly aware of the current program
space and global state in general, since it calls functions that are
dependent on the global state.  And as you add a new function dependent
on the global state, that's one more function dependent on the global
state in GDB, so I would consider that a "net loss".

When possible, I try to change leaf functions to make them "pure",
receiving the context as parameters rather than using the global
context.  And slowly progress up the call tree like this, making
incremental progress.  It's true that the absolute number of literal
"current_program_space" reference can increase.  Like if I change a
function to accept a program_space parameter, rather than using
"current_program_space" directly, and inline "current_program_space" in
all its callers.  But that's a necessary step towards the end goal.

> 
> (c) publishing struct ada_tasks_pspace_data and
> get_ada_tasks_pspace_data wouldn't be much better (ravenscar-thread.c
> would still need to learn about current_program_space), but it does feel
> like a step in the right direction?  I.e. it would then "just" be a
> matter of…

Whether we have a ada_get_runtime_cpu_index free function or we expose
ada_tasks_pspace_data and add a get_runtime_cpu_index method does not
change anything w.r.t. references to global state.

> 
> (d) … somehow adding a struct ada_tasks_pspace_data member to
> ravenscar_thread_target.  That would allow
> ravenscar_thread_target::active_task to call
> m_pspace_data->get_runtime_cpu_index (cpu).

I don't think that makes sense, we don't want to create intances of
ada_tasks_pspace_data not managed by ada_tasks_pspace_data_handle.

> 
> So, with utmost consideration for your life goal, it seems to me that
> the solutions, ranked by decreasing order of preference, would be
> 
>   (c)+(d) > (a) > (b)

I disagree, I don't think (d) is right.

> Does that make sense?  If so, I'd like to advocate for (a) (+ the doc
> fixes you noted), and write "figure out how to pass an
> ada_tasks_pspace_data instance to ravenscar_thread_target" on a post-it
> and stick it to my monitor, in order to unblock (d).

I'm fine with doing (a) if you prefer, it doesn't change much in the
grand scheme of things.  However, I might just notice that function in
the future being a good candidate for becoming "pure", and change it to
push the global state reference up the call tree :).

Simon

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] Fix Ada tasking for baremetal targets using Ravenscar threads
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Kévin Le Gouguec via Gdb-patches @ 2023-01-13 17:16 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Simon Marchi <simark@simark.ca> writes:

>> (b) naively pushing current_program_space to the caller would mean
>> making ravenscar-thread.c aware of current_program_space, so a net loss
>> according to the "reduce the number of references to global state in
>> GDB" metric,
>
> ravenscar-thread.c is already implicitly aware of the current program
> space and global state in general, since it calls functions that are
> dependent on the global state.  And as you add a new function dependent
> on the global state, that's one more function dependent on the global
> state in GDB, so I would consider that a "net loss".
>
> When possible, I try to change leaf functions to make them "pure",
> receiving the context as parameters rather than using the global
> context.  And slowly progress up the call tree like this, making
> incremental progress.  It's true that the absolute number of literal
> "current_program_space" reference can increase.  Like if I change a
> function to accept a program_space parameter, rather than using
> "current_program_space" directly, and inline "current_program_space" in
> all its callers.  But that's a necessary step towards the end goal.

OK, I see the value of endeavoring to keep newly introduced leaf
functions pure, and "revealing" the implicit dependencies by pushing
references to global state up to the callers.

>> (d) … somehow adding a struct ada_tasks_pspace_data member to
>> ravenscar_thread_target.  That would allow
>> ravenscar_thread_target::active_task to call
>> m_pspace_data->get_runtime_cpu_index (cpu).
>
> I don't think that makes sense, we don't want to create intances of
> ada_tasks_pspace_data not managed by ada_tasks_pspace_data_handle.

ACK; naively I assumed that ravenscar_thread_target holding a reference
to an ada_tasks_pspace_data would be fine as long as the latter outlived
the former, but I had no reason to believe that this condition holds
when I wrote that.

>> Does that make sense?  If so, I'd like to advocate for (a) (+ the doc
>> fixes you noted), and write "figure out how to pass an
>> ada_tasks_pspace_data instance to ravenscar_thread_target" on a post-it
>> and stick it to my monitor, in order to unblock (d).
>
> I'm fine with doing (a) if you prefer, it doesn't change much in the
> grand scheme of things.  However, I might just notice that function in
> the future being a good candidate for becoming "pure", and change it to
> push the global state reference up the call tree :).

I'm game for keeping this new function (ada_get_runtime_cpu_index) pure
from the get go.

So my current plan for v2 (taking the other remarks into account) is
thus to…

1. introduce ada-tasks.h and move stuff defined in ada-tasks.c there,
2. make ada_tasks_pspace_data and its getter public,
3. introduce a get_runtime_cpu_index method,
4. rebase v1 on top of that,

… on the hunch that exposing ada_tasks_pspace_data will encourage giving
it pure(r) methods and keeping references to global state confined to
its callers.

Not sure what I'm basing this hunch on though, so let me know if this
sounds like overkill, and I should just leave it at:

diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index a9c45ab6b9c..e9a52937966 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -350,10 +350,10 @@ ada_get_task_number (thread_info *thread)
    arrays dimensioned with System.Multiprocessors.CPU.  */
 
 int
-ada_get_runtime_cpu_index (int target_cpu)
+ada_get_runtime_cpu_index (struct program_space *pspace, int target_cpu)
 {
   const struct ada_tasks_pspace_data *pspace_data
-    = get_ada_tasks_pspace_data (current_program_space);
+    = get_ada_tasks_pspace_data (pspace);
 
   gdb_assert (pspace_data->initialized_p);
 

Thanks for your patience.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v2 0/3] Fix Ada tasking for baremetal targets using Ravenscar threads
  2023-01-13 17:16       ` Kévin Le Gouguec via Gdb-patches
@ 2023-01-27 16:53         ` 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
                             ` (3 more replies)
  0 siblings, 4 replies; 19+ messages in thread
From: Kévin Le Gouguec via Gdb-patches @ 2023-01-27 16:53 UTC (permalink / raw)
  To: gdb-patches; +Cc: simark, Kévin Le Gouguec

Changes since v1:

* New patch to add a header for functions defined in ada-tasks.c.
* New patch to expose a getter for ada_tasks_pspace_data.
* Tweak original patch to use that getter from ravenscar-thread.c.

This ensures we do not introduce a function that refers to global
state; instead, we let ada_get_runtime_cpu_index's caller get its
hands "dirty".


I considered exposing ada_tasks_pspace_data entirely and making
ada_get_runtime_cpu_index a method as discussed, but AFAIU this would
have required either

  (a) keeping all members of that struct public so that existing
  functions can keep accessing them,

  (b) ponder which members ought to remain public, which should not,
  and which portions of ada-tasks.c should be turned into methods for
  ada_tasks_pspace_data.

(a) does not sound like progress in terms of encapsulation; I am not
confident I can get (b) done soon.  Hence the opaque pointer as a
first step toward (b).

Will stay on the lookout for opportunities to refactor uses of that
struct into "method-like" functions, make members private when it
makes sense, and finally, expose the struct definition, turning the
"method-like" functions into methods.


Tried to follow the coding conventions re. #include strategies;
apologies if I've missed something there, or anywhere else.


Kévin Le Gouguec (3):
  gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  gdb/ada-tasks: Make the ada_tasks_pspace_data getter public
  gdb: Fix Ada tasking for baremetal targets using Ravenscar threads

 gdb/ada-lang.h             | 25 +++--------
 gdb/ada-tasks.c            | 44 +++++++++---------
 gdb/ada-tasks.h            | 91 ++++++++++++++++++++++++++++++++++++++
 gdb/breakpoint.c           |  1 +
 gdb/guile/scm-breakpoint.c |  2 +-
 gdb/mi/mi-main.c           |  2 +-
 gdb/python/py-breakpoint.c |  2 +-
 gdb/ravenscar-thread.c     | 17 +++++--
 8 files changed, 134 insertions(+), 50 deletions(-)
 create mode 100644 gdb/ada-tasks.h

-- 
2.25.1


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v2 1/3] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  2023-01-27 16:53         ` [PATCH v2 0/3] " Kévin Le Gouguec via Gdb-patches
@ 2023-01-27 16:53           ` 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
                             ` (2 subsequent siblings)
  3 siblings, 0 replies; 19+ messages in thread
From: Kévin Le Gouguec via Gdb-patches @ 2023-01-27 16:53 UTC (permalink / raw)
  To: gdb-patches; +Cc: simark, Kévin Le Gouguec

Also move the documentation for these functions to the new header, instead
of at the top of their definition, to align with our current practice.

I tried to honor our guideline that .c files should not rely on indirect
inclusion, hence I only removed #include "ada-lang.h" directives in .c files
that, as far as I could tell, only rely on symbols declared in ada-tasks.h.
---
 gdb/ada-lang.h             | 19 ----------
 gdb/ada-tasks.c            | 26 ++++----------
 gdb/ada-tasks.h            | 72 ++++++++++++++++++++++++++++++++++++++
 gdb/breakpoint.c           |  1 +
 gdb/guile/scm-breakpoint.c |  2 +-
 gdb/mi/mi-main.c           |  2 +-
 gdb/python/py-breakpoint.c |  2 +-
 gdb/ravenscar-thread.c     |  1 +
 8 files changed, 84 insertions(+), 41 deletions(-)
 create mode 100644 gdb/ada-tasks.h

diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
index 9fb7ac7f384..e0d44756218 100644
--- a/gdb/ada-lang.h
+++ b/gdb/ada-lang.h
@@ -366,25 +366,6 @@ struct ada_exc_info
 
 extern std::vector<ada_exc_info> ada_exceptions_list (const char *regexp);
 
-/* Tasking-related: ada-tasks.c */
-
-extern int valid_task_id (int);
-
-extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
-
-extern int ada_get_task_number (thread_info *thread);
-
-typedef gdb::function_view<void (struct ada_task_info *task)>
-  ada_task_list_iterator_ftype;
-extern void iterate_over_live_ada_tasks
-  (ada_task_list_iterator_ftype iterator);
-
-extern const char *ada_get_tcb_types_info (void);
-
-extern void print_ada_task_info (struct ui_out *uiout,
-				 const char *taskno_str,
-				 struct inferior *inf);
-
 /* Look for a symbol for an overloaded operator for the operation OP.
    PARSE_COMPLETION is true if currently parsing for completion.
    NARGS and ARGVEC describe the arguments to the call.  Returns a
diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index a3a28063daa..bdabc0909cb 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -16,6 +16,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include "ada-tasks.h"
 #include "observable.h"
 #include "gdbcmd.h"
 #include "target.h"
@@ -327,8 +328,7 @@ get_ada_tasks_inferior_data (struct inferior *inf)
   return data;
 }
 
-/* Return the task number of the task whose thread is THREAD, or zero
-   if the task could not be found.  */
+/* See ada-tasks.h.  */
 
 int
 ada_get_task_number (thread_info *thread)
@@ -364,7 +364,7 @@ get_task_number_from_id (CORE_ADDR task_id, struct inferior *inf)
   return 0;
 }
 
-/* Return non-zero if TASK_NUM is a valid task number.  */
+/* See ada-tasks.h.  */
 
 int
 valid_task_id (int task_num)
@@ -385,8 +385,7 @@ ada_task_is_alive (const struct ada_task_info *task_info)
   return (task_info->state != Terminated);
 }
 
-/* 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.  */
+/* See ada-tasks.h.  */
 
 struct ada_task_info *
 ada_get_task_info_from_ptid (ptid_t ptid)
@@ -405,8 +404,7 @@ ada_get_task_info_from_ptid (ptid_t ptid)
   return NULL;
 }
 
-/* Call the ITERATOR function once for each Ada task that hasn't been
-   terminated yet.  */
+/* See ada-tasks.h.  */
 
 void
 iterate_over_live_ada_tasks (ada_task_list_iterator_ftype iterator)
@@ -490,14 +488,7 @@ read_fat_string_value (char *dest, struct value *val, int max_len)
   dest[len] = '\0';
 }
 
-/* Get, from the debugging information, the type description of all types
-   related to the Ada Task Control Block that are needed in order to
-   read the list of known tasks in the Ada runtime.  If all of the info
-   needed to do so is found, then save that info in the module's per-
-   program-space data, and return NULL.  Otherwise, if any information
-   cannot be found, leave the per-program-space data untouched, and
-   return an error message explaining what was missing (that error
-   message does NOT need to be deallocated).  */
+/* See ada-tasks.h.  */
 
 const char *
 ada_get_tcb_types_info (void)
@@ -1056,10 +1047,7 @@ ada_build_task_list ()
   return data->task_list.size ();
 }
 
-/* Print a table providing a short description of all Ada tasks
-   running inside inferior INF.  If ARG_STR is set, it will be
-   interpreted as a task number, and the table will be limited to
-   that task only.  */
+/* See ada-tasks.h.  */
 
 void
 print_ada_task_info (struct ui_out *uiout,
diff --git a/gdb/ada-tasks.h b/gdb/ada-tasks.h
new file mode 100644
index 00000000000..5a948c27a44
--- /dev/null
+++ b/gdb/ada-tasks.h
@@ -0,0 +1,72 @@
+/* Helper routines for Ada tasking support in GBD.
+
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef ADA_TASKS_H
+#define ADA_TASKS_H
+
+#include "gdbsupport/function-view.h"
+#include "ada-lang.h"
+#include "gdbthread.h"
+#include "inferior.h"
+#include "ui-out.h"
+
+/* Return non-zero if TASK_NUM is a valid task number.  */
+
+extern int valid_task_id (int task_num);
+
+/* 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.  */
+
+extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
+
+/* Return the task number of the task whose thread is THREAD, or zero
+   if the task could not be found.  */
+
+extern int ada_get_task_number (thread_info *thread);
+
+typedef gdb::function_view<void (struct ada_task_info *task)>
+  ada_task_list_iterator_ftype;
+
+/* Call the ITERATOR function once for each Ada task that hasn't been
+   terminated yet.  */
+
+extern void iterate_over_live_ada_tasks
+  (ada_task_list_iterator_ftype iterator);
+
+/* Get, from the debugging information, the type description of all types
+   related to the Ada Task Control Block that are needed in order to
+   read the list of known tasks in the Ada runtime.  If all of the info
+   needed to do so is found, then save that info in the module's per-
+   program-space data, and return NULL.  Otherwise, if any information
+   cannot be found, leave the per-program-space data untouched, and
+   return an error message explaining what was missing (that error
+   message does NOT need to be deallocated).  */
+
+extern const char *ada_get_tcb_types_info (void);
+
+/* Print a table providing a short description of all Ada tasks
+   running inside inferior INF.  If ARG_STR is set, it will be
+   interpreted as a task number, and the table will be limited to
+   that task only.  */
+
+extern void print_ada_task_info (struct ui_out *uiout,
+				 const char *taskno_str,
+				 struct inferior *inf);
+
+#endif /* ADA_TASKS_H */
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 00cc2ab401c..a21f8d5f8ae 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -52,6 +52,7 @@
 #include "observable.h"
 #include "memattr.h"
 #include "ada-lang.h"
+#include "ada-tasks.h"
 #include "top.h"
 #include "valprint.h"
 #include "jit.h"
diff --git a/gdb/guile/scm-breakpoint.c b/gdb/guile/scm-breakpoint.c
index a7e043d847b..6b7b91cd322 100644
--- a/gdb/guile/scm-breakpoint.c
+++ b/gdb/guile/scm-breakpoint.c
@@ -27,7 +27,7 @@
 #include "gdbthread.h"
 #include "observable.h"
 #include "cli/cli-script.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 #include "arch-utils.h"
 #include "language.h"
 #include "guile-internal.h"
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index e0cade2edc3..73f917f6071 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -46,7 +46,7 @@
 #include "osdata.h"
 #include "gdbsupport/gdb_splay_tree.h"
 #include "tracepoint.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 #include "linespec.h"
 #include "extension.h"
 #include "gdbcmd.h"
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 1b10ccd5415..5189b3af0ad 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -27,7 +27,7 @@
 #include "gdbthread.h"
 #include "observable.h"
 #include "cli/cli-script.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 #include "arch-utils.h"
 #include "language.h"
 #include "location.h"
diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c
index 22fbdbe9662..57ac9634233 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -21,6 +21,7 @@
 #include "gdbcore.h"
 #include "gdbthread.h"
 #include "ada-lang.h"
+#include "ada-tasks.h"
 #include "target.h"
 #include "inferior.h"
 #include "command.h"
-- 
2.25.1


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v2 2/3] gdb/ada-tasks: Make the ada_tasks_pspace_data getter public
  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           ` 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
  3 siblings, 0 replies; 19+ messages in thread
From: Kévin Le Gouguec via Gdb-patches @ 2023-01-27 16:53 UTC (permalink / raw)
  To: gdb-patches; +Cc: simark, Kévin Le Gouguec

A future commit will make ravenscar-thread.c call a new function that
operates on this object.
---
 gdb/ada-tasks.c |  7 ++-----
 gdb/ada-tasks.h | 13 +++++++++++++
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index bdabc0909cb..a1afd25f9a5 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -287,12 +287,9 @@ task_to_str (int taskno, const ada_task_info *task_info)
     return string_printf ("%d \"%s\"", taskno, task_info->name);
 }
 
-/* Return the ada-tasks module's data for the given program space (PSPACE).
-   If none is found, add a zero'ed one now.
-
-   This function always returns a valid object.  */
+/* See ada-tasks.h.  */
 
-static struct ada_tasks_pspace_data *
+struct ada_tasks_pspace_data *
 get_ada_tasks_pspace_data (struct program_space *pspace)
 {
   struct ada_tasks_pspace_data *data;
diff --git a/gdb/ada-tasks.h b/gdb/ada-tasks.h
index 5a948c27a44..aa85c853a47 100644
--- a/gdb/ada-tasks.h
+++ b/gdb/ada-tasks.h
@@ -24,6 +24,7 @@
 #include "ada-lang.h"
 #include "gdbthread.h"
 #include "inferior.h"
+#include "progspace.h"
 #include "ui-out.h"
 
 /* Return non-zero if TASK_NUM is a valid task number.  */
@@ -69,4 +70,16 @@ extern void print_ada_task_info (struct ui_out *uiout,
 				 const char *taskno_str,
 				 struct inferior *inf);
 
+/* An opaque type for this module's per-program-space data.  */
+
+struct ada_tasks_pspace_data;
+
+/* Return the ada-tasks module's data for the given program space (PSPACE).
+   If none is found, add a zero'ed one now.
+
+   This function always returns a valid object.  */
+
+struct ada_tasks_pspace_data *
+get_ada_tasks_pspace_data (struct program_space *pspace);
+
 #endif /* ADA_TASKS_H */
-- 
2.25.1


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v2 3/3] gdb: Fix Ada tasking for baremetal targets using Ravenscar threads
  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           ` Kévin Le Gouguec via Gdb-patches
  2026-11-06 16:51           ` [PATCH v3 0/2] " Kévin Le Gouguec
  3 siblings, 0 replies; 19+ messages in thread
From: Kévin Le Gouguec via Gdb-patches @ 2023-01-27 16:53 UTC (permalink / raw)
  To: gdb-patches; +Cc: simark, Kévin Le Gouguec

For some boards, the Ada tasking runtime defines a __gnat_gdb_cpu_first_id
symbol to let GDB map the ATCB's "Base_CPU" indices (ranging from 1 to
System.Multiprocessors.CPU) onto the CPU numbers that we read from the
remote stub and record in our ptids.

In other words, this symbol lets GDB translate "Ada CPUs" into "target
CPUs".  For example, for the Microchip PolarFire board, the runtime defines
this offset 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");

This is because on this board, CPU#1 is the "monitor" CPU; CPUs #2-5 are the
"application" CPUs that run user code.  So while the ATCB shows Base_CPUs
equal to 1, QEMU reports that our code is running on CPU #2.

We convert the ATCB's Base_CPUs into "target" CPUs before recording them in
struct ada_task_info.base_cpu; this is what we need in most of
ravenscar-thread.c, except in one specific spot: when reading the active
thread from __gnat_running_thread_table, which is defined as:

  package System.BB.Threads.Queues is
     -- [...]
     Running_Thread_Table : array (System.Multiprocessors.CPU) of Thread_Id

On baremetal targets, System.Multiprocessors.CPU is defined as:

  package System.Multiprocessors is
     -- [...]
     type CPU_Range is range 0 .. System.BB.Parameters.Max_Number_Of_CPUs;

     subtype CPU is CPU_Range range 1 .. CPU_Range'Last;

     Not_A_Specific_CPU : constant CPU_Range := 0;

Thus __gnat_running_thread_table has Max_Number_Of_CPUs elements; for the
Microchip PolarFire board, the runtime define this as:

  package System.BB.Parameters is
     -- [...]
     Max_Number_Of_CPUs : constant := 1;

So the table has just one element, but ravenscar-thread.c attempts to index
it using the "target" CPU ID, i.e. 2.

This remained undetected because as luck would have it, with the specific
compiler we were using, *(__gnat_running_thread_table+8) happened to contain
exactly the same content as *__gnat_running_thread_table.  After bumping the
compiler, the layout of the tasking runtime changed, and so did the content
of *(__gnat_running_thread_table+8).

This commit introduces a new function to let GDB convert a "target" CPU back
to an "Ada" CPU.

Tested on x86_64-linux and riscv64-elf with AdaCore's internal testsuite,
which has more extensive coverage of Ada tasking and "Ravenscar thread"
features.
---
 gdb/ada-lang.h         |  6 +++++-
 gdb/ada-tasks.c        | 11 +++++++++++
 gdb/ada-tasks.h        |  6 ++++++
 gdb/ravenscar-thread.c | 16 +++++++++++++---
 4 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
index e0d44756218..e634a8d1d40 100644
--- a/gdb/ada-lang.h
+++ b/gdb/ada-lang.h
@@ -145,7 +145,11 @@ struct ada_task_info
   /* The CPU on which the task is running.  This is dependent on
      the runtime actually providing that info, which is not always
      the case.  Normally, we should be able to count on it on
-     bare-metal targets.  */
+     bare-metal targets.
+
+     NB: This CPU number has been normalized to match the IDs reported by the
+     target, as recorded in the LWP field of PTIDs.  It may not map directly to
+     the Base_CPU recorded in the ATCB; see ada_get_runtime_cpu_index.  */
   int base_cpu;
 };
 
diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index a1afd25f9a5..7d98a16228d 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -301,6 +301,17 @@ get_ada_tasks_pspace_data (struct program_space *pspace)
   return data;
 }
 
+/* See ada-tasks.h.  */
+
+int
+ada_get_runtime_cpu_index (struct ada_tasks_pspace_data *pspace_data,
+			   int target_cpu)
+{
+  gdb_assert (pspace_data->initialized_p);
+
+  return target_cpu - pspace_data->cpu_id_offset;
+}
+
 /* Return the ada-tasks module's data for the given inferior (INF).
    If none is found, add a zero'ed one now.
 
diff --git a/gdb/ada-tasks.h b/gdb/ada-tasks.h
index aa85c853a47..325f99ca0f3 100644
--- a/gdb/ada-tasks.h
+++ b/gdb/ada-tasks.h
@@ -82,4 +82,10 @@ struct ada_tasks_pspace_data;
 struct ada_tasks_pspace_data *
 get_ada_tasks_pspace_data (struct program_space *pspace);
 
+/* Translate a "target" CPU index into a "runtime" index suitable for addressing
+   arrays dimensioned with System.Multiprocessors.CPU.  */
+
+extern int ada_get_runtime_cpu_index (struct ada_tasks_pspace_data *pspace_data,
+				      int target_cpu);
+
 #endif /* ADA_TASKS_H */
diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c
index 57ac9634233..8289b9999bc 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -31,6 +31,7 @@
 #include "top.h"
 #include "regcache.h"
 #include "objfiles.h"
+#include "progspace.h"
 #include <unordered_map>
 
 /* This module provides support for "Ravenscar" tasks (Ada) when
@@ -194,7 +195,11 @@ 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.  */
+     ada-tasks.c.
+
+     NB: These CPU numbers correspond to those reported by the target,
+     which may differ from the numbers recorded in the ATCB.  See
+     ada_get_runtime_cpu_index.  */
   std::unordered_map<ULONGEST, int> m_cpu_map;
 };
 
@@ -376,8 +381,13 @@ ravenscar_thread_target::runtime_initialized ()
    Return 0 if the ID could not be determined.  */
 
 static CORE_ADDR
-get_running_thread_id (int cpu)
+get_running_thread_id (int target_cpu)
 {
+  struct ada_tasks_pspace_data *pspace_data
+    = get_ada_tasks_pspace_data (current_program_space);
+
+  int runtime_cpu = ada_get_runtime_cpu_index (pspace_data, target_cpu);
+
   struct bound_minimal_symbol object_msym = get_running_thread_msymbol ();
   int object_size;
   int buf_size;
@@ -391,7 +401,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);
+		 + (runtime_cpu - 1) * object_size);
   buf_size = object_size;
   buf = (gdb_byte *) alloca (buf_size);
   read_memory (object_addr, buf, buf_size);
-- 
2.25.1


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v3 0/2] Fix Ada tasking for baremetal targets using Ravenscar threads
  2023-01-27 16:53         ` [PATCH v2 0/3] " Kévin Le Gouguec via Gdb-patches
                             ` (2 preceding siblings ...)
  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           ` 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
                               ` (2 more replies)
  3 siblings, 3 replies; 19+ messages in thread
From: Kévin Le Gouguec @ 2026-11-06 16:51 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kévin Le Gouguec, Simon Marchi

Howdy,

New revision of the fixup that I submitted a couple years back¹,
prompted by observing more regressions with Ada tasking on PolarFire
after recently enabling SMP support in the Ada runtime - allowing the
tasking kernel to spread tasks on more than one CPU.

Changes since v2:

* adjustments to patch 1/3 to account for rebase churn;

* dropped patch 2/3: instead of exposing a function that returns the
kitchen sink, let patch 3/3 add a function with a narrower purpose;

* overhauled patch 3/3: it took me a good long while to get
re-acquainted with the GDB code and the GNAT tasking runtime, so I
endeavored to make my (or anyone's) next visit more "guided":

  * instead of shotgunning offset additions/subtractions across
  ravenscar-thread.c, introduced a class with explicitly-named
  constructors & accessors,

  * moved the bulk of the v[12] storytelling^Wcommit message into code
  comments.

  * also, re. Simon's concern about global state: realized that instead
  of assuming current_program_space, it would make sense to use
  inf->space in ravenscar_inferior_created.

Let me know how this looks.


¹ v1: <20230112171043.1159787-1-legouguec@adacore.com>
  v2: <20230127165337.1832937-1-legouguec@adacore.com>

Kévin Le Gouguec (2):
  gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  gdb: Fix CPU ID offset for Ravenscar Threads

 gdb/Makefile.in            |   1 +
 gdb/ada-lang.h             |  19 -----
 gdb/ada-tasks.c            |  39 +++++-----
 gdb/ada-tasks.h            |  77 ++++++++++++++++++
 gdb/break-cond-parse.c     |   2 +-
 gdb/breakpoint.c           |   1 +
 gdb/guile/scm-breakpoint.c |   2 +-
 gdb/mi/mi-main.c           |   2 +-
 gdb/python/py-breakpoint.c |   2 +-
 gdb/ravenscar-thread.c     | 155 +++++++++++++++++++++++++++----------
 10 files changed, 217 insertions(+), 83 deletions(-)
 create mode 100644 gdb/ada-tasks.h

-- 
2.43.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v3 1/2] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  2026-11-06 16:51           ` [PATCH v3 0/2] " Kévin Le Gouguec
@ 2026-11-06 16:51             ` Kévin Le Gouguec
  2026-11-06 19:43               ` Simon Marchi
  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
  2 siblings, 1 reply; 19+ messages in thread
From: Kévin Le Gouguec @ 2026-11-06 16:51 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kévin Le Gouguec, Simon Marchi

Also move the documentation for these functions to the new header, instead
of at the top of their definition, to align with our current practice.

I tried to honor our guideline that .c files should not rely on indirect
inclusion, hence I only removed #include "ada-lang.h" directives in .c files
that, as far as I could tell, only rely on symbols declared in ada-tasks.h.
---
 gdb/Makefile.in            |  1 +
 gdb/ada-lang.h             | 19 ----------
 gdb/ada-tasks.c            | 26 ++++----------
 gdb/ada-tasks.h            | 72 ++++++++++++++++++++++++++++++++++++++
 gdb/break-cond-parse.c     |  2 +-
 gdb/breakpoint.c           |  1 +
 gdb/guile/scm-breakpoint.c |  2 +-
 gdb/mi/mi-main.c           |  2 +-
 gdb/python/py-breakpoint.c |  2 +-
 gdb/ravenscar-thread.c     |  1 +
 10 files changed, 86 insertions(+), 42 deletions(-)
 create mode 100644 gdb/ada-tasks.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 41dbb814787..d7964e67cab 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1285,6 +1285,7 @@ HFILES_NO_SRCDIR = \
 	ada-exp-parser.h \
 	ada-exp.h \
 	ada-lang.h \
+	ada-tasks.h \
 	addrmap.h \
 	alpha-bsd-tdep.h \
 	alpha-tdep.h \
diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
index cf89a90f2f5..96043b96b92 100644
--- a/gdb/ada-lang.h
+++ b/gdb/ada-lang.h
@@ -362,25 +362,6 @@ struct ada_exc_info
 
 extern std::vector<ada_exc_info> ada_exceptions_list (const char *regexp);
 
-/* Tasking-related: ada-tasks.c */
-
-extern bool valid_task_id (int);
-
-extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
-
-extern int ada_get_task_number (thread_info *thread);
-
-using ada_task_list_iterator_ftype
-  = gdb::function_view<void (struct ada_task_info *task)>;
-extern void iterate_over_live_ada_tasks
-  (ada_task_list_iterator_ftype iterator);
-
-extern const char *ada_get_tcb_types_info (void);
-
-extern void print_ada_task_info (struct ui_out *uiout,
-				 const char *taskno_str,
-				 struct inferior *inf);
-
 /* Look for a symbol for an overloaded operator for the operation OP.
    PARSE_COMPLETION is true if currently parsing for completion.
    NARGS and ARGVEC describe the arguments to the call.  Returns a
diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index 7dfa819a937..503563de42a 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -20,6 +20,7 @@
 #include "cli/cli-cmds.h"
 #include "target.h"
 #include "ada-lang.h"
+#include "ada-tasks.h"
 #include "gdbcore.h"
 #include "inferior.h"
 #include "gdbthread.h"
@@ -317,8 +318,7 @@ get_ada_tasks_inferior_data (struct inferior *inf)
   return &ada_tasks_inferior_data_handle.try_emplace (inf);
 }
 
-/* Return the task number of the task whose thread is THREAD, or zero
-   if the task could not be found.  */
+/* See ada-tasks.h.  */
 
 int
 ada_get_task_number (thread_info *thread)
@@ -354,7 +354,7 @@ get_task_number_from_id (CORE_ADDR task_id, struct inferior *inf)
   return 0;
 }
 
-/* Return true if TASK_NUM is a valid task number.  */
+/* See ada-tasks.h.  */
 
 bool
 valid_task_id (int task_num)
@@ -375,8 +375,7 @@ ada_task_is_alive (const struct ada_task_info *task_info)
   return (task_info->state != Terminated);
 }
 
-/* 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.  */
+/* See ada-tasks.h.  */
 
 struct ada_task_info *
 ada_get_task_info_from_ptid (ptid_t ptid)
@@ -395,8 +394,7 @@ ada_get_task_info_from_ptid (ptid_t ptid)
   return NULL;
 }
 
-/* Call the ITERATOR function once for each Ada task that hasn't been
-   terminated yet.  */
+/* See ada-tasks.h.  */
 
 void
 iterate_over_live_ada_tasks (ada_task_list_iterator_ftype iterator)
@@ -480,14 +478,7 @@ read_fat_string_value (char *dest, struct value *val, int max_len)
   dest[len] = '\0';
 }
 
-/* Get, from the debugging information, the type description of all types
-   related to the Ada Task Control Block that are needed in order to
-   read the list of known tasks in the Ada runtime.  If all of the info
-   needed to do so is found, then save that info in the module's per-
-   program-space data, and return NULL.  Otherwise, if any information
-   cannot be found, leave the per-program-space data untouched, and
-   return an error message explaining what was missing (that error
-   message does NOT need to be deallocated).  */
+/* See ada-tasks.h.  */
 
 const char *
 ada_get_tcb_types_info (void)
@@ -1036,10 +1027,7 @@ ada_build_task_list ()
   return data->task_list.size ();
 }
 
-/* Print a table providing a short description of all Ada tasks
-   running inside inferior INF.  If ARG_STR is set, it will be
-   interpreted as a task number, and the table will be limited to
-   that task only.  */
+/* See ada-tasks.h.  */
 
 void
 print_ada_task_info (struct ui_out *uiout,
diff --git a/gdb/ada-tasks.h b/gdb/ada-tasks.h
new file mode 100644
index 00000000000..a9fa798a5c2
--- /dev/null
+++ b/gdb/ada-tasks.h
@@ -0,0 +1,72 @@
+/* Helper routines for Ada tasking support in GBD.
+
+   Copyright (C) 2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_ADA_TASKS_H
+#define GDB_ADA_TASKS_H
+
+#include "gdbsupport/function-view.h"
+#include "ada-lang.h"
+#include "gdbthread.h"
+#include "inferior.h"
+#include "ui-out.h"
+
+/* Return true if TASK_NUM is a valid task number.  */
+
+extern bool valid_task_id (int task_num);
+
+/* 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.  */
+
+extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
+
+/* Return the task number of the task whose thread is THREAD, or zero
+   if the task could not be found.  */
+
+extern int ada_get_task_number (thread_info *thread);
+
+using ada_task_list_iterator_ftype
+  = gdb::function_view<void (struct ada_task_info *task)>;
+
+/* Call the ITERATOR function once for each Ada task that hasn't been
+   terminated yet.  */
+
+extern void iterate_over_live_ada_tasks
+  (ada_task_list_iterator_ftype iterator);
+
+/* Get, from the debugging information, the type description of all types
+   related to the Ada Task Control Block that are needed in order to
+   read the list of known tasks in the Ada runtime.  If all of the info
+   needed to do so is found, then save that info in the module's per-
+   program-space data, and return NULL.  Otherwise, if any information
+   cannot be found, leave the per-program-space data untouched, and
+   return an error message explaining what was missing (that error
+   message does NOT need to be deallocated).  */
+
+extern const char *ada_get_tcb_types_info (void);
+
+/* Print a table providing a short description of all Ada tasks
+   running inside inferior INF.  If ARG_STR is set, it will be
+   interpreted as a task number, and the table will be limited to
+   that task only.  */
+
+extern void print_ada_task_info (struct ui_out *uiout,
+				 const char *taskno_str,
+				 struct inferior *inf);
+
+#endif /* GDB_ADA_TASKS_H */
diff --git a/gdb/break-cond-parse.c b/gdb/break-cond-parse.c
index f552c171c05..fa532f21669 100644
--- a/gdb/break-cond-parse.c
+++ b/gdb/break-cond-parse.c
@@ -21,7 +21,7 @@
 #include "scoped-mock-context.h"
 #include "break-cond-parse.h"
 #include "tid-parse.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 
 /* When parsing tokens from a string, which direction are we parsing?
 
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index a23f5998b20..6e993f332b4 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -52,6 +52,7 @@
 #include "observable.h"
 #include "memattr.h"
 #include "ada-lang.h"
+#include "ada-tasks.h"
 #include "top.h"
 #include "ui.h"
 #include "valprint.h"
diff --git a/gdb/guile/scm-breakpoint.c b/gdb/guile/scm-breakpoint.c
index 5df2c00f5d3..911e13448a2 100644
--- a/gdb/guile/scm-breakpoint.c
+++ b/gdb/guile/scm-breakpoint.c
@@ -26,7 +26,7 @@
 #include "gdbthread.h"
 #include "observable.h"
 #include "cli/cli-script.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 #include "arch-utils.h"
 #include "language.h"
 #include "guile-internal.h"
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 42f5dcc3ded..f4bca4048d5 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -44,7 +44,7 @@
 #include "valprint.h"
 #include "osdata.h"
 #include "tracepoint.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 #include "linespec.h"
 #include "extension.h"
 #include "cli/cli-cmds.h"
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 6a5e133087e..eefe7314152 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -27,7 +27,7 @@
 #include "gdbthread.h"
 #include "observable.h"
 #include "cli/cli-script.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 #include "arch-utils.h"
 #include "language.h"
 #include "location.h"
diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c
index b5e0979a7f1..bb3d7f9e847 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -21,6 +21,7 @@
 #include "gdbcore.h"
 #include "gdbthread.h"
 #include "ada-lang.h"
+#include "ada-tasks.h"
 #include "target.h"
 #include "inferior.h"
 #include "command.h"
-- 
2.43.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v3 2/2] gdb: Fix CPU ID offset for Ravenscar Threads
  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 16:51             ` 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
  2 siblings, 0 replies; 19+ messages in thread
From: Kévin Le Gouguec @ 2026-11-06 16:51 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kévin Le Gouguec, Simon Marchi

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<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


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v3 1/2] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Simon Marchi @ 2026-11-06 19:43 UTC (permalink / raw)
  To: Kévin Le Gouguec, gdb-patches

On 11/6/26 11:51 AM, Kévin Le Gouguec wrote:
> Also move the documentation for these functions to the new header, instead
> of at the top of their definition, to align with our current practice.
> 
> I tried to honor our guideline that .c files should not rely on indirect
> inclusion, hence I only removed #include "ada-lang.h" directives in .c files
> that, as far as I could tell, only rely on symbols declared in ada-tasks.h.

Tom can perhaps more formally approve, but I like this patch.

> diff --git a/gdb/ada-tasks.h b/gdb/ada-tasks.h
> new file mode 100644
> index 00000000000..a9fa798a5c2
> --- /dev/null
> +++ b/gdb/ada-tasks.h
> @@ -0,0 +1,72 @@
> +/* Helper routines for Ada tasking support in GBD.
> +
> +   Copyright (C) 2026 Free Software Foundation, Inc.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
> +
> +#ifndef GDB_ADA_TASKS_H
> +#define GDB_ADA_TASKS_H
> +
> +#include "gdbsupport/function-view.h"
> +#include "ada-lang.h"
> +#include "gdbthread.h"
> +#include "inferior.h"
> +#include "ui-out.h"

As far as I can tell, ada-tasks.h does not need any header, only the
forward declaration:

struct thread_info;

The other types don't need a forward declaration as it is, because they
spelled with the "struct" keyword, which is itself a declaration.  And
the header does not need to see the complete declaration of the types.

I check that by opening the file in my VSCode+clangd, if there is any
error reported it means something is missing.

> +/* Get, from the debugging information, the type description of all types
> +   related to the Ada Task Control Block that are needed in order to
> +   read the list of known tasks in the Ada runtime.  If all of the info
> +   needed to do so is found, then save that info in the module's per-
> +   program-space data, and return NULL.  Otherwise, if any information
> +   cannot be found, leave the per-program-space data untouched, and
> +   return an error message explaining what was missing (that error
> +   message does NOT need to be deallocated).  */
> +
> +extern const char *ada_get_tcb_types_info (void);

You can remove the `void`.

Simon

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v3 1/2] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  2026-11-06 19:43               ` Simon Marchi
@ 2026-11-09  9:14                 ` Kévin Le Gouguec
  2026-11-09 15:02                   ` Simon Marchi
  0 siblings, 1 reply; 19+ messages in thread
From: Kévin Le Gouguec @ 2026-11-09  9:14 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

Simon Marchi <simark@simark.ca> writes:

> On 11/6/26 11:51 AM, Kévin Le Gouguec wrote:
>> Also move the documentation for these functions to the new header, instead
>> of at the top of their definition, to align with our current practice.
>> 
>> I tried to honor our guideline that .c files should not rely on indirect
>> inclusion, hence I only removed #include "ada-lang.h" directives in .c files
>> that, as far as I could tell, only rely on symbols declared in ada-tasks.h.
>
> Tom can perhaps more formally approve, but I like this patch.

(Thanks for taking a look!)

> I check that by opening the file in my VSCode+clangd, if there is any
> error reported it means something is missing.

Thanks for mentioning; decided to give clangd a try and after some
.clangd fiddling¹, I do get a warning-free ada-tasks.h with your
amendments.  I'm curious whether & how you configure it tho, since I get
more spurious-looking diagnoses if I venture into other files²; I'm
assuming I'm missing more compilation flags, and wondering how best to
get an "authoritative" list & keep it up-to-date.

(Searched gdb-patches because I'm sure you've mentioned your clangd
setup before, but could not find my answers)

(FWIW, I think that's the first language server I try with Eglot that
defaults to enabling inlay hints & semantic tokens.  Thanks for the
hearty fruit salad 😀)


¹ Current state of affairs:

    If:
      PathMatch: gdb.*/.*.[ch]
    CompileFlags:
      Add:
        - -I/ABSOLUTE/PATH/TO/binutils-gdb/include
        - -I/ABSOLUTE/PATH/TO/binutils-gdb/gdb
        - -I/ABSOLUTE/PATH/TO/binutils-gdb/gnulib/import
        - -I/ABSOLUTE/PATH/TO/binutils-gdb
        - -I/ABSOLUTE/PATH/TO/binutils-gdb/bfd
        - -include
        - /ABSOLUTE/PATH/TO/binutils-gdb/gdbsupport/common-defs.h

Curious if the file can be kept "relocatable", i.e. free of absolute
paths.  From the doc & open GH issues, unsure whether compilation
commands are relative to the project root, or if some placeholder exists
that will be expanded to the project root.

² E.g. all members of gdb/ada-tasks.c:struct ada_tasks_pspace_data are
flagged with

> clang [expected_semi_decl_list]: Expected ';' at end of declaration list


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v3 1/2] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Simon Marchi @ 2026-11-09 15:02 UTC (permalink / raw)
  To: Kévin Le Gouguec; +Cc: gdb-patches

On 11/9/26 4:14 AM, Kévin Le Gouguec wrote:
> Simon Marchi <simark@simark.ca> writes:
> 
>> On 11/6/26 11:51 AM, Kévin Le Gouguec wrote:
>>> Also move the documentation for these functions to the new header, instead
>>> of at the top of their definition, to align with our current practice.
>>>
>>> I tried to honor our guideline that .c files should not rely on indirect
>>> inclusion, hence I only removed #include "ada-lang.h" directives in .c files
>>> that, as far as I could tell, only rely on symbols declared in ada-tasks.h.
>>
>> Tom can perhaps more formally approve, but I like this patch.
> 
> (Thanks for taking a look!)
> 
>> I check that by opening the file in my VSCode+clangd, if there is any
>> error reported it means something is missing.
> 
> Thanks for mentioning; decided to give clangd a try and after some
> .clangd fiddling¹, I do get a warning-free ada-tasks.h with your
> amendments.  I'm curious whether & how you configure it tho, since I get
> more spurious-looking diagnoses if I venture into other files²; I'm
> assuming I'm missing more compilation flags, and wondering how best to
> get an "authoritative" list & keep it up-to-date.
> 
> (Searched gdb-patches because I'm sure you've mentioned your clangd
> setup before, but could not find my answers)
> 
> (FWIW, I think that's the first language server I try with Eglot that
> defaults to enabling inlay hints & semantic tokens.  Thanks for the
> hearty fruit salad 😀)
> 
> 
> ¹ Current state of affairs:
> 
>     If:
>       PathMatch: gdb.*/.*.[ch]
>     CompileFlags:
>       Add:
>         - -I/ABSOLUTE/PATH/TO/binutils-gdb/include
>         - -I/ABSOLUTE/PATH/TO/binutils-gdb/gdb
>         - -I/ABSOLUTE/PATH/TO/binutils-gdb/gnulib/import
>         - -I/ABSOLUTE/PATH/TO/binutils-gdb
>         - -I/ABSOLUTE/PATH/TO/binutils-gdb/bfd
>         - -include
>         - /ABSOLUTE/PATH/TO/binutils-gdb/gdbsupport/common-defs.h

I would not try to do it by hand, use a generated compile_commands.json
isntead.  Do a clang build somewhere (if you don't have one already),
you can start with this and adjust to your liking:

    $ ../path/to/configure CC="ccache clang" CXX="ccache clang++" --enable-targets=all --with-python=python3 --with-guile --enable-sim --disable-werror

Then build with "bear" to generate compile_commands.json:

    $ bear -- make -j $(nproc)

If the build fails in a middle and you have to tweak something, do a
"make clean" and start over, because bear by default replaces the entire
compile_commands.json, so you could be missing what was previously
successfully compiled (or you could use "bear -a" I guess, but I prefer
starting from scratch).

The reason I am using a clang build for this is that if you build with
gcc, then compile_commands.json will contain some flags unknown to
clang, and at least a long time ago clangd would warn about them.  Maybe
that improved in the recent years.

You can disable the directories you don't care about (e.g. "--disable-ld").

Then, make a symlink from your source dir to the compile_commands.json:

    $ cd path/to/source
    $ ln -s path/to/clang/build/compile_commands.json

Now, clangd should be mostly error-free when opening anything.

> Curious if the file can be kept "relocatable", i.e. free of absolute
> paths.  From the doc & open GH issues, unsure whether compilation
> commands are relative to the project root, or if some placeholder exists
> that will be expanded to the project root.

I don't think it's worth maintaining something like that.  There are
also some include paths that point into the actual build you are using,
so that would be quite complicated.

> ² E.g. all members of gdb/ada-tasks.c:struct ada_tasks_pspace_data are
> flagged with
> 
>> clang [expected_semi_decl_list]: Expected ';' at end of declaration list

It's not expected, let's see if that goes away once you have a
compile_commands.json.

Simon

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH v3 1/2] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  2026-11-09 15:02                   ` Simon Marchi
@ 2026-11-09 17:07                     ` Kévin Le Gouguec
  0 siblings, 0 replies; 19+ messages in thread
From: Kévin Le Gouguec @ 2026-11-09 17:07 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 3738 bytes --]

Simon Marchi <simark@simark.ca> writes:

>>> I check that by opening the file in my VSCode+clangd, if there is any
>>> error reported it means something is missing.
>> 
>> Thanks for mentioning; decided to give clangd a try and after some
>> .clangd fiddling¹, I do get a warning-free ada-tasks.h with your
>> amendments.  I'm curious whether & how you configure it tho, since I get
>> more spurious-looking diagnoses if I venture into other files²; I'm
>> assuming I'm missing more compilation flags, and wondering how best to
>> get an "authoritative" list & keep it up-to-date.
>> 
>> (Searched gdb-patches because I'm sure you've mentioned your clangd
>> setup before, but could not find my answers)
>> 
>> (FWIW, I think that's the first language server I try with Eglot that
>> defaults to enabling inlay hints & semantic tokens.  Thanks for the
>> hearty fruit salad 😀)
>> 
>> 
>> ¹ Current state of affairs:
>> 
>>     <snip handcrafted .clang>
>
> I would not try to do it by hand, use a generated compile_commands.json
> isntead.  Do a clang build somewhere (if you don't have one already),
> you can start with this and adjust to your liking:
>
>     $ ../path/to/configure CC="ccache clang" CXX="ccache clang++" --enable-targets=all --with-python=python3 --with-guile --enable-sim --disable-werror
>
> Then build with "bear" to generate compile_commands.json:
>
>     $ bear -- make -j $(nproc)
>
> If the build fails in a middle and you have to tweak something, do a
> "make clean" and start over, because bear by default replaces the entire
> compile_commands.json, so you could be missing what was previously
> successfully compiled (or you could use "bear -a" I guess, but I prefer
> starting from scratch).
>
> The reason I am using a clang build for this is that if you build with
> gcc, then compile_commands.json will contain some flags unknown to
> clang, and at least a long time ago clangd would warn about them.  Maybe
> that improved in the recent years.
>
> You can disable the directories you don't care about (e.g. "--disable-ld").
>
> Then, make a symlink from your source dir to the compile_commands.json:
>
>     $ cd path/to/source
>     $ ln -s path/to/clang/build/compile_commands.json
>
> Now, clangd should be mostly error-free when opening anything.

Thanks for the recipe; worked flawlessly AFAICT.  IOW, after

1. 'apt install bear ccache clang clangd'
2. your recipe, (more-or-less; see attached)
3. M-x eglot,

Most LSP functions I cared to test before reporting seemed to work.

"Friendship ended with 'make TAGS', .dir-locals.el & tags-table-list;
now 'bear -- make', compile_commands.json, clangd & Eglot are my best
friends", as the meme goes.

(Tho I really need to 'M-x customize-group
eglot-semantic-fontification'; the current colors feel like nyan-mode
got sick of the mode-line and broke containment)

>> ² E.g. all members of gdb/ada-tasks.c:struct ada_tasks_pspace_data are
>> flagged with
>> 
>>> clang [expected_semi_decl_list]: Expected ';' at end of declaration list
>
> It's not expected, let's see if that goes away once you have a
> compile_commands.json.

The error did go away.

Reviewing compile_commands.json, the missing piece might have been just
'-x c++': if I scrap compile_commands.json, restore my handcrafted
.clangd and add that switch, those specific errors on struct
ada_tasks_pspace_data do go away.

Tho there's still a dozen more errors, so I'll keep
compile_commands.json for now.

Again, thanks for taking the time to describe your setup, that's going
to help me with much more than just include hygiene.

(And as for the series, will submit v4 momentarily)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: clangd.sh --]
[-- Type: text/x-sh, Size: 547 bytes --]

#!/bin/bash

set -eux

test -e .git || {
    echo >&2 "Re-run from the repository root directory."
    exit 1
}

SRCDIR=${PWD}
CLANGDIR=${PWD}.clangd

declare -a CFGFLAGS=(
    CC='ccache clang'
    CXX='ccache clang++'
    --enable-targets=all
    --with-python=python3
    --disable-{binutils,gas,gprof,gprofng,ld,libctf,nls,plugins,sim}
    --disable-werror
)

mkdir -p "${CLANGDIR}"
(
    cd "${CLANGDIR}"

    "${SRCDIR}"/configure "${CFGFLAGS[@]}"
    bear -- make -j$(nproc --all)
)

ln -fsv "${CLANGDIR}"/compile_commands.json "${SRCDIR}"

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v4 0/2] Fix Ada tasking for baremetal targets using Ravenscar threads
  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 16:51             ` [PATCH v3 2/2] gdb: Fix CPU ID offset for Ravenscar Threads Kévin Le Gouguec
@ 2026-11-13 16:40             ` 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               ` [PATCH v4 2/2] gdb: Fix CPU ID offset for Ravenscar Threads Kévin Le Gouguec
  2 siblings, 2 replies; 19+ messages in thread
From: Kévin Le Gouguec @ 2026-11-13 16:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kévin Le Gouguec, Simon Marchi

This series addresses bugs with CPU indices in ravenscar-thread.c, found
while analyzing regressions with task debugging on AdaCore's runtime for
Microchip PolarFire, following various toolchain changes (upgrading the
compiler; enabling support for SMP in the runtime).

* Patch 1 is preparatory cleanup.
* Patch 2 is the actual fix.

Changes since v3: (see also range diff below)
* (patch 1) make new ada-tasks.h header cleaner:
  * remove redundant #include's in new ada-tasks.h header,
  * remove C-ism (s/(void)/()/).

Kévin Le Gouguec (2):
  gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  gdb: Fix CPU ID offset for Ravenscar Threads

 gdb/Makefile.in            |   1 +
 gdb/ada-lang.h             |  19 -----
 gdb/ada-tasks.c            |  39 +++++-----
 gdb/ada-tasks.h            |  73 +++++++++++++++++
 gdb/break-cond-parse.c     |   2 +-
 gdb/breakpoint.c           |   1 +
 gdb/guile/scm-breakpoint.c |   2 +-
 gdb/mi/mi-main.c           |   2 +-
 gdb/python/py-breakpoint.c |   2 +-
 gdb/ravenscar-thread.c     | 155 +++++++++++++++++++++++++++----------
 10 files changed, 213 insertions(+), 83 deletions(-)
 create mode 100644 gdb/ada-tasks.h

Range-diff against v3:
1:  75610076731 ! 1:  747b666d5ed gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
    @@ gdb/ada-tasks.h (new)
     +#ifndef GDB_ADA_TASKS_H
     +#define GDB_ADA_TASKS_H
     +
    -+#include "gdbsupport/function-view.h"
    -+#include "ada-lang.h"
    -+#include "gdbthread.h"
    -+#include "inferior.h"
    -+#include "ui-out.h"
    -+
     +/* Return true if TASK_NUM is a valid task number.  */
     +
     +extern bool valid_task_id (int task_num);
    @@ gdb/ada-tasks.h (new)
     +
     +extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
     +
    ++struct thread_info;
    ++
     +/* Return the task number of the task whose thread is THREAD, or zero
     +   if the task could not be found.  */
     +
    @@ gdb/ada-tasks.h (new)
     +   return an error message explaining what was missing (that error
     +   message does NOT need to be deallocated).  */
     +
    -+extern const char *ada_get_tcb_types_info (void);
    ++extern const char *ada_get_tcb_types_info ();
     +
     +/* Print a table providing a short description of all Ada tasks
     +   running inside inferior INF.  If ARG_STR is set, it will be
2:  ec3fe94d3d7 = 2:  4c600a461fb gdb: Fix CPU ID offset for Ravenscar Threads
-- 
2.43.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v4 1/2] gdb: Introduce ada-tasks.h for functions defined in ada-tasks.c
  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               ` Kévin Le Gouguec
  2026-11-13 16:40               ` [PATCH v4 2/2] gdb: Fix CPU ID offset for Ravenscar Threads Kévin Le Gouguec
  1 sibling, 0 replies; 19+ messages in thread
From: Kévin Le Gouguec @ 2026-11-13 16:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kévin Le Gouguec, Simon Marchi

Also move the documentation for these functions to the new header, instead
of at the top of their definition, to align with our current practice.

I tried to honor our guideline that .c files should not rely on indirect
inclusion, hence I only removed #include "ada-lang.h" directives in .c files
that, as far as I could tell, only rely on symbols declared in ada-tasks.h.
---
 gdb/Makefile.in            |  1 +
 gdb/ada-lang.h             | 19 -----------
 gdb/ada-tasks.c            | 26 ++++-----------
 gdb/ada-tasks.h            | 68 ++++++++++++++++++++++++++++++++++++++
 gdb/break-cond-parse.c     |  2 +-
 gdb/breakpoint.c           |  1 +
 gdb/guile/scm-breakpoint.c |  2 +-
 gdb/mi/mi-main.c           |  2 +-
 gdb/python/py-breakpoint.c |  2 +-
 gdb/ravenscar-thread.c     |  1 +
 10 files changed, 82 insertions(+), 42 deletions(-)
 create mode 100644 gdb/ada-tasks.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 41dbb814787..d7964e67cab 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1285,6 +1285,7 @@ HFILES_NO_SRCDIR = \
 	ada-exp-parser.h \
 	ada-exp.h \
 	ada-lang.h \
+	ada-tasks.h \
 	addrmap.h \
 	alpha-bsd-tdep.h \
 	alpha-tdep.h \
diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
index cf89a90f2f5..96043b96b92 100644
--- a/gdb/ada-lang.h
+++ b/gdb/ada-lang.h
@@ -362,25 +362,6 @@ struct ada_exc_info
 
 extern std::vector<ada_exc_info> ada_exceptions_list (const char *regexp);
 
-/* Tasking-related: ada-tasks.c */
-
-extern bool valid_task_id (int);
-
-extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
-
-extern int ada_get_task_number (thread_info *thread);
-
-using ada_task_list_iterator_ftype
-  = gdb::function_view<void (struct ada_task_info *task)>;
-extern void iterate_over_live_ada_tasks
-  (ada_task_list_iterator_ftype iterator);
-
-extern const char *ada_get_tcb_types_info (void);
-
-extern void print_ada_task_info (struct ui_out *uiout,
-				 const char *taskno_str,
-				 struct inferior *inf);
-
 /* Look for a symbol for an overloaded operator for the operation OP.
    PARSE_COMPLETION is true if currently parsing for completion.
    NARGS and ARGVEC describe the arguments to the call.  Returns a
diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index 7dfa819a937..503563de42a 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -20,6 +20,7 @@
 #include "cli/cli-cmds.h"
 #include "target.h"
 #include "ada-lang.h"
+#include "ada-tasks.h"
 #include "gdbcore.h"
 #include "inferior.h"
 #include "gdbthread.h"
@@ -317,8 +318,7 @@ get_ada_tasks_inferior_data (struct inferior *inf)
   return &ada_tasks_inferior_data_handle.try_emplace (inf);
 }
 
-/* Return the task number of the task whose thread is THREAD, or zero
-   if the task could not be found.  */
+/* See ada-tasks.h.  */
 
 int
 ada_get_task_number (thread_info *thread)
@@ -354,7 +354,7 @@ get_task_number_from_id (CORE_ADDR task_id, struct inferior *inf)
   return 0;
 }
 
-/* Return true if TASK_NUM is a valid task number.  */
+/* See ada-tasks.h.  */
 
 bool
 valid_task_id (int task_num)
@@ -375,8 +375,7 @@ ada_task_is_alive (const struct ada_task_info *task_info)
   return (task_info->state != Terminated);
 }
 
-/* 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.  */
+/* See ada-tasks.h.  */
 
 struct ada_task_info *
 ada_get_task_info_from_ptid (ptid_t ptid)
@@ -395,8 +394,7 @@ ada_get_task_info_from_ptid (ptid_t ptid)
   return NULL;
 }
 
-/* Call the ITERATOR function once for each Ada task that hasn't been
-   terminated yet.  */
+/* See ada-tasks.h.  */
 
 void
 iterate_over_live_ada_tasks (ada_task_list_iterator_ftype iterator)
@@ -480,14 +478,7 @@ read_fat_string_value (char *dest, struct value *val, int max_len)
   dest[len] = '\0';
 }
 
-/* Get, from the debugging information, the type description of all types
-   related to the Ada Task Control Block that are needed in order to
-   read the list of known tasks in the Ada runtime.  If all of the info
-   needed to do so is found, then save that info in the module's per-
-   program-space data, and return NULL.  Otherwise, if any information
-   cannot be found, leave the per-program-space data untouched, and
-   return an error message explaining what was missing (that error
-   message does NOT need to be deallocated).  */
+/* See ada-tasks.h.  */
 
 const char *
 ada_get_tcb_types_info (void)
@@ -1036,10 +1027,7 @@ ada_build_task_list ()
   return data->task_list.size ();
 }
 
-/* Print a table providing a short description of all Ada tasks
-   running inside inferior INF.  If ARG_STR is set, it will be
-   interpreted as a task number, and the table will be limited to
-   that task only.  */
+/* See ada-tasks.h.  */
 
 void
 print_ada_task_info (struct ui_out *uiout,
diff --git a/gdb/ada-tasks.h b/gdb/ada-tasks.h
new file mode 100644
index 00000000000..66870ac323f
--- /dev/null
+++ b/gdb/ada-tasks.h
@@ -0,0 +1,68 @@
+/* Helper routines for Ada tasking support in GBD.
+
+   Copyright (C) 2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_ADA_TASKS_H
+#define GDB_ADA_TASKS_H
+
+/* Return true if TASK_NUM is a valid task number.  */
+
+extern bool valid_task_id (int task_num);
+
+/* 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.  */
+
+extern struct ada_task_info *ada_get_task_info_from_ptid (ptid_t ptid);
+
+struct thread_info;
+
+/* Return the task number of the task whose thread is THREAD, or zero
+   if the task could not be found.  */
+
+extern int ada_get_task_number (thread_info *thread);
+
+using ada_task_list_iterator_ftype
+  = gdb::function_view<void (struct ada_task_info *task)>;
+
+/* Call the ITERATOR function once for each Ada task that hasn't been
+   terminated yet.  */
+
+extern void iterate_over_live_ada_tasks
+  (ada_task_list_iterator_ftype iterator);
+
+/* Get, from the debugging information, the type description of all types
+   related to the Ada Task Control Block that are needed in order to
+   read the list of known tasks in the Ada runtime.  If all of the info
+   needed to do so is found, then save that info in the module's per-
+   program-space data, and return NULL.  Otherwise, if any information
+   cannot be found, leave the per-program-space data untouched, and
+   return an error message explaining what was missing (that error
+   message does NOT need to be deallocated).  */
+
+extern const char *ada_get_tcb_types_info ();
+
+/* Print a table providing a short description of all Ada tasks
+   running inside inferior INF.  If ARG_STR is set, it will be
+   interpreted as a task number, and the table will be limited to
+   that task only.  */
+
+extern void print_ada_task_info (struct ui_out *uiout,
+				 const char *taskno_str,
+				 struct inferior *inf);
+
+#endif /* GDB_ADA_TASKS_H */
diff --git a/gdb/break-cond-parse.c b/gdb/break-cond-parse.c
index f552c171c05..fa532f21669 100644
--- a/gdb/break-cond-parse.c
+++ b/gdb/break-cond-parse.c
@@ -21,7 +21,7 @@
 #include "scoped-mock-context.h"
 #include "break-cond-parse.h"
 #include "tid-parse.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 
 /* When parsing tokens from a string, which direction are we parsing?
 
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index a23f5998b20..6e993f332b4 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -52,6 +52,7 @@
 #include "observable.h"
 #include "memattr.h"
 #include "ada-lang.h"
+#include "ada-tasks.h"
 #include "top.h"
 #include "ui.h"
 #include "valprint.h"
diff --git a/gdb/guile/scm-breakpoint.c b/gdb/guile/scm-breakpoint.c
index 5df2c00f5d3..911e13448a2 100644
--- a/gdb/guile/scm-breakpoint.c
+++ b/gdb/guile/scm-breakpoint.c
@@ -26,7 +26,7 @@
 #include "gdbthread.h"
 #include "observable.h"
 #include "cli/cli-script.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 #include "arch-utils.h"
 #include "language.h"
 #include "guile-internal.h"
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 42f5dcc3ded..f4bca4048d5 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -44,7 +44,7 @@
 #include "valprint.h"
 #include "osdata.h"
 #include "tracepoint.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 #include "linespec.h"
 #include "extension.h"
 #include "cli/cli-cmds.h"
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index ee7276f1792..2a539513502 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -27,7 +27,7 @@
 #include "gdbthread.h"
 #include "observable.h"
 #include "cli/cli-script.h"
-#include "ada-lang.h"
+#include "ada-tasks.h"
 #include "arch-utils.h"
 #include "language.h"
 #include "location.h"
diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c
index b5e0979a7f1..bb3d7f9e847 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -21,6 +21,7 @@
 #include "gdbcore.h"
 #include "gdbthread.h"
 #include "ada-lang.h"
+#include "ada-tasks.h"
 #include "target.h"
 #include "inferior.h"
 #include "command.h"
-- 
2.43.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v4 2/2] gdb: Fix CPU ID offset for Ravenscar Threads
  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
  1 sibling, 0 replies; 19+ messages in thread
From: Kévin Le Gouguec @ 2026-11-13 16:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kévin Le Gouguec, Simon Marchi

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


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2026-11-13 16:42 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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               ` [PATCH v4 2/2] gdb: Fix CPU ID offset for Ravenscar Threads Kévin Le Gouguec

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox