1 /* Low level Unix child interface to ttrace, for GDB when running under HP-UX.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "gdb_string.h"
30 /* Some hackery to work around a use of the #define name NO_FLAGS
31 * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
34 #define INFTTRACE_TEMP_HACK NO_FLAGS
39 #include <sys/types.h>
42 #include <sys/param.h>
45 #include <sys/ioctl.h>
47 #include <sys/ttrace.h>
54 #ifdef PTRACE_IN_WRONG_PLACE
57 #include <sys/ptrace.h>
59 #endif /* NO_PTRACE_H */
61 /* Second half of the hackery above. Non-ANSI C, so
62 * we can't use "#error", alas.
65 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
66 /* #error "Hackery to remove warning didn't work right" */
68 /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
71 /* #error "Didn't get expected re-definition of NO_FLAGS" */
72 #define NO_FLAGS INFTTRACE_TEMP_HACK
75 #if !defined (PT_SETTRC)
76 #define PT_SETTRC 0 /* Make process traceable by parent */
78 #if !defined (PT_READ_I)
79 #define PT_READ_I 1 /* Read word from text space */
81 #if !defined (PT_READ_D)
82 #define PT_READ_D 2 /* Read word from data space */
84 #if !defined (PT_READ_U)
85 #define PT_READ_U 3 /* Read word from kernel user struct */
87 #if !defined (PT_WRITE_I)
88 #define PT_WRITE_I 4 /* Write word to text space */
90 #if !defined (PT_WRITE_D)
91 #define PT_WRITE_D 5 /* Write word to data space */
93 #if !defined (PT_WRITE_U)
94 #define PT_WRITE_U 6 /* Write word to kernel user struct */
96 #if !defined (PT_CONTINUE)
97 #define PT_CONTINUE 7 /* Continue after signal */
99 #if !defined (PT_STEP)
100 #define PT_STEP 9 /* Set flag for single stepping */
102 #if !defined (PT_KILL)
103 #define PT_KILL 8 /* Send child a SIGKILL signal */
107 #define PT_ATTACH PTRACE_ATTACH
110 #define PT_DETACH PTRACE_DETACH
115 #include <sys/file.h>
118 /* This semaphore is used to coordinate the child and parent processes
119 after a fork(), and before an exec() by the child. See parent_attach_all
124 int parent_channel
[2]; /* Parent "talks" to [1], child "listens" to [0] */
125 int child_channel
[2]; /* Child "talks" to [1], parent "listens" to [0] */
130 #define SEM_LISTEN (0)
132 static startup_semaphore_t startup_semaphore
;
134 /* See can_touch_threads_of_process for details. */
135 static int vforking_child_pid
= 0;
136 static int vfork_in_flight
= 0;
138 /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed).
140 static pid_t old_gdb_pid
= 0;
141 static pid_t reported_pid
= 0;
142 static int reported_bpt
= 0;
144 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
146 #define TT_OK( _status, _errno ) \
147 (((_status) == 1) && ((_errno) == 0))
149 #define TTRACE_ARG_TYPE uint64_t
151 /* When supplied as the "addr" operand, ttrace interprets this
152 to mean, "from the current address".
154 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
156 /* When supplied as the "addr", "data" or "addr2" operand for most
157 requests, ttrace interprets this to mean, "pay no heed to this
160 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
162 /* This is capable of holding the value of a 32-bit register. The
163 value is always left-aligned in the buffer; i.e., [0] contains
164 the most-significant byte of the register's value, and [sizeof(reg)]
165 contains the least-significant value.
167 ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
168 that registers are 32-bits on HP-UX. The latter assumption changes
171 typedef int register_value_t
;
173 /********************************************************************
179 The rest of GDB sees threads as being things with different
180 "pid" (process id) values. See "thread.c" for details. The
181 separate threads will be seen and reacted to if infttrace passes
182 back different pid values (for _events_). See wait_for_inferior
185 So infttrace is going to use thread ids externally, pretending
186 they are process ids, and keep track internally so that it can
187 use the real process id (and thread id) when calling ttrace.
189 The data structure that supports this is a linked list of the
190 current threads. Since at some date infttrace will have to
191 deal with multiple processes, each list element records its
192 corresponding pid, rather than having a single global.
194 Note that the list is only approximately current; that's ok, as
195 it's up to date when we need it (we hope!). Also, it can contain
196 dead threads, as there's no harm if it does.
198 The approach taken here is to bury the translation from external
199 to internal inside "call_ttrace" and a few other places.
201 There are some wrinkles:
203 o When GDB forks itself to create the debug target process,
204 there's only a pid of 0 around in the child, so the
205 TT_PROC_SETTRC operation uses a more direct call to ttrace;
206 Similiarly, the initial setting of the event mask happens
207 early as well, and so is also special-cased, and an attach
210 o We define an unthreaded application as having a "pseudo"
213 o To keep from confusing the rest of GDB, we don't switch
214 the PID for the pseudo thread to a TID. A table will help:
216 Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ...
218 Our thread list stores: pid pid pid pid ...
221 Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ...
223 Both pid and tid0 will map to tid0, as there are infttrace.c-internal
224 calls to ttrace using tid0.
228 Since we're implementing the "stop the world" model, sub-model
229 "other threads run during step", we have some stuff to do:
231 o User steps require continuing all threads other than the
232 one the user is stepping;
234 o Internal debugger steps (such as over a breakpoint or watchpoint,
235 but not out of a library load thunk) require stepping only
236 the selected thread; this means that we have to report the
237 step finish on that thread, which can lead to complications;
239 o When a thread is created, it is created running, rather
240 than stopped--so we have to stop it.
242 The OS doesn't guarantee the stopped thread list will be stable,
243 no does it guarantee where on the stopped thread list a thread
244 that is single-stepped will wind up: it's possible that it will
245 be off the list for a while, it's possible the step will complete
246 and it will be re-posted to the end...
248 This means we have to scan the stopped thread list, build up
249 a work-list, and then run down the work list; we can't do the
250 step/continue during the scan.
254 Then there's the issue of waiting for an event. We do this by
255 noticing how many events are reported at the end of each wait.
256 From then on, we "fake" all resumes and steps, returning instantly,
257 and don't do another wait. Once all pending events are reported,
258 we can really resume again.
260 To keep this hidden, all the routines which know about tids and
261 pids or real events and simulated ones are static (file-local).
263 This code can make lots of calls to ttrace, in particular it
264 can spin down the list of thread states more than once. If this
265 becomes a performance hit, the spin could be done once and the
266 various "tsp" blocks saved, keeping all later spins in this
269 The O/S doesn't promise to keep the list straight, and so we must
270 re-scan a lot. By observation, it looks like a single-step/wait
271 puts the stepped thread at the end of the list but doesn't change
274 ****************************************************************
277 /* Uncomment these to turn on various debugging output */
278 /* #define THREAD_DEBUG */
279 /* #define WAIT_BUFFER_DEBUG */
280 /* #define PARANOIA */
283 #define INFTTRACE_ALL_THREADS (-1)
284 #define INFTTRACE_STEP (1)
285 #define INFTTRACE_CONTINUE (0)
287 /* FIX: this is used in inftarg.c/child_wait, in a hack.
289 extern int not_same_real_pid
;
291 /* This is used to count buffered events.
293 static unsigned int more_events_left
= 0;
297 typedef enum process_state_enum
301 FAKE_CONTINUE
, /* For later use */
308 static process_state_t process_state
= STOPPED
;
310 /* User-specified stepping modality.
312 typedef enum stepping_mode_enum
314 DO_DEFAULT
, /* ...which is a continue! */
320 /* Action to take on an attach, depends on
321 * what kind (user command, fork, vfork).
323 * At the moment, this is either:
325 * o continue with a SIGTRAP signal, or
329 typedef enum attach_continue_enum
336 /* This flag is true if we are doing a step-over-bpt
337 * with buffered events. We will have to be sure to
338 * report the right thread, as otherwise the spaghetti
339 * code in "infrun.c/wait_for_inferior" will get
342 static int doing_fake_step
= 0;
343 static lwpid_t fake_step_tid
= 0;
346 /****************************************************
347 * Thread information structure routines and types. *
348 ****************************************************
351 struct thread_info_struct
353 int am_pseudo
; /* This is a pseudo-thread for the process. */
354 int pid
; /* Process ID */
355 lwpid_t tid
; /* Thread ID */
356 int handled
; /* 1 if a buffered event was handled. */
357 int seen
; /* 1 if this thread was seen on a traverse. */
358 int terminated
; /* 1 if thread has terminated. */
359 int have_signal
; /* 1 if signal to be sent */
360 enum target_signal signal_value
; /* Signal to send */
361 int have_start
; /* 1 if alternate starting address */
362 stepping_mode_t stepping_mode
; /* Whether to step or continue */
363 CORE_ADDR start
; /* Where to start */
364 int have_state
; /* 1 if the event state has been set */
365 ttstate_t last_stop_state
; /* The most recently-waited event for this thread. */
366 struct thread_info_struct
367 *next
; /* All threads are linked via this field. */
368 struct thread_info_struct
369 *next_pseudo
; /* All pseudo-threads are linked via this field. */
374 struct thread_info_header_struct
378 thread_info
*head_pseudo
;
383 static thread_info_header thread_head
=
385 static thread_info_header deleted_threads
=
388 static saved_real_pid
= 0;
391 /*************************************************
392 * Debugging support functions *
393 *************************************************
399 unsigned long pc_val
;
403 offset
= register_addr (PC_REGNUM
, U_REGS_OFFSET
);
404 res
= read_from_register_save_state (
406 (TTRACE_ARG_TYPE
) offset
,
411 return (CORE_ADDR
) pc_val
;
415 return (CORE_ADDR
) 0;
420 get_printable_name_of_stepping_mode (mode
)
421 stepping_mode_t mode
;
430 return "DO_CONTINUE";
432 return "?unknown mode?";
436 /* This function returns a pointer to a string describing the
437 * ttrace event being reported.
440 get_printable_name_of_ttrace_event (event
)
443 /* This enumeration is "gappy", so don't use a table. */
450 return "TTEVT_SIGNAL";
458 return "TTEVT_VFORK";
459 case TTEVT_SYSCALL_RETURN
:
460 return "TTEVT_SYSCALL_RETURN";
461 case TTEVT_LWP_CREATE
:
462 return "TTEVT_LWP_CREATE";
463 case TTEVT_LWP_TERMINATE
:
464 return "TTEVT_LWP_TERMINATE";
466 return "TTEVT_LWP_EXIT";
467 case TTEVT_LWP_ABORT_SYSCALL
:
468 return "TTEVT_LWP_ABORT_SYSCALL";
469 case TTEVT_SYSCALL_ENTRY
:
470 return "TTEVT_SYSCALL_ENTRY";
471 case TTEVT_SYSCALL_RESTART
:
472 return "TTEVT_SYSCALL_RESTART";
474 return "?new event?";
479 /* This function translates the ttrace request enumeration into
480 * a character string that is its printable (aka "human readable")
484 get_printable_name_of_ttrace_request (request
)
487 if (!IS_TTRACE_REQ (request
))
490 /* This enumeration is "gappy", so don't use a table. */
494 return "TT_PROC_SETTRC";
496 return "TT_PROC_ATTACH";
498 return "TT_PROC_DETACH";
500 return "TT_PROC_RDTEXT";
502 return "TT_PROC_WRTEXT";
504 return "TT_PROC_RDDATA";
506 return "TT_PROC_WRDATA";
508 return "TT_PROC_STOP";
509 case TT_PROC_CONTINUE
:
510 return "TT_PROC_CONTINUE";
511 case TT_PROC_GET_PATHNAME
:
512 return "TT_PROC_GET_PATHNAME";
513 case TT_PROC_GET_EVENT_MASK
:
514 return "TT_PROC_GET_EVENT_MASK";
515 case TT_PROC_SET_EVENT_MASK
:
516 return "TT_PROC_SET_EVENT_MASK";
517 case TT_PROC_GET_FIRST_LWP_STATE
:
518 return "TT_PROC_GET_FIRST_LWP_STATE";
519 case TT_PROC_GET_NEXT_LWP_STATE
:
520 return "TT_PROC_GET_NEXT_LWP_STATE";
522 return "TT_PROC_EXIT";
523 case TT_PROC_GET_MPROTECT
:
524 return "TT_PROC_GET_MPROTECT";
525 case TT_PROC_SET_MPROTECT
:
526 return "TT_PROC_SET_MPROTECT";
527 case TT_PROC_SET_SCBM
:
528 return "TT_PROC_SET_SCBM";
530 return "TT_LWP_STOP";
531 case TT_LWP_CONTINUE
:
532 return "TT_LWP_CONTINUE";
534 return "TT_LWP_SINGLE";
536 return "TT_LWP_RUREGS";
538 return "TT_LWP_WUREGS";
539 case TT_LWP_GET_EVENT_MASK
:
540 return "TT_LWP_GET_EVENT_MASK";
541 case TT_LWP_SET_EVENT_MASK
:
542 return "TT_LWP_SET_EVENT_MASK";
543 case TT_LWP_GET_STATE
:
544 return "TT_LWP_GET_STATE";
551 /* This function translates the process state enumeration into
552 * a character string that is its printable (aka "human readable")
556 get_printable_name_of_process_state (process_state
)
557 process_state_t process_state
;
559 switch (process_state
)
564 return "FAKE_STEPPING";
572 return "?some unknown state?";
576 /* Set a ttrace thread state to a safe, initial state.
579 clear_ttstate_t (tts
)
584 tts
->tts_user_tid
= 0;
585 tts
->tts_event
= TTEVT_NONE
;
588 /* Copy ttrace thread state TTS_FROM into TTS_TO.
591 copy_ttstate_t (tts_to
, tts_from
)
595 memcpy ((char *) tts_to
, (char *) tts_from
, sizeof (*tts_to
));
598 /* Are there any live threads we know about?
601 any_thread_records ()
603 return (thread_head
.count
> 0);
606 /* Create, fill in and link in a thread descriptor.
609 create_thread_info (pid
, tid
)
615 int thread_count_of_pid
;
617 new_p
= malloc (sizeof (thread_info
));
620 new_p
->have_signal
= 0;
621 new_p
->have_start
= 0;
622 new_p
->have_state
= 0;
623 clear_ttstate_t (&new_p
->last_stop_state
);
624 new_p
->am_pseudo
= 0;
627 new_p
->terminated
= 0;
629 new_p
->next_pseudo
= NULL
;
630 new_p
->stepping_mode
= DO_DEFAULT
;
632 if (0 == thread_head
.count
)
636 printf ("First thread, pid %d tid %d!\n", pid
, tid
);
638 saved_real_pid
= inferior_pid
;
644 printf ("Subsequent thread, pid %d tid %d\n", pid
, tid
);
648 /* Another day, another thread...
652 /* The new thread always goes at the head of the list.
654 new_p
->next
= thread_head
.head
;
655 thread_head
.head
= new_p
;
657 /* Is this the "pseudo" thread of a process? It is if there's
658 * no other thread for this process on the list. (Note that this
659 * accomodates multiple processes, such as we see even for simple
660 * cases like forking "non-threaded" programs.)
662 p
= thread_head
.head
;
663 thread_count_of_pid
= 0;
666 if (p
->pid
== new_p
->pid
)
667 thread_count_of_pid
++;
671 /* Did we see any other threads for this pid? (Recall that we just
672 * added this thread to the list...)
674 if (thread_count_of_pid
== 1)
676 new_p
->am_pseudo
= 1;
677 new_p
->next_pseudo
= thread_head
.head_pseudo
;
678 thread_head
.head_pseudo
= new_p
;
684 /* Get rid of our thread info.
694 printf ("Clearing all thread info\n");
697 p
= thread_head
.head
;
705 thread_head
.head
= NULL
;
706 thread_head
.head_pseudo
= NULL
;
707 thread_head
.count
= 0;
709 p
= deleted_threads
.head
;
717 deleted_threads
.head
= NULL
;
718 deleted_threads
.head_pseudo
= NULL
;
719 deleted_threads
.count
= 0;
721 /* No threads, so can't have pending events.
723 more_events_left
= 0;
726 /* Given a tid, find the thread block for it.
729 find_thread_info (tid
)
734 for (p
= thread_head
.head
; p
; p
= p
->next
)
742 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
753 /* For any but the pseudo thread, this maps to the
754 * thread ID. For the pseudo thread, if you pass either
755 * the thread id or the PID, you get the pseudo thread ID.
757 * We have to be prepared for core gdb to ask about
758 * deleted threads. We do the map, but we don't like it.
761 map_from_gdb_tid (gdb_tid
)
766 /* First assume gdb_tid really is a tid, and try to find a
767 * matching entry on the threads list.
769 for (p
= thread_head
.head
; p
; p
= p
->next
)
771 if (p
->tid
== gdb_tid
)
775 /* It doesn't appear to be a tid; perhaps it's really a pid?
776 * Try to find a "pseudo" thread entry on the threads list.
778 for (p
= thread_head
.head_pseudo
; p
!= NULL
; p
= p
->next_pseudo
)
780 if (p
->pid
== gdb_tid
)
784 /* Perhaps it's the tid of a deleted thread we may still
785 * have some knowledge of?
787 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
789 if (p
->tid
== gdb_tid
)
793 /* Or perhaps it's the pid of a deleted process we may still
796 for (p
= deleted_threads
.head_pseudo
; p
!= NULL
; p
= p
->next_pseudo
)
798 if (p
->pid
== gdb_tid
)
802 return 0; /* Error? */
805 /* Map the other way: from a real tid to the
806 * "pid" known by core gdb. This tid may be
807 * for a thread that just got deleted, so we
808 * also need to consider deleted threads.
811 map_to_gdb_tid (real_tid
)
816 for (p
= thread_head
.head
; p
; p
= p
->next
)
818 if (p
->tid
== real_tid
)
827 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
829 if (p
->tid
== real_tid
)
831 return p
->pid
; /* Error? */
836 return 0; /* Error? Never heard of this thread! */
839 /* Do any threads have saved signals?
842 saved_signals_exist ()
846 for (p
= thread_head
.head
; p
; p
= p
->next
)
857 /* Is this the tid for the zero-th thread?
860 is_pseudo_thread (tid
)
863 thread_info
*p
= find_thread_info (tid
);
864 if (NULL
== p
|| p
->terminated
)
870 /* Is this thread terminated?
876 thread_info
*p
= find_thread_info (tid
);
879 return p
->terminated
;
884 /* Is this pid a real PID or a TID?
895 /* What does PID really represent?
897 tid
= map_from_gdb_tid (pid
);
899 return 0; /* Actually, is probably an error... */
901 tinfo
= find_thread_info (tid
);
903 /* Does it appear to be a true thread?
905 if (!tinfo
->am_pseudo
)
908 /* Else, it looks like it may be a process. See if there's any other
909 * threads with the same process ID, though. If there are, then TID
910 * just happens to be the first thread of several for this process.
912 this_pid
= tinfo
->pid
;
914 for (tinfo
= thread_head
.head
; tinfo
; tinfo
= tinfo
->next
)
916 if (tinfo
->pid
== this_pid
)
920 return (this_pid_count
== 1);
924 /* Add a thread to our info. Prevent duplicate entries.
927 add_tthread (pid
, tid
)
933 p
= find_thread_info (tid
);
935 p
= create_thread_info (pid
, tid
);
940 /* Notice that a thread was deleted.
949 if (thread_head
.count
<= 0)
951 error ("Internal error in thread database.");
956 for (p
= thread_head
.head
; p
; p
= p
->next
)
963 printf ("Delete here: %d \n", tid
);
969 * Deleting a main thread is ok if we're doing
970 * a parent-follow on a child; this is odd but
971 * not wrong. It apparently _doesn't_ happen
972 * on the child-follow, as we don't just delete
973 * the pseudo while keeping the rest of the
974 * threads around--instead, we clear out the whole
975 * thread list at once.
978 thread_info
*q_chase
;
981 for (q
= thread_head
.head_pseudo
; q
; q
= q
->next
)
985 /* Remove from pseudo list.
988 thread_head
.head_pseudo
= p
->next_pseudo
;
990 q_chase
->next
= p
->next_pseudo
;
997 /* Remove from live list.
1002 thread_head
.head
= p
->next
;
1004 chase
->next
= p
->next
;
1006 /* Add to deleted thread list.
1008 p
->next
= deleted_threads
.head
;
1009 deleted_threads
.head
= p
;
1010 deleted_threads
.count
++;
1013 p
->next_pseudo
= deleted_threads
.head_pseudo
;
1014 deleted_threads
.head_pseudo
= p
;
1026 /* Get the pid for this tid. (Has to be a real TID!).
1034 for (p
= thread_head
.head
; p
; p
= p
->next
)
1042 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
1053 /* Note that this thread's current event has been handled.
1056 set_handled (pid
, tid
)
1062 p
= find_thread_info (tid
);
1064 p
= add_tthread (pid
, tid
);
1069 /* Was this thread's current event handled?
1077 p
= find_thread_info (tid
);
1081 return 0; /* New threads have not been handled */
1084 /* Set this thread to unhandled.
1092 #ifdef WAIT_BUFFER_DEBUG
1094 printf ("clear_handled %d\n", (int) tid
);
1097 p
= find_thread_info (tid
);
1099 error ("Internal error: No thread state to clear?");
1104 /* Set all threads to unhandled.
1107 clear_all_handled ()
1111 #ifdef WAIT_BUFFER_DEBUG
1113 printf ("clear_all_handled\n");
1116 for (p
= thread_head
.head
; p
; p
= p
->next
)
1121 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
1127 /* Set this thread to default stepping mode.
1130 clear_stepping_mode (tid
)
1135 #ifdef WAIT_BUFFER_DEBUG
1137 printf ("clear_stepping_mode %d\n", (int) tid
);
1140 p
= find_thread_info (tid
);
1142 error ("Internal error: No thread state to clear?");
1144 p
->stepping_mode
= DO_DEFAULT
;
1147 /* Set all threads to do default continue on resume.
1150 clear_all_stepping_mode ()
1154 #ifdef WAIT_BUFFER_DEBUG
1156 printf ("clear_all_stepping_mode\n");
1159 for (p
= thread_head
.head
; p
; p
= p
->next
)
1161 p
->stepping_mode
= DO_DEFAULT
;
1164 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
1166 p
->stepping_mode
= DO_DEFAULT
;
1170 /* Set all threads to unseen on this pass.
1177 for (p
= thread_head
.head
; p
; p
= p
->next
)
1183 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1184 /* debugging routine.
1190 printf (" Thread pid %d, tid %d", p
->pid
, p
->tid
);
1192 printf (", event is %s",
1193 get_printable_name_of_ttrace_event (p
->last_stop_state
.tts_event
));
1196 printf (", pseudo thread");
1199 printf (", have signal 0x%x", p
->signal_value
);
1202 printf (", have start at 0x%x", p
->start
);
1204 printf (", step is %s", get_printable_name_of_stepping_mode (p
->stepping_mode
));
1207 printf (", handled");
1209 printf (", not handled");
1214 printf (", not seen");
1224 if (thread_head
.count
== 0)
1225 printf ("Thread list is empty\n");
1228 printf ("Thread list has ");
1229 if (thread_head
.count
== 1)
1230 printf ("1 entry:\n");
1232 printf ("%d entries:\n", thread_head
.count
);
1233 for (p
= thread_head
.head
; p
; p
= p
->next
)
1239 if (deleted_threads
.count
== 0)
1240 printf ("Deleted thread list is empty\n");
1243 printf ("Deleted thread list has ");
1244 if (deleted_threads
.count
== 1)
1245 printf ("1 entry:\n");
1247 printf ("%d entries:\n", deleted_threads
.count
);
1249 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
1257 /* Update the thread list based on the "seen" bits.
1260 update_thread_list ()
1266 for (p
= thread_head
.head
; p
; p
= p
->next
)
1268 /* Is this an "unseen" thread which really happens to be a process?
1269 If so, is it inferior_pid and is a vfork in flight? If yes to
1270 all, then DON'T REMOVE IT! We're in the midst of moving a vfork
1271 operation, which is a multiple step thing, to the point where we
1272 can touch the parent again. We've most likely stopped to examine
1273 the child at a late stage in the vfork, and if we're not following
1274 the child, we'd best not treat the parent as a dead "thread"...
1276 if ((!p
->seen
) && p
->am_pseudo
&& vfork_in_flight
1277 && (p
->pid
!= vforking_child_pid
))
1287 printf ("Delete unseen thread: %d \n", p
->tid
);
1289 del_tthread (p
->tid
);
1296 /************************************************
1297 * O/S call wrappers *
1298 ************************************************
1301 /* This function simply calls ttrace with the given arguments.
1302 * It exists so that all calls to ttrace are isolated. All
1303 * parameters should be as specified by "man 2 ttrace".
1305 * No other "raw" calls to ttrace should exist in this module.
1308 call_real_ttrace (request
, pid
, tid
, addr
, data
, addr2
)
1312 TTRACE_ARG_TYPE addr
, data
, addr2
;
1317 tt_status
= ttrace (request
, pid
, tid
, addr
, data
, addr2
);
1322 /* Don't bother for a known benign error: if you ask for the
1323 * first thread state, but there is only one thread and it's
1324 * not stopped, ttrace complains.
1326 * We have this inside the #ifdef because our caller will do
1327 * this check for real.
1329 if (request
!= TT_PROC_GET_FIRST_LWP_STATE
1333 printf ("TT fail for %s, with pid %d, tid %d, status %d \n",
1334 get_printable_name_of_ttrace_request (request
),
1335 pid
, tid
, tt_status
);
1341 /* ??rehrauer: It would probably be most robust to catch and report
1342 * failed requests here. However, some clients of this interface
1343 * seem to expect to catch & deal with them, so we'd best not.
1347 strcpy (reason_for_failure
, "ttrace (");
1348 strcat (reason_for_failure
, get_printable_name_of_ttrace_request (request
));
1349 strcat (reason_for_failure
, ")");
1350 printf ("ttrace error, errno = %d\n", errno
);
1351 perror_with_name (reason_for_failure
);
1359 /* This function simply calls ttrace_wait with the given arguments.
1360 * It exists so that all calls to ttrace_wait are isolated.
1362 * No "raw" calls to ttrace_wait should exist elsewhere.
1365 call_real_ttrace_wait (pid
, tid
, option
, tsp
, tsp_size
)
1373 thread_info
*tinfo
= NULL
;
1376 ttw_status
= ttrace_wait (pid
, tid
, option
, tsp
, tsp_size
);
1382 printf ("TW fail with pid %d, tid %d \n", pid
, tid
);
1385 perror_with_name ("ttrace wait");
1392 /* A process may have one or more kernel threads, of which all or
1393 none may be stopped. This function returns the ID of the first
1394 kernel thread in a stopped state, or 0 if none are stopped.
1396 This function can be used with get_process_next_stopped_thread_id
1397 to iterate over the IDs of all stopped threads of this process.
1400 get_process_first_stopped_thread_id (pid
, thread_state
)
1402 ttstate_t
*thread_state
;
1406 tt_status
= call_real_ttrace (
1407 TT_PROC_GET_FIRST_LWP_STATE
,
1410 (TTRACE_ARG_TYPE
) thread_state
,
1411 (TTRACE_ARG_TYPE
) sizeof (*thread_state
),
1416 if (errno
== EPROTO
)
1418 /* This is an error we can handle: there isn't any stopped
1419 * thread. This happens when we're re-starting the application
1420 * and it has only one thread. GET_NEXT handles the case of
1421 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1429 perror_with_name ("ttrace");
1437 return thread_state
->tts_lwpid
;
1441 /* This function returns the ID of the "next" kernel thread in a
1442 stopped state, or 0 if there are none. "Next" refers to the
1443 thread following that of the last successful call to this
1444 function or to get_process_first_stopped_thread_id, using
1445 the value of thread_state returned by that call.
1447 This function can be used with get_process_first_stopped_thread_id
1448 to iterate over the IDs of all stopped threads of this process.
1451 get_process_next_stopped_thread_id (pid
, thread_state
)
1453 ttstate_t
*thread_state
;
1457 tt_status
= call_real_ttrace (
1458 TT_PROC_GET_NEXT_LWP_STATE
,
1461 (TTRACE_ARG_TYPE
) thread_state
,
1462 (TTRACE_ARG_TYPE
) sizeof (*thread_state
),
1465 perror_with_name ("ttrace");
1472 else if (tt_status
== 0)
1474 /* End of list, no next state. Don't return the
1475 * tts_lwpid, as it's a meaningless "240".
1477 * This is an HPUX "feature".
1482 return thread_state
->tts_lwpid
;
1485 /* ??rehrauer: Eventually this function perhaps should be calling
1486 pid_to_thread_id. However, that function currently does nothing
1487 for HP-UX. Even then, I'm not clear whether that function
1488 will return a "kernel" thread ID, or a "user" thread ID. If
1489 the former, we can just call it here. If the latter, we must
1490 map from the "user" tid to a "kernel" tid.
1492 NOTE: currently not called.
1495 get_active_tid_of_pid (pid
)
1498 ttstate_t thread_state
;
1500 return get_process_first_stopped_thread_id (pid
, &thread_state
);
1503 /* This function returns 1 if tt_request is a ttrace request that
1504 * operates upon all threads of a (i.e., the entire) process.
1507 is_process_ttrace_request (tt_request
)
1510 return IS_TTRACE_PROCREQ (tt_request
);
1514 /* This function translates a thread ttrace request into
1515 * the equivalent process request for a one-thread process.
1518 make_process_version (request
)
1521 if (!IS_TTRACE_REQ (request
))
1523 error ("Internal error, bad ttrace request made\n");
1530 return TT_PROC_STOP
;
1532 case TT_LWP_CONTINUE
:
1533 return TT_PROC_CONTINUE
;
1535 case TT_LWP_GET_EVENT_MASK
:
1536 return TT_PROC_GET_EVENT_MASK
;
1538 case TT_LWP_SET_EVENT_MASK
:
1539 return TT_PROC_SET_EVENT_MASK
;
1544 case TT_LWP_GET_STATE
:
1545 return -1; /* No equivalent */
1553 /* This function translates the "pid" used by the rest of
1554 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1555 * with the given arguments.
1557 * In general, other parts of this module should call this
1558 * function when they are dealing with external users, who only
1559 * have tids to pass (but they call it "pid" for historical
1563 call_ttrace (request
, gdb_tid
, addr
, data
, addr2
)
1566 TTRACE_ARG_TYPE addr
, data
, addr2
;
1570 ttreq_t new_request
;
1572 char reason_for_failure
[100]; /* Arbitrary size, should be big enough. */
1575 int is_interesting
= 0;
1577 if (TT_LWP_RUREGS
== request
)
1579 is_interesting
= 1; /* Adjust code here as desired */
1582 if (is_interesting
&& 0 && debug_on
)
1584 if (!is_process_ttrace_request (request
))
1586 printf ("TT: Thread request, tid is %d", gdb_tid
);
1587 printf ("== SINGLE at %x", addr
);
1591 printf ("TT: Process request, tid is %d\n", gdb_tid
);
1592 printf ("==! SINGLE at %x", addr
);
1597 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1598 * which happen before any threads get set up) should go
1599 * directly to "call_real_ttrace", so they don't happen here.
1601 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1605 if (request
== TT_PROC_SETTRC
&& debug_on
)
1606 printf ("Unexpected call for TT_PROC_SETTRC\n");
1609 /* Sometimes we get called with a bogus tid (e.g., if a
1610 * thread has terminated, we return 0; inftarg later asks
1611 * whether the thread has exited/forked/vforked).
1615 errno
= ESRCH
; /* ttrace's response would probably be "No such process". */
1619 /* All other cases should be able to expect that there are
1622 if (!any_thread_records ())
1626 warning ("No thread records for ttrace call");
1628 errno
= ESRCH
; /* ttrace's response would be "No such process". */
1632 /* OK, now the task is to translate the incoming tid into
1635 real_tid
= map_from_gdb_tid (gdb_tid
);
1636 real_pid
= get_pid_for (real_tid
);
1638 /* Now check the result. "Real_pid" is NULL if our list
1639 * didn't find it. We have some tricks we can play to fix
1644 ttstate_t thread_state
;
1648 printf ("No saved pid for tid %d\n", gdb_tid
);
1651 if (is_process_ttrace_request (request
))
1654 /* Ok, we couldn't get a tid. Try to translate to
1655 * the equivalent process operation. We expect this
1656 * NOT to happen, so this is a desparation-type
1657 * move. It can happen if there is an internal
1658 * error and so no "wait()" call is ever done.
1660 new_request
= make_process_version (request
);
1661 if (new_request
== -1)
1666 printf ("...and couldn't make process version of thread operation\n");
1669 /* Use hacky saved pid, which won't always be correct
1670 * in the multi-process future. Use tid as thread,
1671 * probably dooming this to failure. FIX!
1673 if (saved_real_pid
!= 0)
1677 printf ("...using saved pid %d\n", saved_real_pid
);
1680 real_pid
= saved_real_pid
;
1685 error ("Unable to perform thread operation");
1690 /* Sucessfully translated this to a process request,
1691 * which needs no thread value.
1695 request
= new_request
;
1700 printf ("Translated thread request to process request\n");
1701 if (saved_real_pid
== 0)
1702 printf ("...but there's no saved pid\n");
1706 if (gdb_tid
!= saved_real_pid
)
1707 printf ("...but have the wrong pid (%d rather than %d)\n",
1708 gdb_tid
, saved_real_pid
);
1712 } /* Translated to a process request */
1713 } /* Is a process request */
1717 /* We have to have a thread. Ooops.
1719 error ("Thread request with no threads (%s)",
1720 get_printable_name_of_ttrace_request (request
));
1724 /* Ttrace doesn't like to see tid values on process requests,
1725 * even if we have the right one.
1727 if (is_process_ttrace_request (request
))
1733 if (is_interesting
&& 0 && debug_on
)
1735 printf (" now tid %d, pid %d\n", real_tid
, real_pid
);
1736 printf (" request is %s\n", get_printable_name_of_ttrace_request (request
));
1740 /* Finally, the (almost) real call.
1742 tt_status
= call_real_ttrace (request
, real_pid
, real_tid
, addr
, data
, addr2
);
1745 if (is_interesting
&& debug_on
)
1747 if (!TT_OK (tt_status
, errno
)
1748 && !(tt_status
== 0 & errno
== 0))
1749 printf (" got error (errno==%d, status==%d)\n", errno
, tt_status
);
1757 /* Stop all the threads of a process.
1759 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1760 * to get a TTEVT_NONE event, discarding the old event. Be
1761 * very careful, and only call TT_PROC_STOP when you mean it!
1764 stop_all_threads_of_process (real_pid
)
1769 ttw_status
= call_real_ttrace (TT_PROC_STOP
,
1772 (TTRACE_ARG_TYPE
) TT_NIL
,
1773 (TTRACE_ARG_TYPE
) TT_NIL
,
1776 perror_with_name ("ttrace stop of other threads");
1780 /* Under some circumstances, it's unsafe to attempt to stop, or even
1781 query the state of, a process' threads.
1783 In ttrace-based HP-UX, an example is a vforking child process. The
1784 vforking parent and child are somewhat fragile, w/r/t what we can do
1785 what we can do to them with ttrace, until after the child exits or
1786 execs, or until the parent's vfork event is delivered. Until that
1787 time, we must not try to stop the process' threads, or inquire how
1788 many there are, or even alter its data segments, or it typically dies
1789 with a SIGILL. Sigh.
1791 This function returns 1 if this stopped process, and the event that
1792 we're told was responsible for its current stopped state, cannot safely
1793 have its threads examined.
1795 #define CHILD_VFORKED(evt,pid) \
1796 (((evt) == TTEVT_VFORK) && ((pid) != inferior_pid))
1797 #define CHILD_URPED(evt,pid) \
1798 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1799 #define PARENT_VFORKED(evt,pid) \
1800 (((evt) == TTEVT_VFORK) && ((pid) == inferior_pid))
1803 can_touch_threads_of_process (pid
, stopping_event
)
1805 ttevents_t stopping_event
;
1807 if (CHILD_VFORKED (stopping_event
, pid
))
1809 vforking_child_pid
= pid
;
1810 vfork_in_flight
= 1;
1813 else if (vfork_in_flight
&&
1814 (PARENT_VFORKED (stopping_event
, pid
) ||
1815 CHILD_URPED (stopping_event
, pid
)))
1817 vfork_in_flight
= 0;
1818 vforking_child_pid
= 0;
1821 return !vfork_in_flight
;
1825 /* If we can find an as-yet-unhandled thread state of a
1826 * stopped thread of this process return 1 and set "tsp".
1827 * Return 0 if we can't.
1829 * If this function is used when the threads of PIS haven't
1830 * been stopped, undefined behaviour is guaranteed!
1833 select_stopped_thread_of_process (pid
, tsp
)
1837 lwpid_t candidate_tid
, tid
;
1838 ttstate_t candidate_tstate
, tstate
;
1840 /* If we're not allowed to touch the process now, then just
1841 * return the current value of *TSP.
1843 * This supports "vfork". It's ok, really, to double the
1844 * current event (the child EXEC, we hope!).
1846 if (!can_touch_threads_of_process (pid
, tsp
->tts_event
))
1849 /* Decide which of (possibly more than one) events to
1850 * return as the first one. We scan them all so that
1851 * we always return the result of a fake-step first.
1854 for (tid
= get_process_first_stopped_thread_id (pid
, &tstate
);
1856 tid
= get_process_next_stopped_thread_id (pid
, &tstate
))
1858 /* TTEVT_NONE events are uninteresting to our clients. They're
1859 * an artifact of our "stop the world" model--the thread is
1860 * stopped because we stopped it.
1862 if (tstate
.tts_event
== TTEVT_NONE
)
1864 set_handled (pid
, tstate
.tts_lwpid
);
1867 /* Did we just single-step a single thread, without letting any
1868 * of the others run? Is this an event for that thread?
1870 * If so, we believe our client would prefer to see this event
1871 * over any others. (Typically the client wants to just push
1872 * one thread a little farther forward, and then go around
1873 * checking for what all threads are doing.)
1875 else if (doing_fake_step
&& (tstate
.tts_lwpid
== fake_step_tid
))
1877 #ifdef WAIT_BUFFER_DEBUG
1878 /* It's possible here to see either a SIGTRAP (due to
1879 * successful completion of a step) or a SYSCALL_ENTRY
1880 * (due to a step completion with active hardware
1884 printf ("Ending fake step with tid %d, state %s\n",
1886 get_printable_name_of_ttrace_event (tstate
.tts_event
));
1889 /* Remember this one, and throw away any previous
1892 candidate_tid
= tstate
.tts_lwpid
;
1893 candidate_tstate
= tstate
;
1896 #ifdef FORGET_DELETED_BPTS
1898 /* We can't just do this, as if we do, and then wind
1899 * up the loop with no unhandled events, we need to
1900 * handle that case--the appropriate reaction is to
1901 * just continue, but there's no easy way to do that.
1903 * Better to put this in the ttrace_wait call--if, when
1904 * we fake a wait, we update our events based on the
1905 * breakpoint_here_pc call and find there are no more events,
1906 * then we better continue and so on.
1908 * Or we could put it in the next/continue fake.
1909 * But it has to go in the buffering code, not in the
1910 * real go/wait code.
1912 else if ((TTEVT_SIGNAL
== tstate
.tts_event
)
1913 && (5 == tstate
.tts_u
.tts_signal
.tts_signo
)
1914 && (0 != get_raw_pc (tstate
.tts_lwpid
))
1915 && !breakpoint_here_p (get_raw_pc (tstate
.tts_lwpid
)))
1918 * If the user deleted a breakpoint while this
1919 * breakpoint-hit event was buffered, we can forget
1922 #ifdef WAIT_BUFFER_DEBUG
1924 printf ("Forgetting deleted bp hit for thread %d\n",
1928 set_handled (pid
, tstate
.tts_lwpid
);
1932 /* Else, is this the first "unhandled" event? If so,
1933 * we believe our client wants to see it (if we don't
1934 * see a fake-step later on in the scan).
1936 else if (!was_handled (tstate
.tts_lwpid
) && candidate_tid
== 0)
1938 candidate_tid
= tstate
.tts_lwpid
;
1939 candidate_tstate
= tstate
;
1942 /* This is either an event that has already been "handled",
1943 * and thus we believe is uninteresting to our client, or we
1944 * already have a candidate event. Ignore it...
1948 /* What do we report?
1950 if (doing_fake_step
)
1952 if (candidate_tid
== fake_step_tid
)
1956 tstate
= candidate_tstate
;
1960 warning ("Internal error: fake-step failed to complete.");
1964 else if (candidate_tid
!= 0)
1966 /* Found a candidate unhandled event.
1968 tstate
= candidate_tstate
;
1972 warning ("Internal error in call of ttrace_wait.");
1977 warning ("Internal error: no unhandled thread event to select");
1981 copy_ttstate_t (tsp
, &tstate
);
1983 } /* End of select_stopped_thread_of_process */
1986 /* Check our internal thread data against the real thing.
1989 check_thread_consistency (real_pid
)
1992 int tid
; /* really lwpid_t */
1996 /* Spin down the O/S list of threads, checking that they
1997 * match what we've got.
1999 for (tid
= get_process_first_stopped_thread_id (real_pid
, &tstate
);
2001 tid
= get_process_next_stopped_thread_id (real_pid
, &tstate
))
2004 p
= find_thread_info (tid
);
2008 warning ("No internal thread data for thread %d.", tid
);
2014 warning ("Inconsistent internal thread data for thread %d.", tid
);
2019 warning ("Thread %d is not terminated, internal error.", tid
);
2024 #define TT_COMPARE( fld ) \
2025 tstate.fld != p->last_stop_state.fld
2029 if (TT_COMPARE (tts_pid
)
2030 || TT_COMPARE (tts_lwpid
)
2031 || TT_COMPARE (tts_user_tid
)
2032 || TT_COMPARE (tts_event
)
2033 || TT_COMPARE (tts_flags
)
2034 || TT_COMPARE (tts_scno
)
2035 || TT_COMPARE (tts_scnargs
))
2037 warning ("Internal thread data for thread %d is wrong.", tid
);
2043 #endif /* PARANOIA */
2046 /* This function wraps calls to "call_real_ttrace_wait" so
2047 * that a actual wait is only done when all pending events
2048 * have been reported.
2050 * Note that typically it is called with a pid of "0", i.e.
2051 * the "don't care" value.
2053 * Return value is the status of the pseudo wait.
2056 call_ttrace_wait (pid
, option
, tsp
, tsp_size
)
2062 /* This holds the actual, for-real, true process ID.
2064 static int real_pid
;
2066 /* As an argument to ttrace_wait, zero pid
2067 * means "Any process", and zero tid means
2068 * "Any thread of the specified process".
2071 lwpid_t wait_tid
= 0;
2074 int ttw_status
= 0; /* To be returned */
2076 thread_info
*tinfo
= NULL
;
2084 printf ("TW: Pid to wait on is %d\n", pid
);
2087 if (!any_thread_records ())
2088 error ("No thread records for ttrace call w. specific pid");
2090 /* OK, now the task is to translate the incoming tid into
2093 real_tid
= map_from_gdb_tid (pid
);
2094 real_pid
= get_pid_for (real_tid
);
2097 printf ("==TW: real pid %d, real tid %d\n", real_pid
, real_tid
);
2102 /* Sanity checks and set-up.
2105 * Stopped Running Fake-step (v)Fork
2106 * \________________________________________
2108 * No buffered events | error wait wait wait
2110 * Buffered events | debuffer error wait debuffer (?)
2113 if (more_events_left
== 0)
2116 if (process_state
== RUNNING
)
2118 /* OK--normal call of ttrace_wait with no buffered events.
2122 else if (process_state
== FAKE_STEPPING
)
2124 /* Ok--call of ttrace_wait to support
2125 * fake stepping with no buffered events.
2127 * But we better be fake-stepping!
2129 if (!doing_fake_step
)
2131 warning ("Inconsistent thread state.");
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.
2143 else if (process_state
== STOPPED
)
2145 warning ("Process not running at wait call.");
2150 warning ("Inconsistent process state.");
2157 if (process_state
== STOPPED
)
2159 /* OK--buffered events being unbuffered.
2163 else if (process_state
== RUNNING
)
2165 /* An error--shouldn't have buffered events
2168 warning ("Trying to continue with buffered events:");
2170 else if (process_state
== FAKE_STEPPING
)
2173 * Better be fake-stepping!
2175 if (!doing_fake_step
)
2177 warning ("Losing buffered thread events!\n");
2180 else if ((process_state
== FORKING
)
2181 || (process_state
== VFORKING
))
2183 /* Ok--there are two processes, so waiting
2184 * for the second while the first is stopped
2185 * is ok. Handled bits stay as they were.
2190 warning ("Process in unknown state with buffered events.");
2193 /* Sometimes we have to wait for a particular thread
2194 * (if we're stepping over a bpt). In that case, we
2195 * _know_ it's going to complete the single-step we
2196 * asked for (because we're only doing the step under
2197 * certain very well-understood circumstances), so it
2200 if (doing_fake_step
)
2202 wait_tid
= fake_step_tid
;
2203 wait_pid
= get_pid_for (fake_step_tid
);
2205 #ifdef WAIT_BUFFER_DEBUG
2207 printf ("Doing a wait after a fake-step for %d, pid %d\n",
2208 wait_tid
, wait_pid
);
2212 if (more_events_left
== 0 /* No buffered events, need real ones. */
2213 || process_state
!= STOPPED
)
2215 /* If there are no buffered events, and so we need
2216 * real ones, or if we are FORKING, VFORKING,
2217 * FAKE_STEPPING or RUNNING, and thus have to do
2218 * a real wait, then do a real wait.
2221 #ifdef WAIT_BUFFER_DEBUG
2222 /* Normal case... */
2224 printf ("TW: do it for real; pid %d, tid %d\n", wait_pid
, wait_tid
);
2227 /* The actual wait call.
2229 ttw_status
= call_real_ttrace_wait (wait_pid
, wait_tid
, option
, tsp
, tsp_size
);
2231 /* Note that the routines we'll call will be using "call_real_ttrace",
2232 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2233 * the rest of the world uses (which is actually the tid).
2235 real_pid
= tsp
->tts_pid
;
2237 /* For most events: Stop the world!
2239 * It's sometimes not safe to stop all threads of a process.
2240 * Sometimes it's not even safe to ask for the thread state
2243 if (can_touch_threads_of_process (real_pid
, tsp
->tts_event
))
2245 /* If we're really only stepping a single thread, then don't
2246 * try to stop all the others -- we only do this single-stepping
2247 * business when all others were already stopped...and the stop
2248 * would mess up other threads' events.
2250 * Similiarly, if there are other threads with events,
2251 * don't do the stop.
2253 if (!doing_fake_step
)
2255 if (more_events_left
> 0)
2256 warning ("Internal error in stopping process");
2258 stop_all_threads_of_process (real_pid
);
2260 /* At this point, we could scan and update_thread_list(),
2261 * and only use the local list for the rest of the
2262 * module! We'd get rid of the scans in the various
2263 * continue routines (adding one in attach). It'd
2264 * be great--UPGRADE ME!
2272 if (more_events_left
> 0)
2273 printf ("== Can't stop process; more events!\n");
2275 printf ("== Can't stop process!\n");
2279 process_state
= STOPPED
;
2281 #ifdef WAIT_BUFFER_DEBUG
2283 printf ("Process set to STOPPED\n");
2289 /* Fake a call to ttrace_wait. The process must be
2290 * STOPPED, as we aren't going to do any wait.
2292 #ifdef WAIT_BUFFER_DEBUG
2294 printf ("TW: fake it\n");
2297 if (process_state
!= STOPPED
)
2299 warning ("Process not stopped at wait call, in state '%s'.\n",
2300 get_printable_name_of_process_state (process_state
));
2303 if (doing_fake_step
)
2304 error ("Internal error in stepping over breakpoint");
2306 ttw_status
= 0; /* Faking it is always successful! */
2307 } /* End of fake or not? if */
2309 /* Pick an event to pass to our caller. Be paranoid.
2311 if (!select_stopped_thread_of_process (real_pid
, tsp
))
2312 warning ("Can't find event, using previous event.");
2314 else if (tsp
->tts_event
== TTEVT_NONE
)
2315 warning ("Internal error: no thread has a real event.");
2317 else if (doing_fake_step
)
2319 if (fake_step_tid
!= tsp
->tts_lwpid
)
2320 warning ("Internal error in stepping over breakpoint.");
2322 /* This wait clears the (current) fake-step if there was one.
2324 doing_fake_step
= 0;
2328 /* We now have a correct tsp and ttw_status for the thread
2329 * which we want to report. So it's "handled"! This call
2330 * will add it to our list if it's not there already.
2332 set_handled (real_pid
, tsp
->tts_lwpid
);
2334 /* Save a copy of the ttrace state of this thread, in our local
2337 This caches the state. The implementation of queries like
2338 target_has_execd can then use this cached state, rather than
2339 be forced to make an explicit ttrace call to get it.
2341 (Guard against the condition that this is the first time we've
2342 waited on, i.e., seen this thread, and so haven't yet entered
2343 it into our list of threads.)
2345 tinfo
= find_thread_info (tsp
->tts_lwpid
);
2348 copy_ttstate_t (&tinfo
->last_stop_state
, tsp
);
2349 tinfo
->have_state
= 1;
2353 } /* call_ttrace_wait */
2355 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2357 child_reported_exec_events_per_exec_call ()
2359 return 1; /* ttrace reports the event once per call. */
2365 /* Our implementation of hardware watchpoints involves making memory
2366 pages write-protected. We must remember a page's original permissions,
2367 and we must also know when it is appropriate to restore a page's
2368 permissions to its original state.
2370 We use a "dictionary" of hardware-watched pages to do this. Each
2371 hardware-watched page is recorded in the dictionary. Each page's
2372 dictionary entry contains the original permissions and a reference
2373 count. Pages are hashed into the dictionary by their start address.
2375 When hardware watchpoint is set on page X for the first time, page X
2376 is added to the dictionary with a reference count of 1. If other
2377 hardware watchpoints are subsequently set on page X, its reference
2378 count is incremented. When hardware watchpoints are removed from
2379 page X, its reference count is decremented. If a page's reference
2380 count drops to 0, it's permissions are restored and the page's entry
2381 is thrown out of the dictionary.
2383 typedef struct memory_page
2385 CORE_ADDR page_start
;
2386 int reference_count
;
2387 int original_permissions
;
2388 struct memory_page
*next
;
2389 struct memory_page
*previous
;
2393 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2399 int page_protections_allowed
;
2400 /* These are just the heads of chains of actual page descriptors. */
2401 memory_page_t buckets
[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
];
2403 memory_page_dictionary
;
2407 require_memory_page_dictionary ()
2411 /* Is the memory page dictionary ready for use? If so, we're done. */
2412 if (memory_page_dictionary
.page_count
>= (LONGEST
) 0)
2415 /* Else, initialize it. */
2416 memory_page_dictionary
.page_count
= (LONGEST
) 0;
2418 for (i
= 0; i
< MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; i
++)
2420 memory_page_dictionary
.buckets
[i
].page_start
= (CORE_ADDR
) 0;
2421 memory_page_dictionary
.buckets
[i
].reference_count
= 0;
2422 memory_page_dictionary
.buckets
[i
].next
= NULL
;
2423 memory_page_dictionary
.buckets
[i
].previous
= NULL
;
2429 retire_memory_page_dictionary ()
2431 memory_page_dictionary
.page_count
= (LONGEST
) - 1;
2435 /* Write-protect the memory page that starts at this address.
2437 Returns the original permissions of the page.
2440 write_protect_page (pid
, page_start
)
2442 CORE_ADDR page_start
;
2445 int original_permissions
;
2446 int new_permissions
;
2448 tt_status
= call_ttrace (TT_PROC_GET_MPROTECT
,
2450 (TTRACE_ARG_TYPE
) page_start
,
2452 (TTRACE_ARG_TYPE
) & original_permissions
);
2453 if (errno
|| (tt_status
< 0))
2455 return 0; /* What else can we do? */
2458 /* We'll also write-protect the page now, if that's allowed. */
2459 if (memory_page_dictionary
.page_protections_allowed
)
2461 new_permissions
= original_permissions
& ~PROT_WRITE
;
2462 tt_status
= call_ttrace (TT_PROC_SET_MPROTECT
,
2464 (TTRACE_ARG_TYPE
) page_start
,
2465 (TTRACE_ARG_TYPE
) memory_page_dictionary
.page_size
,
2466 (TTRACE_ARG_TYPE
) new_permissions
);
2467 if (errno
|| (tt_status
< 0))
2469 return 0; /* What else can we do? */
2473 return original_permissions
;
2477 /* Unwrite-protect the memory page that starts at this address, restoring
2478 (what we must assume are) its original permissions.
2481 unwrite_protect_page (pid
, page_start
, original_permissions
)
2483 CORE_ADDR page_start
;
2484 int original_permissions
;
2488 tt_status
= call_ttrace (TT_PROC_SET_MPROTECT
,
2490 (TTRACE_ARG_TYPE
) page_start
,
2491 (TTRACE_ARG_TYPE
) memory_page_dictionary
.page_size
,
2492 (TTRACE_ARG_TYPE
) original_permissions
);
2493 if (errno
|| (tt_status
< 0))
2495 return; /* What else can we do? */
2500 /* Memory page-protections are used to implement "hardware" watchpoints
2503 For every memory page that is currently being watched (i.e., that
2504 presently should be write-protected), write-protect it.
2507 hppa_enable_page_protection_events (pid
)
2512 memory_page_dictionary
.page_protections_allowed
= 1;
2514 for (bucket
= 0; bucket
< MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; bucket
++)
2516 memory_page_t
*page
;
2518 page
= memory_page_dictionary
.buckets
[bucket
].next
;
2519 while (page
!= NULL
)
2521 page
->original_permissions
= write_protect_page (pid
, page
->page_start
);
2528 /* Memory page-protections are used to implement "hardware" watchpoints
2531 For every memory page that is currently being watched (i.e., that
2532 presently is or should be write-protected), un-write-protect it.
2535 hppa_disable_page_protection_events (pid
)
2540 for (bucket
= 0; bucket
< MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; bucket
++)
2542 memory_page_t
*page
;
2544 page
= memory_page_dictionary
.buckets
[bucket
].next
;
2545 while (page
!= NULL
)
2547 unwrite_protect_page (pid
, page
->page_start
, page
->original_permissions
);
2552 memory_page_dictionary
.page_protections_allowed
= 0;
2555 /* Count the number of outstanding events. At this
2556 * point, we have selected one thread and its event
2557 * as the one to be "reported" upwards to core gdb.
2558 * That thread is already marked as "handled".
2560 * Note: we could just scan our own thread list. FIXME!
2563 count_unhandled_events (real_pid
, real_tid
)
2571 /* Ok, find out how many threads have real events to report.
2574 ttid
= get_process_first_stopped_thread_id (real_pid
, &tstate
);
2580 printf ("Process %d has no threads\n", real_pid
);
2582 printf ("Process %d has these threads:\n", real_pid
);
2588 if (tstate
.tts_event
!= TTEVT_NONE
2589 && !was_handled (ttid
))
2591 /* TTEVT_NONE implies we just stopped it ourselves
2592 * because we're the stop-the-world guys, so it's
2593 * not an event from our point of view.
2595 * If "was_handled" is true, this is an event we
2596 * already handled, so don't count it.
2598 * Note that we don't count the thread with the
2599 * currently-reported event, as it's already marked
2605 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2608 if (ttid
== real_tid
)
2609 printf ("*"); /* Thread we're reporting */
2613 if (tstate
.tts_event
!= TTEVT_NONE
)
2614 printf ("+"); /* Thread with a real event */
2618 if (was_handled (ttid
))
2619 printf ("h"); /* Thread has been handled */
2623 printf (" %d, with event %s", ttid
,
2624 get_printable_name_of_ttrace_event (tstate
.tts_event
));
2626 if (tstate
.tts_event
== TTEVT_SIGNAL
2627 && 5 == tstate
.tts_u
.tts_signal
.tts_signo
)
2631 pc_val
= get_raw_pc (ttid
);
2634 printf (" breakpoint at 0x%x\n", pc_val
);
2636 printf (" bpt, can't fetch pc.\n");
2643 ttid
= get_process_next_stopped_thread_id (real_pid
, &tstate
);
2646 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2648 if (events_left
> 0)
2649 printf ("There are thus %d pending events\n", events_left
);
2655 /* This function is provided as a sop to clients that are calling
2656 * ptrace_wait to wait for a process to stop. (see the
2657 * implementation of child_wait.) Return value is the pid for
2658 * the event that ended the wait.
2660 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2663 ptrace_wait (pid
, status
)
2674 /* The ptrace implementation of this also ignores pid.
2678 ttwait_return
= call_ttrace_wait (0, TTRACE_WAITOK
, &tsp
, sizeof (tsp
));
2679 if (ttwait_return
< 0)
2681 /* ??rehrauer: It appears that if our inferior exits and we
2682 haven't asked for exit events, that we're not getting any
2683 indication save a negative return from ttrace_wait and an
2688 *status
= 0; /* WIFEXITED */
2689 return inferior_pid
;
2692 warning ("Call of ttrace_wait returned with errno %d.",
2694 *status
= ttwait_return
;
2695 return inferior_pid
;
2698 real_pid
= tsp
.tts_pid
;
2699 real_tid
= tsp
.tts_lwpid
;
2701 /* One complication is that the "tts_event" structure has
2702 * a set of flags, and more than one can be set. So we
2703 * either have to force an order (as we do here), or handle
2704 * more than one flag at a time.
2706 if (tsp
.tts_event
& TTEVT_LWP_CREATE
)
2709 /* Unlike what you might expect, this event is reported in
2710 * the _creating_ thread, and the _created_ thread (whose tid
2711 * we have) is still running. So we have to stop it. This
2712 * has already been done in "call_ttrace_wait", but should we
2713 * ever abandon the "stop-the-world" model, here's the command
2716 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2718 * Note that this would depend on being called _after_ "add_tthread"
2719 * below for the tid-to-pid translation to be done in "call_ttrace".
2724 printf ("New thread: pid %d, tid %d, creator tid %d\n",
2725 real_pid
, tsp
.tts_u
.tts_thread
.tts_target_lwpid
,
2729 /* Now we have to return the tid of the created thread, not
2730 * the creating thread, or "wait_for_inferior" won't know we
2731 * have a new "process" (thread). Plus we should record it
2734 real_tid
= tsp
.tts_u
.tts_thread
.tts_target_lwpid
;
2736 add_tthread (real_pid
, real_tid
);
2739 else if ((tsp
.tts_event
& TTEVT_LWP_TERMINATE
)
2740 || (tsp
.tts_event
& TTEVT_LWP_EXIT
))
2745 printf ("Thread dies: %d\n", real_tid
);
2748 del_tthread (real_tid
);
2751 else if (tsp
.tts_event
& TTEVT_EXEC
)
2756 printf ("Pid %d has zero'th thread %d; inferior pid is %d\n",
2757 real_pid
, real_tid
, inferior_pid
);
2760 add_tthread (real_pid
, real_tid
);
2766 printf ("Process-level event %s, using tid %d\n",
2767 get_printable_name_of_ttrace_event (tsp
.tts_event
),
2770 /* OK to do this, as "add_tthread" won't add
2771 * duplicate entries. Also OK not to do it,
2772 * as this event isn't one which can change the
2775 add_tthread (real_pid
, real_tid
);
2780 /* How many events are left to report later?
2781 * In a non-stop-the-world model, this isn't needed.
2783 * Note that it's not always safe to query the thread state of a process,
2784 * which is what count_unhandled_events does. (If unsafe, we're left with
2785 * no other resort than to assume that no more events remain...)
2787 if (can_touch_threads_of_process (real_pid
, tsp
.tts_event
))
2788 more_events_left
= count_unhandled_events (real_pid
, real_tid
);
2792 if (more_events_left
> 0)
2793 warning ("Vfork or fork causing loss of %d buffered events.",
2796 more_events_left
= 0;
2799 /* Attempt to translate the ttrace_wait-returned status into the
2802 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2803 clients that expect to pick apart a ptrace wait status, to use
2804 something a little more abstract.
2806 if ((tsp
.tts_event
& TTEVT_EXEC
)
2807 || (tsp
.tts_event
& TTEVT_FORK
)
2808 || (tsp
.tts_event
& TTEVT_VFORK
))
2810 /* Forks come in pairs (parent and child), so core gdb
2811 * will do two waits. Be ready to notice this.
2813 if (tsp
.tts_event
& TTEVT_FORK
)
2815 process_state
= FORKING
;
2817 #ifdef WAIT_BUFFER_DEBUG
2819 printf ("Process set to FORKING\n");
2822 else if (tsp
.tts_event
& TTEVT_VFORK
)
2824 process_state
= VFORKING
;
2826 #ifdef WAIT_BUFFER_DEBUG
2828 printf ("Process set to VFORKING\n");
2832 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2833 but I don't think non HP-UX-specific clients really carefully
2834 inspect the first events they get after inferior startup, so
2835 it probably almost doesn't matter what we claim this is.
2840 printf ("..a process 'event'\n");
2843 /* Also make fork and exec events look like bpts, so they can be caught.
2845 *status
= 0177 | (_SIGTRAP
<< 8);
2848 /* Special-cases: We ask for syscall entry and exit events to implement
2849 "fast" (aka "hardware") watchpoints.
2851 When we get a syscall entry, we want to disable page-protections,
2852 and resume the inferior; this isn't an event we wish for
2853 wait_for_inferior to see. Note that we must resume ONLY the
2854 thread that reported the syscall entry; we don't want to allow
2855 other threads to run with the page protections off, as they might
2856 then be able to write to watch memory without it being caught.
2858 When we get a syscall exit, we want to reenable page-protections,
2859 but we don't want to resume the inferior; this is an event we wish
2860 wait_for_inferior to see. Make it look like the signal we normally
2861 get for a single-step completion. This should cause wait_for_inferior
2862 to evaluate whether any watchpoint triggered.
2864 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2865 due to some HP-UX "features". Some syscalls have problems with
2866 write-protections on some pages, and some syscalls seem to have
2867 pending writes to those pages at the time we're getting the return
2868 event. So, we'll single-step the inferior to get out of the syscall,
2869 and then reenable protections.
2871 Note that we're intentionally allowing the syscall exit case to
2872 fall through into the succeeding cases, as sometimes we single-
2873 step out of one syscall only to immediately enter another...
2875 else if ((tsp
.tts_event
& TTEVT_SYSCALL_ENTRY
)
2876 || (tsp
.tts_event
& TTEVT_SYSCALL_RETURN
))
2878 /* Make a syscall event look like a breakpoint. Same comments
2879 as for exec & fork events.
2883 printf ("..a syscall 'event'\n");
2886 /* Also make syscall events look like bpts, so they can be caught.
2888 *status
= 0177 | (_SIGTRAP
<< 8);
2891 else if ((tsp
.tts_event
& TTEVT_LWP_CREATE
)
2892 || (tsp
.tts_event
& TTEVT_LWP_TERMINATE
)
2893 || (tsp
.tts_event
& TTEVT_LWP_EXIT
))
2895 /* Make a thread event look like a breakpoint. Same comments
2896 * as for exec & fork events.
2900 printf ("..a thread 'event'\n");
2903 /* Also make thread events look like bpts, so they can be caught.
2905 *status
= 0177 | (_SIGTRAP
<< 8);
2908 else if ((tsp
.tts_event
& TTEVT_EXIT
))
2913 printf ("..an exit\n");
2916 /* Prevent rest of gdb from thinking this is
2917 * a new thread if for some reason it's never
2918 * seen the main thread before.
2920 inferior_pid
= map_to_gdb_tid (real_tid
); /* HACK, FIX */
2922 *status
= 0 | (tsp
.tts_u
.tts_exit
.tts_exitcode
);
2925 else if (tsp
.tts_event
& TTEVT_SIGNAL
)
2929 printf ("..a signal, %d\n", tsp
.tts_u
.tts_signal
.tts_signo
);
2932 *status
= 0177 | (tsp
.tts_u
.tts_signal
.tts_signo
<< 8);
2938 /* This means the process or thread terminated. But we should've
2939 caught an explicit exit/termination above. So warn (this is
2940 really an internal error) and claim the process or thread
2941 terminated with a SIGTRAP.
2944 warning ("process_wait: unknown process state");
2948 printf ("Process-level event %s, using tid %d\n",
2949 get_printable_name_of_ttrace_event (tsp
.tts_event
),
2956 target_post_wait (tsp
.tts_pid
, *status
);
2961 printf ("Done waiting, pid is %d, tid %d\n", real_pid
, real_tid
);
2964 /* All code external to this module uses the tid, but calls
2965 * it "pid". There's some tweaking so that the outside sees
2966 * the first thread as having the same number as the starting
2969 return_pid
= map_to_gdb_tid (real_tid
);
2971 /* Remember this for later use in "hppa_prepare_to_proceed".
2973 old_gdb_pid
= inferior_pid
;
2974 reported_pid
= return_pid
;
2975 reported_bpt
= ((tsp
.tts_event
& TTEVT_SIGNAL
) && (5 == tsp
.tts_u
.tts_signal
.tts_signo
));
2977 if (real_tid
== 0 || return_pid
== 0)
2979 warning ("Internal error: process-wait failed.");
2986 /* This function causes the caller's process to be traced by its
2987 parent. This is intended to be called after GDB forks itself,
2988 and before the child execs the target. Despite the name, it
2989 is called by the child.
2991 Note that HP-UX ttrace is rather funky in how this is done.
2992 If the parent wants to get the initial exec event of a child,
2993 it must set the ttrace event mask of the child to include execs.
2994 (The child cannot do this itself.) This must be done after the
2995 child is forked, but before it execs.
2997 To coordinate the parent and child, we implement a semaphore using
2998 pipes. After SETTRC'ing itself, the child tells the parent that
2999 it is now traceable by the parent, and waits for the parent's
3000 acknowledgement. The parent can then set the child's event mask,
3001 and notify the child that it can now exec.
3003 (The acknowledgement by parent happens as a result of a call to
3004 child_acknowledge_created_inferior.)
3007 parent_attach_all ()
3011 /* We need a memory home for a constant, to pass it to ttrace.
3012 The value of the constant is arbitrary, so long as both
3013 parent and child use the same value. Might as well use the
3014 "magic" constant provided by ttrace...
3016 uint64_t tc_magic_child
= TT_VERSION
;
3017 uint64_t tc_magic_parent
= 0;
3019 tt_status
= call_real_ttrace (
3024 (TTRACE_ARG_TYPE
) TT_VERSION
,
3030 /* Notify the parent that we're potentially ready to exec(). */
3031 write (startup_semaphore
.child_channel
[SEM_TALK
],
3033 sizeof (tc_magic_child
));
3035 /* Wait for acknowledgement from the parent. */
3036 read (startup_semaphore
.parent_channel
[SEM_LISTEN
],
3038 sizeof (tc_magic_parent
));
3040 if (tc_magic_child
!= tc_magic_parent
)
3041 warning ("mismatched semaphore magic");
3043 /* Discard our copy of the semaphore. */
3044 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
3045 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
3046 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
3047 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
3052 /* Despite being file-local, this routine is dealing with
3053 * actual process IDs, not thread ids. That's because it's
3054 * called before the first "wait" call, and there's no map
3055 * yet from tids to pids.
3057 * When it is called, a forked child is running, but waiting on
3058 * the semaphore. If you stop the child and re-start it,
3059 * things get confused, so don't do that! An attached child is
3062 * Since this is called after either attach or run, we
3063 * have to be the common part of both.
3066 require_notification_of_events (real_pid
)
3070 ttevent_t notifiable_events
;
3073 ttstate_t thread_state
;
3077 printf ("Require notif, pid is %d\n", real_pid
);
3080 /* Temporary HACK: tell inftarg.c/child_wait to not
3081 * loop until pids are the same.
3083 not_same_real_pid
= 0;
3085 sigemptyset (¬ifiable_events
.tte_signals
);
3086 notifiable_events
.tte_opts
= TTEO_NONE
;
3088 /* This ensures that forked children inherit their parent's
3089 * event mask, which we're setting here.
3091 * NOTE: if you debug gdb with itself, then the ultimate
3092 * debuggee gets flags set by the outermost gdb, as
3093 * a child of a child will still inherit.
3095 notifiable_events
.tte_opts
|= TTEO_PROC_INHERIT
;
3097 notifiable_events
.tte_events
= TTEVT_DEFAULT
;
3098 notifiable_events
.tte_events
|= TTEVT_SIGNAL
;
3099 notifiable_events
.tte_events
|= TTEVT_EXEC
;
3100 notifiable_events
.tte_events
|= TTEVT_EXIT
;
3101 notifiable_events
.tte_events
|= TTEVT_FORK
;
3102 notifiable_events
.tte_events
|= TTEVT_VFORK
;
3103 notifiable_events
.tte_events
|= TTEVT_LWP_CREATE
;
3104 notifiable_events
.tte_events
|= TTEVT_LWP_EXIT
;
3105 notifiable_events
.tte_events
|= TTEVT_LWP_TERMINATE
;
3107 tt_status
= call_real_ttrace (
3108 TT_PROC_SET_EVENT_MASK
,
3111 (TTRACE_ARG_TYPE
) & notifiable_events
,
3112 (TTRACE_ARG_TYPE
) sizeof (notifiable_events
),
3117 require_notification_of_exec_events (real_pid
)
3121 ttevent_t notifiable_events
;
3124 ttstate_t thread_state
;
3128 printf ("Require notif, pid is %d\n", real_pid
);
3131 /* Temporary HACK: tell inftarg.c/child_wait to not
3132 * loop until pids are the same.
3134 not_same_real_pid
= 0;
3136 sigemptyset (¬ifiable_events
.tte_signals
);
3137 notifiable_events
.tte_opts
= TTEO_NOSTRCCHLD
;
3139 /* This ensures that forked children don't inherit their parent's
3140 * event mask, which we're setting here.
3142 notifiable_events
.tte_opts
&= ~TTEO_PROC_INHERIT
;
3144 notifiable_events
.tte_events
= TTEVT_DEFAULT
;
3145 notifiable_events
.tte_events
|= TTEVT_EXEC
;
3146 notifiable_events
.tte_events
|= TTEVT_EXIT
;
3148 tt_status
= call_real_ttrace (
3149 TT_PROC_SET_EVENT_MASK
,
3152 (TTRACE_ARG_TYPE
) & notifiable_events
,
3153 (TTRACE_ARG_TYPE
) sizeof (notifiable_events
),
3158 /* This function is called by the parent process, with pid being the
3159 * ID of the child process, after the debugger has forked.
3162 child_acknowledge_created_inferior (pid
)
3165 /* We need a memory home for a constant, to pass it to ttrace.
3166 The value of the constant is arbitrary, so long as both
3167 parent and child use the same value. Might as well use the
3168 "magic" constant provided by ttrace...
3170 uint64_t tc_magic_parent
= TT_VERSION
;
3171 uint64_t tc_magic_child
= 0;
3173 /* Wait for the child to tell us that it has forked. */
3174 read (startup_semaphore
.child_channel
[SEM_LISTEN
],
3176 sizeof (tc_magic_child
));
3178 /* Clear thread info now. We'd like to do this in
3179 * "require...", but that messes up attach.
3181 clear_thread_info ();
3183 /* Tell the "rest of gdb" that the initial thread exists.
3184 * This isn't really a hack. Other thread-based versions
3185 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3187 * Q: Why don't we also add this thread to the local
3188 * list via "add_tthread"?
3190 * A: Because we don't know the tid, and can't stop the
3191 * the process safely to ask what it is. Anyway, we'll
3192 * add it when it gets the EXEC event.
3194 add_thread (pid
); /* in thread.c */
3196 /* We can now set the child's ttrace event mask.
3198 require_notification_of_exec_events (pid
);
3200 /* Tell ourselves that the process is running.
3202 process_state
= RUNNING
;
3204 /* Notify the child that it can exec. */
3205 write (startup_semaphore
.parent_channel
[SEM_TALK
],
3207 sizeof (tc_magic_parent
));
3209 /* Discard our copy of the semaphore. */
3210 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
3211 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
3212 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
3213 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
3218 * arrange for notification of all events by
3219 * calling require_notification_of_events.
3222 child_post_startup_inferior (real_pid
)
3225 require_notification_of_events (real_pid
);
3228 /* From here on, we should expect tids rather than pids.
3231 hppa_enable_catch_fork (tid
)
3235 ttevent_t ttrace_events
;
3237 /* Get the set of events that are currently enabled.
3239 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3241 (TTRACE_ARG_TYPE
) & ttrace_events
,
3242 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3245 perror_with_name ("ttrace");
3247 /* Add forks to that set. */
3248 ttrace_events
.tte_events
|= TTEVT_FORK
;
3252 printf ("enable fork, tid is %d\n", tid
);
3255 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3257 (TTRACE_ARG_TYPE
) & ttrace_events
,
3258 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3261 perror_with_name ("ttrace");
3266 hppa_disable_catch_fork (tid
)
3270 ttevent_t ttrace_events
;
3272 /* Get the set of events that are currently enabled.
3274 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3276 (TTRACE_ARG_TYPE
) & ttrace_events
,
3277 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3281 perror_with_name ("ttrace");
3283 /* Remove forks from that set. */
3284 ttrace_events
.tte_events
&= ~TTEVT_FORK
;
3288 printf ("disable fork, tid is %d\n", tid
);
3291 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3293 (TTRACE_ARG_TYPE
) & ttrace_events
,
3294 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3298 perror_with_name ("ttrace");
3302 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3304 child_insert_fork_catchpoint (tid
)
3307 /* Enable reporting of fork events from the kernel. */
3308 /* ??rehrauer: For the moment, we're always enabling these events,
3309 and just ignoring them if there's no catchpoint to catch them.
3316 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3318 child_remove_fork_catchpoint (tid
)
3321 /* Disable reporting of fork events from the kernel. */
3322 /* ??rehrauer: For the moment, we're always enabling these events,
3323 and just ignoring them if there's no catchpoint to catch them.
3331 hppa_enable_catch_vfork (tid
)
3335 ttevent_t ttrace_events
;
3337 /* Get the set of events that are currently enabled.
3339 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3341 (TTRACE_ARG_TYPE
) & ttrace_events
,
3342 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3346 perror_with_name ("ttrace");
3348 /* Add vforks to that set. */
3349 ttrace_events
.tte_events
|= TTEVT_VFORK
;
3353 printf ("enable vfork, tid is %d\n", tid
);
3356 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3358 (TTRACE_ARG_TYPE
) & ttrace_events
,
3359 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3363 perror_with_name ("ttrace");
3368 hppa_disable_catch_vfork (tid
)
3372 ttevent_t ttrace_events
;
3374 /* Get the set of events that are currently enabled. */
3375 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3377 (TTRACE_ARG_TYPE
) & ttrace_events
,
3378 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3382 perror_with_name ("ttrace");
3384 /* Remove vforks from that set. */
3385 ttrace_events
.tte_events
&= ~TTEVT_VFORK
;
3389 printf ("disable vfork, tid is %d\n", tid
);
3391 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3393 (TTRACE_ARG_TYPE
) & ttrace_events
,
3394 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3398 perror_with_name ("ttrace");
3402 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3404 child_insert_vfork_catchpoint (tid
)
3407 /* Enable reporting of vfork events from the kernel. */
3408 /* ??rehrauer: For the moment, we're always enabling these events,
3409 and just ignoring them if there's no catchpoint to catch them.
3416 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3418 child_remove_vfork_catchpoint (tid
)
3421 /* Disable reporting of vfork events from the kernel. */
3422 /* ??rehrauer: For the moment, we're always enabling these events,
3423 and just ignoring them if there's no catchpoint to catch them.
3429 #if defined(CHILD_HAS_FORKED)
3431 /* Q: Do we need to map the returned process ID to a thread ID?
3433 * A: I don't think so--here we want a _real_ pid. Any later
3434 * operations will call "require_notification_of_events" and
3435 * start the mapping.
3438 child_has_forked (tid
, childpid
)
3443 ttstate_t ttrace_state
;
3446 /* Do we have cached thread state that we can consult? If so, use it. */
3447 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3450 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3453 /* Nope, must read the thread's current state */
3456 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3458 (TTRACE_ARG_TYPE
) & ttrace_state
,
3459 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3463 perror_with_name ("ttrace");
3469 if (ttrace_state
.tts_event
& TTEVT_FORK
)
3471 *childpid
= ttrace_state
.tts_u
.tts_fork
.tts_fpid
;
3480 #if defined(CHILD_HAS_VFORKED)
3482 /* See child_has_forked for pid discussion.
3485 child_has_vforked (tid
, childpid
)
3490 ttstate_t ttrace_state
;
3493 /* Do we have cached thread state that we can consult? If so, use it. */
3494 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3496 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3498 /* Nope, must read the thread's current state */
3501 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3503 (TTRACE_ARG_TYPE
) & ttrace_state
,
3504 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3508 perror_with_name ("ttrace");
3514 if (ttrace_state
.tts_event
& TTEVT_VFORK
)
3516 *childpid
= ttrace_state
.tts_u
.tts_fork
.tts_fpid
;
3525 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3527 child_can_follow_vfork_prior_to_exec ()
3529 /* ttrace does allow this.
3531 ??rehrauer: However, I had major-league problems trying to
3532 convince wait_for_inferior to handle that case. Perhaps when
3533 it is rewritten to grok multiple processes in an explicit way...
3540 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3542 child_insert_exec_catchpoint (tid
)
3545 /* Enable reporting of exec events from the kernel. */
3546 /* ??rehrauer: For the moment, we're always enabling these events,
3547 and just ignoring them if there's no catchpoint to catch them.
3554 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3556 child_remove_exec_catchpoint (tid
)
3559 /* Disable reporting of execevents from the kernel. */
3560 /* ??rehrauer: For the moment, we're always enabling these events,
3561 and just ignoring them if there's no catchpoint to catch them.
3568 #if defined(CHILD_HAS_EXECD)
3570 child_has_execd (tid
, execd_pathname
)
3572 char **execd_pathname
;
3575 ttstate_t ttrace_state
;
3578 /* Do we have cached thread state that we can consult? If so, use it. */
3579 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3581 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3583 /* Nope, must read the thread's current state */
3586 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3588 (TTRACE_ARG_TYPE
) & ttrace_state
,
3589 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3593 perror_with_name ("ttrace");
3599 if (ttrace_state
.tts_event
& TTEVT_EXEC
)
3601 /* See child_pid_to_exec_file in this file: this is a macro.
3603 char *exec_file
= target_pid_to_exec_file (tid
);
3605 *execd_pathname
= savestring (exec_file
, strlen (exec_file
));
3614 #if defined(CHILD_HAS_SYSCALL_EVENT)
3616 child_has_syscall_event (pid
, kind
, syscall_id
)
3618 enum target_waitkind
*kind
;
3622 ttstate_t ttrace_state
;
3625 /* Do we have cached thread state that we can consult? If so, use it. */
3626 tinfo
= find_thread_info (map_from_gdb_tid (pid
));
3628 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3630 /* Nope, must read the thread's current state */
3633 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3635 (TTRACE_ARG_TYPE
) & ttrace_state
,
3636 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3640 perror_with_name ("ttrace");
3646 *kind
= TARGET_WAITKIND_SPURIOUS
; /* Until proven otherwise... */
3649 if (ttrace_state
.tts_event
& TTEVT_SYSCALL_ENTRY
)
3650 *kind
= TARGET_WAITKIND_SYSCALL_ENTRY
;
3651 else if (ttrace_state
.tts_event
& TTEVT_SYSCALL_RETURN
)
3652 *kind
= TARGET_WAITKIND_SYSCALL_RETURN
;
3656 *syscall_id
= ttrace_state
.tts_scno
;
3663 #if defined(CHILD_THREAD_ALIVE)
3665 /* Check to see if the given thread is alive.
3667 * We'll trust the thread list, as the more correct
3668 * approach of stopping the process and spinning down
3669 * the OS's thread list is _very_ expensive.
3671 * May need a FIXME for that reason.
3674 child_thread_alive (gdb_tid
)
3679 /* This spins down the lists twice.
3680 * Possible peformance improvement here!
3682 tid
= map_from_gdb_tid (gdb_tid
);
3683 return !is_terminated (tid
);
3690 /* This function attempts to read the specified number of bytes from the
3691 save_state_t that is our view into the hardware registers, starting at
3692 ss_offset, and ending at ss_offset + sizeof_buf - 1
3694 If this function succeeds, it deposits the fetched bytes into buf,
3697 If it fails, it returns a negative result. The contents of buf are
3698 undefined it this function fails.
3701 read_from_register_save_state (tid
, ss_offset
, buf
, sizeof_buf
)
3703 TTRACE_ARG_TYPE ss_offset
;
3708 register_value_t register_value
= 0;
3710 tt_status
= call_ttrace (TT_LWP_RUREGS
,
3713 (TTRACE_ARG_TYPE
) sizeof_buf
,
3714 (TTRACE_ARG_TYPE
) buf
);
3717 /* Map ttrace's version of success to our version.
3718 * Sometime ttrace returns 0, but that's ok here.
3726 /* This function attempts to write the specified number of bytes to the
3727 save_state_t that is our view into the hardware registers, starting at
3728 ss_offset, and ending at ss_offset + sizeof_buf - 1
3730 If this function succeeds, it deposits the bytes in buf, and returns 0.
3732 If it fails, it returns a negative result. The contents of the save_state_t
3733 are undefined it this function fails.
3736 write_to_register_save_state (tid
, ss_offset
, buf
, sizeof_buf
)
3738 TTRACE_ARG_TYPE ss_offset
;
3743 register_value_t register_value
= 0;
3745 tt_status
= call_ttrace (TT_LWP_WUREGS
,
3748 (TTRACE_ARG_TYPE
) sizeof_buf
,
3749 (TTRACE_ARG_TYPE
) buf
);
3754 /* This function is a sop to the largeish number of direct calls
3755 to call_ptrace that exist in other files. Rather than create
3756 functions whose name abstracts away from ptrace, and change all
3757 the present callers of call_ptrace, we'll do the expedient (and
3758 perhaps only practical) thing.
3760 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3761 process. Thus, we must translate all ptrace requests into their
3762 process-specific, ttrace equivalents.
3765 call_ptrace (pt_request
, gdb_tid
, addr
, data
)
3768 PTRACE_ARG3_TYPE addr
;
3772 TTRACE_ARG_TYPE tt_addr
= (TTRACE_ARG_TYPE
) addr
;
3773 TTRACE_ARG_TYPE tt_data
= (TTRACE_ARG_TYPE
) data
;
3774 TTRACE_ARG_TYPE tt_addr2
= TT_NIL
;
3776 register_value_t register_value
;
3779 /* Perform the necessary argument translation. Note that some
3780 cases are funky enough in the ttrace realm that we handle them
3785 /* The following cases cannot conveniently be handled conveniently
3786 by merely adjusting the ptrace arguments and feeding into the
3787 generic call to ttrace at the bottom of this function.
3789 Note that because all branches of this switch end in "return",
3790 there's no need for any "break" statements.
3793 return parent_attach_all ();
3796 tt_status
= read_from_register_save_state (gdb_tid
,
3799 sizeof (register_value
));
3802 return register_value
;
3805 register_value
= (int) tt_data
;
3806 tt_status
= write_to_register_save_state (gdb_tid
,
3809 sizeof (register_value
));
3814 tt_status
= call_ttrace (TT_PROC_RDTEXT
, /* Implicit 4-byte xfer becomes block-xfer. */
3817 (TTRACE_ARG_TYPE
) 4,
3818 (TTRACE_ARG_TYPE
) & read_buf
);
3824 tt_status
= call_ttrace (TT_PROC_RDDATA
, /* Implicit 4-byte xfer becomes block-xfer. */
3827 (TTRACE_ARG_TYPE
) 4,
3828 (TTRACE_ARG_TYPE
) & read_buf
);
3834 tt_status
= call_real_ttrace (TT_PROC_ATTACH
,
3835 map_from_gdb_tid (gdb_tid
),
3838 (TTRACE_ARG_TYPE
) TT_VERSION
,
3844 /* The following cases are handled by merely adjusting the ptrace
3845 arguments and feeding into the generic call to ttrace.
3848 tt_request
= TT_PROC_DETACH
;
3852 tt_request
= TT_PROC_WRTEXT
; /* Translates 4-byte xfer to block-xfer. */
3853 tt_data
= 4; /* This many bytes. */
3854 tt_addr2
= (TTRACE_ARG_TYPE
) & data
; /* Address of xfer source. */
3858 tt_request
= TT_PROC_WRDATA
; /* Translates 4-byte xfer to block-xfer. */
3859 tt_data
= 4; /* This many bytes. */
3860 tt_addr2
= (TTRACE_ARG_TYPE
) & data
; /* Address of xfer source. */
3864 tt_request
= TT_PROC_RDTEXT
;
3868 tt_request
= TT_PROC_RDDATA
;
3872 tt_request
= TT_PROC_WRTEXT
;
3876 tt_request
= TT_PROC_WRDATA
;
3880 tt_request
= TT_PROC_CONTINUE
;
3884 tt_request
= TT_LWP_SINGLE
; /* Should not be making this request? */
3888 tt_request
= TT_PROC_EXIT
;
3891 case PT_GET_PROCESS_PATHNAME
:
3892 tt_request
= TT_PROC_GET_PATHNAME
;
3896 tt_request
= pt_request
; /* Let ttrace be the one to complain. */
3900 return call_ttrace (tt_request
,
3907 /* Kill that pesky process!
3915 thread_info
**paranoia
;
3918 if (inferior_pid
== 0)
3921 /* Walk the list of "threads", some of which are "pseudo threads",
3922 aka "processes". For each that is NOT inferior_pid, stop it,
3925 You see, we may not have just a single process to kill. If we're
3926 restarting or quitting or detaching just after the inferior has
3927 forked, then we've actually two processes to clean up.
3929 But we can't just call target_mourn_inferior() for each, since that
3930 zaps the target vector.
3933 paranoia
= (thread_info
**) malloc (thread_head
.count
*
3934 sizeof (thread_info
*));
3937 t
= thread_head
.head
;
3941 paranoia
[para_count
] = t
;
3942 for (i
= 0; i
< para_count
; i
++)
3944 if (t
->next
== paranoia
[i
])
3946 warning ("Bad data in gdb's thread data; repairing.");
3952 if (t
->am_pseudo
&& (t
->pid
!= inferior_pid
))
3954 /* TT_PROC_STOP doesn't require a subsequent ttrace_wait, as it
3955 * generates no event.
3957 call_ttrace (TT_PROC_STOP
,
3963 call_ttrace (TT_PROC_DETACH
,
3966 (TTRACE_ARG_TYPE
) TARGET_SIGNAL_0
,
3974 call_ttrace (TT_PROC_STOP
,
3979 target_mourn_inferior ();
3980 clear_thread_info ();
3984 #ifndef CHILD_RESUME
3986 /* Sanity check a thread about to be continued.
3989 thread_dropping_event_check (p
)
3995 * This seems to happen when we "next" over a
3996 * "fork()" while following the parent. If it's
3997 * the FORK event, that's ok. If it's a SIGNAL
3998 * in the unfollowed child, that's ok to--but
3999 * how can we know that's what's going on?
4005 if (p
->last_stop_state
.tts_event
== TTEVT_FORK
)
4010 else if (p
->last_stop_state
.tts_event
== TTEVT_SIGNAL
)
4012 /* Ok, close eyes and let it happen.
4018 /* This shouldn't happen--we're dropping a
4021 warning ("About to continue process %d, thread %d with unhandled event %s.",
4023 get_printable_name_of_ttrace_event (
4024 p
->last_stop_state
.tts_event
));
4034 /* No saved state, have to assume it failed.
4036 warning ("About to continue process %d, thread %d with unhandled event.",
4045 } /* thread_dropping_event_check */
4047 /* Use a loop over the threads to continue all the threads but
4048 * the one specified, which is to be stepped.
4051 threads_continue_all_but_one (gdb_tid
, signal
)
4064 printf ("Using loop over threads to step/resume with signals\n");
4067 /* First update the thread list.
4070 real_tid
= map_from_gdb_tid (gdb_tid
);
4071 real_pid
= get_pid_for (real_tid
);
4073 scan_tid
= get_process_first_stopped_thread_id (real_pid
, &state
);
4074 while (0 != scan_tid
)
4078 /* FIX: later should check state is stopped;
4079 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
4082 if (state
.tts_flags
& TTS_STATEMASK
!= TTS_WASSUSPENDED
)
4083 printf ("About to continue non-stopped thread %d\n", scan_tid
);
4086 p
= find_thread_info (scan_tid
);
4089 add_tthread (real_pid
, scan_tid
);
4090 p
= find_thread_info (scan_tid
);
4092 /* This is either a newly-created thread or the
4093 * result of a fork; in either case there's no
4094 * actual event to worry about.
4098 if (state
.tts_event
!= TTEVT_NONE
)
4100 /* Oops, do need to worry!
4102 warning ("Unexpected thread with \"%s\" event.",
4103 get_printable_name_of_ttrace_event (state
.tts_event
));
4106 else if (scan_tid
!= p
->tid
)
4107 error ("Bad data in thread database.");
4112 printf ("Why are we continuing a dead thread?\n");
4117 scan_tid
= get_process_next_stopped_thread_id (real_pid
, &state
);
4120 /* Remove unseen threads.
4122 update_thread_list ();
4124 /* Now run down the thread list and continue or step.
4126 for (p
= thread_head
.head
; p
; p
= p
->next
)
4131 thread_dropping_event_check (p
);
4133 /* Pass the correct signals along.
4137 thread_signal
= p
->signal_value
;
4143 if (p
->tid
!= real_tid
)
4146 * Not the thread of interest, so continue it
4147 * as the user expects.
4149 if (p
->stepping_mode
== DO_STEP
)
4151 /* Just step this thread.
4157 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4162 /* Regular continue (default case).
4168 (TTRACE_ARG_TYPE
) target_signal_to_host (thread_signal
),
4174 /* Step the thread of interest.
4180 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4183 } /* Loop over threads */
4184 } /* End threads_continue_all_but_one */
4186 /* Use a loop over the threads to continue all the threads.
4187 * This is done when a signal must be sent to any of the threads.
4190 threads_continue_all_with_signals (gdb_tid
, signal
)
4203 printf ("Using loop over threads to resume with signals\n");
4206 /* Scan and update thread list.
4209 real_tid
= map_from_gdb_tid (gdb_tid
);
4210 real_pid
= get_pid_for (real_tid
);
4212 scan_tid
= get_process_first_stopped_thread_id (real_pid
, &state
);
4213 while (0 != scan_tid
)
4218 if (state
.tts_flags
& TTS_STATEMASK
!= TTS_WASSUSPENDED
)
4219 warning ("About to continue non-stopped thread %d\n", scan_tid
);
4222 p
= find_thread_info (scan_tid
);
4225 add_tthread (real_pid
, scan_tid
);
4226 p
= find_thread_info (scan_tid
);
4228 /* This is either a newly-created thread or the
4229 * result of a fork; in either case there's no
4230 * actual event to worry about.
4234 if (state
.tts_event
!= TTEVT_NONE
)
4236 /* Oops, do need to worry!
4238 warning ("Unexpected thread with \"%s\" event.",
4239 get_printable_name_of_ttrace_event (state
.tts_event
));
4246 printf ("Why are we continuing a dead thread? (1)\n");
4251 scan_tid
= get_process_next_stopped_thread_id (real_pid
, &state
);
4254 /* Remove unseen threads from our list.
4256 update_thread_list ();
4258 /* Continue the threads.
4260 for (p
= thread_head
.head
; p
; p
= p
->next
)
4265 thread_dropping_event_check (p
);
4267 /* Pass the correct signals along.
4269 if (p
->tid
== real_tid
)
4271 thread_signal
= signal
;
4274 else if (p
->have_signal
)
4276 thread_signal
= p
->signal_value
;
4282 if (p
->stepping_mode
== DO_STEP
)
4288 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4293 /* Continue this thread (default case).
4299 (TTRACE_ARG_TYPE
) target_signal_to_host (thread_signal
),
4303 } /* End threads_continue_all_with_signals */
4305 /* Step one thread only.
4308 thread_fake_step (tid
, signal
)
4310 enum target_signal signal
;
4317 printf ("Doing a fake-step over a bpt, etc. for %d\n", tid
);
4319 if (is_terminated (tid
))
4320 printf ("Why are we continuing a dead thread? (4)\n");
4324 if (doing_fake_step
)
4325 warning ("Step while step already in progress.");
4327 /* See if there's a saved signal value for this
4328 * thread to be passed on, but no current signal.
4330 p
= find_thread_info (tid
);
4333 if (p
->have_signal
&& signal
== NULL
)
4335 /* Pass on a saved signal.
4337 signal
= p
->signal_value
;
4344 warning ("Internal error: continuing unhandled thread.");
4346 call_ttrace (TT_LWP_SINGLE
,
4349 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4352 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4353 * for this thread only, and clear any saved signal info.
4355 doing_fake_step
= 1;
4356 fake_step_tid
= tid
;
4358 } /* End thread_fake_step */
4360 /* Continue one thread when a signal must be sent to it.
4363 threads_continue_one_with_signal (gdb_tid
, signal
)
4373 printf ("Continuing one thread with a signal\n");
4376 real_tid
= map_from_gdb_tid (gdb_tid
);
4377 real_pid
= get_pid_for (real_tid
);
4379 p
= find_thread_info (real_tid
);
4382 add_tthread (real_pid
, real_tid
);
4388 printf ("Why are we continuing a dead thread? (2)\n");
4392 warning ("Internal error: continuing unhandled thread.");
4396 call_ttrace (TT_LWP_CONTINUE
,
4399 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4404 #ifndef CHILD_RESUME
4406 /* Resume execution of the inferior process.
4408 * This routine is in charge of setting the "handled" bits.
4410 * If STEP is zero, continue it.
4411 * If STEP is nonzero, single-step it.
4413 * If SIGNAL is nonzero, give it that signal.
4415 * If TID is -1, apply to all threads.
4416 * If TID is not -1, apply to specified thread.
4420 * TID \________________________________________________
4422 * -1 | Step current Continue all threads
4423 * | thread and (but which gets any
4424 * | continue others signal?--We look at
4427 * N | Step _this_ thread Continue _this_ thread
4428 * | and leave others and leave others
4429 * | stopped; internally stopped; used only for
4430 * | used by gdb, never hardware watchpoints
4431 * | a user command. and attach, never a
4435 child_resume (gdb_tid
, step
, signal
)
4438 enum target_signal signal
;
4440 int resume_all_threads
;
4442 process_state_t new_process_state
;
4444 resume_all_threads
=
4445 (gdb_tid
== INFTTRACE_ALL_THREADS
) ||
4448 if (resume_all_threads
)
4450 /* Resume all threads, but first pick a tid value
4451 * so we can get the pid when in call_ttrace doing
4454 if (vfork_in_flight
)
4455 tid
= vforking_child_pid
;
4457 tid
= map_from_gdb_tid (inferior_pid
);
4460 tid
= map_from_gdb_tid (gdb_tid
);
4465 if (more_events_left
)
4466 printf ("More events; ");
4469 printf ("Sending signal %d; ", signal
);
4471 if (resume_all_threads
)
4474 printf ("Continue process %d\n", tid
);
4476 printf ("Step/continue thread %d\n", tid
);
4481 printf ("Continue thread %d\n", tid
);
4483 printf ("Step just thread %d\n", tid
);
4486 if (vfork_in_flight
)
4487 printf ("Vfork in flight\n");
4491 if (process_state
== RUNNING
)
4492 warning ("Internal error in resume logic; doing resume or step anyway.");
4494 if (!step
/* Asked to continue... */
4495 && resume_all_threads
/* whole process.. */
4496 && signal
!= 0 /* with a signal... */
4497 && more_events_left
> 0)
4498 { /* but we can't yet--save it! */
4500 /* Continue with signal means we have to set the pending
4501 * signal value for this thread.
4507 printf ("Saving signal %d for thread %d\n", signal
, tid
);
4510 k
= find_thread_info (tid
);
4514 k
->signal_value
= signal
;
4519 printf ("Why are we continuing a dead thread? (3)\n");
4527 printf ("No thread info for tid %d\n", tid
);
4532 /* Are we faking this "continue" or "step"?
4534 * We used to do steps by continuing all the threads for
4535 * which the events had been handled already. While
4536 * conceptually nicer (hides it all in a lower level), this
4537 * can lead to starvation and a hang (e.g. all but one thread
4538 * are unhandled at a breakpoint just before a "join" operation,
4539 * and one thread is in the join, and the user wants to step that
4542 if (resume_all_threads
/* Whole process, therefore user command */
4543 && more_events_left
> 0)
4544 { /* But we can't do this yet--fake it! */
4549 /* No need to do any notes on a per-thread
4550 * basis--we're done!
4552 #ifdef WAIT_BUFFER_DEBUG
4554 printf ("Faking a process resume.\n");
4562 #ifdef WAIT_BUFFER_DEBUG
4564 printf ("Faking a process step.\n");
4569 p
= find_thread_info (tid
);
4572 warning ("No thread information for tid %d, 'next' command ignored.\n", tid
);
4581 printf ("Why are we continuing a dead thread? (3.5)\n");
4584 if (p
->stepping_mode
!= DO_DEFAULT
)
4586 warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored.");
4592 p
->stepping_mode
= DO_STEP
;
4594 p
->stepping_mode
= DO_CONTINUE
;
4597 } /* Have thread info */
4598 } /* Must fake step or go */
4600 /* Execept for fake-steps, from here on we know we are
4601 * going to wind up with a running process which will
4604 new_process_state
= RUNNING
;
4606 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4607 * it was. (If GDB wanted it to start some other way, we have already
4608 * written a new PC value to the child.)
4610 * If this system does not support PT_STEP, a higher level function will
4611 * have called single_step() to transmute the step request into a
4612 * continue request (by setting breakpoints on all possible successor
4613 * instructions), so we don't have to worry about that here.
4617 if (resume_all_threads
)
4620 * Regular user step: other threads get a "continue".
4622 threads_continue_all_but_one (tid
, signal
);
4623 clear_all_handled ();
4624 clear_all_stepping_mode ();
4629 /* "Fake step": gdb is stepping one thread over a
4630 * breakpoint, watchpoint, or out of a library load
4631 * event, etc. The rest just stay where they are.
4633 * Also used when there are pending events: we really
4634 * step the current thread, but leave the rest stopped.
4635 * Users can't request this, but "wait_for_inferior"
4638 thread_fake_step (tid
, signal
);
4640 /* Clear the "handled" state of this thread, because
4641 * we'll soon get a new event for it. Other events
4642 * stay as they were.
4644 clear_handled (tid
);
4645 clear_stepping_mode (tid
);
4646 new_process_state
= FAKE_STEPPING
;
4652 /* TT_LWP_CONTINUE can pass signals to threads,
4653 * TT_PROC_CONTINUE can't. So if there are any
4654 * signals to pass, we have to use the (slower)
4655 * loop over the stopped threads.
4657 * Equally, if we have to not continue some threads,
4658 * due to saved events, we have to use the loop.
4660 if ((signal
!= 0) || saved_signals_exist ())
4662 if (resume_all_threads
)
4667 printf ("Doing a continue by loop of all threads\n");
4670 threads_continue_all_with_signals (tid
, signal
);
4672 clear_all_handled ();
4673 clear_all_stepping_mode ();
4679 printf ("Doing a continue w/signal of just thread %d\n", tid
);
4682 threads_continue_one_with_signal (tid
, signal
);
4684 /* Clear the "handled" state of this thread, because
4685 * we'll soon get a new event for it. Other events
4686 * can stay as they were.
4688 clear_handled (tid
);
4689 clear_stepping_mode (tid
);
4695 /* No signals to send.
4697 if (resume_all_threads
)
4701 printf ("Doing a continue by process of process %d\n", tid
);
4704 if (more_events_left
> 0)
4706 warning ("Losing buffered events on continue.");
4707 more_events_left
= 0;
4710 call_ttrace (TT_PROC_CONTINUE
,
4716 clear_all_handled ();
4717 clear_all_stepping_mode ();
4725 printf ("Doing a continue of just thread %d\n", tid
);
4726 if (is_terminated (tid
))
4727 printf ("Why are we continuing a dead thread? (5)\n");
4731 call_ttrace (TT_LWP_CONTINUE
,
4737 /* Clear the "handled" state of this thread, because
4738 * we'll soon get a new event for it. Other events
4739 * can stay as they were.
4741 clear_handled (tid
);
4742 clear_stepping_mode (tid
);
4747 process_state
= new_process_state
;
4749 #ifdef WAIT_BUFFER_DEBUG
4751 printf ("Process set to %s\n",
4752 get_printable_name_of_process_state (process_state
));
4756 #endif /* CHILD_RESUME */
4759 #ifdef ATTACH_DETACH
4763 * One worry is that we may not be attaching to "inferior_pid"
4764 * and thus may not want to clear out our data. FIXME?
4768 update_thread_state_after_attach (pid
, kind_of_go
)
4770 attach_continue_t kind_of_go
;
4773 ttstate_t thread_state
;
4777 /* The process better be stopped.
4779 if (process_state
!= STOPPED
4780 && process_state
!= VFORKING
)
4781 warning ("Internal error attaching.");
4783 /* Clear out old tthread info and start over. This has the
4784 * side effect of ensuring that the TRAP is reported as being
4785 * in the right thread (re-mapped from tid to pid).
4787 * It's because we need to add the tthread _now_ that we
4788 * need to call "clear_thread_info" _now_, and that's why
4789 * "require_notification_of_events" doesn't clear the thread
4790 * info (it's called later than this routine).
4792 clear_thread_info ();
4795 for (tid
= get_process_first_stopped_thread_id (pid
, &thread_state
);
4797 tid
= get_process_next_stopped_thread_id (pid
, &thread_state
))
4806 printf ("Attaching to process %d, thread %d\n",
4811 /* Tell ourselves and the "rest of gdb" that this thread
4814 * This isn't really a hack. Other thread-based versions
4815 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4817 * We don't need to do mapping here, as we know this
4818 * is the first thread and thus gets the real pid
4819 * (and is "inferior_pid").
4821 * NOTE: it probably isn't the originating thread,
4822 * but that doesn't matter (we hope!).
4824 add_tthread (pid
, tid
);
4825 p
= find_thread_info (tid
);
4826 if (NULL
== p
) /* ?We just added it! */
4827 error ("Internal error adding a thread on attach.");
4829 copy_ttstate_t (&p
->last_stop_state
, thread_state
);
4832 if (DO_ATTACH_CONTINUE
== kind_of_go
)
4835 * If we are going to CONTINUE afterwards,
4836 * raising a SIGTRAP, don't bother trying to
4837 * handle this event. But check first!
4839 switch (p
->last_stop_state
.tts_event
)
4843 /* Ok to set this handled.
4848 warning ("Internal error; skipping event %s on process %d, thread %d.",
4849 get_printable_name_of_ttrace_event (
4850 p
->last_stop_state
.tts_event
),
4854 set_handled (pid
, tid
);
4859 /* There will be no "continue" opertion, so the
4860 * process remains stopped. Don't set any events
4861 * handled except the "gimmies".
4863 switch (p
->last_stop_state
.tts_event
)
4867 /* Ok to ignore this.
4869 set_handled (pid
, tid
);
4874 /* Expected "other" FORK or EXEC event from a
4880 printf ("Internal error: failed to handle event %s on process %d, thread %d.",
4881 get_printable_name_of_ttrace_event (
4882 p
->last_stop_state
.tts_event
),
4887 add_thread (tid
); /* in thread.c */
4895 /* One mustn't call ttrace_wait() after attaching via ttrace,
4896 'cause the process is stopped already.
4898 However, the upper layers of gdb's execution control will
4899 want to wait after attaching (but not after forks, in
4900 which case they will be doing a "target_resume", anticipating
4901 a later TTEVT_EXEC or TTEVT_FORK event).
4903 To make this attach() implementation more compatible with
4904 others, we'll make the attached-to process raise a SIGTRAP.
4906 Issue: this continues only one thread. That could be
4907 dangerous if the thread is blocked--the process won't run
4908 and no trap will be raised. FIX! (check state.tts_flags?
4909 need one that's either TTS_WASRUNNING--but we've stopped
4910 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4912 if (DO_ATTACH_CONTINUE
== kind_of_go
)
4914 tt_status
= call_real_ttrace (
4919 (TTRACE_ARG_TYPE
) target_signal_to_host (TARGET_SIGNAL_TRAP
),
4922 perror_with_name ("ttrace");
4924 clear_handled (a_thread
); /* So TRAP will be reported. */
4928 process_state
= RUNNING
;
4933 #endif /* ATTACH_DETACH */
4936 #ifdef ATTACH_DETACH
4937 /* Start debugging the process whose number is PID.
4946 tt_status
= call_real_ttrace (
4951 (TTRACE_ARG_TYPE
) TT_VERSION
,
4954 perror_with_name ("ttrace attach");
4956 /* If successful, the process is now stopped.
4958 process_state
= STOPPED
;
4960 /* Our caller ("attach_command" in "infcmd.c")
4961 * expects to do a "wait_for_inferior" after
4962 * the attach, so make sure the inferior is
4963 * running when we're done.
4965 update_thread_state_after_attach (pid
, DO_ATTACH_CONTINUE
);
4971 #if defined(CHILD_POST_ATTACH)
4973 child_post_attach (pid
)
4978 printf ("child-post-attach call\n");
4981 require_notification_of_events (pid
);
4986 /* Stop debugging the process whose number is PID
4987 and continue it with signal number SIGNAL.
4988 SIGNAL = 0 means just continue it.
4995 call_ttrace (TT_PROC_DETACH
,
4998 (TTRACE_ARG_TYPE
) signal
,
5002 clear_thread_info ();
5004 /* Process-state? */
5006 #endif /* ATTACH_DETACH */
5009 /* Default the type of the ttrace transfer to int. */
5010 #ifndef TTRACE_XFER_TYPE
5011 #define TTRACE_XFER_TYPE int
5015 _initialize_kernel_u_addr ()
5019 #if !defined (CHILD_XFER_MEMORY)
5020 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
5021 in the NEW_SUN_TTRACE case.
5022 It ought to be straightforward. But it appears that writing did
5023 not write the data that I specified. I cannot understand where
5024 it got the data that it actually did write. */
5026 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
5027 to debugger memory starting at MYADDR. Copy to inferior if
5030 Returns the length copied, which is either the LEN argument or zero.
5031 This xfer function does not do partial moves, since child_ops
5032 doesn't allow memory operations to cross below us in the target stack
5036 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
5041 struct target_ops
*target
; /* ignored */
5044 /* Round starting address down to longword boundary. */
5045 register CORE_ADDR addr
= memaddr
& -sizeof (TTRACE_XFER_TYPE
);
5046 /* Round ending address up; get number of longwords that makes. */
5048 = (((memaddr
+ len
) - addr
) + sizeof (TTRACE_XFER_TYPE
) - 1)
5049 / sizeof (TTRACE_XFER_TYPE
);
5050 /* Allocate buffer of that many longwords. */
5051 register TTRACE_XFER_TYPE
*buffer
5052 = (TTRACE_XFER_TYPE
*) alloca (count
* sizeof (TTRACE_XFER_TYPE
));
5056 /* Fill start and end extra bytes of buffer with existing memory data. */
5058 if (addr
!= memaddr
|| len
< (int) sizeof (TTRACE_XFER_TYPE
))
5060 /* Need part of initial word -- fetch it. */
5061 buffer
[0] = call_ttrace (TT_LWP_RDTEXT
,
5063 (TTRACE_ARG_TYPE
) addr
,
5068 if (count
> 1) /* FIXME, avoid if even boundary */
5070 buffer
[count
- 1] = call_ttrace (TT_LWP_RDTEXT
,
5073 (addr
+ (count
- 1) * sizeof (TTRACE_XFER_TYPE
))),
5078 /* Copy data to be written over corresponding part of buffer */
5080 memcpy ((char *) buffer
+ (memaddr
& (sizeof (TTRACE_XFER_TYPE
) - 1)),
5084 /* Write the entire buffer. */
5086 for (i
= 0; i
< count
; i
++, addr
+= sizeof (TTRACE_XFER_TYPE
))
5089 call_ttrace (TT_LWP_WRDATA
,
5091 (TTRACE_ARG_TYPE
) addr
,
5092 (TTRACE_ARG_TYPE
) buffer
[i
],
5096 /* Using the appropriate one (I or D) is necessary for
5097 Gould NP1, at least. */
5099 call_ttrace (TT_LWP_WRTEXT
,
5101 (TTRACE_ARG_TYPE
) addr
,
5102 (TTRACE_ARG_TYPE
) buffer
[i
],
5111 /* Read all the longwords */
5112 for (i
= 0; i
< count
; i
++, addr
+= sizeof (TTRACE_XFER_TYPE
))
5115 buffer
[i
] = call_ttrace (TT_LWP_RDTEXT
,
5117 (TTRACE_ARG_TYPE
) addr
,
5125 /* Copy appropriate bytes out of the buffer. */
5127 (char *) buffer
+ (memaddr
& (sizeof (TTRACE_XFER_TYPE
) - 1)),
5137 int udot_off
; /* Offset into user struct */
5138 int udot_val
; /* Value from user struct at udot_off */
5139 char mess
[128]; /* For messages */
5141 if (!target_has_execution
)
5143 error ("The program is not being run.");
5146 #if !defined (KERNEL_U_SIZE)
5148 /* Adding support for this command is easy. Typically you just add a
5149 routine, called "kernel_u_size" that returns the size of the user
5150 struct, to the appropriate *-nat.c file and then add to the native
5151 config file "#define KERNEL_U_SIZE kernel_u_size()" */
5152 error ("Don't know how large ``struct user'' is in this version of gdb.");
5156 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
5158 if ((udot_off
% 24) == 0)
5162 printf_filtered ("\n");
5164 printf_filtered ("%04x:", udot_off
);
5166 udot_val
= call_ttrace (TT_LWP_RUREGS
,
5168 (TTRACE_ARG_TYPE
) udot_off
,
5173 sprintf (mess
, "\nreading user struct at offset 0x%x", udot_off
);
5174 perror_with_name (mess
);
5176 /* Avoid using nonportable (?) "*" in print specs */
5177 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
5179 printf_filtered ("\n");
5183 #endif /* !defined (CHILD_XFER_MEMORY). */
5185 /* TTrace version of "target_pid_to_exec_file"
5188 child_pid_to_exec_file (tid
)
5191 static char exec_file_buffer
[1024];
5193 CORE_ADDR top_of_stack
;
5198 int saved_inferior_pid
;
5200 /* As of 10.x HP-UX, there's an explicit request to get the
5203 tt_status
= call_ttrace (TT_PROC_GET_PATHNAME
,
5205 (TTRACE_ARG_TYPE
) exec_file_buffer
,
5206 (TTRACE_ARG_TYPE
) sizeof (exec_file_buffer
) - 1,
5209 return exec_file_buffer
;
5211 /* ??rehrauer: The above request may or may not be broken. It
5212 doesn't seem to work when I use it. But, it may be designed
5213 to only work immediately after an exec event occurs. (I'm
5214 waiting for COSL to explain.)
5216 In any case, if it fails, try a really, truly amazingly gross
5217 hack that DDE uses, of pawing through the process' data
5218 segment to find the pathname.
5220 top_of_stack
= 0x7b03a000;
5224 /* On the chance that pid != inferior_pid, set inferior_pid
5225 to pid, so that (grrrr!) implicit uses of inferior_pid get
5228 saved_inferior_pid
= inferior_pid
;
5231 /* Try to grab a null-terminated string. */
5234 if (target_read_memory (top_of_stack
, four_chars
, 4) != 0)
5236 inferior_pid
= saved_inferior_pid
;
5239 for (i
= 0; i
< 4; i
++)
5241 exec_file_buffer
[name_index
++] = four_chars
[i
];
5242 done
= (four_chars
[i
] == '\0');
5249 if (exec_file_buffer
[0] == '\0')
5251 inferior_pid
= saved_inferior_pid
;
5255 inferior_pid
= saved_inferior_pid
;
5256 return exec_file_buffer
;
5261 pre_fork_inferior ()
5265 status
= pipe (startup_semaphore
.parent_channel
);
5268 warning ("error getting parent pipe for startup semaphore");
5272 status
= pipe (startup_semaphore
.child_channel
);
5275 warning ("error getting child pipe for startup semaphore");
5280 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5281 * ultimately from "follow_inferior_fork" in infrun.c,
5282 * itself called from "resume".
5284 * This seems to be intended to attach after a fork or
5285 * vfork, while "attach" is used to attach to a pid
5286 * given by the user. The check for an existing attach
5287 * seems odd--it always fails in our test system.
5290 hppa_require_attach (pid
)
5296 unsigned int regs_offset
;
5297 process_state_t old_process_state
= process_state
;
5299 /* Are we already attached? There appears to be no explicit
5300 * way to answer this via ttrace, so we try something which
5301 * should be innocuous if we are attached. If that fails,
5302 * then we assume we're not attached, and so attempt to make
5306 tt_status
= call_real_ttrace (TT_PROC_STOP
,
5309 (TTRACE_ARG_TYPE
) TT_NIL
,
5310 (TTRACE_ARG_TYPE
) TT_NIL
,
5315 /* No change to process-state!
5322 /* If successful, the process is now stopped. But if
5323 * we're VFORKING, the parent is still running, so don't
5324 * change the process state.
5326 if (process_state
!= VFORKING
)
5327 process_state
= STOPPED
;
5329 /* If we were already attached, you'd think that we
5330 * would need to start going again--but you'd be wrong,
5331 * as the fork-following code is actually in the middle
5332 * of the "resume" routine in in "infrun.c" and so
5333 * will (almost) immediately do a resume.
5335 * On the other hand, if we are VFORKING, which means
5336 * that the child and the parent share a process for a
5337 * while, we know that "resume" won't be resuming
5338 * until the child EXEC event is seen. But we still
5339 * don't want to continue, as the event is already
5342 update_thread_state_after_attach (pid
, DONT_ATTACH_CONTINUE
);
5343 } /* STOP succeeded */
5349 hppa_require_detach (pid
, signal
)
5355 /* If signal is non-zero, we must pass the signal on to the active
5356 thread prior to detaching. We do this by continuing the threads
5362 threads_continue_all_with_signals (pid
, signal
);
5366 tt_status
= call_ttrace (TT_PROC_DETACH
,
5372 errno
= 0; /* Ignore any errors. */
5374 /* process_state? */
5379 /* Given the starting address of a memory page, hash it to a bucket in
5380 the memory page dictionary.
5383 get_dictionary_bucket_of_page (page_start
)
5384 CORE_ADDR page_start
;
5388 hash
= (page_start
/ memory_page_dictionary
.page_size
);
5389 hash
= hash
% MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
;
5395 /* Given a memory page's starting address, get (i.e., find an existing
5396 or create a new) dictionary entry for the page. The page will be
5397 write-protected when this function returns, but may have a reference
5398 count of 0 (if the page was newly-added to the dictionary).
5400 static memory_page_t
*
5401 get_dictionary_entry_of_page (pid
, page_start
)
5403 CORE_ADDR page_start
;
5406 memory_page_t
*page
= NULL
;
5407 memory_page_t
*previous_page
= NULL
;
5409 /* We're going to be using the dictionary now, than-kew. */
5410 require_memory_page_dictionary (pid
);
5412 /* Try to find an existing dictionary entry for this page. Hash
5413 on the page's starting address.
5415 bucket
= get_dictionary_bucket_of_page (page_start
);
5416 page
= &memory_page_dictionary
.buckets
[bucket
];
5417 while (page
!= NULL
)
5419 if (page
->page_start
== page_start
)
5421 previous_page
= page
;
5425 /* Did we find a dictionary entry for this page? If not, then
5426 add it to the dictionary now.
5430 /* Create a new entry. */
5431 page
= (memory_page_t
*) xmalloc (sizeof (memory_page_t
));
5432 page
->page_start
= page_start
;
5433 page
->reference_count
= 0;
5435 page
->previous
= NULL
;
5437 /* We'll write-protect the page now, if that's allowed. */
5438 page
->original_permissions
= write_protect_page (pid
, page_start
);
5440 /* Add the new entry to the dictionary. */
5441 page
->previous
= previous_page
;
5442 previous_page
->next
= page
;
5444 memory_page_dictionary
.page_count
++;
5452 remove_dictionary_entry_of_page (pid
, page
)
5454 memory_page_t
*page
;
5456 /* Restore the page's original permissions. */
5457 unwrite_protect_page (pid
, page
->page_start
, page
->original_permissions
);
5459 /* Kick the page out of the dictionary. */
5460 if (page
->previous
!= NULL
)
5461 page
->previous
->next
= page
->next
;
5462 if (page
->next
!= NULL
)
5463 page
->next
->previous
= page
->previous
;
5465 /* Just in case someone retains a handle to this after it's freed. */
5466 page
->page_start
= (CORE_ADDR
) 0;
5468 memory_page_dictionary
.page_count
--;
5475 hppa_enable_syscall_events (pid
)
5479 ttevent_t ttrace_events
;
5481 /* Get the set of events that are currently enabled. */
5482 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
5484 (TTRACE_ARG_TYPE
) & ttrace_events
,
5485 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5488 perror_with_name ("ttrace");
5490 /* Add syscall events to that set. */
5491 ttrace_events
.tte_events
|= TTEVT_SYSCALL_ENTRY
;
5492 ttrace_events
.tte_events
|= TTEVT_SYSCALL_RETURN
;
5494 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
5496 (TTRACE_ARG_TYPE
) & ttrace_events
,
5497 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5500 perror_with_name ("ttrace");
5505 hppa_disable_syscall_events (pid
)
5509 ttevent_t ttrace_events
;
5511 /* Get the set of events that are currently enabled. */
5512 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
5514 (TTRACE_ARG_TYPE
) & ttrace_events
,
5515 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5518 perror_with_name ("ttrace");
5520 /* Remove syscall events from that set. */
5521 ttrace_events
.tte_events
&= ~TTEVT_SYSCALL_ENTRY
;
5522 ttrace_events
.tte_events
&= ~TTEVT_SYSCALL_RETURN
;
5524 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
5526 (TTRACE_ARG_TYPE
) & ttrace_events
,
5527 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5530 perror_with_name ("ttrace");
5534 /* The address range beginning with START and ending with START+LEN-1
5535 (inclusive) is to be watched via page-protection by a new watchpoint.
5536 Set protection for all pages that overlap that range.
5538 Note that our caller sets TYPE to:
5539 0 for a bp_hardware_watchpoint,
5540 1 for a bp_read_watchpoint,
5541 2 for a bp_access_watchpoint
5543 (Yes, this is intentionally (though lord only knows why) different
5544 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5547 hppa_insert_hw_watchpoint (pid
, start
, len
, type
)
5553 CORE_ADDR page_start
;
5554 int dictionary_was_empty
;
5557 LONGEST range_size_in_pages
;
5560 error ("read or access hardware watchpoints not supported on HP-UX");
5562 /* Examine all pages in the address range. */
5563 require_memory_page_dictionary ();
5565 dictionary_was_empty
= (memory_page_dictionary
.page_count
== (LONGEST
) 0);
5567 page_size
= memory_page_dictionary
.page_size
;
5568 page_start
= (start
/ page_size
) * page_size
;
5569 range_size_in_pages
= ((LONGEST
) len
+ (LONGEST
) page_size
- 1) / (LONGEST
) page_size
;
5571 for (page_id
= 0; page_id
< range_size_in_pages
; page_id
++, page_start
+= page_size
)
5573 memory_page_t
*page
;
5575 /* This gets the page entered into the dictionary if it was
5576 not already entered.
5578 page
= get_dictionary_entry_of_page (pid
, page_start
);
5579 page
->reference_count
++;
5582 /* Our implementation depends on seeing calls to kernel code, for the
5583 following reason. Here we ask to be notified of syscalls.
5585 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5588 But when kernel code accesses the page, it doesn't give a SIGBUS.
5589 Rather, the system call that touched the page fails, with errno=EFAULT.
5592 We could accomodate this "feature" by asking to be notified of syscall
5593 entries & exits; upon getting an entry event, disabling page-protections;
5594 upon getting an exit event, reenabling page-protections and then checking
5595 if any watchpoints triggered.
5597 However, this turns out to be a real performance loser. syscalls are
5598 usually a frequent occurrence. Having to unprotect-reprotect all watched
5599 pages, and also to then read all watched memory locations and compare for
5600 triggers, can be quite expensive.
5602 Instead, we'll only ask to be notified of syscall exits. When we get
5603 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5604 we can just continue the inferior.
5606 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5607 hackish stuff to determine whether the failure really was due to a
5608 page-protect trap on a watched location.
5610 if (dictionary_was_empty
)
5611 hppa_enable_syscall_events (pid
);
5617 /* The address range beginning with START and ending with START+LEN-1
5618 (inclusive) was being watched via page-protection by a watchpoint
5619 which has been removed. Remove protection for all pages that
5620 overlap that range, which are not also being watched by other
5624 hppa_remove_hw_watchpoint (pid
, start
, len
, type
)
5630 CORE_ADDR page_start
;
5631 int dictionary_is_empty
;
5634 LONGEST range_size_in_pages
;
5637 error ("read or access hardware watchpoints not supported on HP-UX");
5639 /* Examine all pages in the address range. */
5640 require_memory_page_dictionary ();
5642 page_size
= memory_page_dictionary
.page_size
;
5643 page_start
= (start
/ page_size
) * page_size
;
5644 range_size_in_pages
= ((LONGEST
) len
+ (LONGEST
) page_size
- 1) / (LONGEST
) page_size
;
5646 for (page_id
= 0; page_id
< range_size_in_pages
; page_id
++, page_start
+= page_size
)
5648 memory_page_t
*page
;
5650 page
= get_dictionary_entry_of_page (pid
, page_start
);
5651 page
->reference_count
--;
5653 /* Was this the last reference of this page? If so, then we
5654 must scrub the entry from the dictionary, and also restore
5655 the page's original permissions.
5657 if (page
->reference_count
== 0)
5658 remove_dictionary_entry_of_page (pid
, page
);
5661 dictionary_is_empty
= (memory_page_dictionary
.page_count
== (LONGEST
) 0);
5663 /* If write protections are currently disallowed, then that implies that
5664 wait_for_inferior believes that the inferior is within a system call.
5665 Since we want to see both syscall entry and return, it's clearly not
5666 good to disable syscall events in this state!
5668 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5669 "inferior is between syscall events now". Oh well.
5671 if (dictionary_is_empty
&& memory_page_dictionary
.page_protections_allowed
)
5672 hppa_disable_syscall_events (pid
);
5678 /* Could we implement a watchpoint of this type via our available
5681 This query does not consider whether a particular address range
5682 could be so watched, but just whether support is generally available
5683 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5684 query that answers whether a particular range should be watched via
5688 hppa_can_use_hw_watchpoint (type
, cnt
, ot
)
5693 return (type
== bp_hardware_watchpoint
);
5697 /* Assuming we could set a hardware watchpoint on this address, do
5698 we think it would be profitable ("a good idea") to do so? If not,
5699 we can always set a regular (aka single-step & test) watchpoint
5703 hppa_range_profitable_for_hw_watchpoint (pid
, start
, len
)
5708 int range_is_stack_based
;
5709 int range_is_accessible
;
5710 CORE_ADDR page_start
;
5713 LONGEST range_size_in_pages
;
5715 /* ??rehrauer: For now, say that all addresses are potentially
5716 profitable. Possibly later we'll want to test the address
5719 range_is_stack_based
= 0;
5721 /* If any page in the range is inaccessible, then we cannot
5722 really use hardware watchpointing, even though our client
5723 thinks we can. In that case, it's actually an error to
5724 attempt to use hw watchpoints, so we'll tell our client
5725 that the range is "unprofitable", and hope that they listen...
5727 range_is_accessible
= 1; /* Until proven otherwise. */
5729 /* Examine all pages in the address range. */
5731 page_size
= sysconf (_SC_PAGE_SIZE
);
5733 /* If we can't determine page size, we're hosed. Tell our
5734 client it's unprofitable to use hw watchpoints for this
5737 if (errno
|| (page_size
<= 0))
5743 page_start
= (start
/ page_size
) * page_size
;
5744 range_size_in_pages
= len
/ (LONGEST
) page_size
;
5746 for (page
= 0; page
< range_size_in_pages
; page
++, page_start
+= page_size
)
5749 int page_permissions
;
5751 /* Is this page accessible? */
5753 tt_status
= call_ttrace (TT_PROC_GET_MPROTECT
,
5755 (TTRACE_ARG_TYPE
) page_start
,
5757 (TTRACE_ARG_TYPE
) & page_permissions
);
5758 if (errno
|| (tt_status
< 0))
5761 range_is_accessible
= 0;
5765 /* Yes, go for another... */
5768 return (!range_is_stack_based
&& range_is_accessible
);
5773 hppa_pid_or_tid_to_str (id
)
5776 static char buf
[100]; /* Static because address returned. */
5778 /* Does this appear to be a process? If so, print it that way. */
5779 if (is_process_id (id
))
5780 return hppa_pid_to_str (id
);
5782 /* Else, print both the GDB thread number and the system thread id. */
5783 sprintf (buf
, "thread %d (", pid_to_thread_id (id
));
5784 strcat (buf
, hppa_tid_to_str (id
));
5785 strcat (buf
, ")\0");
5791 /* If the current pid is not the pid this module reported
5792 * from "ptrace_wait" with the most recent event, then the
5793 * user has switched threads.
5795 * If the last reported event was a breakpoint, then return
5796 * the old thread id, else return 0.
5799 hppa_switched_threads (gdb_pid
)
5802 if (gdb_pid
== old_gdb_pid
)
5805 * Core gdb is working with the same pid that it
5806 * was before we reported the last event. This
5807 * is ok: e.g. we reported hitting a thread-specific
5808 * breakpoint, but we were reporting the wrong
5809 * thread, so the core just ignored the event.
5811 * No thread switch has happened.
5815 else if (gdb_pid
== reported_pid
)
5818 * Core gdb is working with the pid we reported, so
5819 * any continue or step will be able to figure out
5820 * that it needs to step over any hit breakpoints
5821 * without our (i.e. PREPARE_TO_PROCEED's) help.
5825 else if (!reported_bpt
)
5828 * The core switched, but we didn't just report a
5829 * breakpoint, so there's no just-hit breakpoint
5830 * instruction at "reported_pid"'s PC, and thus there
5831 * is no need to step over it.
5837 /* There's been a real switch, and we reported
5838 * a hit breakpoint. Let "hppa_prepare_to_proceed"
5839 * know, so it can see whether the breakpoint is
5842 return reported_pid
;
5845 /* Keep compiler happy with an obvious return at the end.
5851 hppa_ensure_vforking_parent_remains_stopped (pid
)
5854 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5861 hppa_resume_execd_vforking_child_to_get_parent_vfork ()
5863 return 0; /* No, the parent vfork is available now. */
5869 _initialize_infttrace ()
5871 /* Initialize the ttrace-based hardware watchpoint implementation. */
5872 memory_page_dictionary
.page_count
= (LONGEST
) - 1;
5873 memory_page_dictionary
.page_protections_allowed
= 1;
5876 memory_page_dictionary
.page_size
= sysconf (_SC_PAGE_SIZE
);
5878 if (errno
|| (memory_page_dictionary
.page_size
<= 0))
5879 perror_with_name ("sysconf");