1 /* File ada-tasks.c: Ada tasking control for GDB
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Contributed by Ada Core Technologies, Inc.
6 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
18 #include <gdb_string.h>
27 #include "gdbthread.h"
28 #include "regcache.h" /* for registers_changed */
30 #if defined (__fsu__) || defined (HAVE_SPYTHREAD) \
31 || (defined(__alpha__) && defined(__osf__) && !defined(__alpha_vxworks))
32 #include <sys/procfs.h>
35 #if defined (__fsu__) || defined (HAVE_SPYTHREAD) \
36 || (defined(__alpha__) && defined(__osf__) && !defined(VXWORKS_TARGET))
40 #ifdef I386_GNULINUX_TARGET
41 #include "gdb_thread_db.h"
44 #if defined (HAVE_SPYTHREAD)
45 #include "irix6-spyThread.h"
61 Master_Completion_Sleep,
65 /* The index of certain important fields in the Ada Task Control Block
66 record and sub-records. */
70 /* Fields in record Ada_Task_Control_Block. */
73 /* Fields in record Common_ATCB. */
78 int image_len; /* This field may be missing. */
82 /* Fields in Task_Primitives.Private_Data. */
84 int ll_lwp; /* This field may be missing. */
88 #define TASK_LWP(atcb) 0L
90 #define TASK_LWP(atcb) extract_unsigned_integer (&(atcb).lwp, sizeof ((atcb).lwp))
93 /* FIXME: move all this conditional compilation in description
94 files or in configure.in */
96 #if defined(__alpha__) && defined(__osf__) && !defined(VXWORKS_TARGET)
97 #define SPECIAL_THREAD_SUPPORT_ACTIVE() thread_support
98 #define SAVE_TASK_REGISTERS(task) \
99 do { fill_gregset (&gregset_saved, -1); \
100 fill_fpregset (&fpregset_saved, -1); \
102 #define RESTORE_TASK_REGISTERS(task) \
103 do { supply_gregset (&gregset_saved); \
104 supply_fpregset (&fpregset_saved); \
107 #define THREAD_FETCH_REGISTERS dec_thread_fetch_registers
108 #define GET_CURRENT_THREAD(PTID) dec_thread_get_current_thread ()
109 extern unsigned long dec_thread_get_current_thread (void);
110 extern int dec_thread_get_registers (gdb_gregset_t *, gdb_fpregset_t *);
111 extern int dec_thread_fetch_registers (void);
115 #define SPECIAL_THREAD_SUPPORT_ACTIVE() \
116 (thread_support && pthread_kern_addr != 0)
117 #define SAVE_TASK_REGISTERS(task) \
119 if (pthread_kern_addr != 0) \
121 fill_gregset (&gregset_saved, -1); \
122 fill_fpregset (&fpregset_saved, -1); \
125 #define RESTORE_TASK_REGISTERS(task) \
127 if (pthread_kern_addr != 0) \
129 supply_gregset (&gregset_saved); \
130 supply_fpregset (&fpregset_saved); \
134 extern int fsu_thread_fetch_registers (void);
135 extern unsigned long fsu_thread_get_current_thread (void);
136 static int fsu_or_linux_thread_fetch_registers (void);
137 static long fsu_or_linux_thread_get_current_thread (ptid_t);
138 #define THREAD_FETCH_REGISTERS fsu_or_linux_thread_fetch_registers
139 #define GET_CURRENT_THREAD(PTID) fsu_or_linux_thread_get_current_thread (PTID)
140 #define PTHREAD_KERN "pthread_kern"
143 #ifdef I386_GNULINUX_TARGET
144 extern td_thrinfo_t thread_db_pid_to_thread_info (int pid);
145 extern int thread_db_tid_to_pid (void *tid);
148 #if defined(VXWORKS_TARGET)
149 #define GET_CURRENT_THREAD(PTID) (unsigned long) ptid_get_pid (PTID)
150 #define THREAD_FETCH_REGISTERS() (-1)
152 #elif defined (__WIN32__) || defined (__CYGWIN__) || defined (hpux)
153 #define GET_CURRENT_THREAD(PTID) ptid_get_pid (PTID)
154 #define THREAD_FETCH_REGISTERS() (-1)
157 #if defined (HAVE_SPYTHREAD)
158 #define GET_CURRENT_THREAD(PTID) (unsigned long) TIDGET (PTID)
161 #if !defined(GET_CURRENT_THREAD)
162 #define GET_CURRENT_THREAD(PTID) (unsigned long) ptid_get_tid (PTID)
165 #if !defined(THREAD_FETCH_REGISTERS)
166 #define THREAD_FETCH_REGISTERS() (target_fetch_registers (-1), 0)
169 #if !defined(SAVE_TASK_REGISTERS)
170 #define SAVE_TASK_REGISTERS(task)
171 #define RESTORE_TASK_REGISTERS(task)
174 #if !defined(SPECIAL_THREAD_SUPPORT_ACTIVE)
175 #define SPECIAL_THREAD_SUPPORT_ACTIVE() 0
178 #define KNOWN_TASKS_NAME "system__tasking__debug__known_tasks"
180 #define READ_MEMORY(addr, var) read_memory (addr, (char *) &var, sizeof (var))
182 /* If defined to 1, means that the thread ptids maintained by core GDB
183 follow this format : first field (pid) contains the tid
184 second field (lwp) contains 0
185 third field (tid) contains 0 */
186 #ifndef THREAD_PTID_CONTAINS_TID_NULL_NULL
187 #define THREAD_PTID_CONTAINS_TID_NULL_NULL (0)
190 /* If defined to 1, means that the thread ptids maintained by core GDB
191 follow this format: first field (pid) contains the LWP id
192 second field (lwp) contains 0
193 third field (tid) contains 0 */
194 #ifndef THREAD_PTID_CONTAINS_LWP_NULL_NULL
195 #define THREAD_PTID_CONTAINS_LWP_NULL_NULL (0)
198 /* If defined to 1, means that the thread ptids maintained by core GDB
199 follow this format: first field (pid) contains the PID
200 second field (lwp) contains 0
201 third field (tid) contains the TID */
202 #ifndef THREAD_PTID_CONTAINS_PID_NULL_TID
203 #define THREAD_PTID_CONTAINS_PID_NULL_TID (0)
206 /* If defined to 1, means that the thread ptids maintained by core GDB
207 follow this format: first field (pid) contains the PID
208 second field (lwp) contains the TID
209 third field (tid) contains the 0 */
211 #ifndef THREAD_PTID_CONTAINS_PID_TID_NULL
212 #define THREAD_PTID_CONTAINS_PID_TID_NULL (0)
215 /* If defined to 1, means that the thread id is not stored in the tid
216 field of the task_ptid, but rather in the lwp field. */
217 #ifndef ADA_THREAD_ID_IN_LWP
218 #define ADA_THREAD_ID_IN_LWP (0)
221 static int task_ptid_get_pid (task_ptid_t task_ptid);
222 static long task_ptid_get_lwp (task_ptid_t task_ptid);
223 static long task_ptid_get_tid (task_ptid_t task_ptid);
224 static task_ptid_t task_ptid_build (int pid, long lwp, long tid);
225 static ptid_t task_ptid_get_ptid (task_ptid_t task_ptid);
226 static long task_ptid_get_thread_id (task_ptid_t task_ptid);
228 static int task_is_alive (enum task_states state);
229 static CORE_ADDR get_self_id (ptid_t);
230 static int get_entry_number (CORE_ADDR);
231 static void get_tcb_types_info (struct type **atcb_type,
232 struct type **atcb_common_type,
233 struct type **atcb_ll_type,
234 struct tcb_fieldnos *atcb_fieldnos);
235 static void get_tcb_call_type_info (struct type **atcb_call_type,
236 int *atcb_call_self_fieldno);
237 static CORE_ADDR get_known_tasks_addr (void);
238 static int read_known_tasks_array (void);
239 static void value_as_string (char *dest, struct value *val, int length);
240 static struct task_control_block read_atcb (CORE_ADDR atcb_addr);
241 static CORE_ADDR read_caller (const CORE_ADDR call);
242 static void display_current_task_id (void);
243 static void task_command_1 (char *tidstr, int from_tty);
245 /* Ada-tasks observers. */
247 static void normal_stop_notification (void);
248 static void ada_tasks_attach_observers (void);
250 /* Global visible variables */
252 int ada__tasks_check_symbol_table = 1;
253 CORE_ADDR pthread_kern_addr = 0;
254 struct task_entry *task_list = NULL;
256 /* Local global variables. */
258 /* When non-zero, this flag indicates that the current task_list
259 is obsolete, and should be recomputed before it is accessed. */
260 static int stale_task_list_p = 1;
262 #if defined (__fsu__) || (defined(__alpha__) && defined(__osf__) && !defined(VXWORKS_TARGET))
263 gdb_gregset_t gregset_saved;
264 gdb_fpregset_t fpregset_saved;
267 /* The maximum number of tasks known to the Ada runtime */
268 static const int MAX_NUMBER_OF_KNOWN_TASKS = 1000;
270 /* the current task, as seen by the user. Modified everytime the user
271 does a task switch. */
272 static int current_task = -1;
274 unsigned long current_thread;
276 /* The task where the debugger stopped, giving control back to the user.
277 Not affected by task switching. Used to restore the registers before
278 continuing the inferior. */
279 int current_task_id = -1;
281 static char *task_states[] = {
285 "Child Activation Wait",
287 "Waiting on entry call",
290 "Child Termination Wait",
291 "Wait Child in Term Alt",
299 /* Global internal types */
301 static char *long_task_states[] = {
305 "Waiting for child activation",
306 "Blocked in accept statement",
307 "Waiting on entry call",
308 "Asynchronous Selective Wait",
310 "Waiting for children termination",
311 "Waiting for children in terminate alternative",
319 /* Global internal variables */
321 static int highest_task_num = 0;
322 static int thread_support = 0; /* 1 if the thread library in use is
323 supported. FIXME: Not reinitialized
324 properly when program reloaded.
328 fsu_or_linux_thread_fetch_registers (void)
330 if (pthread_kern_addr != 0)
331 return fsu_thread_fetch_registers ();
333 target_fetch_registers (-1);
338 fsu_or_linux_thread_get_current_thread (ptid_t ptid)
340 if (pthread_kern_addr != 0)
341 return fsu_thread_get_current_thread ();
343 return ptid_get_tid (ptid);
349 add_task_entry (CORE_ADDR p_task_id, int index)
351 struct task_entry *new_task_entry = NULL;
352 struct task_entry *pt;
355 new_task_entry = xmalloc (sizeof (struct task_entry));
356 new_task_entry->task_num = highest_task_num;
357 new_task_entry->task_id = p_task_id;
358 new_task_entry->atcb = read_atcb (p_task_id);
359 new_task_entry->known_tasks_index = index;
360 new_task_entry->task_ptid =
361 task_ptid_build (ptid_get_pid (inferior_ptid), /* ? */
362 TASK_LWP (new_task_entry->atcb),
363 new_task_entry->atcb.thread);
364 new_task_entry->next_task = NULL;
368 while (pt->next_task)
370 pt->next_task = new_task_entry;
374 task_list = new_task_entry;
375 return new_task_entry->task_num;
379 get_entry_number (CORE_ADDR p_task_id)
381 struct task_entry *pt;
386 if (pt->task_id == p_task_id)
393 static struct task_entry *
394 get_thread_entry_vptr (long thread)
396 struct task_entry *pt;
401 if (task_ptid_get_thread_id (pt->task_ptid) == thread)
408 static struct task_entry *
409 get_entry_vptr (int p_task_num)
411 struct task_entry *pt;
416 if (pt->task_num == p_task_num)
424 init_task_list (void)
426 struct task_entry *pt, *old_pt;
428 target_find_new_threads ();
438 highest_task_num = 0;
442 valid_task_id (int task)
444 return get_entry_vptr (task) != NULL;
447 /* Return the pid of a given task ptid. */
450 task_ptid_get_pid (task_ptid_t task_ptid)
452 return task_ptid.pid;
455 /* Return the lwp of a given task ptid. */
458 task_ptid_get_lwp (task_ptid_t task_ptid)
460 return task_ptid.lwp;
463 /* Return the tid of a given task ptid. */
466 task_ptid_get_tid (task_ptid_t task_ptid)
468 return task_ptid.tid;
471 /* Build a task ptid from the associated pid, lwp, and tid. */
474 task_ptid_build (int pid, long lwp, long tid)
476 task_ptid_t task_ptid;
484 /* Translate a task ptid into a ptid (the ptid maintained by core GDB).
486 On most platforms, they are equivalent, and this function can be
487 regarded as the identity. However, there are other platforms where
488 the task ptid and the ptid are not equivalent. For instance, the task
489 LWP value is sometimes stored by GDB-core as a pid! This translation
490 therefore becomes necessary before invoking the GDB thread services. */
493 task_ptid_get_ptid (task_ptid_t task_ptid)
497 if (THREAD_PTID_CONTAINS_TID_NULL_NULL)
498 ptid = ptid_build (task_ptid_get_tid (task_ptid), 0, 0);
499 else if (THREAD_PTID_CONTAINS_LWP_NULL_NULL)
500 ptid = ptid_build (task_ptid_get_lwp (task_ptid), 0, 0);
501 else if (THREAD_PTID_CONTAINS_PID_NULL_TID)
502 ptid = ptid_build (task_ptid_get_pid (task_ptid),
503 0, task_ptid_get_tid (task_ptid));
504 else if (THREAD_PTID_CONTAINS_PID_TID_NULL)
505 ptid = ptid_build (task_ptid_get_pid (task_ptid),
506 task_ptid_get_tid (task_ptid), 0);
508 ptid = ptid_build (task_ptid_get_pid (task_ptid),
509 task_ptid_get_lwp (task_ptid),
510 task_ptid_get_tid (task_ptid));
515 /* Extract and return the thread_id for the given TASK_PTID. */
518 task_ptid_get_thread_id (task_ptid_t task_ptid)
520 /* On most platforms, the thread_id is stored in task_ptid.tid.
521 Unfortunately, some other platforms store it as the task_ptid.lwp... */
523 if (ADA_THREAD_ID_IN_LWP)
524 return task_ptid_get_lwp (task_ptid);
526 return task_ptid_get_tid (task_ptid);
529 /* Return non-zero iff the task STATE corresponds to a non-terminated
533 task_is_alive (enum task_states state)
535 return (state != Terminated);
539 get_self_id (ptid_t ptid)
542 struct task_entry *ent;
544 #if !((defined(sun) && defined(__SVR4)) || defined(VXWORKS_TARGET) || defined(__WIN32__))
548 ent = get_thread_entry_vptr (GET_CURRENT_THREAD (ptid));
549 return ent ? ent->task_id : 0;
553 /* FIXME: calling a function in the inferior with a multithreaded application
554 is not reliable, so return a null address if there is no safe way to
555 get the current task */
560 ada_get_current_task (ptid_t ptid)
564 if (current_language->la_language != language_ada)
567 result = get_entry_number (get_self_id (ptid));
569 /* return -1 if not found */
570 return result == 0 ? -1 : result;
573 /* Get from the debugging information the type description of all types
574 related to the Ada Task Control Block that will be needed in order to
575 read the list of known tasks in the Ada runtime. Also return the
576 associated ATCB_FIELDNOS.
578 Error handling: Any data missing from the debugging info will cause
579 an error to be raised, and none of the return values to be set.
580 Users of this function can depend on the fact that all or none of the
581 return values will be set. */
584 get_tcb_types_info (struct type **atcb_type,
585 struct type **atcb_common_type,
586 struct type **atcb_ll_type,
587 struct tcb_fieldnos *atcb_fieldnos)
590 struct type *common_type;
591 struct type *ll_type;
592 struct tcb_fieldnos fieldnos;
594 #ifndef ADA_RETAIN_DOTS
595 const char *atcb_name = "system__tasking__ada_task_control_block___XVE";
596 const char *common_atcb_name = "system__tasking__common_atcb";
597 const char *private_data_name = "system__task_primitives__private_data";
599 const char *atcb_name = "system.tasking.ada_task_control_block___XVE";
600 const char *common_atcb_name = "system.tasking.common_atcb";
601 const char *private_data_name = "system.task_primitives.private_data";
604 const struct symbol *atcb_sym =
605 lookup_symbol (atcb_name, NULL, VAR_DOMAIN, NULL, NULL);
606 const struct symbol *common_atcb_sym =
607 lookup_symbol (common_atcb_name, NULL, VAR_DOMAIN, NULL, NULL);
608 const struct symbol *private_data_sym =
609 lookup_symbol (private_data_name, NULL, VAR_DOMAIN, NULL, NULL);
611 if (atcb_sym == NULL || atcb_sym->type == NULL)
612 error ("Can not find Ada_Task_Control_Block type. Aborting");
613 if (common_atcb_sym == NULL || common_atcb_sym->type == NULL)
614 error ("Can not find Common_ATCB type. Aborting");
615 if (private_data_sym == NULL || private_data_sym->type == NULL)
616 error ("Can not find Private_Data type. Aborting");
618 /* Get a static representation of the type record Ada_Task_Control_Block. */
619 type = atcb_sym->type;
620 type = ada_template_to_fixed_record_type_1 (type, NULL, 0, NULL, 0);
622 /* Get the type for Ada_Task_Control_Block.Common. */
623 common_type = common_atcb_sym->type;
625 /* Get the type for Ada_Task_Control_Bloc.Common.Call.LL. */
626 ll_type = private_data_sym->type;
628 /* Get the field indices. */
629 fieldnos.common = ada_get_field_index (type, "common", 0);
630 fieldnos.state = ada_get_field_index (common_type, "state", 0);
631 fieldnos.parent = ada_get_field_index (common_type, "parent", 0);
632 fieldnos.priority = ada_get_field_index (common_type, "base_priority", 0);
633 fieldnos.image = ada_get_field_index (common_type, "task_image", 0);
634 fieldnos.image_len = ada_get_field_index (common_type, "task_image_len", 1);
635 fieldnos.call = ada_get_field_index (common_type, "call", 0);
636 fieldnos.ll = ada_get_field_index (common_type, "ll", 0);
637 fieldnos.ll_thread = ada_get_field_index (ll_type, "thread", 0);
638 fieldnos.ll_lwp = ada_get_field_index (ll_type, "lwp", 1);
640 /* On certain platforms such as x86-windows, the "lwp" field has been
641 named "thread_id". This field will likely be renamed in the future,
642 but we need to support both possibilities to avoid an unnecessary
643 dependency on a recent compiler. We therefore try locating the
644 "thread_id" field in place of the "lwp" field if we did not find
646 if (fieldnos.ll_lwp < 0)
647 fieldnos.ll_lwp = ada_get_field_index (ll_type, "thread_id", 1);
649 /* Set all the out parameters all at once, now that we are certain
650 that there are no potential error() anymore. */
652 *atcb_common_type = common_type;
653 *atcb_ll_type = ll_type;
654 *atcb_fieldnos = fieldnos;
657 /* Get from the debugging information the type description of the
658 record type Entry_Call_Record (this is the type of the field
659 Call.all in the Common_ATCB record type). Also return the index
660 of the field "Self" in Entry_Call_Record.
662 Error handling: Any data missing from the debugging info will cause
663 an error to be raised, and none of the return values to be set.
664 Users of this function can depend on the fact that all or none of the
665 return values will be set. */
668 get_tcb_call_type_info (struct type **atcb_call_type,
669 int *atcb_call_self_fieldno)
671 struct type *call_type;
672 int call_self_fieldno;
674 #ifndef ADA_RETAIN_DOTS
675 const char *entry_call_record_name = "system__tasking__entry_call_record";
677 const char *entry_call_record_name = "system.tasking.entry_call_record";
680 const struct symbol *entry_call_record_sym =
681 lookup_symbol (entry_call_record_name, NULL, VAR_DOMAIN, NULL, NULL);
683 if (entry_call_record_sym == NULL || entry_call_record_sym->type == NULL)
684 error ("Can not find Entry_Call_Record type. Aborting");
686 call_type = entry_call_record_sym->type;
687 call_self_fieldno = ada_get_field_index (call_type, "self", 0);
689 /* Set all the out parameters all at once, now that we are certain
690 that there are no potential error() anymore. */
691 *atcb_call_type = call_type;
692 *atcb_call_self_fieldno = call_self_fieldno;
695 /* Return the address of the Known_Tasks array maintained in
696 the Ada Runtime. Return NULL if the array could not be found,
697 meaning that the inferior program probably does not use tasking.
699 In order to provide a fast response time, this function caches
700 the Known_Tasks array address after the lookup during the first
701 call. Subsequent calls will simply return this cached address. */
704 get_known_tasks_addr (void)
706 static CORE_ADDR known_tasks_addr = 0;
708 if (ada__tasks_check_symbol_table)
711 struct minimal_symbol *msym;
714 #if (defined(__alpha__) && defined(__osf__) & !defined(VXWORKS_TARGET)) \
715 || defined (_AIX) || defined (__CYGWIN__)
717 #elif defined (__fsu__)
718 msym = lookup_minimal_symbol (PTHREAD_KERN, NULL, NULL);
721 pthread_kern_addr = SYMBOL_VALUE_ADDRESS (msym);
724 #elif defined (HAVE_SPYTHREAD)
725 thread_support = libspy_enabled;
728 #ifdef I386_GNULINUX_TARGET
729 /* We support threads via the Linux Threads... */
733 msym = lookup_minimal_symbol (KNOWN_TASKS_NAME, NULL, NULL);
735 known_tasks_addr = SYMBOL_VALUE_ADDRESS (msym);
737 #ifndef VXWORKS_TARGET
741 if (target_lookup_symbol (KNOWN_TASKS_NAME, &known_tasks_addr) != 0)
746 /* FIXME: brobecker 2003-03-05: Here would be a much better place
747 to attach the ada-tasks observers, instead of doing this
748 unconditionaly in _initialize_tasks. This would avoid an
749 unecessary notification when the inferior does not use tasking
750 or as long as the user does not use the ada-tasks commands.
751 Unfortunately, this is not possible for the moment: the current
752 code resets ada__tasks_check_symbol_table back to 1 whenever
753 symbols for a new program are being loaded. If we place the
754 observers intialization here, we will end up adding new observers
755 everytime we do the check for Ada tasking-related symbols
756 above. This would currently have benign effects, but is still
757 undesirable. The cleanest approach is probably to create a new
758 observer to notify us when the user is debugging a new program.
759 We would then reset ada__tasks_check_symbol_table back to 1
760 during the notification, but also detach all observers.
761 BTW: observers are probably not reentrant, so detaching during
762 a notification may not be the safest thing to do... Sigh...
763 But creating the new observer would be a good idea in any case,
764 since this allow us to make ada__tasks_check_symbol_table
765 static, which is a good bonus. */
766 ada__tasks_check_symbol_table = 0;
769 return known_tasks_addr;
772 /* Read the Known_Tasks array from the inferior memory, and store
773 it in task_list. Return non-zero upon success. */
776 read_known_tasks_array (void)
778 const int target_ptr_byte = TARGET_PTR_BIT / TARGET_CHAR_BIT;
779 const int temp_tasks_size = target_ptr_byte * MAX_NUMBER_OF_KNOWN_TASKS;
780 const CORE_ADDR known_tasks_addr = get_known_tasks_addr ();
781 char *temp_tasks = (char *) alloca (temp_tasks_size);
785 /* Step 1: Clear the current list, if any. */
788 /* If the application does not use task, then no more needs to be done.
789 It is important to have the task list cleared (see above) before we
790 return, as we don't want a stale task list to be used... This can
791 happen for instance when debugging a non-multitasking program after
792 having debugged a multitasking one. */
793 if (known_tasks_addr == 0)
796 /* Step 2: Build a new list by reading the ATCBs from the Known_Tasks
797 array in the Ada runtime. */
798 read_memory (known_tasks_addr, temp_tasks, temp_tasks_size);
799 for (i = 0; i < MAX_NUMBER_OF_KNOWN_TASKS; i++)
801 temp_task = extract_typed_address (temp_tasks + i * target_ptr_byte,
802 builtin_type_void_data_ptr);
806 if (get_entry_number (temp_task) == 0)
807 add_task_entry (temp_task, i);
811 /* Step 3: Unset stale_task_list_p, to avoid re-reading the Known_Tasks
812 array unless needed. Then report a success. */
813 stale_task_list_p = 0;
817 /* Builds the task_list by reading the Known_Tasks array from
818 the inferior. Prints an appropriate message and returns non-zero
819 if it failed to build this list. */
822 ada_build_task_list (void)
824 if (!target_has_stack)
827 if (stale_task_list_p)
828 read_known_tasks_array ();
830 if (task_list == NULL)
832 printf_filtered ("Your application does not use any Ada task.\n");
839 /* Extract the contents of the value as a string whose length is LENGTH,
840 and store the result in DEST. */
843 value_as_string (char *dest, struct value *val, int length)
845 memcpy (dest, VALUE_CONTENTS (val), length);
848 /* Extract the string image from the fat string corresponding to VAL,
849 and store it in DEST. The length of the string is stored in LEN. If
850 the string length is greater than MAX_LEN, then truncate the result
851 to the first MAX_LEN characters of the fat string. */
854 read_fat_string_value (char *dest, int *len, struct value *val, int max_len)
856 struct value *array_val;
857 struct value *bounds_val;
859 /* The following variables are made static to avoid recomputing them
860 each time this function is called. */
861 static int initialize_fieldnos = 1;
862 static int array_fieldno;
863 static int bounds_fieldno;
864 static int upper_bound_fieldno;
866 /* Get the index of the fields that we will need to read in order
867 to extract the string from the fat string. */
868 if (initialize_fieldnos)
870 struct type *type = VALUE_TYPE (val);
871 struct type *bounds_type;
873 array_fieldno = ada_get_field_index (type, "P_ARRAY", 0);
874 bounds_fieldno = ada_get_field_index (type, "P_BOUNDS", 0);
876 bounds_type = TYPE_FIELD_TYPE (type, bounds_fieldno);
877 if (TYPE_CODE (bounds_type) == TYPE_CODE_PTR)
878 bounds_type = TYPE_TARGET_TYPE (bounds_type);
879 if (TYPE_CODE (bounds_type) != TYPE_CODE_STRUCT)
880 error ("Unknown task name format. Aborting");
881 upper_bound_fieldno = ada_get_field_index (bounds_type, "UB0", 0);
883 initialize_fieldnos = 0;
886 /* Get the size of the task image by checking the value of the bounds.
887 The lower bound is always 1, so we only need to read the upper bound. */
888 bounds_val = value_ind (value_field (val, bounds_fieldno));
889 *len = value_as_long (value_field (bounds_val, upper_bound_fieldno));
891 /* Make sure that we do not read more than max_len characters... */
895 /* Extract LEN characters from the fat string. */
896 array_val = value_ind (value_field (val, array_fieldno));
897 read_memory (VALUE_ADDRESS (array_val), dest, *len);
900 /* Read the ATCB stored at ATCB_ADDR from the inferior memory. */
902 static struct task_control_block
903 read_atcb (CORE_ADDR atcb_addr)
905 /* The type description for the ATCB record and subrecords, and
906 the associated tcb_fieldnos. For efficiency reasons, these are made
907 static so that we can compute them only once the first time and
909 static struct type *atcb_type = NULL;
910 static struct type *atcb_common_type = NULL;
911 static struct type *atcb_ll_type = NULL;
912 static struct tcb_fieldnos fieldno;
914 struct task_control_block result;
915 struct value *tcb_value;
916 struct value *ll_value;
918 if (atcb_type == NULL)
919 get_tcb_types_info (&atcb_type, &atcb_common_type, &atcb_ll_type, &fieldno);
921 tcb_value = value_from_contents_and_address (atcb_type, NULL, atcb_addr);
922 tcb_value = value_field (tcb_value, fieldno.common);
924 result.state = value_as_long (value_field (tcb_value, fieldno.state));
925 result.parent = value_as_address (value_field (tcb_value, fieldno.parent));
926 result.priority = value_as_long (value_field (tcb_value, fieldno.priority));
928 /* Depending on the GNAT version used, the task image is either a fat
929 string, or a thin array of characters. Older versions of GNAT used
930 to use fat strings, and therefore did not need an extra field in
931 the ATCB to store the string length. For efficiency reasons, newer
932 versions of GNAT replaced the fat string by a static buffer, but this
933 also required the addition of a new field named "Image_Len" containing
934 the length of the task name. The method used to extract the task name
935 is selected depending on the existence of this field. */
936 if (fieldno.image_len == -1)
938 read_fat_string_value (result.image, &result.image_len,
939 value_field (tcb_value, fieldno.image),
940 sizeof (result.image));
944 value_as_string (result.image, value_field (tcb_value, fieldno.image),
945 sizeof (result.image));
947 value_as_long (value_field (tcb_value, fieldno.image_len));
950 result.call = value_as_address (value_field (tcb_value, fieldno.call));
952 ll_value = value_field (tcb_value, fieldno.ll);
953 result.thread = value_as_address (value_field (ll_value, fieldno.ll_thread));
954 if (fieldno.ll_lwp >= 0)
955 result.lwp = value_as_address (value_field (ll_value, fieldno.ll_lwp));
962 /* Read the ID of the task with which a task is attempting a rendez-vous
963 from the address of its Entry_Call_Record in the Ada TCB.
964 If the address of the Entry_Call_Record is null, then return null. */
967 read_caller (const CORE_ADDR call)
969 /* The type description for the Entry_Call_Record, and the index of
970 the field "Self". For efficiency reasons, these are made static
971 so that we can compute them only once the first time and reuse them
973 static struct type *atcb_call_type;
974 static int self_fieldno = -1;
976 struct value *call_value;
981 if (atcb_call_type == NULL)
982 get_tcb_call_type_info (&atcb_call_type, &self_fieldno);
984 call_value = value_from_contents_and_address (atcb_call_type, NULL, call);
985 return value_as_address (value_field (call_value, self_fieldno));
989 /* FIXME: Now modified and back in breakpoint.c */
990 /* breakpoint_task_match (PC) returns true if the breakpoint at PC
991 is valid for current task. */
994 breakpoint_task_match (CORE_ADDR pc)
996 const int this_task = get_current_task ();
997 const struct breakpoint *breakpoints = get_breakpoint_chain ();
998 const struct breakpoint *b;
1000 for (b = breakpoints; b; b = b->next)
1002 if (b->enable_state != bp_disabled
1003 && b->enable_state != bp_shlib_disabled
1004 && (b->address == 0 || b->address == pc)
1005 && (b->task == 0 || b->task == this_task))
1015 /* Print detailed information about specified task */
1018 info_task (char *arg, int from_tty)
1021 struct task_entry *pt, *pt2;
1025 if (current_language->la_language != language_ada)
1027 printf_filtered ("The current language does not support tasks.\n");
1031 target_find_new_threads ();
1033 pt = get_entry_vptr (atoi (arg));
1036 printf_filtered ("Task %s not found.\n", arg);
1040 /* print the Ada task id */
1041 printf_filtered ("Ada Task: %s\n", paddr_nz (pt->task_id));
1043 /* print the name of the task */
1044 if (pt->atcb.image_len != 0)
1045 printf_filtered ("Name: %.*s\n", pt->atcb.image_len, pt->atcb.image);
1047 printf_filtered ("<no name>\n");
1049 /* print the thread id */
1051 if (task_ptid_get_tid (pt->task_ptid) < 65536)
1053 ("Thread: %ld\n", (long int) task_ptid_get_tid (pt->task_ptid));
1056 ("Thread: %#lx\n", (long int) task_ptid_get_tid (pt->task_ptid));
1058 if (task_ptid_get_lwp (pt->task_ptid) != 0)
1060 if ((long) task_ptid_get_lwp (pt->task_ptid) < 65536)
1062 ("LWP: %ld\n", (long int) task_ptid_get_lwp (pt->task_ptid));
1065 ("LWP: %#lx\n", (long int) task_ptid_get_lwp (pt->task_ptid));
1068 /* print the parent gdb task id */
1069 num = get_entry_number (pt->atcb.parent);
1072 printf_filtered ("Parent: %d", num);
1073 pt2 = get_entry_vptr (num);
1075 /* print the name of the task */
1076 if (pt2->atcb.image_len != 0)
1077 printf_filtered (" (%.*s)\n", pt2->atcb.image_len, pt2->atcb.image);
1079 printf_filtered ("\n");
1082 printf_filtered ("No parent\n");
1084 /* print the base priority of the task */
1085 printf_filtered ("Base Priority: %d\n", pt->atcb.priority);
1087 /* print the current state of the task */
1089 /* check if this task is accepting a rendezvous */
1090 caller = read_caller (pt->atcb.call);
1093 num = get_entry_number (caller);
1094 printf_filtered ("Accepting rendezvous with %d", num);
1098 pt2 = get_entry_vptr (num);
1100 /* print the name of the task */
1101 if (pt2->atcb.image_len != 0) {
1102 printf_filtered (" (%.*s)\n", pt2->atcb.image_len, pt2->atcb.image);
1105 printf_filtered ("\n");
1108 printf_filtered ("\n");
1111 printf_filtered ("State: %s\n", long_task_states[pt->atcb.state]);
1115 /* Print information about currently known tasks */
1118 info_tasks (char *arg, int from_tty)
1121 struct task_entry *pt;
1123 long thread_id = 0L;
1126 ptid_t current_ptid;
1128 #if defined(__alpha__) && defined(__osf__) && !defined(VXWORKS_TARGET)
1133 current_ptid = inferior_ptid;
1135 #if !((defined(sun) && defined(__SVR4)) || defined(VXWORKS_TARGET) || defined(__WIN32__) || defined (hpux))
1138 thread_id = GET_CURRENT_THREAD (inferior_ptid);
1140 /* print the header */
1142 #if defined(__alpha__) && defined(__osf__) && !defined(VXWORKS_TARGET)
1144 (" ID TID P-ID Pri Stack %% State Name\n");
1146 printf_filtered (" ID TID P-ID Pri State Name\n");
1149 /* Now that we have a list of task id's, we can print them */
1153 /* print a star if this task is the current one */
1155 #if defined (__WIN32__) || defined (SGI) || defined (hpux)
1157 (task_ptid_get_lwp (pt->task_ptid) == thread_id ? "*" : " ");
1160 (task_ptid_get_thread_id (pt->task_ptid) == thread_id ? "*" : " ");
1163 /* print the gdb task id */
1164 printf_filtered ("%3d", pt->task_num);
1166 /* print the Ada task id */
1167 printf_filtered (" %9lx", (long) pt->task_id);
1169 /* print the parent gdb task id */
1170 printf_filtered (" %4d", get_entry_number (pt->atcb.parent));
1172 /* print the base priority of the task */
1173 printf_filtered (" %3d", pt->atcb.priority);
1175 #if defined(__alpha__) && defined(__osf__) && !defined(VXWORKS_TARGET)
1176 if (pt->task_num == 1 || pt->atcb.state == Terminated)
1178 printf_filtered (" Unknown");
1182 READ_MEMORY (pt->atcb.thread, thr);
1183 switch_to_thread (task_ptid_get_ptid (pt->task_ptid));
1184 /* ??? Brobecker 2003-03-13: Not sure what the next line is used for.
1185 And even if useful, it should probably be replaced by call to
1186 task_ptid_get_thread_id. */
1187 current_thread = task_ptid_get_tid (pt->task_ptid);
1188 regs.regs[SP_REGNUM] = 0;
1189 if (dec_thread_get_registers (®s, NULL) == 0)
1191 pt->stack_per = (100 * ((long) thr.__stack_base -
1192 regs.regs[SP_REGNUM])) / thr.__stack_size;
1193 /* if the thread is terminated but still there, the
1194 stack_base/size values are erroneous. Try to patch it */
1195 if (pt->stack_per < 0 || pt->stack_per > 100)
1200 /* Set stack_per to an invalid value to signal that we did not
1201 manage to compute its value. */
1205 /* print information about stack space used in the thread */
1206 if (thr.__stack_size < 1024 * 1024)
1208 size = thr.__stack_size / 1024;
1211 else if (thr.__stack_size < 1024 * 1024 * 1024)
1213 size = thr.__stack_size / 1024 / 1024;
1216 else /* Who knows... */
1218 size = thr.__stack_size / 1024 / 1024 / 1024;
1222 /* print the stack usage in percent, if available. */
1223 if (pt->stack_per != -1)
1224 printf_filtered (" %4d%c %2d", size, car, pt->stack_per);
1227 /* This error is not serious enough that we should raise
1228 an internal error, but print '???' to make it unambiguous
1229 that we failed to compute this value. */
1230 printf_filtered (" ???");
1236 /* print the current state of the task */
1238 /* check if this task is accepting a rendezvous */
1239 caller = read_caller (pt->atcb.call);
1241 printf_filtered (" Accepting RV with %-4d",
1242 get_entry_number (caller));
1245 #if defined (__WIN32__) || defined (SGI) || defined (hpux)
1246 if (pt->atcb.state == Runnable
1248 && task_ptid_get_lwp (pt->task_ptid) == thread_id))
1250 if (pt->atcb.state == Runnable
1252 && task_ptid_get_thread_id (pt->task_ptid) == thread_id))
1254 /* Replace "Runnable" by "Running" if this is the current task */
1255 printf_filtered (" %-22s", "Running");
1257 printf_filtered (" %-22s", task_states[pt->atcb.state]);
1260 /* finally, print the name of the task */
1261 if (pt->atcb.image_len != 0) {
1262 printf_filtered (" %.*s\n", pt->atcb.image_len, pt->atcb.image);
1265 printf_filtered (" <no name>\n");
1269 if (!ptid_equal (inferior_ptid, current_ptid))
1270 switch_to_thread (current_ptid);
1275 info_tasks_command (char *arg, int from_tty)
1277 const int task_list_built = ada_build_task_list ();
1279 if (!task_list_built)
1282 if (arg == NULL || *arg == '\000')
1283 info_tasks (arg, from_tty);
1285 info_task (arg, from_tty);
1288 /* Switch to task indicated by NEW_TASK. Return 0 iff successful. */
1291 switch_to_task (struct task_entry *new_task)
1294 /* Raise an error if task-switching is currently not allowed. */
1295 if (!THREAD_SWITCH_ALLOWED ())
1296 error ("Task switching is currently not allowed.");
1298 if (!task_is_alive (new_task->atcb.state))
1299 error ("Can not switch to task %d: Task is no longer running",
1300 new_task->task_num);
1302 current_task = new_task->task_num;
1303 current_thread = task_ptid_get_thread_id (new_task->task_ptid);
1305 if (current_task_id == -1)
1307 SAVE_TASK_REGISTERS (new_task);
1308 current_task_id = ada_get_current_task (inferior_ptid);
1311 if (SPECIAL_THREAD_SUPPORT_ACTIVE ())
1313 /* FIXME: Integrate with switch_to_thread */
1315 flush_cached_frames ();
1316 registers_changed ();
1317 if (current_task == current_task_id)
1319 RESTORE_TASK_REGISTERS (new_task);
1323 ret_code = THREAD_FETCH_REGISTERS ();
1325 stop_pc = read_pc ();
1326 select_frame (get_current_frame ());
1330 switch_to_thread (task_ptid_get_ptid (new_task->task_ptid));
1335 /* Print a message telling the user id of the current task.
1336 Print an error message if the application does not appear to
1337 be using any Ada task. */
1340 display_current_task_id (void)
1342 const int current_task = ada_get_current_task (inferior_ptid);
1344 if (current_task == -1)
1345 printf_filtered ("[Current task is unknown]\n");
1347 printf_filtered ("[Current task is %d]\n", current_task);
1350 /* Parse and evaluate TIDSTR into a task id, and try to switch to
1351 that task. Print an error message if the task switch failed. */
1354 task_command_1 (char *tidstr, int from_tty)
1356 const int num = value_as_long (parse_and_eval (tidstr));
1357 struct task_entry *e = get_entry_vptr (num);
1360 error ("Task ID %d not known. Use the \"info tasks\" command to\n"
1361 "see the IDs of currently known tasks", num);
1363 if (switch_to_task (e) == 0)
1365 ada_find_printable_frame (get_selected_frame ());
1366 printf_filtered ("[Switching to task %d]\n", num);
1367 print_stack_frame (get_selected_frame (),
1368 frame_relative_level (get_selected_frame ()), 1);
1371 printf_filtered ("Unable to switch to task %d\n", num);
1374 /* Switch to task indicated in TIDSTR. Simply print the current task
1375 if TIDSTR is empty or NULL. */
1378 task_command (char *tidstr, int from_tty)
1380 const int task_list_built = ada_build_task_list ();
1382 if (!task_list_built)
1385 if (tidstr == NULL || tidstr[0] == '\0')
1386 display_current_task_id ();
1388 task_command_1 (tidstr, from_tty);
1391 #if defined (__fsu__) || (defined(__alpha__) && defined(__osf__) && !defined(VXWORKS_TARGET))
1392 /* Restore saved registers if there was a task switch. */
1394 ada_reset_thread_registers (void)
1396 if (current_task_id != -1 && SPECIAL_THREAD_SUPPORT_ACTIVE ())
1398 supply_gregset (&gregset_saved);
1399 supply_fpregset (&fpregset_saved);
1400 reinit_frame_cache ();
1401 stop_pc = read_pc ();
1403 current_task_id = -1;
1407 ada_reset_thread_registers (void)
1412 /* The 'normal_stop' observer notification callback. */
1415 normal_stop_notification (void)
1417 /* The inferior has been resumed, and just stopped. This means that
1418 our task_list needs to be recomputed before it can be used again. */
1419 stale_task_list_p = 1;
1422 /* Attach all the observers needed by the ada-tasks module. */
1425 ada_tasks_attach_observers (void)
1427 observer_attach_normal_stop (&normal_stop_notification);
1431 _initialize_tasks (void)
1434 extern struct cmd_list_element *cmdlist;
1436 ada_tasks_attach_observers ();
1438 add_info ("tasks", info_tasks_command,
1439 "Without argument: list all known Ada tasks, with status information.\n"
1440 "info tasks n: print detailed information of task n.");
1442 add_cmd ("task", class_run, task_command,
1443 "Without argument: print the current task ID.\n"
1444 "task n: Use this command to switch to task n.",