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>
47 #include <sys/unistd.h>
51 #ifdef PTRACE_IN_WRONG_PLACE
54 #include <sys/ptrace.h>
56 #endif /* NO_PTRACE_H */
58 /* Second half of the hackery above. Non-ANSI C, so
59 * we can't use "#error", alas.
62 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
63 /* #error "Hackery to remove warning didn't work right" */
65 /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
68 /* #error "Didn't get expected re-definition of NO_FLAGS" */
69 #define NO_FLAGS INFTTRACE_TEMP_HACK
72 #if !defined (PT_SETTRC)
73 #define PT_SETTRC 0 /* Make process traceable by parent */
75 #if !defined (PT_READ_I)
76 #define PT_READ_I 1 /* Read word from text space */
78 #if !defined (PT_READ_D)
79 #define PT_READ_D 2 /* Read word from data space */
81 #if !defined (PT_READ_U)
82 #define PT_READ_U 3 /* Read word from kernel user struct */
84 #if !defined (PT_WRITE_I)
85 #define PT_WRITE_I 4 /* Write word to text space */
87 #if !defined (PT_WRITE_D)
88 #define PT_WRITE_D 5 /* Write word to data space */
90 #if !defined (PT_WRITE_U)
91 #define PT_WRITE_U 6 /* Write word to kernel user struct */
93 #if !defined (PT_CONTINUE)
94 #define PT_CONTINUE 7 /* Continue after signal */
96 #if !defined (PT_STEP)
97 #define PT_STEP 9 /* Set flag for single stepping */
99 #if !defined (PT_KILL)
100 #define PT_KILL 8 /* Send child a SIGKILL signal */
104 #define PT_ATTACH PTRACE_ATTACH
107 #define PT_DETACH PTRACE_DETACH
112 #include <sys/file.h>
115 /* This semaphore is used to coordinate the child and parent processes
116 after a fork(), and before an exec() by the child. See parent_attach_all
120 int parent_channel
[2]; /* Parent "talks" to [1], child "listens" to [0] */
121 int child_channel
[2]; /* Child "talks" to [1], parent "listens" to [0] */
122 } startup_semaphore_t
;
125 #define SEM_LISTEN (0)
127 static startup_semaphore_t startup_semaphore
;
129 /* See can_touch_threads_of_process for details. */
130 static int vforking_child_pid
= 0;
131 static int vfork_in_flight
= 0;
133 /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed).
135 static pid_t old_gdb_pid
= 0;
136 static pid_t reported_pid
= 0;
137 static int reported_bpt
= 0;
139 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
141 #define TT_OK( _status, _errno ) \
142 (((_status) == 1) && ((_errno) == 0))
144 #define TTRACE_ARG_TYPE uint64_t
146 /* When supplied as the "addr" operand, ttrace interprets this
147 to mean, "from the current address".
149 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
151 /* When supplied as the "addr", "data" or "addr2" operand for most
152 requests, ttrace interprets this to mean, "pay no heed to this
155 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
157 /* This is capable of holding the value of a 32-bit register. The
158 value is always left-aligned in the buffer; i.e., [0] contains
159 the most-significant byte of the register's value, and [sizeof(reg)]
160 contains the least-significant value.
162 ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
163 that registers are 32-bits on HP-UX. The latter assumption changes
166 typedef int register_value_t
;
168 /********************************************************************
174 The rest of GDB sees threads as being things with different
175 "pid" (process id) values. See "thread.c" for details. The
176 separate threads will be seen and reacted to if infttrace passes
177 back different pid values (for _events_). See wait_for_inferior
180 So infttrace is going to use thread ids externally, pretending
181 they are process ids, and keep track internally so that it can
182 use the real process id (and thread id) when calling ttrace.
184 The data structure that supports this is a linked list of the
185 current threads. Since at some date infttrace will have to
186 deal with multiple processes, each list element records its
187 corresponding pid, rather than having a single global.
189 Note that the list is only approximately current; that's ok, as
190 it's up to date when we need it (we hope!). Also, it can contain
191 dead threads, as there's no harm if it does.
193 The approach taken here is to bury the translation from external
194 to internal inside "call_ttrace" and a few other places.
196 There are some wrinkles:
198 o When GDB forks itself to create the debug target process,
199 there's only a pid of 0 around in the child, so the
200 TT_PROC_SETTRC operation uses a more direct call to ttrace;
201 Similiarly, the initial setting of the event mask happens
202 early as well, and so is also special-cased, and an attach
205 o We define an unthreaded application as having a "pseudo"
208 o To keep from confusing the rest of GDB, we don't switch
209 the PID for the pseudo thread to a TID. A table will help:
211 Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ...
213 Our thread list stores: pid pid pid pid ...
216 Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ...
218 Both pid and tid0 will map to tid0, as there are infttrace.c-internal
219 calls to ttrace using tid0.
223 Since we're implementing the "stop the world" model, sub-model
224 "other threads run during step", we have some stuff to do:
226 o User steps require continuing all threads other than the
227 one the user is stepping;
229 o Internal debugger steps (such as over a breakpoint or watchpoint,
230 but not out of a library load thunk) require stepping only
231 the selected thread; this means that we have to report the
232 step finish on that thread, which can lead to complications;
234 o When a thread is created, it is created running, rather
235 than stopped--so we have to stop it.
237 The OS doesn't guarantee the stopped thread list will be stable,
238 no does it guarantee where on the stopped thread list a thread
239 that is single-stepped will wind up: it's possible that it will
240 be off the list for a while, it's possible the step will complete
241 and it will be re-posted to the end...
243 This means we have to scan the stopped thread list, build up
244 a work-list, and then run down the work list; we can't do the
245 step/continue during the scan.
249 Then there's the issue of waiting for an event. We do this by
250 noticing how many events are reported at the end of each wait.
251 From then on, we "fake" all resumes and steps, returning instantly,
252 and don't do another wait. Once all pending events are reported,
253 we can really resume again.
255 To keep this hidden, all the routines which know about tids and
256 pids or real events and simulated ones are static (file-local).
258 This code can make lots of calls to ttrace, in particular it
259 can spin down the list of thread states more than once. If this
260 becomes a performance hit, the spin could be done once and the
261 various "tsp" blocks saved, keeping all later spins in this
264 The O/S doesn't promise to keep the list straight, and so we must
265 re-scan a lot. By observation, it looks like a single-step/wait
266 puts the stepped thread at the end of the list but doesn't change
269 ****************************************************************
272 /* Uncomment these to turn on various debugging output */
273 /* #define THREAD_DEBUG */
274 /* #define WAIT_BUFFER_DEBUG */
275 /* #define PARANOIA */
278 #define INFTTRACE_ALL_THREADS (-1)
279 #define INFTTRACE_STEP (1)
280 #define INFTTRACE_CONTINUE (0)
282 /* FIX: this is used in inftarg.c/child_wait, in a hack.
284 extern int not_same_real_pid
;
286 /* This is used to count buffered events.
288 static unsigned int more_events_left
= 0;
292 typedef enum process_state_enum
{
295 FAKE_CONTINUE
, /* For later use */
301 static process_state_t process_state
= STOPPED
;
303 /* User-specified stepping modality.
305 typedef enum stepping_mode_enum
{
306 DO_DEFAULT
, /* ...which is a continue! */
311 /* Action to take on an attach, depends on
312 * what kind (user command, fork, vfork).
314 * At the moment, this is either:
316 * o continue with a SIGTRAP signal, or
320 typedef enum attach_continue_enum
{
325 /* This flag is true if we are doing a step-over-bpt
326 * with buffered events. We will have to be sure to
327 * report the right thread, as otherwise the spaghetti
328 * code in "infrun.c/wait_for_inferior" will get
331 static int doing_fake_step
= 0;
332 static lwpid_t fake_step_tid
= 0;
335 /****************************************************
336 * Thread information structure routines and types. *
337 ****************************************************
340 struct thread_info_struct
342 int am_pseudo
; /* This is a pseudo-thread for the process. */
343 int pid
; /* Process ID */
344 lwpid_t tid
; /* Thread ID */
345 int handled
; /* 1 if a buffered event was handled. */
346 int seen
; /* 1 if this thread was seen on a traverse. */
347 int terminated
; /* 1 if thread has terminated. */
348 int have_signal
; /* 1 if signal to be sent */
349 enum target_signal signal_value
; /* Signal to send */
350 int have_start
; /* 1 if alternate starting address */
351 stepping_mode_t stepping_mode
; /* Whether to step or continue */
352 CORE_ADDR start
; /* Where to start */
353 int have_state
; /* 1 if the event state has been set */
354 ttstate_t last_stop_state
;/* The most recently-waited event for this thread. */
355 struct thread_info_struct
356 *next
; /* All threads are linked via this field. */
357 struct thread_info_struct
358 *next_pseudo
; /* All pseudo-threads are linked via this field. */
362 struct thread_info_header_struct
366 thread_info
*head_pseudo
;
368 } thread_info_header
;
370 static thread_info_header thread_head
= { 0, NULL
, NULL
};
371 static thread_info_header deleted_threads
= { 0, NULL
, NULL
};
373 static saved_real_pid
= 0;
376 /*************************************************
377 * Debugging support functions *
378 *************************************************
384 unsigned long pc_val
;
388 offset
= register_addr( PC_REGNUM
, U_REGS_OFFSET
);
389 res
= read_from_register_save_state(
391 (TTRACE_ARG_TYPE
) offset
,
395 return (CORE_ADDR
) pc_val
;
398 return (CORE_ADDR
) 0;
403 get_printable_name_of_stepping_mode( mode
)
404 stepping_mode_t mode
;
407 case DO_DEFAULT
: return "DO_DEFAULT";
408 case DO_STEP
: return "DO_STEP";
409 case DO_CONTINUE
: return "DO_CONTINUE";
410 default: return "?unknown mode?";
414 /* This function returns a pointer to a string describing the
415 * ttrace event being reported.
418 get_printable_name_of_ttrace_event (event
)
421 /* This enumeration is "gappy", so don't use a table. */
427 return "TTEVT_SIGNAL";
435 return "TTEVT_VFORK";
436 case TTEVT_SYSCALL_RETURN
:
437 return "TTEVT_SYSCALL_RETURN";
438 case TTEVT_LWP_CREATE
:
439 return "TTEVT_LWP_CREATE";
440 case TTEVT_LWP_TERMINATE
:
441 return "TTEVT_LWP_TERMINATE";
443 return "TTEVT_LWP_EXIT";
444 case TTEVT_LWP_ABORT_SYSCALL
:
445 return "TTEVT_LWP_ABORT_SYSCALL";
446 case TTEVT_SYSCALL_ENTRY
:
447 return "TTEVT_SYSCALL_ENTRY";
448 case TTEVT_SYSCALL_RESTART
:
449 return "TTEVT_SYSCALL_RESTART";
451 return "?new event?";
456 /* This function translates the ttrace request enumeration into
457 * a character string that is its printable (aka "human readable")
461 get_printable_name_of_ttrace_request (request
)
464 if (!IS_TTRACE_REQ (request
))
467 /* This enumeration is "gappy", so don't use a table. */
469 case TT_PROC_SETTRC
:
470 return "TT_PROC_SETTRC";
471 case TT_PROC_ATTACH
:
472 return "TT_PROC_ATTACH";
473 case TT_PROC_DETACH
:
474 return "TT_PROC_DETACH";
475 case TT_PROC_RDTEXT
:
476 return "TT_PROC_RDTEXT";
477 case TT_PROC_WRTEXT
:
478 return "TT_PROC_WRTEXT";
479 case TT_PROC_RDDATA
:
480 return "TT_PROC_RDDATA";
481 case TT_PROC_WRDATA
:
482 return "TT_PROC_WRDATA";
484 return "TT_PROC_STOP";
485 case TT_PROC_CONTINUE
:
486 return "TT_PROC_CONTINUE";
487 case TT_PROC_GET_PATHNAME
:
488 return "TT_PROC_GET_PATHNAME";
489 case TT_PROC_GET_EVENT_MASK
:
490 return "TT_PROC_GET_EVENT_MASK";
491 case TT_PROC_SET_EVENT_MASK
:
492 return "TT_PROC_SET_EVENT_MASK";
493 case TT_PROC_GET_FIRST_LWP_STATE
:
494 return "TT_PROC_GET_FIRST_LWP_STATE";
495 case TT_PROC_GET_NEXT_LWP_STATE
:
496 return "TT_PROC_GET_NEXT_LWP_STATE";
498 return "TT_PROC_EXIT";
499 case TT_PROC_GET_MPROTECT
:
500 return "TT_PROC_GET_MPROTECT";
501 case TT_PROC_SET_MPROTECT
:
502 return "TT_PROC_SET_MPROTECT";
503 case TT_PROC_SET_SCBM
:
504 return "TT_PROC_SET_SCBM";
506 return "TT_LWP_STOP";
507 case TT_LWP_CONTINUE
:
508 return "TT_LWP_CONTINUE";
510 return "TT_LWP_SINGLE";
512 return "TT_LWP_RUREGS";
514 return "TT_LWP_WUREGS";
515 case TT_LWP_GET_EVENT_MASK
:
516 return "TT_LWP_GET_EVENT_MASK";
517 case TT_LWP_SET_EVENT_MASK
:
518 return "TT_LWP_SET_EVENT_MASK";
519 case TT_LWP_GET_STATE
:
520 return "TT_LWP_GET_STATE";
527 /* This function translates the process state enumeration into
528 * a character string that is its printable (aka "human readable")
532 get_printable_name_of_process_state (process_state
)
533 process_state_t process_state
;
535 switch (process_state
) {
539 return "FAKE_STEPPING";
547 return "?some unknown state?";
551 /* Set a ttrace thread state to a safe, initial state.
554 clear_ttstate_t (tts
)
559 tts
->tts_user_tid
= 0;
560 tts
->tts_event
= TTEVT_NONE
;
563 /* Copy ttrace thread state TTS_FROM into TTS_TO.
566 copy_ttstate_t (tts_to
, tts_from
)
568 ttstate_t
* tts_from
;
570 memcpy ((char *) tts_to
, (char *) tts_from
, sizeof (*tts_to
));
573 /* Are there any live threads we know about?
578 return( thread_head
.count
> 0 );
581 /* Create, fill in and link in a thread descriptor.
584 create_thread_info (pid
, tid
)
590 int thread_count_of_pid
;
592 new_p
= malloc( sizeof( thread_info
));
595 new_p
->have_signal
= 0;
596 new_p
->have_start
= 0;
597 new_p
->have_state
= 0;
598 clear_ttstate_t( &new_p
->last_stop_state
);
599 new_p
->am_pseudo
= 0;
602 new_p
->terminated
= 0;
604 new_p
->next_pseudo
= NULL
;
605 new_p
->stepping_mode
= DO_DEFAULT
;
607 if( 0 == thread_head
.count
) {
610 printf( "First thread, pid %d tid %d!\n", pid
, tid
);
612 saved_real_pid
= inferior_pid
;
617 printf( "Subsequent thread, pid %d tid %d\n", pid
, tid
);
621 /* Another day, another thread...
625 /* The new thread always goes at the head of the list.
627 new_p
->next
= thread_head
.head
;
628 thread_head
.head
= new_p
;
630 /* Is this the "pseudo" thread of a process? It is if there's
631 * no other thread for this process on the list. (Note that this
632 * accomodates multiple processes, such as we see even for simple
633 * cases like forking "non-threaded" programs.)
635 p
= thread_head
.head
;
636 thread_count_of_pid
= 0;
639 if (p
->pid
== new_p
->pid
)
640 thread_count_of_pid
++;
644 /* Did we see any other threads for this pid? (Recall that we just
645 * added this thread to the list...)
647 if (thread_count_of_pid
== 1)
649 new_p
->am_pseudo
= 1;
650 new_p
->next_pseudo
= thread_head
.head_pseudo
;
651 thread_head
.head_pseudo
= new_p
;
657 /* Get rid of our thread info.
667 printf( "Clearing all thread info\n" );
670 p
= thread_head
.head
;
677 thread_head
.head
= NULL
;
678 thread_head
.head_pseudo
= NULL
;
679 thread_head
.count
= 0;
681 p
= deleted_threads
.head
;
688 deleted_threads
.head
= NULL
;
689 deleted_threads
.head_pseudo
= NULL
;
690 deleted_threads
.count
= 0;
692 /* No threads, so can't have pending events.
694 more_events_left
= 0;
697 /* Given a tid, find the thread block for it.
700 find_thread_info (tid
)
705 for( p
= thread_head
.head
; p
; p
= p
->next
) {
706 if( p
->tid
== tid
) {
711 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
712 if( p
->tid
== tid
) {
720 /* For any but the pseudo thread, this maps to the
721 * thread ID. For the pseudo thread, if you pass either
722 * the thread id or the PID, you get the pseudo thread ID.
724 * We have to be prepared for core gdb to ask about
725 * deleted threads. We do the map, but we don't like it.
728 map_from_gdb_tid( gdb_tid
)
733 /* First assume gdb_tid really is a tid, and try to find a
734 * matching entry on the threads list.
736 for( p
= thread_head
.head
; p
; p
= p
->next
) {
737 if( p
->tid
== gdb_tid
)
741 /* It doesn't appear to be a tid; perhaps it's really a pid?
742 * Try to find a "pseudo" thread entry on the threads list.
744 for (p
= thread_head
.head_pseudo
; p
!= NULL
; p
= p
->next_pseudo
)
746 if (p
->pid
== gdb_tid
)
750 /* Perhaps it's the tid of a deleted thread we may still
751 * have some knowledge of?
753 for( p
= deleted_threads
.head
; p
; p
= p
-> next
) {
754 if( p
->tid
== gdb_tid
)
758 /* Or perhaps it's the pid of a deleted process we may still
761 for (p
= deleted_threads
.head_pseudo
; p
!= NULL
; p
= p
->next_pseudo
)
763 if (p
->pid
== gdb_tid
)
767 return 0; /* Error? */
770 /* Map the other way: from a real tid to the
771 * "pid" known by core gdb. This tid may be
772 * for a thread that just got deleted, so we
773 * also need to consider deleted threads.
776 map_to_gdb_tid( real_tid
)
781 for( p
= thread_head
.head
; p
; p
= p
->next
) {
782 if( p
->tid
== real_tid
) {
790 for( p
= deleted_threads
.head
; p
; p
= p
-> next
) {
791 if( p
->tid
== real_tid
)
793 return p
->pid
; /* Error? */
798 return 0; /* Error? Never heard of this thread! */
801 /* Do any threads have saved signals?
804 saved_signals_exist ()
808 for( p
= thread_head
.head
; p
; p
= p
->next
) {
809 if( p
->have_signal
) {
817 /* Is this the tid for the zero-th thread?
820 is_pseudo_thread (tid
)
823 thread_info
*p
= find_thread_info( tid
);
824 if( NULL
== p
|| p
->terminated
)
830 /* Is this thread terminated?
836 thread_info
*p
= find_thread_info( tid
);
839 return p
->terminated
;
844 /* Is this pid a real PID or a TID?
855 /* What does PID really represent?
857 tid
= map_from_gdb_tid (pid
);
859 return 0; /* Actually, is probably an error... */
861 tinfo
= find_thread_info (tid
);
863 /* Does it appear to be a true thread?
865 if (! tinfo
->am_pseudo
)
868 /* Else, it looks like it may be a process. See if there's any other
869 * threads with the same process ID, though. If there are, then TID
870 * just happens to be the first thread of several for this process.
872 this_pid
= tinfo
->pid
;
874 for (tinfo
= thread_head
.head
; tinfo
; tinfo
= tinfo
->next
)
876 if (tinfo
->pid
== this_pid
)
880 return (this_pid_count
== 1);
884 /* Add a thread to our info. Prevent duplicate entries.
887 add_tthread (pid
, tid
)
893 p
= find_thread_info( tid
);
895 p
= create_thread_info( pid
, tid
);
900 /* Notice that a thread was deleted.
909 if( thread_head
.count
<= 0 ) {
910 error( "Internal error in thread database." );
915 for( p
= thread_head
.head
; p
; p
= p
->next
) {
916 if( p
->tid
== tid
) {
920 printf( "Delete here: %d \n", tid
);
925 * Deleting a main thread is ok if we're doing
926 * a parent-follow on a child; this is odd but
927 * not wrong. It apparently _doesn't_ happen
928 * on the child-follow, as we don't just delete
929 * the pseudo while keeping the rest of the
930 * threads around--instead, we clear out the whole
931 * thread list at once.
934 thread_info
*q_chase
;
937 for( q
= thread_head
.head_pseudo
; q
; q
= q
-> next
) {
939 /* Remove from pseudo list.
941 if( q_chase
== NULL
)
942 thread_head
.head_pseudo
= p
->next_pseudo
;
944 q_chase
-> next
= p
->next_pseudo
;
951 /* Remove from live list.
956 thread_head
.head
= p
->next
;
958 chase
->next
= p
->next
;
960 /* Add to deleted thread list.
962 p
->next
= deleted_threads
.head
;
963 deleted_threads
.head
= p
;
964 deleted_threads
.count
++;
966 p
->next_pseudo
= deleted_threads
.head_pseudo
;
967 deleted_threads
.head_pseudo
= p
;
979 /* Get the pid for this tid. (Has to be a real TID!).
987 for( p
= thread_head
.head
; p
; p
= p
->next
) {
988 if( p
->tid
== tid
) {
993 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
994 if( p
->tid
== tid
) {
1002 /* Note that this thread's current event has been handled.
1005 set_handled( pid
, tid
)
1011 p
= find_thread_info( tid
);
1013 p
= add_tthread( pid
, tid
);
1018 /* Was this thread's current event handled?
1026 p
= find_thread_info( tid
);
1030 return 0; /* New threads have not been handled */
1033 /* Set this thread to unhandled.
1036 clear_handled( tid
)
1041 #ifdef WAIT_BUFFER_DEBUG
1043 printf( "clear_handled %d\n", (int) tid
);
1046 p
= find_thread_info (tid
);
1048 error ("Internal error: No thread state to clear?");
1053 /* Set all threads to unhandled.
1056 clear_all_handled ()
1060 #ifdef WAIT_BUFFER_DEBUG
1062 printf( "clear_all_handled\n" );
1065 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1069 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
1074 /* Set this thread to default stepping mode.
1077 clear_stepping_mode( tid
)
1082 #ifdef WAIT_BUFFER_DEBUG
1084 printf( "clear_stepping_mode %d\n", (int) tid
);
1087 p
= find_thread_info (tid
);
1089 error ("Internal error: No thread state to clear?");
1091 p
->stepping_mode
= DO_DEFAULT
;
1094 /* Set all threads to do default continue on resume.
1097 clear_all_stepping_mode ()
1101 #ifdef WAIT_BUFFER_DEBUG
1103 printf( "clear_all_stepping_mode\n" );
1106 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1107 p
->stepping_mode
= DO_DEFAULT
;
1110 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
1111 p
->stepping_mode
= DO_DEFAULT
;
1115 /* Set all threads to unseen on this pass.
1122 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1127 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1128 /* debugging routine.
1134 printf( " Thread pid %d, tid %d", p
->pid
, p
->tid
);
1136 printf( ", event is %s",
1137 get_printable_name_of_ttrace_event( p
->last_stop_state
.tts_event
));
1140 printf( ", pseudo thread" );
1142 if( p
->have_signal
)
1143 printf( ", have signal 0x%x", p
->signal_value
);
1146 printf( ", have start at 0x%x", p
->start
);
1148 printf( ", step is %s", get_printable_name_of_stepping_mode( p
->stepping_mode
));
1151 printf( ", handled" );
1153 printf( ", not handled" );
1158 printf( ", not seen" );
1168 if( thread_head
.count
== 0 )
1169 printf( "Thread list is empty\n" );
1171 printf( "Thread list has " );
1172 if( thread_head
.count
== 1 )
1173 printf( "1 entry:\n" );
1175 printf( "%d entries:\n", thread_head
.count
);
1176 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1181 if( deleted_threads
.count
== 0 )
1182 printf( "Deleted thread list is empty\n" );
1184 printf( "Deleted thread list has " );
1185 if( deleted_threads
.count
== 1 )
1186 printf( "1 entry:\n" );
1188 printf( "%d entries:\n", deleted_threads
.count
);
1190 for( p
= deleted_threads
.head
; p
; p
= p
->next
) {
1197 /* Update the thread list based on the "seen" bits.
1200 update_thread_list ()
1206 for( p
= thread_head
.head
; p
; p
= p
->next
) {
1207 /* Is this an "unseen" thread which really happens to be a process?
1208 If so, is it inferior_pid and is a vfork in flight? If yes to
1209 all, then DON'T REMOVE IT! We're in the midst of moving a vfork
1210 operation, which is a multiple step thing, to the point where we
1211 can touch the parent again. We've most likely stopped to examine
1212 the child at a late stage in the vfork, and if we're not following
1213 the child, we'd best not treat the parent as a dead "thread"...
1215 if( (!p
->seen
) && p
->am_pseudo
&& vfork_in_flight
1216 && (p
->pid
!= vforking_child_pid
))
1225 printf( "Delete unseen thread: %d \n", p
->tid
);
1227 del_tthread( p
->tid
);
1234 /************************************************
1235 * O/S call wrappers *
1236 ************************************************
1239 /* This function simply calls ttrace with the given arguments.
1240 * It exists so that all calls to ttrace are isolated. All
1241 * parameters should be as specified by "man 2 ttrace".
1243 * No other "raw" calls to ttrace should exist in this module.
1246 call_real_ttrace( request
, pid
, tid
, addr
, data
, addr2
)
1250 TTRACE_ARG_TYPE addr
, data
, addr2
;
1255 tt_status
= ttrace( request
, pid
, tid
, addr
, data
, addr2
);
1259 /* Don't bother for a known benign error: if you ask for the
1260 * first thread state, but there is only one thread and it's
1261 * not stopped, ttrace complains.
1263 * We have this inside the #ifdef because our caller will do
1264 * this check for real.
1266 if( request
!= TT_PROC_GET_FIRST_LWP_STATE
1267 || errno
!= EPROTO
) {
1269 printf( "TT fail for %s, with pid %d, tid %d, status %d \n",
1270 get_printable_name_of_ttrace_request (request
),
1271 pid
, tid
, tt_status
);
1277 /* ??rehrauer: It would probably be most robust to catch and report
1278 * failed requests here. However, some clients of this interface
1279 * seem to expect to catch & deal with them, so we'd best not.
1282 strcpy (reason_for_failure
, "ttrace (");
1283 strcat (reason_for_failure
, get_printable_name_of_ttrace_request (request
));
1284 strcat (reason_for_failure
, ")");
1285 printf( "ttrace error, errno = %d\n", errno
);
1286 perror_with_name (reason_for_failure
);
1294 /* This function simply calls ttrace_wait with the given arguments.
1295 * It exists so that all calls to ttrace_wait are isolated.
1297 * No "raw" calls to ttrace_wait should exist elsewhere.
1300 call_real_ttrace_wait( pid
, tid
, option
, tsp
, tsp_size
)
1308 thread_info
* tinfo
= NULL
;
1311 ttw_status
= ttrace_wait (pid
, tid
, option
, tsp
, tsp_size
);
1316 printf( "TW fail with pid %d, tid %d \n", pid
, tid
);
1319 perror_with_name ("ttrace wait");
1326 /* A process may have one or more kernel threads, of which all or
1327 none may be stopped. This function returns the ID of the first
1328 kernel thread in a stopped state, or 0 if none are stopped.
1330 This function can be used with get_process_next_stopped_thread_id
1331 to iterate over the IDs of all stopped threads of this process.
1334 get_process_first_stopped_thread_id (pid
, thread_state
)
1336 ttstate_t
* thread_state
;
1340 tt_status
= call_real_ttrace (
1341 TT_PROC_GET_FIRST_LWP_STATE
,
1344 (TTRACE_ARG_TYPE
) thread_state
,
1345 (TTRACE_ARG_TYPE
) sizeof (*thread_state
),
1349 if( errno
== EPROTO
) {
1350 /* This is an error we can handle: there isn't any stopped
1351 * thread. This happens when we're re-starting the application
1352 * and it has only one thread. GET_NEXT handles the case of
1353 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1361 perror_with_name ("ttrace");
1369 return thread_state
->tts_lwpid
;
1373 /* This function returns the ID of the "next" kernel thread in a
1374 stopped state, or 0 if there are none. "Next" refers to the
1375 thread following that of the last successful call to this
1376 function or to get_process_first_stopped_thread_id, using
1377 the value of thread_state returned by that call.
1379 This function can be used with get_process_first_stopped_thread_id
1380 to iterate over the IDs of all stopped threads of this process.
1383 get_process_next_stopped_thread_id (pid
, thread_state
)
1385 ttstate_t
* thread_state
;
1389 tt_status
= call_real_ttrace (
1390 TT_PROC_GET_NEXT_LWP_STATE
,
1393 (TTRACE_ARG_TYPE
) thread_state
,
1394 (TTRACE_ARG_TYPE
) sizeof (*thread_state
),
1397 perror_with_name ("ttrace");
1404 else if( tt_status
== 0 ) {
1405 /* End of list, no next state. Don't return the
1406 * tts_lwpid, as it's a meaningless "240".
1408 * This is an HPUX "feature".
1413 return thread_state
->tts_lwpid
;
1416 /* ??rehrauer: Eventually this function perhaps should be calling
1417 pid_to_thread_id. However, that function currently does nothing
1418 for HP-UX. Even then, I'm not clear whether that function
1419 will return a "kernel" thread ID, or a "user" thread ID. If
1420 the former, we can just call it here. If the latter, we must
1421 map from the "user" tid to a "kernel" tid.
1423 NOTE: currently not called.
1426 get_active_tid_of_pid (pid
)
1429 ttstate_t thread_state
;
1431 return get_process_first_stopped_thread_id (pid
, &thread_state
);
1434 /* This function returns 1 if tt_request is a ttrace request that
1435 * operates upon all threads of a (i.e., the entire) process.
1438 is_process_ttrace_request (tt_request
)
1441 return IS_TTRACE_PROCREQ (tt_request
);
1445 /* This function translates a thread ttrace request into
1446 * the equivalent process request for a one-thread process.
1449 make_process_version( request
)
1452 if (!IS_TTRACE_REQ (request
)) {
1453 error( "Internal error, bad ttrace request made\n" );
1459 return TT_PROC_STOP
;
1461 case TT_LWP_CONTINUE
:
1462 return TT_PROC_CONTINUE
;
1464 case TT_LWP_GET_EVENT_MASK
:
1465 return TT_PROC_GET_EVENT_MASK
;
1467 case TT_LWP_SET_EVENT_MASK
:
1468 return TT_PROC_SET_EVENT_MASK
;
1470 case TT_LWP_SINGLE
:
1471 case TT_LWP_RUREGS
:
1472 case TT_LWP_WUREGS
:
1473 case TT_LWP_GET_STATE
:
1474 return -1; /* No equivalent */
1482 /* This function translates the "pid" used by the rest of
1483 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1484 * with the given arguments.
1486 * In general, other parts of this module should call this
1487 * function when they are dealing with external users, who only
1488 * have tids to pass (but they call it "pid" for historical
1492 call_ttrace( request
, gdb_tid
, addr
, data
, addr2
)
1495 TTRACE_ARG_TYPE addr
, data
, addr2
;
1499 ttreq_t new_request
;
1501 char reason_for_failure
[100]; /* Arbitrary size, should be big enough. */
1504 int is_interesting
= 0;
1506 if( TT_LWP_RUREGS
== request
) {
1507 is_interesting
= 1; /* Adjust code here as desired */
1510 if( is_interesting
&& 0 && debug_on
) {
1511 if( !is_process_ttrace_request( request
)) {
1512 printf( "TT: Thread request, tid is %d", gdb_tid
);
1513 printf( "== SINGLE at %x", addr
);
1516 printf( "TT: Process request, tid is %d\n", gdb_tid
);
1517 printf( "==! SINGLE at %x", addr
);
1522 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1523 * which happen before any threads get set up) should go
1524 * directly to "call_real_ttrace", so they don't happen here.
1526 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1530 if( request
== TT_PROC_SETTRC
&& debug_on
)
1531 printf( "Unexpected call for TT_PROC_SETTRC\n" );
1534 /* Sometimes we get called with a bogus tid (e.g., if a
1535 * thread has terminated, we return 0; inftarg later asks
1536 * whether the thread has exited/forked/vforked).
1540 errno
= ESRCH
; /* ttrace's response would probably be "No such process". */
1544 /* All other cases should be able to expect that there are
1547 if( !any_thread_records()) {
1550 warning ("No thread records for ttrace call");
1552 errno
= ESRCH
; /* ttrace's response would be "No such process". */
1556 /* OK, now the task is to translate the incoming tid into
1559 real_tid
= map_from_gdb_tid( gdb_tid
);
1560 real_pid
= get_pid_for( real_tid
);
1562 /* Now check the result. "Real_pid" is NULL if our list
1563 * didn't find it. We have some tricks we can play to fix
1566 if( 0 == real_pid
) {
1567 ttstate_t thread_state
;
1571 printf( "No saved pid for tid %d\n", gdb_tid
);
1574 if( is_process_ttrace_request( request
)) {
1576 /* Ok, we couldn't get a tid. Try to translate to
1577 * the equivalent process operation. We expect this
1578 * NOT to happen, so this is a desparation-type
1579 * move. It can happen if there is an internal
1580 * error and so no "wait()" call is ever done.
1582 new_request
= make_process_version( request
);
1583 if( new_request
== -1 ) {
1587 printf( "...and couldn't make process version of thread operation\n" );
1590 /* Use hacky saved pid, which won't always be correct
1591 * in the multi-process future. Use tid as thread,
1592 * probably dooming this to failure. FIX!
1594 if( saved_real_pid
!= 0 ) {
1597 printf( "...using saved pid %d\n", saved_real_pid
);
1600 real_pid
= saved_real_pid
;
1605 error( "Unable to perform thread operation" );
1609 /* Sucessfully translated this to a process request,
1610 * which needs no thread value.
1614 request
= new_request
;
1618 printf( "Translated thread request to process request\n" );
1619 if( saved_real_pid
== 0 )
1620 printf( "...but there's no saved pid\n" );
1623 if( gdb_tid
!= saved_real_pid
)
1624 printf( "...but have the wrong pid (%d rather than %d)\n",
1625 gdb_tid
, saved_real_pid
);
1629 } /* Translated to a process request */
1630 } /* Is a process request */
1633 /* We have to have a thread. Ooops.
1635 error( "Thread request with no threads (%s)",
1636 get_printable_name_of_ttrace_request( request
));
1640 /* Ttrace doesn't like to see tid values on process requests,
1641 * even if we have the right one.
1643 if (is_process_ttrace_request (request
)) {
1648 if( is_interesting
&& 0 && debug_on
) {
1649 printf( " now tid %d, pid %d\n", real_tid
, real_pid
);
1650 printf( " request is %s\n", get_printable_name_of_ttrace_request (request
));
1654 /* Finally, the (almost) real call.
1656 tt_status
= call_real_ttrace (request
, real_pid
, real_tid
, addr
, data
, addr2
);
1659 if(is_interesting
&& debug_on
) {
1660 if( !TT_OK( tt_status
, errno
)
1661 && !(tt_status
== 0 & errno
== 0))
1662 printf( " got error (errno==%d, status==%d)\n", errno
, tt_status
);
1670 /* Stop all the threads of a process.
1672 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1673 * to get a TTEVT_NONE event, discarding the old event. Be
1674 * very careful, and only call TT_PROC_STOP when you mean it!
1677 stop_all_threads_of_process( real_pid
)
1682 ttw_status
= call_real_ttrace (TT_PROC_STOP
,
1685 (TTRACE_ARG_TYPE
) TT_NIL
,
1686 (TTRACE_ARG_TYPE
) TT_NIL
,
1689 perror_with_name ("ttrace stop of other threads");
1693 /* Under some circumstances, it's unsafe to attempt to stop, or even
1694 query the state of, a process' threads.
1696 In ttrace-based HP-UX, an example is a vforking child process. The
1697 vforking parent and child are somewhat fragile, w/r/t what we can do
1698 what we can do to them with ttrace, until after the child exits or
1699 execs, or until the parent's vfork event is delivered. Until that
1700 time, we must not try to stop the process' threads, or inquire how
1701 many there are, or even alter its data segments, or it typically dies
1702 with a SIGILL. Sigh.
1704 This function returns 1 if this stopped process, and the event that
1705 we're told was responsible for its current stopped state, cannot safely
1706 have its threads examined.
1708 #define CHILD_VFORKED(evt,pid) \
1709 (((evt) == TTEVT_VFORK) && ((pid) != inferior_pid))
1710 #define CHILD_URPED(evt,pid) \
1711 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1712 #define PARENT_VFORKED(evt,pid) \
1713 (((evt) == TTEVT_VFORK) && ((pid) == inferior_pid))
1716 can_touch_threads_of_process (pid
, stopping_event
)
1718 ttevents_t stopping_event
;
1720 if (CHILD_VFORKED (stopping_event
, pid
))
1722 vforking_child_pid
= pid
;
1723 vfork_in_flight
= 1;
1726 else if (vfork_in_flight
&&
1727 (PARENT_VFORKED (stopping_event
, pid
) ||
1728 CHILD_URPED (stopping_event
, pid
)))
1730 vfork_in_flight
= 0;
1731 vforking_child_pid
= 0;
1734 return ! vfork_in_flight
;
1738 /* If we can find an as-yet-unhandled thread state of a
1739 * stopped thread of this process return 1 and set "tsp".
1740 * Return 0 if we can't.
1742 * If this function is used when the threads of PIS haven't
1743 * been stopped, undefined behaviour is guaranteed!
1746 select_stopped_thread_of_process (pid
, tsp
)
1750 lwpid_t candidate_tid
, tid
;
1751 ttstate_t candidate_tstate
, tstate
;
1753 /* If we're not allowed to touch the process now, then just
1754 * return the current value of *TSP.
1756 * This supports "vfork". It's ok, really, to double the
1757 * current event (the child EXEC, we hope!).
1759 if (! can_touch_threads_of_process (pid
, tsp
->tts_event
))
1762 /* Decide which of (possibly more than one) events to
1763 * return as the first one. We scan them all so that
1764 * we always return the result of a fake-step first.
1767 for (tid
= get_process_first_stopped_thread_id (pid
, &tstate
);
1769 tid
= get_process_next_stopped_thread_id (pid
, &tstate
))
1771 /* TTEVT_NONE events are uninteresting to our clients. They're
1772 * an artifact of our "stop the world" model--the thread is
1773 * stopped because we stopped it.
1775 if (tstate
.tts_event
== TTEVT_NONE
) {
1776 set_handled( pid
, tstate
.tts_lwpid
);
1779 /* Did we just single-step a single thread, without letting any
1780 * of the others run? Is this an event for that thread?
1782 * If so, we believe our client would prefer to see this event
1783 * over any others. (Typically the client wants to just push
1784 * one thread a little farther forward, and then go around
1785 * checking for what all threads are doing.)
1787 else if (doing_fake_step
&& (tstate
.tts_lwpid
== fake_step_tid
))
1789 #ifdef WAIT_BUFFER_DEBUG
1790 /* It's possible here to see either a SIGTRAP (due to
1791 * successful completion of a step) or a SYSCALL_ENTRY
1792 * (due to a step completion with active hardware
1796 printf( "Ending fake step with tid %d, state %s\n",
1798 get_printable_name_of_ttrace_event( tstate
.tts_event
));
1801 /* Remember this one, and throw away any previous
1804 candidate_tid
= tstate
.tts_lwpid
;
1805 candidate_tstate
= tstate
;
1808 #ifdef FORGET_DELETED_BPTS
1810 /* We can't just do this, as if we do, and then wind
1811 * up the loop with no unhandled events, we need to
1812 * handle that case--the appropriate reaction is to
1813 * just continue, but there's no easy way to do that.
1815 * Better to put this in the ttrace_wait call--if, when
1816 * we fake a wait, we update our events based on the
1817 * breakpoint_here_pc call and find there are no more events,
1818 * then we better continue and so on.
1820 * Or we could put it in the next/continue fake.
1821 * But it has to go in the buffering code, not in the
1822 * real go/wait code.
1824 else if( (TTEVT_SIGNAL
== tstate
.tts_event
)
1825 && (5 == tstate
.tts_u
.tts_signal
.tts_signo
)
1826 && (0 != get_raw_pc( tstate
.tts_lwpid
))
1827 && ! breakpoint_here_p( get_raw_pc( tstate
.tts_lwpid
)) ) {
1829 * If the user deleted a breakpoint while this
1830 * breakpoint-hit event was buffered, we can forget
1833 #ifdef WAIT_BUFFER_DEBUG
1835 printf( "Forgetting deleted bp hit for thread %d\n",
1839 set_handled( pid
, tstate
.tts_lwpid
);
1843 /* Else, is this the first "unhandled" event? If so,
1844 * we believe our client wants to see it (if we don't
1845 * see a fake-step later on in the scan).
1847 else if( !was_handled( tstate
.tts_lwpid
) && candidate_tid
== 0 ) {
1848 candidate_tid
= tstate
.tts_lwpid
;
1849 candidate_tstate
= tstate
;
1852 /* This is either an event that has already been "handled",
1853 * and thus we believe is uninteresting to our client, or we
1854 * already have a candidate event. Ignore it...
1858 /* What do we report?
1860 if( doing_fake_step
) {
1861 if( candidate_tid
== fake_step_tid
) {
1864 tstate
= candidate_tstate
;
1867 warning( "Internal error: fake-step failed to complete." );
1871 else if( candidate_tid
!= 0 ) {
1872 /* Found a candidate unhandled event.
1874 tstate
= candidate_tstate
;
1876 else if( tid
!= 0 ) {
1877 warning( "Internal error in call of ttrace_wait." );
1881 warning ("Internal error: no unhandled thread event to select");
1885 copy_ttstate_t (tsp
, &tstate
);
1887 } /* End of select_stopped_thread_of_process */
1890 /* Check our internal thread data against the real thing.
1893 check_thread_consistency( real_pid
)
1896 int tid
; /* really lwpid_t */
1900 /* Spin down the O/S list of threads, checking that they
1901 * match what we've got.
1903 for (tid
= get_process_first_stopped_thread_id( real_pid
, &tstate
);
1905 tid
= get_process_next_stopped_thread_id( real_pid
, &tstate
)) {
1907 p
= find_thread_info( tid
);
1910 warning( "No internal thread data for thread %d.", tid
);
1915 warning( "Inconsistent internal thread data for thread %d.", tid
);
1918 if( p
->terminated
) {
1919 warning( "Thread %d is not terminated, internal error.", tid
);
1924 #define TT_COMPARE( fld ) \
1925 tstate.fld != p->last_stop_state.fld
1927 if( p
->have_state
) {
1928 if( TT_COMPARE( tts_pid
)
1929 || TT_COMPARE( tts_lwpid
)
1930 || TT_COMPARE( tts_user_tid
)
1931 || TT_COMPARE( tts_event
)
1932 || TT_COMPARE( tts_flags
)
1933 || TT_COMPARE( tts_scno
)
1934 || TT_COMPARE( tts_scnargs
)) {
1935 warning( "Internal thread data for thread %d is wrong.", tid
);
1941 #endif /* PARANOIA */
1944 /* This function wraps calls to "call_real_ttrace_wait" so
1945 * that a actual wait is only done when all pending events
1946 * have been reported.
1948 * Note that typically it is called with a pid of "0", i.e.
1949 * the "don't care" value.
1951 * Return value is the status of the pseudo wait.
1954 call_ttrace_wait( pid
, option
, tsp
, tsp_size
)
1960 /* This holds the actual, for-real, true process ID.
1962 static int real_pid
;
1964 /* As an argument to ttrace_wait, zero pid
1965 * means "Any process", and zero tid means
1966 * "Any thread of the specified process".
1969 lwpid_t wait_tid
= 0;
1972 int ttw_status
= 0; /* To be returned */
1974 thread_info
* tinfo
= NULL
;
1981 printf( "TW: Pid to wait on is %d\n", pid
);
1984 if( !any_thread_records())
1985 error( "No thread records for ttrace call w. specific pid" );
1987 /* OK, now the task is to translate the incoming tid into
1990 real_tid
= map_from_gdb_tid( pid
);
1991 real_pid
= get_pid_for( real_tid
);
1994 printf( "==TW: real pid %d, real tid %d\n", real_pid
, real_tid
);
1999 /* Sanity checks and set-up.
2002 * Stopped Running Fake-step (v)Fork
2003 * \________________________________________
2005 * No buffered events | error wait wait wait
2007 * Buffered events | debuffer error wait debuffer (?)
2010 if( more_events_left
== 0 ) {
2012 if( process_state
== RUNNING
) {
2013 /* OK--normal call of ttrace_wait with no buffered events.
2017 else if( process_state
== FAKE_STEPPING
) {
2018 /* Ok--call of ttrace_wait to support
2019 * fake stepping with no buffered events.
2021 * But we better be fake-stepping!
2023 if( !doing_fake_step
) {
2024 warning( "Inconsistent thread state." );
2027 else if( (process_state
== FORKING
)
2028 || (process_state
== VFORKING
)) {
2029 /* Ok--there are two processes, so waiting
2030 * for the second while the first is stopped
2031 * is ok. Handled bits stay as they were.
2035 else if( process_state
== STOPPED
) {
2036 warning( "Process not running at wait call." );
2041 warning( "Inconsistent process state." );
2047 if( process_state
== STOPPED
) {
2048 /* OK--buffered events being unbuffered.
2052 else if( process_state
== RUNNING
) {
2053 /* An error--shouldn't have buffered events
2056 warning( "Trying to continue with buffered events:" );
2058 else if( process_state
== FAKE_STEPPING
) {
2060 * Better be fake-stepping!
2062 if( !doing_fake_step
) {
2063 warning( "Losing buffered thread events!\n" );
2066 else if( (process_state
== FORKING
)
2067 || (process_state
== VFORKING
)) {
2068 /* Ok--there are two processes, so waiting
2069 * for the second while the first is stopped
2070 * is ok. Handled bits stay as they were.
2075 warning( "Process in unknown state with buffered events." );
2078 /* Sometimes we have to wait for a particular thread
2079 * (if we're stepping over a bpt). In that case, we
2080 * _know_ it's going to complete the single-step we
2081 * asked for (because we're only doing the step under
2082 * certain very well-understood circumstances), so it
2085 if( doing_fake_step
) {
2086 wait_tid
= fake_step_tid
;
2087 wait_pid
= get_pid_for( fake_step_tid
);
2089 #ifdef WAIT_BUFFER_DEBUG
2091 printf( "Doing a wait after a fake-step for %d, pid %d\n",
2092 wait_tid
, wait_pid
);
2096 if( more_events_left
== 0 /* No buffered events, need real ones. */
2097 || process_state
!= STOPPED
) {
2098 /* If there are no buffered events, and so we need
2099 * real ones, or if we are FORKING, VFORKING,
2100 * FAKE_STEPPING or RUNNING, and thus have to do
2101 * a real wait, then do a real wait.
2104 #ifdef WAIT_BUFFER_DEBUG
2105 /* Normal case... */
2107 printf( "TW: do it for real; pid %d, tid %d\n", wait_pid
, wait_tid
);
2110 /* The actual wait call.
2112 ttw_status
= call_real_ttrace_wait( wait_pid
, wait_tid
, option
, tsp
, tsp_size
);
2114 /* Note that the routines we'll call will be using "call_real_ttrace",
2115 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2116 * the rest of the world uses (which is actually the tid).
2118 real_pid
= tsp
->tts_pid
;
2120 /* For most events: Stop the world!
2122 * It's sometimes not safe to stop all threads of a process.
2123 * Sometimes it's not even safe to ask for the thread state
2126 if (can_touch_threads_of_process (real_pid
, tsp
->tts_event
))
2128 /* If we're really only stepping a single thread, then don't
2129 * try to stop all the others -- we only do this single-stepping
2130 * business when all others were already stopped...and the stop
2131 * would mess up other threads' events.
2133 * Similiarly, if there are other threads with events,
2134 * don't do the stop.
2136 if( !doing_fake_step
) {
2137 if( more_events_left
> 0 )
2138 warning( "Internal error in stopping process" );
2140 stop_all_threads_of_process (real_pid
);
2142 /* At this point, we could scan and update_thread_list(),
2143 * and only use the local list for the rest of the
2144 * module! We'd get rid of the scans in the various
2145 * continue routines (adding one in attach). It'd
2146 * be great--UPGRADE ME!
2152 else if( debug_on
) {
2153 if( more_events_left
> 0 )
2154 printf( "== Can't stop process; more events!\n" );
2156 printf( "== Can't stop process!\n" );
2160 process_state
= STOPPED
;
2162 #ifdef WAIT_BUFFER_DEBUG
2164 printf( "Process set to STOPPED\n" );
2169 /* Fake a call to ttrace_wait. The process must be
2170 * STOPPED, as we aren't going to do any wait.
2172 #ifdef WAIT_BUFFER_DEBUG
2174 printf( "TW: fake it\n" );
2177 if( process_state
!= STOPPED
) {
2178 warning( "Process not stopped at wait call, in state '%s'.\n",
2179 get_printable_name_of_process_state( process_state
));
2182 if( doing_fake_step
)
2183 error( "Internal error in stepping over breakpoint" );
2185 ttw_status
= 0; /* Faking it is always successful! */
2186 } /* End of fake or not? if */
2188 /* Pick an event to pass to our caller. Be paranoid.
2190 if( !select_stopped_thread_of_process( real_pid
, tsp
))
2191 warning( "Can't find event, using previous event." );
2193 else if( tsp
->tts_event
== TTEVT_NONE
)
2194 warning( "Internal error: no thread has a real event." );
2196 else if( doing_fake_step
) {
2197 if( fake_step_tid
!= tsp
->tts_lwpid
)
2198 warning( "Internal error in stepping over breakpoint." );
2200 /* This wait clears the (current) fake-step if there was one.
2202 doing_fake_step
= 0;
2206 /* We now have a correct tsp and ttw_status for the thread
2207 * which we want to report. So it's "handled"! This call
2208 * will add it to our list if it's not there already.
2210 set_handled( real_pid
, tsp
->tts_lwpid
);
2212 /* Save a copy of the ttrace state of this thread, in our local
2215 This caches the state. The implementation of queries like
2216 target_has_execd can then use this cached state, rather than
2217 be forced to make an explicit ttrace call to get it.
2219 (Guard against the condition that this is the first time we've
2220 waited on, i.e., seen this thread, and so haven't yet entered
2221 it into our list of threads.)
2223 tinfo
= find_thread_info (tsp
->tts_lwpid
);
2224 if (tinfo
!= NULL
) {
2225 copy_ttstate_t (&tinfo
->last_stop_state
, tsp
);
2226 tinfo
->have_state
= 1;
2230 } /* call_ttrace_wait */
2232 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2234 child_reported_exec_events_per_exec_call ()
2236 return 1; /* ttrace reports the event once per call. */
2242 /* Our implementation of hardware watchpoints involves making memory
2243 pages write-protected. We must remember a page's original permissions,
2244 and we must also know when it is appropriate to restore a page's
2245 permissions to its original state.
2247 We use a "dictionary" of hardware-watched pages to do this. Each
2248 hardware-watched page is recorded in the dictionary. Each page's
2249 dictionary entry contains the original permissions and a reference
2250 count. Pages are hashed into the dictionary by their start address.
2252 When hardware watchpoint is set on page X for the first time, page X
2253 is added to the dictionary with a reference count of 1. If other
2254 hardware watchpoints are subsequently set on page X, its reference
2255 count is incremented. When hardware watchpoints are removed from
2256 page X, its reference count is decremented. If a page's reference
2257 count drops to 0, it's permissions are restored and the page's entry
2258 is thrown out of the dictionary.
2260 typedef struct memory_page
{
2261 CORE_ADDR page_start
;
2262 int reference_count
;
2263 int original_permissions
;
2264 struct memory_page
* next
;
2265 struct memory_page
* previous
;
2268 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2273 int page_protections_allowed
;
2274 /* These are just the heads of chains of actual page descriptors. */
2275 memory_page_t buckets
[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
];
2276 } memory_page_dictionary
;
2280 require_memory_page_dictionary ()
2284 /* Is the memory page dictionary ready for use? If so, we're done. */
2285 if (memory_page_dictionary
.page_count
>= (LONGEST
) 0)
2288 /* Else, initialize it. */
2289 memory_page_dictionary
.page_count
= (LONGEST
) 0;
2291 for (i
=0; i
<MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; i
++)
2293 memory_page_dictionary
.buckets
[i
].page_start
= (CORE_ADDR
) 0;
2294 memory_page_dictionary
.buckets
[i
].reference_count
= 0;
2295 memory_page_dictionary
.buckets
[i
].next
= NULL
;
2296 memory_page_dictionary
.buckets
[i
].previous
= NULL
;
2302 retire_memory_page_dictionary ()
2304 memory_page_dictionary
.page_count
= (LONGEST
) -1;
2308 /* Write-protect the memory page that starts at this address.
2310 Returns the original permissions of the page.
2313 write_protect_page (pid
, page_start
)
2315 CORE_ADDR page_start
;
2318 int original_permissions
;
2319 int new_permissions
;
2321 tt_status
= call_ttrace (TT_PROC_GET_MPROTECT
,
2323 (TTRACE_ARG_TYPE
) page_start
,
2325 (TTRACE_ARG_TYPE
) &original_permissions
);
2326 if (errno
|| (tt_status
< 0))
2328 return 0; /* What else can we do? */
2331 /* We'll also write-protect the page now, if that's allowed. */
2332 if (memory_page_dictionary
.page_protections_allowed
)
2334 new_permissions
= original_permissions
& ~PROT_WRITE
;
2335 tt_status
= call_ttrace (TT_PROC_SET_MPROTECT
,
2337 (TTRACE_ARG_TYPE
) page_start
,
2338 (TTRACE_ARG_TYPE
) memory_page_dictionary
.page_size
,
2339 (TTRACE_ARG_TYPE
) new_permissions
);
2340 if (errno
|| (tt_status
< 0))
2342 return 0; /* What else can we do? */
2346 return original_permissions
;
2350 /* Unwrite-protect the memory page that starts at this address, restoring
2351 (what we must assume are) its original permissions.
2354 unwrite_protect_page (pid
, page_start
, original_permissions
)
2356 CORE_ADDR page_start
;
2357 int original_permissions
;
2361 tt_status
= call_ttrace (TT_PROC_SET_MPROTECT
,
2363 (TTRACE_ARG_TYPE
) page_start
,
2364 (TTRACE_ARG_TYPE
) memory_page_dictionary
.page_size
,
2365 (TTRACE_ARG_TYPE
) original_permissions
);
2366 if (errno
|| (tt_status
< 0))
2368 return; /* What else can we do? */
2373 /* Memory page-protections are used to implement "hardware" watchpoints
2376 For every memory page that is currently being watched (i.e., that
2377 presently should be write-protected), write-protect it.
2380 hppa_enable_page_protection_events (pid
)
2385 memory_page_dictionary
.page_protections_allowed
= 1;
2387 for (bucket
=0; bucket
<MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; bucket
++)
2389 memory_page_t
* page
;
2391 page
= memory_page_dictionary
.buckets
[bucket
].next
;
2392 while (page
!= NULL
)
2394 page
->original_permissions
= write_protect_page (pid
, page
->page_start
);
2401 /* Memory page-protections are used to implement "hardware" watchpoints
2404 For every memory page that is currently being watched (i.e., that
2405 presently is or should be write-protected), un-write-protect it.
2408 hppa_disable_page_protection_events (pid
)
2413 for (bucket
=0; bucket
<MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; bucket
++)
2415 memory_page_t
* page
;
2417 page
= memory_page_dictionary
.buckets
[bucket
].next
;
2418 while (page
!= NULL
)
2420 unwrite_protect_page (pid
, page
->page_start
, page
->original_permissions
);
2425 memory_page_dictionary
.page_protections_allowed
= 0;
2428 /* Count the number of outstanding events. At this
2429 * point, we have selected one thread and its event
2430 * as the one to be "reported" upwards to core gdb.
2431 * That thread is already marked as "handled".
2433 * Note: we could just scan our own thread list. FIXME!
2436 count_unhandled_events( real_pid
, real_tid
)
2444 /* Ok, find out how many threads have real events to report.
2447 ttid
= get_process_first_stopped_thread_id( real_pid
, &tstate
);
2452 printf( "Process %d has no threads\n", real_pid
);
2454 printf( "Process %d has these threads:\n", real_pid
);
2459 if( tstate
.tts_event
!= TTEVT_NONE
2460 && !was_handled( ttid
)) {
2461 /* TTEVT_NONE implies we just stopped it ourselves
2462 * because we're the stop-the-world guys, so it's
2463 * not an event from our point of view.
2465 * If "was_handled" is true, this is an event we
2466 * already handled, so don't count it.
2468 * Note that we don't count the thread with the
2469 * currently-reported event, as it's already marked
2475 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2477 if( ttid
== real_tid
)
2478 printf( "*" ); /* Thread we're reporting */
2482 if( tstate
.tts_event
!= TTEVT_NONE
)
2483 printf( "+" ); /* Thread with a real event */
2487 if( was_handled( ttid
))
2488 printf( "h" ); /* Thread has been handled */
2492 printf( " %d, with event %s", ttid
,
2493 get_printable_name_of_ttrace_event( tstate
.tts_event
));
2495 if( tstate
.tts_event
== TTEVT_SIGNAL
2496 && 5 == tstate
.tts_u
.tts_signal
.tts_signo
) {
2499 pc_val
= get_raw_pc( ttid
);
2502 printf( " breakpoint at 0x%x\n", pc_val
);
2504 printf( " bpt, can't fetch pc.\n" );
2511 ttid
= get_process_next_stopped_thread_id (real_pid
, &tstate
);
2514 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2516 if( events_left
> 0 )
2517 printf( "There are thus %d pending events\n", events_left
);
2523 /* This function is provided as a sop to clients that are calling
2524 * proc_wait to wait for a process to stop. (see the
2525 * implementation of child_wait.) Return value is the pid for
2526 * the event that ended the wait.
2528 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2531 proc_wait (pid
, status
)
2542 /* The ptrace implementation of this also ignores pid.
2546 ttwait_return
= call_ttrace_wait( 0, TTRACE_WAITOK
, &tsp
, sizeof (tsp
) );
2547 if (ttwait_return
< 0)
2549 /* ??rehrauer: It appears that if our inferior exits and we
2550 haven't asked for exit events, that we're not getting any
2551 indication save a negative return from ttrace_wait and an
2556 *status
= 0; /* WIFEXITED */
2557 return inferior_pid
;
2560 warning( "Call of ttrace_wait returned with errno %d.",
2562 *status
= ttwait_return
;
2563 return inferior_pid
;
2566 real_pid
= tsp
.tts_pid
;
2567 real_tid
= tsp
.tts_lwpid
;
2569 /* One complication is that the "tts_event" structure has
2570 * a set of flags, and more than one can be set. So we
2571 * either have to force an order (as we do here), or handle
2572 * more than one flag at a time.
2574 if (tsp
.tts_event
& TTEVT_LWP_CREATE
) {
2576 /* Unlike what you might expect, this event is reported in
2577 * the _creating_ thread, and the _created_ thread (whose tid
2578 * we have) is still running. So we have to stop it. This
2579 * has already been done in "call_ttrace_wait", but should we
2580 * ever abandon the "stop-the-world" model, here's the command
2583 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2585 * Note that this would depend on being called _after_ "add_tthread"
2586 * below for the tid-to-pid translation to be done in "call_ttrace".
2591 printf( "New thread: pid %d, tid %d, creator tid %d\n",
2592 real_pid
, tsp
.tts_u
.tts_thread
.tts_target_lwpid
,
2596 /* Now we have to return the tid of the created thread, not
2597 * the creating thread, or "wait_for_inferior" won't know we
2598 * have a new "process" (thread). Plus we should record it
2601 real_tid
= tsp
.tts_u
.tts_thread
.tts_target_lwpid
;
2603 add_tthread( real_pid
, real_tid
);
2606 else if( (tsp
.tts_event
& TTEVT_LWP_TERMINATE
)
2607 || (tsp
.tts_event
& TTEVT_LWP_EXIT
) ) {
2611 printf( "Thread dies: %d\n", real_tid
);
2614 del_tthread( real_tid
);
2617 else if (tsp
.tts_event
& TTEVT_EXEC
) {
2621 printf( "Pid %d has zero'th thread %d; inferior pid is %d\n",
2622 real_pid
, real_tid
, inferior_pid
);
2625 add_tthread( real_pid
, real_tid
);
2629 else if( debug_on
) {
2630 printf( "Process-level event %s, using tid %d\n",
2631 get_printable_name_of_ttrace_event( tsp
.tts_event
),
2634 /* OK to do this, as "add_tthread" won't add
2635 * duplicate entries. Also OK not to do it,
2636 * as this event isn't one which can change the
2639 add_tthread( real_pid
, real_tid
);
2644 /* How many events are left to report later?
2645 * In a non-stop-the-world model, this isn't needed.
2647 * Note that it's not always safe to query the thread state of a process,
2648 * which is what count_unhandled_events does. (If unsafe, we're left with
2649 * no other resort than to assume that no more events remain...)
2651 if (can_touch_threads_of_process (real_pid
, tsp
.tts_event
))
2652 more_events_left
= count_unhandled_events( real_pid
, real_tid
);
2655 if( more_events_left
> 0 )
2656 warning( "Vfork or fork causing loss of %d buffered events.",
2659 more_events_left
= 0;
2662 /* Attempt to translate the ttrace_wait-returned status into the
2665 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2666 clients that expect to pick apart a ptrace wait status, to use
2667 something a little more abstract.
2669 if ( (tsp
.tts_event
& TTEVT_EXEC
)
2670 || (tsp
.tts_event
& TTEVT_FORK
)
2671 || (tsp
.tts_event
& TTEVT_VFORK
))
2673 /* Forks come in pairs (parent and child), so core gdb
2674 * will do two waits. Be ready to notice this.
2676 if (tsp
.tts_event
& TTEVT_FORK
)
2678 process_state
= FORKING
;
2680 #ifdef WAIT_BUFFER_DEBUG
2682 printf( "Process set to FORKING\n" );
2685 else if (tsp
.tts_event
& TTEVT_VFORK
)
2687 process_state
= VFORKING
;
2689 #ifdef WAIT_BUFFER_DEBUG
2691 printf( "Process set to VFORKING\n" );
2695 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2696 but I don't think non HP-UX-specific clients really carefully
2697 inspect the first events they get after inferior startup, so
2698 it probably almost doesn't matter what we claim this is.
2703 printf( "..a process 'event'\n" );
2706 /* Also make fork and exec events look like bpts, so they can be caught.
2708 *status
= 0177 | (_SIGTRAP
<< 8);
2711 /* Special-cases: We ask for syscall entry and exit events to implement
2712 "fast" (aka "hardware") watchpoints.
2714 When we get a syscall entry, we want to disable page-protections,
2715 and resume the inferior; this isn't an event we wish for
2716 wait_for_inferior to see. Note that we must resume ONLY the
2717 thread that reported the syscall entry; we don't want to allow
2718 other threads to run with the page protections off, as they might
2719 then be able to write to watch memory without it being caught.
2721 When we get a syscall exit, we want to reenable page-protections,
2722 but we don't want to resume the inferior; this is an event we wish
2723 wait_for_inferior to see. Make it look like the signal we normally
2724 get for a single-step completion. This should cause wait_for_inferior
2725 to evaluate whether any watchpoint triggered.
2727 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2728 due to some HP-UX "features". Some syscalls have problems with
2729 write-protections on some pages, and some syscalls seem to have
2730 pending writes to those pages at the time we're getting the return
2731 event. So, we'll single-step the inferior to get out of the syscall,
2732 and then reenable protections.
2734 Note that we're intentionally allowing the syscall exit case to
2735 fall through into the succeeding cases, as sometimes we single-
2736 step out of one syscall only to immediately enter another...
2738 else if ((tsp
.tts_event
& TTEVT_SYSCALL_ENTRY
)
2739 || (tsp
.tts_event
& TTEVT_SYSCALL_RETURN
))
2741 /* Make a syscall event look like a breakpoint. Same comments
2742 as for exec & fork events.
2746 printf( "..a syscall 'event'\n" );
2749 /* Also make syscall events look like bpts, so they can be caught.
2751 *status
= 0177 | (_SIGTRAP
<< 8);
2754 else if ((tsp
.tts_event
& TTEVT_LWP_CREATE
)
2755 || (tsp
.tts_event
& TTEVT_LWP_TERMINATE
)
2756 || (tsp
.tts_event
& TTEVT_LWP_EXIT
))
2758 /* Make a thread event look like a breakpoint. Same comments
2759 * as for exec & fork events.
2763 printf( "..a thread 'event'\n" );
2766 /* Also make thread events look like bpts, so they can be caught.
2768 *status
= 0177 | (_SIGTRAP
<< 8);
2771 else if ((tsp
.tts_event
& TTEVT_EXIT
))
2776 printf( "..an exit\n" );
2779 /* Prevent rest of gdb from thinking this is
2780 * a new thread if for some reason it's never
2781 * seen the main thread before.
2783 inferior_pid
= map_to_gdb_tid( real_tid
); /* HACK, FIX */
2785 *status
= 0 | (tsp
.tts_u
.tts_exit
.tts_exitcode
);
2788 else if (tsp
.tts_event
& TTEVT_SIGNAL
)
2792 printf( "..a signal, %d\n", tsp
.tts_u
.tts_signal
.tts_signo
);
2795 *status
= 0177 | (tsp
.tts_u
.tts_signal
.tts_signo
<< 8);
2801 /* This means the process or thread terminated. But we should've
2802 caught an explicit exit/termination above. So warn (this is
2803 really an internal error) and claim the process or thread
2804 terminated with a SIGTRAP.
2807 warning ("process_wait: unknown process state");
2811 printf( "Process-level event %s, using tid %d\n",
2812 get_printable_name_of_ttrace_event( tsp
.tts_event
),
2819 target_post_wait (tsp
.tts_pid
, *status
);
2824 printf( "Done waiting, pid is %d, tid %d\n", real_pid
, real_tid
);
2827 /* All code external to this module uses the tid, but calls
2828 * it "pid". There's some tweaking so that the outside sees
2829 * the first thread as having the same number as the starting
2832 return_pid
= map_to_gdb_tid( real_tid
);
2834 /* Remember this for later use in "hppa_prepare_to_proceed".
2836 old_gdb_pid
= inferior_pid
;
2837 reported_pid
= return_pid
;
2838 reported_bpt
= ((tsp
.tts_event
& TTEVT_SIGNAL
) && (5 == tsp
.tts_u
.tts_signal
.tts_signo
));
2840 if( real_tid
== 0 || return_pid
== 0 ) {
2841 warning( "Internal error: process-wait failed." );
2848 /* This function causes the caller's process to be traced by its
2849 parent. This is intended to be called after GDB forks itself,
2850 and before the child execs the target. Despite the name, it
2851 is called by the child.
2853 Note that HP-UX ttrace is rather funky in how this is done.
2854 If the parent wants to get the initial exec event of a child,
2855 it must set the ttrace event mask of the child to include execs.
2856 (The child cannot do this itself.) This must be done after the
2857 child is forked, but before it execs.
2859 To coordinate the parent and child, we implement a semaphore using
2860 pipes. After SETTRC'ing itself, the child tells the parent that
2861 it is now traceable by the parent, and waits for the parent's
2862 acknowledgement. The parent can then set the child's event mask,
2863 and notify the child that it can now exec.
2865 (The acknowledgement by parent happens as a result of a call to
2866 child_acknowledge_created_inferior.)
2869 parent_attach_all ()
2873 /* We need a memory home for a constant, to pass it to ttrace.
2874 The value of the constant is arbitrary, so long as both
2875 parent and child use the same value. Might as well use the
2876 "magic" constant provided by ttrace...
2878 uint64_t tc_magic_child
= TT_VERSION
;
2879 uint64_t tc_magic_parent
= 0;
2881 tt_status
= call_real_ttrace (
2886 (TTRACE_ARG_TYPE
) TT_VERSION
,
2892 /* Notify the parent that we're potentially ready to exec(). */
2893 write (startup_semaphore
.child_channel
[SEM_TALK
],
2895 sizeof (tc_magic_child
));
2897 /* Wait for acknowledgement from the parent. */
2898 read (startup_semaphore
.parent_channel
[SEM_LISTEN
],
2900 sizeof (tc_magic_parent
));
2902 if (tc_magic_child
!= tc_magic_parent
)
2903 warning ("mismatched semaphore magic");
2905 /* Discard our copy of the semaphore. */
2906 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
2907 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
2908 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
2909 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
2914 /* Despite being file-local, this routine is dealing with
2915 * actual process IDs, not thread ids. That's because it's
2916 * called before the first "wait" call, and there's no map
2917 * yet from tids to pids.
2919 * When it is called, a forked child is running, but waiting on
2920 * the semaphore. If you stop the child and re-start it,
2921 * things get confused, so don't do that! An attached child is
2924 * Since this is called after either attach or run, we
2925 * have to be the common part of both.
2928 require_notification_of_events ( real_pid
)
2932 ttevent_t notifiable_events
;
2935 ttstate_t thread_state
;
2939 printf( "Require notif, pid is %d\n", real_pid
);
2942 /* Temporary HACK: tell inftarg.c/child_wait to not
2943 * loop until pids are the same.
2945 not_same_real_pid
= 0;
2947 sigemptyset (¬ifiable_events
.tte_signals
);
2948 notifiable_events
.tte_opts
= TTEO_NONE
;
2950 /* This ensures that forked children inherit their parent's
2951 * event mask, which we're setting here.
2953 * NOTE: if you debug gdb with itself, then the ultimate
2954 * debuggee gets flags set by the outermost gdb, as
2955 * a child of a child will still inherit.
2957 notifiable_events
.tte_opts
|= TTEO_PROC_INHERIT
;
2959 notifiable_events
.tte_events
= TTEVT_DEFAULT
;
2960 notifiable_events
.tte_events
|= TTEVT_SIGNAL
;
2961 notifiable_events
.tte_events
|= TTEVT_EXEC
;
2962 notifiable_events
.tte_events
|= TTEVT_EXIT
;
2963 notifiable_events
.tte_events
|= TTEVT_FORK
;
2964 notifiable_events
.tte_events
|= TTEVT_VFORK
;
2965 notifiable_events
.tte_events
|= TTEVT_LWP_CREATE
;
2966 notifiable_events
.tte_events
|= TTEVT_LWP_EXIT
;
2967 notifiable_events
.tte_events
|= TTEVT_LWP_TERMINATE
;
2969 tt_status
= call_real_ttrace (
2970 TT_PROC_SET_EVENT_MASK
,
2973 (TTRACE_ARG_TYPE
) ¬ifiable_events
,
2974 (TTRACE_ARG_TYPE
) sizeof (notifiable_events
),
2979 require_notification_of_exec_events ( real_pid
)
2983 ttevent_t notifiable_events
;
2986 ttstate_t thread_state
;
2990 printf( "Require notif, pid is %d\n", real_pid
);
2993 /* Temporary HACK: tell inftarg.c/child_wait to not
2994 * loop until pids are the same.
2996 not_same_real_pid
= 0;
2998 sigemptyset (¬ifiable_events
.tte_signals
);
2999 notifiable_events
.tte_opts
= TTEO_NOSTRCCHLD
;
3001 /* This ensures that forked children don't inherit their parent's
3002 * event mask, which we're setting here.
3004 notifiable_events
.tte_opts
&= ~TTEO_PROC_INHERIT
;
3006 notifiable_events
.tte_events
= TTEVT_DEFAULT
;
3007 notifiable_events
.tte_events
|= TTEVT_EXEC
;
3008 notifiable_events
.tte_events
|= TTEVT_EXIT
;
3010 tt_status
= call_real_ttrace (
3011 TT_PROC_SET_EVENT_MASK
,
3014 (TTRACE_ARG_TYPE
) ¬ifiable_events
,
3015 (TTRACE_ARG_TYPE
) sizeof (notifiable_events
),
3020 /* This function is called by the parent process, with pid being the
3021 * ID of the child process, after the debugger has forked.
3024 child_acknowledge_created_inferior (pid
)
3027 /* We need a memory home for a constant, to pass it to ttrace.
3028 The value of the constant is arbitrary, so long as both
3029 parent and child use the same value. Might as well use the
3030 "magic" constant provided by ttrace...
3032 uint64_t tc_magic_parent
= TT_VERSION
;
3033 uint64_t tc_magic_child
= 0;
3035 /* Wait for the child to tell us that it has forked. */
3036 read (startup_semaphore
.child_channel
[SEM_LISTEN
],
3038 sizeof(tc_magic_child
));
3040 /* Clear thread info now. We'd like to do this in
3041 * "require...", but that messes up attach.
3043 clear_thread_info();
3045 /* Tell the "rest of gdb" that the initial thread exists.
3046 * This isn't really a hack. Other thread-based versions
3047 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3049 * Q: Why don't we also add this thread to the local
3050 * list via "add_tthread"?
3052 * A: Because we don't know the tid, and can't stop the
3053 * the process safely to ask what it is. Anyway, we'll
3054 * add it when it gets the EXEC event.
3056 add_thread( pid
); /* in thread.c */
3058 /* We can now set the child's ttrace event mask.
3060 require_notification_of_exec_events (pid
);
3062 /* Tell ourselves that the process is running.
3064 process_state
= RUNNING
;
3066 /* Notify the child that it can exec. */
3067 write (startup_semaphore
.parent_channel
[SEM_TALK
],
3069 sizeof (tc_magic_parent
));
3071 /* Discard our copy of the semaphore. */
3072 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
3073 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
3074 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
3075 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
3080 * arrange for notification of all events by
3081 * calling require_notification_of_events.
3084 child_post_startup_inferior ( real_pid
)
3087 require_notification_of_events (real_pid
);
3090 /* From here on, we should expect tids rather than pids.
3093 hppa_enable_catch_fork (tid
)
3097 ttevent_t ttrace_events
;
3099 /* Get the set of events that are currently enabled.
3101 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3103 (TTRACE_ARG_TYPE
) &ttrace_events
,
3104 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3107 perror_with_name ("ttrace");
3109 /* Add forks to that set. */
3110 ttrace_events
.tte_events
|= TTEVT_FORK
;
3114 printf( "enable fork, tid is %d\n", tid
);
3117 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3119 (TTRACE_ARG_TYPE
) &ttrace_events
,
3120 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3123 perror_with_name ("ttrace");
3128 hppa_disable_catch_fork (tid
)
3132 ttevent_t ttrace_events
;
3134 /* Get the set of events that are currently enabled.
3136 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3138 (TTRACE_ARG_TYPE
) &ttrace_events
,
3139 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3143 perror_with_name ("ttrace");
3145 /* Remove forks from that set. */
3146 ttrace_events
.tte_events
&= ~TTEVT_FORK
;
3150 printf("disable fork, tid is %d\n", tid
);
3153 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3155 (TTRACE_ARG_TYPE
) &ttrace_events
,
3156 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3160 perror_with_name ("ttrace");
3164 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3166 child_insert_fork_catchpoint (tid
)
3169 /* Enable reporting of fork events from the kernel. */
3170 /* ??rehrauer: For the moment, we're always enabling these events,
3171 and just ignoring them if there's no catchpoint to catch them.
3178 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3180 child_remove_fork_catchpoint (tid
)
3183 /* Disable reporting of fork events from the kernel. */
3184 /* ??rehrauer: For the moment, we're always enabling these events,
3185 and just ignoring them if there's no catchpoint to catch them.
3193 hppa_enable_catch_vfork (tid
)
3197 ttevent_t ttrace_events
;
3199 /* Get the set of events that are currently enabled.
3201 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3203 (TTRACE_ARG_TYPE
) &ttrace_events
,
3204 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3208 perror_with_name ("ttrace");
3210 /* Add vforks to that set. */
3211 ttrace_events
.tte_events
|= TTEVT_VFORK
;
3215 printf("enable vfork, tid is %d\n", tid
);
3218 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3220 (TTRACE_ARG_TYPE
) &ttrace_events
,
3221 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3225 perror_with_name ("ttrace");
3230 hppa_disable_catch_vfork (tid
)
3234 ttevent_t ttrace_events
;
3236 /* Get the set of events that are currently enabled. */
3237 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3239 (TTRACE_ARG_TYPE
) &ttrace_events
,
3240 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3244 perror_with_name ("ttrace");
3246 /* Remove vforks from that set. */
3247 ttrace_events
.tte_events
&= ~TTEVT_VFORK
;
3251 printf("disable vfork, tid is %d\n", tid
);
3253 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3255 (TTRACE_ARG_TYPE
) &ttrace_events
,
3256 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3260 perror_with_name ("ttrace");
3264 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3266 child_insert_vfork_catchpoint (tid
)
3269 /* Enable reporting of vfork events from the kernel. */
3270 /* ??rehrauer: For the moment, we're always enabling these events,
3271 and just ignoring them if there's no catchpoint to catch them.
3278 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3280 child_remove_vfork_catchpoint (tid
)
3283 /* Disable reporting of vfork events from the kernel. */
3284 /* ??rehrauer: For the moment, we're always enabling these events,
3285 and just ignoring them if there's no catchpoint to catch them.
3291 #if defined(CHILD_HAS_FORKED)
3293 /* Q: Do we need to map the returned process ID to a thread ID?
3295 * A: I don't think so--here we want a _real_ pid. Any later
3296 * operations will call "require_notification_of_events" and
3297 * start the mapping.
3300 child_has_forked (tid
, childpid
)
3305 ttstate_t ttrace_state
;
3306 thread_info
* tinfo
;
3308 /* Do we have cached thread state that we can consult? If so, use it. */
3309 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3310 if (tinfo
!= NULL
) {
3311 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3314 /* Nope, must read the thread's current state */
3317 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3319 (TTRACE_ARG_TYPE
) &ttrace_state
,
3320 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3324 perror_with_name ("ttrace");
3330 if (ttrace_state
.tts_event
& TTEVT_FORK
)
3332 *childpid
= ttrace_state
.tts_u
.tts_fork
.tts_fpid
;
3341 #if defined(CHILD_HAS_VFORKED)
3343 /* See child_has_forked for pid discussion.
3346 child_has_vforked (tid
, childpid
)
3351 ttstate_t ttrace_state
;
3352 thread_info
* tinfo
;
3354 /* Do we have cached thread state that we can consult? If so, use it. */
3355 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3357 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3359 /* Nope, must read the thread's current state */
3362 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3364 (TTRACE_ARG_TYPE
) &ttrace_state
,
3365 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3369 perror_with_name ("ttrace");
3375 if (ttrace_state
.tts_event
& TTEVT_VFORK
)
3377 *childpid
= ttrace_state
.tts_u
.tts_fork
.tts_fpid
;
3386 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3388 child_can_follow_vfork_prior_to_exec ()
3390 /* ttrace does allow this.
3392 ??rehrauer: However, I had major-league problems trying to
3393 convince wait_for_inferior to handle that case. Perhaps when
3394 it is rewritten to grok multiple processes in an explicit way...
3401 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3403 child_insert_exec_catchpoint (tid
)
3406 /* Enable reporting of exec events from the kernel. */
3407 /* ??rehrauer: For the moment, we're always enabling these events,
3408 and just ignoring them if there's no catchpoint to catch them.
3415 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3417 child_remove_exec_catchpoint (tid
)
3420 /* Disable reporting of execevents from the kernel. */
3421 /* ??rehrauer: For the moment, we're always enabling these events,
3422 and just ignoring them if there's no catchpoint to catch them.
3429 #if defined(CHILD_HAS_EXECD)
3431 child_has_execd (tid
, execd_pathname
)
3433 char ** execd_pathname
;
3436 ttstate_t ttrace_state
;
3437 thread_info
* tinfo
;
3439 /* Do we have cached thread state that we can consult? If so, use it. */
3440 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3442 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3444 /* Nope, must read the thread's current state */
3447 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3449 (TTRACE_ARG_TYPE
) &ttrace_state
,
3450 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3454 perror_with_name ("ttrace");
3460 if (ttrace_state
.tts_event
& TTEVT_EXEC
)
3462 /* See child_pid_to_exec_file in this file: this is a macro.
3464 char * exec_file
= target_pid_to_exec_file (tid
);
3466 *execd_pathname
= savestring (exec_file
, strlen (exec_file
));
3475 #if defined(CHILD_HAS_SYSCALL_EVENT)
3477 child_has_syscall_event (pid
, kind
, syscall_id
)
3479 enum target_waitkind
* kind
;
3483 ttstate_t ttrace_state
;
3484 thread_info
* tinfo
;
3486 /* Do we have cached thread state that we can consult? If so, use it. */
3487 tinfo
= find_thread_info (map_from_gdb_tid (pid
));
3489 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3491 /* Nope, must read the thread's current state */
3494 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3496 (TTRACE_ARG_TYPE
) &ttrace_state
,
3497 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3501 perror_with_name ("ttrace");
3507 *kind
= TARGET_WAITKIND_SPURIOUS
; /* Until proven otherwise... */
3510 if (ttrace_state
.tts_event
& TTEVT_SYSCALL_ENTRY
)
3511 *kind
= TARGET_WAITKIND_SYSCALL_ENTRY
;
3512 else if (ttrace_state
.tts_event
& TTEVT_SYSCALL_RETURN
)
3513 *kind
= TARGET_WAITKIND_SYSCALL_RETURN
;
3517 *syscall_id
= ttrace_state
.tts_scno
;
3524 #if defined(CHILD_THREAD_ALIVE)
3526 /* Check to see if the given thread is alive.
3528 * We'll trust the thread list, as the more correct
3529 * approach of stopping the process and spinning down
3530 * the OS's thread list is _very_ expensive.
3532 * May need a FIXME for that reason.
3535 child_thread_alive (gdb_tid
)
3540 /* This spins down the lists twice.
3541 * Possible peformance improvement here!
3543 tid
= map_from_gdb_tid( gdb_tid
);
3544 return !is_terminated( tid
);
3551 /* This function attempts to read the specified number of bytes from the
3552 save_state_t that is our view into the hardware registers, starting at
3553 ss_offset, and ending at ss_offset + sizeof_buf - 1
3555 If this function succeeds, it deposits the fetched bytes into buf,
3558 If it fails, it returns a negative result. The contents of buf are
3559 undefined it this function fails.
3562 read_from_register_save_state (tid
, ss_offset
, buf
, sizeof_buf
)
3564 TTRACE_ARG_TYPE ss_offset
;
3569 register_value_t register_value
= 0;
3571 tt_status
= call_ttrace (TT_LWP_RUREGS
,
3574 (TTRACE_ARG_TYPE
) sizeof_buf
,
3575 (TTRACE_ARG_TYPE
) buf
);
3577 if( tt_status
== 1 )
3578 /* Map ttrace's version of success to our version.
3579 * Sometime ttrace returns 0, but that's ok here.
3587 /* This function attempts to write the specified number of bytes to the
3588 save_state_t that is our view into the hardware registers, starting at
3589 ss_offset, and ending at ss_offset + sizeof_buf - 1
3591 If this function succeeds, it deposits the bytes in buf, and returns 0.
3593 If it fails, it returns a negative result. The contents of the save_state_t
3594 are undefined it this function fails.
3597 write_to_register_save_state (tid
, ss_offset
, buf
, sizeof_buf
)
3599 TTRACE_ARG_TYPE ss_offset
;
3604 register_value_t register_value
= 0;
3606 tt_status
= call_ttrace (TT_LWP_WUREGS
,
3609 (TTRACE_ARG_TYPE
) sizeof_buf
,
3610 (TTRACE_ARG_TYPE
) buf
);
3615 /* This function is a sop to the largeish number of direct calls
3616 to call_ptrace that exist in other files. Rather than create
3617 functions whose name abstracts away from ptrace, and change all
3618 the present callers of call_ptrace, we'll do the expedient (and
3619 perhaps only practical) thing.
3621 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3622 process. Thus, we must translate all ptrace requests into their
3623 process-specific, ttrace equivalents.
3626 call_ptrace (pt_request
, gdb_tid
, addr
, data
)
3629 PTRACE_ARG3_TYPE addr
;
3633 TTRACE_ARG_TYPE tt_addr
= (TTRACE_ARG_TYPE
) addr
;
3634 TTRACE_ARG_TYPE tt_data
= (TTRACE_ARG_TYPE
) data
;
3635 TTRACE_ARG_TYPE tt_addr2
= TT_NIL
;
3637 register_value_t register_value
;
3640 /* Perform the necessary argument translation. Note that some
3641 cases are funky enough in the ttrace realm that we handle them
3644 switch (pt_request
) {
3645 /* The following cases cannot conveniently be handled conveniently
3646 by merely adjusting the ptrace arguments and feeding into the
3647 generic call to ttrace at the bottom of this function.
3649 Note that because all branches of this switch end in "return",
3650 there's no need for any "break" statements.
3653 return parent_attach_all ();
3656 tt_status
= read_from_register_save_state (gdb_tid
,
3659 sizeof (register_value
));
3662 return register_value
;
3665 register_value
= (int) tt_data
;
3666 tt_status
= write_to_register_save_state (gdb_tid
,
3669 sizeof (register_value
));
3674 tt_status
= call_ttrace (TT_PROC_RDTEXT
, /* Implicit 4-byte xfer becomes block-xfer. */
3677 (TTRACE_ARG_TYPE
) 4,
3678 (TTRACE_ARG_TYPE
) &read_buf
);
3684 tt_status
= call_ttrace (TT_PROC_RDDATA
, /* Implicit 4-byte xfer becomes block-xfer. */
3687 (TTRACE_ARG_TYPE
) 4,
3688 (TTRACE_ARG_TYPE
) &read_buf
);
3694 tt_status
= call_real_ttrace (TT_PROC_ATTACH
,
3695 map_from_gdb_tid (gdb_tid
),
3698 (TTRACE_ARG_TYPE
) TT_VERSION
,
3704 /* The following cases are handled by merely adjusting the ptrace
3705 arguments and feeding into the generic call to ttrace.
3708 tt_request
= TT_PROC_DETACH
;
3712 tt_request
= TT_PROC_WRTEXT
; /* Translates 4-byte xfer to block-xfer. */
3713 tt_data
= 4; /* This many bytes. */
3714 tt_addr2
= (TTRACE_ARG_TYPE
) &data
; /* Address of xfer source. */
3718 tt_request
= TT_PROC_WRDATA
; /* Translates 4-byte xfer to block-xfer. */
3719 tt_data
= 4; /* This many bytes. */
3720 tt_addr2
= (TTRACE_ARG_TYPE
) &data
; /* Address of xfer source. */
3724 tt_request
= TT_PROC_RDTEXT
;
3728 tt_request
= TT_PROC_RDDATA
;
3732 tt_request
= TT_PROC_WRTEXT
;
3736 tt_request
= TT_PROC_WRDATA
;
3740 tt_request
= TT_PROC_CONTINUE
;
3744 tt_request
= TT_LWP_SINGLE
; /* Should not be making this request? */
3748 tt_request
= TT_PROC_EXIT
;
3751 case PT_GET_PROCESS_PATHNAME
:
3752 tt_request
= TT_PROC_GET_PATHNAME
;
3756 tt_request
= pt_request
; /* Let ttrace be the one to complain. */
3760 return call_ttrace (tt_request
,
3767 /* Kill that pesky process!
3775 thread_info
**paranoia
;
3778 if (inferior_pid
== 0)
3781 /* Walk the list of "threads", some of which are "pseudo threads",
3782 aka "processes". For each that is NOT inferior_pid, stop it,
3785 You see, we may not have just a single process to kill. If we're
3786 restarting or quitting or detaching just after the inferior has
3787 forked, then we've actually two processes to clean up.
3789 But we can't just call target_mourn_inferior() for each, since that
3790 zaps the target vector.
3793 paranoia
= (thread_info
**) malloc( thread_head
.count
*
3794 sizeof(thread_info
*));
3797 t
= thread_head
.head
;
3800 paranoia
[ para_count
] = t
;
3801 for( i
= 0; i
< para_count
; i
++ ){
3802 if( t
->next
== paranoia
[i
] ) {
3803 warning( "Bad data in gdb's thread data; repairing." );
3809 if (t
->am_pseudo
&& (t
->pid
!= inferior_pid
))
3811 /* TT_PROC_STOP doesn't require a subsequent ttrace_wait, as it
3812 * generates no event.
3814 call_ttrace (TT_PROC_STOP
,
3820 call_ttrace (TT_PROC_DETACH
,
3823 (TTRACE_ARG_TYPE
) TARGET_SIGNAL_0
,
3831 call_ttrace (TT_PROC_STOP
,
3836 target_mourn_inferior ();
3837 clear_thread_info();
3841 #ifndef CHILD_RESUME
3843 /* Sanity check a thread about to be continued.
3846 thread_dropping_event_check( p
)
3851 * This seems to happen when we "next" over a
3852 * "fork()" while following the parent. If it's
3853 * the FORK event, that's ok. If it's a SIGNAL
3854 * in the unfollowed child, that's ok to--but
3855 * how can we know that's what's going on?
3859 if( p
->have_state
) {
3860 if( p
->last_stop_state
.tts_event
== TTEVT_FORK
) {
3864 else if( p
->last_stop_state
.tts_event
== TTEVT_SIGNAL
) {
3865 /* Ok, close eyes and let it happen.
3870 /* This shouldn't happen--we're dropping a
3873 warning( "About to continue process %d, thread %d with unhandled event %s.",
3875 get_printable_name_of_ttrace_event(
3876 p
->last_stop_state
.tts_event
));
3885 /* No saved state, have to assume it failed.
3887 warning( "About to continue process %d, thread %d with unhandled event.",
3896 } /* thread_dropping_event_check */
3898 /* Use a loop over the threads to continue all the threads but
3899 * the one specified, which is to be stepped.
3902 threads_continue_all_but_one( gdb_tid
, signal
)
3915 printf( "Using loop over threads to step/resume with signals\n" );
3918 /* First update the thread list.
3921 real_tid
= map_from_gdb_tid( gdb_tid
);
3922 real_pid
= get_pid_for( real_tid
);
3924 scan_tid
= get_process_first_stopped_thread_id( real_pid
, &state
);
3925 while ( 0 != scan_tid
) {
3928 /* FIX: later should check state is stopped;
3929 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
3932 if( state
.tts_flags
& TTS_STATEMASK
!= TTS_WASSUSPENDED
)
3933 printf( "About to continue non-stopped thread %d\n", scan_tid
);
3936 p
= find_thread_info( scan_tid
);
3938 add_tthread( real_pid
, scan_tid
);
3939 p
= find_thread_info( scan_tid
);
3941 /* This is either a newly-created thread or the
3942 * result of a fork; in either case there's no
3943 * actual event to worry about.
3947 if( state
.tts_event
!= TTEVT_NONE
) {
3948 /* Oops, do need to worry!
3950 warning( "Unexpected thread with \"%s\" event.",
3951 get_printable_name_of_ttrace_event( state
.tts_event
));
3954 else if( scan_tid
!= p
->tid
)
3955 error( "Bad data in thread database." );
3960 printf( "Why are we continuing a dead thread?\n" );
3965 scan_tid
= get_process_next_stopped_thread_id( real_pid
, &state
);
3968 /* Remove unseen threads.
3970 update_thread_list();
3972 /* Now run down the thread list and continue or step.
3974 for( p
= thread_head
.head
; p
; p
= p
->next
) {
3978 thread_dropping_event_check( p
);
3980 /* Pass the correct signals along.
3982 if( p
->have_signal
) {
3983 thread_signal
= p
->signal_value
;
3989 if( p
->tid
!= real_tid
) {
3991 * Not the thread of interest, so continue it
3992 * as the user expects.
3994 if( p
->stepping_mode
== DO_STEP
) {
3995 /* Just step this thread.
4001 (TTRACE_ARG_TYPE
) target_signal_to_host( signal
),
4005 /* Regular continue (default case).
4011 (TTRACE_ARG_TYPE
) target_signal_to_host( thread_signal
),
4016 /* Step the thread of interest.
4022 (TTRACE_ARG_TYPE
) target_signal_to_host( signal
),
4025 } /* Loop over threads */
4026 } /* End threads_continue_all_but_one */
4028 /* Use a loop over the threads to continue all the threads.
4029 * This is done when a signal must be sent to any of the threads.
4032 threads_continue_all_with_signals( gdb_tid
, signal
)
4045 printf( "Using loop over threads to resume with signals\n" );
4048 /* Scan and update thread list.
4051 real_tid
= map_from_gdb_tid( gdb_tid
);
4052 real_pid
= get_pid_for( real_tid
);
4054 scan_tid
= get_process_first_stopped_thread_id( real_pid
, &state
);
4055 while ( 0 != scan_tid
) {
4059 if( state
.tts_flags
& TTS_STATEMASK
!= TTS_WASSUSPENDED
)
4060 warning( "About to continue non-stopped thread %d\n", scan_tid
);
4063 p
= find_thread_info( scan_tid
);
4065 add_tthread( real_pid
, scan_tid
);
4066 p
= find_thread_info( scan_tid
);
4068 /* This is either a newly-created thread or the
4069 * result of a fork; in either case there's no
4070 * actual event to worry about.
4074 if( state
.tts_event
!= TTEVT_NONE
) {
4075 /* Oops, do need to worry!
4077 warning( "Unexpected thread with \"%s\" event.",
4078 get_printable_name_of_ttrace_event( state
.tts_event
));
4085 printf( "Why are we continuing a dead thread? (1)\n" );
4090 scan_tid
= get_process_next_stopped_thread_id( real_pid
, &state
);
4093 /* Remove unseen threads from our list.
4095 update_thread_list();
4097 /* Continue the threads.
4099 for( p
= thread_head
.head
; p
; p
= p
->next
) {
4103 thread_dropping_event_check( p
);
4105 /* Pass the correct signals along.
4107 if( p
->tid
== real_tid
) {
4108 thread_signal
= signal
;
4111 else if( p
->have_signal
) {
4112 thread_signal
= p
->signal_value
;
4118 if( p
->stepping_mode
== DO_STEP
) {
4123 (TTRACE_ARG_TYPE
) target_signal_to_host( signal
),
4127 /* Continue this thread (default case).
4133 (TTRACE_ARG_TYPE
) target_signal_to_host( thread_signal
),
4137 } /* End threads_continue_all_with_signals */
4139 /* Step one thread only.
4142 thread_fake_step( tid
, signal
)
4144 enum target_signal signal
;
4150 printf( "Doing a fake-step over a bpt, etc. for %d\n", tid
);
4152 if( is_terminated( tid
))
4153 printf( "Why are we continuing a dead thread? (4)\n" );
4157 if( doing_fake_step
)
4158 warning( "Step while step already in progress." );
4160 /* See if there's a saved signal value for this
4161 * thread to be passed on, but no current signal.
4163 p
= find_thread_info( tid
);
4165 if( p
->have_signal
&& signal
== NULL
) {
4166 /* Pass on a saved signal.
4168 signal
= p
->signal_value
;
4175 warning( "Internal error: continuing unhandled thread." );
4177 call_ttrace( TT_LWP_SINGLE
,
4180 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4183 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4184 * for this thread only, and clear any saved signal info.
4186 doing_fake_step
= 1;
4187 fake_step_tid
= tid
;
4189 } /* End thread_fake_step */
4191 /* Continue one thread when a signal must be sent to it.
4194 threads_continue_one_with_signal( gdb_tid
, signal
)
4204 printf( "Continuing one thread with a signal\n" );
4207 real_tid
= map_from_gdb_tid( gdb_tid
);
4208 real_pid
= get_pid_for( real_tid
);
4210 p
= find_thread_info( real_tid
);
4212 add_tthread( real_pid
, real_tid
);
4218 printf( "Why are we continuing a dead thread? (2)\n" );
4222 warning( "Internal error: continuing unhandled thread." );
4226 call_ttrace( TT_LWP_CONTINUE
,
4229 (TTRACE_ARG_TYPE
) target_signal_to_host( signal
),
4234 #ifndef CHILD_RESUME
4236 /* Resume execution of the inferior process.
4238 * This routine is in charge of setting the "handled" bits.
4240 * If STEP is zero, continue it.
4241 * If STEP is nonzero, single-step it.
4243 * If SIGNAL is nonzero, give it that signal.
4245 * If TID is -1, apply to all threads.
4246 * If TID is not -1, apply to specified thread.
4250 * TID \________________________________________________
4252 * -1 | Step current Continue all threads
4253 * | thread and (but which gets any
4254 * | continue others signal?--We look at
4257 * N | Step _this_ thread Continue _this_ thread
4258 * | and leave others and leave others
4259 * | stopped; internally stopped; used only for
4260 * | used by gdb, never hardware watchpoints
4261 * | a user command. and attach, never a
4265 child_resume( gdb_tid
, step
, signal
)
4268 enum target_signal signal
;
4270 int resume_all_threads
;
4272 process_state_t new_process_state
;
4274 resume_all_threads
=
4275 (gdb_tid
== INFTTRACE_ALL_THREADS
) ||
4278 if (resume_all_threads
) {
4279 /* Resume all threads, but first pick a tid value
4280 * so we can get the pid when in call_ttrace doing
4283 if (vfork_in_flight
)
4284 tid
= vforking_child_pid
;
4286 tid
= map_from_gdb_tid( inferior_pid
);
4289 tid
= map_from_gdb_tid( gdb_tid
);
4293 if( more_events_left
)
4294 printf( "More events; " );
4297 printf( "Sending signal %d; ", signal
);
4299 if( resume_all_threads
) {
4301 printf( "Continue process %d\n", tid
);
4303 printf( "Step/continue thread %d\n", tid
);
4307 printf( "Continue thread %d\n", tid
);
4309 printf( "Step just thread %d\n", tid
);
4312 if( vfork_in_flight
)
4313 printf( "Vfork in flight\n" );
4317 if( process_state
== RUNNING
)
4318 warning( "Internal error in resume logic; doing resume or step anyway." );
4320 if( !step
/* Asked to continue... */
4321 && resume_all_threads
/* whole process.. */
4322 && signal
!= 0 /* with a signal... */
4323 && more_events_left
> 0 ) { /* but we can't yet--save it! */
4325 /* Continue with signal means we have to set the pending
4326 * signal value for this thread.
4332 printf( "Saving signal %d for thread %d\n", signal
, tid
);
4335 k
= find_thread_info( tid
);
4338 k
->signal_value
= signal
;
4343 printf( "Why are we continuing a dead thread? (3)\n" );
4349 else if( debug_on
) {
4350 printf( "No thread info for tid %d\n", tid
);
4355 /* Are we faking this "continue" or "step"?
4357 * We used to do steps by continuing all the threads for
4358 * which the events had been handled already. While
4359 * conceptually nicer (hides it all in a lower level), this
4360 * can lead to starvation and a hang (e.g. all but one thread
4361 * are unhandled at a breakpoint just before a "join" operation,
4362 * and one thread is in the join, and the user wants to step that
4365 if( resume_all_threads
/* Whole process, therefore user command */
4366 && more_events_left
> 0 ) { /* But we can't do this yet--fake it! */
4370 /* No need to do any notes on a per-thread
4371 * basis--we're done!
4373 #ifdef WAIT_BUFFER_DEBUG
4375 printf( "Faking a process resume.\n" );
4382 #ifdef WAIT_BUFFER_DEBUG
4384 printf( "Faking a process step.\n" );
4389 p
= find_thread_info( tid
);
4391 warning( "No thread information for tid %d, 'next' command ignored.\n", tid
);
4399 printf( "Why are we continuing a dead thread? (3.5)\n" );
4402 if( p
->stepping_mode
!= DO_DEFAULT
) {
4403 warning( "Step or continue command applied to thread which is already stepping or continuing; command ignored." );
4409 p
->stepping_mode
= DO_STEP
;
4411 p
->stepping_mode
= DO_CONTINUE
;
4414 } /* Have thread info */
4415 } /* Must fake step or go */
4417 /* Execept for fake-steps, from here on we know we are
4418 * going to wind up with a running process which will
4421 new_process_state
= RUNNING
;
4423 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4424 * it was. (If GDB wanted it to start some other way, we have already
4425 * written a new PC value to the child.)
4427 * If this system does not support PT_STEP, a higher level function will
4428 * have called single_step() to transmute the step request into a
4429 * continue request (by setting breakpoints on all possible successor
4430 * instructions), so we don't have to worry about that here.
4433 if( resume_all_threads
) {
4435 * Regular user step: other threads get a "continue".
4437 threads_continue_all_but_one( tid
, signal
);
4438 clear_all_handled();
4439 clear_all_stepping_mode();
4443 /* "Fake step": gdb is stepping one thread over a
4444 * breakpoint, watchpoint, or out of a library load
4445 * event, etc. The rest just stay where they are.
4447 * Also used when there are pending events: we really
4448 * step the current thread, but leave the rest stopped.
4449 * Users can't request this, but "wait_for_inferior"
4452 thread_fake_step( tid
, signal
);
4454 /* Clear the "handled" state of this thread, because
4455 * we'll soon get a new event for it. Other events
4456 * stay as they were.
4458 clear_handled( tid
);
4459 clear_stepping_mode( tid
);
4460 new_process_state
= FAKE_STEPPING
;
4465 /* TT_LWP_CONTINUE can pass signals to threads,
4466 * TT_PROC_CONTINUE can't. So if there are any
4467 * signals to pass, we have to use the (slower)
4468 * loop over the stopped threads.
4470 * Equally, if we have to not continue some threads,
4471 * due to saved events, we have to use the loop.
4473 if( (signal
!= 0) || saved_signals_exist()) {
4474 if( resume_all_threads
) {
4478 printf( "Doing a continue by loop of all threads\n" );
4481 threads_continue_all_with_signals( tid
, signal
);
4483 clear_all_handled();
4484 clear_all_stepping_mode();
4489 printf( "Doing a continue w/signal of just thread %d\n", tid
);
4492 threads_continue_one_with_signal( tid
, signal
);
4494 /* Clear the "handled" state of this thread, because
4495 * we'll soon get a new event for it. Other events
4496 * can stay as they were.
4498 clear_handled( tid
);
4499 clear_stepping_mode( tid
);
4504 /* No signals to send.
4506 if( resume_all_threads
) {
4509 printf( "Doing a continue by process of process %d\n", tid
);
4512 if( more_events_left
> 0 ) {
4513 warning( "Losing buffered events on continue." );
4514 more_events_left
= 0;
4517 call_ttrace( TT_PROC_CONTINUE
,
4523 clear_all_handled();
4524 clear_all_stepping_mode();
4530 printf( "Doing a continue of just thread %d\n", tid
);
4531 if( is_terminated( tid
))
4532 printf( "Why are we continuing a dead thread? (5)\n" );
4536 call_ttrace( TT_LWP_CONTINUE
,
4542 /* Clear the "handled" state of this thread, because
4543 * we'll soon get a new event for it. Other events
4544 * can stay as they were.
4546 clear_handled( tid
);
4547 clear_stepping_mode( tid
);
4552 process_state
= new_process_state
;
4554 #ifdef WAIT_BUFFER_DEBUG
4556 printf( "Process set to %s\n",
4557 get_printable_name_of_process_state (process_state
) );
4561 #endif /* CHILD_RESUME */
4564 #ifdef ATTACH_DETACH
4568 * One worry is that we may not be attaching to "inferior_pid"
4569 * and thus may not want to clear out our data. FIXME?
4573 update_thread_state_after_attach( pid
, kind_of_go
)
4575 attach_continue_t kind_of_go
;
4578 ttstate_t thread_state
;
4582 /* The process better be stopped.
4584 if( process_state
!= STOPPED
4585 && process_state
!= VFORKING
)
4586 warning( "Internal error attaching." );
4588 /* Clear out old tthread info and start over. This has the
4589 * side effect of ensuring that the TRAP is reported as being
4590 * in the right thread (re-mapped from tid to pid).
4592 * It's because we need to add the tthread _now_ that we
4593 * need to call "clear_thread_info" _now_, and that's why
4594 * "require_notification_of_events" doesn't clear the thread
4595 * info (it's called later than this routine).
4597 clear_thread_info();
4600 for (tid
= get_process_first_stopped_thread_id (pid
, &thread_state
);
4602 tid
= get_process_next_stopped_thread_id (pid
, &thread_state
))
4611 printf( "Attaching to process %d, thread %d\n",
4616 /* Tell ourselves and the "rest of gdb" that this thread
4619 * This isn't really a hack. Other thread-based versions
4620 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4622 * We don't need to do mapping here, as we know this
4623 * is the first thread and thus gets the real pid
4624 * (and is "inferior_pid").
4626 * NOTE: it probably isn't the originating thread,
4627 * but that doesn't matter (we hope!).
4629 add_tthread( pid
, tid
);
4630 p
= find_thread_info( tid
);
4631 if( NULL
== p
) /* ?We just added it! */
4632 error( "Internal error adding a thread on attach." );
4634 copy_ttstate_t( &p
->last_stop_state
, thread_state
);
4637 if( DO_ATTACH_CONTINUE
== kind_of_go
) {
4639 * If we are going to CONTINUE afterwards,
4640 * raising a SIGTRAP, don't bother trying to
4641 * handle this event. But check first!
4643 switch( p
->last_stop_state
.tts_event
) {
4646 /* Ok to set this handled.
4651 warning( "Internal error; skipping event %s on process %d, thread %d.",
4652 get_printable_name_of_ttrace_event(
4653 p
->last_stop_state
.tts_event
),
4657 set_handled( pid
, tid
);
4661 /* There will be no "continue" opertion, so the
4662 * process remains stopped. Don't set any events
4663 * handled except the "gimmies".
4665 switch( p
->last_stop_state
.tts_event
) {
4668 /* Ok to ignore this.
4670 set_handled( pid
, tid
);
4675 /* Expected "other" FORK or EXEC event from a
4681 printf( "Internal error: failed to handle event %s on process %d, thread %d.",
4682 get_printable_name_of_ttrace_event(
4683 p
->last_stop_state
.tts_event
),
4688 add_thread( tid
); /* in thread.c */
4696 /* One mustn't call ttrace_wait() after attaching via ttrace,
4697 'cause the process is stopped already.
4699 However, the upper layers of gdb's execution control will
4700 want to wait after attaching (but not after forks, in
4701 which case they will be doing a "target_resume", anticipating
4702 a later TTEVT_EXEC or TTEVT_FORK event).
4704 To make this attach() implementation more compatible with
4705 others, we'll make the attached-to process raise a SIGTRAP.
4707 Issue: this continues only one thread. That could be
4708 dangerous if the thread is blocked--the process won't run
4709 and no trap will be raised. FIX! (check state.tts_flags?
4710 need one that's either TTS_WASRUNNING--but we've stopped
4711 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4713 if( DO_ATTACH_CONTINUE
== kind_of_go
) {
4714 tt_status
= call_real_ttrace(
4719 (TTRACE_ARG_TYPE
) target_signal_to_host (TARGET_SIGNAL_TRAP
),
4722 perror_with_name ("ttrace");
4724 clear_handled( a_thread
); /* So TRAP will be reported. */
4728 process_state
= RUNNING
;
4733 #endif /* ATTACH_DETACH */
4736 #ifdef ATTACH_DETACH
4737 /* Start debugging the process whose number is PID.
4746 tt_status
= call_real_ttrace (
4751 (TTRACE_ARG_TYPE
) TT_VERSION
,
4754 perror_with_name ("ttrace attach");
4756 /* If successful, the process is now stopped.
4758 process_state
= STOPPED
;
4760 /* Our caller ("attach_command" in "infcmd.c")
4761 * expects to do a "wait_for_inferior" after
4762 * the attach, so make sure the inferior is
4763 * running when we're done.
4765 update_thread_state_after_attach( pid
, DO_ATTACH_CONTINUE
);
4771 #if defined(CHILD_POST_ATTACH)
4773 child_post_attach (pid
)
4778 printf( "child-post-attach call\n" );
4781 require_notification_of_events (pid
);
4786 /* Stop debugging the process whose number is PID
4787 and continue it with signal number SIGNAL.
4788 SIGNAL = 0 means just continue it.
4795 call_ttrace (TT_PROC_DETACH
,
4798 (TTRACE_ARG_TYPE
) signal
,
4802 clear_thread_info();
4804 /* Process-state? */
4806 #endif /* ATTACH_DETACH */
4809 /* Default the type of the ttrace transfer to int. */
4810 #ifndef TTRACE_XFER_TYPE
4811 #define TTRACE_XFER_TYPE int
4815 _initialize_kernel_u_addr ()
4819 #if !defined (CHILD_XFER_MEMORY)
4820 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
4821 in the NEW_SUN_TTRACE case.
4822 It ought to be straightforward. But it appears that writing did
4823 not write the data that I specified. I cannot understand where
4824 it got the data that it actually did write. */
4826 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
4827 to debugger memory starting at MYADDR. Copy to inferior if
4830 Returns the length copied, which is either the LEN argument or zero.
4831 This xfer function does not do partial moves, since child_ops
4832 doesn't allow memory operations to cross below us in the target stack
4836 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
4841 struct target_ops
*target
; /* ignored */
4844 /* Round starting address down to longword boundary. */
4845 register CORE_ADDR addr
= memaddr
& - sizeof (TTRACE_XFER_TYPE
);
4846 /* Round ending address up; get number of longwords that makes. */
4848 = (((memaddr
+ len
) - addr
) + sizeof (TTRACE_XFER_TYPE
) - 1)
4849 / sizeof (TTRACE_XFER_TYPE
);
4850 /* Allocate buffer of that many longwords. */
4851 register TTRACE_XFER_TYPE
*buffer
4852 = (TTRACE_XFER_TYPE
*) alloca (count
* sizeof (TTRACE_XFER_TYPE
));
4856 /* Fill start and end extra bytes of buffer with existing memory data. */
4858 if (addr
!= memaddr
|| len
< (int) sizeof (TTRACE_XFER_TYPE
)) {
4859 /* Need part of initial word -- fetch it. */
4860 buffer
[0] = call_ttrace (TT_LWP_RDTEXT
,
4862 (TTRACE_ARG_TYPE
) addr
,
4867 if (count
> 1) /* FIXME, avoid if even boundary */
4869 buffer
[count
- 1] = call_ttrace (TT_LWP_RDTEXT
,
4872 (addr
+ (count
- 1) * sizeof (TTRACE_XFER_TYPE
))),
4877 /* Copy data to be written over corresponding part of buffer */
4879 memcpy ((char *) buffer
+ (memaddr
& (sizeof (TTRACE_XFER_TYPE
) - 1)),
4883 /* Write the entire buffer. */
4885 for (i
= 0; i
< count
; i
++, addr
+= sizeof (TTRACE_XFER_TYPE
))
4888 call_ttrace (TT_LWP_WRDATA
,
4890 (TTRACE_ARG_TYPE
) addr
,
4891 (TTRACE_ARG_TYPE
) buffer
[i
],
4895 /* Using the appropriate one (I or D) is necessary for
4896 Gould NP1, at least. */
4898 call_ttrace (TT_LWP_WRTEXT
,
4900 (TTRACE_ARG_TYPE
) addr
,
4901 (TTRACE_ARG_TYPE
) buffer
[i
],
4910 /* Read all the longwords */
4911 for (i
= 0; i
< count
; i
++, addr
+= sizeof (TTRACE_XFER_TYPE
))
4914 buffer
[i
] = call_ttrace (TT_LWP_RDTEXT
,
4916 (TTRACE_ARG_TYPE
) addr
,
4924 /* Copy appropriate bytes out of the buffer. */
4926 (char *) buffer
+ (memaddr
& (sizeof (TTRACE_XFER_TYPE
) - 1)),
4936 int udot_off
; /* Offset into user struct */
4937 int udot_val
; /* Value from user struct at udot_off */
4938 char mess
[128]; /* For messages */
4940 if (!target_has_execution
)
4942 error ("The program is not being run.");
4945 #if !defined (KERNEL_U_SIZE)
4947 /* Adding support for this command is easy. Typically you just add a
4948 routine, called "kernel_u_size" that returns the size of the user
4949 struct, to the appropriate *-nat.c file and then add to the native
4950 config file "#define KERNEL_U_SIZE kernel_u_size()" */
4951 error ("Don't know how large ``struct user'' is in this version of gdb.");
4955 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
4957 if ((udot_off
% 24) == 0)
4961 printf_filtered ("\n");
4963 printf_filtered ("%04x:", udot_off
);
4965 udot_val
= call_ttrace (TT_LWP_RUREGS
,
4967 (TTRACE_ARG_TYPE
) udot_off
,
4972 sprintf (mess
, "\nreading user struct at offset 0x%x", udot_off
);
4973 perror_with_name (mess
);
4975 /* Avoid using nonportable (?) "*" in print specs */
4976 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
4978 printf_filtered ("\n");
4982 #endif /* !defined (CHILD_XFER_MEMORY). */
4984 /* TTrace version of "target_pid_to_exec_file"
4987 child_pid_to_exec_file (tid
)
4990 static char exec_file_buffer
[1024];
4992 CORE_ADDR top_of_stack
;
4997 int saved_inferior_pid
;
4999 /* As of 10.x HP-UX, there's an explicit request to get the
5002 tt_status
= call_ttrace (TT_PROC_GET_PATHNAME
,
5004 (TTRACE_ARG_TYPE
) exec_file_buffer
,
5005 (TTRACE_ARG_TYPE
) sizeof (exec_file_buffer
) - 1,
5008 return exec_file_buffer
;
5010 /* ??rehrauer: The above request may or may not be broken. It
5011 doesn't seem to work when I use it. But, it may be designed
5012 to only work immediately after an exec event occurs. (I'm
5013 waiting for COSL to explain.)
5015 In any case, if it fails, try a really, truly amazingly gross
5016 hack that DDE uses, of pawing through the process' data
5017 segment to find the pathname.
5019 top_of_stack
= 0x7b03a000;
5023 /* On the chance that pid != inferior_pid, set inferior_pid
5024 to pid, so that (grrrr!) implicit uses of inferior_pid get
5027 saved_inferior_pid
= inferior_pid
;
5030 /* Try to grab a null-terminated string. */
5032 if (target_read_memory (top_of_stack
, four_chars
, 4) != 0)
5034 inferior_pid
= saved_inferior_pid
;
5037 for (i
= 0; i
< 4; i
++) {
5038 exec_file_buffer
[name_index
++] = four_chars
[i
];
5039 done
= (four_chars
[i
] == '\0');
5046 if (exec_file_buffer
[0] == '\0')
5048 inferior_pid
= saved_inferior_pid
;
5052 inferior_pid
= saved_inferior_pid
;
5053 return exec_file_buffer
;
5058 pre_fork_inferior ()
5062 status
= pipe (startup_semaphore
.parent_channel
);
5064 warning ("error getting parent pipe for startup semaphore");
5068 status
= pipe (startup_semaphore
.child_channel
);
5070 warning ("error getting child pipe for startup semaphore");
5075 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5076 * ultimately from "follow_inferior_fork" in infrun.c,
5077 * itself called from "resume".
5079 * This seems to be intended to attach after a fork or
5080 * vfork, while "attach" is used to attach to a pid
5081 * given by the user. The check for an existing attach
5082 * seems odd--it always fails in our test system.
5085 hppa_require_attach (pid
)
5091 unsigned int regs_offset
;
5092 process_state_t old_process_state
= process_state
;
5094 /* Are we already attached? There appears to be no explicit
5095 * way to answer this via ttrace, so we try something which
5096 * should be innocuous if we are attached. If that fails,
5097 * then we assume we're not attached, and so attempt to make
5101 tt_status
= call_real_ttrace (TT_PROC_STOP
,
5104 (TTRACE_ARG_TYPE
) TT_NIL
,
5105 (TTRACE_ARG_TYPE
) TT_NIL
,
5110 /* No change to process-state!
5117 /* If successful, the process is now stopped. But if
5118 * we're VFORKING, the parent is still running, so don't
5119 * change the process state.
5121 if( process_state
!= VFORKING
)
5122 process_state
= STOPPED
;
5124 /* If we were already attached, you'd think that we
5125 * would need to start going again--but you'd be wrong,
5126 * as the fork-following code is actually in the middle
5127 * of the "resume" routine in in "infrun.c" and so
5128 * will (almost) immediately do a resume.
5130 * On the other hand, if we are VFORKING, which means
5131 * that the child and the parent share a process for a
5132 * while, we know that "resume" won't be resuming
5133 * until the child EXEC event is seen. But we still
5134 * don't want to continue, as the event is already
5137 update_thread_state_after_attach( pid
, DONT_ATTACH_CONTINUE
);
5138 } /* STOP succeeded */
5144 hppa_require_detach (pid
, signal
)
5150 /* If signal is non-zero, we must pass the signal on to the active
5151 thread prior to detaching. We do this by continuing the threads
5157 threads_continue_all_with_signals( pid
, signal
);
5161 tt_status
= call_ttrace (TT_PROC_DETACH
,
5167 errno
= 0; /* Ignore any errors. */
5169 /* process_state? */
5174 #if defined(HPPA_GET_PROCESS_EVENTS)
5175 process_event_vector
5176 hppa_get_process_events (pid
, wait_status
, must_continue_pid_after
)
5179 int * must_continue_pid_after
;
5182 ttstate_t ttrace_state
;
5183 process_event_vector events
= PEVT_NONE
;
5185 /* This is always 1 with ptrace. */
5186 *must_continue_pid_after
= 0;
5189 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
5191 (TTRACE_ARG_TYPE
) &ttrace_state
,
5192 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
5195 perror_with_name ("ttrace");
5199 if (ttrace_state
.tts_event
& TTEVT_SIGNAL
)
5200 events
|= PEVT_SIGNAL
;
5201 if (ttrace_state
.tts_event
& TTEVT_FORK
)
5202 events
|= PEVT_FORK
;
5203 if (ttrace_state
.tts_event
& TTEVT_VFORK
)
5204 events
|= PEVT_VFORK
;
5205 if (ttrace_state
.tts_event
& TTEVT_EXEC
)
5206 events
|= PEVT_EXEC
;
5207 if (ttrace_state
.tts_event
& TTEVT_EXIT
)
5208 events
|= PEVT_EXIT
;
5212 #endif /* HPPA_GET_PROCESS_EVENTS */
5215 /* Given the starting address of a memory page, hash it to a bucket in
5216 the memory page dictionary.
5219 get_dictionary_bucket_of_page (page_start
)
5220 CORE_ADDR page_start
;
5224 hash
= (page_start
/ memory_page_dictionary
.page_size
);
5225 hash
= hash
% MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
;
5231 /* Given a memory page's starting address, get (i.e., find an existing
5232 or create a new) dictionary entry for the page. The page will be
5233 write-protected when this function returns, but may have a reference
5234 count of 0 (if the page was newly-added to the dictionary).
5236 static memory_page_t
*
5237 get_dictionary_entry_of_page (pid
, page_start
)
5239 CORE_ADDR page_start
;
5242 memory_page_t
* page
= NULL
;
5243 memory_page_t
* previous_page
= NULL
;
5245 /* We're going to be using the dictionary now, than-kew. */
5246 require_memory_page_dictionary (pid
);
5248 /* Try to find an existing dictionary entry for this page. Hash
5249 on the page's starting address.
5251 bucket
= get_dictionary_bucket_of_page (page_start
);
5252 page
= &memory_page_dictionary
.buckets
[bucket
];
5253 while (page
!= NULL
)
5255 if (page
->page_start
== page_start
)
5257 previous_page
= page
;
5261 /* Did we find a dictionary entry for this page? If not, then
5262 add it to the dictionary now.
5266 /* Create a new entry. */
5267 page
= (memory_page_t
*) xmalloc (sizeof (memory_page_t
));
5268 page
->page_start
= page_start
;
5269 page
->reference_count
= 0;
5271 page
->previous
= NULL
;
5273 /* We'll write-protect the page now, if that's allowed. */
5274 page
->original_permissions
= write_protect_page (pid
, page_start
);
5276 /* Add the new entry to the dictionary. */
5277 page
->previous
= previous_page
;
5278 previous_page
->next
= page
;
5280 memory_page_dictionary
.page_count
++;
5288 remove_dictionary_entry_of_page (pid
, page
)
5290 memory_page_t
* page
;
5292 /* Restore the page's original permissions. */
5293 unwrite_protect_page (pid
, page
->page_start
, page
->original_permissions
);
5295 /* Kick the page out of the dictionary. */
5296 if (page
->previous
!= NULL
)
5297 page
->previous
->next
= page
->next
;
5298 if (page
->next
!= NULL
)
5299 page
->next
->previous
= page
->previous
;
5301 /* Just in case someone retains a handle to this after it's freed. */
5302 page
->page_start
= (CORE_ADDR
) 0;
5304 memory_page_dictionary
.page_count
--;
5311 hppa_enable_syscall_events (pid
)
5315 ttevent_t ttrace_events
;
5317 /* Get the set of events that are currently enabled. */
5318 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
5320 (TTRACE_ARG_TYPE
) &ttrace_events
,
5321 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5324 perror_with_name ("ttrace");
5326 /* Add syscall events to that set. */
5327 ttrace_events
.tte_events
|= TTEVT_SYSCALL_ENTRY
;
5328 ttrace_events
.tte_events
|= TTEVT_SYSCALL_RETURN
;
5330 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
5332 (TTRACE_ARG_TYPE
) &ttrace_events
,
5333 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5336 perror_with_name ("ttrace");
5341 hppa_disable_syscall_events (pid
)
5345 ttevent_t ttrace_events
;
5347 /* Get the set of events that are currently enabled. */
5348 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
5350 (TTRACE_ARG_TYPE
) &ttrace_events
,
5351 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5354 perror_with_name ("ttrace");
5356 /* Remove syscall events from that set. */
5357 ttrace_events
.tte_events
&= ~TTEVT_SYSCALL_ENTRY
;
5358 ttrace_events
.tte_events
&= ~TTEVT_SYSCALL_RETURN
;
5360 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
5362 (TTRACE_ARG_TYPE
) &ttrace_events
,
5363 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5366 perror_with_name ("ttrace");
5370 /* The address range beginning with START and ending with START+LEN-1
5371 (inclusive) is to be watched via page-protection by a new watchpoint.
5372 Set protection for all pages that overlap that range.
5374 Note that our caller sets TYPE to:
5375 0 for a bp_hardware_watchpoint,
5376 1 for a bp_read_watchpoint,
5377 2 for a bp_access_watchpoint
5379 (Yes, this is intentionally (though lord only knows why) different
5380 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5383 hppa_insert_hw_watchpoint (pid
, start
, len
, type
)
5389 CORE_ADDR page_start
;
5390 int dictionary_was_empty
;
5393 LONGEST range_size_in_pages
;
5396 error ("read or access hardware watchpoints not supported on HP-UX");
5398 /* Examine all pages in the address range. */
5399 require_memory_page_dictionary ();
5401 dictionary_was_empty
= (memory_page_dictionary
.page_count
== (LONGEST
) 0);
5403 page_size
= memory_page_dictionary
.page_size
;
5404 page_start
= (start
/ page_size
) * page_size
;
5405 range_size_in_pages
= ((LONGEST
) len
+ (LONGEST
) page_size
- 1) / (LONGEST
) page_size
;
5407 for (page_id
=0; page_id
< range_size_in_pages
; page_id
++, page_start
+=page_size
)
5409 memory_page_t
* page
;
5411 /* This gets the page entered into the dictionary if it was
5412 not already entered.
5414 page
= get_dictionary_entry_of_page (pid
, page_start
);
5415 page
->reference_count
++;
5418 /* Our implementation depends on seeing calls to kernel code, for the
5419 following reason. Here we ask to be notified of syscalls.
5421 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5424 But when kernel code accesses the page, it doesn't give a SIGBUS.
5425 Rather, the system call that touched the page fails, with errno=EFAULT.
5428 We could accomodate this "feature" by asking to be notified of syscall
5429 entries & exits; upon getting an entry event, disabling page-protections;
5430 upon getting an exit event, reenabling page-protections and then checking
5431 if any watchpoints triggered.
5433 However, this turns out to be a real performance loser. syscalls are
5434 usually a frequent occurrence. Having to unprotect-reprotect all watched
5435 pages, and also to then read all watched memory locations and compare for
5436 triggers, can be quite expensive.
5438 Instead, we'll only ask to be notified of syscall exits. When we get
5439 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5440 we can just continue the inferior.
5442 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5443 hackish stuff to determine whether the failure really was due to a
5444 page-protect trap on a watched location.
5446 if (dictionary_was_empty
)
5447 hppa_enable_syscall_events (pid
);
5451 /* The address range beginning with START and ending with START+LEN-1
5452 (inclusive) was being watched via page-protection by a watchpoint
5453 which has been removed. Remove protection for all pages that
5454 overlap that range, which are not also being watched by other
5458 hppa_remove_hw_watchpoint (pid
, start
, len
, type
)
5464 CORE_ADDR page_start
;
5465 int dictionary_is_empty
;
5468 LONGEST range_size_in_pages
;
5471 error ("read or access hardware watchpoints not supported on HP-UX");
5473 /* Examine all pages in the address range. */
5474 require_memory_page_dictionary ();
5476 page_size
= memory_page_dictionary
.page_size
;
5477 page_start
= (start
/ page_size
) * page_size
;
5478 range_size_in_pages
= ((LONGEST
) len
+ (LONGEST
) page_size
- 1) / (LONGEST
) page_size
;
5480 for (page_id
=0; page_id
< range_size_in_pages
; page_id
++, page_start
+=page_size
)
5482 memory_page_t
* page
;
5484 page
= get_dictionary_entry_of_page (pid
, page_start
);
5485 page
->reference_count
--;
5487 /* Was this the last reference of this page? If so, then we
5488 must scrub the entry from the dictionary, and also restore
5489 the page's original permissions.
5491 if (page
->reference_count
== 0)
5492 remove_dictionary_entry_of_page (pid
, page
);
5495 dictionary_is_empty
= (memory_page_dictionary
.page_count
== (LONGEST
) 0);
5497 /* If write protections are currently disallowed, then that implies that
5498 wait_for_inferior believes that the inferior is within a system call.
5499 Since we want to see both syscall entry and return, it's clearly not
5500 good to disable syscall events in this state!
5502 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5503 "inferior is between syscall events now". Oh well.
5505 if (dictionary_is_empty
&& memory_page_dictionary
.page_protections_allowed
)
5506 hppa_disable_syscall_events (pid
);
5510 /* Could we implement a watchpoint of this type via our available
5513 This query does not consider whether a particular address range
5514 could be so watched, but just whether support is generally available
5515 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5516 query that answers whether a particular range should be watched via
5520 hppa_can_use_hw_watchpoint (type
, cnt
, ot
)
5525 return (type
== bp_hardware_watchpoint
);
5529 /* Assuming we could set a hardware watchpoint on this address, do
5530 we think it would be profitable ("a good idea") to do so? If not,
5531 we can always set a regular (aka single-step & test) watchpoint
5535 hppa_range_profitable_for_hw_watchpoint (pid
, start
, len
)
5540 int range_is_stack_based
;
5541 int range_is_accessible
;
5542 CORE_ADDR page_start
;
5545 LONGEST range_size_in_pages
;
5547 /* ??rehrauer: For now, say that all addresses are potentially
5548 profitable. Possibly later we'll want to test the address
5551 range_is_stack_based
= 0;
5553 /* If any page in the range is inaccessible, then we cannot
5554 really use hardware watchpointing, even though our client
5555 thinks we can. In that case, it's actually an error to
5556 attempt to use hw watchpoints, so we'll tell our client
5557 that the range is "unprofitable", and hope that they listen...
5559 range_is_accessible
= 1; /* Until proven otherwise. */
5561 /* Examine all pages in the address range. */
5563 page_size
= sysconf (_SC_PAGE_SIZE
);
5565 /* If we can't determine page size, we're hosed. Tell our
5566 client it's unprofitable to use hw watchpoints for this
5569 if (errno
|| (page_size
<= 0))
5575 page_start
= (start
/ page_size
) * page_size
;
5576 range_size_in_pages
= len
/ (LONGEST
)page_size
;
5578 for (page
=0; page
< range_size_in_pages
; page
++, page_start
+=page_size
)
5581 int page_permissions
;
5583 /* Is this page accessible? */
5585 tt_status
= call_ttrace (TT_PROC_GET_MPROTECT
,
5587 (TTRACE_ARG_TYPE
) page_start
,
5589 (TTRACE_ARG_TYPE
) &page_permissions
);
5590 if (errno
|| (tt_status
< 0))
5593 range_is_accessible
= 0;
5597 /* Yes, go for another... */
5600 return (! range_is_stack_based
&& range_is_accessible
);
5605 hppa_pid_or_tid_to_str (id
)
5608 static char buf
[100]; /* Static because address returned. */
5610 /* Does this appear to be a process? If so, print it that way. */
5611 if (is_process_id (id
))
5612 return hppa_pid_to_str (id
);
5614 /* Else, print both the GDB thread number and the system thread id. */
5615 sprintf (buf
, "thread %d (", pid_to_thread_id (id
));
5616 strcat (buf
, hppa_tid_to_str (id
));
5617 strcat (buf
, ")\0");
5623 /* If the current pid is not the pid this module reported
5624 * from "proc_wait" with the most recent event, then the
5625 * user has switched threads.
5627 * If the last reported event was a breakpoint, then return
5628 * the old thread id, else return 0.
5631 hppa_switched_threads( gdb_pid
)
5634 if( gdb_pid
== old_gdb_pid
) {
5636 * Core gdb is working with the same pid that it
5637 * was before we reported the last event. This
5638 * is ok: e.g. we reported hitting a thread-specific
5639 * breakpoint, but we were reporting the wrong
5640 * thread, so the core just ignored the event.
5642 * No thread switch has happened.
5646 else if( gdb_pid
== reported_pid
) {
5648 * Core gdb is working with the pid we reported, so
5649 * any continue or step will be able to figure out
5650 * that it needs to step over any hit breakpoints
5651 * without our (i.e. PREPARE_TO_PROCEED's) help.
5655 else if( !reported_bpt
) {
5657 * The core switched, but we didn't just report a
5658 * breakpoint, so there's no just-hit breakpoint
5659 * instruction at "reported_pid"'s PC, and thus there
5660 * is no need to step over it.
5665 /* There's been a real switch, and we reported
5666 * a hit breakpoint. Let "hppa_prepare_to_proceed"
5667 * know, so it can see whether the breakpoint is
5670 return reported_pid
;
5673 /* Keep compiler happy with an obvious return at the end.
5679 hppa_ensure_vforking_parent_remains_stopped (pid
)
5682 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5689 hppa_resume_execd_vforking_child_to_get_parent_vfork ()
5691 return 0; /* No, the parent vfork is available now. */
5697 _initialize_infttrace ()
5699 /* Initialize the ttrace-based hardware watchpoint implementation. */
5700 memory_page_dictionary
.page_count
= (LONGEST
) -1;
5701 memory_page_dictionary
.page_protections_allowed
= 1;
5704 memory_page_dictionary
.page_size
= sysconf (_SC_PAGE_SIZE
);
5706 if (errno
|| (memory_page_dictionary
.page_size
<= 0))
5707 perror_with_name ("sysconf");