1 /* Low level Unix child interface to ttrace, for GDB when running under HP-UX.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "gdb_string.h"
31 /* We need pstat functionality so that we can get the exec file
32 for a process we attach to.
34 According to HP, we should use the 64bit interfaces, so we
35 define _PSTAT64 to achieve this. */
37 #include <sys/pstat.h>
39 /* Some hackery to work around a use of the #define name NO_FLAGS
40 * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
43 #define INFTTRACE_TEMP_HACK NO_FLAGS
48 #include <sys/types.h>
51 #include <sys/param.h>
54 #include <sys/ioctl.h>
56 #include <sys/ttrace.h>
60 #ifdef PTRACE_IN_WRONG_PLACE
63 #include <sys/ptrace.h>
65 #endif /* NO_PTRACE_H */
67 /* Second half of the hackery above. Non-ANSI C, so
68 * we can't use "#error", alas.
71 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
72 /* #error "Hackery to remove warning didn't work right" */
74 /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
77 /* #error "Didn't get expected re-definition of NO_FLAGS" */
78 #define NO_FLAGS INFTTRACE_TEMP_HACK
81 #if !defined (PT_SETTRC)
82 #define PT_SETTRC 0 /* Make process traceable by parent */
84 #if !defined (PT_READ_I)
85 #define PT_READ_I 1 /* Read word from text space */
87 #if !defined (PT_READ_D)
88 #define PT_READ_D 2 /* Read word from data space */
90 #if !defined (PT_READ_U)
91 #define PT_READ_U 3 /* Read word from kernel user struct */
93 #if !defined (PT_WRITE_I)
94 #define PT_WRITE_I 4 /* Write word to text space */
96 #if !defined (PT_WRITE_D)
97 #define PT_WRITE_D 5 /* Write word to data space */
99 #if !defined (PT_WRITE_U)
100 #define PT_WRITE_U 6 /* Write word to kernel user struct */
102 #if !defined (PT_CONTINUE)
103 #define PT_CONTINUE 7 /* Continue after signal */
105 #if !defined (PT_STEP)
106 #define PT_STEP 9 /* Set flag for single stepping */
108 #if !defined (PT_KILL)
109 #define PT_KILL 8 /* Send child a SIGKILL signal */
113 #define PT_ATTACH PTRACE_ATTACH
116 #define PT_DETACH PTRACE_DETACH
121 #include <sys/file.h>
124 /* This semaphore is used to coordinate the child and parent processes
125 after a fork(), and before an exec() by the child. See parent_attach_all
130 int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */
131 int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */
136 #define SEM_LISTEN (0)
138 static startup_semaphore_t startup_semaphore;
140 /* See can_touch_threads_of_process for details. */
141 static int vforking_child_pid = 0;
142 static int vfork_in_flight = 0;
144 /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed).
146 static pid_t old_gdb_pid = 0;
147 static pid_t reported_pid = 0;
148 static int reported_bpt = 0;
150 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
152 #define TT_OK( _status, _errno ) \
153 (((_status) == 1) && ((_errno) == 0))
155 #define TTRACE_ARG_TYPE uint64_t
157 /* When supplied as the "addr" operand, ttrace interprets this
158 to mean, "from the current address".
160 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
162 /* When supplied as the "addr", "data" or "addr2" operand for most
163 requests, ttrace interprets this to mean, "pay no heed to this
166 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
168 /* This is capable of holding the value of a 32-bit register. The
169 value is always left-aligned in the buffer; i.e., [0] contains
170 the most-significant byte of the register's value, and [sizeof(reg)]
171 contains the least-significant value.
173 ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
174 that registers are 32-bits on HP-UX. The latter assumption changes
177 typedef int register_value_t;
179 /********************************************************************
185 The rest of GDB sees threads as being things with different
186 "pid" (process id) values. See "thread.c" for details. The
187 separate threads will be seen and reacted to if infttrace passes
188 back different pid values (for _events_). See wait_for_inferior
191 So infttrace is going to use thread ids externally, pretending
192 they are process ids, and keep track internally so that it can
193 use the real process id (and thread id) when calling ttrace.
195 The data structure that supports this is a linked list of the
196 current threads. Since at some date infttrace will have to
197 deal with multiple processes, each list element records its
198 corresponding pid, rather than having a single global.
200 Note that the list is only approximately current; that's ok, as
201 it's up to date when we need it (we hope!). Also, it can contain
202 dead threads, as there's no harm if it does.
204 The approach taken here is to bury the translation from external
205 to internal inside "call_ttrace" and a few other places.
207 There are some wrinkles:
209 o When GDB forks itself to create the debug target process,
210 there's only a pid of 0 around in the child, so the
211 TT_PROC_SETTRC operation uses a more direct call to ttrace;
212 Similiarly, the initial setting of the event mask happens
213 early as well, and so is also special-cased, and an attach
216 o We define an unthreaded application as having a "pseudo"
219 o To keep from confusing the rest of GDB, we don't switch
220 the PID for the pseudo thread to a TID. A table will help:
222 Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ...
224 Our thread list stores: pid pid pid pid ...
227 Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ...
229 Both pid and tid0 will map to tid0, as there are infttrace.c-internal
230 calls to ttrace using tid0.
234 Since we're implementing the "stop the world" model, sub-model
235 "other threads run during step", we have some stuff to do:
237 o User steps require continuing all threads other than the
238 one the user is stepping;
240 o Internal debugger steps (such as over a breakpoint or watchpoint,
241 but not out of a library load thunk) require stepping only
242 the selected thread; this means that we have to report the
243 step finish on that thread, which can lead to complications;
245 o When a thread is created, it is created running, rather
246 than stopped--so we have to stop it.
248 The OS doesn't guarantee the stopped thread list will be stable,
249 no does it guarantee where on the stopped thread list a thread
250 that is single-stepped will wind up: it's possible that it will
251 be off the list for a while, it's possible the step will complete
252 and it will be re-posted to the end...
254 This means we have to scan the stopped thread list, build up
255 a work-list, and then run down the work list; we can't do the
256 step/continue during the scan.
260 Then there's the issue of waiting for an event. We do this by
261 noticing how many events are reported at the end of each wait.
262 From then on, we "fake" all resumes and steps, returning instantly,
263 and don't do another wait. Once all pending events are reported,
264 we can really resume again.
266 To keep this hidden, all the routines which know about tids and
267 pids or real events and simulated ones are static (file-local).
269 This code can make lots of calls to ttrace, in particular it
270 can spin down the list of thread states more than once. If this
271 becomes a performance hit, the spin could be done once and the
272 various "tsp" blocks saved, keeping all later spins in this
275 The O/S doesn't promise to keep the list straight, and so we must
276 re-scan a lot. By observation, it looks like a single-step/wait
277 puts the stepped thread at the end of the list but doesn't change
280 ****************************************************************
283 /* Uncomment these to turn on various debugging output */
284 /* #define THREAD_DEBUG */
285 /* #define WAIT_BUFFER_DEBUG */
286 /* #define PARANOIA */
289 #define INFTTRACE_ALL_THREADS (-1)
290 #define INFTTRACE_STEP (1)
291 #define INFTTRACE_CONTINUE (0)
293 /* FIX: this is used in inftarg.c/child_wait, in a hack.
295 extern int not_same_real_pid;
297 /* This is used to count buffered events.
299 static unsigned int more_events_left = 0;
303 typedef enum process_state_enum
307 FAKE_CONTINUE, /* For later use */
314 static process_state_t process_state = STOPPED;
316 /* User-specified stepping modality.
318 typedef enum stepping_mode_enum
320 DO_DEFAULT, /* ...which is a continue! */
326 /* Action to take on an attach, depends on
327 * what kind (user command, fork, vfork).
329 * At the moment, this is either:
331 * o continue with a SIGTRAP signal, or
335 typedef enum attach_continue_enum
342 /* This flag is true if we are doing a step-over-bpt
343 * with buffered events. We will have to be sure to
344 * report the right thread, as otherwise the spaghetti
345 * code in "infrun.c/wait_for_inferior" will get
348 static int doing_fake_step = 0;
349 static lwpid_t fake_step_tid = 0;
352 /****************************************************
353 * Thread information structure routines and types. *
354 ****************************************************
357 struct thread_info_struct
359 int am_pseudo; /* This is a pseudo-thread for the process. */
360 int pid; /* Process ID */
361 lwpid_t tid; /* Thread ID */
362 int handled; /* 1 if a buffered event was handled. */
363 int seen; /* 1 if this thread was seen on a traverse. */
364 int terminated; /* 1 if thread has terminated. */
365 int have_signal; /* 1 if signal to be sent */
366 enum target_signal signal_value; /* Signal to send */
367 int have_start; /* 1 if alternate starting address */
368 stepping_mode_t stepping_mode; /* Whether to step or continue */
369 CORE_ADDR start; /* Where to start */
370 int have_state; /* 1 if the event state has been set */
371 ttstate_t last_stop_state; /* The most recently-waited event for this thread. */
372 struct thread_info_struct
373 *next; /* All threads are linked via this field. */
374 struct thread_info_struct
375 *next_pseudo; /* All pseudo-threads are linked via this field. */
380 struct thread_info_header_struct
384 thread_info *head_pseudo;
389 static thread_info_header thread_head =
391 static thread_info_header deleted_threads =
394 static ptid_t saved_real_ptid;
397 /*************************************************
398 * Debugging support functions *
399 *************************************************
402 get_raw_pc (lwpid_t ttid)
404 unsigned long pc_val;
408 offset = register_addr (PC_REGNUM, U_REGS_OFFSET);
409 res = read_from_register_save_state (
411 (TTRACE_ARG_TYPE) offset,
416 return (CORE_ADDR) pc_val;
420 return (CORE_ADDR) 0;
425 get_printable_name_of_stepping_mode (stepping_mode_t mode)
434 return "DO_CONTINUE";
436 return "?unknown mode?";
440 /* This function returns a pointer to a string describing the
441 * ttrace event being reported.
444 get_printable_name_of_ttrace_event (ttevents_t event)
446 /* This enumeration is "gappy", so don't use a table. */
453 return "TTEVT_SIGNAL";
461 return "TTEVT_VFORK";
462 case TTEVT_SYSCALL_RETURN:
463 return "TTEVT_SYSCALL_RETURN";
464 case TTEVT_LWP_CREATE:
465 return "TTEVT_LWP_CREATE";
466 case TTEVT_LWP_TERMINATE:
467 return "TTEVT_LWP_TERMINATE";
469 return "TTEVT_LWP_EXIT";
470 case TTEVT_LWP_ABORT_SYSCALL:
471 return "TTEVT_LWP_ABORT_SYSCALL";
472 case TTEVT_SYSCALL_ENTRY:
473 return "TTEVT_SYSCALL_ENTRY";
474 case TTEVT_SYSCALL_RESTART:
475 return "TTEVT_SYSCALL_RESTART";
477 return "?new event?";
482 /* This function translates the ttrace request enumeration into
483 * a character string that is its printable (aka "human readable")
487 get_printable_name_of_ttrace_request (ttreq_t request)
489 if (!IS_TTRACE_REQ (request))
492 /* This enumeration is "gappy", so don't use a table. */
496 return "TT_PROC_SETTRC";
498 return "TT_PROC_ATTACH";
500 return "TT_PROC_DETACH";
502 return "TT_PROC_RDTEXT";
504 return "TT_PROC_WRTEXT";
506 return "TT_PROC_RDDATA";
508 return "TT_PROC_WRDATA";
510 return "TT_PROC_STOP";
511 case TT_PROC_CONTINUE:
512 return "TT_PROC_CONTINUE";
513 case TT_PROC_GET_PATHNAME:
514 return "TT_PROC_GET_PATHNAME";
515 case TT_PROC_GET_EVENT_MASK:
516 return "TT_PROC_GET_EVENT_MASK";
517 case TT_PROC_SET_EVENT_MASK:
518 return "TT_PROC_SET_EVENT_MASK";
519 case TT_PROC_GET_FIRST_LWP_STATE:
520 return "TT_PROC_GET_FIRST_LWP_STATE";
521 case TT_PROC_GET_NEXT_LWP_STATE:
522 return "TT_PROC_GET_NEXT_LWP_STATE";
524 return "TT_PROC_EXIT";
525 case TT_PROC_GET_MPROTECT:
526 return "TT_PROC_GET_MPROTECT";
527 case TT_PROC_SET_MPROTECT:
528 return "TT_PROC_SET_MPROTECT";
529 case TT_PROC_SET_SCBM:
530 return "TT_PROC_SET_SCBM";
532 return "TT_LWP_STOP";
533 case TT_LWP_CONTINUE:
534 return "TT_LWP_CONTINUE";
536 return "TT_LWP_SINGLE";
538 return "TT_LWP_RUREGS";
540 return "TT_LWP_WUREGS";
541 case TT_LWP_GET_EVENT_MASK:
542 return "TT_LWP_GET_EVENT_MASK";
543 case TT_LWP_SET_EVENT_MASK:
544 return "TT_LWP_SET_EVENT_MASK";
545 case TT_LWP_GET_STATE:
546 return "TT_LWP_GET_STATE";
553 /* This function translates the process state enumeration into
554 * a character string that is its printable (aka "human readable")
558 get_printable_name_of_process_state (process_state_t process_state)
560 switch (process_state)
565 return "FAKE_STEPPING";
573 return "?some unknown state?";
577 /* Set a ttrace thread state to a safe, initial state.
580 clear_ttstate_t (ttstate_t *tts)
584 tts->tts_user_tid = 0;
585 tts->tts_event = TTEVT_NONE;
588 /* Copy ttrace thread state TTS_FROM into TTS_TO.
591 copy_ttstate_t (ttstate_t *tts_to, ttstate_t *tts_from)
593 memcpy ((char *) tts_to, (char *) tts_from, sizeof (*tts_to));
596 /* Are there any live threads we know about?
599 any_thread_records (void)
601 return (thread_head.count > 0);
604 /* Create, fill in and link in a thread descriptor.
607 create_thread_info (int pid, lwpid_t tid)
611 int thread_count_of_pid;
613 new_p = xmalloc (sizeof (thread_info));
616 new_p->have_signal = 0;
617 new_p->have_start = 0;
618 new_p->have_state = 0;
619 clear_ttstate_t (&new_p->last_stop_state);
620 new_p->am_pseudo = 0;
623 new_p->terminated = 0;
625 new_p->next_pseudo = NULL;
626 new_p->stepping_mode = DO_DEFAULT;
628 if (0 == thread_head.count)
632 printf ("First thread, pid %d tid %d!\n", pid, tid);
634 saved_real_ptid = inferior_ptid;
640 printf ("Subsequent thread, pid %d tid %d\n", pid, tid);
644 /* Another day, another thread...
648 /* The new thread always goes at the head of the list.
650 new_p->next = thread_head.head;
651 thread_head.head = new_p;
653 /* Is this the "pseudo" thread of a process? It is if there's
654 * no other thread for this process on the list. (Note that this
655 * accomodates multiple processes, such as we see even for simple
656 * cases like forking "non-threaded" programs.)
658 p = thread_head.head;
659 thread_count_of_pid = 0;
662 if (p->pid == new_p->pid)
663 thread_count_of_pid++;
667 /* Did we see any other threads for this pid? (Recall that we just
668 * added this thread to the list...)
670 if (thread_count_of_pid == 1)
672 new_p->am_pseudo = 1;
673 new_p->next_pseudo = thread_head.head_pseudo;
674 thread_head.head_pseudo = new_p;
680 /* Get rid of our thread info.
683 clear_thread_info (void)
690 printf ("Clearing all thread info\n");
693 p = thread_head.head;
701 thread_head.head = NULL;
702 thread_head.head_pseudo = NULL;
703 thread_head.count = 0;
705 p = deleted_threads.head;
713 deleted_threads.head = NULL;
714 deleted_threads.head_pseudo = NULL;
715 deleted_threads.count = 0;
717 /* No threads, so can't have pending events.
719 more_events_left = 0;
722 /* Given a tid, find the thread block for it.
725 find_thread_info (lwpid_t tid)
729 for (p = thread_head.head; p; p = p->next)
737 for (p = deleted_threads.head; p; p = p->next)
748 /* For any but the pseudo thread, this maps to the
749 * thread ID. For the pseudo thread, if you pass either
750 * the thread id or the PID, you get the pseudo thread ID.
752 * We have to be prepared for core gdb to ask about
753 * deleted threads. We do the map, but we don't like it.
756 map_from_gdb_tid (lwpid_t gdb_tid)
760 /* First assume gdb_tid really is a tid, and try to find a
761 * matching entry on the threads list.
763 for (p = thread_head.head; p; p = p->next)
765 if (p->tid == gdb_tid)
769 /* It doesn't appear to be a tid; perhaps it's really a pid?
770 * Try to find a "pseudo" thread entry on the threads list.
772 for (p = thread_head.head_pseudo; p != NULL; p = p->next_pseudo)
774 if (p->pid == gdb_tid)
778 /* Perhaps it's the tid of a deleted thread we may still
779 * have some knowledge of?
781 for (p = deleted_threads.head; p; p = p->next)
783 if (p->tid == gdb_tid)
787 /* Or perhaps it's the pid of a deleted process we may still
790 for (p = deleted_threads.head_pseudo; p != NULL; p = p->next_pseudo)
792 if (p->pid == gdb_tid)
796 return 0; /* Error? */
799 /* Map the other way: from a real tid to the
800 * "pid" known by core gdb. This tid may be
801 * for a thread that just got deleted, so we
802 * also need to consider deleted threads.
805 map_to_gdb_tid (lwpid_t real_tid)
809 for (p = thread_head.head; p; p = p->next)
811 if (p->tid == real_tid)
820 for (p = deleted_threads.head; p; p = p->next)
822 if (p->tid == real_tid)
824 return p->pid; /* Error? */
829 return 0; /* Error? Never heard of this thread! */
832 /* Do any threads have saved signals?
835 saved_signals_exist (void)
839 for (p = thread_head.head; p; p = p->next)
850 /* Is this the tid for the zero-th thread?
853 is_pseudo_thread (lwpid_t tid)
855 thread_info *p = find_thread_info (tid);
856 if (NULL == p || p->terminated)
862 /* Is this thread terminated?
865 is_terminated (lwpid_t tid)
867 thread_info *p = find_thread_info (tid);
870 return p->terminated;
875 /* Is this pid a real PID or a TID?
878 is_process_id (int pid)
885 /* What does PID really represent?
887 tid = map_from_gdb_tid (pid);
889 return 0; /* Actually, is probably an error... */
891 tinfo = find_thread_info (tid);
893 /* Does it appear to be a true thread?
895 if (!tinfo->am_pseudo)
898 /* Else, it looks like it may be a process. See if there's any other
899 * threads with the same process ID, though. If there are, then TID
900 * just happens to be the first thread of several for this process.
902 this_pid = tinfo->pid;
904 for (tinfo = thread_head.head; tinfo; tinfo = tinfo->next)
906 if (tinfo->pid == this_pid)
910 return (this_pid_count == 1);
914 /* Add a thread to our info. Prevent duplicate entries.
917 add_tthread (int pid, lwpid_t tid)
921 p = find_thread_info (tid);
923 p = create_thread_info (pid, tid);
928 /* Notice that a thread was deleted.
931 del_tthread (lwpid_t tid)
936 if (thread_head.count <= 0)
938 error ("Internal error in thread database.");
943 for (p = thread_head.head; p; p = p->next)
950 printf ("Delete here: %d \n", tid);
956 * Deleting a main thread is ok if we're doing
957 * a parent-follow on a child; this is odd but
958 * not wrong. It apparently _doesn't_ happen
959 * on the child-follow, as we don't just delete
960 * the pseudo while keeping the rest of the
961 * threads around--instead, we clear out the whole
962 * thread list at once.
965 thread_info *q_chase;
968 for (q = thread_head.head_pseudo; q; q = q->next)
972 /* Remove from pseudo list.
975 thread_head.head_pseudo = p->next_pseudo;
977 q_chase->next = p->next_pseudo;
984 /* Remove from live list.
989 thread_head.head = p->next;
991 chase->next = p->next;
993 /* Add to deleted thread list.
995 p->next = deleted_threads.head;
996 deleted_threads.head = p;
997 deleted_threads.count++;
1000 p->next_pseudo = deleted_threads.head_pseudo;
1001 deleted_threads.head_pseudo = p;
1013 /* Get the pid for this tid. (Has to be a real TID!).
1016 get_pid_for (lwpid_t tid)
1020 for (p = thread_head.head; p; p = p->next)
1028 for (p = deleted_threads.head; p; p = p->next)
1039 /* Note that this thread's current event has been handled.
1042 set_handled (int pid, lwpid_t tid)
1046 p = find_thread_info (tid);
1048 p = add_tthread (pid, tid);
1053 /* Was this thread's current event handled?
1056 was_handled (lwpid_t tid)
1060 p = find_thread_info (tid);
1064 return 0; /* New threads have not been handled */
1067 /* Set this thread to unhandled.
1070 clear_handled (lwpid_t tid)
1074 #ifdef WAIT_BUFFER_DEBUG
1076 printf ("clear_handled %d\n", (int) tid);
1079 p = find_thread_info (tid);
1081 error ("Internal error: No thread state to clear?");
1086 /* Set all threads to unhandled.
1089 clear_all_handled (void)
1093 #ifdef WAIT_BUFFER_DEBUG
1095 printf ("clear_all_handled\n");
1098 for (p = thread_head.head; p; p = p->next)
1103 for (p = deleted_threads.head; p; p = p->next)
1109 /* Set this thread to default stepping mode.
1112 clear_stepping_mode (lwpid_t tid)
1116 #ifdef WAIT_BUFFER_DEBUG
1118 printf ("clear_stepping_mode %d\n", (int) tid);
1121 p = find_thread_info (tid);
1123 error ("Internal error: No thread state to clear?");
1125 p->stepping_mode = DO_DEFAULT;
1128 /* Set all threads to do default continue on resume.
1131 clear_all_stepping_mode (void)
1135 #ifdef WAIT_BUFFER_DEBUG
1137 printf ("clear_all_stepping_mode\n");
1140 for (p = thread_head.head; p; p = p->next)
1142 p->stepping_mode = DO_DEFAULT;
1145 for (p = deleted_threads.head; p; p = p->next)
1147 p->stepping_mode = DO_DEFAULT;
1151 /* Set all threads to unseen on this pass.
1154 set_all_unseen (void)
1158 for (p = thread_head.head; p; p = p->next)
1164 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1165 /* debugging routine.
1168 print_tthread (thread_info *p)
1170 printf (" Thread pid %d, tid %d", p->pid, p->tid);
1172 printf (", event is %s",
1173 get_printable_name_of_ttrace_event (p->last_stop_state.tts_event));
1176 printf (", pseudo thread");
1179 printf (", have signal 0x%x", p->signal_value);
1182 printf (", have start at 0x%x", p->start);
1184 printf (", step is %s", get_printable_name_of_stepping_mode (p->stepping_mode));
1187 printf (", handled");
1189 printf (", not handled");
1194 printf (", not seen");
1200 print_tthreads (void)
1204 if (thread_head.count == 0)
1205 printf ("Thread list is empty\n");
1208 printf ("Thread list has ");
1209 if (thread_head.count == 1)
1210 printf ("1 entry:\n");
1212 printf ("%d entries:\n", thread_head.count);
1213 for (p = thread_head.head; p; p = p->next)
1219 if (deleted_threads.count == 0)
1220 printf ("Deleted thread list is empty\n");
1223 printf ("Deleted thread list has ");
1224 if (deleted_threads.count == 1)
1225 printf ("1 entry:\n");
1227 printf ("%d entries:\n", deleted_threads.count);
1229 for (p = deleted_threads.head; p; p = p->next)
1237 /* Update the thread list based on the "seen" bits.
1240 update_thread_list (void)
1246 for (p = thread_head.head; p; p = p->next)
1248 /* Is this an "unseen" thread which really happens to be a process?
1249 If so, is it inferior_ptid and is a vfork in flight? If yes to
1250 all, then DON'T REMOVE IT! We're in the midst of moving a vfork
1251 operation, which is a multiple step thing, to the point where we
1252 can touch the parent again. We've most likely stopped to examine
1253 the child at a late stage in the vfork, and if we're not following
1254 the child, we'd best not treat the parent as a dead "thread"...
1256 if ((!p->seen) && p->am_pseudo && vfork_in_flight
1257 && (p->pid != vforking_child_pid))
1267 printf ("Delete unseen thread: %d \n", p->tid);
1269 del_tthread (p->tid);
1276 /************************************************
1277 * O/S call wrappers *
1278 ************************************************
1281 /* This function simply calls ttrace with the given arguments.
1282 * It exists so that all calls to ttrace are isolated. All
1283 * parameters should be as specified by "man 2 ttrace".
1285 * No other "raw" calls to ttrace should exist in this module.
1288 call_real_ttrace (ttreq_t request, pid_t pid, lwpid_t tid, TTRACE_ARG_TYPE addr,
1289 TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2)
1294 tt_status = ttrace (request, pid, tid, addr, data, addr2);
1299 /* Don't bother for a known benign error: if you ask for the
1300 * first thread state, but there is only one thread and it's
1301 * not stopped, ttrace complains.
1303 * We have this inside the #ifdef because our caller will do
1304 * this check for real.
1306 if (request != TT_PROC_GET_FIRST_LWP_STATE
1310 printf ("TT fail for %s, with pid %d, tid %d, status %d \n",
1311 get_printable_name_of_ttrace_request (request),
1312 pid, tid, tt_status);
1318 /* ??rehrauer: It would probably be most robust to catch and report
1319 * failed requests here. However, some clients of this interface
1320 * seem to expect to catch & deal with them, so we'd best not.
1324 strcpy (reason_for_failure, "ttrace (");
1325 strcat (reason_for_failure, get_printable_name_of_ttrace_request (request));
1326 strcat (reason_for_failure, ")");
1327 printf ("ttrace error, errno = %d\n", errno);
1328 perror_with_name (reason_for_failure);
1336 /* This function simply calls ttrace_wait with the given arguments.
1337 * It exists so that all calls to ttrace_wait are isolated.
1339 * No "raw" calls to ttrace_wait should exist elsewhere.
1342 call_real_ttrace_wait (int pid, lwpid_t tid, ttwopt_t option, ttstate_t *tsp,
1346 thread_info *tinfo = NULL;
1349 ttw_status = ttrace_wait (pid, tid, option, tsp, tsp_size);
1355 printf ("TW fail with pid %d, tid %d \n", pid, tid);
1358 perror_with_name ("ttrace wait");
1365 /* A process may have one or more kernel threads, of which all or
1366 none may be stopped. This function returns the ID of the first
1367 kernel thread in a stopped state, or 0 if none are stopped.
1369 This function can be used with get_process_next_stopped_thread_id
1370 to iterate over the IDs of all stopped threads of this process.
1373 get_process_first_stopped_thread_id (int pid, ttstate_t *thread_state)
1377 tt_status = call_real_ttrace (TT_PROC_GET_FIRST_LWP_STATE,
1380 (TTRACE_ARG_TYPE) thread_state,
1381 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1386 if (errno == EPROTO)
1388 /* This is an error we can handle: there isn't any stopped
1389 * thread. This happens when we're re-starting the application
1390 * and it has only one thread. GET_NEXT handles the case of
1391 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1399 perror_with_name ("ttrace");
1407 return thread_state->tts_lwpid;
1411 /* This function returns the ID of the "next" kernel thread in a
1412 stopped state, or 0 if there are none. "Next" refers to the
1413 thread following that of the last successful call to this
1414 function or to get_process_first_stopped_thread_id, using
1415 the value of thread_state returned by that call.
1417 This function can be used with get_process_first_stopped_thread_id
1418 to iterate over the IDs of all stopped threads of this process.
1421 get_process_next_stopped_thread_id (int pid, ttstate_t *thread_state)
1425 tt_status = call_real_ttrace (
1426 TT_PROC_GET_NEXT_LWP_STATE,
1429 (TTRACE_ARG_TYPE) thread_state,
1430 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1433 perror_with_name ("ttrace");
1440 else if (tt_status == 0)
1442 /* End of list, no next state. Don't return the
1443 * tts_lwpid, as it's a meaningless "240".
1445 * This is an HPUX "feature".
1450 return thread_state->tts_lwpid;
1453 /* ??rehrauer: Eventually this function perhaps should be calling
1454 pid_to_thread_id. However, that function currently does nothing
1455 for HP-UX. Even then, I'm not clear whether that function
1456 will return a "kernel" thread ID, or a "user" thread ID. If
1457 the former, we can just call it here. If the latter, we must
1458 map from the "user" tid to a "kernel" tid.
1460 NOTE: currently not called.
1463 get_active_tid_of_pid (int pid)
1465 ttstate_t thread_state;
1467 return get_process_first_stopped_thread_id (pid, &thread_state);
1470 /* This function returns 1 if tt_request is a ttrace request that
1471 * operates upon all threads of a (i.e., the entire) process.
1474 is_process_ttrace_request (ttreq_t tt_request)
1476 return IS_TTRACE_PROCREQ (tt_request);
1480 /* This function translates a thread ttrace request into
1481 * the equivalent process request for a one-thread process.
1484 make_process_version (ttreq_t request)
1486 if (!IS_TTRACE_REQ (request))
1488 error ("Internal error, bad ttrace request made\n");
1495 return TT_PROC_STOP;
1497 case TT_LWP_CONTINUE:
1498 return TT_PROC_CONTINUE;
1500 case TT_LWP_GET_EVENT_MASK:
1501 return TT_PROC_GET_EVENT_MASK;
1503 case TT_LWP_SET_EVENT_MASK:
1504 return TT_PROC_SET_EVENT_MASK;
1509 case TT_LWP_GET_STATE:
1510 return -1; /* No equivalent */
1518 /* This function translates the "pid" used by the rest of
1519 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1520 * with the given arguments.
1522 * In general, other parts of this module should call this
1523 * function when they are dealing with external users, who only
1524 * have tids to pass (but they call it "pid" for historical
1528 call_ttrace (ttreq_t request, int gdb_tid, TTRACE_ARG_TYPE addr,
1529 TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2)
1533 ttreq_t new_request;
1535 char reason_for_failure[100]; /* Arbitrary size, should be big enough. */
1538 int is_interesting = 0;
1540 if (TT_LWP_RUREGS == request)
1542 is_interesting = 1; /* Adjust code here as desired */
1545 if (is_interesting && 0 && debug_on)
1547 if (!is_process_ttrace_request (request))
1549 printf ("TT: Thread request, tid is %d", gdb_tid);
1550 printf ("== SINGLE at %x", addr);
1554 printf ("TT: Process request, tid is %d\n", gdb_tid);
1555 printf ("==! SINGLE at %x", addr);
1560 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1561 * which happen before any threads get set up) should go
1562 * directly to "call_real_ttrace", so they don't happen here.
1564 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1568 if (request == TT_PROC_SETTRC && debug_on)
1569 printf ("Unexpected call for TT_PROC_SETTRC\n");
1572 /* Sometimes we get called with a bogus tid (e.g., if a
1573 * thread has terminated, we return 0; inftarg later asks
1574 * whether the thread has exited/forked/vforked).
1578 errno = ESRCH; /* ttrace's response would probably be "No such process". */
1582 /* All other cases should be able to expect that there are
1585 if (!any_thread_records ())
1589 warning ("No thread records for ttrace call");
1591 errno = ESRCH; /* ttrace's response would be "No such process". */
1595 /* OK, now the task is to translate the incoming tid into
1598 real_tid = map_from_gdb_tid (gdb_tid);
1599 real_pid = get_pid_for (real_tid);
1601 /* Now check the result. "Real_pid" is NULL if our list
1602 * didn't find it. We have some tricks we can play to fix
1607 ttstate_t thread_state;
1611 printf ("No saved pid for tid %d\n", gdb_tid);
1614 if (is_process_ttrace_request (request))
1617 /* Ok, we couldn't get a tid. Try to translate to
1618 * the equivalent process operation. We expect this
1619 * NOT to happen, so this is a desparation-type
1620 * move. It can happen if there is an internal
1621 * error and so no "wait()" call is ever done.
1623 new_request = make_process_version (request);
1624 if (new_request == -1)
1629 printf ("...and couldn't make process version of thread operation\n");
1632 /* Use hacky saved pid, which won't always be correct
1633 * in the multi-process future. Use tid as thread,
1634 * probably dooming this to failure. FIX!
1636 if (! ptid_equal (saved_real_ptid, null_ptid))
1640 printf ("...using saved pid %d\n",
1641 PIDGET (saved_real_ptid));
1644 real_pid = PIDGET (saved_real_ptid);
1649 error ("Unable to perform thread operation");
1654 /* Sucessfully translated this to a process request,
1655 * which needs no thread value.
1659 request = new_request;
1664 printf ("Translated thread request to process request\n");
1665 if (ptid_equal (saved_real_ptid, null_ptid))
1666 printf ("...but there's no saved pid\n");
1670 if (gdb_tid != PIDGET (saved_real_ptid))
1671 printf ("...but have the wrong pid (%d rather than %d)\n",
1672 gdb_tid, PIDGET (saved_real_ptid));
1676 } /* Translated to a process request */
1677 } /* Is a process request */
1681 /* We have to have a thread. Ooops.
1683 error ("Thread request with no threads (%s)",
1684 get_printable_name_of_ttrace_request (request));
1688 /* Ttrace doesn't like to see tid values on process requests,
1689 * even if we have the right one.
1691 if (is_process_ttrace_request (request))
1697 if (is_interesting && 0 && debug_on)
1699 printf (" now tid %d, pid %d\n", real_tid, real_pid);
1700 printf (" request is %s\n", get_printable_name_of_ttrace_request (request));
1704 /* Finally, the (almost) real call.
1706 tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2);
1709 if (is_interesting && debug_on)
1711 if (!TT_OK (tt_status, errno)
1712 && !(tt_status == 0 & errno == 0))
1713 printf (" got error (errno==%d, status==%d)\n", errno, tt_status);
1721 /* Stop all the threads of a process.
1723 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1724 * to get a TTEVT_NONE event, discarding the old event. Be
1725 * very careful, and only call TT_PROC_STOP when you mean it!
1728 stop_all_threads_of_process (pid_t real_pid)
1732 ttw_status = call_real_ttrace (TT_PROC_STOP,
1735 (TTRACE_ARG_TYPE) TT_NIL,
1736 (TTRACE_ARG_TYPE) TT_NIL,
1739 perror_with_name ("ttrace stop of other threads");
1743 /* Under some circumstances, it's unsafe to attempt to stop, or even
1744 query the state of, a process' threads.
1746 In ttrace-based HP-UX, an example is a vforking child process. The
1747 vforking parent and child are somewhat fragile, w/r/t what we can do
1748 what we can do to them with ttrace, until after the child exits or
1749 execs, or until the parent's vfork event is delivered. Until that
1750 time, we must not try to stop the process' threads, or inquire how
1751 many there are, or even alter its data segments, or it typically dies
1752 with a SIGILL. Sigh.
1754 This function returns 1 if this stopped process, and the event that
1755 we're told was responsible for its current stopped state, cannot safely
1756 have its threads examined.
1758 #define CHILD_VFORKED(evt,pid) \
1759 (((evt) == TTEVT_VFORK) && ((pid) != PIDGET (inferior_ptid)))
1760 #define CHILD_URPED(evt,pid) \
1761 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1762 #define PARENT_VFORKED(evt,pid) \
1763 (((evt) == TTEVT_VFORK) && ((pid) == PIDGET (inferior_ptid)))
1766 can_touch_threads_of_process (int pid, ttevents_t stopping_event)
1768 if (CHILD_VFORKED (stopping_event, pid))
1770 vforking_child_pid = pid;
1771 vfork_in_flight = 1;
1774 else if (vfork_in_flight &&
1775 (PARENT_VFORKED (stopping_event, pid) ||
1776 CHILD_URPED (stopping_event, pid)))
1778 vfork_in_flight = 0;
1779 vforking_child_pid = 0;
1782 return !vfork_in_flight;
1786 /* If we can find an as-yet-unhandled thread state of a
1787 * stopped thread of this process return 1 and set "tsp".
1788 * Return 0 if we can't.
1790 * If this function is used when the threads of PIS haven't
1791 * been stopped, undefined behaviour is guaranteed!
1794 select_stopped_thread_of_process (int pid, ttstate_t *tsp)
1796 lwpid_t candidate_tid, tid;
1797 ttstate_t candidate_tstate, tstate;
1799 /* If we're not allowed to touch the process now, then just
1800 * return the current value of *TSP.
1802 * This supports "vfork". It's ok, really, to double the
1803 * current event (the child EXEC, we hope!).
1805 if (!can_touch_threads_of_process (pid, tsp->tts_event))
1808 /* Decide which of (possibly more than one) events to
1809 * return as the first one. We scan them all so that
1810 * we always return the result of a fake-step first.
1813 for (tid = get_process_first_stopped_thread_id (pid, &tstate);
1815 tid = get_process_next_stopped_thread_id (pid, &tstate))
1817 /* TTEVT_NONE events are uninteresting to our clients. They're
1818 * an artifact of our "stop the world" model--the thread is
1819 * stopped because we stopped it.
1821 if (tstate.tts_event == TTEVT_NONE)
1823 set_handled (pid, tstate.tts_lwpid);
1826 /* Did we just single-step a single thread, without letting any
1827 * of the others run? Is this an event for that thread?
1829 * If so, we believe our client would prefer to see this event
1830 * over any others. (Typically the client wants to just push
1831 * one thread a little farther forward, and then go around
1832 * checking for what all threads are doing.)
1834 else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid))
1836 #ifdef WAIT_BUFFER_DEBUG
1837 /* It's possible here to see either a SIGTRAP (due to
1838 * successful completion of a step) or a SYSCALL_ENTRY
1839 * (due to a step completion with active hardware
1843 printf ("Ending fake step with tid %d, state %s\n",
1845 get_printable_name_of_ttrace_event (tstate.tts_event));
1848 /* Remember this one, and throw away any previous
1851 candidate_tid = tstate.tts_lwpid;
1852 candidate_tstate = tstate;
1855 #ifdef FORGET_DELETED_BPTS
1857 /* We can't just do this, as if we do, and then wind
1858 * up the loop with no unhandled events, we need to
1859 * handle that case--the appropriate reaction is to
1860 * just continue, but there's no easy way to do that.
1862 * Better to put this in the ttrace_wait call--if, when
1863 * we fake a wait, we update our events based on the
1864 * breakpoint_here_pc call and find there are no more events,
1865 * then we better continue and so on.
1867 * Or we could put it in the next/continue fake.
1868 * But it has to go in the buffering code, not in the
1869 * real go/wait code.
1871 else if ((TTEVT_SIGNAL == tstate.tts_event)
1872 && (5 == tstate.tts_u.tts_signal.tts_signo)
1873 && (0 != get_raw_pc (tstate.tts_lwpid))
1874 && !breakpoint_here_p (get_raw_pc (tstate.tts_lwpid)))
1877 * If the user deleted a breakpoint while this
1878 * breakpoint-hit event was buffered, we can forget
1881 #ifdef WAIT_BUFFER_DEBUG
1883 printf ("Forgetting deleted bp hit for thread %d\n",
1887 set_handled (pid, tstate.tts_lwpid);
1891 /* Else, is this the first "unhandled" event? If so,
1892 * we believe our client wants to see it (if we don't
1893 * see a fake-step later on in the scan).
1895 else if (!was_handled (tstate.tts_lwpid) && candidate_tid == 0)
1897 candidate_tid = tstate.tts_lwpid;
1898 candidate_tstate = tstate;
1901 /* This is either an event that has already been "handled",
1902 * and thus we believe is uninteresting to our client, or we
1903 * already have a candidate event. Ignore it...
1907 /* What do we report?
1909 if (doing_fake_step)
1911 if (candidate_tid == fake_step_tid)
1915 tstate = candidate_tstate;
1919 warning ("Internal error: fake-step failed to complete.");
1923 else if (candidate_tid != 0)
1925 /* Found a candidate unhandled event.
1927 tstate = candidate_tstate;
1931 warning ("Internal error in call of ttrace_wait.");
1936 warning ("Internal error: no unhandled thread event to select");
1940 copy_ttstate_t (tsp, &tstate);
1942 } /* End of select_stopped_thread_of_process */
1945 /* Check our internal thread data against the real thing.
1948 check_thread_consistency (pid_t real_pid)
1950 int tid; /* really lwpid_t */
1954 /* Spin down the O/S list of threads, checking that they
1955 * match what we've got.
1957 for (tid = get_process_first_stopped_thread_id (real_pid, &tstate);
1959 tid = get_process_next_stopped_thread_id (real_pid, &tstate))
1962 p = find_thread_info (tid);
1966 warning ("No internal thread data for thread %d.", tid);
1972 warning ("Inconsistent internal thread data for thread %d.", tid);
1977 warning ("Thread %d is not terminated, internal error.", tid);
1982 #define TT_COMPARE( fld ) \
1983 tstate.fld != p->last_stop_state.fld
1987 if (TT_COMPARE (tts_pid)
1988 || TT_COMPARE (tts_lwpid)
1989 || TT_COMPARE (tts_user_tid)
1990 || TT_COMPARE (tts_event)
1991 || TT_COMPARE (tts_flags)
1992 || TT_COMPARE (tts_scno)
1993 || TT_COMPARE (tts_scnargs))
1995 warning ("Internal thread data for thread %d is wrong.", tid);
2001 #endif /* PARANOIA */
2004 /* This function wraps calls to "call_real_ttrace_wait" so
2005 * that a actual wait is only done when all pending events
2006 * have been reported.
2008 * Note that typically it is called with a pid of "0", i.e.
2009 * the "don't care" value.
2011 * Return value is the status of the pseudo wait.
2014 call_ttrace_wait (int pid, ttwopt_t option, ttstate_t *tsp, size_t tsp_size)
2016 /* This holds the actual, for-real, true process ID.
2018 static int real_pid;
2020 /* As an argument to ttrace_wait, zero pid
2021 * means "Any process", and zero tid means
2022 * "Any thread of the specified process".
2025 lwpid_t wait_tid = 0;
2028 int ttw_status = 0; /* To be returned */
2030 thread_info *tinfo = NULL;
2038 printf ("TW: Pid to wait on is %d\n", pid);
2041 if (!any_thread_records ())
2042 error ("No thread records for ttrace call w. specific pid");
2044 /* OK, now the task is to translate the incoming tid into
2047 real_tid = map_from_gdb_tid (pid);
2048 real_pid = get_pid_for (real_tid);
2051 printf ("==TW: real pid %d, real tid %d\n", real_pid, real_tid);
2056 /* Sanity checks and set-up.
2059 * Stopped Running Fake-step (v)Fork
2060 * \________________________________________
2062 * No buffered events | error wait wait wait
2064 * Buffered events | debuffer error wait debuffer (?)
2067 if (more_events_left == 0)
2070 if (process_state == RUNNING)
2072 /* OK--normal call of ttrace_wait with no buffered events.
2076 else if (process_state == FAKE_STEPPING)
2078 /* Ok--call of ttrace_wait to support
2079 * fake stepping with no buffered events.
2081 * But we better be fake-stepping!
2083 if (!doing_fake_step)
2085 warning ("Inconsistent thread state.");
2088 else if ((process_state == FORKING)
2089 || (process_state == VFORKING))
2091 /* Ok--there are two processes, so waiting
2092 * for the second while the first is stopped
2093 * is ok. Handled bits stay as they were.
2097 else if (process_state == STOPPED)
2099 warning ("Process not running at wait call.");
2104 warning ("Inconsistent process state.");
2111 if (process_state == STOPPED)
2113 /* OK--buffered events being unbuffered.
2117 else if (process_state == RUNNING)
2119 /* An error--shouldn't have buffered events
2122 warning ("Trying to continue with buffered events:");
2124 else if (process_state == FAKE_STEPPING)
2127 * Better be fake-stepping!
2129 if (!doing_fake_step)
2131 warning ("Losing buffered thread events!\n");
2134 else if ((process_state == FORKING)
2135 || (process_state == VFORKING))
2137 /* Ok--there are two processes, so waiting
2138 * for the second while the first is stopped
2139 * is ok. Handled bits stay as they were.
2144 warning ("Process in unknown state with buffered events.");
2147 /* Sometimes we have to wait for a particular thread
2148 * (if we're stepping over a bpt). In that case, we
2149 * _know_ it's going to complete the single-step we
2150 * asked for (because we're only doing the step under
2151 * certain very well-understood circumstances), so it
2154 if (doing_fake_step)
2156 wait_tid = fake_step_tid;
2157 wait_pid = get_pid_for (fake_step_tid);
2159 #ifdef WAIT_BUFFER_DEBUG
2161 printf ("Doing a wait after a fake-step for %d, pid %d\n",
2162 wait_tid, wait_pid);
2166 if (more_events_left == 0 /* No buffered events, need real ones. */
2167 || process_state != STOPPED)
2169 /* If there are no buffered events, and so we need
2170 * real ones, or if we are FORKING, VFORKING,
2171 * FAKE_STEPPING or RUNNING, and thus have to do
2172 * a real wait, then do a real wait.
2175 #ifdef WAIT_BUFFER_DEBUG
2176 /* Normal case... */
2178 printf ("TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid);
2181 /* The actual wait call.
2183 ttw_status = call_real_ttrace_wait (wait_pid, wait_tid, option, tsp, tsp_size);
2185 /* Note that the routines we'll call will be using "call_real_ttrace",
2186 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2187 * the rest of the world uses (which is actually the tid).
2189 real_pid = tsp->tts_pid;
2191 /* For most events: Stop the world!
2193 * It's sometimes not safe to stop all threads of a process.
2194 * Sometimes it's not even safe to ask for the thread state
2197 if (can_touch_threads_of_process (real_pid, tsp->tts_event))
2199 /* If we're really only stepping a single thread, then don't
2200 * try to stop all the others -- we only do this single-stepping
2201 * business when all others were already stopped...and the stop
2202 * would mess up other threads' events.
2204 * Similiarly, if there are other threads with events,
2205 * don't do the stop.
2207 if (!doing_fake_step)
2209 if (more_events_left > 0)
2210 warning ("Internal error in stopping process");
2212 stop_all_threads_of_process (real_pid);
2214 /* At this point, we could scan and update_thread_list(),
2215 * and only use the local list for the rest of the
2216 * module! We'd get rid of the scans in the various
2217 * continue routines (adding one in attach). It'd
2218 * be great--UPGRADE ME!
2226 if (more_events_left > 0)
2227 printf ("== Can't stop process; more events!\n");
2229 printf ("== Can't stop process!\n");
2233 process_state = STOPPED;
2235 #ifdef WAIT_BUFFER_DEBUG
2237 printf ("Process set to STOPPED\n");
2243 /* Fake a call to ttrace_wait. The process must be
2244 * STOPPED, as we aren't going to do any wait.
2246 #ifdef WAIT_BUFFER_DEBUG
2248 printf ("TW: fake it\n");
2251 if (process_state != STOPPED)
2253 warning ("Process not stopped at wait call, in state '%s'.\n",
2254 get_printable_name_of_process_state (process_state));
2257 if (doing_fake_step)
2258 error ("Internal error in stepping over breakpoint");
2260 ttw_status = 0; /* Faking it is always successful! */
2261 } /* End of fake or not? if */
2263 /* Pick an event to pass to our caller. Be paranoid.
2265 if (!select_stopped_thread_of_process (real_pid, tsp))
2266 warning ("Can't find event, using previous event.");
2268 else if (tsp->tts_event == TTEVT_NONE)
2269 warning ("Internal error: no thread has a real event.");
2271 else if (doing_fake_step)
2273 if (fake_step_tid != tsp->tts_lwpid)
2274 warning ("Internal error in stepping over breakpoint.");
2276 /* This wait clears the (current) fake-step if there was one.
2278 doing_fake_step = 0;
2282 /* We now have a correct tsp and ttw_status for the thread
2283 * which we want to report. So it's "handled"! This call
2284 * will add it to our list if it's not there already.
2286 set_handled (real_pid, tsp->tts_lwpid);
2288 /* Save a copy of the ttrace state of this thread, in our local
2291 This caches the state. The implementation of queries like
2292 target_has_execd can then use this cached state, rather than
2293 be forced to make an explicit ttrace call to get it.
2295 (Guard against the condition that this is the first time we've
2296 waited on, i.e., seen this thread, and so haven't yet entered
2297 it into our list of threads.)
2299 tinfo = find_thread_info (tsp->tts_lwpid);
2302 copy_ttstate_t (&tinfo->last_stop_state, tsp);
2303 tinfo->have_state = 1;
2307 } /* call_ttrace_wait */
2309 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2311 child_reported_exec_events_per_exec_call (void)
2313 return 1; /* ttrace reports the event once per call. */
2319 /* Our implementation of hardware watchpoints involves making memory
2320 pages write-protected. We must remember a page's original permissions,
2321 and we must also know when it is appropriate to restore a page's
2322 permissions to its original state.
2324 We use a "dictionary" of hardware-watched pages to do this. Each
2325 hardware-watched page is recorded in the dictionary. Each page's
2326 dictionary entry contains the original permissions and a reference
2327 count. Pages are hashed into the dictionary by their start address.
2329 When hardware watchpoint is set on page X for the first time, page X
2330 is added to the dictionary with a reference count of 1. If other
2331 hardware watchpoints are subsequently set on page X, its reference
2332 count is incremented. When hardware watchpoints are removed from
2333 page X, its reference count is decremented. If a page's reference
2334 count drops to 0, it's permissions are restored and the page's entry
2335 is thrown out of the dictionary.
2337 typedef struct memory_page
2339 CORE_ADDR page_start;
2340 int reference_count;
2341 int original_permissions;
2342 struct memory_page *next;
2343 struct memory_page *previous;
2347 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2353 int page_protections_allowed;
2354 /* These are just the heads of chains of actual page descriptors. */
2355 memory_page_t buckets[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT];
2357 memory_page_dictionary;
2361 require_memory_page_dictionary (void)
2365 /* Is the memory page dictionary ready for use? If so, we're done. */
2366 if (memory_page_dictionary.page_count >= (LONGEST) 0)
2369 /* Else, initialize it. */
2370 memory_page_dictionary.page_count = (LONGEST) 0;
2372 for (i = 0; i < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++)
2374 memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0;
2375 memory_page_dictionary.buckets[i].reference_count = 0;
2376 memory_page_dictionary.buckets[i].next = NULL;
2377 memory_page_dictionary.buckets[i].previous = NULL;
2383 retire_memory_page_dictionary (void)
2385 memory_page_dictionary.page_count = (LONGEST) - 1;
2389 /* Write-protect the memory page that starts at this address.
2391 Returns the original permissions of the page.
2394 write_protect_page (int pid, CORE_ADDR page_start)
2397 int original_permissions;
2398 int new_permissions;
2400 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
2402 (TTRACE_ARG_TYPE) page_start,
2404 (TTRACE_ARG_TYPE) & original_permissions);
2405 if (errno || (tt_status < 0))
2407 return 0; /* What else can we do? */
2410 /* We'll also write-protect the page now, if that's allowed. */
2411 if (memory_page_dictionary.page_protections_allowed)
2413 new_permissions = original_permissions & ~PROT_WRITE;
2414 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2416 (TTRACE_ARG_TYPE) page_start,
2417 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2418 (TTRACE_ARG_TYPE) new_permissions);
2419 if (errno || (tt_status < 0))
2421 return 0; /* What else can we do? */
2425 return original_permissions;
2429 /* Unwrite-protect the memory page that starts at this address, restoring
2430 (what we must assume are) its original permissions.
2433 unwrite_protect_page (int pid, CORE_ADDR page_start, int original_permissions)
2437 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2439 (TTRACE_ARG_TYPE) page_start,
2440 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2441 (TTRACE_ARG_TYPE) original_permissions);
2442 if (errno || (tt_status < 0))
2444 return; /* What else can we do? */
2449 /* Memory page-protections are used to implement "hardware" watchpoints
2452 For every memory page that is currently being watched (i.e., that
2453 presently should be write-protected), write-protect it.
2456 hppa_enable_page_protection_events (int pid)
2460 memory_page_dictionary.page_protections_allowed = 1;
2462 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2464 memory_page_t *page;
2466 page = memory_page_dictionary.buckets[bucket].next;
2467 while (page != NULL)
2469 page->original_permissions = write_protect_page (pid, page->page_start);
2476 /* Memory page-protections are used to implement "hardware" watchpoints
2479 For every memory page that is currently being watched (i.e., that
2480 presently is or should be write-protected), un-write-protect it.
2483 hppa_disable_page_protection_events (int pid)
2487 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2489 memory_page_t *page;
2491 page = memory_page_dictionary.buckets[bucket].next;
2492 while (page != NULL)
2494 unwrite_protect_page (pid, page->page_start, page->original_permissions);
2499 memory_page_dictionary.page_protections_allowed = 0;
2502 /* Count the number of outstanding events. At this
2503 * point, we have selected one thread and its event
2504 * as the one to be "reported" upwards to core gdb.
2505 * That thread is already marked as "handled".
2507 * Note: we could just scan our own thread list. FIXME!
2510 count_unhandled_events (int real_pid, lwpid_t real_tid)
2516 /* Ok, find out how many threads have real events to report.
2519 ttid = get_process_first_stopped_thread_id (real_pid, &tstate);
2525 printf ("Process %d has no threads\n", real_pid);
2527 printf ("Process %d has these threads:\n", real_pid);
2533 if (tstate.tts_event != TTEVT_NONE
2534 && !was_handled (ttid))
2536 /* TTEVT_NONE implies we just stopped it ourselves
2537 * because we're the stop-the-world guys, so it's
2538 * not an event from our point of view.
2540 * If "was_handled" is true, this is an event we
2541 * already handled, so don't count it.
2543 * Note that we don't count the thread with the
2544 * currently-reported event, as it's already marked
2550 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2553 if (ttid == real_tid)
2554 printf ("*"); /* Thread we're reporting */
2558 if (tstate.tts_event != TTEVT_NONE)
2559 printf ("+"); /* Thread with a real event */
2563 if (was_handled (ttid))
2564 printf ("h"); /* Thread has been handled */
2568 printf (" %d, with event %s", ttid,
2569 get_printable_name_of_ttrace_event (tstate.tts_event));
2571 if (tstate.tts_event == TTEVT_SIGNAL
2572 && 5 == tstate.tts_u.tts_signal.tts_signo)
2576 pc_val = get_raw_pc (ttid);
2579 printf (" breakpoint at 0x%x\n", pc_val);
2581 printf (" bpt, can't fetch pc.\n");
2588 ttid = get_process_next_stopped_thread_id (real_pid, &tstate);
2591 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2593 if (events_left > 0)
2594 printf ("There are thus %d pending events\n", events_left);
2600 /* This function is provided as a sop to clients that are calling
2601 * ptrace_wait to wait for a process to stop. (see the
2602 * implementation of child_wait.) Return value is the pid for
2603 * the event that ended the wait.
2605 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2608 ptrace_wait (ptid_t ptid, int *status)
2617 /* The ptrace implementation of this also ignores pid.
2621 ttwait_return = call_ttrace_wait (0, TTRACE_WAITOK, &tsp, sizeof (tsp));
2622 if (ttwait_return < 0)
2624 /* ??rehrauer: It appears that if our inferior exits and we
2625 haven't asked for exit events, that we're not getting any
2626 indication save a negative return from ttrace_wait and an
2631 *status = 0; /* WIFEXITED */
2632 return PIDGET (inferior_ptid);
2635 warning ("Call of ttrace_wait returned with errno %d.",
2637 *status = ttwait_return;
2638 return PIDGET (inferior_ptid);
2641 real_pid = tsp.tts_pid;
2642 real_tid = tsp.tts_lwpid;
2644 /* One complication is that the "tts_event" structure has
2645 * a set of flags, and more than one can be set. So we
2646 * either have to force an order (as we do here), or handle
2647 * more than one flag at a time.
2649 if (tsp.tts_event & TTEVT_LWP_CREATE)
2652 /* Unlike what you might expect, this event is reported in
2653 * the _creating_ thread, and the _created_ thread (whose tid
2654 * we have) is still running. So we have to stop it. This
2655 * has already been done in "call_ttrace_wait", but should we
2656 * ever abandon the "stop-the-world" model, here's the command
2659 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2661 * Note that this would depend on being called _after_ "add_tthread"
2662 * below for the tid-to-pid translation to be done in "call_ttrace".
2667 printf ("New thread: pid %d, tid %d, creator tid %d\n",
2668 real_pid, tsp.tts_u.tts_thread.tts_target_lwpid,
2672 /* Now we have to return the tid of the created thread, not
2673 * the creating thread, or "wait_for_inferior" won't know we
2674 * have a new "process" (thread). Plus we should record it
2677 real_tid = tsp.tts_u.tts_thread.tts_target_lwpid;
2679 add_tthread (real_pid, real_tid);
2682 else if ((tsp.tts_event & TTEVT_LWP_TERMINATE)
2683 || (tsp.tts_event & TTEVT_LWP_EXIT))
2688 printf ("Thread dies: %d\n", real_tid);
2691 del_tthread (real_tid);
2694 else if (tsp.tts_event & TTEVT_EXEC)
2699 printf ("Pid %d has zero'th thread %d; inferior pid is %d\n",
2700 real_pid, real_tid, PIDGET (inferior_ptid));
2703 add_tthread (real_pid, real_tid);
2709 printf ("Process-level event %s, using tid %d\n",
2710 get_printable_name_of_ttrace_event (tsp.tts_event),
2713 /* OK to do this, as "add_tthread" won't add
2714 * duplicate entries. Also OK not to do it,
2715 * as this event isn't one which can change the
2718 add_tthread (real_pid, real_tid);
2723 /* How many events are left to report later?
2724 * In a non-stop-the-world model, this isn't needed.
2726 * Note that it's not always safe to query the thread state of a process,
2727 * which is what count_unhandled_events does. (If unsafe, we're left with
2728 * no other resort than to assume that no more events remain...)
2730 if (can_touch_threads_of_process (real_pid, tsp.tts_event))
2731 more_events_left = count_unhandled_events (real_pid, real_tid);
2735 if (more_events_left > 0)
2736 warning ("Vfork or fork causing loss of %d buffered events.",
2739 more_events_left = 0;
2742 /* Attempt to translate the ttrace_wait-returned status into the
2745 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2746 clients that expect to pick apart a ptrace wait status, to use
2747 something a little more abstract.
2749 if ((tsp.tts_event & TTEVT_EXEC)
2750 || (tsp.tts_event & TTEVT_FORK)
2751 || (tsp.tts_event & TTEVT_VFORK))
2753 /* Forks come in pairs (parent and child), so core gdb
2754 * will do two waits. Be ready to notice this.
2756 if (tsp.tts_event & TTEVT_FORK)
2758 process_state = FORKING;
2760 #ifdef WAIT_BUFFER_DEBUG
2762 printf ("Process set to FORKING\n");
2765 else if (tsp.tts_event & TTEVT_VFORK)
2767 process_state = VFORKING;
2769 #ifdef WAIT_BUFFER_DEBUG
2771 printf ("Process set to VFORKING\n");
2775 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2776 but I don't think non HP-UX-specific clients really carefully
2777 inspect the first events they get after inferior startup, so
2778 it probably almost doesn't matter what we claim this is.
2783 printf ("..a process 'event'\n");
2786 /* Also make fork and exec events look like bpts, so they can be caught.
2788 *status = 0177 | (_SIGTRAP << 8);
2791 /* Special-cases: We ask for syscall entry and exit events to implement
2792 "fast" (aka "hardware") watchpoints.
2794 When we get a syscall entry, we want to disable page-protections,
2795 and resume the inferior; this isn't an event we wish for
2796 wait_for_inferior to see. Note that we must resume ONLY the
2797 thread that reported the syscall entry; we don't want to allow
2798 other threads to run with the page protections off, as they might
2799 then be able to write to watch memory without it being caught.
2801 When we get a syscall exit, we want to reenable page-protections,
2802 but we don't want to resume the inferior; this is an event we wish
2803 wait_for_inferior to see. Make it look like the signal we normally
2804 get for a single-step completion. This should cause wait_for_inferior
2805 to evaluate whether any watchpoint triggered.
2807 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2808 due to some HP-UX "features". Some syscalls have problems with
2809 write-protections on some pages, and some syscalls seem to have
2810 pending writes to those pages at the time we're getting the return
2811 event. So, we'll single-step the inferior to get out of the syscall,
2812 and then reenable protections.
2814 Note that we're intentionally allowing the syscall exit case to
2815 fall through into the succeeding cases, as sometimes we single-
2816 step out of one syscall only to immediately enter another...
2818 else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY)
2819 || (tsp.tts_event & TTEVT_SYSCALL_RETURN))
2821 /* Make a syscall event look like a breakpoint. Same comments
2822 as for exec & fork events.
2826 printf ("..a syscall 'event'\n");
2829 /* Also make syscall events look like bpts, so they can be caught.
2831 *status = 0177 | (_SIGTRAP << 8);
2834 else if ((tsp.tts_event & TTEVT_LWP_CREATE)
2835 || (tsp.tts_event & TTEVT_LWP_TERMINATE)
2836 || (tsp.tts_event & TTEVT_LWP_EXIT))
2838 /* Make a thread event look like a breakpoint. Same comments
2839 * as for exec & fork events.
2843 printf ("..a thread 'event'\n");
2846 /* Also make thread events look like bpts, so they can be caught.
2848 *status = 0177 | (_SIGTRAP << 8);
2851 else if ((tsp.tts_event & TTEVT_EXIT))
2856 printf ("..an exit\n");
2859 /* Prevent rest of gdb from thinking this is
2860 * a new thread if for some reason it's never
2861 * seen the main thread before.
2863 inferior_ptid = pid_to_ptid (map_to_gdb_tid (real_tid)); /* HACK, FIX */
2865 *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode);
2868 else if (tsp.tts_event & TTEVT_SIGNAL)
2872 printf ("..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo);
2875 *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8);
2881 /* This means the process or thread terminated. But we should've
2882 caught an explicit exit/termination above. So warn (this is
2883 really an internal error) and claim the process or thread
2884 terminated with a SIGTRAP.
2887 warning ("process_wait: unknown process state");
2891 printf ("Process-level event %s, using tid %d\n",
2892 get_printable_name_of_ttrace_event (tsp.tts_event),
2899 target_post_wait (pid_to_ptid (tsp.tts_pid), *status);
2904 printf ("Done waiting, pid is %d, tid %d\n", real_pid, real_tid);
2907 /* All code external to this module uses the tid, but calls
2908 * it "pid". There's some tweaking so that the outside sees
2909 * the first thread as having the same number as the starting
2912 return_pid = map_to_gdb_tid (real_tid);
2914 /* Remember this for later use in "hppa_prepare_to_proceed".
2916 old_gdb_pid = PIDGET (inferior_ptid);
2917 reported_pid = return_pid;
2918 reported_bpt = ((tsp.tts_event & TTEVT_SIGNAL) && (5 == tsp.tts_u.tts_signal.tts_signo));
2920 if (real_tid == 0 || return_pid == 0)
2922 warning ("Internal error: process-wait failed.");
2929 /* This function causes the caller's process to be traced by its
2930 parent. This is intended to be called after GDB forks itself,
2931 and before the child execs the target. Despite the name, it
2932 is called by the child.
2934 Note that HP-UX ttrace is rather funky in how this is done.
2935 If the parent wants to get the initial exec event of a child,
2936 it must set the ttrace event mask of the child to include execs.
2937 (The child cannot do this itself.) This must be done after the
2938 child is forked, but before it execs.
2940 To coordinate the parent and child, we implement a semaphore using
2941 pipes. After SETTRC'ing itself, the child tells the parent that
2942 it is now traceable by the parent, and waits for the parent's
2943 acknowledgement. The parent can then set the child's event mask,
2944 and notify the child that it can now exec.
2946 (The acknowledgement by parent happens as a result of a call to
2947 child_acknowledge_created_inferior.)
2950 parent_attach_all (void)
2954 /* We need a memory home for a constant, to pass it to ttrace.
2955 The value of the constant is arbitrary, so long as both
2956 parent and child use the same value. Might as well use the
2957 "magic" constant provided by ttrace...
2959 uint64_t tc_magic_child = TT_VERSION;
2960 uint64_t tc_magic_parent = 0;
2962 tt_status = call_real_ttrace (
2967 (TTRACE_ARG_TYPE) TT_VERSION,
2973 /* Notify the parent that we're potentially ready to exec(). */
2974 write (startup_semaphore.child_channel[SEM_TALK],
2976 sizeof (tc_magic_child));
2978 /* Wait for acknowledgement from the parent. */
2979 read (startup_semaphore.parent_channel[SEM_LISTEN],
2981 sizeof (tc_magic_parent));
2983 if (tc_magic_child != tc_magic_parent)
2984 warning ("mismatched semaphore magic");
2986 /* Discard our copy of the semaphore. */
2987 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
2988 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
2989 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
2990 (void) close (startup_semaphore.child_channel[SEM_TALK]);
2995 /* Despite being file-local, this routine is dealing with
2996 * actual process IDs, not thread ids. That's because it's
2997 * called before the first "wait" call, and there's no map
2998 * yet from tids to pids.
3000 * When it is called, a forked child is running, but waiting on
3001 * the semaphore. If you stop the child and re-start it,
3002 * things get confused, so don't do that! An attached child is
3005 * Since this is called after either attach or run, we
3006 * have to be the common part of both.
3009 require_notification_of_events (int real_pid)
3012 ttevent_t notifiable_events;
3015 ttstate_t thread_state;
3019 printf ("Require notif, pid is %d\n", real_pid);
3022 /* Temporary HACK: tell inftarg.c/child_wait to not
3023 * loop until pids are the same.
3025 not_same_real_pid = 0;
3027 sigemptyset (¬ifiable_events.tte_signals);
3028 notifiable_events.tte_opts = TTEO_NONE;
3030 /* This ensures that forked children inherit their parent's
3031 * event mask, which we're setting here.
3033 * NOTE: if you debug gdb with itself, then the ultimate
3034 * debuggee gets flags set by the outermost gdb, as
3035 * a child of a child will still inherit.
3037 notifiable_events.tte_opts |= TTEO_PROC_INHERIT;
3039 notifiable_events.tte_events = TTEVT_DEFAULT;
3040 notifiable_events.tte_events |= TTEVT_SIGNAL;
3041 notifiable_events.tte_events |= TTEVT_EXEC;
3042 notifiable_events.tte_events |= TTEVT_EXIT;
3043 notifiable_events.tte_events |= TTEVT_FORK;
3044 notifiable_events.tte_events |= TTEVT_VFORK;
3045 notifiable_events.tte_events |= TTEVT_LWP_CREATE;
3046 notifiable_events.tte_events |= TTEVT_LWP_EXIT;
3047 notifiable_events.tte_events |= TTEVT_LWP_TERMINATE;
3049 tt_status = call_real_ttrace (
3050 TT_PROC_SET_EVENT_MASK,
3053 (TTRACE_ARG_TYPE) & notifiable_events,
3054 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3059 require_notification_of_exec_events (int real_pid)
3062 ttevent_t notifiable_events;
3065 ttstate_t thread_state;
3069 printf ("Require notif, pid is %d\n", real_pid);
3072 /* Temporary HACK: tell inftarg.c/child_wait to not
3073 * loop until pids are the same.
3075 not_same_real_pid = 0;
3077 sigemptyset (¬ifiable_events.tte_signals);
3078 notifiable_events.tte_opts = TTEO_NOSTRCCHLD;
3080 /* This ensures that forked children don't inherit their parent's
3081 * event mask, which we're setting here.
3083 notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT;
3085 notifiable_events.tte_events = TTEVT_DEFAULT;
3086 notifiable_events.tte_events |= TTEVT_EXEC;
3087 notifiable_events.tte_events |= TTEVT_EXIT;
3089 tt_status = call_real_ttrace (
3090 TT_PROC_SET_EVENT_MASK,
3093 (TTRACE_ARG_TYPE) & notifiable_events,
3094 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3099 /* This function is called by the parent process, with pid being the
3100 * ID of the child process, after the debugger has forked.
3103 child_acknowledge_created_inferior (int pid)
3105 /* We need a memory home for a constant, to pass it to ttrace.
3106 The value of the constant is arbitrary, so long as both
3107 parent and child use the same value. Might as well use the
3108 "magic" constant provided by ttrace...
3110 uint64_t tc_magic_parent = TT_VERSION;
3111 uint64_t tc_magic_child = 0;
3113 /* Wait for the child to tell us that it has forked. */
3114 read (startup_semaphore.child_channel[SEM_LISTEN],
3116 sizeof (tc_magic_child));
3118 /* Clear thread info now. We'd like to do this in
3119 * "require...", but that messes up attach.
3121 clear_thread_info ();
3123 /* Tell the "rest of gdb" that the initial thread exists.
3124 * This isn't really a hack. Other thread-based versions
3125 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3127 * Q: Why don't we also add this thread to the local
3128 * list via "add_tthread"?
3130 * A: Because we don't know the tid, and can't stop the
3131 * the process safely to ask what it is. Anyway, we'll
3132 * add it when it gets the EXEC event.
3134 add_thread (pid_to_ptid (pid)); /* in thread.c */
3136 /* We can now set the child's ttrace event mask.
3138 require_notification_of_exec_events (pid);
3140 /* Tell ourselves that the process is running.
3142 process_state = RUNNING;
3144 /* Notify the child that it can exec. */
3145 write (startup_semaphore.parent_channel[SEM_TALK],
3147 sizeof (tc_magic_parent));
3149 /* Discard our copy of the semaphore. */
3150 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3151 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3152 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3153 (void) close (startup_semaphore.child_channel[SEM_TALK]);
3158 * arrange for notification of all events by
3159 * calling require_notification_of_events.
3162 child_post_startup_inferior (ptid_t ptid)
3164 require_notification_of_events (PIDGET (ptid));
3167 /* From here on, we should expect tids rather than pids.
3170 hppa_enable_catch_fork (int tid)
3173 ttevent_t ttrace_events;
3175 /* Get the set of events that are currently enabled.
3177 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3179 (TTRACE_ARG_TYPE) & ttrace_events,
3180 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3183 perror_with_name ("ttrace");
3185 /* Add forks to that set. */
3186 ttrace_events.tte_events |= TTEVT_FORK;
3190 printf ("enable fork, tid is %d\n", tid);
3193 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3195 (TTRACE_ARG_TYPE) & ttrace_events,
3196 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3199 perror_with_name ("ttrace");
3204 hppa_disable_catch_fork (int tid)
3207 ttevent_t ttrace_events;
3209 /* Get the set of events that are currently enabled.
3211 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3213 (TTRACE_ARG_TYPE) & ttrace_events,
3214 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3218 perror_with_name ("ttrace");
3220 /* Remove forks from that set. */
3221 ttrace_events.tte_events &= ~TTEVT_FORK;
3225 printf ("disable fork, tid is %d\n", tid);
3228 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3230 (TTRACE_ARG_TYPE) & ttrace_events,
3231 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3235 perror_with_name ("ttrace");
3239 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3241 child_insert_fork_catchpoint (int tid)
3243 /* Enable reporting of fork events from the kernel. */
3244 /* ??rehrauer: For the moment, we're always enabling these events,
3245 and just ignoring them if there's no catchpoint to catch them.
3252 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3254 child_remove_fork_catchpoint (int tid)
3256 /* Disable reporting of fork events from the kernel. */
3257 /* ??rehrauer: For the moment, we're always enabling these events,
3258 and just ignoring them if there's no catchpoint to catch them.
3266 hppa_enable_catch_vfork (int tid)
3269 ttevent_t ttrace_events;
3271 /* Get the set of events that are currently enabled.
3273 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3275 (TTRACE_ARG_TYPE) & ttrace_events,
3276 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3280 perror_with_name ("ttrace");
3282 /* Add vforks to that set. */
3283 ttrace_events.tte_events |= TTEVT_VFORK;
3287 printf ("enable vfork, tid is %d\n", tid);
3290 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3292 (TTRACE_ARG_TYPE) & ttrace_events,
3293 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3297 perror_with_name ("ttrace");
3302 hppa_disable_catch_vfork (int tid)
3305 ttevent_t ttrace_events;
3307 /* Get the set of events that are currently enabled. */
3308 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3310 (TTRACE_ARG_TYPE) & ttrace_events,
3311 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3315 perror_with_name ("ttrace");
3317 /* Remove vforks from that set. */
3318 ttrace_events.tte_events &= ~TTEVT_VFORK;
3322 printf ("disable vfork, tid is %d\n", tid);
3324 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3326 (TTRACE_ARG_TYPE) & ttrace_events,
3327 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3331 perror_with_name ("ttrace");
3335 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3337 child_insert_vfork_catchpoint (int tid)
3339 /* Enable reporting of vfork events from the kernel. */
3340 /* ??rehrauer: For the moment, we're always enabling these events,
3341 and just ignoring them if there's no catchpoint to catch them.
3348 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3350 child_remove_vfork_catchpoint (int tid)
3352 /* Disable reporting of vfork events from the kernel. */
3353 /* ??rehrauer: For the moment, we're always enabling these events,
3354 and just ignoring them if there's no catchpoint to catch them.
3360 #if defined(CHILD_HAS_FORKED)
3362 /* Q: Do we need to map the returned process ID to a thread ID?
3364 * A: I don't think so--here we want a _real_ pid. Any later
3365 * operations will call "require_notification_of_events" and
3366 * start the mapping.
3369 child_has_forked (int tid, int *childpid)
3372 ttstate_t ttrace_state;
3375 /* Do we have cached thread state that we can consult? If so, use it. */
3376 tinfo = find_thread_info (map_from_gdb_tid (tid));
3379 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3382 /* Nope, must read the thread's current state */
3385 tt_status = call_ttrace (TT_LWP_GET_STATE,
3387 (TTRACE_ARG_TYPE) & ttrace_state,
3388 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3392 perror_with_name ("ttrace");
3398 if (ttrace_state.tts_event & TTEVT_FORK)
3400 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3409 #if defined(CHILD_HAS_VFORKED)
3411 /* See child_has_forked for pid discussion.
3414 child_has_vforked (int tid, int *childpid)
3417 ttstate_t ttrace_state;
3420 /* Do we have cached thread state that we can consult? If so, use it. */
3421 tinfo = find_thread_info (map_from_gdb_tid (tid));
3423 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3425 /* Nope, must read the thread's current state */
3428 tt_status = call_ttrace (TT_LWP_GET_STATE,
3430 (TTRACE_ARG_TYPE) & ttrace_state,
3431 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3435 perror_with_name ("ttrace");
3441 if (ttrace_state.tts_event & TTEVT_VFORK)
3443 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3452 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3454 child_can_follow_vfork_prior_to_exec (void)
3456 /* ttrace does allow this.
3458 ??rehrauer: However, I had major-league problems trying to
3459 convince wait_for_inferior to handle that case. Perhaps when
3460 it is rewritten to grok multiple processes in an explicit way...
3467 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3469 child_insert_exec_catchpoint (int tid)
3471 /* Enable reporting of exec events from the kernel. */
3472 /* ??rehrauer: For the moment, we're always enabling these events,
3473 and just ignoring them if there's no catchpoint to catch them.
3480 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3482 child_remove_exec_catchpoint (int tid)
3484 /* Disable reporting of execevents from the kernel. */
3485 /* ??rehrauer: For the moment, we're always enabling these events,
3486 and just ignoring them if there's no catchpoint to catch them.
3493 #if defined(CHILD_HAS_EXECD)
3495 child_has_execd (int tid, char **execd_pathname)
3498 ttstate_t ttrace_state;
3501 /* Do we have cached thread state that we can consult? If so, use it. */
3502 tinfo = find_thread_info (map_from_gdb_tid (tid));
3504 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3506 /* Nope, must read the thread's current state */
3509 tt_status = call_ttrace (TT_LWP_GET_STATE,
3511 (TTRACE_ARG_TYPE) & ttrace_state,
3512 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3516 perror_with_name ("ttrace");
3522 if (ttrace_state.tts_event & TTEVT_EXEC)
3524 /* See child_pid_to_exec_file in this file: this is a macro.
3526 char *exec_file = target_pid_to_exec_file (tid);
3528 *execd_pathname = savestring (exec_file, strlen (exec_file));
3537 #if defined(CHILD_HAS_SYSCALL_EVENT)
3539 child_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
3542 ttstate_t ttrace_state;
3545 /* Do we have cached thread state that we can consult? If so, use it. */
3546 tinfo = find_thread_info (map_from_gdb_tid (pid));
3548 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3550 /* Nope, must read the thread's current state */
3553 tt_status = call_ttrace (TT_LWP_GET_STATE,
3555 (TTRACE_ARG_TYPE) & ttrace_state,
3556 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3560 perror_with_name ("ttrace");
3566 *kind = TARGET_WAITKIND_SPURIOUS; /* Until proven otherwise... */
3569 if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY)
3570 *kind = TARGET_WAITKIND_SYSCALL_ENTRY;
3571 else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN)
3572 *kind = TARGET_WAITKIND_SYSCALL_RETURN;
3576 *syscall_id = ttrace_state.tts_scno;
3583 #if defined(CHILD_THREAD_ALIVE)
3585 /* Check to see if the given thread is alive.
3587 * We'll trust the thread list, as the more correct
3588 * approach of stopping the process and spinning down
3589 * the OS's thread list is _very_ expensive.
3591 * May need a FIXME for that reason.
3594 child_thread_alive (ptid_t ptid)
3596 lwpid_t gdb_tid = PIDGET (ptid);
3599 /* This spins down the lists twice.
3600 * Possible peformance improvement here!
3602 tid = map_from_gdb_tid (gdb_tid);
3603 return !is_terminated (tid);
3610 /* This function attempts to read the specified number of bytes from the
3611 save_state_t that is our view into the hardware registers, starting at
3612 ss_offset, and ending at ss_offset + sizeof_buf - 1
3614 If this function succeeds, it deposits the fetched bytes into buf,
3617 If it fails, it returns a negative result. The contents of buf are
3618 undefined it this function fails.
3621 read_from_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf,
3625 register_value_t register_value = 0;
3627 tt_status = call_ttrace (TT_LWP_RUREGS,
3630 (TTRACE_ARG_TYPE) sizeof_buf,
3631 (TTRACE_ARG_TYPE) buf);
3634 /* Map ttrace's version of success to our version.
3635 * Sometime ttrace returns 0, but that's ok here.
3643 /* This function attempts to write the specified number of bytes to the
3644 save_state_t that is our view into the hardware registers, starting at
3645 ss_offset, and ending at ss_offset + sizeof_buf - 1
3647 If this function succeeds, it deposits the bytes in buf, and returns 0.
3649 If it fails, it returns a negative result. The contents of the save_state_t
3650 are undefined it this function fails.
3653 write_to_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf,
3657 register_value_t register_value = 0;
3659 tt_status = call_ttrace (TT_LWP_WUREGS,
3662 (TTRACE_ARG_TYPE) sizeof_buf,
3663 (TTRACE_ARG_TYPE) buf);
3668 /* This function is a sop to the largeish number of direct calls
3669 to call_ptrace that exist in other files. Rather than create
3670 functions whose name abstracts away from ptrace, and change all
3671 the present callers of call_ptrace, we'll do the expedient (and
3672 perhaps only practical) thing.
3674 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3675 process. Thus, we must translate all ptrace requests into their
3676 process-specific, ttrace equivalents.
3679 call_ptrace (int pt_request, int gdb_tid, PTRACE_ARG3_TYPE addr, int data)
3682 TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr;
3683 TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data;
3684 TTRACE_ARG_TYPE tt_addr2 = TT_NIL;
3686 register_value_t register_value;
3689 /* Perform the necessary argument translation. Note that some
3690 cases are funky enough in the ttrace realm that we handle them
3695 /* The following cases cannot conveniently be handled conveniently
3696 by merely adjusting the ptrace arguments and feeding into the
3697 generic call to ttrace at the bottom of this function.
3699 Note that because all branches of this switch end in "return",
3700 there's no need for any "break" statements.
3703 return parent_attach_all ();
3706 tt_status = read_from_register_save_state (gdb_tid,
3709 sizeof (register_value));
3712 return register_value;
3715 register_value = (int) tt_data;
3716 tt_status = write_to_register_save_state (gdb_tid,
3719 sizeof (register_value));
3724 tt_status = call_ttrace (TT_PROC_RDTEXT, /* Implicit 4-byte xfer becomes block-xfer. */
3727 (TTRACE_ARG_TYPE) 4,
3728 (TTRACE_ARG_TYPE) & read_buf);
3734 tt_status = call_ttrace (TT_PROC_RDDATA, /* Implicit 4-byte xfer becomes block-xfer. */
3737 (TTRACE_ARG_TYPE) 4,
3738 (TTRACE_ARG_TYPE) & read_buf);
3744 tt_status = call_real_ttrace (TT_PROC_ATTACH,
3745 map_from_gdb_tid (gdb_tid),
3748 (TTRACE_ARG_TYPE) TT_VERSION,
3754 /* The following cases are handled by merely adjusting the ptrace
3755 arguments and feeding into the generic call to ttrace.
3758 tt_request = TT_PROC_DETACH;
3762 tt_request = TT_PROC_WRTEXT; /* Translates 4-byte xfer to block-xfer. */
3763 tt_data = 4; /* This many bytes. */
3764 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3768 tt_request = TT_PROC_WRDATA; /* Translates 4-byte xfer to block-xfer. */
3769 tt_data = 4; /* This many bytes. */
3770 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3774 tt_request = TT_PROC_RDTEXT;
3778 tt_request = TT_PROC_RDDATA;
3782 tt_request = TT_PROC_WRTEXT;
3786 tt_request = TT_PROC_WRDATA;
3790 tt_request = TT_PROC_CONTINUE;
3794 tt_request = TT_LWP_SINGLE; /* Should not be making this request? */
3798 tt_request = TT_PROC_EXIT;
3801 case PT_GET_PROCESS_PATHNAME:
3802 tt_request = TT_PROC_GET_PATHNAME;
3806 tt_request = pt_request; /* Let ttrace be the one to complain. */
3810 return call_ttrace (tt_request,
3817 /* Kill that pesky process!
3820 kill_inferior (void)
3825 thread_info **paranoia;
3828 if (PIDGET (inferior_ptid) == 0)
3831 /* Walk the list of "threads", some of which are "pseudo threads",
3832 aka "processes". For each that is NOT inferior_ptid, stop it,
3835 You see, we may not have just a single process to kill. If we're
3836 restarting or quitting or detaching just after the inferior has
3837 forked, then we've actually two processes to clean up.
3839 But we can't just call target_mourn_inferior() for each, since that
3840 zaps the target vector.
3843 paranoia = (thread_info **) xmalloc (thread_head.count *
3844 sizeof (thread_info *));
3847 t = thread_head.head;
3851 paranoia[para_count] = t;
3852 for (i = 0; i < para_count; i++)
3854 if (t->next == paranoia[i])
3856 warning ("Bad data in gdb's thread data; repairing.");
3862 if (t->am_pseudo && (t->pid != PIDGET (inferior_ptid)))
3864 call_ttrace (TT_PROC_EXIT,
3875 call_ttrace (TT_PROC_EXIT,
3876 PIDGET (inferior_ptid),
3880 target_mourn_inferior ();
3881 clear_thread_info ();
3885 #ifndef CHILD_RESUME
3887 /* Sanity check a thread about to be continued.
3890 thread_dropping_event_check (thread_info *p)
3895 * This seems to happen when we "next" over a
3896 * "fork()" while following the parent. If it's
3897 * the FORK event, that's ok. If it's a SIGNAL
3898 * in the unfollowed child, that's ok to--but
3899 * how can we know that's what's going on?
3905 if (p->last_stop_state.tts_event == TTEVT_FORK)
3910 else if (p->last_stop_state.tts_event == TTEVT_SIGNAL)
3912 /* Ok, close eyes and let it happen.
3918 /* This shouldn't happen--we're dropping a
3921 warning ("About to continue process %d, thread %d with unhandled event %s.",
3923 get_printable_name_of_ttrace_event (
3924 p->last_stop_state.tts_event));
3934 /* No saved state, have to assume it failed.
3936 warning ("About to continue process %d, thread %d with unhandled event.",
3945 } /* thread_dropping_event_check */
3947 /* Use a loop over the threads to continue all the threads but
3948 * the one specified, which is to be stepped.
3951 threads_continue_all_but_one (lwpid_t gdb_tid, int signal)
3962 printf ("Using loop over threads to step/resume with signals\n");
3965 /* First update the thread list.
3968 real_tid = map_from_gdb_tid (gdb_tid);
3969 real_pid = get_pid_for (real_tid);
3971 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
3972 while (0 != scan_tid)
3976 /* FIX: later should check state is stopped;
3977 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
3980 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
3981 printf ("About to continue non-stopped thread %d\n", scan_tid);
3984 p = find_thread_info (scan_tid);
3987 add_tthread (real_pid, scan_tid);
3988 p = find_thread_info (scan_tid);
3990 /* This is either a newly-created thread or the
3991 * result of a fork; in either case there's no
3992 * actual event to worry about.
3996 if (state.tts_event != TTEVT_NONE)
3998 /* Oops, do need to worry!
4000 warning ("Unexpected thread with \"%s\" event.",
4001 get_printable_name_of_ttrace_event (state.tts_event));
4004 else if (scan_tid != p->tid)
4005 error ("Bad data in thread database.");
4010 printf ("Why are we continuing a dead thread?\n");
4015 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4018 /* Remove unseen threads.
4020 update_thread_list ();
4022 /* Now run down the thread list and continue or step.
4024 for (p = thread_head.head; p; p = p->next)
4029 thread_dropping_event_check (p);
4031 /* Pass the correct signals along.
4035 thread_signal = p->signal_value;
4041 if (p->tid != real_tid)
4044 * Not the thread of interest, so continue it
4045 * as the user expects.
4047 if (p->stepping_mode == DO_STEP)
4049 /* Just step this thread.
4055 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4060 /* Regular continue (default case).
4066 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4072 /* Step the thread of interest.
4078 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4081 } /* Loop over threads */
4082 } /* End threads_continue_all_but_one */
4084 /* Use a loop over the threads to continue all the threads.
4085 * This is done when a signal must be sent to any of the threads.
4088 threads_continue_all_with_signals (lwpid_t gdb_tid, int signal)
4099 printf ("Using loop over threads to resume with signals\n");
4102 /* Scan and update thread list.
4105 real_tid = map_from_gdb_tid (gdb_tid);
4106 real_pid = get_pid_for (real_tid);
4108 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
4109 while (0 != scan_tid)
4114 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
4115 warning ("About to continue non-stopped thread %d\n", scan_tid);
4118 p = find_thread_info (scan_tid);
4121 add_tthread (real_pid, scan_tid);
4122 p = find_thread_info (scan_tid);
4124 /* This is either a newly-created thread or the
4125 * result of a fork; in either case there's no
4126 * actual event to worry about.
4130 if (state.tts_event != TTEVT_NONE)
4132 /* Oops, do need to worry!
4134 warning ("Unexpected thread with \"%s\" event.",
4135 get_printable_name_of_ttrace_event (state.tts_event));
4142 printf ("Why are we continuing a dead thread? (1)\n");
4147 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4150 /* Remove unseen threads from our list.
4152 update_thread_list ();
4154 /* Continue the threads.
4156 for (p = thread_head.head; p; p = p->next)
4161 thread_dropping_event_check (p);
4163 /* Pass the correct signals along.
4165 if (p->tid == real_tid)
4167 thread_signal = signal;
4170 else if (p->have_signal)
4172 thread_signal = p->signal_value;
4178 if (p->stepping_mode == DO_STEP)
4184 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4189 /* Continue this thread (default case).
4195 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4199 } /* End threads_continue_all_with_signals */
4201 /* Step one thread only.
4204 thread_fake_step (lwpid_t tid, enum target_signal signal)
4211 printf ("Doing a fake-step over a bpt, etc. for %d\n", tid);
4213 if (is_terminated (tid))
4214 printf ("Why are we continuing a dead thread? (4)\n");
4218 if (doing_fake_step)
4219 warning ("Step while step already in progress.");
4221 /* See if there's a saved signal value for this
4222 * thread to be passed on, but no current signal.
4224 p = find_thread_info (tid);
4227 if (p->have_signal && signal == TARGET_SIGNAL_0)
4229 /* Pass on a saved signal.
4231 signal = p->signal_value;
4238 warning ("Internal error: continuing unhandled thread.");
4240 call_ttrace (TT_LWP_SINGLE,
4243 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4246 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4247 * for this thread only, and clear any saved signal info.
4249 doing_fake_step = 1;
4250 fake_step_tid = tid;
4252 } /* End thread_fake_step */
4254 /* Continue one thread when a signal must be sent to it.
4257 threads_continue_one_with_signal (lwpid_t gdb_tid, int signal)
4265 printf ("Continuing one thread with a signal\n");
4268 real_tid = map_from_gdb_tid (gdb_tid);
4269 real_pid = get_pid_for (real_tid);
4271 p = find_thread_info (real_tid);
4274 add_tthread (real_pid, real_tid);
4280 printf ("Why are we continuing a dead thread? (2)\n");
4284 warning ("Internal error: continuing unhandled thread.");
4288 call_ttrace (TT_LWP_CONTINUE,
4291 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4296 #ifndef CHILD_RESUME
4298 /* Resume execution of the inferior process.
4300 * This routine is in charge of setting the "handled" bits.
4302 * If STEP is zero, continue it.
4303 * If STEP is nonzero, single-step it.
4305 * If SIGNAL is nonzero, give it that signal.
4307 * If TID is -1, apply to all threads.
4308 * If TID is not -1, apply to specified thread.
4312 * TID \________________________________________________
4314 * -1 | Step current Continue all threads
4315 * | thread and (but which gets any
4316 * | continue others signal?--We look at
4317 * | "inferior_ptid")
4319 * N | Step _this_ thread Continue _this_ thread
4320 * | and leave others and leave others
4321 * | stopped; internally stopped; used only for
4322 * | used by gdb, never hardware watchpoints
4323 * | a user command. and attach, never a
4327 child_resume (ptid_t ptid, int step, enum target_signal signal)
4329 int resume_all_threads;
4331 process_state_t new_process_state;
4332 lwpid_t gdb_tid = PIDGET (ptid);
4334 resume_all_threads =
4335 (gdb_tid == INFTTRACE_ALL_THREADS) ||
4338 if (resume_all_threads)
4340 /* Resume all threads, but first pick a tid value
4341 * so we can get the pid when in call_ttrace doing
4344 if (vfork_in_flight)
4345 tid = vforking_child_pid;
4347 tid = map_from_gdb_tid (PIDGET (inferior_ptid));
4350 tid = map_from_gdb_tid (gdb_tid);
4355 if (more_events_left)
4356 printf ("More events; ");
4359 printf ("Sending signal %d; ", signal);
4361 if (resume_all_threads)
4364 printf ("Continue process %d\n", tid);
4366 printf ("Step/continue thread %d\n", tid);
4371 printf ("Continue thread %d\n", tid);
4373 printf ("Step just thread %d\n", tid);
4376 if (vfork_in_flight)
4377 printf ("Vfork in flight\n");
4381 if (process_state == RUNNING)
4382 warning ("Internal error in resume logic; doing resume or step anyway.");
4384 if (!step /* Asked to continue... */
4385 && resume_all_threads /* whole process.. */
4386 && signal != 0 /* with a signal... */
4387 && more_events_left > 0)
4388 { /* but we can't yet--save it! */
4390 /* Continue with signal means we have to set the pending
4391 * signal value for this thread.
4397 printf ("Saving signal %d for thread %d\n", signal, tid);
4400 k = find_thread_info (tid);
4404 k->signal_value = signal;
4409 printf ("Why are we continuing a dead thread? (3)\n");
4417 printf ("No thread info for tid %d\n", tid);
4422 /* Are we faking this "continue" or "step"?
4424 * We used to do steps by continuing all the threads for
4425 * which the events had been handled already. While
4426 * conceptually nicer (hides it all in a lower level), this
4427 * can lead to starvation and a hang (e.g. all but one thread
4428 * are unhandled at a breakpoint just before a "join" operation,
4429 * and one thread is in the join, and the user wants to step that
4432 if (resume_all_threads /* Whole process, therefore user command */
4433 && more_events_left > 0)
4434 { /* But we can't do this yet--fake it! */
4439 /* No need to do any notes on a per-thread
4440 * basis--we're done!
4442 #ifdef WAIT_BUFFER_DEBUG
4444 printf ("Faking a process resume.\n");
4452 #ifdef WAIT_BUFFER_DEBUG
4454 printf ("Faking a process step.\n");
4459 p = find_thread_info (tid);
4462 warning ("No thread information for tid %d, 'next' command ignored.\n", tid);
4471 printf ("Why are we continuing a dead thread? (3.5)\n");
4474 if (p->stepping_mode != DO_DEFAULT)
4476 warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored.");
4482 p->stepping_mode = DO_STEP;
4484 p->stepping_mode = DO_CONTINUE;
4487 } /* Have thread info */
4488 } /* Must fake step or go */
4490 /* Execept for fake-steps, from here on we know we are
4491 * going to wind up with a running process which will
4494 new_process_state = RUNNING;
4496 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4497 * it was. (If GDB wanted it to start some other way, we have already
4498 * written a new PC value to the child.)
4500 * If this system does not support PT_STEP, a higher level function will
4501 * have called single_step() to transmute the step request into a
4502 * continue request (by setting breakpoints on all possible successor
4503 * instructions), so we don't have to worry about that here.
4507 if (resume_all_threads)
4510 * Regular user step: other threads get a "continue".
4512 threads_continue_all_but_one (tid, signal);
4513 clear_all_handled ();
4514 clear_all_stepping_mode ();
4519 /* "Fake step": gdb is stepping one thread over a
4520 * breakpoint, watchpoint, or out of a library load
4521 * event, etc. The rest just stay where they are.
4523 * Also used when there are pending events: we really
4524 * step the current thread, but leave the rest stopped.
4525 * Users can't request this, but "wait_for_inferior"
4528 thread_fake_step (tid, signal);
4530 /* Clear the "handled" state of this thread, because
4531 * we'll soon get a new event for it. Other events
4532 * stay as they were.
4534 clear_handled (tid);
4535 clear_stepping_mode (tid);
4536 new_process_state = FAKE_STEPPING;
4542 /* TT_LWP_CONTINUE can pass signals to threads,
4543 * TT_PROC_CONTINUE can't. So if there are any
4544 * signals to pass, we have to use the (slower)
4545 * loop over the stopped threads.
4547 * Equally, if we have to not continue some threads,
4548 * due to saved events, we have to use the loop.
4550 if ((signal != 0) || saved_signals_exist ())
4552 if (resume_all_threads)
4557 printf ("Doing a continue by loop of all threads\n");
4560 threads_continue_all_with_signals (tid, signal);
4562 clear_all_handled ();
4563 clear_all_stepping_mode ();
4569 printf ("Doing a continue w/signal of just thread %d\n", tid);
4572 threads_continue_one_with_signal (tid, signal);
4574 /* Clear the "handled" state of this thread, because
4575 * we'll soon get a new event for it. Other events
4576 * can stay as they were.
4578 clear_handled (tid);
4579 clear_stepping_mode (tid);
4585 /* No signals to send.
4587 if (resume_all_threads)
4591 printf ("Doing a continue by process of process %d\n", tid);
4594 if (more_events_left > 0)
4596 warning ("Losing buffered events on continue.");
4597 more_events_left = 0;
4600 call_ttrace (TT_PROC_CONTINUE,
4606 clear_all_handled ();
4607 clear_all_stepping_mode ();
4615 printf ("Doing a continue of just thread %d\n", tid);
4616 if (is_terminated (tid))
4617 printf ("Why are we continuing a dead thread? (5)\n");
4621 call_ttrace (TT_LWP_CONTINUE,
4627 /* Clear the "handled" state of this thread, because
4628 * we'll soon get a new event for it. Other events
4629 * can stay as they were.
4631 clear_handled (tid);
4632 clear_stepping_mode (tid);
4637 process_state = new_process_state;
4639 #ifdef WAIT_BUFFER_DEBUG
4641 printf ("Process set to %s\n",
4642 get_printable_name_of_process_state (process_state));
4646 #endif /* CHILD_RESUME */
4649 #ifdef ATTACH_DETACH
4653 * One worry is that we may not be attaching to "inferior_ptid"
4654 * and thus may not want to clear out our data. FIXME?
4658 update_thread_state_after_attach (int pid, attach_continue_t kind_of_go)
4661 ttstate_t thread_state;
4665 /* The process better be stopped.
4667 if (process_state != STOPPED
4668 && process_state != VFORKING)
4669 warning ("Internal error attaching.");
4671 /* Clear out old tthread info and start over. This has the
4672 * side effect of ensuring that the TRAP is reported as being
4673 * in the right thread (re-mapped from tid to pid).
4675 * It's because we need to add the tthread _now_ that we
4676 * need to call "clear_thread_info" _now_, and that's why
4677 * "require_notification_of_events" doesn't clear the thread
4678 * info (it's called later than this routine).
4680 clear_thread_info ();
4683 for (tid = get_process_first_stopped_thread_id (pid, &thread_state);
4685 tid = get_process_next_stopped_thread_id (pid, &thread_state))
4694 printf ("Attaching to process %d, thread %d\n",
4699 /* Tell ourselves and the "rest of gdb" that this thread
4702 * This isn't really a hack. Other thread-based versions
4703 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4705 * We don't need to do mapping here, as we know this
4706 * is the first thread and thus gets the real pid
4707 * (and is "inferior_ptid").
4709 * NOTE: it probably isn't the originating thread,
4710 * but that doesn't matter (we hope!).
4712 add_tthread (pid, tid);
4713 p = find_thread_info (tid);
4714 if (NULL == p) /* ?We just added it! */
4715 error ("Internal error adding a thread on attach.");
4717 copy_ttstate_t (&p->last_stop_state, &thread_state);
4720 if (DO_ATTACH_CONTINUE == kind_of_go)
4723 * If we are going to CONTINUE afterwards,
4724 * raising a SIGTRAP, don't bother trying to
4725 * handle this event. But check first!
4727 switch (p->last_stop_state.tts_event)
4731 /* Ok to set this handled.
4736 warning ("Internal error; skipping event %s on process %d, thread %d.",
4737 get_printable_name_of_ttrace_event (
4738 p->last_stop_state.tts_event),
4742 set_handled (pid, tid);
4747 /* There will be no "continue" opertion, so the
4748 * process remains stopped. Don't set any events
4749 * handled except the "gimmies".
4751 switch (p->last_stop_state.tts_event)
4755 /* Ok to ignore this.
4757 set_handled (pid, tid);
4762 /* Expected "other" FORK or EXEC event from a
4768 printf ("Internal error: failed to handle event %s on process %d, thread %d.",
4769 get_printable_name_of_ttrace_event (
4770 p->last_stop_state.tts_event),
4775 add_thread (pid_to_ptid (pid)); /* in thread.c */
4783 /* One mustn't call ttrace_wait() after attaching via ttrace,
4784 'cause the process is stopped already.
4786 However, the upper layers of gdb's execution control will
4787 want to wait after attaching (but not after forks, in
4788 which case they will be doing a "target_resume", anticipating
4789 a later TTEVT_EXEC or TTEVT_FORK event).
4791 To make this attach() implementation more compatible with
4792 others, we'll make the attached-to process raise a SIGTRAP.
4794 Issue: this continues only one thread. That could be
4795 dangerous if the thread is blocked--the process won't run
4796 and no trap will be raised. FIX! (check state.tts_flags?
4797 need one that's either TTS_WASRUNNING--but we've stopped
4798 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4800 if (DO_ATTACH_CONTINUE == kind_of_go)
4802 tt_status = call_real_ttrace (
4807 (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP),
4810 perror_with_name ("ttrace");
4812 clear_handled (a_thread); /* So TRAP will be reported. */
4816 process_state = RUNNING;
4821 #endif /* ATTACH_DETACH */
4824 #ifdef ATTACH_DETACH
4825 /* Start debugging the process whose number is PID.
4833 tt_status = call_real_ttrace (
4838 (TTRACE_ARG_TYPE) TT_VERSION,
4841 perror_with_name ("ttrace attach");
4843 /* If successful, the process is now stopped.
4845 process_state = STOPPED;
4847 /* Our caller ("attach_command" in "infcmd.c")
4848 * expects to do a "wait_for_inferior" after
4849 * the attach, so make sure the inferior is
4850 * running when we're done.
4852 update_thread_state_after_attach (pid, DO_ATTACH_CONTINUE);
4858 #if defined(CHILD_POST_ATTACH)
4860 child_post_attach (int pid)
4864 printf ("child-post-attach call\n");
4867 require_notification_of_events (pid);
4872 /* Stop debugging the process whose number is PID
4873 and continue it with signal number SIGNAL.
4874 SIGNAL = 0 means just continue it.
4880 call_ttrace (TT_PROC_DETACH,
4881 PIDGET (inferior_ptid),
4883 (TTRACE_ARG_TYPE) signal,
4887 clear_thread_info ();
4889 /* Process-state? */
4891 #endif /* ATTACH_DETACH */
4894 /* Default the type of the ttrace transfer to int. */
4895 #ifndef TTRACE_XFER_TYPE
4896 #define TTRACE_XFER_TYPE int
4900 _initialize_kernel_u_addr (void)
4904 #if !defined (CHILD_XFER_MEMORY)
4905 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
4906 in the NEW_SUN_TTRACE case.
4907 It ought to be straightforward. But it appears that writing did
4908 not write the data that I specified. I cannot understand where
4909 it got the data that it actually did write. */
4911 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
4912 to debugger memory starting at MYADDR. Copy to inferior if
4913 WRITE is nonzero. TARGET is ignored.
4915 Returns the length copied, which is either the LEN argument or zero.
4916 This xfer function does not do partial moves, since child_ops
4917 doesn't allow memory operations to cross below us in the target stack
4921 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
4922 struct mem_attrib *attrib,
4923 struct target_ops *target)
4926 /* Round starting address down to longword boundary. */
4927 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (TTRACE_XFER_TYPE);
4928 /* Round ending address up; get number of longwords that makes. */
4930 = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1)
4931 / sizeof (TTRACE_XFER_TYPE);
4932 /* Allocate buffer of that many longwords. */
4933 /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe
4934 because it uses alloca to allocate a buffer of arbitrary size.
4935 For very large xfers, this could crash GDB's stack. */
4936 register TTRACE_XFER_TYPE *buffer
4937 = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));
4941 /* Fill start and end extra bytes of buffer with existing memory data. */
4943 if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE))
4945 /* Need part of initial word -- fetch it. */
4946 buffer[0] = call_ttrace (TT_LWP_RDTEXT,
4947 PIDGET (inferior_ptid),
4948 (TTRACE_ARG_TYPE) addr,
4953 if (count > 1) /* FIXME, avoid if even boundary */
4955 buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT,
4956 PIDGET (inferior_ptid),
4958 (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))),
4963 /* Copy data to be written over corresponding part of buffer */
4965 memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
4969 /* Write the entire buffer. */
4971 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
4974 call_ttrace (TT_LWP_WRDATA,
4975 PIDGET (inferior_ptid),
4976 (TTRACE_ARG_TYPE) addr,
4977 (TTRACE_ARG_TYPE) buffer[i],
4981 /* Using the appropriate one (I or D) is necessary for
4982 Gould NP1, at least. */
4984 call_ttrace (TT_LWP_WRTEXT,
4985 PIDGET (inferior_ptid),
4986 (TTRACE_ARG_TYPE) addr,
4987 (TTRACE_ARG_TYPE) buffer[i],
4996 /* Read all the longwords */
4997 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
5000 buffer[i] = call_ttrace (TT_LWP_RDTEXT,
5001 PIDGET (inferior_ptid),
5002 (TTRACE_ARG_TYPE) addr,
5010 /* Copy appropriate bytes out of the buffer. */
5012 (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
5022 int udot_off; /* Offset into user struct */
5023 int udot_val; /* Value from user struct at udot_off */
5024 char mess[128]; /* For messages */
5026 if (!target_has_execution)
5028 error ("The program is not being run.");
5031 #if !defined (KERNEL_U_SIZE)
5033 /* Adding support for this command is easy. Typically you just add a
5034 routine, called "kernel_u_size" that returns the size of the user
5035 struct, to the appropriate *-nat.c file and then add to the native
5036 config file "#define KERNEL_U_SIZE kernel_u_size()" */
5037 error ("Don't know how large ``struct user'' is in this version of gdb.");
5041 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
5043 if ((udot_off % 24) == 0)
5047 printf_filtered ("\n");
5049 printf_filtered ("%04x:", udot_off);
5051 udot_val = call_ttrace (TT_LWP_RUREGS,
5052 PIDGET (inferior_ptid),
5053 (TTRACE_ARG_TYPE) udot_off,
5058 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
5059 perror_with_name (mess);
5061 /* Avoid using nonportable (?) "*" in print specs */
5062 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
5064 printf_filtered ("\n");
5068 #endif /* !defined (CHILD_XFER_MEMORY). */
5071 /* TTrace version of "target_pid_to_exec_file"
5074 child_pid_to_exec_file (int tid)
5077 static char exec_file_buffer[1024];
5079 static struct pst_status buf;
5081 /* On various versions of hpux11, this may fail due to a supposed
5082 kernel bug. We have alternate methods to get this information
5084 tt_status = call_ttrace (TT_PROC_GET_PATHNAME,
5086 (uint64_t) exec_file_buffer,
5087 sizeof (exec_file_buffer) - 1,
5090 return exec_file_buffer;
5092 /* Try to get process information via pstat and extract the filename
5093 from the pst_cmd field within the pst_status structure. */
5094 if (pstat_getproc (&buf, sizeof (struct pst_status), 0, tid) != -1)
5096 char *p = buf.pst_cmd;
5098 while (*p && *p != ' ')
5102 return (buf.pst_cmd);
5109 pre_fork_inferior (void)
5113 status = pipe (startup_semaphore.parent_channel);
5116 warning ("error getting parent pipe for startup semaphore");
5120 status = pipe (startup_semaphore.child_channel);
5123 warning ("error getting child pipe for startup semaphore");
5128 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5129 * ultimately from "follow_inferior_fork" in infrun.c,
5130 * itself called from "resume".
5132 * This seems to be intended to attach after a fork or
5133 * vfork, while "attach" is used to attach to a pid
5134 * given by the user. The check for an existing attach
5135 * seems odd--it always fails in our test system.
5138 hppa_require_attach (int pid)
5143 unsigned int regs_offset;
5144 process_state_t old_process_state = process_state;
5146 /* Are we already attached? There appears to be no explicit
5147 * way to answer this via ttrace, so we try something which
5148 * should be innocuous if we are attached. If that fails,
5149 * then we assume we're not attached, and so attempt to make
5153 tt_status = call_real_ttrace (TT_PROC_STOP,
5156 (TTRACE_ARG_TYPE) TT_NIL,
5157 (TTRACE_ARG_TYPE) TT_NIL,
5162 /* No change to process-state!
5169 /* If successful, the process is now stopped. But if
5170 * we're VFORKING, the parent is still running, so don't
5171 * change the process state.
5173 if (process_state != VFORKING)
5174 process_state = STOPPED;
5176 /* If we were already attached, you'd think that we
5177 * would need to start going again--but you'd be wrong,
5178 * as the fork-following code is actually in the middle
5179 * of the "resume" routine in in "infrun.c" and so
5180 * will (almost) immediately do a resume.
5182 * On the other hand, if we are VFORKING, which means
5183 * that the child and the parent share a process for a
5184 * while, we know that "resume" won't be resuming
5185 * until the child EXEC event is seen. But we still
5186 * don't want to continue, as the event is already
5189 update_thread_state_after_attach (pid, DONT_ATTACH_CONTINUE);
5190 } /* STOP succeeded */
5196 hppa_require_detach (int pid, int signal)
5200 /* If signal is non-zero, we must pass the signal on to the active
5201 thread prior to detaching. We do this by continuing the threads
5207 threads_continue_all_with_signals (pid, signal);
5211 tt_status = call_ttrace (TT_PROC_DETACH,
5217 errno = 0; /* Ignore any errors. */
5219 /* process_state? */
5224 /* Given the starting address of a memory page, hash it to a bucket in
5225 the memory page dictionary.
5228 get_dictionary_bucket_of_page (CORE_ADDR page_start)
5232 hash = (page_start / memory_page_dictionary.page_size);
5233 hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT;
5239 /* Given a memory page's starting address, get (i.e., find an existing
5240 or create a new) dictionary entry for the page. The page will be
5241 write-protected when this function returns, but may have a reference
5242 count of 0 (if the page was newly-added to the dictionary).
5244 static memory_page_t *
5245 get_dictionary_entry_of_page (int pid, CORE_ADDR page_start)
5248 memory_page_t *page = NULL;
5249 memory_page_t *previous_page = NULL;
5251 /* We're going to be using the dictionary now, than-kew. */
5252 require_memory_page_dictionary ();
5254 /* Try to find an existing dictionary entry for this page. Hash
5255 on the page's starting address.
5257 bucket = get_dictionary_bucket_of_page (page_start);
5258 page = &memory_page_dictionary.buckets[bucket];
5259 while (page != NULL)
5261 if (page->page_start == page_start)
5263 previous_page = page;
5267 /* Did we find a dictionary entry for this page? If not, then
5268 add it to the dictionary now.
5272 /* Create a new entry. */
5273 page = (memory_page_t *) xmalloc (sizeof (memory_page_t));
5274 page->page_start = page_start;
5275 page->reference_count = 0;
5277 page->previous = NULL;
5279 /* We'll write-protect the page now, if that's allowed. */
5280 page->original_permissions = write_protect_page (pid, page_start);
5282 /* Add the new entry to the dictionary. */
5283 page->previous = previous_page;
5284 previous_page->next = page;
5286 memory_page_dictionary.page_count++;
5294 remove_dictionary_entry_of_page (int pid, memory_page_t *page)
5296 /* Restore the page's original permissions. */
5297 unwrite_protect_page (pid, page->page_start, page->original_permissions);
5299 /* Kick the page out of the dictionary. */
5300 if (page->previous != NULL)
5301 page->previous->next = page->next;
5302 if (page->next != NULL)
5303 page->next->previous = page->previous;
5305 /* Just in case someone retains a handle to this after it's freed. */
5306 page->page_start = (CORE_ADDR) 0;
5308 memory_page_dictionary.page_count--;
5315 hppa_enable_syscall_events (int pid)
5318 ttevent_t ttrace_events;
5320 /* Get the set of events that are currently enabled. */
5321 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5323 (TTRACE_ARG_TYPE) & ttrace_events,
5324 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5327 perror_with_name ("ttrace");
5329 /* Add syscall events to that set. */
5330 ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY;
5331 ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN;
5333 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5335 (TTRACE_ARG_TYPE) & ttrace_events,
5336 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5339 perror_with_name ("ttrace");
5344 hppa_disable_syscall_events (int pid)
5347 ttevent_t ttrace_events;
5349 /* Get the set of events that are currently enabled. */
5350 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5352 (TTRACE_ARG_TYPE) & ttrace_events,
5353 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5356 perror_with_name ("ttrace");
5358 /* Remove syscall events from that set. */
5359 ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY;
5360 ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN;
5362 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5364 (TTRACE_ARG_TYPE) & ttrace_events,
5365 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5368 perror_with_name ("ttrace");
5372 /* The address range beginning with START and ending with START+LEN-1
5373 (inclusive) is to be watched via page-protection by a new watchpoint.
5374 Set protection for all pages that overlap that range.
5376 Note that our caller sets TYPE to:
5377 0 for a bp_hardware_watchpoint,
5378 1 for a bp_read_watchpoint,
5379 2 for a bp_access_watchpoint
5381 (Yes, this is intentionally (though lord only knows why) different
5382 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5385 hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
5387 CORE_ADDR page_start;
5388 int dictionary_was_empty;
5391 LONGEST range_size_in_pages;
5394 error ("read or access hardware watchpoints not supported on HP-UX");
5396 /* Examine all pages in the address range. */
5397 require_memory_page_dictionary ();
5399 dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5401 page_size = memory_page_dictionary.page_size;
5402 page_start = (start / page_size) * page_size;
5403 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5405 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5407 memory_page_t *page;
5409 /* This gets the page entered into the dictionary if it was
5410 not already entered.
5412 page = get_dictionary_entry_of_page (pid, page_start);
5413 page->reference_count++;
5416 /* Our implementation depends on seeing calls to kernel code, for the
5417 following reason. Here we ask to be notified of syscalls.
5419 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5422 But when kernel code accesses the page, it doesn't give a SIGBUS.
5423 Rather, the system call that touched the page fails, with errno=EFAULT.
5426 We could accomodate this "feature" by asking to be notified of syscall
5427 entries & exits; upon getting an entry event, disabling page-protections;
5428 upon getting an exit event, reenabling page-protections and then checking
5429 if any watchpoints triggered.
5431 However, this turns out to be a real performance loser. syscalls are
5432 usually a frequent occurrence. Having to unprotect-reprotect all watched
5433 pages, and also to then read all watched memory locations and compare for
5434 triggers, can be quite expensive.
5436 Instead, we'll only ask to be notified of syscall exits. When we get
5437 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5438 we can just continue the inferior.
5440 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5441 hackish stuff to determine whether the failure really was due to a
5442 page-protect trap on a watched location.
5444 if (dictionary_was_empty)
5445 hppa_enable_syscall_events (pid);
5451 /* The address range beginning with START and ending with START+LEN-1
5452 (inclusive) was being watched via page-protection by a watchpoint
5453 which has been removed. Remove protection for all pages that
5454 overlap that range, which are not also being watched by other
5458 hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len,
5461 CORE_ADDR page_start;
5462 int dictionary_is_empty;
5465 LONGEST range_size_in_pages;
5468 error ("read or access hardware watchpoints not supported on HP-UX");
5470 /* Examine all pages in the address range. */
5471 require_memory_page_dictionary ();
5473 page_size = memory_page_dictionary.page_size;
5474 page_start = (start / page_size) * page_size;
5475 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5477 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5479 memory_page_t *page;
5481 page = get_dictionary_entry_of_page (pid, page_start);
5482 page->reference_count--;
5484 /* Was this the last reference of this page? If so, then we
5485 must scrub the entry from the dictionary, and also restore
5486 the page's original permissions.
5488 if (page->reference_count == 0)
5489 remove_dictionary_entry_of_page (pid, page);
5492 dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5494 /* If write protections are currently disallowed, then that implies that
5495 wait_for_inferior believes that the inferior is within a system call.
5496 Since we want to see both syscall entry and return, it's clearly not
5497 good to disable syscall events in this state!
5499 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5500 "inferior is between syscall events now". Oh well.
5502 if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed)
5503 hppa_disable_syscall_events (pid);
5509 /* Could we implement a watchpoint of this type via our available
5512 This query does not consider whether a particular address range
5513 could be so watched, but just whether support is generally available
5514 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5515 query that answers whether a particular range should be watched via
5519 hppa_can_use_hw_watchpoint (enum bptype type, int cnt, enum bptype ot)
5521 return (type == bp_hardware_watchpoint);
5525 /* Assuming we could set a hardware watchpoint on this address, do
5526 we think it would be profitable ("a good idea") to do so? If not,
5527 we can always set a regular (aka single-step & test) watchpoint
5531 hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
5533 int range_is_stack_based;
5534 int range_is_accessible;
5535 CORE_ADDR page_start;
5538 LONGEST range_size_in_pages;
5540 /* ??rehrauer: For now, say that all addresses are potentially
5541 profitable. Possibly later we'll want to test the address
5544 range_is_stack_based = 0;
5546 /* If any page in the range is inaccessible, then we cannot
5547 really use hardware watchpointing, even though our client
5548 thinks we can. In that case, it's actually an error to
5549 attempt to use hw watchpoints, so we'll tell our client
5550 that the range is "unprofitable", and hope that they listen...
5552 range_is_accessible = 1; /* Until proven otherwise. */
5554 /* Examine all pages in the address range. */
5556 page_size = sysconf (_SC_PAGE_SIZE);
5558 /* If we can't determine page size, we're hosed. Tell our
5559 client it's unprofitable to use hw watchpoints for this
5562 if (errno || (page_size <= 0))
5568 page_start = (start / page_size) * page_size;
5569 range_size_in_pages = len / (LONGEST) page_size;
5571 for (page = 0; page < range_size_in_pages; page++, page_start += page_size)
5574 int page_permissions;
5576 /* Is this page accessible? */
5578 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
5580 (TTRACE_ARG_TYPE) page_start,
5582 (TTRACE_ARG_TYPE) & page_permissions);
5583 if (errno || (tt_status < 0))
5586 range_is_accessible = 0;
5590 /* Yes, go for another... */
5593 return (!range_is_stack_based && range_is_accessible);
5598 hppa_pid_or_tid_to_str (ptid_t ptid)
5600 static char buf[100]; /* Static because address returned. */
5601 pid_t id = PIDGET (ptid);
5603 /* Does this appear to be a process? If so, print it that way. */
5604 if (is_process_id (id))
5605 return child_pid_to_str (ptid);
5607 /* Else, print both the GDB thread number and the system thread id. */
5608 sprintf (buf, "thread %d (", pid_to_thread_id (ptid));
5609 strcat (buf, hppa_tid_to_str (ptid));
5610 strcat (buf, ")\0");
5616 /* If the current pid is not the pid this module reported
5617 * from "ptrace_wait" with the most recent event, then the
5618 * user has switched threads.
5620 * If the last reported event was a breakpoint, then return
5621 * the old thread id, else return 0.
5624 hppa_switched_threads (pid_t gdb_pid)
5626 if (gdb_pid == old_gdb_pid)
5629 * Core gdb is working with the same pid that it
5630 * was before we reported the last event. This
5631 * is ok: e.g. we reported hitting a thread-specific
5632 * breakpoint, but we were reporting the wrong
5633 * thread, so the core just ignored the event.
5635 * No thread switch has happened.
5639 else if (gdb_pid == reported_pid)
5642 * Core gdb is working with the pid we reported, so
5643 * any continue or step will be able to figure out
5644 * that it needs to step over any hit breakpoints
5645 * without our (i.e. PREPARE_TO_PROCEED's) help.
5649 else if (!reported_bpt)
5652 * The core switched, but we didn't just report a
5653 * breakpoint, so there's no just-hit breakpoint
5654 * instruction at "reported_pid"'s PC, and thus there
5655 * is no need to step over it.
5661 /* There's been a real switch, and we reported
5662 * a hit breakpoint. Let "hppa_prepare_to_proceed"
5663 * know, so it can see whether the breakpoint is
5666 return reported_pid;
5669 /* Keep compiler happy with an obvious return at the end.
5675 hppa_ensure_vforking_parent_remains_stopped (int pid)
5677 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5684 hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
5686 return 0; /* No, the parent vfork is available now. */
5690 /* Write a register as a 64bit value. This may be necessary if the
5691 native OS is too braindamaged to allow some (or all) registers to
5692 be written in 32bit hunks such as hpux11 and the PC queue registers.
5694 This is horribly gross and disgusting. */
5697 ttrace_write_reg_64 (int gdb_tid, CORE_ADDR dest_addr, CORE_ADDR src_addr)
5703 tid = map_from_gdb_tid (gdb_tid);
5704 pid = get_pid_for (tid);
5707 tt_status = ttrace (TT_LWP_WUREGS,
5710 (TTRACE_ARG_TYPE) dest_addr,
5712 (TTRACE_ARG_TYPE) src_addr );
5717 /* Don't bother for a known benign error: if you ask for the
5718 first thread state, but there is only one thread and it's
5719 not stopped, ttrace complains.
5721 We have this inside the #ifdef because our caller will do
5722 this check for real. */
5723 if( request != TT_PROC_GET_FIRST_LWP_STATE
5724 || errno != EPROTO )
5727 printf( "TT fail for %s, with pid %d, tid %d, status %d \n",
5728 get_printable_name_of_ttrace_request (TT_LWP_WUREGS),
5729 pid, tid, tt_status );
5738 _initialize_infttrace (void)
5740 /* Initialize the ttrace-based hardware watchpoint implementation. */
5741 memory_page_dictionary.page_count = (LONGEST) - 1;
5742 memory_page_dictionary.page_protections_allowed = 1;
5745 memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE);
5747 /* We do a lot of casts from pointers to TTRACE_ARG_TYPE; make sure
5749 if (sizeof (TTRACE_ARG_TYPE) < sizeof (void *))
5750 internal_error (__FILE__, __LINE__, "failed internal consistency check");
5752 if (errno || (memory_page_dictionary.page_size <= 0))
5753 perror_with_name ("sysconf");