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, Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
29 /* Some hackery to work around a use of the #define name NO_FLAGS
30 * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
33 #define INFTTRACE_TEMP_HACK NO_FLAGS
38 #include <sys/types.h>
41 #include <sys/param.h>
44 #include <sys/ioctl.h>
46 #include <sys/ttrace.h>
53 #ifdef PTRACE_IN_WRONG_PLACE
56 #include <sys/ptrace.h>
58 #endif /* NO_PTRACE_H */
60 /* Second half of the hackery above. Non-ANSI C, so
61 * we can't use "#error", alas.
64 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
65 /* #error "Hackery to remove warning didn't work right" */
67 /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
70 /* #error "Didn't get expected re-definition of NO_FLAGS" */
71 #define NO_FLAGS INFTTRACE_TEMP_HACK
74 #if !defined (PT_SETTRC)
75 #define PT_SETTRC 0 /* Make process traceable by parent */
77 #if !defined (PT_READ_I)
78 #define PT_READ_I 1 /* Read word from text space */
80 #if !defined (PT_READ_D)
81 #define PT_READ_D 2 /* Read word from data space */
83 #if !defined (PT_READ_U)
84 #define PT_READ_U 3 /* Read word from kernel user struct */
86 #if !defined (PT_WRITE_I)
87 #define PT_WRITE_I 4 /* Write word to text space */
89 #if !defined (PT_WRITE_D)
90 #define PT_WRITE_D 5 /* Write word to data space */
92 #if !defined (PT_WRITE_U)
93 #define PT_WRITE_U 6 /* Write word to kernel user struct */
95 #if !defined (PT_CONTINUE)
96 #define PT_CONTINUE 7 /* Continue after signal */
98 #if !defined (PT_STEP)
99 #define PT_STEP 9 /* Set flag for single stepping */
101 #if !defined (PT_KILL)
102 #define PT_KILL 8 /* Send child a SIGKILL signal */
106 #define PT_ATTACH PTRACE_ATTACH
109 #define PT_DETACH PTRACE_DETACH
114 #include <sys/file.h>
117 /* This semaphore is used to coordinate the child and parent processes
118 after a fork(), and before an exec() by the child. See parent_attach_all
122 int parent_channel
[2]; /* Parent "talks" to [1], child "listens" to [0] */
123 int child_channel
[2]; /* Child "talks" to [1], parent "listens" to [0] */
124 } startup_semaphore_t
;
127 #define SEM_LISTEN (0)
129 static startup_semaphore_t startup_semaphore
;
131 /* See can_touch_threads_of_process for details. */
132 static int vforking_child_pid
= 0;
133 static int vfork_in_flight
= 0;
135 /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed).
137 static pid_t old_gdb_pid
= 0;
138 static pid_t reported_pid
= 0;
139 static int reported_bpt
= 0;
141 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
143 #define TT_OK( _status, _errno ) \
144 (((_status) == 1) && ((_errno) == 0))
146 #define TTRACE_ARG_TYPE uint64_t
148 /* When supplied as the "addr" operand, ttrace interprets this
149 to mean, "from the current address".
151 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
153 /* When supplied as the "addr", "data" or "addr2" operand for most
154 requests, ttrace interprets this to mean, "pay no heed to this
157 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
159 /* This is capable of holding the value of a 32-bit register. The
160 value is always left-aligned in the buffer; i.e., [0] contains
161 the most-significant byte of the register's value, and [sizeof(reg)]
162 contains the least-significant value.
164 ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
165 that registers are 32-bits on HP-UX. The latter assumption changes
168 typedef int register_value_t
;
170 /********************************************************************
176 The rest of GDB sees threads as being things with different
177 "pid" (process id) values. See "thread.c" for details. The
178 separate threads will be seen and reacted to if infttrace passes
179 back different pid values (for _events_). See wait_for_inferior
182 So infttrace is going to use thread ids externally, pretending
183 they are process ids, and keep track internally so that it can
184 use the real process id (and thread id) when calling ttrace.
186 The data structure that supports this is a linked list of the
187 current threads. Since at some date infttrace will have to
188 deal with multiple processes, each list element records its
189 corresponding pid, rather than having a single global.
191 Note that the list is only approximately current; that's ok, as
192 it's up to date when we need it (we hope!). Also, it can contain
193 dead threads, as there's no harm if it does.
195 The approach taken here is to bury the translation from external
196 to internal inside "call_ttrace" and a few other places.
198 There are some wrinkles:
200 o When GDB forks itself to create the debug target process,
201 there's only a pid of 0 around in the child, so the
202 TT_PROC_SETTRC operation uses a more direct call to ttrace;
203 Similiarly, the initial setting of the event mask happens
204 early as well, and so is also special-cased, and an attach
207 o We define an unthreaded application as having a "pseudo"
210 o To keep from confusing the rest of GDB, we don't switch
211 the PID for the pseudo thread to a TID. A table will help:
213 Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ...
215 Our thread list stores: pid pid pid pid ...
218 Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ...
220 Both pid and tid0 will map to tid0, as there are infttrace.c-internal
221 calls to ttrace using tid0.
225 Since we're implementing the "stop the world" model, sub-model
226 "other threads run during step", we have some stuff to do:
228 o User steps require continuing all threads other than the
229 one the user is stepping;
231 o Internal debugger steps (such as over a breakpoint or watchpoint,
232 but not out of a library load thunk) require stepping only
233 the selected thread; this means that we have to report the
234 step finish on that thread, which can lead to complications;
236 o When a thread is created, it is created running, rather
237 than stopped--so we have to stop it.
239 The OS doesn't guarantee the stopped thread list will be stable,
240 no does it guarantee where on the stopped thread list a thread
241 that is single-stepped will wind up: it's possible that it will
242 be off the list for a while, it's possible the step will complete
243 and it will be re-posted to the end...
245 This means we have to scan the stopped thread list, build up
246 a work-list, and then run down the work list; we can't do the
247 step/continue during the scan.
251 Then there's the issue of waiting for an event. We do this by
252 noticing how many events are reported at the end of each wait.
253 From then on, we "fake" all resumes and steps, returning instantly,
254 and don't do another wait. Once all pending events are reported,
255 we can really resume again.
257 To keep this hidden, all the routines which know about tids and
258 pids or real events and simulated ones are static (file-local).
260 This code can make lots of calls to ttrace, in particular it
261 can spin down the list of thread states more than once. If this
262 becomes a performance hit, the spin could be done once and the
263 various "tsp" blocks saved, keeping all later spins in this
266 The O/S doesn't promise to keep the list straight, and so we must
267 re-scan a lot. By observation, it looks like a single-step/wait
268 puts the stepped thread at the end of the list but doesn't change
271 ****************************************************************
274 /* Uncomment these to turn on various debugging output */
275 /* #define THREAD_DEBUG */
276 /* #define WAIT_BUFFER_DEBUG */
277 /* #define PARANOIA */
280 #define INFTTRACE_ALL_THREADS (-1)
281 #define INFTTRACE_STEP (1)
282 #define INFTTRACE_CONTINUE (0)
284 /* FIX: this is used in inftarg.c/child_wait, in a hack.
286 extern int not_same_real_pid
;
288 /* This is used to count buffered events.
290 static unsigned int more_events_left
= 0;
294 typedef enum process_state_enum
{
297 FAKE_CONTINUE
, /* For later use */
303 static process_state_t process_state
= STOPPED
;
305 /* User-specified stepping modality.
307 typedef enum stepping_mode_enum
{
308 DO_DEFAULT
, /* ...which is a continue! */
313 /* Action to take on an attach, depends on
314 * what kind (user command, fork, vfork).
316 * At the moment, this is either:
318 * o continue with a SIGTRAP signal, or
322 typedef enum attach_continue_enum
{
327 /* This flag is true if we are doing a step-over-bpt
328 * with buffered events. We will have to be sure to
329 * report the right thread, as otherwise the spaghetti
330 * code in "infrun.c/wait_for_inferior" will get
333 static int doing_fake_step
= 0;
334 static lwpid_t fake_step_tid
= 0;
337 /****************************************************
338 * Thread information structure routines and types. *
339 ****************************************************
342 struct thread_info_struct
344 int am_pseudo
; /* This is a pseudo-thread for the process. */
345 int pid
; /* Process ID */
346 lwpid_t tid
; /* Thread ID */
347 int handled
; /* 1 if a buffered event was handled. */
348 int seen
; /* 1 if this thread was seen on a traverse. */
349 int terminated
; /* 1 if thread has terminated. */
350 int have_signal
; /* 1 if signal to be sent */
351 enum target_signal signal_value
; /* Signal to send */
352 int have_start
; /* 1 if alternate starting address */
353 stepping_mode_t stepping_mode
; /* Whether to step or continue */
354 CORE_ADDR start
; /* Where to start */
355 int have_state
; /* 1 if the event state has been set */
356 ttstate_t last_stop_state
;/* The most recently-waited event for this thread. */
357 struct thread_info_struct
358 *next
; /* All threads are linked via this field. */
359 struct thread_info_struct
360 *next_pseudo
; /* All pseudo-threads are linked via this field. */
364 struct thread_info_header_struct
368 thread_info
*head_pseudo
;
370 } thread_info_header
;
372 static thread_info_header thread_head
= { 0, NULL
, NULL
};
373 static thread_info_header deleted_threads
= { 0, NULL
, NULL
};
375 static saved_real_pid
= 0;
378 /*************************************************
379 * Debugging support functions *
380 *************************************************
386 unsigned long pc_val
;
390 offset
= register_addr( PC_REGNUM
, U_REGS_OFFSET
);
391 res
= read_from_register_save_state(
393 (TTRACE_ARG_TYPE
) offset
,
397 return (CORE_ADDR
) pc_val
;
400 return (CORE_ADDR
) 0;
405 get_printable_name_of_stepping_mode( mode
)
406 stepping_mode_t mode
;
409 case DO_DEFAULT
: return "DO_DEFAULT";
410 case DO_STEP
: return "DO_STEP";
411 case DO_CONTINUE
: return "DO_CONTINUE";
412 default: return "?unknown mode?";
416 /* This function returns a pointer to a string describing the
417 * ttrace event being reported.
420 get_printable_name_of_ttrace_event (event
)
423 /* This enumeration is "gappy", so don't use a table. */
429 return "TTEVT_SIGNAL";
437 return "TTEVT_VFORK";
438 case TTEVT_SYSCALL_RETURN
:
439 return "TTEVT_SYSCALL_RETURN";
440 case TTEVT_LWP_CREATE
:
441 return "TTEVT_LWP_CREATE";
442 case TTEVT_LWP_TERMINATE
:
443 return "TTEVT_LWP_TERMINATE";
445 return "TTEVT_LWP_EXIT";
446 case TTEVT_LWP_ABORT_SYSCALL
:
447 return "TTEVT_LWP_ABORT_SYSCALL";
448 case TTEVT_SYSCALL_ENTRY
:
449 return "TTEVT_SYSCALL_ENTRY";
450 case TTEVT_SYSCALL_RESTART
:
451 return "TTEVT_SYSCALL_RESTART";
453 return "?new event?";
458 /* This function translates the ttrace request enumeration into
459 * a character string that is its printable (aka "human readable")
463 get_printable_name_of_ttrace_request (request
)
466 if (!IS_TTRACE_REQ (request
))
469 /* This enumeration is "gappy", so don't use a table. */
471 case TT_PROC_SETTRC
:
472 return "TT_PROC_SETTRC";
473 case TT_PROC_ATTACH
:
474 return "TT_PROC_ATTACH";
475 case TT_PROC_DETACH
:
476 return "TT_PROC_DETACH";
477 case TT_PROC_RDTEXT
:
478 return "TT_PROC_RDTEXT";
479 case TT_PROC_WRTEXT
:
480 return "TT_PROC_WRTEXT";
481 case TT_PROC_RDDATA
:
482 return "TT_PROC_RDDATA";
483 case TT_PROC_WRDATA
:
484 return "TT_PROC_WRDATA";
486 return "TT_PROC_STOP";
487 case TT_PROC_CONTINUE
:
488 return "TT_PROC_CONTINUE";
489 case TT_PROC_GET_PATHNAME
:
490 return "TT_PROC_GET_PATHNAME";
491 case TT_PROC_GET_EVENT_MASK
:
492 return "TT_PROC_GET_EVENT_MASK";
493 case TT_PROC_SET_EVENT_MASK
:
494 return "TT_PROC_SET_EVENT_MASK";
495 case TT_PROC_GET_FIRST_LWP_STATE
:
496 return "TT_PROC_GET_FIRST_LWP_STATE";
497 case TT_PROC_GET_NEXT_LWP_STATE
:
498 return "TT_PROC_GET_NEXT_LWP_STATE";
500 return "TT_PROC_EXIT";
501 case TT_PROC_GET_MPROTECT
:
502 return "TT_PROC_GET_MPROTECT";
503 case TT_PROC_SET_MPROTECT
:
504 return "TT_PROC_SET_MPROTECT";
505 case TT_PROC_SET_SCBM
:
506 return "TT_PROC_SET_SCBM";
508 return "TT_LWP_STOP";
509 case TT_LWP_CONTINUE
:
510 return "TT_LWP_CONTINUE";
512 return "TT_LWP_SINGLE";
514 return "TT_LWP_RUREGS";
516 return "TT_LWP_WUREGS";
517 case TT_LWP_GET_EVENT_MASK
:
518 return "TT_LWP_GET_EVENT_MASK";
519 case TT_LWP_SET_EVENT_MASK
:
520 return "TT_LWP_SET_EVENT_MASK";
521 case TT_LWP_GET_STATE
:
522 return "TT_LWP_GET_STATE";
529 /* This function translates the process state enumeration into
530 * a character string that is its printable (aka "human readable")
534 get_printable_name_of_process_state (process_state
)
535 process_state_t process_state
;
537 switch (process_state
) {
541 return "FAKE_STEPPING";
549 return "?some unknown state?";
553 /* Set a ttrace thread state to a safe, initial state.
556 clear_ttstate_t (tts
)
561 tts
->tts_user_tid
= 0;
562 tts
->tts_event
= TTEVT_NONE
;
565 /* Copy ttrace thread state TTS_FROM into TTS_TO.
568 copy_ttstate_t (tts_to
, tts_from
)
570 ttstate_t
* tts_from
;
572 memcpy ((char *) tts_to
, (char *) tts_from
, sizeof (*tts_to
));
575 /* Are there any live threads we know about?
580 return( thread_head
.count
> 0 );
583 /* Create, fill in and link in a thread descriptor.
586 create_thread_info (pid
, tid
)
592 int thread_count_of_pid
;
594 new_p
= malloc( sizeof( thread_info
));
597 new_p
->have_signal
= 0;
598 new_p
->have_start
= 0;
599 new_p
->have_state
= 0;
600 clear_ttstate_t( &new_p
->last_stop_state
);
601 new_p
->am_pseudo
= 0;
604 new_p
->terminated
= 0;
606 new_p
->next_pseudo
= NULL
;
607 new_p
->stepping_mode
= DO_DEFAULT
;
609 if( 0 == thread_head
.count
) {
612 printf( "First thread, pid %d tid %d!\n", pid
, tid
);
614 saved_real_pid
= inferior_pid
;
619 printf( "Subsequent thread, pid %d tid %d\n", pid
, tid
);
623 /* Another day, another thread...
627 /* The new thread always goes at the head of the list.
629 new_p
->next
= thread_head
.head
;
630 thread_head
.head
= new_p
;
632 /* Is this the "pseudo" thread of a process? It is if there's
633 * no other thread for this process on the list. (Note that this
634 * accomodates multiple processes, such as we see even for simple
635 * cases like forking "non-threaded" programs.)
637 p
= thread_head
.head
;
638 thread_count_of_pid
= 0;
641 if (p
->pid
== new_p
->pid
)
642 thread_count_of_pid
++;
646 /* Did we see any other threads for this pid? (Recall that we just
647 * added this thread to the list...)
649 if (thread_count_of_pid
== 1)
651 new_p
->am_pseudo
= 1;
652 new_p
->next_pseudo
= thread_head
.head_pseudo
;
653 thread_head
.head_pseudo
= new_p
;
659 /* Get rid of our thread info.
669 printf( "Clearing all thread info\n" );
672 p
= thread_head
.head
;
679 thread_head
.head
= NULL
;
680 thread_head
.head_pseudo
= NULL
;
681 thread_head
.count
= 0;
683 p
= deleted_threads
.head
;
690 deleted_threads
.head
= NULL
;
691 deleted_threads
.head_pseudo
= NULL
;
692 deleted_threads
.count
= 0;
694 /* No threads, so can't have pending events.
696 more_events_left
= 0;
699 /* Given a tid, find the thread block for it.
702 find_thread_info (tid
)
707 for( p
= thread_head
.head
; p
; p
= p
->next
) {
708 if( p
->tid
== tid
) {
713 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
714 if( p
->tid
== tid
) {
722 /* For any but the pseudo thread, this maps to the
723 * thread ID. For the pseudo thread, if you pass either
724 * the thread id or the PID, you get the pseudo thread ID.
726 * We have to be prepared for core gdb to ask about
727 * deleted threads. We do the map, but we don't like it.
730 map_from_gdb_tid( gdb_tid
)
735 /* First assume gdb_tid really is a tid, and try to find a
736 * matching entry on the threads list.
738 for( p
= thread_head
.head
; p
; p
= p
->next
) {
739 if( p
->tid
== gdb_tid
)
743 /* It doesn't appear to be a tid; perhaps it's really a pid?
744 * Try to find a "pseudo" thread entry on the threads list.
746 for (p
= thread_head
.head_pseudo
; p
!= NULL
; p
= p
->next_pseudo
)
748 if (p
->pid
== gdb_tid
)
752 /* Perhaps it's the tid of a deleted thread we may still
753 * have some knowledge of?
755 for( p
= deleted_threads
.head
; p
; p
= p
-> next
) {
756 if( p
->tid
== gdb_tid
)
760 /* Or perhaps it's the pid of a deleted process we may still
763 for (p
= deleted_threads
.head_pseudo
; p
!= NULL
; p
= p
->next_pseudo
)
765 if (p
->pid
== gdb_tid
)
769 return 0; /* Error? */
772 /* Map the other way: from a real tid to the
773 * "pid" known by core gdb. This tid may be
774 * for a thread that just got deleted, so we
775 * also need to consider deleted threads.
778 map_to_gdb_tid( real_tid
)
783 for( p
= thread_head
.head
; p
; p
= p
->next
) {
784 if( p
->tid
== real_tid
) {
792 for( p
= deleted_threads
.head
; p
; p
= p
-> next
) {
793 if( p
->tid
== real_tid
)
795 return p
->pid
; /* Error? */
800 return 0; /* Error? Never heard of this thread! */
803 /* Do any threads have saved signals?
806 saved_signals_exist ()
810 for( p
= thread_head
.head
; p
; p
= p
->next
) {
811 if( p
->have_signal
) {
819 /* Is this the tid for the zero-th thread?
822 is_pseudo_thread (tid
)
825 thread_info
*p
= find_thread_info( tid
);
826 if( NULL
== p
|| p
->terminated
)
832 /* Is this thread terminated?
838 thread_info
*p
= find_thread_info( tid
);
841 return p
->terminated
;
846 /* Is this pid a real PID or a TID?
857 /* What does PID really represent?
859 tid
= map_from_gdb_tid (pid
);
861 return 0; /* Actually, is probably an error... */
863 tinfo
= find_thread_info (tid
);
865 /* Does it appear to be a true thread?
867 if (! tinfo
->am_pseudo
)
870 /* Else, it looks like it may be a process. See if there's any other
871 * threads with the same process ID, though. If there are, then TID
872 * just happens to be the first thread of several for this process.
874 this_pid
= tinfo
->pid
;
876 for (tinfo
= thread_head
.head
; tinfo
; tinfo
= tinfo
->next
)
878 if (tinfo
->pid
== this_pid
)
882 return (this_pid_count
== 1);
886 /* Add a thread to our info. Prevent duplicate entries.
889 add_tthread (pid
, tid
)
895 p
= find_thread_info( tid
);
897 p
= create_thread_info( pid
, tid
);
902 /* Notice that a thread was deleted.
911 if( thread_head
.count
<= 0 ) {
912 error( "Internal error in thread database." );
917 for( p
= thread_head
.head
; p
; p
= p
->next
) {
918 if( p
->tid
== tid
) {
922 printf( "Delete here: %d \n", tid
);
927 * Deleting a main thread is ok if we're doing
928 * a parent-follow on a child; this is odd but
929 * not wrong. It apparently _doesn't_ happen
930 * on the child-follow, as we don't just delete
931 * the pseudo while keeping the rest of the
932 * threads around--instead, we clear out the whole
933 * thread list at once.
936 thread_info
*q_chase
;
939 for( q
= thread_head
.head_pseudo
; q
; q
= q
-> next
) {
941 /* Remove from pseudo list.
943 if( q_chase
== NULL
)
944 thread_head
.head_pseudo
= p
->next_pseudo
;
946 q_chase
-> next
= p
->next_pseudo
;
953 /* Remove from live list.
958 thread_head
.head
= p
->next
;
960 chase
->next
= p
->next
;
962 /* Add to deleted thread list.
964 p
->next
= deleted_threads
.head
;
965 deleted_threads
.head
= p
;
966 deleted_threads
.count
++;
968 p
->next_pseudo
= deleted_threads
.head_pseudo
;
969 deleted_threads
.head_pseudo
= p
;
981 /* Get the pid for this tid. (Has to be a real TID!).
989 for( p
= thread_head
.head
; p
; p
= p
->next
) {
990 if( p
->tid
== tid
) {
995 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
996 if( p
->tid
== tid
) {
1004 /* Note that this thread's current event has been handled.
1007 set_handled( pid
, tid
)
1013 p
= find_thread_info( tid
);
1015 p
= add_tthread( pid
, tid
);
1020 /* Was this thread's current event handled?
1028 p
= find_thread_info( tid
);
1032 return 0; /* New threads have not been handled */
1035 /* Set this thread to unhandled.
1038 clear_handled( tid
)
1043 #ifdef WAIT_BUFFER_DEBUG
1045 printf( "clear_handled %d\n", (int) tid
);
1048 p
= find_thread_info (tid
);
1050 error ("Internal error: No thread state to clear?");
1055 /* Set all threads to unhandled.
1058 clear_all_handled ()
1062 #ifdef WAIT_BUFFER_DEBUG
1064 printf( "clear_all_handled\n" );
1067 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1071 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
1076 /* Set this thread to default stepping mode.
1079 clear_stepping_mode( tid
)
1084 #ifdef WAIT_BUFFER_DEBUG
1086 printf( "clear_stepping_mode %d\n", (int) tid
);
1089 p
= find_thread_info (tid
);
1091 error ("Internal error: No thread state to clear?");
1093 p
->stepping_mode
= DO_DEFAULT
;
1096 /* Set all threads to do default continue on resume.
1099 clear_all_stepping_mode ()
1103 #ifdef WAIT_BUFFER_DEBUG
1105 printf( "clear_all_stepping_mode\n" );
1108 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1109 p
->stepping_mode
= DO_DEFAULT
;
1112 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
1113 p
->stepping_mode
= DO_DEFAULT
;
1117 /* Set all threads to unseen on this pass.
1124 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1129 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1130 /* debugging routine.
1136 printf( " Thread pid %d, tid %d", p
->pid
, p
->tid
);
1138 printf( ", event is %s",
1139 get_printable_name_of_ttrace_event( p
->last_stop_state
.tts_event
));
1142 printf( ", pseudo thread" );
1144 if( p
->have_signal
)
1145 printf( ", have signal 0x%x", p
->signal_value
);
1148 printf( ", have start at 0x%x", p
->start
);
1150 printf( ", step is %s", get_printable_name_of_stepping_mode( p
->stepping_mode
));
1153 printf( ", handled" );
1155 printf( ", not handled" );
1160 printf( ", not seen" );
1170 if( thread_head
.count
== 0 )
1171 printf( "Thread list is empty\n" );
1173 printf( "Thread list has " );
1174 if( thread_head
.count
== 1 )
1175 printf( "1 entry:\n" );
1177 printf( "%d entries:\n", thread_head
.count
);
1178 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1183 if( deleted_threads
.count
== 0 )
1184 printf( "Deleted thread list is empty\n" );
1186 printf( "Deleted thread list has " );
1187 if( deleted_threads
.count
== 1 )
1188 printf( "1 entry:\n" );
1190 printf( "%d entries:\n", deleted_threads
.count
);
1192 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
1199 /* Update the thread list based on the "seen" bits.
1202 update_thread_list ()
1208 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1209 /* Is this an "unseen" thread which really happens to be a process?
1210 If so, is it inferior_pid and is a vfork in flight? If yes to
1211 all, then DON'T REMOVE IT! We're in the midst of moving a vfork
1212 operation, which is a multiple step thing, to the point where we
1213 can touch the parent again. We've most likely stopped to examine
1214 the child at a late stage in the vfork, and if we're not following
1215 the child, we'd best not treat the parent as a dead "thread"...
1217 if( (!p
->seen
) && p
->am_pseudo
&& vfork_in_flight
1218 && (p
->pid
!= vforking_child_pid
))
1227 printf( "Delete unseen thread: %d \n", p
->tid
);
1229 del_tthread( p
->tid
);
1236 /************************************************
1237 * O/S call wrappers *
1238 ************************************************
1241 /* This function simply calls ttrace with the given arguments.
1242 * It exists so that all calls to ttrace are isolated. All
1243 * parameters should be as specified by "man 2 ttrace".
1245 * No other "raw" calls to ttrace should exist in this module.
1248 call_real_ttrace( request
, pid
, tid
, addr
, data
, addr2
)
1252 TTRACE_ARG_TYPE addr
, data
, addr2
;
1257 tt_status
= ttrace( request
, pid
, tid
, addr
, data
, addr2
);
1261 /* Don't bother for a known benign error: if you ask for the
1262 * first thread state, but there is only one thread and it's
1263 * not stopped, ttrace complains.
1265 * We have this inside the #ifdef because our caller will do
1266 * this check for real.
1268 if( request
!= TT_PROC_GET_FIRST_LWP_STATE
1269 || errno
!= EPROTO
) {
1271 printf( "TT fail for %s, with pid %d, tid %d, status %d \n",
1272 get_printable_name_of_ttrace_request (request
),
1273 pid
, tid
, tt_status
);
1279 /* ??rehrauer: It would probably be most robust to catch and report
1280 * failed requests here. However, some clients of this interface
1281 * seem to expect to catch & deal with them, so we'd best not.
1284 strcpy (reason_for_failure
, "ttrace (");
1285 strcat (reason_for_failure
, get_printable_name_of_ttrace_request (request
));
1286 strcat (reason_for_failure
, ")");
1287 printf( "ttrace error, errno = %d\n", errno
);
1288 perror_with_name (reason_for_failure
);
1296 /* This function simply calls ttrace_wait with the given arguments.
1297 * It exists so that all calls to ttrace_wait are isolated.
1299 * No "raw" calls to ttrace_wait should exist elsewhere.
1302 call_real_ttrace_wait( pid
, tid
, option
, tsp
, tsp_size
)
1310 thread_info
* tinfo
= NULL
;
1313 ttw_status
= ttrace_wait (pid
, tid
, option
, tsp
, tsp_size
);
1318 printf( "TW fail with pid %d, tid %d \n", pid
, tid
);
1321 perror_with_name ("ttrace wait");
1328 /* A process may have one or more kernel threads, of which all or
1329 none may be stopped. This function returns the ID of the first
1330 kernel thread in a stopped state, or 0 if none are stopped.
1332 This function can be used with get_process_next_stopped_thread_id
1333 to iterate over the IDs of all stopped threads of this process.
1336 get_process_first_stopped_thread_id (pid
, thread_state
)
1338 ttstate_t
* thread_state
;
1342 tt_status
= call_real_ttrace (
1343 TT_PROC_GET_FIRST_LWP_STATE
,
1346 (TTRACE_ARG_TYPE
) thread_state
,
1347 (TTRACE_ARG_TYPE
) sizeof (*thread_state
),
1351 if( errno
== EPROTO
) {
1352 /* This is an error we can handle: there isn't any stopped
1353 * thread. This happens when we're re-starting the application
1354 * and it has only one thread. GET_NEXT handles the case of
1355 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1363 perror_with_name ("ttrace");
1371 return thread_state
->tts_lwpid
;
1375 /* This function returns the ID of the "next" kernel thread in a
1376 stopped state, or 0 if there are none. "Next" refers to the
1377 thread following that of the last successful call to this
1378 function or to get_process_first_stopped_thread_id, using
1379 the value of thread_state returned by that call.
1381 This function can be used with get_process_first_stopped_thread_id
1382 to iterate over the IDs of all stopped threads of this process.
1385 get_process_next_stopped_thread_id (pid
, thread_state
)
1387 ttstate_t
* thread_state
;
1391 tt_status
= call_real_ttrace (
1392 TT_PROC_GET_NEXT_LWP_STATE
,
1395 (TTRACE_ARG_TYPE
) thread_state
,
1396 (TTRACE_ARG_TYPE
) sizeof (*thread_state
),
1399 perror_with_name ("ttrace");
1406 else if( tt_status
== 0 ) {
1407 /* End of list, no next state. Don't return the
1408 * tts_lwpid, as it's a meaningless "240".
1410 * This is an HPUX "feature".
1415 return thread_state
->tts_lwpid
;
1418 /* ??rehrauer: Eventually this function perhaps should be calling
1419 pid_to_thread_id. However, that function currently does nothing
1420 for HP-UX. Even then, I'm not clear whether that function
1421 will return a "kernel" thread ID, or a "user" thread ID. If
1422 the former, we can just call it here. If the latter, we must
1423 map from the "user" tid to a "kernel" tid.
1425 NOTE: currently not called.
1428 get_active_tid_of_pid (pid
)
1431 ttstate_t thread_state
;
1433 return get_process_first_stopped_thread_id (pid
, &thread_state
);
1436 /* This function returns 1 if tt_request is a ttrace request that
1437 * operates upon all threads of a (i.e., the entire) process.
1440 is_process_ttrace_request (tt_request
)
1443 return IS_TTRACE_PROCREQ (tt_request
);
1447 /* This function translates a thread ttrace request into
1448 * the equivalent process request for a one-thread process.
1451 make_process_version( request
)
1454 if (!IS_TTRACE_REQ (request
)) {
1455 error( "Internal error, bad ttrace request made\n" );
1461 return TT_PROC_STOP
;
1463 case TT_LWP_CONTINUE
:
1464 return TT_PROC_CONTINUE
;
1466 case TT_LWP_GET_EVENT_MASK
:
1467 return TT_PROC_GET_EVENT_MASK
;
1469 case TT_LWP_SET_EVENT_MASK
:
1470 return TT_PROC_SET_EVENT_MASK
;
1472 case TT_LWP_SINGLE
:
1473 case TT_LWP_RUREGS
:
1474 case TT_LWP_WUREGS
:
1475 case TT_LWP_GET_STATE
:
1476 return -1; /* No equivalent */
1484 /* This function translates the "pid" used by the rest of
1485 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1486 * with the given arguments.
1488 * In general, other parts of this module should call this
1489 * function when they are dealing with external users, who only
1490 * have tids to pass (but they call it "pid" for historical
1494 call_ttrace( request
, gdb_tid
, addr
, data
, addr2
)
1497 TTRACE_ARG_TYPE addr
, data
, addr2
;
1501 ttreq_t new_request
;
1503 char reason_for_failure
[100]; /* Arbitrary size, should be big enough. */
1506 int is_interesting
= 0;
1508 if( TT_LWP_RUREGS
== request
) {
1509 is_interesting
= 1; /* Adjust code here as desired */
1512 if( is_interesting
&& 0 && debug_on
) {
1513 if( !is_process_ttrace_request( request
)) {
1514 printf( "TT: Thread request, tid is %d", gdb_tid
);
1515 printf( "== SINGLE at %x", addr
);
1518 printf( "TT: Process request, tid is %d\n", gdb_tid
);
1519 printf( "==! SINGLE at %x", addr
);
1524 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1525 * which happen before any threads get set up) should go
1526 * directly to "call_real_ttrace", so they don't happen here.
1528 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1532 if( request
== TT_PROC_SETTRC
&& debug_on
)
1533 printf( "Unexpected call for TT_PROC_SETTRC\n" );
1536 /* Sometimes we get called with a bogus tid (e.g., if a
1537 * thread has terminated, we return 0; inftarg later asks
1538 * whether the thread has exited/forked/vforked).
1542 errno
= ESRCH
; /* ttrace's response would probably be "No such process". */
1546 /* All other cases should be able to expect that there are
1549 if( !any_thread_records()) {
1552 warning ("No thread records for ttrace call");
1554 errno
= ESRCH
; /* ttrace's response would be "No such process". */
1558 /* OK, now the task is to translate the incoming tid into
1561 real_tid
= map_from_gdb_tid( gdb_tid
);
1562 real_pid
= get_pid_for( real_tid
);
1564 /* Now check the result. "Real_pid" is NULL if our list
1565 * didn't find it. We have some tricks we can play to fix
1568 if( 0 == real_pid
) {
1569 ttstate_t thread_state
;
1573 printf( "No saved pid for tid %d\n", gdb_tid
);
1576 if( is_process_ttrace_request( request
)) {
1578 /* Ok, we couldn't get a tid. Try to translate to
1579 * the equivalent process operation. We expect this
1580 * NOT to happen, so this is a desparation-type
1581 * move. It can happen if there is an internal
1582 * error and so no "wait()" call is ever done.
1584 new_request
= make_process_version( request
);
1585 if( new_request
== -1 ) {
1589 printf( "...and couldn't make process version of thread operation\n" );
1592 /* Use hacky saved pid, which won't always be correct
1593 * in the multi-process future. Use tid as thread,
1594 * probably dooming this to failure. FIX!
1596 if( saved_real_pid
!= 0 ) {
1599 printf( "...using saved pid %d\n", saved_real_pid
);
1602 real_pid
= saved_real_pid
;
1607 error( "Unable to perform thread operation" );
1611 /* Sucessfully translated this to a process request,
1612 * which needs no thread value.
1616 request
= new_request
;
1620 printf( "Translated thread request to process request\n" );
1621 if( saved_real_pid
== 0 )
1622 printf( "...but there's no saved pid\n" );
1625 if( gdb_tid
!= saved_real_pid
)
1626 printf( "...but have the wrong pid (%d rather than %d)\n",
1627 gdb_tid
, saved_real_pid
);
1631 } /* Translated to a process request */
1632 } /* Is a process request */
1635 /* We have to have a thread. Ooops.
1637 error( "Thread request with no threads (%s)",
1638 get_printable_name_of_ttrace_request( request
));
1642 /* Ttrace doesn't like to see tid values on process requests,
1643 * even if we have the right one.
1645 if (is_process_ttrace_request (request
)) {
1650 if( is_interesting
&& 0 && debug_on
) {
1651 printf( " now tid %d, pid %d\n", real_tid
, real_pid
);
1652 printf( " request is %s\n", get_printable_name_of_ttrace_request (request
));
1656 /* Finally, the (almost) real call.
1658 tt_status
= call_real_ttrace (request
, real_pid
, real_tid
, addr
, data
, addr2
);
1661 if(is_interesting
&& debug_on
) {
1662 if( !TT_OK( tt_status
, errno
)
1663 && !(tt_status
== 0 & errno
== 0))
1664 printf( " got error (errno==%d, status==%d)\n", errno
, tt_status
);
1672 /* Stop all the threads of a process.
1674 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1675 * to get a TTEVT_NONE event, discarding the old event. Be
1676 * very careful, and only call TT_PROC_STOP when you mean it!
1679 stop_all_threads_of_process( real_pid
)
1684 ttw_status
= call_real_ttrace (TT_PROC_STOP
,
1687 (TTRACE_ARG_TYPE
) TT_NIL
,
1688 (TTRACE_ARG_TYPE
) TT_NIL
,
1691 perror_with_name ("ttrace stop of other threads");
1695 /* Under some circumstances, it's unsafe to attempt to stop, or even
1696 query the state of, a process' threads.
1698 In ttrace-based HP-UX, an example is a vforking child process. The
1699 vforking parent and child are somewhat fragile, w/r/t what we can do
1700 what we can do to them with ttrace, until after the child exits or
1701 execs, or until the parent's vfork event is delivered. Until that
1702 time, we must not try to stop the process' threads, or inquire how
1703 many there are, or even alter its data segments, or it typically dies
1704 with a SIGILL. Sigh.
1706 This function returns 1 if this stopped process, and the event that
1707 we're told was responsible for its current stopped state, cannot safely
1708 have its threads examined.
1710 #define CHILD_VFORKED(evt,pid) \
1711 (((evt) == TTEVT_VFORK) && ((pid) != inferior_pid))
1712 #define CHILD_URPED(evt,pid) \
1713 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1714 #define PARENT_VFORKED(evt,pid) \
1715 (((evt) == TTEVT_VFORK) && ((pid) == inferior_pid))
1718 can_touch_threads_of_process (pid
, stopping_event
)
1720 ttevents_t stopping_event
;
1722 if (CHILD_VFORKED (stopping_event
, pid
))
1724 vforking_child_pid
= pid
;
1725 vfork_in_flight
= 1;
1728 else if (vfork_in_flight
&&
1729 (PARENT_VFORKED (stopping_event
, pid
) ||
1730 CHILD_URPED (stopping_event
, pid
)))
1732 vfork_in_flight
= 0;
1733 vforking_child_pid
= 0;
1736 return ! vfork_in_flight
;
1740 /* If we can find an as-yet-unhandled thread state of a
1741 * stopped thread of this process return 1 and set "tsp".
1742 * Return 0 if we can't.
1744 * If this function is used when the threads of PIS haven't
1745 * been stopped, undefined behaviour is guaranteed!
1748 select_stopped_thread_of_process (pid
, tsp
)
1752 lwpid_t candidate_tid
, tid
;
1753 ttstate_t candidate_tstate
, tstate
;
1755 /* If we're not allowed to touch the process now, then just
1756 * return the current value of *TSP.
1758 * This supports "vfork". It's ok, really, to double the
1759 * current event (the child EXEC, we hope!).
1761 if (! can_touch_threads_of_process (pid
, tsp
->tts_event
))
1764 /* Decide which of (possibly more than one) events to
1765 * return as the first one. We scan them all so that
1766 * we always return the result of a fake-step first.
1769 for (tid
= get_process_first_stopped_thread_id (pid
, &tstate
);
1771 tid
= get_process_next_stopped_thread_id (pid
, &tstate
))
1773 /* TTEVT_NONE events are uninteresting to our clients. They're
1774 * an artifact of our "stop the world" model--the thread is
1775 * stopped because we stopped it.
1777 if (tstate
.tts_event
== TTEVT_NONE
) {
1778 set_handled( pid
, tstate
.tts_lwpid
);
1781 /* Did we just single-step a single thread, without letting any
1782 * of the others run? Is this an event for that thread?
1784 * If so, we believe our client would prefer to see this event
1785 * over any others. (Typically the client wants to just push
1786 * one thread a little farther forward, and then go around
1787 * checking for what all threads are doing.)
1789 else if (doing_fake_step
&& (tstate
.tts_lwpid
== fake_step_tid
))
1791 #ifdef WAIT_BUFFER_DEBUG
1792 /* It's possible here to see either a SIGTRAP (due to
1793 * successful completion of a step) or a SYSCALL_ENTRY
1794 * (due to a step completion with active hardware
1798 printf( "Ending fake step with tid %d, state %s\n",
1800 get_printable_name_of_ttrace_event( tstate
.tts_event
));
1803 /* Remember this one, and throw away any previous
1806 candidate_tid
= tstate
.tts_lwpid
;
1807 candidate_tstate
= tstate
;
1810 #ifdef FORGET_DELETED_BPTS
1812 /* We can't just do this, as if we do, and then wind
1813 * up the loop with no unhandled events, we need to
1814 * handle that case--the appropriate reaction is to
1815 * just continue, but there's no easy way to do that.
1817 * Better to put this in the ttrace_wait call--if, when
1818 * we fake a wait, we update our events based on the
1819 * breakpoint_here_pc call and find there are no more events,
1820 * then we better continue and so on.
1822 * Or we could put it in the next/continue fake.
1823 * But it has to go in the buffering code, not in the
1824 * real go/wait code.
1826 else if( (TTEVT_SIGNAL
== tstate
.tts_event
)
1827 && (5 == tstate
.tts_u
.tts_signal
.tts_signo
)
1828 && (0 != get_raw_pc( tstate
.tts_lwpid
))
1829 && ! breakpoint_here_p( get_raw_pc( tstate
.tts_lwpid
)) ) {
1831 * If the user deleted a breakpoint while this
1832 * breakpoint-hit event was buffered, we can forget
1835 #ifdef WAIT_BUFFER_DEBUG
1837 printf( "Forgetting deleted bp hit for thread %d\n",
1841 set_handled( pid
, tstate
.tts_lwpid
);
1845 /* Else, is this the first "unhandled" event? If so,
1846 * we believe our client wants to see it (if we don't
1847 * see a fake-step later on in the scan).
1849 else if( !was_handled( tstate
.tts_lwpid
) && candidate_tid
== 0 ) {
1850 candidate_tid
= tstate
.tts_lwpid
;
1851 candidate_tstate
= tstate
;
1854 /* This is either an event that has already been "handled",
1855 * and thus we believe is uninteresting to our client, or we
1856 * already have a candidate event. Ignore it...
1860 /* What do we report?
1862 if( doing_fake_step
) {
1863 if( candidate_tid
== fake_step_tid
) {
1866 tstate
= candidate_tstate
;
1869 warning( "Internal error: fake-step failed to complete." );
1873 else if( candidate_tid
!= 0 ) {
1874 /* Found a candidate unhandled event.
1876 tstate
= candidate_tstate
;
1878 else if( tid
!= 0 ) {
1879 warning( "Internal error in call of ttrace_wait." );
1883 warning ("Internal error: no unhandled thread event to select");
1887 copy_ttstate_t (tsp
, &tstate
);
1889 } /* End of select_stopped_thread_of_process */
1892 /* Check our internal thread data against the real thing.
1895 check_thread_consistency( real_pid
)
1898 int tid
; /* really lwpid_t */
1902 /* Spin down the O/S list of threads, checking that they
1903 * match what we've got.
1905 for (tid
= get_process_first_stopped_thread_id( real_pid
, &tstate
);
1907 tid
= get_process_next_stopped_thread_id( real_pid
, &tstate
)) {
1909 p
= find_thread_info( tid
);
1912 warning( "No internal thread data for thread %d.", tid
);
1917 warning( "Inconsistent internal thread data for thread %d.", tid
);
1920 if( p
->terminated
) {
1921 warning( "Thread %d is not terminated, internal error.", tid
);
1926 #define TT_COMPARE( fld ) \
1927 tstate.fld != p->last_stop_state.fld
1929 if( p
->have_state
) {
1930 if( TT_COMPARE( tts_pid
)
1931 || TT_COMPARE( tts_lwpid
)
1932 || TT_COMPARE( tts_user_tid
)
1933 || TT_COMPARE( tts_event
)
1934 || TT_COMPARE( tts_flags
)
1935 || TT_COMPARE( tts_scno
)
1936 || TT_COMPARE( tts_scnargs
)) {
1937 warning( "Internal thread data for thread %d is wrong.", tid
);
1943 #endif /* PARANOIA */
1946 /* This function wraps calls to "call_real_ttrace_wait" so
1947 * that a actual wait is only done when all pending events
1948 * have been reported.
1950 * Note that typically it is called with a pid of "0", i.e.
1951 * the "don't care" value.
1953 * Return value is the status of the pseudo wait.
1956 call_ttrace_wait( pid
, option
, tsp
, tsp_size
)
1962 /* This holds the actual, for-real, true process ID.
1964 static int real_pid
;
1966 /* As an argument to ttrace_wait, zero pid
1967 * means "Any process", and zero tid means
1968 * "Any thread of the specified process".
1971 lwpid_t wait_tid
= 0;
1974 int ttw_status
= 0; /* To be returned */
1976 thread_info
* tinfo
= NULL
;
1983 printf( "TW: Pid to wait on is %d\n", pid
);
1986 if( !any_thread_records())
1987 error( "No thread records for ttrace call w. specific pid" );
1989 /* OK, now the task is to translate the incoming tid into
1992 real_tid
= map_from_gdb_tid( pid
);
1993 real_pid
= get_pid_for( real_tid
);
1996 printf( "==TW: real pid %d, real tid %d\n", real_pid
, real_tid
);
2001 /* Sanity checks and set-up.
2004 * Stopped Running Fake-step (v)Fork
2005 * \________________________________________
2007 * No buffered events | error wait wait wait
2009 * Buffered events | debuffer error wait debuffer (?)
2012 if( more_events_left
== 0 ) {
2014 if( process_state
== RUNNING
) {
2015 /* OK--normal call of ttrace_wait with no buffered events.
2019 else if( process_state
== FAKE_STEPPING
) {
2020 /* Ok--call of ttrace_wait to support
2021 * fake stepping with no buffered events.
2023 * But we better be fake-stepping!
2025 if( !doing_fake_step
) {
2026 warning( "Inconsistent thread state." );
2029 else if( (process_state
== FORKING
)
2030 || (process_state
== VFORKING
)) {
2031 /* Ok--there are two processes, so waiting
2032 * for the second while the first is stopped
2033 * is ok. Handled bits stay as they were.
2037 else if( process_state
== STOPPED
) {
2038 warning( "Process not running at wait call." );
2043 warning( "Inconsistent process state." );
2049 if( process_state
== STOPPED
) {
2050 /* OK--buffered events being unbuffered.
2054 else if( process_state
== RUNNING
) {
2055 /* An error--shouldn't have buffered events
2058 warning( "Trying to continue with buffered events:" );
2060 else if( process_state
== FAKE_STEPPING
) {
2062 * Better be fake-stepping!
2064 if( !doing_fake_step
) {
2065 warning( "Losing buffered thread events!\n" );
2068 else if( (process_state
== FORKING
)
2069 || (process_state
== VFORKING
)) {
2070 /* Ok--there are two processes, so waiting
2071 * for the second while the first is stopped
2072 * is ok. Handled bits stay as they were.
2077 warning( "Process in unknown state with buffered events." );
2080 /* Sometimes we have to wait for a particular thread
2081 * (if we're stepping over a bpt). In that case, we
2082 * _know_ it's going to complete the single-step we
2083 * asked for (because we're only doing the step under
2084 * certain very well-understood circumstances), so it
2087 if( doing_fake_step
) {
2088 wait_tid
= fake_step_tid
;
2089 wait_pid
= get_pid_for( fake_step_tid
);
2091 #ifdef WAIT_BUFFER_DEBUG
2093 printf( "Doing a wait after a fake-step for %d, pid %d\n",
2094 wait_tid
, wait_pid
);
2098 if( more_events_left
== 0 /* No buffered events, need real ones. */
2099 || process_state
!= STOPPED
) {
2100 /* If there are no buffered events, and so we need
2101 * real ones, or if we are FORKING, VFORKING,
2102 * FAKE_STEPPING or RUNNING, and thus have to do
2103 * a real wait, then do a real wait.
2106 #ifdef WAIT_BUFFER_DEBUG
2107 /* Normal case... */
2109 printf( "TW: do it for real; pid %d, tid %d\n", wait_pid
, wait_tid
);
2112 /* The actual wait call.
2114 ttw_status
= call_real_ttrace_wait( wait_pid
, wait_tid
, option
, tsp
, tsp_size
);
2116 /* Note that the routines we'll call will be using "call_real_ttrace",
2117 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2118 * the rest of the world uses (which is actually the tid).
2120 real_pid
= tsp
->tts_pid
;
2122 /* For most events: Stop the world!
2124 * It's sometimes not safe to stop all threads of a process.
2125 * Sometimes it's not even safe to ask for the thread state
2128 if (can_touch_threads_of_process (real_pid
, tsp
->tts_event
))
2130 /* If we're really only stepping a single thread, then don't
2131 * try to stop all the others -- we only do this single-stepping
2132 * business when all others were already stopped...and the stop
2133 * would mess up other threads' events.
2135 * Similiarly, if there are other threads with events,
2136 * don't do the stop.
2138 if( !doing_fake_step
) {
2139 if( more_events_left
> 0 )
2140 warning( "Internal error in stopping process" );
2142 stop_all_threads_of_process (real_pid
);
2144 /* At this point, we could scan and update_thread_list(),
2145 * and only use the local list for the rest of the
2146 * module! We'd get rid of the scans in the various
2147 * continue routines (adding one in attach). It'd
2148 * be great--UPGRADE ME!
2154 else if( debug_on
) {
2155 if( more_events_left
> 0 )
2156 printf( "== Can't stop process; more events!\n" );
2158 printf( "== Can't stop process!\n" );
2162 process_state
= STOPPED
;
2164 #ifdef WAIT_BUFFER_DEBUG
2166 printf( "Process set to STOPPED\n" );
2171 /* Fake a call to ttrace_wait. The process must be
2172 * STOPPED, as we aren't going to do any wait.
2174 #ifdef WAIT_BUFFER_DEBUG
2176 printf( "TW: fake it\n" );
2179 if( process_state
!= STOPPED
) {
2180 warning( "Process not stopped at wait call, in state '%s'.\n",
2181 get_printable_name_of_process_state( process_state
));
2184 if( doing_fake_step
)
2185 error( "Internal error in stepping over breakpoint" );
2187 ttw_status
= 0; /* Faking it is always successful! */
2188 } /* End of fake or not? if */
2190 /* Pick an event to pass to our caller. Be paranoid.
2192 if( !select_stopped_thread_of_process( real_pid
, tsp
))
2193 warning( "Can't find event, using previous event." );
2195 else if( tsp
->tts_event
== TTEVT_NONE
)
2196 warning( "Internal error: no thread has a real event." );
2198 else if( doing_fake_step
) {
2199 if( fake_step_tid
!= tsp
->tts_lwpid
)
2200 warning( "Internal error in stepping over breakpoint." );
2202 /* This wait clears the (current) fake-step if there was one.
2204 doing_fake_step
= 0;
2208 /* We now have a correct tsp and ttw_status for the thread
2209 * which we want to report. So it's "handled"! This call
2210 * will add it to our list if it's not there already.
2212 set_handled( real_pid
, tsp
->tts_lwpid
);
2214 /* Save a copy of the ttrace state of this thread, in our local
2217 This caches the state. The implementation of queries like
2218 target_has_execd can then use this cached state, rather than
2219 be forced to make an explicit ttrace call to get it.
2221 (Guard against the condition that this is the first time we've
2222 waited on, i.e., seen this thread, and so haven't yet entered
2223 it into our list of threads.)
2225 tinfo
= find_thread_info (tsp
->tts_lwpid
);
2226 if (tinfo
!= NULL
) {
2227 copy_ttstate_t (&tinfo
->last_stop_state
, tsp
);
2228 tinfo
->have_state
= 1;
2232 } /* call_ttrace_wait */
2234 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2236 child_reported_exec_events_per_exec_call ()
2238 return 1; /* ttrace reports the event once per call. */
2244 /* Our implementation of hardware watchpoints involves making memory
2245 pages write-protected. We must remember a page's original permissions,
2246 and we must also know when it is appropriate to restore a page's
2247 permissions to its original state.
2249 We use a "dictionary" of hardware-watched pages to do this. Each
2250 hardware-watched page is recorded in the dictionary. Each page's
2251 dictionary entry contains the original permissions and a reference
2252 count. Pages are hashed into the dictionary by their start address.
2254 When hardware watchpoint is set on page X for the first time, page X
2255 is added to the dictionary with a reference count of 1. If other
2256 hardware watchpoints are subsequently set on page X, its reference
2257 count is incremented. When hardware watchpoints are removed from
2258 page X, its reference count is decremented. If a page's reference
2259 count drops to 0, it's permissions are restored and the page's entry
2260 is thrown out of the dictionary.
2262 typedef struct memory_page
{
2263 CORE_ADDR page_start
;
2264 int reference_count
;
2265 int original_permissions
;
2266 struct memory_page
* next
;
2267 struct memory_page
* previous
;
2270 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2275 int page_protections_allowed
;
2276 /* These are just the heads of chains of actual page descriptors. */
2277 memory_page_t buckets
[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
];
2278 } memory_page_dictionary
;
2282 require_memory_page_dictionary ()
2286 /* Is the memory page dictionary ready for use? If so, we're done. */
2287 if (memory_page_dictionary
.page_count
>= (LONGEST
) 0)
2290 /* Else, initialize it. */
2291 memory_page_dictionary
.page_count
= (LONGEST
) 0;
2293 for (i
=0; i
<MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; i
++)
2295 memory_page_dictionary
.buckets
[i
].page_start
= (CORE_ADDR
) 0;
2296 memory_page_dictionary
.buckets
[i
].reference_count
= 0;
2297 memory_page_dictionary
.buckets
[i
].next
= NULL
;
2298 memory_page_dictionary
.buckets
[i
].previous
= NULL
;
2304 retire_memory_page_dictionary ()
2306 memory_page_dictionary
.page_count
= (LONGEST
) -1;
2310 /* Write-protect the memory page that starts at this address.
2312 Returns the original permissions of the page.
2315 write_protect_page (pid
, page_start
)
2317 CORE_ADDR page_start
;
2320 int original_permissions
;
2321 int new_permissions
;
2323 tt_status
= call_ttrace (TT_PROC_GET_MPROTECT
,
2325 (TTRACE_ARG_TYPE
) page_start
,
2327 (TTRACE_ARG_TYPE
) &original_permissions
);
2328 if (errno
|| (tt_status
< 0))
2330 return 0; /* What else can we do? */
2333 /* We'll also write-protect the page now, if that's allowed. */
2334 if (memory_page_dictionary
.page_protections_allowed
)
2336 new_permissions
= original_permissions
& ~PROT_WRITE
;
2337 tt_status
= call_ttrace (TT_PROC_SET_MPROTECT
,
2339 (TTRACE_ARG_TYPE
) page_start
,
2340 (TTRACE_ARG_TYPE
) memory_page_dictionary
.page_size
,
2341 (TTRACE_ARG_TYPE
) new_permissions
);
2342 if (errno
|| (tt_status
< 0))
2344 return 0; /* What else can we do? */
2348 return original_permissions
;
2352 /* Unwrite-protect the memory page that starts at this address, restoring
2353 (what we must assume are) its original permissions.
2356 unwrite_protect_page (pid
, page_start
, original_permissions
)
2358 CORE_ADDR page_start
;
2359 int original_permissions
;
2363 tt_status
= call_ttrace (TT_PROC_SET_MPROTECT
,
2365 (TTRACE_ARG_TYPE
) page_start
,
2366 (TTRACE_ARG_TYPE
) memory_page_dictionary
.page_size
,
2367 (TTRACE_ARG_TYPE
) original_permissions
);
2368 if (errno
|| (tt_status
< 0))
2370 return; /* What else can we do? */
2375 /* Memory page-protections are used to implement "hardware" watchpoints
2378 For every memory page that is currently being watched (i.e., that
2379 presently should be write-protected), write-protect it.
2382 hppa_enable_page_protection_events (pid
)
2387 memory_page_dictionary
.page_protections_allowed
= 1;
2389 for (bucket
=0; bucket
<MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; bucket
++)
2391 memory_page_t
* page
;
2393 page
= memory_page_dictionary
.buckets
[bucket
].next
;
2394 while (page
!= NULL
)
2396 page
->original_permissions
= write_protect_page (pid
, page
->page_start
);
2403 /* Memory page-protections are used to implement "hardware" watchpoints
2406 For every memory page that is currently being watched (i.e., that
2407 presently is or should be write-protected), un-write-protect it.
2410 hppa_disable_page_protection_events (pid
)
2415 for (bucket
=0; bucket
<MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; bucket
++)
2417 memory_page_t
* page
;
2419 page
= memory_page_dictionary
.buckets
[bucket
].next
;
2420 while (page
!= NULL
)
2422 unwrite_protect_page (pid
, page
->page_start
, page
->original_permissions
);
2427 memory_page_dictionary
.page_protections_allowed
= 0;
2430 /* Count the number of outstanding events. At this
2431 * point, we have selected one thread and its event
2432 * as the one to be "reported" upwards to core gdb.
2433 * That thread is already marked as "handled".
2435 * Note: we could just scan our own thread list. FIXME!
2438 count_unhandled_events( real_pid
, real_tid
)
2446 /* Ok, find out how many threads have real events to report.
2449 ttid
= get_process_first_stopped_thread_id( real_pid
, &tstate
);
2454 printf( "Process %d has no threads\n", real_pid
);
2456 printf( "Process %d has these threads:\n", real_pid
);
2461 if( tstate
.tts_event
!= TTEVT_NONE
2462 && !was_handled( ttid
)) {
2463 /* TTEVT_NONE implies we just stopped it ourselves
2464 * because we're the stop-the-world guys, so it's
2465 * not an event from our point of view.
2467 * If "was_handled" is true, this is an event we
2468 * already handled, so don't count it.
2470 * Note that we don't count the thread with the
2471 * currently-reported event, as it's already marked
2477 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2479 if( ttid
== real_tid
)
2480 printf( "*" ); /* Thread we're reporting */
2484 if( tstate
.tts_event
!= TTEVT_NONE
)
2485 printf( "+" ); /* Thread with a real event */
2489 if( was_handled( ttid
))
2490 printf( "h" ); /* Thread has been handled */
2494 printf( " %d, with event %s", ttid
,
2495 get_printable_name_of_ttrace_event( tstate
.tts_event
));
2497 if( tstate
.tts_event
== TTEVT_SIGNAL
2498 && 5 == tstate
.tts_u
.tts_signal
.tts_signo
) {
2501 pc_val
= get_raw_pc( ttid
);
2504 printf( " breakpoint at 0x%x\n", pc_val
);
2506 printf( " bpt, can't fetch pc.\n" );
2513 ttid
= get_process_next_stopped_thread_id (real_pid
, &tstate
);
2516 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2518 if( events_left
> 0 )
2519 printf( "There are thus %d pending events\n", events_left
);
2525 /* This function is provided as a sop to clients that are calling
2526 * proc_wait to wait for a process to stop. (see the
2527 * implementation of child_wait.) Return value is the pid for
2528 * the event that ended the wait.
2530 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2533 proc_wait (pid
, status
)
2544 /* The ptrace implementation of this also ignores pid.
2548 ttwait_return
= call_ttrace_wait( 0, TTRACE_WAITOK
, &tsp
, sizeof (tsp
) );
2549 if (ttwait_return
< 0)
2551 /* ??rehrauer: It appears that if our inferior exits and we
2552 haven't asked for exit events, that we're not getting any
2553 indication save a negative return from ttrace_wait and an
2558 *status
= 0; /* WIFEXITED */
2559 return inferior_pid
;
2562 warning( "Call of ttrace_wait returned with errno %d.",
2564 *status
= ttwait_return
;
2565 return inferior_pid
;
2568 real_pid
= tsp
.tts_pid
;
2569 real_tid
= tsp
.tts_lwpid
;
2571 /* One complication is that the "tts_event" structure has
2572 * a set of flags, and more than one can be set. So we
2573 * either have to force an order (as we do here), or handle
2574 * more than one flag at a time.
2576 if (tsp
.tts_event
& TTEVT_LWP_CREATE
) {
2578 /* Unlike what you might expect, this event is reported in
2579 * the _creating_ thread, and the _created_ thread (whose tid
2580 * we have) is still running. So we have to stop it. This
2581 * has already been done in "call_ttrace_wait", but should we
2582 * ever abandon the "stop-the-world" model, here's the command
2585 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2587 * Note that this would depend on being called _after_ "add_tthread"
2588 * below for the tid-to-pid translation to be done in "call_ttrace".
2593 printf( "New thread: pid %d, tid %d, creator tid %d\n",
2594 real_pid
, tsp
.tts_u
.tts_thread
.tts_target_lwpid
,
2598 /* Now we have to return the tid of the created thread, not
2599 * the creating thread, or "wait_for_inferior" won't know we
2600 * have a new "process" (thread). Plus we should record it
2603 real_tid
= tsp
.tts_u
.tts_thread
.tts_target_lwpid
;
2605 add_tthread( real_pid
, real_tid
);
2608 else if( (tsp
.tts_event
& TTEVT_LWP_TERMINATE
)
2609 || (tsp
.tts_event
& TTEVT_LWP_EXIT
) ) {
2613 printf( "Thread dies: %d\n", real_tid
);
2616 del_tthread( real_tid
);
2619 else if (tsp
.tts_event
& TTEVT_EXEC
) {
2623 printf( "Pid %d has zero'th thread %d; inferior pid is %d\n",
2624 real_pid
, real_tid
, inferior_pid
);
2627 add_tthread( real_pid
, real_tid
);
2631 else if( debug_on
) {
2632 printf( "Process-level event %s, using tid %d\n",
2633 get_printable_name_of_ttrace_event( tsp
.tts_event
),
2636 /* OK to do this, as "add_tthread" won't add
2637 * duplicate entries. Also OK not to do it,
2638 * as this event isn't one which can change the
2641 add_tthread( real_pid
, real_tid
);
2646 /* How many events are left to report later?
2647 * In a non-stop-the-world model, this isn't needed.
2649 * Note that it's not always safe to query the thread state of a process,
2650 * which is what count_unhandled_events does. (If unsafe, we're left with
2651 * no other resort than to assume that no more events remain...)
2653 if (can_touch_threads_of_process (real_pid
, tsp
.tts_event
))
2654 more_events_left
= count_unhandled_events( real_pid
, real_tid
);
2657 if( more_events_left
> 0 )
2658 warning( "Vfork or fork causing loss of %d buffered events.",
2661 more_events_left
= 0;
2664 /* Attempt to translate the ttrace_wait-returned status into the
2667 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2668 clients that expect to pick apart a ptrace wait status, to use
2669 something a little more abstract.
2671 if ( (tsp
.tts_event
& TTEVT_EXEC
)
2672 || (tsp
.tts_event
& TTEVT_FORK
)
2673 || (tsp
.tts_event
& TTEVT_VFORK
))
2675 /* Forks come in pairs (parent and child), so core gdb
2676 * will do two waits. Be ready to notice this.
2678 if (tsp
.tts_event
& TTEVT_FORK
)
2680 process_state
= FORKING
;
2682 #ifdef WAIT_BUFFER_DEBUG
2684 printf( "Process set to FORKING\n" );
2687 else if (tsp
.tts_event
& TTEVT_VFORK
)
2689 process_state
= VFORKING
;
2691 #ifdef WAIT_BUFFER_DEBUG
2693 printf( "Process set to VFORKING\n" );
2697 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2698 but I don't think non HP-UX-specific clients really carefully
2699 inspect the first events they get after inferior startup, so
2700 it probably almost doesn't matter what we claim this is.
2705 printf( "..a process 'event'\n" );
2708 /* Also make fork and exec events look like bpts, so they can be caught.
2710 *status
= 0177 | (_SIGTRAP
<< 8);
2713 /* Special-cases: We ask for syscall entry and exit events to implement
2714 "fast" (aka "hardware") watchpoints.
2716 When we get a syscall entry, we want to disable page-protections,
2717 and resume the inferior; this isn't an event we wish for
2718 wait_for_inferior to see. Note that we must resume ONLY the
2719 thread that reported the syscall entry; we don't want to allow
2720 other threads to run with the page protections off, as they might
2721 then be able to write to watch memory without it being caught.
2723 When we get a syscall exit, we want to reenable page-protections,
2724 but we don't want to resume the inferior; this is an event we wish
2725 wait_for_inferior to see. Make it look like the signal we normally
2726 get for a single-step completion. This should cause wait_for_inferior
2727 to evaluate whether any watchpoint triggered.
2729 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2730 due to some HP-UX "features". Some syscalls have problems with
2731 write-protections on some pages, and some syscalls seem to have
2732 pending writes to those pages at the time we're getting the return
2733 event. So, we'll single-step the inferior to get out of the syscall,
2734 and then reenable protections.
2736 Note that we're intentionally allowing the syscall exit case to
2737 fall through into the succeeding cases, as sometimes we single-
2738 step out of one syscall only to immediately enter another...
2740 else if ((tsp
.tts_event
& TTEVT_SYSCALL_ENTRY
)
2741 || (tsp
.tts_event
& TTEVT_SYSCALL_RETURN
))
2743 /* Make a syscall event look like a breakpoint. Same comments
2744 as for exec & fork events.
2748 printf( "..a syscall 'event'\n" );
2751 /* Also make syscall events look like bpts, so they can be caught.
2753 *status
= 0177 | (_SIGTRAP
<< 8);
2756 else if ((tsp
.tts_event
& TTEVT_LWP_CREATE
)
2757 || (tsp
.tts_event
& TTEVT_LWP_TERMINATE
)
2758 || (tsp
.tts_event
& TTEVT_LWP_EXIT
))
2760 /* Make a thread event look like a breakpoint. Same comments
2761 * as for exec & fork events.
2765 printf( "..a thread 'event'\n" );
2768 /* Also make thread events look like bpts, so they can be caught.
2770 *status
= 0177 | (_SIGTRAP
<< 8);
2773 else if ((tsp
.tts_event
& TTEVT_EXIT
))
2778 printf( "..an exit\n" );
2781 /* Prevent rest of gdb from thinking this is
2782 * a new thread if for some reason it's never
2783 * seen the main thread before.
2785 inferior_pid
= map_to_gdb_tid( real_tid
); /* HACK, FIX */
2787 *status
= 0 | (tsp
.tts_u
.tts_exit
.tts_exitcode
);
2790 else if (tsp
.tts_event
& TTEVT_SIGNAL
)
2794 printf( "..a signal, %d\n", tsp
.tts_u
.tts_signal
.tts_signo
);
2797 *status
= 0177 | (tsp
.tts_u
.tts_signal
.tts_signo
<< 8);
2803 /* This means the process or thread terminated. But we should've
2804 caught an explicit exit/termination above. So warn (this is
2805 really an internal error) and claim the process or thread
2806 terminated with a SIGTRAP.
2809 warning ("process_wait: unknown process state");
2813 printf( "Process-level event %s, using tid %d\n",
2814 get_printable_name_of_ttrace_event( tsp
.tts_event
),
2821 target_post_wait (tsp
.tts_pid
, *status
);
2826 printf( "Done waiting, pid is %d, tid %d\n", real_pid
, real_tid
);
2829 /* All code external to this module uses the tid, but calls
2830 * it "pid". There's some tweaking so that the outside sees
2831 * the first thread as having the same number as the starting
2834 return_pid
= map_to_gdb_tid( real_tid
);
2836 /* Remember this for later use in "hppa_prepare_to_proceed".
2838 old_gdb_pid
= inferior_pid
;
2839 reported_pid
= return_pid
;
2840 reported_bpt
= ((tsp
.tts_event
& TTEVT_SIGNAL
) && (5 == tsp
.tts_u
.tts_signal
.tts_signo
));
2842 if( real_tid
== 0 || return_pid
== 0 ) {
2843 warning( "Internal error: process-wait failed." );
2850 /* This function causes the caller's process to be traced by its
2851 parent. This is intended to be called after GDB forks itself,
2852 and before the child execs the target. Despite the name, it
2853 is called by the child.
2855 Note that HP-UX ttrace is rather funky in how this is done.
2856 If the parent wants to get the initial exec event of a child,
2857 it must set the ttrace event mask of the child to include execs.
2858 (The child cannot do this itself.) This must be done after the
2859 child is forked, but before it execs.
2861 To coordinate the parent and child, we implement a semaphore using
2862 pipes. After SETTRC'ing itself, the child tells the parent that
2863 it is now traceable by the parent, and waits for the parent's
2864 acknowledgement. The parent can then set the child's event mask,
2865 and notify the child that it can now exec.
2867 (The acknowledgement by parent happens as a result of a call to
2868 child_acknowledge_created_inferior.)
2871 parent_attach_all ()
2875 /* We need a memory home for a constant, to pass it to ttrace.
2876 The value of the constant is arbitrary, so long as both
2877 parent and child use the same value. Might as well use the
2878 "magic" constant provided by ttrace...
2880 uint64_t tc_magic_child
= TT_VERSION
;
2881 uint64_t tc_magic_parent
= 0;
2883 tt_status
= call_real_ttrace (
2888 (TTRACE_ARG_TYPE
) TT_VERSION
,
2894 /* Notify the parent that we're potentially ready to exec(). */
2895 write (startup_semaphore
.child_channel
[SEM_TALK
],
2897 sizeof (tc_magic_child
));
2899 /* Wait for acknowledgement from the parent. */
2900 read (startup_semaphore
.parent_channel
[SEM_LISTEN
],
2902 sizeof (tc_magic_parent
));
2904 if (tc_magic_child
!= tc_magic_parent
)
2905 warning ("mismatched semaphore magic");
2907 /* Discard our copy of the semaphore. */
2908 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
2909 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
2910 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
2911 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
2916 /* Despite being file-local, this routine is dealing with
2917 * actual process IDs, not thread ids. That's because it's
2918 * called before the first "wait" call, and there's no map
2919 * yet from tids to pids.
2921 * When it is called, a forked child is running, but waiting on
2922 * the semaphore. If you stop the child and re-start it,
2923 * things get confused, so don't do that! An attached child is
2926 * Since this is called after either attach or run, we
2927 * have to be the common part of both.
2930 require_notification_of_events ( real_pid
)
2934 ttevent_t notifiable_events
;
2937 ttstate_t thread_state
;
2941 printf( "Require notif, pid is %d\n", real_pid
);
2944 /* Temporary HACK: tell inftarg.c/child_wait to not
2945 * loop until pids are the same.
2947 not_same_real_pid
= 0;
2949 sigemptyset (¬ifiable_events
.tte_signals
);
2950 notifiable_events
.tte_opts
= TTEO_NONE
;
2952 /* This ensures that forked children inherit their parent's
2953 * event mask, which we're setting here.
2955 * NOTE: if you debug gdb with itself, then the ultimate
2956 * debuggee gets flags set by the outermost gdb, as
2957 * a child of a child will still inherit.
2959 notifiable_events
.tte_opts
|= TTEO_PROC_INHERIT
;
2961 notifiable_events
.tte_events
= TTEVT_DEFAULT
;
2962 notifiable_events
.tte_events
|= TTEVT_SIGNAL
;
2963 notifiable_events
.tte_events
|= TTEVT_EXEC
;
2964 notifiable_events
.tte_events
|= TTEVT_EXIT
;
2965 notifiable_events
.tte_events
|= TTEVT_FORK
;
2966 notifiable_events
.tte_events
|= TTEVT_VFORK
;
2967 notifiable_events
.tte_events
|= TTEVT_LWP_CREATE
;
2968 notifiable_events
.tte_events
|= TTEVT_LWP_EXIT
;
2969 notifiable_events
.tte_events
|= TTEVT_LWP_TERMINATE
;
2971 tt_status
= call_real_ttrace (
2972 TT_PROC_SET_EVENT_MASK
,
2975 (TTRACE_ARG_TYPE
) ¬ifiable_events
,
2976 (TTRACE_ARG_TYPE
) sizeof (notifiable_events
),
2981 require_notification_of_exec_events ( real_pid
)
2985 ttevent_t notifiable_events
;
2988 ttstate_t thread_state
;
2992 printf( "Require notif, pid is %d\n", real_pid
);
2995 /* Temporary HACK: tell inftarg.c/child_wait to not
2996 * loop until pids are the same.
2998 not_same_real_pid
= 0;
3000 sigemptyset (¬ifiable_events
.tte_signals
);
3001 notifiable_events
.tte_opts
= TTEO_NOSTRCCHLD
;
3003 /* This ensures that forked children don't inherit their parent's
3004 * event mask, which we're setting here.
3006 notifiable_events
.tte_opts
&= ~TTEO_PROC_INHERIT
;
3008 notifiable_events
.tte_events
= TTEVT_DEFAULT
;
3009 notifiable_events
.tte_events
|= TTEVT_EXEC
;
3010 notifiable_events
.tte_events
|= TTEVT_EXIT
;
3012 tt_status
= call_real_ttrace (
3013 TT_PROC_SET_EVENT_MASK
,
3016 (TTRACE_ARG_TYPE
) ¬ifiable_events
,
3017 (TTRACE_ARG_TYPE
) sizeof (notifiable_events
),
3022 /* This function is called by the parent process, with pid being the
3023 * ID of the child process, after the debugger has forked.
3026 child_acknowledge_created_inferior (pid
)
3029 /* We need a memory home for a constant, to pass it to ttrace.
3030 The value of the constant is arbitrary, so long as both
3031 parent and child use the same value. Might as well use the
3032 "magic" constant provided by ttrace...
3034 uint64_t tc_magic_parent
= TT_VERSION
;
3035 uint64_t tc_magic_child
= 0;
3037 /* Wait for the child to tell us that it has forked. */
3038 read (startup_semaphore
.child_channel
[SEM_LISTEN
],
3040 sizeof(tc_magic_child
));
3042 /* Clear thread info now. We'd like to do this in
3043 * "require...", but that messes up attach.
3045 clear_thread_info();
3047 /* Tell the "rest of gdb" that the initial thread exists.
3048 * This isn't really a hack. Other thread-based versions
3049 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3051 * Q: Why don't we also add this thread to the local
3052 * list via "add_tthread"?
3054 * A: Because we don't know the tid, and can't stop the
3055 * the process safely to ask what it is. Anyway, we'll
3056 * add it when it gets the EXEC event.
3058 add_thread( pid
); /* in thread.c */
3060 /* We can now set the child's ttrace event mask.
3062 require_notification_of_exec_events (pid
);
3064 /* Tell ourselves that the process is running.
3066 process_state
= RUNNING
;
3068 /* Notify the child that it can exec. */
3069 write (startup_semaphore
.parent_channel
[SEM_TALK
],
3071 sizeof (tc_magic_parent
));
3073 /* Discard our copy of the semaphore. */
3074 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
3075 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
3076 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
3077 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
3082 * arrange for notification of all events by
3083 * calling require_notification_of_events.
3086 child_post_startup_inferior ( real_pid
)
3089 require_notification_of_events (real_pid
);
3092 /* From here on, we should expect tids rather than pids.
3095 hppa_enable_catch_fork (tid
)
3099 ttevent_t ttrace_events
;
3101 /* Get the set of events that are currently enabled.
3103 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3105 (TTRACE_ARG_TYPE
) &ttrace_events
,
3106 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3109 perror_with_name ("ttrace");
3111 /* Add forks to that set. */
3112 ttrace_events
.tte_events
|= TTEVT_FORK
;
3116 printf( "enable fork, tid is %d\n", tid
);
3119 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3121 (TTRACE_ARG_TYPE
) &ttrace_events
,
3122 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3125 perror_with_name ("ttrace");
3130 hppa_disable_catch_fork (tid
)
3134 ttevent_t ttrace_events
;
3136 /* Get the set of events that are currently enabled.
3138 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3140 (TTRACE_ARG_TYPE
) &ttrace_events
,
3141 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3145 perror_with_name ("ttrace");
3147 /* Remove forks from that set. */
3148 ttrace_events
.tte_events
&= ~TTEVT_FORK
;
3152 printf("disable fork, tid is %d\n", tid
);
3155 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3157 (TTRACE_ARG_TYPE
) &ttrace_events
,
3158 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3162 perror_with_name ("ttrace");
3166 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3168 child_insert_fork_catchpoint (tid
)
3171 /* Enable reporting of fork events from the kernel. */
3172 /* ??rehrauer: For the moment, we're always enabling these events,
3173 and just ignoring them if there's no catchpoint to catch them.
3180 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3182 child_remove_fork_catchpoint (tid
)
3185 /* Disable reporting of fork events from the kernel. */
3186 /* ??rehrauer: For the moment, we're always enabling these events,
3187 and just ignoring them if there's no catchpoint to catch them.
3195 hppa_enable_catch_vfork (tid
)
3199 ttevent_t ttrace_events
;
3201 /* Get the set of events that are currently enabled.
3203 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3205 (TTRACE_ARG_TYPE
) &ttrace_events
,
3206 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3210 perror_with_name ("ttrace");
3212 /* Add vforks to that set. */
3213 ttrace_events
.tte_events
|= TTEVT_VFORK
;
3217 printf("enable vfork, tid is %d\n", tid
);
3220 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3222 (TTRACE_ARG_TYPE
) &ttrace_events
,
3223 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3227 perror_with_name ("ttrace");
3232 hppa_disable_catch_vfork (tid
)
3236 ttevent_t ttrace_events
;
3238 /* 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
),
3246 perror_with_name ("ttrace");
3248 /* Remove vforks from that set. */
3249 ttrace_events
.tte_events
&= ~TTEVT_VFORK
;
3253 printf("disable vfork, 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
),
3262 perror_with_name ("ttrace");
3266 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3268 child_insert_vfork_catchpoint (tid
)
3271 /* Enable reporting of vfork events from the kernel. */
3272 /* ??rehrauer: For the moment, we're always enabling these events,
3273 and just ignoring them if there's no catchpoint to catch them.
3280 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3282 child_remove_vfork_catchpoint (tid
)
3285 /* Disable reporting of vfork events from the kernel. */
3286 /* ??rehrauer: For the moment, we're always enabling these events,
3287 and just ignoring them if there's no catchpoint to catch them.
3293 #if defined(CHILD_HAS_FORKED)
3295 /* Q: Do we need to map the returned process ID to a thread ID?
3297 * A: I don't think so--here we want a _real_ pid. Any later
3298 * operations will call "require_notification_of_events" and
3299 * start the mapping.
3302 child_has_forked (tid
, childpid
)
3307 ttstate_t ttrace_state
;
3308 thread_info
* tinfo
;
3310 /* Do we have cached thread state that we can consult? If so, use it. */
3311 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3312 if (tinfo
!= NULL
) {
3313 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3316 /* Nope, must read the thread's current state */
3319 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3321 (TTRACE_ARG_TYPE
) &ttrace_state
,
3322 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3326 perror_with_name ("ttrace");
3332 if (ttrace_state
.tts_event
& TTEVT_FORK
)
3334 *childpid
= ttrace_state
.tts_u
.tts_fork
.tts_fpid
;
3343 #if defined(CHILD_HAS_VFORKED)
3345 /* See child_has_forked for pid discussion.
3348 child_has_vforked (tid
, childpid
)
3353 ttstate_t ttrace_state
;
3354 thread_info
* tinfo
;
3356 /* Do we have cached thread state that we can consult? If so, use it. */
3357 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3359 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3361 /* Nope, must read the thread's current state */
3364 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3366 (TTRACE_ARG_TYPE
) &ttrace_state
,
3367 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3371 perror_with_name ("ttrace");
3377 if (ttrace_state
.tts_event
& TTEVT_VFORK
)
3379 *childpid
= ttrace_state
.tts_u
.tts_fork
.tts_fpid
;
3388 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3390 child_can_follow_vfork_prior_to_exec ()
3392 /* ttrace does allow this.
3394 ??rehrauer: However, I had major-league problems trying to
3395 convince wait_for_inferior to handle that case. Perhaps when
3396 it is rewritten to grok multiple processes in an explicit way...
3403 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3405 child_insert_exec_catchpoint (tid
)
3408 /* Enable reporting of exec events from the kernel. */
3409 /* ??rehrauer: For the moment, we're always enabling these events,
3410 and just ignoring them if there's no catchpoint to catch them.
3417 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3419 child_remove_exec_catchpoint (tid
)
3422 /* Disable reporting of execevents from the kernel. */
3423 /* ??rehrauer: For the moment, we're always enabling these events,
3424 and just ignoring them if there's no catchpoint to catch them.
3431 #if defined(CHILD_HAS_EXECD)
3433 child_has_execd (tid
, execd_pathname
)
3435 char ** execd_pathname
;
3438 ttstate_t ttrace_state
;
3439 thread_info
* tinfo
;
3441 /* Do we have cached thread state that we can consult? If so, use it. */
3442 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3444 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3446 /* Nope, must read the thread's current state */
3449 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3451 (TTRACE_ARG_TYPE
) &ttrace_state
,
3452 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3456 perror_with_name ("ttrace");
3462 if (ttrace_state
.tts_event
& TTEVT_EXEC
)
3464 /* See child_pid_to_exec_file in this file: this is a macro.
3466 char * exec_file
= target_pid_to_exec_file (tid
);
3468 *execd_pathname
= savestring (exec_file
, strlen (exec_file
));
3477 #if defined(CHILD_HAS_SYSCALL_EVENT)
3479 child_has_syscall_event (pid
, kind
, syscall_id
)
3481 enum target_waitkind
* kind
;
3485 ttstate_t ttrace_state
;
3486 thread_info
* tinfo
;
3488 /* Do we have cached thread state that we can consult? If so, use it. */
3489 tinfo
= find_thread_info (map_from_gdb_tid (pid
));
3491 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3493 /* Nope, must read the thread's current state */
3496 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3498 (TTRACE_ARG_TYPE
) &ttrace_state
,
3499 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3503 perror_with_name ("ttrace");
3509 *kind
= TARGET_WAITKIND_SPURIOUS
; /* Until proven otherwise... */
3512 if (ttrace_state
.tts_event
& TTEVT_SYSCALL_ENTRY
)
3513 *kind
= TARGET_WAITKIND_SYSCALL_ENTRY
;
3514 else if (ttrace_state
.tts_event
& TTEVT_SYSCALL_RETURN
)
3515 *kind
= TARGET_WAITKIND_SYSCALL_RETURN
;
3519 *syscall_id
= ttrace_state
.tts_scno
;
3526 #if defined(CHILD_THREAD_ALIVE)
3528 /* Check to see if the given thread is alive.
3530 * We'll trust the thread list, as the more correct
3531 * approach of stopping the process and spinning down
3532 * the OS's thread list is _very_ expensive.
3534 * May need a FIXME for that reason.
3537 child_thread_alive (gdb_tid
)
3542 /* This spins down the lists twice.
3543 * Possible peformance improvement here!
3545 tid
= map_from_gdb_tid( gdb_tid
);
3546 return !is_terminated( tid
);
3553 /* This function attempts to read the specified number of bytes from the
3554 save_state_t that is our view into the hardware registers, starting at
3555 ss_offset, and ending at ss_offset + sizeof_buf - 1
3557 If this function succeeds, it deposits the fetched bytes into buf,
3560 If it fails, it returns a negative result. The contents of buf are
3561 undefined it this function fails.
3564 read_from_register_save_state (tid
, ss_offset
, buf
, sizeof_buf
)
3566 TTRACE_ARG_TYPE ss_offset
;
3571 register_value_t register_value
= 0;
3573 tt_status
= call_ttrace (TT_LWP_RUREGS
,
3576 (TTRACE_ARG_TYPE
) sizeof_buf
,
3577 (TTRACE_ARG_TYPE
) buf
);
3579 if( tt_status
== 1 )
3580 /* Map ttrace's version of success to our version.
3581 * Sometime ttrace returns 0, but that's ok here.
3589 /* This function attempts to write the specified number of bytes to the
3590 save_state_t that is our view into the hardware registers, starting at
3591 ss_offset, and ending at ss_offset + sizeof_buf - 1
3593 If this function succeeds, it deposits the bytes in buf, and returns 0.
3595 If it fails, it returns a negative result. The contents of the save_state_t
3596 are undefined it this function fails.
3599 write_to_register_save_state (tid
, ss_offset
, buf
, sizeof_buf
)
3601 TTRACE_ARG_TYPE ss_offset
;
3606 register_value_t register_value
= 0;
3608 tt_status
= call_ttrace (TT_LWP_WUREGS
,
3611 (TTRACE_ARG_TYPE
) sizeof_buf
,
3612 (TTRACE_ARG_TYPE
) buf
);
3617 /* This function is a sop to the largeish number of direct calls
3618 to call_ptrace that exist in other files. Rather than create
3619 functions whose name abstracts away from ptrace, and change all
3620 the present callers of call_ptrace, we'll do the expedient (and
3621 perhaps only practical) thing.
3623 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3624 process. Thus, we must translate all ptrace requests into their
3625 process-specific, ttrace equivalents.
3628 call_ptrace (pt_request
, gdb_tid
, addr
, data
)
3631 PTRACE_ARG3_TYPE addr
;
3635 TTRACE_ARG_TYPE tt_addr
= (TTRACE_ARG_TYPE
) addr
;
3636 TTRACE_ARG_TYPE tt_data
= (TTRACE_ARG_TYPE
) data
;
3637 TTRACE_ARG_TYPE tt_addr2
= TT_NIL
;
3639 register_value_t register_value
;
3642 /* Perform the necessary argument translation. Note that some
3643 cases are funky enough in the ttrace realm that we handle them
3646 switch (pt_request
) {
3647 /* The following cases cannot conveniently be handled conveniently
3648 by merely adjusting the ptrace arguments and feeding into the
3649 generic call to ttrace at the bottom of this function.
3651 Note that because all branches of this switch end in "return",
3652 there's no need for any "break" statements.
3655 return parent_attach_all ();
3658 tt_status
= read_from_register_save_state (gdb_tid
,
3661 sizeof (register_value
));
3664 return register_value
;
3667 register_value
= (int) tt_data
;
3668 tt_status
= write_to_register_save_state (gdb_tid
,
3671 sizeof (register_value
));
3676 tt_status
= call_ttrace (TT_PROC_RDTEXT
, /* Implicit 4-byte xfer becomes block-xfer. */
3679 (TTRACE_ARG_TYPE
) 4,
3680 (TTRACE_ARG_TYPE
) &read_buf
);
3686 tt_status
= call_ttrace (TT_PROC_RDDATA
, /* Implicit 4-byte xfer becomes block-xfer. */
3689 (TTRACE_ARG_TYPE
) 4,
3690 (TTRACE_ARG_TYPE
) &read_buf
);
3696 tt_status
= call_real_ttrace (TT_PROC_ATTACH
,
3697 map_from_gdb_tid (gdb_tid
),
3700 (TTRACE_ARG_TYPE
) TT_VERSION
,
3706 /* The following cases are handled by merely adjusting the ptrace
3707 arguments and feeding into the generic call to ttrace.
3710 tt_request
= TT_PROC_DETACH
;
3714 tt_request
= TT_PROC_WRTEXT
; /* Translates 4-byte xfer to block-xfer. */
3715 tt_data
= 4; /* This many bytes. */
3716 tt_addr2
= (TTRACE_ARG_TYPE
) &data
; /* Address of xfer source. */
3720 tt_request
= TT_PROC_WRDATA
; /* Translates 4-byte xfer to block-xfer. */
3721 tt_data
= 4; /* This many bytes. */
3722 tt_addr2
= (TTRACE_ARG_TYPE
) &data
; /* Address of xfer source. */
3726 tt_request
= TT_PROC_RDTEXT
;
3730 tt_request
= TT_PROC_RDDATA
;
3734 tt_request
= TT_PROC_WRTEXT
;
3738 tt_request
= TT_PROC_WRDATA
;
3742 tt_request
= TT_PROC_CONTINUE
;
3746 tt_request
= TT_LWP_SINGLE
; /* Should not be making this request? */
3750 tt_request
= TT_PROC_EXIT
;
3753 case PT_GET_PROCESS_PATHNAME
:
3754 tt_request
= TT_PROC_GET_PATHNAME
;
3758 tt_request
= pt_request
; /* Let ttrace be the one to complain. */
3762 return call_ttrace (tt_request
,
3769 /* Kill that pesky process!
3777 thread_info
**paranoia
;
3780 if (inferior_pid
== 0)
3783 /* Walk the list of "threads", some of which are "pseudo threads",
3784 aka "processes". For each that is NOT inferior_pid, stop it,
3787 You see, we may not have just a single process to kill. If we're
3788 restarting or quitting or detaching just after the inferior has
3789 forked, then we've actually two processes to clean up.
3791 But we can't just call target_mourn_inferior() for each, since that
3792 zaps the target vector.
3795 paranoia
= (thread_info
**) malloc( thread_head
.count
*
3796 sizeof(thread_info
*));
3799 t
= thread_head
.head
;
3802 paranoia
[ para_count
] = t
;
3803 for( i
= 0; i
< para_count
; i
++ ){
3804 if( t
->next
== paranoia
[i
] ) {
3805 warning( "Bad data in gdb's thread data; repairing." );
3811 if (t
->am_pseudo
&& (t
->pid
!= inferior_pid
))
3813 /* TT_PROC_STOP doesn't require a subsequent ttrace_wait, as it
3814 * generates no event.
3816 call_ttrace (TT_PROC_STOP
,
3822 call_ttrace (TT_PROC_DETACH
,
3825 (TTRACE_ARG_TYPE
) TARGET_SIGNAL_0
,
3833 call_ttrace (TT_PROC_STOP
,
3838 target_mourn_inferior ();
3839 clear_thread_info();
3843 #ifndef CHILD_RESUME
3845 /* Sanity check a thread about to be continued.
3848 thread_dropping_event_check( p
)
3853 * This seems to happen when we "next" over a
3854 * "fork()" while following the parent. If it's
3855 * the FORK event, that's ok. If it's a SIGNAL
3856 * in the unfollowed child, that's ok to--but
3857 * how can we know that's what's going on?
3861 if( p
->have_state
) {
3862 if( p
->last_stop_state
.tts_event
== TTEVT_FORK
) {
3866 else if( p
->last_stop_state
.tts_event
== TTEVT_SIGNAL
) {
3867 /* Ok, close eyes and let it happen.
3872 /* This shouldn't happen--we're dropping a
3875 warning( "About to continue process %d, thread %d with unhandled event %s.",
3877 get_printable_name_of_ttrace_event(
3878 p
->last_stop_state
.tts_event
));
3887 /* No saved state, have to assume it failed.
3889 warning( "About to continue process %d, thread %d with unhandled event.",
3898 } /* thread_dropping_event_check */
3900 /* Use a loop over the threads to continue all the threads but
3901 * the one specified, which is to be stepped.
3904 threads_continue_all_but_one( gdb_tid
, signal
)
3917 printf( "Using loop over threads to step/resume with signals\n" );
3920 /* First update the thread list.
3923 real_tid
= map_from_gdb_tid( gdb_tid
);
3924 real_pid
= get_pid_for( real_tid
);
3926 scan_tid
= get_process_first_stopped_thread_id( real_pid
, &state
);
3927 while ( 0 != scan_tid
) {
3930 /* FIX: later should check state is stopped;
3931 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
3934 if( state
.tts_flags
& TTS_STATEMASK
!= TTS_WASSUSPENDED
)
3935 printf( "About to continue non-stopped thread %d\n", scan_tid
);
3938 p
= find_thread_info( scan_tid
);
3940 add_tthread( real_pid
, scan_tid
);
3941 p
= find_thread_info( scan_tid
);
3943 /* This is either a newly-created thread or the
3944 * result of a fork; in either case there's no
3945 * actual event to worry about.
3949 if( state
.tts_event
!= TTEVT_NONE
) {
3950 /* Oops, do need to worry!
3952 warning( "Unexpected thread with \"%s\" event.",
3953 get_printable_name_of_ttrace_event( state
.tts_event
));
3956 else if( scan_tid
!= p
->tid
)
3957 error( "Bad data in thread database." );
3962 printf( "Why are we continuing a dead thread?\n" );
3967 scan_tid
= get_process_next_stopped_thread_id( real_pid
, &state
);
3970 /* Remove unseen threads.
3972 update_thread_list();
3974 /* Now run down the thread list and continue or step.
3976 for( p
= thread_head
.head
; p
; p
= p
->next
) {
3980 thread_dropping_event_check( p
);
3982 /* Pass the correct signals along.
3984 if( p
->have_signal
) {
3985 thread_signal
= p
->signal_value
;
3991 if( p
->tid
!= real_tid
) {
3993 * Not the thread of interest, so continue it
3994 * as the user expects.
3996 if( p
->stepping_mode
== DO_STEP
) {
3997 /* Just step this thread.
4003 (TTRACE_ARG_TYPE
) target_signal_to_host( signal
),
4007 /* Regular continue (default case).
4013 (TTRACE_ARG_TYPE
) target_signal_to_host( thread_signal
),
4018 /* Step the thread of interest.
4024 (TTRACE_ARG_TYPE
) target_signal_to_host( signal
),
4027 } /* Loop over threads */
4028 } /* End threads_continue_all_but_one */
4030 /* Use a loop over the threads to continue all the threads.
4031 * This is done when a signal must be sent to any of the threads.
4034 threads_continue_all_with_signals( gdb_tid
, signal
)
4047 printf( "Using loop over threads to resume with signals\n" );
4050 /* Scan and update thread list.
4053 real_tid
= map_from_gdb_tid( gdb_tid
);
4054 real_pid
= get_pid_for( real_tid
);
4056 scan_tid
= get_process_first_stopped_thread_id( real_pid
, &state
);
4057 while ( 0 != scan_tid
) {
4061 if( state
.tts_flags
& TTS_STATEMASK
!= TTS_WASSUSPENDED
)
4062 warning( "About to continue non-stopped thread %d\n", scan_tid
);
4065 p
= find_thread_info( scan_tid
);
4067 add_tthread( real_pid
, scan_tid
);
4068 p
= find_thread_info( scan_tid
);
4070 /* This is either a newly-created thread or the
4071 * result of a fork; in either case there's no
4072 * actual event to worry about.
4076 if( state
.tts_event
!= TTEVT_NONE
) {
4077 /* Oops, do need to worry!
4079 warning( "Unexpected thread with \"%s\" event.",
4080 get_printable_name_of_ttrace_event( state
.tts_event
));
4087 printf( "Why are we continuing a dead thread? (1)\n" );
4092 scan_tid
= get_process_next_stopped_thread_id( real_pid
, &state
);
4095 /* Remove unseen threads from our list.
4097 update_thread_list();
4099 /* Continue the threads.
4101 for( p
= thread_head
.head
; p
; p
= p
->next
) {
4105 thread_dropping_event_check( p
);
4107 /* Pass the correct signals along.
4109 if( p
->tid
== real_tid
) {
4110 thread_signal
= signal
;
4113 else if( p
->have_signal
) {
4114 thread_signal
= p
->signal_value
;
4120 if( p
->stepping_mode
== DO_STEP
) {
4125 (TTRACE_ARG_TYPE
) target_signal_to_host( signal
),
4129 /* Continue this thread (default case).
4135 (TTRACE_ARG_TYPE
) target_signal_to_host( thread_signal
),
4139 } /* End threads_continue_all_with_signals */
4141 /* Step one thread only.
4144 thread_fake_step( tid
, signal
)
4146 enum target_signal signal
;
4152 printf( "Doing a fake-step over a bpt, etc. for %d\n", tid
);
4154 if( is_terminated( tid
))
4155 printf( "Why are we continuing a dead thread? (4)\n" );
4159 if( doing_fake_step
)
4160 warning( "Step while step already in progress." );
4162 /* See if there's a saved signal value for this
4163 * thread to be passed on, but no current signal.
4165 p
= find_thread_info( tid
);
4167 if( p
->have_signal
&& signal
== NULL
) {
4168 /* Pass on a saved signal.
4170 signal
= p
->signal_value
;
4177 warning( "Internal error: continuing unhandled thread." );
4179 call_ttrace( TT_LWP_SINGLE
,
4182 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4185 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4186 * for this thread only, and clear any saved signal info.
4188 doing_fake_step
= 1;
4189 fake_step_tid
= tid
;
4191 } /* End thread_fake_step */
4193 /* Continue one thread when a signal must be sent to it.
4196 threads_continue_one_with_signal( gdb_tid
, signal
)
4206 printf( "Continuing one thread with a signal\n" );
4209 real_tid
= map_from_gdb_tid( gdb_tid
);
4210 real_pid
= get_pid_for( real_tid
);
4212 p
= find_thread_info( real_tid
);
4214 add_tthread( real_pid
, real_tid
);
4220 printf( "Why are we continuing a dead thread? (2)\n" );
4224 warning( "Internal error: continuing unhandled thread." );
4228 call_ttrace( TT_LWP_CONTINUE
,
4231 (TTRACE_ARG_TYPE
) target_signal_to_host( signal
),
4236 #ifndef CHILD_RESUME
4238 /* Resume execution of the inferior process.
4240 * This routine is in charge of setting the "handled" bits.
4242 * If STEP is zero, continue it.
4243 * If STEP is nonzero, single-step it.
4245 * If SIGNAL is nonzero, give it that signal.
4247 * If TID is -1, apply to all threads.
4248 * If TID is not -1, apply to specified thread.
4252 * TID \________________________________________________
4254 * -1 | Step current Continue all threads
4255 * | thread and (but which gets any
4256 * | continue others signal?--We look at
4259 * N | Step _this_ thread Continue _this_ thread
4260 * | and leave others and leave others
4261 * | stopped; internally stopped; used only for
4262 * | used by gdb, never hardware watchpoints
4263 * | a user command. and attach, never a
4267 child_resume( gdb_tid
, step
, signal
)
4270 enum target_signal signal
;
4272 int resume_all_threads
;
4274 process_state_t new_process_state
;
4276 resume_all_threads
=
4277 (gdb_tid
== INFTTRACE_ALL_THREADS
) ||
4280 if (resume_all_threads
) {
4281 /* Resume all threads, but first pick a tid value
4282 * so we can get the pid when in call_ttrace doing
4285 if (vfork_in_flight
)
4286 tid
= vforking_child_pid
;
4288 tid
= map_from_gdb_tid( inferior_pid
);
4291 tid
= map_from_gdb_tid( gdb_tid
);
4295 if( more_events_left
)
4296 printf( "More events; " );
4299 printf( "Sending signal %d; ", signal
);
4301 if( resume_all_threads
) {
4303 printf( "Continue process %d\n", tid
);
4305 printf( "Step/continue thread %d\n", tid
);
4309 printf( "Continue thread %d\n", tid
);
4311 printf( "Step just thread %d\n", tid
);
4314 if( vfork_in_flight
)
4315 printf( "Vfork in flight\n" );
4319 if( process_state
== RUNNING
)
4320 warning( "Internal error in resume logic; doing resume or step anyway." );
4322 if( !step
/* Asked to continue... */
4323 && resume_all_threads
/* whole process.. */
4324 && signal
!= 0 /* with a signal... */
4325 && more_events_left
> 0 ) { /* but we can't yet--save it! */
4327 /* Continue with signal means we have to set the pending
4328 * signal value for this thread.
4334 printf( "Saving signal %d for thread %d\n", signal
, tid
);
4337 k
= find_thread_info( tid
);
4340 k
->signal_value
= signal
;
4345 printf( "Why are we continuing a dead thread? (3)\n" );
4351 else if( debug_on
) {
4352 printf( "No thread info for tid %d\n", tid
);
4357 /* Are we faking this "continue" or "step"?
4359 * We used to do steps by continuing all the threads for
4360 * which the events had been handled already. While
4361 * conceptually nicer (hides it all in a lower level), this
4362 * can lead to starvation and a hang (e.g. all but one thread
4363 * are unhandled at a breakpoint just before a "join" operation,
4364 * and one thread is in the join, and the user wants to step that
4367 if( resume_all_threads
/* Whole process, therefore user command */
4368 && more_events_left
> 0 ) { /* But we can't do this yet--fake it! */
4372 /* No need to do any notes on a per-thread
4373 * basis--we're done!
4375 #ifdef WAIT_BUFFER_DEBUG
4377 printf( "Faking a process resume.\n" );
4384 #ifdef WAIT_BUFFER_DEBUG
4386 printf( "Faking a process step.\n" );
4391 p
= find_thread_info( tid
);
4393 warning( "No thread information for tid %d, 'next' command ignored.\n", tid
);
4401 printf( "Why are we continuing a dead thread? (3.5)\n" );
4404 if( p
->stepping_mode
!= DO_DEFAULT
) {
4405 warning( "Step or continue command applied to thread which is already stepping or continuing; command ignored." );
4411 p
->stepping_mode
= DO_STEP
;
4413 p
->stepping_mode
= DO_CONTINUE
;
4416 } /* Have thread info */
4417 } /* Must fake step or go */
4419 /* Execept for fake-steps, from here on we know we are
4420 * going to wind up with a running process which will
4423 new_process_state
= RUNNING
;
4425 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4426 * it was. (If GDB wanted it to start some other way, we have already
4427 * written a new PC value to the child.)
4429 * If this system does not support PT_STEP, a higher level function will
4430 * have called single_step() to transmute the step request into a
4431 * continue request (by setting breakpoints on all possible successor
4432 * instructions), so we don't have to worry about that here.
4435 if( resume_all_threads
) {
4437 * Regular user step: other threads get a "continue".
4439 threads_continue_all_but_one( tid
, signal
);
4440 clear_all_handled();
4441 clear_all_stepping_mode();
4445 /* "Fake step": gdb is stepping one thread over a
4446 * breakpoint, watchpoint, or out of a library load
4447 * event, etc. The rest just stay where they are.
4449 * Also used when there are pending events: we really
4450 * step the current thread, but leave the rest stopped.
4451 * Users can't request this, but "wait_for_inferior"
4454 thread_fake_step( tid
, signal
);
4456 /* Clear the "handled" state of this thread, because
4457 * we'll soon get a new event for it. Other events
4458 * stay as they were.
4460 clear_handled( tid
);
4461 clear_stepping_mode( tid
);
4462 new_process_state
= FAKE_STEPPING
;
4467 /* TT_LWP_CONTINUE can pass signals to threads,
4468 * TT_PROC_CONTINUE can't. So if there are any
4469 * signals to pass, we have to use the (slower)
4470 * loop over the stopped threads.
4472 * Equally, if we have to not continue some threads,
4473 * due to saved events, we have to use the loop.
4475 if( (signal
!= 0) || saved_signals_exist()) {
4476 if( resume_all_threads
) {
4480 printf( "Doing a continue by loop of all threads\n" );
4483 threads_continue_all_with_signals( tid
, signal
);
4485 clear_all_handled();
4486 clear_all_stepping_mode();
4491 printf( "Doing a continue w/signal of just thread %d\n", tid
);
4494 threads_continue_one_with_signal( tid
, signal
);
4496 /* Clear the "handled" state of this thread, because
4497 * we'll soon get a new event for it. Other events
4498 * can stay as they were.
4500 clear_handled( tid
);
4501 clear_stepping_mode( tid
);
4506 /* No signals to send.
4508 if( resume_all_threads
) {
4511 printf( "Doing a continue by process of process %d\n", tid
);
4514 if( more_events_left
> 0 ) {
4515 warning( "Losing buffered events on continue." );
4516 more_events_left
= 0;
4519 call_ttrace( TT_PROC_CONTINUE
,
4525 clear_all_handled();
4526 clear_all_stepping_mode();
4532 printf( "Doing a continue of just thread %d\n", tid
);
4533 if( is_terminated( tid
))
4534 printf( "Why are we continuing a dead thread? (5)\n" );
4538 call_ttrace( TT_LWP_CONTINUE
,
4544 /* Clear the "handled" state of this thread, because
4545 * we'll soon get a new event for it. Other events
4546 * can stay as they were.
4548 clear_handled( tid
);
4549 clear_stepping_mode( tid
);
4554 process_state
= new_process_state
;
4556 #ifdef WAIT_BUFFER_DEBUG
4558 printf( "Process set to %s\n",
4559 get_printable_name_of_process_state (process_state
) );
4563 #endif /* CHILD_RESUME */
4566 #ifdef ATTACH_DETACH
4570 * One worry is that we may not be attaching to "inferior_pid"
4571 * and thus may not want to clear out our data. FIXME?
4575 update_thread_state_after_attach( pid
, kind_of_go
)
4577 attach_continue_t kind_of_go
;
4580 ttstate_t thread_state
;
4584 /* The process better be stopped.
4586 if( process_state
!= STOPPED
4587 && process_state
!= VFORKING
)
4588 warning( "Internal error attaching." );
4590 /* Clear out old tthread info and start over. This has the
4591 * side effect of ensuring that the TRAP is reported as being
4592 * in the right thread (re-mapped from tid to pid).
4594 * It's because we need to add the tthread _now_ that we
4595 * need to call "clear_thread_info" _now_, and that's why
4596 * "require_notification_of_events" doesn't clear the thread
4597 * info (it's called later than this routine).
4599 clear_thread_info();
4602 for (tid
= get_process_first_stopped_thread_id (pid
, &thread_state
);
4604 tid
= get_process_next_stopped_thread_id (pid
, &thread_state
))
4613 printf( "Attaching to process %d, thread %d\n",
4618 /* Tell ourselves and the "rest of gdb" that this thread
4621 * This isn't really a hack. Other thread-based versions
4622 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4624 * We don't need to do mapping here, as we know this
4625 * is the first thread and thus gets the real pid
4626 * (and is "inferior_pid").
4628 * NOTE: it probably isn't the originating thread,
4629 * but that doesn't matter (we hope!).
4631 add_tthread( pid
, tid
);
4632 p
= find_thread_info( tid
);
4633 if( NULL
== p
) /* ?We just added it! */
4634 error( "Internal error adding a thread on attach." );
4636 copy_ttstate_t( &p
->last_stop_state
, thread_state
);
4639 if( DO_ATTACH_CONTINUE
== kind_of_go
) {
4641 * If we are going to CONTINUE afterwards,
4642 * raising a SIGTRAP, don't bother trying to
4643 * handle this event. But check first!
4645 switch( p
->last_stop_state
.tts_event
) {
4648 /* Ok to set this handled.
4653 warning( "Internal error; skipping event %s on process %d, thread %d.",
4654 get_printable_name_of_ttrace_event(
4655 p
->last_stop_state
.tts_event
),
4659 set_handled( pid
, tid
);
4663 /* There will be no "continue" opertion, so the
4664 * process remains stopped. Don't set any events
4665 * handled except the "gimmies".
4667 switch( p
->last_stop_state
.tts_event
) {
4670 /* Ok to ignore this.
4672 set_handled( pid
, tid
);
4677 /* Expected "other" FORK or EXEC event from a
4683 printf( "Internal error: failed to handle event %s on process %d, thread %d.",
4684 get_printable_name_of_ttrace_event(
4685 p
->last_stop_state
.tts_event
),
4690 add_thread( tid
); /* in thread.c */
4698 /* One mustn't call ttrace_wait() after attaching via ttrace,
4699 'cause the process is stopped already.
4701 However, the upper layers of gdb's execution control will
4702 want to wait after attaching (but not after forks, in
4703 which case they will be doing a "target_resume", anticipating
4704 a later TTEVT_EXEC or TTEVT_FORK event).
4706 To make this attach() implementation more compatible with
4707 others, we'll make the attached-to process raise a SIGTRAP.
4709 Issue: this continues only one thread. That could be
4710 dangerous if the thread is blocked--the process won't run
4711 and no trap will be raised. FIX! (check state.tts_flags?
4712 need one that's either TTS_WASRUNNING--but we've stopped
4713 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4715 if( DO_ATTACH_CONTINUE
== kind_of_go
) {
4716 tt_status
= call_real_ttrace(
4721 (TTRACE_ARG_TYPE
) target_signal_to_host (TARGET_SIGNAL_TRAP
),
4724 perror_with_name ("ttrace");
4726 clear_handled( a_thread
); /* So TRAP will be reported. */
4730 process_state
= RUNNING
;
4735 #endif /* ATTACH_DETACH */
4738 #ifdef ATTACH_DETACH
4739 /* Start debugging the process whose number is PID.
4748 tt_status
= call_real_ttrace (
4753 (TTRACE_ARG_TYPE
) TT_VERSION
,
4756 perror_with_name ("ttrace attach");
4758 /* If successful, the process is now stopped.
4760 process_state
= STOPPED
;
4762 /* Our caller ("attach_command" in "infcmd.c")
4763 * expects to do a "wait_for_inferior" after
4764 * the attach, so make sure the inferior is
4765 * running when we're done.
4767 update_thread_state_after_attach( pid
, DO_ATTACH_CONTINUE
);
4773 #if defined(CHILD_POST_ATTACH)
4775 child_post_attach (pid
)
4780 printf( "child-post-attach call\n" );
4783 require_notification_of_events (pid
);
4788 /* Stop debugging the process whose number is PID
4789 and continue it with signal number SIGNAL.
4790 SIGNAL = 0 means just continue it.
4797 call_ttrace (TT_PROC_DETACH
,
4800 (TTRACE_ARG_TYPE
) signal
,
4804 clear_thread_info();
4806 /* Process-state? */
4808 #endif /* ATTACH_DETACH */
4811 /* Default the type of the ttrace transfer to int. */
4812 #ifndef TTRACE_XFER_TYPE
4813 #define TTRACE_XFER_TYPE int
4817 _initialize_kernel_u_addr ()
4821 #if !defined (CHILD_XFER_MEMORY)
4822 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
4823 in the NEW_SUN_TTRACE case.
4824 It ought to be straightforward. But it appears that writing did
4825 not write the data that I specified. I cannot understand where
4826 it got the data that it actually did write. */
4828 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
4829 to debugger memory starting at MYADDR. Copy to inferior if
4832 Returns the length copied, which is either the LEN argument or zero.
4833 This xfer function does not do partial moves, since child_ops
4834 doesn't allow memory operations to cross below us in the target stack
4838 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
4843 struct target_ops
*target
; /* ignored */
4846 /* Round starting address down to longword boundary. */
4847 register CORE_ADDR addr
= memaddr
& - sizeof (TTRACE_XFER_TYPE
);
4848 /* Round ending address up; get number of longwords that makes. */
4850 = (((memaddr
+ len
) - addr
) + sizeof (TTRACE_XFER_TYPE
) - 1)
4851 / sizeof (TTRACE_XFER_TYPE
);
4852 /* Allocate buffer of that many longwords. */
4853 register TTRACE_XFER_TYPE
*buffer
4854 = (TTRACE_XFER_TYPE
*) alloca (count
* sizeof (TTRACE_XFER_TYPE
));
4858 /* Fill start and end extra bytes of buffer with existing memory data. */
4860 if (addr
!= memaddr
|| len
< (int) sizeof (TTRACE_XFER_TYPE
)) {
4861 /* Need part of initial word -- fetch it. */
4862 buffer
[0] = call_ttrace (TT_LWP_RDTEXT
,
4864 (TTRACE_ARG_TYPE
) addr
,
4869 if (count
> 1) /* FIXME, avoid if even boundary */
4871 buffer
[count
- 1] = call_ttrace (TT_LWP_RDTEXT
,
4874 (addr
+ (count
- 1) * sizeof (TTRACE_XFER_TYPE
))),
4879 /* Copy data to be written over corresponding part of buffer */
4881 memcpy ((char *) buffer
+ (memaddr
& (sizeof (TTRACE_XFER_TYPE
) - 1)),
4885 /* Write the entire buffer. */
4887 for (i
= 0; i
< count
; i
++, addr
+= sizeof (TTRACE_XFER_TYPE
))
4890 call_ttrace (TT_LWP_WRDATA
,
4892 (TTRACE_ARG_TYPE
) addr
,
4893 (TTRACE_ARG_TYPE
) buffer
[i
],
4897 /* Using the appropriate one (I or D) is necessary for
4898 Gould NP1, at least. */
4900 call_ttrace (TT_LWP_WRTEXT
,
4902 (TTRACE_ARG_TYPE
) addr
,
4903 (TTRACE_ARG_TYPE
) buffer
[i
],
4912 /* Read all the longwords */
4913 for (i
= 0; i
< count
; i
++, addr
+= sizeof (TTRACE_XFER_TYPE
))
4916 buffer
[i
] = call_ttrace (TT_LWP_RDTEXT
,
4918 (TTRACE_ARG_TYPE
) addr
,
4926 /* Copy appropriate bytes out of the buffer. */
4928 (char *) buffer
+ (memaddr
& (sizeof (TTRACE_XFER_TYPE
) - 1)),
4938 int udot_off
; /* Offset into user struct */
4939 int udot_val
; /* Value from user struct at udot_off */
4940 char mess
[128]; /* For messages */
4942 if (!target_has_execution
)
4944 error ("The program is not being run.");
4947 #if !defined (KERNEL_U_SIZE)
4949 /* Adding support for this command is easy. Typically you just add a
4950 routine, called "kernel_u_size" that returns the size of the user
4951 struct, to the appropriate *-nat.c file and then add to the native
4952 config file "#define KERNEL_U_SIZE kernel_u_size()" */
4953 error ("Don't know how large ``struct user'' is in this version of gdb.");
4957 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
4959 if ((udot_off
% 24) == 0)
4963 printf_filtered ("\n");
4965 printf_filtered ("%04x:", udot_off
);
4967 udot_val
= call_ttrace (TT_LWP_RUREGS
,
4969 (TTRACE_ARG_TYPE
) udot_off
,
4974 sprintf (mess
, "\nreading user struct at offset 0x%x", udot_off
);
4975 perror_with_name (mess
);
4977 /* Avoid using nonportable (?) "*" in print specs */
4978 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
4980 printf_filtered ("\n");
4984 #endif /* !defined (CHILD_XFER_MEMORY). */
4986 /* TTrace version of "target_pid_to_exec_file"
4989 child_pid_to_exec_file (tid
)
4992 static char exec_file_buffer
[1024];
4994 CORE_ADDR top_of_stack
;
4999 int saved_inferior_pid
;
5001 /* As of 10.x HP-UX, there's an explicit request to get the
5004 tt_status
= call_ttrace (TT_PROC_GET_PATHNAME
,
5006 (TTRACE_ARG_TYPE
) exec_file_buffer
,
5007 (TTRACE_ARG_TYPE
) sizeof (exec_file_buffer
) - 1,
5010 return exec_file_buffer
;
5012 /* ??rehrauer: The above request may or may not be broken. It
5013 doesn't seem to work when I use it. But, it may be designed
5014 to only work immediately after an exec event occurs. (I'm
5015 waiting for COSL to explain.)
5017 In any case, if it fails, try a really, truly amazingly gross
5018 hack that DDE uses, of pawing through the process' data
5019 segment to find the pathname.
5021 top_of_stack
= 0x7b03a000;
5025 /* On the chance that pid != inferior_pid, set inferior_pid
5026 to pid, so that (grrrr!) implicit uses of inferior_pid get
5029 saved_inferior_pid
= inferior_pid
;
5032 /* Try to grab a null-terminated string. */
5034 if (target_read_memory (top_of_stack
, four_chars
, 4) != 0)
5036 inferior_pid
= saved_inferior_pid
;
5039 for (i
= 0; i
< 4; i
++) {
5040 exec_file_buffer
[name_index
++] = four_chars
[i
];
5041 done
= (four_chars
[i
] == '\0');
5048 if (exec_file_buffer
[0] == '\0')
5050 inferior_pid
= saved_inferior_pid
;
5054 inferior_pid
= saved_inferior_pid
;
5055 return exec_file_buffer
;
5060 pre_fork_inferior ()
5064 status
= pipe (startup_semaphore
.parent_channel
);
5066 warning ("error getting parent pipe for startup semaphore");
5070 status
= pipe (startup_semaphore
.child_channel
);
5072 warning ("error getting child pipe for startup semaphore");
5077 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5078 * ultimately from "follow_inferior_fork" in infrun.c,
5079 * itself called from "resume".
5081 * This seems to be intended to attach after a fork or
5082 * vfork, while "attach" is used to attach to a pid
5083 * given by the user. The check for an existing attach
5084 * seems odd--it always fails in our test system.
5087 hppa_require_attach (pid
)
5093 unsigned int regs_offset
;
5094 process_state_t old_process_state
= process_state
;
5096 /* Are we already attached? There appears to be no explicit
5097 * way to answer this via ttrace, so we try something which
5098 * should be innocuous if we are attached. If that fails,
5099 * then we assume we're not attached, and so attempt to make
5103 tt_status
= call_real_ttrace (TT_PROC_STOP
,
5106 (TTRACE_ARG_TYPE
) TT_NIL
,
5107 (TTRACE_ARG_TYPE
) TT_NIL
,
5112 /* No change to process-state!
5119 /* If successful, the process is now stopped. But if
5120 * we're VFORKING, the parent is still running, so don't
5121 * change the process state.
5123 if( process_state
!= VFORKING
)
5124 process_state
= STOPPED
;
5126 /* If we were already attached, you'd think that we
5127 * would need to start going again--but you'd be wrong,
5128 * as the fork-following code is actually in the middle
5129 * of the "resume" routine in in "infrun.c" and so
5130 * will (almost) immediately do a resume.
5132 * On the other hand, if we are VFORKING, which means
5133 * that the child and the parent share a process for a
5134 * while, we know that "resume" won't be resuming
5135 * until the child EXEC event is seen. But we still
5136 * don't want to continue, as the event is already
5139 update_thread_state_after_attach( pid
, DONT_ATTACH_CONTINUE
);
5140 } /* STOP succeeded */
5146 hppa_require_detach (pid
, signal
)
5152 /* If signal is non-zero, we must pass the signal on to the active
5153 thread prior to detaching. We do this by continuing the threads
5159 threads_continue_all_with_signals( pid
, signal
);
5163 tt_status
= call_ttrace (TT_PROC_DETACH
,
5169 errno
= 0; /* Ignore any errors. */
5171 /* process_state? */
5176 #if defined(HPPA_GET_PROCESS_EVENTS)
5177 process_event_vector
5178 hppa_get_process_events (pid
, wait_status
, must_continue_pid_after
)
5181 int * must_continue_pid_after
;
5184 ttstate_t ttrace_state
;
5185 process_event_vector events
= PEVT_NONE
;
5187 /* This is always 1 with ptrace. */
5188 *must_continue_pid_after
= 0;
5191 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
5193 (TTRACE_ARG_TYPE
) &ttrace_state
,
5194 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
5197 perror_with_name ("ttrace");
5201 if (ttrace_state
.tts_event
& TTEVT_SIGNAL
)
5202 events
|= PEVT_SIGNAL
;
5203 if (ttrace_state
.tts_event
& TTEVT_FORK
)
5204 events
|= PEVT_FORK
;
5205 if (ttrace_state
.tts_event
& TTEVT_VFORK
)
5206 events
|= PEVT_VFORK
;
5207 if (ttrace_state
.tts_event
& TTEVT_EXEC
)
5208 events
|= PEVT_EXEC
;
5209 if (ttrace_state
.tts_event
& TTEVT_EXIT
)
5210 events
|= PEVT_EXIT
;
5214 #endif /* HPPA_GET_PROCESS_EVENTS */
5217 /* Given the starting address of a memory page, hash it to a bucket in
5218 the memory page dictionary.
5221 get_dictionary_bucket_of_page (page_start
)
5222 CORE_ADDR page_start
;
5226 hash
= (page_start
/ memory_page_dictionary
.page_size
);
5227 hash
= hash
% MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
;
5233 /* Given a memory page's starting address, get (i.e., find an existing
5234 or create a new) dictionary entry for the page. The page will be
5235 write-protected when this function returns, but may have a reference
5236 count of 0 (if the page was newly-added to the dictionary).
5238 static memory_page_t
*
5239 get_dictionary_entry_of_page (pid
, page_start
)
5241 CORE_ADDR page_start
;
5244 memory_page_t
* page
= NULL
;
5245 memory_page_t
* previous_page
= NULL
;
5247 /* We're going to be using the dictionary now, than-kew. */
5248 require_memory_page_dictionary (pid
);
5250 /* Try to find an existing dictionary entry for this page. Hash
5251 on the page's starting address.
5253 bucket
= get_dictionary_bucket_of_page (page_start
);
5254 page
= &memory_page_dictionary
.buckets
[bucket
];
5255 while (page
!= NULL
)
5257 if (page
->page_start
== page_start
)
5259 previous_page
= page
;
5263 /* Did we find a dictionary entry for this page? If not, then
5264 add it to the dictionary now.
5268 /* Create a new entry. */
5269 page
= (memory_page_t
*) xmalloc (sizeof (memory_page_t
));
5270 page
->page_start
= page_start
;
5271 page
->reference_count
= 0;
5273 page
->previous
= NULL
;
5275 /* We'll write-protect the page now, if that's allowed. */
5276 page
->original_permissions
= write_protect_page (pid
, page_start
);
5278 /* Add the new entry to the dictionary. */
5279 page
->previous
= previous_page
;
5280 previous_page
->next
= page
;
5282 memory_page_dictionary
.page_count
++;
5290 remove_dictionary_entry_of_page (pid
, page
)
5292 memory_page_t
* page
;
5294 /* Restore the page's original permissions. */
5295 unwrite_protect_page (pid
, page
->page_start
, page
->original_permissions
);
5297 /* Kick the page out of the dictionary. */
5298 if (page
->previous
!= NULL
)
5299 page
->previous
->next
= page
->next
;
5300 if (page
->next
!= NULL
)
5301 page
->next
->previous
= page
->previous
;
5303 /* Just in case someone retains a handle to this after it's freed. */
5304 page
->page_start
= (CORE_ADDR
) 0;
5306 memory_page_dictionary
.page_count
--;
5313 hppa_enable_syscall_events (pid
)
5317 ttevent_t ttrace_events
;
5319 /* Get the set of events that are currently enabled. */
5320 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
5322 (TTRACE_ARG_TYPE
) &ttrace_events
,
5323 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5326 perror_with_name ("ttrace");
5328 /* Add syscall events to that set. */
5329 ttrace_events
.tte_events
|= TTEVT_SYSCALL_ENTRY
;
5330 ttrace_events
.tte_events
|= TTEVT_SYSCALL_RETURN
;
5332 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
5334 (TTRACE_ARG_TYPE
) &ttrace_events
,
5335 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5338 perror_with_name ("ttrace");
5343 hppa_disable_syscall_events (pid
)
5347 ttevent_t ttrace_events
;
5349 /* Get the set of events that are currently enabled. */
5350 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
5352 (TTRACE_ARG_TYPE
) &ttrace_events
,
5353 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5356 perror_with_name ("ttrace");
5358 /* Remove syscall events from that set. */
5359 ttrace_events
.tte_events
&= ~TTEVT_SYSCALL_ENTRY
;
5360 ttrace_events
.tte_events
&= ~TTEVT_SYSCALL_RETURN
;
5362 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
5364 (TTRACE_ARG_TYPE
) &ttrace_events
,
5365 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5368 perror_with_name ("ttrace");
5372 /* The address range beginning with START and ending with START+LEN-1
5373 (inclusive) is to be watched via page-protection by a new watchpoint.
5374 Set protection for all pages that overlap that range.
5376 Note that our caller sets TYPE to:
5377 0 for a bp_hardware_watchpoint,
5378 1 for a bp_read_watchpoint,
5379 2 for a bp_access_watchpoint
5381 (Yes, this is intentionally (though lord only knows why) different
5382 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5385 hppa_insert_hw_watchpoint (pid
, start
, len
, type
)
5391 CORE_ADDR page_start
;
5392 int dictionary_was_empty
;
5395 LONGEST range_size_in_pages
;
5398 error ("read or access hardware watchpoints not supported on HP-UX");
5400 /* Examine all pages in the address range. */
5401 require_memory_page_dictionary ();
5403 dictionary_was_empty
= (memory_page_dictionary
.page_count
== (LONGEST
) 0);
5405 page_size
= memory_page_dictionary
.page_size
;
5406 page_start
= (start
/ page_size
) * page_size
;
5407 range_size_in_pages
= ((LONGEST
) len
+ (LONGEST
) page_size
- 1) / (LONGEST
) page_size
;
5409 for (page_id
=0; page_id
< range_size_in_pages
; page_id
++, page_start
+=page_size
)
5411 memory_page_t
* page
;
5413 /* This gets the page entered into the dictionary if it was
5414 not already entered.
5416 page
= get_dictionary_entry_of_page (pid
, page_start
);
5417 page
->reference_count
++;
5420 /* Our implementation depends on seeing calls to kernel code, for the
5421 following reason. Here we ask to be notified of syscalls.
5423 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5426 But when kernel code accesses the page, it doesn't give a SIGBUS.
5427 Rather, the system call that touched the page fails, with errno=EFAULT.
5430 We could accomodate this "feature" by asking to be notified of syscall
5431 entries & exits; upon getting an entry event, disabling page-protections;
5432 upon getting an exit event, reenabling page-protections and then checking
5433 if any watchpoints triggered.
5435 However, this turns out to be a real performance loser. syscalls are
5436 usually a frequent occurrence. Having to unprotect-reprotect all watched
5437 pages, and also to then read all watched memory locations and compare for
5438 triggers, can be quite expensive.
5440 Instead, we'll only ask to be notified of syscall exits. When we get
5441 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5442 we can just continue the inferior.
5444 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5445 hackish stuff to determine whether the failure really was due to a
5446 page-protect trap on a watched location.
5448 if (dictionary_was_empty
)
5449 hppa_enable_syscall_events (pid
);
5453 /* The address range beginning with START and ending with START+LEN-1
5454 (inclusive) was being watched via page-protection by a watchpoint
5455 which has been removed. Remove protection for all pages that
5456 overlap that range, which are not also being watched by other
5460 hppa_remove_hw_watchpoint (pid
, start
, len
, type
)
5466 CORE_ADDR page_start
;
5467 int dictionary_is_empty
;
5470 LONGEST range_size_in_pages
;
5473 error ("read or access hardware watchpoints not supported on HP-UX");
5475 /* Examine all pages in the address range. */
5476 require_memory_page_dictionary ();
5478 page_size
= memory_page_dictionary
.page_size
;
5479 page_start
= (start
/ page_size
) * page_size
;
5480 range_size_in_pages
= ((LONGEST
) len
+ (LONGEST
) page_size
- 1) / (LONGEST
) page_size
;
5482 for (page_id
=0; page_id
< range_size_in_pages
; page_id
++, page_start
+=page_size
)
5484 memory_page_t
* page
;
5486 page
= get_dictionary_entry_of_page (pid
, page_start
);
5487 page
->reference_count
--;
5489 /* Was this the last reference of this page? If so, then we
5490 must scrub the entry from the dictionary, and also restore
5491 the page's original permissions.
5493 if (page
->reference_count
== 0)
5494 remove_dictionary_entry_of_page (pid
, page
);
5497 dictionary_is_empty
= (memory_page_dictionary
.page_count
== (LONGEST
) 0);
5499 /* If write protections are currently disallowed, then that implies that
5500 wait_for_inferior believes that the inferior is within a system call.
5501 Since we want to see both syscall entry and return, it's clearly not
5502 good to disable syscall events in this state!
5504 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5505 "inferior is between syscall events now". Oh well.
5507 if (dictionary_is_empty
&& memory_page_dictionary
.page_protections_allowed
)
5508 hppa_disable_syscall_events (pid
);
5512 /* Could we implement a watchpoint of this type via our available
5515 This query does not consider whether a particular address range
5516 could be so watched, but just whether support is generally available
5517 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5518 query that answers whether a particular range should be watched via
5522 hppa_can_use_hw_watchpoint (type
, cnt
, ot
)
5527 return (type
== bp_hardware_watchpoint
);
5531 /* Assuming we could set a hardware watchpoint on this address, do
5532 we think it would be profitable ("a good idea") to do so? If not,
5533 we can always set a regular (aka single-step & test) watchpoint
5537 hppa_range_profitable_for_hw_watchpoint (pid
, start
, len
)
5542 int range_is_stack_based
;
5543 int range_is_accessible
;
5544 CORE_ADDR page_start
;
5547 LONGEST range_size_in_pages
;
5549 /* ??rehrauer: For now, say that all addresses are potentially
5550 profitable. Possibly later we'll want to test the address
5553 range_is_stack_based
= 0;
5555 /* If any page in the range is inaccessible, then we cannot
5556 really use hardware watchpointing, even though our client
5557 thinks we can. In that case, it's actually an error to
5558 attempt to use hw watchpoints, so we'll tell our client
5559 that the range is "unprofitable", and hope that they listen...
5561 range_is_accessible
= 1; /* Until proven otherwise. */
5563 /* Examine all pages in the address range. */
5565 page_size
= sysconf (_SC_PAGE_SIZE
);
5567 /* If we can't determine page size, we're hosed. Tell our
5568 client it's unprofitable to use hw watchpoints for this
5571 if (errno
|| (page_size
<= 0))
5577 page_start
= (start
/ page_size
) * page_size
;
5578 range_size_in_pages
= len
/ (LONGEST
)page_size
;
5580 for (page
=0; page
< range_size_in_pages
; page
++, page_start
+=page_size
)
5583 int page_permissions
;
5585 /* Is this page accessible? */
5587 tt_status
= call_ttrace (TT_PROC_GET_MPROTECT
,
5589 (TTRACE_ARG_TYPE
) page_start
,
5591 (TTRACE_ARG_TYPE
) &page_permissions
);
5592 if (errno
|| (tt_status
< 0))
5595 range_is_accessible
= 0;
5599 /* Yes, go for another... */
5602 return (! range_is_stack_based
&& range_is_accessible
);
5607 hppa_pid_or_tid_to_str (id
)
5610 static char buf
[100]; /* Static because address returned. */
5612 /* Does this appear to be a process? If so, print it that way. */
5613 if (is_process_id (id
))
5614 return hppa_pid_to_str (id
);
5616 /* Else, print both the GDB thread number and the system thread id. */
5617 sprintf (buf
, "thread %d (", pid_to_thread_id (id
));
5618 strcat (buf
, hppa_tid_to_str (id
));
5619 strcat (buf
, ")\0");
5625 /* If the current pid is not the pid this module reported
5626 * from "proc_wait" with the most recent event, then the
5627 * user has switched threads.
5629 * If the last reported event was a breakpoint, then return
5630 * the old thread id, else return 0.
5633 hppa_switched_threads( gdb_pid
)
5636 if( gdb_pid
== old_gdb_pid
) {
5638 * Core gdb is working with the same pid that it
5639 * was before we reported the last event. This
5640 * is ok: e.g. we reported hitting a thread-specific
5641 * breakpoint, but we were reporting the wrong
5642 * thread, so the core just ignored the event.
5644 * No thread switch has happened.
5648 else if( gdb_pid
== reported_pid
) {
5650 * Core gdb is working with the pid we reported, so
5651 * any continue or step will be able to figure out
5652 * that it needs to step over any hit breakpoints
5653 * without our (i.e. PREPARE_TO_PROCEED's) help.
5657 else if( !reported_bpt
) {
5659 * The core switched, but we didn't just report a
5660 * breakpoint, so there's no just-hit breakpoint
5661 * instruction at "reported_pid"'s PC, and thus there
5662 * is no need to step over it.
5667 /* There's been a real switch, and we reported
5668 * a hit breakpoint. Let "hppa_prepare_to_proceed"
5669 * know, so it can see whether the breakpoint is
5672 return reported_pid
;
5675 /* Keep compiler happy with an obvious return at the end.
5681 hppa_ensure_vforking_parent_remains_stopped (pid
)
5684 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5691 hppa_resume_execd_vforking_child_to_get_parent_vfork ()
5693 return 0; /* No, the parent vfork is available now. */
5699 _initialize_infttrace ()
5701 /* Initialize the ttrace-based hardware watchpoint implementation. */
5702 memory_page_dictionary
.page_count
= (LONGEST
) -1;
5703 memory_page_dictionary
.page_protections_allowed
= 1;
5706 memory_page_dictionary
.page_size
= sysconf (_SC_PAGE_SIZE
);
5708 if (errno
|| (memory_page_dictionary
.page_size
<= 0))
5709 perror_with_name ("sysconf");