gdb/ravenscar-thread.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Ada Ravenscar thread support.

  2.    Copyright (C) 2004-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "gdbthread.h"
  17. #include "ada-lang.h"
  18. #include "target.h"
  19. #include "inferior.h"
  20. #include "command.h"
  21. #include "ravenscar-thread.h"
  22. #include "observer.h"
  23. #include "gdbcmd.h"
  24. #include "top.h"
  25. #include "regcache.h"
  26. #include "objfiles.h"

  27. /* If non-null, ravenscar task support is enabled.  */
  28. static int ravenscar_task_support = 1;

  29. /* This module's target-specific operations.  */
  30. static struct target_ops ravenscar_ops;

  31. /* Some base target uses a special value for the null PID (exempli gratia
  32.    remote).  */
  33. static ptid_t base_magic_null_ptid;

  34. /* Ptid of the inferior as seen by the process stratum.  */
  35. static ptid_t base_ptid;

  36. static const char running_thread_name[] = "__gnat_running_thread_table";

  37. static const char known_tasks_name[] = "system__tasking__debug__known_tasks";
  38. static const char first_task_name[] = "system__tasking__debug__first_task";

  39. static const char ravenscar_runtime_initializer[] =
  40.   "system__bb__threads__initialize";

  41. static void ravenscar_update_thread_list (struct target_ops *ops);
  42. static ptid_t ravenscar_running_thread (void);
  43. static char *ravenscar_extra_thread_info (struct target_ops *self,
  44.                                           struct thread_info *tp);
  45. static int ravenscar_thread_alive (struct target_ops *ops, ptid_t ptid);
  46. static void ravenscar_fetch_registers (struct target_ops *ops,
  47.                                        struct regcache *regcache, int regnum);
  48. static void ravenscar_store_registers (struct target_ops *ops,
  49.                                        struct regcache *regcache, int regnum);
  50. static void ravenscar_prepare_to_store (struct target_ops *self,
  51.                                         struct regcache *regcache);
  52. static void ravenscar_resume (struct target_ops *ops, ptid_t ptid, int step,
  53.                               enum gdb_signal siggnal);
  54. static void ravenscar_mourn_inferior (struct target_ops *ops);
  55. static void ravenscar_update_inferior_ptid (void);
  56. static int has_ravenscar_runtime (void);
  57. static int ravenscar_runtime_initialized (void);
  58. static void ravenscar_inferior_created (struct target_ops *target,
  59.                                         int from_tty);

  60. /* Fetch the ravenscar running thread from target memory and
  61.    update inferior_ptid accordingly.  */

  62. static void
  63. ravenscar_update_inferior_ptid (void)
  64. {
  65.   base_ptid = inferior_ptid;

  66.   /* If the runtime has not been initialized yet, the inferior_ptid is
  67.      the only ptid that there is.  */
  68.   if (!ravenscar_runtime_initialized ())
  69.     return;

  70.   /* Make sure we set base_ptid before calling ravenscar_running_thread
  71.      as the latter relies on it.  */
  72.   inferior_ptid = ravenscar_running_thread ();
  73.   gdb_assert (!ptid_equal (inferior_ptid, null_ptid));

  74.   /* The running thread may not have been added to
  75.      system.tasking.debug's list yet; so ravenscar_update_thread_list
  76.      may not always add it to the thread list.  Add it here.  */
  77.   if (!find_thread_ptid (inferior_ptid))
  78.     add_thread (inferior_ptid);
  79. }

  80. /* The Ravenscar Runtime exports a symbol which contains the ID of
  81.    the thread that is currently running.  Try to locate that symbol
  82.    and return its associated minimal symbol.
  83.    Return NULL if not found.  */

  84. static struct bound_minimal_symbol
  85. get_running_thread_msymbol (void)
  86. {
  87.   struct bound_minimal_symbol msym;

  88.   msym = lookup_minimal_symbol (running_thread_name, NULL, NULL);
  89.   if (!msym.minsym)
  90.     /* Older versions of the GNAT runtime were using a different
  91.        (less ideal) name for the symbol where the active thread ID
  92.        is stored.  If we couldn't find the symbol using the latest
  93.        name, then try the old one.  */
  94.     msym = lookup_minimal_symbol ("running_thread", NULL, NULL);

  95.   return msym;
  96. }

  97. /* Return True if the Ada Ravenscar run-time can be found in the
  98.    application.  */

  99. static int
  100. has_ravenscar_runtime (void)
  101. {
  102.   struct bound_minimal_symbol msym_ravenscar_runtime_initializer =
  103.     lookup_minimal_symbol (ravenscar_runtime_initializer, NULL, NULL);
  104.   struct bound_minimal_symbol msym_known_tasks =
  105.     lookup_minimal_symbol (known_tasks_name, NULL, NULL);
  106.   struct bound_minimal_symbol msym_first_task =
  107.     lookup_minimal_symbol (first_task_name, NULL, NULL);
  108.   struct bound_minimal_symbol msym_running_thread
  109.     = get_running_thread_msymbol ();

  110.   return (msym_ravenscar_runtime_initializer.minsym
  111.           && (msym_known_tasks.minsym || msym_first_task.minsym)
  112.           && msym_running_thread.minsym);
  113. }

  114. /* Return True if the Ada Ravenscar run-time can be found in the
  115.    application, and if it has been initialized on target.  */

  116. static int
  117. ravenscar_runtime_initialized (void)
  118. {
  119.   return (!(ptid_equal (ravenscar_running_thread (), null_ptid)));
  120. }

  121. /* Return the ID of the thread that is currently running.
  122.    Return 0 if the ID could not be determined.  */

  123. static CORE_ADDR
  124. get_running_thread_id (void)
  125. {
  126.   struct bound_minimal_symbol object_msym = get_running_thread_msymbol ();
  127.   int object_size;
  128.   int buf_size;
  129.   gdb_byte *buf;
  130.   CORE_ADDR object_addr;
  131.   struct type *builtin_type_void_data_ptr =
  132.     builtin_type (target_gdbarch ())->builtin_data_ptr;

  133.   if (!object_msym.minsym)
  134.     return 0;

  135.   object_addr = BMSYMBOL_VALUE_ADDRESS (object_msym);
  136.   object_size = TYPE_LENGTH (builtin_type_void_data_ptr);
  137.   buf_size = object_size;
  138.   buf = alloca (buf_size);
  139.   read_memory (object_addr, buf, buf_size);
  140.   return extract_typed_address (buf, builtin_type_void_data_ptr);
  141. }

  142. static void
  143. ravenscar_resume (struct target_ops *ops, ptid_t ptid, int step,
  144.                   enum gdb_signal siggnal)
  145. {
  146.   struct target_ops *beneath = find_target_beneath (ops);

  147.   inferior_ptid = base_ptid;
  148.   beneath->to_resume (beneath, base_ptid, step, siggnal);
  149. }

  150. static ptid_t
  151. ravenscar_wait (struct target_ops *ops, ptid_t ptid,
  152.                 struct target_waitstatus *status,
  153.                 int options)
  154. {
  155.   struct target_ops *beneath = find_target_beneath (ops);

  156.   inferior_ptid = base_ptid;
  157.   beneath->to_wait (beneath, base_ptid, status, 0);
  158.   /* Find any new threads that might have been created, and update
  159.      inferior_ptid to the active thread.

  160.      Only do it if the program is still alive, though.  Otherwise,
  161.      this causes problems when debugging through the remote protocol,
  162.      because we might try switching threads (and thus sending packets)
  163.      after the remote has disconnected.  */
  164.   if (status->kind != TARGET_WAITKIND_EXITED
  165.       && status->kind != TARGET_WAITKIND_SIGNALLED)
  166.     {
  167.       ravenscar_update_thread_list (ops);
  168.       ravenscar_update_inferior_ptid ();
  169.     }
  170.   return inferior_ptid;
  171. }

  172. /* Add the thread associated to the given TASK to the thread list
  173.    (if the thread has already been added, this is a no-op).  */

  174. static void
  175. ravenscar_add_thread (struct ada_task_info *task)
  176. {
  177.   if (find_thread_ptid (task->ptid) == NULL)
  178.     add_thread (task->ptid);
  179. }

  180. static void
  181. ravenscar_update_thread_list (struct target_ops *ops)
  182. {
  183.   ada_build_task_list ();

  184.   /* Do not clear the thread list before adding the Ada task, to keep
  185.      the thread that the process stratum has included into it
  186.      (base_ptid) and the running thread, that may not have been included
  187.      to system.tasking.debug's list yet.  */

  188.   iterate_over_live_ada_tasks (ravenscar_add_thread);
  189. }

  190. static ptid_t
  191. ravenscar_running_thread (void)
  192. {
  193.   CORE_ADDR tid = get_running_thread_id ();

  194.   if (tid == 0)
  195.     return null_ptid;
  196.   else
  197.     return ptid_build (ptid_get_pid (base_ptid), 0, tid);
  198. }

  199. static char *
  200. ravenscar_extra_thread_info (struct target_ops *self, struct thread_info *tp)
  201. {
  202.   return "Ravenscar task";
  203. }

  204. static int
  205. ravenscar_thread_alive (struct target_ops *ops, ptid_t ptid)
  206. {
  207.   /* Ravenscar tasks are non-terminating.  */
  208.   return 1;
  209. }

  210. static char *
  211. ravenscar_pid_to_str (struct target_ops *ops, ptid_t ptid)
  212. {
  213.   static char buf[30];

  214.   snprintf (buf, sizeof (buf), "Thread %#x", (int) ptid_get_tid (ptid));
  215.   return buf;
  216. }

  217. static void
  218. ravenscar_fetch_registers (struct target_ops *ops,
  219.                            struct regcache *regcache, int regnum)
  220. {
  221.   struct target_ops *beneath = find_target_beneath (ops);

  222.   if (!ravenscar_runtime_initialized ()
  223.       || ptid_equal (inferior_ptid, base_magic_null_ptid)
  224.       || ptid_equal (inferior_ptid, ravenscar_running_thread ()))
  225.     beneath->to_fetch_registers (beneath, regcache, regnum);
  226.   else
  227.     {
  228.       struct gdbarch *gdbarch = get_regcache_arch (regcache);
  229.       struct ravenscar_arch_ops *arch_ops
  230.         = gdbarch_ravenscar_ops (gdbarch);

  231.       arch_ops->to_fetch_registers (regcache, regnum);
  232.     }
  233. }

  234. static void
  235. ravenscar_store_registers (struct target_ops *ops,
  236.                            struct regcache *regcache, int regnum)
  237. {
  238.   struct target_ops *beneath = find_target_beneath (ops);

  239.   if (!ravenscar_runtime_initialized ()
  240.       || ptid_equal (inferior_ptid, base_magic_null_ptid)
  241.       || ptid_equal (inferior_ptid, ravenscar_running_thread ()))
  242.     beneath->to_store_registers (beneath, regcache, regnum);
  243.   else
  244.     {
  245.       struct gdbarch *gdbarch = get_regcache_arch (regcache);
  246.       struct ravenscar_arch_ops *arch_ops
  247.         = gdbarch_ravenscar_ops (gdbarch);

  248.       arch_ops->to_store_registers (regcache, regnum);
  249.     }
  250. }

  251. static void
  252. ravenscar_prepare_to_store (struct target_ops *self,
  253.                             struct regcache *regcache)
  254. {
  255.   struct target_ops *beneath = find_target_beneath (self);

  256.   if (!ravenscar_runtime_initialized ()
  257.       || ptid_equal (inferior_ptid, base_magic_null_ptid)
  258.       || ptid_equal (inferior_ptid, ravenscar_running_thread ()))
  259.     beneath->to_prepare_to_store (beneath, regcache);
  260.   else
  261.     {
  262.       struct gdbarch *gdbarch = get_regcache_arch (regcache);
  263.       struct ravenscar_arch_ops *arch_ops
  264.         = gdbarch_ravenscar_ops (gdbarch);

  265.       arch_ops->to_prepare_to_store (regcache);
  266.     }
  267. }

  268. static void
  269. ravenscar_mourn_inferior (struct target_ops *ops)
  270. {
  271.   struct target_ops *beneath = find_target_beneath (ops);

  272.   base_ptid = null_ptid;
  273.   beneath->to_mourn_inferior (beneath);
  274.   unpush_target (&ravenscar_ops);
  275. }

  276. /* Observer on inferior_created: push ravenscar thread stratum if needed.  */

  277. static void
  278. ravenscar_inferior_created (struct target_ops *target, int from_tty)
  279. {
  280.   struct ravenscar_arch_ops *ops;

  281.   if (!ravenscar_task_support
  282.       || gdbarch_ravenscar_ops (current_inferior ()->gdbarch) == NULL
  283.       || !has_ravenscar_runtime ())
  284.     return;

  285.   base_magic_null_ptid = inferior_ptid;
  286.   ravenscar_update_inferior_ptid ();
  287.   push_target (&ravenscar_ops);
  288. }

  289. static ptid_t
  290. ravenscar_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
  291. {
  292.   return ptid_build (ptid_get_pid (base_ptid), 0, thread);
  293. }

  294. static void
  295. init_ravenscar_thread_ops (void)
  296. {
  297.   ravenscar_ops.to_shortname = "ravenscar";
  298.   ravenscar_ops.to_longname = "Ravenscar tasks.";
  299.   ravenscar_ops.to_doc = "Ravenscar tasks support.";
  300.   ravenscar_ops.to_resume = ravenscar_resume;
  301.   ravenscar_ops.to_wait = ravenscar_wait;
  302.   ravenscar_ops.to_fetch_registers = ravenscar_fetch_registers;
  303.   ravenscar_ops.to_store_registers = ravenscar_store_registers;
  304.   ravenscar_ops.to_prepare_to_store = ravenscar_prepare_to_store;
  305.   ravenscar_ops.to_thread_alive = ravenscar_thread_alive;
  306.   ravenscar_ops.to_update_thread_list = ravenscar_update_thread_list;
  307.   ravenscar_ops.to_pid_to_str = ravenscar_pid_to_str;
  308.   ravenscar_ops.to_extra_thread_info = ravenscar_extra_thread_info;
  309.   ravenscar_ops.to_get_ada_task_ptid = ravenscar_get_ada_task_ptid;
  310.   ravenscar_ops.to_mourn_inferior = ravenscar_mourn_inferior;
  311.   ravenscar_ops.to_has_all_memory = default_child_has_all_memory;
  312.   ravenscar_ops.to_has_memory = default_child_has_memory;
  313.   ravenscar_ops.to_has_stack = default_child_has_stack;
  314.   ravenscar_ops.to_has_registers = default_child_has_registers;
  315.   ravenscar_ops.to_has_execution = default_child_has_execution;
  316.   ravenscar_ops.to_stratum = thread_stratum;
  317.   ravenscar_ops.to_magic = OPS_MAGIC;
  318. }

  319. /* Command-list for the "set/show ravenscar" prefix command.  */
  320. static struct cmd_list_element *set_ravenscar_list;
  321. static struct cmd_list_element *show_ravenscar_list;

  322. /* Implement the "set ravenscar" prefix command.  */

  323. static void
  324. set_ravenscar_command (char *arg, int from_tty)
  325. {
  326.   printf_unfiltered (_(\
  327. "\"set ravenscar\" must be followed by the name of a setting.\n"));
  328.   help_list (set_ravenscar_list, "set ravenscar ", all_commands, gdb_stdout);
  329. }

  330. /* Implement the "show ravenscar" prefix command.  */

  331. static void
  332. show_ravenscar_command (char *args, int from_tty)
  333. {
  334.   cmd_show_list (show_ravenscar_list, from_tty, "");
  335. }

  336. /* Implement the "show ravenscar task-switching" command.  */

  337. static void
  338. show_ravenscar_task_switching_command (struct ui_file *file, int from_tty,
  339.                                        struct cmd_list_element *c,
  340.                                        const char *value)
  341. {
  342.   if (ravenscar_task_support)
  343.     fprintf_filtered (file, _("\
  344. Support for Ravenscar task/thread switching is enabled\n"));
  345.   else
  346.     fprintf_filtered (file, _("\
  347. Support for Ravenscar task/thread switching is disabled\n"));
  348. }

  349. /* Provide a prototype to silence -Wmissing-prototypes.  */
  350. extern void _initialize_ravenscar (void);

  351. /* Module startup initialization function, automagically called by
  352.    init.c.  */

  353. void
  354. _initialize_ravenscar (void)
  355. {
  356.   init_ravenscar_thread_ops ();
  357.   base_ptid = null_ptid;

  358.   /* Notice when the inferior is created in order to push the
  359.      ravenscar ops if needed.  */
  360.   observer_attach_inferior_created (ravenscar_inferior_created);

  361.   complete_target_initialization (&ravenscar_ops);

  362.   add_prefix_cmd ("ravenscar", no_class, set_ravenscar_command,
  363.                   _("Prefix command for changing Ravenscar-specific settings"),
  364.                   &set_ravenscar_list, "set ravenscar ", 0, &setlist);

  365.   add_prefix_cmd ("ravenscar", no_class, show_ravenscar_command,
  366.                   _("Prefix command for showing Ravenscar-specific settings"),
  367.                   &show_ravenscar_list, "show ravenscar ", 0, &showlist);

  368.   add_setshow_boolean_cmd ("task-switching", class_obscure,
  369.                            &ravenscar_task_support, _("\
  370. Enable or disable support for GNAT Ravenscar tasks"), _("\
  371. Show whether support for GNAT Ravenscar tasks is enabled"),
  372.                            _("\
  373. Enable or disable support for task/thread switching with the GNAT\n\
  374. Ravenscar run-time library for bareboard configuration."),
  375.                            NULL, show_ravenscar_task_switching_command,
  376.                            &set_ravenscar_list, &show_ravenscar_list);
  377. }