Automatic date update in version.in
[binutils-gdb.git] / gdb / ada-tasks.c
blob864c5cfdf1c686538cc7715ce267aad83b6abffd
1 /* Copyright (C) 1992-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "observable.h"
19 #include "gdbcmd.h"
20 #include "target.h"
21 #include "ada-lang.h"
22 #include "gdbcore.h"
23 #include "inferior.h"
24 #include "gdbthread.h"
25 #include "progspace.h"
26 #include "objfiles.h"
27 #include "cli/cli-style.h"
29 static int ada_build_task_list ();
31 /* The name of the array in the GNAT runtime where the Ada Task Control
32 Block of each task is stored. */
33 #define KNOWN_TASKS_NAME "system__tasking__debug__known_tasks"
35 /* The maximum number of tasks known to the Ada runtime. */
36 static const int MAX_NUMBER_OF_KNOWN_TASKS = 1000;
38 /* The name of the variable in the GNAT runtime where the head of a task
39 chain is saved. This is an alternate mechanism to find the list of known
40 tasks. */
41 #define KNOWN_TASKS_LIST "system__tasking__debug__first_task"
43 enum task_states
45 Unactivated,
46 Runnable,
47 Terminated,
48 Activator_Sleep,
49 Acceptor_Sleep,
50 Entry_Caller_Sleep,
51 Async_Select_Sleep,
52 Delay_Sleep,
53 Master_Completion_Sleep,
54 Master_Phase_2_Sleep,
55 Interrupt_Server_Idle_Sleep,
56 Interrupt_Server_Blocked_Interrupt_Sleep,
57 Timer_Server_Sleep,
58 AST_Server_Sleep,
59 Asynchronous_Hold,
60 Interrupt_Server_Blocked_On_Event_Flag,
61 Activating,
62 Acceptor_Delay_Sleep
65 /* A short description corresponding to each possible task state. */
66 static const char * const task_states[] = {
67 N_("Unactivated"),
68 N_("Runnable"),
69 N_("Terminated"),
70 N_("Child Activation Wait"),
71 N_("Accept or Select Term"),
72 N_("Waiting on entry call"),
73 N_("Async Select Wait"),
74 N_("Delay Sleep"),
75 N_("Child Termination Wait"),
76 N_("Wait Child in Term Alt"),
77 "",
78 "",
79 "",
80 "",
81 N_("Asynchronous Hold"),
82 "",
83 N_("Activating"),
84 N_("Selective Wait")
87 /* Return a string representing the task state. */
88 static const char *
89 get_state (unsigned value)
91 if (value >= 0
92 && value <= ARRAY_SIZE (task_states)
93 && task_states[value][0] != '\0')
94 return _(task_states[value]);
96 static char buffer[100];
97 xsnprintf (buffer, sizeof (buffer), _("Unknown task state: %d"), value);
98 return buffer;
101 /* A longer description corresponding to each possible task state. */
102 static const char * const long_task_states[] = {
103 N_("Unactivated"),
104 N_("Runnable"),
105 N_("Terminated"),
106 N_("Waiting for child activation"),
107 N_("Blocked in accept or select with terminate"),
108 N_("Waiting on entry call"),
109 N_("Asynchronous Selective Wait"),
110 N_("Delay Sleep"),
111 N_("Waiting for children termination"),
112 N_("Waiting for children in terminate alternative"),
117 N_("Asynchronous Hold"),
119 N_("Activating"),
120 N_("Blocked in selective wait statement")
123 /* Return a string representing the task state. This uses the long
124 descriptions. */
125 static const char *
126 get_long_state (unsigned value)
128 if (value >= 0
129 && value <= ARRAY_SIZE (long_task_states)
130 && long_task_states[value][0] != '\0')
131 return _(long_task_states[value]);
133 static char buffer[100];
134 xsnprintf (buffer, sizeof (buffer), _("Unknown task state: %d"), value);
135 return buffer;
138 /* The index of certain important fields in the Ada Task Control Block
139 record and sub-records. */
141 struct atcb_fieldnos
143 /* Fields in record Ada_Task_Control_Block. */
144 int common;
145 int entry_calls;
146 int atc_nesting_level;
148 /* Fields in record Common_ATCB. */
149 int state;
150 int parent;
151 int priority;
152 int image;
153 int image_len; /* This field may be missing. */
154 int activation_link;
155 int call;
156 int ll;
157 int base_cpu;
159 /* Fields in Task_Primitives.Private_Data. */
160 int ll_thread;
161 int ll_lwp; /* This field may be missing. */
163 /* Fields in Common_ATCB.Call.all. */
164 int call_self;
167 /* This module's per-program-space data. */
169 struct ada_tasks_pspace_data
171 /* Nonzero if the data has been initialized. If set to zero,
172 it means that the data has either not been initialized, or
173 has potentially become stale. */
174 int initialized_p = 0;
176 /* The ATCB record type. */
177 struct type *atcb_type = nullptr;
179 /* The ATCB "Common" component type. */
180 struct type *atcb_common_type = nullptr;
182 /* The type of the "ll" field, from the atcb_common_type. */
183 struct type *atcb_ll_type = nullptr;
185 /* The type of the "call" field, from the atcb_common_type. */
186 struct type *atcb_call_type = nullptr;
188 /* The index of various fields in the ATCB record and sub-records. */
189 struct atcb_fieldnos atcb_fieldno {};
191 /* On some systems, gdbserver applies an offset to the CPU that is
192 reported. */
193 unsigned int cpu_id_offset = 0;
196 /* Key to our per-program-space data. */
197 static const registry<program_space>::key<ada_tasks_pspace_data>
198 ada_tasks_pspace_data_handle;
200 /* The kind of data structure used by the runtime to store the list
201 of Ada tasks. */
203 enum ada_known_tasks_kind
205 /* Use this value when we haven't determined which kind of structure
206 is being used, or when we need to recompute it.
208 We set the value of this enumerate to zero on purpose: This allows
209 us to use this enumerate in a structure where setting all fields
210 to zero will result in this kind being set to unknown. */
211 ADA_TASKS_UNKNOWN = 0,
213 /* This value means that we did not find any task list. Unless
214 there is a bug somewhere, this means that the inferior does not
215 use tasking. */
216 ADA_TASKS_NOT_FOUND,
218 /* This value means that the task list is stored as an array.
219 This is the usual method, as it causes very little overhead.
220 But this method is not always used, as it does use a certain
221 amount of memory, which might be scarse in certain environments. */
222 ADA_TASKS_ARRAY,
224 /* This value means that the task list is stored as a linked list.
225 This has more runtime overhead than the array approach, but
226 also require less memory when the number of tasks is small. */
227 ADA_TASKS_LIST,
230 /* This module's per-inferior data. */
232 struct ada_tasks_inferior_data
234 /* The type of data structure used by the runtime to store
235 the list of Ada tasks. The value of this field influences
236 the interpretation of the known_tasks_addr field below:
237 - ADA_TASKS_UNKNOWN: The value of known_tasks_addr hasn't
238 been determined yet;
239 - ADA_TASKS_NOT_FOUND: The program probably does not use tasking
240 and the known_tasks_addr is irrelevant;
241 - ADA_TASKS_ARRAY: The known_tasks is an array;
242 - ADA_TASKS_LIST: The known_tasks is a list. */
243 enum ada_known_tasks_kind known_tasks_kind = ADA_TASKS_UNKNOWN;
245 /* The address of the known_tasks structure. This is where
246 the runtime stores the information for all Ada tasks.
247 The interpretation of this field depends on KNOWN_TASKS_KIND
248 above. */
249 CORE_ADDR known_tasks_addr = 0;
251 /* Type of elements of the known task. Usually a pointer. */
252 struct type *known_tasks_element = nullptr;
254 /* Number of elements in the known tasks array. */
255 unsigned int known_tasks_length = 0;
257 /* When nonzero, this flag indicates that the task_list field
258 below is up to date. When set to zero, the list has either
259 not been initialized, or has potentially become stale. */
260 bool task_list_valid_p = false;
262 /* The list of Ada tasks.
264 Note: To each task we associate a number that the user can use to
265 reference it - this number is printed beside each task in the tasks
266 info listing displayed by "info tasks". This number is equal to
267 its index in the vector + 1. Reciprocally, to compute the index
268 of a task in the vector, we need to substract 1 from its number. */
269 std::vector<ada_task_info> task_list;
272 /* Key to our per-inferior data. */
273 static const registry<inferior>::key<ada_tasks_inferior_data>
274 ada_tasks_inferior_data_handle;
276 /* Return a string with TASKNO followed by the task name if TASK_INFO
277 contains a name. */
279 static std::string
280 task_to_str (int taskno, const ada_task_info *task_info)
282 if (task_info->name[0] == '\0')
283 return string_printf ("%d", taskno);
284 else
285 return string_printf ("%d \"%s\"", taskno, task_info->name);
288 /* Return the ada-tasks module's data for the given program space (PSPACE).
289 If none is found, add a zero'ed one now.
291 This function always returns a valid object. */
293 static struct ada_tasks_pspace_data *
294 get_ada_tasks_pspace_data (struct program_space *pspace)
296 struct ada_tasks_pspace_data *data;
298 data = ada_tasks_pspace_data_handle.get (pspace);
299 if (data == NULL)
300 data = ada_tasks_pspace_data_handle.emplace (pspace);
302 return data;
305 /* Return the ada-tasks module's data for the given inferior (INF).
306 If none is found, add a zero'ed one now.
308 This function always returns a valid object.
310 Note that we could use an observer of the inferior-created event
311 to make sure that the ada-tasks per-inferior data always exists.
312 But we preferred this approach, as it avoids this entirely as long
313 as the user does not use any of the tasking features. This is
314 quite possible, particularly in the case where the inferior does
315 not use tasking. */
317 static struct ada_tasks_inferior_data *
318 get_ada_tasks_inferior_data (struct inferior *inf)
320 struct ada_tasks_inferior_data *data;
322 data = ada_tasks_inferior_data_handle.get (inf);
323 if (data == NULL)
324 data = ada_tasks_inferior_data_handle.emplace (inf);
326 return data;
329 /* Return the task number of the task whose thread is THREAD, or zero
330 if the task could not be found. */
333 ada_get_task_number (thread_info *thread)
335 struct inferior *inf = thread->inf;
336 struct ada_tasks_inferior_data *data;
338 gdb_assert (inf != NULL);
339 data = get_ada_tasks_inferior_data (inf);
341 for (int i = 0; i < data->task_list.size (); i++)
342 if (data->task_list[i].ptid == thread->ptid)
343 return i + 1;
345 return 0; /* No matching task found. */
348 /* Return the task number of the task running in inferior INF which
349 matches TASK_ID , or zero if the task could not be found. */
351 static int
352 get_task_number_from_id (CORE_ADDR task_id, struct inferior *inf)
354 struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
356 for (int i = 0; i < data->task_list.size (); i++)
358 if (data->task_list[i].task_id == task_id)
359 return i + 1;
362 /* Task not found. Return 0. */
363 return 0;
366 /* Return non-zero if TASK_NUM is a valid task number. */
369 valid_task_id (int task_num)
371 struct ada_tasks_inferior_data *data;
373 ada_build_task_list ();
374 data = get_ada_tasks_inferior_data (current_inferior ());
375 return task_num > 0 && task_num <= data->task_list.size ();
378 /* Return non-zero iff the task STATE corresponds to a non-terminated
379 task state. */
381 static int
382 ada_task_is_alive (const struct ada_task_info *task_info)
384 return (task_info->state != Terminated);
387 /* Search through the list of known tasks for the one whose ptid is
388 PTID, and return it. Return NULL if the task was not found. */
390 struct ada_task_info *
391 ada_get_task_info_from_ptid (ptid_t ptid)
393 struct ada_tasks_inferior_data *data;
395 ada_build_task_list ();
396 data = get_ada_tasks_inferior_data (current_inferior ());
398 for (ada_task_info &task : data->task_list)
400 if (task.ptid == ptid)
401 return &task;
404 return NULL;
407 /* Call the ITERATOR function once for each Ada task that hasn't been
408 terminated yet. */
410 void
411 iterate_over_live_ada_tasks (ada_task_list_iterator_ftype iterator)
413 struct ada_tasks_inferior_data *data;
415 ada_build_task_list ();
416 data = get_ada_tasks_inferior_data (current_inferior ());
418 for (ada_task_info &task : data->task_list)
420 if (!ada_task_is_alive (&task))
421 continue;
422 iterator (&task);
426 /* Extract the contents of the value as a string whose length is LENGTH,
427 and store the result in DEST. */
429 static void
430 value_as_string (char *dest, struct value *val, int length)
432 memcpy (dest, val->contents ().data (), length);
433 dest[length] = '\0';
436 /* Extract the string image from the fat string corresponding to VAL,
437 and store it in DEST. If the string length is greater than MAX_LEN,
438 then truncate the result to the first MAX_LEN characters of the fat
439 string. */
441 static void
442 read_fat_string_value (char *dest, struct value *val, int max_len)
444 struct value *array_val;
445 struct value *bounds_val;
446 int len;
448 /* The following variables are made static to avoid recomputing them
449 each time this function is called. */
450 static int initialize_fieldnos = 1;
451 static int array_fieldno;
452 static int bounds_fieldno;
453 static int upper_bound_fieldno;
455 /* Get the index of the fields that we will need to read in order
456 to extract the string from the fat string. */
457 if (initialize_fieldnos)
459 struct type *type = val->type ();
460 struct type *bounds_type;
462 array_fieldno = ada_get_field_index (type, "P_ARRAY", 0);
463 bounds_fieldno = ada_get_field_index (type, "P_BOUNDS", 0);
465 bounds_type = type->field (bounds_fieldno).type ();
466 if (bounds_type->code () == TYPE_CODE_PTR)
467 bounds_type = bounds_type->target_type ();
468 if (bounds_type->code () != TYPE_CODE_STRUCT)
469 error (_("Unknown task name format. Aborting"));
470 upper_bound_fieldno = ada_get_field_index (bounds_type, "UB0", 0);
472 initialize_fieldnos = 0;
475 /* Get the size of the task image by checking the value of the bounds.
476 The lower bound is always 1, so we only need to read the upper bound. */
477 bounds_val = value_ind (value_field (val, bounds_fieldno));
478 len = value_as_long (value_field (bounds_val, upper_bound_fieldno));
480 /* Make sure that we do not read more than max_len characters... */
481 if (len > max_len)
482 len = max_len;
484 /* Extract LEN characters from the fat string. */
485 array_val = value_ind (value_field (val, array_fieldno));
486 read_memory (array_val->address (), (gdb_byte *) dest, len);
488 /* Add the NUL character to close the string. */
489 dest[len] = '\0';
492 /* Get, from the debugging information, the type description of all types
493 related to the Ada Task Control Block that are needed in order to
494 read the list of known tasks in the Ada runtime. If all of the info
495 needed to do so is found, then save that info in the module's per-
496 program-space data, and return NULL. Otherwise, if any information
497 cannot be found, leave the per-program-space data untouched, and
498 return an error message explaining what was missing (that error
499 message does NOT need to be deallocated). */
501 const char *
502 ada_get_tcb_types_info (void)
504 struct type *type;
505 struct type *common_type;
506 struct type *ll_type;
507 struct type *call_type;
508 struct atcb_fieldnos fieldnos;
509 struct ada_tasks_pspace_data *pspace_data;
511 const char *atcb_name = "system__tasking__ada_task_control_block___XVE";
512 const char *atcb_name_fixed = "system__tasking__ada_task_control_block";
513 const char *common_atcb_name = "system__tasking__common_atcb";
514 const char *private_data_name = "system__task_primitives__private_data";
515 const char *entry_call_record_name = "system__tasking__entry_call_record";
517 /* ATCB symbols may be found in several compilation units. As we
518 are only interested in one instance, use standard (literal,
519 C-like) lookups to get the first match. */
521 struct symbol *atcb_sym =
522 lookup_symbol_in_language (atcb_name, NULL, SEARCH_TYPE_DOMAIN,
523 language_c, NULL).symbol;
524 const struct symbol *common_atcb_sym =
525 lookup_symbol_in_language (common_atcb_name, NULL, SEARCH_TYPE_DOMAIN,
526 language_c, NULL).symbol;
527 const struct symbol *private_data_sym =
528 lookup_symbol_in_language (private_data_name, NULL, SEARCH_TYPE_DOMAIN,
529 language_c, NULL).symbol;
530 const struct symbol *entry_call_record_sym =
531 lookup_symbol_in_language (entry_call_record_name, NULL,
532 SEARCH_TYPE_DOMAIN,
533 language_c, NULL).symbol;
535 if (atcb_sym == NULL || atcb_sym->type () == NULL)
537 /* In Ravenscar run-time libs, the ATCB does not have a dynamic
538 size, so the symbol name differs. */
539 atcb_sym = lookup_symbol_in_language (atcb_name_fixed, NULL,
540 SEARCH_TYPE_DOMAIN, language_c,
541 NULL).symbol;
543 if (atcb_sym == NULL || atcb_sym->type () == NULL)
544 return _("Cannot find Ada_Task_Control_Block type");
546 type = atcb_sym->type ();
548 else
550 /* Get a static representation of the type record
551 Ada_Task_Control_Block. */
552 type = atcb_sym->type ();
553 type = ada_template_to_fixed_record_type_1 (type, NULL, 0, NULL, 0);
556 if (common_atcb_sym == NULL || common_atcb_sym->type () == NULL)
557 return _("Cannot find Common_ATCB type");
558 if (private_data_sym == NULL || private_data_sym->type ()== NULL)
559 return _("Cannot find Private_Data type");
560 if (entry_call_record_sym == NULL || entry_call_record_sym->type () == NULL)
561 return _("Cannot find Entry_Call_Record type");
563 /* Get the type for Ada_Task_Control_Block.Common. */
564 common_type = common_atcb_sym->type ();
566 /* Get the type for Ada_Task_Control_Bloc.Common.Call.LL. */
567 ll_type = private_data_sym->type ();
569 /* Get the type for Common_ATCB.Call.all. */
570 call_type = entry_call_record_sym->type ();
572 /* Get the field indices. */
573 fieldnos.common = ada_get_field_index (type, "common", 0);
574 fieldnos.entry_calls = ada_get_field_index (type, "entry_calls", 1);
575 fieldnos.atc_nesting_level =
576 ada_get_field_index (type, "atc_nesting_level", 1);
577 fieldnos.state = ada_get_field_index (common_type, "state", 0);
578 fieldnos.parent = ada_get_field_index (common_type, "parent", 1);
579 fieldnos.priority = ada_get_field_index (common_type, "base_priority", 0);
580 fieldnos.image = ada_get_field_index (common_type, "task_image", 1);
581 fieldnos.image_len = ada_get_field_index (common_type, "task_image_len", 1);
582 fieldnos.activation_link = ada_get_field_index (common_type,
583 "activation_link", 1);
584 fieldnos.call = ada_get_field_index (common_type, "call", 1);
585 fieldnos.ll = ada_get_field_index (common_type, "ll", 0);
586 fieldnos.base_cpu = ada_get_field_index (common_type, "base_cpu", 0);
587 fieldnos.ll_thread = ada_get_field_index (ll_type, "thread", 0);
588 fieldnos.ll_lwp = ada_get_field_index (ll_type, "lwp", 1);
589 fieldnos.call_self = ada_get_field_index (call_type, "self", 0);
591 /* On certain platforms such as x86-windows, the "lwp" field has been
592 named "thread_id". This field will likely be renamed in the future,
593 but we need to support both possibilities to avoid an unnecessary
594 dependency on a recent compiler. We therefore try locating the
595 "thread_id" field in place of the "lwp" field if we did not find
596 the latter. */
597 if (fieldnos.ll_lwp < 0)
598 fieldnos.ll_lwp = ada_get_field_index (ll_type, "thread_id", 1);
600 /* Check for the CPU offset. */
601 bound_minimal_symbol first_id_sym
602 = lookup_bound_minimal_symbol ("__gnat_gdb_cpu_first_id");
603 unsigned int first_id = 0;
604 if (first_id_sym.minsym != nullptr)
606 CORE_ADDR addr = first_id_sym.value_address ();
607 gdbarch *arch = current_inferior ()->arch ();
608 /* This symbol always has type uint32_t. */
609 struct type *u32type = builtin_type (arch)->builtin_uint32;
610 first_id = value_as_long (value_at (u32type, addr));
613 /* Set all the out parameters all at once, now that we are certain
614 that there are no potential error() anymore. */
615 pspace_data = get_ada_tasks_pspace_data (current_program_space);
616 pspace_data->initialized_p = 1;
617 pspace_data->atcb_type = type;
618 pspace_data->atcb_common_type = common_type;
619 pspace_data->atcb_ll_type = ll_type;
620 pspace_data->atcb_call_type = call_type;
621 pspace_data->atcb_fieldno = fieldnos;
622 pspace_data->cpu_id_offset = first_id;
623 return NULL;
626 /* Build the PTID of the task from its COMMON_VALUE, which is the "Common"
627 component of its ATCB record. This PTID needs to match the PTID used
628 by the thread layer. */
630 static ptid_t
631 ptid_from_atcb_common (struct value *common_value)
633 ULONGEST thread;
634 CORE_ADDR lwp = 0;
635 struct value *ll_value;
636 ptid_t ptid;
637 const struct ada_tasks_pspace_data *pspace_data
638 = get_ada_tasks_pspace_data (current_program_space);
640 ll_value = value_field (common_value, pspace_data->atcb_fieldno.ll);
642 if (pspace_data->atcb_fieldno.ll_lwp >= 0)
643 lwp = value_as_address (value_field (ll_value,
644 pspace_data->atcb_fieldno.ll_lwp));
645 thread = value_as_long (value_field (ll_value,
646 pspace_data->atcb_fieldno.ll_thread));
648 ptid = target_get_ada_task_ptid (lwp, thread);
650 return ptid;
653 /* Read the ATCB data of a given task given its TASK_ID (which is in practice
654 the address of its associated ATCB record), and store the result inside
655 TASK_INFO. */
657 static void
658 read_atcb (CORE_ADDR task_id, struct ada_task_info *task_info)
660 struct value *tcb_value;
661 struct value *common_value;
662 struct value *atc_nesting_level_value;
663 struct value *entry_calls_value;
664 struct value *entry_calls_value_element;
665 int called_task_fieldno = -1;
666 static const char ravenscar_task_name[] = "Ravenscar task";
667 const struct ada_tasks_pspace_data *pspace_data
668 = get_ada_tasks_pspace_data (current_program_space);
670 /* Clear the whole structure to start with, so that everything
671 is always initialized the same. */
672 memset (task_info, 0, sizeof (struct ada_task_info));
674 if (!pspace_data->initialized_p)
676 const char *err_msg = ada_get_tcb_types_info ();
678 if (err_msg != NULL)
679 error (_("%s. Aborting"), err_msg);
682 tcb_value = value_from_contents_and_address (pspace_data->atcb_type,
683 NULL, task_id);
684 common_value = value_field (tcb_value, pspace_data->atcb_fieldno.common);
686 /* Fill in the task_id. */
688 task_info->task_id = task_id;
690 /* Compute the name of the task.
692 Depending on the GNAT version used, the task image is either a fat
693 string, or a thin array of characters. Older versions of GNAT used
694 to use fat strings, and therefore did not need an extra field in
695 the ATCB to store the string length. For efficiency reasons, newer
696 versions of GNAT replaced the fat string by a static buffer, but this
697 also required the addition of a new field named "Image_Len" containing
698 the length of the task name. The method used to extract the task name
699 is selected depending on the existence of this field.
701 In some run-time libs (e.g. Ravenscar), the name is not in the ATCB;
702 we may want to get it from the first user frame of the stack. For now,
703 we just give a dummy name. */
705 if (pspace_data->atcb_fieldno.image_len == -1)
707 if (pspace_data->atcb_fieldno.image >= 0)
708 read_fat_string_value (task_info->name,
709 value_field (common_value,
710 pspace_data->atcb_fieldno.image),
711 sizeof (task_info->name) - 1);
712 else
714 struct bound_minimal_symbol msym;
716 msym = lookup_minimal_symbol_by_pc (task_id);
717 if (msym.minsym)
719 const char *full_name = msym.minsym->linkage_name ();
720 const char *task_name = full_name;
721 const char *p;
723 /* Strip the prefix. */
724 for (p = full_name; *p; p++)
725 if (p[0] == '_' && p[1] == '_')
726 task_name = p + 2;
728 /* Copy the task name. */
729 strncpy (task_info->name, task_name,
730 sizeof (task_info->name) - 1);
731 task_info->name[sizeof (task_info->name) - 1] = 0;
733 else
735 /* No symbol found. Use a default name. */
736 strcpy (task_info->name, ravenscar_task_name);
740 else
742 int len = value_as_long
743 (value_field (common_value,
744 pspace_data->atcb_fieldno.image_len));
746 value_as_string (task_info->name,
747 value_field (common_value,
748 pspace_data->atcb_fieldno.image),
749 len);
752 /* Compute the task state and priority. */
754 task_info->state =
755 value_as_long (value_field (common_value,
756 pspace_data->atcb_fieldno.state));
757 task_info->priority =
758 value_as_long (value_field (common_value,
759 pspace_data->atcb_fieldno.priority));
761 /* If the ATCB contains some information about the parent task,
762 then compute it as well. Otherwise, zero. */
764 if (pspace_data->atcb_fieldno.parent >= 0)
765 task_info->parent =
766 value_as_address (value_field (common_value,
767 pspace_data->atcb_fieldno.parent));
769 /* If the task is in an entry call waiting for another task,
770 then determine which task it is. */
772 if (task_info->state == Entry_Caller_Sleep
773 && pspace_data->atcb_fieldno.atc_nesting_level > 0
774 && pspace_data->atcb_fieldno.entry_calls > 0)
776 /* Let My_ATCB be the Ada task control block of a task calling the
777 entry of another task; then the Task_Id of the called task is
778 in My_ATCB.Entry_Calls (My_ATCB.ATC_Nesting_Level).Called_Task. */
779 atc_nesting_level_value =
780 value_field (tcb_value, pspace_data->atcb_fieldno.atc_nesting_level);
781 entry_calls_value =
782 ada_coerce_to_simple_array_ptr
783 (value_field (tcb_value, pspace_data->atcb_fieldno.entry_calls));
784 entry_calls_value_element =
785 value_subscript (entry_calls_value,
786 value_as_long (atc_nesting_level_value));
787 called_task_fieldno =
788 ada_get_field_index (entry_calls_value_element->type (),
789 "called_task", 0);
790 task_info->called_task =
791 value_as_address (value_field (entry_calls_value_element,
792 called_task_fieldno));
795 /* If the ATCB contains some information about RV callers, then
796 compute the "caller_task". Otherwise, leave it as zero. */
798 if (pspace_data->atcb_fieldno.call >= 0)
800 /* Get the ID of the caller task from Common_ATCB.Call.all.Self.
801 If Common_ATCB.Call is null, then there is no caller. */
802 const CORE_ADDR call =
803 value_as_address (value_field (common_value,
804 pspace_data->atcb_fieldno.call));
805 struct value *call_val;
807 if (call != 0)
809 call_val =
810 value_from_contents_and_address (pspace_data->atcb_call_type,
811 NULL, call);
812 task_info->caller_task =
813 value_as_address
814 (value_field (call_val, pspace_data->atcb_fieldno.call_self));
818 task_info->base_cpu
819 = (pspace_data->cpu_id_offset
820 + value_as_long (value_field (common_value,
821 pspace_data->atcb_fieldno.base_cpu)));
823 /* And finally, compute the task ptid. Note that there is not point
824 in computing it if the task is no longer alive, in which case
825 it is good enough to set its ptid to the null_ptid. */
826 if (ada_task_is_alive (task_info))
827 task_info->ptid = ptid_from_atcb_common (common_value);
828 else
829 task_info->ptid = null_ptid;
832 /* Read the ATCB info of the given task (identified by TASK_ID), and
833 add the result to the given inferior's TASK_LIST. */
835 static void
836 add_ada_task (CORE_ADDR task_id, struct inferior *inf)
838 struct ada_task_info task_info;
839 struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
841 read_atcb (task_id, &task_info);
842 data->task_list.push_back (task_info);
845 /* Read the Known_Tasks array from the inferior memory, and store
846 it in the current inferior's TASK_LIST. Return true upon success. */
848 static bool
849 read_known_tasks_array (struct ada_tasks_inferior_data *data)
851 const int target_ptr_byte = data->known_tasks_element->length ();
852 const int known_tasks_size = target_ptr_byte * data->known_tasks_length;
853 gdb_byte *known_tasks = (gdb_byte *) alloca (known_tasks_size);
854 int i;
856 /* Build a new list by reading the ATCBs from the Known_Tasks array
857 in the Ada runtime. */
858 read_memory (data->known_tasks_addr, known_tasks, known_tasks_size);
859 for (i = 0; i < data->known_tasks_length; i++)
861 CORE_ADDR task_id =
862 extract_typed_address (known_tasks + i * target_ptr_byte,
863 data->known_tasks_element);
865 if (task_id != 0)
866 add_ada_task (task_id, current_inferior ());
869 return true;
872 /* Read the known tasks from the inferior memory, and store it in
873 the current inferior's TASK_LIST. Return true upon success. */
875 static bool
876 read_known_tasks_list (struct ada_tasks_inferior_data *data)
878 const int target_ptr_byte = data->known_tasks_element->length ();
879 gdb_byte *known_tasks = (gdb_byte *) alloca (target_ptr_byte);
880 CORE_ADDR task_id;
881 const struct ada_tasks_pspace_data *pspace_data
882 = get_ada_tasks_pspace_data (current_program_space);
884 /* Sanity check. */
885 if (pspace_data->atcb_fieldno.activation_link < 0)
886 return false;
888 /* Build a new list by reading the ATCBs. Read head of the list. */
889 read_memory (data->known_tasks_addr, known_tasks, target_ptr_byte);
890 task_id = extract_typed_address (known_tasks, data->known_tasks_element);
891 while (task_id != 0)
893 struct value *tcb_value;
894 struct value *common_value;
896 add_ada_task (task_id, current_inferior ());
898 /* Read the chain. */
899 tcb_value = value_from_contents_and_address (pspace_data->atcb_type,
900 NULL, task_id);
901 common_value = value_field (tcb_value, pspace_data->atcb_fieldno.common);
902 task_id = value_as_address
903 (value_field (common_value,
904 pspace_data->atcb_fieldno.activation_link));
907 return true;
910 /* Set all fields of the current inferior ada-tasks data pointed by DATA.
911 Do nothing if those fields are already set and still up to date. */
913 static void
914 ada_tasks_inferior_data_sniffer (struct ada_tasks_inferior_data *data)
916 struct bound_minimal_symbol msym;
917 struct symbol *sym;
919 /* Return now if already set. */
920 if (data->known_tasks_kind != ADA_TASKS_UNKNOWN)
921 return;
923 /* Try array. */
925 msym = lookup_minimal_symbol (KNOWN_TASKS_NAME, NULL, NULL);
926 if (msym.minsym != NULL)
928 data->known_tasks_kind = ADA_TASKS_ARRAY;
929 data->known_tasks_addr = msym.value_address ();
931 /* Try to get pointer type and array length from the symtab. */
932 sym = lookup_symbol_in_language (KNOWN_TASKS_NAME, NULL,
933 SEARCH_VAR_DOMAIN,
934 language_c, NULL).symbol;
935 if (sym != NULL)
937 /* Validate. */
938 struct type *type = check_typedef (sym->type ());
939 struct type *eltype = NULL;
940 struct type *idxtype = NULL;
942 if (type->code () == TYPE_CODE_ARRAY)
943 eltype = check_typedef (type->target_type ());
944 if (eltype != NULL
945 && eltype->code () == TYPE_CODE_PTR)
946 idxtype = check_typedef (type->index_type ());
947 if (idxtype != NULL
948 && idxtype->bounds ()->low.is_constant ()
949 && idxtype->bounds ()->high.is_constant ())
951 data->known_tasks_element = eltype;
952 data->known_tasks_length =
953 (idxtype->bounds ()->high.const_val ()
954 - idxtype->bounds ()->low.const_val () + 1);
955 return;
959 /* Fallback to default values. The runtime may have been stripped (as
960 in some distributions), but it is likely that the executable still
961 contains debug information on the task type (due to implicit with of
962 Ada.Tasking). */
963 data->known_tasks_element =
964 builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
965 data->known_tasks_length = MAX_NUMBER_OF_KNOWN_TASKS;
966 return;
970 /* Try list. */
972 msym = lookup_minimal_symbol (KNOWN_TASKS_LIST, NULL, NULL);
973 if (msym.minsym != NULL)
975 data->known_tasks_kind = ADA_TASKS_LIST;
976 data->known_tasks_addr = msym.value_address ();
977 data->known_tasks_length = 1;
979 sym = lookup_symbol_in_language (KNOWN_TASKS_LIST, NULL,
980 SEARCH_VAR_DOMAIN,
981 language_c, NULL).symbol;
982 if (sym != NULL && sym->value_address () != 0)
984 /* Validate. */
985 struct type *type = check_typedef (sym->type ());
987 if (type->code () == TYPE_CODE_PTR)
989 data->known_tasks_element = type;
990 return;
994 /* Fallback to default values. */
995 data->known_tasks_element =
996 builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
997 data->known_tasks_length = 1;
998 return;
1001 /* Can't find tasks. */
1003 data->known_tasks_kind = ADA_TASKS_NOT_FOUND;
1004 data->known_tasks_addr = 0;
1007 /* Read the known tasks from the current inferior's memory, and store it
1008 in the current inferior's data TASK_LIST. */
1010 static void
1011 read_known_tasks ()
1013 struct ada_tasks_inferior_data *data =
1014 get_ada_tasks_inferior_data (current_inferior ());
1016 /* Step 1: Clear the current list, if necessary. */
1017 data->task_list.clear ();
1019 /* Step 2: do the real work.
1020 If the application does not use task, then no more needs to be done.
1021 It is important to have the task list cleared (see above) before we
1022 return, as we don't want a stale task list to be used... This can
1023 happen for instance when debugging a non-multitasking program after
1024 having debugged a multitasking one. */
1025 ada_tasks_inferior_data_sniffer (data);
1026 gdb_assert (data->known_tasks_kind != ADA_TASKS_UNKNOWN);
1028 /* Step 3: Set task_list_valid_p, to avoid re-reading the Known_Tasks
1029 array unless needed. */
1030 switch (data->known_tasks_kind)
1032 case ADA_TASKS_NOT_FOUND: /* Tasking not in use in inferior. */
1033 break;
1034 case ADA_TASKS_ARRAY:
1035 data->task_list_valid_p = read_known_tasks_array (data);
1036 break;
1037 case ADA_TASKS_LIST:
1038 data->task_list_valid_p = read_known_tasks_list (data);
1039 break;
1043 /* Build the task_list by reading the Known_Tasks array from
1044 the inferior, and return the number of tasks in that list
1045 (zero means that the program is not using tasking at all). */
1047 static int
1048 ada_build_task_list ()
1050 struct ada_tasks_inferior_data *data;
1052 if (!target_has_stack ())
1053 error (_("Cannot inspect Ada tasks when program is not running"));
1055 data = get_ada_tasks_inferior_data (current_inferior ());
1056 if (!data->task_list_valid_p)
1057 read_known_tasks ();
1059 return data->task_list.size ();
1062 /* Print a table providing a short description of all Ada tasks
1063 running inside inferior INF. If ARG_STR is set, it will be
1064 interpreted as a task number, and the table will be limited to
1065 that task only. */
1067 void
1068 print_ada_task_info (struct ui_out *uiout,
1069 const char *arg_str,
1070 struct inferior *inf)
1072 struct ada_tasks_inferior_data *data;
1073 int taskno, nb_tasks;
1074 int taskno_arg = 0;
1075 int nb_columns;
1077 if (ada_build_task_list () == 0)
1079 uiout->message (_("Your application does not use any Ada tasks.\n"));
1080 return;
1083 if (arg_str != NULL && arg_str[0] != '\0')
1084 taskno_arg = value_as_long (parse_and_eval (arg_str));
1086 if (uiout->is_mi_like_p ())
1087 /* In GDB/MI mode, we want to provide the thread ID corresponding
1088 to each task. This allows clients to quickly find the thread
1089 associated to any task, which is helpful for commands that
1090 take a --thread argument. However, in order to be able to
1091 provide that thread ID, the thread list must be up to date
1092 first. */
1093 target_update_thread_list ();
1095 data = get_ada_tasks_inferior_data (inf);
1097 /* Compute the number of tasks that are going to be displayed
1098 in the output. If an argument was given, there will be
1099 at most 1 entry. Otherwise, there will be as many entries
1100 as we have tasks. */
1101 if (taskno_arg)
1103 if (taskno_arg > 0 && taskno_arg <= data->task_list.size ())
1104 nb_tasks = 1;
1105 else
1106 nb_tasks = 0;
1108 else
1109 nb_tasks = data->task_list.size ();
1111 nb_columns = uiout->is_mi_like_p () ? 8 : 7;
1112 ui_out_emit_table table_emitter (uiout, nb_columns, nb_tasks, "tasks");
1113 uiout->table_header (1, ui_left, "current", "");
1114 uiout->table_header (3, ui_right, "id", "ID");
1116 size_t tid_width = 9;
1117 /* Grown below in case the largest entry is bigger. */
1119 if (!uiout->is_mi_like_p ())
1121 for (taskno = 1; taskno <= data->task_list.size (); taskno++)
1123 const struct ada_task_info *const task_info
1124 = &data->task_list[taskno - 1];
1126 gdb_assert (task_info != NULL);
1128 tid_width = std::max (tid_width,
1129 1 + strlen (phex_nz (task_info->task_id,
1130 sizeof (CORE_ADDR))));
1133 uiout->table_header (tid_width, ui_right, "task-id", "TID");
1135 /* The following column is provided in GDB/MI mode only because
1136 it is only really useful in that mode, and also because it
1137 allows us to keep the CLI output shorter and more compact. */
1138 if (uiout->is_mi_like_p ())
1139 uiout->table_header (4, ui_right, "thread-id", "");
1140 uiout->table_header (4, ui_right, "parent-id", "P-ID");
1141 uiout->table_header (3, ui_right, "priority", "Pri");
1142 uiout->table_header (22, ui_left, "state", "State");
1143 /* Use ui_noalign for the last column, to prevent the CLI uiout
1144 from printing an extra space at the end of each row. This
1145 is a bit of a hack, but does get the job done. */
1146 uiout->table_header (1, ui_noalign, "name", "Name");
1147 uiout->table_body ();
1149 for (taskno = 1; taskno <= data->task_list.size (); taskno++)
1151 const struct ada_task_info *const task_info =
1152 &data->task_list[taskno - 1];
1153 int parent_id;
1155 gdb_assert (task_info != NULL);
1157 /* If the user asked for the output to be restricted
1158 to one task only, and this is not the task, skip
1159 to the next one. */
1160 if (taskno_arg && taskno != taskno_arg)
1161 continue;
1163 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1165 /* Print a star if this task is the current task (or the task
1166 currently selected). */
1167 if (task_info->ptid == inferior_ptid)
1168 uiout->field_string ("current", "*");
1169 else
1170 uiout->field_skip ("current");
1172 /* Print the task number. */
1173 uiout->field_signed ("id", taskno);
1175 /* Print the Task ID. */
1176 uiout->field_string ("task-id", phex_nz (task_info->task_id,
1177 sizeof (CORE_ADDR)));
1179 /* Print the associated Thread ID. */
1180 if (uiout->is_mi_like_p ())
1182 thread_info *thread = (ada_task_is_alive (task_info)
1183 ? inf->find_thread (task_info->ptid)
1184 : nullptr);
1186 if (thread != NULL)
1187 uiout->field_signed ("thread-id", thread->global_num);
1188 else
1190 /* This can happen if the thread is no longer alive. */
1191 uiout->field_skip ("thread-id");
1195 /* Print the ID of the parent task. */
1196 parent_id = get_task_number_from_id (task_info->parent, inf);
1197 if (parent_id)
1198 uiout->field_signed ("parent-id", parent_id);
1199 else
1200 uiout->field_skip ("parent-id");
1202 /* Print the base priority of the task. */
1203 uiout->field_signed ("priority", task_info->priority);
1205 /* Print the task current state. */
1206 if (task_info->caller_task)
1207 uiout->field_fmt ("state",
1208 _("Accepting RV with %-4d"),
1209 get_task_number_from_id (task_info->caller_task,
1210 inf));
1211 else if (task_info->called_task)
1212 uiout->field_fmt ("state",
1213 _("Waiting on RV with %-3d"),
1214 get_task_number_from_id (task_info->called_task,
1215 inf));
1216 else
1217 uiout->field_string ("state", get_state (task_info->state));
1219 /* Finally, print the task name, without quotes around it, as mi like
1220 is not expecting quotes, and in non mi-like no need for quotes
1221 as there is a specific column for the name. */
1222 uiout->field_fmt ("name",
1223 (task_info->name[0] != '\0'
1224 ? ui_file_style ()
1225 : metadata_style.style ()),
1226 "%s",
1227 (task_info->name[0] != '\0'
1228 ? task_info->name
1229 : _("<no name>")));
1231 uiout->text ("\n");
1235 /* Print a detailed description of the Ada task whose ID is TASKNO_STR
1236 for the given inferior (INF). */
1238 static void
1239 info_task (struct ui_out *uiout, const char *taskno_str, struct inferior *inf)
1241 const int taskno = value_as_long (parse_and_eval (taskno_str));
1242 struct ada_task_info *task_info;
1243 int parent_taskno = 0;
1244 struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
1246 if (ada_build_task_list () == 0)
1248 uiout->message (_("Your application does not use any Ada tasks.\n"));
1249 return;
1252 if (taskno <= 0 || taskno > data->task_list.size ())
1253 error (_("Task ID %d not known. Use the \"info tasks\" command to\n"
1254 "see the IDs of currently known tasks"), taskno);
1255 task_info = &data->task_list[taskno - 1];
1257 /* Print the Ada task ID. */
1258 gdb_printf (_("Ada Task: %s\n"),
1259 paddress (current_inferior ()->arch (), task_info->task_id));
1261 /* Print the name of the task. */
1262 if (task_info->name[0] != '\0')
1263 gdb_printf (_("Name: %s\n"), task_info->name);
1264 else
1265 fprintf_styled (gdb_stdout, metadata_style.style (), _("<no name>\n"));
1267 /* Print the TID and LWP. */
1268 gdb_printf (_("Thread: 0x%s\n"), phex_nz (task_info->ptid.tid (),
1269 sizeof (ULONGEST)));
1270 gdb_printf (_("LWP: %#lx\n"), task_info->ptid.lwp ());
1272 /* If set, print the base CPU. */
1273 if (task_info->base_cpu != 0)
1274 gdb_printf (_("Base CPU: %d\n"), task_info->base_cpu);
1276 /* Print who is the parent (if any). */
1277 if (task_info->parent != 0)
1278 parent_taskno = get_task_number_from_id (task_info->parent, inf);
1279 if (parent_taskno)
1281 struct ada_task_info *parent = &data->task_list[parent_taskno - 1];
1283 gdb_printf (_("Parent: %d"), parent_taskno);
1284 if (parent->name[0] != '\0')
1285 gdb_printf (" (%s)", parent->name);
1286 gdb_printf ("\n");
1288 else
1289 gdb_printf (_("No parent\n"));
1291 /* Print the base priority. */
1292 gdb_printf (_("Base Priority: %d\n"), task_info->priority);
1294 /* print the task current state. */
1296 int target_taskno = 0;
1298 if (task_info->caller_task)
1300 target_taskno = get_task_number_from_id (task_info->caller_task, inf);
1301 gdb_printf (_("State: Accepting rendezvous with %d"),
1302 target_taskno);
1304 else if (task_info->called_task)
1306 target_taskno = get_task_number_from_id (task_info->called_task, inf);
1307 gdb_printf (_("State: Waiting on task %d's entry"),
1308 target_taskno);
1310 else
1311 gdb_printf (_("State: %s"), get_long_state (task_info->state));
1313 if (target_taskno)
1315 ada_task_info *target_task_info = &data->task_list[target_taskno - 1];
1317 if (target_task_info->name[0] != '\0')
1318 gdb_printf (" (%s)", target_task_info->name);
1321 gdb_printf ("\n");
1325 /* If ARG is empty or null, then print a list of all Ada tasks.
1326 Otherwise, print detailed information about the task whose ID
1327 is ARG.
1329 Does nothing if the program doesn't use Ada tasking. */
1331 static void
1332 info_tasks_command (const char *arg, int from_tty)
1334 struct ui_out *uiout = current_uiout;
1336 if (arg == NULL || *arg == '\0')
1337 print_ada_task_info (uiout, NULL, current_inferior ());
1338 else
1339 info_task (uiout, arg, current_inferior ());
1342 /* Print a message telling the user id of the current task.
1343 This function assumes that tasking is in use in the inferior. */
1345 static void
1346 display_current_task_id (void)
1348 const int current_task = ada_get_task_number (inferior_thread ());
1350 if (current_task == 0)
1351 gdb_printf (_("[Current task is unknown]\n"));
1352 else
1354 struct ada_tasks_inferior_data *data
1355 = get_ada_tasks_inferior_data (current_inferior ());
1356 struct ada_task_info *task_info = &data->task_list[current_task - 1];
1358 gdb_printf (_("[Current task is %s]\n"),
1359 task_to_str (current_task, task_info).c_str ());
1363 /* Parse and evaluate TIDSTR into a task id, and try to switch to
1364 that task. Print an error message if the task switch failed. */
1366 static void
1367 task_command_1 (const char *taskno_str, int from_tty, struct inferior *inf)
1369 const int taskno = value_as_long (parse_and_eval (taskno_str));
1370 struct ada_task_info *task_info;
1371 struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
1373 if (taskno <= 0 || taskno > data->task_list.size ())
1374 error (_("Task ID %d not known. Use the \"info tasks\" command to\n"
1375 "see the IDs of currently known tasks"), taskno);
1376 task_info = &data->task_list[taskno - 1];
1378 if (!ada_task_is_alive (task_info))
1379 error (_("Cannot switch to task %s: Task is no longer running"),
1380 task_to_str (taskno, task_info).c_str ());
1382 /* On some platforms, the thread list is not updated until the user
1383 performs a thread-related operation (by using the "info threads"
1384 command, for instance). So this thread list may not be up to date
1385 when the user attempts this task switch. Since we cannot switch
1386 to the thread associated to our task if GDB does not know about
1387 that thread, we need to make sure that any new threads gets added
1388 to the thread list. */
1389 target_update_thread_list ();
1391 /* Verify that the ptid of the task we want to switch to is valid
1392 (in other words, a ptid that GDB knows about). Otherwise, we will
1393 cause an assertion failure later on, when we try to determine
1394 the ptid associated thread_info data. We should normally never
1395 encounter such an error, but the wrong ptid can actually easily be
1396 computed if target_get_ada_task_ptid has not been implemented for
1397 our target (yet). Rather than cause an assertion error in that case,
1398 it's nicer for the user to just refuse to perform the task switch. */
1399 thread_info *tp = inf->find_thread (task_info->ptid);
1400 if (tp == NULL)
1401 error (_("Unable to compute thread ID for task %s.\n"
1402 "Cannot switch to this task."),
1403 task_to_str (taskno, task_info).c_str ());
1405 switch_to_thread (tp);
1406 ada_find_printable_frame (get_selected_frame (NULL));
1407 gdb_printf (_("[Switching to task %s]\n"),
1408 task_to_str (taskno, task_info).c_str ());
1409 print_stack_frame (get_selected_frame (NULL),
1410 frame_relative_level (get_selected_frame (NULL)),
1411 SRC_AND_LOC, 1);
1415 /* Print the ID of the current task if TASKNO_STR is empty or NULL.
1416 Otherwise, switch to the task indicated by TASKNO_STR. */
1418 static void
1419 task_command (const char *taskno_str, int from_tty)
1421 struct ui_out *uiout = current_uiout;
1423 if (ada_build_task_list () == 0)
1425 uiout->message (_("Your application does not use any Ada tasks.\n"));
1426 return;
1429 if (taskno_str == NULL || taskno_str[0] == '\0')
1430 display_current_task_id ();
1431 else
1432 task_command_1 (taskno_str, from_tty, current_inferior ());
1435 /* Indicate that the given inferior's task list may have changed,
1436 so invalidate the cache. */
1438 static void
1439 ada_task_list_changed (struct inferior *inf)
1441 struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
1443 data->task_list_valid_p = false;
1446 /* Invalidate the per-program-space data. */
1448 static void
1449 ada_tasks_invalidate_pspace_data (struct program_space *pspace)
1451 get_ada_tasks_pspace_data (pspace)->initialized_p = 0;
1454 /* Invalidate the per-inferior data. */
1456 static void
1457 ada_tasks_invalidate_inferior_data (struct inferior *inf)
1459 struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
1461 data->known_tasks_kind = ADA_TASKS_UNKNOWN;
1462 data->task_list_valid_p = false;
1465 /* The 'normal_stop' observer notification callback. */
1467 static void
1468 ada_tasks_normal_stop_observer (struct bpstat *unused_args, int unused_args2)
1470 /* The inferior has been resumed, and just stopped. This means that
1471 our task_list needs to be recomputed before it can be used again. */
1472 ada_task_list_changed (current_inferior ());
1475 /* Clear data associated to PSPACE and all inferiors using that program
1476 space. */
1478 static void
1479 ada_tasks_clear_pspace_data (program_space *pspace)
1481 /* The associated program-space data might have changed after
1482 this objfile was added. Invalidate all cached data. */
1483 ada_tasks_invalidate_pspace_data (pspace);
1485 /* Invalidate the per-inferior cache for all inferiors using
1486 this program space. */
1487 for (inferior *inf : all_inferiors ())
1488 if (inf->pspace == pspace)
1489 ada_tasks_invalidate_inferior_data (inf);
1492 /* Called when a new objfile was added. */
1494 static void
1495 ada_tasks_new_objfile_observer (objfile *objfile)
1497 ada_tasks_clear_pspace_data (objfile->pspace);
1500 /* The qcs command line flags for the "task apply" commands. Keep
1501 this in sync with the "frame apply" commands. */
1503 using qcs_flag_option_def
1504 = gdb::option::flag_option_def<qcs_flags>;
1506 static const gdb::option::option_def task_qcs_flags_option_defs[] = {
1507 qcs_flag_option_def {
1508 "q", [] (qcs_flags *opt) { return &opt->quiet; },
1509 N_("Disables printing the task information."),
1512 qcs_flag_option_def {
1513 "c", [] (qcs_flags *opt) { return &opt->cont; },
1514 N_("Print any error raised by COMMAND and continue."),
1517 qcs_flag_option_def {
1518 "s", [] (qcs_flags *opt) { return &opt->silent; },
1519 N_("Silently ignore any errors or empty output produced by COMMAND."),
1523 /* Create an option_def_group for the "task apply all" options, with
1524 FLAGS as context. */
1526 static inline std::array<gdb::option::option_def_group, 1>
1527 make_task_apply_all_options_def_group (qcs_flags *flags)
1529 return {{
1530 { {task_qcs_flags_option_defs}, flags },
1534 /* Create an option_def_group for the "task apply" options, with
1535 FLAGS as context. */
1537 static inline gdb::option::option_def_group
1538 make_task_apply_options_def_group (qcs_flags *flags)
1540 return {{task_qcs_flags_option_defs}, flags};
1543 /* Implementation of 'task apply all'. */
1545 static void
1546 task_apply_all_command (const char *cmd, int from_tty)
1548 qcs_flags flags;
1550 auto group = make_task_apply_all_options_def_group (&flags);
1551 gdb::option::process_options
1552 (&cmd, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group);
1554 validate_flags_qcs ("task apply all", &flags);
1556 if (cmd == nullptr || *cmd == '\0')
1557 error (_("Please specify a command at the end of 'task apply all'"));
1559 update_thread_list ();
1560 ada_build_task_list ();
1562 inferior *inf = current_inferior ();
1563 struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
1565 /* Save a copy of the thread list and increment each thread's
1566 refcount while executing the command in the context of each
1567 thread, in case the command affects this. */
1568 std::vector<std::pair<int, thread_info_ref>> thr_list_cpy;
1570 for (int i = 1; i <= data->task_list.size (); ++i)
1572 ada_task_info &task = data->task_list[i - 1];
1573 if (!ada_task_is_alive (&task))
1574 continue;
1576 thread_info *tp = inf->find_thread (task.ptid);
1577 if (tp == nullptr)
1578 warning (_("Unable to compute thread ID for task %s.\n"
1579 "Cannot switch to this task."),
1580 task_to_str (i, &task).c_str ());
1581 else
1582 thr_list_cpy.emplace_back (i, thread_info_ref::new_reference (tp));
1585 scoped_restore_current_thread restore_thread;
1587 for (const auto &info : thr_list_cpy)
1588 if (switch_to_thread_if_alive (info.second.get ()))
1589 thread_try_catch_cmd (info.second.get (), info.first, cmd,
1590 from_tty, flags);
1593 /* Implementation of 'task apply'. */
1595 static void
1596 task_apply_command (const char *tidlist, int from_tty)
1599 if (tidlist == nullptr || *tidlist == '\0')
1600 error (_("Please specify a task ID list"));
1602 update_thread_list ();
1603 ada_build_task_list ();
1605 inferior *inf = current_inferior ();
1606 struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf);
1608 /* Save a copy of the thread list and increment each thread's
1609 refcount while executing the command in the context of each
1610 thread, in case the command affects this. */
1611 std::vector<std::pair<int, thread_info_ref>> thr_list_cpy;
1613 number_or_range_parser parser (tidlist);
1614 while (!parser.finished ())
1616 int num = parser.get_number ();
1618 if (num < 1 || num - 1 >= data->task_list.size ())
1619 warning (_("no Ada Task with number %d"), num);
1620 else
1622 ada_task_info &task = data->task_list[num - 1];
1623 if (!ada_task_is_alive (&task))
1624 continue;
1626 thread_info *tp = inf->find_thread (task.ptid);
1627 if (tp == nullptr)
1628 warning (_("Unable to compute thread ID for task %s.\n"
1629 "Cannot switch to this task."),
1630 task_to_str (num, &task).c_str ());
1631 else
1632 thr_list_cpy.emplace_back (num,
1633 thread_info_ref::new_reference (tp));
1637 qcs_flags flags;
1638 const char *cmd = parser.cur_tok ();
1640 auto group = make_task_apply_options_def_group (&flags);
1641 gdb::option::process_options
1642 (&cmd, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group);
1644 validate_flags_qcs ("task apply", &flags);
1646 if (*cmd == '\0')
1647 error (_("Please specify a command following the task ID list"));
1649 scoped_restore_current_thread restore_thread;
1651 for (const auto &info : thr_list_cpy)
1652 if (switch_to_thread_if_alive (info.second.get ()))
1653 thread_try_catch_cmd (info.second.get (), info.first, cmd,
1654 from_tty, flags);
1657 void _initialize_tasks ();
1658 void
1659 _initialize_tasks ()
1661 /* Attach various observers. */
1662 gdb::observers::normal_stop.attach (ada_tasks_normal_stop_observer,
1663 "ada-tasks");
1664 gdb::observers::new_objfile.attach (ada_tasks_new_objfile_observer,
1665 "ada-tasks");
1666 gdb::observers::all_objfiles_removed.attach (ada_tasks_clear_pspace_data,
1667 "ada-tasks");
1669 static struct cmd_list_element *task_cmd_list;
1670 static struct cmd_list_element *task_apply_list;
1673 /* Some new commands provided by this module. */
1674 add_info ("tasks", info_tasks_command,
1675 _("Provide information about all known Ada tasks."));
1677 add_prefix_cmd ("task", class_run, task_command,
1678 _("Use this command to switch between Ada tasks.\n\
1679 Without argument, this command simply prints the current task ID."),
1680 &task_cmd_list, 1, &cmdlist);
1682 #define TASK_APPLY_OPTION_HELP "\
1683 Prints per-inferior task number followed by COMMAND output.\n\
1685 By default, an error raised during the execution of COMMAND\n\
1686 aborts \"task apply\".\n\
1688 Options:\n\
1689 %OPTIONS%"
1691 static const auto task_apply_opts
1692 = make_task_apply_options_def_group (nullptr);
1694 static std::string task_apply_help = gdb::option::build_help (_("\
1695 Apply a command to a list of tasks.\n\
1696 Usage: task apply ID... [OPTION]... COMMAND\n\
1697 ID is a space-separated list of IDs of tasks to apply COMMAND on.\n"
1698 TASK_APPLY_OPTION_HELP), task_apply_opts);
1700 add_prefix_cmd ("apply", class_run,
1701 task_apply_command,
1702 task_apply_help.c_str (),
1703 &task_apply_list, 1,
1704 &task_cmd_list);
1706 static const auto task_apply_all_opts
1707 = make_task_apply_all_options_def_group (nullptr);
1709 static std::string task_apply_all_help = gdb::option::build_help (_("\
1710 Apply a command to all tasks in the current inferior.\n\
1712 Usage: task apply all [OPTION]... COMMAND\n"
1713 TASK_APPLY_OPTION_HELP), task_apply_all_opts);
1715 add_cmd ("all", class_run, task_apply_all_command,
1716 task_apply_all_help.c_str (), &task_apply_list);