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>
51 #ifdef PTRACE_IN_WRONG_PLACE
54 #include <sys/ptrace.h>
56 #endif /* NO_PTRACE_H */
58 /* Second half of the hackery above. Non-ANSI C, so
59 * we can't use "#error", alas.
62 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
63 /* #error "Hackery to remove warning didn't work right" */
65 /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
68 /* #error "Didn't get expected re-definition of NO_FLAGS" */
69 #define NO_FLAGS INFTTRACE_TEMP_HACK
72 #if !defined (PT_SETTRC)
73 #define PT_SETTRC 0 /* Make process traceable by parent */
75 #if !defined (PT_READ_I)
76 #define PT_READ_I 1 /* Read word from text space */
78 #if !defined (PT_READ_D)
79 #define PT_READ_D 2 /* Read word from data space */
81 #if !defined (PT_READ_U)
82 #define PT_READ_U 3 /* Read word from kernel user struct */
84 #if !defined (PT_WRITE_I)
85 #define PT_WRITE_I 4 /* Write word to text space */
87 #if !defined (PT_WRITE_D)
88 #define PT_WRITE_D 5 /* Write word to data space */
90 #if !defined (PT_WRITE_U)
91 #define PT_WRITE_U 6 /* Write word to kernel user struct */
93 #if !defined (PT_CONTINUE)
94 #define PT_CONTINUE 7 /* Continue after signal */
96 #if !defined (PT_STEP)
97 #define PT_STEP 9 /* Set flag for single stepping */
99 #if !defined (PT_KILL)
100 #define PT_KILL 8 /* Send child a SIGKILL signal */
104 #define PT_ATTACH PTRACE_ATTACH
107 #define PT_DETACH PTRACE_DETACH
112 #include <sys/file.h>
115 /* This semaphore is used to coordinate the child and parent processes
116 after a fork(), and before an exec() by the child. See parent_attach_all
121 int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */
122 int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */
127 #define SEM_LISTEN (0)
129 static startup_semaphore_t startup_semaphore;
131 /* See can_touch_threads_of_process for details. */
132 static int vforking_child_pid = 0;
133 static int vfork_in_flight = 0;
135 /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed).
137 static pid_t old_gdb_pid = 0;
138 static pid_t reported_pid = 0;
139 static int reported_bpt = 0;
141 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
143 #define TT_OK( _status, _errno ) \
144 (((_status) == 1) && ((_errno) == 0))
146 #define TTRACE_ARG_TYPE uint64_t
148 /* When supplied as the "addr" operand, ttrace interprets this
149 to mean, "from the current address".
151 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
153 /* When supplied as the "addr", "data" or "addr2" operand for most
154 requests, ttrace interprets this to mean, "pay no heed to this
157 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
159 /* This is capable of holding the value of a 32-bit register. The
160 value is always left-aligned in the buffer; i.e., [0] contains
161 the most-significant byte of the register's value, and [sizeof(reg)]
162 contains the least-significant value.
164 ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
165 that registers are 32-bits on HP-UX. The latter assumption changes
168 typedef int register_value_t;
170 /********************************************************************
176 The rest of GDB sees threads as being things with different
177 "pid" (process id) values. See "thread.c" for details. The
178 separate threads will be seen and reacted to if infttrace passes
179 back different pid values (for _events_). See wait_for_inferior
182 So infttrace is going to use thread ids externally, pretending
183 they are process ids, and keep track internally so that it can
184 use the real process id (and thread id) when calling ttrace.
186 The data structure that supports this is a linked list of the
187 current threads. Since at some date infttrace will have to
188 deal with multiple processes, each list element records its
189 corresponding pid, rather than having a single global.
191 Note that the list is only approximately current; that's ok, as
192 it's up to date when we need it (we hope!). Also, it can contain
193 dead threads, as there's no harm if it does.
195 The approach taken here is to bury the translation from external
196 to internal inside "call_ttrace" and a few other places.
198 There are some wrinkles:
200 o When GDB forks itself to create the debug target process,
201 there's only a pid of 0 around in the child, so the
202 TT_PROC_SETTRC operation uses a more direct call to ttrace;
203 Similiarly, the initial setting of the event mask happens
204 early as well, and so is also special-cased, and an attach
207 o We define an unthreaded application as having a "pseudo"
210 o To keep from confusing the rest of GDB, we don't switch
211 the PID for the pseudo thread to a TID. A table will help:
213 Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ...
215 Our thread list stores: pid pid pid pid ...
218 Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ...
220 Both pid and tid0 will map to tid0, as there are infttrace.c-internal
221 calls to ttrace using tid0.
225 Since we're implementing the "stop the world" model, sub-model
226 "other threads run during step", we have some stuff to do:
228 o User steps require continuing all threads other than the
229 one the user is stepping;
231 o Internal debugger steps (such as over a breakpoint or watchpoint,
232 but not out of a library load thunk) require stepping only
233 the selected thread; this means that we have to report the
234 step finish on that thread, which can lead to complications;
236 o When a thread is created, it is created running, rather
237 than stopped--so we have to stop it.
239 The OS doesn't guarantee the stopped thread list will be stable,
240 no does it guarantee where on the stopped thread list a thread
241 that is single-stepped will wind up: it's possible that it will
242 be off the list for a while, it's possible the step will complete
243 and it will be re-posted to the end...
245 This means we have to scan the stopped thread list, build up
246 a work-list, and then run down the work list; we can't do the
247 step/continue during the scan.
251 Then there's the issue of waiting for an event. We do this by
252 noticing how many events are reported at the end of each wait.
253 From then on, we "fake" all resumes and steps, returning instantly,
254 and don't do another wait. Once all pending events are reported,
255 we can really resume again.
257 To keep this hidden, all the routines which know about tids and
258 pids or real events and simulated ones are static (file-local).
260 This code can make lots of calls to ttrace, in particular it
261 can spin down the list of thread states more than once. If this
262 becomes a performance hit, the spin could be done once and the
263 various "tsp" blocks saved, keeping all later spins in this
266 The O/S doesn't promise to keep the list straight, and so we must
267 re-scan a lot. By observation, it looks like a single-step/wait
268 puts the stepped thread at the end of the list but doesn't change
271 ****************************************************************
274 /* Uncomment these to turn on various debugging output */
275 /* #define THREAD_DEBUG */
276 /* #define WAIT_BUFFER_DEBUG */
277 /* #define PARANOIA */
280 #define INFTTRACE_ALL_THREADS (-1)
281 #define INFTTRACE_STEP (1)
282 #define INFTTRACE_CONTINUE (0)
284 /* FIX: this is used in inftarg.c/child_wait, in a hack.
286 extern int not_same_real_pid;
288 /* This is used to count buffered events.
290 static unsigned int more_events_left = 0;
294 typedef enum process_state_enum
298 FAKE_CONTINUE, /* For later use */
305 static process_state_t process_state = STOPPED;
307 /* User-specified stepping modality.
309 typedef enum stepping_mode_enum
311 DO_DEFAULT, /* ...which is a continue! */
317 /* Action to take on an attach, depends on
318 * what kind (user command, fork, vfork).
320 * At the moment, this is either:
322 * o continue with a SIGTRAP signal, or
326 typedef enum attach_continue_enum
333 /* This flag is true if we are doing a step-over-bpt
334 * with buffered events. We will have to be sure to
335 * report the right thread, as otherwise the spaghetti
336 * code in "infrun.c/wait_for_inferior" will get
339 static int doing_fake_step = 0;
340 static lwpid_t fake_step_tid = 0;
343 /****************************************************
344 * Thread information structure routines and types. *
345 ****************************************************
348 struct thread_info_struct
350 int am_pseudo; /* This is a pseudo-thread for the process. */
351 int pid; /* Process ID */
352 lwpid_t tid; /* Thread ID */
353 int handled; /* 1 if a buffered event was handled. */
354 int seen; /* 1 if this thread was seen on a traverse. */
355 int terminated; /* 1 if thread has terminated. */
356 int have_signal; /* 1 if signal to be sent */
357 enum target_signal signal_value; /* Signal to send */
358 int have_start; /* 1 if alternate starting address */
359 stepping_mode_t stepping_mode; /* Whether to step or continue */
360 CORE_ADDR start; /* Where to start */
361 int have_state; /* 1 if the event state has been set */
362 ttstate_t last_stop_state; /* The most recently-waited event for this thread. */
363 struct thread_info_struct
364 *next; /* All threads are linked via this field. */
365 struct thread_info_struct
366 *next_pseudo; /* All pseudo-threads are linked via this field. */
371 struct thread_info_header_struct
375 thread_info *head_pseudo;
380 static thread_info_header thread_head =
382 static thread_info_header deleted_threads =
385 static saved_real_pid = 0;
388 /*************************************************
389 * Debugging support functions *
390 *************************************************
396 unsigned long pc_val;
400 offset = register_addr (PC_REGNUM, U_REGS_OFFSET);
401 res = read_from_register_save_state (
403 (TTRACE_ARG_TYPE) offset,
408 return (CORE_ADDR) pc_val;
412 return (CORE_ADDR) 0;
417 get_printable_name_of_stepping_mode (mode)
418 stepping_mode_t mode;
427 return "DO_CONTINUE";
429 return "?unknown mode?";
433 /* This function returns a pointer to a string describing the
434 * ttrace event being reported.
437 get_printable_name_of_ttrace_event (event)
440 /* This enumeration is "gappy", so don't use a table. */
447 return "TTEVT_SIGNAL";
455 return "TTEVT_VFORK";
456 case TTEVT_SYSCALL_RETURN:
457 return "TTEVT_SYSCALL_RETURN";
458 case TTEVT_LWP_CREATE:
459 return "TTEVT_LWP_CREATE";
460 case TTEVT_LWP_TERMINATE:
461 return "TTEVT_LWP_TERMINATE";
463 return "TTEVT_LWP_EXIT";
464 case TTEVT_LWP_ABORT_SYSCALL:
465 return "TTEVT_LWP_ABORT_SYSCALL";
466 case TTEVT_SYSCALL_ENTRY:
467 return "TTEVT_SYSCALL_ENTRY";
468 case TTEVT_SYSCALL_RESTART:
469 return "TTEVT_SYSCALL_RESTART";
471 return "?new event?";
476 /* This function translates the ttrace request enumeration into
477 * a character string that is its printable (aka "human readable")
481 get_printable_name_of_ttrace_request (request)
484 if (!IS_TTRACE_REQ (request))
487 /* This enumeration is "gappy", so don't use a table. */
491 return "TT_PROC_SETTRC";
493 return "TT_PROC_ATTACH";
495 return "TT_PROC_DETACH";
497 return "TT_PROC_RDTEXT";
499 return "TT_PROC_WRTEXT";
501 return "TT_PROC_RDDATA";
503 return "TT_PROC_WRDATA";
505 return "TT_PROC_STOP";
506 case TT_PROC_CONTINUE:
507 return "TT_PROC_CONTINUE";
508 case TT_PROC_GET_PATHNAME:
509 return "TT_PROC_GET_PATHNAME";
510 case TT_PROC_GET_EVENT_MASK:
511 return "TT_PROC_GET_EVENT_MASK";
512 case TT_PROC_SET_EVENT_MASK:
513 return "TT_PROC_SET_EVENT_MASK";
514 case TT_PROC_GET_FIRST_LWP_STATE:
515 return "TT_PROC_GET_FIRST_LWP_STATE";
516 case TT_PROC_GET_NEXT_LWP_STATE:
517 return "TT_PROC_GET_NEXT_LWP_STATE";
519 return "TT_PROC_EXIT";
520 case TT_PROC_GET_MPROTECT:
521 return "TT_PROC_GET_MPROTECT";
522 case TT_PROC_SET_MPROTECT:
523 return "TT_PROC_SET_MPROTECT";
524 case TT_PROC_SET_SCBM:
525 return "TT_PROC_SET_SCBM";
527 return "TT_LWP_STOP";
528 case TT_LWP_CONTINUE:
529 return "TT_LWP_CONTINUE";
531 return "TT_LWP_SINGLE";
533 return "TT_LWP_RUREGS";
535 return "TT_LWP_WUREGS";
536 case TT_LWP_GET_EVENT_MASK:
537 return "TT_LWP_GET_EVENT_MASK";
538 case TT_LWP_SET_EVENT_MASK:
539 return "TT_LWP_SET_EVENT_MASK";
540 case TT_LWP_GET_STATE:
541 return "TT_LWP_GET_STATE";
548 /* This function translates the process state enumeration into
549 * a character string that is its printable (aka "human readable")
553 get_printable_name_of_process_state (process_state)
554 process_state_t process_state;
556 switch (process_state)
561 return "FAKE_STEPPING";
569 return "?some unknown state?";
573 /* Set a ttrace thread state to a safe, initial state.
576 clear_ttstate_t (tts)
581 tts->tts_user_tid = 0;
582 tts->tts_event = TTEVT_NONE;
585 /* Copy ttrace thread state TTS_FROM into TTS_TO.
588 copy_ttstate_t (tts_to, tts_from)
592 memcpy ((char *) tts_to, (char *) tts_from, sizeof (*tts_to));
595 /* Are there any live threads we know about?
598 any_thread_records ()
600 return (thread_head.count > 0);
603 /* Create, fill in and link in a thread descriptor.
606 create_thread_info (pid, tid)
612 int thread_count_of_pid;
614 new_p = malloc (sizeof (thread_info));
617 new_p->have_signal = 0;
618 new_p->have_start = 0;
619 new_p->have_state = 0;
620 clear_ttstate_t (&new_p->last_stop_state);
621 new_p->am_pseudo = 0;
624 new_p->terminated = 0;
626 new_p->next_pseudo = NULL;
627 new_p->stepping_mode = DO_DEFAULT;
629 if (0 == thread_head.count)
633 printf ("First thread, pid %d tid %d!\n", pid, tid);
635 saved_real_pid = inferior_pid;
641 printf ("Subsequent thread, pid %d tid %d\n", pid, tid);
645 /* Another day, another thread...
649 /* The new thread always goes at the head of the list.
651 new_p->next = thread_head.head;
652 thread_head.head = new_p;
654 /* Is this the "pseudo" thread of a process? It is if there's
655 * no other thread for this process on the list. (Note that this
656 * accomodates multiple processes, such as we see even for simple
657 * cases like forking "non-threaded" programs.)
659 p = thread_head.head;
660 thread_count_of_pid = 0;
663 if (p->pid == new_p->pid)
664 thread_count_of_pid++;
668 /* Did we see any other threads for this pid? (Recall that we just
669 * added this thread to the list...)
671 if (thread_count_of_pid == 1)
673 new_p->am_pseudo = 1;
674 new_p->next_pseudo = thread_head.head_pseudo;
675 thread_head.head_pseudo = new_p;
681 /* Get rid of our thread info.
691 printf ("Clearing all thread info\n");
694 p = thread_head.head;
702 thread_head.head = NULL;
703 thread_head.head_pseudo = NULL;
704 thread_head.count = 0;
706 p = deleted_threads.head;
714 deleted_threads.head = NULL;
715 deleted_threads.head_pseudo = NULL;
716 deleted_threads.count = 0;
718 /* No threads, so can't have pending events.
720 more_events_left = 0;
723 /* Given a tid, find the thread block for it.
726 find_thread_info (tid)
731 for (p = thread_head.head; p; p = p->next)
739 for (p = deleted_threads.head; p; p = p->next)
750 /* For any but the pseudo thread, this maps to the
751 * thread ID. For the pseudo thread, if you pass either
752 * the thread id or the PID, you get the pseudo thread ID.
754 * We have to be prepared for core gdb to ask about
755 * deleted threads. We do the map, but we don't like it.
758 map_from_gdb_tid (gdb_tid)
763 /* First assume gdb_tid really is a tid, and try to find a
764 * matching entry on the threads list.
766 for (p = thread_head.head; p; p = p->next)
768 if (p->tid == gdb_tid)
772 /* It doesn't appear to be a tid; perhaps it's really a pid?
773 * Try to find a "pseudo" thread entry on the threads list.
775 for (p = thread_head.head_pseudo; p != NULL; p = p->next_pseudo)
777 if (p->pid == gdb_tid)
781 /* Perhaps it's the tid of a deleted thread we may still
782 * have some knowledge of?
784 for (p = deleted_threads.head; p; p = p->next)
786 if (p->tid == gdb_tid)
790 /* Or perhaps it's the pid of a deleted process we may still
793 for (p = deleted_threads.head_pseudo; p != NULL; p = p->next_pseudo)
795 if (p->pid == gdb_tid)
799 return 0; /* Error? */
802 /* Map the other way: from a real tid to the
803 * "pid" known by core gdb. This tid may be
804 * for a thread that just got deleted, so we
805 * also need to consider deleted threads.
808 map_to_gdb_tid (real_tid)
813 for (p = thread_head.head; p; p = p->next)
815 if (p->tid == real_tid)
824 for (p = deleted_threads.head; p; p = p->next)
826 if (p->tid == real_tid)
828 return p->pid; /* Error? */
833 return 0; /* Error? Never heard of this thread! */
836 /* Do any threads have saved signals?
839 saved_signals_exist ()
843 for (p = thread_head.head; p; p = p->next)
854 /* Is this the tid for the zero-th thread?
857 is_pseudo_thread (tid)
860 thread_info *p = find_thread_info (tid);
861 if (NULL == p || p->terminated)
867 /* Is this thread terminated?
873 thread_info *p = find_thread_info (tid);
876 return p->terminated;
881 /* Is this pid a real PID or a TID?
892 /* What does PID really represent?
894 tid = map_from_gdb_tid (pid);
896 return 0; /* Actually, is probably an error... */
898 tinfo = find_thread_info (tid);
900 /* Does it appear to be a true thread?
902 if (!tinfo->am_pseudo)
905 /* Else, it looks like it may be a process. See if there's any other
906 * threads with the same process ID, though. If there are, then TID
907 * just happens to be the first thread of several for this process.
909 this_pid = tinfo->pid;
911 for (tinfo = thread_head.head; tinfo; tinfo = tinfo->next)
913 if (tinfo->pid == this_pid)
917 return (this_pid_count == 1);
921 /* Add a thread to our info. Prevent duplicate entries.
924 add_tthread (pid, tid)
930 p = find_thread_info (tid);
932 p = create_thread_info (pid, tid);
937 /* Notice that a thread was deleted.
946 if (thread_head.count <= 0)
948 error ("Internal error in thread database.");
953 for (p = thread_head.head; p; p = p->next)
960 printf ("Delete here: %d \n", tid);
966 * Deleting a main thread is ok if we're doing
967 * a parent-follow on a child; this is odd but
968 * not wrong. It apparently _doesn't_ happen
969 * on the child-follow, as we don't just delete
970 * the pseudo while keeping the rest of the
971 * threads around--instead, we clear out the whole
972 * thread list at once.
975 thread_info *q_chase;
978 for (q = thread_head.head_pseudo; q; q = q->next)
982 /* Remove from pseudo list.
985 thread_head.head_pseudo = p->next_pseudo;
987 q_chase->next = p->next_pseudo;
994 /* Remove from live list.
999 thread_head.head = p->next;
1001 chase->next = p->next;
1003 /* Add to deleted thread list.
1005 p->next = deleted_threads.head;
1006 deleted_threads.head = p;
1007 deleted_threads.count++;
1010 p->next_pseudo = deleted_threads.head_pseudo;
1011 deleted_threads.head_pseudo = p;
1023 /* Get the pid for this tid. (Has to be a real TID!).
1031 for (p = thread_head.head; p; p = p->next)
1039 for (p = deleted_threads.head; p; p = p->next)
1050 /* Note that this thread's current event has been handled.
1053 set_handled (pid, tid)
1059 p = find_thread_info (tid);
1061 p = add_tthread (pid, tid);
1066 /* Was this thread's current event handled?
1074 p = find_thread_info (tid);
1078 return 0; /* New threads have not been handled */
1081 /* Set this thread to unhandled.
1089 #ifdef WAIT_BUFFER_DEBUG
1091 printf ("clear_handled %d\n", (int) tid);
1094 p = find_thread_info (tid);
1096 error ("Internal error: No thread state to clear?");
1101 /* Set all threads to unhandled.
1104 clear_all_handled ()
1108 #ifdef WAIT_BUFFER_DEBUG
1110 printf ("clear_all_handled\n");
1113 for (p = thread_head.head; p; p = p->next)
1118 for (p = deleted_threads.head; p; p = p->next)
1124 /* Set this thread to default stepping mode.
1127 clear_stepping_mode (tid)
1132 #ifdef WAIT_BUFFER_DEBUG
1134 printf ("clear_stepping_mode %d\n", (int) tid);
1137 p = find_thread_info (tid);
1139 error ("Internal error: No thread state to clear?");
1141 p->stepping_mode = DO_DEFAULT;
1144 /* Set all threads to do default continue on resume.
1147 clear_all_stepping_mode ()
1151 #ifdef WAIT_BUFFER_DEBUG
1153 printf ("clear_all_stepping_mode\n");
1156 for (p = thread_head.head; p; p = p->next)
1158 p->stepping_mode = DO_DEFAULT;
1161 for (p = deleted_threads.head; p; p = p->next)
1163 p->stepping_mode = DO_DEFAULT;
1167 /* Set all threads to unseen on this pass.
1174 for (p = thread_head.head; p; p = p->next)
1180 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1181 /* debugging routine.
1187 printf (" Thread pid %d, tid %d", p->pid, p->tid);
1189 printf (", event is %s",
1190 get_printable_name_of_ttrace_event (p->last_stop_state.tts_event));
1193 printf (", pseudo thread");
1196 printf (", have signal 0x%x", p->signal_value);
1199 printf (", have start at 0x%x", p->start);
1201 printf (", step is %s", get_printable_name_of_stepping_mode (p->stepping_mode));
1204 printf (", handled");
1206 printf (", not handled");
1211 printf (", not seen");
1221 if (thread_head.count == 0)
1222 printf ("Thread list is empty\n");
1225 printf ("Thread list has ");
1226 if (thread_head.count == 1)
1227 printf ("1 entry:\n");
1229 printf ("%d entries:\n", thread_head.count);
1230 for (p = thread_head.head; p; p = p->next)
1236 if (deleted_threads.count == 0)
1237 printf ("Deleted thread list is empty\n");
1240 printf ("Deleted thread list has ");
1241 if (deleted_threads.count == 1)
1242 printf ("1 entry:\n");
1244 printf ("%d entries:\n", deleted_threads.count);
1246 for (p = deleted_threads.head; p; p = p->next)
1254 /* Update the thread list based on the "seen" bits.
1257 update_thread_list ()
1263 for (p = thread_head.head; p; p = p->next)
1265 /* Is this an "unseen" thread which really happens to be a process?
1266 If so, is it inferior_pid and is a vfork in flight? If yes to
1267 all, then DON'T REMOVE IT! We're in the midst of moving a vfork
1268 operation, which is a multiple step thing, to the point where we
1269 can touch the parent again. We've most likely stopped to examine
1270 the child at a late stage in the vfork, and if we're not following
1271 the child, we'd best not treat the parent as a dead "thread"...
1273 if ((!p->seen) && p->am_pseudo && vfork_in_flight
1274 && (p->pid != vforking_child_pid))
1284 printf ("Delete unseen thread: %d \n", p->tid);
1286 del_tthread (p->tid);
1293 /************************************************
1294 * O/S call wrappers *
1295 ************************************************
1298 /* This function simply calls ttrace with the given arguments.
1299 * It exists so that all calls to ttrace are isolated. All
1300 * parameters should be as specified by "man 2 ttrace".
1302 * No other "raw" calls to ttrace should exist in this module.
1305 call_real_ttrace (request, pid, tid, addr, data, addr2)
1309 TTRACE_ARG_TYPE addr, data, addr2;
1314 tt_status = ttrace (request, pid, tid, addr, data, addr2);
1319 /* Don't bother for a known benign error: if you ask for the
1320 * first thread state, but there is only one thread and it's
1321 * not stopped, ttrace complains.
1323 * We have this inside the #ifdef because our caller will do
1324 * this check for real.
1326 if (request != TT_PROC_GET_FIRST_LWP_STATE
1330 printf ("TT fail for %s, with pid %d, tid %d, status %d \n",
1331 get_printable_name_of_ttrace_request (request),
1332 pid, tid, tt_status);
1338 /* ??rehrauer: It would probably be most robust to catch and report
1339 * failed requests here. However, some clients of this interface
1340 * seem to expect to catch & deal with them, so we'd best not.
1344 strcpy (reason_for_failure, "ttrace (");
1345 strcat (reason_for_failure, get_printable_name_of_ttrace_request (request));
1346 strcat (reason_for_failure, ")");
1347 printf ("ttrace error, errno = %d\n", errno);
1348 perror_with_name (reason_for_failure);
1356 /* This function simply calls ttrace_wait with the given arguments.
1357 * It exists so that all calls to ttrace_wait are isolated.
1359 * No "raw" calls to ttrace_wait should exist elsewhere.
1362 call_real_ttrace_wait (pid, tid, option, tsp, tsp_size)
1370 thread_info *tinfo = NULL;
1373 ttw_status = ttrace_wait (pid, tid, option, tsp, tsp_size);
1379 printf ("TW fail with pid %d, tid %d \n", pid, tid);
1382 perror_with_name ("ttrace wait");
1389 /* A process may have one or more kernel threads, of which all or
1390 none may be stopped. This function returns the ID of the first
1391 kernel thread in a stopped state, or 0 if none are stopped.
1393 This function can be used with get_process_next_stopped_thread_id
1394 to iterate over the IDs of all stopped threads of this process.
1397 get_process_first_stopped_thread_id (pid, thread_state)
1399 ttstate_t *thread_state;
1403 tt_status = call_real_ttrace (TT_PROC_GET_FIRST_LWP_STATE,
1406 (TTRACE_ARG_TYPE) thread_state,
1407 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1412 if (errno == EPROTO)
1414 /* This is an error we can handle: there isn't any stopped
1415 * thread. This happens when we're re-starting the application
1416 * and it has only one thread. GET_NEXT handles the case of
1417 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1425 perror_with_name ("ttrace");
1433 return thread_state->tts_lwpid;
1437 /* This function returns the ID of the "next" kernel thread in a
1438 stopped state, or 0 if there are none. "Next" refers to the
1439 thread following that of the last successful call to this
1440 function or to get_process_first_stopped_thread_id, using
1441 the value of thread_state returned by that call.
1443 This function can be used with get_process_first_stopped_thread_id
1444 to iterate over the IDs of all stopped threads of this process.
1447 get_process_next_stopped_thread_id (pid, thread_state)
1449 ttstate_t *thread_state;
1453 tt_status = call_real_ttrace (
1454 TT_PROC_GET_NEXT_LWP_STATE,
1457 (TTRACE_ARG_TYPE) thread_state,
1458 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1461 perror_with_name ("ttrace");
1468 else if (tt_status == 0)
1470 /* End of list, no next state. Don't return the
1471 * tts_lwpid, as it's a meaningless "240".
1473 * This is an HPUX "feature".
1478 return thread_state->tts_lwpid;
1481 /* ??rehrauer: Eventually this function perhaps should be calling
1482 pid_to_thread_id. However, that function currently does nothing
1483 for HP-UX. Even then, I'm not clear whether that function
1484 will return a "kernel" thread ID, or a "user" thread ID. If
1485 the former, we can just call it here. If the latter, we must
1486 map from the "user" tid to a "kernel" tid.
1488 NOTE: currently not called.
1491 get_active_tid_of_pid (pid)
1494 ttstate_t thread_state;
1496 return get_process_first_stopped_thread_id (pid, &thread_state);
1499 /* This function returns 1 if tt_request is a ttrace request that
1500 * operates upon all threads of a (i.e., the entire) process.
1503 is_process_ttrace_request (tt_request)
1506 return IS_TTRACE_PROCREQ (tt_request);
1510 /* This function translates a thread ttrace request into
1511 * the equivalent process request for a one-thread process.
1514 make_process_version (request)
1517 if (!IS_TTRACE_REQ (request))
1519 error ("Internal error, bad ttrace request made\n");
1526 return TT_PROC_STOP;
1528 case TT_LWP_CONTINUE:
1529 return TT_PROC_CONTINUE;
1531 case TT_LWP_GET_EVENT_MASK:
1532 return TT_PROC_GET_EVENT_MASK;
1534 case TT_LWP_SET_EVENT_MASK:
1535 return TT_PROC_SET_EVENT_MASK;
1540 case TT_LWP_GET_STATE:
1541 return -1; /* No equivalent */
1549 /* This function translates the "pid" used by the rest of
1550 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1551 * with the given arguments.
1553 * In general, other parts of this module should call this
1554 * function when they are dealing with external users, who only
1555 * have tids to pass (but they call it "pid" for historical
1559 call_ttrace (request, gdb_tid, addr, data, addr2)
1562 TTRACE_ARG_TYPE addr, data, addr2;
1566 ttreq_t new_request;
1568 char reason_for_failure[100]; /* Arbitrary size, should be big enough. */
1571 int is_interesting = 0;
1573 if (TT_LWP_RUREGS == request)
1575 is_interesting = 1; /* Adjust code here as desired */
1578 if (is_interesting && 0 && debug_on)
1580 if (!is_process_ttrace_request (request))
1582 printf ("TT: Thread request, tid is %d", gdb_tid);
1583 printf ("== SINGLE at %x", addr);
1587 printf ("TT: Process request, tid is %d\n", gdb_tid);
1588 printf ("==! SINGLE at %x", addr);
1593 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1594 * which happen before any threads get set up) should go
1595 * directly to "call_real_ttrace", so they don't happen here.
1597 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1601 if (request == TT_PROC_SETTRC && debug_on)
1602 printf ("Unexpected call for TT_PROC_SETTRC\n");
1605 /* Sometimes we get called with a bogus tid (e.g., if a
1606 * thread has terminated, we return 0; inftarg later asks
1607 * whether the thread has exited/forked/vforked).
1611 errno = ESRCH; /* ttrace's response would probably be "No such process". */
1615 /* All other cases should be able to expect that there are
1618 if (!any_thread_records ())
1622 warning ("No thread records for ttrace call");
1624 errno = ESRCH; /* ttrace's response would be "No such process". */
1628 /* OK, now the task is to translate the incoming tid into
1631 real_tid = map_from_gdb_tid (gdb_tid);
1632 real_pid = get_pid_for (real_tid);
1634 /* Now check the result. "Real_pid" is NULL if our list
1635 * didn't find it. We have some tricks we can play to fix
1640 ttstate_t thread_state;
1644 printf ("No saved pid for tid %d\n", gdb_tid);
1647 if (is_process_ttrace_request (request))
1650 /* Ok, we couldn't get a tid. Try to translate to
1651 * the equivalent process operation. We expect this
1652 * NOT to happen, so this is a desparation-type
1653 * move. It can happen if there is an internal
1654 * error and so no "wait()" call is ever done.
1656 new_request = make_process_version (request);
1657 if (new_request == -1)
1662 printf ("...and couldn't make process version of thread operation\n");
1665 /* Use hacky saved pid, which won't always be correct
1666 * in the multi-process future. Use tid as thread,
1667 * probably dooming this to failure. FIX!
1669 if (saved_real_pid != 0)
1673 printf ("...using saved pid %d\n", saved_real_pid);
1676 real_pid = saved_real_pid;
1681 error ("Unable to perform thread operation");
1686 /* Sucessfully translated this to a process request,
1687 * which needs no thread value.
1691 request = new_request;
1696 printf ("Translated thread request to process request\n");
1697 if (saved_real_pid == 0)
1698 printf ("...but there's no saved pid\n");
1702 if (gdb_tid != saved_real_pid)
1703 printf ("...but have the wrong pid (%d rather than %d)\n",
1704 gdb_tid, saved_real_pid);
1708 } /* Translated to a process request */
1709 } /* Is a process request */
1713 /* We have to have a thread. Ooops.
1715 error ("Thread request with no threads (%s)",
1716 get_printable_name_of_ttrace_request (request));
1720 /* Ttrace doesn't like to see tid values on process requests,
1721 * even if we have the right one.
1723 if (is_process_ttrace_request (request))
1729 if (is_interesting && 0 && debug_on)
1731 printf (" now tid %d, pid %d\n", real_tid, real_pid);
1732 printf (" request is %s\n", get_printable_name_of_ttrace_request (request));
1736 /* Finally, the (almost) real call.
1738 tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2);
1741 if (is_interesting && debug_on)
1743 if (!TT_OK (tt_status, errno)
1744 && !(tt_status == 0 & errno == 0))
1745 printf (" got error (errno==%d, status==%d)\n", errno, tt_status);
1753 /* Stop all the threads of a process.
1755 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1756 * to get a TTEVT_NONE event, discarding the old event. Be
1757 * very careful, and only call TT_PROC_STOP when you mean it!
1760 stop_all_threads_of_process (real_pid)
1765 ttw_status = call_real_ttrace (TT_PROC_STOP,
1768 (TTRACE_ARG_TYPE) TT_NIL,
1769 (TTRACE_ARG_TYPE) TT_NIL,
1772 perror_with_name ("ttrace stop of other threads");
1776 /* Under some circumstances, it's unsafe to attempt to stop, or even
1777 query the state of, a process' threads.
1779 In ttrace-based HP-UX, an example is a vforking child process. The
1780 vforking parent and child are somewhat fragile, w/r/t what we can do
1781 what we can do to them with ttrace, until after the child exits or
1782 execs, or until the parent's vfork event is delivered. Until that
1783 time, we must not try to stop the process' threads, or inquire how
1784 many there are, or even alter its data segments, or it typically dies
1785 with a SIGILL. Sigh.
1787 This function returns 1 if this stopped process, and the event that
1788 we're told was responsible for its current stopped state, cannot safely
1789 have its threads examined.
1791 #define CHILD_VFORKED(evt,pid) \
1792 (((evt) == TTEVT_VFORK) && ((pid) != inferior_pid))
1793 #define CHILD_URPED(evt,pid) \
1794 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1795 #define PARENT_VFORKED(evt,pid) \
1796 (((evt) == TTEVT_VFORK) && ((pid) == inferior_pid))
1799 can_touch_threads_of_process (pid, stopping_event)
1801 ttevents_t stopping_event;
1803 if (CHILD_VFORKED (stopping_event, pid))
1805 vforking_child_pid = pid;
1806 vfork_in_flight = 1;
1809 else if (vfork_in_flight &&
1810 (PARENT_VFORKED (stopping_event, pid) ||
1811 CHILD_URPED (stopping_event, pid)))
1813 vfork_in_flight = 0;
1814 vforking_child_pid = 0;
1817 return !vfork_in_flight;
1821 /* If we can find an as-yet-unhandled thread state of a
1822 * stopped thread of this process return 1 and set "tsp".
1823 * Return 0 if we can't.
1825 * If this function is used when the threads of PIS haven't
1826 * been stopped, undefined behaviour is guaranteed!
1829 select_stopped_thread_of_process (pid, tsp)
1833 lwpid_t candidate_tid, tid;
1834 ttstate_t candidate_tstate, tstate;
1836 /* If we're not allowed to touch the process now, then just
1837 * return the current value of *TSP.
1839 * This supports "vfork". It's ok, really, to double the
1840 * current event (the child EXEC, we hope!).
1842 if (!can_touch_threads_of_process (pid, tsp->tts_event))
1845 /* Decide which of (possibly more than one) events to
1846 * return as the first one. We scan them all so that
1847 * we always return the result of a fake-step first.
1850 for (tid = get_process_first_stopped_thread_id (pid, &tstate);
1852 tid = get_process_next_stopped_thread_id (pid, &tstate))
1854 /* TTEVT_NONE events are uninteresting to our clients. They're
1855 * an artifact of our "stop the world" model--the thread is
1856 * stopped because we stopped it.
1858 if (tstate.tts_event == TTEVT_NONE)
1860 set_handled (pid, tstate.tts_lwpid);
1863 /* Did we just single-step a single thread, without letting any
1864 * of the others run? Is this an event for that thread?
1866 * If so, we believe our client would prefer to see this event
1867 * over any others. (Typically the client wants to just push
1868 * one thread a little farther forward, and then go around
1869 * checking for what all threads are doing.)
1871 else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid))
1873 #ifdef WAIT_BUFFER_DEBUG
1874 /* It's possible here to see either a SIGTRAP (due to
1875 * successful completion of a step) or a SYSCALL_ENTRY
1876 * (due to a step completion with active hardware
1880 printf ("Ending fake step with tid %d, state %s\n",
1882 get_printable_name_of_ttrace_event (tstate.tts_event));
1885 /* Remember this one, and throw away any previous
1888 candidate_tid = tstate.tts_lwpid;
1889 candidate_tstate = tstate;
1892 #ifdef FORGET_DELETED_BPTS
1894 /* We can't just do this, as if we do, and then wind
1895 * up the loop with no unhandled events, we need to
1896 * handle that case--the appropriate reaction is to
1897 * just continue, but there's no easy way to do that.
1899 * Better to put this in the ttrace_wait call--if, when
1900 * we fake a wait, we update our events based on the
1901 * breakpoint_here_pc call and find there are no more events,
1902 * then we better continue and so on.
1904 * Or we could put it in the next/continue fake.
1905 * But it has to go in the buffering code, not in the
1906 * real go/wait code.
1908 else if ((TTEVT_SIGNAL == tstate.tts_event)
1909 && (5 == tstate.tts_u.tts_signal.tts_signo)
1910 && (0 != get_raw_pc (tstate.tts_lwpid))
1911 && !breakpoint_here_p (get_raw_pc (tstate.tts_lwpid)))
1914 * If the user deleted a breakpoint while this
1915 * breakpoint-hit event was buffered, we can forget
1918 #ifdef WAIT_BUFFER_DEBUG
1920 printf ("Forgetting deleted bp hit for thread %d\n",
1924 set_handled (pid, tstate.tts_lwpid);
1928 /* Else, is this the first "unhandled" event? If so,
1929 * we believe our client wants to see it (if we don't
1930 * see a fake-step later on in the scan).
1932 else if (!was_handled (tstate.tts_lwpid) && candidate_tid == 0)
1934 candidate_tid = tstate.tts_lwpid;
1935 candidate_tstate = tstate;
1938 /* This is either an event that has already been "handled",
1939 * and thus we believe is uninteresting to our client, or we
1940 * already have a candidate event. Ignore it...
1944 /* What do we report?
1946 if (doing_fake_step)
1948 if (candidate_tid == fake_step_tid)
1952 tstate = candidate_tstate;
1956 warning ("Internal error: fake-step failed to complete.");
1960 else if (candidate_tid != 0)
1962 /* Found a candidate unhandled event.
1964 tstate = candidate_tstate;
1968 warning ("Internal error in call of ttrace_wait.");
1973 warning ("Internal error: no unhandled thread event to select");
1977 copy_ttstate_t (tsp, &tstate);
1979 } /* End of select_stopped_thread_of_process */
1982 /* Check our internal thread data against the real thing.
1985 check_thread_consistency (real_pid)
1988 int tid; /* really lwpid_t */
1992 /* Spin down the O/S list of threads, checking that they
1993 * match what we've got.
1995 for (tid = get_process_first_stopped_thread_id (real_pid, &tstate);
1997 tid = get_process_next_stopped_thread_id (real_pid, &tstate))
2000 p = find_thread_info (tid);
2004 warning ("No internal thread data for thread %d.", tid);
2010 warning ("Inconsistent internal thread data for thread %d.", tid);
2015 warning ("Thread %d is not terminated, internal error.", tid);
2020 #define TT_COMPARE( fld ) \
2021 tstate.fld != p->last_stop_state.fld
2025 if (TT_COMPARE (tts_pid)
2026 || TT_COMPARE (tts_lwpid)
2027 || TT_COMPARE (tts_user_tid)
2028 || TT_COMPARE (tts_event)
2029 || TT_COMPARE (tts_flags)
2030 || TT_COMPARE (tts_scno)
2031 || TT_COMPARE (tts_scnargs))
2033 warning ("Internal thread data for thread %d is wrong.", tid);
2039 #endif /* PARANOIA */
2042 /* This function wraps calls to "call_real_ttrace_wait" so
2043 * that a actual wait is only done when all pending events
2044 * have been reported.
2046 * Note that typically it is called with a pid of "0", i.e.
2047 * the "don't care" value.
2049 * Return value is the status of the pseudo wait.
2052 call_ttrace_wait (pid, option, tsp, tsp_size)
2058 /* This holds the actual, for-real, true process ID.
2060 static int real_pid;
2062 /* As an argument to ttrace_wait, zero pid
2063 * means "Any process", and zero tid means
2064 * "Any thread of the specified process".
2067 lwpid_t wait_tid = 0;
2070 int ttw_status = 0; /* To be returned */
2072 thread_info *tinfo = NULL;
2080 printf ("TW: Pid to wait on is %d\n", pid);
2083 if (!any_thread_records ())
2084 error ("No thread records for ttrace call w. specific pid");
2086 /* OK, now the task is to translate the incoming tid into
2089 real_tid = map_from_gdb_tid (pid);
2090 real_pid = get_pid_for (real_tid);
2093 printf ("==TW: real pid %d, real tid %d\n", real_pid, real_tid);
2098 /* Sanity checks and set-up.
2101 * Stopped Running Fake-step (v)Fork
2102 * \________________________________________
2104 * No buffered events | error wait wait wait
2106 * Buffered events | debuffer error wait debuffer (?)
2109 if (more_events_left == 0)
2112 if (process_state == RUNNING)
2114 /* OK--normal call of ttrace_wait with no buffered events.
2118 else if (process_state == FAKE_STEPPING)
2120 /* Ok--call of ttrace_wait to support
2121 * fake stepping with no buffered events.
2123 * But we better be fake-stepping!
2125 if (!doing_fake_step)
2127 warning ("Inconsistent thread state.");
2130 else if ((process_state == FORKING)
2131 || (process_state == VFORKING))
2133 /* Ok--there are two processes, so waiting
2134 * for the second while the first is stopped
2135 * is ok. Handled bits stay as they were.
2139 else if (process_state == STOPPED)
2141 warning ("Process not running at wait call.");
2146 warning ("Inconsistent process state.");
2153 if (process_state == STOPPED)
2155 /* OK--buffered events being unbuffered.
2159 else if (process_state == RUNNING)
2161 /* An error--shouldn't have buffered events
2164 warning ("Trying to continue with buffered events:");
2166 else if (process_state == FAKE_STEPPING)
2169 * Better be fake-stepping!
2171 if (!doing_fake_step)
2173 warning ("Losing buffered thread events!\n");
2176 else if ((process_state == FORKING)
2177 || (process_state == VFORKING))
2179 /* Ok--there are two processes, so waiting
2180 * for the second while the first is stopped
2181 * is ok. Handled bits stay as they were.
2186 warning ("Process in unknown state with buffered events.");
2189 /* Sometimes we have to wait for a particular thread
2190 * (if we're stepping over a bpt). In that case, we
2191 * _know_ it's going to complete the single-step we
2192 * asked for (because we're only doing the step under
2193 * certain very well-understood circumstances), so it
2196 if (doing_fake_step)
2198 wait_tid = fake_step_tid;
2199 wait_pid = get_pid_for (fake_step_tid);
2201 #ifdef WAIT_BUFFER_DEBUG
2203 printf ("Doing a wait after a fake-step for %d, pid %d\n",
2204 wait_tid, wait_pid);
2208 if (more_events_left == 0 /* No buffered events, need real ones. */
2209 || process_state != STOPPED)
2211 /* If there are no buffered events, and so we need
2212 * real ones, or if we are FORKING, VFORKING,
2213 * FAKE_STEPPING or RUNNING, and thus have to do
2214 * a real wait, then do a real wait.
2217 #ifdef WAIT_BUFFER_DEBUG
2218 /* Normal case... */
2220 printf ("TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid);
2223 /* The actual wait call.
2225 ttw_status = call_real_ttrace_wait (wait_pid, wait_tid, option, tsp, tsp_size);
2227 /* Note that the routines we'll call will be using "call_real_ttrace",
2228 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2229 * the rest of the world uses (which is actually the tid).
2231 real_pid = tsp->tts_pid;
2233 /* For most events: Stop the world!
2235 * It's sometimes not safe to stop all threads of a process.
2236 * Sometimes it's not even safe to ask for the thread state
2239 if (can_touch_threads_of_process (real_pid, tsp->tts_event))
2241 /* If we're really only stepping a single thread, then don't
2242 * try to stop all the others -- we only do this single-stepping
2243 * business when all others were already stopped...and the stop
2244 * would mess up other threads' events.
2246 * Similiarly, if there are other threads with events,
2247 * don't do the stop.
2249 if (!doing_fake_step)
2251 if (more_events_left > 0)
2252 warning ("Internal error in stopping process");
2254 stop_all_threads_of_process (real_pid);
2256 /* At this point, we could scan and update_thread_list(),
2257 * and only use the local list for the rest of the
2258 * module! We'd get rid of the scans in the various
2259 * continue routines (adding one in attach). It'd
2260 * be great--UPGRADE ME!
2268 if (more_events_left > 0)
2269 printf ("== Can't stop process; more events!\n");
2271 printf ("== Can't stop process!\n");
2275 process_state = STOPPED;
2277 #ifdef WAIT_BUFFER_DEBUG
2279 printf ("Process set to STOPPED\n");
2285 /* Fake a call to ttrace_wait. The process must be
2286 * STOPPED, as we aren't going to do any wait.
2288 #ifdef WAIT_BUFFER_DEBUG
2290 printf ("TW: fake it\n");
2293 if (process_state != STOPPED)
2295 warning ("Process not stopped at wait call, in state '%s'.\n",
2296 get_printable_name_of_process_state (process_state));
2299 if (doing_fake_step)
2300 error ("Internal error in stepping over breakpoint");
2302 ttw_status = 0; /* Faking it is always successful! */
2303 } /* End of fake or not? if */
2305 /* Pick an event to pass to our caller. Be paranoid.
2307 if (!select_stopped_thread_of_process (real_pid, tsp))
2308 warning ("Can't find event, using previous event.");
2310 else if (tsp->tts_event == TTEVT_NONE)
2311 warning ("Internal error: no thread has a real event.");
2313 else if (doing_fake_step)
2315 if (fake_step_tid != tsp->tts_lwpid)
2316 warning ("Internal error in stepping over breakpoint.");
2318 /* This wait clears the (current) fake-step if there was one.
2320 doing_fake_step = 0;
2324 /* We now have a correct tsp and ttw_status for the thread
2325 * which we want to report. So it's "handled"! This call
2326 * will add it to our list if it's not there already.
2328 set_handled (real_pid, tsp->tts_lwpid);
2330 /* Save a copy of the ttrace state of this thread, in our local
2333 This caches the state. The implementation of queries like
2334 target_has_execd can then use this cached state, rather than
2335 be forced to make an explicit ttrace call to get it.
2337 (Guard against the condition that this is the first time we've
2338 waited on, i.e., seen this thread, and so haven't yet entered
2339 it into our list of threads.)
2341 tinfo = find_thread_info (tsp->tts_lwpid);
2344 copy_ttstate_t (&tinfo->last_stop_state, tsp);
2345 tinfo->have_state = 1;
2349 } /* call_ttrace_wait */
2351 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2353 child_reported_exec_events_per_exec_call ()
2355 return 1; /* ttrace reports the event once per call. */
2361 /* Our implementation of hardware watchpoints involves making memory
2362 pages write-protected. We must remember a page's original permissions,
2363 and we must also know when it is appropriate to restore a page's
2364 permissions to its original state.
2366 We use a "dictionary" of hardware-watched pages to do this. Each
2367 hardware-watched page is recorded in the dictionary. Each page's
2368 dictionary entry contains the original permissions and a reference
2369 count. Pages are hashed into the dictionary by their start address.
2371 When hardware watchpoint is set on page X for the first time, page X
2372 is added to the dictionary with a reference count of 1. If other
2373 hardware watchpoints are subsequently set on page X, its reference
2374 count is incremented. When hardware watchpoints are removed from
2375 page X, its reference count is decremented. If a page's reference
2376 count drops to 0, it's permissions are restored and the page's entry
2377 is thrown out of the dictionary.
2379 typedef struct memory_page
2381 CORE_ADDR page_start;
2382 int reference_count;
2383 int original_permissions;
2384 struct memory_page *next;
2385 struct memory_page *previous;
2389 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2395 int page_protections_allowed;
2396 /* These are just the heads of chains of actual page descriptors. */
2397 memory_page_t buckets[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT];
2399 memory_page_dictionary;
2403 require_memory_page_dictionary ()
2407 /* Is the memory page dictionary ready for use? If so, we're done. */
2408 if (memory_page_dictionary.page_count >= (LONGEST) 0)
2411 /* Else, initialize it. */
2412 memory_page_dictionary.page_count = (LONGEST) 0;
2414 for (i = 0; i < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++)
2416 memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0;
2417 memory_page_dictionary.buckets[i].reference_count = 0;
2418 memory_page_dictionary.buckets[i].next = NULL;
2419 memory_page_dictionary.buckets[i].previous = NULL;
2425 retire_memory_page_dictionary ()
2427 memory_page_dictionary.page_count = (LONGEST) - 1;
2431 /* Write-protect the memory page that starts at this address.
2433 Returns the original permissions of the page.
2436 write_protect_page (pid, page_start)
2438 CORE_ADDR page_start;
2441 int original_permissions;
2442 int new_permissions;
2444 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
2446 (TTRACE_ARG_TYPE) page_start,
2448 (TTRACE_ARG_TYPE) & original_permissions);
2449 if (errno || (tt_status < 0))
2451 return 0; /* What else can we do? */
2454 /* We'll also write-protect the page now, if that's allowed. */
2455 if (memory_page_dictionary.page_protections_allowed)
2457 new_permissions = original_permissions & ~PROT_WRITE;
2458 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2460 (TTRACE_ARG_TYPE) page_start,
2461 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2462 (TTRACE_ARG_TYPE) new_permissions);
2463 if (errno || (tt_status < 0))
2465 return 0; /* What else can we do? */
2469 return original_permissions;
2473 /* Unwrite-protect the memory page that starts at this address, restoring
2474 (what we must assume are) its original permissions.
2477 unwrite_protect_page (pid, page_start, original_permissions)
2479 CORE_ADDR page_start;
2480 int original_permissions;
2484 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2486 (TTRACE_ARG_TYPE) page_start,
2487 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2488 (TTRACE_ARG_TYPE) original_permissions);
2489 if (errno || (tt_status < 0))
2491 return; /* What else can we do? */
2496 /* Memory page-protections are used to implement "hardware" watchpoints
2499 For every memory page that is currently being watched (i.e., that
2500 presently should be write-protected), write-protect it.
2503 hppa_enable_page_protection_events (pid)
2508 memory_page_dictionary.page_protections_allowed = 1;
2510 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2512 memory_page_t *page;
2514 page = memory_page_dictionary.buckets[bucket].next;
2515 while (page != NULL)
2517 page->original_permissions = write_protect_page (pid, page->page_start);
2524 /* Memory page-protections are used to implement "hardware" watchpoints
2527 For every memory page that is currently being watched (i.e., that
2528 presently is or should be write-protected), un-write-protect it.
2531 hppa_disable_page_protection_events (pid)
2536 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2538 memory_page_t *page;
2540 page = memory_page_dictionary.buckets[bucket].next;
2541 while (page != NULL)
2543 unwrite_protect_page (pid, page->page_start, page->original_permissions);
2548 memory_page_dictionary.page_protections_allowed = 0;
2551 /* Count the number of outstanding events. At this
2552 * point, we have selected one thread and its event
2553 * as the one to be "reported" upwards to core gdb.
2554 * That thread is already marked as "handled".
2556 * Note: we could just scan our own thread list. FIXME!
2559 count_unhandled_events (real_pid, real_tid)
2567 /* Ok, find out how many threads have real events to report.
2570 ttid = get_process_first_stopped_thread_id (real_pid, &tstate);
2576 printf ("Process %d has no threads\n", real_pid);
2578 printf ("Process %d has these threads:\n", real_pid);
2584 if (tstate.tts_event != TTEVT_NONE
2585 && !was_handled (ttid))
2587 /* TTEVT_NONE implies we just stopped it ourselves
2588 * because we're the stop-the-world guys, so it's
2589 * not an event from our point of view.
2591 * If "was_handled" is true, this is an event we
2592 * already handled, so don't count it.
2594 * Note that we don't count the thread with the
2595 * currently-reported event, as it's already marked
2601 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2604 if (ttid == real_tid)
2605 printf ("*"); /* Thread we're reporting */
2609 if (tstate.tts_event != TTEVT_NONE)
2610 printf ("+"); /* Thread with a real event */
2614 if (was_handled (ttid))
2615 printf ("h"); /* Thread has been handled */
2619 printf (" %d, with event %s", ttid,
2620 get_printable_name_of_ttrace_event (tstate.tts_event));
2622 if (tstate.tts_event == TTEVT_SIGNAL
2623 && 5 == tstate.tts_u.tts_signal.tts_signo)
2627 pc_val = get_raw_pc (ttid);
2630 printf (" breakpoint at 0x%x\n", pc_val);
2632 printf (" bpt, can't fetch pc.\n");
2639 ttid = get_process_next_stopped_thread_id (real_pid, &tstate);
2642 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2644 if (events_left > 0)
2645 printf ("There are thus %d pending events\n", events_left);
2651 /* This function is provided as a sop to clients that are calling
2652 * ptrace_wait to wait for a process to stop. (see the
2653 * implementation of child_wait.) Return value is the pid for
2654 * the event that ended the wait.
2656 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2659 ptrace_wait (pid, status)
2670 /* The ptrace implementation of this also ignores pid.
2674 ttwait_return = call_ttrace_wait (0, TTRACE_WAITOK, &tsp, sizeof (tsp));
2675 if (ttwait_return < 0)
2677 /* ??rehrauer: It appears that if our inferior exits and we
2678 haven't asked for exit events, that we're not getting any
2679 indication save a negative return from ttrace_wait and an
2684 *status = 0; /* WIFEXITED */
2685 return inferior_pid;
2688 warning ("Call of ttrace_wait returned with errno %d.",
2690 *status = ttwait_return;
2691 return inferior_pid;
2694 real_pid = tsp.tts_pid;
2695 real_tid = tsp.tts_lwpid;
2697 /* One complication is that the "tts_event" structure has
2698 * a set of flags, and more than one can be set. So we
2699 * either have to force an order (as we do here), or handle
2700 * more than one flag at a time.
2702 if (tsp.tts_event & TTEVT_LWP_CREATE)
2705 /* Unlike what you might expect, this event is reported in
2706 * the _creating_ thread, and the _created_ thread (whose tid
2707 * we have) is still running. So we have to stop it. This
2708 * has already been done in "call_ttrace_wait", but should we
2709 * ever abandon the "stop-the-world" model, here's the command
2712 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2714 * Note that this would depend on being called _after_ "add_tthread"
2715 * below for the tid-to-pid translation to be done in "call_ttrace".
2720 printf ("New thread: pid %d, tid %d, creator tid %d\n",
2721 real_pid, tsp.tts_u.tts_thread.tts_target_lwpid,
2725 /* Now we have to return the tid of the created thread, not
2726 * the creating thread, or "wait_for_inferior" won't know we
2727 * have a new "process" (thread). Plus we should record it
2730 real_tid = tsp.tts_u.tts_thread.tts_target_lwpid;
2732 add_tthread (real_pid, real_tid);
2735 else if ((tsp.tts_event & TTEVT_LWP_TERMINATE)
2736 || (tsp.tts_event & TTEVT_LWP_EXIT))
2741 printf ("Thread dies: %d\n", real_tid);
2744 del_tthread (real_tid);
2747 else if (tsp.tts_event & TTEVT_EXEC)
2752 printf ("Pid %d has zero'th thread %d; inferior pid is %d\n",
2753 real_pid, real_tid, inferior_pid);
2756 add_tthread (real_pid, real_tid);
2762 printf ("Process-level event %s, using tid %d\n",
2763 get_printable_name_of_ttrace_event (tsp.tts_event),
2766 /* OK to do this, as "add_tthread" won't add
2767 * duplicate entries. Also OK not to do it,
2768 * as this event isn't one which can change the
2771 add_tthread (real_pid, real_tid);
2776 /* How many events are left to report later?
2777 * In a non-stop-the-world model, this isn't needed.
2779 * Note that it's not always safe to query the thread state of a process,
2780 * which is what count_unhandled_events does. (If unsafe, we're left with
2781 * no other resort than to assume that no more events remain...)
2783 if (can_touch_threads_of_process (real_pid, tsp.tts_event))
2784 more_events_left = count_unhandled_events (real_pid, real_tid);
2788 if (more_events_left > 0)
2789 warning ("Vfork or fork causing loss of %d buffered events.",
2792 more_events_left = 0;
2795 /* Attempt to translate the ttrace_wait-returned status into the
2798 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2799 clients that expect to pick apart a ptrace wait status, to use
2800 something a little more abstract.
2802 if ((tsp.tts_event & TTEVT_EXEC)
2803 || (tsp.tts_event & TTEVT_FORK)
2804 || (tsp.tts_event & TTEVT_VFORK))
2806 /* Forks come in pairs (parent and child), so core gdb
2807 * will do two waits. Be ready to notice this.
2809 if (tsp.tts_event & TTEVT_FORK)
2811 process_state = FORKING;
2813 #ifdef WAIT_BUFFER_DEBUG
2815 printf ("Process set to FORKING\n");
2818 else if (tsp.tts_event & TTEVT_VFORK)
2820 process_state = VFORKING;
2822 #ifdef WAIT_BUFFER_DEBUG
2824 printf ("Process set to VFORKING\n");
2828 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2829 but I don't think non HP-UX-specific clients really carefully
2830 inspect the first events they get after inferior startup, so
2831 it probably almost doesn't matter what we claim this is.
2836 printf ("..a process 'event'\n");
2839 /* Also make fork and exec events look like bpts, so they can be caught.
2841 *status = 0177 | (_SIGTRAP << 8);
2844 /* Special-cases: We ask for syscall entry and exit events to implement
2845 "fast" (aka "hardware") watchpoints.
2847 When we get a syscall entry, we want to disable page-protections,
2848 and resume the inferior; this isn't an event we wish for
2849 wait_for_inferior to see. Note that we must resume ONLY the
2850 thread that reported the syscall entry; we don't want to allow
2851 other threads to run with the page protections off, as they might
2852 then be able to write to watch memory without it being caught.
2854 When we get a syscall exit, we want to reenable page-protections,
2855 but we don't want to resume the inferior; this is an event we wish
2856 wait_for_inferior to see. Make it look like the signal we normally
2857 get for a single-step completion. This should cause wait_for_inferior
2858 to evaluate whether any watchpoint triggered.
2860 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2861 due to some HP-UX "features". Some syscalls have problems with
2862 write-protections on some pages, and some syscalls seem to have
2863 pending writes to those pages at the time we're getting the return
2864 event. So, we'll single-step the inferior to get out of the syscall,
2865 and then reenable protections.
2867 Note that we're intentionally allowing the syscall exit case to
2868 fall through into the succeeding cases, as sometimes we single-
2869 step out of one syscall only to immediately enter another...
2871 else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY)
2872 || (tsp.tts_event & TTEVT_SYSCALL_RETURN))
2874 /* Make a syscall event look like a breakpoint. Same comments
2875 as for exec & fork events.
2879 printf ("..a syscall 'event'\n");
2882 /* Also make syscall events look like bpts, so they can be caught.
2884 *status = 0177 | (_SIGTRAP << 8);
2887 else if ((tsp.tts_event & TTEVT_LWP_CREATE)
2888 || (tsp.tts_event & TTEVT_LWP_TERMINATE)
2889 || (tsp.tts_event & TTEVT_LWP_EXIT))
2891 /* Make a thread event look like a breakpoint. Same comments
2892 * as for exec & fork events.
2896 printf ("..a thread 'event'\n");
2899 /* Also make thread events look like bpts, so they can be caught.
2901 *status = 0177 | (_SIGTRAP << 8);
2904 else if ((tsp.tts_event & TTEVT_EXIT))
2909 printf ("..an exit\n");
2912 /* Prevent rest of gdb from thinking this is
2913 * a new thread if for some reason it's never
2914 * seen the main thread before.
2916 inferior_pid = map_to_gdb_tid (real_tid); /* HACK, FIX */
2918 *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode);
2921 else if (tsp.tts_event & TTEVT_SIGNAL)
2925 printf ("..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo);
2928 *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8);
2934 /* This means the process or thread terminated. But we should've
2935 caught an explicit exit/termination above. So warn (this is
2936 really an internal error) and claim the process or thread
2937 terminated with a SIGTRAP.
2940 warning ("process_wait: unknown process state");
2944 printf ("Process-level event %s, using tid %d\n",
2945 get_printable_name_of_ttrace_event (tsp.tts_event),
2952 target_post_wait (tsp.tts_pid, *status);
2957 printf ("Done waiting, pid is %d, tid %d\n", real_pid, real_tid);
2960 /* All code external to this module uses the tid, but calls
2961 * it "pid". There's some tweaking so that the outside sees
2962 * the first thread as having the same number as the starting
2965 return_pid = map_to_gdb_tid (real_tid);
2967 /* Remember this for later use in "hppa_prepare_to_proceed".
2969 old_gdb_pid = inferior_pid;
2970 reported_pid = return_pid;
2971 reported_bpt = ((tsp.tts_event & TTEVT_SIGNAL) && (5 == tsp.tts_u.tts_signal.tts_signo));
2973 if (real_tid == 0 || return_pid == 0)
2975 warning ("Internal error: process-wait failed.");
2982 /* This function causes the caller's process to be traced by its
2983 parent. This is intended to be called after GDB forks itself,
2984 and before the child execs the target. Despite the name, it
2985 is called by the child.
2987 Note that HP-UX ttrace is rather funky in how this is done.
2988 If the parent wants to get the initial exec event of a child,
2989 it must set the ttrace event mask of the child to include execs.
2990 (The child cannot do this itself.) This must be done after the
2991 child is forked, but before it execs.
2993 To coordinate the parent and child, we implement a semaphore using
2994 pipes. After SETTRC'ing itself, the child tells the parent that
2995 it is now traceable by the parent, and waits for the parent's
2996 acknowledgement. The parent can then set the child's event mask,
2997 and notify the child that it can now exec.
2999 (The acknowledgement by parent happens as a result of a call to
3000 child_acknowledge_created_inferior.)
3003 parent_attach_all ()
3007 /* We need a memory home for a constant, to pass it to ttrace.
3008 The value of the constant is arbitrary, so long as both
3009 parent and child use the same value. Might as well use the
3010 "magic" constant provided by ttrace...
3012 uint64_t tc_magic_child = TT_VERSION;
3013 uint64_t tc_magic_parent = 0;
3015 tt_status = call_real_ttrace (
3020 (TTRACE_ARG_TYPE) TT_VERSION,
3026 /* Notify the parent that we're potentially ready to exec(). */
3027 write (startup_semaphore.child_channel[SEM_TALK],
3029 sizeof (tc_magic_child));
3031 /* Wait for acknowledgement from the parent. */
3032 read (startup_semaphore.parent_channel[SEM_LISTEN],
3034 sizeof (tc_magic_parent));
3036 if (tc_magic_child != tc_magic_parent)
3037 warning ("mismatched semaphore magic");
3039 /* Discard our copy of the semaphore. */
3040 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3041 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3042 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3043 (void) close (startup_semaphore.child_channel[SEM_TALK]);
3048 /* Despite being file-local, this routine is dealing with
3049 * actual process IDs, not thread ids. That's because it's
3050 * called before the first "wait" call, and there's no map
3051 * yet from tids to pids.
3053 * When it is called, a forked child is running, but waiting on
3054 * the semaphore. If you stop the child and re-start it,
3055 * things get confused, so don't do that! An attached child is
3058 * Since this is called after either attach or run, we
3059 * have to be the common part of both.
3062 require_notification_of_events (real_pid)
3066 ttevent_t notifiable_events;
3069 ttstate_t thread_state;
3073 printf ("Require notif, pid is %d\n", real_pid);
3076 /* Temporary HACK: tell inftarg.c/child_wait to not
3077 * loop until pids are the same.
3079 not_same_real_pid = 0;
3081 sigemptyset (¬ifiable_events.tte_signals);
3082 notifiable_events.tte_opts = TTEO_NONE;
3084 /* This ensures that forked children inherit their parent's
3085 * event mask, which we're setting here.
3087 * NOTE: if you debug gdb with itself, then the ultimate
3088 * debuggee gets flags set by the outermost gdb, as
3089 * a child of a child will still inherit.
3091 notifiable_events.tte_opts |= TTEO_PROC_INHERIT;
3093 notifiable_events.tte_events = TTEVT_DEFAULT;
3094 notifiable_events.tte_events |= TTEVT_SIGNAL;
3095 notifiable_events.tte_events |= TTEVT_EXEC;
3096 notifiable_events.tte_events |= TTEVT_EXIT;
3097 notifiable_events.tte_events |= TTEVT_FORK;
3098 notifiable_events.tte_events |= TTEVT_VFORK;
3099 notifiable_events.tte_events |= TTEVT_LWP_CREATE;
3100 notifiable_events.tte_events |= TTEVT_LWP_EXIT;
3101 notifiable_events.tte_events |= TTEVT_LWP_TERMINATE;
3103 tt_status = call_real_ttrace (
3104 TT_PROC_SET_EVENT_MASK,
3107 (TTRACE_ARG_TYPE) & notifiable_events,
3108 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3113 require_notification_of_exec_events (real_pid)
3117 ttevent_t notifiable_events;
3120 ttstate_t thread_state;
3124 printf ("Require notif, pid is %d\n", real_pid);
3127 /* Temporary HACK: tell inftarg.c/child_wait to not
3128 * loop until pids are the same.
3130 not_same_real_pid = 0;
3132 sigemptyset (¬ifiable_events.tte_signals);
3133 notifiable_events.tte_opts = TTEO_NOSTRCCHLD;
3135 /* This ensures that forked children don't inherit their parent's
3136 * event mask, which we're setting here.
3138 notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT;
3140 notifiable_events.tte_events = TTEVT_DEFAULT;
3141 notifiable_events.tte_events |= TTEVT_EXEC;
3142 notifiable_events.tte_events |= TTEVT_EXIT;
3144 tt_status = call_real_ttrace (
3145 TT_PROC_SET_EVENT_MASK,
3148 (TTRACE_ARG_TYPE) & notifiable_events,
3149 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3154 /* This function is called by the parent process, with pid being the
3155 * ID of the child process, after the debugger has forked.
3158 child_acknowledge_created_inferior (pid)
3161 /* We need a memory home for a constant, to pass it to ttrace.
3162 The value of the constant is arbitrary, so long as both
3163 parent and child use the same value. Might as well use the
3164 "magic" constant provided by ttrace...
3166 uint64_t tc_magic_parent = TT_VERSION;
3167 uint64_t tc_magic_child = 0;
3169 /* Wait for the child to tell us that it has forked. */
3170 read (startup_semaphore.child_channel[SEM_LISTEN],
3172 sizeof (tc_magic_child));
3174 /* Clear thread info now. We'd like to do this in
3175 * "require...", but that messes up attach.
3177 clear_thread_info ();
3179 /* Tell the "rest of gdb" that the initial thread exists.
3180 * This isn't really a hack. Other thread-based versions
3181 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3183 * Q: Why don't we also add this thread to the local
3184 * list via "add_tthread"?
3186 * A: Because we don't know the tid, and can't stop the
3187 * the process safely to ask what it is. Anyway, we'll
3188 * add it when it gets the EXEC event.
3190 add_thread (pid); /* in thread.c */
3192 /* We can now set the child's ttrace event mask.
3194 require_notification_of_exec_events (pid);
3196 /* Tell ourselves that the process is running.
3198 process_state = RUNNING;
3200 /* Notify the child that it can exec. */
3201 write (startup_semaphore.parent_channel[SEM_TALK],
3203 sizeof (tc_magic_parent));
3205 /* Discard our copy of the semaphore. */
3206 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3207 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3208 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3209 (void) close (startup_semaphore.child_channel[SEM_TALK]);
3214 * arrange for notification of all events by
3215 * calling require_notification_of_events.
3218 child_post_startup_inferior (real_pid)
3221 require_notification_of_events (real_pid);
3224 /* From here on, we should expect tids rather than pids.
3227 hppa_enable_catch_fork (tid)
3231 ttevent_t ttrace_events;
3233 /* Get the set of events that are currently enabled.
3235 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3237 (TTRACE_ARG_TYPE) & ttrace_events,
3238 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3241 perror_with_name ("ttrace");
3243 /* Add forks to that set. */
3244 ttrace_events.tte_events |= TTEVT_FORK;
3248 printf ("enable fork, tid is %d\n", tid);
3251 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3253 (TTRACE_ARG_TYPE) & ttrace_events,
3254 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3257 perror_with_name ("ttrace");
3262 hppa_disable_catch_fork (tid)
3266 ttevent_t ttrace_events;
3268 /* Get the set of events that are currently enabled.
3270 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3272 (TTRACE_ARG_TYPE) & ttrace_events,
3273 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3277 perror_with_name ("ttrace");
3279 /* Remove forks from that set. */
3280 ttrace_events.tte_events &= ~TTEVT_FORK;
3284 printf ("disable fork, tid is %d\n", tid);
3287 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3289 (TTRACE_ARG_TYPE) & ttrace_events,
3290 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3294 perror_with_name ("ttrace");
3298 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3300 child_insert_fork_catchpoint (tid)
3303 /* Enable reporting of fork events from the kernel. */
3304 /* ??rehrauer: For the moment, we're always enabling these events,
3305 and just ignoring them if there's no catchpoint to catch them.
3312 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3314 child_remove_fork_catchpoint (tid)
3317 /* Disable reporting of fork events from the kernel. */
3318 /* ??rehrauer: For the moment, we're always enabling these events,
3319 and just ignoring them if there's no catchpoint to catch them.
3327 hppa_enable_catch_vfork (tid)
3331 ttevent_t ttrace_events;
3333 /* Get the set of events that are currently enabled.
3335 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3337 (TTRACE_ARG_TYPE) & ttrace_events,
3338 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3342 perror_with_name ("ttrace");
3344 /* Add vforks to that set. */
3345 ttrace_events.tte_events |= TTEVT_VFORK;
3349 printf ("enable vfork, tid is %d\n", tid);
3352 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3354 (TTRACE_ARG_TYPE) & ttrace_events,
3355 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3359 perror_with_name ("ttrace");
3364 hppa_disable_catch_vfork (tid)
3368 ttevent_t ttrace_events;
3370 /* Get the set of events that are currently enabled. */
3371 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3373 (TTRACE_ARG_TYPE) & ttrace_events,
3374 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3378 perror_with_name ("ttrace");
3380 /* Remove vforks from that set. */
3381 ttrace_events.tte_events &= ~TTEVT_VFORK;
3385 printf ("disable vfork, tid is %d\n", tid);
3387 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3389 (TTRACE_ARG_TYPE) & ttrace_events,
3390 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3394 perror_with_name ("ttrace");
3398 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3400 child_insert_vfork_catchpoint (tid)
3403 /* Enable reporting of vfork events from the kernel. */
3404 /* ??rehrauer: For the moment, we're always enabling these events,
3405 and just ignoring them if there's no catchpoint to catch them.
3412 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3414 child_remove_vfork_catchpoint (tid)
3417 /* Disable reporting of vfork events from the kernel. */
3418 /* ??rehrauer: For the moment, we're always enabling these events,
3419 and just ignoring them if there's no catchpoint to catch them.
3425 #if defined(CHILD_HAS_FORKED)
3427 /* Q: Do we need to map the returned process ID to a thread ID?
3429 * A: I don't think so--here we want a _real_ pid. Any later
3430 * operations will call "require_notification_of_events" and
3431 * start the mapping.
3434 child_has_forked (tid, childpid)
3439 ttstate_t ttrace_state;
3442 /* Do we have cached thread state that we can consult? If so, use it. */
3443 tinfo = find_thread_info (map_from_gdb_tid (tid));
3446 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3449 /* Nope, must read the thread's current state */
3452 tt_status = call_ttrace (TT_LWP_GET_STATE,
3454 (TTRACE_ARG_TYPE) & ttrace_state,
3455 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3459 perror_with_name ("ttrace");
3465 if (ttrace_state.tts_event & TTEVT_FORK)
3467 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3476 #if defined(CHILD_HAS_VFORKED)
3478 /* See child_has_forked for pid discussion.
3481 child_has_vforked (tid, childpid)
3486 ttstate_t ttrace_state;
3489 /* Do we have cached thread state that we can consult? If so, use it. */
3490 tinfo = find_thread_info (map_from_gdb_tid (tid));
3492 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3494 /* Nope, must read the thread's current state */
3497 tt_status = call_ttrace (TT_LWP_GET_STATE,
3499 (TTRACE_ARG_TYPE) & ttrace_state,
3500 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3504 perror_with_name ("ttrace");
3510 if (ttrace_state.tts_event & TTEVT_VFORK)
3512 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3521 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3523 child_can_follow_vfork_prior_to_exec ()
3525 /* ttrace does allow this.
3527 ??rehrauer: However, I had major-league problems trying to
3528 convince wait_for_inferior to handle that case. Perhaps when
3529 it is rewritten to grok multiple processes in an explicit way...
3536 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3538 child_insert_exec_catchpoint (tid)
3541 /* Enable reporting of exec events from the kernel. */
3542 /* ??rehrauer: For the moment, we're always enabling these events,
3543 and just ignoring them if there's no catchpoint to catch them.
3550 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3552 child_remove_exec_catchpoint (tid)
3555 /* Disable reporting of execevents from the kernel. */
3556 /* ??rehrauer: For the moment, we're always enabling these events,
3557 and just ignoring them if there's no catchpoint to catch them.
3564 #if defined(CHILD_HAS_EXECD)
3566 child_has_execd (tid, execd_pathname)
3568 char **execd_pathname;
3571 ttstate_t ttrace_state;
3574 /* Do we have cached thread state that we can consult? If so, use it. */
3575 tinfo = find_thread_info (map_from_gdb_tid (tid));
3577 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3579 /* Nope, must read the thread's current state */
3582 tt_status = call_ttrace (TT_LWP_GET_STATE,
3584 (TTRACE_ARG_TYPE) & ttrace_state,
3585 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3589 perror_with_name ("ttrace");
3595 if (ttrace_state.tts_event & TTEVT_EXEC)
3597 /* See child_pid_to_exec_file in this file: this is a macro.
3599 char *exec_file = target_pid_to_exec_file (tid);
3601 *execd_pathname = savestring (exec_file, strlen (exec_file));
3610 #if defined(CHILD_HAS_SYSCALL_EVENT)
3612 child_has_syscall_event (pid, kind, syscall_id)
3614 enum target_waitkind *kind;
3618 ttstate_t ttrace_state;
3621 /* Do we have cached thread state that we can consult? If so, use it. */
3622 tinfo = find_thread_info (map_from_gdb_tid (pid));
3624 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3626 /* Nope, must read the thread's current state */
3629 tt_status = call_ttrace (TT_LWP_GET_STATE,
3631 (TTRACE_ARG_TYPE) & ttrace_state,
3632 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3636 perror_with_name ("ttrace");
3642 *kind = TARGET_WAITKIND_SPURIOUS; /* Until proven otherwise... */
3645 if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY)
3646 *kind = TARGET_WAITKIND_SYSCALL_ENTRY;
3647 else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN)
3648 *kind = TARGET_WAITKIND_SYSCALL_RETURN;
3652 *syscall_id = ttrace_state.tts_scno;
3659 #if defined(CHILD_THREAD_ALIVE)
3661 /* Check to see if the given thread is alive.
3663 * We'll trust the thread list, as the more correct
3664 * approach of stopping the process and spinning down
3665 * the OS's thread list is _very_ expensive.
3667 * May need a FIXME for that reason.
3670 child_thread_alive (gdb_tid)
3675 /* This spins down the lists twice.
3676 * Possible peformance improvement here!
3678 tid = map_from_gdb_tid (gdb_tid);
3679 return !is_terminated (tid);
3686 /* This function attempts to read the specified number of bytes from the
3687 save_state_t that is our view into the hardware registers, starting at
3688 ss_offset, and ending at ss_offset + sizeof_buf - 1
3690 If this function succeeds, it deposits the fetched bytes into buf,
3693 If it fails, it returns a negative result. The contents of buf are
3694 undefined it this function fails.
3697 read_from_register_save_state (tid, ss_offset, buf, sizeof_buf)
3699 TTRACE_ARG_TYPE ss_offset;
3704 register_value_t register_value = 0;
3706 tt_status = call_ttrace (TT_LWP_RUREGS,
3709 (TTRACE_ARG_TYPE) sizeof_buf,
3710 (TTRACE_ARG_TYPE) buf);
3713 /* Map ttrace's version of success to our version.
3714 * Sometime ttrace returns 0, but that's ok here.
3722 /* This function attempts to write the specified number of bytes to the
3723 save_state_t that is our view into the hardware registers, starting at
3724 ss_offset, and ending at ss_offset + sizeof_buf - 1
3726 If this function succeeds, it deposits the bytes in buf, and returns 0.
3728 If it fails, it returns a negative result. The contents of the save_state_t
3729 are undefined it this function fails.
3732 write_to_register_save_state (tid, ss_offset, buf, sizeof_buf)
3734 TTRACE_ARG_TYPE ss_offset;
3739 register_value_t register_value = 0;
3741 tt_status = call_ttrace (TT_LWP_WUREGS,
3744 (TTRACE_ARG_TYPE) sizeof_buf,
3745 (TTRACE_ARG_TYPE) buf);
3750 /* This function is a sop to the largeish number of direct calls
3751 to call_ptrace that exist in other files. Rather than create
3752 functions whose name abstracts away from ptrace, and change all
3753 the present callers of call_ptrace, we'll do the expedient (and
3754 perhaps only practical) thing.
3756 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3757 process. Thus, we must translate all ptrace requests into their
3758 process-specific, ttrace equivalents.
3761 call_ptrace (pt_request, gdb_tid, addr, data)
3764 PTRACE_ARG3_TYPE addr;
3768 TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr;
3769 TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data;
3770 TTRACE_ARG_TYPE tt_addr2 = TT_NIL;
3772 register_value_t register_value;
3775 /* Perform the necessary argument translation. Note that some
3776 cases are funky enough in the ttrace realm that we handle them
3781 /* The following cases cannot conveniently be handled conveniently
3782 by merely adjusting the ptrace arguments and feeding into the
3783 generic call to ttrace at the bottom of this function.
3785 Note that because all branches of this switch end in "return",
3786 there's no need for any "break" statements.
3789 return parent_attach_all ();
3792 tt_status = read_from_register_save_state (gdb_tid,
3795 sizeof (register_value));
3798 return register_value;
3801 register_value = (int) tt_data;
3802 tt_status = write_to_register_save_state (gdb_tid,
3805 sizeof (register_value));
3810 tt_status = call_ttrace (TT_PROC_RDTEXT, /* Implicit 4-byte xfer becomes block-xfer. */
3813 (TTRACE_ARG_TYPE) 4,
3814 (TTRACE_ARG_TYPE) & read_buf);
3820 tt_status = call_ttrace (TT_PROC_RDDATA, /* Implicit 4-byte xfer becomes block-xfer. */
3823 (TTRACE_ARG_TYPE) 4,
3824 (TTRACE_ARG_TYPE) & read_buf);
3830 tt_status = call_real_ttrace (TT_PROC_ATTACH,
3831 map_from_gdb_tid (gdb_tid),
3834 (TTRACE_ARG_TYPE) TT_VERSION,
3840 /* The following cases are handled by merely adjusting the ptrace
3841 arguments and feeding into the generic call to ttrace.
3844 tt_request = TT_PROC_DETACH;
3848 tt_request = TT_PROC_WRTEXT; /* Translates 4-byte xfer to block-xfer. */
3849 tt_data = 4; /* This many bytes. */
3850 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3854 tt_request = TT_PROC_WRDATA; /* Translates 4-byte xfer to block-xfer. */
3855 tt_data = 4; /* This many bytes. */
3856 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3860 tt_request = TT_PROC_RDTEXT;
3864 tt_request = TT_PROC_RDDATA;
3868 tt_request = TT_PROC_WRTEXT;
3872 tt_request = TT_PROC_WRDATA;
3876 tt_request = TT_PROC_CONTINUE;
3880 tt_request = TT_LWP_SINGLE; /* Should not be making this request? */
3884 tt_request = TT_PROC_EXIT;
3887 case PT_GET_PROCESS_PATHNAME:
3888 tt_request = TT_PROC_GET_PATHNAME;
3892 tt_request = pt_request; /* Let ttrace be the one to complain. */
3896 return call_ttrace (tt_request,
3903 /* Kill that pesky process!
3911 thread_info **paranoia;
3914 if (inferior_pid == 0)
3917 /* Walk the list of "threads", some of which are "pseudo threads",
3918 aka "processes". For each that is NOT inferior_pid, stop it,
3921 You see, we may not have just a single process to kill. If we're
3922 restarting or quitting or detaching just after the inferior has
3923 forked, then we've actually two processes to clean up.
3925 But we can't just call target_mourn_inferior() for each, since that
3926 zaps the target vector.
3929 paranoia = (thread_info **) malloc (thread_head.count *
3930 sizeof (thread_info *));
3933 t = thread_head.head;
3937 paranoia[para_count] = t;
3938 for (i = 0; i < para_count; i++)
3940 if (t->next == paranoia[i])
3942 warning ("Bad data in gdb's thread data; repairing.");
3948 if (t->am_pseudo && (t->pid != inferior_pid))
3950 /* TT_PROC_STOP doesn't require a subsequent ttrace_wait, as it
3951 * generates no event.
3953 call_ttrace (TT_PROC_STOP,
3959 call_ttrace (TT_PROC_DETACH,
3962 (TTRACE_ARG_TYPE) TARGET_SIGNAL_0,
3970 call_ttrace (TT_PROC_STOP,
3975 target_mourn_inferior ();
3976 clear_thread_info ();
3980 #ifndef CHILD_RESUME
3982 /* Sanity check a thread about to be continued.
3985 thread_dropping_event_check (p)
3991 * This seems to happen when we "next" over a
3992 * "fork()" while following the parent. If it's
3993 * the FORK event, that's ok. If it's a SIGNAL
3994 * in the unfollowed child, that's ok to--but
3995 * how can we know that's what's going on?
4001 if (p->last_stop_state.tts_event == TTEVT_FORK)
4006 else if (p->last_stop_state.tts_event == TTEVT_SIGNAL)
4008 /* Ok, close eyes and let it happen.
4014 /* This shouldn't happen--we're dropping a
4017 warning ("About to continue process %d, thread %d with unhandled event %s.",
4019 get_printable_name_of_ttrace_event (
4020 p->last_stop_state.tts_event));
4030 /* No saved state, have to assume it failed.
4032 warning ("About to continue process %d, thread %d with unhandled event.",
4041 } /* thread_dropping_event_check */
4043 /* Use a loop over the threads to continue all the threads but
4044 * the one specified, which is to be stepped.
4047 threads_continue_all_but_one (gdb_tid, signal)
4060 printf ("Using loop over threads to step/resume with signals\n");
4063 /* First update the thread list.
4066 real_tid = map_from_gdb_tid (gdb_tid);
4067 real_pid = get_pid_for (real_tid);
4069 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
4070 while (0 != scan_tid)
4074 /* FIX: later should check state is stopped;
4075 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
4078 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
4079 printf ("About to continue non-stopped thread %d\n", scan_tid);
4082 p = find_thread_info (scan_tid);
4085 add_tthread (real_pid, scan_tid);
4086 p = find_thread_info (scan_tid);
4088 /* This is either a newly-created thread or the
4089 * result of a fork; in either case there's no
4090 * actual event to worry about.
4094 if (state.tts_event != TTEVT_NONE)
4096 /* Oops, do need to worry!
4098 warning ("Unexpected thread with \"%s\" event.",
4099 get_printable_name_of_ttrace_event (state.tts_event));
4102 else if (scan_tid != p->tid)
4103 error ("Bad data in thread database.");
4108 printf ("Why are we continuing a dead thread?\n");
4113 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4116 /* Remove unseen threads.
4118 update_thread_list ();
4120 /* Now run down the thread list and continue or step.
4122 for (p = thread_head.head; p; p = p->next)
4127 thread_dropping_event_check (p);
4129 /* Pass the correct signals along.
4133 thread_signal = p->signal_value;
4139 if (p->tid != real_tid)
4142 * Not the thread of interest, so continue it
4143 * as the user expects.
4145 if (p->stepping_mode == DO_STEP)
4147 /* Just step this thread.
4153 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4158 /* Regular continue (default case).
4164 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4170 /* Step the thread of interest.
4176 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4179 } /* Loop over threads */
4180 } /* End threads_continue_all_but_one */
4182 /* Use a loop over the threads to continue all the threads.
4183 * This is done when a signal must be sent to any of the threads.
4186 threads_continue_all_with_signals (gdb_tid, signal)
4199 printf ("Using loop over threads to resume with signals\n");
4202 /* Scan and update thread list.
4205 real_tid = map_from_gdb_tid (gdb_tid);
4206 real_pid = get_pid_for (real_tid);
4208 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
4209 while (0 != scan_tid)
4214 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
4215 warning ("About to continue non-stopped thread %d\n", scan_tid);
4218 p = find_thread_info (scan_tid);
4221 add_tthread (real_pid, scan_tid);
4222 p = find_thread_info (scan_tid);
4224 /* This is either a newly-created thread or the
4225 * result of a fork; in either case there's no
4226 * actual event to worry about.
4230 if (state.tts_event != TTEVT_NONE)
4232 /* Oops, do need to worry!
4234 warning ("Unexpected thread with \"%s\" event.",
4235 get_printable_name_of_ttrace_event (state.tts_event));
4242 printf ("Why are we continuing a dead thread? (1)\n");
4247 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4250 /* Remove unseen threads from our list.
4252 update_thread_list ();
4254 /* Continue the threads.
4256 for (p = thread_head.head; p; p = p->next)
4261 thread_dropping_event_check (p);
4263 /* Pass the correct signals along.
4265 if (p->tid == real_tid)
4267 thread_signal = signal;
4270 else if (p->have_signal)
4272 thread_signal = p->signal_value;
4278 if (p->stepping_mode == DO_STEP)
4284 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4289 /* Continue this thread (default case).
4295 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4299 } /* End threads_continue_all_with_signals */
4301 /* Step one thread only.
4304 thread_fake_step (tid, signal)
4306 enum target_signal signal;
4313 printf ("Doing a fake-step over a bpt, etc. for %d\n", tid);
4315 if (is_terminated (tid))
4316 printf ("Why are we continuing a dead thread? (4)\n");
4320 if (doing_fake_step)
4321 warning ("Step while step already in progress.");
4323 /* See if there's a saved signal value for this
4324 * thread to be passed on, but no current signal.
4326 p = find_thread_info (tid);
4329 if (p->have_signal && signal == TARGET_SIGNAL_0)
4331 /* Pass on a saved signal.
4333 signal = p->signal_value;
4340 warning ("Internal error: continuing unhandled thread.");
4342 call_ttrace (TT_LWP_SINGLE,
4345 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4348 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4349 * for this thread only, and clear any saved signal info.
4351 doing_fake_step = 1;
4352 fake_step_tid = tid;
4354 } /* End thread_fake_step */
4356 /* Continue one thread when a signal must be sent to it.
4359 threads_continue_one_with_signal (gdb_tid, signal)
4369 printf ("Continuing one thread with a signal\n");
4372 real_tid = map_from_gdb_tid (gdb_tid);
4373 real_pid = get_pid_for (real_tid);
4375 p = find_thread_info (real_tid);
4378 add_tthread (real_pid, real_tid);
4384 printf ("Why are we continuing a dead thread? (2)\n");
4388 warning ("Internal error: continuing unhandled thread.");
4392 call_ttrace (TT_LWP_CONTINUE,
4395 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4400 #ifndef CHILD_RESUME
4402 /* Resume execution of the inferior process.
4404 * This routine is in charge of setting the "handled" bits.
4406 * If STEP is zero, continue it.
4407 * If STEP is nonzero, single-step it.
4409 * If SIGNAL is nonzero, give it that signal.
4411 * If TID is -1, apply to all threads.
4412 * If TID is not -1, apply to specified thread.
4416 * TID \________________________________________________
4418 * -1 | Step current Continue all threads
4419 * | thread and (but which gets any
4420 * | continue others signal?--We look at
4423 * N | Step _this_ thread Continue _this_ thread
4424 * | and leave others and leave others
4425 * | stopped; internally stopped; used only for
4426 * | used by gdb, never hardware watchpoints
4427 * | a user command. and attach, never a
4431 child_resume (gdb_tid, step, signal)
4434 enum target_signal signal;
4436 int resume_all_threads;
4438 process_state_t new_process_state;
4440 resume_all_threads =
4441 (gdb_tid == INFTTRACE_ALL_THREADS) ||
4444 if (resume_all_threads)
4446 /* Resume all threads, but first pick a tid value
4447 * so we can get the pid when in call_ttrace doing
4450 if (vfork_in_flight)
4451 tid = vforking_child_pid;
4453 tid = map_from_gdb_tid (inferior_pid);
4456 tid = map_from_gdb_tid (gdb_tid);
4461 if (more_events_left)
4462 printf ("More events; ");
4465 printf ("Sending signal %d; ", signal);
4467 if (resume_all_threads)
4470 printf ("Continue process %d\n", tid);
4472 printf ("Step/continue thread %d\n", tid);
4477 printf ("Continue thread %d\n", tid);
4479 printf ("Step just thread %d\n", tid);
4482 if (vfork_in_flight)
4483 printf ("Vfork in flight\n");
4487 if (process_state == RUNNING)
4488 warning ("Internal error in resume logic; doing resume or step anyway.");
4490 if (!step /* Asked to continue... */
4491 && resume_all_threads /* whole process.. */
4492 && signal != 0 /* with a signal... */
4493 && more_events_left > 0)
4494 { /* but we can't yet--save it! */
4496 /* Continue with signal means we have to set the pending
4497 * signal value for this thread.
4503 printf ("Saving signal %d for thread %d\n", signal, tid);
4506 k = find_thread_info (tid);
4510 k->signal_value = signal;
4515 printf ("Why are we continuing a dead thread? (3)\n");
4523 printf ("No thread info for tid %d\n", tid);
4528 /* Are we faking this "continue" or "step"?
4530 * We used to do steps by continuing all the threads for
4531 * which the events had been handled already. While
4532 * conceptually nicer (hides it all in a lower level), this
4533 * can lead to starvation and a hang (e.g. all but one thread
4534 * are unhandled at a breakpoint just before a "join" operation,
4535 * and one thread is in the join, and the user wants to step that
4538 if (resume_all_threads /* Whole process, therefore user command */
4539 && more_events_left > 0)
4540 { /* But we can't do this yet--fake it! */
4545 /* No need to do any notes on a per-thread
4546 * basis--we're done!
4548 #ifdef WAIT_BUFFER_DEBUG
4550 printf ("Faking a process resume.\n");
4558 #ifdef WAIT_BUFFER_DEBUG
4560 printf ("Faking a process step.\n");
4565 p = find_thread_info (tid);
4568 warning ("No thread information for tid %d, 'next' command ignored.\n", tid);
4577 printf ("Why are we continuing a dead thread? (3.5)\n");
4580 if (p->stepping_mode != DO_DEFAULT)
4582 warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored.");
4588 p->stepping_mode = DO_STEP;
4590 p->stepping_mode = DO_CONTINUE;
4593 } /* Have thread info */
4594 } /* Must fake step or go */
4596 /* Execept for fake-steps, from here on we know we are
4597 * going to wind up with a running process which will
4600 new_process_state = RUNNING;
4602 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4603 * it was. (If GDB wanted it to start some other way, we have already
4604 * written a new PC value to the child.)
4606 * If this system does not support PT_STEP, a higher level function will
4607 * have called single_step() to transmute the step request into a
4608 * continue request (by setting breakpoints on all possible successor
4609 * instructions), so we don't have to worry about that here.
4613 if (resume_all_threads)
4616 * Regular user step: other threads get a "continue".
4618 threads_continue_all_but_one (tid, signal);
4619 clear_all_handled ();
4620 clear_all_stepping_mode ();
4625 /* "Fake step": gdb is stepping one thread over a
4626 * breakpoint, watchpoint, or out of a library load
4627 * event, etc. The rest just stay where they are.
4629 * Also used when there are pending events: we really
4630 * step the current thread, but leave the rest stopped.
4631 * Users can't request this, but "wait_for_inferior"
4634 thread_fake_step (tid, signal);
4636 /* Clear the "handled" state of this thread, because
4637 * we'll soon get a new event for it. Other events
4638 * stay as they were.
4640 clear_handled (tid);
4641 clear_stepping_mode (tid);
4642 new_process_state = FAKE_STEPPING;
4648 /* TT_LWP_CONTINUE can pass signals to threads,
4649 * TT_PROC_CONTINUE can't. So if there are any
4650 * signals to pass, we have to use the (slower)
4651 * loop over the stopped threads.
4653 * Equally, if we have to not continue some threads,
4654 * due to saved events, we have to use the loop.
4656 if ((signal != 0) || saved_signals_exist ())
4658 if (resume_all_threads)
4663 printf ("Doing a continue by loop of all threads\n");
4666 threads_continue_all_with_signals (tid, signal);
4668 clear_all_handled ();
4669 clear_all_stepping_mode ();
4675 printf ("Doing a continue w/signal of just thread %d\n", tid);
4678 threads_continue_one_with_signal (tid, signal);
4680 /* Clear the "handled" state of this thread, because
4681 * we'll soon get a new event for it. Other events
4682 * can stay as they were.
4684 clear_handled (tid);
4685 clear_stepping_mode (tid);
4691 /* No signals to send.
4693 if (resume_all_threads)
4697 printf ("Doing a continue by process of process %d\n", tid);
4700 if (more_events_left > 0)
4702 warning ("Losing buffered events on continue.");
4703 more_events_left = 0;
4706 call_ttrace (TT_PROC_CONTINUE,
4712 clear_all_handled ();
4713 clear_all_stepping_mode ();
4721 printf ("Doing a continue of just thread %d\n", tid);
4722 if (is_terminated (tid))
4723 printf ("Why are we continuing a dead thread? (5)\n");
4727 call_ttrace (TT_LWP_CONTINUE,
4733 /* Clear the "handled" state of this thread, because
4734 * we'll soon get a new event for it. Other events
4735 * can stay as they were.
4737 clear_handled (tid);
4738 clear_stepping_mode (tid);
4743 process_state = new_process_state;
4745 #ifdef WAIT_BUFFER_DEBUG
4747 printf ("Process set to %s\n",
4748 get_printable_name_of_process_state (process_state));
4752 #endif /* CHILD_RESUME */
4755 #ifdef ATTACH_DETACH
4759 * One worry is that we may not be attaching to "inferior_pid"
4760 * and thus may not want to clear out our data. FIXME?
4764 update_thread_state_after_attach (pid, kind_of_go)
4766 attach_continue_t kind_of_go;
4769 ttstate_t thread_state;
4773 /* The process better be stopped.
4775 if (process_state != STOPPED
4776 && process_state != VFORKING)
4777 warning ("Internal error attaching.");
4779 /* Clear out old tthread info and start over. This has the
4780 * side effect of ensuring that the TRAP is reported as being
4781 * in the right thread (re-mapped from tid to pid).
4783 * It's because we need to add the tthread _now_ that we
4784 * need to call "clear_thread_info" _now_, and that's why
4785 * "require_notification_of_events" doesn't clear the thread
4786 * info (it's called later than this routine).
4788 clear_thread_info ();
4791 for (tid = get_process_first_stopped_thread_id (pid, &thread_state);
4793 tid = get_process_next_stopped_thread_id (pid, &thread_state))
4802 printf ("Attaching to process %d, thread %d\n",
4807 /* Tell ourselves and the "rest of gdb" that this thread
4810 * This isn't really a hack. Other thread-based versions
4811 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4813 * We don't need to do mapping here, as we know this
4814 * is the first thread and thus gets the real pid
4815 * (and is "inferior_pid").
4817 * NOTE: it probably isn't the originating thread,
4818 * but that doesn't matter (we hope!).
4820 add_tthread (pid, tid);
4821 p = find_thread_info (tid);
4822 if (NULL == p) /* ?We just added it! */
4823 error ("Internal error adding a thread on attach.");
4825 copy_ttstate_t (&p->last_stop_state, thread_state);
4828 if (DO_ATTACH_CONTINUE == kind_of_go)
4831 * If we are going to CONTINUE afterwards,
4832 * raising a SIGTRAP, don't bother trying to
4833 * handle this event. But check first!
4835 switch (p->last_stop_state.tts_event)
4839 /* Ok to set this handled.
4844 warning ("Internal error; skipping event %s on process %d, thread %d.",
4845 get_printable_name_of_ttrace_event (
4846 p->last_stop_state.tts_event),
4850 set_handled (pid, tid);
4855 /* There will be no "continue" opertion, so the
4856 * process remains stopped. Don't set any events
4857 * handled except the "gimmies".
4859 switch (p->last_stop_state.tts_event)
4863 /* Ok to ignore this.
4865 set_handled (pid, tid);
4870 /* Expected "other" FORK or EXEC event from a
4876 printf ("Internal error: failed to handle event %s on process %d, thread %d.",
4877 get_printable_name_of_ttrace_event (
4878 p->last_stop_state.tts_event),
4883 add_thread (tid); /* in thread.c */
4891 /* One mustn't call ttrace_wait() after attaching via ttrace,
4892 'cause the process is stopped already.
4894 However, the upper layers of gdb's execution control will
4895 want to wait after attaching (but not after forks, in
4896 which case they will be doing a "target_resume", anticipating
4897 a later TTEVT_EXEC or TTEVT_FORK event).
4899 To make this attach() implementation more compatible with
4900 others, we'll make the attached-to process raise a SIGTRAP.
4902 Issue: this continues only one thread. That could be
4903 dangerous if the thread is blocked--the process won't run
4904 and no trap will be raised. FIX! (check state.tts_flags?
4905 need one that's either TTS_WASRUNNING--but we've stopped
4906 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4908 if (DO_ATTACH_CONTINUE == kind_of_go)
4910 tt_status = call_real_ttrace (
4915 (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP),
4918 perror_with_name ("ttrace");
4920 clear_handled (a_thread); /* So TRAP will be reported. */
4924 process_state = RUNNING;
4929 #endif /* ATTACH_DETACH */
4932 #ifdef ATTACH_DETACH
4933 /* Start debugging the process whose number is PID.
4942 tt_status = call_real_ttrace (
4947 (TTRACE_ARG_TYPE) TT_VERSION,
4950 perror_with_name ("ttrace attach");
4952 /* If successful, the process is now stopped.
4954 process_state = STOPPED;
4956 /* Our caller ("attach_command" in "infcmd.c")
4957 * expects to do a "wait_for_inferior" after
4958 * the attach, so make sure the inferior is
4959 * running when we're done.
4961 update_thread_state_after_attach (pid, DO_ATTACH_CONTINUE);
4967 #if defined(CHILD_POST_ATTACH)
4969 child_post_attach (pid)
4974 printf ("child-post-attach call\n");
4977 require_notification_of_events (pid);
4982 /* Stop debugging the process whose number is PID
4983 and continue it with signal number SIGNAL.
4984 SIGNAL = 0 means just continue it.
4991 call_ttrace (TT_PROC_DETACH,
4994 (TTRACE_ARG_TYPE) signal,
4998 clear_thread_info ();
5000 /* Process-state? */
5002 #endif /* ATTACH_DETACH */
5005 /* Default the type of the ttrace transfer to int. */
5006 #ifndef TTRACE_XFER_TYPE
5007 #define TTRACE_XFER_TYPE int
5011 _initialize_kernel_u_addr ()
5015 #if !defined (CHILD_XFER_MEMORY)
5016 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
5017 in the NEW_SUN_TTRACE case.
5018 It ought to be straightforward. But it appears that writing did
5019 not write the data that I specified. I cannot understand where
5020 it got the data that it actually did write. */
5022 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
5023 to debugger memory starting at MYADDR. Copy to inferior if
5026 Returns the length copied, which is either the LEN argument or zero.
5027 This xfer function does not do partial moves, since child_ops
5028 doesn't allow memory operations to cross below us in the target stack
5032 child_xfer_memory (memaddr, myaddr, len, write, target)
5037 struct target_ops *target; /* ignored */
5040 /* Round starting address down to longword boundary. */
5041 register CORE_ADDR addr = memaddr & -sizeof (TTRACE_XFER_TYPE);
5042 /* Round ending address up; get number of longwords that makes. */
5044 = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1)
5045 / sizeof (TTRACE_XFER_TYPE);
5046 /* Allocate buffer of that many longwords. */
5047 register TTRACE_XFER_TYPE *buffer
5048 = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));
5052 /* Fill start and end extra bytes of buffer with existing memory data. */
5054 if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE))
5056 /* Need part of initial word -- fetch it. */
5057 buffer[0] = call_ttrace (TT_LWP_RDTEXT,
5059 (TTRACE_ARG_TYPE) addr,
5064 if (count > 1) /* FIXME, avoid if even boundary */
5066 buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT,
5069 (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))),
5074 /* Copy data to be written over corresponding part of buffer */
5076 memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
5080 /* Write the entire buffer. */
5082 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
5085 call_ttrace (TT_LWP_WRDATA,
5087 (TTRACE_ARG_TYPE) addr,
5088 (TTRACE_ARG_TYPE) buffer[i],
5092 /* Using the appropriate one (I or D) is necessary for
5093 Gould NP1, at least. */
5095 call_ttrace (TT_LWP_WRTEXT,
5097 (TTRACE_ARG_TYPE) addr,
5098 (TTRACE_ARG_TYPE) buffer[i],
5107 /* Read all the longwords */
5108 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
5111 buffer[i] = call_ttrace (TT_LWP_RDTEXT,
5113 (TTRACE_ARG_TYPE) addr,
5121 /* Copy appropriate bytes out of the buffer. */
5123 (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
5133 int udot_off; /* Offset into user struct */
5134 int udot_val; /* Value from user struct at udot_off */
5135 char mess[128]; /* For messages */
5137 if (!target_has_execution)
5139 error ("The program is not being run.");
5142 #if !defined (KERNEL_U_SIZE)
5144 /* Adding support for this command is easy. Typically you just add a
5145 routine, called "kernel_u_size" that returns the size of the user
5146 struct, to the appropriate *-nat.c file and then add to the native
5147 config file "#define KERNEL_U_SIZE kernel_u_size()" */
5148 error ("Don't know how large ``struct user'' is in this version of gdb.");
5152 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
5154 if ((udot_off % 24) == 0)
5158 printf_filtered ("\n");
5160 printf_filtered ("%04x:", udot_off);
5162 udot_val = call_ttrace (TT_LWP_RUREGS,
5164 (TTRACE_ARG_TYPE) udot_off,
5169 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
5170 perror_with_name (mess);
5172 /* Avoid using nonportable (?) "*" in print specs */
5173 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
5175 printf_filtered ("\n");
5179 #endif /* !defined (CHILD_XFER_MEMORY). */
5181 /* TTrace version of "target_pid_to_exec_file"
5184 child_pid_to_exec_file (tid)
5187 static char exec_file_buffer[1024];
5189 CORE_ADDR top_of_stack;
5194 int saved_inferior_pid;
5196 /* As of 10.x HP-UX, there's an explicit request to get the
5199 tt_status = call_ttrace (TT_PROC_GET_PATHNAME,
5201 (TTRACE_ARG_TYPE) exec_file_buffer,
5202 (TTRACE_ARG_TYPE) sizeof (exec_file_buffer) - 1,
5205 return exec_file_buffer;
5207 /* ??rehrauer: The above request may or may not be broken. It
5208 doesn't seem to work when I use it. But, it may be designed
5209 to only work immediately after an exec event occurs. (I'm
5210 waiting for COSL to explain.)
5212 In any case, if it fails, try a really, truly amazingly gross
5213 hack that DDE uses, of pawing through the process' data
5214 segment to find the pathname.
5216 top_of_stack = (TARGET_PTR_BIT == 64 ? 0x800003ffff7f0000 : 0x7b03a000);
5220 /* On the chance that pid != inferior_pid, set inferior_pid
5221 to pid, so that (grrrr!) implicit uses of inferior_pid get
5224 saved_inferior_pid = inferior_pid;
5227 /* Try to grab a null-terminated string. */
5230 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
5232 inferior_pid = saved_inferior_pid;
5235 for (i = 0; i < 4; i++)
5237 exec_file_buffer[name_index++] = four_chars[i];
5238 done = (four_chars[i] == '\0');
5245 if (exec_file_buffer[0] == '\0')
5247 inferior_pid = saved_inferior_pid;
5251 inferior_pid = saved_inferior_pid;
5252 return exec_file_buffer;
5257 pre_fork_inferior ()
5261 status = pipe (startup_semaphore.parent_channel);
5264 warning ("error getting parent pipe for startup semaphore");
5268 status = pipe (startup_semaphore.child_channel);
5271 warning ("error getting child pipe for startup semaphore");
5276 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5277 * ultimately from "follow_inferior_fork" in infrun.c,
5278 * itself called from "resume".
5280 * This seems to be intended to attach after a fork or
5281 * vfork, while "attach" is used to attach to a pid
5282 * given by the user. The check for an existing attach
5283 * seems odd--it always fails in our test system.
5286 hppa_require_attach (pid)
5292 unsigned int regs_offset;
5293 process_state_t old_process_state = process_state;
5295 /* Are we already attached? There appears to be no explicit
5296 * way to answer this via ttrace, so we try something which
5297 * should be innocuous if we are attached. If that fails,
5298 * then we assume we're not attached, and so attempt to make
5302 tt_status = call_real_ttrace (TT_PROC_STOP,
5305 (TTRACE_ARG_TYPE) TT_NIL,
5306 (TTRACE_ARG_TYPE) TT_NIL,
5311 /* No change to process-state!
5318 /* If successful, the process is now stopped. But if
5319 * we're VFORKING, the parent is still running, so don't
5320 * change the process state.
5322 if (process_state != VFORKING)
5323 process_state = STOPPED;
5325 /* If we were already attached, you'd think that we
5326 * would need to start going again--but you'd be wrong,
5327 * as the fork-following code is actually in the middle
5328 * of the "resume" routine in in "infrun.c" and so
5329 * will (almost) immediately do a resume.
5331 * On the other hand, if we are VFORKING, which means
5332 * that the child and the parent share a process for a
5333 * while, we know that "resume" won't be resuming
5334 * until the child EXEC event is seen. But we still
5335 * don't want to continue, as the event is already
5338 update_thread_state_after_attach (pid, DONT_ATTACH_CONTINUE);
5339 } /* STOP succeeded */
5345 hppa_require_detach (pid, signal)
5351 /* If signal is non-zero, we must pass the signal on to the active
5352 thread prior to detaching. We do this by continuing the threads
5358 threads_continue_all_with_signals (pid, signal);
5362 tt_status = call_ttrace (TT_PROC_DETACH,
5368 errno = 0; /* Ignore any errors. */
5370 /* process_state? */
5375 /* Given the starting address of a memory page, hash it to a bucket in
5376 the memory page dictionary.
5379 get_dictionary_bucket_of_page (page_start)
5380 CORE_ADDR page_start;
5384 hash = (page_start / memory_page_dictionary.page_size);
5385 hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT;
5391 /* Given a memory page's starting address, get (i.e., find an existing
5392 or create a new) dictionary entry for the page. The page will be
5393 write-protected when this function returns, but may have a reference
5394 count of 0 (if the page was newly-added to the dictionary).
5396 static memory_page_t *
5397 get_dictionary_entry_of_page (pid, page_start)
5399 CORE_ADDR page_start;
5402 memory_page_t *page = NULL;
5403 memory_page_t *previous_page = NULL;
5405 /* We're going to be using the dictionary now, than-kew. */
5406 require_memory_page_dictionary (pid);
5408 /* Try to find an existing dictionary entry for this page. Hash
5409 on the page's starting address.
5411 bucket = get_dictionary_bucket_of_page (page_start);
5412 page = &memory_page_dictionary.buckets[bucket];
5413 while (page != NULL)
5415 if (page->page_start == page_start)
5417 previous_page = page;
5421 /* Did we find a dictionary entry for this page? If not, then
5422 add it to the dictionary now.
5426 /* Create a new entry. */
5427 page = (memory_page_t *) xmalloc (sizeof (memory_page_t));
5428 page->page_start = page_start;
5429 page->reference_count = 0;
5431 page->previous = NULL;
5433 /* We'll write-protect the page now, if that's allowed. */
5434 page->original_permissions = write_protect_page (pid, page_start);
5436 /* Add the new entry to the dictionary. */
5437 page->previous = previous_page;
5438 previous_page->next = page;
5440 memory_page_dictionary.page_count++;
5448 remove_dictionary_entry_of_page (pid, page)
5450 memory_page_t *page;
5452 /* Restore the page's original permissions. */
5453 unwrite_protect_page (pid, page->page_start, page->original_permissions);
5455 /* Kick the page out of the dictionary. */
5456 if (page->previous != NULL)
5457 page->previous->next = page->next;
5458 if (page->next != NULL)
5459 page->next->previous = page->previous;
5461 /* Just in case someone retains a handle to this after it's freed. */
5462 page->page_start = (CORE_ADDR) 0;
5464 memory_page_dictionary.page_count--;
5471 hppa_enable_syscall_events (pid)
5475 ttevent_t ttrace_events;
5477 /* Get the set of events that are currently enabled. */
5478 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5480 (TTRACE_ARG_TYPE) & ttrace_events,
5481 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5484 perror_with_name ("ttrace");
5486 /* Add syscall events to that set. */
5487 ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY;
5488 ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN;
5490 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5492 (TTRACE_ARG_TYPE) & ttrace_events,
5493 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5496 perror_with_name ("ttrace");
5501 hppa_disable_syscall_events (pid)
5505 ttevent_t ttrace_events;
5507 /* Get the set of events that are currently enabled. */
5508 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5510 (TTRACE_ARG_TYPE) & ttrace_events,
5511 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5514 perror_with_name ("ttrace");
5516 /* Remove syscall events from that set. */
5517 ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY;
5518 ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN;
5520 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5522 (TTRACE_ARG_TYPE) & ttrace_events,
5523 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5526 perror_with_name ("ttrace");
5530 /* The address range beginning with START and ending with START+LEN-1
5531 (inclusive) is to be watched via page-protection by a new watchpoint.
5532 Set protection for all pages that overlap that range.
5534 Note that our caller sets TYPE to:
5535 0 for a bp_hardware_watchpoint,
5536 1 for a bp_read_watchpoint,
5537 2 for a bp_access_watchpoint
5539 (Yes, this is intentionally (though lord only knows why) different
5540 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5543 hppa_insert_hw_watchpoint (pid, start, len, type)
5549 CORE_ADDR page_start;
5550 int dictionary_was_empty;
5553 LONGEST range_size_in_pages;
5556 error ("read or access hardware watchpoints not supported on HP-UX");
5558 /* Examine all pages in the address range. */
5559 require_memory_page_dictionary ();
5561 dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5563 page_size = memory_page_dictionary.page_size;
5564 page_start = (start / page_size) * page_size;
5565 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5567 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5569 memory_page_t *page;
5571 /* This gets the page entered into the dictionary if it was
5572 not already entered.
5574 page = get_dictionary_entry_of_page (pid, page_start);
5575 page->reference_count++;
5578 /* Our implementation depends on seeing calls to kernel code, for the
5579 following reason. Here we ask to be notified of syscalls.
5581 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5584 But when kernel code accesses the page, it doesn't give a SIGBUS.
5585 Rather, the system call that touched the page fails, with errno=EFAULT.
5588 We could accomodate this "feature" by asking to be notified of syscall
5589 entries & exits; upon getting an entry event, disabling page-protections;
5590 upon getting an exit event, reenabling page-protections and then checking
5591 if any watchpoints triggered.
5593 However, this turns out to be a real performance loser. syscalls are
5594 usually a frequent occurrence. Having to unprotect-reprotect all watched
5595 pages, and also to then read all watched memory locations and compare for
5596 triggers, can be quite expensive.
5598 Instead, we'll only ask to be notified of syscall exits. When we get
5599 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5600 we can just continue the inferior.
5602 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5603 hackish stuff to determine whether the failure really was due to a
5604 page-protect trap on a watched location.
5606 if (dictionary_was_empty)
5607 hppa_enable_syscall_events (pid);
5613 /* The address range beginning with START and ending with START+LEN-1
5614 (inclusive) was being watched via page-protection by a watchpoint
5615 which has been removed. Remove protection for all pages that
5616 overlap that range, which are not also being watched by other
5620 hppa_remove_hw_watchpoint (pid, start, len, type)
5626 CORE_ADDR page_start;
5627 int dictionary_is_empty;
5630 LONGEST range_size_in_pages;
5633 error ("read or access hardware watchpoints not supported on HP-UX");
5635 /* Examine all pages in the address range. */
5636 require_memory_page_dictionary ();
5638 page_size = memory_page_dictionary.page_size;
5639 page_start = (start / page_size) * page_size;
5640 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5642 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5644 memory_page_t *page;
5646 page = get_dictionary_entry_of_page (pid, page_start);
5647 page->reference_count--;
5649 /* Was this the last reference of this page? If so, then we
5650 must scrub the entry from the dictionary, and also restore
5651 the page's original permissions.
5653 if (page->reference_count == 0)
5654 remove_dictionary_entry_of_page (pid, page);
5657 dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5659 /* If write protections are currently disallowed, then that implies that
5660 wait_for_inferior believes that the inferior is within a system call.
5661 Since we want to see both syscall entry and return, it's clearly not
5662 good to disable syscall events in this state!
5664 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5665 "inferior is between syscall events now". Oh well.
5667 if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed)
5668 hppa_disable_syscall_events (pid);
5674 /* Could we implement a watchpoint of this type via our available
5677 This query does not consider whether a particular address range
5678 could be so watched, but just whether support is generally available
5679 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5680 query that answers whether a particular range should be watched via
5684 hppa_can_use_hw_watchpoint (type, cnt, ot)
5689 return (type == bp_hardware_watchpoint);
5693 /* Assuming we could set a hardware watchpoint on this address, do
5694 we think it would be profitable ("a good idea") to do so? If not,
5695 we can always set a regular (aka single-step & test) watchpoint
5699 hppa_range_profitable_for_hw_watchpoint (pid, start, len)
5704 int range_is_stack_based;
5705 int range_is_accessible;
5706 CORE_ADDR page_start;
5709 LONGEST range_size_in_pages;
5711 /* ??rehrauer: For now, say that all addresses are potentially
5712 profitable. Possibly later we'll want to test the address
5715 range_is_stack_based = 0;
5717 /* If any page in the range is inaccessible, then we cannot
5718 really use hardware watchpointing, even though our client
5719 thinks we can. In that case, it's actually an error to
5720 attempt to use hw watchpoints, so we'll tell our client
5721 that the range is "unprofitable", and hope that they listen...
5723 range_is_accessible = 1; /* Until proven otherwise. */
5725 /* Examine all pages in the address range. */
5727 page_size = sysconf (_SC_PAGE_SIZE);
5729 /* If we can't determine page size, we're hosed. Tell our
5730 client it's unprofitable to use hw watchpoints for this
5733 if (errno || (page_size <= 0))
5739 page_start = (start / page_size) * page_size;
5740 range_size_in_pages = len / (LONGEST) page_size;
5742 for (page = 0; page < range_size_in_pages; page++, page_start += page_size)
5745 int page_permissions;
5747 /* Is this page accessible? */
5749 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
5751 (TTRACE_ARG_TYPE) page_start,
5753 (TTRACE_ARG_TYPE) & page_permissions);
5754 if (errno || (tt_status < 0))
5757 range_is_accessible = 0;
5761 /* Yes, go for another... */
5764 return (!range_is_stack_based && range_is_accessible);
5769 hppa_pid_or_tid_to_str (id)
5772 static char buf[100]; /* Static because address returned. */
5774 /* Does this appear to be a process? If so, print it that way. */
5775 if (is_process_id (id))
5776 return child_pid_to_str (id);
5778 /* Else, print both the GDB thread number and the system thread id. */
5779 sprintf (buf, "thread %d (", pid_to_thread_id (id));
5780 strcat (buf, hppa_tid_to_str (id));
5781 strcat (buf, ")\0");
5787 /* If the current pid is not the pid this module reported
5788 * from "ptrace_wait" with the most recent event, then the
5789 * user has switched threads.
5791 * If the last reported event was a breakpoint, then return
5792 * the old thread id, else return 0.
5795 hppa_switched_threads (gdb_pid)
5798 if (gdb_pid == old_gdb_pid)
5801 * Core gdb is working with the same pid that it
5802 * was before we reported the last event. This
5803 * is ok: e.g. we reported hitting a thread-specific
5804 * breakpoint, but we were reporting the wrong
5805 * thread, so the core just ignored the event.
5807 * No thread switch has happened.
5811 else if (gdb_pid == reported_pid)
5814 * Core gdb is working with the pid we reported, so
5815 * any continue or step will be able to figure out
5816 * that it needs to step over any hit breakpoints
5817 * without our (i.e. PREPARE_TO_PROCEED's) help.
5821 else if (!reported_bpt)
5824 * The core switched, but we didn't just report a
5825 * breakpoint, so there's no just-hit breakpoint
5826 * instruction at "reported_pid"'s PC, and thus there
5827 * is no need to step over it.
5833 /* There's been a real switch, and we reported
5834 * a hit breakpoint. Let "hppa_prepare_to_proceed"
5835 * know, so it can see whether the breakpoint is
5838 return reported_pid;
5841 /* Keep compiler happy with an obvious return at the end.
5847 hppa_ensure_vforking_parent_remains_stopped (pid)
5850 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5857 hppa_resume_execd_vforking_child_to_get_parent_vfork ()
5859 return 0; /* No, the parent vfork is available now. */
5863 /* Write a register as a 64bit value. This may be necessary if the
5864 native OS is too braindamaged to allow some (or all) registers to
5865 be written in 32bit hunks such as hpux11 and the PC queue registers.
5867 This is horribly gross and disgusting. */
5870 ttrace_write_reg_64 (gdb_tid, dest_addr, src_addr)
5872 CORE_ADDR dest_addr;
5879 tid = map_from_gdb_tid (gdb_tid);
5880 pid = get_pid_for (tid);
5883 tt_status = ttrace (TT_LWP_WUREGS,
5886 (TTRACE_ARG_TYPE) dest_addr,
5888 (TTRACE_ARG_TYPE) src_addr );
5893 /* Don't bother for a known benign error: if you ask for the
5894 first thread state, but there is only one thread and it's
5895 not stopped, ttrace complains.
5897 We have this inside the #ifdef because our caller will do
5898 this check for real. */
5899 if( request != TT_PROC_GET_FIRST_LWP_STATE
5900 || errno != EPROTO )
5903 printf( "TT fail for %s, with pid %d, tid %d, status %d \n",
5904 get_printable_name_of_ttrace_request (TT_LWP_WUREGS),
5905 pid, tid, tt_status );
5914 _initialize_infttrace ()
5916 /* Initialize the ttrace-based hardware watchpoint implementation. */
5917 memory_page_dictionary.page_count = (LONGEST) - 1;
5918 memory_page_dictionary.page_protections_allowed = 1;
5921 memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE);
5923 /* We do a lot of casts from pointers to TTRACE_ARG_TYPE; make sure
5925 if (sizeof (TTRACE_ARG_TYPE) < sizeof (void *))
5928 if (errno || (memory_page_dictionary.page_size <= 0))
5929 perror_with_name ("sysconf");