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
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "gdb_string.h"
30 /* Some hackery to work around a use of the #define name NO_FLAGS
31 * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
34 #define INFTTRACE_TEMP_HACK NO_FLAGS
39 #include <sys/types.h>
42 #include <sys/param.h>
45 #include <sys/ioctl.h>
47 #include <sys/ttrace.h>
54 #ifdef PTRACE_IN_WRONG_PLACE
57 #include <sys/ptrace.h>
59 #endif /* NO_PTRACE_H */
61 /* Second half of the hackery above. Non-ANSI C, so
62 * we can't use "#error", alas.
65 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
66 /* #error "Hackery to remove warning didn't work right" */
68 /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
71 /* #error "Didn't get expected re-definition of NO_FLAGS" */
72 #define NO_FLAGS INFTTRACE_TEMP_HACK
75 #if !defined (PT_SETTRC)
76 #define PT_SETTRC 0 /* Make process traceable by parent */
78 #if !defined (PT_READ_I)
79 #define PT_READ_I 1 /* Read word from text space */
81 #if !defined (PT_READ_D)
82 #define PT_READ_D 2 /* Read word from data space */
84 #if !defined (PT_READ_U)
85 #define PT_READ_U 3 /* Read word from kernel user struct */
87 #if !defined (PT_WRITE_I)
88 #define PT_WRITE_I 4 /* Write word to text space */
90 #if !defined (PT_WRITE_D)
91 #define PT_WRITE_D 5 /* Write word to data space */
93 #if !defined (PT_WRITE_U)
94 #define PT_WRITE_U 6 /* Write word to kernel user struct */
96 #if !defined (PT_CONTINUE)
97 #define PT_CONTINUE 7 /* Continue after signal */
99 #if !defined (PT_STEP)
100 #define PT_STEP 9 /* Set flag for single stepping */
102 #if !defined (PT_KILL)
103 #define PT_KILL 8 /* Send child a SIGKILL signal */
107 #define PT_ATTACH PTRACE_ATTACH
110 #define PT_DETACH PTRACE_DETACH
115 #include <sys/file.h>
118 /* This semaphore is used to coordinate the child and parent processes
119 after a fork(), and before an exec() by the child. See parent_attach_all
124 int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */
125 int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */
130 #define SEM_LISTEN (0)
132 static startup_semaphore_t startup_semaphore;
134 /* See can_touch_threads_of_process for details. */
135 static int vforking_child_pid = 0;
136 static int vfork_in_flight = 0;
138 /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed).
140 static pid_t old_gdb_pid = 0;
141 static pid_t reported_pid = 0;
142 static int reported_bpt = 0;
144 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
146 #define TT_OK( _status, _errno ) \
147 (((_status) == 1) && ((_errno) == 0))
149 #define TTRACE_ARG_TYPE uint64_t
151 /* When supplied as the "addr" operand, ttrace interprets this
152 to mean, "from the current address".
154 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
156 /* When supplied as the "addr", "data" or "addr2" operand for most
157 requests, ttrace interprets this to mean, "pay no heed to this
160 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
162 /* This is capable of holding the value of a 32-bit register. The
163 value is always left-aligned in the buffer; i.e., [0] contains
164 the most-significant byte of the register's value, and [sizeof(reg)]
165 contains the least-significant value.
167 ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
168 that registers are 32-bits on HP-UX. The latter assumption changes
171 typedef int register_value_t;
173 /********************************************************************
179 The rest of GDB sees threads as being things with different
180 "pid" (process id) values. See "thread.c" for details. The
181 separate threads will be seen and reacted to if infttrace passes
182 back different pid values (for _events_). See wait_for_inferior
185 So infttrace is going to use thread ids externally, pretending
186 they are process ids, and keep track internally so that it can
187 use the real process id (and thread id) when calling ttrace.
189 The data structure that supports this is a linked list of the
190 current threads. Since at some date infttrace will have to
191 deal with multiple processes, each list element records its
192 corresponding pid, rather than having a single global.
194 Note that the list is only approximately current; that's ok, as
195 it's up to date when we need it (we hope!). Also, it can contain
196 dead threads, as there's no harm if it does.
198 The approach taken here is to bury the translation from external
199 to internal inside "call_ttrace" and a few other places.
201 There are some wrinkles:
203 o When GDB forks itself to create the debug target process,
204 there's only a pid of 0 around in the child, so the
205 TT_PROC_SETTRC operation uses a more direct call to ttrace;
206 Similiarly, the initial setting of the event mask happens
207 early as well, and so is also special-cased, and an attach
210 o We define an unthreaded application as having a "pseudo"
213 o To keep from confusing the rest of GDB, we don't switch
214 the PID for the pseudo thread to a TID. A table will help:
216 Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ...
218 Our thread list stores: pid pid pid pid ...
221 Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ...
223 Both pid and tid0 will map to tid0, as there are infttrace.c-internal
224 calls to ttrace using tid0.
228 Since we're implementing the "stop the world" model, sub-model
229 "other threads run during step", we have some stuff to do:
231 o User steps require continuing all threads other than the
232 one the user is stepping;
234 o Internal debugger steps (such as over a breakpoint or watchpoint,
235 but not out of a library load thunk) require stepping only
236 the selected thread; this means that we have to report the
237 step finish on that thread, which can lead to complications;
239 o When a thread is created, it is created running, rather
240 than stopped--so we have to stop it.
242 The OS doesn't guarantee the stopped thread list will be stable,
243 no does it guarantee where on the stopped thread list a thread
244 that is single-stepped will wind up: it's possible that it will
245 be off the list for a while, it's possible the step will complete
246 and it will be re-posted to the end...
248 This means we have to scan the stopped thread list, build up
249 a work-list, and then run down the work list; we can't do the
250 step/continue during the scan.
254 Then there's the issue of waiting for an event. We do this by
255 noticing how many events are reported at the end of each wait.
256 From then on, we "fake" all resumes and steps, returning instantly,
257 and don't do another wait. Once all pending events are reported,
258 we can really resume again.
260 To keep this hidden, all the routines which know about tids and
261 pids or real events and simulated ones are static (file-local).
263 This code can make lots of calls to ttrace, in particular it
264 can spin down the list of thread states more than once. If this
265 becomes a performance hit, the spin could be done once and the
266 various "tsp" blocks saved, keeping all later spins in this
269 The O/S doesn't promise to keep the list straight, and so we must
270 re-scan a lot. By observation, it looks like a single-step/wait
271 puts the stepped thread at the end of the list but doesn't change
274 ****************************************************************
277 /* Uncomment these to turn on various debugging output */
278 /* #define THREAD_DEBUG */
279 /* #define WAIT_BUFFER_DEBUG */
280 /* #define PARANOIA */
283 #define INFTTRACE_ALL_THREADS (-1)
284 #define INFTTRACE_STEP (1)
285 #define INFTTRACE_CONTINUE (0)
287 /* FIX: this is used in inftarg.c/child_wait, in a hack.
289 extern int not_same_real_pid;
291 /* This is used to count buffered events.
293 static unsigned int more_events_left = 0;
297 typedef enum process_state_enum
301 FAKE_CONTINUE, /* For later use */
308 static process_state_t process_state = STOPPED;
310 /* User-specified stepping modality.
312 typedef enum stepping_mode_enum
314 DO_DEFAULT, /* ...which is a continue! */
320 /* Action to take on an attach, depends on
321 * what kind (user command, fork, vfork).
323 * At the moment, this is either:
325 * o continue with a SIGTRAP signal, or
329 typedef enum attach_continue_enum
336 /* This flag is true if we are doing a step-over-bpt
337 * with buffered events. We will have to be sure to
338 * report the right thread, as otherwise the spaghetti
339 * code in "infrun.c/wait_for_inferior" will get
342 static int doing_fake_step = 0;
343 static lwpid_t fake_step_tid = 0;
346 /****************************************************
347 * Thread information structure routines and types. *
348 ****************************************************
351 struct thread_info_struct
353 int am_pseudo; /* This is a pseudo-thread for the process. */
354 int pid; /* Process ID */
355 lwpid_t tid; /* Thread ID */
356 int handled; /* 1 if a buffered event was handled. */
357 int seen; /* 1 if this thread was seen on a traverse. */
358 int terminated; /* 1 if thread has terminated. */
359 int have_signal; /* 1 if signal to be sent */
360 enum target_signal signal_value; /* Signal to send */
361 int have_start; /* 1 if alternate starting address */
362 stepping_mode_t stepping_mode; /* Whether to step or continue */
363 CORE_ADDR start; /* Where to start */
364 int have_state; /* 1 if the event state has been set */
365 ttstate_t last_stop_state; /* The most recently-waited event for this thread. */
366 struct thread_info_struct
367 *next; /* All threads are linked via this field. */
368 struct thread_info_struct
369 *next_pseudo; /* All pseudo-threads are linked via this field. */
374 struct thread_info_header_struct
378 thread_info *head_pseudo;
383 static thread_info_header thread_head =
385 static thread_info_header deleted_threads =
388 static saved_real_pid = 0;
391 /*************************************************
392 * Debugging support functions *
393 *************************************************
399 unsigned long pc_val;
403 offset = register_addr (PC_REGNUM, U_REGS_OFFSET);
404 res = read_from_register_save_state (
406 (TTRACE_ARG_TYPE) offset,
411 return (CORE_ADDR) pc_val;
415 return (CORE_ADDR) 0;
420 get_printable_name_of_stepping_mode (mode)
421 stepping_mode_t mode;
430 return "DO_CONTINUE";
432 return "?unknown mode?";
436 /* This function returns a pointer to a string describing the
437 * ttrace event being reported.
440 get_printable_name_of_ttrace_event (event)
443 /* This enumeration is "gappy", so don't use a table. */
450 return "TTEVT_SIGNAL";
458 return "TTEVT_VFORK";
459 case TTEVT_SYSCALL_RETURN:
460 return "TTEVT_SYSCALL_RETURN";
461 case TTEVT_LWP_CREATE:
462 return "TTEVT_LWP_CREATE";
463 case TTEVT_LWP_TERMINATE:
464 return "TTEVT_LWP_TERMINATE";
466 return "TTEVT_LWP_EXIT";
467 case TTEVT_LWP_ABORT_SYSCALL:
468 return "TTEVT_LWP_ABORT_SYSCALL";
469 case TTEVT_SYSCALL_ENTRY:
470 return "TTEVT_SYSCALL_ENTRY";
471 case TTEVT_SYSCALL_RESTART:
472 return "TTEVT_SYSCALL_RESTART";
474 return "?new event?";
479 /* This function translates the ttrace request enumeration into
480 * a character string that is its printable (aka "human readable")
484 get_printable_name_of_ttrace_request (request)
487 if (!IS_TTRACE_REQ (request))
490 /* This enumeration is "gappy", so don't use a table. */
494 return "TT_PROC_SETTRC";
496 return "TT_PROC_ATTACH";
498 return "TT_PROC_DETACH";
500 return "TT_PROC_RDTEXT";
502 return "TT_PROC_WRTEXT";
504 return "TT_PROC_RDDATA";
506 return "TT_PROC_WRDATA";
508 return "TT_PROC_STOP";
509 case TT_PROC_CONTINUE:
510 return "TT_PROC_CONTINUE";
511 case TT_PROC_GET_PATHNAME:
512 return "TT_PROC_GET_PATHNAME";
513 case TT_PROC_GET_EVENT_MASK:
514 return "TT_PROC_GET_EVENT_MASK";
515 case TT_PROC_SET_EVENT_MASK:
516 return "TT_PROC_SET_EVENT_MASK";
517 case TT_PROC_GET_FIRST_LWP_STATE:
518 return "TT_PROC_GET_FIRST_LWP_STATE";
519 case TT_PROC_GET_NEXT_LWP_STATE:
520 return "TT_PROC_GET_NEXT_LWP_STATE";
522 return "TT_PROC_EXIT";
523 case TT_PROC_GET_MPROTECT:
524 return "TT_PROC_GET_MPROTECT";
525 case TT_PROC_SET_MPROTECT:
526 return "TT_PROC_SET_MPROTECT";
527 case TT_PROC_SET_SCBM:
528 return "TT_PROC_SET_SCBM";
530 return "TT_LWP_STOP";
531 case TT_LWP_CONTINUE:
532 return "TT_LWP_CONTINUE";
534 return "TT_LWP_SINGLE";
536 return "TT_LWP_RUREGS";
538 return "TT_LWP_WUREGS";
539 case TT_LWP_GET_EVENT_MASK:
540 return "TT_LWP_GET_EVENT_MASK";
541 case TT_LWP_SET_EVENT_MASK:
542 return "TT_LWP_SET_EVENT_MASK";
543 case TT_LWP_GET_STATE:
544 return "TT_LWP_GET_STATE";
551 /* This function translates the process state enumeration into
552 * a character string that is its printable (aka "human readable")
556 get_printable_name_of_process_state (process_state)
557 process_state_t process_state;
559 switch (process_state)
564 return "FAKE_STEPPING";
572 return "?some unknown state?";
576 /* Set a ttrace thread state to a safe, initial state.
579 clear_ttstate_t (tts)
584 tts->tts_user_tid = 0;
585 tts->tts_event = TTEVT_NONE;
588 /* Copy ttrace thread state TTS_FROM into TTS_TO.
591 copy_ttstate_t (tts_to, tts_from)
595 memcpy ((char *) tts_to, (char *) tts_from, sizeof (*tts_to));
598 /* Are there any live threads we know about?
601 any_thread_records ()
603 return (thread_head.count > 0);
606 /* Create, fill in and link in a thread descriptor.
609 create_thread_info (pid, tid)
615 int thread_count_of_pid;
617 new_p = malloc (sizeof (thread_info));
620 new_p->have_signal = 0;
621 new_p->have_start = 0;
622 new_p->have_state = 0;
623 clear_ttstate_t (&new_p->last_stop_state);
624 new_p->am_pseudo = 0;
627 new_p->terminated = 0;
629 new_p->next_pseudo = NULL;
630 new_p->stepping_mode = DO_DEFAULT;
632 if (0 == thread_head.count)
636 printf ("First thread, pid %d tid %d!\n", pid, tid);
638 saved_real_pid = inferior_pid;
644 printf ("Subsequent thread, pid %d tid %d\n", pid, tid);
648 /* Another day, another thread...
652 /* The new thread always goes at the head of the list.
654 new_p->next = thread_head.head;
655 thread_head.head = new_p;
657 /* Is this the "pseudo" thread of a process? It is if there's
658 * no other thread for this process on the list. (Note that this
659 * accomodates multiple processes, such as we see even for simple
660 * cases like forking "non-threaded" programs.)
662 p = thread_head.head;
663 thread_count_of_pid = 0;
666 if (p->pid == new_p->pid)
667 thread_count_of_pid++;
671 /* Did we see any other threads for this pid? (Recall that we just
672 * added this thread to the list...)
674 if (thread_count_of_pid == 1)
676 new_p->am_pseudo = 1;
677 new_p->next_pseudo = thread_head.head_pseudo;
678 thread_head.head_pseudo = new_p;
684 /* Get rid of our thread info.
694 printf ("Clearing all thread info\n");
697 p = thread_head.head;
705 thread_head.head = NULL;
706 thread_head.head_pseudo = NULL;
707 thread_head.count = 0;
709 p = deleted_threads.head;
717 deleted_threads.head = NULL;
718 deleted_threads.head_pseudo = NULL;
719 deleted_threads.count = 0;
721 /* No threads, so can't have pending events.
723 more_events_left = 0;
726 /* Given a tid, find the thread block for it.
729 find_thread_info (tid)
734 for (p = thread_head.head; p; p = p->next)
742 for (p = deleted_threads.head; p; p = p->next)
753 /* For any but the pseudo thread, this maps to the
754 * thread ID. For the pseudo thread, if you pass either
755 * the thread id or the PID, you get the pseudo thread ID.
757 * We have to be prepared for core gdb to ask about
758 * deleted threads. We do the map, but we don't like it.
761 map_from_gdb_tid (gdb_tid)
766 /* First assume gdb_tid really is a tid, and try to find a
767 * matching entry on the threads list.
769 for (p = thread_head.head; p; p = p->next)
771 if (p->tid == gdb_tid)
775 /* It doesn't appear to be a tid; perhaps it's really a pid?
776 * Try to find a "pseudo" thread entry on the threads list.
778 for (p = thread_head.head_pseudo; p != NULL; p = p->next_pseudo)
780 if (p->pid == gdb_tid)
784 /* Perhaps it's the tid of a deleted thread we may still
785 * have some knowledge of?
787 for (p = deleted_threads.head; p; p = p->next)
789 if (p->tid == gdb_tid)
793 /* Or perhaps it's the pid of a deleted process we may still
796 for (p = deleted_threads.head_pseudo; p != NULL; p = p->next_pseudo)
798 if (p->pid == gdb_tid)
802 return 0; /* Error? */
805 /* Map the other way: from a real tid to the
806 * "pid" known by core gdb. This tid may be
807 * for a thread that just got deleted, so we
808 * also need to consider deleted threads.
811 map_to_gdb_tid (real_tid)
816 for (p = thread_head.head; p; p = p->next)
818 if (p->tid == real_tid)
827 for (p = deleted_threads.head; p; p = p->next)
829 if (p->tid == real_tid)
831 return p->pid; /* Error? */
836 return 0; /* Error? Never heard of this thread! */
839 /* Do any threads have saved signals?
842 saved_signals_exist ()
846 for (p = thread_head.head; p; p = p->next)
857 /* Is this the tid for the zero-th thread?
860 is_pseudo_thread (tid)
863 thread_info *p = find_thread_info (tid);
864 if (NULL == p || p->terminated)
870 /* Is this thread terminated?
876 thread_info *p = find_thread_info (tid);
879 return p->terminated;
884 /* Is this pid a real PID or a TID?
895 /* What does PID really represent?
897 tid = map_from_gdb_tid (pid);
899 return 0; /* Actually, is probably an error... */
901 tinfo = find_thread_info (tid);
903 /* Does it appear to be a true thread?
905 if (!tinfo->am_pseudo)
908 /* Else, it looks like it may be a process. See if there's any other
909 * threads with the same process ID, though. If there are, then TID
910 * just happens to be the first thread of several for this process.
912 this_pid = tinfo->pid;
914 for (tinfo = thread_head.head; tinfo; tinfo = tinfo->next)
916 if (tinfo->pid == this_pid)
920 return (this_pid_count == 1);
924 /* Add a thread to our info. Prevent duplicate entries.
927 add_tthread (pid, tid)
933 p = find_thread_info (tid);
935 p = create_thread_info (pid, tid);
940 /* Notice that a thread was deleted.
949 if (thread_head.count <= 0)
951 error ("Internal error in thread database.");
956 for (p = thread_head.head; p; p = p->next)
963 printf ("Delete here: %d \n", tid);
969 * Deleting a main thread is ok if we're doing
970 * a parent-follow on a child; this is odd but
971 * not wrong. It apparently _doesn't_ happen
972 * on the child-follow, as we don't just delete
973 * the pseudo while keeping the rest of the
974 * threads around--instead, we clear out the whole
975 * thread list at once.
978 thread_info *q_chase;
981 for (q = thread_head.head_pseudo; q; q = q->next)
985 /* Remove from pseudo list.
988 thread_head.head_pseudo = p->next_pseudo;
990 q_chase->next = p->next_pseudo;
997 /* Remove from live list.
1002 thread_head.head = p->next;
1004 chase->next = p->next;
1006 /* Add to deleted thread list.
1008 p->next = deleted_threads.head;
1009 deleted_threads.head = p;
1010 deleted_threads.count++;
1013 p->next_pseudo = deleted_threads.head_pseudo;
1014 deleted_threads.head_pseudo = p;
1026 /* Get the pid for this tid. (Has to be a real TID!).
1034 for (p = thread_head.head; p; p = p->next)
1042 for (p = deleted_threads.head; p; p = p->next)
1053 /* Note that this thread's current event has been handled.
1056 set_handled (pid, tid)
1062 p = find_thread_info (tid);
1064 p = add_tthread (pid, tid);
1069 /* Was this thread's current event handled?
1077 p = find_thread_info (tid);
1081 return 0; /* New threads have not been handled */
1084 /* Set this thread to unhandled.
1092 #ifdef WAIT_BUFFER_DEBUG
1094 printf ("clear_handled %d\n", (int) tid);
1097 p = find_thread_info (tid);
1099 error ("Internal error: No thread state to clear?");
1104 /* Set all threads to unhandled.
1107 clear_all_handled ()
1111 #ifdef WAIT_BUFFER_DEBUG
1113 printf ("clear_all_handled\n");
1116 for (p = thread_head.head; p; p = p->next)
1121 for (p = deleted_threads.head; p; p = p->next)
1127 /* Set this thread to default stepping mode.
1130 clear_stepping_mode (tid)
1135 #ifdef WAIT_BUFFER_DEBUG
1137 printf ("clear_stepping_mode %d\n", (int) tid);
1140 p = find_thread_info (tid);
1142 error ("Internal error: No thread state to clear?");
1144 p->stepping_mode = DO_DEFAULT;
1147 /* Set all threads to do default continue on resume.
1150 clear_all_stepping_mode ()
1154 #ifdef WAIT_BUFFER_DEBUG
1156 printf ("clear_all_stepping_mode\n");
1159 for (p = thread_head.head; p; p = p->next)
1161 p->stepping_mode = DO_DEFAULT;
1164 for (p = deleted_threads.head; p; p = p->next)
1166 p->stepping_mode = DO_DEFAULT;
1170 /* Set all threads to unseen on this pass.
1177 for (p = thread_head.head; p; p = p->next)
1183 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1184 /* debugging routine.
1190 printf (" Thread pid %d, tid %d", p->pid, p->tid);
1192 printf (", event is %s",
1193 get_printable_name_of_ttrace_event (p->last_stop_state.tts_event));
1196 printf (", pseudo thread");
1199 printf (", have signal 0x%x", p->signal_value);
1202 printf (", have start at 0x%x", p->start);
1204 printf (", step is %s", get_printable_name_of_stepping_mode (p->stepping_mode));
1207 printf (", handled");
1209 printf (", not handled");
1214 printf (", not seen");
1224 if (thread_head.count == 0)
1225 printf ("Thread list is empty\n");
1228 printf ("Thread list has ");
1229 if (thread_head.count == 1)
1230 printf ("1 entry:\n");
1232 printf ("%d entries:\n", thread_head.count);
1233 for (p = thread_head.head; p; p = p->next)
1239 if (deleted_threads.count == 0)
1240 printf ("Deleted thread list is empty\n");
1243 printf ("Deleted thread list has ");
1244 if (deleted_threads.count == 1)
1245 printf ("1 entry:\n");
1247 printf ("%d entries:\n", deleted_threads.count);
1249 for (p = deleted_threads.head; p; p = p->next)
1257 /* Update the thread list based on the "seen" bits.
1260 update_thread_list ()
1266 for (p = thread_head.head; p; p = p->next)
1268 /* Is this an "unseen" thread which really happens to be a process?
1269 If so, is it inferior_pid and is a vfork in flight? If yes to
1270 all, then DON'T REMOVE IT! We're in the midst of moving a vfork
1271 operation, which is a multiple step thing, to the point where we
1272 can touch the parent again. We've most likely stopped to examine
1273 the child at a late stage in the vfork, and if we're not following
1274 the child, we'd best not treat the parent as a dead "thread"...
1276 if ((!p->seen) && p->am_pseudo && vfork_in_flight
1277 && (p->pid != vforking_child_pid))
1287 printf ("Delete unseen thread: %d \n", p->tid);
1289 del_tthread (p->tid);
1296 /************************************************
1297 * O/S call wrappers *
1298 ************************************************
1301 /* This function simply calls ttrace with the given arguments.
1302 * It exists so that all calls to ttrace are isolated. All
1303 * parameters should be as specified by "man 2 ttrace".
1305 * No other "raw" calls to ttrace should exist in this module.
1308 call_real_ttrace (request, pid, tid, addr, data, addr2)
1312 TTRACE_ARG_TYPE addr, data, addr2;
1317 tt_status = ttrace (request, pid, tid, addr, data, addr2);
1322 /* Don't bother for a known benign error: if you ask for the
1323 * first thread state, but there is only one thread and it's
1324 * not stopped, ttrace complains.
1326 * We have this inside the #ifdef because our caller will do
1327 * this check for real.
1329 if (request != TT_PROC_GET_FIRST_LWP_STATE
1333 printf ("TT fail for %s, with pid %d, tid %d, status %d \n",
1334 get_printable_name_of_ttrace_request (request),
1335 pid, tid, tt_status);
1341 /* ??rehrauer: It would probably be most robust to catch and report
1342 * failed requests here. However, some clients of this interface
1343 * seem to expect to catch & deal with them, so we'd best not.
1347 strcpy (reason_for_failure, "ttrace (");
1348 strcat (reason_for_failure, get_printable_name_of_ttrace_request (request));
1349 strcat (reason_for_failure, ")");
1350 printf ("ttrace error, errno = %d\n", errno);
1351 perror_with_name (reason_for_failure);
1359 /* This function simply calls ttrace_wait with the given arguments.
1360 * It exists so that all calls to ttrace_wait are isolated.
1362 * No "raw" calls to ttrace_wait should exist elsewhere.
1365 call_real_ttrace_wait (pid, tid, option, tsp, tsp_size)
1373 thread_info *tinfo = NULL;
1376 ttw_status = ttrace_wait (pid, tid, option, tsp, tsp_size);
1382 printf ("TW fail with pid %d, tid %d \n", pid, tid);
1385 perror_with_name ("ttrace wait");
1392 /* A process may have one or more kernel threads, of which all or
1393 none may be stopped. This function returns the ID of the first
1394 kernel thread in a stopped state, or 0 if none are stopped.
1396 This function can be used with get_process_next_stopped_thread_id
1397 to iterate over the IDs of all stopped threads of this process.
1400 get_process_first_stopped_thread_id (pid, thread_state)
1402 ttstate_t *thread_state;
1406 tt_status = call_real_ttrace (TT_PROC_GET_FIRST_LWP_STATE,
1409 (TTRACE_ARG_TYPE) thread_state,
1410 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1415 if (errno == EPROTO)
1417 /* This is an error we can handle: there isn't any stopped
1418 * thread. This happens when we're re-starting the application
1419 * and it has only one thread. GET_NEXT handles the case of
1420 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1428 perror_with_name ("ttrace");
1436 return thread_state->tts_lwpid;
1440 /* This function returns the ID of the "next" kernel thread in a
1441 stopped state, or 0 if there are none. "Next" refers to the
1442 thread following that of the last successful call to this
1443 function or to get_process_first_stopped_thread_id, using
1444 the value of thread_state returned by that call.
1446 This function can be used with get_process_first_stopped_thread_id
1447 to iterate over the IDs of all stopped threads of this process.
1450 get_process_next_stopped_thread_id (pid, thread_state)
1452 ttstate_t *thread_state;
1456 tt_status = call_real_ttrace (
1457 TT_PROC_GET_NEXT_LWP_STATE,
1460 (TTRACE_ARG_TYPE) thread_state,
1461 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1464 perror_with_name ("ttrace");
1471 else if (tt_status == 0)
1473 /* End of list, no next state. Don't return the
1474 * tts_lwpid, as it's a meaningless "240".
1476 * This is an HPUX "feature".
1481 return thread_state->tts_lwpid;
1484 /* ??rehrauer: Eventually this function perhaps should be calling
1485 pid_to_thread_id. However, that function currently does nothing
1486 for HP-UX. Even then, I'm not clear whether that function
1487 will return a "kernel" thread ID, or a "user" thread ID. If
1488 the former, we can just call it here. If the latter, we must
1489 map from the "user" tid to a "kernel" tid.
1491 NOTE: currently not called.
1494 get_active_tid_of_pid (pid)
1497 ttstate_t thread_state;
1499 return get_process_first_stopped_thread_id (pid, &thread_state);
1502 /* This function returns 1 if tt_request is a ttrace request that
1503 * operates upon all threads of a (i.e., the entire) process.
1506 is_process_ttrace_request (tt_request)
1509 return IS_TTRACE_PROCREQ (tt_request);
1513 /* This function translates a thread ttrace request into
1514 * the equivalent process request for a one-thread process.
1517 make_process_version (request)
1520 if (!IS_TTRACE_REQ (request))
1522 error ("Internal error, bad ttrace request made\n");
1529 return TT_PROC_STOP;
1531 case TT_LWP_CONTINUE:
1532 return TT_PROC_CONTINUE;
1534 case TT_LWP_GET_EVENT_MASK:
1535 return TT_PROC_GET_EVENT_MASK;
1537 case TT_LWP_SET_EVENT_MASK:
1538 return TT_PROC_SET_EVENT_MASK;
1543 case TT_LWP_GET_STATE:
1544 return -1; /* No equivalent */
1552 /* This function translates the "pid" used by the rest of
1553 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1554 * with the given arguments.
1556 * In general, other parts of this module should call this
1557 * function when they are dealing with external users, who only
1558 * have tids to pass (but they call it "pid" for historical
1562 call_ttrace (request, gdb_tid, addr, data, addr2)
1565 TTRACE_ARG_TYPE addr, data, addr2;
1569 ttreq_t new_request;
1571 char reason_for_failure[100]; /* Arbitrary size, should be big enough. */
1574 int is_interesting = 0;
1576 if (TT_LWP_RUREGS == request)
1578 is_interesting = 1; /* Adjust code here as desired */
1581 if (is_interesting && 0 && debug_on)
1583 if (!is_process_ttrace_request (request))
1585 printf ("TT: Thread request, tid is %d", gdb_tid);
1586 printf ("== SINGLE at %x", addr);
1590 printf ("TT: Process request, tid is %d\n", gdb_tid);
1591 printf ("==! SINGLE at %x", addr);
1596 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1597 * which happen before any threads get set up) should go
1598 * directly to "call_real_ttrace", so they don't happen here.
1600 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1604 if (request == TT_PROC_SETTRC && debug_on)
1605 printf ("Unexpected call for TT_PROC_SETTRC\n");
1608 /* Sometimes we get called with a bogus tid (e.g., if a
1609 * thread has terminated, we return 0; inftarg later asks
1610 * whether the thread has exited/forked/vforked).
1614 errno = ESRCH; /* ttrace's response would probably be "No such process". */
1618 /* All other cases should be able to expect that there are
1621 if (!any_thread_records ())
1625 warning ("No thread records for ttrace call");
1627 errno = ESRCH; /* ttrace's response would be "No such process". */
1631 /* OK, now the task is to translate the incoming tid into
1634 real_tid = map_from_gdb_tid (gdb_tid);
1635 real_pid = get_pid_for (real_tid);
1637 /* Now check the result. "Real_pid" is NULL if our list
1638 * didn't find it. We have some tricks we can play to fix
1643 ttstate_t thread_state;
1647 printf ("No saved pid for tid %d\n", gdb_tid);
1650 if (is_process_ttrace_request (request))
1653 /* Ok, we couldn't get a tid. Try to translate to
1654 * the equivalent process operation. We expect this
1655 * NOT to happen, so this is a desparation-type
1656 * move. It can happen if there is an internal
1657 * error and so no "wait()" call is ever done.
1659 new_request = make_process_version (request);
1660 if (new_request == -1)
1665 printf ("...and couldn't make process version of thread operation\n");
1668 /* Use hacky saved pid, which won't always be correct
1669 * in the multi-process future. Use tid as thread,
1670 * probably dooming this to failure. FIX!
1672 if (saved_real_pid != 0)
1676 printf ("...using saved pid %d\n", saved_real_pid);
1679 real_pid = saved_real_pid;
1684 error ("Unable to perform thread operation");
1689 /* Sucessfully translated this to a process request,
1690 * which needs no thread value.
1694 request = new_request;
1699 printf ("Translated thread request to process request\n");
1700 if (saved_real_pid == 0)
1701 printf ("...but there's no saved pid\n");
1705 if (gdb_tid != saved_real_pid)
1706 printf ("...but have the wrong pid (%d rather than %d)\n",
1707 gdb_tid, saved_real_pid);
1711 } /* Translated to a process request */
1712 } /* Is a process request */
1716 /* We have to have a thread. Ooops.
1718 error ("Thread request with no threads (%s)",
1719 get_printable_name_of_ttrace_request (request));
1723 /* Ttrace doesn't like to see tid values on process requests,
1724 * even if we have the right one.
1726 if (is_process_ttrace_request (request))
1732 if (is_interesting && 0 && debug_on)
1734 printf (" now tid %d, pid %d\n", real_tid, real_pid);
1735 printf (" request is %s\n", get_printable_name_of_ttrace_request (request));
1739 /* Finally, the (almost) real call.
1741 tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2);
1744 if (is_interesting && debug_on)
1746 if (!TT_OK (tt_status, errno)
1747 && !(tt_status == 0 & errno == 0))
1748 printf (" got error (errno==%d, status==%d)\n", errno, tt_status);
1756 /* Stop all the threads of a process.
1758 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1759 * to get a TTEVT_NONE event, discarding the old event. Be
1760 * very careful, and only call TT_PROC_STOP when you mean it!
1763 stop_all_threads_of_process (real_pid)
1768 ttw_status = call_real_ttrace (TT_PROC_STOP,
1771 (TTRACE_ARG_TYPE) TT_NIL,
1772 (TTRACE_ARG_TYPE) TT_NIL,
1775 perror_with_name ("ttrace stop of other threads");
1779 /* Under some circumstances, it's unsafe to attempt to stop, or even
1780 query the state of, a process' threads.
1782 In ttrace-based HP-UX, an example is a vforking child process. The
1783 vforking parent and child are somewhat fragile, w/r/t what we can do
1784 what we can do to them with ttrace, until after the child exits or
1785 execs, or until the parent's vfork event is delivered. Until that
1786 time, we must not try to stop the process' threads, or inquire how
1787 many there are, or even alter its data segments, or it typically dies
1788 with a SIGILL. Sigh.
1790 This function returns 1 if this stopped process, and the event that
1791 we're told was responsible for its current stopped state, cannot safely
1792 have its threads examined.
1794 #define CHILD_VFORKED(evt,pid) \
1795 (((evt) == TTEVT_VFORK) && ((pid) != inferior_pid))
1796 #define CHILD_URPED(evt,pid) \
1797 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1798 #define PARENT_VFORKED(evt,pid) \
1799 (((evt) == TTEVT_VFORK) && ((pid) == inferior_pid))
1802 can_touch_threads_of_process (pid, stopping_event)
1804 ttevents_t stopping_event;
1806 if (CHILD_VFORKED (stopping_event, pid))
1808 vforking_child_pid = pid;
1809 vfork_in_flight = 1;
1812 else if (vfork_in_flight &&
1813 (PARENT_VFORKED (stopping_event, pid) ||
1814 CHILD_URPED (stopping_event, pid)))
1816 vfork_in_flight = 0;
1817 vforking_child_pid = 0;
1820 return !vfork_in_flight;
1824 /* If we can find an as-yet-unhandled thread state of a
1825 * stopped thread of this process return 1 and set "tsp".
1826 * Return 0 if we can't.
1828 * If this function is used when the threads of PIS haven't
1829 * been stopped, undefined behaviour is guaranteed!
1832 select_stopped_thread_of_process (pid, tsp)
1836 lwpid_t candidate_tid, tid;
1837 ttstate_t candidate_tstate, tstate;
1839 /* If we're not allowed to touch the process now, then just
1840 * return the current value of *TSP.
1842 * This supports "vfork". It's ok, really, to double the
1843 * current event (the child EXEC, we hope!).
1845 if (!can_touch_threads_of_process (pid, tsp->tts_event))
1848 /* Decide which of (possibly more than one) events to
1849 * return as the first one. We scan them all so that
1850 * we always return the result of a fake-step first.
1853 for (tid = get_process_first_stopped_thread_id (pid, &tstate);
1855 tid = get_process_next_stopped_thread_id (pid, &tstate))
1857 /* TTEVT_NONE events are uninteresting to our clients. They're
1858 * an artifact of our "stop the world" model--the thread is
1859 * stopped because we stopped it.
1861 if (tstate.tts_event == TTEVT_NONE)
1863 set_handled (pid, tstate.tts_lwpid);
1866 /* Did we just single-step a single thread, without letting any
1867 * of the others run? Is this an event for that thread?
1869 * If so, we believe our client would prefer to see this event
1870 * over any others. (Typically the client wants to just push
1871 * one thread a little farther forward, and then go around
1872 * checking for what all threads are doing.)
1874 else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid))
1876 #ifdef WAIT_BUFFER_DEBUG
1877 /* It's possible here to see either a SIGTRAP (due to
1878 * successful completion of a step) or a SYSCALL_ENTRY
1879 * (due to a step completion with active hardware
1883 printf ("Ending fake step with tid %d, state %s\n",
1885 get_printable_name_of_ttrace_event (tstate.tts_event));
1888 /* Remember this one, and throw away any previous
1891 candidate_tid = tstate.tts_lwpid;
1892 candidate_tstate = tstate;
1895 #ifdef FORGET_DELETED_BPTS
1897 /* We can't just do this, as if we do, and then wind
1898 * up the loop with no unhandled events, we need to
1899 * handle that case--the appropriate reaction is to
1900 * just continue, but there's no easy way to do that.
1902 * Better to put this in the ttrace_wait call--if, when
1903 * we fake a wait, we update our events based on the
1904 * breakpoint_here_pc call and find there are no more events,
1905 * then we better continue and so on.
1907 * Or we could put it in the next/continue fake.
1908 * But it has to go in the buffering code, not in the
1909 * real go/wait code.
1911 else if ((TTEVT_SIGNAL == tstate.tts_event)
1912 && (5 == tstate.tts_u.tts_signal.tts_signo)
1913 && (0 != get_raw_pc (tstate.tts_lwpid))
1914 && !breakpoint_here_p (get_raw_pc (tstate.tts_lwpid)))
1917 * If the user deleted a breakpoint while this
1918 * breakpoint-hit event was buffered, we can forget
1921 #ifdef WAIT_BUFFER_DEBUG
1923 printf ("Forgetting deleted bp hit for thread %d\n",
1927 set_handled (pid, tstate.tts_lwpid);
1931 /* Else, is this the first "unhandled" event? If so,
1932 * we believe our client wants to see it (if we don't
1933 * see a fake-step later on in the scan).
1935 else if (!was_handled (tstate.tts_lwpid) && candidate_tid == 0)
1937 candidate_tid = tstate.tts_lwpid;
1938 candidate_tstate = tstate;
1941 /* This is either an event that has already been "handled",
1942 * and thus we believe is uninteresting to our client, or we
1943 * already have a candidate event. Ignore it...
1947 /* What do we report?
1949 if (doing_fake_step)
1951 if (candidate_tid == fake_step_tid)
1955 tstate = candidate_tstate;
1959 warning ("Internal error: fake-step failed to complete.");
1963 else if (candidate_tid != 0)
1965 /* Found a candidate unhandled event.
1967 tstate = candidate_tstate;
1971 warning ("Internal error in call of ttrace_wait.");
1976 warning ("Internal error: no unhandled thread event to select");
1980 copy_ttstate_t (tsp, &tstate);
1982 } /* End of select_stopped_thread_of_process */
1985 /* Check our internal thread data against the real thing.
1988 check_thread_consistency (real_pid)
1991 int tid; /* really lwpid_t */
1995 /* Spin down the O/S list of threads, checking that they
1996 * match what we've got.
1998 for (tid = get_process_first_stopped_thread_id (real_pid, &tstate);
2000 tid = get_process_next_stopped_thread_id (real_pid, &tstate))
2003 p = find_thread_info (tid);
2007 warning ("No internal thread data for thread %d.", tid);
2013 warning ("Inconsistent internal thread data for thread %d.", tid);
2018 warning ("Thread %d is not terminated, internal error.", tid);
2023 #define TT_COMPARE( fld ) \
2024 tstate.fld != p->last_stop_state.fld
2028 if (TT_COMPARE (tts_pid)
2029 || TT_COMPARE (tts_lwpid)
2030 || TT_COMPARE (tts_user_tid)
2031 || TT_COMPARE (tts_event)
2032 || TT_COMPARE (tts_flags)
2033 || TT_COMPARE (tts_scno)
2034 || TT_COMPARE (tts_scnargs))
2036 warning ("Internal thread data for thread %d is wrong.", tid);
2042 #endif /* PARANOIA */
2045 /* This function wraps calls to "call_real_ttrace_wait" so
2046 * that a actual wait is only done when all pending events
2047 * have been reported.
2049 * Note that typically it is called with a pid of "0", i.e.
2050 * the "don't care" value.
2052 * Return value is the status of the pseudo wait.
2055 call_ttrace_wait (pid, option, tsp, tsp_size)
2061 /* This holds the actual, for-real, true process ID.
2063 static int real_pid;
2065 /* As an argument to ttrace_wait, zero pid
2066 * means "Any process", and zero tid means
2067 * "Any thread of the specified process".
2070 lwpid_t wait_tid = 0;
2073 int ttw_status = 0; /* To be returned */
2075 thread_info *tinfo = NULL;
2083 printf ("TW: Pid to wait on is %d\n", pid);
2086 if (!any_thread_records ())
2087 error ("No thread records for ttrace call w. specific pid");
2089 /* OK, now the task is to translate the incoming tid into
2092 real_tid = map_from_gdb_tid (pid);
2093 real_pid = get_pid_for (real_tid);
2096 printf ("==TW: real pid %d, real tid %d\n", real_pid, real_tid);
2101 /* Sanity checks and set-up.
2104 * Stopped Running Fake-step (v)Fork
2105 * \________________________________________
2107 * No buffered events | error wait wait wait
2109 * Buffered events | debuffer error wait debuffer (?)
2112 if (more_events_left == 0)
2115 if (process_state == RUNNING)
2117 /* OK--normal call of ttrace_wait with no buffered events.
2121 else if (process_state == FAKE_STEPPING)
2123 /* Ok--call of ttrace_wait to support
2124 * fake stepping with no buffered events.
2126 * But we better be fake-stepping!
2128 if (!doing_fake_step)
2130 warning ("Inconsistent thread state.");
2133 else if ((process_state == FORKING)
2134 || (process_state == VFORKING))
2136 /* Ok--there are two processes, so waiting
2137 * for the second while the first is stopped
2138 * is ok. Handled bits stay as they were.
2142 else if (process_state == STOPPED)
2144 warning ("Process not running at wait call.");
2149 warning ("Inconsistent process state.");
2156 if (process_state == STOPPED)
2158 /* OK--buffered events being unbuffered.
2162 else if (process_state == RUNNING)
2164 /* An error--shouldn't have buffered events
2167 warning ("Trying to continue with buffered events:");
2169 else if (process_state == FAKE_STEPPING)
2172 * Better be fake-stepping!
2174 if (!doing_fake_step)
2176 warning ("Losing buffered thread events!\n");
2179 else if ((process_state == FORKING)
2180 || (process_state == VFORKING))
2182 /* Ok--there are two processes, so waiting
2183 * for the second while the first is stopped
2184 * is ok. Handled bits stay as they were.
2189 warning ("Process in unknown state with buffered events.");
2192 /* Sometimes we have to wait for a particular thread
2193 * (if we're stepping over a bpt). In that case, we
2194 * _know_ it's going to complete the single-step we
2195 * asked for (because we're only doing the step under
2196 * certain very well-understood circumstances), so it
2199 if (doing_fake_step)
2201 wait_tid = fake_step_tid;
2202 wait_pid = get_pid_for (fake_step_tid);
2204 #ifdef WAIT_BUFFER_DEBUG
2206 printf ("Doing a wait after a fake-step for %d, pid %d\n",
2207 wait_tid, wait_pid);
2211 if (more_events_left == 0 /* No buffered events, need real ones. */
2212 || process_state != STOPPED)
2214 /* If there are no buffered events, and so we need
2215 * real ones, or if we are FORKING, VFORKING,
2216 * FAKE_STEPPING or RUNNING, and thus have to do
2217 * a real wait, then do a real wait.
2220 #ifdef WAIT_BUFFER_DEBUG
2221 /* Normal case... */
2223 printf ("TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid);
2226 /* The actual wait call.
2228 ttw_status = call_real_ttrace_wait (wait_pid, wait_tid, option, tsp, tsp_size);
2230 /* Note that the routines we'll call will be using "call_real_ttrace",
2231 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2232 * the rest of the world uses (which is actually the tid).
2234 real_pid = tsp->tts_pid;
2236 /* For most events: Stop the world!
2238 * It's sometimes not safe to stop all threads of a process.
2239 * Sometimes it's not even safe to ask for the thread state
2242 if (can_touch_threads_of_process (real_pid, tsp->tts_event))
2244 /* If we're really only stepping a single thread, then don't
2245 * try to stop all the others -- we only do this single-stepping
2246 * business when all others were already stopped...and the stop
2247 * would mess up other threads' events.
2249 * Similiarly, if there are other threads with events,
2250 * don't do the stop.
2252 if (!doing_fake_step)
2254 if (more_events_left > 0)
2255 warning ("Internal error in stopping process");
2257 stop_all_threads_of_process (real_pid);
2259 /* At this point, we could scan and update_thread_list(),
2260 * and only use the local list for the rest of the
2261 * module! We'd get rid of the scans in the various
2262 * continue routines (adding one in attach). It'd
2263 * be great--UPGRADE ME!
2271 if (more_events_left > 0)
2272 printf ("== Can't stop process; more events!\n");
2274 printf ("== Can't stop process!\n");
2278 process_state = STOPPED;
2280 #ifdef WAIT_BUFFER_DEBUG
2282 printf ("Process set to STOPPED\n");
2288 /* Fake a call to ttrace_wait. The process must be
2289 * STOPPED, as we aren't going to do any wait.
2291 #ifdef WAIT_BUFFER_DEBUG
2293 printf ("TW: fake it\n");
2296 if (process_state != STOPPED)
2298 warning ("Process not stopped at wait call, in state '%s'.\n",
2299 get_printable_name_of_process_state (process_state));
2302 if (doing_fake_step)
2303 error ("Internal error in stepping over breakpoint");
2305 ttw_status = 0; /* Faking it is always successful! */
2306 } /* End of fake or not? if */
2308 /* Pick an event to pass to our caller. Be paranoid.
2310 if (!select_stopped_thread_of_process (real_pid, tsp))
2311 warning ("Can't find event, using previous event.");
2313 else if (tsp->tts_event == TTEVT_NONE)
2314 warning ("Internal error: no thread has a real event.");
2316 else if (doing_fake_step)
2318 if (fake_step_tid != tsp->tts_lwpid)
2319 warning ("Internal error in stepping over breakpoint.");
2321 /* This wait clears the (current) fake-step if there was one.
2323 doing_fake_step = 0;
2327 /* We now have a correct tsp and ttw_status for the thread
2328 * which we want to report. So it's "handled"! This call
2329 * will add it to our list if it's not there already.
2331 set_handled (real_pid, tsp->tts_lwpid);
2333 /* Save a copy of the ttrace state of this thread, in our local
2336 This caches the state. The implementation of queries like
2337 target_has_execd can then use this cached state, rather than
2338 be forced to make an explicit ttrace call to get it.
2340 (Guard against the condition that this is the first time we've
2341 waited on, i.e., seen this thread, and so haven't yet entered
2342 it into our list of threads.)
2344 tinfo = find_thread_info (tsp->tts_lwpid);
2347 copy_ttstate_t (&tinfo->last_stop_state, tsp);
2348 tinfo->have_state = 1;
2352 } /* call_ttrace_wait */
2354 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2356 child_reported_exec_events_per_exec_call ()
2358 return 1; /* ttrace reports the event once per call. */
2364 /* Our implementation of hardware watchpoints involves making memory
2365 pages write-protected. We must remember a page's original permissions,
2366 and we must also know when it is appropriate to restore a page's
2367 permissions to its original state.
2369 We use a "dictionary" of hardware-watched pages to do this. Each
2370 hardware-watched page is recorded in the dictionary. Each page's
2371 dictionary entry contains the original permissions and a reference
2372 count. Pages are hashed into the dictionary by their start address.
2374 When hardware watchpoint is set on page X for the first time, page X
2375 is added to the dictionary with a reference count of 1. If other
2376 hardware watchpoints are subsequently set on page X, its reference
2377 count is incremented. When hardware watchpoints are removed from
2378 page X, its reference count is decremented. If a page's reference
2379 count drops to 0, it's permissions are restored and the page's entry
2380 is thrown out of the dictionary.
2382 typedef struct memory_page
2384 CORE_ADDR page_start;
2385 int reference_count;
2386 int original_permissions;
2387 struct memory_page *next;
2388 struct memory_page *previous;
2392 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2398 int page_protections_allowed;
2399 /* These are just the heads of chains of actual page descriptors. */
2400 memory_page_t buckets[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT];
2402 memory_page_dictionary;
2406 require_memory_page_dictionary ()
2410 /* Is the memory page dictionary ready for use? If so, we're done. */
2411 if (memory_page_dictionary.page_count >= (LONGEST) 0)
2414 /* Else, initialize it. */
2415 memory_page_dictionary.page_count = (LONGEST) 0;
2417 for (i = 0; i < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++)
2419 memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0;
2420 memory_page_dictionary.buckets[i].reference_count = 0;
2421 memory_page_dictionary.buckets[i].next = NULL;
2422 memory_page_dictionary.buckets[i].previous = NULL;
2428 retire_memory_page_dictionary ()
2430 memory_page_dictionary.page_count = (LONGEST) - 1;
2434 /* Write-protect the memory page that starts at this address.
2436 Returns the original permissions of the page.
2439 write_protect_page (pid, page_start)
2441 CORE_ADDR page_start;
2444 int original_permissions;
2445 int new_permissions;
2447 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
2449 (TTRACE_ARG_TYPE) page_start,
2451 (TTRACE_ARG_TYPE) & original_permissions);
2452 if (errno || (tt_status < 0))
2454 return 0; /* What else can we do? */
2457 /* We'll also write-protect the page now, if that's allowed. */
2458 if (memory_page_dictionary.page_protections_allowed)
2460 new_permissions = original_permissions & ~PROT_WRITE;
2461 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2463 (TTRACE_ARG_TYPE) page_start,
2464 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2465 (TTRACE_ARG_TYPE) new_permissions);
2466 if (errno || (tt_status < 0))
2468 return 0; /* What else can we do? */
2472 return original_permissions;
2476 /* Unwrite-protect the memory page that starts at this address, restoring
2477 (what we must assume are) its original permissions.
2480 unwrite_protect_page (pid, page_start, original_permissions)
2482 CORE_ADDR page_start;
2483 int original_permissions;
2487 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2489 (TTRACE_ARG_TYPE) page_start,
2490 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2491 (TTRACE_ARG_TYPE) original_permissions);
2492 if (errno || (tt_status < 0))
2494 return; /* What else can we do? */
2499 /* Memory page-protections are used to implement "hardware" watchpoints
2502 For every memory page that is currently being watched (i.e., that
2503 presently should be write-protected), write-protect it.
2506 hppa_enable_page_protection_events (pid)
2511 memory_page_dictionary.page_protections_allowed = 1;
2513 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2515 memory_page_t *page;
2517 page = memory_page_dictionary.buckets[bucket].next;
2518 while (page != NULL)
2520 page->original_permissions = write_protect_page (pid, page->page_start);
2527 /* Memory page-protections are used to implement "hardware" watchpoints
2530 For every memory page that is currently being watched (i.e., that
2531 presently is or should be write-protected), un-write-protect it.
2534 hppa_disable_page_protection_events (pid)
2539 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2541 memory_page_t *page;
2543 page = memory_page_dictionary.buckets[bucket].next;
2544 while (page != NULL)
2546 unwrite_protect_page (pid, page->page_start, page->original_permissions);
2551 memory_page_dictionary.page_protections_allowed = 0;
2554 /* Count the number of outstanding events. At this
2555 * point, we have selected one thread and its event
2556 * as the one to be "reported" upwards to core gdb.
2557 * That thread is already marked as "handled".
2559 * Note: we could just scan our own thread list. FIXME!
2562 count_unhandled_events (real_pid, real_tid)
2570 /* Ok, find out how many threads have real events to report.
2573 ttid = get_process_first_stopped_thread_id (real_pid, &tstate);
2579 printf ("Process %d has no threads\n", real_pid);
2581 printf ("Process %d has these threads:\n", real_pid);
2587 if (tstate.tts_event != TTEVT_NONE
2588 && !was_handled (ttid))
2590 /* TTEVT_NONE implies we just stopped it ourselves
2591 * because we're the stop-the-world guys, so it's
2592 * not an event from our point of view.
2594 * If "was_handled" is true, this is an event we
2595 * already handled, so don't count it.
2597 * Note that we don't count the thread with the
2598 * currently-reported event, as it's already marked
2604 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2607 if (ttid == real_tid)
2608 printf ("*"); /* Thread we're reporting */
2612 if (tstate.tts_event != TTEVT_NONE)
2613 printf ("+"); /* Thread with a real event */
2617 if (was_handled (ttid))
2618 printf ("h"); /* Thread has been handled */
2622 printf (" %d, with event %s", ttid,
2623 get_printable_name_of_ttrace_event (tstate.tts_event));
2625 if (tstate.tts_event == TTEVT_SIGNAL
2626 && 5 == tstate.tts_u.tts_signal.tts_signo)
2630 pc_val = get_raw_pc (ttid);
2633 printf (" breakpoint at 0x%x\n", pc_val);
2635 printf (" bpt, can't fetch pc.\n");
2642 ttid = get_process_next_stopped_thread_id (real_pid, &tstate);
2645 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2647 if (events_left > 0)
2648 printf ("There are thus %d pending events\n", events_left);
2654 /* This function is provided as a sop to clients that are calling
2655 * ptrace_wait to wait for a process to stop. (see the
2656 * implementation of child_wait.) Return value is the pid for
2657 * the event that ended the wait.
2659 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2662 ptrace_wait (pid, status)
2673 /* The ptrace implementation of this also ignores pid.
2677 ttwait_return = call_ttrace_wait (0, TTRACE_WAITOK, &tsp, sizeof (tsp));
2678 if (ttwait_return < 0)
2680 /* ??rehrauer: It appears that if our inferior exits and we
2681 haven't asked for exit events, that we're not getting any
2682 indication save a negative return from ttrace_wait and an
2687 *status = 0; /* WIFEXITED */
2688 return inferior_pid;
2691 warning ("Call of ttrace_wait returned with errno %d.",
2693 *status = ttwait_return;
2694 return inferior_pid;
2697 real_pid = tsp.tts_pid;
2698 real_tid = tsp.tts_lwpid;
2700 /* One complication is that the "tts_event" structure has
2701 * a set of flags, and more than one can be set. So we
2702 * either have to force an order (as we do here), or handle
2703 * more than one flag at a time.
2705 if (tsp.tts_event & TTEVT_LWP_CREATE)
2708 /* Unlike what you might expect, this event is reported in
2709 * the _creating_ thread, and the _created_ thread (whose tid
2710 * we have) is still running. So we have to stop it. This
2711 * has already been done in "call_ttrace_wait", but should we
2712 * ever abandon the "stop-the-world" model, here's the command
2715 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2717 * Note that this would depend on being called _after_ "add_tthread"
2718 * below for the tid-to-pid translation to be done in "call_ttrace".
2723 printf ("New thread: pid %d, tid %d, creator tid %d\n",
2724 real_pid, tsp.tts_u.tts_thread.tts_target_lwpid,
2728 /* Now we have to return the tid of the created thread, not
2729 * the creating thread, or "wait_for_inferior" won't know we
2730 * have a new "process" (thread). Plus we should record it
2733 real_tid = tsp.tts_u.tts_thread.tts_target_lwpid;
2735 add_tthread (real_pid, real_tid);
2738 else if ((tsp.tts_event & TTEVT_LWP_TERMINATE)
2739 || (tsp.tts_event & TTEVT_LWP_EXIT))
2744 printf ("Thread dies: %d\n", real_tid);
2747 del_tthread (real_tid);
2750 else if (tsp.tts_event & TTEVT_EXEC)
2755 printf ("Pid %d has zero'th thread %d; inferior pid is %d\n",
2756 real_pid, real_tid, inferior_pid);
2759 add_tthread (real_pid, real_tid);
2765 printf ("Process-level event %s, using tid %d\n",
2766 get_printable_name_of_ttrace_event (tsp.tts_event),
2769 /* OK to do this, as "add_tthread" won't add
2770 * duplicate entries. Also OK not to do it,
2771 * as this event isn't one which can change the
2774 add_tthread (real_pid, real_tid);
2779 /* How many events are left to report later?
2780 * In a non-stop-the-world model, this isn't needed.
2782 * Note that it's not always safe to query the thread state of a process,
2783 * which is what count_unhandled_events does. (If unsafe, we're left with
2784 * no other resort than to assume that no more events remain...)
2786 if (can_touch_threads_of_process (real_pid, tsp.tts_event))
2787 more_events_left = count_unhandled_events (real_pid, real_tid);
2791 if (more_events_left > 0)
2792 warning ("Vfork or fork causing loss of %d buffered events.",
2795 more_events_left = 0;
2798 /* Attempt to translate the ttrace_wait-returned status into the
2801 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2802 clients that expect to pick apart a ptrace wait status, to use
2803 something a little more abstract.
2805 if ((tsp.tts_event & TTEVT_EXEC)
2806 || (tsp.tts_event & TTEVT_FORK)
2807 || (tsp.tts_event & TTEVT_VFORK))
2809 /* Forks come in pairs (parent and child), so core gdb
2810 * will do two waits. Be ready to notice this.
2812 if (tsp.tts_event & TTEVT_FORK)
2814 process_state = FORKING;
2816 #ifdef WAIT_BUFFER_DEBUG
2818 printf ("Process set to FORKING\n");
2821 else if (tsp.tts_event & TTEVT_VFORK)
2823 process_state = VFORKING;
2825 #ifdef WAIT_BUFFER_DEBUG
2827 printf ("Process set to VFORKING\n");
2831 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2832 but I don't think non HP-UX-specific clients really carefully
2833 inspect the first events they get after inferior startup, so
2834 it probably almost doesn't matter what we claim this is.
2839 printf ("..a process 'event'\n");
2842 /* Also make fork and exec events look like bpts, so they can be caught.
2844 *status = 0177 | (_SIGTRAP << 8);
2847 /* Special-cases: We ask for syscall entry and exit events to implement
2848 "fast" (aka "hardware") watchpoints.
2850 When we get a syscall entry, we want to disable page-protections,
2851 and resume the inferior; this isn't an event we wish for
2852 wait_for_inferior to see. Note that we must resume ONLY the
2853 thread that reported the syscall entry; we don't want to allow
2854 other threads to run with the page protections off, as they might
2855 then be able to write to watch memory without it being caught.
2857 When we get a syscall exit, we want to reenable page-protections,
2858 but we don't want to resume the inferior; this is an event we wish
2859 wait_for_inferior to see. Make it look like the signal we normally
2860 get for a single-step completion. This should cause wait_for_inferior
2861 to evaluate whether any watchpoint triggered.
2863 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2864 due to some HP-UX "features". Some syscalls have problems with
2865 write-protections on some pages, and some syscalls seem to have
2866 pending writes to those pages at the time we're getting the return
2867 event. So, we'll single-step the inferior to get out of the syscall,
2868 and then reenable protections.
2870 Note that we're intentionally allowing the syscall exit case to
2871 fall through into the succeeding cases, as sometimes we single-
2872 step out of one syscall only to immediately enter another...
2874 else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY)
2875 || (tsp.tts_event & TTEVT_SYSCALL_RETURN))
2877 /* Make a syscall event look like a breakpoint. Same comments
2878 as for exec & fork events.
2882 printf ("..a syscall 'event'\n");
2885 /* Also make syscall events look like bpts, so they can be caught.
2887 *status = 0177 | (_SIGTRAP << 8);
2890 else if ((tsp.tts_event & TTEVT_LWP_CREATE)
2891 || (tsp.tts_event & TTEVT_LWP_TERMINATE)
2892 || (tsp.tts_event & TTEVT_LWP_EXIT))
2894 /* Make a thread event look like a breakpoint. Same comments
2895 * as for exec & fork events.
2899 printf ("..a thread 'event'\n");
2902 /* Also make thread events look like bpts, so they can be caught.
2904 *status = 0177 | (_SIGTRAP << 8);
2907 else if ((tsp.tts_event & TTEVT_EXIT))
2912 printf ("..an exit\n");
2915 /* Prevent rest of gdb from thinking this is
2916 * a new thread if for some reason it's never
2917 * seen the main thread before.
2919 inferior_pid = map_to_gdb_tid (real_tid); /* HACK, FIX */
2921 *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode);
2924 else if (tsp.tts_event & TTEVT_SIGNAL)
2928 printf ("..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo);
2931 *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8);
2937 /* This means the process or thread terminated. But we should've
2938 caught an explicit exit/termination above. So warn (this is
2939 really an internal error) and claim the process or thread
2940 terminated with a SIGTRAP.
2943 warning ("process_wait: unknown process state");
2947 printf ("Process-level event %s, using tid %d\n",
2948 get_printable_name_of_ttrace_event (tsp.tts_event),
2955 target_post_wait (tsp.tts_pid, *status);
2960 printf ("Done waiting, pid is %d, tid %d\n", real_pid, real_tid);
2963 /* All code external to this module uses the tid, but calls
2964 * it "pid". There's some tweaking so that the outside sees
2965 * the first thread as having the same number as the starting
2968 return_pid = map_to_gdb_tid (real_tid);
2970 /* Remember this for later use in "hppa_prepare_to_proceed".
2972 old_gdb_pid = inferior_pid;
2973 reported_pid = return_pid;
2974 reported_bpt = ((tsp.tts_event & TTEVT_SIGNAL) && (5 == tsp.tts_u.tts_signal.tts_signo));
2976 if (real_tid == 0 || return_pid == 0)
2978 warning ("Internal error: process-wait failed.");
2985 /* This function causes the caller's process to be traced by its
2986 parent. This is intended to be called after GDB forks itself,
2987 and before the child execs the target. Despite the name, it
2988 is called by the child.
2990 Note that HP-UX ttrace is rather funky in how this is done.
2991 If the parent wants to get the initial exec event of a child,
2992 it must set the ttrace event mask of the child to include execs.
2993 (The child cannot do this itself.) This must be done after the
2994 child is forked, but before it execs.
2996 To coordinate the parent and child, we implement a semaphore using
2997 pipes. After SETTRC'ing itself, the child tells the parent that
2998 it is now traceable by the parent, and waits for the parent's
2999 acknowledgement. The parent can then set the child's event mask,
3000 and notify the child that it can now exec.
3002 (The acknowledgement by parent happens as a result of a call to
3003 child_acknowledge_created_inferior.)
3006 parent_attach_all ()
3010 /* We need a memory home for a constant, to pass it to ttrace.
3011 The value of the constant is arbitrary, so long as both
3012 parent and child use the same value. Might as well use the
3013 "magic" constant provided by ttrace...
3015 uint64_t tc_magic_child = TT_VERSION;
3016 uint64_t tc_magic_parent = 0;
3018 tt_status = call_real_ttrace (
3023 (TTRACE_ARG_TYPE) TT_VERSION,
3029 /* Notify the parent that we're potentially ready to exec(). */
3030 write (startup_semaphore.child_channel[SEM_TALK],
3032 sizeof (tc_magic_child));
3034 /* Wait for acknowledgement from the parent. */
3035 read (startup_semaphore.parent_channel[SEM_LISTEN],
3037 sizeof (tc_magic_parent));
3039 if (tc_magic_child != tc_magic_parent)
3040 warning ("mismatched semaphore magic");
3042 /* Discard our copy of the semaphore. */
3043 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3044 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3045 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3046 (void) close (startup_semaphore.child_channel[SEM_TALK]);
3051 /* Despite being file-local, this routine is dealing with
3052 * actual process IDs, not thread ids. That's because it's
3053 * called before the first "wait" call, and there's no map
3054 * yet from tids to pids.
3056 * When it is called, a forked child is running, but waiting on
3057 * the semaphore. If you stop the child and re-start it,
3058 * things get confused, so don't do that! An attached child is
3061 * Since this is called after either attach or run, we
3062 * have to be the common part of both.
3065 require_notification_of_events (real_pid)
3069 ttevent_t notifiable_events;
3072 ttstate_t thread_state;
3076 printf ("Require notif, pid is %d\n", real_pid);
3079 /* Temporary HACK: tell inftarg.c/child_wait to not
3080 * loop until pids are the same.
3082 not_same_real_pid = 0;
3084 sigemptyset (¬ifiable_events.tte_signals);
3085 notifiable_events.tte_opts = TTEO_NONE;
3087 /* This ensures that forked children inherit their parent's
3088 * event mask, which we're setting here.
3090 * NOTE: if you debug gdb with itself, then the ultimate
3091 * debuggee gets flags set by the outermost gdb, as
3092 * a child of a child will still inherit.
3094 notifiable_events.tte_opts |= TTEO_PROC_INHERIT;
3096 notifiable_events.tte_events = TTEVT_DEFAULT;
3097 notifiable_events.tte_events |= TTEVT_SIGNAL;
3098 notifiable_events.tte_events |= TTEVT_EXEC;
3099 notifiable_events.tte_events |= TTEVT_EXIT;
3100 notifiable_events.tte_events |= TTEVT_FORK;
3101 notifiable_events.tte_events |= TTEVT_VFORK;
3102 notifiable_events.tte_events |= TTEVT_LWP_CREATE;
3103 notifiable_events.tte_events |= TTEVT_LWP_EXIT;
3104 notifiable_events.tte_events |= TTEVT_LWP_TERMINATE;
3106 tt_status = call_real_ttrace (
3107 TT_PROC_SET_EVENT_MASK,
3110 (TTRACE_ARG_TYPE) & notifiable_events,
3111 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3116 require_notification_of_exec_events (real_pid)
3120 ttevent_t notifiable_events;
3123 ttstate_t thread_state;
3127 printf ("Require notif, pid is %d\n", real_pid);
3130 /* Temporary HACK: tell inftarg.c/child_wait to not
3131 * loop until pids are the same.
3133 not_same_real_pid = 0;
3135 sigemptyset (¬ifiable_events.tte_signals);
3136 notifiable_events.tte_opts = TTEO_NOSTRCCHLD;
3138 /* This ensures that forked children don't inherit their parent's
3139 * event mask, which we're setting here.
3141 notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT;
3143 notifiable_events.tte_events = TTEVT_DEFAULT;
3144 notifiable_events.tte_events |= TTEVT_EXEC;
3145 notifiable_events.tte_events |= TTEVT_EXIT;
3147 tt_status = call_real_ttrace (
3148 TT_PROC_SET_EVENT_MASK,
3151 (TTRACE_ARG_TYPE) & notifiable_events,
3152 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3157 /* This function is called by the parent process, with pid being the
3158 * ID of the child process, after the debugger has forked.
3161 child_acknowledge_created_inferior (pid)
3164 /* We need a memory home for a constant, to pass it to ttrace.
3165 The value of the constant is arbitrary, so long as both
3166 parent and child use the same value. Might as well use the
3167 "magic" constant provided by ttrace...
3169 uint64_t tc_magic_parent = TT_VERSION;
3170 uint64_t tc_magic_child = 0;
3172 /* Wait for the child to tell us that it has forked. */
3173 read (startup_semaphore.child_channel[SEM_LISTEN],
3175 sizeof (tc_magic_child));
3177 /* Clear thread info now. We'd like to do this in
3178 * "require...", but that messes up attach.
3180 clear_thread_info ();
3182 /* Tell the "rest of gdb" that the initial thread exists.
3183 * This isn't really a hack. Other thread-based versions
3184 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3186 * Q: Why don't we also add this thread to the local
3187 * list via "add_tthread"?
3189 * A: Because we don't know the tid, and can't stop the
3190 * the process safely to ask what it is. Anyway, we'll
3191 * add it when it gets the EXEC event.
3193 add_thread (pid); /* in thread.c */
3195 /* We can now set the child's ttrace event mask.
3197 require_notification_of_exec_events (pid);
3199 /* Tell ourselves that the process is running.
3201 process_state = RUNNING;
3203 /* Notify the child that it can exec. */
3204 write (startup_semaphore.parent_channel[SEM_TALK],
3206 sizeof (tc_magic_parent));
3208 /* Discard our copy of the semaphore. */
3209 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3210 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3211 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3212 (void) close (startup_semaphore.child_channel[SEM_TALK]);
3217 * arrange for notification of all events by
3218 * calling require_notification_of_events.
3221 child_post_startup_inferior (real_pid)
3224 require_notification_of_events (real_pid);
3227 /* From here on, we should expect tids rather than pids.
3230 hppa_enable_catch_fork (tid)
3234 ttevent_t ttrace_events;
3236 /* Get the set of events that are currently enabled.
3238 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3240 (TTRACE_ARG_TYPE) & ttrace_events,
3241 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3244 perror_with_name ("ttrace");
3246 /* Add forks to that set. */
3247 ttrace_events.tte_events |= TTEVT_FORK;
3251 printf ("enable fork, tid is %d\n", tid);
3254 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3256 (TTRACE_ARG_TYPE) & ttrace_events,
3257 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3260 perror_with_name ("ttrace");
3265 hppa_disable_catch_fork (tid)
3269 ttevent_t ttrace_events;
3271 /* Get the set of events that are currently enabled.
3273 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3275 (TTRACE_ARG_TYPE) & ttrace_events,
3276 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3280 perror_with_name ("ttrace");
3282 /* Remove forks from that set. */
3283 ttrace_events.tte_events &= ~TTEVT_FORK;
3287 printf ("disable fork, tid is %d\n", tid);
3290 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3292 (TTRACE_ARG_TYPE) & ttrace_events,
3293 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3297 perror_with_name ("ttrace");
3301 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3303 child_insert_fork_catchpoint (tid)
3306 /* Enable reporting of fork events from the kernel. */
3307 /* ??rehrauer: For the moment, we're always enabling these events,
3308 and just ignoring them if there's no catchpoint to catch them.
3315 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3317 child_remove_fork_catchpoint (tid)
3320 /* Disable reporting of fork events from the kernel. */
3321 /* ??rehrauer: For the moment, we're always enabling these events,
3322 and just ignoring them if there's no catchpoint to catch them.
3330 hppa_enable_catch_vfork (tid)
3334 ttevent_t ttrace_events;
3336 /* Get the set of events that are currently enabled.
3338 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3340 (TTRACE_ARG_TYPE) & ttrace_events,
3341 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3345 perror_with_name ("ttrace");
3347 /* Add vforks to that set. */
3348 ttrace_events.tte_events |= TTEVT_VFORK;
3352 printf ("enable vfork, tid is %d\n", tid);
3355 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3357 (TTRACE_ARG_TYPE) & ttrace_events,
3358 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3362 perror_with_name ("ttrace");
3367 hppa_disable_catch_vfork (tid)
3371 ttevent_t ttrace_events;
3373 /* Get the set of events that are currently enabled. */
3374 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3376 (TTRACE_ARG_TYPE) & ttrace_events,
3377 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3381 perror_with_name ("ttrace");
3383 /* Remove vforks from that set. */
3384 ttrace_events.tte_events &= ~TTEVT_VFORK;
3388 printf ("disable vfork, tid is %d\n", tid);
3390 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3392 (TTRACE_ARG_TYPE) & ttrace_events,
3393 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3397 perror_with_name ("ttrace");
3401 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3403 child_insert_vfork_catchpoint (tid)
3406 /* Enable reporting of vfork events from the kernel. */
3407 /* ??rehrauer: For the moment, we're always enabling these events,
3408 and just ignoring them if there's no catchpoint to catch them.
3415 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3417 child_remove_vfork_catchpoint (tid)
3420 /* Disable reporting of vfork events from the kernel. */
3421 /* ??rehrauer: For the moment, we're always enabling these events,
3422 and just ignoring them if there's no catchpoint to catch them.
3428 #if defined(CHILD_HAS_FORKED)
3430 /* Q: Do we need to map the returned process ID to a thread ID?
3432 * A: I don't think so--here we want a _real_ pid. Any later
3433 * operations will call "require_notification_of_events" and
3434 * start the mapping.
3437 child_has_forked (tid, childpid)
3442 ttstate_t ttrace_state;
3445 /* Do we have cached thread state that we can consult? If so, use it. */
3446 tinfo = find_thread_info (map_from_gdb_tid (tid));
3449 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3452 /* Nope, must read the thread's current state */
3455 tt_status = call_ttrace (TT_LWP_GET_STATE,
3457 (TTRACE_ARG_TYPE) & ttrace_state,
3458 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3462 perror_with_name ("ttrace");
3468 if (ttrace_state.tts_event & TTEVT_FORK)
3470 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3479 #if defined(CHILD_HAS_VFORKED)
3481 /* See child_has_forked for pid discussion.
3484 child_has_vforked (tid, childpid)
3489 ttstate_t ttrace_state;
3492 /* Do we have cached thread state that we can consult? If so, use it. */
3493 tinfo = find_thread_info (map_from_gdb_tid (tid));
3495 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3497 /* Nope, must read the thread's current state */
3500 tt_status = call_ttrace (TT_LWP_GET_STATE,
3502 (TTRACE_ARG_TYPE) & ttrace_state,
3503 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3507 perror_with_name ("ttrace");
3513 if (ttrace_state.tts_event & TTEVT_VFORK)
3515 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3524 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3526 child_can_follow_vfork_prior_to_exec ()
3528 /* ttrace does allow this.
3530 ??rehrauer: However, I had major-league problems trying to
3531 convince wait_for_inferior to handle that case. Perhaps when
3532 it is rewritten to grok multiple processes in an explicit way...
3539 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3541 child_insert_exec_catchpoint (tid)
3544 /* Enable reporting of exec events from the kernel. */
3545 /* ??rehrauer: For the moment, we're always enabling these events,
3546 and just ignoring them if there's no catchpoint to catch them.
3553 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3555 child_remove_exec_catchpoint (tid)
3558 /* Disable reporting of execevents from the kernel. */
3559 /* ??rehrauer: For the moment, we're always enabling these events,
3560 and just ignoring them if there's no catchpoint to catch them.
3567 #if defined(CHILD_HAS_EXECD)
3569 child_has_execd (tid, execd_pathname)
3571 char **execd_pathname;
3574 ttstate_t ttrace_state;
3577 /* Do we have cached thread state that we can consult? If so, use it. */
3578 tinfo = find_thread_info (map_from_gdb_tid (tid));
3580 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3582 /* Nope, must read the thread's current state */
3585 tt_status = call_ttrace (TT_LWP_GET_STATE,
3587 (TTRACE_ARG_TYPE) & ttrace_state,
3588 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3592 perror_with_name ("ttrace");
3598 if (ttrace_state.tts_event & TTEVT_EXEC)
3600 /* See child_pid_to_exec_file in this file: this is a macro.
3602 char *exec_file = target_pid_to_exec_file (tid);
3604 *execd_pathname = savestring (exec_file, strlen (exec_file));
3613 #if defined(CHILD_HAS_SYSCALL_EVENT)
3615 child_has_syscall_event (pid, kind, syscall_id)
3617 enum target_waitkind *kind;
3621 ttstate_t ttrace_state;
3624 /* Do we have cached thread state that we can consult? If so, use it. */
3625 tinfo = find_thread_info (map_from_gdb_tid (pid));
3627 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3629 /* Nope, must read the thread's current state */
3632 tt_status = call_ttrace (TT_LWP_GET_STATE,
3634 (TTRACE_ARG_TYPE) & ttrace_state,
3635 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3639 perror_with_name ("ttrace");
3645 *kind = TARGET_WAITKIND_SPURIOUS; /* Until proven otherwise... */
3648 if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY)
3649 *kind = TARGET_WAITKIND_SYSCALL_ENTRY;
3650 else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN)
3651 *kind = TARGET_WAITKIND_SYSCALL_RETURN;
3655 *syscall_id = ttrace_state.tts_scno;
3662 #if defined(CHILD_THREAD_ALIVE)
3664 /* Check to see if the given thread is alive.
3666 * We'll trust the thread list, as the more correct
3667 * approach of stopping the process and spinning down
3668 * the OS's thread list is _very_ expensive.
3670 * May need a FIXME for that reason.
3673 child_thread_alive (gdb_tid)
3678 /* This spins down the lists twice.
3679 * Possible peformance improvement here!
3681 tid = map_from_gdb_tid (gdb_tid);
3682 return !is_terminated (tid);
3689 /* This function attempts to read the specified number of bytes from the
3690 save_state_t that is our view into the hardware registers, starting at
3691 ss_offset, and ending at ss_offset + sizeof_buf - 1
3693 If this function succeeds, it deposits the fetched bytes into buf,
3696 If it fails, it returns a negative result. The contents of buf are
3697 undefined it this function fails.
3700 read_from_register_save_state (tid, ss_offset, buf, sizeof_buf)
3702 TTRACE_ARG_TYPE ss_offset;
3707 register_value_t register_value = 0;
3709 tt_status = call_ttrace (TT_LWP_RUREGS,
3712 (TTRACE_ARG_TYPE) sizeof_buf,
3713 (TTRACE_ARG_TYPE) buf);
3716 /* Map ttrace's version of success to our version.
3717 * Sometime ttrace returns 0, but that's ok here.
3725 /* This function attempts to write the specified number of bytes to the
3726 save_state_t that is our view into the hardware registers, starting at
3727 ss_offset, and ending at ss_offset + sizeof_buf - 1
3729 If this function succeeds, it deposits the bytes in buf, and returns 0.
3731 If it fails, it returns a negative result. The contents of the save_state_t
3732 are undefined it this function fails.
3735 write_to_register_save_state (tid, ss_offset, buf, sizeof_buf)
3737 TTRACE_ARG_TYPE ss_offset;
3742 register_value_t register_value = 0;
3744 tt_status = call_ttrace (TT_LWP_WUREGS,
3747 (TTRACE_ARG_TYPE) sizeof_buf,
3748 (TTRACE_ARG_TYPE) buf);
3753 /* This function is a sop to the largeish number of direct calls
3754 to call_ptrace that exist in other files. Rather than create
3755 functions whose name abstracts away from ptrace, and change all
3756 the present callers of call_ptrace, we'll do the expedient (and
3757 perhaps only practical) thing.
3759 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3760 process. Thus, we must translate all ptrace requests into their
3761 process-specific, ttrace equivalents.
3764 call_ptrace (pt_request, gdb_tid, addr, data)
3767 PTRACE_ARG3_TYPE addr;
3771 TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr;
3772 TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data;
3773 TTRACE_ARG_TYPE tt_addr2 = TT_NIL;
3775 register_value_t register_value;
3778 /* Perform the necessary argument translation. Note that some
3779 cases are funky enough in the ttrace realm that we handle them
3784 /* The following cases cannot conveniently be handled conveniently
3785 by merely adjusting the ptrace arguments and feeding into the
3786 generic call to ttrace at the bottom of this function.
3788 Note that because all branches of this switch end in "return",
3789 there's no need for any "break" statements.
3792 return parent_attach_all ();
3795 tt_status = read_from_register_save_state (gdb_tid,
3798 sizeof (register_value));
3801 return register_value;
3804 register_value = (int) tt_data;
3805 tt_status = write_to_register_save_state (gdb_tid,
3808 sizeof (register_value));
3813 tt_status = call_ttrace (TT_PROC_RDTEXT, /* Implicit 4-byte xfer becomes block-xfer. */
3816 (TTRACE_ARG_TYPE) 4,
3817 (TTRACE_ARG_TYPE) & read_buf);
3823 tt_status = call_ttrace (TT_PROC_RDDATA, /* Implicit 4-byte xfer becomes block-xfer. */
3826 (TTRACE_ARG_TYPE) 4,
3827 (TTRACE_ARG_TYPE) & read_buf);
3833 tt_status = call_real_ttrace (TT_PROC_ATTACH,
3834 map_from_gdb_tid (gdb_tid),
3837 (TTRACE_ARG_TYPE) TT_VERSION,
3843 /* The following cases are handled by merely adjusting the ptrace
3844 arguments and feeding into the generic call to ttrace.
3847 tt_request = TT_PROC_DETACH;
3851 tt_request = TT_PROC_WRTEXT; /* Translates 4-byte xfer to block-xfer. */
3852 tt_data = 4; /* This many bytes. */
3853 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3857 tt_request = TT_PROC_WRDATA; /* Translates 4-byte xfer to block-xfer. */
3858 tt_data = 4; /* This many bytes. */
3859 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3863 tt_request = TT_PROC_RDTEXT;
3867 tt_request = TT_PROC_RDDATA;
3871 tt_request = TT_PROC_WRTEXT;
3875 tt_request = TT_PROC_WRDATA;
3879 tt_request = TT_PROC_CONTINUE;
3883 tt_request = TT_LWP_SINGLE; /* Should not be making this request? */
3887 tt_request = TT_PROC_EXIT;
3890 case PT_GET_PROCESS_PATHNAME:
3891 tt_request = TT_PROC_GET_PATHNAME;
3895 tt_request = pt_request; /* Let ttrace be the one to complain. */
3899 return call_ttrace (tt_request,
3906 /* Kill that pesky process!
3914 thread_info **paranoia;
3917 if (inferior_pid == 0)
3920 /* Walk the list of "threads", some of which are "pseudo threads",
3921 aka "processes". For each that is NOT inferior_pid, stop it,
3924 You see, we may not have just a single process to kill. If we're
3925 restarting or quitting or detaching just after the inferior has
3926 forked, then we've actually two processes to clean up.
3928 But we can't just call target_mourn_inferior() for each, since that
3929 zaps the target vector.
3932 paranoia = (thread_info **) malloc (thread_head.count *
3933 sizeof (thread_info *));
3936 t = thread_head.head;
3940 paranoia[para_count] = t;
3941 for (i = 0; i < para_count; i++)
3943 if (t->next == paranoia[i])
3945 warning ("Bad data in gdb's thread data; repairing.");
3951 if (t->am_pseudo && (t->pid != inferior_pid))
3953 /* TT_PROC_STOP doesn't require a subsequent ttrace_wait, as it
3954 * generates no event.
3956 call_ttrace (TT_PROC_STOP,
3962 call_ttrace (TT_PROC_DETACH,
3965 (TTRACE_ARG_TYPE) TARGET_SIGNAL_0,
3973 call_ttrace (TT_PROC_STOP,
3978 target_mourn_inferior ();
3979 clear_thread_info ();
3983 #ifndef CHILD_RESUME
3985 /* Sanity check a thread about to be continued.
3988 thread_dropping_event_check (p)
3994 * This seems to happen when we "next" over a
3995 * "fork()" while following the parent. If it's
3996 * the FORK event, that's ok. If it's a SIGNAL
3997 * in the unfollowed child, that's ok to--but
3998 * how can we know that's what's going on?
4004 if (p->last_stop_state.tts_event == TTEVT_FORK)
4009 else if (p->last_stop_state.tts_event == TTEVT_SIGNAL)
4011 /* Ok, close eyes and let it happen.
4017 /* This shouldn't happen--we're dropping a
4020 warning ("About to continue process %d, thread %d with unhandled event %s.",
4022 get_printable_name_of_ttrace_event (
4023 p->last_stop_state.tts_event));
4033 /* No saved state, have to assume it failed.
4035 warning ("About to continue process %d, thread %d with unhandled event.",
4044 } /* thread_dropping_event_check */
4046 /* Use a loop over the threads to continue all the threads but
4047 * the one specified, which is to be stepped.
4050 threads_continue_all_but_one (gdb_tid, signal)
4063 printf ("Using loop over threads to step/resume with signals\n");
4066 /* First update the thread list.
4069 real_tid = map_from_gdb_tid (gdb_tid);
4070 real_pid = get_pid_for (real_tid);
4072 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
4073 while (0 != scan_tid)
4077 /* FIX: later should check state is stopped;
4078 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
4081 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
4082 printf ("About to continue non-stopped thread %d\n", scan_tid);
4085 p = find_thread_info (scan_tid);
4088 add_tthread (real_pid, scan_tid);
4089 p = find_thread_info (scan_tid);
4091 /* This is either a newly-created thread or the
4092 * result of a fork; in either case there's no
4093 * actual event to worry about.
4097 if (state.tts_event != TTEVT_NONE)
4099 /* Oops, do need to worry!
4101 warning ("Unexpected thread with \"%s\" event.",
4102 get_printable_name_of_ttrace_event (state.tts_event));
4105 else if (scan_tid != p->tid)
4106 error ("Bad data in thread database.");
4111 printf ("Why are we continuing a dead thread?\n");
4116 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4119 /* Remove unseen threads.
4121 update_thread_list ();
4123 /* Now run down the thread list and continue or step.
4125 for (p = thread_head.head; p; p = p->next)
4130 thread_dropping_event_check (p);
4132 /* Pass the correct signals along.
4136 thread_signal = p->signal_value;
4142 if (p->tid != real_tid)
4145 * Not the thread of interest, so continue it
4146 * as the user expects.
4148 if (p->stepping_mode == DO_STEP)
4150 /* Just step this thread.
4156 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4161 /* Regular continue (default case).
4167 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4173 /* Step the thread of interest.
4179 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4182 } /* Loop over threads */
4183 } /* End threads_continue_all_but_one */
4185 /* Use a loop over the threads to continue all the threads.
4186 * This is done when a signal must be sent to any of the threads.
4189 threads_continue_all_with_signals (gdb_tid, signal)
4202 printf ("Using loop over threads to resume with signals\n");
4205 /* Scan and update thread list.
4208 real_tid = map_from_gdb_tid (gdb_tid);
4209 real_pid = get_pid_for (real_tid);
4211 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
4212 while (0 != scan_tid)
4217 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
4218 warning ("About to continue non-stopped thread %d\n", scan_tid);
4221 p = find_thread_info (scan_tid);
4224 add_tthread (real_pid, scan_tid);
4225 p = find_thread_info (scan_tid);
4227 /* This is either a newly-created thread or the
4228 * result of a fork; in either case there's no
4229 * actual event to worry about.
4233 if (state.tts_event != TTEVT_NONE)
4235 /* Oops, do need to worry!
4237 warning ("Unexpected thread with \"%s\" event.",
4238 get_printable_name_of_ttrace_event (state.tts_event));
4245 printf ("Why are we continuing a dead thread? (1)\n");
4250 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4253 /* Remove unseen threads from our list.
4255 update_thread_list ();
4257 /* Continue the threads.
4259 for (p = thread_head.head; p; p = p->next)
4264 thread_dropping_event_check (p);
4266 /* Pass the correct signals along.
4268 if (p->tid == real_tid)
4270 thread_signal = signal;
4273 else if (p->have_signal)
4275 thread_signal = p->signal_value;
4281 if (p->stepping_mode == DO_STEP)
4287 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4292 /* Continue this thread (default case).
4298 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4302 } /* End threads_continue_all_with_signals */
4304 /* Step one thread only.
4307 thread_fake_step (tid, signal)
4309 enum target_signal signal;
4316 printf ("Doing a fake-step over a bpt, etc. for %d\n", tid);
4318 if (is_terminated (tid))
4319 printf ("Why are we continuing a dead thread? (4)\n");
4323 if (doing_fake_step)
4324 warning ("Step while step already in progress.");
4326 /* See if there's a saved signal value for this
4327 * thread to be passed on, but no current signal.
4329 p = find_thread_info (tid);
4332 if (p->have_signal && signal == TARGET_SIGNAL_0)
4334 /* Pass on a saved signal.
4336 signal = p->signal_value;
4343 warning ("Internal error: continuing unhandled thread.");
4345 call_ttrace (TT_LWP_SINGLE,
4348 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4351 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4352 * for this thread only, and clear any saved signal info.
4354 doing_fake_step = 1;
4355 fake_step_tid = tid;
4357 } /* End thread_fake_step */
4359 /* Continue one thread when a signal must be sent to it.
4362 threads_continue_one_with_signal (gdb_tid, signal)
4372 printf ("Continuing one thread with a signal\n");
4375 real_tid = map_from_gdb_tid (gdb_tid);
4376 real_pid = get_pid_for (real_tid);
4378 p = find_thread_info (real_tid);
4381 add_tthread (real_pid, real_tid);
4387 printf ("Why are we continuing a dead thread? (2)\n");
4391 warning ("Internal error: continuing unhandled thread.");
4395 call_ttrace (TT_LWP_CONTINUE,
4398 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4403 #ifndef CHILD_RESUME
4405 /* Resume execution of the inferior process.
4407 * This routine is in charge of setting the "handled" bits.
4409 * If STEP is zero, continue it.
4410 * If STEP is nonzero, single-step it.
4412 * If SIGNAL is nonzero, give it that signal.
4414 * If TID is -1, apply to all threads.
4415 * If TID is not -1, apply to specified thread.
4419 * TID \________________________________________________
4421 * -1 | Step current Continue all threads
4422 * | thread and (but which gets any
4423 * | continue others signal?--We look at
4426 * N | Step _this_ thread Continue _this_ thread
4427 * | and leave others and leave others
4428 * | stopped; internally stopped; used only for
4429 * | used by gdb, never hardware watchpoints
4430 * | a user command. and attach, never a
4434 child_resume (gdb_tid, step, signal)
4437 enum target_signal signal;
4439 int resume_all_threads;
4441 process_state_t new_process_state;
4443 resume_all_threads =
4444 (gdb_tid == INFTTRACE_ALL_THREADS) ||
4447 if (resume_all_threads)
4449 /* Resume all threads, but first pick a tid value
4450 * so we can get the pid when in call_ttrace doing
4453 if (vfork_in_flight)
4454 tid = vforking_child_pid;
4456 tid = map_from_gdb_tid (inferior_pid);
4459 tid = map_from_gdb_tid (gdb_tid);
4464 if (more_events_left)
4465 printf ("More events; ");
4468 printf ("Sending signal %d; ", signal);
4470 if (resume_all_threads)
4473 printf ("Continue process %d\n", tid);
4475 printf ("Step/continue thread %d\n", tid);
4480 printf ("Continue thread %d\n", tid);
4482 printf ("Step just thread %d\n", tid);
4485 if (vfork_in_flight)
4486 printf ("Vfork in flight\n");
4490 if (process_state == RUNNING)
4491 warning ("Internal error in resume logic; doing resume or step anyway.");
4493 if (!step /* Asked to continue... */
4494 && resume_all_threads /* whole process.. */
4495 && signal != 0 /* with a signal... */
4496 && more_events_left > 0)
4497 { /* but we can't yet--save it! */
4499 /* Continue with signal means we have to set the pending
4500 * signal value for this thread.
4506 printf ("Saving signal %d for thread %d\n", signal, tid);
4509 k = find_thread_info (tid);
4513 k->signal_value = signal;
4518 printf ("Why are we continuing a dead thread? (3)\n");
4526 printf ("No thread info for tid %d\n", tid);
4531 /* Are we faking this "continue" or "step"?
4533 * We used to do steps by continuing all the threads for
4534 * which the events had been handled already. While
4535 * conceptually nicer (hides it all in a lower level), this
4536 * can lead to starvation and a hang (e.g. all but one thread
4537 * are unhandled at a breakpoint just before a "join" operation,
4538 * and one thread is in the join, and the user wants to step that
4541 if (resume_all_threads /* Whole process, therefore user command */
4542 && more_events_left > 0)
4543 { /* But we can't do this yet--fake it! */
4548 /* No need to do any notes on a per-thread
4549 * basis--we're done!
4551 #ifdef WAIT_BUFFER_DEBUG
4553 printf ("Faking a process resume.\n");
4561 #ifdef WAIT_BUFFER_DEBUG
4563 printf ("Faking a process step.\n");
4568 p = find_thread_info (tid);
4571 warning ("No thread information for tid %d, 'next' command ignored.\n", tid);
4580 printf ("Why are we continuing a dead thread? (3.5)\n");
4583 if (p->stepping_mode != DO_DEFAULT)
4585 warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored.");
4591 p->stepping_mode = DO_STEP;
4593 p->stepping_mode = DO_CONTINUE;
4596 } /* Have thread info */
4597 } /* Must fake step or go */
4599 /* Execept for fake-steps, from here on we know we are
4600 * going to wind up with a running process which will
4603 new_process_state = RUNNING;
4605 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4606 * it was. (If GDB wanted it to start some other way, we have already
4607 * written a new PC value to the child.)
4609 * If this system does not support PT_STEP, a higher level function will
4610 * have called single_step() to transmute the step request into a
4611 * continue request (by setting breakpoints on all possible successor
4612 * instructions), so we don't have to worry about that here.
4616 if (resume_all_threads)
4619 * Regular user step: other threads get a "continue".
4621 threads_continue_all_but_one (tid, signal);
4622 clear_all_handled ();
4623 clear_all_stepping_mode ();
4628 /* "Fake step": gdb is stepping one thread over a
4629 * breakpoint, watchpoint, or out of a library load
4630 * event, etc. The rest just stay where they are.
4632 * Also used when there are pending events: we really
4633 * step the current thread, but leave the rest stopped.
4634 * Users can't request this, but "wait_for_inferior"
4637 thread_fake_step (tid, signal);
4639 /* Clear the "handled" state of this thread, because
4640 * we'll soon get a new event for it. Other events
4641 * stay as they were.
4643 clear_handled (tid);
4644 clear_stepping_mode (tid);
4645 new_process_state = FAKE_STEPPING;
4651 /* TT_LWP_CONTINUE can pass signals to threads,
4652 * TT_PROC_CONTINUE can't. So if there are any
4653 * signals to pass, we have to use the (slower)
4654 * loop over the stopped threads.
4656 * Equally, if we have to not continue some threads,
4657 * due to saved events, we have to use the loop.
4659 if ((signal != 0) || saved_signals_exist ())
4661 if (resume_all_threads)
4666 printf ("Doing a continue by loop of all threads\n");
4669 threads_continue_all_with_signals (tid, signal);
4671 clear_all_handled ();
4672 clear_all_stepping_mode ();
4678 printf ("Doing a continue w/signal of just thread %d\n", tid);
4681 threads_continue_one_with_signal (tid, signal);
4683 /* Clear the "handled" state of this thread, because
4684 * we'll soon get a new event for it. Other events
4685 * can stay as they were.
4687 clear_handled (tid);
4688 clear_stepping_mode (tid);
4694 /* No signals to send.
4696 if (resume_all_threads)
4700 printf ("Doing a continue by process of process %d\n", tid);
4703 if (more_events_left > 0)
4705 warning ("Losing buffered events on continue.");
4706 more_events_left = 0;
4709 call_ttrace (TT_PROC_CONTINUE,
4715 clear_all_handled ();
4716 clear_all_stepping_mode ();
4724 printf ("Doing a continue of just thread %d\n", tid);
4725 if (is_terminated (tid))
4726 printf ("Why are we continuing a dead thread? (5)\n");
4730 call_ttrace (TT_LWP_CONTINUE,
4736 /* Clear the "handled" state of this thread, because
4737 * we'll soon get a new event for it. Other events
4738 * can stay as they were.
4740 clear_handled (tid);
4741 clear_stepping_mode (tid);
4746 process_state = new_process_state;
4748 #ifdef WAIT_BUFFER_DEBUG
4750 printf ("Process set to %s\n",
4751 get_printable_name_of_process_state (process_state));
4755 #endif /* CHILD_RESUME */
4758 #ifdef ATTACH_DETACH
4762 * One worry is that we may not be attaching to "inferior_pid"
4763 * and thus may not want to clear out our data. FIXME?
4767 update_thread_state_after_attach (pid, kind_of_go)
4769 attach_continue_t kind_of_go;
4772 ttstate_t thread_state;
4776 /* The process better be stopped.
4778 if (process_state != STOPPED
4779 && process_state != VFORKING)
4780 warning ("Internal error attaching.");
4782 /* Clear out old tthread info and start over. This has the
4783 * side effect of ensuring that the TRAP is reported as being
4784 * in the right thread (re-mapped from tid to pid).
4786 * It's because we need to add the tthread _now_ that we
4787 * need to call "clear_thread_info" _now_, and that's why
4788 * "require_notification_of_events" doesn't clear the thread
4789 * info (it's called later than this routine).
4791 clear_thread_info ();
4794 for (tid = get_process_first_stopped_thread_id (pid, &thread_state);
4796 tid = get_process_next_stopped_thread_id (pid, &thread_state))
4805 printf ("Attaching to process %d, thread %d\n",
4810 /* Tell ourselves and the "rest of gdb" that this thread
4813 * This isn't really a hack. Other thread-based versions
4814 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4816 * We don't need to do mapping here, as we know this
4817 * is the first thread and thus gets the real pid
4818 * (and is "inferior_pid").
4820 * NOTE: it probably isn't the originating thread,
4821 * but that doesn't matter (we hope!).
4823 add_tthread (pid, tid);
4824 p = find_thread_info (tid);
4825 if (NULL == p) /* ?We just added it! */
4826 error ("Internal error adding a thread on attach.");
4828 copy_ttstate_t (&p->last_stop_state, thread_state);
4831 if (DO_ATTACH_CONTINUE == kind_of_go)
4834 * If we are going to CONTINUE afterwards,
4835 * raising a SIGTRAP, don't bother trying to
4836 * handle this event. But check first!
4838 switch (p->last_stop_state.tts_event)
4842 /* Ok to set this handled.
4847 warning ("Internal error; skipping event %s on process %d, thread %d.",
4848 get_printable_name_of_ttrace_event (
4849 p->last_stop_state.tts_event),
4853 set_handled (pid, tid);
4858 /* There will be no "continue" opertion, so the
4859 * process remains stopped. Don't set any events
4860 * handled except the "gimmies".
4862 switch (p->last_stop_state.tts_event)
4866 /* Ok to ignore this.
4868 set_handled (pid, tid);
4873 /* Expected "other" FORK or EXEC event from a
4879 printf ("Internal error: failed to handle event %s on process %d, thread %d.",
4880 get_printable_name_of_ttrace_event (
4881 p->last_stop_state.tts_event),
4886 add_thread (tid); /* in thread.c */
4894 /* One mustn't call ttrace_wait() after attaching via ttrace,
4895 'cause the process is stopped already.
4897 However, the upper layers of gdb's execution control will
4898 want to wait after attaching (but not after forks, in
4899 which case they will be doing a "target_resume", anticipating
4900 a later TTEVT_EXEC or TTEVT_FORK event).
4902 To make this attach() implementation more compatible with
4903 others, we'll make the attached-to process raise a SIGTRAP.
4905 Issue: this continues only one thread. That could be
4906 dangerous if the thread is blocked--the process won't run
4907 and no trap will be raised. FIX! (check state.tts_flags?
4908 need one that's either TTS_WASRUNNING--but we've stopped
4909 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4911 if (DO_ATTACH_CONTINUE == kind_of_go)
4913 tt_status = call_real_ttrace (
4918 (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP),
4921 perror_with_name ("ttrace");
4923 clear_handled (a_thread); /* So TRAP will be reported. */
4927 process_state = RUNNING;
4932 #endif /* ATTACH_DETACH */
4935 #ifdef ATTACH_DETACH
4936 /* Start debugging the process whose number is PID.
4945 tt_status = call_real_ttrace (
4950 (TTRACE_ARG_TYPE) TT_VERSION,
4953 perror_with_name ("ttrace attach");
4955 /* If successful, the process is now stopped.
4957 process_state = STOPPED;
4959 /* Our caller ("attach_command" in "infcmd.c")
4960 * expects to do a "wait_for_inferior" after
4961 * the attach, so make sure the inferior is
4962 * running when we're done.
4964 update_thread_state_after_attach (pid, DO_ATTACH_CONTINUE);
4970 #if defined(CHILD_POST_ATTACH)
4972 child_post_attach (pid)
4977 printf ("child-post-attach call\n");
4980 require_notification_of_events (pid);
4985 /* Stop debugging the process whose number is PID
4986 and continue it with signal number SIGNAL.
4987 SIGNAL = 0 means just continue it.
4994 call_ttrace (TT_PROC_DETACH,
4997 (TTRACE_ARG_TYPE) signal,
5001 clear_thread_info ();
5003 /* Process-state? */
5005 #endif /* ATTACH_DETACH */
5008 /* Default the type of the ttrace transfer to int. */
5009 #ifndef TTRACE_XFER_TYPE
5010 #define TTRACE_XFER_TYPE int
5014 _initialize_kernel_u_addr ()
5018 #if !defined (CHILD_XFER_MEMORY)
5019 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
5020 in the NEW_SUN_TTRACE case.
5021 It ought to be straightforward. But it appears that writing did
5022 not write the data that I specified. I cannot understand where
5023 it got the data that it actually did write. */
5025 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
5026 to debugger memory starting at MYADDR. Copy to inferior if
5029 Returns the length copied, which is either the LEN argument or zero.
5030 This xfer function does not do partial moves, since child_ops
5031 doesn't allow memory operations to cross below us in the target stack
5035 child_xfer_memory (memaddr, myaddr, len, write, target)
5040 struct target_ops *target; /* ignored */
5043 /* Round starting address down to longword boundary. */
5044 register CORE_ADDR addr = memaddr & -sizeof (TTRACE_XFER_TYPE);
5045 /* Round ending address up; get number of longwords that makes. */
5047 = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1)
5048 / sizeof (TTRACE_XFER_TYPE);
5049 /* Allocate buffer of that many longwords. */
5050 register TTRACE_XFER_TYPE *buffer
5051 = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));
5055 /* Fill start and end extra bytes of buffer with existing memory data. */
5057 if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE))
5059 /* Need part of initial word -- fetch it. */
5060 buffer[0] = call_ttrace (TT_LWP_RDTEXT,
5062 (TTRACE_ARG_TYPE) addr,
5067 if (count > 1) /* FIXME, avoid if even boundary */
5069 buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT,
5072 (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))),
5077 /* Copy data to be written over corresponding part of buffer */
5079 memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
5083 /* Write the entire buffer. */
5085 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
5088 call_ttrace (TT_LWP_WRDATA,
5090 (TTRACE_ARG_TYPE) addr,
5091 (TTRACE_ARG_TYPE) buffer[i],
5095 /* Using the appropriate one (I or D) is necessary for
5096 Gould NP1, at least. */
5098 call_ttrace (TT_LWP_WRTEXT,
5100 (TTRACE_ARG_TYPE) addr,
5101 (TTRACE_ARG_TYPE) buffer[i],
5110 /* Read all the longwords */
5111 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
5114 buffer[i] = call_ttrace (TT_LWP_RDTEXT,
5116 (TTRACE_ARG_TYPE) addr,
5124 /* Copy appropriate bytes out of the buffer. */
5126 (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
5136 int udot_off; /* Offset into user struct */
5137 int udot_val; /* Value from user struct at udot_off */
5138 char mess[128]; /* For messages */
5140 if (!target_has_execution)
5142 error ("The program is not being run.");
5145 #if !defined (KERNEL_U_SIZE)
5147 /* Adding support for this command is easy. Typically you just add a
5148 routine, called "kernel_u_size" that returns the size of the user
5149 struct, to the appropriate *-nat.c file and then add to the native
5150 config file "#define KERNEL_U_SIZE kernel_u_size()" */
5151 error ("Don't know how large ``struct user'' is in this version of gdb.");
5155 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
5157 if ((udot_off % 24) == 0)
5161 printf_filtered ("\n");
5163 printf_filtered ("%04x:", udot_off);
5165 udot_val = call_ttrace (TT_LWP_RUREGS,
5167 (TTRACE_ARG_TYPE) udot_off,
5172 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
5173 perror_with_name (mess);
5175 /* Avoid using nonportable (?) "*" in print specs */
5176 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
5178 printf_filtered ("\n");
5182 #endif /* !defined (CHILD_XFER_MEMORY). */
5184 /* TTrace version of "target_pid_to_exec_file"
5187 child_pid_to_exec_file (tid)
5190 static char exec_file_buffer[1024];
5192 CORE_ADDR top_of_stack;
5197 int saved_inferior_pid;
5199 /* As of 10.x HP-UX, there's an explicit request to get the
5202 tt_status = call_ttrace (TT_PROC_GET_PATHNAME,
5204 (TTRACE_ARG_TYPE) exec_file_buffer,
5205 (TTRACE_ARG_TYPE) sizeof (exec_file_buffer) - 1,
5208 return exec_file_buffer;
5210 /* ??rehrauer: The above request may or may not be broken. It
5211 doesn't seem to work when I use it. But, it may be designed
5212 to only work immediately after an exec event occurs. (I'm
5213 waiting for COSL to explain.)
5215 In any case, if it fails, try a really, truly amazingly gross
5216 hack that DDE uses, of pawing through the process' data
5217 segment to find the pathname.
5219 top_of_stack = 0x7b03a000;
5223 /* On the chance that pid != inferior_pid, set inferior_pid
5224 to pid, so that (grrrr!) implicit uses of inferior_pid get
5227 saved_inferior_pid = inferior_pid;
5230 /* Try to grab a null-terminated string. */
5233 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
5235 inferior_pid = saved_inferior_pid;
5238 for (i = 0; i < 4; i++)
5240 exec_file_buffer[name_index++] = four_chars[i];
5241 done = (four_chars[i] == '\0');
5248 if (exec_file_buffer[0] == '\0')
5250 inferior_pid = saved_inferior_pid;
5254 inferior_pid = saved_inferior_pid;
5255 return exec_file_buffer;
5260 pre_fork_inferior ()
5264 status = pipe (startup_semaphore.parent_channel);
5267 warning ("error getting parent pipe for startup semaphore");
5271 status = pipe (startup_semaphore.child_channel);
5274 warning ("error getting child pipe for startup semaphore");
5279 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5280 * ultimately from "follow_inferior_fork" in infrun.c,
5281 * itself called from "resume".
5283 * This seems to be intended to attach after a fork or
5284 * vfork, while "attach" is used to attach to a pid
5285 * given by the user. The check for an existing attach
5286 * seems odd--it always fails in our test system.
5289 hppa_require_attach (pid)
5295 unsigned int regs_offset;
5296 process_state_t old_process_state = process_state;
5298 /* Are we already attached? There appears to be no explicit
5299 * way to answer this via ttrace, so we try something which
5300 * should be innocuous if we are attached. If that fails,
5301 * then we assume we're not attached, and so attempt to make
5305 tt_status = call_real_ttrace (TT_PROC_STOP,
5308 (TTRACE_ARG_TYPE) TT_NIL,
5309 (TTRACE_ARG_TYPE) TT_NIL,
5314 /* No change to process-state!
5321 /* If successful, the process is now stopped. But if
5322 * we're VFORKING, the parent is still running, so don't
5323 * change the process state.
5325 if (process_state != VFORKING)
5326 process_state = STOPPED;
5328 /* If we were already attached, you'd think that we
5329 * would need to start going again--but you'd be wrong,
5330 * as the fork-following code is actually in the middle
5331 * of the "resume" routine in in "infrun.c" and so
5332 * will (almost) immediately do a resume.
5334 * On the other hand, if we are VFORKING, which means
5335 * that the child and the parent share a process for a
5336 * while, we know that "resume" won't be resuming
5337 * until the child EXEC event is seen. But we still
5338 * don't want to continue, as the event is already
5341 update_thread_state_after_attach (pid, DONT_ATTACH_CONTINUE);
5342 } /* STOP succeeded */
5348 hppa_require_detach (pid, signal)
5354 /* If signal is non-zero, we must pass the signal on to the active
5355 thread prior to detaching. We do this by continuing the threads
5361 threads_continue_all_with_signals (pid, signal);
5365 tt_status = call_ttrace (TT_PROC_DETACH,
5371 errno = 0; /* Ignore any errors. */
5373 /* process_state? */
5378 /* Given the starting address of a memory page, hash it to a bucket in
5379 the memory page dictionary.
5382 get_dictionary_bucket_of_page (page_start)
5383 CORE_ADDR page_start;
5387 hash = (page_start / memory_page_dictionary.page_size);
5388 hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT;
5394 /* Given a memory page's starting address, get (i.e., find an existing
5395 or create a new) dictionary entry for the page. The page will be
5396 write-protected when this function returns, but may have a reference
5397 count of 0 (if the page was newly-added to the dictionary).
5399 static memory_page_t *
5400 get_dictionary_entry_of_page (pid, page_start)
5402 CORE_ADDR page_start;
5405 memory_page_t *page = NULL;
5406 memory_page_t *previous_page = NULL;
5408 /* We're going to be using the dictionary now, than-kew. */
5409 require_memory_page_dictionary (pid);
5411 /* Try to find an existing dictionary entry for this page. Hash
5412 on the page's starting address.
5414 bucket = get_dictionary_bucket_of_page (page_start);
5415 page = &memory_page_dictionary.buckets[bucket];
5416 while (page != NULL)
5418 if (page->page_start == page_start)
5420 previous_page = page;
5424 /* Did we find a dictionary entry for this page? If not, then
5425 add it to the dictionary now.
5429 /* Create a new entry. */
5430 page = (memory_page_t *) xmalloc (sizeof (memory_page_t));
5431 page->page_start = page_start;
5432 page->reference_count = 0;
5434 page->previous = NULL;
5436 /* We'll write-protect the page now, if that's allowed. */
5437 page->original_permissions = write_protect_page (pid, page_start);
5439 /* Add the new entry to the dictionary. */
5440 page->previous = previous_page;
5441 previous_page->next = page;
5443 memory_page_dictionary.page_count++;
5451 remove_dictionary_entry_of_page (pid, page)
5453 memory_page_t *page;
5455 /* Restore the page's original permissions. */
5456 unwrite_protect_page (pid, page->page_start, page->original_permissions);
5458 /* Kick the page out of the dictionary. */
5459 if (page->previous != NULL)
5460 page->previous->next = page->next;
5461 if (page->next != NULL)
5462 page->next->previous = page->previous;
5464 /* Just in case someone retains a handle to this after it's freed. */
5465 page->page_start = (CORE_ADDR) 0;
5467 memory_page_dictionary.page_count--;
5474 hppa_enable_syscall_events (pid)
5478 ttevent_t ttrace_events;
5480 /* Get the set of events that are currently enabled. */
5481 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5483 (TTRACE_ARG_TYPE) & ttrace_events,
5484 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5487 perror_with_name ("ttrace");
5489 /* Add syscall events to that set. */
5490 ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY;
5491 ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN;
5493 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5495 (TTRACE_ARG_TYPE) & ttrace_events,
5496 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5499 perror_with_name ("ttrace");
5504 hppa_disable_syscall_events (pid)
5508 ttevent_t ttrace_events;
5510 /* Get the set of events that are currently enabled. */
5511 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5513 (TTRACE_ARG_TYPE) & ttrace_events,
5514 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5517 perror_with_name ("ttrace");
5519 /* Remove syscall events from that set. */
5520 ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY;
5521 ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN;
5523 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5525 (TTRACE_ARG_TYPE) & ttrace_events,
5526 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5529 perror_with_name ("ttrace");
5533 /* The address range beginning with START and ending with START+LEN-1
5534 (inclusive) is to be watched via page-protection by a new watchpoint.
5535 Set protection for all pages that overlap that range.
5537 Note that our caller sets TYPE to:
5538 0 for a bp_hardware_watchpoint,
5539 1 for a bp_read_watchpoint,
5540 2 for a bp_access_watchpoint
5542 (Yes, this is intentionally (though lord only knows why) different
5543 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5546 hppa_insert_hw_watchpoint (pid, start, len, type)
5552 CORE_ADDR page_start;
5553 int dictionary_was_empty;
5556 LONGEST range_size_in_pages;
5559 error ("read or access hardware watchpoints not supported on HP-UX");
5561 /* Examine all pages in the address range. */
5562 require_memory_page_dictionary ();
5564 dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5566 page_size = memory_page_dictionary.page_size;
5567 page_start = (start / page_size) * page_size;
5568 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5570 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5572 memory_page_t *page;
5574 /* This gets the page entered into the dictionary if it was
5575 not already entered.
5577 page = get_dictionary_entry_of_page (pid, page_start);
5578 page->reference_count++;
5581 /* Our implementation depends on seeing calls to kernel code, for the
5582 following reason. Here we ask to be notified of syscalls.
5584 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5587 But when kernel code accesses the page, it doesn't give a SIGBUS.
5588 Rather, the system call that touched the page fails, with errno=EFAULT.
5591 We could accomodate this "feature" by asking to be notified of syscall
5592 entries & exits; upon getting an entry event, disabling page-protections;
5593 upon getting an exit event, reenabling page-protections and then checking
5594 if any watchpoints triggered.
5596 However, this turns out to be a real performance loser. syscalls are
5597 usually a frequent occurrence. Having to unprotect-reprotect all watched
5598 pages, and also to then read all watched memory locations and compare for
5599 triggers, can be quite expensive.
5601 Instead, we'll only ask to be notified of syscall exits. When we get
5602 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5603 we can just continue the inferior.
5605 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5606 hackish stuff to determine whether the failure really was due to a
5607 page-protect trap on a watched location.
5609 if (dictionary_was_empty)
5610 hppa_enable_syscall_events (pid);
5616 /* The address range beginning with START and ending with START+LEN-1
5617 (inclusive) was being watched via page-protection by a watchpoint
5618 which has been removed. Remove protection for all pages that
5619 overlap that range, which are not also being watched by other
5623 hppa_remove_hw_watchpoint (pid, start, len, type)
5629 CORE_ADDR page_start;
5630 int dictionary_is_empty;
5633 LONGEST range_size_in_pages;
5636 error ("read or access hardware watchpoints not supported on HP-UX");
5638 /* Examine all pages in the address range. */
5639 require_memory_page_dictionary ();
5641 page_size = memory_page_dictionary.page_size;
5642 page_start = (start / page_size) * page_size;
5643 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5645 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5647 memory_page_t *page;
5649 page = get_dictionary_entry_of_page (pid, page_start);
5650 page->reference_count--;
5652 /* Was this the last reference of this page? If so, then we
5653 must scrub the entry from the dictionary, and also restore
5654 the page's original permissions.
5656 if (page->reference_count == 0)
5657 remove_dictionary_entry_of_page (pid, page);
5660 dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5662 /* If write protections are currently disallowed, then that implies that
5663 wait_for_inferior believes that the inferior is within a system call.
5664 Since we want to see both syscall entry and return, it's clearly not
5665 good to disable syscall events in this state!
5667 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5668 "inferior is between syscall events now". Oh well.
5670 if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed)
5671 hppa_disable_syscall_events (pid);
5677 /* Could we implement a watchpoint of this type via our available
5680 This query does not consider whether a particular address range
5681 could be so watched, but just whether support is generally available
5682 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5683 query that answers whether a particular range should be watched via
5687 hppa_can_use_hw_watchpoint (type, cnt, ot)
5692 return (type == bp_hardware_watchpoint);
5696 /* Assuming we could set a hardware watchpoint on this address, do
5697 we think it would be profitable ("a good idea") to do so? If not,
5698 we can always set a regular (aka single-step & test) watchpoint
5702 hppa_range_profitable_for_hw_watchpoint (pid, start, len)
5707 int range_is_stack_based;
5708 int range_is_accessible;
5709 CORE_ADDR page_start;
5712 LONGEST range_size_in_pages;
5714 /* ??rehrauer: For now, say that all addresses are potentially
5715 profitable. Possibly later we'll want to test the address
5718 range_is_stack_based = 0;
5720 /* If any page in the range is inaccessible, then we cannot
5721 really use hardware watchpointing, even though our client
5722 thinks we can. In that case, it's actually an error to
5723 attempt to use hw watchpoints, so we'll tell our client
5724 that the range is "unprofitable", and hope that they listen...
5726 range_is_accessible = 1; /* Until proven otherwise. */
5728 /* Examine all pages in the address range. */
5730 page_size = sysconf (_SC_PAGE_SIZE);
5732 /* If we can't determine page size, we're hosed. Tell our
5733 client it's unprofitable to use hw watchpoints for this
5736 if (errno || (page_size <= 0))
5742 page_start = (start / page_size) * page_size;
5743 range_size_in_pages = len / (LONGEST) page_size;
5745 for (page = 0; page < range_size_in_pages; page++, page_start += page_size)
5748 int page_permissions;
5750 /* Is this page accessible? */
5752 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
5754 (TTRACE_ARG_TYPE) page_start,
5756 (TTRACE_ARG_TYPE) & page_permissions);
5757 if (errno || (tt_status < 0))
5760 range_is_accessible = 0;
5764 /* Yes, go for another... */
5767 return (!range_is_stack_based && range_is_accessible);
5772 hppa_pid_or_tid_to_str (id)
5775 static char buf[100]; /* Static because address returned. */
5777 /* Does this appear to be a process? If so, print it that way. */
5778 if (is_process_id (id))
5779 return hppa_pid_to_str (id);
5781 /* Else, print both the GDB thread number and the system thread id. */
5782 sprintf (buf, "thread %d (", pid_to_thread_id (id));
5783 strcat (buf, hppa_tid_to_str (id));
5784 strcat (buf, ")\0");
5790 /* If the current pid is not the pid this module reported
5791 * from "ptrace_wait" with the most recent event, then the
5792 * user has switched threads.
5794 * If the last reported event was a breakpoint, then return
5795 * the old thread id, else return 0.
5798 hppa_switched_threads (gdb_pid)
5801 if (gdb_pid == old_gdb_pid)
5804 * Core gdb is working with the same pid that it
5805 * was before we reported the last event. This
5806 * is ok: e.g. we reported hitting a thread-specific
5807 * breakpoint, but we were reporting the wrong
5808 * thread, so the core just ignored the event.
5810 * No thread switch has happened.
5814 else if (gdb_pid == reported_pid)
5817 * Core gdb is working with the pid we reported, so
5818 * any continue or step will be able to figure out
5819 * that it needs to step over any hit breakpoints
5820 * without our (i.e. PREPARE_TO_PROCEED's) help.
5824 else if (!reported_bpt)
5827 * The core switched, but we didn't just report a
5828 * breakpoint, so there's no just-hit breakpoint
5829 * instruction at "reported_pid"'s PC, and thus there
5830 * is no need to step over it.
5836 /* There's been a real switch, and we reported
5837 * a hit breakpoint. Let "hppa_prepare_to_proceed"
5838 * know, so it can see whether the breakpoint is
5841 return reported_pid;
5844 /* Keep compiler happy with an obvious return at the end.
5850 hppa_ensure_vforking_parent_remains_stopped (pid)
5853 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5860 hppa_resume_execd_vforking_child_to_get_parent_vfork ()
5862 return 0; /* No, the parent vfork is available now. */
5868 _initialize_infttrace ()
5870 /* Initialize the ttrace-based hardware watchpoint implementation. */
5871 memory_page_dictionary.page_count = (LONGEST) - 1;
5872 memory_page_dictionary.page_protections_allowed = 1;
5875 memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE);
5877 /* We do a lot of casts from pointers to TTRACE_ARG_TYPE; make sure
5879 if (sizeof (TTRACE_ARG_TYPE) < sizeof (void *))
5882 if (errno || (memory_page_dictionary.page_size <= 0))
5883 perror_with_name ("sysconf");