ada-tasks.c 53 KB

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