1 /* Low level Unix child interface to ttrace, for GDB when running under HP-UX.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "gdb_string.h"
31 /* Some hackery to work around a use of the #define name NO_FLAGS
32 * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
35 #define INFTTRACE_TEMP_HACK NO_FLAGS
40 #include <sys/types.h>
43 #include <sys/param.h>
46 #include <sys/ioctl.h>
48 #include <sys/ttrace.h>
52 #ifdef PTRACE_IN_WRONG_PLACE
55 #include <sys/ptrace.h>
57 #endif /* NO_PTRACE_H */
59 /* Second half of the hackery above. Non-ANSI C, so
60 * we can't use "#error", alas.
63 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
64 /* #error "Hackery to remove warning didn't work right" */
66 /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
69 /* #error "Didn't get expected re-definition of NO_FLAGS" */
70 #define NO_FLAGS INFTTRACE_TEMP_HACK
73 #if !defined (PT_SETTRC)
74 #define PT_SETTRC 0 /* Make process traceable by parent */
76 #if !defined (PT_READ_I)
77 #define PT_READ_I 1 /* Read word from text space */
79 #if !defined (PT_READ_D)
80 #define PT_READ_D 2 /* Read word from data space */
82 #if !defined (PT_READ_U)
83 #define PT_READ_U 3 /* Read word from kernel user struct */
85 #if !defined (PT_WRITE_I)
86 #define PT_WRITE_I 4 /* Write word to text space */
88 #if !defined (PT_WRITE_D)
89 #define PT_WRITE_D 5 /* Write word to data space */
91 #if !defined (PT_WRITE_U)
92 #define PT_WRITE_U 6 /* Write word to kernel user struct */
94 #if !defined (PT_CONTINUE)
95 #define PT_CONTINUE 7 /* Continue after signal */
97 #if !defined (PT_STEP)
98 #define PT_STEP 9 /* Set flag for single stepping */
100 #if !defined (PT_KILL)
101 #define PT_KILL 8 /* Send child a SIGKILL signal */
105 #define PT_ATTACH PTRACE_ATTACH
108 #define PT_DETACH PTRACE_DETACH
113 #include <sys/file.h>
116 /* This semaphore is used to coordinate the child and parent processes
117 after a fork(), and before an exec() by the child. See parent_attach_all
122 int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */
123 int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */
128 #define SEM_LISTEN (0)
130 static startup_semaphore_t startup_semaphore;
132 /* See can_touch_threads_of_process for details. */
133 static int vforking_child_pid = 0;
134 static int vfork_in_flight = 0;
136 /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed).
138 static pid_t old_gdb_pid = 0;
139 static pid_t reported_pid = 0;
140 static int reported_bpt = 0;
142 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
144 #define TT_OK( _status, _errno ) \
145 (((_status) == 1) && ((_errno) == 0))
147 #define TTRACE_ARG_TYPE uint64_t
149 /* When supplied as the "addr" operand, ttrace interprets this
150 to mean, "from the current address".
152 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
154 /* When supplied as the "addr", "data" or "addr2" operand for most
155 requests, ttrace interprets this to mean, "pay no heed to this
158 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
160 /* This is capable of holding the value of a 32-bit register. The
161 value is always left-aligned in the buffer; i.e., [0] contains
162 the most-significant byte of the register's value, and [sizeof(reg)]
163 contains the least-significant value.
165 ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
166 that registers are 32-bits on HP-UX. The latter assumption changes
169 typedef int register_value_t;
171 /********************************************************************
177 The rest of GDB sees threads as being things with different
178 "pid" (process id) values. See "thread.c" for details. The
179 separate threads will be seen and reacted to if infttrace passes
180 back different pid values (for _events_). See wait_for_inferior
183 So infttrace is going to use thread ids externally, pretending
184 they are process ids, and keep track internally so that it can
185 use the real process id (and thread id) when calling ttrace.
187 The data structure that supports this is a linked list of the
188 current threads. Since at some date infttrace will have to
189 deal with multiple processes, each list element records its
190 corresponding pid, rather than having a single global.
192 Note that the list is only approximately current; that's ok, as
193 it's up to date when we need it (we hope!). Also, it can contain
194 dead threads, as there's no harm if it does.
196 The approach taken here is to bury the translation from external
197 to internal inside "call_ttrace" and a few other places.
199 There are some wrinkles:
201 o When GDB forks itself to create the debug target process,
202 there's only a pid of 0 around in the child, so the
203 TT_PROC_SETTRC operation uses a more direct call to ttrace;
204 Similiarly, the initial setting of the event mask happens
205 early as well, and so is also special-cased, and an attach
208 o We define an unthreaded application as having a "pseudo"
211 o To keep from confusing the rest of GDB, we don't switch
212 the PID for the pseudo thread to a TID. A table will help:
214 Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ...
216 Our thread list stores: pid pid pid pid ...
219 Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ...
221 Both pid and tid0 will map to tid0, as there are infttrace.c-internal
222 calls to ttrace using tid0.
226 Since we're implementing the "stop the world" model, sub-model
227 "other threads run during step", we have some stuff to do:
229 o User steps require continuing all threads other than the
230 one the user is stepping;
232 o Internal debugger steps (such as over a breakpoint or watchpoint,
233 but not out of a library load thunk) require stepping only
234 the selected thread; this means that we have to report the
235 step finish on that thread, which can lead to complications;
237 o When a thread is created, it is created running, rather
238 than stopped--so we have to stop it.
240 The OS doesn't guarantee the stopped thread list will be stable,
241 no does it guarantee where on the stopped thread list a thread
242 that is single-stepped will wind up: it's possible that it will
243 be off the list for a while, it's possible the step will complete
244 and it will be re-posted to the end...
246 This means we have to scan the stopped thread list, build up
247 a work-list, and then run down the work list; we can't do the
248 step/continue during the scan.
252 Then there's the issue of waiting for an event. We do this by
253 noticing how many events are reported at the end of each wait.
254 From then on, we "fake" all resumes and steps, returning instantly,
255 and don't do another wait. Once all pending events are reported,
256 we can really resume again.
258 To keep this hidden, all the routines which know about tids and
259 pids or real events and simulated ones are static (file-local).
261 This code can make lots of calls to ttrace, in particular it
262 can spin down the list of thread states more than once. If this
263 becomes a performance hit, the spin could be done once and the
264 various "tsp" blocks saved, keeping all later spins in this
267 The O/S doesn't promise to keep the list straight, and so we must
268 re-scan a lot. By observation, it looks like a single-step/wait
269 puts the stepped thread at the end of the list but doesn't change
272 ****************************************************************
275 /* Uncomment these to turn on various debugging output */
276 /* #define THREAD_DEBUG */
277 /* #define WAIT_BUFFER_DEBUG */
278 /* #define PARANOIA */
281 #define INFTTRACE_ALL_THREADS (-1)
282 #define INFTTRACE_STEP (1)
283 #define INFTTRACE_CONTINUE (0)
285 /* FIX: this is used in inftarg.c/child_wait, in a hack.
287 extern int not_same_real_pid;
289 /* This is used to count buffered events.
291 static unsigned int more_events_left = 0;
295 typedef enum process_state_enum
299 FAKE_CONTINUE, /* For later use */
306 static process_state_t process_state = STOPPED;
308 /* User-specified stepping modality.
310 typedef enum stepping_mode_enum
312 DO_DEFAULT, /* ...which is a continue! */
318 /* Action to take on an attach, depends on
319 * what kind (user command, fork, vfork).
321 * At the moment, this is either:
323 * o continue with a SIGTRAP signal, or
327 typedef enum attach_continue_enum
334 /* This flag is true if we are doing a step-over-bpt
335 * with buffered events. We will have to be sure to
336 * report the right thread, as otherwise the spaghetti
337 * code in "infrun.c/wait_for_inferior" will get
340 static int doing_fake_step = 0;
341 static lwpid_t fake_step_tid = 0;
344 /****************************************************
345 * Thread information structure routines and types. *
346 ****************************************************
349 struct thread_info_struct
351 int am_pseudo; /* This is a pseudo-thread for the process. */
352 int pid; /* Process ID */
353 lwpid_t tid; /* Thread ID */
354 int handled; /* 1 if a buffered event was handled. */
355 int seen; /* 1 if this thread was seen on a traverse. */
356 int terminated; /* 1 if thread has terminated. */
357 int have_signal; /* 1 if signal to be sent */
358 enum target_signal signal_value; /* Signal to send */
359 int have_start; /* 1 if alternate starting address */
360 stepping_mode_t stepping_mode; /* Whether to step or continue */
361 CORE_ADDR start; /* Where to start */
362 int have_state; /* 1 if the event state has been set */
363 ttstate_t last_stop_state; /* The most recently-waited event for this thread. */
364 struct thread_info_struct
365 *next; /* All threads are linked via this field. */
366 struct thread_info_struct
367 *next_pseudo; /* All pseudo-threads are linked via this field. */
372 struct thread_info_header_struct
376 thread_info *head_pseudo;
381 static thread_info_header thread_head =
383 static thread_info_header deleted_threads =
386 static ptid_t saved_real_ptid;
389 /*************************************************
390 * Debugging support functions *
391 *************************************************
394 get_raw_pc (lwpid_t ttid)
396 unsigned long pc_val;
400 offset = register_addr (PC_REGNUM, U_REGS_OFFSET);
401 res = read_from_register_save_state (
403 (TTRACE_ARG_TYPE) offset,
408 return (CORE_ADDR) pc_val;
412 return (CORE_ADDR) 0;
417 get_printable_name_of_stepping_mode (stepping_mode_t mode)
426 return "DO_CONTINUE";
428 return "?unknown mode?";
432 /* This function returns a pointer to a string describing the
433 * ttrace event being reported.
436 get_printable_name_of_ttrace_event (ttevents_t event)
438 /* This enumeration is "gappy", so don't use a table. */
445 return "TTEVT_SIGNAL";
453 return "TTEVT_VFORK";
454 case TTEVT_SYSCALL_RETURN:
455 return "TTEVT_SYSCALL_RETURN";
456 case TTEVT_LWP_CREATE:
457 return "TTEVT_LWP_CREATE";
458 case TTEVT_LWP_TERMINATE:
459 return "TTEVT_LWP_TERMINATE";
461 return "TTEVT_LWP_EXIT";
462 case TTEVT_LWP_ABORT_SYSCALL:
463 return "TTEVT_LWP_ABORT_SYSCALL";
464 case TTEVT_SYSCALL_ENTRY:
465 return "TTEVT_SYSCALL_ENTRY";
466 case TTEVT_SYSCALL_RESTART:
467 return "TTEVT_SYSCALL_RESTART";
469 return "?new event?";
474 /* This function translates the ttrace request enumeration into
475 * a character string that is its printable (aka "human readable")
479 get_printable_name_of_ttrace_request (ttreq_t request)
481 if (!IS_TTRACE_REQ (request))
484 /* This enumeration is "gappy", so don't use a table. */
488 return "TT_PROC_SETTRC";
490 return "TT_PROC_ATTACH";
492 return "TT_PROC_DETACH";
494 return "TT_PROC_RDTEXT";
496 return "TT_PROC_WRTEXT";
498 return "TT_PROC_RDDATA";
500 return "TT_PROC_WRDATA";
502 return "TT_PROC_STOP";
503 case TT_PROC_CONTINUE:
504 return "TT_PROC_CONTINUE";
505 case TT_PROC_GET_PATHNAME:
506 return "TT_PROC_GET_PATHNAME";
507 case TT_PROC_GET_EVENT_MASK:
508 return "TT_PROC_GET_EVENT_MASK";
509 case TT_PROC_SET_EVENT_MASK:
510 return "TT_PROC_SET_EVENT_MASK";
511 case TT_PROC_GET_FIRST_LWP_STATE:
512 return "TT_PROC_GET_FIRST_LWP_STATE";
513 case TT_PROC_GET_NEXT_LWP_STATE:
514 return "TT_PROC_GET_NEXT_LWP_STATE";
516 return "TT_PROC_EXIT";
517 case TT_PROC_GET_MPROTECT:
518 return "TT_PROC_GET_MPROTECT";
519 case TT_PROC_SET_MPROTECT:
520 return "TT_PROC_SET_MPROTECT";
521 case TT_PROC_SET_SCBM:
522 return "TT_PROC_SET_SCBM";
524 return "TT_LWP_STOP";
525 case TT_LWP_CONTINUE:
526 return "TT_LWP_CONTINUE";
528 return "TT_LWP_SINGLE";
530 return "TT_LWP_RUREGS";
532 return "TT_LWP_WUREGS";
533 case TT_LWP_GET_EVENT_MASK:
534 return "TT_LWP_GET_EVENT_MASK";
535 case TT_LWP_SET_EVENT_MASK:
536 return "TT_LWP_SET_EVENT_MASK";
537 case TT_LWP_GET_STATE:
538 return "TT_LWP_GET_STATE";
545 /* This function translates the process state enumeration into
546 * a character string that is its printable (aka "human readable")
550 get_printable_name_of_process_state (process_state_t process_state)
552 switch (process_state)
557 return "FAKE_STEPPING";
565 return "?some unknown state?";
569 /* Set a ttrace thread state to a safe, initial state.
572 clear_ttstate_t (ttstate_t *tts)
576 tts->tts_user_tid = 0;
577 tts->tts_event = TTEVT_NONE;
580 /* Copy ttrace thread state TTS_FROM into TTS_TO.
583 copy_ttstate_t (ttstate_t *tts_to, ttstate_t *tts_from)
585 memcpy ((char *) tts_to, (char *) tts_from, sizeof (*tts_to));
588 /* Are there any live threads we know about?
591 any_thread_records (void)
593 return (thread_head.count > 0);
596 /* Create, fill in and link in a thread descriptor.
599 create_thread_info (int pid, lwpid_t tid)
603 int thread_count_of_pid;
605 new_p = xmalloc (sizeof (thread_info));
608 new_p->have_signal = 0;
609 new_p->have_start = 0;
610 new_p->have_state = 0;
611 clear_ttstate_t (&new_p->last_stop_state);
612 new_p->am_pseudo = 0;
615 new_p->terminated = 0;
617 new_p->next_pseudo = NULL;
618 new_p->stepping_mode = DO_DEFAULT;
620 if (0 == thread_head.count)
624 printf ("First thread, pid %d tid %d!\n", pid, tid);
626 saved_real_ptid = inferior_ptid;
632 printf ("Subsequent thread, pid %d tid %d\n", pid, tid);
636 /* Another day, another thread...
640 /* The new thread always goes at the head of the list.
642 new_p->next = thread_head.head;
643 thread_head.head = new_p;
645 /* Is this the "pseudo" thread of a process? It is if there's
646 * no other thread for this process on the list. (Note that this
647 * accomodates multiple processes, such as we see even for simple
648 * cases like forking "non-threaded" programs.)
650 p = thread_head.head;
651 thread_count_of_pid = 0;
654 if (p->pid == new_p->pid)
655 thread_count_of_pid++;
659 /* Did we see any other threads for this pid? (Recall that we just
660 * added this thread to the list...)
662 if (thread_count_of_pid == 1)
664 new_p->am_pseudo = 1;
665 new_p->next_pseudo = thread_head.head_pseudo;
666 thread_head.head_pseudo = new_p;
672 /* Get rid of our thread info.
675 clear_thread_info (void)
682 printf ("Clearing all thread info\n");
685 p = thread_head.head;
693 thread_head.head = NULL;
694 thread_head.head_pseudo = NULL;
695 thread_head.count = 0;
697 p = deleted_threads.head;
705 deleted_threads.head = NULL;
706 deleted_threads.head_pseudo = NULL;
707 deleted_threads.count = 0;
709 /* No threads, so can't have pending events.
711 more_events_left = 0;
714 /* Given a tid, find the thread block for it.
717 find_thread_info (lwpid_t tid)
721 for (p = thread_head.head; p; p = p->next)
729 for (p = deleted_threads.head; p; p = p->next)
740 /* For any but the pseudo thread, this maps to the
741 * thread ID. For the pseudo thread, if you pass either
742 * the thread id or the PID, you get the pseudo thread ID.
744 * We have to be prepared for core gdb to ask about
745 * deleted threads. We do the map, but we don't like it.
748 map_from_gdb_tid (lwpid_t gdb_tid)
752 /* First assume gdb_tid really is a tid, and try to find a
753 * matching entry on the threads list.
755 for (p = thread_head.head; p; p = p->next)
757 if (p->tid == gdb_tid)
761 /* It doesn't appear to be a tid; perhaps it's really a pid?
762 * Try to find a "pseudo" thread entry on the threads list.
764 for (p = thread_head.head_pseudo; p != NULL; p = p->next_pseudo)
766 if (p->pid == gdb_tid)
770 /* Perhaps it's the tid of a deleted thread we may still
771 * have some knowledge of?
773 for (p = deleted_threads.head; p; p = p->next)
775 if (p->tid == gdb_tid)
779 /* Or perhaps it's the pid of a deleted process we may still
782 for (p = deleted_threads.head_pseudo; p != NULL; p = p->next_pseudo)
784 if (p->pid == gdb_tid)
788 return 0; /* Error? */
791 /* Map the other way: from a real tid to the
792 * "pid" known by core gdb. This tid may be
793 * for a thread that just got deleted, so we
794 * also need to consider deleted threads.
797 map_to_gdb_tid (lwpid_t real_tid)
801 for (p = thread_head.head; p; p = p->next)
803 if (p->tid == real_tid)
812 for (p = deleted_threads.head; p; p = p->next)
814 if (p->tid == real_tid)
816 return p->pid; /* Error? */
821 return 0; /* Error? Never heard of this thread! */
824 /* Do any threads have saved signals?
827 saved_signals_exist (void)
831 for (p = thread_head.head; p; p = p->next)
842 /* Is this the tid for the zero-th thread?
845 is_pseudo_thread (lwpid_t tid)
847 thread_info *p = find_thread_info (tid);
848 if (NULL == p || p->terminated)
854 /* Is this thread terminated?
857 is_terminated (lwpid_t tid)
859 thread_info *p = find_thread_info (tid);
862 return p->terminated;
867 /* Is this pid a real PID or a TID?
870 is_process_id (int pid)
877 /* What does PID really represent?
879 tid = map_from_gdb_tid (pid);
881 return 0; /* Actually, is probably an error... */
883 tinfo = find_thread_info (tid);
885 /* Does it appear to be a true thread?
887 if (!tinfo->am_pseudo)
890 /* Else, it looks like it may be a process. See if there's any other
891 * threads with the same process ID, though. If there are, then TID
892 * just happens to be the first thread of several for this process.
894 this_pid = tinfo->pid;
896 for (tinfo = thread_head.head; tinfo; tinfo = tinfo->next)
898 if (tinfo->pid == this_pid)
902 return (this_pid_count == 1);
906 /* Add a thread to our info. Prevent duplicate entries.
909 add_tthread (int pid, lwpid_t tid)
913 p = find_thread_info (tid);
915 p = create_thread_info (pid, tid);
920 /* Notice that a thread was deleted.
923 del_tthread (lwpid_t tid)
928 if (thread_head.count <= 0)
930 error ("Internal error in thread database.");
935 for (p = thread_head.head; p; p = p->next)
942 printf ("Delete here: %d \n", tid);
948 * Deleting a main thread is ok if we're doing
949 * a parent-follow on a child; this is odd but
950 * not wrong. It apparently _doesn't_ happen
951 * on the child-follow, as we don't just delete
952 * the pseudo while keeping the rest of the
953 * threads around--instead, we clear out the whole
954 * thread list at once.
957 thread_info *q_chase;
960 for (q = thread_head.head_pseudo; q; q = q->next)
964 /* Remove from pseudo list.
967 thread_head.head_pseudo = p->next_pseudo;
969 q_chase->next = p->next_pseudo;
976 /* Remove from live list.
981 thread_head.head = p->next;
983 chase->next = p->next;
985 /* Add to deleted thread list.
987 p->next = deleted_threads.head;
988 deleted_threads.head = p;
989 deleted_threads.count++;
992 p->next_pseudo = deleted_threads.head_pseudo;
993 deleted_threads.head_pseudo = p;
1005 /* Get the pid for this tid. (Has to be a real TID!).
1008 get_pid_for (lwpid_t tid)
1012 for (p = thread_head.head; p; p = p->next)
1020 for (p = deleted_threads.head; p; p = p->next)
1031 /* Note that this thread's current event has been handled.
1034 set_handled (int pid, lwpid_t tid)
1038 p = find_thread_info (tid);
1040 p = add_tthread (pid, tid);
1045 /* Was this thread's current event handled?
1048 was_handled (lwpid_t tid)
1052 p = find_thread_info (tid);
1056 return 0; /* New threads have not been handled */
1059 /* Set this thread to unhandled.
1062 clear_handled (lwpid_t tid)
1066 #ifdef WAIT_BUFFER_DEBUG
1068 printf ("clear_handled %d\n", (int) tid);
1071 p = find_thread_info (tid);
1073 error ("Internal error: No thread state to clear?");
1078 /* Set all threads to unhandled.
1081 clear_all_handled (void)
1085 #ifdef WAIT_BUFFER_DEBUG
1087 printf ("clear_all_handled\n");
1090 for (p = thread_head.head; p; p = p->next)
1095 for (p = deleted_threads.head; p; p = p->next)
1101 /* Set this thread to default stepping mode.
1104 clear_stepping_mode (lwpid_t tid)
1108 #ifdef WAIT_BUFFER_DEBUG
1110 printf ("clear_stepping_mode %d\n", (int) tid);
1113 p = find_thread_info (tid);
1115 error ("Internal error: No thread state to clear?");
1117 p->stepping_mode = DO_DEFAULT;
1120 /* Set all threads to do default continue on resume.
1123 clear_all_stepping_mode (void)
1127 #ifdef WAIT_BUFFER_DEBUG
1129 printf ("clear_all_stepping_mode\n");
1132 for (p = thread_head.head; p; p = p->next)
1134 p->stepping_mode = DO_DEFAULT;
1137 for (p = deleted_threads.head; p; p = p->next)
1139 p->stepping_mode = DO_DEFAULT;
1143 /* Set all threads to unseen on this pass.
1146 set_all_unseen (void)
1150 for (p = thread_head.head; p; p = p->next)
1156 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1157 /* debugging routine.
1160 print_tthread (thread_info *p)
1162 printf (" Thread pid %d, tid %d", p->pid, p->tid);
1164 printf (", event is %s",
1165 get_printable_name_of_ttrace_event (p->last_stop_state.tts_event));
1168 printf (", pseudo thread");
1171 printf (", have signal 0x%x", p->signal_value);
1174 printf (", have start at 0x%x", p->start);
1176 printf (", step is %s", get_printable_name_of_stepping_mode (p->stepping_mode));
1179 printf (", handled");
1181 printf (", not handled");
1186 printf (", not seen");
1192 print_tthreads (void)
1196 if (thread_head.count == 0)
1197 printf ("Thread list is empty\n");
1200 printf ("Thread list has ");
1201 if (thread_head.count == 1)
1202 printf ("1 entry:\n");
1204 printf ("%d entries:\n", thread_head.count);
1205 for (p = thread_head.head; p; p = p->next)
1211 if (deleted_threads.count == 0)
1212 printf ("Deleted thread list is empty\n");
1215 printf ("Deleted thread list has ");
1216 if (deleted_threads.count == 1)
1217 printf ("1 entry:\n");
1219 printf ("%d entries:\n", deleted_threads.count);
1221 for (p = deleted_threads.head; p; p = p->next)
1229 /* Update the thread list based on the "seen" bits.
1232 update_thread_list (void)
1238 for (p = thread_head.head; p; p = p->next)
1240 /* Is this an "unseen" thread which really happens to be a process?
1241 If so, is it inferior_ptid and is a vfork in flight? If yes to
1242 all, then DON'T REMOVE IT! We're in the midst of moving a vfork
1243 operation, which is a multiple step thing, to the point where we
1244 can touch the parent again. We've most likely stopped to examine
1245 the child at a late stage in the vfork, and if we're not following
1246 the child, we'd best not treat the parent as a dead "thread"...
1248 if ((!p->seen) && p->am_pseudo && vfork_in_flight
1249 && (p->pid != vforking_child_pid))
1259 printf ("Delete unseen thread: %d \n", p->tid);
1261 del_tthread (p->tid);
1268 /************************************************
1269 * O/S call wrappers *
1270 ************************************************
1273 /* This function simply calls ttrace with the given arguments.
1274 * It exists so that all calls to ttrace are isolated. All
1275 * parameters should be as specified by "man 2 ttrace".
1277 * No other "raw" calls to ttrace should exist in this module.
1280 call_real_ttrace (ttreq_t request, pid_t pid, lwpid_t tid, TTRACE_ARG_TYPE addr,
1281 TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2)
1286 tt_status = ttrace (request, pid, tid, addr, data, addr2);
1291 /* Don't bother for a known benign error: if you ask for the
1292 * first thread state, but there is only one thread and it's
1293 * not stopped, ttrace complains.
1295 * We have this inside the #ifdef because our caller will do
1296 * this check for real.
1298 if (request != TT_PROC_GET_FIRST_LWP_STATE
1302 printf ("TT fail for %s, with pid %d, tid %d, status %d \n",
1303 get_printable_name_of_ttrace_request (request),
1304 pid, tid, tt_status);
1310 /* ??rehrauer: It would probably be most robust to catch and report
1311 * failed requests here. However, some clients of this interface
1312 * seem to expect to catch & deal with them, so we'd best not.
1316 strcpy (reason_for_failure, "ttrace (");
1317 strcat (reason_for_failure, get_printable_name_of_ttrace_request (request));
1318 strcat (reason_for_failure, ")");
1319 printf ("ttrace error, errno = %d\n", errno);
1320 perror_with_name (reason_for_failure);
1328 /* This function simply calls ttrace_wait with the given arguments.
1329 * It exists so that all calls to ttrace_wait are isolated.
1331 * No "raw" calls to ttrace_wait should exist elsewhere.
1334 call_real_ttrace_wait (int pid, lwpid_t tid, ttwopt_t option, ttstate_t *tsp,
1338 thread_info *tinfo = NULL;
1341 ttw_status = ttrace_wait (pid, tid, option, tsp, tsp_size);
1347 printf ("TW fail with pid %d, tid %d \n", pid, tid);
1350 perror_with_name ("ttrace wait");
1357 /* A process may have one or more kernel threads, of which all or
1358 none may be stopped. This function returns the ID of the first
1359 kernel thread in a stopped state, or 0 if none are stopped.
1361 This function can be used with get_process_next_stopped_thread_id
1362 to iterate over the IDs of all stopped threads of this process.
1365 get_process_first_stopped_thread_id (int pid, ttstate_t *thread_state)
1369 tt_status = call_real_ttrace (TT_PROC_GET_FIRST_LWP_STATE,
1372 (TTRACE_ARG_TYPE) thread_state,
1373 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1378 if (errno == EPROTO)
1380 /* This is an error we can handle: there isn't any stopped
1381 * thread. This happens when we're re-starting the application
1382 * and it has only one thread. GET_NEXT handles the case of
1383 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1391 perror_with_name ("ttrace");
1399 return thread_state->tts_lwpid;
1403 /* This function returns the ID of the "next" kernel thread in a
1404 stopped state, or 0 if there are none. "Next" refers to the
1405 thread following that of the last successful call to this
1406 function or to get_process_first_stopped_thread_id, using
1407 the value of thread_state returned by that call.
1409 This function can be used with get_process_first_stopped_thread_id
1410 to iterate over the IDs of all stopped threads of this process.
1413 get_process_next_stopped_thread_id (int pid, ttstate_t *thread_state)
1417 tt_status = call_real_ttrace (
1418 TT_PROC_GET_NEXT_LWP_STATE,
1421 (TTRACE_ARG_TYPE) thread_state,
1422 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1425 perror_with_name ("ttrace");
1432 else if (tt_status == 0)
1434 /* End of list, no next state. Don't return the
1435 * tts_lwpid, as it's a meaningless "240".
1437 * This is an HPUX "feature".
1442 return thread_state->tts_lwpid;
1445 /* ??rehrauer: Eventually this function perhaps should be calling
1446 pid_to_thread_id. However, that function currently does nothing
1447 for HP-UX. Even then, I'm not clear whether that function
1448 will return a "kernel" thread ID, or a "user" thread ID. If
1449 the former, we can just call it here. If the latter, we must
1450 map from the "user" tid to a "kernel" tid.
1452 NOTE: currently not called.
1455 get_active_tid_of_pid (int pid)
1457 ttstate_t thread_state;
1459 return get_process_first_stopped_thread_id (pid, &thread_state);
1462 /* This function returns 1 if tt_request is a ttrace request that
1463 * operates upon all threads of a (i.e., the entire) process.
1466 is_process_ttrace_request (ttreq_t tt_request)
1468 return IS_TTRACE_PROCREQ (tt_request);
1472 /* This function translates a thread ttrace request into
1473 * the equivalent process request for a one-thread process.
1476 make_process_version (ttreq_t request)
1478 if (!IS_TTRACE_REQ (request))
1480 error ("Internal error, bad ttrace request made\n");
1487 return TT_PROC_STOP;
1489 case TT_LWP_CONTINUE:
1490 return TT_PROC_CONTINUE;
1492 case TT_LWP_GET_EVENT_MASK:
1493 return TT_PROC_GET_EVENT_MASK;
1495 case TT_LWP_SET_EVENT_MASK:
1496 return TT_PROC_SET_EVENT_MASK;
1501 case TT_LWP_GET_STATE:
1502 return -1; /* No equivalent */
1510 /* This function translates the "pid" used by the rest of
1511 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1512 * with the given arguments.
1514 * In general, other parts of this module should call this
1515 * function when they are dealing with external users, who only
1516 * have tids to pass (but they call it "pid" for historical
1520 call_ttrace (ttreq_t request, int gdb_tid, TTRACE_ARG_TYPE addr,
1521 TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2)
1525 ttreq_t new_request;
1527 char reason_for_failure[100]; /* Arbitrary size, should be big enough. */
1530 int is_interesting = 0;
1532 if (TT_LWP_RUREGS == request)
1534 is_interesting = 1; /* Adjust code here as desired */
1537 if (is_interesting && 0 && debug_on)
1539 if (!is_process_ttrace_request (request))
1541 printf ("TT: Thread request, tid is %d", gdb_tid);
1542 printf ("== SINGLE at %x", addr);
1546 printf ("TT: Process request, tid is %d\n", gdb_tid);
1547 printf ("==! SINGLE at %x", addr);
1552 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1553 * which happen before any threads get set up) should go
1554 * directly to "call_real_ttrace", so they don't happen here.
1556 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1560 if (request == TT_PROC_SETTRC && debug_on)
1561 printf ("Unexpected call for TT_PROC_SETTRC\n");
1564 /* Sometimes we get called with a bogus tid (e.g., if a
1565 * thread has terminated, we return 0; inftarg later asks
1566 * whether the thread has exited/forked/vforked).
1570 errno = ESRCH; /* ttrace's response would probably be "No such process". */
1574 /* All other cases should be able to expect that there are
1577 if (!any_thread_records ())
1581 warning ("No thread records for ttrace call");
1583 errno = ESRCH; /* ttrace's response would be "No such process". */
1587 /* OK, now the task is to translate the incoming tid into
1590 real_tid = map_from_gdb_tid (gdb_tid);
1591 real_pid = get_pid_for (real_tid);
1593 /* Now check the result. "Real_pid" is NULL if our list
1594 * didn't find it. We have some tricks we can play to fix
1599 ttstate_t thread_state;
1603 printf ("No saved pid for tid %d\n", gdb_tid);
1606 if (is_process_ttrace_request (request))
1609 /* Ok, we couldn't get a tid. Try to translate to
1610 * the equivalent process operation. We expect this
1611 * NOT to happen, so this is a desparation-type
1612 * move. It can happen if there is an internal
1613 * error and so no "wait()" call is ever done.
1615 new_request = make_process_version (request);
1616 if (new_request == -1)
1621 printf ("...and couldn't make process version of thread operation\n");
1624 /* Use hacky saved pid, which won't always be correct
1625 * in the multi-process future. Use tid as thread,
1626 * probably dooming this to failure. FIX!
1628 if (! ptid_equal (saved_real_ptid, null_ptid))
1632 printf ("...using saved pid %d\n",
1633 PIDGET (saved_real_ptid));
1636 real_pid = PIDGET (saved_real_ptid);
1641 error ("Unable to perform thread operation");
1646 /* Sucessfully translated this to a process request,
1647 * which needs no thread value.
1651 request = new_request;
1656 printf ("Translated thread request to process request\n");
1657 if (ptid_equal (saved_real_ptid, null_ptid))
1658 printf ("...but there's no saved pid\n");
1662 if (gdb_tid != PIDGET (saved_real_ptid))
1663 printf ("...but have the wrong pid (%d rather than %d)\n",
1664 gdb_tid, PIDGET (saved_real_ptid));
1668 } /* Translated to a process request */
1669 } /* Is a process request */
1673 /* We have to have a thread. Ooops.
1675 error ("Thread request with no threads (%s)",
1676 get_printable_name_of_ttrace_request (request));
1680 /* Ttrace doesn't like to see tid values on process requests,
1681 * even if we have the right one.
1683 if (is_process_ttrace_request (request))
1689 if (is_interesting && 0 && debug_on)
1691 printf (" now tid %d, pid %d\n", real_tid, real_pid);
1692 printf (" request is %s\n", get_printable_name_of_ttrace_request (request));
1696 /* Finally, the (almost) real call.
1698 tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2);
1701 if (is_interesting && debug_on)
1703 if (!TT_OK (tt_status, errno)
1704 && !(tt_status == 0 & errno == 0))
1705 printf (" got error (errno==%d, status==%d)\n", errno, tt_status);
1713 /* Stop all the threads of a process.
1715 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1716 * to get a TTEVT_NONE event, discarding the old event. Be
1717 * very careful, and only call TT_PROC_STOP when you mean it!
1720 stop_all_threads_of_process (pid_t real_pid)
1724 ttw_status = call_real_ttrace (TT_PROC_STOP,
1727 (TTRACE_ARG_TYPE) TT_NIL,
1728 (TTRACE_ARG_TYPE) TT_NIL,
1731 perror_with_name ("ttrace stop of other threads");
1735 /* Under some circumstances, it's unsafe to attempt to stop, or even
1736 query the state of, a process' threads.
1738 In ttrace-based HP-UX, an example is a vforking child process. The
1739 vforking parent and child are somewhat fragile, w/r/t what we can do
1740 what we can do to them with ttrace, until after the child exits or
1741 execs, or until the parent's vfork event is delivered. Until that
1742 time, we must not try to stop the process' threads, or inquire how
1743 many there are, or even alter its data segments, or it typically dies
1744 with a SIGILL. Sigh.
1746 This function returns 1 if this stopped process, and the event that
1747 we're told was responsible for its current stopped state, cannot safely
1748 have its threads examined.
1750 #define CHILD_VFORKED(evt,pid) \
1751 (((evt) == TTEVT_VFORK) && ((pid) != PIDGET (inferior_ptid)))
1752 #define CHILD_URPED(evt,pid) \
1753 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1754 #define PARENT_VFORKED(evt,pid) \
1755 (((evt) == TTEVT_VFORK) && ((pid) == PIDGET (inferior_ptid)))
1758 can_touch_threads_of_process (int pid, ttevents_t stopping_event)
1760 if (CHILD_VFORKED (stopping_event, pid))
1762 vforking_child_pid = pid;
1763 vfork_in_flight = 1;
1766 else if (vfork_in_flight &&
1767 (PARENT_VFORKED (stopping_event, pid) ||
1768 CHILD_URPED (stopping_event, pid)))
1770 vfork_in_flight = 0;
1771 vforking_child_pid = 0;
1774 return !vfork_in_flight;
1778 /* If we can find an as-yet-unhandled thread state of a
1779 * stopped thread of this process return 1 and set "tsp".
1780 * Return 0 if we can't.
1782 * If this function is used when the threads of PIS haven't
1783 * been stopped, undefined behaviour is guaranteed!
1786 select_stopped_thread_of_process (int pid, ttstate_t *tsp)
1788 lwpid_t candidate_tid, tid;
1789 ttstate_t candidate_tstate, tstate;
1791 /* If we're not allowed to touch the process now, then just
1792 * return the current value of *TSP.
1794 * This supports "vfork". It's ok, really, to double the
1795 * current event (the child EXEC, we hope!).
1797 if (!can_touch_threads_of_process (pid, tsp->tts_event))
1800 /* Decide which of (possibly more than one) events to
1801 * return as the first one. We scan them all so that
1802 * we always return the result of a fake-step first.
1805 for (tid = get_process_first_stopped_thread_id (pid, &tstate);
1807 tid = get_process_next_stopped_thread_id (pid, &tstate))
1809 /* TTEVT_NONE events are uninteresting to our clients. They're
1810 * an artifact of our "stop the world" model--the thread is
1811 * stopped because we stopped it.
1813 if (tstate.tts_event == TTEVT_NONE)
1815 set_handled (pid, tstate.tts_lwpid);
1818 /* Did we just single-step a single thread, without letting any
1819 * of the others run? Is this an event for that thread?
1821 * If so, we believe our client would prefer to see this event
1822 * over any others. (Typically the client wants to just push
1823 * one thread a little farther forward, and then go around
1824 * checking for what all threads are doing.)
1826 else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid))
1828 #ifdef WAIT_BUFFER_DEBUG
1829 /* It's possible here to see either a SIGTRAP (due to
1830 * successful completion of a step) or a SYSCALL_ENTRY
1831 * (due to a step completion with active hardware
1835 printf ("Ending fake step with tid %d, state %s\n",
1837 get_printable_name_of_ttrace_event (tstate.tts_event));
1840 /* Remember this one, and throw away any previous
1843 candidate_tid = tstate.tts_lwpid;
1844 candidate_tstate = tstate;
1847 #ifdef FORGET_DELETED_BPTS
1849 /* We can't just do this, as if we do, and then wind
1850 * up the loop with no unhandled events, we need to
1851 * handle that case--the appropriate reaction is to
1852 * just continue, but there's no easy way to do that.
1854 * Better to put this in the ttrace_wait call--if, when
1855 * we fake a wait, we update our events based on the
1856 * breakpoint_here_pc call and find there are no more events,
1857 * then we better continue and so on.
1859 * Or we could put it in the next/continue fake.
1860 * But it has to go in the buffering code, not in the
1861 * real go/wait code.
1863 else if ((TTEVT_SIGNAL == tstate.tts_event)
1864 && (5 == tstate.tts_u.tts_signal.tts_signo)
1865 && (0 != get_raw_pc (tstate.tts_lwpid))
1866 && !breakpoint_here_p (get_raw_pc (tstate.tts_lwpid)))
1869 * If the user deleted a breakpoint while this
1870 * breakpoint-hit event was buffered, we can forget
1873 #ifdef WAIT_BUFFER_DEBUG
1875 printf ("Forgetting deleted bp hit for thread %d\n",
1879 set_handled (pid, tstate.tts_lwpid);
1883 /* Else, is this the first "unhandled" event? If so,
1884 * we believe our client wants to see it (if we don't
1885 * see a fake-step later on in the scan).
1887 else if (!was_handled (tstate.tts_lwpid) && candidate_tid == 0)
1889 candidate_tid = tstate.tts_lwpid;
1890 candidate_tstate = tstate;
1893 /* This is either an event that has already been "handled",
1894 * and thus we believe is uninteresting to our client, or we
1895 * already have a candidate event. Ignore it...
1899 /* What do we report?
1901 if (doing_fake_step)
1903 if (candidate_tid == fake_step_tid)
1907 tstate = candidate_tstate;
1911 warning ("Internal error: fake-step failed to complete.");
1915 else if (candidate_tid != 0)
1917 /* Found a candidate unhandled event.
1919 tstate = candidate_tstate;
1923 warning ("Internal error in call of ttrace_wait.");
1928 warning ("Internal error: no unhandled thread event to select");
1932 copy_ttstate_t (tsp, &tstate);
1934 } /* End of select_stopped_thread_of_process */
1937 /* Check our internal thread data against the real thing.
1940 check_thread_consistency (pid_t real_pid)
1942 int tid; /* really lwpid_t */
1946 /* Spin down the O/S list of threads, checking that they
1947 * match what we've got.
1949 for (tid = get_process_first_stopped_thread_id (real_pid, &tstate);
1951 tid = get_process_next_stopped_thread_id (real_pid, &tstate))
1954 p = find_thread_info (tid);
1958 warning ("No internal thread data for thread %d.", tid);
1964 warning ("Inconsistent internal thread data for thread %d.", tid);
1969 warning ("Thread %d is not terminated, internal error.", tid);
1974 #define TT_COMPARE( fld ) \
1975 tstate.fld != p->last_stop_state.fld
1979 if (TT_COMPARE (tts_pid)
1980 || TT_COMPARE (tts_lwpid)
1981 || TT_COMPARE (tts_user_tid)
1982 || TT_COMPARE (tts_event)
1983 || TT_COMPARE (tts_flags)
1984 || TT_COMPARE (tts_scno)
1985 || TT_COMPARE (tts_scnargs))
1987 warning ("Internal thread data for thread %d is wrong.", tid);
1993 #endif /* PARANOIA */
1996 /* This function wraps calls to "call_real_ttrace_wait" so
1997 * that a actual wait is only done when all pending events
1998 * have been reported.
2000 * Note that typically it is called with a pid of "0", i.e.
2001 * the "don't care" value.
2003 * Return value is the status of the pseudo wait.
2006 call_ttrace_wait (int pid, ttwopt_t option, ttstate_t *tsp, size_t tsp_size)
2008 /* This holds the actual, for-real, true process ID.
2010 static int real_pid;
2012 /* As an argument to ttrace_wait, zero pid
2013 * means "Any process", and zero tid means
2014 * "Any thread of the specified process".
2017 lwpid_t wait_tid = 0;
2020 int ttw_status = 0; /* To be returned */
2022 thread_info *tinfo = NULL;
2030 printf ("TW: Pid to wait on is %d\n", pid);
2033 if (!any_thread_records ())
2034 error ("No thread records for ttrace call w. specific pid");
2036 /* OK, now the task is to translate the incoming tid into
2039 real_tid = map_from_gdb_tid (pid);
2040 real_pid = get_pid_for (real_tid);
2043 printf ("==TW: real pid %d, real tid %d\n", real_pid, real_tid);
2048 /* Sanity checks and set-up.
2051 * Stopped Running Fake-step (v)Fork
2052 * \________________________________________
2054 * No buffered events | error wait wait wait
2056 * Buffered events | debuffer error wait debuffer (?)
2059 if (more_events_left == 0)
2062 if (process_state == RUNNING)
2064 /* OK--normal call of ttrace_wait with no buffered events.
2068 else if (process_state == FAKE_STEPPING)
2070 /* Ok--call of ttrace_wait to support
2071 * fake stepping with no buffered events.
2073 * But we better be fake-stepping!
2075 if (!doing_fake_step)
2077 warning ("Inconsistent thread state.");
2080 else if ((process_state == FORKING)
2081 || (process_state == VFORKING))
2083 /* Ok--there are two processes, so waiting
2084 * for the second while the first is stopped
2085 * is ok. Handled bits stay as they were.
2089 else if (process_state == STOPPED)
2091 warning ("Process not running at wait call.");
2096 warning ("Inconsistent process state.");
2103 if (process_state == STOPPED)
2105 /* OK--buffered events being unbuffered.
2109 else if (process_state == RUNNING)
2111 /* An error--shouldn't have buffered events
2114 warning ("Trying to continue with buffered events:");
2116 else if (process_state == FAKE_STEPPING)
2119 * Better be fake-stepping!
2121 if (!doing_fake_step)
2123 warning ("Losing buffered thread events!\n");
2126 else if ((process_state == FORKING)
2127 || (process_state == VFORKING))
2129 /* Ok--there are two processes, so waiting
2130 * for the second while the first is stopped
2131 * is ok. Handled bits stay as they were.
2136 warning ("Process in unknown state with buffered events.");
2139 /* Sometimes we have to wait for a particular thread
2140 * (if we're stepping over a bpt). In that case, we
2141 * _know_ it's going to complete the single-step we
2142 * asked for (because we're only doing the step under
2143 * certain very well-understood circumstances), so it
2146 if (doing_fake_step)
2148 wait_tid = fake_step_tid;
2149 wait_pid = get_pid_for (fake_step_tid);
2151 #ifdef WAIT_BUFFER_DEBUG
2153 printf ("Doing a wait after a fake-step for %d, pid %d\n",
2154 wait_tid, wait_pid);
2158 if (more_events_left == 0 /* No buffered events, need real ones. */
2159 || process_state != STOPPED)
2161 /* If there are no buffered events, and so we need
2162 * real ones, or if we are FORKING, VFORKING,
2163 * FAKE_STEPPING or RUNNING, and thus have to do
2164 * a real wait, then do a real wait.
2167 #ifdef WAIT_BUFFER_DEBUG
2168 /* Normal case... */
2170 printf ("TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid);
2173 /* The actual wait call.
2175 ttw_status = call_real_ttrace_wait (wait_pid, wait_tid, option, tsp, tsp_size);
2177 /* Note that the routines we'll call will be using "call_real_ttrace",
2178 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2179 * the rest of the world uses (which is actually the tid).
2181 real_pid = tsp->tts_pid;
2183 /* For most events: Stop the world!
2185 * It's sometimes not safe to stop all threads of a process.
2186 * Sometimes it's not even safe to ask for the thread state
2189 if (can_touch_threads_of_process (real_pid, tsp->tts_event))
2191 /* If we're really only stepping a single thread, then don't
2192 * try to stop all the others -- we only do this single-stepping
2193 * business when all others were already stopped...and the stop
2194 * would mess up other threads' events.
2196 * Similiarly, if there are other threads with events,
2197 * don't do the stop.
2199 if (!doing_fake_step)
2201 if (more_events_left > 0)
2202 warning ("Internal error in stopping process");
2204 stop_all_threads_of_process (real_pid);
2206 /* At this point, we could scan and update_thread_list(),
2207 * and only use the local list for the rest of the
2208 * module! We'd get rid of the scans in the various
2209 * continue routines (adding one in attach). It'd
2210 * be great--UPGRADE ME!
2218 if (more_events_left > 0)
2219 printf ("== Can't stop process; more events!\n");
2221 printf ("== Can't stop process!\n");
2225 process_state = STOPPED;
2227 #ifdef WAIT_BUFFER_DEBUG
2229 printf ("Process set to STOPPED\n");
2235 /* Fake a call to ttrace_wait. The process must be
2236 * STOPPED, as we aren't going to do any wait.
2238 #ifdef WAIT_BUFFER_DEBUG
2240 printf ("TW: fake it\n");
2243 if (process_state != STOPPED)
2245 warning ("Process not stopped at wait call, in state '%s'.\n",
2246 get_printable_name_of_process_state (process_state));
2249 if (doing_fake_step)
2250 error ("Internal error in stepping over breakpoint");
2252 ttw_status = 0; /* Faking it is always successful! */
2253 } /* End of fake or not? if */
2255 /* Pick an event to pass to our caller. Be paranoid.
2257 if (!select_stopped_thread_of_process (real_pid, tsp))
2258 warning ("Can't find event, using previous event.");
2260 else if (tsp->tts_event == TTEVT_NONE)
2261 warning ("Internal error: no thread has a real event.");
2263 else if (doing_fake_step)
2265 if (fake_step_tid != tsp->tts_lwpid)
2266 warning ("Internal error in stepping over breakpoint.");
2268 /* This wait clears the (current) fake-step if there was one.
2270 doing_fake_step = 0;
2274 /* We now have a correct tsp and ttw_status for the thread
2275 * which we want to report. So it's "handled"! This call
2276 * will add it to our list if it's not there already.
2278 set_handled (real_pid, tsp->tts_lwpid);
2280 /* Save a copy of the ttrace state of this thread, in our local
2283 This caches the state. The implementation of queries like
2284 target_has_execd can then use this cached state, rather than
2285 be forced to make an explicit ttrace call to get it.
2287 (Guard against the condition that this is the first time we've
2288 waited on, i.e., seen this thread, and so haven't yet entered
2289 it into our list of threads.)
2291 tinfo = find_thread_info (tsp->tts_lwpid);
2294 copy_ttstate_t (&tinfo->last_stop_state, tsp);
2295 tinfo->have_state = 1;
2299 } /* call_ttrace_wait */
2301 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2303 child_reported_exec_events_per_exec_call (void)
2305 return 1; /* ttrace reports the event once per call. */
2311 /* Our implementation of hardware watchpoints involves making memory
2312 pages write-protected. We must remember a page's original permissions,
2313 and we must also know when it is appropriate to restore a page's
2314 permissions to its original state.
2316 We use a "dictionary" of hardware-watched pages to do this. Each
2317 hardware-watched page is recorded in the dictionary. Each page's
2318 dictionary entry contains the original permissions and a reference
2319 count. Pages are hashed into the dictionary by their start address.
2321 When hardware watchpoint is set on page X for the first time, page X
2322 is added to the dictionary with a reference count of 1. If other
2323 hardware watchpoints are subsequently set on page X, its reference
2324 count is incremented. When hardware watchpoints are removed from
2325 page X, its reference count is decremented. If a page's reference
2326 count drops to 0, it's permissions are restored and the page's entry
2327 is thrown out of the dictionary.
2329 typedef struct memory_page
2331 CORE_ADDR page_start;
2332 int reference_count;
2333 int original_permissions;
2334 struct memory_page *next;
2335 struct memory_page *previous;
2339 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2345 int page_protections_allowed;
2346 /* These are just the heads of chains of actual page descriptors. */
2347 memory_page_t buckets[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT];
2349 memory_page_dictionary;
2353 require_memory_page_dictionary (void)
2357 /* Is the memory page dictionary ready for use? If so, we're done. */
2358 if (memory_page_dictionary.page_count >= (LONGEST) 0)
2361 /* Else, initialize it. */
2362 memory_page_dictionary.page_count = (LONGEST) 0;
2364 for (i = 0; i < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++)
2366 memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0;
2367 memory_page_dictionary.buckets[i].reference_count = 0;
2368 memory_page_dictionary.buckets[i].next = NULL;
2369 memory_page_dictionary.buckets[i].previous = NULL;
2375 retire_memory_page_dictionary (void)
2377 memory_page_dictionary.page_count = (LONGEST) - 1;
2381 /* Write-protect the memory page that starts at this address.
2383 Returns the original permissions of the page.
2386 write_protect_page (int pid, CORE_ADDR page_start)
2389 int original_permissions;
2390 int new_permissions;
2392 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
2394 (TTRACE_ARG_TYPE) page_start,
2396 (TTRACE_ARG_TYPE) & original_permissions);
2397 if (errno || (tt_status < 0))
2399 return 0; /* What else can we do? */
2402 /* We'll also write-protect the page now, if that's allowed. */
2403 if (memory_page_dictionary.page_protections_allowed)
2405 new_permissions = original_permissions & ~PROT_WRITE;
2406 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2408 (TTRACE_ARG_TYPE) page_start,
2409 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2410 (TTRACE_ARG_TYPE) new_permissions);
2411 if (errno || (tt_status < 0))
2413 return 0; /* What else can we do? */
2417 return original_permissions;
2421 /* Unwrite-protect the memory page that starts at this address, restoring
2422 (what we must assume are) its original permissions.
2425 unwrite_protect_page (int pid, CORE_ADDR page_start, int original_permissions)
2429 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2431 (TTRACE_ARG_TYPE) page_start,
2432 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2433 (TTRACE_ARG_TYPE) original_permissions);
2434 if (errno || (tt_status < 0))
2436 return; /* What else can we do? */
2441 /* Memory page-protections are used to implement "hardware" watchpoints
2444 For every memory page that is currently being watched (i.e., that
2445 presently should be write-protected), write-protect it.
2448 hppa_enable_page_protection_events (int pid)
2452 memory_page_dictionary.page_protections_allowed = 1;
2454 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2456 memory_page_t *page;
2458 page = memory_page_dictionary.buckets[bucket].next;
2459 while (page != NULL)
2461 page->original_permissions = write_protect_page (pid, page->page_start);
2468 /* Memory page-protections are used to implement "hardware" watchpoints
2471 For every memory page that is currently being watched (i.e., that
2472 presently is or should be write-protected), un-write-protect it.
2475 hppa_disable_page_protection_events (int pid)
2479 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2481 memory_page_t *page;
2483 page = memory_page_dictionary.buckets[bucket].next;
2484 while (page != NULL)
2486 unwrite_protect_page (pid, page->page_start, page->original_permissions);
2491 memory_page_dictionary.page_protections_allowed = 0;
2494 /* Count the number of outstanding events. At this
2495 * point, we have selected one thread and its event
2496 * as the one to be "reported" upwards to core gdb.
2497 * That thread is already marked as "handled".
2499 * Note: we could just scan our own thread list. FIXME!
2502 count_unhandled_events (int real_pid, lwpid_t real_tid)
2508 /* Ok, find out how many threads have real events to report.
2511 ttid = get_process_first_stopped_thread_id (real_pid, &tstate);
2517 printf ("Process %d has no threads\n", real_pid);
2519 printf ("Process %d has these threads:\n", real_pid);
2525 if (tstate.tts_event != TTEVT_NONE
2526 && !was_handled (ttid))
2528 /* TTEVT_NONE implies we just stopped it ourselves
2529 * because we're the stop-the-world guys, so it's
2530 * not an event from our point of view.
2532 * If "was_handled" is true, this is an event we
2533 * already handled, so don't count it.
2535 * Note that we don't count the thread with the
2536 * currently-reported event, as it's already marked
2542 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2545 if (ttid == real_tid)
2546 printf ("*"); /* Thread we're reporting */
2550 if (tstate.tts_event != TTEVT_NONE)
2551 printf ("+"); /* Thread with a real event */
2555 if (was_handled (ttid))
2556 printf ("h"); /* Thread has been handled */
2560 printf (" %d, with event %s", ttid,
2561 get_printable_name_of_ttrace_event (tstate.tts_event));
2563 if (tstate.tts_event == TTEVT_SIGNAL
2564 && 5 == tstate.tts_u.tts_signal.tts_signo)
2568 pc_val = get_raw_pc (ttid);
2571 printf (" breakpoint at 0x%x\n", pc_val);
2573 printf (" bpt, can't fetch pc.\n");
2580 ttid = get_process_next_stopped_thread_id (real_pid, &tstate);
2583 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2585 if (events_left > 0)
2586 printf ("There are thus %d pending events\n", events_left);
2592 /* This function is provided as a sop to clients that are calling
2593 * ptrace_wait to wait for a process to stop. (see the
2594 * implementation of child_wait.) Return value is the pid for
2595 * the event that ended the wait.
2597 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2600 ptrace_wait (ptid_t ptid, int *status)
2609 /* The ptrace implementation of this also ignores pid.
2613 ttwait_return = call_ttrace_wait (0, TTRACE_WAITOK, &tsp, sizeof (tsp));
2614 if (ttwait_return < 0)
2616 /* ??rehrauer: It appears that if our inferior exits and we
2617 haven't asked for exit events, that we're not getting any
2618 indication save a negative return from ttrace_wait and an
2623 *status = 0; /* WIFEXITED */
2624 return PIDGET (inferior_ptid);
2627 warning ("Call of ttrace_wait returned with errno %d.",
2629 *status = ttwait_return;
2630 return PIDGET (inferior_ptid);
2633 real_pid = tsp.tts_pid;
2634 real_tid = tsp.tts_lwpid;
2636 /* One complication is that the "tts_event" structure has
2637 * a set of flags, and more than one can be set. So we
2638 * either have to force an order (as we do here), or handle
2639 * more than one flag at a time.
2641 if (tsp.tts_event & TTEVT_LWP_CREATE)
2644 /* Unlike what you might expect, this event is reported in
2645 * the _creating_ thread, and the _created_ thread (whose tid
2646 * we have) is still running. So we have to stop it. This
2647 * has already been done in "call_ttrace_wait", but should we
2648 * ever abandon the "stop-the-world" model, here's the command
2651 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2653 * Note that this would depend on being called _after_ "add_tthread"
2654 * below for the tid-to-pid translation to be done in "call_ttrace".
2659 printf ("New thread: pid %d, tid %d, creator tid %d\n",
2660 real_pid, tsp.tts_u.tts_thread.tts_target_lwpid,
2664 /* Now we have to return the tid of the created thread, not
2665 * the creating thread, or "wait_for_inferior" won't know we
2666 * have a new "process" (thread). Plus we should record it
2669 real_tid = tsp.tts_u.tts_thread.tts_target_lwpid;
2671 add_tthread (real_pid, real_tid);
2674 else if ((tsp.tts_event & TTEVT_LWP_TERMINATE)
2675 || (tsp.tts_event & TTEVT_LWP_EXIT))
2680 printf ("Thread dies: %d\n", real_tid);
2683 del_tthread (real_tid);
2686 else if (tsp.tts_event & TTEVT_EXEC)
2691 printf ("Pid %d has zero'th thread %d; inferior pid is %d\n",
2692 real_pid, real_tid, PIDGET (inferior_ptid));
2695 add_tthread (real_pid, real_tid);
2701 printf ("Process-level event %s, using tid %d\n",
2702 get_printable_name_of_ttrace_event (tsp.tts_event),
2705 /* OK to do this, as "add_tthread" won't add
2706 * duplicate entries. Also OK not to do it,
2707 * as this event isn't one which can change the
2710 add_tthread (real_pid, real_tid);
2715 /* How many events are left to report later?
2716 * In a non-stop-the-world model, this isn't needed.
2718 * Note that it's not always safe to query the thread state of a process,
2719 * which is what count_unhandled_events does. (If unsafe, we're left with
2720 * no other resort than to assume that no more events remain...)
2722 if (can_touch_threads_of_process (real_pid, tsp.tts_event))
2723 more_events_left = count_unhandled_events (real_pid, real_tid);
2727 if (more_events_left > 0)
2728 warning ("Vfork or fork causing loss of %d buffered events.",
2731 more_events_left = 0;
2734 /* Attempt to translate the ttrace_wait-returned status into the
2737 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2738 clients that expect to pick apart a ptrace wait status, to use
2739 something a little more abstract.
2741 if ((tsp.tts_event & TTEVT_EXEC)
2742 || (tsp.tts_event & TTEVT_FORK)
2743 || (tsp.tts_event & TTEVT_VFORK))
2745 /* Forks come in pairs (parent and child), so core gdb
2746 * will do two waits. Be ready to notice this.
2748 if (tsp.tts_event & TTEVT_FORK)
2750 process_state = FORKING;
2752 #ifdef WAIT_BUFFER_DEBUG
2754 printf ("Process set to FORKING\n");
2757 else if (tsp.tts_event & TTEVT_VFORK)
2759 process_state = VFORKING;
2761 #ifdef WAIT_BUFFER_DEBUG
2763 printf ("Process set to VFORKING\n");
2767 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2768 but I don't think non HP-UX-specific clients really carefully
2769 inspect the first events they get after inferior startup, so
2770 it probably almost doesn't matter what we claim this is.
2775 printf ("..a process 'event'\n");
2778 /* Also make fork and exec events look like bpts, so they can be caught.
2780 *status = 0177 | (_SIGTRAP << 8);
2783 /* Special-cases: We ask for syscall entry and exit events to implement
2784 "fast" (aka "hardware") watchpoints.
2786 When we get a syscall entry, we want to disable page-protections,
2787 and resume the inferior; this isn't an event we wish for
2788 wait_for_inferior to see. Note that we must resume ONLY the
2789 thread that reported the syscall entry; we don't want to allow
2790 other threads to run with the page protections off, as they might
2791 then be able to write to watch memory without it being caught.
2793 When we get a syscall exit, we want to reenable page-protections,
2794 but we don't want to resume the inferior; this is an event we wish
2795 wait_for_inferior to see. Make it look like the signal we normally
2796 get for a single-step completion. This should cause wait_for_inferior
2797 to evaluate whether any watchpoint triggered.
2799 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2800 due to some HP-UX "features". Some syscalls have problems with
2801 write-protections on some pages, and some syscalls seem to have
2802 pending writes to those pages at the time we're getting the return
2803 event. So, we'll single-step the inferior to get out of the syscall,
2804 and then reenable protections.
2806 Note that we're intentionally allowing the syscall exit case to
2807 fall through into the succeeding cases, as sometimes we single-
2808 step out of one syscall only to immediately enter another...
2810 else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY)
2811 || (tsp.tts_event & TTEVT_SYSCALL_RETURN))
2813 /* Make a syscall event look like a breakpoint. Same comments
2814 as for exec & fork events.
2818 printf ("..a syscall 'event'\n");
2821 /* Also make syscall events look like bpts, so they can be caught.
2823 *status = 0177 | (_SIGTRAP << 8);
2826 else if ((tsp.tts_event & TTEVT_LWP_CREATE)
2827 || (tsp.tts_event & TTEVT_LWP_TERMINATE)
2828 || (tsp.tts_event & TTEVT_LWP_EXIT))
2830 /* Make a thread event look like a breakpoint. Same comments
2831 * as for exec & fork events.
2835 printf ("..a thread 'event'\n");
2838 /* Also make thread events look like bpts, so they can be caught.
2840 *status = 0177 | (_SIGTRAP << 8);
2843 else if ((tsp.tts_event & TTEVT_EXIT))
2848 printf ("..an exit\n");
2851 /* Prevent rest of gdb from thinking this is
2852 * a new thread if for some reason it's never
2853 * seen the main thread before.
2855 inferior_ptid = pid_to_ptid (map_to_gdb_tid (real_tid)); /* HACK, FIX */
2857 *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode);
2860 else if (tsp.tts_event & TTEVT_SIGNAL)
2864 printf ("..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo);
2867 *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8);
2873 /* This means the process or thread terminated. But we should've
2874 caught an explicit exit/termination above. So warn (this is
2875 really an internal error) and claim the process or thread
2876 terminated with a SIGTRAP.
2879 warning ("process_wait: unknown process state");
2883 printf ("Process-level event %s, using tid %d\n",
2884 get_printable_name_of_ttrace_event (tsp.tts_event),
2891 target_post_wait (pid_to_ptid (tsp.tts_pid), *status);
2896 printf ("Done waiting, pid is %d, tid %d\n", real_pid, real_tid);
2899 /* All code external to this module uses the tid, but calls
2900 * it "pid". There's some tweaking so that the outside sees
2901 * the first thread as having the same number as the starting
2904 return_pid = map_to_gdb_tid (real_tid);
2906 /* Remember this for later use in "hppa_prepare_to_proceed".
2908 old_gdb_pid = PIDGET (inferior_ptid);
2909 reported_pid = return_pid;
2910 reported_bpt = ((tsp.tts_event & TTEVT_SIGNAL) && (5 == tsp.tts_u.tts_signal.tts_signo));
2912 if (real_tid == 0 || return_pid == 0)
2914 warning ("Internal error: process-wait failed.");
2921 /* This function causes the caller's process to be traced by its
2922 parent. This is intended to be called after GDB forks itself,
2923 and before the child execs the target. Despite the name, it
2924 is called by the child.
2926 Note that HP-UX ttrace is rather funky in how this is done.
2927 If the parent wants to get the initial exec event of a child,
2928 it must set the ttrace event mask of the child to include execs.
2929 (The child cannot do this itself.) This must be done after the
2930 child is forked, but before it execs.
2932 To coordinate the parent and child, we implement a semaphore using
2933 pipes. After SETTRC'ing itself, the child tells the parent that
2934 it is now traceable by the parent, and waits for the parent's
2935 acknowledgement. The parent can then set the child's event mask,
2936 and notify the child that it can now exec.
2938 (The acknowledgement by parent happens as a result of a call to
2939 child_acknowledge_created_inferior.)
2942 parent_attach_all (void)
2946 /* We need a memory home for a constant, to pass it to ttrace.
2947 The value of the constant is arbitrary, so long as both
2948 parent and child use the same value. Might as well use the
2949 "magic" constant provided by ttrace...
2951 uint64_t tc_magic_child = TT_VERSION;
2952 uint64_t tc_magic_parent = 0;
2954 tt_status = call_real_ttrace (
2959 (TTRACE_ARG_TYPE) TT_VERSION,
2965 /* Notify the parent that we're potentially ready to exec(). */
2966 write (startup_semaphore.child_channel[SEM_TALK],
2968 sizeof (tc_magic_child));
2970 /* Wait for acknowledgement from the parent. */
2971 read (startup_semaphore.parent_channel[SEM_LISTEN],
2973 sizeof (tc_magic_parent));
2975 if (tc_magic_child != tc_magic_parent)
2976 warning ("mismatched semaphore magic");
2978 /* Discard our copy of the semaphore. */
2979 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
2980 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
2981 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
2982 (void) close (startup_semaphore.child_channel[SEM_TALK]);
2987 /* Despite being file-local, this routine is dealing with
2988 * actual process IDs, not thread ids. That's because it's
2989 * called before the first "wait" call, and there's no map
2990 * yet from tids to pids.
2992 * When it is called, a forked child is running, but waiting on
2993 * the semaphore. If you stop the child and re-start it,
2994 * things get confused, so don't do that! An attached child is
2997 * Since this is called after either attach or run, we
2998 * have to be the common part of both.
3001 require_notification_of_events (int real_pid)
3004 ttevent_t notifiable_events;
3007 ttstate_t thread_state;
3011 printf ("Require notif, pid is %d\n", real_pid);
3014 /* Temporary HACK: tell inftarg.c/child_wait to not
3015 * loop until pids are the same.
3017 not_same_real_pid = 0;
3019 sigemptyset (¬ifiable_events.tte_signals);
3020 notifiable_events.tte_opts = TTEO_NONE;
3022 /* This ensures that forked children inherit their parent's
3023 * event mask, which we're setting here.
3025 * NOTE: if you debug gdb with itself, then the ultimate
3026 * debuggee gets flags set by the outermost gdb, as
3027 * a child of a child will still inherit.
3029 notifiable_events.tte_opts |= TTEO_PROC_INHERIT;
3031 notifiable_events.tte_events = TTEVT_DEFAULT;
3032 notifiable_events.tte_events |= TTEVT_SIGNAL;
3033 notifiable_events.tte_events |= TTEVT_EXEC;
3034 notifiable_events.tte_events |= TTEVT_EXIT;
3035 notifiable_events.tte_events |= TTEVT_FORK;
3036 notifiable_events.tte_events |= TTEVT_VFORK;
3037 notifiable_events.tte_events |= TTEVT_LWP_CREATE;
3038 notifiable_events.tte_events |= TTEVT_LWP_EXIT;
3039 notifiable_events.tte_events |= TTEVT_LWP_TERMINATE;
3041 tt_status = call_real_ttrace (
3042 TT_PROC_SET_EVENT_MASK,
3045 (TTRACE_ARG_TYPE) & notifiable_events,
3046 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3051 require_notification_of_exec_events (int real_pid)
3054 ttevent_t notifiable_events;
3057 ttstate_t thread_state;
3061 printf ("Require notif, pid is %d\n", real_pid);
3064 /* Temporary HACK: tell inftarg.c/child_wait to not
3065 * loop until pids are the same.
3067 not_same_real_pid = 0;
3069 sigemptyset (¬ifiable_events.tte_signals);
3070 notifiable_events.tte_opts = TTEO_NOSTRCCHLD;
3072 /* This ensures that forked children don't inherit their parent's
3073 * event mask, which we're setting here.
3075 notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT;
3077 notifiable_events.tte_events = TTEVT_DEFAULT;
3078 notifiable_events.tte_events |= TTEVT_EXEC;
3079 notifiable_events.tte_events |= TTEVT_EXIT;
3081 tt_status = call_real_ttrace (
3082 TT_PROC_SET_EVENT_MASK,
3085 (TTRACE_ARG_TYPE) & notifiable_events,
3086 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3091 /* This function is called by the parent process, with pid being the
3092 * ID of the child process, after the debugger has forked.
3095 child_acknowledge_created_inferior (int pid)
3097 /* We need a memory home for a constant, to pass it to ttrace.
3098 The value of the constant is arbitrary, so long as both
3099 parent and child use the same value. Might as well use the
3100 "magic" constant provided by ttrace...
3102 uint64_t tc_magic_parent = TT_VERSION;
3103 uint64_t tc_magic_child = 0;
3105 /* Wait for the child to tell us that it has forked. */
3106 read (startup_semaphore.child_channel[SEM_LISTEN],
3108 sizeof (tc_magic_child));
3110 /* Clear thread info now. We'd like to do this in
3111 * "require...", but that messes up attach.
3113 clear_thread_info ();
3115 /* Tell the "rest of gdb" that the initial thread exists.
3116 * This isn't really a hack. Other thread-based versions
3117 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3119 * Q: Why don't we also add this thread to the local
3120 * list via "add_tthread"?
3122 * A: Because we don't know the tid, and can't stop the
3123 * the process safely to ask what it is. Anyway, we'll
3124 * add it when it gets the EXEC event.
3126 add_thread (pid); /* in thread.c */
3128 /* We can now set the child's ttrace event mask.
3130 require_notification_of_exec_events (pid);
3132 /* Tell ourselves that the process is running.
3134 process_state = RUNNING;
3136 /* Notify the child that it can exec. */
3137 write (startup_semaphore.parent_channel[SEM_TALK],
3139 sizeof (tc_magic_parent));
3141 /* Discard our copy of the semaphore. */
3142 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3143 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3144 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3145 (void) close (startup_semaphore.child_channel[SEM_TALK]);
3150 * arrange for notification of all events by
3151 * calling require_notification_of_events.
3154 child_post_startup_inferior (ptid_t ptid)
3156 require_notification_of_events (PIDGET (ptid));
3159 /* From here on, we should expect tids rather than pids.
3162 hppa_enable_catch_fork (int tid)
3165 ttevent_t ttrace_events;
3167 /* Get the set of events that are currently enabled.
3169 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3171 (TTRACE_ARG_TYPE) & ttrace_events,
3172 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3175 perror_with_name ("ttrace");
3177 /* Add forks to that set. */
3178 ttrace_events.tte_events |= TTEVT_FORK;
3182 printf ("enable fork, tid is %d\n", tid);
3185 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3187 (TTRACE_ARG_TYPE) & ttrace_events,
3188 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3191 perror_with_name ("ttrace");
3196 hppa_disable_catch_fork (int tid)
3199 ttevent_t ttrace_events;
3201 /* Get the set of events that are currently enabled.
3203 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3205 (TTRACE_ARG_TYPE) & ttrace_events,
3206 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3210 perror_with_name ("ttrace");
3212 /* Remove forks from that set. */
3213 ttrace_events.tte_events &= ~TTEVT_FORK;
3217 printf ("disable fork, tid is %d\n", tid);
3220 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3222 (TTRACE_ARG_TYPE) & ttrace_events,
3223 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3227 perror_with_name ("ttrace");
3231 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3233 child_insert_fork_catchpoint (int tid)
3235 /* Enable reporting of fork events from the kernel. */
3236 /* ??rehrauer: For the moment, we're always enabling these events,
3237 and just ignoring them if there's no catchpoint to catch them.
3244 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3246 child_remove_fork_catchpoint (int tid)
3248 /* Disable reporting of fork events from the kernel. */
3249 /* ??rehrauer: For the moment, we're always enabling these events,
3250 and just ignoring them if there's no catchpoint to catch them.
3258 hppa_enable_catch_vfork (int tid)
3261 ttevent_t ttrace_events;
3263 /* Get the set of events that are currently enabled.
3265 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3267 (TTRACE_ARG_TYPE) & ttrace_events,
3268 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3272 perror_with_name ("ttrace");
3274 /* Add vforks to that set. */
3275 ttrace_events.tte_events |= TTEVT_VFORK;
3279 printf ("enable vfork, tid is %d\n", tid);
3282 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3284 (TTRACE_ARG_TYPE) & ttrace_events,
3285 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3289 perror_with_name ("ttrace");
3294 hppa_disable_catch_vfork (int tid)
3297 ttevent_t ttrace_events;
3299 /* Get the set of events that are currently enabled. */
3300 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3302 (TTRACE_ARG_TYPE) & ttrace_events,
3303 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3307 perror_with_name ("ttrace");
3309 /* Remove vforks from that set. */
3310 ttrace_events.tte_events &= ~TTEVT_VFORK;
3314 printf ("disable vfork, tid is %d\n", tid);
3316 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3318 (TTRACE_ARG_TYPE) & ttrace_events,
3319 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3323 perror_with_name ("ttrace");
3327 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3329 child_insert_vfork_catchpoint (int tid)
3331 /* Enable reporting of vfork events from the kernel. */
3332 /* ??rehrauer: For the moment, we're always enabling these events,
3333 and just ignoring them if there's no catchpoint to catch them.
3340 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3342 child_remove_vfork_catchpoint (int tid)
3344 /* Disable reporting of vfork events from the kernel. */
3345 /* ??rehrauer: For the moment, we're always enabling these events,
3346 and just ignoring them if there's no catchpoint to catch them.
3352 #if defined(CHILD_HAS_FORKED)
3354 /* Q: Do we need to map the returned process ID to a thread ID?
3356 * A: I don't think so--here we want a _real_ pid. Any later
3357 * operations will call "require_notification_of_events" and
3358 * start the mapping.
3361 child_has_forked (int tid, int *childpid)
3364 ttstate_t ttrace_state;
3367 /* Do we have cached thread state that we can consult? If so, use it. */
3368 tinfo = find_thread_info (map_from_gdb_tid (tid));
3371 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3374 /* Nope, must read the thread's current state */
3377 tt_status = call_ttrace (TT_LWP_GET_STATE,
3379 (TTRACE_ARG_TYPE) & ttrace_state,
3380 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3384 perror_with_name ("ttrace");
3390 if (ttrace_state.tts_event & TTEVT_FORK)
3392 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3401 #if defined(CHILD_HAS_VFORKED)
3403 /* See child_has_forked for pid discussion.
3406 child_has_vforked (int tid, int *childpid)
3409 ttstate_t ttrace_state;
3412 /* Do we have cached thread state that we can consult? If so, use it. */
3413 tinfo = find_thread_info (map_from_gdb_tid (tid));
3415 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3417 /* Nope, must read the thread's current state */
3420 tt_status = call_ttrace (TT_LWP_GET_STATE,
3422 (TTRACE_ARG_TYPE) & ttrace_state,
3423 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3427 perror_with_name ("ttrace");
3433 if (ttrace_state.tts_event & TTEVT_VFORK)
3435 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3444 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3446 child_can_follow_vfork_prior_to_exec (void)
3448 /* ttrace does allow this.
3450 ??rehrauer: However, I had major-league problems trying to
3451 convince wait_for_inferior to handle that case. Perhaps when
3452 it is rewritten to grok multiple processes in an explicit way...
3459 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3461 child_insert_exec_catchpoint (int tid)
3463 /* Enable reporting of exec events from the kernel. */
3464 /* ??rehrauer: For the moment, we're always enabling these events,
3465 and just ignoring them if there's no catchpoint to catch them.
3472 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3474 child_remove_exec_catchpoint (int tid)
3476 /* Disable reporting of execevents from the kernel. */
3477 /* ??rehrauer: For the moment, we're always enabling these events,
3478 and just ignoring them if there's no catchpoint to catch them.
3485 #if defined(CHILD_HAS_EXECD)
3487 child_has_execd (int tid, char **execd_pathname)
3490 ttstate_t ttrace_state;
3493 /* Do we have cached thread state that we can consult? If so, use it. */
3494 tinfo = find_thread_info (map_from_gdb_tid (tid));
3496 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3498 /* Nope, must read the thread's current state */
3501 tt_status = call_ttrace (TT_LWP_GET_STATE,
3503 (TTRACE_ARG_TYPE) & ttrace_state,
3504 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3508 perror_with_name ("ttrace");
3514 if (ttrace_state.tts_event & TTEVT_EXEC)
3516 /* See child_pid_to_exec_file in this file: this is a macro.
3518 char *exec_file = target_pid_to_exec_file (tid);
3520 *execd_pathname = savestring (exec_file, strlen (exec_file));
3529 #if defined(CHILD_HAS_SYSCALL_EVENT)
3531 child_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
3534 ttstate_t ttrace_state;
3537 /* Do we have cached thread state that we can consult? If so, use it. */
3538 tinfo = find_thread_info (map_from_gdb_tid (pid));
3540 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3542 /* Nope, must read the thread's current state */
3545 tt_status = call_ttrace (TT_LWP_GET_STATE,
3547 (TTRACE_ARG_TYPE) & ttrace_state,
3548 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3552 perror_with_name ("ttrace");
3558 *kind = TARGET_WAITKIND_SPURIOUS; /* Until proven otherwise... */
3561 if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY)
3562 *kind = TARGET_WAITKIND_SYSCALL_ENTRY;
3563 else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN)
3564 *kind = TARGET_WAITKIND_SYSCALL_RETURN;
3568 *syscall_id = ttrace_state.tts_scno;
3575 #if defined(CHILD_THREAD_ALIVE)
3577 /* Check to see if the given thread is alive.
3579 * We'll trust the thread list, as the more correct
3580 * approach of stopping the process and spinning down
3581 * the OS's thread list is _very_ expensive.
3583 * May need a FIXME for that reason.
3586 child_thread_alive (ptid_t ptid)
3588 lwpid_t gdb_tid = PIDGET (ptid);
3591 /* This spins down the lists twice.
3592 * Possible peformance improvement here!
3594 tid = map_from_gdb_tid (gdb_tid);
3595 return !is_terminated (tid);
3602 /* This function attempts to read the specified number of bytes from the
3603 save_state_t that is our view into the hardware registers, starting at
3604 ss_offset, and ending at ss_offset + sizeof_buf - 1
3606 If this function succeeds, it deposits the fetched bytes into buf,
3609 If it fails, it returns a negative result. The contents of buf are
3610 undefined it this function fails.
3613 read_from_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf,
3617 register_value_t register_value = 0;
3619 tt_status = call_ttrace (TT_LWP_RUREGS,
3622 (TTRACE_ARG_TYPE) sizeof_buf,
3623 (TTRACE_ARG_TYPE) buf);
3626 /* Map ttrace's version of success to our version.
3627 * Sometime ttrace returns 0, but that's ok here.
3635 /* This function attempts to write the specified number of bytes to the
3636 save_state_t that is our view into the hardware registers, starting at
3637 ss_offset, and ending at ss_offset + sizeof_buf - 1
3639 If this function succeeds, it deposits the bytes in buf, and returns 0.
3641 If it fails, it returns a negative result. The contents of the save_state_t
3642 are undefined it this function fails.
3645 write_to_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf,
3649 register_value_t register_value = 0;
3651 tt_status = call_ttrace (TT_LWP_WUREGS,
3654 (TTRACE_ARG_TYPE) sizeof_buf,
3655 (TTRACE_ARG_TYPE) buf);
3660 /* This function is a sop to the largeish number of direct calls
3661 to call_ptrace that exist in other files. Rather than create
3662 functions whose name abstracts away from ptrace, and change all
3663 the present callers of call_ptrace, we'll do the expedient (and
3664 perhaps only practical) thing.
3666 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3667 process. Thus, we must translate all ptrace requests into their
3668 process-specific, ttrace equivalents.
3671 call_ptrace (int pt_request, int gdb_tid, PTRACE_ARG3_TYPE addr, int data)
3674 TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr;
3675 TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data;
3676 TTRACE_ARG_TYPE tt_addr2 = TT_NIL;
3678 register_value_t register_value;
3681 /* Perform the necessary argument translation. Note that some
3682 cases are funky enough in the ttrace realm that we handle them
3687 /* The following cases cannot conveniently be handled conveniently
3688 by merely adjusting the ptrace arguments and feeding into the
3689 generic call to ttrace at the bottom of this function.
3691 Note that because all branches of this switch end in "return",
3692 there's no need for any "break" statements.
3695 return parent_attach_all ();
3698 tt_status = read_from_register_save_state (gdb_tid,
3701 sizeof (register_value));
3704 return register_value;
3707 register_value = (int) tt_data;
3708 tt_status = write_to_register_save_state (gdb_tid,
3711 sizeof (register_value));
3716 tt_status = call_ttrace (TT_PROC_RDTEXT, /* Implicit 4-byte xfer becomes block-xfer. */
3719 (TTRACE_ARG_TYPE) 4,
3720 (TTRACE_ARG_TYPE) & read_buf);
3726 tt_status = call_ttrace (TT_PROC_RDDATA, /* Implicit 4-byte xfer becomes block-xfer. */
3729 (TTRACE_ARG_TYPE) 4,
3730 (TTRACE_ARG_TYPE) & read_buf);
3736 tt_status = call_real_ttrace (TT_PROC_ATTACH,
3737 map_from_gdb_tid (gdb_tid),
3740 (TTRACE_ARG_TYPE) TT_VERSION,
3746 /* The following cases are handled by merely adjusting the ptrace
3747 arguments and feeding into the generic call to ttrace.
3750 tt_request = TT_PROC_DETACH;
3754 tt_request = TT_PROC_WRTEXT; /* Translates 4-byte xfer to block-xfer. */
3755 tt_data = 4; /* This many bytes. */
3756 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3760 tt_request = TT_PROC_WRDATA; /* Translates 4-byte xfer to block-xfer. */
3761 tt_data = 4; /* This many bytes. */
3762 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3766 tt_request = TT_PROC_RDTEXT;
3770 tt_request = TT_PROC_RDDATA;
3774 tt_request = TT_PROC_WRTEXT;
3778 tt_request = TT_PROC_WRDATA;
3782 tt_request = TT_PROC_CONTINUE;
3786 tt_request = TT_LWP_SINGLE; /* Should not be making this request? */
3790 tt_request = TT_PROC_EXIT;
3793 case PT_GET_PROCESS_PATHNAME:
3794 tt_request = TT_PROC_GET_PATHNAME;
3798 tt_request = pt_request; /* Let ttrace be the one to complain. */
3802 return call_ttrace (tt_request,
3809 /* Kill that pesky process!
3812 kill_inferior (void)
3817 thread_info **paranoia;
3820 if (PIDGET (inferior_ptid) == 0)
3823 /* Walk the list of "threads", some of which are "pseudo threads",
3824 aka "processes". For each that is NOT inferior_ptid, stop it,
3827 You see, we may not have just a single process to kill. If we're
3828 restarting or quitting or detaching just after the inferior has
3829 forked, then we've actually two processes to clean up.
3831 But we can't just call target_mourn_inferior() for each, since that
3832 zaps the target vector.
3835 paranoia = (thread_info **) xmalloc (thread_head.count *
3836 sizeof (thread_info *));
3839 t = thread_head.head;
3843 paranoia[para_count] = t;
3844 for (i = 0; i < para_count; i++)
3846 if (t->next == paranoia[i])
3848 warning ("Bad data in gdb's thread data; repairing.");
3854 if (t->am_pseudo && (t->pid != PIDGET (inferior_ptid)))
3856 call_ttrace (TT_PROC_EXIT,
3867 call_ttrace (TT_PROC_EXIT,
3868 PIDGET (inferior_ptid),
3872 target_mourn_inferior ();
3873 clear_thread_info ();
3877 #ifndef CHILD_RESUME
3879 /* Sanity check a thread about to be continued.
3882 thread_dropping_event_check (thread_info *p)
3887 * This seems to happen when we "next" over a
3888 * "fork()" while following the parent. If it's
3889 * the FORK event, that's ok. If it's a SIGNAL
3890 * in the unfollowed child, that's ok to--but
3891 * how can we know that's what's going on?
3897 if (p->last_stop_state.tts_event == TTEVT_FORK)
3902 else if (p->last_stop_state.tts_event == TTEVT_SIGNAL)
3904 /* Ok, close eyes and let it happen.
3910 /* This shouldn't happen--we're dropping a
3913 warning ("About to continue process %d, thread %d with unhandled event %s.",
3915 get_printable_name_of_ttrace_event (
3916 p->last_stop_state.tts_event));
3926 /* No saved state, have to assume it failed.
3928 warning ("About to continue process %d, thread %d with unhandled event.",
3937 } /* thread_dropping_event_check */
3939 /* Use a loop over the threads to continue all the threads but
3940 * the one specified, which is to be stepped.
3943 threads_continue_all_but_one (lwpid_t gdb_tid, int signal)
3954 printf ("Using loop over threads to step/resume with signals\n");
3957 /* First update the thread list.
3960 real_tid = map_from_gdb_tid (gdb_tid);
3961 real_pid = get_pid_for (real_tid);
3963 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
3964 while (0 != scan_tid)
3968 /* FIX: later should check state is stopped;
3969 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
3972 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
3973 printf ("About to continue non-stopped thread %d\n", scan_tid);
3976 p = find_thread_info (scan_tid);
3979 add_tthread (real_pid, scan_tid);
3980 p = find_thread_info (scan_tid);
3982 /* This is either a newly-created thread or the
3983 * result of a fork; in either case there's no
3984 * actual event to worry about.
3988 if (state.tts_event != TTEVT_NONE)
3990 /* Oops, do need to worry!
3992 warning ("Unexpected thread with \"%s\" event.",
3993 get_printable_name_of_ttrace_event (state.tts_event));
3996 else if (scan_tid != p->tid)
3997 error ("Bad data in thread database.");
4002 printf ("Why are we continuing a dead thread?\n");
4007 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4010 /* Remove unseen threads.
4012 update_thread_list ();
4014 /* Now run down the thread list and continue or step.
4016 for (p = thread_head.head; p; p = p->next)
4021 thread_dropping_event_check (p);
4023 /* Pass the correct signals along.
4027 thread_signal = p->signal_value;
4033 if (p->tid != real_tid)
4036 * Not the thread of interest, so continue it
4037 * as the user expects.
4039 if (p->stepping_mode == DO_STEP)
4041 /* Just step this thread.
4047 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4052 /* Regular continue (default case).
4058 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4064 /* Step the thread of interest.
4070 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4073 } /* Loop over threads */
4074 } /* End threads_continue_all_but_one */
4076 /* Use a loop over the threads to continue all the threads.
4077 * This is done when a signal must be sent to any of the threads.
4080 threads_continue_all_with_signals (lwpid_t gdb_tid, int signal)
4091 printf ("Using loop over threads to resume with signals\n");
4094 /* Scan and update thread list.
4097 real_tid = map_from_gdb_tid (gdb_tid);
4098 real_pid = get_pid_for (real_tid);
4100 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
4101 while (0 != scan_tid)
4106 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
4107 warning ("About to continue non-stopped thread %d\n", scan_tid);
4110 p = find_thread_info (scan_tid);
4113 add_tthread (real_pid, scan_tid);
4114 p = find_thread_info (scan_tid);
4116 /* This is either a newly-created thread or the
4117 * result of a fork; in either case there's no
4118 * actual event to worry about.
4122 if (state.tts_event != TTEVT_NONE)
4124 /* Oops, do need to worry!
4126 warning ("Unexpected thread with \"%s\" event.",
4127 get_printable_name_of_ttrace_event (state.tts_event));
4134 printf ("Why are we continuing a dead thread? (1)\n");
4139 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4142 /* Remove unseen threads from our list.
4144 update_thread_list ();
4146 /* Continue the threads.
4148 for (p = thread_head.head; p; p = p->next)
4153 thread_dropping_event_check (p);
4155 /* Pass the correct signals along.
4157 if (p->tid == real_tid)
4159 thread_signal = signal;
4162 else if (p->have_signal)
4164 thread_signal = p->signal_value;
4170 if (p->stepping_mode == DO_STEP)
4176 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4181 /* Continue this thread (default case).
4187 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4191 } /* End threads_continue_all_with_signals */
4193 /* Step one thread only.
4196 thread_fake_step (lwpid_t tid, enum target_signal signal)
4203 printf ("Doing a fake-step over a bpt, etc. for %d\n", tid);
4205 if (is_terminated (tid))
4206 printf ("Why are we continuing a dead thread? (4)\n");
4210 if (doing_fake_step)
4211 warning ("Step while step already in progress.");
4213 /* See if there's a saved signal value for this
4214 * thread to be passed on, but no current signal.
4216 p = find_thread_info (tid);
4219 if (p->have_signal && signal == TARGET_SIGNAL_0)
4221 /* Pass on a saved signal.
4223 signal = p->signal_value;
4230 warning ("Internal error: continuing unhandled thread.");
4232 call_ttrace (TT_LWP_SINGLE,
4235 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4238 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4239 * for this thread only, and clear any saved signal info.
4241 doing_fake_step = 1;
4242 fake_step_tid = tid;
4244 } /* End thread_fake_step */
4246 /* Continue one thread when a signal must be sent to it.
4249 threads_continue_one_with_signal (lwpid_t gdb_tid, int signal)
4257 printf ("Continuing one thread with a signal\n");
4260 real_tid = map_from_gdb_tid (gdb_tid);
4261 real_pid = get_pid_for (real_tid);
4263 p = find_thread_info (real_tid);
4266 add_tthread (real_pid, real_tid);
4272 printf ("Why are we continuing a dead thread? (2)\n");
4276 warning ("Internal error: continuing unhandled thread.");
4280 call_ttrace (TT_LWP_CONTINUE,
4283 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4288 #ifndef CHILD_RESUME
4290 /* Resume execution of the inferior process.
4292 * This routine is in charge of setting the "handled" bits.
4294 * If STEP is zero, continue it.
4295 * If STEP is nonzero, single-step it.
4297 * If SIGNAL is nonzero, give it that signal.
4299 * If TID is -1, apply to all threads.
4300 * If TID is not -1, apply to specified thread.
4304 * TID \________________________________________________
4306 * -1 | Step current Continue all threads
4307 * | thread and (but which gets any
4308 * | continue others signal?--We look at
4309 * | "inferior_ptid")
4311 * N | Step _this_ thread Continue _this_ thread
4312 * | and leave others and leave others
4313 * | stopped; internally stopped; used only for
4314 * | used by gdb, never hardware watchpoints
4315 * | a user command. and attach, never a
4319 child_resume (ptid_t ptid, int step, enum target_signal signal)
4321 int resume_all_threads;
4323 process_state_t new_process_state;
4324 lwpid_t gdb_tid = PIDGET (ptid);
4326 resume_all_threads =
4327 (gdb_tid == INFTTRACE_ALL_THREADS) ||
4330 if (resume_all_threads)
4332 /* Resume all threads, but first pick a tid value
4333 * so we can get the pid when in call_ttrace doing
4336 if (vfork_in_flight)
4337 tid = vforking_child_pid;
4339 tid = map_from_gdb_tid (PIDGET (inferior_ptid));
4342 tid = map_from_gdb_tid (gdb_tid);
4347 if (more_events_left)
4348 printf ("More events; ");
4351 printf ("Sending signal %d; ", signal);
4353 if (resume_all_threads)
4356 printf ("Continue process %d\n", tid);
4358 printf ("Step/continue thread %d\n", tid);
4363 printf ("Continue thread %d\n", tid);
4365 printf ("Step just thread %d\n", tid);
4368 if (vfork_in_flight)
4369 printf ("Vfork in flight\n");
4373 if (process_state == RUNNING)
4374 warning ("Internal error in resume logic; doing resume or step anyway.");
4376 if (!step /* Asked to continue... */
4377 && resume_all_threads /* whole process.. */
4378 && signal != 0 /* with a signal... */
4379 && more_events_left > 0)
4380 { /* but we can't yet--save it! */
4382 /* Continue with signal means we have to set the pending
4383 * signal value for this thread.
4389 printf ("Saving signal %d for thread %d\n", signal, tid);
4392 k = find_thread_info (tid);
4396 k->signal_value = signal;
4401 printf ("Why are we continuing a dead thread? (3)\n");
4409 printf ("No thread info for tid %d\n", tid);
4414 /* Are we faking this "continue" or "step"?
4416 * We used to do steps by continuing all the threads for
4417 * which the events had been handled already. While
4418 * conceptually nicer (hides it all in a lower level), this
4419 * can lead to starvation and a hang (e.g. all but one thread
4420 * are unhandled at a breakpoint just before a "join" operation,
4421 * and one thread is in the join, and the user wants to step that
4424 if (resume_all_threads /* Whole process, therefore user command */
4425 && more_events_left > 0)
4426 { /* But we can't do this yet--fake it! */
4431 /* No need to do any notes on a per-thread
4432 * basis--we're done!
4434 #ifdef WAIT_BUFFER_DEBUG
4436 printf ("Faking a process resume.\n");
4444 #ifdef WAIT_BUFFER_DEBUG
4446 printf ("Faking a process step.\n");
4451 p = find_thread_info (tid);
4454 warning ("No thread information for tid %d, 'next' command ignored.\n", tid);
4463 printf ("Why are we continuing a dead thread? (3.5)\n");
4466 if (p->stepping_mode != DO_DEFAULT)
4468 warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored.");
4474 p->stepping_mode = DO_STEP;
4476 p->stepping_mode = DO_CONTINUE;
4479 } /* Have thread info */
4480 } /* Must fake step or go */
4482 /* Execept for fake-steps, from here on we know we are
4483 * going to wind up with a running process which will
4486 new_process_state = RUNNING;
4488 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4489 * it was. (If GDB wanted it to start some other way, we have already
4490 * written a new PC value to the child.)
4492 * If this system does not support PT_STEP, a higher level function will
4493 * have called single_step() to transmute the step request into a
4494 * continue request (by setting breakpoints on all possible successor
4495 * instructions), so we don't have to worry about that here.
4499 if (resume_all_threads)
4502 * Regular user step: other threads get a "continue".
4504 threads_continue_all_but_one (tid, signal);
4505 clear_all_handled ();
4506 clear_all_stepping_mode ();
4511 /* "Fake step": gdb is stepping one thread over a
4512 * breakpoint, watchpoint, or out of a library load
4513 * event, etc. The rest just stay where they are.
4515 * Also used when there are pending events: we really
4516 * step the current thread, but leave the rest stopped.
4517 * Users can't request this, but "wait_for_inferior"
4520 thread_fake_step (tid, signal);
4522 /* Clear the "handled" state of this thread, because
4523 * we'll soon get a new event for it. Other events
4524 * stay as they were.
4526 clear_handled (tid);
4527 clear_stepping_mode (tid);
4528 new_process_state = FAKE_STEPPING;
4534 /* TT_LWP_CONTINUE can pass signals to threads,
4535 * TT_PROC_CONTINUE can't. So if there are any
4536 * signals to pass, we have to use the (slower)
4537 * loop over the stopped threads.
4539 * Equally, if we have to not continue some threads,
4540 * due to saved events, we have to use the loop.
4542 if ((signal != 0) || saved_signals_exist ())
4544 if (resume_all_threads)
4549 printf ("Doing a continue by loop of all threads\n");
4552 threads_continue_all_with_signals (tid, signal);
4554 clear_all_handled ();
4555 clear_all_stepping_mode ();
4561 printf ("Doing a continue w/signal of just thread %d\n", tid);
4564 threads_continue_one_with_signal (tid, signal);
4566 /* Clear the "handled" state of this thread, because
4567 * we'll soon get a new event for it. Other events
4568 * can stay as they were.
4570 clear_handled (tid);
4571 clear_stepping_mode (tid);
4577 /* No signals to send.
4579 if (resume_all_threads)
4583 printf ("Doing a continue by process of process %d\n", tid);
4586 if (more_events_left > 0)
4588 warning ("Losing buffered events on continue.");
4589 more_events_left = 0;
4592 call_ttrace (TT_PROC_CONTINUE,
4598 clear_all_handled ();
4599 clear_all_stepping_mode ();
4607 printf ("Doing a continue of just thread %d\n", tid);
4608 if (is_terminated (tid))
4609 printf ("Why are we continuing a dead thread? (5)\n");
4613 call_ttrace (TT_LWP_CONTINUE,
4619 /* Clear the "handled" state of this thread, because
4620 * we'll soon get a new event for it. Other events
4621 * can stay as they were.
4623 clear_handled (tid);
4624 clear_stepping_mode (tid);
4629 process_state = new_process_state;
4631 #ifdef WAIT_BUFFER_DEBUG
4633 printf ("Process set to %s\n",
4634 get_printable_name_of_process_state (process_state));
4638 #endif /* CHILD_RESUME */
4641 #ifdef ATTACH_DETACH
4645 * One worry is that we may not be attaching to "inferior_ptid"
4646 * and thus may not want to clear out our data. FIXME?
4650 update_thread_state_after_attach (int pid, attach_continue_t kind_of_go)
4653 ttstate_t thread_state;
4657 /* The process better be stopped.
4659 if (process_state != STOPPED
4660 && process_state != VFORKING)
4661 warning ("Internal error attaching.");
4663 /* Clear out old tthread info and start over. This has the
4664 * side effect of ensuring that the TRAP is reported as being
4665 * in the right thread (re-mapped from tid to pid).
4667 * It's because we need to add the tthread _now_ that we
4668 * need to call "clear_thread_info" _now_, and that's why
4669 * "require_notification_of_events" doesn't clear the thread
4670 * info (it's called later than this routine).
4672 clear_thread_info ();
4675 for (tid = get_process_first_stopped_thread_id (pid, &thread_state);
4677 tid = get_process_next_stopped_thread_id (pid, &thread_state))
4686 printf ("Attaching to process %d, thread %d\n",
4691 /* Tell ourselves and the "rest of gdb" that this thread
4694 * This isn't really a hack. Other thread-based versions
4695 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4697 * We don't need to do mapping here, as we know this
4698 * is the first thread and thus gets the real pid
4699 * (and is "inferior_ptid").
4701 * NOTE: it probably isn't the originating thread,
4702 * but that doesn't matter (we hope!).
4704 add_tthread (pid, tid);
4705 p = find_thread_info (tid);
4706 if (NULL == p) /* ?We just added it! */
4707 error ("Internal error adding a thread on attach.");
4709 copy_ttstate_t (&p->last_stop_state, &thread_state);
4712 if (DO_ATTACH_CONTINUE == kind_of_go)
4715 * If we are going to CONTINUE afterwards,
4716 * raising a SIGTRAP, don't bother trying to
4717 * handle this event. But check first!
4719 switch (p->last_stop_state.tts_event)
4723 /* Ok to set this handled.
4728 warning ("Internal error; skipping event %s on process %d, thread %d.",
4729 get_printable_name_of_ttrace_event (
4730 p->last_stop_state.tts_event),
4734 set_handled (pid, tid);
4739 /* There will be no "continue" opertion, so the
4740 * process remains stopped. Don't set any events
4741 * handled except the "gimmies".
4743 switch (p->last_stop_state.tts_event)
4747 /* Ok to ignore this.
4749 set_handled (pid, tid);
4754 /* Expected "other" FORK or EXEC event from a
4760 printf ("Internal error: failed to handle event %s on process %d, thread %d.",
4761 get_printable_name_of_ttrace_event (
4762 p->last_stop_state.tts_event),
4767 add_thread (tid); /* in thread.c */
4775 /* One mustn't call ttrace_wait() after attaching via ttrace,
4776 'cause the process is stopped already.
4778 However, the upper layers of gdb's execution control will
4779 want to wait after attaching (but not after forks, in
4780 which case they will be doing a "target_resume", anticipating
4781 a later TTEVT_EXEC or TTEVT_FORK event).
4783 To make this attach() implementation more compatible with
4784 others, we'll make the attached-to process raise a SIGTRAP.
4786 Issue: this continues only one thread. That could be
4787 dangerous if the thread is blocked--the process won't run
4788 and no trap will be raised. FIX! (check state.tts_flags?
4789 need one that's either TTS_WASRUNNING--but we've stopped
4790 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4792 if (DO_ATTACH_CONTINUE == kind_of_go)
4794 tt_status = call_real_ttrace (
4799 (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP),
4802 perror_with_name ("ttrace");
4804 clear_handled (a_thread); /* So TRAP will be reported. */
4808 process_state = RUNNING;
4813 #endif /* ATTACH_DETACH */
4816 #ifdef ATTACH_DETACH
4817 /* Start debugging the process whose number is PID.
4825 tt_status = call_real_ttrace (
4830 (TTRACE_ARG_TYPE) TT_VERSION,
4833 perror_with_name ("ttrace attach");
4835 /* If successful, the process is now stopped.
4837 process_state = STOPPED;
4839 /* Our caller ("attach_command" in "infcmd.c")
4840 * expects to do a "wait_for_inferior" after
4841 * the attach, so make sure the inferior is
4842 * running when we're done.
4844 update_thread_state_after_attach (pid, DO_ATTACH_CONTINUE);
4850 #if defined(CHILD_POST_ATTACH)
4852 child_post_attach (int pid)
4856 printf ("child-post-attach call\n");
4859 require_notification_of_events (pid);
4864 /* Stop debugging the process whose number is PID
4865 and continue it with signal number SIGNAL.
4866 SIGNAL = 0 means just continue it.
4872 call_ttrace (TT_PROC_DETACH,
4873 PIDGET (inferior_ptid),
4875 (TTRACE_ARG_TYPE) signal,
4879 clear_thread_info ();
4881 /* Process-state? */
4883 #endif /* ATTACH_DETACH */
4886 /* Default the type of the ttrace transfer to int. */
4887 #ifndef TTRACE_XFER_TYPE
4888 #define TTRACE_XFER_TYPE int
4892 _initialize_kernel_u_addr (void)
4896 #if !defined (CHILD_XFER_MEMORY)
4897 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
4898 in the NEW_SUN_TTRACE case.
4899 It ought to be straightforward. But it appears that writing did
4900 not write the data that I specified. I cannot understand where
4901 it got the data that it actually did write. */
4903 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
4904 to debugger memory starting at MYADDR. Copy to inferior if
4905 WRITE is nonzero. TARGET is ignored.
4907 Returns the length copied, which is either the LEN argument or zero.
4908 This xfer function does not do partial moves, since child_ops
4909 doesn't allow memory operations to cross below us in the target stack
4913 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
4914 struct mem_attrib *attrib,
4915 struct target_ops *target)
4918 /* Round starting address down to longword boundary. */
4919 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (TTRACE_XFER_TYPE);
4920 /* Round ending address up; get number of longwords that makes. */
4922 = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1)
4923 / sizeof (TTRACE_XFER_TYPE);
4924 /* Allocate buffer of that many longwords. */
4925 register TTRACE_XFER_TYPE *buffer
4926 = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));
4930 /* Fill start and end extra bytes of buffer with existing memory data. */
4932 if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE))
4934 /* Need part of initial word -- fetch it. */
4935 buffer[0] = call_ttrace (TT_LWP_RDTEXT,
4936 PIDGET (inferior_ptid),
4937 (TTRACE_ARG_TYPE) addr,
4942 if (count > 1) /* FIXME, avoid if even boundary */
4944 buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT,
4945 PIDGET (inferior_ptid),
4947 (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))),
4952 /* Copy data to be written over corresponding part of buffer */
4954 memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
4958 /* Write the entire buffer. */
4960 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
4963 call_ttrace (TT_LWP_WRDATA,
4964 PIDGET (inferior_ptid),
4965 (TTRACE_ARG_TYPE) addr,
4966 (TTRACE_ARG_TYPE) buffer[i],
4970 /* Using the appropriate one (I or D) is necessary for
4971 Gould NP1, at least. */
4973 call_ttrace (TT_LWP_WRTEXT,
4974 PIDGET (inferior_ptid),
4975 (TTRACE_ARG_TYPE) addr,
4976 (TTRACE_ARG_TYPE) buffer[i],
4985 /* Read all the longwords */
4986 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
4989 buffer[i] = call_ttrace (TT_LWP_RDTEXT,
4990 PIDGET (inferior_ptid),
4991 (TTRACE_ARG_TYPE) addr,
4999 /* Copy appropriate bytes out of the buffer. */
5001 (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
5011 int udot_off; /* Offset into user struct */
5012 int udot_val; /* Value from user struct at udot_off */
5013 char mess[128]; /* For messages */
5015 if (!target_has_execution)
5017 error ("The program is not being run.");
5020 #if !defined (KERNEL_U_SIZE)
5022 /* Adding support for this command is easy. Typically you just add a
5023 routine, called "kernel_u_size" that returns the size of the user
5024 struct, to the appropriate *-nat.c file and then add to the native
5025 config file "#define KERNEL_U_SIZE kernel_u_size()" */
5026 error ("Don't know how large ``struct user'' is in this version of gdb.");
5030 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
5032 if ((udot_off % 24) == 0)
5036 printf_filtered ("\n");
5038 printf_filtered ("%04x:", udot_off);
5040 udot_val = call_ttrace (TT_LWP_RUREGS,
5041 PIDGET (inferior_ptid),
5042 (TTRACE_ARG_TYPE) udot_off,
5047 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
5048 perror_with_name (mess);
5050 /* Avoid using nonportable (?) "*" in print specs */
5051 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
5053 printf_filtered ("\n");
5057 #endif /* !defined (CHILD_XFER_MEMORY). */
5059 /* TTrace version of "target_pid_to_exec_file"
5062 child_pid_to_exec_file (int tid)
5064 static char exec_file_buffer[1024];
5066 CORE_ADDR top_of_stack;
5071 ptid_t saved_inferior_ptid;
5073 /* As of 10.x HP-UX, there's an explicit request to get the
5076 tt_status = call_ttrace (TT_PROC_GET_PATHNAME,
5078 (TTRACE_ARG_TYPE) exec_file_buffer,
5079 (TTRACE_ARG_TYPE) sizeof (exec_file_buffer) - 1,
5082 return exec_file_buffer;
5084 /* ??rehrauer: The above request may or may not be broken. It
5085 doesn't seem to work when I use it. But, it may be designed
5086 to only work immediately after an exec event occurs. (I'm
5087 waiting for COSL to explain.)
5089 In any case, if it fails, try a really, truly amazingly gross
5090 hack that DDE uses, of pawing through the process' data
5091 segment to find the pathname.
5093 top_of_stack = (TARGET_PTR_BIT == 64 ? 0x800003ffff7f0000 : 0x7b03a000);
5097 /* On the chance that pid != inferior_ptid, set inferior_ptid
5098 to pid, so that (grrrr!) implicit uses of inferior_ptid get
5101 saved_inferior_ptid = inferior_ptid;
5102 inferior_ptid = pid_to_ptid (tid);
5104 /* Try to grab a null-terminated string. */
5107 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
5109 inferior_ptid = saved_inferior_ptid;
5112 for (i = 0; i < 4; i++)
5114 exec_file_buffer[name_index++] = four_chars[i];
5115 done = (four_chars[i] == '\0');
5122 if (exec_file_buffer[0] == '\0')
5124 inferior_ptid = saved_inferior_ptid;
5128 inferior_ptid = saved_inferior_ptid;
5129 return exec_file_buffer;
5134 pre_fork_inferior (void)
5138 status = pipe (startup_semaphore.parent_channel);
5141 warning ("error getting parent pipe for startup semaphore");
5145 status = pipe (startup_semaphore.child_channel);
5148 warning ("error getting child pipe for startup semaphore");
5153 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5154 * ultimately from "follow_inferior_fork" in infrun.c,
5155 * itself called from "resume".
5157 * This seems to be intended to attach after a fork or
5158 * vfork, while "attach" is used to attach to a pid
5159 * given by the user. The check for an existing attach
5160 * seems odd--it always fails in our test system.
5163 hppa_require_attach (int pid)
5168 unsigned int regs_offset;
5169 process_state_t old_process_state = process_state;
5171 /* Are we already attached? There appears to be no explicit
5172 * way to answer this via ttrace, so we try something which
5173 * should be innocuous if we are attached. If that fails,
5174 * then we assume we're not attached, and so attempt to make
5178 tt_status = call_real_ttrace (TT_PROC_STOP,
5181 (TTRACE_ARG_TYPE) TT_NIL,
5182 (TTRACE_ARG_TYPE) TT_NIL,
5187 /* No change to process-state!
5194 /* If successful, the process is now stopped. But if
5195 * we're VFORKING, the parent is still running, so don't
5196 * change the process state.
5198 if (process_state != VFORKING)
5199 process_state = STOPPED;
5201 /* If we were already attached, you'd think that we
5202 * would need to start going again--but you'd be wrong,
5203 * as the fork-following code is actually in the middle
5204 * of the "resume" routine in in "infrun.c" and so
5205 * will (almost) immediately do a resume.
5207 * On the other hand, if we are VFORKING, which means
5208 * that the child and the parent share a process for a
5209 * while, we know that "resume" won't be resuming
5210 * until the child EXEC event is seen. But we still
5211 * don't want to continue, as the event is already
5214 update_thread_state_after_attach (pid, DONT_ATTACH_CONTINUE);
5215 } /* STOP succeeded */
5221 hppa_require_detach (int pid, int signal)
5225 /* If signal is non-zero, we must pass the signal on to the active
5226 thread prior to detaching. We do this by continuing the threads
5232 threads_continue_all_with_signals (pid, signal);
5236 tt_status = call_ttrace (TT_PROC_DETACH,
5242 errno = 0; /* Ignore any errors. */
5244 /* process_state? */
5249 /* Given the starting address of a memory page, hash it to a bucket in
5250 the memory page dictionary.
5253 get_dictionary_bucket_of_page (CORE_ADDR page_start)
5257 hash = (page_start / memory_page_dictionary.page_size);
5258 hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT;
5264 /* Given a memory page's starting address, get (i.e., find an existing
5265 or create a new) dictionary entry for the page. The page will be
5266 write-protected when this function returns, but may have a reference
5267 count of 0 (if the page was newly-added to the dictionary).
5269 static memory_page_t *
5270 get_dictionary_entry_of_page (int pid, CORE_ADDR page_start)
5273 memory_page_t *page = NULL;
5274 memory_page_t *previous_page = NULL;
5276 /* We're going to be using the dictionary now, than-kew. */
5277 require_memory_page_dictionary ();
5279 /* Try to find an existing dictionary entry for this page. Hash
5280 on the page's starting address.
5282 bucket = get_dictionary_bucket_of_page (page_start);
5283 page = &memory_page_dictionary.buckets[bucket];
5284 while (page != NULL)
5286 if (page->page_start == page_start)
5288 previous_page = page;
5292 /* Did we find a dictionary entry for this page? If not, then
5293 add it to the dictionary now.
5297 /* Create a new entry. */
5298 page = (memory_page_t *) xmalloc (sizeof (memory_page_t));
5299 page->page_start = page_start;
5300 page->reference_count = 0;
5302 page->previous = NULL;
5304 /* We'll write-protect the page now, if that's allowed. */
5305 page->original_permissions = write_protect_page (pid, page_start);
5307 /* Add the new entry to the dictionary. */
5308 page->previous = previous_page;
5309 previous_page->next = page;
5311 memory_page_dictionary.page_count++;
5319 remove_dictionary_entry_of_page (int pid, memory_page_t *page)
5321 /* Restore the page's original permissions. */
5322 unwrite_protect_page (pid, page->page_start, page->original_permissions);
5324 /* Kick the page out of the dictionary. */
5325 if (page->previous != NULL)
5326 page->previous->next = page->next;
5327 if (page->next != NULL)
5328 page->next->previous = page->previous;
5330 /* Just in case someone retains a handle to this after it's freed. */
5331 page->page_start = (CORE_ADDR) 0;
5333 memory_page_dictionary.page_count--;
5340 hppa_enable_syscall_events (int pid)
5343 ttevent_t ttrace_events;
5345 /* Get the set of events that are currently enabled. */
5346 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5348 (TTRACE_ARG_TYPE) & ttrace_events,
5349 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5352 perror_with_name ("ttrace");
5354 /* Add syscall events to that set. */
5355 ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY;
5356 ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN;
5358 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5360 (TTRACE_ARG_TYPE) & ttrace_events,
5361 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5364 perror_with_name ("ttrace");
5369 hppa_disable_syscall_events (int pid)
5372 ttevent_t ttrace_events;
5374 /* Get the set of events that are currently enabled. */
5375 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5377 (TTRACE_ARG_TYPE) & ttrace_events,
5378 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5381 perror_with_name ("ttrace");
5383 /* Remove syscall events from that set. */
5384 ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY;
5385 ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN;
5387 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5389 (TTRACE_ARG_TYPE) & ttrace_events,
5390 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5393 perror_with_name ("ttrace");
5397 /* The address range beginning with START and ending with START+LEN-1
5398 (inclusive) is to be watched via page-protection by a new watchpoint.
5399 Set protection for all pages that overlap that range.
5401 Note that our caller sets TYPE to:
5402 0 for a bp_hardware_watchpoint,
5403 1 for a bp_read_watchpoint,
5404 2 for a bp_access_watchpoint
5406 (Yes, this is intentionally (though lord only knows why) different
5407 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5410 hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
5412 CORE_ADDR page_start;
5413 int dictionary_was_empty;
5416 LONGEST range_size_in_pages;
5419 error ("read or access hardware watchpoints not supported on HP-UX");
5421 /* Examine all pages in the address range. */
5422 require_memory_page_dictionary ();
5424 dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5426 page_size = memory_page_dictionary.page_size;
5427 page_start = (start / page_size) * page_size;
5428 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5430 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5432 memory_page_t *page;
5434 /* This gets the page entered into the dictionary if it was
5435 not already entered.
5437 page = get_dictionary_entry_of_page (pid, page_start);
5438 page->reference_count++;
5441 /* Our implementation depends on seeing calls to kernel code, for the
5442 following reason. Here we ask to be notified of syscalls.
5444 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5447 But when kernel code accesses the page, it doesn't give a SIGBUS.
5448 Rather, the system call that touched the page fails, with errno=EFAULT.
5451 We could accomodate this "feature" by asking to be notified of syscall
5452 entries & exits; upon getting an entry event, disabling page-protections;
5453 upon getting an exit event, reenabling page-protections and then checking
5454 if any watchpoints triggered.
5456 However, this turns out to be a real performance loser. syscalls are
5457 usually a frequent occurrence. Having to unprotect-reprotect all watched
5458 pages, and also to then read all watched memory locations and compare for
5459 triggers, can be quite expensive.
5461 Instead, we'll only ask to be notified of syscall exits. When we get
5462 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5463 we can just continue the inferior.
5465 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5466 hackish stuff to determine whether the failure really was due to a
5467 page-protect trap on a watched location.
5469 if (dictionary_was_empty)
5470 hppa_enable_syscall_events (pid);
5476 /* The address range beginning with START and ending with START+LEN-1
5477 (inclusive) was being watched via page-protection by a watchpoint
5478 which has been removed. Remove protection for all pages that
5479 overlap that range, which are not also being watched by other
5483 hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len,
5486 CORE_ADDR page_start;
5487 int dictionary_is_empty;
5490 LONGEST range_size_in_pages;
5493 error ("read or access hardware watchpoints not supported on HP-UX");
5495 /* Examine all pages in the address range. */
5496 require_memory_page_dictionary ();
5498 page_size = memory_page_dictionary.page_size;
5499 page_start = (start / page_size) * page_size;
5500 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5502 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5504 memory_page_t *page;
5506 page = get_dictionary_entry_of_page (pid, page_start);
5507 page->reference_count--;
5509 /* Was this the last reference of this page? If so, then we
5510 must scrub the entry from the dictionary, and also restore
5511 the page's original permissions.
5513 if (page->reference_count == 0)
5514 remove_dictionary_entry_of_page (pid, page);
5517 dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5519 /* If write protections are currently disallowed, then that implies that
5520 wait_for_inferior believes that the inferior is within a system call.
5521 Since we want to see both syscall entry and return, it's clearly not
5522 good to disable syscall events in this state!
5524 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5525 "inferior is between syscall events now". Oh well.
5527 if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed)
5528 hppa_disable_syscall_events (pid);
5534 /* Could we implement a watchpoint of this type via our available
5537 This query does not consider whether a particular address range
5538 could be so watched, but just whether support is generally available
5539 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5540 query that answers whether a particular range should be watched via
5544 hppa_can_use_hw_watchpoint (enum bptype type, int cnt, enum bptype ot)
5546 return (type == bp_hardware_watchpoint);
5550 /* Assuming we could set a hardware watchpoint on this address, do
5551 we think it would be profitable ("a good idea") to do so? If not,
5552 we can always set a regular (aka single-step & test) watchpoint
5556 hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
5558 int range_is_stack_based;
5559 int range_is_accessible;
5560 CORE_ADDR page_start;
5563 LONGEST range_size_in_pages;
5565 /* ??rehrauer: For now, say that all addresses are potentially
5566 profitable. Possibly later we'll want to test the address
5569 range_is_stack_based = 0;
5571 /* If any page in the range is inaccessible, then we cannot
5572 really use hardware watchpointing, even though our client
5573 thinks we can. In that case, it's actually an error to
5574 attempt to use hw watchpoints, so we'll tell our client
5575 that the range is "unprofitable", and hope that they listen...
5577 range_is_accessible = 1; /* Until proven otherwise. */
5579 /* Examine all pages in the address range. */
5581 page_size = sysconf (_SC_PAGE_SIZE);
5583 /* If we can't determine page size, we're hosed. Tell our
5584 client it's unprofitable to use hw watchpoints for this
5587 if (errno || (page_size <= 0))
5593 page_start = (start / page_size) * page_size;
5594 range_size_in_pages = len / (LONGEST) page_size;
5596 for (page = 0; page < range_size_in_pages; page++, page_start += page_size)
5599 int page_permissions;
5601 /* Is this page accessible? */
5603 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
5605 (TTRACE_ARG_TYPE) page_start,
5607 (TTRACE_ARG_TYPE) & page_permissions);
5608 if (errno || (tt_status < 0))
5611 range_is_accessible = 0;
5615 /* Yes, go for another... */
5618 return (!range_is_stack_based && range_is_accessible);
5623 hppa_pid_or_tid_to_str (ptid_t ptid)
5625 static char buf[100]; /* Static because address returned. */
5626 pid_t id = PIDGET (ptid);
5628 /* Does this appear to be a process? If so, print it that way. */
5629 if (is_process_id (id))
5630 return child_pid_to_str (ptid);
5632 /* Else, print both the GDB thread number and the system thread id. */
5633 sprintf (buf, "thread %d (", pid_to_thread_id (ptid));
5634 strcat (buf, hppa_tid_to_str (ptid));
5635 strcat (buf, ")\0");
5641 /* If the current pid is not the pid this module reported
5642 * from "ptrace_wait" with the most recent event, then the
5643 * user has switched threads.
5645 * If the last reported event was a breakpoint, then return
5646 * the old thread id, else return 0.
5649 hppa_switched_threads (pid_t gdb_pid)
5651 if (gdb_pid == old_gdb_pid)
5654 * Core gdb is working with the same pid that it
5655 * was before we reported the last event. This
5656 * is ok: e.g. we reported hitting a thread-specific
5657 * breakpoint, but we were reporting the wrong
5658 * thread, so the core just ignored the event.
5660 * No thread switch has happened.
5664 else if (gdb_pid == reported_pid)
5667 * Core gdb is working with the pid we reported, so
5668 * any continue or step will be able to figure out
5669 * that it needs to step over any hit breakpoints
5670 * without our (i.e. PREPARE_TO_PROCEED's) help.
5674 else if (!reported_bpt)
5677 * The core switched, but we didn't just report a
5678 * breakpoint, so there's no just-hit breakpoint
5679 * instruction at "reported_pid"'s PC, and thus there
5680 * is no need to step over it.
5686 /* There's been a real switch, and we reported
5687 * a hit breakpoint. Let "hppa_prepare_to_proceed"
5688 * know, so it can see whether the breakpoint is
5691 return reported_pid;
5694 /* Keep compiler happy with an obvious return at the end.
5700 hppa_ensure_vforking_parent_remains_stopped (int pid)
5702 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5709 hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
5711 return 0; /* No, the parent vfork is available now. */
5715 /* Write a register as a 64bit value. This may be necessary if the
5716 native OS is too braindamaged to allow some (or all) registers to
5717 be written in 32bit hunks such as hpux11 and the PC queue registers.
5719 This is horribly gross and disgusting. */
5722 ttrace_write_reg_64 (int gdb_tid, CORE_ADDR dest_addr, CORE_ADDR src_addr)
5728 tid = map_from_gdb_tid (gdb_tid);
5729 pid = get_pid_for (tid);
5732 tt_status = ttrace (TT_LWP_WUREGS,
5735 (TTRACE_ARG_TYPE) dest_addr,
5737 (TTRACE_ARG_TYPE) src_addr );
5742 /* Don't bother for a known benign error: if you ask for the
5743 first thread state, but there is only one thread and it's
5744 not stopped, ttrace complains.
5746 We have this inside the #ifdef because our caller will do
5747 this check for real. */
5748 if( request != TT_PROC_GET_FIRST_LWP_STATE
5749 || errno != EPROTO )
5752 printf( "TT fail for %s, with pid %d, tid %d, status %d \n",
5753 get_printable_name_of_ttrace_request (TT_LWP_WUREGS),
5754 pid, tid, tt_status );
5763 _initialize_infttrace (void)
5765 /* Initialize the ttrace-based hardware watchpoint implementation. */
5766 memory_page_dictionary.page_count = (LONGEST) - 1;
5767 memory_page_dictionary.page_protections_allowed = 1;
5770 memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE);
5772 /* We do a lot of casts from pointers to TTRACE_ARG_TYPE; make sure
5774 if (sizeof (TTRACE_ARG_TYPE) < sizeof (void *))
5775 internal_error (__FILE__, __LINE__, "failed internal consistency check");
5777 if (errno || (memory_page_dictionary.page_size <= 0))
5778 perror_with_name ("sysconf");