gdb/ada-tasks.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Copyright (C) 1992-2015 Free Software Foundation, Inc.

  2.    This file is part of GDB.

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

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

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

  13. #include "defs.h"
  14. #include "observer.h"
  15. #include "gdbcmd.h"
  16. #include "target.h"
  17. #include "ada-lang.h"
  18. #include "gdbcore.h"
  19. #include "inferior.h"
  20. #include "gdbthread.h"
  21. #include "progspace.h"
  22. #include "objfiles.h"

  23. /* The name of the array in the GNAT runtime where the Ada Task Control
  24.    Block of each task is stored.  */
  25. #define KNOWN_TASKS_NAME "system__tasking__debug__known_tasks"

  26. /* The maximum number of tasks known to the Ada runtime.  */
  27. static const int MAX_NUMBER_OF_KNOWN_TASKS = 1000;

  28. /* The name of the variable in the GNAT runtime where the head of a task
  29.    chain is saved.  This is an alternate mechanism to find the list of known
  30.    tasks.  */
  31. #define KNOWN_TASKS_LIST "system__tasking__debug__first_task"

  32. enum task_states
  33. {
  34.   Unactivated,
  35.   Runnable,
  36.   Terminated,
  37.   Activator_Sleep,
  38.   Acceptor_Sleep,
  39.   Entry_Caller_Sleep,
  40.   Async_Select_Sleep,
  41.   Delay_Sleep,
  42.   Master_Completion_Sleep,
  43.   Master_Phase_2_Sleep,
  44.   Interrupt_Server_Idle_Sleep,
  45.   Interrupt_Server_Blocked_Interrupt_Sleep,
  46.   Timer_Server_Sleep,
  47.   AST_Server_Sleep,
  48.   Asynchronous_Hold,
  49.   Interrupt_Server_Blocked_On_Event_Flag,
  50.   Activating,
  51.   Acceptor_Delay_Sleep
  52. };

  53. /* A short description corresponding to each possible task state.  */
  54. static const char *task_states[] = {
  55.   N_("Unactivated"),
  56.   N_("Runnable"),
  57.   N_("Terminated"),
  58.   N_("Child Activation Wait"),
  59.   N_("Accept or Select Term"),
  60.   N_("Waiting on entry call"),
  61.   N_("Async Select Wait"),
  62.   N_("Delay Sleep"),
  63.   N_("Child Termination Wait"),
  64.   N_("Wait Child in Term Alt"),
  65.   "",
  66.   "",
  67.   "",
  68.   "",
  69.   N_("Asynchronous Hold"),
  70.   "",
  71.   N_("Activating"),
  72.   N_("Selective Wait")
  73. };

  74. /* A longer description corresponding to each possible task state.  */
  75. static const char *long_task_states[] = {
  76.   N_("Unactivated"),
  77.   N_("Runnable"),
  78.   N_("Terminated"),
  79.   N_("Waiting for child activation"),
  80.   N_("Blocked in accept or select with terminate"),
  81.   N_("Waiting on entry call"),
  82.   N_("Asynchronous Selective Wait"),
  83.   N_("Delay Sleep"),
  84.   N_("Waiting for children termination"),
  85.   N_("Waiting for children in terminate alternative"),
  86.   "",
  87.   "",
  88.   "",
  89.   "",
  90.   N_("Asynchronous Hold"),
  91.   "",
  92.   N_("Activating"),
  93.   N_("Blocked in selective wait statement")
  94. };

  95. /* The index of certain important fields in the Ada Task Control Block
  96.    record and sub-records.  */

  97. struct atcb_fieldnos
  98. {
  99.   /* Fields in record Ada_Task_Control_Block.  */
  100.   int common;
  101.   int entry_calls;
  102.   int atc_nesting_level;

  103.   /* Fields in record Common_ATCB.  */
  104.   int state;
  105.   int parent;
  106.   int priority;
  107.   int image;
  108.   int image_len;     /* This field may be missing.  */
  109.   int activation_link;
  110.   int call;
  111.   int ll;

  112.   /* Fields in Task_Primitives.Private_Data.  */
  113.   int ll_thread;
  114.   int ll_lwp;        /* This field may be missing.  */

  115.   /* Fields in Common_ATCB.Call.all.  */
  116.   int call_self;
  117. };

  118. /* This module's per-program-space data.  */

  119. struct ada_tasks_pspace_data
  120. {
  121.   /* Nonzero if the data has been initialized.  If set to zero,
  122.      it means that the data has either not been initialized, or
  123.      has potentially become stale.  */
  124.   int initialized_p;

  125.   /* The ATCB record type.  */
  126.   struct type *atcb_type;

  127.   /* The ATCB "Common" component type.  */
  128.   struct type *atcb_common_type;

  129.   /* The type of the "ll" field, from the atcb_common_type.  */
  130.   struct type *atcb_ll_type;

  131.   /* The type of the "call" field, from the atcb_common_type.  */
  132.   struct type *atcb_call_type;

  133.   /* The index of various fields in the ATCB record and sub-records.  */
  134.   struct atcb_fieldnos atcb_fieldno;
  135. };

  136. /* Key to our per-program-space data.  */
  137. static const struct program_space_data *ada_tasks_pspace_data_handle;

  138. typedef struct ada_task_info ada_task_info_s;
  139. DEF_VEC_O(ada_task_info_s);

  140. /* The kind of data structure used by the runtime to store the list
  141.    of Ada tasks.  */

  142. enum ada_known_tasks_kind
  143. {
  144.   /* Use this value when we haven't determined which kind of structure
  145.      is being used, or when we need to recompute it.

  146.      We set the value of this enumerate to zero on purpose: This allows
  147.      us to use this enumerate in a structure where setting all fields
  148.      to zero will result in this kind being set to unknown.  */
  149.   ADA_TASKS_UNKNOWN = 0,

  150.   /* This value means that we did not find any task list.  Unless
  151.      there is a bug somewhere, this means that the inferior does not
  152.      use tasking.  */
  153.   ADA_TASKS_NOT_FOUND,

  154.   /* This value means that the task list is stored as an array.
  155.      This is the usual method, as it causes very little overhead.
  156.      But this method is not always used, as it does use a certain
  157.      amount of memory, which might be scarse in certain environments.  */
  158.   ADA_TASKS_ARRAY,

  159.   /* This value means that the task list is stored as a linked list.
  160.      This has more runtime overhead than the array approach, but
  161.      also require less memory when the number of tasks is small.  */
  162.   ADA_TASKS_LIST,
  163. };

  164. /* This module's per-inferior data.  */

  165. struct ada_tasks_inferior_data
  166. {
  167.   /* The type of data structure used by the runtime to store
  168.      the list of Ada tasks.  The value of this field influences
  169.      the interpretation of the known_tasks_addr field below:
  170.        - ADA_TASKS_UNKNOWN: The value of known_tasks_addr hasn't
  171.          been determined yet;
  172.        - ADA_TASKS_NOT_FOUND: The program probably does not use tasking
  173.          and the known_tasks_addr is irrelevant;
  174.        - ADA_TASKS_ARRAY: The known_tasks is an array;
  175.        - ADA_TASKS_LIST: The known_tasks is a list.  */
  176.   enum ada_known_tasks_kind known_tasks_kind;

  177.   /* The address of the known_tasks structure.  This is where
  178.      the runtime stores the information for all Ada tasks.
  179.      The interpretation of this field depends on KNOWN_TASKS_KIND
  180.      above.  */
  181.   CORE_ADDR known_tasks_addr;

  182.   /* Type of elements of the known task.  Usually a pointer.  */
  183.   struct type *known_tasks_element;

  184.   /* Number of elements in the known tasks array.  */
  185.   unsigned int known_tasks_length;

  186.   /* When nonzero, this flag indicates that the task_list field
  187.      below is up to date.  When set to zero, the list has either
  188.      not been initialized, or has potentially become stale.  */
  189.   int task_list_valid_p;

  190.   /* The list of Ada tasks.

  191.      Note: To each task we associate a number that the user can use to
  192.      reference it - this number is printed beside each task in the tasks
  193.      info listing displayed by "info tasks".  This number is equal to
  194.      its index in the vector + 1.  Reciprocally, to compute the index
  195.      of a task in the vector, we need to substract 1 from its number.  */
  196.   VEC(ada_task_info_s) *task_list;
  197. };

  198. /* Key to our per-inferior data.  */
  199. static const struct inferior_data *ada_tasks_inferior_data_handle;

  200. /* Return the ada-tasks module's data for the given program space (PSPACE).
  201.    If none is found, add a zero'ed one now.

  202.    This function always returns a valid object.  */

  203. static struct ada_tasks_pspace_data *
  204. get_ada_tasks_pspace_data (struct program_space *pspace)
  205. {
  206.   struct ada_tasks_pspace_data *data;

  207.   data = program_space_data (pspace, ada_tasks_pspace_data_handle);
  208.   if (data == NULL)
  209.     {
  210.       data = XCNEW (struct ada_tasks_pspace_data);
  211.       set_program_space_data (pspace, ada_tasks_pspace_data_handle, data);
  212.     }

  213.   return data;
  214. }

  215. /* Return the ada-tasks module's data for the given inferior (INF).
  216.    If none is found, add a zero'ed one now.

  217.    This function always returns a valid object.

  218.    Note that we could use an observer of the inferior-created event
  219.    to make sure that the ada-tasks per-inferior data always exists.
  220.    But we prefered this approach, as it avoids this entirely as long
  221.    as the user does not use any of the tasking features.  This is
  222.    quite possible, particularly in the case where the inferior does
  223.    not use tasking.  */

  224. static struct ada_tasks_inferior_data *
  225. get_ada_tasks_inferior_data (struct inferior *inf)
  226. {
  227.   struct ada_tasks_inferior_data *data;

  228.   data = inferior_data (inf, ada_tasks_inferior_data_handle);
  229.   if (data == NULL)
  230.     {
  231.       data = XCNEW (struct ada_tasks_inferior_data);
  232.       set_inferior_data (inf, ada_tasks_inferior_data_handle, data);
  233.     }

  234.   return data;
  235. }

  236. /* Return the task number of the task whose ptid is PTID, or zero
  237.    if the task could not be found.  */

  238. int
  239. ada_get_task_number (ptid_t ptid)
  240. {
  241.   int i;
  242.   struct inferior *inf = find_inferior_ptid (ptid);
  243.   struct ada_tasks_inferior_data *data;

  244.   gdb_assert (inf != NULL);
  245.   data = get_ada_tasks_inferior_data (inf);

  246.   for (i = 0; i < VEC_length (ada_task_info_s, data->task_list); i++)
  247.     if (ptid_equal (VEC_index (ada_task_info_s, data->task_list, i)->ptid,
  248.                     ptid))
  249.       return i + 1;

  250.   return 0/* No matching task found.  */
  251. }

  252. /* Return the task number of the task running in inferior INF which
  253.    matches TASK_ID , or zero if the task could not be found.  */

  254. static int
  255. get_task_number_from_id (CORE_ADDR task_id, struct inferior *inf)
  256. {
  257.   struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
  258.   int i;

  259.   for (i = 0; i < VEC_length (ada_task_info_s, data->task_list); i++)
  260.     {
  261.       struct ada_task_info *task_info =
  262.         VEC_index (ada_task_info_s, data->task_list, i);

  263.       if (task_info->task_id == task_id)
  264.         return i + 1;
  265.     }

  266.   /* Task not found.  Return 0.  */
  267.   return 0;
  268. }

  269. /* Return non-zero if TASK_NUM is a valid task number.  */

  270. int
  271. valid_task_id (int task_num)
  272. {
  273.   struct ada_tasks_inferior_data *data;

  274.   ada_build_task_list ();
  275.   data = get_ada_tasks_inferior_data (current_inferior ());
  276.   return (task_num > 0
  277.           && task_num <= VEC_length (ada_task_info_s, data->task_list));
  278. }

  279. /* Return non-zero iff the task STATE corresponds to a non-terminated
  280.    task state.  */

  281. static int
  282. ada_task_is_alive (struct ada_task_info *task_info)
  283. {
  284.   return (task_info->state != Terminated);
  285. }

  286. /* Call the ITERATOR function once for each Ada task that hasn't been
  287.    terminated yet.  */

  288. void
  289. iterate_over_live_ada_tasks (ada_task_list_iterator_ftype *iterator)
  290. {
  291.   int i, nb_tasks;
  292.   struct ada_task_info *task;
  293.   struct ada_tasks_inferior_data *data;

  294.   ada_build_task_list ();
  295.   data = get_ada_tasks_inferior_data (current_inferior ());
  296.   nb_tasks = VEC_length (ada_task_info_s, data->task_list);

  297.   for (i = 0; i < nb_tasks; i++)
  298.     {
  299.       task = VEC_index (ada_task_info_s, data->task_list, i);
  300.       if (!ada_task_is_alive (task))
  301.         continue;
  302.       iterator (task);
  303.     }
  304. }

  305. /* Extract the contents of the value as a string whose length is LENGTH,
  306.    and store the result in DEST.  */

  307. static void
  308. value_as_string (char *dest, struct value *val, int length)
  309. {
  310.   memcpy (dest, value_contents (val), length);
  311.   dest[length] = '\0';
  312. }

  313. /* Extract the string image from the fat string corresponding to VAL,
  314.    and store it in DEST.  If the string length is greater than MAX_LEN,
  315.    then truncate the result to the first MAX_LEN characters of the fat
  316.    string.  */

  317. static void
  318. read_fat_string_value (char *dest, struct value *val, int max_len)
  319. {
  320.   struct value *array_val;
  321.   struct value *bounds_val;
  322.   int len;

  323.   /* The following variables are made static to avoid recomputing them
  324.      each time this function is called.  */
  325.   static int initialize_fieldnos = 1;
  326.   static int array_fieldno;
  327.   static int bounds_fieldno;
  328.   static int upper_bound_fieldno;

  329.   /* Get the index of the fields that we will need to read in order
  330.      to extract the string from the fat string.  */
  331.   if (initialize_fieldnos)
  332.     {
  333.       struct type *type = value_type (val);
  334.       struct type *bounds_type;

  335.       array_fieldno = ada_get_field_index (type, "P_ARRAY", 0);
  336.       bounds_fieldno = ada_get_field_index (type, "P_BOUNDS", 0);

  337.       bounds_type = TYPE_FIELD_TYPE (type, bounds_fieldno);
  338.       if (TYPE_CODE (bounds_type) == TYPE_CODE_PTR)
  339.         bounds_type = TYPE_TARGET_TYPE (bounds_type);
  340.       if (TYPE_CODE (bounds_type) != TYPE_CODE_STRUCT)
  341.         error (_("Unknown task name format. Aborting"));
  342.       upper_bound_fieldno = ada_get_field_index (bounds_type, "UB0", 0);

  343.       initialize_fieldnos = 0;
  344.     }

  345.   /* Get the size of the task image by checking the value of the bounds.
  346.      The lower bound is always 1, so we only need to read the upper bound.  */
  347.   bounds_val = value_ind (value_field (val, bounds_fieldno));
  348.   len = value_as_long (value_field (bounds_val, upper_bound_fieldno));

  349.   /* Make sure that we do not read more than max_len characters...  */
  350.   if (len > max_len)
  351.     len = max_len;

  352.   /* Extract LEN characters from the fat string.  */
  353.   array_val = value_ind (value_field (val, array_fieldno));
  354.   read_memory (value_address (array_val), (gdb_byte *) dest, len);

  355.   /* Add the NUL character to close the string.  */
  356.   dest[len] = '\0';
  357. }

  358. /* Get from the debugging information the type description of all types
  359.    related to the Ada Task Control Block that will be needed in order to
  360.    read the list of known tasks in the Ada runtime.  Also return the
  361.    associated ATCB_FIELDNOS.

  362.    Error handling:  Any data missing from the debugging info will cause
  363.    an error to be raised, and none of the return values to be set.
  364.    Users of this function can depend on the fact that all or none of the
  365.    return values will be set.  */

  366. static void
  367. get_tcb_types_info (void)
  368. {
  369.   struct type *type;
  370.   struct type *common_type;
  371.   struct type *ll_type;
  372.   struct type *call_type;
  373.   struct atcb_fieldnos fieldnos;
  374.   struct ada_tasks_pspace_data *pspace_data;

  375.   const char *atcb_name = "system__tasking__ada_task_control_block___XVE";
  376.   const char *atcb_name_fixed = "system__tasking__ada_task_control_block";
  377.   const char *common_atcb_name = "system__tasking__common_atcb";
  378.   const char *private_data_name = "system__task_primitives__private_data";
  379.   const char *entry_call_record_name = "system__tasking__entry_call_record";

  380.   /* ATCB symbols may be found in several compilation units.  As we
  381.      are only interested in one instance, use standard (literal,
  382.      C-like) lookups to get the first match.  */

  383.   struct symbol *atcb_sym =
  384.     lookup_symbol_in_language (atcb_name, NULL, STRUCT_DOMAIN,
  385.                                language_c, NULL);
  386.   const struct symbol *common_atcb_sym =
  387.     lookup_symbol_in_language (common_atcb_name, NULL, STRUCT_DOMAIN,
  388.                                language_c, NULL);
  389.   const struct symbol *private_data_sym =
  390.     lookup_symbol_in_language (private_data_name, NULL, STRUCT_DOMAIN,
  391.                                language_c, NULL);
  392.   const struct symbol *entry_call_record_sym =
  393.     lookup_symbol_in_language (entry_call_record_name, NULL, STRUCT_DOMAIN,
  394.                                language_c, NULL);

  395.   if (atcb_sym == NULL || atcb_sym->type == NULL)
  396.     {
  397.       /* In Ravenscar run-time libs, the  ATCB does not have a dynamic
  398.          size, so the symbol name differs.  */
  399.       atcb_sym = lookup_symbol_in_language (atcb_name_fixed, NULL,
  400.                                             STRUCT_DOMAIN, language_c, NULL);

  401.       if (atcb_sym == NULL || atcb_sym->type == NULL)
  402.         error (_("Cannot find Ada_Task_Control_Block type. Aborting"));

  403.       type = atcb_sym->type;
  404.     }
  405.   else
  406.     {
  407.       /* Get a static representation of the type record
  408.          Ada_Task_Control_Block.  */
  409.       type = atcb_sym->type;
  410.       type = ada_template_to_fixed_record_type_1 (type, NULL, 0, NULL, 0);
  411.     }

  412.   if (common_atcb_sym == NULL || common_atcb_sym->type == NULL)
  413.     error (_("Cannot find Common_ATCB type. Aborting"));
  414.   if (private_data_sym == NULL || private_data_sym->type == NULL)
  415.     error (_("Cannot find Private_Data type. Aborting"));
  416.   if (entry_call_record_sym == NULL || entry_call_record_sym->type == NULL)
  417.     error (_("Cannot find Entry_Call_Record type. Aborting"));

  418.   /* Get the type for Ada_Task_Control_Block.Common.  */
  419.   common_type = common_atcb_sym->type;

  420.   /* Get the type for Ada_Task_Control_Bloc.Common.Call.LL.  */
  421.   ll_type = private_data_sym->type;

  422.   /* Get the type for Common_ATCB.Call.all.  */
  423.   call_type = entry_call_record_sym->type;

  424.   /* Get the field indices.  */
  425.   fieldnos.common = ada_get_field_index (type, "common", 0);
  426.   fieldnos.entry_calls = ada_get_field_index (type, "entry_calls", 1);
  427.   fieldnos.atc_nesting_level =
  428.     ada_get_field_index (type, "atc_nesting_level", 1);
  429.   fieldnos.state = ada_get_field_index (common_type, "state", 0);
  430.   fieldnos.parent = ada_get_field_index (common_type, "parent", 1);
  431.   fieldnos.priority = ada_get_field_index (common_type, "base_priority", 0);
  432.   fieldnos.image = ada_get_field_index (common_type, "task_image", 1);
  433.   fieldnos.image_len = ada_get_field_index (common_type, "task_image_len", 1);
  434.   fieldnos.activation_link = ada_get_field_index (common_type,
  435.                                                   "activation_link", 1);
  436.   fieldnos.call = ada_get_field_index (common_type, "call", 1);
  437.   fieldnos.ll = ada_get_field_index (common_type, "ll", 0);
  438.   fieldnos.ll_thread = ada_get_field_index (ll_type, "thread", 0);
  439.   fieldnos.ll_lwp = ada_get_field_index (ll_type, "lwp", 1);
  440.   fieldnos.call_self = ada_get_field_index (call_type, "self", 0);

  441.   /* On certain platforms such as x86-windows, the "lwp" field has been
  442.      named "thread_id".  This field will likely be renamed in the future,
  443.      but we need to support both possibilities to avoid an unnecessary
  444.      dependency on a recent compiler.  We therefore try locating the
  445.      "thread_id" field in place of the "lwp" field if we did not find
  446.      the latter.  */
  447.   if (fieldnos.ll_lwp < 0)
  448.     fieldnos.ll_lwp = ada_get_field_index (ll_type, "thread_id", 1);

  449.   /* Set all the out parameters all at once, now that we are certain
  450.      that there are no potential error() anymore.  */
  451.   pspace_data = get_ada_tasks_pspace_data (current_program_space);
  452.   pspace_data->initialized_p = 1;
  453.   pspace_data->atcb_type = type;
  454.   pspace_data->atcb_common_type = common_type;
  455.   pspace_data->atcb_ll_type = ll_type;
  456.   pspace_data->atcb_call_type = call_type;
  457.   pspace_data->atcb_fieldno = fieldnos;
  458. }

  459. /* Build the PTID of the task from its COMMON_VALUE, which is the "Common"
  460.    component of its ATCB record.  This PTID needs to match the PTID used
  461.    by the thread layer.  */

  462. static ptid_t
  463. ptid_from_atcb_common (struct value *common_value)
  464. {
  465.   long thread = 0;
  466.   CORE_ADDR lwp = 0;
  467.   struct value *ll_value;
  468.   ptid_t ptid;
  469.   const struct ada_tasks_pspace_data *pspace_data
  470.     = get_ada_tasks_pspace_data (current_program_space);

  471.   ll_value = value_field (common_value, pspace_data->atcb_fieldno.ll);

  472.   if (pspace_data->atcb_fieldno.ll_lwp >= 0)
  473.     lwp = value_as_address (value_field (ll_value,
  474.                                          pspace_data->atcb_fieldno.ll_lwp));
  475.   thread = value_as_long (value_field (ll_value,
  476.                                        pspace_data->atcb_fieldno.ll_thread));

  477.   ptid = target_get_ada_task_ptid (lwp, thread);

  478.   return ptid;
  479. }

  480. /* Read the ATCB data of a given task given its TASK_ID (which is in practice
  481.    the address of its assocated ATCB record), and store the result inside
  482.    TASK_INFO.  */

  483. static void
  484. read_atcb (CORE_ADDR task_id, struct ada_task_info *task_info)
  485. {
  486.   struct value *tcb_value;
  487.   struct value *common_value;
  488.   struct value *atc_nesting_level_value;
  489.   struct value *entry_calls_value;
  490.   struct value *entry_calls_value_element;
  491.   int called_task_fieldno = -1;
  492.   static const char ravenscar_task_name[] = "Ravenscar task";
  493.   const struct ada_tasks_pspace_data *pspace_data
  494.     = get_ada_tasks_pspace_data (current_program_space);

  495.   if (!pspace_data->initialized_p)
  496.     get_tcb_types_info ();

  497.   tcb_value = value_from_contents_and_address (pspace_data->atcb_type,
  498.                                                NULL, task_id);
  499.   common_value = value_field (tcb_value, pspace_data->atcb_fieldno.common);

  500.   /* Fill in the task_id.  */

  501.   task_info->task_id = task_id;

  502.   /* Compute the name of the task.

  503.      Depending on the GNAT version used, the task image is either a fat
  504.      string, or a thin array of characters.  Older versions of GNAT used
  505.      to use fat strings, and therefore did not need an extra field in
  506.      the ATCB to store the string length.  For efficiency reasons, newer
  507.      versions of GNAT replaced the fat string by a static buffer, but this
  508.      also required the addition of a new field named "Image_Len" containing
  509.      the length of the task name.  The method used to extract the task name
  510.      is selected depending on the existence of this field.

  511.      In some run-time libs (e.g. Ravenscar), the name is not in the ATCB;
  512.      we may want to get it from the first user frame of the stack.  For now,
  513.      we just give a dummy name.  */

  514.   if (pspace_data->atcb_fieldno.image_len == -1)
  515.     {
  516.       if (pspace_data->atcb_fieldno.image >= 0)
  517.         read_fat_string_value (task_info->name,
  518.                                value_field (common_value,
  519.                                             pspace_data->atcb_fieldno.image),
  520.                                sizeof (task_info->name) - 1);
  521.       else
  522.         {
  523.           struct bound_minimal_symbol msym;

  524.           msym = lookup_minimal_symbol_by_pc (task_id);
  525.           if (msym.minsym)
  526.             {
  527.               const char *full_name = MSYMBOL_LINKAGE_NAME (msym.minsym);
  528.               const char *task_name = full_name;
  529.               const char *p;

  530.               /* Strip the prefix.  */
  531.               for (p = full_name; *p; p++)
  532.                 if (p[0] == '_' && p[1] == '_')
  533.                   task_name = p + 2;

  534.               /* Copy the task name.  */
  535.               strncpy (task_info->name, task_name, sizeof (task_info->name));
  536.               task_info->name[sizeof (task_info->name) - 1] = 0;
  537.             }
  538.           else
  539.             {
  540.               /* No symbol found.  Use a default name.  */
  541.               strcpy (task_info->name, ravenscar_task_name);
  542.             }
  543.         }
  544.     }
  545.   else
  546.     {
  547.       int len = value_as_long
  548.                   (value_field (common_value,
  549.                                 pspace_data->atcb_fieldno.image_len));

  550.       value_as_string (task_info->name,
  551.                        value_field (common_value,
  552.                                     pspace_data->atcb_fieldno.image),
  553.                        len);
  554.     }

  555.   /* Compute the task state and priority.  */

  556.   task_info->state =
  557.     value_as_long (value_field (common_value,
  558.                                 pspace_data->atcb_fieldno.state));
  559.   task_info->priority =
  560.     value_as_long (value_field (common_value,
  561.                                 pspace_data->atcb_fieldno.priority));

  562.   /* If the ATCB contains some information about the parent task,
  563.      then compute it as well.  Otherwise, zero.  */

  564.   if (pspace_data->atcb_fieldno.parent >= 0)
  565.     task_info->parent =
  566.       value_as_address (value_field (common_value,
  567.                                      pspace_data->atcb_fieldno.parent));
  568.   else
  569.     task_info->parent = 0;


  570.   /* If the ATCB contains some information about entry calls, then
  571.      compute the "called_task" as well.  Otherwise, zero.  */

  572.   if (pspace_data->atcb_fieldno.atc_nesting_level > 0
  573.       && pspace_data->atcb_fieldno.entry_calls > 0)
  574.     {
  575.       /* Let My_ATCB be the Ada task control block of a task calling the
  576.          entry of another task; then the Task_Id of the called task is
  577.          in My_ATCB.Entry_Calls (My_ATCB.ATC_Nesting_Level).Called_Task.  */
  578.       atc_nesting_level_value =
  579.         value_field (tcb_value, pspace_data->atcb_fieldno.atc_nesting_level);
  580.       entry_calls_value =
  581.         ada_coerce_to_simple_array_ptr
  582.           (value_field (tcb_value, pspace_data->atcb_fieldno.entry_calls));
  583.       entry_calls_value_element =
  584.         value_subscript (entry_calls_value,
  585.                          value_as_long (atc_nesting_level_value));
  586.       called_task_fieldno =
  587.         ada_get_field_index (value_type (entry_calls_value_element),
  588.                              "called_task", 0);
  589.       task_info->called_task =
  590.         value_as_address (value_field (entry_calls_value_element,
  591.                                        called_task_fieldno));
  592.     }
  593.   else
  594.     {
  595.       task_info->called_task = 0;
  596.     }

  597.   /* If the ATCB cotnains some information about RV callers,
  598.      then compute the "caller_task".  Otherwise, zero.  */

  599.   task_info->caller_task = 0;
  600.   if (pspace_data->atcb_fieldno.call >= 0)
  601.     {
  602.       /* Get the ID of the caller task from Common_ATCB.Call.all.Self.
  603.          If Common_ATCB.Call is null, then there is no caller.  */
  604.       const CORE_ADDR call =
  605.         value_as_address (value_field (common_value,
  606.                                        pspace_data->atcb_fieldno.call));
  607.       struct value *call_val;

  608.       if (call != 0)
  609.         {
  610.           call_val =
  611.             value_from_contents_and_address (pspace_data->atcb_call_type,
  612.                                              NULL, call);
  613.           task_info->caller_task =
  614.             value_as_address
  615.               (value_field (call_val, pspace_data->atcb_fieldno.call_self));
  616.         }
  617.     }

  618.   /* And finally, compute the task ptid.  Note that there are situations
  619.      where this cannot be determined:
  620.        - The task is no longer alive - the ptid is irrelevant;
  621.        - We are debugging a core file - the thread is not always
  622.          completely preserved for us to link back a task to its
  623.          underlying thread.  Since we do not support task switching
  624.          when debugging core files anyway, we don't need to compute
  625.          that task ptid.
  626.      In either case, we don't need that ptid, and it is just good enough
  627.      to set it to null_ptid.  */

  628.   if (target_has_execution && ada_task_is_alive (task_info))
  629.     task_info->ptid = ptid_from_atcb_common (common_value);
  630.   else
  631.     task_info->ptid = null_ptid;
  632. }

  633. /* Read the ATCB info of the given task (identified by TASK_ID), and
  634.    add the result to the given inferior's TASK_LIST.  */

  635. static void
  636. add_ada_task (CORE_ADDR task_id, struct inferior *inf)
  637. {
  638.   struct ada_task_info task_info;
  639.   struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);

  640.   read_atcb (task_id, &task_info);
  641.   VEC_safe_push (ada_task_info_s, data->task_list, &task_info);
  642. }

  643. /* Read the Known_Tasks array from the inferior memory, and store
  644.    it in the current inferior's TASK_LIST.  Return non-zero upon success.  */

  645. static int
  646. read_known_tasks_array (struct ada_tasks_inferior_data *data)
  647. {
  648.   const int target_ptr_byte = TYPE_LENGTH (data->known_tasks_element);
  649.   const int known_tasks_size = target_ptr_byte * data->known_tasks_length;
  650.   gdb_byte *known_tasks = alloca (known_tasks_size);
  651.   int i;

  652.   /* Build a new list by reading the ATCBs from the Known_Tasks array
  653.      in the Ada runtime.  */
  654.   read_memory (data->known_tasks_addr, known_tasks, known_tasks_size);
  655.   for (i = 0; i < data->known_tasks_length; i++)
  656.     {
  657.       CORE_ADDR task_id =
  658.         extract_typed_address (known_tasks + i * target_ptr_byte,
  659.                                data->known_tasks_element);

  660.       if (task_id != 0)
  661.         add_ada_task (task_id, current_inferior ());
  662.     }

  663.   return 1;
  664. }

  665. /* Read the known tasks from the inferior memory, and store it in
  666.    the current inferior's TASK_LIST.  Return non-zero upon success.  */

  667. static int
  668. read_known_tasks_list (struct ada_tasks_inferior_data *data)
  669. {
  670.   const int target_ptr_byte = TYPE_LENGTH (data->known_tasks_element);
  671.   gdb_byte *known_tasks = alloca (target_ptr_byte);
  672.   CORE_ADDR task_id;
  673.   const struct ada_tasks_pspace_data *pspace_data
  674.     = get_ada_tasks_pspace_data (current_program_space);

  675.   /* Sanity check.  */
  676.   if (pspace_data->atcb_fieldno.activation_link < 0)
  677.     return 0;

  678.   /* Build a new list by reading the ATCBs.  Read head of the list.  */
  679.   read_memory (data->known_tasks_addr, known_tasks, target_ptr_byte);
  680.   task_id = extract_typed_address (known_tasks, data->known_tasks_element);
  681.   while (task_id != 0)
  682.     {
  683.       struct value *tcb_value;
  684.       struct value *common_value;

  685.       add_ada_task (task_id, current_inferior ());

  686.       /* Read the chain.  */
  687.       tcb_value = value_from_contents_and_address (pspace_data->atcb_type,
  688.                                                    NULL, task_id);
  689.       common_value = value_field (tcb_value, pspace_data->atcb_fieldno.common);
  690.       task_id = value_as_address
  691.                   (value_field (common_value,
  692.                                 pspace_data->atcb_fieldno.activation_link));
  693.     }

  694.   return 1;
  695. }

  696. /* Set all fields of the current inferior ada-tasks data pointed by DATA.
  697.    Do nothing if those fields are already set and still up to date.  */

  698. static void
  699. ada_tasks_inferior_data_sniffer (struct ada_tasks_inferior_data *data)
  700. {
  701.   struct bound_minimal_symbol msym;
  702.   struct symbol *sym;

  703.   /* Return now if already set.  */
  704.   if (data->known_tasks_kind != ADA_TASKS_UNKNOWN)
  705.     return;

  706.   /* Try array.  */

  707.   msym = lookup_minimal_symbol (KNOWN_TASKS_NAME, NULL, NULL);
  708.   if (msym.minsym != NULL)
  709.     {
  710.       data->known_tasks_kind = ADA_TASKS_ARRAY;
  711.       data->known_tasks_addr = BMSYMBOL_VALUE_ADDRESS (msym);

  712.       /* Try to get pointer type and array length from the symtab.  */
  713.       sym = lookup_symbol_in_language (KNOWN_TASKS_NAME, NULL, VAR_DOMAIN,
  714.                                        language_c, NULL);
  715.       if (sym != NULL)
  716.         {
  717.           /* Validate.  */
  718.           struct type *type = check_typedef (SYMBOL_TYPE (sym));
  719.           struct type *eltype = NULL;
  720.           struct type *idxtype = NULL;

  721.           if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
  722.             eltype = check_typedef (TYPE_TARGET_TYPE (type));
  723.           if (eltype != NULL
  724.               && TYPE_CODE (eltype) == TYPE_CODE_PTR)
  725.             idxtype = check_typedef (TYPE_INDEX_TYPE (type));
  726.           if (idxtype != NULL
  727.               && !TYPE_LOW_BOUND_UNDEFINED (idxtype)
  728.               && !TYPE_HIGH_BOUND_UNDEFINED (idxtype))
  729.             {
  730.               data->known_tasks_element = eltype;
  731.               data->known_tasks_length =
  732.                 TYPE_HIGH_BOUND (idxtype) - TYPE_LOW_BOUND (idxtype) + 1;
  733.               return;
  734.             }
  735.         }

  736.       /* Fallback to default values.  The runtime may have been stripped (as
  737.          in some distributions), but it is likely that the executable still
  738.          contains debug information on the task type (due to implicit with of
  739.          Ada.Tasking).  */
  740.       data->known_tasks_element =
  741.         builtin_type (target_gdbarch ())->builtin_data_ptr;
  742.       data->known_tasks_length = MAX_NUMBER_OF_KNOWN_TASKS;
  743.       return;
  744.     }


  745.   /* Try list.  */

  746.   msym = lookup_minimal_symbol (KNOWN_TASKS_LIST, NULL, NULL);
  747.   if (msym.minsym != NULL)
  748.     {
  749.       data->known_tasks_kind = ADA_TASKS_LIST;
  750.       data->known_tasks_addr = BMSYMBOL_VALUE_ADDRESS (msym);
  751.       data->known_tasks_length = 1;

  752.       sym = lookup_symbol_in_language (KNOWN_TASKS_LIST, NULL, VAR_DOMAIN,
  753.                                        language_c, NULL);
  754.       if (sym != NULL && SYMBOL_VALUE_ADDRESS (sym) != 0)
  755.         {
  756.           /* Validate.  */
  757.           struct type *type = check_typedef (SYMBOL_TYPE (sym));

  758.           if (TYPE_CODE (type) == TYPE_CODE_PTR)
  759.             {
  760.               data->known_tasks_element = type;
  761.               return;
  762.             }
  763.         }

  764.       /* Fallback to default values.  */
  765.       data->known_tasks_element =
  766.         builtin_type (target_gdbarch ())->builtin_data_ptr;
  767.       data->known_tasks_length = 1;
  768.       return;
  769.     }

  770.   /* Can't find tasks.  */

  771.   data->known_tasks_kind = ADA_TASKS_NOT_FOUND;
  772.   data->known_tasks_addr = 0;
  773. }

  774. /* Read the known tasks from the current inferior's memory, and store it
  775.    in the current inferior's data TASK_LIST.
  776.    Return non-zero upon success.  */

  777. static int
  778. read_known_tasks (void)
  779. {
  780.   struct ada_tasks_inferior_data *data =
  781.     get_ada_tasks_inferior_data (current_inferior ());

  782.   /* Step 1: Clear the current list, if necessary.  */
  783.   VEC_truncate (ada_task_info_s, data->task_list, 0);

  784.   /* Step 2: do the real work.
  785.      If the application does not use task, then no more needs to be done.
  786.      It is important to have the task list cleared (see above) before we
  787.      return, as we don't want a stale task list to be used...  This can
  788.      happen for instance when debugging a non-multitasking program after
  789.      having debugged a multitasking one.  */
  790.   ada_tasks_inferior_data_sniffer (data);
  791.   gdb_assert (data->known_tasks_kind != ADA_TASKS_UNKNOWN);

  792.   switch (data->known_tasks_kind)
  793.     {
  794.       case ADA_TASKS_NOT_FOUND: /* Tasking not in use in inferior.  */
  795.         return 0;
  796.       case ADA_TASKS_ARRAY:
  797.         return read_known_tasks_array (data);
  798.       case ADA_TASKS_LIST:
  799.         return read_known_tasks_list (data);
  800.     }

  801.   /* Step 3: Set task_list_valid_p, to avoid re-reading the Known_Tasks
  802.      array unless needed.  Then report a success.  */
  803.   data->task_list_valid_p = 1;

  804.   return 1;
  805. }

  806. /* Build the task_list by reading the Known_Tasks array from
  807.    the inferior, and return the number of tasks in that list
  808.    (zero means that the program is not using tasking at all).  */

  809. int
  810. ada_build_task_list (void)
  811. {
  812.   struct ada_tasks_inferior_data *data;

  813.   if (!target_has_stack)
  814.     error (_("Cannot inspect Ada tasks when program is not running"));

  815.   data = get_ada_tasks_inferior_data (current_inferior ());
  816.   if (!data->task_list_valid_p)
  817.     read_known_tasks ();

  818.   return VEC_length (ada_task_info_s, data->task_list);
  819. }

  820. /* Print a table providing a short description of all Ada tasks
  821.    running inside inferior INF.  If ARG_STR is set, it will be
  822.    interpreted as a task number, and the table will be limited to
  823.    that task only.  */

  824. void
  825. print_ada_task_info (struct ui_out *uiout,
  826.                      char *arg_str,
  827.                      struct inferior *inf)
  828. {
  829.   struct ada_tasks_inferior_data *data;
  830.   int taskno, nb_tasks;
  831.   int taskno_arg = 0;
  832.   struct cleanup *old_chain;
  833.   int nb_columns;

  834.   if (ada_build_task_list () == 0)
  835.     {
  836.       ui_out_message (uiout, 0,
  837.                       _("Your application does not use any Ada tasks.\n"));
  838.       return;
  839.     }

  840.   if (arg_str != NULL && arg_str[0] != '\0')
  841.     taskno_arg = value_as_long (parse_and_eval (arg_str));

  842.   if (ui_out_is_mi_like_p (uiout))
  843.     /* In GDB/MI mode, we want to provide the thread ID corresponding
  844.        to each task.  This allows clients to quickly find the thread
  845.        associated to any task, which is helpful for commands that
  846.        take a --thread argument.  However, in order to be able to
  847.        provide that thread ID, the thread list must be up to date
  848.        first.  */
  849.     target_update_thread_list ();

  850.   data = get_ada_tasks_inferior_data (inf);

  851.   /* Compute the number of tasks that are going to be displayed
  852.      in the output.  If an argument was given, there will be
  853.      at most 1 entry.  Otherwise, there will be as many entries
  854.      as we have tasks.  */
  855.   if (taskno_arg)
  856.     {
  857.       if (taskno_arg > 0
  858.           && taskno_arg <= VEC_length (ada_task_info_s, data->task_list))
  859.         nb_tasks = 1;
  860.       else
  861.         nb_tasks = 0;
  862.     }
  863.   else
  864.     nb_tasks = VEC_length (ada_task_info_s, data->task_list);

  865.   nb_columns = ui_out_is_mi_like_p (uiout) ? 8 : 7;
  866.   old_chain = make_cleanup_ui_out_table_begin_end (uiout, nb_columns,
  867.                                                    nb_tasks, "tasks");
  868.   ui_out_table_header (uiout, 1, ui_left, "current", "");
  869.   ui_out_table_header (uiout, 3, ui_right, "id", "ID");
  870.   ui_out_table_header (uiout, 9, ui_right, "task-id", "TID");
  871.   /* The following column is provided in GDB/MI mode only because
  872.      it is only really useful in that mode, and also because it
  873.      allows us to keep the CLI output shorter and more compact.  */
  874.   if (ui_out_is_mi_like_p (uiout))
  875.     ui_out_table_header (uiout, 4, ui_right, "thread-id", "");
  876.   ui_out_table_header (uiout, 4, ui_right, "parent-id", "P-ID");
  877.   ui_out_table_header (uiout, 3, ui_right, "priority", "Pri");
  878.   ui_out_table_header (uiout, 22, ui_left, "state", "State");
  879.   /* Use ui_noalign for the last column, to prevent the CLI uiout
  880.      from printing an extra space at the end of each row.  This
  881.      is a bit of a hack, but does get the job done.  */
  882.   ui_out_table_header (uiout, 1, ui_noalign, "name", "Name");
  883.   ui_out_table_body (uiout);

  884.   for (taskno = 1;
  885.        taskno <= VEC_length (ada_task_info_s, data->task_list);
  886.        taskno++)
  887.     {
  888.       const struct ada_task_info *const task_info =
  889.         VEC_index (ada_task_info_s, data->task_list, taskno - 1);
  890.       int parent_id;
  891.       struct cleanup *chain2;

  892.       gdb_assert (task_info != NULL);

  893.       /* If the user asked for the output to be restricted
  894.          to one task only, and this is not the task, skip
  895.          to the next one.  */
  896.       if (taskno_arg && taskno != taskno_arg)
  897.         continue;

  898.       chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  899.       /* Print a star if this task is the current task (or the task
  900.          currently selected).  */
  901.       if (ptid_equal (task_info->ptid, inferior_ptid))
  902.         ui_out_field_string (uiout, "current", "*");
  903.       else
  904.         ui_out_field_skip (uiout, "current");

  905.       /* Print the task number.  */
  906.       ui_out_field_int (uiout, "id", taskno);

  907.       /* Print the Task ID.  */
  908.       ui_out_field_fmt (uiout, "task-id", "%9lx", (long) task_info->task_id);

  909.       /* Print the associated Thread ID.  */
  910.       if (ui_out_is_mi_like_p (uiout))
  911.         {
  912.           const int thread_id = pid_to_thread_id (task_info->ptid);

  913.           if (thread_id != 0)
  914.             ui_out_field_int (uiout, "thread-id", thread_id);
  915.           else
  916.             /* This should never happen unless there is a bug somewhere,
  917.                but be resilient when that happens.  */
  918.             ui_out_field_skip (uiout, "thread-id");
  919.         }

  920.       /* Print the ID of the parent task.  */
  921.       parent_id = get_task_number_from_id (task_info->parent, inf);
  922.       if (parent_id)
  923.         ui_out_field_int (uiout, "parent-id", parent_id);
  924.       else
  925.         ui_out_field_skip (uiout, "parent-id");

  926.       /* Print the base priority of the task.  */
  927.       ui_out_field_int (uiout, "priority", task_info->priority);

  928.       /* Print the task current state.  */
  929.       if (task_info->caller_task)
  930.         ui_out_field_fmt (uiout, "state",
  931.                           _("Accepting RV with %-4d"),
  932.                           get_task_number_from_id (task_info->caller_task,
  933.                                                    inf));
  934.       else if (task_info->state == Entry_Caller_Sleep
  935.                && task_info->called_task)
  936.         ui_out_field_fmt (uiout, "state",
  937.                           _("Waiting on RV with %-3d"),
  938.                           get_task_number_from_id (task_info->called_task,
  939.                                                    inf));
  940.       else
  941.         ui_out_field_string (uiout, "state", task_states[task_info->state]);

  942.       /* Finally, print the task name.  */
  943.       ui_out_field_fmt (uiout, "name",
  944.                         "%s",
  945.                         task_info->name[0] != '\0' ? task_info->name
  946.                                                    : _("<no name>"));

  947.       ui_out_text (uiout, "\n");
  948.       do_cleanups (chain2);
  949.     }

  950.   do_cleanups (old_chain);
  951. }

  952. /* Print a detailed description of the Ada task whose ID is TASKNO_STR
  953.    for the given inferior (INF).  */

  954. static void
  955. info_task (struct ui_out *uiout, char *taskno_str, struct inferior *inf)
  956. {
  957.   const int taskno = value_as_long (parse_and_eval (taskno_str));
  958.   struct ada_task_info *task_info;
  959.   int parent_taskno = 0;
  960.   struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);

  961.   if (ada_build_task_list () == 0)
  962.     {
  963.       ui_out_message (uiout, 0,
  964.                       _("Your application does not use any Ada tasks.\n"));
  965.       return;
  966.     }

  967.   if (taskno <= 0 || taskno > VEC_length (ada_task_info_s, data->task_list))
  968.     error (_("Task ID %d not known.  Use the \"info tasks\" command to\n"
  969.              "see the IDs of currently known tasks"), taskno);
  970.   task_info = VEC_index (ada_task_info_s, data->task_list, taskno - 1);

  971.   /* Print the Ada task ID.  */
  972.   printf_filtered (_("Ada Task: %s\n"),
  973.                    paddress (target_gdbarch (), task_info->task_id));

  974.   /* Print the name of the task.  */
  975.   if (task_info->name[0] != '\0')
  976.     printf_filtered (_("Name: %s\n"), task_info->name);
  977.   else
  978.     printf_filtered (_("<no name>\n"));

  979.   /* Print the TID and LWP.  */
  980.   printf_filtered (_("Thread: %#lx\n"), ptid_get_tid (task_info->ptid));
  981.   printf_filtered (_("LWP: %#lx\n"), ptid_get_lwp (task_info->ptid));

  982.   /* Print who is the parent (if any).  */
  983.   if (task_info->parent != 0)
  984.     parent_taskno = get_task_number_from_id (task_info->parent, inf);
  985.   if (parent_taskno)
  986.     {
  987.       struct ada_task_info *parent =
  988.         VEC_index (ada_task_info_s, data->task_list, parent_taskno - 1);

  989.       printf_filtered (_("Parent: %d"), parent_taskno);
  990.       if (parent->name[0] != '\0')
  991.         printf_filtered (" (%s)", parent->name);
  992.       printf_filtered ("\n");
  993.     }
  994.   else
  995.     printf_filtered (_("No parent\n"));

  996.   /* Print the base priority.  */
  997.   printf_filtered (_("Base Priority: %d\n"), task_info->priority);

  998.   /* print the task current state.  */
  999.   {
  1000.     int target_taskno = 0;

  1001.     if (task_info->caller_task)
  1002.       {
  1003.         target_taskno = get_task_number_from_id (task_info->caller_task, inf);
  1004.         printf_filtered (_("State: Accepting rendezvous with %d"),
  1005.                          target_taskno);
  1006.       }
  1007.     else if (task_info->state == Entry_Caller_Sleep && task_info->called_task)
  1008.       {
  1009.         target_taskno = get_task_number_from_id (task_info->called_task, inf);
  1010.         printf_filtered (_("State: Waiting on task %d's entry"),
  1011.                          target_taskno);
  1012.       }
  1013.     else
  1014.       printf_filtered (_("State: %s"), _(long_task_states[task_info->state]));

  1015.     if (target_taskno)
  1016.       {
  1017.         struct ada_task_info *target_task_info =
  1018.           VEC_index (ada_task_info_s, data->task_list, target_taskno - 1);

  1019.         if (target_task_info->name[0] != '\0')
  1020.           printf_filtered (" (%s)", target_task_info->name);
  1021.       }

  1022.     printf_filtered ("\n");
  1023.   }
  1024. }

  1025. /* If ARG is empty or null, then print a list of all Ada tasks.
  1026.    Otherwise, print detailed information about the task whose ID
  1027.    is ARG.

  1028.    Does nothing if the program doesn't use Ada tasking.  */

  1029. static void
  1030. info_tasks_command (char *arg, int from_tty)
  1031. {
  1032.   struct ui_out *uiout = current_uiout;

  1033.   if (arg == NULL || *arg == '\0')
  1034.     print_ada_task_info (uiout, NULL, current_inferior ());
  1035.   else
  1036.     info_task (uiout, arg, current_inferior ());
  1037. }

  1038. /* Print a message telling the user id of the current task.
  1039.    This function assumes that tasking is in use in the inferior.  */

  1040. static void
  1041. display_current_task_id (void)
  1042. {
  1043.   const int current_task = ada_get_task_number (inferior_ptid);

  1044.   if (current_task == 0)
  1045.     printf_filtered (_("[Current task is unknown]\n"));
  1046.   else
  1047.     printf_filtered (_("[Current task is %d]\n"), current_task);
  1048. }

  1049. /* Parse and evaluate TIDSTR into a task id, and try to switch to
  1050.    that task.  Print an error message if the task switch failed.  */

  1051. static void
  1052. task_command_1 (char *taskno_str, int from_tty, struct inferior *inf)
  1053. {
  1054.   const int taskno = value_as_long (parse_and_eval (taskno_str));
  1055.   struct ada_task_info *task_info;
  1056.   struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);

  1057.   if (taskno <= 0 || taskno > VEC_length (ada_task_info_s, data->task_list))
  1058.     error (_("Task ID %d not known.  Use the \"info tasks\" command to\n"
  1059.              "see the IDs of currently known tasks"), taskno);
  1060.   task_info = VEC_index (ada_task_info_s, data->task_list, taskno - 1);

  1061.   if (!ada_task_is_alive (task_info))
  1062.     error (_("Cannot switch to task %d: Task is no longer running"), taskno);

  1063.   /* On some platforms, the thread list is not updated until the user
  1064.      performs a thread-related operation (by using the "info threads"
  1065.      command, for instance).  So this thread list may not be up to date
  1066.      when the user attempts this task switch.  Since we cannot switch
  1067.      to the thread associated to our task if GDB does not know about
  1068.      that thread, we need to make sure that any new threads gets added
  1069.      to the thread list.  */
  1070.   target_update_thread_list ();

  1071.   /* Verify that the ptid of the task we want to switch to is valid
  1072.      (in other words, a ptid that GDB knows about).  Otherwise, we will
  1073.      cause an assertion failure later on, when we try to determine
  1074.      the ptid associated thread_info data.  We should normally never
  1075.      encounter such an error, but the wrong ptid can actually easily be
  1076.      computed if target_get_ada_task_ptid has not been implemented for
  1077.      our target (yet).  Rather than cause an assertion error in that case,
  1078.      it's nicer for the user to just refuse to perform the task switch.  */
  1079.   if (!find_thread_ptid (task_info->ptid))
  1080.     error (_("Unable to compute thread ID for task %d.\n"
  1081.              "Cannot switch to this task."),
  1082.            taskno);

  1083.   switch_to_thread (task_info->ptid);
  1084.   ada_find_printable_frame (get_selected_frame (NULL));
  1085.   printf_filtered (_("[Switching to task %d]\n"), taskno);
  1086.   print_stack_frame (get_selected_frame (NULL),
  1087.                      frame_relative_level (get_selected_frame (NULL)),
  1088.                      SRC_AND_LOC, 1);
  1089. }


  1090. /* Print the ID of the current task if TASKNO_STR is empty or NULL.
  1091.    Otherwise, switch to the task indicated by TASKNO_STR.  */

  1092. static void
  1093. task_command (char *taskno_str, int from_tty)
  1094. {
  1095.   struct ui_out *uiout = current_uiout;

  1096.   if (ada_build_task_list () == 0)
  1097.     {
  1098.       ui_out_message (uiout, 0,
  1099.                       _("Your application does not use any Ada tasks.\n"));
  1100.       return;
  1101.     }

  1102.   if (taskno_str == NULL || taskno_str[0] == '\0')
  1103.     display_current_task_id ();
  1104.   else
  1105.     {
  1106.       /* Task switching in core files doesn't work, either because:
  1107.            1. Thread support is not implemented with core files
  1108.            2. Thread support is implemented, but the thread IDs created
  1109.               after having read the core file are not the same as the ones
  1110.               that were used during the program life, before the crash.
  1111.               As a consequence, there is no longer a way for the debugger
  1112.               to find the associated thead ID of any given Ada task.
  1113.          So, instead of attempting a task switch without giving the user
  1114.          any clue as to what might have happened, just error-out with
  1115.          a message explaining that this feature is not supported.  */
  1116.       if (!target_has_execution)
  1117.         error (_("\
  1118. Task switching not supported when debugging from core files\n\
  1119. (use thread support instead)"));
  1120.       task_command_1 (taskno_str, from_tty, current_inferior ());
  1121.     }
  1122. }

  1123. /* Indicate that the given inferior's task list may have changed,
  1124.    so invalidate the cache.  */

  1125. static void
  1126. ada_task_list_changed (struct inferior *inf)
  1127. {
  1128.   struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);

  1129.   data->task_list_valid_p = 0;
  1130. }

  1131. /* Invalidate the per-program-space data.  */

  1132. static void
  1133. ada_tasks_invalidate_pspace_data (struct program_space *pspace)
  1134. {
  1135.   get_ada_tasks_pspace_data (pspace)->initialized_p = 0;
  1136. }

  1137. /* Invalidate the per-inferior data.  */

  1138. static void
  1139. ada_tasks_invalidate_inferior_data (struct inferior *inf)
  1140. {
  1141.   struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);

  1142.   data->known_tasks_kind = ADA_TASKS_UNKNOWN;
  1143.   data->task_list_valid_p = 0;
  1144. }

  1145. /* The 'normal_stop' observer notification callback.  */

  1146. static void
  1147. ada_tasks_normal_stop_observer (struct bpstats *unused_args, int unused_args2)
  1148. {
  1149.   /* The inferior has been resumed, and just stopped. This means that
  1150.      our task_list needs to be recomputed before it can be used again.  */
  1151.   ada_task_list_changed (current_inferior ());
  1152. }

  1153. /* A routine to be called when the objfiles have changed.  */

  1154. static void
  1155. ada_tasks_new_objfile_observer (struct objfile *objfile)
  1156. {
  1157.   struct inferior *inf;

  1158.   /* Invalidate the relevant data in our program-space data.  */

  1159.   if (objfile == NULL)
  1160.     {
  1161.       /* All objfiles are being cleared, so we should clear all
  1162.          our caches for all program spaces.  */
  1163.       struct program_space *pspace;

  1164.       for (pspace = program_spaces; pspace != NULL; pspace = pspace->next)
  1165.         ada_tasks_invalidate_pspace_data (pspace);
  1166.     }
  1167.   else
  1168.     {
  1169.       /* The associated program-space data might have changed after
  1170.          this objfile was added.  Invalidate all cached data.  */
  1171.       ada_tasks_invalidate_pspace_data (objfile->pspace);
  1172.     }

  1173.   /* Invalidate the per-inferior cache for all inferiors using
  1174.      this objfile (or, in other words, for all inferiors who have
  1175.      the same program-space as the objfile's program space).
  1176.      If all objfiles are being cleared (OBJFILE is NULL), then
  1177.      clear the caches for all inferiors.  */

  1178.   for (inf = inferior_list; inf != NULL; inf = inf->next)
  1179.     if (objfile == NULL || inf->pspace == objfile->pspace)
  1180.       ada_tasks_invalidate_inferior_data (inf);
  1181. }

  1182. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1183. extern initialize_file_ftype _initialize_tasks;

  1184. void
  1185. _initialize_tasks (void)
  1186. {
  1187.   ada_tasks_pspace_data_handle = register_program_space_data ();
  1188.   ada_tasks_inferior_data_handle = register_inferior_data ();

  1189.   /* Attach various observers.  */
  1190.   observer_attach_normal_stop (ada_tasks_normal_stop_observer);
  1191.   observer_attach_new_objfile (ada_tasks_new_objfile_observer);

  1192.   /* Some new commands provided by this module.  */
  1193.   add_info ("tasks", info_tasks_command,
  1194.             _("Provide information about all known Ada tasks"));
  1195.   add_cmd ("task", class_run, task_command,
  1196.            _("Use this command to switch between Ada tasks.\n\
  1197. Without argument, this command simply prints the current task ID"),
  1198.            &cmdlist);
  1199. }