GDB (xrefs)
Loading...
Searching...
No Matches
ada-tasks.c
Go to the documentation of this file.
1/* Copyright (C) 1992-2023 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
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.
9
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.
14
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/>. */
17
18#include "defs.h"
19#include "observable.h"
20#include "gdbcmd.h"
21#include "target.h"
22#include "ada-lang.h"
23#include "gdbcore.h"
24#include "inferior.h"
25#include "gdbthread.h"
26#include "progspace.h"
27#include "objfiles.h"
28#include "cli/cli-style.h"
29
30static int ada_build_task_list ();
31
32/* The name of the array in the GNAT runtime where the Ada Task Control
33 Block of each task is stored. */
34#define KNOWN_TASKS_NAME "system__tasking__debug__known_tasks"
35
36/* The maximum number of tasks known to the Ada runtime. */
37static const int MAX_NUMBER_OF_KNOWN_TASKS = 1000;
38
39/* The name of the variable in the GNAT runtime where the head of a task
40 chain is saved. This is an alternate mechanism to find the list of known
41 tasks. */
42#define KNOWN_TASKS_LIST "system__tasking__debug__first_task"
43
65
66/* A short description corresponding to each possible task state. */
67static const char * const task_states[] = {
68 N_("Unactivated"),
69 N_("Runnable"),
70 N_("Terminated"),
71 N_("Child Activation Wait"),
72 N_("Accept or Select Term"),
73 N_("Waiting on entry call"),
74 N_("Async Select Wait"),
75 N_("Delay Sleep"),
76 N_("Child Termination Wait"),
77 N_("Wait Child in Term Alt"),
78 "",
79 "",
80 "",
81 "",
82 N_("Asynchronous Hold"),
83 "",
84 N_("Activating"),
85 N_("Selective Wait")
86};
87
88/* Return a string representing the task state. */
89static const char *
90get_state (unsigned value)
91{
92 if (value >= 0
93 && value <= ARRAY_SIZE (task_states)
94 && task_states[value][0] != '\0')
95 return _(task_states[value]);
96
97 static char buffer[100];
98 xsnprintf (buffer, sizeof (buffer), _("Unknown task state: %d"), value);
99 return buffer;
100}
101
102/* A longer description corresponding to each possible task state. */
103static const char * const long_task_states[] = {
104 N_("Unactivated"),
105 N_("Runnable"),
106 N_("Terminated"),
107 N_("Waiting for child activation"),
108 N_("Blocked in accept or select with terminate"),
109 N_("Waiting on entry call"),
110 N_("Asynchronous Selective Wait"),
111 N_("Delay Sleep"),
112 N_("Waiting for children termination"),
113 N_("Waiting for children in terminate alternative"),
114 "",
115 "",
116 "",
117 "",
118 N_("Asynchronous Hold"),
119 "",
120 N_("Activating"),
121 N_("Blocked in selective wait statement")
122};
123
124/* Return a string representing the task state. This uses the long
125 descriptions. */
126static const char *
128{
129 if (value >= 0
130 && value <= ARRAY_SIZE (long_task_states)
131 && long_task_states[value][0] != '\0')
132 return _(long_task_states[value]);
133
134 static char buffer[100];
135 xsnprintf (buffer, sizeof (buffer), _("Unknown task state: %d"), value);
136 return buffer;
137}
138
139/* The index of certain important fields in the Ada Task Control Block
140 record and sub-records. */
141
143{
144 /* Fields in record Ada_Task_Control_Block. */
148
149 /* Fields in record Common_ATCB. */
150 int state;
153 int image;
154 int image_len; /* This field may be missing. */
156 int call;
157 int ll;
159
160 /* Fields in Task_Primitives.Private_Data. */
162 int ll_lwp; /* This field may be missing. */
163
164 /* Fields in Common_ATCB.Call.all. */
166};
167
168/* This module's per-program-space data. */
169
171{
172 /* Nonzero if the data has been initialized. If set to zero,
173 it means that the data has either not been initialized, or
174 has potentially become stale. */
176
177 /* The ATCB record type. */
178 struct type *atcb_type = nullptr;
179
180 /* The ATCB "Common" component type. */
181 struct type *atcb_common_type = nullptr;
182
183 /* The type of the "ll" field, from the atcb_common_type. */
184 struct type *atcb_ll_type = nullptr;
185
186 /* The type of the "call" field, from the atcb_common_type. */
187 struct type *atcb_call_type = nullptr;
188
189 /* The index of various fields in the ATCB record and sub-records. */
191
192 /* On some systems, gdbserver applies an offset to the CPU that is
193 reported. */
194 unsigned int cpu_id_offset = 0;
195};
196
197/* Key to our per-program-space data. */
200
201/* The kind of data structure used by the runtime to store the list
202 of Ada tasks. */
203
205{
206 /* Use this value when we haven't determined which kind of structure
207 is being used, or when we need to recompute it.
208
209 We set the value of this enumerate to zero on purpose: This allows
210 us to use this enumerate in a structure where setting all fields
211 to zero will result in this kind being set to unknown. */
213
214 /* This value means that we did not find any task list. Unless
215 there is a bug somewhere, this means that the inferior does not
216 use tasking. */
218
219 /* This value means that the task list is stored as an array.
220 This is the usual method, as it causes very little overhead.
221 But this method is not always used, as it does use a certain
222 amount of memory, which might be scarse in certain environments. */
224
225 /* This value means that the task list is stored as a linked list.
226 This has more runtime overhead than the array approach, but
227 also require less memory when the number of tasks is small. */
229};
230
231/* This module's per-inferior data. */
232
234{
235 /* The type of data structure used by the runtime to store
236 the list of Ada tasks. The value of this field influences
237 the interpretation of the known_tasks_addr field below:
238 - ADA_TASKS_UNKNOWN: The value of known_tasks_addr hasn't
239 been determined yet;
240 - ADA_TASKS_NOT_FOUND: The program probably does not use tasking
241 and the known_tasks_addr is irrelevant;
242 - ADA_TASKS_ARRAY: The known_tasks is an array;
243 - ADA_TASKS_LIST: The known_tasks is a list. */
245
246 /* The address of the known_tasks structure. This is where
247 the runtime stores the information for all Ada tasks.
248 The interpretation of this field depends on KNOWN_TASKS_KIND
249 above. */
250 CORE_ADDR known_tasks_addr = 0;
251
252 /* Type of elements of the known task. Usually a pointer. */
253 struct type *known_tasks_element = nullptr;
254
255 /* Number of elements in the known tasks array. */
256 unsigned int known_tasks_length = 0;
257
258 /* When nonzero, this flag indicates that the task_list field
259 below is up to date. When set to zero, the list has either
260 not been initialized, or has potentially become stale. */
261 bool task_list_valid_p = false;
262
263 /* The list of Ada tasks.
264
265 Note: To each task we associate a number that the user can use to
266 reference it - this number is printed beside each task in the tasks
267 info listing displayed by "info tasks". This number is equal to
268 its index in the vector + 1. Reciprocally, to compute the index
269 of a task in the vector, we need to substract 1 from its number. */
270 std::vector<ada_task_info> task_list;
271};
272
273/* Key to our per-inferior data. */
276
277/* Return a string with TASKNO followed by the task name if TASK_INFO
278 contains a name. */
279
280static std::string
281task_to_str (int taskno, const ada_task_info *task_info)
282{
283 if (task_info->name[0] == '\0')
284 return string_printf ("%d", taskno);
285 else
286 return string_printf ("%d \"%s\"", taskno, task_info->name);
287}
288
289/* Return the ada-tasks module's data for the given program space (PSPACE).
290 If none is found, add a zero'ed one now.
291
292 This function always returns a valid object. */
293
294static struct ada_tasks_pspace_data *
296{
297 struct ada_tasks_pspace_data *data;
298
299 data = ada_tasks_pspace_data_handle.get (pspace);
300 if (data == NULL)
301 data = ada_tasks_pspace_data_handle.emplace (pspace);
302
303 return data;
304}
305
306/* Return the ada-tasks module's data for the given inferior (INF).
307 If none is found, add a zero'ed one now.
308
309 This function always returns a valid object.
310
311 Note that we could use an observer of the inferior-created event
312 to make sure that the ada-tasks per-inferior data always exists.
313 But we preferred this approach, as it avoids this entirely as long
314 as the user does not use any of the tasking features. This is
315 quite possible, particularly in the case where the inferior does
316 not use tasking. */
317
318static struct ada_tasks_inferior_data *
320{
321 struct ada_tasks_inferior_data *data;
322
324 if (data == NULL)
325 data = ada_tasks_inferior_data_handle.emplace (inf);
326
327 return data;
328}
329
330/* Return the task number of the task whose thread is THREAD, or zero
331 if the task could not be found. */
332
333int
335{
336 struct inferior *inf = thread->inf;
337 struct ada_tasks_inferior_data *data;
338
339 gdb_assert (inf != NULL);
341
342 for (int i = 0; i < data->task_list.size (); i++)
343 if (data->task_list[i].ptid == thread->ptid)
344 return i + 1;
345
346 return 0; /* No matching task found. */
347}
348
349/* Return the task number of the task running in inferior INF which
350 matches TASK_ID , or zero if the task could not be found. */
351
352static int
353get_task_number_from_id (CORE_ADDR task_id, struct inferior *inf)
354{
356
357 for (int i = 0; i < data->task_list.size (); i++)
358 {
359 if (data->task_list[i].task_id == task_id)
360 return i + 1;
361 }
362
363 /* Task not found. Return 0. */
364 return 0;
365}
366
367/* Return non-zero if TASK_NUM is a valid task number. */
368
369int
370valid_task_id (int task_num)
371{
372 struct ada_tasks_inferior_data *data;
373
376 return task_num > 0 && task_num <= data->task_list.size ();
377}
378
379/* Return non-zero iff the task STATE corresponds to a non-terminated
380 task state. */
381
382static int
383ada_task_is_alive (const struct ada_task_info *task_info)
384{
385 return (task_info->state != Terminated);
386}
387
388/* Search through the list of known tasks for the one whose ptid is
389 PTID, and return it. Return NULL if the task was not found. */
390
391struct ada_task_info *
393{
394 struct ada_tasks_inferior_data *data;
395
398
399 for (ada_task_info &task : data->task_list)
400 {
401 if (task.ptid == ptid)
402 return &task;
403 }
404
405 return NULL;
406}
407
408/* Call the ITERATOR function once for each Ada task that hasn't been
409 terminated yet. */
410
411void
413{
414 struct ada_tasks_inferior_data *data;
415
418
419 for (ada_task_info &task : data->task_list)
420 {
421 if (!ada_task_is_alive (&task))
422 continue;
423 iterator (&task);
424 }
425}
426
427/* Extract the contents of the value as a string whose length is LENGTH,
428 and store the result in DEST. */
429
430static void
431value_as_string (char *dest, struct value *val, int length)
432{
433 memcpy (dest, val->contents ().data (), length);
434 dest[length] = '\0';
435}
436
437/* Extract the string image from the fat string corresponding to VAL,
438 and store it in DEST. If the string length is greater than MAX_LEN,
439 then truncate the result to the first MAX_LEN characters of the fat
440 string. */
441
442static void
443read_fat_string_value (char *dest, struct value *val, int max_len)
444{
445 struct value *array_val;
446 struct value *bounds_val;
447 int len;
448
449 /* The following variables are made static to avoid recomputing them
450 each time this function is called. */
451 static int initialize_fieldnos = 1;
452 static int array_fieldno;
453 static int bounds_fieldno;
454 static int upper_bound_fieldno;
455
456 /* Get the index of the fields that we will need to read in order
457 to extract the string from the fat string. */
458 if (initialize_fieldnos)
459 {
460 struct type *type = val->type ();
461 struct type *bounds_type;
462
463 array_fieldno = ada_get_field_index (type, "P_ARRAY", 0);
464 bounds_fieldno = ada_get_field_index (type, "P_BOUNDS", 0);
465
466 bounds_type = type->field (bounds_fieldno).type ();
467 if (bounds_type->code () == TYPE_CODE_PTR)
468 bounds_type = bounds_type->target_type ();
469 if (bounds_type->code () != TYPE_CODE_STRUCT)
470 error (_("Unknown task name format. Aborting"));
471 upper_bound_fieldno = ada_get_field_index (bounds_type, "UB0", 0);
472
473 initialize_fieldnos = 0;
474 }
475
476 /* Get the size of the task image by checking the value of the bounds.
477 The lower bound is always 1, so we only need to read the upper bound. */
478 bounds_val = value_ind (value_field (val, bounds_fieldno));
479 len = value_as_long (value_field (bounds_val, upper_bound_fieldno));
480
481 /* Make sure that we do not read more than max_len characters... */
482 if (len > max_len)
483 len = max_len;
484
485 /* Extract LEN characters from the fat string. */
486 array_val = value_ind (value_field (val, array_fieldno));
487 read_memory (array_val->address (), (gdb_byte *) dest, len);
488
489 /* Add the NUL character to close the string. */
490 dest[len] = '\0';
491}
492
493/* Get, from the debugging information, the type description of all types
494 related to the Ada Task Control Block that are needed in order to
495 read the list of known tasks in the Ada runtime. If all of the info
496 needed to do so is found, then save that info in the module's per-
497 program-space data, and return NULL. Otherwise, if any information
498 cannot be found, leave the per-program-space data untouched, and
499 return an error message explaining what was missing (that error
500 message does NOT need to be deallocated). */
501
502const char *
504{
505 struct type *type;
506 struct type *common_type;
507 struct type *ll_type;
508 struct type *call_type;
509 struct atcb_fieldnos fieldnos;
510 struct ada_tasks_pspace_data *pspace_data;
511
512 const char *atcb_name = "system__tasking__ada_task_control_block___XVE";
513 const char *atcb_name_fixed = "system__tasking__ada_task_control_block";
514 const char *common_atcb_name = "system__tasking__common_atcb";
515 const char *private_data_name = "system__task_primitives__private_data";
516 const char *entry_call_record_name = "system__tasking__entry_call_record";
517
518 /* ATCB symbols may be found in several compilation units. As we
519 are only interested in one instance, use standard (literal,
520 C-like) lookups to get the first match. */
521
522 struct symbol *atcb_sym =
524 language_c, NULL).symbol;
525 const struct symbol *common_atcb_sym =
526 lookup_symbol_in_language (common_atcb_name, NULL, STRUCT_DOMAIN,
527 language_c, NULL).symbol;
528 const struct symbol *private_data_sym =
529 lookup_symbol_in_language (private_data_name, NULL, STRUCT_DOMAIN,
530 language_c, NULL).symbol;
531 const struct symbol *entry_call_record_sym =
532 lookup_symbol_in_language (entry_call_record_name, NULL, STRUCT_DOMAIN,
533 language_c, NULL).symbol;
534
535 if (atcb_sym == NULL || atcb_sym->type () == NULL)
536 {
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,
541 NULL).symbol;
542
543 if (atcb_sym == NULL || atcb_sym->type () == NULL)
544 return _("Cannot find Ada_Task_Control_Block type");
545
546 type = atcb_sym->type ();
547 }
548 else
549 {
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);
554 }
555
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");
562
563 /* Get the type for Ada_Task_Control_Block.Common. */
564 common_type = common_atcb_sym->type ();
565
566 /* Get the type for Ada_Task_Control_Bloc.Common.Call.LL. */
567 ll_type = private_data_sym->type ();
568
569 /* Get the type for Common_ATCB.Call.all. */
570 call_type = entry_call_record_sym->type ();
571
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);
590
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);
599
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)
605 {
606 CORE_ADDR addr = first_id_sym.value_address ();
607 /* This symbol always has type uint32_t. */
608 struct type *u32type = builtin_type (target_gdbarch ())->builtin_uint32;
609 first_id = value_as_long (value_at (u32type, addr));
610 }
611
612 /* Set all the out parameters all at once, now that we are certain
613 that there are no potential error() anymore. */
615 pspace_data->initialized_p = 1;
616 pspace_data->atcb_type = type;
617 pspace_data->atcb_common_type = common_type;
618 pspace_data->atcb_ll_type = ll_type;
619 pspace_data->atcb_call_type = call_type;
620 pspace_data->atcb_fieldno = fieldnos;
621 pspace_data->cpu_id_offset = first_id;
622 return NULL;
623}
624
625/* Build the PTID of the task from its COMMON_VALUE, which is the "Common"
626 component of its ATCB record. This PTID needs to match the PTID used
627 by the thread layer. */
628
629static ptid_t
630ptid_from_atcb_common (struct value *common_value)
631{
632 ULONGEST thread;
633 CORE_ADDR lwp = 0;
634 struct value *ll_value;
635 ptid_t ptid;
636 const struct ada_tasks_pspace_data *pspace_data
638
639 ll_value = value_field (common_value, pspace_data->atcb_fieldno.ll);
640
641 if (pspace_data->atcb_fieldno.ll_lwp >= 0)
642 lwp = value_as_address (value_field (ll_value,
643 pspace_data->atcb_fieldno.ll_lwp));
644 thread = value_as_long (value_field (ll_value,
645 pspace_data->atcb_fieldno.ll_thread));
646
647 ptid = target_get_ada_task_ptid (lwp, thread);
648
649 return ptid;
650}
651
652/* Read the ATCB data of a given task given its TASK_ID (which is in practice
653 the address of its associated ATCB record), and store the result inside
654 TASK_INFO. */
655
656static void
657read_atcb (CORE_ADDR task_id, struct ada_task_info *task_info)
658{
659 struct value *tcb_value;
660 struct value *common_value;
661 struct value *atc_nesting_level_value;
662 struct value *entry_calls_value;
663 struct value *entry_calls_value_element;
664 int called_task_fieldno = -1;
665 static const char ravenscar_task_name[] = "Ravenscar task";
666 const struct ada_tasks_pspace_data *pspace_data
668
669 /* Clear the whole structure to start with, so that everything
670 is always initialized the same. */
671 memset (task_info, 0, sizeof (struct ada_task_info));
672
673 if (!pspace_data->initialized_p)
674 {
675 const char *err_msg = ada_get_tcb_types_info ();
676
677 if (err_msg != NULL)
678 error (_("%s. Aborting"), err_msg);
679 }
680
681 tcb_value = value_from_contents_and_address (pspace_data->atcb_type,
682 NULL, task_id);
683 common_value = value_field (tcb_value, pspace_data->atcb_fieldno.common);
684
685 /* Fill in the task_id. */
686
687 task_info->task_id = task_id;
688
689 /* Compute the name of the task.
690
691 Depending on the GNAT version used, the task image is either a fat
692 string, or a thin array of characters. Older versions of GNAT used
693 to use fat strings, and therefore did not need an extra field in
694 the ATCB to store the string length. For efficiency reasons, newer
695 versions of GNAT replaced the fat string by a static buffer, but this
696 also required the addition of a new field named "Image_Len" containing
697 the length of the task name. The method used to extract the task name
698 is selected depending on the existence of this field.
699
700 In some run-time libs (e.g. Ravenscar), the name is not in the ATCB;
701 we may want to get it from the first user frame of the stack. For now,
702 we just give a dummy name. */
703
704 if (pspace_data->atcb_fieldno.image_len == -1)
705 {
706 if (pspace_data->atcb_fieldno.image >= 0)
707 read_fat_string_value (task_info->name,
708 value_field (common_value,
709 pspace_data->atcb_fieldno.image),
710 sizeof (task_info->name) - 1);
711 else
712 {
713 struct bound_minimal_symbol msym;
714
715 msym = lookup_minimal_symbol_by_pc (task_id);
716 if (msym.minsym)
717 {
718 const char *full_name = msym.minsym->linkage_name ();
719 const char *task_name = full_name;
720 const char *p;
721
722 /* Strip the prefix. */
723 for (p = full_name; *p; p++)
724 if (p[0] == '_' && p[1] == '_')
725 task_name = p + 2;
726
727 /* Copy the task name. */
728 strncpy (task_info->name, task_name,
729 sizeof (task_info->name) - 1);
730 task_info->name[sizeof (task_info->name) - 1] = 0;
731 }
732 else
733 {
734 /* No symbol found. Use a default name. */
735 strcpy (task_info->name, ravenscar_task_name);
736 }
737 }
738 }
739 else
740 {
741 int len = value_as_long
742 (value_field (common_value,
743 pspace_data->atcb_fieldno.image_len));
744
745 value_as_string (task_info->name,
746 value_field (common_value,
747 pspace_data->atcb_fieldno.image),
748 len);
749 }
750
751 /* Compute the task state and priority. */
752
753 task_info->state =
754 value_as_long (value_field (common_value,
755 pspace_data->atcb_fieldno.state));
756 task_info->priority =
757 value_as_long (value_field (common_value,
758 pspace_data->atcb_fieldno.priority));
759
760 /* If the ATCB contains some information about the parent task,
761 then compute it as well. Otherwise, zero. */
762
763 if (pspace_data->atcb_fieldno.parent >= 0)
764 task_info->parent =
765 value_as_address (value_field (common_value,
766 pspace_data->atcb_fieldno.parent));
767
768 /* If the task is in an entry call waiting for another task,
769 then determine which task it is. */
770
771 if (task_info->state == Entry_Caller_Sleep
772 && pspace_data->atcb_fieldno.atc_nesting_level > 0
773 && pspace_data->atcb_fieldno.entry_calls > 0)
774 {
775 /* Let My_ATCB be the Ada task control block of a task calling the
776 entry of another task; then the Task_Id of the called task is
777 in My_ATCB.Entry_Calls (My_ATCB.ATC_Nesting_Level).Called_Task. */
778 atc_nesting_level_value =
779 value_field (tcb_value, pspace_data->atcb_fieldno.atc_nesting_level);
780 entry_calls_value =
782 (value_field (tcb_value, pspace_data->atcb_fieldno.entry_calls));
783 entry_calls_value_element =
784 value_subscript (entry_calls_value,
785 value_as_long (atc_nesting_level_value));
786 called_task_fieldno =
787 ada_get_field_index (entry_calls_value_element->type (),
788 "called_task", 0);
789 task_info->called_task =
790 value_as_address (value_field (entry_calls_value_element,
791 called_task_fieldno));
792 }
793
794 /* If the ATCB contains some information about RV callers, then
795 compute the "caller_task". Otherwise, leave it as zero. */
796
797 if (pspace_data->atcb_fieldno.call >= 0)
798 {
799 /* Get the ID of the caller task from Common_ATCB.Call.all.Self.
800 If Common_ATCB.Call is null, then there is no caller. */
801 const CORE_ADDR call =
802 value_as_address (value_field (common_value,
803 pspace_data->atcb_fieldno.call));
804 struct value *call_val;
805
806 if (call != 0)
807 {
808 call_val =
810 NULL, call);
811 task_info->caller_task =
813 (value_field (call_val, pspace_data->atcb_fieldno.call_self));
814 }
815 }
816
817 task_info->base_cpu
818 = (pspace_data->cpu_id_offset
819 + value_as_long (value_field (common_value,
820 pspace_data->atcb_fieldno.base_cpu)));
821
822 /* And finally, compute the task ptid. Note that there is not point
823 in computing it if the task is no longer alive, in which case
824 it is good enough to set its ptid to the null_ptid. */
825 if (ada_task_is_alive (task_info))
826 task_info->ptid = ptid_from_atcb_common (common_value);
827 else
828 task_info->ptid = null_ptid;
829}
830
831/* Read the ATCB info of the given task (identified by TASK_ID), and
832 add the result to the given inferior's TASK_LIST. */
833
834static void
835add_ada_task (CORE_ADDR task_id, struct inferior *inf)
836{
837 struct ada_task_info task_info;
839
840 read_atcb (task_id, &task_info);
841 data->task_list.push_back (task_info);
842}
843
844/* Read the Known_Tasks array from the inferior memory, and store
845 it in the current inferior's TASK_LIST. Return true upon success. */
846
847static bool
849{
850 const int target_ptr_byte = data->known_tasks_element->length ();
851 const int known_tasks_size = target_ptr_byte * data->known_tasks_length;
852 gdb_byte *known_tasks = (gdb_byte *) alloca (known_tasks_size);
853 int i;
854
855 /* Build a new list by reading the ATCBs from the Known_Tasks array
856 in the Ada runtime. */
857 read_memory (data->known_tasks_addr, known_tasks, known_tasks_size);
858 for (i = 0; i < data->known_tasks_length; i++)
859 {
860 CORE_ADDR task_id =
861 extract_typed_address (known_tasks + i * target_ptr_byte,
862 data->known_tasks_element);
863
864 if (task_id != 0)
865 add_ada_task (task_id, current_inferior ());
866 }
867
868 return true;
869}
870
871/* Read the known tasks from the inferior memory, and store it in
872 the current inferior's TASK_LIST. Return true upon success. */
873
874static bool
876{
877 const int target_ptr_byte = data->known_tasks_element->length ();
878 gdb_byte *known_tasks = (gdb_byte *) alloca (target_ptr_byte);
879 CORE_ADDR task_id;
880 const struct ada_tasks_pspace_data *pspace_data
882
883 /* Sanity check. */
884 if (pspace_data->atcb_fieldno.activation_link < 0)
885 return false;
886
887 /* Build a new list by reading the ATCBs. Read head of the list. */
888 read_memory (data->known_tasks_addr, known_tasks, target_ptr_byte);
889 task_id = extract_typed_address (known_tasks, data->known_tasks_element);
890 while (task_id != 0)
891 {
892 struct value *tcb_value;
893 struct value *common_value;
894
895 add_ada_task (task_id, current_inferior ());
896
897 /* Read the chain. */
898 tcb_value = value_from_contents_and_address (pspace_data->atcb_type,
899 NULL, task_id);
900 common_value = value_field (tcb_value, pspace_data->atcb_fieldno.common);
901 task_id = value_as_address
902 (value_field (common_value,
903 pspace_data->atcb_fieldno.activation_link));
904 }
905
906 return true;
907}
908
909/* Set all fields of the current inferior ada-tasks data pointed by DATA.
910 Do nothing if those fields are already set and still up to date. */
911
912static void
914{
915 struct bound_minimal_symbol msym;
916 struct symbol *sym;
917
918 /* Return now if already set. */
919 if (data->known_tasks_kind != ADA_TASKS_UNKNOWN)
920 return;
921
922 /* Try array. */
923
924 msym = lookup_minimal_symbol (KNOWN_TASKS_NAME, NULL, NULL);
925 if (msym.minsym != NULL)
926 {
927 data->known_tasks_kind = ADA_TASKS_ARRAY;
928 data->known_tasks_addr = msym.value_address ();
929
930 /* Try to get pointer type and array length from the symtab. */
932 language_c, NULL).symbol;
933 if (sym != NULL)
934 {
935 /* Validate. */
936 struct type *type = check_typedef (sym->type ());
937 struct type *eltype = NULL;
938 struct type *idxtype = NULL;
939
940 if (type->code () == TYPE_CODE_ARRAY)
941 eltype = check_typedef (type->target_type ());
942 if (eltype != NULL
943 && eltype->code () == TYPE_CODE_PTR)
944 idxtype = check_typedef (type->index_type ());
945 if (idxtype != NULL
946 && idxtype->bounds ()->low.is_constant ()
947 && idxtype->bounds ()->high.is_constant ())
948 {
949 data->known_tasks_element = eltype;
950 data->known_tasks_length =
951 (idxtype->bounds ()->high.const_val ()
952 - idxtype->bounds ()->low.const_val () + 1);
953 return;
954 }
955 }
956
957 /* Fallback to default values. The runtime may have been stripped (as
958 in some distributions), but it is likely that the executable still
959 contains debug information on the task type (due to implicit with of
960 Ada.Tasking). */
961 data->known_tasks_element =
963 data->known_tasks_length = MAX_NUMBER_OF_KNOWN_TASKS;
964 return;
965 }
966
967
968 /* Try list. */
969
970 msym = lookup_minimal_symbol (KNOWN_TASKS_LIST, NULL, NULL);
971 if (msym.minsym != NULL)
972 {
973 data->known_tasks_kind = ADA_TASKS_LIST;
974 data->known_tasks_addr = msym.value_address ();
975 data->known_tasks_length = 1;
976
978 language_c, NULL).symbol;
979 if (sym != NULL && sym->value_address () != 0)
980 {
981 /* Validate. */
982 struct type *type = check_typedef (sym->type ());
983
984 if (type->code () == TYPE_CODE_PTR)
985 {
986 data->known_tasks_element = type;
987 return;
988 }
989 }
990
991 /* Fallback to default values. */
992 data->known_tasks_element =
994 data->known_tasks_length = 1;
995 return;
996 }
997
998 /* Can't find tasks. */
999
1000 data->known_tasks_kind = ADA_TASKS_NOT_FOUND;
1001 data->known_tasks_addr = 0;
1002}
1003
1004/* Read the known tasks from the current inferior's memory, and store it
1005 in the current inferior's data TASK_LIST. */
1006
1007static void
1009{
1010 struct ada_tasks_inferior_data *data =
1012
1013 /* Step 1: Clear the current list, if necessary. */
1014 data->task_list.clear ();
1015
1016 /* Step 2: do the real work.
1017 If the application does not use task, then no more needs to be done.
1018 It is important to have the task list cleared (see above) before we
1019 return, as we don't want a stale task list to be used... This can
1020 happen for instance when debugging a non-multitasking program after
1021 having debugged a multitasking one. */
1023 gdb_assert (data->known_tasks_kind != ADA_TASKS_UNKNOWN);
1024
1025 /* Step 3: Set task_list_valid_p, to avoid re-reading the Known_Tasks
1026 array unless needed. */
1027 switch (data->known_tasks_kind)
1028 {
1029 case ADA_TASKS_NOT_FOUND: /* Tasking not in use in inferior. */
1030 break;
1031 case ADA_TASKS_ARRAY:
1032 data->task_list_valid_p = read_known_tasks_array (data);
1033 break;
1034 case ADA_TASKS_LIST:
1035 data->task_list_valid_p = read_known_tasks_list (data);
1036 break;
1037 }
1038}
1039
1040/* Build the task_list by reading the Known_Tasks array from
1041 the inferior, and return the number of tasks in that list
1042 (zero means that the program is not using tasking at all). */
1043
1044static int
1046{
1047 struct ada_tasks_inferior_data *data;
1048
1049 if (!target_has_stack ())
1050 error (_("Cannot inspect Ada tasks when program is not running"));
1051
1053 if (!data->task_list_valid_p)
1055
1056 return data->task_list.size ();
1057}
1058
1059/* Print a table providing a short description of all Ada tasks
1060 running inside inferior INF. If ARG_STR is set, it will be
1061 interpreted as a task number, and the table will be limited to
1062 that task only. */
1063
1064void
1066 const char *arg_str,
1067 struct inferior *inf)
1068{
1069 struct ada_tasks_inferior_data *data;
1070 int taskno, nb_tasks;
1071 int taskno_arg = 0;
1072 int nb_columns;
1073
1074 if (ada_build_task_list () == 0)
1075 {
1076 uiout->message (_("Your application does not use any Ada tasks.\n"));
1077 return;
1078 }
1079
1080 if (arg_str != NULL && arg_str[0] != '\0')
1081 taskno_arg = value_as_long (parse_and_eval (arg_str));
1082
1083 if (uiout->is_mi_like_p ())
1084 /* In GDB/MI mode, we want to provide the thread ID corresponding
1085 to each task. This allows clients to quickly find the thread
1086 associated to any task, which is helpful for commands that
1087 take a --thread argument. However, in order to be able to
1088 provide that thread ID, the thread list must be up to date
1089 first. */
1091
1093
1094 /* Compute the number of tasks that are going to be displayed
1095 in the output. If an argument was given, there will be
1096 at most 1 entry. Otherwise, there will be as many entries
1097 as we have tasks. */
1098 if (taskno_arg)
1099 {
1100 if (taskno_arg > 0 && taskno_arg <= data->task_list.size ())
1101 nb_tasks = 1;
1102 else
1103 nb_tasks = 0;
1104 }
1105 else
1106 nb_tasks = data->task_list.size ();
1107
1108 nb_columns = uiout->is_mi_like_p () ? 8 : 7;
1109 ui_out_emit_table table_emitter (uiout, nb_columns, nb_tasks, "tasks");
1110 uiout->table_header (1, ui_left, "current", "");
1111 uiout->table_header (3, ui_right, "id", "ID");
1112 {
1113 size_t tid_width = 9;
1114 /* Grown below in case the largest entry is bigger. */
1115
1116 if (!uiout->is_mi_like_p ())
1117 {
1118 for (taskno = 1; taskno <= data->task_list.size (); taskno++)
1119 {
1120 const struct ada_task_info *const task_info
1121 = &data->task_list[taskno - 1];
1122
1123 gdb_assert (task_info != NULL);
1124
1125 tid_width = std::max (tid_width,
1126 1 + strlen (phex_nz (task_info->task_id,
1127 sizeof (CORE_ADDR))));
1128 }
1129 }
1130 uiout->table_header (tid_width, ui_right, "task-id", "TID");
1131 }
1132 /* The following column is provided in GDB/MI mode only because
1133 it is only really useful in that mode, and also because it
1134 allows us to keep the CLI output shorter and more compact. */
1135 if (uiout->is_mi_like_p ())
1136 uiout->table_header (4, ui_right, "thread-id", "");
1137 uiout->table_header (4, ui_right, "parent-id", "P-ID");
1138 uiout->table_header (3, ui_right, "priority", "Pri");
1139 uiout->table_header (22, ui_left, "state", "State");
1140 /* Use ui_noalign for the last column, to prevent the CLI uiout
1141 from printing an extra space at the end of each row. This
1142 is a bit of a hack, but does get the job done. */
1143 uiout->table_header (1, ui_noalign, "name", "Name");
1144 uiout->table_body ();
1145
1146 for (taskno = 1; taskno <= data->task_list.size (); taskno++)
1147 {
1148 const struct ada_task_info *const task_info =
1149 &data->task_list[taskno - 1];
1150 int parent_id;
1151
1152 gdb_assert (task_info != NULL);
1153
1154 /* If the user asked for the output to be restricted
1155 to one task only, and this is not the task, skip
1156 to the next one. */
1157 if (taskno_arg && taskno != taskno_arg)
1158 continue;
1159
1160 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1161
1162 /* Print a star if this task is the current task (or the task
1163 currently selected). */
1164 if (task_info->ptid == inferior_ptid)
1165 uiout->field_string ("current", "*");
1166 else
1167 uiout->field_skip ("current");
1168
1169 /* Print the task number. */
1170 uiout->field_signed ("id", taskno);
1171
1172 /* Print the Task ID. */
1173 uiout->field_string ("task-id", phex_nz (task_info->task_id,
1174 sizeof (CORE_ADDR)));
1175
1176 /* Print the associated Thread ID. */
1177 if (uiout->is_mi_like_p ())
1178 {
1179 thread_info *thread = (ada_task_is_alive (task_info)
1180 ? inf->find_thread (task_info->ptid)
1181 : nullptr);
1182
1183 if (thread != NULL)
1184 uiout->field_signed ("thread-id", thread->global_num);
1185 else
1186 {
1187 /* This can happen if the thread is no longer alive. */
1188 uiout->field_skip ("thread-id");
1189 }
1190 }
1191
1192 /* Print the ID of the parent task. */
1193 parent_id = get_task_number_from_id (task_info->parent, inf);
1194 if (parent_id)
1195 uiout->field_signed ("parent-id", parent_id);
1196 else
1197 uiout->field_skip ("parent-id");
1198
1199 /* Print the base priority of the task. */
1200 uiout->field_signed ("priority", task_info->priority);
1201
1202 /* Print the task current state. */
1203 if (task_info->caller_task)
1204 uiout->field_fmt ("state",
1205 _("Accepting RV with %-4d"),
1207 inf));
1208 else if (task_info->called_task)
1209 uiout->field_fmt ("state",
1210 _("Waiting on RV with %-3d"),
1212 inf));
1213 else
1214 uiout->field_string ("state", get_state (task_info->state));
1215
1216 /* Finally, print the task name, without quotes around it, as mi like
1217 is not expecting quotes, and in non mi-like no need for quotes
1218 as there is a specific column for the name. */
1219 uiout->field_fmt ("name",
1220 (task_info->name[0] != '\0'
1221 ? ui_file_style ()
1222 : metadata_style.style ()),
1223 "%s",
1224 (task_info->name[0] != '\0'
1225 ? task_info->name
1226 : _("<no name>")));
1227
1228 uiout->text ("\n");
1229 }
1230}
1231
1232/* Print a detailed description of the Ada task whose ID is TASKNO_STR
1233 for the given inferior (INF). */
1234
1235static void
1236info_task (struct ui_out *uiout, const char *taskno_str, struct inferior *inf)
1237{
1238 const int taskno = value_as_long (parse_and_eval (taskno_str));
1239 struct ada_task_info *task_info;
1240 int parent_taskno = 0;
1242
1243 if (ada_build_task_list () == 0)
1244 {
1245 uiout->message (_("Your application does not use any Ada tasks.\n"));
1246 return;
1247 }
1248
1249 if (taskno <= 0 || taskno > data->task_list.size ())
1250 error (_("Task ID %d not known. Use the \"info tasks\" command to\n"
1251 "see the IDs of currently known tasks"), taskno);
1252 task_info = &data->task_list[taskno - 1];
1253
1254 /* Print the Ada task ID. */
1255 gdb_printf (_("Ada Task: %s\n"),
1256 paddress (target_gdbarch (), task_info->task_id));
1257
1258 /* Print the name of the task. */
1259 if (task_info->name[0] != '\0')
1260 gdb_printf (_("Name: %s\n"), task_info->name);
1261 else
1262 fprintf_styled (gdb_stdout, metadata_style.style (), _("<no name>\n"));
1263
1264 /* Print the TID and LWP. */
1265 gdb_printf (_("Thread: 0x%s\n"), phex_nz (task_info->ptid.tid (),
1266 sizeof (ULONGEST)));
1267 gdb_printf (_("LWP: %#lx\n"), task_info->ptid.lwp ());
1268
1269 /* If set, print the base CPU. */
1270 if (task_info->base_cpu != 0)
1271 gdb_printf (_("Base CPU: %d\n"), task_info->base_cpu);
1272
1273 /* Print who is the parent (if any). */
1274 if (task_info->parent != 0)
1275 parent_taskno = get_task_number_from_id (task_info->parent, inf);
1276 if (parent_taskno)
1277 {
1278 struct ada_task_info *parent = &data->task_list[parent_taskno - 1];
1279
1280 gdb_printf (_("Parent: %d"), parent_taskno);
1281 if (parent->name[0] != '\0')
1282 gdb_printf (" (%s)", parent->name);
1283 gdb_printf ("\n");
1284 }
1285 else
1286 gdb_printf (_("No parent\n"));
1287
1288 /* Print the base priority. */
1289 gdb_printf (_("Base Priority: %d\n"), task_info->priority);
1290
1291 /* print the task current state. */
1292 {
1293 int target_taskno = 0;
1294
1295 if (task_info->caller_task)
1296 {
1297 target_taskno = get_task_number_from_id (task_info->caller_task, inf);
1298 gdb_printf (_("State: Accepting rendezvous with %d"),
1299 target_taskno);
1300 }
1301 else if (task_info->called_task)
1302 {
1303 target_taskno = get_task_number_from_id (task_info->called_task, inf);
1304 gdb_printf (_("State: Waiting on task %d's entry"),
1305 target_taskno);
1306 }
1307 else
1308 gdb_printf (_("State: %s"), get_long_state (task_info->state));
1309
1310 if (target_taskno)
1311 {
1312 ada_task_info *target_task_info = &data->task_list[target_taskno - 1];
1313
1314 if (target_task_info->name[0] != '\0')
1315 gdb_printf (" (%s)", target_task_info->name);
1316 }
1317
1318 gdb_printf ("\n");
1319 }
1320}
1321
1322/* If ARG is empty or null, then print a list of all Ada tasks.
1323 Otherwise, print detailed information about the task whose ID
1324 is ARG.
1325
1326 Does nothing if the program doesn't use Ada tasking. */
1327
1328static void
1329info_tasks_command (const char *arg, int from_tty)
1330{
1331 struct ui_out *uiout = current_uiout;
1332
1333 if (arg == NULL || *arg == '\0')
1334 print_ada_task_info (uiout, NULL, current_inferior ());
1335 else
1336 info_task (uiout, arg, current_inferior ());
1337}
1338
1339/* Print a message telling the user id of the current task.
1340 This function assumes that tasking is in use in the inferior. */
1341
1342static void
1344{
1345 const int current_task = ada_get_task_number (inferior_thread ());
1346
1347 if (current_task == 0)
1348 gdb_printf (_("[Current task is unknown]\n"));
1349 else
1350 {
1351 struct ada_tasks_inferior_data *data
1353 struct ada_task_info *task_info = &data->task_list[current_task - 1];
1354
1355 gdb_printf (_("[Current task is %s]\n"),
1356 task_to_str (current_task, task_info).c_str ());
1357 }
1358}
1359
1360/* Parse and evaluate TIDSTR into a task id, and try to switch to
1361 that task. Print an error message if the task switch failed. */
1362
1363static void
1364task_command_1 (const char *taskno_str, int from_tty, struct inferior *inf)
1365{
1366 const int taskno = value_as_long (parse_and_eval (taskno_str));
1367 struct ada_task_info *task_info;
1369
1370 if (taskno <= 0 || taskno > data->task_list.size ())
1371 error (_("Task ID %d not known. Use the \"info tasks\" command to\n"
1372 "see the IDs of currently known tasks"), taskno);
1373 task_info = &data->task_list[taskno - 1];
1374
1375 if (!ada_task_is_alive (task_info))
1376 error (_("Cannot switch to task %s: Task is no longer running"),
1377 task_to_str (taskno, task_info).c_str ());
1378
1379 /* On some platforms, the thread list is not updated until the user
1380 performs a thread-related operation (by using the "info threads"
1381 command, for instance). So this thread list may not be up to date
1382 when the user attempts this task switch. Since we cannot switch
1383 to the thread associated to our task if GDB does not know about
1384 that thread, we need to make sure that any new threads gets added
1385 to the thread list. */
1387
1388 /* Verify that the ptid of the task we want to switch to is valid
1389 (in other words, a ptid that GDB knows about). Otherwise, we will
1390 cause an assertion failure later on, when we try to determine
1391 the ptid associated thread_info data. We should normally never
1392 encounter such an error, but the wrong ptid can actually easily be
1393 computed if target_get_ada_task_ptid has not been implemented for
1394 our target (yet). Rather than cause an assertion error in that case,
1395 it's nicer for the user to just refuse to perform the task switch. */
1396 thread_info *tp = inf->find_thread (task_info->ptid);
1397 if (tp == NULL)
1398 error (_("Unable to compute thread ID for task %s.\n"
1399 "Cannot switch to this task."),
1400 task_to_str (taskno, task_info).c_str ());
1401
1402 switch_to_thread (tp);
1404 gdb_printf (_("[Switching to task %s]\n"),
1405 task_to_str (taskno, task_info).c_str ());
1408 SRC_AND_LOC, 1);
1409}
1410
1411
1412/* Print the ID of the current task if TASKNO_STR is empty or NULL.
1413 Otherwise, switch to the task indicated by TASKNO_STR. */
1414
1415static void
1416task_command (const char *taskno_str, int from_tty)
1417{
1418 struct ui_out *uiout = current_uiout;
1419
1420 if (ada_build_task_list () == 0)
1421 {
1422 uiout->message (_("Your application does not use any Ada tasks.\n"));
1423 return;
1424 }
1425
1426 if (taskno_str == NULL || taskno_str[0] == '\0')
1428 else
1429 task_command_1 (taskno_str, from_tty, current_inferior ());
1430}
1431
1432/* Indicate that the given inferior's task list may have changed,
1433 so invalidate the cache. */
1434
1435static void
1437{
1439
1440 data->task_list_valid_p = false;
1441}
1442
1443/* Invalidate the per-program-space data. */
1444
1445static void
1450
1451/* Invalidate the per-inferior data. */
1452
1453static void
1455{
1457
1458 data->known_tasks_kind = ADA_TASKS_UNKNOWN;
1459 data->task_list_valid_p = false;
1460}
1461
1462/* The 'normal_stop' observer notification callback. */
1463
1464static void
1465ada_tasks_normal_stop_observer (struct bpstat *unused_args, int unused_args2)
1466{
1467 /* The inferior has been resumed, and just stopped. This means that
1468 our task_list needs to be recomputed before it can be used again. */
1470}
1471
1472/* Clear data associated to PSPACE and all inferiors using that program
1473 space. */
1474
1475static void
1477{
1478 /* The associated program-space data might have changed after
1479 this objfile was added. Invalidate all cached data. */
1481
1482 /* Invalidate the per-inferior cache for all inferiors using
1483 this program space. */
1484 for (inferior *inf : all_inferiors ())
1485 if (inf->pspace == pspace)
1487}
1488
1489/* Called when a new objfile was added. */
1490
1491static void
1496
1497/* The qcs command line flags for the "task apply" commands. Keep
1498 this in sync with the "frame apply" commands. */
1499
1505 "q", [] (qcs_flags *opt) { return &opt->quiet; },
1506 N_("Disables printing the task information."),
1507 },
1508
1510 "c", [] (qcs_flags *opt) { return &opt->cont; },
1511 N_("Print any error raised by COMMAND and continue."),
1512 },
1513
1515 "s", [] (qcs_flags *opt) { return &opt->silent; },
1516 N_("Silently ignore any errors or empty output produced by COMMAND."),
1517 },
1518};
1519
1520/* Create an option_def_group for the "task apply all" options, with
1521 FLAGS as context. */
1522
1523static inline std::array<gdb::option::option_def_group, 1>
1525{
1526 return {{
1528 }};
1529}
1530
1531/* Create an option_def_group for the "task apply" options, with
1532 FLAGS as context. */
1533
1538}
1539
1540/* Implementation of 'task apply all'. */
1541
1542static void
1543task_apply_all_command (const char *cmd, int from_tty)
1544{
1546
1550
1551 validate_flags_qcs ("task apply all", &flags);
1552
1553 if (cmd == nullptr || *cmd == '\0')
1554 error (_("Please specify a command at the end of 'task apply all'"));
1555
1558
1561
1562 /* Save a copy of the thread list and increment each thread's
1563 refcount while executing the command in the context of each
1564 thread, in case the command affects this. */
1565 std::vector<std::pair<int, thread_info_ref>> thr_list_cpy;
1566
1567 for (int i = 1; i <= data->task_list.size (); ++i)
1568 {
1569 ada_task_info &task = data->task_list[i - 1];
1570 if (!ada_task_is_alive (&task))
1571 continue;
1572
1573 thread_info *tp = inf->find_thread (task.ptid);
1574 if (tp == nullptr)
1575 warning (_("Unable to compute thread ID for task %s.\n"
1576 "Cannot switch to this task."),
1577 task_to_str (i, &task).c_str ());
1578 else
1579 thr_list_cpy.emplace_back (i, thread_info_ref::new_reference (tp));
1580 }
1581
1582 scoped_restore_current_thread restore_thread;
1583
1584 for (const auto &info : thr_list_cpy)
1585 if (switch_to_thread_if_alive (info.second.get ()))
1586 thread_try_catch_cmd (info.second.get (), info.first, cmd,
1587 from_tty, flags);
1588}
1589
1590/* Implementation of 'task apply'. */
1591
1592static void
1593task_apply_command (const char *tidlist, int from_tty)
1594{
1595
1596 if (tidlist == nullptr || *tidlist == '\0')
1597 error (_("Please specify a task ID list"));
1598
1601
1604
1605 /* Save a copy of the thread list and increment each thread's
1606 refcount while executing the command in the context of each
1607 thread, in case the command affects this. */
1608 std::vector<std::pair<int, thread_info_ref>> thr_list_cpy;
1609
1610 number_or_range_parser parser (tidlist);
1611 while (!parser.finished ())
1612 {
1613 int num = parser.get_number ();
1614
1615 if (num < 1 || num - 1 >= data->task_list.size ())
1616 warning (_("no Ada Task with number %d"), num);
1617 else
1618 {
1619 ada_task_info &task = data->task_list[num - 1];
1620 if (!ada_task_is_alive (&task))
1621 continue;
1622
1623 thread_info *tp = inf->find_thread (task.ptid);
1624 if (tp == nullptr)
1625 warning (_("Unable to compute thread ID for task %s.\n"
1626 "Cannot switch to this task."),
1627 task_to_str (num, &task).c_str ());
1628 else
1629 thr_list_cpy.emplace_back (num,
1630 thread_info_ref::new_reference (tp));
1631 }
1632 }
1633
1635 const char *cmd = parser.cur_tok ();
1636
1640
1641 validate_flags_qcs ("task apply", &flags);
1642
1643 if (*cmd == '\0')
1644 error (_("Please specify a command following the task ID list"));
1645
1646 scoped_restore_current_thread restore_thread;
1647
1648 for (const auto &info : thr_list_cpy)
1649 if (switch_to_thread_if_alive (info.second.get ()))
1650 thread_try_catch_cmd (info.second.get (), info.first, cmd,
1651 from_tty, flags);
1652}
1653
1654void _initialize_tasks ();
1655void
1657{
1658 /* Attach various observers. */
1660 "ada-tasks");
1662 "ada-tasks");
1664 "ada-tasks");
1665
1666 static struct cmd_list_element *task_cmd_list;
1667 static struct cmd_list_element *task_apply_list;
1668
1669
1670 /* Some new commands provided by this module. */
1671 add_info ("tasks", info_tasks_command,
1672 _("Provide information about all known Ada tasks."));
1673
1675 _("Use this command to switch between Ada tasks.\n\
1676Without argument, this command simply prints the current task ID."),
1677 &task_cmd_list, 1, &cmdlist);
1678
1679#define TASK_APPLY_OPTION_HELP "\
1680Prints per-inferior task number followed by COMMAND output.\n\
1681\n\
1682By default, an error raised during the execution of COMMAND\n\
1683aborts \"task apply\".\n\
1684\n\
1685Options:\n\
1686%OPTIONS%"
1687
1688 static const auto task_apply_opts
1690
1691 static std::string task_apply_help = gdb::option::build_help (_("\
1692Apply a command to a list of tasks.\n\
1693Usage: task apply ID... [OPTION]... COMMAND\n\
1694ID is a space-separated list of IDs of tasks to apply COMMAND on.\n"
1695TASK_APPLY_OPTION_HELP), task_apply_opts);
1696
1697 add_prefix_cmd ("apply", class_run,
1699 task_apply_help.c_str (),
1700 &task_apply_list, 1,
1701 &task_cmd_list);
1702
1703 static const auto task_apply_all_opts
1705
1706 static std::string task_apply_all_help = gdb::option::build_help (_("\
1707Apply a command to all tasks in the current inferior.\n\
1708\n\
1709Usage: task apply all [OPTION]... COMMAND\n"
1710TASK_APPLY_OPTION_HELP), task_apply_all_opts);
1711
1713 task_apply_all_help.c_str (), &task_apply_list);
1714}
struct value * ada_coerce_to_simple_array_ptr(struct value *arr)
Definition ada-lang.c:2183
void ada_find_printable_frame(frame_info_ptr fi)
Definition ada-lang.c:11850
struct type * ada_template_to_fixed_record_type_1(struct type *type, const gdb_byte *valaddr, CORE_ADDR address, struct value *dval0, int keep_dynamic_fields)
Definition ada-lang.c:7771
int ada_get_field_index(const struct type *type, const char *field_name, int maybe_missing)
Definition ada-lang.c:501
gdb::function_view< void(struct ada_task_info *task) ada_task_list_iterator_ftype)
Definition ada-lang.h:374
static void info_tasks_command(const char *arg, int from_tty)
Definition ada-tasks.c:1329
static int get_task_number_from_id(CORE_ADDR task_id, struct inferior *inf)
Definition ada-tasks.c:353
static void read_fat_string_value(char *dest, struct value *val, int max_len)
Definition ada-tasks.c:443
int ada_get_task_number(thread_info *thread)
Definition ada-tasks.c:334
#define KNOWN_TASKS_LIST
Definition ada-tasks.c:42
static const registry< program_space >::key< ada_tasks_pspace_data > ada_tasks_pspace_data_handle
Definition ada-tasks.c:199
static void ada_tasks_inferior_data_sniffer(struct ada_tasks_inferior_data *data)
Definition ada-tasks.c:913
static const gdb::option::option_def task_qcs_flags_option_defs[]
Definition ada-tasks.c:1502
static bool read_known_tasks_list(struct ada_tasks_inferior_data *data)
Definition ada-tasks.c:875
struct ada_task_info * ada_get_task_info_from_ptid(ptid_t ptid)
Definition ada-tasks.c:392
static void display_current_task_id(void)
Definition ada-tasks.c:1343
static void task_command(const char *taskno_str, int from_tty)
Definition ada-tasks.c:1416
static const char *const long_task_states[]
Definition ada-tasks.c:103
static const registry< inferior >::key< ada_tasks_inferior_data > ada_tasks_inferior_data_handle
Definition ada-tasks.c:275
static void value_as_string(char *dest, struct value *val, int length)
Definition ada-tasks.c:431
static gdb::option::option_def_group make_task_apply_options_def_group(qcs_flags *flags)
Definition ada-tasks.c:1534
static void task_command_1(const char *taskno_str, int from_tty, struct inferior *inf)
Definition ada-tasks.c:1364
int valid_task_id(int task_num)
Definition ada-tasks.c:370
static std::string task_to_str(int taskno, const ada_task_info *task_info)
Definition ada-tasks.c:281
void _initialize_tasks()
Definition ada-tasks.c:1655
#define TASK_APPLY_OPTION_HELP
static int ada_task_is_alive(const struct ada_task_info *task_info)
Definition ada-tasks.c:383
static const char * get_long_state(unsigned value)
Definition ada-tasks.c:127
static void read_known_tasks()
Definition ada-tasks.c:1008
static void info_task(struct ui_out *uiout, const char *taskno_str, struct inferior *inf)
Definition ada-tasks.c:1236
static void task_apply_all_command(const char *cmd, int from_tty)
Definition ada-tasks.c:1542
static void ada_tasks_clear_pspace_data(program_space *pspace)
Definition ada-tasks.c:1476
static bool read_known_tasks_array(struct ada_tasks_inferior_data *data)
Definition ada-tasks.c:848
#define KNOWN_TASKS_NAME
Definition ada-tasks.c:34
void print_ada_task_info(struct ui_out *uiout, const char *arg_str, struct inferior *inf)
Definition ada-tasks.c:1065
static void ada_tasks_invalidate_pspace_data(struct program_space *pspace)
Definition ada-tasks.c:1446
ada_known_tasks_kind
Definition ada-tasks.c:205
@ ADA_TASKS_UNKNOWN
Definition ada-tasks.c:212
@ ADA_TASKS_NOT_FOUND
Definition ada-tasks.c:217
@ ADA_TASKS_ARRAY
Definition ada-tasks.c:223
@ ADA_TASKS_LIST
Definition ada-tasks.c:228
static void ada_tasks_invalidate_inferior_data(struct inferior *inf)
Definition ada-tasks.c:1454
static const char * get_state(unsigned value)
Definition ada-tasks.c:90
static struct ada_tasks_pspace_data * get_ada_tasks_pspace_data(struct program_space *pspace)
Definition ada-tasks.c:295
static void add_ada_task(CORE_ADDR task_id, struct inferior *inf)
Definition ada-tasks.c:835
static int ada_build_task_list()
Definition ada-tasks.c:1045
const char * ada_get_tcb_types_info(void)
Definition ada-tasks.c:503
static void ada_task_list_changed(struct inferior *inf)
Definition ada-tasks.c:1436
static void task_apply_command(const char *tidlist, int from_tty)
Definition ada-tasks.c:1592
task_states
Definition ada-tasks.c:45
@ Terminated
Definition ada-tasks.c:48
@ Timer_Server_Sleep
Definition ada-tasks.c:58
@ Entry_Caller_Sleep
Definition ada-tasks.c:51
@ Acceptor_Delay_Sleep
Definition ada-tasks.c:63
@ Unactivated
Definition ada-tasks.c:46
@ Activating
Definition ada-tasks.c:62
@ Master_Completion_Sleep
Definition ada-tasks.c:54
@ Acceptor_Sleep
Definition ada-tasks.c:50
@ Activator_Sleep
Definition ada-tasks.c:49
@ Delay_Sleep
Definition ada-tasks.c:53
@ Interrupt_Server_Blocked_Interrupt_Sleep
Definition ada-tasks.c:57
@ AST_Server_Sleep
Definition ada-tasks.c:59
@ Async_Select_Sleep
Definition ada-tasks.c:52
@ Asynchronous_Hold
Definition ada-tasks.c:60
@ Runnable
Definition ada-tasks.c:47
@ Interrupt_Server_Blocked_On_Event_Flag
Definition ada-tasks.c:61
@ Interrupt_Server_Idle_Sleep
Definition ada-tasks.c:56
@ Master_Phase_2_Sleep
Definition ada-tasks.c:55
void iterate_over_live_ada_tasks(ada_task_list_iterator_ftype iterator)
Definition ada-tasks.c:412
static void ada_tasks_normal_stop_observer(struct bpstat *unused_args, int unused_args2)
Definition ada-tasks.c:1465
static void ada_tasks_new_objfile_observer(objfile *objfile)
Definition ada-tasks.c:1492
static struct ada_tasks_inferior_data * get_ada_tasks_inferior_data(struct inferior *inf)
Definition ada-tasks.c:319
static const int MAX_NUMBER_OF_KNOWN_TASKS
Definition ada-tasks.c:37
static void read_atcb(CORE_ADDR task_id, struct ada_task_info *task_info)
Definition ada-tasks.c:657
static ptid_t ptid_from_atcb_common(struct value *common_value)
Definition ada-tasks.c:630
static std::array< gdb::option::option_def_group, 1 > make_task_apply_all_options_def_group(qcs_flags *flags)
Definition ada-tasks.c:1523
struct gdbarch * target_gdbarch(void)
ui_file_style style() const
Definition cli-style.c:169
bool finished() const
Definition cli-utils.c:327
const char * cur_tok() const
Definition cli-utils.h:106
void * get(unsigned key)
Definition registry.h:211
ptid_t ptid
Definition gdbthread.h:259
struct inferior * inf
Definition gdbthread.h:301
void field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
Definition ui-out.c:511
void field_fmt(const char *fldname, const char *format,...) ATTRIBUTE_PRINTF(3
Definition ui-out.c:525
void field_signed(const char *fldname, LONGEST value)
Definition ui-out.c:437
void field_skip(const char *fldname)
Definition ui-out.c:499
void text(const char *string)
Definition ui-out.c:566
void table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr)
Definition ui-out.c:363
bool is_mi_like_p() const
Definition ui-out.c:810
void table_body()
Definition ui-out.c:376
void message(const char *format,...) ATTRIBUTE_PRINTF(2
Definition ui-out.c:774
struct cmd_list_element * cmdlist
Definition cli-cmds.c:87
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
struct cmd_list_element * add_prefix_cmd(const char *name, enum command_class theclass, cmd_simple_func_ftype *fun, const char *doc, struct cmd_list_element **subcommands, int allow_unknown, struct cmd_list_element **list)
Definition cli-decode.c:357
struct cmd_list_element * add_info(const char *name, cmd_simple_func_ftype *fun, const char *doc)
cli_style_option metadata_style
void validate_flags_qcs(const char *which_command, qcs_flags *flags)
Definition cli-utils.c:436
@ class_run
Definition command.h:54
void read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition corefile.c:238
CORE_ADDR extract_typed_address(const gdb_byte *buf, struct type *type)
Definition findvar.c:152
@ language_c
Definition defs.h:213
struct value * parse_and_eval(const char *exp, parser_flags flags)
Definition eval.c:70
int frame_relative_level(frame_info_ptr fi)
Definition frame.c:2946
frame_info_ptr get_selected_frame(const char *message)
Definition frame.c:1888
@ SRC_AND_LOC
Definition frame.h:811
void print_stack_frame(frame_info_ptr, int print_level, enum print_what print_what, int set_current_sal)
Definition stack.c:353
bool switch_to_thread_if_alive(thread_info *thr)
Definition thread.c:716
void update_thread_list(void)
Definition thread.c:2086
struct thread_info * inferior_thread(void)
Definition thread.c:85
void switch_to_thread(struct thread_info *thr)
Definition thread.c:1360
void thread_try_catch_cmd(thread_info *thr, gdb::optional< int > ada_task, const char *cmd, int from_tty, const qcs_flags &flags)
Definition thread.c:1523
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition gdbtypes.c:6168
struct type * check_typedef(struct type *type)
Definition gdbtypes.c:2966
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition gnu-nat.c:1861
ptid_t inferior_ptid
Definition infcmd.c:74
struct inferior * current_inferior(void)
Definition inferior.c:55
all_inferiors_range all_inferiors(process_stratum_target *proc_target=nullptr)
Definition inferior.h:821
struct bound_minimal_symbol lookup_minimal_symbol(const char *name, const char *sfile, struct objfile *objf)
Definition minsyms.c:363
struct bound_minimal_symbol lookup_bound_minimal_symbol(const char *name)
Definition minsyms.c:481
struct bound_minimal_symbol lookup_minimal_symbol_by_pc(CORE_ADDR pc)
Definition minsyms.c:996
observable< struct objfile * > new_objfile
observable< program_space * > all_objfiles_removed
observable< struct bpstat *, int > normal_stop
bool process_options(const char **args, process_options_mode mode, gdb::array_view< const option_def_group > options_group)
Definition cli-option.c:627
@ PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
Definition cli-option.h:337
std::string build_help(const char *help_tmpl, gdb::array_view< const option_def_group > options_group)
Definition cli-option.c:766
struct program_space * current_program_space
Definition progspace.c:40
enum var_types type
Definition scm-param.c:142
CORE_ADDR task_id
Definition ada-lang.h:123
char name[257]
Definition ada-lang.h:126
CORE_ADDR caller_task
Definition ada-lang.h:143
CORE_ADDR called_task
Definition ada-lang.h:139
ptid_t ptid
Definition ada-lang.h:120
CORE_ADDR parent
Definition ada-lang.h:135
unsigned int known_tasks_length
Definition ada-tasks.c:256
std::vector< ada_task_info > task_list
Definition ada-tasks.c:270
enum ada_known_tasks_kind known_tasks_kind
Definition ada-tasks.c:244
struct type * known_tasks_element
Definition ada-tasks.c:253
struct type * atcb_call_type
Definition ada-tasks.c:187
struct type * atcb_type
Definition ada-tasks.c:178
struct type * atcb_ll_type
Definition ada-tasks.c:184
unsigned int cpu_id_offset
Definition ada-tasks.c:194
struct type * atcb_common_type
Definition ada-tasks.c:181
int atc_nesting_level
Definition ada-tasks.c:147
int activation_link
Definition ada-tasks.c:155
struct symbol * symbol
Definition symtab.h:1533
CORE_ADDR value_address() const
Definition minsyms.h:41
struct minimal_symbol * minsym
Definition minsyms.h:49
struct type * builtin_data_ptr
Definition gdbtypes.h:2135
struct type * builtin_uint32
Definition gdbtypes.h:2120
LONGEST const_val() const
Definition gdbtypes.h:330
bool is_constant() const
Definition gdbtypes.h:345
struct type * type() const
Definition gdbtypes.h:547
const char * linkage_name() const
Definition symtab.h:460
Definition gnu-nat.c:153
struct program_space * pspace
Definition objfiles.h:728
struct dynamic_prop high
Definition gdbtypes.h:721
struct dynamic_prop low
Definition gdbtypes.h:717
struct type * type() const
Definition symtab.h:1331
CORE_ADDR value_address() const
Definition symtab.h:1361
struct type * target_type() const
Definition gdbtypes.h:1037
type_code code() const
Definition gdbtypes.h:956
struct field & field(int idx) const
Definition gdbtypes.h:1012
range_bounds * bounds() const
Definition gdbtypes.h:1065
type * index_type() const
Definition gdbtypes.h:1032
Definition value.h:130
gdb::array_view< const gdb_byte > contents()
Definition value.c:1262
struct type * type() const
Definition value.h:180
CORE_ADDR address
Definition value.h:658
struct block_symbol lookup_symbol_in_language(const char *name, const struct block *block, const domain_enum domain, enum language lang, struct field_of_this_result *is_a_field_of_this)
Definition symtab.c:1946
@ VAR_DOMAIN
Definition symtab.h:910
@ STRUCT_DOMAIN
Definition symtab.h:916
ptid_t target_get_ada_task_ptid(long lwp, ULONGEST tid)
Definition target.c:602
int target_has_stack()
Definition target.c:177
void target_update_thread_list(void)
Definition target.c:3776
@ ui_noalign
Definition ui-out.h:48
@ ui_right
Definition ui-out.h:47
@ ui_left
Definition ui-out.h:45
#define current_uiout
Definition ui-out.h:40
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition utils.c:3166
void fprintf_styled(struct ui_file *stream, const ui_file_style &style, const char *format,...)
Definition utils.c:1898
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stdout
Definition utils.h:182
struct value * value_subscript(struct value *array, LONGEST index)
Definition valarith.c:141
struct value * value_at(struct type *type, CORE_ADDR addr)
Definition valops.c:1015
struct value * value_ind(struct value *arg1)
Definition valops.c:1630
struct value * value_field(struct value *arg1, int fieldno)
Definition value.c:3052
CORE_ADDR value_as_address(struct value *val)
Definition value.c:2636
LONGEST value_as_long(struct value *val)
Definition value.c:2554
struct value * value_from_contents_and_address(struct type *type, const gdb_byte *valaddr, CORE_ADDR address, frame_info_ptr frame)
Definition value.c:3552