[v2,3/3] gdb: Fix Ada tasking for baremetal targets using Ravenscar threads
Commit Message
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(-)
@@ -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;
};
@@ -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.
@@ -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 */
@@ -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);