1 /* Ada Ravenscar thread support.
3 Copyright (C) 2004-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdbthread.h"
27 #include "ravenscar-thread.h"
28 #include "observable.h"
34 /* This module provides support for "Ravenscar" tasks (Ada) when
35 debugging on bare-metal targets.
37 The typical situation is when debugging a bare-metal target over
38 the remote protocol. In that situation, the system does not know
39 about high-level comcepts such as threads, only about some code
40 running on one or more CPUs. And since the remote protocol does not
41 provide any handling for CPUs, the de facto standard for handling
42 them is to have one thread per CPU, where the thread's ptid has
43 its lwp field set to the CPU number (eg: 1 for the first CPU,
44 2 for the second one, etc). This module will make that assumption.
46 This module then creates and maintains the list of threads based
47 on the list of Ada tasks, with one thread per Ada tasks. The convention
48 is that threads corresponding to the CPUs (see assumption above)
49 have a ptid_t of the form (PID, LWP, 0), which threads corresponding
50 to our Ada tasks have a ptid_t of the form (PID, 0, TID) where TID
51 is the Ada task's ID as extracted from Ada runtime information.
53 Switching to a given Ada tasks (or its underlying thread) is performed
54 by fetching the registers of that tasks from the memory area where
55 the registers were saved. For any of the other operations, the
56 operation is performed by first finding the CPU on which the task
57 is running, switching to its corresponding ptid, and then performing
58 the operation on that ptid using the target beneath us. */
60 /* If non-null, ravenscar task support is enabled. */
61 static int ravenscar_task_support = 1;
63 /* PTID of the last thread that received an event.
64 This can be useful to determine the associated task that received
65 the event, to make it the current task. */
66 static ptid_t base_ptid;
68 static const char running_thread_name[] = "__gnat_running_thread_table";
70 static const char known_tasks_name[] = "system__tasking__debug__known_tasks";
71 static const char first_task_name[] = "system__tasking__debug__first_task";
73 static const char ravenscar_runtime_initializer[] =
74 "system__bb__threads__initialize";
76 static const target_info ravenscar_target_info = {
78 N_("Ravenscar tasks."),
79 N_("Ravenscar tasks support.")
82 struct ravenscar_thread_target final : public target_ops
84 ravenscar_thread_target ()
85 { to_stratum = thread_stratum; }
87 const target_info &info () const override
88 { return ravenscar_target_info; }
90 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
91 void resume (ptid_t, int, enum gdb_signal) override;
93 void fetch_registers (struct regcache *, int) override;
94 void store_registers (struct regcache *, int) override;
96 void prepare_to_store (struct regcache *) override;
98 bool stopped_by_sw_breakpoint () override;
100 bool stopped_by_hw_breakpoint () override;
102 bool stopped_by_watchpoint () override;
104 bool stopped_data_address (CORE_ADDR *) override;
106 bool thread_alive (ptid_t ptid) override;
108 int core_of_thread (ptid_t ptid) override;
110 void update_thread_list () override;
112 const char *extra_thread_info (struct thread_info *) override;
114 const char *pid_to_str (ptid_t) override;
116 ptid_t get_ada_task_ptid (long lwp, long thread) override;
118 void mourn_inferior () override;
120 bool has_all_memory () override { return default_child_has_all_memory (); }
121 bool has_memory () override { return default_child_has_memory (); }
122 bool has_stack () override { return default_child_has_stack (); }
123 bool has_registers () override { return default_child_has_registers (); }
124 bool has_execution (ptid_t ptid) override
125 { return default_child_has_execution (ptid); }
128 /* This module's target-specific operations. */
129 static ravenscar_thread_target ravenscar_ops;
131 static ptid_t ravenscar_active_task (int cpu);
132 static void ravenscar_update_inferior_ptid (void);
133 static int has_ravenscar_runtime (void);
134 static int ravenscar_runtime_initialized (void);
135 static void ravenscar_inferior_created (struct target_ops *target,
138 /* Return nonzero iff PTID corresponds to a ravenscar task. */
141 is_ravenscar_task (ptid_t ptid)
143 /* By construction, ravenscar tasks have their LWP set to zero.
144 Also make sure that the TID is nonzero, as some remotes, when
145 asked for the list of threads, will return the first thread
146 as having its TID set to zero. For instance, TSIM version
147 2.0.48 for LEON3 sends 'm0' as a reply to the 'qfThreadInfo'
148 query, which the remote protocol layer then treats as a thread
149 whose TID is 0. This is obviously not a ravenscar task. */
150 return ptid_get_lwp (ptid) == 0 && ptid_get_tid (ptid) != 0;
153 /* Given PTID, which can be either a ravenscar task or a CPU thread,
154 return which CPU that ptid is running on.
156 This assume that PTID is a valid ptid_t. Otherwise, a gdb_assert
157 will be triggered. */
160 ravenscar_get_thread_base_cpu (ptid_t ptid)
164 if (is_ravenscar_task (ptid))
166 struct ada_task_info *task_info = ada_get_task_info_from_ptid (ptid);
168 gdb_assert (task_info != NULL);
169 base_cpu = task_info->base_cpu;
173 /* We assume that the LWP of the PTID is equal to the CPU number. */
174 base_cpu = ptid_get_lwp (ptid);
180 /* Given a ravenscar task (identified by its ptid_t PTID), return nonzero
181 if this task is the currently active task on the cpu that task is
184 In other words, this function determine which CPU this task is
185 currently running on, and then return nonzero if the CPU in question
186 is executing the code for that task. If that's the case, then
187 that task's registers are in the CPU bank. Otherwise, the task
188 is currently suspended, and its registers have been saved in memory. */
191 ravenscar_task_is_currently_active (ptid_t ptid)
193 ptid_t active_task_ptid
194 = ravenscar_active_task (ravenscar_get_thread_base_cpu (ptid));
196 return ptid_equal (ptid, active_task_ptid);
199 /* Return the CPU thread (as a ptid_t) on which the given ravenscar
202 This is the thread that corresponds to the CPU on which the task
206 get_base_thread_from_ravenscar_task (ptid_t ptid)
210 if (!is_ravenscar_task (ptid))
213 base_cpu = ravenscar_get_thread_base_cpu (ptid);
214 return ptid_build (ptid_get_pid (ptid), base_cpu, 0);
217 /* Fetch the ravenscar running thread from target memory and
218 update inferior_ptid accordingly. */
221 ravenscar_update_inferior_ptid (void)
225 base_ptid = inferior_ptid;
227 gdb_assert (!is_ravenscar_task (inferior_ptid));
228 base_cpu = ravenscar_get_thread_base_cpu (base_ptid);
230 /* If the runtime has not been initialized yet, the inferior_ptid is
231 the only ptid that there is. */
232 if (!ravenscar_runtime_initialized ())
235 /* Make sure we set base_ptid before calling ravenscar_active_task
236 as the latter relies on it. */
237 inferior_ptid = ravenscar_active_task (base_cpu);
238 gdb_assert (!ptid_equal (inferior_ptid, null_ptid));
240 /* The running thread may not have been added to
241 system.tasking.debug's list yet; so ravenscar_update_thread_list
242 may not always add it to the thread list. Add it here. */
243 if (!find_thread_ptid (inferior_ptid))
244 add_thread (inferior_ptid);
247 /* The Ravenscar Runtime exports a symbol which contains the ID of
248 the thread that is currently running. Try to locate that symbol
249 and return its associated minimal symbol.
250 Return NULL if not found. */
252 static struct bound_minimal_symbol
253 get_running_thread_msymbol (void)
255 struct bound_minimal_symbol msym;
257 msym = lookup_minimal_symbol (running_thread_name, NULL, NULL);
259 /* Older versions of the GNAT runtime were using a different
260 (less ideal) name for the symbol where the active thread ID
261 is stored. If we couldn't find the symbol using the latest
262 name, then try the old one. */
263 msym = lookup_minimal_symbol ("running_thread", NULL, NULL);
268 /* Return True if the Ada Ravenscar run-time can be found in the
272 has_ravenscar_runtime (void)
274 struct bound_minimal_symbol msym_ravenscar_runtime_initializer =
275 lookup_minimal_symbol (ravenscar_runtime_initializer, NULL, NULL);
276 struct bound_minimal_symbol msym_known_tasks =
277 lookup_minimal_symbol (known_tasks_name, NULL, NULL);
278 struct bound_minimal_symbol msym_first_task =
279 lookup_minimal_symbol (first_task_name, NULL, NULL);
280 struct bound_minimal_symbol msym_running_thread
281 = get_running_thread_msymbol ();
283 return (msym_ravenscar_runtime_initializer.minsym
284 && (msym_known_tasks.minsym || msym_first_task.minsym)
285 && msym_running_thread.minsym);
288 /* Return True if the Ada Ravenscar run-time can be found in the
289 application, and if it has been initialized on target. */
292 ravenscar_runtime_initialized (void)
294 return (!(ptid_equal (ravenscar_active_task (1), null_ptid)));
297 /* Return the ID of the thread that is currently running.
298 Return 0 if the ID could not be determined. */
301 get_running_thread_id (int cpu)
303 struct bound_minimal_symbol object_msym = get_running_thread_msymbol ();
307 CORE_ADDR object_addr;
308 struct type *builtin_type_void_data_ptr =
309 builtin_type (target_gdbarch ())->builtin_data_ptr;
311 if (!object_msym.minsym)
314 object_size = TYPE_LENGTH (builtin_type_void_data_ptr);
315 object_addr = (BMSYMBOL_VALUE_ADDRESS (object_msym)
316 + (cpu - 1) * object_size);
317 buf_size = object_size;
318 buf = (gdb_byte *) alloca (buf_size);
319 read_memory (object_addr, buf, buf_size);
320 return extract_typed_address (buf, builtin_type_void_data_ptr);
324 ravenscar_thread_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
326 inferior_ptid = base_ptid;
327 beneath ()->resume (base_ptid, step, siggnal);
331 ravenscar_thread_target::wait (ptid_t ptid,
332 struct target_waitstatus *status,
337 inferior_ptid = base_ptid;
338 event_ptid = beneath ()->wait (base_ptid, status, 0);
339 /* Find any new threads that might have been created, and update
340 inferior_ptid to the active thread.
342 Only do it if the program is still alive, though. Otherwise,
343 this causes problems when debugging through the remote protocol,
344 because we might try switching threads (and thus sending packets)
345 after the remote has disconnected. */
346 if (status->kind != TARGET_WAITKIND_EXITED
347 && status->kind != TARGET_WAITKIND_SIGNALLED)
349 inferior_ptid = event_ptid;
350 this->update_thread_list ();
351 ravenscar_update_inferior_ptid ();
353 return inferior_ptid;
356 /* Add the thread associated to the given TASK to the thread list
357 (if the thread has already been added, this is a no-op). */
360 ravenscar_add_thread (struct ada_task_info *task)
362 if (find_thread_ptid (task->ptid) == NULL)
363 add_thread (task->ptid);
367 ravenscar_thread_target::update_thread_list ()
369 ada_build_task_list ();
371 /* Do not clear the thread list before adding the Ada task, to keep
372 the thread that the process stratum has included into it
373 (base_ptid) and the running thread, that may not have been included
374 to system.tasking.debug's list yet. */
376 iterate_over_live_ada_tasks (ravenscar_add_thread);
380 ravenscar_active_task (int cpu)
382 CORE_ADDR tid = get_running_thread_id (cpu);
387 return ptid_build (ptid_get_pid (base_ptid), 0, tid);
391 ravenscar_thread_target::extra_thread_info (thread_info *tp)
393 return "Ravenscar task";
397 ravenscar_thread_target::thread_alive (ptid_t ptid)
399 /* Ravenscar tasks are non-terminating. */
404 ravenscar_thread_target::pid_to_str (ptid_t ptid)
408 snprintf (buf, sizeof (buf), "Thread %#x", (int) ptid_get_tid (ptid));
413 ravenscar_thread_target::fetch_registers (struct regcache *regcache, int regnum)
415 ptid_t ptid = regcache->ptid ();
417 if (ravenscar_runtime_initialized ()
418 && is_ravenscar_task (ptid)
419 && !ravenscar_task_is_currently_active (ptid))
421 struct gdbarch *gdbarch = regcache->arch ();
422 struct ravenscar_arch_ops *arch_ops
423 = gdbarch_ravenscar_ops (gdbarch);
425 arch_ops->to_fetch_registers (regcache, regnum);
428 beneath ()->fetch_registers (regcache, regnum);
432 ravenscar_thread_target::store_registers (struct regcache *regcache,
435 ptid_t ptid = regcache->ptid ();
437 if (ravenscar_runtime_initialized ()
438 && is_ravenscar_task (ptid)
439 && !ravenscar_task_is_currently_active (ptid))
441 struct gdbarch *gdbarch = regcache->arch ();
442 struct ravenscar_arch_ops *arch_ops
443 = gdbarch_ravenscar_ops (gdbarch);
445 beneath ()->store_registers (regcache, regnum);
448 beneath ()->store_registers (regcache, regnum);
452 ravenscar_thread_target::prepare_to_store (struct regcache *regcache)
454 ptid_t ptid = regcache->ptid ();
456 if (ravenscar_runtime_initialized ()
457 && is_ravenscar_task (ptid)
458 && !ravenscar_task_is_currently_active (ptid))
460 struct gdbarch *gdbarch = regcache->arch ();
461 struct ravenscar_arch_ops *arch_ops
462 = gdbarch_ravenscar_ops (gdbarch);
464 beneath ()->prepare_to_store (regcache);
467 beneath ()->prepare_to_store (regcache);
470 /* Implement the to_stopped_by_sw_breakpoint target_ops "method". */
473 ravenscar_thread_target::stopped_by_sw_breakpoint ()
475 ptid_t saved_ptid = inferior_ptid;
478 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
479 result = beneath ()->stopped_by_sw_breakpoint ();
480 inferior_ptid = saved_ptid;
484 /* Implement the to_stopped_by_hw_breakpoint target_ops "method". */
487 ravenscar_thread_target::stopped_by_hw_breakpoint ()
489 ptid_t saved_ptid = inferior_ptid;
492 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
493 result = beneath ()->stopped_by_hw_breakpoint ();
494 inferior_ptid = saved_ptid;
498 /* Implement the to_stopped_by_watchpoint target_ops "method". */
501 ravenscar_thread_target::stopped_by_watchpoint ()
503 ptid_t saved_ptid = inferior_ptid;
506 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
507 result = beneath ()->stopped_by_watchpoint ();
508 inferior_ptid = saved_ptid;
512 /* Implement the to_stopped_data_address target_ops "method". */
515 ravenscar_thread_target::stopped_data_address (CORE_ADDR *addr_p)
517 ptid_t saved_ptid = inferior_ptid;
520 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
521 result = beneath ()->stopped_data_address (addr_p);
522 inferior_ptid = saved_ptid;
527 ravenscar_thread_target::mourn_inferior ()
529 base_ptid = null_ptid;
530 beneath ()->mourn_inferior ();
531 unpush_target (&ravenscar_ops);
534 /* Implement the to_core_of_thread target_ops "method". */
537 ravenscar_thread_target::core_of_thread (ptid_t ptid)
539 ptid_t saved_ptid = inferior_ptid;
542 inferior_ptid = get_base_thread_from_ravenscar_task (saved_ptid);
543 result = beneath ()->core_of_thread (inferior_ptid);
544 inferior_ptid = saved_ptid;
548 /* Observer on inferior_created: push ravenscar thread stratum if needed. */
551 ravenscar_inferior_created (struct target_ops *target, int from_tty)
555 if (!ravenscar_task_support
556 || gdbarch_ravenscar_ops (target_gdbarch ()) == NULL
557 || !has_ravenscar_runtime ())
560 err_msg = ada_get_tcb_types_info ();
563 warning (_("%s. Task/thread support disabled."), err_msg);
567 ravenscar_update_inferior_ptid ();
568 push_target (&ravenscar_ops);
572 ravenscar_thread_target::get_ada_task_ptid (long lwp, long thread)
574 return ptid_build (ptid_get_pid (base_ptid), 0, thread);
577 /* Command-list for the "set/show ravenscar" prefix command. */
578 static struct cmd_list_element *set_ravenscar_list;
579 static struct cmd_list_element *show_ravenscar_list;
581 /* Implement the "set ravenscar" prefix command. */
584 set_ravenscar_command (const char *arg, int from_tty)
586 printf_unfiltered (_(\
587 "\"set ravenscar\" must be followed by the name of a setting.\n"));
588 help_list (set_ravenscar_list, "set ravenscar ", all_commands, gdb_stdout);
591 /* Implement the "show ravenscar" prefix command. */
594 show_ravenscar_command (const char *args, int from_tty)
596 cmd_show_list (show_ravenscar_list, from_tty, "");
599 /* Implement the "show ravenscar task-switching" command. */
602 show_ravenscar_task_switching_command (struct ui_file *file, int from_tty,
603 struct cmd_list_element *c,
606 if (ravenscar_task_support)
607 fprintf_filtered (file, _("\
608 Support for Ravenscar task/thread switching is enabled\n"));
610 fprintf_filtered (file, _("\
611 Support for Ravenscar task/thread switching is disabled\n"));
614 /* Module startup initialization function, automagically called by
618 _initialize_ravenscar (void)
620 base_ptid = null_ptid;
622 /* Notice when the inferior is created in order to push the
623 ravenscar ops if needed. */
624 gdb::observers::inferior_created.attach (ravenscar_inferior_created);
626 add_prefix_cmd ("ravenscar", no_class, set_ravenscar_command,
627 _("Prefix command for changing Ravenscar-specific settings"),
628 &set_ravenscar_list, "set ravenscar ", 0, &setlist);
630 add_prefix_cmd ("ravenscar", no_class, show_ravenscar_command,
631 _("Prefix command for showing Ravenscar-specific settings"),
632 &show_ravenscar_list, "show ravenscar ", 0, &showlist);
634 add_setshow_boolean_cmd ("task-switching", class_obscure,
635 &ravenscar_task_support, _("\
636 Enable or disable support for GNAT Ravenscar tasks"), _("\
637 Show whether support for GNAT Ravenscar tasks is enabled"),
639 Enable or disable support for task/thread switching with the GNAT\n\
640 Ravenscar run-time library for bareboard configuration."),
641 NULL, show_ravenscar_task_switching_command,
642 &set_ravenscar_list, &show_ravenscar_list);