1 /* Low level Unix child interface to ttrace, for GDB when running under HP-UX.
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1998, 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "gdb_string.h"
30 #include "gdbthread.h"
31 #include "infttrace.h"
33 /* We need pstat functionality so that we can get the exec file
34 for a process we attach to.
36 According to HP, we should use the 64bit interfaces, so we
37 define _PSTAT64 to achieve this. */
39 #include <sys/pstat.h>
41 /* Some hackery to work around a use of the #define name NO_FLAGS
42 * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
45 #define INFTTRACE_TEMP_HACK NO_FLAGS
50 #include <sys/types.h>
53 #include <sys/param.h>
56 #include <sys/ioctl.h>
58 #include <sys/ttrace.h>
62 #ifdef PTRACE_IN_WRONG_PLACE
65 #include <sys/ptrace.h>
67 #endif /* NO_PTRACE_H */
69 /* Second half of the hackery above. Non-ANSI C, so
70 * we can't use "#error", alas.
73 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
74 /* #error "Hackery to remove warning didn't work right" */
76 /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
79 /* #error "Didn't get expected re-definition of NO_FLAGS" */
80 #define NO_FLAGS INFTTRACE_TEMP_HACK
83 #if !defined (PT_SETTRC)
84 #define PT_SETTRC 0 /* Make process traceable by parent */
86 #if !defined (PT_READ_I)
87 #define PT_READ_I 1 /* Read word from text space */
89 #if !defined (PT_READ_D)
90 #define PT_READ_D 2 /* Read word from data space */
92 #if !defined (PT_READ_U)
93 #define PT_READ_U 3 /* Read word from kernel user struct */
95 #if !defined (PT_WRITE_I)
96 #define PT_WRITE_I 4 /* Write word to text space */
98 #if !defined (PT_WRITE_D)
99 #define PT_WRITE_D 5 /* Write word to data space */
101 #if !defined (PT_WRITE_U)
102 #define PT_WRITE_U 6 /* Write word to kernel user struct */
104 #if !defined (PT_CONTINUE)
105 #define PT_CONTINUE 7 /* Continue after signal */
107 #if !defined (PT_STEP)
108 #define PT_STEP 9 /* Set flag for single stepping */
110 #if !defined (PT_KILL)
111 #define PT_KILL 8 /* Send child a SIGKILL signal */
115 #define PT_ATTACH PTRACE_ATTACH
118 #define PT_DETACH PTRACE_DETACH
123 #include <sys/file.h>
126 /* This semaphore is used to coordinate the child and parent processes
127 after a fork(), and before an exec() by the child. See parent_attach_all
132 int parent_channel
[2]; /* Parent "talks" to [1], child "listens" to [0] */
133 int child_channel
[2]; /* Child "talks" to [1], parent "listens" to [0] */
138 #define SEM_LISTEN (0)
140 static startup_semaphore_t startup_semaphore
;
142 /* See can_touch_threads_of_process for details. */
143 static int vforking_child_pid
= 0;
144 static int vfork_in_flight
= 0;
146 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
148 #define TT_OK( _status, _errno ) \
149 (((_status) == 1) && ((_errno) == 0))
151 #define TTRACE_ARG_TYPE uint64_t
153 /* When supplied as the "addr" operand, ttrace interprets this
154 to mean, "from the current address".
156 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
158 /* When supplied as the "addr", "data" or "addr2" operand for most
159 requests, ttrace interprets this to mean, "pay no heed to this
162 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
164 /* This is capable of holding the value of a 32-bit register. The
165 value is always left-aligned in the buffer; i.e., [0] contains
166 the most-significant byte of the register's value, and [sizeof(reg)]
167 contains the least-significant value.
169 ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
170 that registers are 32-bits on HP-UX. The latter assumption changes
173 typedef int register_value_t
;
175 /********************************************************************
181 The rest of GDB sees threads as being things with different
182 "pid" (process id) values. See "thread.c" for details. The
183 separate threads will be seen and reacted to if infttrace passes
184 back different pid values (for _events_). See wait_for_inferior
187 So infttrace is going to use thread ids externally, pretending
188 they are process ids, and keep track internally so that it can
189 use the real process id (and thread id) when calling ttrace.
191 The data structure that supports this is a linked list of the
192 current threads. Since at some date infttrace will have to
193 deal with multiple processes, each list element records its
194 corresponding pid, rather than having a single global.
196 Note that the list is only approximately current; that's ok, as
197 it's up to date when we need it (we hope!). Also, it can contain
198 dead threads, as there's no harm if it does.
200 The approach taken here is to bury the translation from external
201 to internal inside "call_ttrace" and a few other places.
203 There are some wrinkles:
205 o When GDB forks itself to create the debug target process,
206 there's only a pid of 0 around in the child, so the
207 TT_PROC_SETTRC operation uses a more direct call to ttrace;
208 Similiarly, the initial setting of the event mask happens
209 early as well, and so is also special-cased, and an attach
212 o We define an unthreaded application as having a "pseudo"
215 o To keep from confusing the rest of GDB, we don't switch
216 the PID for the pseudo thread to a TID. A table will help:
218 Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ...
220 Our thread list stores: pid pid pid pid ...
223 Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ...
225 Both pid and tid0 will map to tid0, as there are infttrace.c-internal
226 calls to ttrace using tid0.
230 Since we're implementing the "stop the world" model, sub-model
231 "other threads run during step", we have some stuff to do:
233 o User steps require continuing all threads other than the
234 one the user is stepping;
236 o Internal debugger steps (such as over a breakpoint or watchpoint,
237 but not out of a library load thunk) require stepping only
238 the selected thread; this means that we have to report the
239 step finish on that thread, which can lead to complications;
241 o When a thread is created, it is created running, rather
242 than stopped--so we have to stop it.
244 The OS doesn't guarantee the stopped thread list will be stable,
245 no does it guarantee where on the stopped thread list a thread
246 that is single-stepped will wind up: it's possible that it will
247 be off the list for a while, it's possible the step will complete
248 and it will be re-posted to the end...
250 This means we have to scan the stopped thread list, build up
251 a work-list, and then run down the work list; we can't do the
252 step/continue during the scan.
256 Then there's the issue of waiting for an event. We do this by
257 noticing how many events are reported at the end of each wait.
258 From then on, we "fake" all resumes and steps, returning instantly,
259 and don't do another wait. Once all pending events are reported,
260 we can really resume again.
262 To keep this hidden, all the routines which know about tids and
263 pids or real events and simulated ones are static (file-local).
265 This code can make lots of calls to ttrace, in particular it
266 can spin down the list of thread states more than once. If this
267 becomes a performance hit, the spin could be done once and the
268 various "tsp" blocks saved, keeping all later spins in this
271 The O/S doesn't promise to keep the list straight, and so we must
272 re-scan a lot. By observation, it looks like a single-step/wait
273 puts the stepped thread at the end of the list but doesn't change
276 ****************************************************************
279 /* Uncomment these to turn on various debugging output */
280 /* #define THREAD_DEBUG */
281 /* #define WAIT_BUFFER_DEBUG */
282 /* #define PARANOIA */
285 #define INFTTRACE_ALL_THREADS (-1)
286 #define INFTTRACE_STEP (1)
287 #define INFTTRACE_CONTINUE (0)
289 /* FIX: this is used in inftarg.c/child_wait, in a hack.
291 extern int not_same_real_pid
;
293 /* This is used to count buffered events.
295 static unsigned int more_events_left
= 0;
299 typedef enum process_state_enum
303 FAKE_CONTINUE
, /* For later use */
310 static process_state_t process_state
= STOPPED
;
312 /* User-specified stepping modality.
314 typedef enum stepping_mode_enum
316 DO_DEFAULT
, /* ...which is a continue! */
322 /* Action to take on an attach, depends on
323 * what kind (user command, fork, vfork).
325 * At the moment, this is either:
327 * o continue with a SIGTRAP signal, or
331 typedef enum attach_continue_enum
338 /* This flag is true if we are doing a step-over-bpt
339 * with buffered events. We will have to be sure to
340 * report the right thread, as otherwise the spaghetti
341 * code in "infrun.c/wait_for_inferior" will get
344 static int doing_fake_step
= 0;
345 static lwpid_t fake_step_tid
= 0;
348 /****************************************************
349 * Thread information structure routines and types. *
350 ****************************************************
353 struct thread_info_struct
355 int am_pseudo
; /* This is a pseudo-thread for the process. */
356 int pid
; /* Process ID */
357 lwpid_t tid
; /* Thread ID */
358 int handled
; /* 1 if a buffered event was handled. */
359 int seen
; /* 1 if this thread was seen on a traverse. */
360 int terminated
; /* 1 if thread has terminated. */
361 int have_signal
; /* 1 if signal to be sent */
362 enum target_signal signal_value
; /* Signal to send */
363 int have_start
; /* 1 if alternate starting address */
364 stepping_mode_t stepping_mode
; /* Whether to step or continue */
365 CORE_ADDR start
; /* Where to start */
366 int have_state
; /* 1 if the event state has been set */
367 ttstate_t last_stop_state
; /* The most recently-waited event for this thread. */
368 struct thread_info_struct
369 *next
; /* All threads are linked via this field. */
370 struct thread_info_struct
371 *next_pseudo
; /* All pseudo-threads are linked via this field. */
376 struct thread_info_header_struct
380 thread_info
*head_pseudo
;
385 static thread_info_header thread_head
=
387 static thread_info_header deleted_threads
=
390 static ptid_t saved_real_ptid
;
393 /*************************************************
394 * Debugging support functions *
395 *************************************************
398 get_raw_pc (lwpid_t ttid
)
400 unsigned long pc_val
;
404 offset
= register_addr (PC_REGNUM
, U_REGS_OFFSET
);
405 res
= read_from_register_save_state (
407 (TTRACE_ARG_TYPE
) offset
,
412 return (CORE_ADDR
) pc_val
;
416 return (CORE_ADDR
) 0;
421 get_printable_name_of_stepping_mode (stepping_mode_t mode
)
430 return "DO_CONTINUE";
432 return "?unknown mode?";
436 /* This function returns a pointer to a string describing the
437 * ttrace event being reported.
440 get_printable_name_of_ttrace_event (ttevents_t event
)
442 /* This enumeration is "gappy", so don't use a table. */
449 return "TTEVT_SIGNAL";
457 return "TTEVT_VFORK";
458 case TTEVT_SYSCALL_RETURN
:
459 return "TTEVT_SYSCALL_RETURN";
460 case TTEVT_LWP_CREATE
:
461 return "TTEVT_LWP_CREATE";
462 case TTEVT_LWP_TERMINATE
:
463 return "TTEVT_LWP_TERMINATE";
465 return "TTEVT_LWP_EXIT";
466 case TTEVT_LWP_ABORT_SYSCALL
:
467 return "TTEVT_LWP_ABORT_SYSCALL";
468 case TTEVT_SYSCALL_ENTRY
:
469 return "TTEVT_SYSCALL_ENTRY";
470 case TTEVT_SYSCALL_RESTART
:
471 return "TTEVT_SYSCALL_RESTART";
473 return "?new event?";
478 /* This function translates the ttrace request enumeration into
479 * a character string that is its printable (aka "human readable")
483 get_printable_name_of_ttrace_request (ttreq_t request
)
485 if (!IS_TTRACE_REQ (request
))
488 /* This enumeration is "gappy", so don't use a table. */
492 return "TT_PROC_SETTRC";
494 return "TT_PROC_ATTACH";
496 return "TT_PROC_DETACH";
498 return "TT_PROC_RDTEXT";
500 return "TT_PROC_WRTEXT";
502 return "TT_PROC_RDDATA";
504 return "TT_PROC_WRDATA";
506 return "TT_PROC_STOP";
507 case TT_PROC_CONTINUE
:
508 return "TT_PROC_CONTINUE";
509 case TT_PROC_GET_PATHNAME
:
510 return "TT_PROC_GET_PATHNAME";
511 case TT_PROC_GET_EVENT_MASK
:
512 return "TT_PROC_GET_EVENT_MASK";
513 case TT_PROC_SET_EVENT_MASK
:
514 return "TT_PROC_SET_EVENT_MASK";
515 case TT_PROC_GET_FIRST_LWP_STATE
:
516 return "TT_PROC_GET_FIRST_LWP_STATE";
517 case TT_PROC_GET_NEXT_LWP_STATE
:
518 return "TT_PROC_GET_NEXT_LWP_STATE";
520 return "TT_PROC_EXIT";
521 case TT_PROC_GET_MPROTECT
:
522 return "TT_PROC_GET_MPROTECT";
523 case TT_PROC_SET_MPROTECT
:
524 return "TT_PROC_SET_MPROTECT";
525 case TT_PROC_SET_SCBM
:
526 return "TT_PROC_SET_SCBM";
528 return "TT_LWP_STOP";
529 case TT_LWP_CONTINUE
:
530 return "TT_LWP_CONTINUE";
532 return "TT_LWP_SINGLE";
534 return "TT_LWP_RUREGS";
536 return "TT_LWP_WUREGS";
537 case TT_LWP_GET_EVENT_MASK
:
538 return "TT_LWP_GET_EVENT_MASK";
539 case TT_LWP_SET_EVENT_MASK
:
540 return "TT_LWP_SET_EVENT_MASK";
541 case TT_LWP_GET_STATE
:
542 return "TT_LWP_GET_STATE";
549 /* This function translates the process state enumeration into
550 * a character string that is its printable (aka "human readable")
554 get_printable_name_of_process_state (process_state_t process_state
)
556 switch (process_state
)
561 return "FAKE_STEPPING";
569 return "?some unknown state?";
573 /* Set a ttrace thread state to a safe, initial state.
576 clear_ttstate_t (ttstate_t
*tts
)
580 tts
->tts_user_tid
= 0;
581 tts
->tts_event
= TTEVT_NONE
;
584 /* Copy ttrace thread state TTS_FROM into TTS_TO.
587 copy_ttstate_t (ttstate_t
*tts_to
, ttstate_t
*tts_from
)
589 memcpy ((char *) tts_to
, (char *) tts_from
, sizeof (*tts_to
));
592 /* Are there any live threads we know about?
595 any_thread_records (void)
597 return (thread_head
.count
> 0);
600 /* Create, fill in and link in a thread descriptor.
603 create_thread_info (int pid
, lwpid_t tid
)
607 int thread_count_of_pid
;
609 new_p
= xmalloc (sizeof (thread_info
));
612 new_p
->have_signal
= 0;
613 new_p
->have_start
= 0;
614 new_p
->have_state
= 0;
615 clear_ttstate_t (&new_p
->last_stop_state
);
616 new_p
->am_pseudo
= 0;
619 new_p
->terminated
= 0;
621 new_p
->next_pseudo
= NULL
;
622 new_p
->stepping_mode
= DO_DEFAULT
;
624 if (0 == thread_head
.count
)
628 printf ("First thread, pid %d tid %d!\n", pid
, tid
);
630 saved_real_ptid
= inferior_ptid
;
636 printf ("Subsequent thread, pid %d tid %d\n", pid
, tid
);
640 /* Another day, another thread...
644 /* The new thread always goes at the head of the list.
646 new_p
->next
= thread_head
.head
;
647 thread_head
.head
= new_p
;
649 /* Is this the "pseudo" thread of a process? It is if there's
650 * no other thread for this process on the list. (Note that this
651 * accomodates multiple processes, such as we see even for simple
652 * cases like forking "non-threaded" programs.)
654 p
= thread_head
.head
;
655 thread_count_of_pid
= 0;
658 if (p
->pid
== new_p
->pid
)
659 thread_count_of_pid
++;
663 /* Did we see any other threads for this pid? (Recall that we just
664 * added this thread to the list...)
666 if (thread_count_of_pid
== 1)
668 new_p
->am_pseudo
= 1;
669 new_p
->next_pseudo
= thread_head
.head_pseudo
;
670 thread_head
.head_pseudo
= new_p
;
676 /* Get rid of our thread info.
679 clear_thread_info (void)
686 printf ("Clearing all thread info\n");
689 p
= thread_head
.head
;
697 thread_head
.head
= NULL
;
698 thread_head
.head_pseudo
= NULL
;
699 thread_head
.count
= 0;
701 p
= deleted_threads
.head
;
709 deleted_threads
.head
= NULL
;
710 deleted_threads
.head_pseudo
= NULL
;
711 deleted_threads
.count
= 0;
713 /* No threads, so can't have pending events.
715 more_events_left
= 0;
718 /* Given a tid, find the thread block for it.
721 find_thread_info (lwpid_t tid
)
725 for (p
= thread_head
.head
; p
; p
= p
->next
)
733 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
744 /* For any but the pseudo thread, this maps to the
745 * thread ID. For the pseudo thread, if you pass either
746 * the thread id or the PID, you get the pseudo thread ID.
748 * We have to be prepared for core gdb to ask about
749 * deleted threads. We do the map, but we don't like it.
752 map_from_gdb_tid (lwpid_t gdb_tid
)
756 /* First assume gdb_tid really is a tid, and try to find a
757 * matching entry on the threads list.
759 for (p
= thread_head
.head
; p
; p
= p
->next
)
761 if (p
->tid
== gdb_tid
)
765 /* It doesn't appear to be a tid; perhaps it's really a pid?
766 * Try to find a "pseudo" thread entry on the threads list.
768 for (p
= thread_head
.head_pseudo
; p
!= NULL
; p
= p
->next_pseudo
)
770 if (p
->pid
== gdb_tid
)
774 /* Perhaps it's the tid of a deleted thread we may still
775 * have some knowledge of?
777 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
779 if (p
->tid
== gdb_tid
)
783 /* Or perhaps it's the pid of a deleted process we may still
786 for (p
= deleted_threads
.head_pseudo
; p
!= NULL
; p
= p
->next_pseudo
)
788 if (p
->pid
== gdb_tid
)
792 return 0; /* Error? */
795 /* Map the other way: from a real tid to the
796 * "pid" known by core gdb. This tid may be
797 * for a thread that just got deleted, so we
798 * also need to consider deleted threads.
801 map_to_gdb_tid (lwpid_t real_tid
)
805 for (p
= thread_head
.head
; p
; p
= p
->next
)
807 if (p
->tid
== real_tid
)
816 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
818 if (p
->tid
== real_tid
)
820 return p
->pid
; /* Error? */
825 return 0; /* Error? Never heard of this thread! */
828 /* Do any threads have saved signals?
831 saved_signals_exist (void)
835 for (p
= thread_head
.head
; p
; p
= p
->next
)
846 /* Is this the tid for the zero-th thread?
849 is_pseudo_thread (lwpid_t tid
)
851 thread_info
*p
= find_thread_info (tid
);
852 if (NULL
== p
|| p
->terminated
)
858 /* Is this thread terminated?
861 is_terminated (lwpid_t tid
)
863 thread_info
*p
= find_thread_info (tid
);
866 return p
->terminated
;
871 /* Is this pid a real PID or a TID?
874 is_process_id (int pid
)
881 /* What does PID really represent?
883 tid
= map_from_gdb_tid (pid
);
885 return 0; /* Actually, is probably an error... */
887 tinfo
= find_thread_info (tid
);
889 /* Does it appear to be a true thread?
891 if (!tinfo
->am_pseudo
)
894 /* Else, it looks like it may be a process. See if there's any other
895 * threads with the same process ID, though. If there are, then TID
896 * just happens to be the first thread of several for this process.
898 this_pid
= tinfo
->pid
;
900 for (tinfo
= thread_head
.head
; tinfo
; tinfo
= tinfo
->next
)
902 if (tinfo
->pid
== this_pid
)
906 return (this_pid_count
== 1);
910 /* Add a thread to our info. Prevent duplicate entries.
913 add_tthread (int pid
, lwpid_t tid
)
917 p
= find_thread_info (tid
);
919 p
= create_thread_info (pid
, tid
);
924 /* Notice that a thread was deleted.
927 del_tthread (lwpid_t tid
)
932 if (thread_head
.count
<= 0)
934 error ("Internal error in thread database.");
939 for (p
= thread_head
.head
; p
; p
= p
->next
)
946 printf ("Delete here: %d \n", tid
);
952 * Deleting a main thread is ok if we're doing
953 * a parent-follow on a child; this is odd but
954 * not wrong. It apparently _doesn't_ happen
955 * on the child-follow, as we don't just delete
956 * the pseudo while keeping the rest of the
957 * threads around--instead, we clear out the whole
958 * thread list at once.
961 thread_info
*q_chase
;
964 for (q
= thread_head
.head_pseudo
; q
; q
= q
->next
)
968 /* Remove from pseudo list.
971 thread_head
.head_pseudo
= p
->next_pseudo
;
973 q_chase
->next
= p
->next_pseudo
;
980 /* Remove from live list.
985 thread_head
.head
= p
->next
;
987 chase
->next
= p
->next
;
989 /* Add to deleted thread list.
991 p
->next
= deleted_threads
.head
;
992 deleted_threads
.head
= p
;
993 deleted_threads
.count
++;
996 p
->next_pseudo
= deleted_threads
.head_pseudo
;
997 deleted_threads
.head_pseudo
= p
;
1009 /* Get the pid for this tid. (Has to be a real TID!).
1012 get_pid_for (lwpid_t tid
)
1016 for (p
= thread_head
.head
; p
; p
= p
->next
)
1024 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
1035 /* Note that this thread's current event has been handled.
1038 set_handled (int pid
, lwpid_t tid
)
1042 p
= find_thread_info (tid
);
1044 p
= add_tthread (pid
, tid
);
1049 /* Was this thread's current event handled?
1052 was_handled (lwpid_t tid
)
1056 p
= find_thread_info (tid
);
1060 return 0; /* New threads have not been handled */
1063 /* Set this thread to unhandled.
1066 clear_handled (lwpid_t tid
)
1070 #ifdef WAIT_BUFFER_DEBUG
1072 printf ("clear_handled %d\n", (int) tid
);
1075 p
= find_thread_info (tid
);
1077 error ("Internal error: No thread state to clear?");
1082 /* Set all threads to unhandled.
1085 clear_all_handled (void)
1089 #ifdef WAIT_BUFFER_DEBUG
1091 printf ("clear_all_handled\n");
1094 for (p
= thread_head
.head
; p
; p
= p
->next
)
1099 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
1105 /* Set this thread to default stepping mode.
1108 clear_stepping_mode (lwpid_t tid
)
1112 #ifdef WAIT_BUFFER_DEBUG
1114 printf ("clear_stepping_mode %d\n", (int) tid
);
1117 p
= find_thread_info (tid
);
1119 error ("Internal error: No thread state to clear?");
1121 p
->stepping_mode
= DO_DEFAULT
;
1124 /* Set all threads to do default continue on resume.
1127 clear_all_stepping_mode (void)
1131 #ifdef WAIT_BUFFER_DEBUG
1133 printf ("clear_all_stepping_mode\n");
1136 for (p
= thread_head
.head
; p
; p
= p
->next
)
1138 p
->stepping_mode
= DO_DEFAULT
;
1141 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
1143 p
->stepping_mode
= DO_DEFAULT
;
1147 /* Set all threads to unseen on this pass.
1150 set_all_unseen (void)
1154 for (p
= thread_head
.head
; p
; p
= p
->next
)
1160 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1161 /* debugging routine.
1164 print_tthread (thread_info
*p
)
1166 printf (" Thread pid %d, tid %d", p
->pid
, p
->tid
);
1168 printf (", event is %s",
1169 get_printable_name_of_ttrace_event (p
->last_stop_state
.tts_event
));
1172 printf (", pseudo thread");
1175 printf (", have signal 0x%x", p
->signal_value
);
1178 printf (", have start at 0x%x", p
->start
);
1180 printf (", step is %s", get_printable_name_of_stepping_mode (p
->stepping_mode
));
1183 printf (", handled");
1185 printf (", not handled");
1190 printf (", not seen");
1196 print_tthreads (void)
1200 if (thread_head
.count
== 0)
1201 printf ("Thread list is empty\n");
1204 printf ("Thread list has ");
1205 if (thread_head
.count
== 1)
1206 printf ("1 entry:\n");
1208 printf ("%d entries:\n", thread_head
.count
);
1209 for (p
= thread_head
.head
; p
; p
= p
->next
)
1215 if (deleted_threads
.count
== 0)
1216 printf ("Deleted thread list is empty\n");
1219 printf ("Deleted thread list has ");
1220 if (deleted_threads
.count
== 1)
1221 printf ("1 entry:\n");
1223 printf ("%d entries:\n", deleted_threads
.count
);
1225 for (p
= deleted_threads
.head
; p
; p
= p
->next
)
1233 /* Update the thread list based on the "seen" bits.
1236 update_thread_list (void)
1242 for (p
= thread_head
.head
; p
; p
= p
->next
)
1244 /* Is this an "unseen" thread which really happens to be a process?
1245 If so, is it inferior_ptid and is a vfork in flight? If yes to
1246 all, then DON'T REMOVE IT! We're in the midst of moving a vfork
1247 operation, which is a multiple step thing, to the point where we
1248 can touch the parent again. We've most likely stopped to examine
1249 the child at a late stage in the vfork, and if we're not following
1250 the child, we'd best not treat the parent as a dead "thread"...
1252 if ((!p
->seen
) && p
->am_pseudo
&& vfork_in_flight
1253 && (p
->pid
!= vforking_child_pid
))
1263 printf ("Delete unseen thread: %d \n", p
->tid
);
1265 del_tthread (p
->tid
);
1272 /************************************************
1273 * O/S call wrappers *
1274 ************************************************
1277 /* This function simply calls ttrace with the given arguments.
1278 * It exists so that all calls to ttrace are isolated. All
1279 * parameters should be as specified by "man 2 ttrace".
1281 * No other "raw" calls to ttrace should exist in this module.
1284 call_real_ttrace (ttreq_t request
, pid_t pid
, lwpid_t tid
, TTRACE_ARG_TYPE addr
,
1285 TTRACE_ARG_TYPE data
, TTRACE_ARG_TYPE addr2
)
1290 tt_status
= ttrace (request
, pid
, tid
, addr
, data
, addr2
);
1295 /* Don't bother for a known benign error: if you ask for the
1296 * first thread state, but there is only one thread and it's
1297 * not stopped, ttrace complains.
1299 * We have this inside the #ifdef because our caller will do
1300 * this check for real.
1302 if (request
!= TT_PROC_GET_FIRST_LWP_STATE
1306 printf ("TT fail for %s, with pid %d, tid %d, status %d \n",
1307 get_printable_name_of_ttrace_request (request
),
1308 pid
, tid
, tt_status
);
1314 /* ??rehrauer: It would probably be most robust to catch and report
1315 * failed requests here. However, some clients of this interface
1316 * seem to expect to catch & deal with them, so we'd best not.
1320 strcpy (reason_for_failure
, "ttrace (");
1321 strcat (reason_for_failure
, get_printable_name_of_ttrace_request (request
));
1322 strcat (reason_for_failure
, ")");
1323 printf ("ttrace error, errno = %d\n", errno
);
1324 perror_with_name (reason_for_failure
);
1332 /* This function simply calls ttrace_wait with the given arguments.
1333 * It exists so that all calls to ttrace_wait are isolated.
1335 * No "raw" calls to ttrace_wait should exist elsewhere.
1338 call_real_ttrace_wait (int pid
, lwpid_t tid
, ttwopt_t option
, ttstate_t
*tsp
,
1342 thread_info
*tinfo
= NULL
;
1345 ttw_status
= ttrace_wait (pid
, tid
, option
, tsp
, tsp_size
);
1351 printf ("TW fail with pid %d, tid %d \n", pid
, tid
);
1354 perror_with_name ("ttrace wait");
1361 /* A process may have one or more kernel threads, of which all or
1362 none may be stopped. This function returns the ID of the first
1363 kernel thread in a stopped state, or 0 if none are stopped.
1365 This function can be used with get_process_next_stopped_thread_id
1366 to iterate over the IDs of all stopped threads of this process.
1369 get_process_first_stopped_thread_id (int pid
, ttstate_t
*thread_state
)
1373 tt_status
= call_real_ttrace (TT_PROC_GET_FIRST_LWP_STATE
,
1376 (TTRACE_ARG_TYPE
) thread_state
,
1377 (TTRACE_ARG_TYPE
) sizeof (*thread_state
),
1382 if (errno
== EPROTO
)
1384 /* This is an error we can handle: there isn't any stopped
1385 * thread. This happens when we're re-starting the application
1386 * and it has only one thread. GET_NEXT handles the case of
1387 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1395 perror_with_name ("ttrace");
1403 return thread_state
->tts_lwpid
;
1407 /* This function returns the ID of the "next" kernel thread in a
1408 stopped state, or 0 if there are none. "Next" refers to the
1409 thread following that of the last successful call to this
1410 function or to get_process_first_stopped_thread_id, using
1411 the value of thread_state returned by that call.
1413 This function can be used with get_process_first_stopped_thread_id
1414 to iterate over the IDs of all stopped threads of this process.
1417 get_process_next_stopped_thread_id (int pid
, ttstate_t
*thread_state
)
1421 tt_status
= call_real_ttrace (
1422 TT_PROC_GET_NEXT_LWP_STATE
,
1425 (TTRACE_ARG_TYPE
) thread_state
,
1426 (TTRACE_ARG_TYPE
) sizeof (*thread_state
),
1429 perror_with_name ("ttrace");
1436 else if (tt_status
== 0)
1438 /* End of list, no next state. Don't return the
1439 * tts_lwpid, as it's a meaningless "240".
1441 * This is an HPUX "feature".
1446 return thread_state
->tts_lwpid
;
1449 /* ??rehrauer: Eventually this function perhaps should be calling
1450 pid_to_thread_id. However, that function currently does nothing
1451 for HP-UX. Even then, I'm not clear whether that function
1452 will return a "kernel" thread ID, or a "user" thread ID. If
1453 the former, we can just call it here. If the latter, we must
1454 map from the "user" tid to a "kernel" tid.
1456 NOTE: currently not called.
1459 get_active_tid_of_pid (int pid
)
1461 ttstate_t thread_state
;
1463 return get_process_first_stopped_thread_id (pid
, &thread_state
);
1466 /* This function returns 1 if tt_request is a ttrace request that
1467 * operates upon all threads of a (i.e., the entire) process.
1470 is_process_ttrace_request (ttreq_t tt_request
)
1472 return IS_TTRACE_PROCREQ (tt_request
);
1476 /* This function translates a thread ttrace request into
1477 * the equivalent process request for a one-thread process.
1480 make_process_version (ttreq_t request
)
1482 if (!IS_TTRACE_REQ (request
))
1484 error ("Internal error, bad ttrace request made\n");
1491 return TT_PROC_STOP
;
1493 case TT_LWP_CONTINUE
:
1494 return TT_PROC_CONTINUE
;
1496 case TT_LWP_GET_EVENT_MASK
:
1497 return TT_PROC_GET_EVENT_MASK
;
1499 case TT_LWP_SET_EVENT_MASK
:
1500 return TT_PROC_SET_EVENT_MASK
;
1505 case TT_LWP_GET_STATE
:
1506 return -1; /* No equivalent */
1514 /* This function translates the "pid" used by the rest of
1515 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1516 * with the given arguments.
1518 * In general, other parts of this module should call this
1519 * function when they are dealing with external users, who only
1520 * have tids to pass (but they call it "pid" for historical
1524 call_ttrace (ttreq_t request
, int gdb_tid
, TTRACE_ARG_TYPE addr
,
1525 TTRACE_ARG_TYPE data
, TTRACE_ARG_TYPE addr2
)
1529 ttreq_t new_request
;
1531 char reason_for_failure
[100]; /* Arbitrary size, should be big enough. */
1534 int is_interesting
= 0;
1536 if (TT_LWP_RUREGS
== request
)
1538 is_interesting
= 1; /* Adjust code here as desired */
1541 if (is_interesting
&& 0 && debug_on
)
1543 if (!is_process_ttrace_request (request
))
1545 printf ("TT: Thread request, tid is %d", gdb_tid
);
1546 printf ("== SINGLE at %x", addr
);
1550 printf ("TT: Process request, tid is %d\n", gdb_tid
);
1551 printf ("==! SINGLE at %x", addr
);
1556 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1557 * which happen before any threads get set up) should go
1558 * directly to "call_real_ttrace", so they don't happen here.
1560 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1564 if (request
== TT_PROC_SETTRC
&& debug_on
)
1565 printf ("Unexpected call for TT_PROC_SETTRC\n");
1568 /* Sometimes we get called with a bogus tid (e.g., if a
1569 * thread has terminated, we return 0; inftarg later asks
1570 * whether the thread has exited/forked/vforked).
1574 errno
= ESRCH
; /* ttrace's response would probably be "No such process". */
1578 /* All other cases should be able to expect that there are
1581 if (!any_thread_records ())
1585 warning ("No thread records for ttrace call");
1587 errno
= ESRCH
; /* ttrace's response would be "No such process". */
1591 /* OK, now the task is to translate the incoming tid into
1594 real_tid
= map_from_gdb_tid (gdb_tid
);
1595 real_pid
= get_pid_for (real_tid
);
1597 /* Now check the result. "Real_pid" is NULL if our list
1598 * didn't find it. We have some tricks we can play to fix
1603 ttstate_t thread_state
;
1607 printf ("No saved pid for tid %d\n", gdb_tid
);
1610 if (is_process_ttrace_request (request
))
1613 /* Ok, we couldn't get a tid. Try to translate to
1614 * the equivalent process operation. We expect this
1615 * NOT to happen, so this is a desparation-type
1616 * move. It can happen if there is an internal
1617 * error and so no "wait()" call is ever done.
1619 new_request
= make_process_version (request
);
1620 if (new_request
== -1)
1625 printf ("...and couldn't make process version of thread operation\n");
1628 /* Use hacky saved pid, which won't always be correct
1629 * in the multi-process future. Use tid as thread,
1630 * probably dooming this to failure. FIX!
1632 if (! ptid_equal (saved_real_ptid
, null_ptid
))
1636 printf ("...using saved pid %d\n",
1637 PIDGET (saved_real_ptid
));
1640 real_pid
= PIDGET (saved_real_ptid
);
1645 error ("Unable to perform thread operation");
1650 /* Sucessfully translated this to a process request,
1651 * which needs no thread value.
1655 request
= new_request
;
1660 printf ("Translated thread request to process request\n");
1661 if (ptid_equal (saved_real_ptid
, null_ptid
))
1662 printf ("...but there's no saved pid\n");
1666 if (gdb_tid
!= PIDGET (saved_real_ptid
))
1667 printf ("...but have the wrong pid (%d rather than %d)\n",
1668 gdb_tid
, PIDGET (saved_real_ptid
));
1672 } /* Translated to a process request */
1673 } /* Is a process request */
1677 /* We have to have a thread. Ooops.
1679 error ("Thread request with no threads (%s)",
1680 get_printable_name_of_ttrace_request (request
));
1684 /* Ttrace doesn't like to see tid values on process requests,
1685 * even if we have the right one.
1687 if (is_process_ttrace_request (request
))
1693 if (is_interesting
&& 0 && debug_on
)
1695 printf (" now tid %d, pid %d\n", real_tid
, real_pid
);
1696 printf (" request is %s\n", get_printable_name_of_ttrace_request (request
));
1700 /* Finally, the (almost) real call.
1702 tt_status
= call_real_ttrace (request
, real_pid
, real_tid
, addr
, data
, addr2
);
1705 if (is_interesting
&& debug_on
)
1707 if (!TT_OK (tt_status
, errno
)
1708 && !(tt_status
== 0 & errno
== 0))
1709 printf (" got error (errno==%d, status==%d)\n", errno
, tt_status
);
1717 /* Stop all the threads of a process.
1719 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1720 * to get a TTEVT_NONE event, discarding the old event. Be
1721 * very careful, and only call TT_PROC_STOP when you mean it!
1724 stop_all_threads_of_process (pid_t real_pid
)
1728 ttw_status
= call_real_ttrace (TT_PROC_STOP
,
1731 (TTRACE_ARG_TYPE
) TT_NIL
,
1732 (TTRACE_ARG_TYPE
) TT_NIL
,
1735 perror_with_name ("ttrace stop of other threads");
1739 /* Under some circumstances, it's unsafe to attempt to stop, or even
1740 query the state of, a process' threads.
1742 In ttrace-based HP-UX, an example is a vforking child process. The
1743 vforking parent and child are somewhat fragile, w/r/t what we can do
1744 what we can do to them with ttrace, until after the child exits or
1745 execs, or until the parent's vfork event is delivered. Until that
1746 time, we must not try to stop the process' threads, or inquire how
1747 many there are, or even alter its data segments, or it typically dies
1748 with a SIGILL. Sigh.
1750 This function returns 1 if this stopped process, and the event that
1751 we're told was responsible for its current stopped state, cannot safely
1752 have its threads examined.
1754 #define CHILD_VFORKED(evt,pid) \
1755 (((evt) == TTEVT_VFORK) && ((pid) != PIDGET (inferior_ptid)))
1756 #define CHILD_URPED(evt,pid) \
1757 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1758 #define PARENT_VFORKED(evt,pid) \
1759 (((evt) == TTEVT_VFORK) && ((pid) == PIDGET (inferior_ptid)))
1762 can_touch_threads_of_process (int pid
, ttevents_t stopping_event
)
1764 if (CHILD_VFORKED (stopping_event
, pid
))
1766 vforking_child_pid
= pid
;
1767 vfork_in_flight
= 1;
1770 else if (vfork_in_flight
&&
1771 (PARENT_VFORKED (stopping_event
, pid
) ||
1772 CHILD_URPED (stopping_event
, pid
)))
1774 vfork_in_flight
= 0;
1775 vforking_child_pid
= 0;
1778 return !vfork_in_flight
;
1782 /* If we can find an as-yet-unhandled thread state of a
1783 * stopped thread of this process return 1 and set "tsp".
1784 * Return 0 if we can't.
1786 * If this function is used when the threads of PIS haven't
1787 * been stopped, undefined behaviour is guaranteed!
1790 select_stopped_thread_of_process (int pid
, ttstate_t
*tsp
)
1792 lwpid_t candidate_tid
, tid
;
1793 ttstate_t candidate_tstate
, tstate
;
1795 /* If we're not allowed to touch the process now, then just
1796 * return the current value of *TSP.
1798 * This supports "vfork". It's ok, really, to double the
1799 * current event (the child EXEC, we hope!).
1801 if (!can_touch_threads_of_process (pid
, tsp
->tts_event
))
1804 /* Decide which of (possibly more than one) events to
1805 * return as the first one. We scan them all so that
1806 * we always return the result of a fake-step first.
1809 for (tid
= get_process_first_stopped_thread_id (pid
, &tstate
);
1811 tid
= get_process_next_stopped_thread_id (pid
, &tstate
))
1813 /* TTEVT_NONE events are uninteresting to our clients. They're
1814 * an artifact of our "stop the world" model--the thread is
1815 * stopped because we stopped it.
1817 if (tstate
.tts_event
== TTEVT_NONE
)
1819 set_handled (pid
, tstate
.tts_lwpid
);
1822 /* Did we just single-step a single thread, without letting any
1823 * of the others run? Is this an event for that thread?
1825 * If so, we believe our client would prefer to see this event
1826 * over any others. (Typically the client wants to just push
1827 * one thread a little farther forward, and then go around
1828 * checking for what all threads are doing.)
1830 else if (doing_fake_step
&& (tstate
.tts_lwpid
== fake_step_tid
))
1832 #ifdef WAIT_BUFFER_DEBUG
1833 /* It's possible here to see either a SIGTRAP (due to
1834 * successful completion of a step) or a SYSCALL_ENTRY
1835 * (due to a step completion with active hardware
1839 printf ("Ending fake step with tid %d, state %s\n",
1841 get_printable_name_of_ttrace_event (tstate
.tts_event
));
1844 /* Remember this one, and throw away any previous
1847 candidate_tid
= tstate
.tts_lwpid
;
1848 candidate_tstate
= tstate
;
1851 #ifdef FORGET_DELETED_BPTS
1853 /* We can't just do this, as if we do, and then wind
1854 * up the loop with no unhandled events, we need to
1855 * handle that case--the appropriate reaction is to
1856 * just continue, but there's no easy way to do that.
1858 * Better to put this in the ttrace_wait call--if, when
1859 * we fake a wait, we update our events based on the
1860 * breakpoint_here_pc call and find there are no more events,
1861 * then we better continue and so on.
1863 * Or we could put it in the next/continue fake.
1864 * But it has to go in the buffering code, not in the
1865 * real go/wait code.
1867 else if ((TTEVT_SIGNAL
== tstate
.tts_event
)
1868 && (5 == tstate
.tts_u
.tts_signal
.tts_signo
)
1869 && (0 != get_raw_pc (tstate
.tts_lwpid
))
1870 && !breakpoint_here_p (get_raw_pc (tstate
.tts_lwpid
)))
1873 * If the user deleted a breakpoint while this
1874 * breakpoint-hit event was buffered, we can forget
1877 #ifdef WAIT_BUFFER_DEBUG
1879 printf ("Forgetting deleted bp hit for thread %d\n",
1883 set_handled (pid
, tstate
.tts_lwpid
);
1887 /* Else, is this the first "unhandled" event? If so,
1888 * we believe our client wants to see it (if we don't
1889 * see a fake-step later on in the scan).
1891 else if (!was_handled (tstate
.tts_lwpid
) && candidate_tid
== 0)
1893 candidate_tid
= tstate
.tts_lwpid
;
1894 candidate_tstate
= tstate
;
1897 /* This is either an event that has already been "handled",
1898 * and thus we believe is uninteresting to our client, or we
1899 * already have a candidate event. Ignore it...
1903 /* What do we report?
1905 if (doing_fake_step
)
1907 if (candidate_tid
== fake_step_tid
)
1911 tstate
= candidate_tstate
;
1915 warning ("Internal error: fake-step failed to complete.");
1919 else if (candidate_tid
!= 0)
1921 /* Found a candidate unhandled event.
1923 tstate
= candidate_tstate
;
1927 warning ("Internal error in call of ttrace_wait.");
1932 warning ("Internal error: no unhandled thread event to select");
1936 copy_ttstate_t (tsp
, &tstate
);
1938 } /* End of select_stopped_thread_of_process */
1941 /* Check our internal thread data against the real thing.
1944 check_thread_consistency (pid_t real_pid
)
1946 int tid
; /* really lwpid_t */
1950 /* Spin down the O/S list of threads, checking that they
1951 * match what we've got.
1953 for (tid
= get_process_first_stopped_thread_id (real_pid
, &tstate
);
1955 tid
= get_process_next_stopped_thread_id (real_pid
, &tstate
))
1958 p
= find_thread_info (tid
);
1962 warning ("No internal thread data for thread %d.", tid
);
1968 warning ("Inconsistent internal thread data for thread %d.", tid
);
1973 warning ("Thread %d is not terminated, internal error.", tid
);
1978 #define TT_COMPARE( fld ) \
1979 tstate.fld != p->last_stop_state.fld
1983 if (TT_COMPARE (tts_pid
)
1984 || TT_COMPARE (tts_lwpid
)
1985 || TT_COMPARE (tts_user_tid
)
1986 || TT_COMPARE (tts_event
)
1987 || TT_COMPARE (tts_flags
)
1988 || TT_COMPARE (tts_scno
)
1989 || TT_COMPARE (tts_scnargs
))
1991 warning ("Internal thread data for thread %d is wrong.", tid
);
1997 #endif /* PARANOIA */
2000 /* This function wraps calls to "call_real_ttrace_wait" so
2001 * that a actual wait is only done when all pending events
2002 * have been reported.
2004 * Note that typically it is called with a pid of "0", i.e.
2005 * the "don't care" value.
2007 * Return value is the status of the pseudo wait.
2010 call_ttrace_wait (int pid
, ttwopt_t option
, ttstate_t
*tsp
, size_t tsp_size
)
2012 /* This holds the actual, for-real, true process ID.
2014 static int real_pid
;
2016 /* As an argument to ttrace_wait, zero pid
2017 * means "Any process", and zero tid means
2018 * "Any thread of the specified process".
2021 lwpid_t wait_tid
= 0;
2024 int ttw_status
= 0; /* To be returned */
2026 thread_info
*tinfo
= NULL
;
2034 printf ("TW: Pid to wait on is %d\n", pid
);
2037 if (!any_thread_records ())
2038 error ("No thread records for ttrace call w. specific pid");
2040 /* OK, now the task is to translate the incoming tid into
2043 real_tid
= map_from_gdb_tid (pid
);
2044 real_pid
= get_pid_for (real_tid
);
2047 printf ("==TW: real pid %d, real tid %d\n", real_pid
, real_tid
);
2052 /* Sanity checks and set-up.
2055 * Stopped Running Fake-step (v)Fork
2056 * \________________________________________
2058 * No buffered events | error wait wait wait
2060 * Buffered events | debuffer error wait debuffer (?)
2063 if (more_events_left
== 0)
2066 if (process_state
== RUNNING
)
2068 /* OK--normal call of ttrace_wait with no buffered events.
2072 else if (process_state
== FAKE_STEPPING
)
2074 /* Ok--call of ttrace_wait to support
2075 * fake stepping with no buffered events.
2077 * But we better be fake-stepping!
2079 if (!doing_fake_step
)
2081 warning ("Inconsistent thread state.");
2084 else if ((process_state
== FORKING
)
2085 || (process_state
== VFORKING
))
2087 /* Ok--there are two processes, so waiting
2088 * for the second while the first is stopped
2089 * is ok. Handled bits stay as they were.
2093 else if (process_state
== STOPPED
)
2095 warning ("Process not running at wait call.");
2100 warning ("Inconsistent process state.");
2107 if (process_state
== STOPPED
)
2109 /* OK--buffered events being unbuffered.
2113 else if (process_state
== RUNNING
)
2115 /* An error--shouldn't have buffered events
2118 warning ("Trying to continue with buffered events:");
2120 else if (process_state
== FAKE_STEPPING
)
2123 * Better be fake-stepping!
2125 if (!doing_fake_step
)
2127 warning ("Losing buffered thread events!\n");
2130 else if ((process_state
== FORKING
)
2131 || (process_state
== VFORKING
))
2133 /* Ok--there are two processes, so waiting
2134 * for the second while the first is stopped
2135 * is ok. Handled bits stay as they were.
2140 warning ("Process in unknown state with buffered events.");
2143 /* Sometimes we have to wait for a particular thread
2144 * (if we're stepping over a bpt). In that case, we
2145 * _know_ it's going to complete the single-step we
2146 * asked for (because we're only doing the step under
2147 * certain very well-understood circumstances), so it
2150 if (doing_fake_step
)
2152 wait_tid
= fake_step_tid
;
2153 wait_pid
= get_pid_for (fake_step_tid
);
2155 #ifdef WAIT_BUFFER_DEBUG
2157 printf ("Doing a wait after a fake-step for %d, pid %d\n",
2158 wait_tid
, wait_pid
);
2162 if (more_events_left
== 0 /* No buffered events, need real ones. */
2163 || process_state
!= STOPPED
)
2165 /* If there are no buffered events, and so we need
2166 * real ones, or if we are FORKING, VFORKING,
2167 * FAKE_STEPPING or RUNNING, and thus have to do
2168 * a real wait, then do a real wait.
2171 #ifdef WAIT_BUFFER_DEBUG
2172 /* Normal case... */
2174 printf ("TW: do it for real; pid %d, tid %d\n", wait_pid
, wait_tid
);
2177 /* The actual wait call.
2179 ttw_status
= call_real_ttrace_wait (wait_pid
, wait_tid
, option
, tsp
, tsp_size
);
2181 /* Note that the routines we'll call will be using "call_real_ttrace",
2182 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2183 * the rest of the world uses (which is actually the tid).
2185 real_pid
= tsp
->tts_pid
;
2187 /* For most events: Stop the world!
2189 * It's sometimes not safe to stop all threads of a process.
2190 * Sometimes it's not even safe to ask for the thread state
2193 if (can_touch_threads_of_process (real_pid
, tsp
->tts_event
))
2195 /* If we're really only stepping a single thread, then don't
2196 * try to stop all the others -- we only do this single-stepping
2197 * business when all others were already stopped...and the stop
2198 * would mess up other threads' events.
2200 * Similiarly, if there are other threads with events,
2201 * don't do the stop.
2203 if (!doing_fake_step
)
2205 if (more_events_left
> 0)
2206 warning ("Internal error in stopping process");
2208 stop_all_threads_of_process (real_pid
);
2210 /* At this point, we could scan and update_thread_list(),
2211 * and only use the local list for the rest of the
2212 * module! We'd get rid of the scans in the various
2213 * continue routines (adding one in attach). It'd
2214 * be great--UPGRADE ME!
2222 if (more_events_left
> 0)
2223 printf ("== Can't stop process; more events!\n");
2225 printf ("== Can't stop process!\n");
2229 process_state
= STOPPED
;
2231 #ifdef WAIT_BUFFER_DEBUG
2233 printf ("Process set to STOPPED\n");
2239 /* Fake a call to ttrace_wait. The process must be
2240 * STOPPED, as we aren't going to do any wait.
2242 #ifdef WAIT_BUFFER_DEBUG
2244 printf ("TW: fake it\n");
2247 if (process_state
!= STOPPED
)
2249 warning ("Process not stopped at wait call, in state '%s'.\n",
2250 get_printable_name_of_process_state (process_state
));
2253 if (doing_fake_step
)
2254 error ("Internal error in stepping over breakpoint");
2256 ttw_status
= 0; /* Faking it is always successful! */
2257 } /* End of fake or not? if */
2259 /* Pick an event to pass to our caller. Be paranoid.
2261 if (!select_stopped_thread_of_process (real_pid
, tsp
))
2262 warning ("Can't find event, using previous event.");
2264 else if (tsp
->tts_event
== TTEVT_NONE
)
2265 warning ("Internal error: no thread has a real event.");
2267 else if (doing_fake_step
)
2269 if (fake_step_tid
!= tsp
->tts_lwpid
)
2270 warning ("Internal error in stepping over breakpoint.");
2272 /* This wait clears the (current) fake-step if there was one.
2274 doing_fake_step
= 0;
2278 /* We now have a correct tsp and ttw_status for the thread
2279 * which we want to report. So it's "handled"! This call
2280 * will add it to our list if it's not there already.
2282 set_handled (real_pid
, tsp
->tts_lwpid
);
2284 /* Save a copy of the ttrace state of this thread, in our local
2287 This caches the state. The implementation of queries like
2288 hpux_has_execd can then use this cached state, rather than
2289 be forced to make an explicit ttrace call to get it.
2291 (Guard against the condition that this is the first time we've
2292 waited on, i.e., seen this thread, and so haven't yet entered
2293 it into our list of threads.)
2295 tinfo
= find_thread_info (tsp
->tts_lwpid
);
2298 copy_ttstate_t (&tinfo
->last_stop_state
, tsp
);
2299 tinfo
->have_state
= 1;
2303 } /* call_ttrace_wait */
2305 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2307 child_reported_exec_events_per_exec_call (void)
2309 return 1; /* ttrace reports the event once per call. */
2315 /* Our implementation of hardware watchpoints involves making memory
2316 pages write-protected. We must remember a page's original permissions,
2317 and we must also know when it is appropriate to restore a page's
2318 permissions to its original state.
2320 We use a "dictionary" of hardware-watched pages to do this. Each
2321 hardware-watched page is recorded in the dictionary. Each page's
2322 dictionary entry contains the original permissions and a reference
2323 count. Pages are hashed into the dictionary by their start address.
2325 When hardware watchpoint is set on page X for the first time, page X
2326 is added to the dictionary with a reference count of 1. If other
2327 hardware watchpoints are subsequently set on page X, its reference
2328 count is incremented. When hardware watchpoints are removed from
2329 page X, its reference count is decremented. If a page's reference
2330 count drops to 0, it's permissions are restored and the page's entry
2331 is thrown out of the dictionary.
2333 typedef struct memory_page
2335 CORE_ADDR page_start
;
2336 int reference_count
;
2337 int original_permissions
;
2338 struct memory_page
*next
;
2339 struct memory_page
*previous
;
2343 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2349 int page_protections_allowed
;
2350 /* These are just the heads of chains of actual page descriptors. */
2351 memory_page_t buckets
[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
];
2353 memory_page_dictionary
;
2357 require_memory_page_dictionary (void)
2361 /* Is the memory page dictionary ready for use? If so, we're done. */
2362 if (memory_page_dictionary
.page_count
>= (LONGEST
) 0)
2365 /* Else, initialize it. */
2366 memory_page_dictionary
.page_count
= (LONGEST
) 0;
2368 for (i
= 0; i
< MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; i
++)
2370 memory_page_dictionary
.buckets
[i
].page_start
= (CORE_ADDR
) 0;
2371 memory_page_dictionary
.buckets
[i
].reference_count
= 0;
2372 memory_page_dictionary
.buckets
[i
].next
= NULL
;
2373 memory_page_dictionary
.buckets
[i
].previous
= NULL
;
2379 retire_memory_page_dictionary (void)
2381 memory_page_dictionary
.page_count
= (LONGEST
) - 1;
2385 /* Write-protect the memory page that starts at this address.
2387 Returns the original permissions of the page.
2390 write_protect_page (int pid
, CORE_ADDR page_start
)
2393 int original_permissions
;
2394 int new_permissions
;
2396 tt_status
= call_ttrace (TT_PROC_GET_MPROTECT
,
2398 (TTRACE_ARG_TYPE
) page_start
,
2400 (TTRACE_ARG_TYPE
) & original_permissions
);
2401 if (errno
|| (tt_status
< 0))
2403 return 0; /* What else can we do? */
2406 /* We'll also write-protect the page now, if that's allowed. */
2407 if (memory_page_dictionary
.page_protections_allowed
)
2409 new_permissions
= original_permissions
& ~PROT_WRITE
;
2410 tt_status
= call_ttrace (TT_PROC_SET_MPROTECT
,
2412 (TTRACE_ARG_TYPE
) page_start
,
2413 (TTRACE_ARG_TYPE
) memory_page_dictionary
.page_size
,
2414 (TTRACE_ARG_TYPE
) new_permissions
);
2415 if (errno
|| (tt_status
< 0))
2417 return 0; /* What else can we do? */
2421 return original_permissions
;
2425 /* Unwrite-protect the memory page that starts at this address, restoring
2426 (what we must assume are) its original permissions.
2429 unwrite_protect_page (int pid
, CORE_ADDR page_start
, int original_permissions
)
2433 tt_status
= call_ttrace (TT_PROC_SET_MPROTECT
,
2435 (TTRACE_ARG_TYPE
) page_start
,
2436 (TTRACE_ARG_TYPE
) memory_page_dictionary
.page_size
,
2437 (TTRACE_ARG_TYPE
) original_permissions
);
2438 if (errno
|| (tt_status
< 0))
2440 return; /* What else can we do? */
2445 /* Memory page-protections are used to implement "hardware" watchpoints
2448 For every memory page that is currently being watched (i.e., that
2449 presently should be write-protected), write-protect it.
2452 hppa_enable_page_protection_events (int pid
)
2456 memory_page_dictionary
.page_protections_allowed
= 1;
2458 for (bucket
= 0; bucket
< MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; bucket
++)
2460 memory_page_t
*page
;
2462 page
= memory_page_dictionary
.buckets
[bucket
].next
;
2463 while (page
!= NULL
)
2465 page
->original_permissions
= write_protect_page (pid
, page
->page_start
);
2472 /* Memory page-protections are used to implement "hardware" watchpoints
2475 For every memory page that is currently being watched (i.e., that
2476 presently is or should be write-protected), un-write-protect it.
2479 hppa_disable_page_protection_events (int pid
)
2483 for (bucket
= 0; bucket
< MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
; bucket
++)
2485 memory_page_t
*page
;
2487 page
= memory_page_dictionary
.buckets
[bucket
].next
;
2488 while (page
!= NULL
)
2490 unwrite_protect_page (pid
, page
->page_start
, page
->original_permissions
);
2495 memory_page_dictionary
.page_protections_allowed
= 0;
2498 /* Count the number of outstanding events. At this
2499 * point, we have selected one thread and its event
2500 * as the one to be "reported" upwards to core gdb.
2501 * That thread is already marked as "handled".
2503 * Note: we could just scan our own thread list. FIXME!
2506 count_unhandled_events (int real_pid
, lwpid_t real_tid
)
2512 /* Ok, find out how many threads have real events to report.
2515 ttid
= get_process_first_stopped_thread_id (real_pid
, &tstate
);
2521 printf ("Process %d has no threads\n", real_pid
);
2523 printf ("Process %d has these threads:\n", real_pid
);
2529 if (tstate
.tts_event
!= TTEVT_NONE
2530 && !was_handled (ttid
))
2532 /* TTEVT_NONE implies we just stopped it ourselves
2533 * because we're the stop-the-world guys, so it's
2534 * not an event from our point of view.
2536 * If "was_handled" is true, this is an event we
2537 * already handled, so don't count it.
2539 * Note that we don't count the thread with the
2540 * currently-reported event, as it's already marked
2546 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2549 if (ttid
== real_tid
)
2550 printf ("*"); /* Thread we're reporting */
2554 if (tstate
.tts_event
!= TTEVT_NONE
)
2555 printf ("+"); /* Thread with a real event */
2559 if (was_handled (ttid
))
2560 printf ("h"); /* Thread has been handled */
2564 printf (" %d, with event %s", ttid
,
2565 get_printable_name_of_ttrace_event (tstate
.tts_event
));
2567 if (tstate
.tts_event
== TTEVT_SIGNAL
2568 && 5 == tstate
.tts_u
.tts_signal
.tts_signo
)
2572 pc_val
= get_raw_pc (ttid
);
2575 printf (" breakpoint at 0x%x\n", pc_val
);
2577 printf (" bpt, can't fetch pc.\n");
2584 ttid
= get_process_next_stopped_thread_id (real_pid
, &tstate
);
2587 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2589 if (events_left
> 0)
2590 printf ("There are thus %d pending events\n", events_left
);
2596 /* This function is provided as a sop to clients that are calling
2597 * ptrace_wait to wait for a process to stop. (see the
2598 * implementation of child_wait.) Return value is the pid for
2599 * the event that ended the wait.
2601 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2604 ptrace_wait (ptid_t ptid
, int *status
)
2613 /* The ptrace implementation of this also ignores pid.
2617 ttwait_return
= call_ttrace_wait (0, TTRACE_WAITOK
, &tsp
, sizeof (tsp
));
2618 if (ttwait_return
< 0)
2620 /* ??rehrauer: It appears that if our inferior exits and we
2621 haven't asked for exit events, that we're not getting any
2622 indication save a negative return from ttrace_wait and an
2627 *status
= 0; /* WIFEXITED */
2628 return PIDGET (inferior_ptid
);
2631 warning ("Call of ttrace_wait returned with errno %d.",
2633 *status
= ttwait_return
;
2634 return PIDGET (inferior_ptid
);
2637 real_pid
= tsp
.tts_pid
;
2638 real_tid
= tsp
.tts_lwpid
;
2640 /* One complication is that the "tts_event" structure has
2641 * a set of flags, and more than one can be set. So we
2642 * either have to force an order (as we do here), or handle
2643 * more than one flag at a time.
2645 if (tsp
.tts_event
& TTEVT_LWP_CREATE
)
2648 /* Unlike what you might expect, this event is reported in
2649 * the _creating_ thread, and the _created_ thread (whose tid
2650 * we have) is still running. So we have to stop it. This
2651 * has already been done in "call_ttrace_wait", but should we
2652 * ever abandon the "stop-the-world" model, here's the command
2655 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2657 * Note that this would depend on being called _after_ "add_tthread"
2658 * below for the tid-to-pid translation to be done in "call_ttrace".
2663 printf ("New thread: pid %d, tid %d, creator tid %d\n",
2664 real_pid
, tsp
.tts_u
.tts_thread
.tts_target_lwpid
,
2668 /* Now we have to return the tid of the created thread, not
2669 * the creating thread, or "wait_for_inferior" won't know we
2670 * have a new "process" (thread). Plus we should record it
2673 real_tid
= tsp
.tts_u
.tts_thread
.tts_target_lwpid
;
2675 add_tthread (real_pid
, real_tid
);
2678 else if ((tsp
.tts_event
& TTEVT_LWP_TERMINATE
)
2679 || (tsp
.tts_event
& TTEVT_LWP_EXIT
))
2684 printf ("Thread dies: %d\n", real_tid
);
2687 del_tthread (real_tid
);
2690 else if (tsp
.tts_event
& TTEVT_EXEC
)
2695 printf ("Pid %d has zero'th thread %d; inferior pid is %d\n",
2696 real_pid
, real_tid
, PIDGET (inferior_ptid
));
2699 add_tthread (real_pid
, real_tid
);
2705 printf ("Process-level event %s, using tid %d\n",
2706 get_printable_name_of_ttrace_event (tsp
.tts_event
),
2709 /* OK to do this, as "add_tthread" won't add
2710 * duplicate entries. Also OK not to do it,
2711 * as this event isn't one which can change the
2714 add_tthread (real_pid
, real_tid
);
2719 /* How many events are left to report later?
2720 * In a non-stop-the-world model, this isn't needed.
2722 * Note that it's not always safe to query the thread state of a process,
2723 * which is what count_unhandled_events does. (If unsafe, we're left with
2724 * no other resort than to assume that no more events remain...)
2726 if (can_touch_threads_of_process (real_pid
, tsp
.tts_event
))
2727 more_events_left
= count_unhandled_events (real_pid
, real_tid
);
2731 if (more_events_left
> 0)
2732 warning ("Vfork or fork causing loss of %d buffered events.",
2735 more_events_left
= 0;
2738 /* Attempt to translate the ttrace_wait-returned status into the
2741 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2742 clients that expect to pick apart a ptrace wait status, to use
2743 something a little more abstract.
2745 if ((tsp
.tts_event
& TTEVT_EXEC
)
2746 || (tsp
.tts_event
& TTEVT_FORK
)
2747 || (tsp
.tts_event
& TTEVT_VFORK
))
2749 /* Forks come in pairs (parent and child), so core gdb
2750 * will do two waits. Be ready to notice this.
2752 if (tsp
.tts_event
& TTEVT_FORK
)
2754 process_state
= FORKING
;
2756 #ifdef WAIT_BUFFER_DEBUG
2758 printf ("Process set to FORKING\n");
2761 else if (tsp
.tts_event
& TTEVT_VFORK
)
2763 process_state
= VFORKING
;
2765 #ifdef WAIT_BUFFER_DEBUG
2767 printf ("Process set to VFORKING\n");
2771 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2772 but I don't think non HP-UX-specific clients really carefully
2773 inspect the first events they get after inferior startup, so
2774 it probably almost doesn't matter what we claim this is.
2779 printf ("..a process 'event'\n");
2782 /* Also make fork and exec events look like bpts, so they can be caught.
2784 *status
= 0177 | (_SIGTRAP
<< 8);
2787 /* Special-cases: We ask for syscall entry and exit events to implement
2788 "fast" (aka "hardware") watchpoints.
2790 When we get a syscall entry, we want to disable page-protections,
2791 and resume the inferior; this isn't an event we wish for
2792 wait_for_inferior to see. Note that we must resume ONLY the
2793 thread that reported the syscall entry; we don't want to allow
2794 other threads to run with the page protections off, as they might
2795 then be able to write to watch memory without it being caught.
2797 When we get a syscall exit, we want to reenable page-protections,
2798 but we don't want to resume the inferior; this is an event we wish
2799 wait_for_inferior to see. Make it look like the signal we normally
2800 get for a single-step completion. This should cause wait_for_inferior
2801 to evaluate whether any watchpoint triggered.
2803 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2804 due to some HP-UX "features". Some syscalls have problems with
2805 write-protections on some pages, and some syscalls seem to have
2806 pending writes to those pages at the time we're getting the return
2807 event. So, we'll single-step the inferior to get out of the syscall,
2808 and then reenable protections.
2810 Note that we're intentionally allowing the syscall exit case to
2811 fall through into the succeeding cases, as sometimes we single-
2812 step out of one syscall only to immediately enter another...
2814 else if ((tsp
.tts_event
& TTEVT_SYSCALL_ENTRY
)
2815 || (tsp
.tts_event
& TTEVT_SYSCALL_RETURN
))
2817 /* Make a syscall event look like a breakpoint. Same comments
2818 as for exec & fork events.
2822 printf ("..a syscall 'event'\n");
2825 /* Also make syscall events look like bpts, so they can be caught.
2827 *status
= 0177 | (_SIGTRAP
<< 8);
2830 else if ((tsp
.tts_event
& TTEVT_LWP_CREATE
)
2831 || (tsp
.tts_event
& TTEVT_LWP_TERMINATE
)
2832 || (tsp
.tts_event
& TTEVT_LWP_EXIT
))
2834 /* Make a thread event look like a breakpoint. Same comments
2835 * as for exec & fork events.
2839 printf ("..a thread 'event'\n");
2842 /* Also make thread events look like bpts, so they can be caught.
2844 *status
= 0177 | (_SIGTRAP
<< 8);
2847 else if ((tsp
.tts_event
& TTEVT_EXIT
))
2852 printf ("..an exit\n");
2855 /* Prevent rest of gdb from thinking this is
2856 * a new thread if for some reason it's never
2857 * seen the main thread before.
2859 inferior_ptid
= pid_to_ptid (map_to_gdb_tid (real_tid
)); /* HACK, FIX */
2861 *status
= 0 | (tsp
.tts_u
.tts_exit
.tts_exitcode
);
2864 else if (tsp
.tts_event
& TTEVT_SIGNAL
)
2868 printf ("..a signal, %d\n", tsp
.tts_u
.tts_signal
.tts_signo
);
2871 *status
= 0177 | (tsp
.tts_u
.tts_signal
.tts_signo
<< 8);
2877 /* This means the process or thread terminated. But we should've
2878 caught an explicit exit/termination above. So warn (this is
2879 really an internal error) and claim the process or thread
2880 terminated with a SIGTRAP.
2883 warning ("process_wait: unknown process state");
2887 printf ("Process-level event %s, using tid %d\n",
2888 get_printable_name_of_ttrace_event (tsp
.tts_event
),
2895 target_post_wait (pid_to_ptid (tsp
.tts_pid
), *status
);
2900 printf ("Done waiting, pid is %d, tid %d\n", real_pid
, real_tid
);
2903 /* All code external to this module uses the tid, but calls
2904 * it "pid". There's some tweaking so that the outside sees
2905 * the first thread as having the same number as the starting
2908 return_pid
= map_to_gdb_tid (real_tid
);
2910 if (real_tid
== 0 || return_pid
== 0)
2912 warning ("Internal error: process-wait failed.");
2919 /* This function causes the caller's process to be traced by its
2920 parent. This is intended to be called after GDB forks itself,
2921 and before the child execs the target. Despite the name, it
2922 is called by the child.
2924 Note that HP-UX ttrace is rather funky in how this is done.
2925 If the parent wants to get the initial exec event of a child,
2926 it must set the ttrace event mask of the child to include execs.
2927 (The child cannot do this itself.) This must be done after the
2928 child is forked, but before it execs.
2930 To coordinate the parent and child, we implement a semaphore using
2931 pipes. After SETTRC'ing itself, the child tells the parent that
2932 it is now traceable by the parent, and waits for the parent's
2933 acknowledgement. The parent can then set the child's event mask,
2934 and notify the child that it can now exec.
2936 (The acknowledgement by parent happens as a result of a call to
2937 child_acknowledge_created_inferior.)
2940 parent_attach_all (int p1
, PTRACE_ARG3_TYPE p2
, int p3
)
2944 /* We need a memory home for a constant, to pass it to ttrace.
2945 The value of the constant is arbitrary, so long as both
2946 parent and child use the same value. Might as well use the
2947 "magic" constant provided by ttrace...
2949 uint64_t tc_magic_child
= TT_VERSION
;
2950 uint64_t tc_magic_parent
= 0;
2952 tt_status
= call_real_ttrace (
2957 (TTRACE_ARG_TYPE
) TT_VERSION
,
2963 /* Notify the parent that we're potentially ready to exec(). */
2964 write (startup_semaphore
.child_channel
[SEM_TALK
],
2966 sizeof (tc_magic_child
));
2968 /* Wait for acknowledgement from the parent. */
2969 read (startup_semaphore
.parent_channel
[SEM_LISTEN
],
2971 sizeof (tc_magic_parent
));
2973 if (tc_magic_child
!= tc_magic_parent
)
2974 warning ("mismatched semaphore magic");
2976 /* Discard our copy of the semaphore. */
2977 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
2978 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
2979 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
2980 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
2985 /* Despite being file-local, this routine is dealing with
2986 * actual process IDs, not thread ids. That's because it's
2987 * called before the first "wait" call, and there's no map
2988 * yet from tids to pids.
2990 * When it is called, a forked child is running, but waiting on
2991 * the semaphore. If you stop the child and re-start it,
2992 * things get confused, so don't do that! An attached child is
2995 * Since this is called after either attach or run, we
2996 * have to be the common part of both.
2999 require_notification_of_events (int real_pid
)
3002 ttevent_t notifiable_events
;
3005 ttstate_t thread_state
;
3009 printf ("Require notif, pid is %d\n", real_pid
);
3012 /* Temporary HACK: tell inftarg.c/child_wait to not
3013 * loop until pids are the same.
3015 not_same_real_pid
= 0;
3017 sigemptyset (¬ifiable_events
.tte_signals
);
3018 notifiable_events
.tte_opts
= TTEO_NONE
;
3020 /* This ensures that forked children inherit their parent's
3021 * event mask, which we're setting here.
3023 * NOTE: if you debug gdb with itself, then the ultimate
3024 * debuggee gets flags set by the outermost gdb, as
3025 * a child of a child will still inherit.
3027 notifiable_events
.tte_opts
|= TTEO_PROC_INHERIT
;
3029 notifiable_events
.tte_events
= TTEVT_DEFAULT
;
3030 notifiable_events
.tte_events
|= TTEVT_SIGNAL
;
3031 notifiable_events
.tte_events
|= TTEVT_EXEC
;
3032 notifiable_events
.tte_events
|= TTEVT_EXIT
;
3033 notifiable_events
.tte_events
|= TTEVT_FORK
;
3034 notifiable_events
.tte_events
|= TTEVT_VFORK
;
3035 notifiable_events
.tte_events
|= TTEVT_LWP_CREATE
;
3036 notifiable_events
.tte_events
|= TTEVT_LWP_EXIT
;
3037 notifiable_events
.tte_events
|= TTEVT_LWP_TERMINATE
;
3039 tt_status
= call_real_ttrace (
3040 TT_PROC_SET_EVENT_MASK
,
3043 (TTRACE_ARG_TYPE
) & notifiable_events
,
3044 (TTRACE_ARG_TYPE
) sizeof (notifiable_events
),
3049 require_notification_of_exec_events (int real_pid
)
3052 ttevent_t notifiable_events
;
3055 ttstate_t thread_state
;
3059 printf ("Require notif, pid is %d\n", real_pid
);
3062 /* Temporary HACK: tell inftarg.c/child_wait to not
3063 * loop until pids are the same.
3065 not_same_real_pid
= 0;
3067 sigemptyset (¬ifiable_events
.tte_signals
);
3068 notifiable_events
.tte_opts
= TTEO_NOSTRCCHLD
;
3070 /* This ensures that forked children don't inherit their parent's
3071 * event mask, which we're setting here.
3073 notifiable_events
.tte_opts
&= ~TTEO_PROC_INHERIT
;
3075 notifiable_events
.tte_events
= TTEVT_DEFAULT
;
3076 notifiable_events
.tte_events
|= TTEVT_EXEC
;
3077 notifiable_events
.tte_events
|= TTEVT_EXIT
;
3079 tt_status
= call_real_ttrace (
3080 TT_PROC_SET_EVENT_MASK
,
3083 (TTRACE_ARG_TYPE
) & notifiable_events
,
3084 (TTRACE_ARG_TYPE
) sizeof (notifiable_events
),
3089 /* This function is called by the parent process, with pid being the
3090 * ID of the child process, after the debugger has forked.
3093 child_acknowledge_created_inferior (int pid
)
3095 /* We need a memory home for a constant, to pass it to ttrace.
3096 The value of the constant is arbitrary, so long as both
3097 parent and child use the same value. Might as well use the
3098 "magic" constant provided by ttrace...
3100 uint64_t tc_magic_parent
= TT_VERSION
;
3101 uint64_t tc_magic_child
= 0;
3103 /* Wait for the child to tell us that it has forked. */
3104 read (startup_semaphore
.child_channel
[SEM_LISTEN
],
3106 sizeof (tc_magic_child
));
3108 /* Clear thread info now. We'd like to do this in
3109 * "require...", but that messes up attach.
3111 clear_thread_info ();
3113 /* Tell the "rest of gdb" that the initial thread exists.
3114 * This isn't really a hack. Other thread-based versions
3115 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3117 * Q: Why don't we also add this thread to the local
3118 * list via "add_tthread"?
3120 * A: Because we don't know the tid, and can't stop the
3121 * the process safely to ask what it is. Anyway, we'll
3122 * add it when it gets the EXEC event.
3124 add_thread (pid_to_ptid (pid
)); /* in thread.c */
3126 /* We can now set the child's ttrace event mask.
3128 require_notification_of_exec_events (pid
);
3130 /* Tell ourselves that the process is running.
3132 process_state
= RUNNING
;
3134 /* Notify the child that it can exec. */
3135 write (startup_semaphore
.parent_channel
[SEM_TALK
],
3137 sizeof (tc_magic_parent
));
3139 /* Discard our copy of the semaphore. */
3140 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
3141 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
3142 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
3143 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
3148 * arrange for notification of all events by
3149 * calling require_notification_of_events.
3152 child_post_startup_inferior (ptid_t ptid
)
3154 require_notification_of_events (PIDGET (ptid
));
3157 /* From here on, we should expect tids rather than pids.
3160 hppa_enable_catch_fork (int tid
)
3163 ttevent_t ttrace_events
;
3165 /* Get the set of events that are currently enabled.
3167 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3169 (TTRACE_ARG_TYPE
) & ttrace_events
,
3170 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3173 perror_with_name ("ttrace");
3175 /* Add forks to that set. */
3176 ttrace_events
.tte_events
|= TTEVT_FORK
;
3180 printf ("enable fork, tid is %d\n", tid
);
3183 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3185 (TTRACE_ARG_TYPE
) & ttrace_events
,
3186 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3189 perror_with_name ("ttrace");
3194 hppa_disable_catch_fork (int 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 /* Remove forks from that set. */
3211 ttrace_events
.tte_events
&= ~TTEVT_FORK
;
3215 printf ("disable fork, 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");
3229 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3231 child_insert_fork_catchpoint (int tid
)
3233 /* Enable reporting of fork events from the kernel. */
3234 /* ??rehrauer: For the moment, we're always enabling these events,
3235 and just ignoring them if there's no catchpoint to catch them.
3242 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3244 child_remove_fork_catchpoint (int tid
)
3246 /* Disable reporting of fork events from the kernel. */
3247 /* ??rehrauer: For the moment, we're always enabling these events,
3248 and just ignoring them if there's no catchpoint to catch them.
3256 hppa_enable_catch_vfork (int tid
)
3259 ttevent_t ttrace_events
;
3261 /* Get the set of events that are currently enabled.
3263 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3265 (TTRACE_ARG_TYPE
) & ttrace_events
,
3266 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3270 perror_with_name ("ttrace");
3272 /* Add vforks to that set. */
3273 ttrace_events
.tte_events
|= TTEVT_VFORK
;
3277 printf ("enable vfork, tid is %d\n", tid
);
3280 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3282 (TTRACE_ARG_TYPE
) & ttrace_events
,
3283 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3287 perror_with_name ("ttrace");
3292 hppa_disable_catch_vfork (int tid
)
3295 ttevent_t ttrace_events
;
3297 /* Get the set of events that are currently enabled. */
3298 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
3300 (TTRACE_ARG_TYPE
) & ttrace_events
,
3301 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3305 perror_with_name ("ttrace");
3307 /* Remove vforks from that set. */
3308 ttrace_events
.tte_events
&= ~TTEVT_VFORK
;
3312 printf ("disable vfork, tid is %d\n", tid
);
3314 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
3316 (TTRACE_ARG_TYPE
) & ttrace_events
,
3317 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
3321 perror_with_name ("ttrace");
3325 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3327 child_insert_vfork_catchpoint (int tid
)
3329 /* Enable reporting of vfork events from the kernel. */
3330 /* ??rehrauer: For the moment, we're always enabling these events,
3331 and just ignoring them if there's no catchpoint to catch them.
3338 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3340 child_remove_vfork_catchpoint (int tid
)
3342 /* Disable reporting of vfork events from the kernel. */
3343 /* ??rehrauer: For the moment, we're always enabling these events,
3344 and just ignoring them if there's no catchpoint to catch them.
3350 /* Q: Do we need to map the returned process ID to a thread ID?
3352 * A: I don't think so--here we want a _real_ pid. Any later
3353 * operations will call "require_notification_of_events" and
3354 * start the mapping.
3357 hpux_has_forked (int tid
, int *childpid
)
3360 ttstate_t ttrace_state
;
3363 /* Do we have cached thread state that we can consult? If so, use it. */
3364 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3367 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3370 /* Nope, must read the thread's current state */
3373 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3375 (TTRACE_ARG_TYPE
) & ttrace_state
,
3376 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3380 perror_with_name ("ttrace");
3386 if (ttrace_state
.tts_event
& TTEVT_FORK
)
3388 *childpid
= ttrace_state
.tts_u
.tts_fork
.tts_fpid
;
3395 /* See hpux_has_forked for pid discussion.
3398 hpux_has_vforked (int tid
, int *childpid
)
3401 ttstate_t ttrace_state
;
3404 /* Do we have cached thread state that we can consult? If so, use it. */
3405 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3407 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3409 /* Nope, must read the thread's current state */
3412 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3414 (TTRACE_ARG_TYPE
) & ttrace_state
,
3415 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3419 perror_with_name ("ttrace");
3425 if (ttrace_state
.tts_event
& TTEVT_VFORK
)
3427 *childpid
= ttrace_state
.tts_u
.tts_fork
.tts_fpid
;
3435 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3437 child_insert_exec_catchpoint (int tid
)
3439 /* Enable reporting of exec events from the kernel. */
3440 /* ??rehrauer: For the moment, we're always enabling these events,
3441 and just ignoring them if there's no catchpoint to catch them.
3448 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3450 child_remove_exec_catchpoint (int tid
)
3452 /* Disable reporting of execevents from the kernel. */
3453 /* ??rehrauer: For the moment, we're always enabling these events,
3454 and just ignoring them if there's no catchpoint to catch them.
3462 hpux_has_execd (int tid
, char **execd_pathname
)
3465 ttstate_t ttrace_state
;
3468 /* Do we have cached thread state that we can consult? If so, use it. */
3469 tinfo
= find_thread_info (map_from_gdb_tid (tid
));
3471 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3473 /* Nope, must read the thread's current state */
3476 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3478 (TTRACE_ARG_TYPE
) & ttrace_state
,
3479 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3483 perror_with_name ("ttrace");
3489 if (ttrace_state
.tts_event
& TTEVT_EXEC
)
3491 /* See child_pid_to_exec_file in this file: this is a macro.
3493 char *exec_file
= target_pid_to_exec_file (tid
);
3495 *execd_pathname
= savestring (exec_file
, strlen (exec_file
));
3504 hpux_has_syscall_event (int pid
, enum target_waitkind
*kind
, int *syscall_id
)
3507 ttstate_t ttrace_state
;
3510 /* Do we have cached thread state that we can consult? If so, use it. */
3511 tinfo
= find_thread_info (map_from_gdb_tid (pid
));
3513 copy_ttstate_t (&ttrace_state
, &tinfo
->last_stop_state
);
3515 /* Nope, must read the thread's current state */
3518 tt_status
= call_ttrace (TT_LWP_GET_STATE
,
3520 (TTRACE_ARG_TYPE
) & ttrace_state
,
3521 (TTRACE_ARG_TYPE
) sizeof (ttrace_state
),
3525 perror_with_name ("ttrace");
3531 *kind
= TARGET_WAITKIND_SPURIOUS
; /* Until proven otherwise... */
3534 if (ttrace_state
.tts_event
& TTEVT_SYSCALL_ENTRY
)
3535 *kind
= TARGET_WAITKIND_SYSCALL_ENTRY
;
3536 else if (ttrace_state
.tts_event
& TTEVT_SYSCALL_RETURN
)
3537 *kind
= TARGET_WAITKIND_SYSCALL_RETURN
;
3541 *syscall_id
= ttrace_state
.tts_scno
;
3547 #if defined(CHILD_THREAD_ALIVE)
3549 /* Check to see if the given thread is alive.
3551 * We'll trust the thread list, as the more correct
3552 * approach of stopping the process and spinning down
3553 * the OS's thread list is _very_ expensive.
3555 * May need a FIXME for that reason.
3558 child_thread_alive (ptid_t ptid
)
3560 lwpid_t gdb_tid
= PIDGET (ptid
);
3563 /* This spins down the lists twice.
3564 * Possible peformance improvement here!
3566 tid
= map_from_gdb_tid (gdb_tid
);
3567 return !is_terminated (tid
);
3574 /* This function attempts to read the specified number of bytes from the
3575 save_state_t that is our view into the hardware registers, starting at
3576 ss_offset, and ending at ss_offset + sizeof_buf - 1
3578 If this function succeeds, it deposits the fetched bytes into buf,
3581 If it fails, it returns a negative result. The contents of buf are
3582 undefined it this function fails.
3585 read_from_register_save_state (int tid
, TTRACE_ARG_TYPE ss_offset
, char *buf
,
3589 register_value_t register_value
= 0;
3591 tt_status
= call_ttrace (TT_LWP_RUREGS
,
3594 (TTRACE_ARG_TYPE
) sizeof_buf
,
3595 (TTRACE_ARG_TYPE
) buf
);
3598 /* Map ttrace's version of success to our version.
3599 * Sometime ttrace returns 0, but that's ok here.
3607 /* This function attempts to write the specified number of bytes to the
3608 save_state_t that is our view into the hardware registers, starting at
3609 ss_offset, and ending at ss_offset + sizeof_buf - 1
3611 If this function succeeds, it deposits the bytes in buf, and returns 0.
3613 If it fails, it returns a negative result. The contents of the save_state_t
3614 are undefined it this function fails.
3617 write_to_register_save_state (int tid
, TTRACE_ARG_TYPE ss_offset
, char *buf
,
3621 register_value_t register_value
= 0;
3623 tt_status
= call_ttrace (TT_LWP_WUREGS
,
3626 (TTRACE_ARG_TYPE
) sizeof_buf
,
3627 (TTRACE_ARG_TYPE
) buf
);
3632 /* This function is a sop to the largeish number of direct calls
3633 to call_ptrace that exist in other files. Rather than create
3634 functions whose name abstracts away from ptrace, and change all
3635 the present callers of call_ptrace, we'll do the expedient (and
3636 perhaps only practical) thing.
3638 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3639 process. Thus, we must translate all ptrace requests into their
3640 process-specific, ttrace equivalents.
3643 call_ptrace (int pt_request
, int gdb_tid
, PTRACE_ARG3_TYPE addr
, int data
)
3646 TTRACE_ARG_TYPE tt_addr
= (TTRACE_ARG_TYPE
) addr
;
3647 TTRACE_ARG_TYPE tt_data
= (TTRACE_ARG_TYPE
) data
;
3648 TTRACE_ARG_TYPE tt_addr2
= TT_NIL
;
3650 register_value_t register_value
;
3653 /* Perform the necessary argument translation. Note that some
3654 cases are funky enough in the ttrace realm that we handle them
3659 /* The following cases cannot conveniently be handled conveniently
3660 by merely adjusting the ptrace arguments and feeding into the
3661 generic call to ttrace at the bottom of this function.
3663 Note that because all branches of this switch end in "return",
3664 there's no need for any "break" statements.
3667 return parent_attach_all (0, 0, 0);
3670 tt_status
= read_from_register_save_state (gdb_tid
,
3673 sizeof (register_value
));
3676 return register_value
;
3679 register_value
= (int) tt_data
;
3680 tt_status
= write_to_register_save_state (gdb_tid
,
3683 sizeof (register_value
));
3688 tt_status
= call_ttrace (TT_PROC_RDTEXT
, /* Implicit 4-byte xfer becomes block-xfer. */
3691 (TTRACE_ARG_TYPE
) 4,
3692 (TTRACE_ARG_TYPE
) & read_buf
);
3698 tt_status
= call_ttrace (TT_PROC_RDDATA
, /* Implicit 4-byte xfer becomes block-xfer. */
3701 (TTRACE_ARG_TYPE
) 4,
3702 (TTRACE_ARG_TYPE
) & read_buf
);
3708 tt_status
= call_real_ttrace (TT_PROC_ATTACH
,
3709 map_from_gdb_tid (gdb_tid
),
3712 (TTRACE_ARG_TYPE
) TT_VERSION
,
3718 /* The following cases are handled by merely adjusting the ptrace
3719 arguments and feeding into the generic call to ttrace.
3722 tt_request
= TT_PROC_DETACH
;
3726 tt_request
= TT_PROC_WRTEXT
; /* Translates 4-byte xfer to block-xfer. */
3727 tt_data
= 4; /* This many bytes. */
3728 tt_addr2
= (TTRACE_ARG_TYPE
) & data
; /* Address of xfer source. */
3732 tt_request
= TT_PROC_WRDATA
; /* Translates 4-byte xfer to block-xfer. */
3733 tt_data
= 4; /* This many bytes. */
3734 tt_addr2
= (TTRACE_ARG_TYPE
) & data
; /* Address of xfer source. */
3738 tt_request
= TT_PROC_RDTEXT
;
3742 tt_request
= TT_PROC_RDDATA
;
3746 tt_request
= TT_PROC_WRTEXT
;
3750 tt_request
= TT_PROC_WRDATA
;
3754 tt_request
= TT_PROC_CONTINUE
;
3758 tt_request
= TT_LWP_SINGLE
; /* Should not be making this request? */
3762 tt_request
= TT_PROC_EXIT
;
3765 case PT_GET_PROCESS_PATHNAME
:
3766 tt_request
= TT_PROC_GET_PATHNAME
;
3770 tt_request
= pt_request
; /* Let ttrace be the one to complain. */
3774 return call_ttrace (tt_request
,
3781 /* Kill that pesky process!
3784 kill_inferior (void)
3789 thread_info
**paranoia
;
3792 if (PIDGET (inferior_ptid
) == 0)
3795 /* Walk the list of "threads", some of which are "pseudo threads",
3796 aka "processes". For each that is NOT inferior_ptid, stop it,
3799 You see, we may not have just a single process to kill. If we're
3800 restarting or quitting or detaching just after the inferior has
3801 forked, then we've actually two processes to clean up.
3803 But we can't just call target_mourn_inferior() for each, since that
3804 zaps the target vector.
3807 paranoia
= (thread_info
**) xmalloc (thread_head
.count
*
3808 sizeof (thread_info
*));
3811 t
= thread_head
.head
;
3815 paranoia
[para_count
] = t
;
3816 for (i
= 0; i
< para_count
; i
++)
3818 if (t
->next
== paranoia
[i
])
3820 warning ("Bad data in gdb's thread data; repairing.");
3826 if (t
->am_pseudo
&& (t
->pid
!= PIDGET (inferior_ptid
)))
3828 call_ttrace (TT_PROC_EXIT
,
3839 call_ttrace (TT_PROC_EXIT
,
3840 PIDGET (inferior_ptid
),
3844 target_mourn_inferior ();
3845 clear_thread_info ();
3849 #ifndef CHILD_RESUME
3851 /* Sanity check a thread about to be continued.
3854 thread_dropping_event_check (thread_info
*p
)
3859 * This seems to happen when we "next" over a
3860 * "fork()" while following the parent. If it's
3861 * the FORK event, that's ok. If it's a SIGNAL
3862 * in the unfollowed child, that's ok to--but
3863 * how can we know that's what's going on?
3869 if (p
->last_stop_state
.tts_event
== TTEVT_FORK
)
3874 else if (p
->last_stop_state
.tts_event
== TTEVT_SIGNAL
)
3876 /* Ok, close eyes and let it happen.
3882 /* This shouldn't happen--we're dropping a
3885 warning ("About to continue process %d, thread %d with unhandled event %s.",
3887 get_printable_name_of_ttrace_event (
3888 p
->last_stop_state
.tts_event
));
3898 /* No saved state, have to assume it failed.
3900 warning ("About to continue process %d, thread %d with unhandled event.",
3909 } /* thread_dropping_event_check */
3911 /* Use a loop over the threads to continue all the threads but
3912 * the one specified, which is to be stepped.
3915 threads_continue_all_but_one (lwpid_t gdb_tid
, int signal
)
3926 printf ("Using loop over threads to step/resume with signals\n");
3929 /* First update the thread list.
3932 real_tid
= map_from_gdb_tid (gdb_tid
);
3933 real_pid
= get_pid_for (real_tid
);
3935 scan_tid
= get_process_first_stopped_thread_id (real_pid
, &state
);
3936 while (0 != scan_tid
)
3940 /* FIX: later should check state is stopped;
3941 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
3944 if ((state
.tts_flags
& TTS_STATEMASK
) != TTS_WASSUSPENDED
)
3945 printf ("About to continue non-stopped thread %d\n", scan_tid
);
3948 p
= find_thread_info (scan_tid
);
3951 add_tthread (real_pid
, scan_tid
);
3952 p
= find_thread_info (scan_tid
);
3954 /* This is either a newly-created thread or the
3955 * result of a fork; in either case there's no
3956 * actual event to worry about.
3960 if (state
.tts_event
!= TTEVT_NONE
)
3962 /* Oops, do need to worry!
3964 warning ("Unexpected thread with \"%s\" event.",
3965 get_printable_name_of_ttrace_event (state
.tts_event
));
3968 else if (scan_tid
!= p
->tid
)
3969 error ("Bad data in thread database.");
3974 printf ("Why are we continuing a dead thread?\n");
3979 scan_tid
= get_process_next_stopped_thread_id (real_pid
, &state
);
3982 /* Remove unseen threads.
3984 update_thread_list ();
3986 /* Now run down the thread list and continue or step.
3988 for (p
= thread_head
.head
; p
; p
= p
->next
)
3993 thread_dropping_event_check (p
);
3995 /* Pass the correct signals along.
3999 thread_signal
= p
->signal_value
;
4005 if (p
->tid
!= real_tid
)
4008 * Not the thread of interest, so continue it
4009 * as the user expects.
4011 if (p
->stepping_mode
== DO_STEP
)
4013 /* Just step this thread.
4019 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4024 /* Regular continue (default case).
4030 (TTRACE_ARG_TYPE
) target_signal_to_host (thread_signal
),
4036 /* Step the thread of interest.
4042 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4045 } /* Loop over threads */
4046 } /* End threads_continue_all_but_one */
4048 /* Use a loop over the threads to continue all the threads.
4049 * This is done when a signal must be sent to any of the threads.
4052 threads_continue_all_with_signals (lwpid_t gdb_tid
, int signal
)
4063 printf ("Using loop over threads to resume with signals\n");
4066 /* Scan and update thread list.
4069 real_tid
= map_from_gdb_tid (gdb_tid
);
4070 real_pid
= get_pid_for (real_tid
);
4072 scan_tid
= get_process_first_stopped_thread_id (real_pid
, &state
);
4073 while (0 != scan_tid
)
4078 if ((state
.tts_flags
& TTS_STATEMASK
) != TTS_WASSUSPENDED
)
4079 warning ("About to continue non-stopped thread %d\n", scan_tid
);
4082 p
= find_thread_info (scan_tid
);
4085 add_tthread (real_pid
, scan_tid
);
4086 p
= find_thread_info (scan_tid
);
4088 /* This is either a newly-created thread or the
4089 * result of a fork; in either case there's no
4090 * actual event to worry about.
4094 if (state
.tts_event
!= TTEVT_NONE
)
4096 /* Oops, do need to worry!
4098 warning ("Unexpected thread with \"%s\" event.",
4099 get_printable_name_of_ttrace_event (state
.tts_event
));
4106 printf ("Why are we continuing a dead thread? (1)\n");
4111 scan_tid
= get_process_next_stopped_thread_id (real_pid
, &state
);
4114 /* Remove unseen threads from our list.
4116 update_thread_list ();
4118 /* Continue the threads.
4120 for (p
= thread_head
.head
; p
; p
= p
->next
)
4125 thread_dropping_event_check (p
);
4127 /* Pass the correct signals along.
4129 if (p
->tid
== real_tid
)
4131 thread_signal
= signal
;
4134 else if (p
->have_signal
)
4136 thread_signal
= p
->signal_value
;
4142 if (p
->stepping_mode
== DO_STEP
)
4148 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4153 /* Continue this thread (default case).
4159 (TTRACE_ARG_TYPE
) target_signal_to_host (thread_signal
),
4163 } /* End threads_continue_all_with_signals */
4165 /* Step one thread only.
4168 thread_fake_step (lwpid_t tid
, enum target_signal signal
)
4175 printf ("Doing a fake-step over a bpt, etc. for %d\n", tid
);
4177 if (is_terminated (tid
))
4178 printf ("Why are we continuing a dead thread? (4)\n");
4182 if (doing_fake_step
)
4183 warning ("Step while step already in progress.");
4185 /* See if there's a saved signal value for this
4186 * thread to be passed on, but no current signal.
4188 p
= find_thread_info (tid
);
4191 if (p
->have_signal
&& signal
== TARGET_SIGNAL_0
)
4193 /* Pass on a saved signal.
4195 signal
= p
->signal_value
;
4202 warning ("Internal error: continuing unhandled thread.");
4204 call_ttrace (TT_LWP_SINGLE
,
4207 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4210 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4211 * for this thread only, and clear any saved signal info.
4213 doing_fake_step
= 1;
4214 fake_step_tid
= tid
;
4216 } /* End thread_fake_step */
4218 /* Continue one thread when a signal must be sent to it.
4221 threads_continue_one_with_signal (lwpid_t gdb_tid
, int signal
)
4229 printf ("Continuing one thread with a signal\n");
4232 real_tid
= map_from_gdb_tid (gdb_tid
);
4233 real_pid
= get_pid_for (real_tid
);
4235 p
= find_thread_info (real_tid
);
4238 add_tthread (real_pid
, real_tid
);
4244 printf ("Why are we continuing a dead thread? (2)\n");
4248 warning ("Internal error: continuing unhandled thread.");
4252 call_ttrace (TT_LWP_CONTINUE
,
4255 (TTRACE_ARG_TYPE
) target_signal_to_host (signal
),
4260 #ifndef CHILD_RESUME
4262 /* Resume execution of the inferior process.
4264 * This routine is in charge of setting the "handled" bits.
4266 * If STEP is zero, continue it.
4267 * If STEP is nonzero, single-step it.
4269 * If SIGNAL is nonzero, give it that signal.
4271 * If TID is -1, apply to all threads.
4272 * If TID is not -1, apply to specified thread.
4276 * TID \________________________________________________
4278 * -1 | Step current Continue all threads
4279 * | thread and (but which gets any
4280 * | continue others signal?--We look at
4281 * | "inferior_ptid")
4283 * N | Step _this_ thread Continue _this_ thread
4284 * | and leave others and leave others
4285 * | stopped; internally stopped; used only for
4286 * | used by gdb, never hardware watchpoints
4287 * | a user command. and attach, never a
4291 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
4293 int resume_all_threads
;
4295 process_state_t new_process_state
;
4296 lwpid_t gdb_tid
= PIDGET (ptid
);
4298 resume_all_threads
=
4299 (gdb_tid
== INFTTRACE_ALL_THREADS
) ||
4302 if (resume_all_threads
)
4304 /* Resume all threads, but first pick a tid value
4305 * so we can get the pid when in call_ttrace doing
4308 if (vfork_in_flight
)
4309 tid
= vforking_child_pid
;
4311 tid
= map_from_gdb_tid (PIDGET (inferior_ptid
));
4314 tid
= map_from_gdb_tid (gdb_tid
);
4319 if (more_events_left
)
4320 printf ("More events; ");
4323 printf ("Sending signal %d; ", signal
);
4325 if (resume_all_threads
)
4328 printf ("Continue process %d\n", tid
);
4330 printf ("Step/continue thread %d\n", tid
);
4335 printf ("Continue thread %d\n", tid
);
4337 printf ("Step just thread %d\n", tid
);
4340 if (vfork_in_flight
)
4341 printf ("Vfork in flight\n");
4345 if (process_state
== RUNNING
)
4346 warning ("Internal error in resume logic; doing resume or step anyway.");
4348 if (!step
/* Asked to continue... */
4349 && resume_all_threads
/* whole process.. */
4350 && signal
!= 0 /* with a signal... */
4351 && more_events_left
> 0)
4352 { /* but we can't yet--save it! */
4354 /* Continue with signal means we have to set the pending
4355 * signal value for this thread.
4361 printf ("Saving signal %d for thread %d\n", signal
, tid
);
4364 k
= find_thread_info (tid
);
4368 k
->signal_value
= signal
;
4373 printf ("Why are we continuing a dead thread? (3)\n");
4381 printf ("No thread info for tid %d\n", tid
);
4386 /* Are we faking this "continue" or "step"?
4388 * We used to do steps by continuing all the threads for
4389 * which the events had been handled already. While
4390 * conceptually nicer (hides it all in a lower level), this
4391 * can lead to starvation and a hang (e.g. all but one thread
4392 * are unhandled at a breakpoint just before a "join" operation,
4393 * and one thread is in the join, and the user wants to step that
4396 if (resume_all_threads
/* Whole process, therefore user command */
4397 && more_events_left
> 0)
4398 { /* But we can't do this yet--fake it! */
4403 /* No need to do any notes on a per-thread
4404 * basis--we're done!
4406 #ifdef WAIT_BUFFER_DEBUG
4408 printf ("Faking a process resume.\n");
4416 #ifdef WAIT_BUFFER_DEBUG
4418 printf ("Faking a process step.\n");
4423 p
= find_thread_info (tid
);
4426 warning ("No thread information for tid %d, 'next' command ignored.\n", tid
);
4435 printf ("Why are we continuing a dead thread? (3.5)\n");
4438 if (p
->stepping_mode
!= DO_DEFAULT
)
4440 warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored.");
4446 p
->stepping_mode
= DO_STEP
;
4448 p
->stepping_mode
= DO_CONTINUE
;
4451 } /* Have thread info */
4452 } /* Must fake step or go */
4454 /* Execept for fake-steps, from here on we know we are
4455 * going to wind up with a running process which will
4458 new_process_state
= RUNNING
;
4460 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4461 * it was. (If GDB wanted it to start some other way, we have already
4462 * written a new PC value to the child.)
4464 * If this system does not support PT_STEP, a higher level function will
4465 * have called single_step() to transmute the step request into a
4466 * continue request (by setting breakpoints on all possible successor
4467 * instructions), so we don't have to worry about that here.
4471 if (resume_all_threads
)
4474 * Regular user step: other threads get a "continue".
4476 threads_continue_all_but_one (tid
, signal
);
4477 clear_all_handled ();
4478 clear_all_stepping_mode ();
4483 /* "Fake step": gdb is stepping one thread over a
4484 * breakpoint, watchpoint, or out of a library load
4485 * event, etc. The rest just stay where they are.
4487 * Also used when there are pending events: we really
4488 * step the current thread, but leave the rest stopped.
4489 * Users can't request this, but "wait_for_inferior"
4492 thread_fake_step (tid
, signal
);
4494 /* Clear the "handled" state of this thread, because
4495 * we'll soon get a new event for it. Other events
4496 * stay as they were.
4498 clear_handled (tid
);
4499 clear_stepping_mode (tid
);
4500 new_process_state
= FAKE_STEPPING
;
4506 /* TT_LWP_CONTINUE can pass signals to threads, TT_PROC_CONTINUE can't.
4507 Therefore, we really can't use TT_PROC_CONTINUE here.
4509 Consider a process which stopped due to signal which gdb decides
4510 to handle and not pass on to the inferior. In that case we must
4511 clear the pending signal by restarting the inferior using
4512 TT_LWP_CONTINUE and pass zero as the signal number. Else the
4513 pending signal will be passed to the inferior. interrupt.exp
4514 in the testsuite does this precise thing and fails due to the
4515 unwanted signal delivery to the inferior. */
4516 /* drow/2002-12-05: However, note that we must use TT_PROC_CONTINUE
4517 if we are tracing a vfork. */
4518 if (vfork_in_flight
)
4520 call_ttrace (TT_PROC_CONTINUE
, tid
, TT_NIL
, TT_NIL
, TT_NIL
);
4521 clear_all_handled ();
4522 clear_all_stepping_mode ();
4524 else if (resume_all_threads
)
4528 printf ("Doing a continue by loop of all threads\n");
4531 threads_continue_all_with_signals (tid
, signal
);
4533 clear_all_handled ();
4534 clear_all_stepping_mode ();
4539 printf ("Doing a continue w/signal of just thread %d\n", tid
);
4542 threads_continue_one_with_signal (tid
, signal
);
4544 /* Clear the "handled" state of this thread, because we
4545 will soon get a new event for it. Other events can
4546 stay as they were. */
4547 clear_handled (tid
);
4548 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_ptid"
4569 * and thus may not want to clear out our data. FIXME?
4573 update_thread_state_after_attach (int pid
, attach_continue_t kind_of_go
)
4576 ttstate_t thread_state
;
4580 /* The process better be stopped.
4582 if (process_state
!= STOPPED
4583 && process_state
!= VFORKING
)
4584 warning ("Internal error attaching.");
4586 /* Clear out old tthread info and start over. This has the
4587 * side effect of ensuring that the TRAP is reported as being
4588 * in the right thread (re-mapped from tid to pid).
4590 * It's because we need to add the tthread _now_ that we
4591 * need to call "clear_thread_info" _now_, and that's why
4592 * "require_notification_of_events" doesn't clear the thread
4593 * info (it's called later than this routine).
4595 clear_thread_info ();
4598 for (tid
= get_process_first_stopped_thread_id (pid
, &thread_state
);
4600 tid
= get_process_next_stopped_thread_id (pid
, &thread_state
))
4609 printf ("Attaching to process %d, thread %d\n",
4614 /* Tell ourselves and the "rest of gdb" that this thread
4617 * This isn't really a hack. Other thread-based versions
4618 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4620 * We don't need to do mapping here, as we know this
4621 * is the first thread and thus gets the real pid
4622 * (and is "inferior_ptid").
4624 * NOTE: it probably isn't the originating thread,
4625 * but that doesn't matter (we hope!).
4627 add_tthread (pid
, tid
);
4628 p
= find_thread_info (tid
);
4629 if (NULL
== p
) /* ?We just added it! */
4630 error ("Internal error adding a thread on attach.");
4632 copy_ttstate_t (&p
->last_stop_state
, &thread_state
);
4635 if (DO_ATTACH_CONTINUE
== kind_of_go
)
4638 * If we are going to CONTINUE afterwards,
4639 * raising a SIGTRAP, don't bother trying to
4640 * handle this event. But check first!
4642 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
);
4662 /* There will be no "continue" opertion, so the
4663 * process remains stopped. Don't set any events
4664 * handled except the "gimmies".
4666 switch (p
->last_stop_state
.tts_event
)
4670 /* Ok to ignore this.
4672 set_handled (pid
, tid
);
4677 /* Expected "other" FORK or EXEC event from a
4683 printf ("Internal error: failed to handle event %s on process %d, thread %d.",
4684 get_printable_name_of_ttrace_event (
4685 p
->last_stop_state
.tts_event
),
4690 add_thread (pid_to_ptid (pid
)); /* in thread.c */
4698 /* One mustn't call ttrace_wait() after attaching via ttrace,
4699 'cause the process is stopped already.
4701 However, the upper layers of gdb's execution control will
4702 want to wait after attaching (but not after forks, in
4703 which case they will be doing a "target_resume", anticipating
4704 a later TTEVT_EXEC or TTEVT_FORK event).
4706 To make this attach() implementation more compatible with
4707 others, we'll make the attached-to process raise a SIGTRAP.
4709 Issue: this continues only one thread. That could be
4710 dangerous if the thread is blocked--the process won't run
4711 and no trap will be raised. FIX! (check state.tts_flags?
4712 need one that's either TTS_WASRUNNING--but we've stopped
4713 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4715 if (DO_ATTACH_CONTINUE
== kind_of_go
)
4717 tt_status
= call_real_ttrace (
4722 (TTRACE_ARG_TYPE
) target_signal_to_host (TARGET_SIGNAL_TRAP
),
4725 perror_with_name ("ttrace");
4727 clear_handled (a_thread
); /* So TRAP will be reported. */
4731 process_state
= RUNNING
;
4736 #endif /* ATTACH_DETACH */
4739 #ifdef ATTACH_DETACH
4740 /* Start debugging the process whose number is PID.
4748 tt_status
= call_real_ttrace (
4753 (TTRACE_ARG_TYPE
) TT_VERSION
,
4756 perror_with_name ("ttrace attach");
4758 /* If successful, the process is now stopped.
4760 process_state
= STOPPED
;
4762 /* Our caller ("attach_command" in "infcmd.c")
4763 * expects to do a "wait_for_inferior" after
4764 * the attach, so make sure the inferior is
4765 * running when we're done.
4767 update_thread_state_after_attach (pid
, DO_ATTACH_CONTINUE
);
4773 #if defined(CHILD_POST_ATTACH)
4775 child_post_attach (int pid
)
4779 printf ("child-post-attach call\n");
4782 require_notification_of_events (pid
);
4787 /* Stop debugging the process whose number is PID
4788 and continue it with signal number SIGNAL.
4789 SIGNAL = 0 means just continue it.
4795 call_ttrace (TT_PROC_DETACH
,
4796 PIDGET (inferior_ptid
),
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 (void)
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
4828 WRITE is nonzero. TARGET is ignored.
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 (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
4837 struct mem_attrib
*attrib
,
4838 struct target_ops
*target
)
4841 /* Round starting address down to longword boundary. */
4842 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (TTRACE_XFER_TYPE
);
4843 /* Round ending address up; get number of longwords that makes. */
4845 = (((memaddr
+ len
) - addr
) + sizeof (TTRACE_XFER_TYPE
) - 1)
4846 / sizeof (TTRACE_XFER_TYPE
);
4847 /* Allocate buffer of that many longwords. */
4848 /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe
4849 because it uses alloca to allocate a buffer of arbitrary size.
4850 For very large xfers, this could crash GDB's stack. */
4851 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
))
4860 /* Need part of initial word -- fetch it. */
4861 buffer
[0] = call_ttrace (TT_LWP_RDTEXT
,
4862 PIDGET (inferior_ptid
),
4863 (TTRACE_ARG_TYPE
) addr
,
4868 if (count
> 1) /* FIXME, avoid if even boundary */
4870 buffer
[count
- 1] = call_ttrace (TT_LWP_RDTEXT
,
4871 PIDGET (inferior_ptid
),
4873 (addr
+ (count
- 1) * sizeof (TTRACE_XFER_TYPE
))),
4878 /* Copy data to be written over corresponding part of buffer */
4880 memcpy ((char *) buffer
+ (memaddr
& (sizeof (TTRACE_XFER_TYPE
) - 1)),
4884 /* Write the entire buffer. */
4886 for (i
= 0; i
< count
; i
++, addr
+= sizeof (TTRACE_XFER_TYPE
))
4889 call_ttrace (TT_LWP_WRDATA
,
4890 PIDGET (inferior_ptid
),
4891 (TTRACE_ARG_TYPE
) addr
,
4892 (TTRACE_ARG_TYPE
) buffer
[i
],
4896 /* Using the appropriate one (I or D) is necessary for
4897 Gould NP1, at least. */
4899 call_ttrace (TT_LWP_WRTEXT
,
4900 PIDGET (inferior_ptid
),
4901 (TTRACE_ARG_TYPE
) addr
,
4902 (TTRACE_ARG_TYPE
) buffer
[i
],
4911 /* Read all the longwords */
4912 for (i
= 0; i
< count
; i
++, addr
+= sizeof (TTRACE_XFER_TYPE
))
4915 buffer
[i
] = call_ttrace (TT_LWP_RDTEXT
,
4916 PIDGET (inferior_ptid
),
4917 (TTRACE_ARG_TYPE
) addr
,
4925 /* Copy appropriate bytes out of the buffer. */
4927 (char *) buffer
+ (memaddr
& (sizeof (TTRACE_XFER_TYPE
) - 1)),
4937 int udot_off
; /* Offset into user struct */
4938 int udot_val
; /* Value from user struct at udot_off */
4939 char mess
[128]; /* For messages */
4941 if (!target_has_execution
)
4943 error ("The program is not being run.");
4946 #if !defined (KERNEL_U_SIZE)
4948 /* Adding support for this command is easy. Typically you just add a
4949 routine, called "kernel_u_size" that returns the size of the user
4950 struct, to the appropriate *-nat.c file and then add to the native
4951 config file "#define KERNEL_U_SIZE kernel_u_size()" */
4952 error ("Don't know how large ``struct user'' is in this version of gdb.");
4956 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
4958 if ((udot_off
% 24) == 0)
4962 printf_filtered ("\n");
4964 printf_filtered ("%04x:", udot_off
);
4966 udot_val
= call_ttrace (TT_LWP_RUREGS
,
4967 PIDGET (inferior_ptid
),
4968 (TTRACE_ARG_TYPE
) udot_off
,
4973 sprintf (mess
, "\nreading user struct at offset 0x%x", udot_off
);
4974 perror_with_name (mess
);
4976 /* Avoid using nonportable (?) "*" in print specs */
4977 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
4979 printf_filtered ("\n");
4983 #endif /* !defined (CHILD_XFER_MEMORY). */
4986 /* TTrace version of "target_pid_to_exec_file"
4989 child_pid_to_exec_file (int tid
)
4992 static char exec_file_buffer
[1024];
4994 static struct pst_status buf
;
4996 /* On various versions of hpux11, this may fail due to a supposed
4997 kernel bug. We have alternate methods to get this information
4999 tt_status
= call_ttrace (TT_PROC_GET_PATHNAME
,
5001 (uint64_t) exec_file_buffer
,
5002 sizeof (exec_file_buffer
) - 1,
5005 return exec_file_buffer
;
5007 /* Try to get process information via pstat and extract the filename
5008 from the pst_cmd field within the pst_status structure. */
5009 if (pstat_getproc (&buf
, sizeof (struct pst_status
), 0, tid
) != -1)
5011 char *p
= buf
.pst_cmd
;
5013 while (*p
&& *p
!= ' ')
5017 return (buf
.pst_cmd
);
5024 pre_fork_inferior (void)
5028 status
= pipe (startup_semaphore
.parent_channel
);
5031 warning ("error getting parent pipe for startup semaphore");
5035 status
= pipe (startup_semaphore
.child_channel
);
5038 warning ("error getting child pipe for startup semaphore");
5043 /* Called from child_follow_fork in hppah-nat.c.
5045 * This seems to be intended to attach after a fork or
5046 * vfork, while "attach" is used to attach to a pid
5047 * given by the user. The check for an existing attach
5048 * seems odd--it always fails in our test system.
5051 hppa_require_attach (int pid
)
5056 unsigned int regs_offset
;
5057 process_state_t old_process_state
= process_state
;
5059 /* Are we already attached? There appears to be no explicit
5060 * way to answer this via ttrace, so we try something which
5061 * should be innocuous if we are attached. If that fails,
5062 * then we assume we're not attached, and so attempt to make
5066 tt_status
= call_real_ttrace (TT_PROC_STOP
,
5069 (TTRACE_ARG_TYPE
) TT_NIL
,
5070 (TTRACE_ARG_TYPE
) TT_NIL
,
5075 /* No change to process-state!
5082 /* If successful, the process is now stopped. But if
5083 * we're VFORKING, the parent is still running, so don't
5084 * change the process state.
5086 if (process_state
!= VFORKING
)
5087 process_state
= STOPPED
;
5089 /* If we were already attached, you'd think that we
5090 * would need to start going again--but you'd be wrong,
5091 * as the fork-following code is actually in the middle
5092 * of the "resume" routine in in "infrun.c" and so
5093 * will (almost) immediately do a resume.
5095 * On the other hand, if we are VFORKING, which means
5096 * that the child and the parent share a process for a
5097 * while, we know that "resume" won't be resuming
5098 * until the child EXEC event is seen. But we still
5099 * don't want to continue, as the event is already
5102 update_thread_state_after_attach (pid
, DONT_ATTACH_CONTINUE
);
5103 } /* STOP succeeded */
5109 hppa_require_detach (int pid
, int signal
)
5113 /* If signal is non-zero, we must pass the signal on to the active
5114 thread prior to detaching. We do this by continuing the threads
5120 threads_continue_all_with_signals (pid
, signal
);
5124 tt_status
= call_ttrace (TT_PROC_DETACH
,
5130 errno
= 0; /* Ignore any errors. */
5132 /* process_state? */
5137 /* Given the starting address of a memory page, hash it to a bucket in
5138 the memory page dictionary.
5141 get_dictionary_bucket_of_page (CORE_ADDR page_start
)
5145 hash
= (page_start
/ memory_page_dictionary
.page_size
);
5146 hash
= hash
% MEMORY_PAGE_DICTIONARY_BUCKET_COUNT
;
5152 /* Given a memory page's starting address, get (i.e., find an existing
5153 or create a new) dictionary entry for the page. The page will be
5154 write-protected when this function returns, but may have a reference
5155 count of 0 (if the page was newly-added to the dictionary).
5157 static memory_page_t
*
5158 get_dictionary_entry_of_page (int pid
, CORE_ADDR page_start
)
5161 memory_page_t
*page
= NULL
;
5162 memory_page_t
*previous_page
= NULL
;
5164 /* We're going to be using the dictionary now, than-kew. */
5165 require_memory_page_dictionary ();
5167 /* Try to find an existing dictionary entry for this page. Hash
5168 on the page's starting address.
5170 bucket
= get_dictionary_bucket_of_page (page_start
);
5171 page
= &memory_page_dictionary
.buckets
[bucket
];
5172 while (page
!= NULL
)
5174 if (page
->page_start
== page_start
)
5176 previous_page
= page
;
5180 /* Did we find a dictionary entry for this page? If not, then
5181 add it to the dictionary now.
5185 /* Create a new entry. */
5186 page
= (memory_page_t
*) xmalloc (sizeof (memory_page_t
));
5187 page
->page_start
= page_start
;
5188 page
->reference_count
= 0;
5190 page
->previous
= NULL
;
5192 /* We'll write-protect the page now, if that's allowed. */
5193 page
->original_permissions
= write_protect_page (pid
, page_start
);
5195 /* Add the new entry to the dictionary. */
5196 page
->previous
= previous_page
;
5197 previous_page
->next
= page
;
5199 memory_page_dictionary
.page_count
++;
5207 remove_dictionary_entry_of_page (int pid
, memory_page_t
*page
)
5209 /* Restore the page's original permissions. */
5210 unwrite_protect_page (pid
, page
->page_start
, page
->original_permissions
);
5212 /* Kick the page out of the dictionary. */
5213 if (page
->previous
!= NULL
)
5214 page
->previous
->next
= page
->next
;
5215 if (page
->next
!= NULL
)
5216 page
->next
->previous
= page
->previous
;
5218 /* Just in case someone retains a handle to this after it's freed. */
5219 page
->page_start
= (CORE_ADDR
) 0;
5221 memory_page_dictionary
.page_count
--;
5228 hppa_enable_syscall_events (int pid
)
5231 ttevent_t ttrace_events
;
5233 /* Get the set of events that are currently enabled. */
5234 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
5236 (TTRACE_ARG_TYPE
) & ttrace_events
,
5237 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5240 perror_with_name ("ttrace");
5242 /* Add syscall events to that set. */
5243 ttrace_events
.tte_events
|= TTEVT_SYSCALL_ENTRY
;
5244 ttrace_events
.tte_events
|= TTEVT_SYSCALL_RETURN
;
5246 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
5248 (TTRACE_ARG_TYPE
) & ttrace_events
,
5249 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5252 perror_with_name ("ttrace");
5257 hppa_disable_syscall_events (int pid
)
5260 ttevent_t ttrace_events
;
5262 /* Get the set of events that are currently enabled. */
5263 tt_status
= call_ttrace (TT_PROC_GET_EVENT_MASK
,
5265 (TTRACE_ARG_TYPE
) & ttrace_events
,
5266 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5269 perror_with_name ("ttrace");
5271 /* Remove syscall events from that set. */
5272 ttrace_events
.tte_events
&= ~TTEVT_SYSCALL_ENTRY
;
5273 ttrace_events
.tte_events
&= ~TTEVT_SYSCALL_RETURN
;
5275 tt_status
= call_ttrace (TT_PROC_SET_EVENT_MASK
,
5277 (TTRACE_ARG_TYPE
) & ttrace_events
,
5278 (TTRACE_ARG_TYPE
) sizeof (ttrace_events
),
5281 perror_with_name ("ttrace");
5285 /* The address range beginning with START and ending with START+LEN-1
5286 (inclusive) is to be watched via page-protection by a new watchpoint.
5287 Set protection for all pages that overlap that range.
5289 Note that our caller sets TYPE to:
5290 0 for a bp_hardware_watchpoint,
5291 1 for a bp_read_watchpoint,
5292 2 for a bp_access_watchpoint
5294 (Yes, this is intentionally (though lord only knows why) different
5295 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5298 hppa_insert_hw_watchpoint (int pid
, CORE_ADDR start
, LONGEST len
, int type
)
5300 CORE_ADDR page_start
;
5301 int dictionary_was_empty
;
5304 LONGEST range_size_in_pages
;
5307 error ("read or access hardware watchpoints not supported on HP-UX");
5309 /* Examine all pages in the address range. */
5310 require_memory_page_dictionary ();
5312 dictionary_was_empty
= (memory_page_dictionary
.page_count
== (LONGEST
) 0);
5314 page_size
= memory_page_dictionary
.page_size
;
5315 page_start
= (start
/ page_size
) * page_size
;
5316 range_size_in_pages
= ((LONGEST
) len
+ (LONGEST
) page_size
- 1) / (LONGEST
) page_size
;
5318 for (page_id
= 0; page_id
< range_size_in_pages
; page_id
++, page_start
+= page_size
)
5320 memory_page_t
*page
;
5322 /* This gets the page entered into the dictionary if it was
5323 not already entered.
5325 page
= get_dictionary_entry_of_page (pid
, page_start
);
5326 page
->reference_count
++;
5329 /* Our implementation depends on seeing calls to kernel code, for the
5330 following reason. Here we ask to be notified of syscalls.
5332 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5335 But when kernel code accesses the page, it doesn't give a SIGBUS.
5336 Rather, the system call that touched the page fails, with errno=EFAULT.
5339 We could accomodate this "feature" by asking to be notified of syscall
5340 entries & exits; upon getting an entry event, disabling page-protections;
5341 upon getting an exit event, reenabling page-protections and then checking
5342 if any watchpoints triggered.
5344 However, this turns out to be a real performance loser. syscalls are
5345 usually a frequent occurrence. Having to unprotect-reprotect all watched
5346 pages, and also to then read all watched memory locations and compare for
5347 triggers, can be quite expensive.
5349 Instead, we'll only ask to be notified of syscall exits. When we get
5350 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5351 we can just continue the inferior.
5353 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5354 hackish stuff to determine whether the failure really was due to a
5355 page-protect trap on a watched location.
5357 if (dictionary_was_empty
)
5358 hppa_enable_syscall_events (pid
);
5364 /* The address range beginning with START and ending with START+LEN-1
5365 (inclusive) was being watched via page-protection by a watchpoint
5366 which has been removed. Remove protection for all pages that
5367 overlap that range, which are not also being watched by other
5371 hppa_remove_hw_watchpoint (int pid
, CORE_ADDR start
, LONGEST len
, int type
)
5373 CORE_ADDR page_start
;
5374 int dictionary_is_empty
;
5377 LONGEST range_size_in_pages
;
5380 error ("read or access hardware watchpoints not supported on HP-UX");
5382 /* Examine all pages in the address range. */
5383 require_memory_page_dictionary ();
5385 page_size
= memory_page_dictionary
.page_size
;
5386 page_start
= (start
/ page_size
) * page_size
;
5387 range_size_in_pages
= ((LONGEST
) len
+ (LONGEST
) page_size
- 1) / (LONGEST
) page_size
;
5389 for (page_id
= 0; page_id
< range_size_in_pages
; page_id
++, page_start
+= page_size
)
5391 memory_page_t
*page
;
5393 page
= get_dictionary_entry_of_page (pid
, page_start
);
5394 page
->reference_count
--;
5396 /* Was this the last reference of this page? If so, then we
5397 must scrub the entry from the dictionary, and also restore
5398 the page's original permissions.
5400 if (page
->reference_count
== 0)
5401 remove_dictionary_entry_of_page (pid
, page
);
5404 dictionary_is_empty
= (memory_page_dictionary
.page_count
== (LONGEST
) 0);
5406 /* If write protections are currently disallowed, then that implies that
5407 wait_for_inferior believes that the inferior is within a system call.
5408 Since we want to see both syscall entry and return, it's clearly not
5409 good to disable syscall events in this state!
5411 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5412 "inferior is between syscall events now". Oh well.
5414 if (dictionary_is_empty
&& memory_page_dictionary
.page_protections_allowed
)
5415 hppa_disable_syscall_events (pid
);
5421 /* Could we implement a watchpoint of this type via our available
5424 This query does not consider whether a particular address range
5425 could be so watched, but just whether support is generally available
5426 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5427 query that answers whether a particular range should be watched via
5431 hppa_can_use_hw_watchpoint (int type
, int cnt
, int ot
)
5433 return (type
== bp_hardware_watchpoint
);
5437 /* Assuming we could set a hardware watchpoint on this address, do
5438 we think it would be profitable ("a good idea") to do so? If not,
5439 we can always set a regular (aka single-step & test) watchpoint
5443 hppa_range_profitable_for_hw_watchpoint (int pid
, CORE_ADDR start
, LONGEST len
)
5445 int range_is_stack_based
;
5446 int range_is_accessible
;
5447 CORE_ADDR page_start
;
5450 LONGEST range_size_in_pages
;
5452 /* ??rehrauer: For now, say that all addresses are potentially
5453 profitable. Possibly later we'll want to test the address
5456 range_is_stack_based
= 0;
5458 /* If any page in the range is inaccessible, then we cannot
5459 really use hardware watchpointing, even though our client
5460 thinks we can. In that case, it's actually an error to
5461 attempt to use hw watchpoints, so we'll tell our client
5462 that the range is "unprofitable", and hope that they listen...
5464 range_is_accessible
= 1; /* Until proven otherwise. */
5466 /* Examine all pages in the address range. */
5468 page_size
= sysconf (_SC_PAGE_SIZE
);
5470 /* If we can't determine page size, we're hosed. Tell our
5471 client it's unprofitable to use hw watchpoints for this
5474 if (errno
|| (page_size
<= 0))
5480 page_start
= (start
/ page_size
) * page_size
;
5481 range_size_in_pages
= len
/ (LONGEST
) page_size
;
5483 for (page
= 0; page
< range_size_in_pages
; page
++, page_start
+= page_size
)
5486 int page_permissions
;
5488 /* Is this page accessible? */
5490 tt_status
= call_ttrace (TT_PROC_GET_MPROTECT
,
5492 (TTRACE_ARG_TYPE
) page_start
,
5494 (TTRACE_ARG_TYPE
) & page_permissions
);
5495 if (errno
|| (tt_status
< 0))
5498 range_is_accessible
= 0;
5502 /* Yes, go for another... */
5505 return (!range_is_stack_based
&& range_is_accessible
);
5510 hppa_pid_or_tid_to_str (ptid_t ptid
)
5512 static char buf
[100]; /* Static because address returned. */
5513 pid_t id
= PIDGET (ptid
);
5515 /* Does this appear to be a process? If so, print it that way. */
5516 if (is_process_id (id
))
5517 return child_pid_to_str (ptid
);
5519 /* Else, print both the GDB thread number and the system thread id. */
5520 sprintf (buf
, "thread %d (", pid_to_thread_id (ptid
));
5521 strcat (buf
, hppa_tid_to_str (ptid
));
5522 strcat (buf
, ")\0");
5529 hppa_ensure_vforking_parent_remains_stopped (int pid
)
5531 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5538 hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
5540 return 0; /* No, the parent vfork is available now. */
5544 /* Write a register as a 64bit value. This may be necessary if the
5545 native OS is too braindamaged to allow some (or all) registers to
5546 be written in 32bit hunks such as hpux11 and the PC queue registers.
5548 This is horribly gross and disgusting. */
5551 ttrace_write_reg_64 (int gdb_tid
, CORE_ADDR dest_addr
, CORE_ADDR src_addr
)
5557 tid
= map_from_gdb_tid (gdb_tid
);
5558 pid
= get_pid_for (tid
);
5561 tt_status
= ttrace (TT_LWP_WUREGS
,
5564 (TTRACE_ARG_TYPE
) dest_addr
,
5566 (TTRACE_ARG_TYPE
) src_addr
);
5571 /* Don't bother for a known benign error: if you ask for the
5572 first thread state, but there is only one thread and it's
5573 not stopped, ttrace complains.
5575 We have this inside the #ifdef because our caller will do
5576 this check for real. */
5577 if( request
!= TT_PROC_GET_FIRST_LWP_STATE
5578 || errno
!= EPROTO
)
5581 printf( "TT fail for %s, with pid %d, tid %d, status %d \n",
5582 get_printable_name_of_ttrace_request (TT_LWP_WUREGS
),
5583 pid
, tid
, tt_status
);
5592 _initialize_infttrace (void)
5594 /* Initialize the ttrace-based hardware watchpoint implementation. */
5595 memory_page_dictionary
.page_count
= (LONGEST
) - 1;
5596 memory_page_dictionary
.page_protections_allowed
= 1;
5599 memory_page_dictionary
.page_size
= sysconf (_SC_PAGE_SIZE
);
5601 /* We do a lot of casts from pointers to TTRACE_ARG_TYPE; make sure
5603 if (sizeof (TTRACE_ARG_TYPE
) < sizeof (void *))
5604 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
5606 if (errno
|| (memory_page_dictionary
.page_size
<= 0))
5607 perror_with_name ("sysconf");