]> Git Repo - binutils.git/blob - gdb/infttrace.c
2001-04-30 Elena Zannoni <[email protected]>
[binutils.git] / gdb / infttrace.c
1 /* Low level Unix child interface to ttrace, for GDB when running under HP-UX.
2    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
3    1999, 2000, 2001
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
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.
12
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.
17
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.  */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "target.h"
27 #include "gdb_string.h"
28 #include "gdb_wait.h"
29 #include "command.h"
30
31 /* Some hackery to work around a use of the #define name NO_FLAGS
32  * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
33  */
34 #ifdef  NO_FLAGS
35 #define INFTTRACE_TEMP_HACK NO_FLAGS
36 #undef  NO_FLAGS
37 #endif
38
39 #ifdef USG
40 #include <sys/types.h>
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/dir.h>
45 #include <signal.h>
46 #include <sys/ioctl.h>
47
48 #include <sys/ttrace.h>
49 #include <sys/mman.h>
50
51 #ifndef NO_PTRACE_H
52 #ifdef PTRACE_IN_WRONG_PLACE
53 #include <ptrace.h>
54 #else
55 #include <sys/ptrace.h>
56 #endif
57 #endif /* NO_PTRACE_H */
58
59 /* Second half of the hackery above.  Non-ANSI C, so
60  * we can't use "#error", alas.
61  */
62 #ifdef NO_FLAGS
63 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
64   /* #error "Hackery to remove warning didn't work right" */
65 #else
66   /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
67 #endif
68 #else
69   /* #error "Didn't get expected re-definition of NO_FLAGS" */
70 #define NO_FLAGS INFTTRACE_TEMP_HACK
71 #endif
72
73 #if !defined (PT_SETTRC)
74 #define PT_SETTRC       0       /* Make process traceable by parent */
75 #endif
76 #if !defined (PT_READ_I)
77 #define PT_READ_I       1       /* Read word from text space */
78 #endif
79 #if !defined (PT_READ_D)
80 #define PT_READ_D       2       /* Read word from data space */
81 #endif
82 #if !defined (PT_READ_U)
83 #define PT_READ_U       3       /* Read word from kernel user struct */
84 #endif
85 #if !defined (PT_WRITE_I)
86 #define PT_WRITE_I      4       /* Write word to text space */
87 #endif
88 #if !defined (PT_WRITE_D)
89 #define PT_WRITE_D      5       /* Write word to data space */
90 #endif
91 #if !defined (PT_WRITE_U)
92 #define PT_WRITE_U      6       /* Write word to kernel user struct */
93 #endif
94 #if !defined (PT_CONTINUE)
95 #define PT_CONTINUE     7       /* Continue after signal */
96 #endif
97 #if !defined (PT_STEP)
98 #define PT_STEP         9       /* Set flag for single stepping */
99 #endif
100 #if !defined (PT_KILL)
101 #define PT_KILL         8       /* Send child a SIGKILL signal */
102 #endif
103
104 #ifndef PT_ATTACH
105 #define PT_ATTACH PTRACE_ATTACH
106 #endif
107 #ifndef PT_DETACH
108 #define PT_DETACH PTRACE_DETACH
109 #endif
110
111 #include "gdbcore.h"
112 #ifndef NO_SYS_FILE
113 #include <sys/file.h>
114 #endif
115
116 /* This semaphore is used to coordinate the child and parent processes
117    after a fork(), and before an exec() by the child.  See parent_attach_all
118    for details.
119  */
120 typedef struct
121   {
122     int parent_channel[2];      /* Parent "talks" to [1], child "listens" to [0] */
123     int child_channel[2];       /* Child "talks" to [1], parent "listens" to [0] */
124   }
125 startup_semaphore_t;
126
127 #define SEM_TALK (1)
128 #define SEM_LISTEN (0)
129
130 static startup_semaphore_t startup_semaphore;
131
132 /* See can_touch_threads_of_process for details. */
133 static int vforking_child_pid = 0;
134 static int vfork_in_flight = 0;
135
136 /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed).
137  */
138 static pid_t old_gdb_pid = 0;
139 static pid_t reported_pid = 0;
140 static int reported_bpt = 0;
141
142 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
143  */
144 #define TT_OK( _status, _errno ) \
145     (((_status) == 1) && ((_errno) == 0))
146
147 #define TTRACE_ARG_TYPE uint64_t
148
149 /* When supplied as the "addr" operand, ttrace interprets this
150    to mean, "from the current address".
151  */
152 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
153
154 /* When supplied as the "addr", "data" or "addr2" operand for most
155    requests, ttrace interprets this to mean, "pay no heed to this
156    argument".
157  */
158 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
159
160 /* This is capable of holding the value of a 32-bit register.  The
161    value is always left-aligned in the buffer; i.e., [0] contains
162    the most-significant byte of the register's value, and [sizeof(reg)]
163    contains the least-significant value.
164
165    ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
166    that registers are 32-bits on HP-UX.  The latter assumption changes
167    with PA2.0.
168  */
169 typedef int register_value_t;
170
171 /********************************************************************
172
173                  How this works:
174
175    1.  Thread numbers
176
177    The rest of GDB sees threads as being things with different
178    "pid" (process id) values.  See "thread.c" for details.  The
179    separate threads will be seen and reacted to if infttrace passes
180    back different pid values (for _events_).  See wait_for_inferior
181    in inftarg.c.
182
183    So infttrace is going to use thread ids externally, pretending
184    they are process ids, and keep track internally so that it can
185    use the real process id (and thread id) when calling ttrace.
186
187    The data structure that supports this is a linked list of the
188    current threads.  Since at some date infttrace will have to
189    deal with multiple processes, each list element records its
190    corresponding pid, rather than having a single global.
191
192    Note that the list is only approximately current; that's ok, as
193    it's up to date when we need it (we hope!).  Also, it can contain
194    dead threads, as there's no harm if it does.
195
196    The approach taken here is to bury the translation from external
197    to internal inside "call_ttrace" and a few other places.
198
199    There are some wrinkles:
200
201    o  When GDB forks itself to create the debug target process,
202       there's only a pid of 0 around in the child, so the
203       TT_PROC_SETTRC operation uses a more direct call to ttrace;
204       Similiarly, the initial setting of the event mask happens
205       early as  well, and so is also special-cased, and an attach
206       uses a real pid;
207
208    o  We define an unthreaded application as having a "pseudo"
209       thread;
210
211    o  To keep from confusing the rest of GDB, we don't switch
212       the PID for the pseudo thread to a TID.  A table will help:
213
214       Rest of GDB sees these PIDs:     pid   tid1  tid2  tid3 ...
215                                         
216       Our thread list stores:          pid   pid   pid   pid  ...
217                                        tid0  tid1  tid2  tid3
218       
219       Ttrace sees these TIDS:          tid0  tid1  tid2  tid3 ...
220
221       Both pid and tid0 will map to tid0, as there are infttrace.c-internal
222       calls to ttrace using tid0.
223
224    2. Step and Continue
225
226    Since we're implementing the "stop the world" model, sub-model
227    "other threads run during step", we have some stuff to do:
228
229    o  User steps require continuing all threads other than the
230       one the user is stepping;
231
232    o  Internal debugger steps (such as over a breakpoint or watchpoint,
233       but not out of a library load thunk) require stepping only
234       the selected thread; this means that we have to report the
235       step finish on that thread, which can lead to complications;
236
237    o  When a thread is created, it is created running, rather
238       than stopped--so we have to stop it.
239
240    The OS doesn't guarantee the stopped thread list will be stable,
241    no does it guarantee where on the stopped thread list a thread
242    that is single-stepped will wind up: it's possible that it will
243    be off the list for a while, it's possible the step will complete
244    and it will be re-posted to the end...
245
246    This means we have to scan the stopped thread list, build up
247    a work-list, and then run down the work list; we can't do the
248    step/continue during the scan.
249
250    3. Buffering events
251
252    Then there's the issue of waiting for an event.  We do this by
253    noticing how many events are reported at the end of each wait.
254    From then on, we "fake" all resumes and steps, returning instantly,
255    and don't do another wait.  Once all pending events are reported,
256    we can really resume again.
257
258    To keep this hidden, all the routines which know about tids and
259    pids or real events and simulated ones are static (file-local).
260
261    This code can make lots of calls to ttrace, in particular it
262    can spin down the list of thread states more than once.  If this
263    becomes a performance hit, the spin could be done once and the
264    various "tsp" blocks saved, keeping all later spins in this
265    process.
266
267    The O/S doesn't promise to keep the list straight, and so we must
268    re-scan a lot.  By observation, it looks like a single-step/wait
269    puts the stepped thread at the end of the list but doesn't change
270    it otherwise.
271
272 ****************************************************************
273 */
274
275 /* Uncomment these to turn on various debugging output */
276 /* #define THREAD_DEBUG */
277 /* #define WAIT_BUFFER_DEBUG */
278 /* #define PARANOIA */
279
280
281 #define INFTTRACE_ALL_THREADS (-1)
282 #define INFTTRACE_STEP        (1)
283 #define INFTTRACE_CONTINUE    (0)
284
285 /* FIX: this is used in inftarg.c/child_wait, in a hack.
286  */
287 extern int not_same_real_pid;
288
289 /* This is used to count buffered events.
290  */
291 static unsigned int more_events_left = 0;
292
293 /* Process state.
294  */
295 typedef enum process_state_enum
296   {
297     STOPPED,
298     FAKE_STEPPING,
299     FAKE_CONTINUE,              /* For later use */
300     RUNNING,
301     FORKING,
302     VFORKING
303   }
304 process_state_t;
305
306 static process_state_t process_state = STOPPED;
307
308 /* User-specified stepping modality.
309  */
310 typedef enum stepping_mode_enum
311   {
312     DO_DEFAULT,                 /* ...which is a continue! */
313     DO_STEP,
314     DO_CONTINUE
315   }
316 stepping_mode_t;
317
318 /* Action to take on an attach, depends on
319  * what kind (user command, fork, vfork).
320  *
321  * At the moment, this is either:
322  *
323  * o  continue with a SIGTRAP signal, or
324  *
325  * o  leave stopped.
326  */
327 typedef enum attach_continue_enum
328   {
329     DO_ATTACH_CONTINUE,
330     DONT_ATTACH_CONTINUE
331   }
332 attach_continue_t;
333
334 /* This flag is true if we are doing a step-over-bpt
335  * with buffered events.  We will have to be sure to
336  * report the right thread, as otherwise the spaghetti
337  * code in "infrun.c/wait_for_inferior" will get
338  * confused.
339  */
340 static int doing_fake_step = 0;
341 static lwpid_t fake_step_tid = 0;
342 \f
343
344 /****************************************************
345  * Thread information structure routines and types. *
346  ****************************************************
347  */
348 typedef
349 struct thread_info_struct
350   {
351     int am_pseudo;              /* This is a pseudo-thread for the process. */
352     int pid;                    /* Process ID */
353     lwpid_t tid;                /* Thread  ID */
354     int handled;                /* 1 if a buffered event was handled. */
355     int seen;                   /* 1 if this thread was seen on a traverse. */
356     int terminated;             /* 1 if thread has terminated. */
357     int have_signal;            /* 1 if signal to be sent */
358     enum target_signal signal_value;    /* Signal to send */
359     int have_start;             /* 1 if alternate starting address */
360     stepping_mode_t stepping_mode;      /* Whether to step or continue */
361     CORE_ADDR start;            /* Where to start */
362     int have_state;             /* 1 if the event state has been set */
363     ttstate_t last_stop_state;  /* The most recently-waited event for this thread. */
364     struct thread_info_struct
365      *next;                     /* All threads are linked via this field. */
366     struct thread_info_struct
367      *next_pseudo;              /* All pseudo-threads are linked via this field. */
368   }
369 thread_info;
370
371 typedef
372 struct thread_info_header_struct
373   {
374     int count;
375     thread_info *head;
376     thread_info *head_pseudo;
377
378   }
379 thread_info_header;
380
381 static thread_info_header thread_head =
382 {0, NULL, NULL};
383 static thread_info_header deleted_threads =
384 {0, NULL, NULL};
385
386 static saved_real_pid = 0;
387 \f
388
389 /*************************************************
390  *          Debugging support functions          *
391  *************************************************
392  */
393 CORE_ADDR
394 get_raw_pc (lwpid_t ttid)
395 {
396   unsigned long pc_val;
397   int offset;
398   int res;
399
400   offset = register_addr (PC_REGNUM, U_REGS_OFFSET);
401   res = read_from_register_save_state (
402                                         ttid,
403                                         (TTRACE_ARG_TYPE) offset,
404                                         (char *) &pc_val,
405                                         sizeof (pc_val));
406   if (res <= 0)
407     {
408       return (CORE_ADDR) pc_val;
409     }
410   else
411     {
412       return (CORE_ADDR) 0;
413     }
414 }
415
416 static char *
417 get_printable_name_of_stepping_mode (stepping_mode_t mode)
418 {
419   switch (mode)
420     {
421     case DO_DEFAULT:
422       return "DO_DEFAULT";
423     case DO_STEP:
424       return "DO_STEP";
425     case DO_CONTINUE:
426       return "DO_CONTINUE";
427     default:
428       return "?unknown mode?";
429     }
430 }
431
432 /* This function returns a pointer to a string describing the
433  * ttrace event being reported.
434  */
435 char *
436 get_printable_name_of_ttrace_event (ttevents_t event)
437 {
438   /* This enumeration is "gappy", so don't use a table. */
439   switch (event)
440     {
441
442     case TTEVT_NONE:
443       return "TTEVT_NONE";
444     case TTEVT_SIGNAL:
445       return "TTEVT_SIGNAL";
446     case TTEVT_FORK:
447       return "TTEVT_FORK";
448     case TTEVT_EXEC:
449       return "TTEVT_EXEC";
450     case TTEVT_EXIT:
451       return "TTEVT_EXIT";
452     case TTEVT_VFORK:
453       return "TTEVT_VFORK";
454     case TTEVT_SYSCALL_RETURN:
455       return "TTEVT_SYSCALL_RETURN";
456     case TTEVT_LWP_CREATE:
457       return "TTEVT_LWP_CREATE";
458     case TTEVT_LWP_TERMINATE:
459       return "TTEVT_LWP_TERMINATE";
460     case TTEVT_LWP_EXIT:
461       return "TTEVT_LWP_EXIT";
462     case TTEVT_LWP_ABORT_SYSCALL:
463       return "TTEVT_LWP_ABORT_SYSCALL";
464     case TTEVT_SYSCALL_ENTRY:
465       return "TTEVT_SYSCALL_ENTRY";
466     case TTEVT_SYSCALL_RESTART:
467       return "TTEVT_SYSCALL_RESTART";
468     default:
469       return "?new event?";
470     }
471 }
472 \f
473
474 /* This function translates the ttrace request enumeration into
475  * a character string that is its printable (aka "human readable")
476  * name.
477  */
478 char *
479 get_printable_name_of_ttrace_request (ttreq_t request)
480 {
481   if (!IS_TTRACE_REQ (request))
482     return "?bad req?";
483
484   /* This enumeration is "gappy", so don't use a table. */
485   switch (request)
486     {
487     case TT_PROC_SETTRC:
488       return "TT_PROC_SETTRC";
489     case TT_PROC_ATTACH:
490       return "TT_PROC_ATTACH";
491     case TT_PROC_DETACH:
492       return "TT_PROC_DETACH";
493     case TT_PROC_RDTEXT:
494       return "TT_PROC_RDTEXT";
495     case TT_PROC_WRTEXT:
496       return "TT_PROC_WRTEXT";
497     case TT_PROC_RDDATA:
498       return "TT_PROC_RDDATA";
499     case TT_PROC_WRDATA:
500       return "TT_PROC_WRDATA";
501     case TT_PROC_STOP:
502       return "TT_PROC_STOP";
503     case TT_PROC_CONTINUE:
504       return "TT_PROC_CONTINUE";
505     case TT_PROC_GET_PATHNAME:
506       return "TT_PROC_GET_PATHNAME";
507     case TT_PROC_GET_EVENT_MASK:
508       return "TT_PROC_GET_EVENT_MASK";
509     case TT_PROC_SET_EVENT_MASK:
510       return "TT_PROC_SET_EVENT_MASK";
511     case TT_PROC_GET_FIRST_LWP_STATE:
512       return "TT_PROC_GET_FIRST_LWP_STATE";
513     case TT_PROC_GET_NEXT_LWP_STATE:
514       return "TT_PROC_GET_NEXT_LWP_STATE";
515     case TT_PROC_EXIT:
516       return "TT_PROC_EXIT";
517     case TT_PROC_GET_MPROTECT:
518       return "TT_PROC_GET_MPROTECT";
519     case TT_PROC_SET_MPROTECT:
520       return "TT_PROC_SET_MPROTECT";
521     case TT_PROC_SET_SCBM:
522       return "TT_PROC_SET_SCBM";
523     case TT_LWP_STOP:
524       return "TT_LWP_STOP";
525     case TT_LWP_CONTINUE:
526       return "TT_LWP_CONTINUE";
527     case TT_LWP_SINGLE:
528       return "TT_LWP_SINGLE";
529     case TT_LWP_RUREGS:
530       return "TT_LWP_RUREGS";
531     case TT_LWP_WUREGS:
532       return "TT_LWP_WUREGS";
533     case TT_LWP_GET_EVENT_MASK:
534       return "TT_LWP_GET_EVENT_MASK";
535     case TT_LWP_SET_EVENT_MASK:
536       return "TT_LWP_SET_EVENT_MASK";
537     case TT_LWP_GET_STATE:
538       return "TT_LWP_GET_STATE";
539     default:
540       return "?new req?";
541     }
542 }
543 \f
544
545 /* This function translates the process state enumeration into
546  * a character string that is its printable (aka "human readable")
547  * name.
548  */
549 static char *
550 get_printable_name_of_process_state (process_state_t process_state)
551 {
552   switch (process_state)
553     {
554     case STOPPED:
555       return "STOPPED";
556     case FAKE_STEPPING:
557       return "FAKE_STEPPING";
558     case RUNNING:
559       return "RUNNING";
560     case FORKING:
561       return "FORKING";
562     case VFORKING:
563       return "VFORKING";
564     default:
565       return "?some unknown state?";
566     }
567 }
568
569 /* Set a ttrace thread state to a safe, initial state.
570  */
571 static void
572 clear_ttstate_t (ttstate_t *tts)
573 {
574   tts->tts_pid = 0;
575   tts->tts_lwpid = 0;
576   tts->tts_user_tid = 0;
577   tts->tts_event = TTEVT_NONE;
578 }
579
580 /* Copy ttrace thread state TTS_FROM into TTS_TO.
581  */
582 static void
583 copy_ttstate_t (ttstate_t *tts_to, ttstate_t *tts_from)
584 {
585   memcpy ((char *) tts_to, (char *) tts_from, sizeof (*tts_to));
586 }
587
588 /* Are there any live threads we know about?
589  */
590 static int
591 any_thread_records (void)
592 {
593   return (thread_head.count > 0);
594 }
595
596 /* Create, fill in and link in a thread descriptor.
597  */
598 static thread_info *
599 create_thread_info (int pid, lwpid_t tid)
600 {
601   thread_info *new_p;
602   thread_info *p;
603   int thread_count_of_pid;
604
605   new_p = xmalloc (sizeof (thread_info));
606   new_p->pid = pid;
607   new_p->tid = tid;
608   new_p->have_signal = 0;
609   new_p->have_start = 0;
610   new_p->have_state = 0;
611   clear_ttstate_t (&new_p->last_stop_state);
612   new_p->am_pseudo = 0;
613   new_p->handled = 0;
614   new_p->seen = 0;
615   new_p->terminated = 0;
616   new_p->next = NULL;
617   new_p->next_pseudo = NULL;
618   new_p->stepping_mode = DO_DEFAULT;
619
620   if (0 == thread_head.count)
621     {
622 #ifdef THREAD_DEBUG
623       if (debug_on)
624         printf ("First thread, pid %d tid %d!\n", pid, tid);
625 #endif
626       saved_real_pid = inferior_pid;
627     }
628   else
629     {
630 #ifdef THREAD_DEBUG
631       if (debug_on)
632         printf ("Subsequent thread, pid %d tid %d\n", pid, tid);
633 #endif
634     }
635
636   /* Another day, another thread...
637    */
638   thread_head.count++;
639
640   /* The new thread always goes at the head of the list.
641    */
642   new_p->next = thread_head.head;
643   thread_head.head = new_p;
644
645   /* Is this the "pseudo" thread of a process?  It is if there's
646    * no other thread for this process on the list.  (Note that this
647    * accomodates multiple processes, such as we see even for simple
648    * cases like forking "non-threaded" programs.)
649    */
650   p = thread_head.head;
651   thread_count_of_pid = 0;
652   while (p)
653     {
654       if (p->pid == new_p->pid)
655         thread_count_of_pid++;
656       p = p->next;
657     }
658
659   /* Did we see any other threads for this pid?  (Recall that we just
660    * added this thread to the list...)
661    */
662   if (thread_count_of_pid == 1)
663     {
664       new_p->am_pseudo = 1;
665       new_p->next_pseudo = thread_head.head_pseudo;
666       thread_head.head_pseudo = new_p;
667     }
668
669   return new_p;
670 }
671
672 /* Get rid of our thread info.
673  */
674 static void
675 clear_thread_info (void)
676 {
677   thread_info *p;
678   thread_info *q;
679
680 #ifdef THREAD_DEBUG
681   if (debug_on)
682     printf ("Clearing all thread info\n");
683 #endif
684
685   p = thread_head.head;
686   while (p)
687     {
688       q = p;
689       p = p->next;
690       xfree (q);
691     }
692
693   thread_head.head = NULL;
694   thread_head.head_pseudo = NULL;
695   thread_head.count = 0;
696
697   p = deleted_threads.head;
698   while (p)
699     {
700       q = p;
701       p = p->next;
702       xfree (q);
703     }
704
705   deleted_threads.head = NULL;
706   deleted_threads.head_pseudo = NULL;
707   deleted_threads.count = 0;
708
709   /* No threads, so can't have pending events.
710    */
711   more_events_left = 0;
712 }
713
714 /* Given a tid, find the thread block for it.
715  */
716 static thread_info *
717 find_thread_info (lwpid_t tid)
718 {
719   thread_info *p;
720
721   for (p = thread_head.head; p; p = p->next)
722     {
723       if (p->tid == tid)
724         {
725           return p;
726         }
727     }
728
729   for (p = deleted_threads.head; p; p = p->next)
730     {
731       if (p->tid == tid)
732         {
733           return p;
734         }
735     }
736
737   return NULL;
738 }
739
740 /* For any but the pseudo thread, this maps to the
741  * thread ID.  For the pseudo thread, if you pass either
742  * the thread id or the PID, you get the pseudo thread ID.
743  *
744  * We have to be prepared for core gdb to ask about
745  * deleted threads.  We do the map, but we don't like it.
746  */
747 static lwpid_t
748 map_from_gdb_tid (lwpid_t gdb_tid)
749 {
750   thread_info *p;
751
752   /* First assume gdb_tid really is a tid, and try to find a
753    * matching entry on the threads list.
754    */
755   for (p = thread_head.head; p; p = p->next)
756     {
757       if (p->tid == gdb_tid)
758         return gdb_tid;
759     }
760
761   /* It doesn't appear to be a tid; perhaps it's really a pid?
762    * Try to find a "pseudo" thread entry on the threads list.
763    */
764   for (p = thread_head.head_pseudo; p != NULL; p = p->next_pseudo)
765     {
766       if (p->pid == gdb_tid)
767         return p->tid;
768     }
769
770   /* Perhaps it's the tid of a deleted thread we may still
771    * have some knowledge of?
772    */
773   for (p = deleted_threads.head; p; p = p->next)
774     {
775       if (p->tid == gdb_tid)
776         return gdb_tid;
777     }
778
779   /* Or perhaps it's the pid of a deleted process we may still
780    * have knowledge of?
781    */
782   for (p = deleted_threads.head_pseudo; p != NULL; p = p->next_pseudo)
783     {
784       if (p->pid == gdb_tid)
785         return p->tid;
786     }
787
788   return 0;                     /* Error? */
789 }
790
791 /* Map the other way: from a real tid to the
792  * "pid" known by core gdb.  This tid may be
793  * for a thread that just got deleted, so we
794  * also need to consider deleted threads.
795  */
796 static lwpid_t
797 map_to_gdb_tid (lwpid_t real_tid)
798 {
799   thread_info *p;
800
801   for (p = thread_head.head; p; p = p->next)
802     {
803       if (p->tid == real_tid)
804         {
805           if (p->am_pseudo)
806             return p->pid;
807           else
808             return real_tid;
809         }
810     }
811
812   for (p = deleted_threads.head; p; p = p->next)
813     {
814       if (p->tid == real_tid)
815         if (p->am_pseudo)
816           return p->pid;        /* Error? */
817         else
818           return real_tid;
819     }
820
821   return 0;                     /* Error?  Never heard of this thread! */
822 }
823
824 /* Do any threads have saved signals?
825  */
826 static int
827 saved_signals_exist (void)
828 {
829   thread_info *p;
830
831   for (p = thread_head.head; p; p = p->next)
832     {
833       if (p->have_signal)
834         {
835           return 1;
836         }
837     }
838
839   return 0;
840 }
841
842 /* Is this the tid for the zero-th thread?
843  */
844 static int
845 is_pseudo_thread (lwpid_t tid)
846 {
847   thread_info *p = find_thread_info (tid);
848   if (NULL == p || p->terminated)
849     return 0;
850   else
851     return p->am_pseudo;
852 }
853
854 /* Is this thread terminated?
855  */
856 static int
857 is_terminated (lwpid_t tid)
858 {
859   thread_info *p = find_thread_info (tid);
860
861   if (NULL != p)
862     return p->terminated;
863
864   return 0;
865 }
866
867 /* Is this pid a real PID or a TID?
868  */
869 static int
870 is_process_id (int pid)
871 {
872   lwpid_t tid;
873   thread_info *tinfo;
874   pid_t this_pid;
875   int this_pid_count;
876
877   /* What does PID really represent?
878    */
879   tid = map_from_gdb_tid (pid);
880   if (tid <= 0)
881     return 0;                   /* Actually, is probably an error... */
882
883   tinfo = find_thread_info (tid);
884
885   /* Does it appear to be a true thread?
886    */
887   if (!tinfo->am_pseudo)
888     return 0;
889
890   /* Else, it looks like it may be a process.  See if there's any other
891    * threads with the same process ID, though.  If there are, then TID
892    * just happens to be the first thread of several for this process.
893    */
894   this_pid = tinfo->pid;
895   this_pid_count = 0;
896   for (tinfo = thread_head.head; tinfo; tinfo = tinfo->next)
897     {
898       if (tinfo->pid == this_pid)
899         this_pid_count++;
900     }
901
902   return (this_pid_count == 1);
903 }
904
905
906 /* Add a thread to our info.  Prevent duplicate entries.
907  */
908 static thread_info *
909 add_tthread (int pid, lwpid_t tid)
910 {
911   thread_info *p;
912
913   p = find_thread_info (tid);
914   if (NULL == p)
915     p = create_thread_info (pid, tid);
916
917   return p;
918 }
919
920 /* Notice that a thread was deleted.
921  */
922 static void
923 del_tthread (lwpid_t tid)
924 {
925   thread_info *p;
926   thread_info *chase;
927
928   if (thread_head.count <= 0)
929     {
930       error ("Internal error in thread database.");
931       return;
932     }
933
934   chase = NULL;
935   for (p = thread_head.head; p; p = p->next)
936     {
937       if (p->tid == tid)
938         {
939
940 #ifdef THREAD_DEBUG
941           if (debug_on)
942             printf ("Delete here: %d \n", tid);
943 #endif
944
945           if (p->am_pseudo)
946             {
947               /*
948                * Deleting a main thread is ok if we're doing
949                * a parent-follow on a child; this is odd but
950                * not wrong.  It apparently _doesn't_ happen
951                * on the child-follow, as we don't just delete
952                * the pseudo while keeping the rest of the
953                * threads around--instead, we clear out the whole
954                * thread list at once.
955                */
956               thread_info *q;
957               thread_info *q_chase;
958
959               q_chase = NULL;
960               for (q = thread_head.head_pseudo; q; q = q->next)
961                 {
962                   if (q == p)
963                     {
964                       /* Remove from pseudo list.
965                        */
966                       if (q_chase == NULL)
967                         thread_head.head_pseudo = p->next_pseudo;
968                       else
969                         q_chase->next = p->next_pseudo;
970                     }
971                   else
972                     q_chase = q;
973                 }
974             }
975
976           /* Remove from live list.
977            */
978           thread_head.count--;
979
980           if (NULL == chase)
981             thread_head.head = p->next;
982           else
983             chase->next = p->next;
984
985           /* Add to deleted thread list.
986            */
987           p->next = deleted_threads.head;
988           deleted_threads.head = p;
989           deleted_threads.count++;
990           if (p->am_pseudo)
991             {
992               p->next_pseudo = deleted_threads.head_pseudo;
993               deleted_threads.head_pseudo = p;
994             }
995           p->terminated = 1;
996
997           return;
998         }
999
1000       else
1001         chase = p;
1002     }
1003 }
1004
1005 /* Get the pid for this tid. (Has to be a real TID!).
1006  */
1007 static int
1008 get_pid_for (lwpid_t tid)
1009 {
1010   thread_info *p;
1011
1012   for (p = thread_head.head; p; p = p->next)
1013     {
1014       if (p->tid == tid)
1015         {
1016           return p->pid;
1017         }
1018     }
1019
1020   for (p = deleted_threads.head; p; p = p->next)
1021     {
1022       if (p->tid == tid)
1023         {
1024           return p->pid;
1025         }
1026     }
1027
1028   return 0;
1029 }
1030
1031 /* Note that this thread's current event has been handled.
1032  */
1033 static void
1034 set_handled (int pid, lwpid_t tid)
1035 {
1036   thread_info *p;
1037
1038   p = find_thread_info (tid);
1039   if (NULL == p)
1040     p = add_tthread (pid, tid);
1041
1042   p->handled = 1;
1043 }
1044
1045 /* Was this thread's current event handled?
1046  */
1047 static int
1048 was_handled (lwpid_t tid)
1049 {
1050   thread_info *p;
1051
1052   p = find_thread_info (tid);
1053   if (NULL != p)
1054     return p->handled;
1055
1056   return 0;                     /* New threads have not been handled */
1057 }
1058
1059 /* Set this thread to unhandled.
1060  */
1061 static void
1062 clear_handled (lwpid_t tid)
1063 {
1064   thread_info *p;
1065
1066 #ifdef WAIT_BUFFER_DEBUG
1067   if (debug_on)
1068     printf ("clear_handled %d\n", (int) tid);
1069 #endif
1070
1071   p = find_thread_info (tid);
1072   if (p == NULL)
1073     error ("Internal error: No thread state to clear?");
1074
1075   p->handled = 0;
1076 }
1077
1078 /* Set all threads to unhandled.
1079  */
1080 static void
1081 clear_all_handled (void)
1082 {
1083   thread_info *p;
1084
1085 #ifdef WAIT_BUFFER_DEBUG
1086   if (debug_on)
1087     printf ("clear_all_handled\n");
1088 #endif
1089
1090   for (p = thread_head.head; p; p = p->next)
1091     {
1092       p->handled = 0;
1093     }
1094
1095   for (p = deleted_threads.head; p; p = p->next)
1096     {
1097       p->handled = 0;
1098     }
1099 }
1100
1101 /* Set this thread to default stepping mode.
1102  */
1103 static void
1104 clear_stepping_mode (lwpid_t tid)
1105 {
1106   thread_info *p;
1107
1108 #ifdef WAIT_BUFFER_DEBUG
1109   if (debug_on)
1110     printf ("clear_stepping_mode %d\n", (int) tid);
1111 #endif
1112
1113   p = find_thread_info (tid);
1114   if (p == NULL)
1115     error ("Internal error: No thread state to clear?");
1116
1117   p->stepping_mode = DO_DEFAULT;
1118 }
1119
1120 /* Set all threads to do default continue on resume.
1121  */
1122 static void
1123 clear_all_stepping_mode (void)
1124 {
1125   thread_info *p;
1126
1127 #ifdef WAIT_BUFFER_DEBUG
1128   if (debug_on)
1129     printf ("clear_all_stepping_mode\n");
1130 #endif
1131
1132   for (p = thread_head.head; p; p = p->next)
1133     {
1134       p->stepping_mode = DO_DEFAULT;
1135     }
1136
1137   for (p = deleted_threads.head; p; p = p->next)
1138     {
1139       p->stepping_mode = DO_DEFAULT;
1140     }
1141 }
1142
1143 /* Set all threads to unseen on this pass.
1144  */
1145 static void
1146 set_all_unseen (void)
1147 {
1148   thread_info *p;
1149
1150   for (p = thread_head.head; p; p = p->next)
1151     {
1152       p->seen = 0;
1153     }
1154 }
1155
1156 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1157 /* debugging routine.
1158  */
1159 static void
1160 print_tthread (thread_info *p)
1161 {
1162   printf (" Thread pid %d, tid %d", p->pid, p->tid);
1163   if (p->have_state)
1164     printf (", event is %s",
1165          get_printable_name_of_ttrace_event (p->last_stop_state.tts_event));
1166
1167   if (p->am_pseudo)
1168     printf (", pseudo thread");
1169
1170   if (p->have_signal)
1171     printf (", have signal 0x%x", p->signal_value);
1172
1173   if (p->have_start)
1174     printf (", have start at 0x%x", p->start);
1175
1176   printf (", step is %s", get_printable_name_of_stepping_mode (p->stepping_mode));
1177
1178   if (p->handled)
1179     printf (", handled");
1180   else
1181     printf (", not handled");
1182
1183   if (p->seen)
1184     printf (", seen");
1185   else
1186     printf (", not seen");
1187
1188   printf ("\n");
1189 }
1190
1191 static void
1192 print_tthreads (void)
1193 {
1194   thread_info *p;
1195
1196   if (thread_head.count == 0)
1197     printf ("Thread list is empty\n");
1198   else
1199     {
1200       printf ("Thread list has ");
1201       if (thread_head.count == 1)
1202         printf ("1 entry:\n");
1203       else
1204         printf ("%d entries:\n", thread_head.count);
1205       for (p = thread_head.head; p; p = p->next)
1206         {
1207           print_tthread (p);
1208         }
1209     }
1210
1211   if (deleted_threads.count == 0)
1212     printf ("Deleted thread list is empty\n");
1213   else
1214     {
1215       printf ("Deleted thread list has ");
1216       if (deleted_threads.count == 1)
1217         printf ("1 entry:\n");
1218       else
1219         printf ("%d entries:\n", deleted_threads.count);
1220
1221       for (p = deleted_threads.head; p; p = p->next)
1222         {
1223           print_tthread (p);
1224         }
1225     }
1226 }
1227 #endif
1228
1229 /* Update the thread list based on the "seen" bits.
1230  */
1231 static void
1232 update_thread_list (void)
1233 {
1234   thread_info *p;
1235   thread_info *chase;
1236
1237   chase = NULL;
1238   for (p = thread_head.head; p; p = p->next)
1239     {
1240       /* Is this an "unseen" thread which really happens to be a process?
1241          If so, is it inferior_pid and is a vfork in flight?  If yes to
1242          all, then DON'T REMOVE IT!  We're in the midst of moving a vfork
1243          operation, which is a multiple step thing, to the point where we
1244          can touch the parent again.  We've most likely stopped to examine
1245          the child at a late stage in the vfork, and if we're not following
1246          the child, we'd best not treat the parent as a dead "thread"...
1247        */
1248       if ((!p->seen) && p->am_pseudo && vfork_in_flight
1249           && (p->pid != vforking_child_pid))
1250         p->seen = 1;
1251
1252       if (!p->seen)
1253         {
1254           /* Remove this one
1255            */
1256
1257 #ifdef THREAD_DEBUG
1258           if (debug_on)
1259             printf ("Delete unseen thread: %d \n", p->tid);
1260 #endif
1261           del_tthread (p->tid);
1262         }
1263     }
1264 }
1265 \f
1266
1267
1268 /************************************************
1269  *            O/S call wrappers                 *
1270  ************************************************
1271  */
1272
1273 /* This function simply calls ttrace with the given arguments.  
1274  * It exists so that all calls to ttrace are isolated.  All
1275  * parameters should be as specified by "man 2 ttrace".
1276  *
1277  * No other "raw" calls to ttrace should exist in this module.
1278  */
1279 static int
1280 call_real_ttrace (ttreq_t request, pid_t pid, lwpid_t tid, TTRACE_ARG_TYPE addr,
1281                   TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2)
1282 {
1283   int tt_status;
1284
1285   errno = 0;
1286   tt_status = ttrace (request, pid, tid, addr, data, addr2);
1287
1288 #ifdef THREAD_DEBUG
1289   if (errno)
1290     {
1291       /* Don't bother for a known benign error: if you ask for the
1292        * first thread state, but there is only one thread and it's
1293        * not stopped, ttrace complains.
1294        *
1295        * We have this inside the #ifdef because our caller will do
1296        * this check for real.
1297        */
1298       if (request != TT_PROC_GET_FIRST_LWP_STATE
1299           || errno != EPROTO)
1300         {
1301           if (debug_on)
1302             printf ("TT fail for %s, with pid %d, tid %d, status %d \n",
1303                     get_printable_name_of_ttrace_request (request),
1304                     pid, tid, tt_status);
1305         }
1306     }
1307 #endif
1308
1309 #if 0
1310   /* ??rehrauer: It would probably be most robust to catch and report
1311    * failed requests here.  However, some clients of this interface
1312    * seem to expect to catch & deal with them, so we'd best not.
1313    */
1314   if (errno)
1315     {
1316       strcpy (reason_for_failure, "ttrace (");
1317       strcat (reason_for_failure, get_printable_name_of_ttrace_request (request));
1318       strcat (reason_for_failure, ")");
1319       printf ("ttrace error, errno = %d\n", errno);
1320       perror_with_name (reason_for_failure);
1321     }
1322 #endif
1323
1324   return tt_status;
1325 }
1326 \f
1327
1328 /* This function simply calls ttrace_wait with the given arguments.  
1329  * It exists so that all calls to ttrace_wait are isolated.
1330  *
1331  * No "raw" calls to ttrace_wait should exist elsewhere.
1332  */
1333 static int
1334 call_real_ttrace_wait (int pid, lwpid_t tid, ttwopt_t option, ttstate_t *tsp,
1335                        size_t tsp_size)
1336 {
1337   int ttw_status;
1338   thread_info *tinfo = NULL;
1339
1340   errno = 0;
1341   ttw_status = ttrace_wait (pid, tid, option, tsp, tsp_size);
1342
1343   if (errno)
1344     {
1345 #ifdef THREAD_DEBUG
1346       if (debug_on)
1347         printf ("TW fail with pid %d, tid %d \n", pid, tid);
1348 #endif
1349
1350       perror_with_name ("ttrace wait");
1351     }
1352
1353   return ttw_status;
1354 }
1355 \f
1356
1357 /* A process may have one or more kernel threads, of which all or
1358    none may be stopped.  This function returns the ID of the first
1359    kernel thread in a stopped state, or 0 if none are stopped.
1360
1361    This function can be used with get_process_next_stopped_thread_id
1362    to iterate over the IDs of all stopped threads of this process.
1363  */
1364 static lwpid_t
1365 get_process_first_stopped_thread_id (int pid, ttstate_t *thread_state)
1366 {
1367   int tt_status;
1368
1369   tt_status = call_real_ttrace (TT_PROC_GET_FIRST_LWP_STATE,
1370                                 (pid_t) pid,
1371                                 (lwpid_t) TT_NIL,
1372                                 (TTRACE_ARG_TYPE) thread_state,
1373                                 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1374                                 TT_NIL);
1375
1376   if (errno)
1377     {
1378       if (errno == EPROTO)
1379         {
1380           /* This is an error we can handle: there isn't any stopped
1381            * thread.  This happens when we're re-starting the application
1382            * and it has only one thread.  GET_NEXT handles the case of
1383            * no more stopped threads well; GET_FIRST doesn't.  (A ttrace
1384            * "feature".)
1385            */
1386           tt_status = 1;
1387           errno = 0;
1388           return 0;
1389         }
1390       else
1391         perror_with_name ("ttrace");
1392     }
1393
1394   if (tt_status < 0)
1395     /* Failed somehow.
1396      */
1397     return 0;
1398
1399   return thread_state->tts_lwpid;
1400 }
1401 \f
1402
1403 /* This function returns the ID of the "next" kernel thread in a
1404    stopped state, or 0 if there are none.  "Next" refers to the
1405    thread following that of the last successful call to this
1406    function or to get_process_first_stopped_thread_id, using
1407    the value of thread_state returned by that call.
1408
1409    This function can be used with get_process_first_stopped_thread_id
1410    to iterate over the IDs of all stopped threads of this process.
1411  */
1412 static lwpid_t
1413 get_process_next_stopped_thread_id (int pid, ttstate_t *thread_state)
1414 {
1415   int tt_status;
1416
1417   tt_status = call_real_ttrace (
1418                                  TT_PROC_GET_NEXT_LWP_STATE,
1419                                  (pid_t) pid,
1420                                  (lwpid_t) TT_NIL,
1421                                  (TTRACE_ARG_TYPE) thread_state,
1422                                  (TTRACE_ARG_TYPE) sizeof (*thread_state),
1423                                  TT_NIL);
1424   if (errno)
1425     perror_with_name ("ttrace");
1426
1427   if (tt_status < 0)
1428     /* Failed
1429      */
1430     return 0;
1431
1432   else if (tt_status == 0)
1433     {
1434       /* End of list, no next state.  Don't return the
1435        * tts_lwpid, as it's a meaningless "240".
1436        *
1437        * This is an HPUX "feature".
1438        */
1439       return 0;
1440     }
1441
1442   return thread_state->tts_lwpid;
1443 }
1444
1445 /* ??rehrauer: Eventually this function perhaps should be calling
1446    pid_to_thread_id.  However, that function currently does nothing
1447    for HP-UX.  Even then, I'm not clear whether that function
1448    will return a "kernel" thread ID, or a "user" thread ID.  If
1449    the former, we can just call it here.  If the latter, we must
1450    map from the "user" tid to a "kernel" tid.
1451
1452    NOTE: currently not called.
1453  */
1454 static lwpid_t
1455 get_active_tid_of_pid (int pid)
1456 {
1457   ttstate_t thread_state;
1458
1459   return get_process_first_stopped_thread_id (pid, &thread_state);
1460 }
1461
1462 /* This function returns 1 if tt_request is a ttrace request that
1463  * operates upon all threads of a (i.e., the entire) process.
1464  */
1465 int
1466 is_process_ttrace_request (ttreq_t tt_request)
1467 {
1468   return IS_TTRACE_PROCREQ (tt_request);
1469 }
1470 \f
1471
1472 /* This function translates a thread ttrace request into
1473  * the equivalent process request for a one-thread process.
1474  */
1475 static ttreq_t
1476 make_process_version (ttreq_t request)
1477 {
1478   if (!IS_TTRACE_REQ (request))
1479     {
1480       error ("Internal error, bad ttrace request made\n");
1481       return -1;
1482     }
1483
1484   switch (request)
1485     {
1486     case TT_LWP_STOP:
1487       return TT_PROC_STOP;
1488
1489     case TT_LWP_CONTINUE:
1490       return TT_PROC_CONTINUE;
1491
1492     case TT_LWP_GET_EVENT_MASK:
1493       return TT_PROC_GET_EVENT_MASK;
1494
1495     case TT_LWP_SET_EVENT_MASK:
1496       return TT_PROC_SET_EVENT_MASK;
1497
1498     case TT_LWP_SINGLE:
1499     case TT_LWP_RUREGS:
1500     case TT_LWP_WUREGS:
1501     case TT_LWP_GET_STATE:
1502       return -1;                /* No equivalent */
1503
1504     default:
1505       return request;
1506     }
1507 }
1508 \f
1509
1510 /* This function translates the "pid" used by the rest of
1511  * gdb to a real pid and a tid.  It then calls "call_real_ttrace"
1512  * with the given arguments.
1513  *
1514  * In general, other parts of this module should call this
1515  * function when they are dealing with external users, who only
1516  * have tids to pass (but they call it "pid" for historical
1517  * reasons).
1518  */
1519 static int
1520 call_ttrace (ttreq_t request, int gdb_tid, TTRACE_ARG_TYPE addr,
1521              TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2)
1522 {
1523   lwpid_t real_tid;
1524   int real_pid;
1525   ttreq_t new_request;
1526   int tt_status;
1527   char reason_for_failure[100]; /* Arbitrary size, should be big enough. */
1528
1529 #ifdef THREAD_DEBUG
1530   int is_interesting = 0;
1531
1532   if (TT_LWP_RUREGS == request)
1533     {
1534       is_interesting = 1;       /* Adjust code here as desired */
1535     }
1536
1537   if (is_interesting && 0 && debug_on)
1538     {
1539       if (!is_process_ttrace_request (request))
1540         {
1541           printf ("TT: Thread request, tid is %d", gdb_tid);
1542           printf ("== SINGLE at %x", addr);
1543         }
1544       else
1545         {
1546           printf ("TT: Process request, tid is %d\n", gdb_tid);
1547           printf ("==! SINGLE at %x", addr);
1548         }
1549     }
1550 #endif
1551
1552   /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1553    * which happen before any threads get set up) should go
1554    * directly to "call_real_ttrace", so they don't happen here.
1555    *
1556    * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1557    * rule them out....
1558    */
1559 #ifdef THREAD_DEBUG
1560   if (request == TT_PROC_SETTRC && debug_on)
1561     printf ("Unexpected call for TT_PROC_SETTRC\n");
1562 #endif
1563
1564   /* Sometimes we get called with a bogus tid (e.g., if a
1565    * thread has terminated, we return 0; inftarg later asks
1566    * whether the thread has exited/forked/vforked).
1567    */
1568   if (gdb_tid == 0)
1569     {
1570       errno = ESRCH;            /* ttrace's response would probably be "No such process". */
1571       return -1;
1572     }
1573
1574   /* All other cases should be able to expect that there are
1575    * thread records.
1576    */
1577   if (!any_thread_records ())
1578     {
1579 #ifdef THREAD_DEBUG
1580       if (debug_on)
1581         warning ("No thread records for ttrace call");
1582 #endif
1583       errno = ESRCH;            /* ttrace's response would be "No such process". */
1584       return -1;
1585     }
1586
1587   /* OK, now the task is to translate the incoming tid into
1588    * a pid/tid pair.
1589    */
1590   real_tid = map_from_gdb_tid (gdb_tid);
1591   real_pid = get_pid_for (real_tid);
1592
1593   /* Now check the result.  "Real_pid" is NULL if our list
1594    * didn't find it.  We have some tricks we can play to fix
1595    * this, however.
1596    */
1597   if (0 == real_pid)
1598     {
1599       ttstate_t thread_state;
1600
1601 #ifdef THREAD_DEBUG
1602       if (debug_on)
1603         printf ("No saved pid for tid %d\n", gdb_tid);
1604 #endif
1605
1606       if (is_process_ttrace_request (request))
1607         {
1608
1609           /* Ok, we couldn't get a tid.  Try to translate to
1610            * the equivalent process operation.  We expect this
1611            * NOT to happen, so this is a desparation-type
1612            * move.  It can happen if there is an internal
1613            * error and so no "wait()" call is ever done.
1614            */
1615           new_request = make_process_version (request);
1616           if (new_request == -1)
1617             {
1618
1619 #ifdef THREAD_DEBUG
1620               if (debug_on)
1621                 printf ("...and couldn't make process version of thread operation\n");
1622 #endif
1623
1624               /* Use hacky saved pid, which won't always be correct
1625                * in the multi-process future.  Use tid as thread,
1626                * probably dooming this to failure.  FIX!
1627                */
1628               if (saved_real_pid != 0)
1629                 {
1630 #ifdef THREAD_DEBUG
1631                   if (debug_on)
1632                     printf ("...using saved pid %d\n", saved_real_pid);
1633 #endif
1634
1635                   real_pid = saved_real_pid;
1636                   real_tid = gdb_tid;
1637                 }
1638
1639               else
1640                 error ("Unable to perform thread operation");
1641             }
1642
1643           else
1644             {
1645               /* Sucessfully translated this to a process request,
1646                * which needs no thread value.
1647                */
1648               real_pid = gdb_tid;
1649               real_tid = 0;
1650               request = new_request;
1651
1652 #ifdef THREAD_DEBUG
1653               if (debug_on)
1654                 {
1655                   printf ("Translated thread request to process request\n");
1656                   if (saved_real_pid == 0)
1657                     printf ("...but there's no saved pid\n");
1658
1659                   else
1660                     {
1661                       if (gdb_tid != saved_real_pid)
1662                         printf ("...but have the wrong pid (%d rather than %d)\n",
1663                                 gdb_tid, saved_real_pid);
1664                     }
1665                 }
1666 #endif
1667             }                   /* Translated to a process request */
1668         }                       /* Is a process request */
1669
1670       else
1671         {
1672           /* We have to have a thread.  Ooops.
1673            */
1674           error ("Thread request with no threads (%s)",
1675                  get_printable_name_of_ttrace_request (request));
1676         }
1677     }
1678
1679   /* Ttrace doesn't like to see tid values on process requests,
1680    * even if we have the right one.
1681    */
1682   if (is_process_ttrace_request (request))
1683     {
1684       real_tid = 0;
1685     }
1686
1687 #ifdef THREAD_DEBUG
1688   if (is_interesting && 0 && debug_on)
1689     {
1690       printf ("    now tid %d, pid %d\n", real_tid, real_pid);
1691       printf ("    request is %s\n", get_printable_name_of_ttrace_request (request));
1692     }
1693 #endif
1694
1695   /* Finally, the (almost) real call.
1696    */
1697   tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2);
1698
1699 #ifdef THREAD_DEBUG
1700   if (is_interesting && debug_on)
1701     {
1702       if (!TT_OK (tt_status, errno)
1703           && !(tt_status == 0 & errno == 0))
1704         printf (" got error (errno==%d, status==%d)\n", errno, tt_status);
1705     }
1706 #endif
1707
1708   return tt_status;
1709 }
1710
1711
1712 /* Stop all the threads of a process.
1713
1714  * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1715  *       to get a TTEVT_NONE event, discarding the old event.  Be
1716  *       very careful, and only call TT_PROC_STOP when you mean it!
1717  */
1718 static void
1719 stop_all_threads_of_process (pid_t real_pid)
1720 {
1721   int ttw_status;
1722
1723   ttw_status = call_real_ttrace (TT_PROC_STOP,
1724                                  (pid_t) real_pid,
1725                                  (lwpid_t) TT_NIL,
1726                                  (TTRACE_ARG_TYPE) TT_NIL,
1727                                  (TTRACE_ARG_TYPE) TT_NIL,
1728                                  TT_NIL);
1729   if (errno)
1730     perror_with_name ("ttrace stop of other threads");
1731 }
1732
1733
1734 /* Under some circumstances, it's unsafe to attempt to stop, or even
1735    query the state of, a process' threads.
1736
1737    In ttrace-based HP-UX, an example is a vforking child process.  The
1738    vforking parent and child are somewhat fragile, w/r/t what we can do
1739    what we can do to them with ttrace, until after the child exits or
1740    execs, or until the parent's vfork event is delivered.  Until that
1741    time, we must not try to stop the process' threads, or inquire how
1742    many there are, or even alter its data segments, or it typically dies
1743    with a SIGILL.  Sigh.
1744
1745    This function returns 1 if this stopped process, and the event that
1746    we're told was responsible for its current stopped state, cannot safely
1747    have its threads examined.
1748  */
1749 #define CHILD_VFORKED(evt,pid) \
1750   (((evt) == TTEVT_VFORK) && ((pid) != inferior_pid))
1751 #define CHILD_URPED(evt,pid) \
1752   ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1753 #define PARENT_VFORKED(evt,pid) \
1754   (((evt) == TTEVT_VFORK) && ((pid) == inferior_pid))
1755
1756 static int
1757 can_touch_threads_of_process (int pid, ttevents_t stopping_event)
1758 {
1759   if (CHILD_VFORKED (stopping_event, pid))
1760     {
1761       vforking_child_pid = pid;
1762       vfork_in_flight = 1;
1763     }
1764
1765   else if (vfork_in_flight &&
1766            (PARENT_VFORKED (stopping_event, pid) ||
1767             CHILD_URPED (stopping_event, pid)))
1768     {
1769       vfork_in_flight = 0;
1770       vforking_child_pid = 0;
1771     }
1772
1773   return !vfork_in_flight;
1774 }
1775
1776
1777 /* If we can find an as-yet-unhandled thread state of a
1778  * stopped thread of this process return 1 and set "tsp".
1779  * Return 0 if we can't.
1780  *
1781  * If this function is used when the threads of PIS haven't
1782  * been stopped, undefined behaviour is guaranteed!
1783  */
1784 static int
1785 select_stopped_thread_of_process (int pid, ttstate_t *tsp)
1786 {
1787   lwpid_t candidate_tid, tid;
1788   ttstate_t candidate_tstate, tstate;
1789
1790   /* If we're not allowed to touch the process now, then just
1791    * return the current value of *TSP.
1792    *
1793    * This supports "vfork".  It's ok, really, to double the
1794    * current event (the child EXEC, we hope!).
1795    */
1796   if (!can_touch_threads_of_process (pid, tsp->tts_event))
1797     return 1;
1798
1799   /* Decide which of (possibly more than one) events to
1800    * return as the first one.  We scan them all so that
1801    * we always return the result of a fake-step first.
1802    */
1803   candidate_tid = 0;
1804   for (tid = get_process_first_stopped_thread_id (pid, &tstate);
1805        tid != 0;
1806        tid = get_process_next_stopped_thread_id (pid, &tstate))
1807     {
1808       /* TTEVT_NONE events are uninteresting to our clients.  They're
1809        * an artifact of our "stop the world" model--the thread is
1810        * stopped because we stopped it.
1811        */
1812       if (tstate.tts_event == TTEVT_NONE)
1813         {
1814           set_handled (pid, tstate.tts_lwpid);
1815         }
1816
1817       /* Did we just single-step a single thread, without letting any
1818        * of the others run?  Is this an event for that thread?
1819        *
1820        * If so, we believe our client would prefer to see this event
1821        * over any others.  (Typically the client wants to just push
1822        * one thread a little farther forward, and then go around
1823        * checking for what all threads are doing.)
1824        */
1825       else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid))
1826         {
1827 #ifdef WAIT_BUFFER_DEBUG
1828           /* It's possible here to see either a SIGTRAP (due to
1829            * successful completion of a step) or a SYSCALL_ENTRY
1830            * (due to a step completion with active hardware
1831            * watchpoints).
1832            */
1833           if (debug_on)
1834             printf ("Ending fake step with tid %d, state %s\n",
1835                     tstate.tts_lwpid,
1836                     get_printable_name_of_ttrace_event (tstate.tts_event));
1837 #endif
1838
1839           /* Remember this one, and throw away any previous
1840            * candidate.
1841            */
1842           candidate_tid = tstate.tts_lwpid;
1843           candidate_tstate = tstate;
1844         }
1845
1846 #ifdef FORGET_DELETED_BPTS
1847
1848       /* We can't just do this, as if we do, and then wind
1849        * up the loop with no unhandled events, we need to
1850        * handle that case--the appropriate reaction is to
1851        * just continue, but there's no easy way to do that.
1852        *
1853        * Better to put this in the ttrace_wait call--if, when
1854        * we fake a wait, we update our events based on the
1855        * breakpoint_here_pc call and find there are no more events,
1856        * then we better continue and so on.
1857        *
1858        * Or we could put it in the next/continue fake.
1859        * But it has to go in the buffering code, not in the
1860        * real go/wait code.
1861        */
1862       else if ((TTEVT_SIGNAL == tstate.tts_event)
1863                && (5 == tstate.tts_u.tts_signal.tts_signo)
1864                && (0 != get_raw_pc (tstate.tts_lwpid))
1865                && !breakpoint_here_p (get_raw_pc (tstate.tts_lwpid)))
1866         {
1867           /*
1868            * If the user deleted a breakpoint while this
1869            * breakpoint-hit event was buffered, we can forget
1870            * it now.
1871            */
1872 #ifdef WAIT_BUFFER_DEBUG
1873           if (debug_on)
1874             printf ("Forgetting deleted bp hit for thread %d\n",
1875                     tstate.tts_lwpid);
1876 #endif
1877
1878           set_handled (pid, tstate.tts_lwpid);
1879         }
1880 #endif
1881
1882       /* Else, is this the first "unhandled" event?  If so,
1883        * we believe our client wants to see it (if we don't
1884        * see a fake-step later on in the scan).
1885        */
1886       else if (!was_handled (tstate.tts_lwpid) && candidate_tid == 0)
1887         {
1888           candidate_tid = tstate.tts_lwpid;
1889           candidate_tstate = tstate;
1890         }
1891
1892       /* This is either an event that has already been "handled",
1893        * and thus we believe is uninteresting to our client, or we
1894        * already have a candidate event.  Ignore it...
1895        */
1896     }
1897
1898   /* What do we report?
1899    */
1900   if (doing_fake_step)
1901     {
1902       if (candidate_tid == fake_step_tid)
1903         {
1904           /* Fake step.
1905            */
1906           tstate = candidate_tstate;
1907         }
1908       else
1909         {
1910           warning ("Internal error: fake-step failed to complete.");
1911           return 0;
1912         }
1913     }
1914   else if (candidate_tid != 0)
1915     {
1916       /* Found a candidate unhandled event.
1917        */
1918       tstate = candidate_tstate;
1919     }
1920   else if (tid != 0)
1921     {
1922       warning ("Internal error in call of ttrace_wait.");
1923       return 0;
1924     }
1925   else
1926     {
1927       warning ("Internal error: no unhandled thread event to select");
1928       return 0;
1929     }
1930
1931   copy_ttstate_t (tsp, &tstate);
1932   return 1;
1933 }                               /* End of select_stopped_thread_of_process */
1934
1935 #ifdef PARANOIA
1936 /* Check our internal thread data against the real thing.
1937  */
1938 static void
1939 check_thread_consistency (pid_t real_pid)
1940 {
1941   int tid;                      /* really lwpid_t */
1942   ttstate_t tstate;
1943   thread_info *p;
1944
1945   /* Spin down the O/S list of threads, checking that they
1946    * match what we've got.
1947    */
1948   for (tid = get_process_first_stopped_thread_id (real_pid, &tstate);
1949        tid != 0;
1950        tid = get_process_next_stopped_thread_id (real_pid, &tstate))
1951     {
1952
1953       p = find_thread_info (tid);
1954
1955       if (NULL == p)
1956         {
1957           warning ("No internal thread data for thread %d.", tid);
1958           continue;
1959         }
1960
1961       if (!p->seen)
1962         {
1963           warning ("Inconsistent internal thread data for thread %d.", tid);
1964         }
1965
1966       if (p->terminated)
1967         {
1968           warning ("Thread %d is not terminated, internal error.", tid);
1969           continue;
1970         }
1971
1972
1973 #define TT_COMPARE( fld ) \
1974             tstate.fld != p->last_stop_state.fld
1975
1976       if (p->have_state)
1977         {
1978           if (TT_COMPARE (tts_pid)
1979               || TT_COMPARE (tts_lwpid)
1980               || TT_COMPARE (tts_user_tid)
1981               || TT_COMPARE (tts_event)
1982               || TT_COMPARE (tts_flags)
1983               || TT_COMPARE (tts_scno)
1984               || TT_COMPARE (tts_scnargs))
1985             {
1986               warning ("Internal thread data for thread %d is wrong.", tid);
1987               continue;
1988             }
1989         }
1990     }
1991 }
1992 #endif /* PARANOIA */
1993 \f
1994
1995 /* This function wraps calls to "call_real_ttrace_wait" so
1996  * that a actual wait is only done when all pending events
1997  * have been reported.
1998  *
1999  * Note that typically it is called with a pid of "0", i.e. 
2000  * the "don't care" value.
2001  *
2002  * Return value is the status of the pseudo wait.
2003  */
2004 static int
2005 call_ttrace_wait (int pid, ttwopt_t option, ttstate_t *tsp, size_t tsp_size)
2006 {
2007   /* This holds the actual, for-real, true process ID.
2008    */
2009   static int real_pid;
2010
2011   /* As an argument to ttrace_wait, zero pid
2012    * means "Any process", and zero tid means
2013    * "Any thread of the specified process".
2014    */
2015   int wait_pid = 0;
2016   lwpid_t wait_tid = 0;
2017   lwpid_t real_tid;
2018
2019   int ttw_status = 0;           /* To be returned */
2020
2021   thread_info *tinfo = NULL;
2022
2023   if (pid != 0)
2024     {
2025       /* Unexpected case.
2026        */
2027 #ifdef THREAD_DEBUG
2028       if (debug_on)
2029         printf ("TW: Pid to wait on is %d\n", pid);
2030 #endif
2031
2032       if (!any_thread_records ())
2033         error ("No thread records for ttrace call w. specific pid");
2034
2035       /* OK, now the task is to translate the incoming tid into
2036        * a pid/tid pair.
2037        */
2038       real_tid = map_from_gdb_tid (pid);
2039       real_pid = get_pid_for (real_tid);
2040 #ifdef THREAD_DEBUG
2041       if (debug_on)
2042         printf ("==TW: real pid %d, real tid %d\n", real_pid, real_tid);
2043 #endif
2044     }
2045
2046
2047   /* Sanity checks and set-up.
2048    *                             Process State
2049    *
2050    *                        Stopped   Running    Fake-step  (v)Fork
2051    *                      \________________________________________
2052    *                      |
2053    *  No buffered events  |  error     wait       wait      wait
2054    *                      |
2055    *  Buffered events     |  debuffer  error      wait      debuffer (?)
2056    *
2057    */
2058   if (more_events_left == 0)
2059     {
2060
2061       if (process_state == RUNNING)
2062         {
2063           /* OK--normal call of ttrace_wait with no buffered events.
2064            */
2065           ;
2066         }
2067       else if (process_state == FAKE_STEPPING)
2068         {
2069           /* Ok--call of ttrace_wait to support
2070            * fake stepping with no buffered events.
2071            *
2072            * But we better be fake-stepping!
2073            */
2074           if (!doing_fake_step)
2075             {
2076               warning ("Inconsistent thread state.");
2077             }
2078         }
2079       else if ((process_state == FORKING)
2080                || (process_state == VFORKING))
2081         {
2082           /* Ok--there are two processes, so waiting
2083            * for the second while the first is stopped
2084            * is ok.  Handled bits stay as they were.
2085            */
2086           ;
2087         }
2088       else if (process_state == STOPPED)
2089         {
2090           warning ("Process not running at wait call.");
2091         }
2092       else
2093         /* No known state.
2094          */
2095         warning ("Inconsistent process state.");
2096     }
2097
2098   else
2099     {
2100       /* More events left
2101        */
2102       if (process_state == STOPPED)
2103         {
2104           /* OK--buffered events being unbuffered.
2105            */
2106           ;
2107         }
2108       else if (process_state == RUNNING)
2109         {
2110           /* An error--shouldn't have buffered events
2111            * when running.
2112            */
2113           warning ("Trying to continue with buffered events:");
2114         }
2115       else if (process_state == FAKE_STEPPING)
2116         {
2117           /*
2118            * Better be fake-stepping!
2119            */
2120           if (!doing_fake_step)
2121             {
2122               warning ("Losing buffered thread events!\n");
2123             }
2124         }
2125       else if ((process_state == FORKING)
2126                || (process_state == VFORKING))
2127         {
2128           /* Ok--there are two processes, so waiting
2129            * for the second while the first is stopped
2130            * is ok.  Handled bits stay as they were.
2131            */
2132           ;
2133         }
2134       else
2135         warning ("Process in unknown state with buffered events.");
2136     }
2137
2138   /* Sometimes we have to wait for a particular thread
2139    * (if we're stepping over a bpt).  In that case, we
2140    * _know_ it's going to complete the single-step we
2141    * asked for (because we're only doing the step under
2142    * certain very well-understood circumstances), so it
2143    * can't block.
2144    */
2145   if (doing_fake_step)
2146     {
2147       wait_tid = fake_step_tid;
2148       wait_pid = get_pid_for (fake_step_tid);
2149
2150 #ifdef WAIT_BUFFER_DEBUG
2151       if (debug_on)
2152         printf ("Doing a wait after a fake-step for %d, pid %d\n",
2153                 wait_tid, wait_pid);
2154 #endif
2155     }
2156
2157   if (more_events_left == 0     /* No buffered events, need real ones. */
2158       || process_state != STOPPED)
2159     {
2160       /* If there are no buffered events, and so we need
2161        * real ones, or if we are FORKING, VFORKING, 
2162        * FAKE_STEPPING or RUNNING, and thus have to do
2163        * a real wait, then do a real wait.
2164        */
2165
2166 #ifdef WAIT_BUFFER_DEBUG
2167       /* Normal case... */
2168       if (debug_on)
2169         printf ("TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid);
2170 #endif
2171
2172       /* The actual wait call.
2173        */
2174       ttw_status = call_real_ttrace_wait (wait_pid, wait_tid, option, tsp, tsp_size);
2175
2176       /* Note that the routines we'll call will be using "call_real_ttrace",
2177        * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2178        * the rest of the world uses (which is actually the tid).
2179        */
2180       real_pid = tsp->tts_pid;
2181
2182       /* For most events: Stop the world!
2183
2184        * It's sometimes not safe to stop all threads of a process.
2185        * Sometimes it's not even safe to ask for the thread state
2186        * of a process!
2187        */
2188       if (can_touch_threads_of_process (real_pid, tsp->tts_event))
2189         {
2190           /* If we're really only stepping a single thread, then don't
2191            * try to stop all the others -- we only do this single-stepping
2192            * business when all others were already stopped...and the stop
2193            * would mess up other threads' events.
2194            *
2195            * Similiarly, if there are other threads with events,
2196            * don't do the stop.
2197            */
2198           if (!doing_fake_step)
2199             {
2200               if (more_events_left > 0)
2201                 warning ("Internal error in stopping process");
2202
2203               stop_all_threads_of_process (real_pid);
2204
2205               /* At this point, we could scan and update_thread_list(),
2206                * and only use the local list for the rest of the
2207                * module! We'd get rid of the scans in the various
2208                * continue routines (adding one in attach).  It'd
2209                * be great--UPGRADE ME!
2210                */
2211             }
2212         }
2213
2214 #ifdef PARANOIA
2215       else if (debug_on)
2216         {
2217           if (more_events_left > 0)
2218             printf ("== Can't stop process; more events!\n");
2219           else
2220             printf ("== Can't stop process!\n");
2221         }
2222 #endif
2223
2224       process_state = STOPPED;
2225
2226 #ifdef WAIT_BUFFER_DEBUG
2227       if (debug_on)
2228         printf ("Process set to STOPPED\n");
2229 #endif
2230     }
2231
2232   else
2233     {
2234       /* Fake a call to ttrace_wait.  The process must be
2235        * STOPPED, as we aren't going to do any wait.
2236        */
2237 #ifdef WAIT_BUFFER_DEBUG
2238       if (debug_on)
2239         printf ("TW: fake it\n");
2240 #endif
2241
2242       if (process_state != STOPPED)
2243         {
2244           warning ("Process not stopped at wait call, in state '%s'.\n",
2245                    get_printable_name_of_process_state (process_state));
2246         }
2247
2248       if (doing_fake_step)
2249         error ("Internal error in stepping over breakpoint");
2250
2251       ttw_status = 0;           /* Faking it is always successful! */
2252     }                           /* End of fake or not? if */
2253
2254   /* Pick an event to pass to our caller.  Be paranoid.
2255    */
2256   if (!select_stopped_thread_of_process (real_pid, tsp))
2257     warning ("Can't find event, using previous event.");
2258
2259   else if (tsp->tts_event == TTEVT_NONE)
2260     warning ("Internal error: no thread has a real event.");
2261
2262   else if (doing_fake_step)
2263     {
2264       if (fake_step_tid != tsp->tts_lwpid)
2265         warning ("Internal error in stepping over breakpoint.");
2266
2267       /* This wait clears the (current) fake-step if there was one.
2268        */
2269       doing_fake_step = 0;
2270       fake_step_tid = 0;
2271     }
2272
2273   /* We now have a correct tsp and ttw_status for the thread
2274    * which we want to report.  So it's "handled"!  This call
2275    * will add it to our list if it's not there already.
2276    */
2277   set_handled (real_pid, tsp->tts_lwpid);
2278
2279   /* Save a copy of the ttrace state of this thread, in our local
2280      thread descriptor.
2281
2282      This caches the state.  The implementation of queries like
2283      target_has_execd can then use this cached state, rather than
2284      be forced to make an explicit ttrace call to get it.
2285
2286      (Guard against the condition that this is the first time we've
2287      waited on, i.e., seen this thread, and so haven't yet entered
2288      it into our list of threads.)
2289    */
2290   tinfo = find_thread_info (tsp->tts_lwpid);
2291   if (tinfo != NULL)
2292     {
2293       copy_ttstate_t (&tinfo->last_stop_state, tsp);
2294       tinfo->have_state = 1;
2295     }
2296
2297   return ttw_status;
2298 }                               /* call_ttrace_wait */
2299
2300 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2301 int
2302 child_reported_exec_events_per_exec_call (void)
2303 {
2304   return 1;                     /* ttrace reports the event once per call. */
2305 }
2306 #endif
2307 \f
2308
2309
2310 /* Our implementation of hardware watchpoints involves making memory
2311    pages write-protected.  We must remember a page's original permissions,
2312    and we must also know when it is appropriate to restore a page's
2313    permissions to its original state.
2314
2315    We use a "dictionary" of hardware-watched pages to do this.  Each
2316    hardware-watched page is recorded in the dictionary.  Each page's
2317    dictionary entry contains the original permissions and a reference
2318    count.  Pages are hashed into the dictionary by their start address.
2319
2320    When hardware watchpoint is set on page X for the first time, page X
2321    is added to the dictionary with a reference count of 1.  If other
2322    hardware watchpoints are subsequently set on page X, its reference
2323    count is incremented.  When hardware watchpoints are removed from
2324    page X, its reference count is decremented.  If a page's reference
2325    count drops to 0, it's permissions are restored and the page's entry
2326    is thrown out of the dictionary.
2327  */
2328 typedef struct memory_page
2329 {
2330   CORE_ADDR page_start;
2331   int reference_count;
2332   int original_permissions;
2333   struct memory_page *next;
2334   struct memory_page *previous;
2335 }
2336 memory_page_t;
2337
2338 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT  128
2339
2340 static struct
2341   {
2342     LONGEST page_count;
2343     int page_size;
2344     int page_protections_allowed;
2345     /* These are just the heads of chains of actual page descriptors. */
2346     memory_page_t buckets[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT];
2347   }
2348 memory_page_dictionary;
2349
2350
2351 static void
2352 require_memory_page_dictionary (void)
2353 {
2354   int i;
2355
2356   /* Is the memory page dictionary ready for use?  If so, we're done. */
2357   if (memory_page_dictionary.page_count >= (LONGEST) 0)
2358     return;
2359
2360   /* Else, initialize it. */
2361   memory_page_dictionary.page_count = (LONGEST) 0;
2362
2363   for (i = 0; i < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++)
2364     {
2365       memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0;
2366       memory_page_dictionary.buckets[i].reference_count = 0;
2367       memory_page_dictionary.buckets[i].next = NULL;
2368       memory_page_dictionary.buckets[i].previous = NULL;
2369     }
2370 }
2371
2372
2373 static void
2374 retire_memory_page_dictionary (void)
2375 {
2376   memory_page_dictionary.page_count = (LONGEST) - 1;
2377 }
2378
2379
2380 /* Write-protect the memory page that starts at this address.
2381
2382    Returns the original permissions of the page.
2383  */
2384 static int
2385 write_protect_page (int pid, CORE_ADDR page_start)
2386 {
2387   int tt_status;
2388   int original_permissions;
2389   int new_permissions;
2390
2391   tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
2392                            pid,
2393                            (TTRACE_ARG_TYPE) page_start,
2394                            TT_NIL,
2395                            (TTRACE_ARG_TYPE) & original_permissions);
2396   if (errno || (tt_status < 0))
2397     {
2398       return 0;                 /* What else can we do? */
2399     }
2400
2401   /* We'll also write-protect the page now, if that's allowed. */
2402   if (memory_page_dictionary.page_protections_allowed)
2403     {
2404       new_permissions = original_permissions & ~PROT_WRITE;
2405       tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2406                                pid,
2407                                (TTRACE_ARG_TYPE) page_start,
2408                          (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2409                                (TTRACE_ARG_TYPE) new_permissions);
2410       if (errno || (tt_status < 0))
2411         {
2412           return 0;             /* What else can we do? */
2413         }
2414     }
2415
2416   return original_permissions;
2417 }
2418
2419
2420 /* Unwrite-protect the memory page that starts at this address, restoring
2421    (what we must assume are) its original permissions.
2422  */
2423 static void
2424 unwrite_protect_page (int pid, CORE_ADDR page_start, int original_permissions)
2425 {
2426   int tt_status;
2427
2428   tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2429                            pid,
2430                            (TTRACE_ARG_TYPE) page_start,
2431                          (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2432                            (TTRACE_ARG_TYPE) original_permissions);
2433   if (errno || (tt_status < 0))
2434     {
2435       return;                   /* What else can we do? */
2436     }
2437 }
2438
2439
2440 /* Memory page-protections are used to implement "hardware" watchpoints
2441    on HP-UX.
2442
2443    For every memory page that is currently being watched (i.e., that
2444    presently should be write-protected), write-protect it.
2445  */
2446 void
2447 hppa_enable_page_protection_events (int pid)
2448 {
2449   int bucket;
2450
2451   memory_page_dictionary.page_protections_allowed = 1;
2452
2453   for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2454     {
2455       memory_page_t *page;
2456
2457       page = memory_page_dictionary.buckets[bucket].next;
2458       while (page != NULL)
2459         {
2460           page->original_permissions = write_protect_page (pid, page->page_start);
2461           page = page->next;
2462         }
2463     }
2464 }
2465
2466
2467 /* Memory page-protections are used to implement "hardware" watchpoints
2468    on HP-UX.
2469
2470    For every memory page that is currently being watched (i.e., that
2471    presently is or should be write-protected), un-write-protect it.
2472  */
2473 void
2474 hppa_disable_page_protection_events (int pid)
2475 {
2476   int bucket;
2477
2478   for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2479     {
2480       memory_page_t *page;
2481
2482       page = memory_page_dictionary.buckets[bucket].next;
2483       while (page != NULL)
2484         {
2485           unwrite_protect_page (pid, page->page_start, page->original_permissions);
2486           page = page->next;
2487         }
2488     }
2489
2490   memory_page_dictionary.page_protections_allowed = 0;
2491 }
2492
2493 /* Count the number of outstanding events.  At this
2494  * point, we have selected one thread and its event
2495  * as the one to be "reported" upwards to core gdb.
2496  * That thread is already marked as "handled".
2497  *
2498  * Note: we could just scan our own thread list.  FIXME!
2499  */
2500 static int
2501 count_unhandled_events (int real_pid, lwpid_t real_tid)
2502 {
2503   ttstate_t tstate;
2504   lwpid_t ttid;
2505   int events_left;
2506
2507   /* Ok, find out how many threads have real events to report.
2508    */
2509   events_left = 0;
2510   ttid = get_process_first_stopped_thread_id (real_pid, &tstate);
2511
2512 #ifdef THREAD_DEBUG
2513   if (debug_on)
2514     {
2515       if (ttid == 0)
2516         printf ("Process %d has no threads\n", real_pid);
2517       else
2518         printf ("Process %d has these threads:\n", real_pid);
2519     }
2520 #endif
2521
2522   while (ttid > 0)
2523     {
2524       if (tstate.tts_event != TTEVT_NONE
2525           && !was_handled (ttid))
2526         {
2527           /* TTEVT_NONE implies we just stopped it ourselves
2528            * because we're the stop-the-world guys, so it's
2529            * not an event from our point of view.
2530            *
2531            * If "was_handled" is true, this is an event we
2532            * already handled, so don't count it.
2533            *
2534            * Note that we don't count the thread with the
2535            * currently-reported event, as it's already marked
2536            * as handled.
2537            */
2538           events_left++;
2539         }
2540
2541 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2542       if (debug_on)
2543         {
2544           if (ttid == real_tid)
2545             printf ("*");       /* Thread we're reporting */
2546           else
2547             printf (" ");
2548
2549           if (tstate.tts_event != TTEVT_NONE)
2550             printf ("+");       /* Thread with a real event */
2551           else
2552             printf (" ");
2553
2554           if (was_handled (ttid))
2555             printf ("h");       /* Thread has been handled */
2556           else
2557             printf (" ");
2558
2559           printf (" %d, with event %s", ttid,
2560                   get_printable_name_of_ttrace_event (tstate.tts_event));
2561
2562           if (tstate.tts_event == TTEVT_SIGNAL
2563               && 5 == tstate.tts_u.tts_signal.tts_signo)
2564             {
2565               CORE_ADDR pc_val;
2566
2567               pc_val = get_raw_pc (ttid);
2568
2569               if (pc_val > 0)
2570                 printf (" breakpoint at 0x%x\n", pc_val);
2571               else
2572                 printf (" bpt, can't fetch pc.\n");
2573             }
2574           else
2575             printf ("\n");
2576         }
2577 #endif
2578
2579       ttid = get_process_next_stopped_thread_id (real_pid, &tstate);
2580     }
2581
2582 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2583   if (debug_on)
2584     if (events_left > 0)
2585       printf ("There are thus %d pending events\n", events_left);
2586 #endif
2587
2588   return events_left;
2589 }
2590
2591 /* This function is provided as a sop to clients that are calling
2592  * ptrace_wait to wait for a process to stop.  (see the
2593  * implementation of child_wait.)  Return value is the pid for
2594  * the event that ended the wait.
2595  *
2596  * Note: used by core gdb and so uses the pseudo-pid (really tid).
2597  */
2598 int
2599 ptrace_wait (int pid, int *status)
2600 {
2601   ttstate_t tsp;
2602   int ttwait_return;
2603   int real_pid;
2604   ttstate_t state;
2605   lwpid_t real_tid;
2606   int return_pid;
2607
2608   /* The ptrace implementation of this also ignores pid.
2609    */
2610   *status = 0;
2611
2612   ttwait_return = call_ttrace_wait (0, TTRACE_WAITOK, &tsp, sizeof (tsp));
2613   if (ttwait_return < 0)
2614     {
2615       /* ??rehrauer: It appears that if our inferior exits and we
2616          haven't asked for exit events, that we're not getting any
2617          indication save a negative return from ttrace_wait and an
2618          errno set to ESRCH?
2619        */
2620       if (errno == ESRCH)
2621         {
2622           *status = 0;          /* WIFEXITED */
2623           return inferior_pid;
2624         }
2625
2626       warning ("Call of ttrace_wait returned with errno %d.",
2627                errno);
2628       *status = ttwait_return;
2629       return inferior_pid;
2630     }
2631
2632   real_pid = tsp.tts_pid;
2633   real_tid = tsp.tts_lwpid;
2634
2635   /* One complication is that the "tts_event" structure has
2636    * a set of flags, and more than one can be set.  So we
2637    * either have to force an order (as we do here), or handle
2638    * more than one flag at a time.
2639    */
2640   if (tsp.tts_event & TTEVT_LWP_CREATE)
2641     {
2642
2643       /* Unlike what you might expect, this event is reported in
2644        * the _creating_ thread, and the _created_ thread (whose tid
2645        * we have) is still running.  So we have to stop it.  This
2646        * has already been done in "call_ttrace_wait", but should we
2647        * ever abandon the "stop-the-world" model, here's the command
2648        * to use:
2649        *
2650        *    call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2651        *
2652        * Note that this would depend on being called _after_ "add_tthread"
2653        * below for the tid-to-pid translation to be done in "call_ttrace".
2654        */
2655
2656 #ifdef THREAD_DEBUG
2657       if (debug_on)
2658         printf ("New thread: pid %d, tid %d, creator tid %d\n",
2659                 real_pid, tsp.tts_u.tts_thread.tts_target_lwpid,
2660                 real_tid);
2661 #endif
2662
2663       /* Now we have to return the tid of the created thread, not
2664        * the creating thread, or "wait_for_inferior" won't know we
2665        * have a new "process" (thread).  Plus we should record it
2666        * right, too.
2667        */
2668       real_tid = tsp.tts_u.tts_thread.tts_target_lwpid;
2669
2670       add_tthread (real_pid, real_tid);
2671     }
2672
2673   else if ((tsp.tts_event & TTEVT_LWP_TERMINATE)
2674            || (tsp.tts_event & TTEVT_LWP_EXIT))
2675     {
2676
2677 #ifdef THREAD_DEBUG
2678       if (debug_on)
2679         printf ("Thread dies: %d\n", real_tid);
2680 #endif
2681
2682       del_tthread (real_tid);
2683     }
2684
2685   else if (tsp.tts_event & TTEVT_EXEC)
2686     {
2687
2688 #ifdef THREAD_DEBUG
2689       if (debug_on)
2690         printf ("Pid %d has zero'th thread %d; inferior pid is %d\n",
2691                 real_pid, real_tid, inferior_pid);
2692 #endif
2693
2694       add_tthread (real_pid, real_tid);
2695     }
2696
2697 #ifdef THREAD_DEBUG
2698   else if (debug_on)
2699     {
2700       printf ("Process-level event %s, using tid %d\n",
2701               get_printable_name_of_ttrace_event (tsp.tts_event),
2702               real_tid);
2703
2704       /* OK to do this, as "add_tthread" won't add
2705        * duplicate entries.  Also OK not to do it,
2706        * as this event isn't one which can change the
2707        * thread state.
2708        */
2709       add_tthread (real_pid, real_tid);
2710     }
2711 #endif
2712
2713
2714   /* How many events are left to report later?
2715    * In a non-stop-the-world model, this isn't needed.
2716    *
2717    * Note that it's not always safe to query the thread state of a process,
2718    * which is what count_unhandled_events does.  (If unsafe, we're left with
2719    * no other resort than to assume that no more events remain...)
2720    */
2721   if (can_touch_threads_of_process (real_pid, tsp.tts_event))
2722     more_events_left = count_unhandled_events (real_pid, real_tid);
2723
2724   else
2725     {
2726       if (more_events_left > 0)
2727         warning ("Vfork or fork causing loss of %d buffered events.",
2728                  more_events_left);
2729
2730       more_events_left = 0;
2731     }
2732
2733   /* Attempt to translate the ttrace_wait-returned status into the
2734      ptrace equivalent.
2735
2736      ??rehrauer: This is somewhat fragile.  We really ought to rewrite
2737      clients that expect to pick apart a ptrace wait status, to use
2738      something a little more abstract.
2739    */
2740   if ((tsp.tts_event & TTEVT_EXEC)
2741       || (tsp.tts_event & TTEVT_FORK)
2742       || (tsp.tts_event & TTEVT_VFORK))
2743     {
2744       /* Forks come in pairs (parent and child), so core gdb
2745        * will do two waits.  Be ready to notice this.
2746        */
2747       if (tsp.tts_event & TTEVT_FORK)
2748         {
2749           process_state = FORKING;
2750
2751 #ifdef WAIT_BUFFER_DEBUG
2752           if (debug_on)
2753             printf ("Process set to FORKING\n");
2754 #endif
2755         }
2756       else if (tsp.tts_event & TTEVT_VFORK)
2757         {
2758           process_state = VFORKING;
2759
2760 #ifdef WAIT_BUFFER_DEBUG
2761           if (debug_on)
2762             printf ("Process set to VFORKING\n");
2763 #endif
2764         }
2765
2766       /* Make an exec or fork look like a breakpoint.  Definitely a hack,
2767          but I don't think non HP-UX-specific clients really carefully
2768          inspect the first events they get after inferior startup, so
2769          it probably almost doesn't matter what we claim this is.
2770        */
2771
2772 #ifdef THREAD_DEBUG
2773       if (debug_on)
2774         printf ("..a process 'event'\n");
2775 #endif
2776
2777       /* Also make fork and exec events look like bpts, so they can be caught.
2778        */
2779       *status = 0177 | (_SIGTRAP << 8);
2780     }
2781
2782   /* Special-cases: We ask for syscall entry and exit events to implement
2783      "fast" (aka "hardware") watchpoints.
2784
2785      When we get a syscall entry, we want to disable page-protections,
2786      and resume the inferior; this isn't an event we wish for
2787      wait_for_inferior to see.  Note that we must resume ONLY the
2788      thread that reported the syscall entry; we don't want to allow
2789      other threads to run with the page protections off, as they might
2790      then be able to write to watch memory without it being caught.
2791
2792      When we get a syscall exit, we want to reenable page-protections,
2793      but we don't want to resume the inferior; this is an event we wish
2794      wait_for_inferior to see.  Make it look like the signal we normally
2795      get for a single-step completion.  This should cause wait_for_inferior
2796      to evaluate whether any watchpoint triggered.
2797
2798      Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2799      due to some HP-UX "features".  Some syscalls have problems with
2800      write-protections on some pages, and some syscalls seem to have
2801      pending writes to those pages at the time we're getting the return
2802      event.  So, we'll single-step the inferior to get out of the syscall,
2803      and then reenable protections.
2804
2805      Note that we're intentionally allowing the syscall exit case to
2806      fall through into the succeeding cases, as sometimes we single-
2807      step out of one syscall only to immediately enter another...
2808    */
2809   else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY)
2810            || (tsp.tts_event & TTEVT_SYSCALL_RETURN))
2811     {
2812       /* Make a syscall event look like a breakpoint.  Same comments
2813          as for exec & fork events.
2814        */
2815 #ifdef THREAD_DEBUG
2816       if (debug_on)
2817         printf ("..a syscall 'event'\n");
2818 #endif
2819
2820       /* Also make syscall events look like bpts, so they can be caught.
2821        */
2822       *status = 0177 | (_SIGTRAP << 8);
2823     }
2824
2825   else if ((tsp.tts_event & TTEVT_LWP_CREATE)
2826            || (tsp.tts_event & TTEVT_LWP_TERMINATE)
2827            || (tsp.tts_event & TTEVT_LWP_EXIT))
2828     {
2829       /* Make a thread event look like a breakpoint.  Same comments
2830        * as for exec & fork events.
2831        */
2832 #ifdef THREAD_DEBUG
2833       if (debug_on)
2834         printf ("..a thread 'event'\n");
2835 #endif
2836
2837       /* Also make thread events look like bpts, so they can be caught.
2838        */
2839       *status = 0177 | (_SIGTRAP << 8);
2840     }
2841
2842   else if ((tsp.tts_event & TTEVT_EXIT))
2843     {                           /* WIFEXITED */
2844
2845 #ifdef THREAD_DEBUG
2846       if (debug_on)
2847         printf ("..an exit\n");
2848 #endif
2849
2850       /* Prevent rest of gdb from thinking this is
2851        * a new thread if for some reason it's never
2852        * seen the main thread before.
2853        */
2854       inferior_pid = map_to_gdb_tid (real_tid);         /* HACK, FIX */
2855
2856       *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode);
2857     }
2858
2859   else if (tsp.tts_event & TTEVT_SIGNAL)
2860     {                           /* WIFSTOPPED */
2861 #ifdef THREAD_DEBUG
2862       if (debug_on)
2863         printf ("..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo);
2864 #endif
2865
2866       *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8);
2867     }
2868
2869   else
2870     {                           /* !WIFSTOPPED */
2871
2872       /* This means the process or thread terminated.  But we should've
2873          caught an explicit exit/termination above.  So warn (this is
2874          really an internal error) and claim the process or thread
2875          terminated with a SIGTRAP.
2876        */
2877
2878       warning ("process_wait: unknown process state");
2879
2880 #ifdef THREAD_DEBUG
2881       if (debug_on)
2882         printf ("Process-level event %s, using tid %d\n",
2883                 get_printable_name_of_ttrace_event (tsp.tts_event),
2884                 real_tid);
2885 #endif
2886
2887       *status = _SIGTRAP;
2888     }
2889
2890   target_post_wait (tsp.tts_pid, *status);
2891
2892
2893 #ifdef THREAD_DEBUG
2894   if (debug_on)
2895     printf ("Done waiting, pid is %d, tid %d\n", real_pid, real_tid);
2896 #endif
2897
2898   /* All code external to this module uses the tid, but calls
2899    * it "pid".  There's some tweaking so that the outside sees
2900    * the first thread as having the same number as the starting
2901    * pid.
2902    */
2903   return_pid = map_to_gdb_tid (real_tid);
2904
2905   /* Remember this for later use in "hppa_prepare_to_proceed".
2906    */
2907   old_gdb_pid = inferior_pid;
2908   reported_pid = return_pid;
2909   reported_bpt = ((tsp.tts_event & TTEVT_SIGNAL) && (5 == tsp.tts_u.tts_signal.tts_signo));
2910
2911   if (real_tid == 0 || return_pid == 0)
2912     {
2913       warning ("Internal error: process-wait failed.");
2914     }
2915
2916   return return_pid;
2917 }
2918 \f
2919
2920 /* This function causes the caller's process to be traced by its
2921    parent.  This is intended to be called after GDB forks itself,
2922    and before the child execs the target.  Despite the name, it
2923    is called by the child.
2924
2925    Note that HP-UX ttrace is rather funky in how this is done.
2926    If the parent wants to get the initial exec event of a child,
2927    it must set the ttrace event mask of the child to include execs.
2928    (The child cannot do this itself.)  This must be done after the
2929    child is forked, but before it execs.
2930
2931    To coordinate the parent and child, we implement a semaphore using
2932    pipes.  After SETTRC'ing itself, the child tells the parent that
2933    it is now traceable by the parent, and waits for the parent's
2934    acknowledgement.  The parent can then set the child's event mask,
2935    and notify the child that it can now exec.
2936
2937    (The acknowledgement by parent happens as a result of a call to
2938    child_acknowledge_created_inferior.)
2939  */
2940 int
2941 parent_attach_all (void)
2942 {
2943   int tt_status;
2944
2945   /* We need a memory home for a constant, to pass it to ttrace.
2946      The value of the constant is arbitrary, so long as both
2947      parent and child use the same value.  Might as well use the
2948      "magic" constant provided by ttrace...
2949    */
2950   uint64_t tc_magic_child = TT_VERSION;
2951   uint64_t tc_magic_parent = 0;
2952
2953   tt_status = call_real_ttrace (
2954                                  TT_PROC_SETTRC,
2955                                  (int) TT_NIL,
2956                                  (lwpid_t) TT_NIL,
2957                                  TT_NIL,
2958                                  (TTRACE_ARG_TYPE) TT_VERSION,
2959                                  TT_NIL);
2960
2961   if (tt_status < 0)
2962     return tt_status;
2963
2964   /* Notify the parent that we're potentially ready to exec(). */
2965   write (startup_semaphore.child_channel[SEM_TALK],
2966          &tc_magic_child,
2967          sizeof (tc_magic_child));
2968
2969   /* Wait for acknowledgement from the parent. */
2970   read (startup_semaphore.parent_channel[SEM_LISTEN],
2971         &tc_magic_parent,
2972         sizeof (tc_magic_parent));
2973
2974   if (tc_magic_child != tc_magic_parent)
2975     warning ("mismatched semaphore magic");
2976
2977   /* Discard our copy of the semaphore. */
2978   (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
2979   (void) close (startup_semaphore.parent_channel[SEM_TALK]);
2980   (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
2981   (void) close (startup_semaphore.child_channel[SEM_TALK]);
2982
2983   return tt_status;
2984 }
2985
2986 /* Despite being file-local, this routine is dealing with
2987  * actual process IDs, not thread ids.  That's because it's
2988  * called before the first "wait" call, and there's no map
2989  * yet from tids to pids.
2990  *
2991  * When it is called, a forked child is running, but waiting on
2992  * the semaphore.  If you stop the child and re-start it,
2993  * things get confused, so don't do that!  An attached child is
2994  * stopped.
2995  *
2996  * Since this is called after either attach or run, we
2997  * have to be the common part of both.
2998  */
2999 static void
3000 require_notification_of_events (int real_pid)
3001 {
3002   int tt_status;
3003   ttevent_t notifiable_events;
3004
3005   lwpid_t tid;
3006   ttstate_t thread_state;
3007
3008 #ifdef THREAD_DEBUG
3009   if (debug_on)
3010     printf ("Require notif, pid is %d\n", real_pid);
3011 #endif
3012
3013   /* Temporary HACK: tell inftarg.c/child_wait to not
3014    * loop until pids are the same.
3015    */
3016   not_same_real_pid = 0;
3017
3018   sigemptyset (&notifiable_events.tte_signals);
3019   notifiable_events.tte_opts = TTEO_NONE;
3020
3021   /* This ensures that forked children inherit their parent's
3022    * event mask, which we're setting here.
3023    *
3024    * NOTE: if you debug gdb with itself, then the ultimate
3025    *       debuggee gets flags set by the outermost gdb, as
3026    *       a child of a child will still inherit.
3027    */
3028   notifiable_events.tte_opts |= TTEO_PROC_INHERIT;
3029
3030   notifiable_events.tte_events = TTEVT_DEFAULT;
3031   notifiable_events.tte_events |= TTEVT_SIGNAL;
3032   notifiable_events.tte_events |= TTEVT_EXEC;
3033   notifiable_events.tte_events |= TTEVT_EXIT;
3034   notifiable_events.tte_events |= TTEVT_FORK;
3035   notifiable_events.tte_events |= TTEVT_VFORK;
3036   notifiable_events.tte_events |= TTEVT_LWP_CREATE;
3037   notifiable_events.tte_events |= TTEVT_LWP_EXIT;
3038   notifiable_events.tte_events |= TTEVT_LWP_TERMINATE;
3039
3040   tt_status = call_real_ttrace (
3041                                  TT_PROC_SET_EVENT_MASK,
3042                                  real_pid,
3043                                  (lwpid_t) TT_NIL,
3044                                  (TTRACE_ARG_TYPE) & notifiable_events,
3045                                (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3046                                  TT_NIL);
3047 }
3048
3049 static void
3050 require_notification_of_exec_events (int real_pid)
3051 {
3052   int tt_status;
3053   ttevent_t notifiable_events;
3054
3055   lwpid_t tid;
3056   ttstate_t thread_state;
3057
3058 #ifdef THREAD_DEBUG
3059   if (debug_on)
3060     printf ("Require notif, pid is %d\n", real_pid);
3061 #endif
3062
3063   /* Temporary HACK: tell inftarg.c/child_wait to not
3064    * loop until pids are the same.
3065    */
3066   not_same_real_pid = 0;
3067
3068   sigemptyset (&notifiable_events.tte_signals);
3069   notifiable_events.tte_opts = TTEO_NOSTRCCHLD;
3070
3071   /* This ensures that forked children don't inherit their parent's
3072    * event mask, which we're setting here.
3073    */
3074   notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT;
3075
3076   notifiable_events.tte_events = TTEVT_DEFAULT;
3077   notifiable_events.tte_events |= TTEVT_EXEC;
3078   notifiable_events.tte_events |= TTEVT_EXIT;
3079
3080   tt_status = call_real_ttrace (
3081                                  TT_PROC_SET_EVENT_MASK,
3082                                  real_pid,
3083                                  (lwpid_t) TT_NIL,
3084                                  (TTRACE_ARG_TYPE) & notifiable_events,
3085                                (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3086                                  TT_NIL);
3087 }
3088 \f
3089
3090 /* This function is called by the parent process, with pid being the
3091  * ID of the child process, after the debugger has forked.
3092  */
3093 void
3094 child_acknowledge_created_inferior (int pid)
3095 {
3096   /* We need a memory home for a constant, to pass it to ttrace.
3097      The value of the constant is arbitrary, so long as both
3098      parent and child use the same value.  Might as well use the
3099      "magic" constant provided by ttrace...
3100    */
3101   uint64_t tc_magic_parent = TT_VERSION;
3102   uint64_t tc_magic_child = 0;
3103
3104   /* Wait for the child to tell us that it has forked. */
3105   read (startup_semaphore.child_channel[SEM_LISTEN],
3106         &tc_magic_child,
3107         sizeof (tc_magic_child));
3108
3109   /* Clear thread info now.  We'd like to do this in
3110    * "require...", but that messes up attach.
3111    */
3112   clear_thread_info ();
3113
3114   /* Tell the "rest of gdb" that the initial thread exists.
3115    * This isn't really a hack.  Other thread-based versions
3116    * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3117    *
3118    * Q: Why don't we also add this thread to the local
3119    *    list via "add_tthread"?
3120    *
3121    * A: Because we don't know the tid, and can't stop the
3122    *    the process safely to ask what it is.  Anyway, we'll
3123    *    add it when it gets the EXEC event.
3124    */
3125   add_thread (pid);             /* in thread.c */
3126
3127   /* We can now set the child's ttrace event mask.
3128    */
3129   require_notification_of_exec_events (pid);
3130
3131   /* Tell ourselves that the process is running.
3132    */
3133   process_state = RUNNING;
3134
3135   /* Notify the child that it can exec. */
3136   write (startup_semaphore.parent_channel[SEM_TALK],
3137          &tc_magic_parent,
3138          sizeof (tc_magic_parent));
3139
3140   /* Discard our copy of the semaphore. */
3141   (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3142   (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3143   (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3144   (void) close (startup_semaphore.child_channel[SEM_TALK]);
3145 }
3146
3147
3148 /*
3149  * arrange for notification of all events by
3150  * calling require_notification_of_events.
3151  */
3152 void
3153 child_post_startup_inferior (int real_pid)
3154 {
3155   require_notification_of_events (real_pid);
3156 }
3157
3158 /* From here on, we should expect tids rather than pids.
3159  */
3160 static void
3161 hppa_enable_catch_fork (int tid)
3162 {
3163   int tt_status;
3164   ttevent_t ttrace_events;
3165
3166   /* Get the set of events that are currently enabled.
3167    */
3168   tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3169                            tid,
3170                            (TTRACE_ARG_TYPE) & ttrace_events,
3171                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3172                            TT_NIL);
3173   if (errno)
3174     perror_with_name ("ttrace");
3175
3176   /* Add forks to that set. */
3177   ttrace_events.tte_events |= TTEVT_FORK;
3178
3179 #ifdef THREAD_DEBUG
3180   if (debug_on)
3181     printf ("enable fork, tid is %d\n", tid);
3182 #endif
3183
3184   tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3185                            tid,
3186                            (TTRACE_ARG_TYPE) & ttrace_events,
3187                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3188                            TT_NIL);
3189   if (errno)
3190     perror_with_name ("ttrace");
3191 }
3192
3193
3194 static void
3195 hppa_disable_catch_fork (int tid)
3196 {
3197   int tt_status;
3198   ttevent_t ttrace_events;
3199
3200   /* Get the set of events that are currently enabled.
3201    */
3202   tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3203                            tid,
3204                            (TTRACE_ARG_TYPE) & ttrace_events,
3205                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3206                            TT_NIL);
3207
3208   if (errno)
3209     perror_with_name ("ttrace");
3210
3211   /* Remove forks from that set. */
3212   ttrace_events.tte_events &= ~TTEVT_FORK;
3213
3214 #ifdef THREAD_DEBUG
3215   if (debug_on)
3216     printf ("disable fork, tid is %d\n", tid);
3217 #endif
3218
3219   tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3220                            tid,
3221                            (TTRACE_ARG_TYPE) & ttrace_events,
3222                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3223                            TT_NIL);
3224
3225   if (errno)
3226     perror_with_name ("ttrace");
3227 }
3228
3229
3230 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3231 int
3232 child_insert_fork_catchpoint (int tid)
3233 {
3234   /* Enable reporting of fork events from the kernel. */
3235   /* ??rehrauer: For the moment, we're always enabling these events,
3236      and just ignoring them if there's no catchpoint to catch them.
3237    */
3238   return 0;
3239 }
3240 #endif
3241
3242
3243 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3244 int
3245 child_remove_fork_catchpoint (int tid)
3246 {
3247   /* Disable reporting of fork events from the kernel. */
3248   /* ??rehrauer: For the moment, we're always enabling these events,
3249      and just ignoring them if there's no catchpoint to catch them.
3250    */
3251   return 0;
3252 }
3253 #endif
3254
3255
3256 static void
3257 hppa_enable_catch_vfork (int tid)
3258 {
3259   int tt_status;
3260   ttevent_t ttrace_events;
3261
3262   /* Get the set of events that are currently enabled.
3263    */
3264   tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3265                            tid,
3266                            (TTRACE_ARG_TYPE) & ttrace_events,
3267                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3268                            TT_NIL);
3269
3270   if (errno)
3271     perror_with_name ("ttrace");
3272
3273   /* Add vforks to that set. */
3274   ttrace_events.tte_events |= TTEVT_VFORK;
3275
3276 #ifdef THREAD_DEBUG
3277   if (debug_on)
3278     printf ("enable vfork, tid is %d\n", tid);
3279 #endif
3280
3281   tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3282                            tid,
3283                            (TTRACE_ARG_TYPE) & ttrace_events,
3284                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3285                            TT_NIL);
3286
3287   if (errno)
3288     perror_with_name ("ttrace");
3289 }
3290
3291
3292 static void
3293 hppa_disable_catch_vfork (int tid)
3294 {
3295   int tt_status;
3296   ttevent_t ttrace_events;
3297
3298   /* Get the set of events that are currently enabled. */
3299   tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3300                            tid,
3301                            (TTRACE_ARG_TYPE) & ttrace_events,
3302                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3303                            TT_NIL);
3304
3305   if (errno)
3306     perror_with_name ("ttrace");
3307
3308   /* Remove vforks from that set. */
3309   ttrace_events.tte_events &= ~TTEVT_VFORK;
3310
3311 #ifdef THREAD_DEBUG
3312   if (debug_on)
3313     printf ("disable vfork, tid is %d\n", tid);
3314 #endif
3315   tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3316                            tid,
3317                            (TTRACE_ARG_TYPE) & ttrace_events,
3318                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3319                            TT_NIL);
3320
3321   if (errno)
3322     perror_with_name ("ttrace");
3323 }
3324
3325
3326 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3327 int
3328 child_insert_vfork_catchpoint (int tid)
3329 {
3330   /* Enable reporting of vfork events from the kernel. */
3331   /* ??rehrauer: For the moment, we're always enabling these events,
3332      and just ignoring them if there's no catchpoint to catch them.
3333    */
3334   return 0;
3335 }
3336 #endif
3337
3338
3339 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3340 int
3341 child_remove_vfork_catchpoint (int tid)
3342 {
3343   /* Disable reporting of vfork events from the kernel. */
3344   /* ??rehrauer: For the moment, we're always enabling these events,
3345      and just ignoring them if there's no catchpoint to catch them.
3346    */
3347   return 0;
3348 }
3349 #endif
3350
3351 #if defined(CHILD_HAS_FORKED)
3352
3353 /* Q: Do we need to map the returned process ID to a thread ID?
3354
3355  * A: I don't think so--here we want a _real_ pid.  Any later
3356  *    operations will call "require_notification_of_events" and
3357  *    start the mapping.
3358  */
3359 int
3360 child_has_forked (int tid, int *childpid)
3361 {
3362   int tt_status;
3363   ttstate_t ttrace_state;
3364   thread_info *tinfo;
3365
3366   /* Do we have cached thread state that we can consult?  If so, use it. */
3367   tinfo = find_thread_info (map_from_gdb_tid (tid));
3368   if (tinfo != NULL)
3369     {
3370       copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3371     }
3372
3373   /* Nope, must read the thread's current state */
3374   else
3375     {
3376       tt_status = call_ttrace (TT_LWP_GET_STATE,
3377                                tid,
3378                                (TTRACE_ARG_TYPE) & ttrace_state,
3379                                (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3380                                TT_NIL);
3381
3382       if (errno)
3383         perror_with_name ("ttrace");
3384
3385       if (tt_status < 0)
3386         return 0;
3387     }
3388
3389   if (ttrace_state.tts_event & TTEVT_FORK)
3390     {
3391       *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3392       return 1;
3393     }
3394
3395   return 0;
3396 }
3397 #endif
3398
3399
3400 #if defined(CHILD_HAS_VFORKED)
3401
3402 /* See child_has_forked for pid discussion.
3403  */
3404 int
3405 child_has_vforked (int tid, int *childpid)
3406 {
3407   int tt_status;
3408   ttstate_t ttrace_state;
3409   thread_info *tinfo;
3410
3411   /* Do we have cached thread state that we can consult?  If so, use it. */
3412   tinfo = find_thread_info (map_from_gdb_tid (tid));
3413   if (tinfo != NULL)
3414     copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3415
3416   /* Nope, must read the thread's current state */
3417   else
3418     {
3419       tt_status = call_ttrace (TT_LWP_GET_STATE,
3420                                tid,
3421                                (TTRACE_ARG_TYPE) & ttrace_state,
3422                                (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3423                                TT_NIL);
3424
3425       if (errno)
3426         perror_with_name ("ttrace");
3427
3428       if (tt_status < 0)
3429         return 0;
3430     }
3431
3432   if (ttrace_state.tts_event & TTEVT_VFORK)
3433     {
3434       *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3435       return 1;
3436     }
3437
3438   return 0;
3439 }
3440 #endif
3441
3442
3443 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3444 int
3445 child_can_follow_vfork_prior_to_exec (void)
3446 {
3447   /* ttrace does allow this.
3448
3449      ??rehrauer: However, I had major-league problems trying to
3450      convince wait_for_inferior to handle that case.  Perhaps when
3451      it is rewritten to grok multiple processes in an explicit way...
3452    */
3453   return 0;
3454 }
3455 #endif
3456
3457
3458 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3459 int
3460 child_insert_exec_catchpoint (int tid)
3461 {
3462   /* Enable reporting of exec events from the kernel. */
3463   /* ??rehrauer: For the moment, we're always enabling these events,
3464      and just ignoring them if there's no catchpoint to catch them.
3465    */
3466   return 0;
3467 }
3468 #endif
3469
3470
3471 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3472 int
3473 child_remove_exec_catchpoint (int tid)
3474 {
3475   /* Disable reporting of execevents from the kernel. */
3476   /* ??rehrauer: For the moment, we're always enabling these events,
3477      and just ignoring them if there's no catchpoint to catch them.
3478    */
3479   return 0;
3480 }
3481 #endif
3482
3483
3484 #if defined(CHILD_HAS_EXECD)
3485 int
3486 child_has_execd (int tid, char **execd_pathname)
3487 {
3488   int tt_status;
3489   ttstate_t ttrace_state;
3490   thread_info *tinfo;
3491
3492   /* Do we have cached thread state that we can consult?  If so, use it. */
3493   tinfo = find_thread_info (map_from_gdb_tid (tid));
3494   if (tinfo != NULL)
3495     copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3496
3497   /* Nope, must read the thread's current state */
3498   else
3499     {
3500       tt_status = call_ttrace (TT_LWP_GET_STATE,
3501                                tid,
3502                                (TTRACE_ARG_TYPE) & ttrace_state,
3503                                (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3504                                TT_NIL);
3505
3506       if (errno)
3507         perror_with_name ("ttrace");
3508
3509       if (tt_status < 0)
3510         return 0;
3511     }
3512
3513   if (ttrace_state.tts_event & TTEVT_EXEC)
3514     {
3515       /* See child_pid_to_exec_file in this file: this is a macro.
3516        */
3517       char *exec_file = target_pid_to_exec_file (tid);
3518
3519       *execd_pathname = savestring (exec_file, strlen (exec_file));
3520       return 1;
3521     }
3522
3523   return 0;
3524 }
3525 #endif
3526
3527
3528 #if defined(CHILD_HAS_SYSCALL_EVENT)
3529 int
3530 child_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
3531 {
3532   int tt_status;
3533   ttstate_t ttrace_state;
3534   thread_info *tinfo;
3535
3536   /* Do we have cached thread state that we can consult?  If so, use it. */
3537   tinfo = find_thread_info (map_from_gdb_tid (pid));
3538   if (tinfo != NULL)
3539     copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3540
3541   /* Nope, must read the thread's current state */
3542   else
3543     {
3544       tt_status = call_ttrace (TT_LWP_GET_STATE,
3545                                pid,
3546                                (TTRACE_ARG_TYPE) & ttrace_state,
3547                                (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3548                                TT_NIL);
3549
3550       if (errno)
3551         perror_with_name ("ttrace");
3552
3553       if (tt_status < 0)
3554         return 0;
3555     }
3556
3557   *kind = TARGET_WAITKIND_SPURIOUS;     /* Until proven otherwise... */
3558   *syscall_id = -1;
3559
3560   if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY)
3561     *kind = TARGET_WAITKIND_SYSCALL_ENTRY;
3562   else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN)
3563     *kind = TARGET_WAITKIND_SYSCALL_RETURN;
3564   else
3565     return 0;
3566
3567   *syscall_id = ttrace_state.tts_scno;
3568   return 1;
3569 }
3570 #endif
3571 \f
3572
3573
3574 #if defined(CHILD_THREAD_ALIVE)
3575
3576 /* Check to see if the given thread is alive.
3577
3578  * We'll trust the thread list, as the more correct
3579  * approach of stopping the process and spinning down
3580  * the OS's thread list is _very_ expensive.
3581  *
3582  * May need a FIXME for that reason.
3583  */
3584 int
3585 child_thread_alive (lwpid_t gdb_tid)
3586 {
3587   lwpid_t tid;
3588
3589   /* This spins down the lists twice.
3590    * Possible peformance improvement here!
3591    */
3592   tid = map_from_gdb_tid (gdb_tid);
3593   return !is_terminated (tid);
3594 }
3595
3596 #endif
3597 \f
3598
3599
3600 /* This function attempts to read the specified number of bytes from the
3601    save_state_t that is our view into the hardware registers, starting at
3602    ss_offset, and ending at ss_offset + sizeof_buf - 1
3603
3604    If this function succeeds, it deposits the fetched bytes into buf,
3605    and returns 0.
3606
3607    If it fails, it returns a negative result.  The contents of buf are
3608    undefined it this function fails.
3609  */
3610 int
3611 read_from_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf,
3612                                int sizeof_buf)
3613 {
3614   int tt_status;
3615   register_value_t register_value = 0;
3616
3617   tt_status = call_ttrace (TT_LWP_RUREGS,
3618                            tid,
3619                            ss_offset,
3620                            (TTRACE_ARG_TYPE) sizeof_buf,
3621                            (TTRACE_ARG_TYPE) buf);
3622
3623   if (tt_status == 1)
3624     /* Map ttrace's version of success to our version.
3625      * Sometime ttrace returns 0, but that's ok here.
3626      */
3627     return 0;
3628
3629   return tt_status;
3630 }
3631 \f
3632
3633 /* This function attempts to write the specified number of bytes to the
3634    save_state_t that is our view into the hardware registers, starting at
3635    ss_offset, and ending at ss_offset + sizeof_buf - 1
3636
3637    If this function succeeds, it deposits the bytes in buf, and returns 0.
3638
3639    If it fails, it returns a negative result.  The contents of the save_state_t
3640    are undefined it this function fails.
3641  */
3642 int
3643 write_to_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf,
3644                               int sizeof_buf)
3645 {
3646   int tt_status;
3647   register_value_t register_value = 0;
3648
3649   tt_status = call_ttrace (TT_LWP_WUREGS,
3650                            tid,
3651                            ss_offset,
3652                            (TTRACE_ARG_TYPE) sizeof_buf,
3653                            (TTRACE_ARG_TYPE) buf);
3654   return tt_status;
3655 }
3656 \f
3657
3658 /* This function is a sop to the largeish number of direct calls
3659    to call_ptrace that exist in other files.  Rather than create
3660    functions whose name abstracts away from ptrace, and change all
3661    the present callers of call_ptrace, we'll do the expedient (and
3662    perhaps only practical) thing.
3663
3664    Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3665    process.  Thus, we must translate all ptrace requests into their
3666    process-specific, ttrace equivalents.
3667  */
3668 int
3669 call_ptrace (int pt_request, int gdb_tid, PTRACE_ARG3_TYPE addr, int data)
3670 {
3671   ttreq_t tt_request;
3672   TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr;
3673   TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data;
3674   TTRACE_ARG_TYPE tt_addr2 = TT_NIL;
3675   int tt_status;
3676   register_value_t register_value;
3677   int read_buf;
3678
3679   /* Perform the necessary argument translation.  Note that some
3680      cases are funky enough in the ttrace realm that we handle them
3681      very specially.
3682    */
3683   switch (pt_request)
3684     {
3685       /* The following cases cannot conveniently be handled conveniently
3686          by merely adjusting the ptrace arguments and feeding into the
3687          generic call to ttrace at the bottom of this function.
3688
3689          Note that because all branches of this switch end in "return",
3690          there's no need for any "break" statements.
3691        */
3692     case PT_SETTRC:
3693       return parent_attach_all ();
3694
3695     case PT_RUREGS:
3696       tt_status = read_from_register_save_state (gdb_tid,
3697                                                  tt_addr,
3698                                                  &register_value,
3699                                                  sizeof (register_value));
3700       if (tt_status < 0)
3701         return tt_status;
3702       return register_value;
3703
3704     case PT_WUREGS:
3705       register_value = (int) tt_data;
3706       tt_status = write_to_register_save_state (gdb_tid,
3707                                                 tt_addr,
3708                                                 &register_value,
3709                                                 sizeof (register_value));
3710       return tt_status;
3711       break;
3712
3713     case PT_READ_I:
3714       tt_status = call_ttrace (TT_PROC_RDTEXT,  /* Implicit 4-byte xfer becomes block-xfer. */
3715                                gdb_tid,
3716                                tt_addr,
3717                                (TTRACE_ARG_TYPE) 4,
3718                                (TTRACE_ARG_TYPE) & read_buf);
3719       if (tt_status < 0)
3720         return tt_status;
3721       return read_buf;
3722
3723     case PT_READ_D:
3724       tt_status = call_ttrace (TT_PROC_RDDATA,  /* Implicit 4-byte xfer becomes block-xfer. */
3725                                gdb_tid,
3726                                tt_addr,
3727                                (TTRACE_ARG_TYPE) 4,
3728                                (TTRACE_ARG_TYPE) & read_buf);
3729       if (tt_status < 0)
3730         return tt_status;
3731       return read_buf;
3732
3733     case PT_ATTACH:
3734       tt_status = call_real_ttrace (TT_PROC_ATTACH,
3735                                     map_from_gdb_tid (gdb_tid),
3736                                     (lwpid_t) TT_NIL,
3737                                     tt_addr,
3738                                     (TTRACE_ARG_TYPE) TT_VERSION,
3739                                     tt_addr2);
3740       if (tt_status < 0)
3741         return tt_status;
3742       return tt_status;
3743
3744       /* The following cases are handled by merely adjusting the ptrace
3745          arguments and feeding into the generic call to ttrace.
3746        */
3747     case PT_DETACH:
3748       tt_request = TT_PROC_DETACH;
3749       break;
3750
3751     case PT_WRITE_I:
3752       tt_request = TT_PROC_WRTEXT;      /* Translates 4-byte xfer to block-xfer. */
3753       tt_data = 4;              /* This many bytes. */
3754       tt_addr2 = (TTRACE_ARG_TYPE) & data;      /* Address of xfer source. */
3755       break;
3756
3757     case PT_WRITE_D:
3758       tt_request = TT_PROC_WRDATA;      /* Translates 4-byte xfer to block-xfer. */
3759       tt_data = 4;              /* This many bytes. */
3760       tt_addr2 = (TTRACE_ARG_TYPE) & data;      /* Address of xfer source. */
3761       break;
3762
3763     case PT_RDTEXT:
3764       tt_request = TT_PROC_RDTEXT;
3765       break;
3766
3767     case PT_RDDATA:
3768       tt_request = TT_PROC_RDDATA;
3769       break;
3770
3771     case PT_WRTEXT:
3772       tt_request = TT_PROC_WRTEXT;
3773       break;
3774
3775     case PT_WRDATA:
3776       tt_request = TT_PROC_WRDATA;
3777       break;
3778
3779     case PT_CONTINUE:
3780       tt_request = TT_PROC_CONTINUE;
3781       break;
3782
3783     case PT_STEP:
3784       tt_request = TT_LWP_SINGLE;       /* Should not be making this request? */
3785       break;
3786
3787     case PT_KILL:
3788       tt_request = TT_PROC_EXIT;
3789       break;
3790
3791     case PT_GET_PROCESS_PATHNAME:
3792       tt_request = TT_PROC_GET_PATHNAME;
3793       break;
3794
3795     default:
3796       tt_request = pt_request;  /* Let ttrace be the one to complain. */
3797       break;
3798     }
3799
3800   return call_ttrace (tt_request,
3801                       gdb_tid,
3802                       tt_addr,
3803                       tt_data,
3804                       tt_addr2);
3805 }
3806
3807 /* Kill that pesky process!
3808  */
3809 void
3810 kill_inferior (void)
3811 {
3812   int tid;
3813   int wait_status;
3814   thread_info *t;
3815   thread_info **paranoia;
3816   int para_count, i;
3817
3818   if (inferior_pid == 0)
3819     return;
3820
3821   /* Walk the list of "threads", some of which are "pseudo threads",
3822      aka "processes".  For each that is NOT inferior_pid, stop it,
3823      and detach it.
3824
3825      You see, we may not have just a single process to kill.  If we're
3826      restarting or quitting or detaching just after the inferior has
3827      forked, then we've actually two processes to clean up.
3828
3829      But we can't just call target_mourn_inferior() for each, since that
3830      zaps the target vector.
3831    */
3832
3833   paranoia = (thread_info **) xmalloc (thread_head.count *
3834                                        sizeof (thread_info *));
3835   para_count = 0;
3836
3837   t = thread_head.head;
3838   while (t)
3839     {
3840
3841       paranoia[para_count] = t;
3842       for (i = 0; i < para_count; i++)
3843         {
3844           if (t->next == paranoia[i])
3845             {
3846               warning ("Bad data in gdb's thread data; repairing.");
3847               t->next = 0;
3848             }
3849         }
3850       para_count++;
3851
3852       if (t->am_pseudo && (t->pid != inferior_pid))
3853         {
3854           /* TT_PROC_STOP doesn't require a subsequent ttrace_wait, as it
3855            * generates no event.
3856            */
3857           call_ttrace (TT_PROC_STOP,
3858                        t->pid,
3859                        TT_NIL,
3860                        TT_NIL,
3861                        TT_NIL);
3862
3863           call_ttrace (TT_PROC_DETACH,
3864                        t->pid,
3865                        TT_NIL,
3866                        (TTRACE_ARG_TYPE) TARGET_SIGNAL_0,
3867                        TT_NIL);
3868         }
3869       t = t->next;
3870     }
3871
3872   xfree (paranoia);
3873
3874   call_ttrace (TT_PROC_STOP,
3875                inferior_pid,
3876                TT_NIL,
3877                TT_NIL,
3878                TT_NIL);
3879   target_mourn_inferior ();
3880   clear_thread_info ();
3881 }
3882
3883
3884 #ifndef CHILD_RESUME
3885
3886 /* Sanity check a thread about to be continued.
3887  */
3888 static void
3889 thread_dropping_event_check (thread_info *p)
3890 {
3891   if (!p->handled)
3892     {
3893       /*
3894        * This seems to happen when we "next" over a
3895        * "fork()" while following the parent.  If it's
3896        * the FORK event, that's ok.  If it's a SIGNAL
3897        * in the unfollowed child, that's ok to--but
3898        * how can we know that's what's going on?
3899        *
3900        * FIXME!
3901        */
3902       if (p->have_state)
3903         {
3904           if (p->last_stop_state.tts_event == TTEVT_FORK)
3905             {
3906               /* Ok */
3907               ;
3908             }
3909           else if (p->last_stop_state.tts_event == TTEVT_SIGNAL)
3910             {
3911               /* Ok, close eyes and let it happen.
3912                */
3913               ;
3914             }
3915           else
3916             {
3917               /* This shouldn't happen--we're dropping a
3918                * real event.
3919                */
3920               warning ("About to continue process %d, thread %d with unhandled event %s.",
3921                        p->pid, p->tid,
3922                        get_printable_name_of_ttrace_event (
3923                                              p->last_stop_state.tts_event));
3924
3925 #ifdef PARANOIA
3926               if (debug_on)
3927                 print_tthread (p);
3928 #endif
3929             }
3930         }
3931       else
3932         {
3933           /* No saved state, have to assume it failed.
3934            */
3935           warning ("About to continue process %d, thread %d with unhandled event.",
3936                    p->pid, p->tid);
3937 #ifdef PARANOIA
3938           if (debug_on)
3939             print_tthread (p);
3940 #endif
3941         }
3942     }
3943
3944 }                               /* thread_dropping_event_check */
3945
3946 /* Use a loop over the threads to continue all the threads but
3947  * the one specified, which is to be stepped.
3948  */
3949 static void
3950 threads_continue_all_but_one (lwpid_t gdb_tid, int signal)
3951 {
3952   thread_info *p;
3953   int thread_signal;
3954   lwpid_t real_tid;
3955   lwpid_t scan_tid;
3956   ttstate_t state;
3957   int real_pid;
3958
3959 #ifdef THREAD_DEBUG
3960   if (debug_on)
3961     printf ("Using loop over threads to step/resume with signals\n");
3962 #endif
3963
3964   /* First update the thread list.
3965    */
3966   set_all_unseen ();
3967   real_tid = map_from_gdb_tid (gdb_tid);
3968   real_pid = get_pid_for (real_tid);
3969
3970   scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
3971   while (0 != scan_tid)
3972     {
3973
3974 #ifdef THREAD_DEBUG
3975       /* FIX: later should check state is stopped;
3976        * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
3977        */
3978       if (debug_on)
3979         if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
3980           printf ("About to continue non-stopped thread %d\n", scan_tid);
3981 #endif
3982
3983       p = find_thread_info (scan_tid);
3984       if (NULL == p)
3985         {
3986           add_tthread (real_pid, scan_tid);
3987           p = find_thread_info (scan_tid);
3988
3989           /* This is either a newly-created thread or the
3990            * result of a fork; in either case there's no
3991            * actual event to worry about.
3992            */
3993           p->handled = 1;
3994
3995           if (state.tts_event != TTEVT_NONE)
3996             {
3997               /* Oops, do need to worry!
3998                */
3999               warning ("Unexpected thread with \"%s\" event.",
4000                        get_printable_name_of_ttrace_event (state.tts_event));
4001             }
4002         }
4003       else if (scan_tid != p->tid)
4004         error ("Bad data in thread database.");
4005
4006 #ifdef THREAD_DEBUG
4007       if (debug_on)
4008         if (p->terminated)
4009           printf ("Why are we continuing a dead thread?\n");
4010 #endif
4011
4012       p->seen = 1;
4013
4014       scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4015     }
4016
4017   /* Remove unseen threads.
4018    */
4019   update_thread_list ();
4020
4021   /* Now run down the thread list and continue or step.
4022    */
4023   for (p = thread_head.head; p; p = p->next)
4024     {
4025
4026       /* Sanity check.
4027        */
4028       thread_dropping_event_check (p);
4029
4030       /* Pass the correct signals along.
4031        */
4032       if (p->have_signal)
4033         {
4034           thread_signal = p->signal_value;
4035           p->have_signal = 0;
4036         }
4037       else
4038         thread_signal = 0;
4039
4040       if (p->tid != real_tid)
4041         {
4042           /*
4043            * Not the thread of interest, so continue it
4044            * as the user expects.
4045            */
4046           if (p->stepping_mode == DO_STEP)
4047             {
4048               /* Just step this thread.
4049                */
4050               call_ttrace (
4051                             TT_LWP_SINGLE,
4052                             p->tid,
4053                             TT_USE_CURRENT_PC,
4054                             (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4055                             TT_NIL);
4056             }
4057           else
4058             {
4059               /* Regular continue (default case).
4060                */
4061               call_ttrace (
4062                             TT_LWP_CONTINUE,
4063                             p->tid,
4064                             TT_USE_CURRENT_PC,
4065                     (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4066                             TT_NIL);
4067             }
4068         }
4069       else
4070         {
4071           /* Step the thread of interest.
4072            */
4073           call_ttrace (
4074                         TT_LWP_SINGLE,
4075                         real_tid,
4076                         TT_USE_CURRENT_PC,
4077                         (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4078                         TT_NIL);
4079         }
4080     }                           /* Loop over threads */
4081 }                               /* End threads_continue_all_but_one */
4082
4083 /* Use a loop over the threads to continue all the threads.
4084  * This is done when a signal must be sent to any of the threads.
4085  */
4086 static void
4087 threads_continue_all_with_signals (lwpid_t gdb_tid, int signal)
4088 {
4089   thread_info *p;
4090   int thread_signal;
4091   lwpid_t real_tid;
4092   lwpid_t scan_tid;
4093   ttstate_t state;
4094   int real_pid;
4095
4096 #ifdef THREAD_DEBUG
4097   if (debug_on)
4098     printf ("Using loop over threads to resume with signals\n");
4099 #endif
4100
4101   /* Scan and update thread list.
4102    */
4103   set_all_unseen ();
4104   real_tid = map_from_gdb_tid (gdb_tid);
4105   real_pid = get_pid_for (real_tid);
4106
4107   scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
4108   while (0 != scan_tid)
4109     {
4110
4111 #ifdef THREAD_DEBUG
4112       if (debug_on)
4113         if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
4114           warning ("About to continue non-stopped thread %d\n", scan_tid);
4115 #endif
4116
4117       p = find_thread_info (scan_tid);
4118       if (NULL == p)
4119         {
4120           add_tthread (real_pid, scan_tid);
4121           p = find_thread_info (scan_tid);
4122
4123           /* This is either a newly-created thread or the
4124            * result of a fork; in either case there's no
4125            * actual event to worry about.
4126            */
4127           p->handled = 1;
4128
4129           if (state.tts_event != TTEVT_NONE)
4130             {
4131               /* Oops, do need to worry!
4132                */
4133               warning ("Unexpected thread with \"%s\" event.",
4134                        get_printable_name_of_ttrace_event (state.tts_event));
4135             }
4136         }
4137
4138 #ifdef THREAD_DEBUG
4139       if (debug_on)
4140         if (p->terminated)
4141           printf ("Why are we continuing a dead thread? (1)\n");
4142 #endif
4143
4144       p->seen = 1;
4145
4146       scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4147     }
4148
4149   /* Remove unseen threads from our list.
4150    */
4151   update_thread_list ();
4152
4153   /* Continue the threads.
4154    */
4155   for (p = thread_head.head; p; p = p->next)
4156     {
4157
4158       /* Sanity check.
4159        */
4160       thread_dropping_event_check (p);
4161
4162       /* Pass the correct signals along.
4163        */
4164       if (p->tid == real_tid)
4165         {
4166           thread_signal = signal;
4167           p->have_signal = 0;
4168         }
4169       else if (p->have_signal)
4170         {
4171           thread_signal = p->signal_value;
4172           p->have_signal = 0;
4173         }
4174       else
4175         thread_signal = 0;
4176
4177       if (p->stepping_mode == DO_STEP)
4178         {
4179           call_ttrace (
4180                         TT_LWP_SINGLE,
4181                         p->tid,
4182                         TT_USE_CURRENT_PC,
4183                         (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4184                         TT_NIL);
4185         }
4186       else
4187         {
4188           /* Continue this thread (default case).
4189            */
4190           call_ttrace (
4191                         TT_LWP_CONTINUE,
4192                         p->tid,
4193                         TT_USE_CURRENT_PC,
4194                     (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4195                         TT_NIL);
4196         }
4197     }
4198 }                               /* End threads_continue_all_with_signals */
4199
4200 /* Step one thread only.  
4201  */
4202 static void
4203 thread_fake_step (lwpid_t tid, enum target_signal signal)
4204 {
4205   thread_info *p;
4206
4207 #ifdef THREAD_DEBUG
4208   if (debug_on)
4209     {
4210       printf ("Doing a fake-step over a bpt, etc. for %d\n", tid);
4211
4212       if (is_terminated (tid))
4213         printf ("Why are we continuing a dead thread? (4)\n");
4214     }
4215 #endif
4216
4217   if (doing_fake_step)
4218     warning ("Step while step already in progress.");
4219
4220   /* See if there's a saved signal value for this
4221    * thread to be passed on, but no current signal.
4222    */
4223   p = find_thread_info (tid);
4224   if (p != NULL)
4225     {
4226       if (p->have_signal && signal == TARGET_SIGNAL_0)
4227         {
4228           /* Pass on a saved signal.
4229            */
4230           signal = p->signal_value;
4231         }
4232
4233       p->have_signal = 0;
4234     }
4235
4236   if (!p->handled)
4237     warning ("Internal error: continuing unhandled thread.");
4238
4239   call_ttrace (TT_LWP_SINGLE,
4240                tid,
4241                TT_USE_CURRENT_PC,
4242                (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4243                TT_NIL);
4244
4245   /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4246    * for this thread only, and clear any saved signal info.
4247    */
4248   doing_fake_step = 1;
4249   fake_step_tid = tid;
4250
4251 }                               /* End thread_fake_step */
4252
4253 /* Continue one thread when a signal must be sent to it.
4254  */
4255 static void
4256 threads_continue_one_with_signal (lwpid_t gdb_tid, int signal)
4257 {
4258   thread_info *p;
4259   lwpid_t real_tid;
4260   int real_pid;
4261
4262 #ifdef THREAD_DEBUG
4263   if (debug_on)
4264     printf ("Continuing one thread with a signal\n");
4265 #endif
4266
4267   real_tid = map_from_gdb_tid (gdb_tid);
4268   real_pid = get_pid_for (real_tid);
4269
4270   p = find_thread_info (real_tid);
4271   if (NULL == p)
4272     {
4273       add_tthread (real_pid, real_tid);
4274     }
4275
4276 #ifdef THREAD_DEBUG
4277   if (debug_on)
4278     if (p->terminated)
4279       printf ("Why are we continuing a dead thread? (2)\n");
4280 #endif
4281
4282   if (!p->handled)
4283     warning ("Internal error: continuing unhandled thread.");
4284
4285   p->have_signal = 0;
4286
4287   call_ttrace (TT_LWP_CONTINUE,
4288                gdb_tid,
4289                TT_USE_CURRENT_PC,
4290                (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4291                TT_NIL);
4292 }
4293 #endif
4294
4295 #ifndef CHILD_RESUME
4296
4297 /* Resume execution of the inferior process.
4298
4299  * This routine is in charge of setting the "handled" bits. 
4300  *
4301  *   If STEP is zero,      continue it.
4302  *   If STEP is nonzero,   single-step it.
4303  *   
4304  *   If SIGNAL is nonzero, give it that signal.
4305  *
4306  *   If TID is -1,         apply to all threads.
4307  *   If TID is not -1,     apply to specified thread.
4308  *   
4309  *           STEP
4310  *      \      !0                        0
4311  *  TID  \________________________________________________
4312  *       |
4313  *   -1  |   Step current            Continue all threads
4314  *       |   thread and              (but which gets any
4315  *       |   continue others         signal?--We look at
4316  *       |                           "inferior_pid")
4317  *       |
4318  *    N  |   Step _this_ thread      Continue _this_ thread
4319  *       |   and leave others        and leave others 
4320  *       |   stopped; internally     stopped; used only for
4321  *       |   used by gdb, never      hardware watchpoints
4322  *       |   a user command.         and attach, never a
4323  *       |                           user command.
4324  */
4325 void
4326 child_resume (lwpid_t gdb_tid, int step, enum target_signal signal)
4327 {
4328   int resume_all_threads;
4329   lwpid_t tid;
4330   process_state_t new_process_state;
4331
4332   resume_all_threads =
4333     (gdb_tid == INFTTRACE_ALL_THREADS) ||
4334     (vfork_in_flight);
4335
4336   if (resume_all_threads)
4337     {
4338       /* Resume all threads, but first pick a tid value
4339        * so we can get the pid when in call_ttrace doing
4340        * the map.
4341        */
4342       if (vfork_in_flight)
4343         tid = vforking_child_pid;
4344       else
4345         tid = map_from_gdb_tid (inferior_pid);
4346     }
4347   else
4348     tid = map_from_gdb_tid (gdb_tid);
4349
4350 #ifdef THREAD_DEBUG
4351   if (debug_on)
4352     {
4353       if (more_events_left)
4354         printf ("More events; ");
4355
4356       if (signal != 0)
4357         printf ("Sending signal %d; ", signal);
4358
4359       if (resume_all_threads)
4360         {
4361           if (step == 0)
4362             printf ("Continue process %d\n", tid);
4363           else
4364             printf ("Step/continue thread %d\n", tid);
4365         }
4366       else
4367         {
4368           if (step == 0)
4369             printf ("Continue thread %d\n", tid);
4370           else
4371             printf ("Step just thread %d\n", tid);
4372         }
4373
4374       if (vfork_in_flight)
4375         printf ("Vfork in flight\n");
4376     }
4377 #endif
4378
4379   if (process_state == RUNNING)
4380     warning ("Internal error in resume logic; doing resume or step anyway.");
4381
4382   if (!step                     /* Asked to continue...       */
4383       && resume_all_threads     /* whole process..            */
4384       && signal != 0            /* with a signal...           */
4385       && more_events_left > 0)
4386     {                           /* but we can't yet--save it! */
4387
4388       /* Continue with signal means we have to set the pending
4389        * signal value for this thread.
4390        */
4391       thread_info *k;
4392
4393 #ifdef THREAD_DEBUG
4394       if (debug_on)
4395         printf ("Saving signal %d for thread %d\n", signal, tid);
4396 #endif
4397
4398       k = find_thread_info (tid);
4399       if (k != NULL)
4400         {
4401           k->have_signal = 1;
4402           k->signal_value = signal;
4403
4404 #ifdef THREAD_DEBUG
4405           if (debug_on)
4406             if (k->terminated)
4407               printf ("Why are we continuing a dead thread? (3)\n");
4408 #endif
4409
4410         }
4411
4412 #ifdef THREAD_DEBUG
4413       else if (debug_on)
4414         {
4415           printf ("No thread info for tid %d\n", tid);
4416         }
4417 #endif
4418     }
4419
4420   /* Are we faking this "continue" or "step"?
4421
4422    * We used to do steps by continuing all the threads for 
4423    * which the events had been handled already.  While
4424    * conceptually nicer (hides it all in a lower level), this
4425    * can lead to starvation and a hang (e.g. all but one thread
4426    * are unhandled at a breakpoint just before a "join" operation,
4427    * and one thread is in the join, and the user wants to step that
4428    * thread).
4429    */
4430   if (resume_all_threads        /* Whole process, therefore user command */
4431       && more_events_left > 0)
4432     {                           /* But we can't do this yet--fake it! */
4433       thread_info *p;
4434
4435       if (!step)
4436         {
4437           /* No need to do any notes on a per-thread
4438            * basis--we're done!
4439            */
4440 #ifdef WAIT_BUFFER_DEBUG
4441           if (debug_on)
4442             printf ("Faking a process resume.\n");
4443 #endif
4444
4445           return;
4446         }
4447       else
4448         {
4449
4450 #ifdef WAIT_BUFFER_DEBUG
4451           if (debug_on)
4452             printf ("Faking a process step.\n");
4453 #endif
4454
4455         }
4456
4457       p = find_thread_info (tid);
4458       if (p == NULL)
4459         {
4460           warning ("No thread information for tid %d, 'next' command ignored.\n", tid);
4461           return;
4462         }
4463       else
4464         {
4465
4466 #ifdef THREAD_DEBUG
4467           if (debug_on)
4468             if (p->terminated)
4469               printf ("Why are we continuing a dead thread? (3.5)\n");
4470 #endif
4471
4472           if (p->stepping_mode != DO_DEFAULT)
4473             {
4474               warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored.");
4475
4476               return;
4477             }
4478
4479           if (step)
4480             p->stepping_mode = DO_STEP;
4481           else
4482             p->stepping_mode = DO_CONTINUE;
4483
4484           return;
4485         }                       /* Have thread info */
4486     }                           /* Must fake step or go */
4487
4488   /* Execept for fake-steps, from here on we know we are
4489    * going to wind up with a running process which will
4490    * need a real wait.
4491    */
4492   new_process_state = RUNNING;
4493
4494   /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4495    * it was.  (If GDB wanted it to start some other way, we have already
4496    * written a new PC value to the child.)
4497    *
4498    * If this system does not support PT_STEP, a higher level function will
4499    * have called single_step() to transmute the step request into a
4500    * continue request (by setting breakpoints on all possible successor
4501    * instructions), so we don't have to worry about that here.
4502    */
4503   if (step)
4504     {
4505       if (resume_all_threads)
4506         {
4507           /*
4508            * Regular user step: other threads get a "continue".
4509            */
4510           threads_continue_all_but_one (tid, signal);
4511           clear_all_handled ();
4512           clear_all_stepping_mode ();
4513         }
4514
4515       else
4516         {
4517           /* "Fake step": gdb is stepping one thread over a
4518            * breakpoint, watchpoint, or out of a library load
4519            * event, etc.  The rest just stay where they are.
4520            *
4521            * Also used when there are pending events: we really
4522            * step the current thread, but leave the rest stopped.
4523            * Users can't request this, but "wait_for_inferior"
4524            * does--a lot!
4525            */
4526           thread_fake_step (tid, signal);
4527
4528           /* Clear the "handled" state of this thread, because
4529            * we'll soon get a new event for it.  Other events
4530            * stay as they were.
4531            */
4532           clear_handled (tid);
4533           clear_stepping_mode (tid);
4534           new_process_state = FAKE_STEPPING;
4535         }
4536     }
4537
4538   else
4539     {
4540       /* TT_LWP_CONTINUE can pass signals to threads,
4541        * TT_PROC_CONTINUE can't.  So if there are any
4542        * signals to pass, we have to use the (slower)
4543        * loop over the stopped threads.
4544        *
4545        * Equally, if we have to not continue some threads,
4546        * due to saved events, we have to use the loop.
4547        */
4548       if ((signal != 0) || saved_signals_exist ())
4549         {
4550           if (resume_all_threads)
4551             {
4552
4553 #ifdef THREAD_DEBUG
4554               if (debug_on)
4555                 printf ("Doing a continue by loop of all threads\n");
4556 #endif
4557
4558               threads_continue_all_with_signals (tid, signal);
4559
4560               clear_all_handled ();
4561               clear_all_stepping_mode ();
4562             }
4563
4564           else
4565             {
4566 #ifdef THREAD_DEBUG
4567               printf ("Doing a continue w/signal of just thread %d\n", tid);
4568 #endif
4569
4570               threads_continue_one_with_signal (tid, signal);
4571
4572               /* Clear the "handled" state of this thread, because
4573                * we'll soon get a new event for it.  Other events
4574                * can stay as they were.
4575                */
4576               clear_handled (tid);
4577               clear_stepping_mode (tid);
4578             }
4579         }
4580
4581       else
4582         {
4583           /* No signals to send.
4584            */
4585           if (resume_all_threads)
4586             {
4587 #ifdef THREAD_DEBUG
4588               if (debug_on)
4589                 printf ("Doing a continue by process of process %d\n", tid);
4590 #endif
4591
4592               if (more_events_left > 0)
4593                 {
4594                   warning ("Losing buffered events on continue.");
4595                   more_events_left = 0;
4596                 }
4597
4598               call_ttrace (TT_PROC_CONTINUE,
4599                            tid,
4600                            TT_NIL,
4601                            TT_NIL,
4602                            TT_NIL);
4603
4604               clear_all_handled ();
4605               clear_all_stepping_mode ();
4606             }
4607
4608           else
4609             {
4610 #ifdef THREAD_DEBUG
4611               if (debug_on)
4612                 {
4613                   printf ("Doing a continue of just thread %d\n", tid);
4614                   if (is_terminated (tid))
4615                     printf ("Why are we continuing a dead thread? (5)\n");
4616                 }
4617 #endif
4618
4619               call_ttrace (TT_LWP_CONTINUE,
4620                            tid,
4621                            TT_NIL,
4622                            TT_NIL,
4623                            TT_NIL);
4624
4625               /* Clear the "handled" state of this thread, because
4626                * we'll soon get a new event for it.  Other events
4627                * can stay as they were.
4628                */
4629               clear_handled (tid);
4630               clear_stepping_mode (tid);
4631             }
4632         }
4633     }
4634
4635   process_state = new_process_state;
4636
4637 #ifdef WAIT_BUFFER_DEBUG
4638   if (debug_on)
4639     printf ("Process set to %s\n",
4640             get_printable_name_of_process_state (process_state));
4641 #endif
4642
4643 }
4644 #endif /* CHILD_RESUME */
4645 \f
4646
4647 #ifdef ATTACH_DETACH
4648 /*
4649  * Like it says.
4650  *
4651  * One worry is that we may not be attaching to "inferior_pid"
4652  * and thus may not want to clear out our data.  FIXME?
4653  * 
4654  */
4655 static void
4656 update_thread_state_after_attach (int pid, attach_continue_t kind_of_go)
4657 {
4658   int tt_status;
4659   ttstate_t thread_state;
4660   lwpid_t a_thread;
4661   lwpid_t tid;
4662
4663   /* The process better be stopped.
4664    */
4665   if (process_state != STOPPED
4666       && process_state != VFORKING)
4667     warning ("Internal error attaching.");
4668
4669   /* Clear out old tthread info and start over.  This has the
4670    * side effect of ensuring that the TRAP is reported as being
4671    * in the right thread (re-mapped from tid to pid).
4672    *
4673    * It's because we need to add the tthread _now_ that we
4674    * need to call "clear_thread_info" _now_, and that's why
4675    * "require_notification_of_events" doesn't clear the thread
4676    * info (it's called later than this routine).
4677    */
4678   clear_thread_info ();
4679   a_thread = 0;
4680
4681   for (tid = get_process_first_stopped_thread_id (pid, &thread_state);
4682        tid != 0;
4683        tid = get_process_next_stopped_thread_id (pid, &thread_state))
4684     {
4685       thread_info *p;
4686
4687       if (a_thread == 0)
4688         {
4689           a_thread = tid;
4690 #ifdef THREAD_DEBUG
4691           if (debug_on)
4692             printf ("Attaching to process %d, thread %d\n",
4693                     pid, a_thread);
4694 #endif
4695         }
4696
4697       /* Tell ourselves and the "rest of gdb" that this thread
4698        * exists.
4699        *
4700        * This isn't really a hack.  Other thread-based versions
4701        * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4702        *
4703        * We don't need to do mapping here, as we know this
4704        * is the first thread and thus gets the real pid
4705        * (and is "inferior_pid").
4706        *
4707        * NOTE: it probably isn't the originating thread,
4708        *       but that doesn't matter (we hope!).
4709        */
4710       add_tthread (pid, tid);
4711       p = find_thread_info (tid);
4712       if (NULL == p)            /* ?We just added it! */
4713         error ("Internal error adding a thread on attach.");
4714
4715       copy_ttstate_t (&p->last_stop_state, &thread_state);
4716       p->have_state = 1;
4717
4718       if (DO_ATTACH_CONTINUE == kind_of_go)
4719         {
4720           /*
4721            * If we are going to CONTINUE afterwards,
4722            * raising a SIGTRAP, don't bother trying to
4723            * handle this event.  But check first!
4724            */
4725           switch (p->last_stop_state.tts_event)
4726             {
4727
4728             case TTEVT_NONE:
4729               /* Ok to set this handled.
4730                */
4731               break;
4732
4733             default:
4734               warning ("Internal error; skipping event %s on process %d, thread %d.",
4735                        get_printable_name_of_ttrace_event (
4736                                               p->last_stop_state.tts_event),
4737                        p->pid, p->tid);
4738             }
4739
4740           set_handled (pid, tid);
4741
4742         }
4743       else
4744         {
4745           /* There will be no "continue" opertion, so the
4746            * process remains stopped.  Don't set any events
4747            * handled except the "gimmies".
4748            */
4749           switch (p->last_stop_state.tts_event)
4750             {
4751
4752             case TTEVT_NONE:
4753               /* Ok to ignore this.
4754                */
4755               set_handled (pid, tid);
4756               break;
4757
4758             case TTEVT_EXEC:
4759             case TTEVT_FORK:
4760               /* Expected "other" FORK or EXEC event from a
4761                * fork or vfork.
4762                */
4763               break;
4764
4765             default:
4766               printf ("Internal error: failed to handle event %s on process %d, thread %d.",
4767                       get_printable_name_of_ttrace_event (
4768                                               p->last_stop_state.tts_event),
4769                       p->pid, p->tid);
4770             }
4771         }
4772
4773       add_thread (tid);         /* in thread.c */
4774     }
4775
4776 #ifdef PARANOIA
4777   if (debug_on)
4778     print_tthreads ();
4779 #endif
4780
4781   /* One mustn't call ttrace_wait() after attaching via ttrace,
4782      'cause the process is stopped already.
4783
4784      However, the upper layers of gdb's execution control will
4785      want to wait after attaching (but not after forks, in
4786      which case they will be doing a "target_resume", anticipating
4787      a later TTEVT_EXEC or TTEVT_FORK event).
4788
4789      To make this attach() implementation more compatible with
4790      others, we'll make the attached-to process raise a SIGTRAP.
4791
4792      Issue: this continues only one thread.  That could be
4793      dangerous if the thread is blocked--the process won't run
4794      and no trap will be raised.  FIX! (check state.tts_flags?
4795      need one that's either TTS_WASRUNNING--but we've stopped
4796      it and made it TTS_WASSUSPENDED.  Hum...FIXME!)
4797    */
4798   if (DO_ATTACH_CONTINUE == kind_of_go)
4799     {
4800       tt_status = call_real_ttrace (
4801                                      TT_LWP_CONTINUE,
4802                                      pid,
4803                                      a_thread,
4804                                      TT_USE_CURRENT_PC,
4805                (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP),
4806                                      TT_NIL);
4807       if (errno)
4808         perror_with_name ("ttrace");
4809
4810       clear_handled (a_thread); /* So TRAP will be reported. */
4811
4812       /* Now running.
4813        */
4814       process_state = RUNNING;
4815     }
4816
4817   attach_flag = 1;
4818 }
4819 #endif /* ATTACH_DETACH */
4820 \f
4821
4822 #ifdef ATTACH_DETACH
4823 /* Start debugging the process whose number is PID.
4824  * (A _real_ pid).
4825  */
4826 int
4827 attach (int pid)
4828 {
4829   int tt_status;
4830
4831   tt_status = call_real_ttrace (
4832                                  TT_PROC_ATTACH,
4833                                  pid,
4834                                  (lwpid_t) TT_NIL,
4835                                  TT_NIL,
4836                                  (TTRACE_ARG_TYPE) TT_VERSION,
4837                                  TT_NIL);
4838   if (errno)
4839     perror_with_name ("ttrace attach");
4840
4841   /* If successful, the process is now stopped.
4842    */
4843   process_state = STOPPED;
4844
4845   /* Our caller ("attach_command" in "infcmd.c")
4846    * expects to do a "wait_for_inferior" after
4847    * the attach, so make sure the inferior is
4848    * running when we're done.
4849    */
4850   update_thread_state_after_attach (pid, DO_ATTACH_CONTINUE);
4851
4852   return pid;
4853 }
4854
4855
4856 #if defined(CHILD_POST_ATTACH)
4857 void
4858 child_post_attach (int pid)
4859 {
4860 #ifdef THREAD_DEBUG
4861   if (debug_on)
4862     printf ("child-post-attach call\n");
4863 #endif
4864
4865   require_notification_of_events (pid);
4866 }
4867 #endif
4868
4869
4870 /* Stop debugging the process whose number is PID
4871    and continue it with signal number SIGNAL.
4872    SIGNAL = 0 means just continue it.
4873  */
4874 void
4875 detach (int signal)
4876 {
4877   errno = 0;
4878   call_ttrace (TT_PROC_DETACH,
4879                inferior_pid,
4880                TT_NIL,
4881                (TTRACE_ARG_TYPE) signal,
4882                TT_NIL);
4883   attach_flag = 0;
4884
4885   clear_thread_info ();
4886
4887   /* Process-state? */
4888 }
4889 #endif /* ATTACH_DETACH */
4890 \f
4891
4892 /* Default the type of the ttrace transfer to int.  */
4893 #ifndef TTRACE_XFER_TYPE
4894 #define TTRACE_XFER_TYPE int
4895 #endif
4896
4897 void
4898 _initialize_kernel_u_addr (void)
4899 {
4900 }
4901
4902 #if !defined (CHILD_XFER_MEMORY)
4903 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
4904    in the NEW_SUN_TTRACE case.
4905    It ought to be straightforward.  But it appears that writing did
4906    not write the data that I specified.  I cannot understand where
4907    it got the data that it actually did write.  */
4908
4909 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
4910    to debugger memory starting at MYADDR.   Copy to inferior if
4911    WRITE is nonzero.  TARGET is ignored.
4912
4913    Returns the length copied, which is either the LEN argument or zero.
4914    This xfer function does not do partial moves, since child_ops
4915    doesn't allow memory operations to cross below us in the target stack
4916    anyway.  */
4917
4918 int
4919 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
4920                    struct mem_attrib *attrib,
4921                    struct target_ops *target)
4922 {
4923   register int i;
4924   /* Round starting address down to longword boundary.  */
4925   register CORE_ADDR addr = memaddr & -sizeof (TTRACE_XFER_TYPE);
4926   /* Round ending address up; get number of longwords that makes.  */
4927   register int count
4928   = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1)
4929   / sizeof (TTRACE_XFER_TYPE);
4930   /* Allocate buffer of that many longwords.  */
4931   register TTRACE_XFER_TYPE *buffer
4932   = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));
4933
4934   if (write)
4935     {
4936       /* Fill start and end extra bytes of buffer with existing memory data.  */
4937
4938       if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE))
4939         {
4940           /* Need part of initial word -- fetch it.  */
4941           buffer[0] = call_ttrace (TT_LWP_RDTEXT,
4942                                    inferior_pid,
4943                                    (TTRACE_ARG_TYPE) addr,
4944                                    TT_NIL,
4945                                    TT_NIL);
4946         }
4947
4948       if (count > 1)            /* FIXME, avoid if even boundary */
4949         {
4950           buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT,
4951                                            inferior_pid,
4952                                            ((TTRACE_ARG_TYPE)
4953                           (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))),
4954                                            TT_NIL,
4955                                            TT_NIL);
4956         }
4957
4958       /* Copy data to be written over corresponding part of buffer */
4959
4960       memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
4961               myaddr,
4962               len);
4963
4964       /* Write the entire buffer.  */
4965
4966       for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
4967         {
4968           errno = 0;
4969           call_ttrace (TT_LWP_WRDATA,
4970                        inferior_pid,
4971                        (TTRACE_ARG_TYPE) addr,
4972                        (TTRACE_ARG_TYPE) buffer[i],
4973                        TT_NIL);
4974           if (errno)
4975             {
4976               /* Using the appropriate one (I or D) is necessary for
4977                  Gould NP1, at least.  */
4978               errno = 0;
4979               call_ttrace (TT_LWP_WRTEXT,
4980                            inferior_pid,
4981                            (TTRACE_ARG_TYPE) addr,
4982                            (TTRACE_ARG_TYPE) buffer[i],
4983                            TT_NIL);
4984             }
4985           if (errno)
4986             return 0;
4987         }
4988     }
4989   else
4990     {
4991       /* Read all the longwords */
4992       for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
4993         {
4994           errno = 0;
4995           buffer[i] = call_ttrace (TT_LWP_RDTEXT,
4996                                    inferior_pid,
4997                                    (TTRACE_ARG_TYPE) addr,
4998                                    TT_NIL,
4999                                    TT_NIL);
5000           if (errno)
5001             return 0;
5002           QUIT;
5003         }
5004
5005       /* Copy appropriate bytes out of the buffer.  */
5006       memcpy (myaddr,
5007               (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
5008               len);
5009     }
5010   return len;
5011 }
5012 \f
5013
5014 static void
5015 udot_info (void)
5016 {
5017   int udot_off;                 /* Offset into user struct */
5018   int udot_val;                 /* Value from user struct at udot_off */
5019   char mess[128];               /* For messages */
5020
5021   if (!target_has_execution)
5022     {
5023       error ("The program is not being run.");
5024     }
5025
5026 #if !defined (KERNEL_U_SIZE)
5027
5028   /* Adding support for this command is easy.  Typically you just add a
5029      routine, called "kernel_u_size" that returns the size of the user
5030      struct, to the appropriate *-nat.c file and then add to the native
5031      config file "#define KERNEL_U_SIZE kernel_u_size()" */
5032   error ("Don't know how large ``struct user'' is in this version of gdb.");
5033
5034 #else
5035
5036   for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
5037     {
5038       if ((udot_off % 24) == 0)
5039         {
5040           if (udot_off > 0)
5041             {
5042               printf_filtered ("\n");
5043             }
5044           printf_filtered ("%04x:", udot_off);
5045         }
5046       udot_val = call_ttrace (TT_LWP_RUREGS,
5047                               inferior_pid,
5048                               (TTRACE_ARG_TYPE) udot_off,
5049                               TT_NIL,
5050                               TT_NIL);
5051       if (errno != 0)
5052         {
5053           sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
5054           perror_with_name (mess);
5055         }
5056       /* Avoid using nonportable (?) "*" in print specs */
5057       printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
5058     }
5059   printf_filtered ("\n");
5060
5061 #endif
5062 }
5063 #endif /* !defined (CHILD_XFER_MEMORY).  */
5064
5065 /* TTrace version of "target_pid_to_exec_file"
5066  */
5067 char *
5068 child_pid_to_exec_file (int tid)
5069 {
5070   static char exec_file_buffer[1024];
5071   int tt_status;
5072   CORE_ADDR top_of_stack;
5073   char four_chars[4];
5074   int name_index;
5075   int i;
5076   int done;
5077   int saved_inferior_pid;
5078
5079   /* As of 10.x HP-UX, there's an explicit request to get the
5080    *pathname.
5081    */
5082   tt_status = call_ttrace (TT_PROC_GET_PATHNAME,
5083                            tid,
5084                            (TTRACE_ARG_TYPE) exec_file_buffer,
5085                            (TTRACE_ARG_TYPE) sizeof (exec_file_buffer) - 1,
5086                            TT_NIL);
5087   if (tt_status >= 0)
5088     return exec_file_buffer;
5089
5090   /* ??rehrauer: The above request may or may not be broken.  It
5091      doesn't seem to work when I use it.  But, it may be designed
5092      to only work immediately after an exec event occurs.  (I'm
5093      waiting for COSL to explain.)
5094
5095      In any case, if it fails, try a really, truly amazingly gross
5096      hack that DDE uses, of pawing through the process' data
5097      segment to find the pathname.
5098    */
5099   top_of_stack = (TARGET_PTR_BIT == 64 ? 0x800003ffff7f0000 : 0x7b03a000);
5100   name_index = 0;
5101   done = 0;
5102
5103   /* On the chance that pid != inferior_pid, set inferior_pid
5104      to pid, so that (grrrr!) implicit uses of inferior_pid get
5105      the right id.
5106    */
5107   saved_inferior_pid = inferior_pid;
5108   inferior_pid = tid;
5109
5110   /* Try to grab a null-terminated string. */
5111   while (!done)
5112     {
5113       if (target_read_memory (top_of_stack, four_chars, 4) != 0)
5114         {
5115           inferior_pid = saved_inferior_pid;
5116           return NULL;
5117         }
5118       for (i = 0; i < 4; i++)
5119         {
5120           exec_file_buffer[name_index++] = four_chars[i];
5121           done = (four_chars[i] == '\0');
5122           if (done)
5123             break;
5124         }
5125       top_of_stack += 4;
5126     }
5127
5128   if (exec_file_buffer[0] == '\0')
5129     {
5130       inferior_pid = saved_inferior_pid;
5131       return NULL;
5132     }
5133
5134   inferior_pid = saved_inferior_pid;
5135   return exec_file_buffer;
5136 }
5137
5138
5139 void
5140 pre_fork_inferior (void)
5141 {
5142   int status;
5143
5144   status = pipe (startup_semaphore.parent_channel);
5145   if (status < 0)
5146     {
5147       warning ("error getting parent pipe for startup semaphore");
5148       return;
5149     }
5150
5151   status = pipe (startup_semaphore.child_channel);
5152   if (status < 0)
5153     {
5154       warning ("error getting child pipe for startup semaphore");
5155       return;
5156     }
5157 }
5158
5159 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5160  * ultimately from "follow_inferior_fork" in infrun.c,
5161  * itself called from "resume".
5162  *
5163  * This seems to be intended to attach after a fork or
5164  * vfork, while "attach" is used to attach to a pid
5165  * given by the user.  The check for an existing attach
5166  * seems odd--it always fails in our test system.
5167  */
5168 int
5169 hppa_require_attach (int pid)
5170 {
5171   int tt_status;
5172   CORE_ADDR pc;
5173   CORE_ADDR pc_addr;
5174   unsigned int regs_offset;
5175   process_state_t old_process_state = process_state;
5176
5177   /* Are we already attached?  There appears to be no explicit
5178    * way to answer this via ttrace, so we try something which
5179    * should be innocuous if we are attached.  If that fails,
5180    * then we assume we're not attached, and so attempt to make
5181    * it so.
5182    */
5183   errno = 0;
5184   tt_status = call_real_ttrace (TT_PROC_STOP,
5185                                 pid,
5186                                 (lwpid_t) TT_NIL,
5187                                 (TTRACE_ARG_TYPE) TT_NIL,
5188                                 (TTRACE_ARG_TYPE) TT_NIL,
5189                                 TT_NIL);
5190
5191   if (errno)
5192     {
5193       /* No change to process-state!
5194        */
5195       errno = 0;
5196       pid = attach (pid);
5197     }
5198   else
5199     {
5200       /* If successful, the process is now stopped.  But if
5201        * we're VFORKING, the parent is still running, so don't
5202        * change the process state.
5203        */
5204       if (process_state != VFORKING)
5205         process_state = STOPPED;
5206
5207       /* If we were already attached, you'd think that we
5208        * would need to start going again--but you'd be wrong,
5209        * as the fork-following code is actually in the middle
5210        * of the "resume" routine in in "infrun.c" and so
5211        * will (almost) immediately do a resume.
5212        *
5213        * On the other hand, if we are VFORKING, which means
5214        * that the child and the parent share a process for a
5215        * while, we know that "resume" won't be resuming
5216        * until the child EXEC event is seen.  But we still
5217        * don't want to continue, as the event is already
5218        * there waiting.
5219        */
5220       update_thread_state_after_attach (pid, DONT_ATTACH_CONTINUE);
5221     }                           /* STOP succeeded */
5222
5223   return pid;
5224 }
5225
5226 int
5227 hppa_require_detach (int pid, int signal)
5228 {
5229   int tt_status;
5230
5231   /* If signal is non-zero, we must pass the signal on to the active
5232      thread prior to detaching.  We do this by continuing the threads
5233      with the signal.
5234    */
5235   if (signal != 0)
5236     {
5237       errno = 0;
5238       threads_continue_all_with_signals (pid, signal);
5239     }
5240
5241   errno = 0;
5242   tt_status = call_ttrace (TT_PROC_DETACH,
5243                            pid,
5244                            TT_NIL,
5245                            TT_NIL,
5246                            TT_NIL);
5247
5248   errno = 0;                    /* Ignore any errors. */
5249
5250   /* process_state? */
5251
5252   return pid;
5253 }
5254
5255 /* Given the starting address of a memory page, hash it to a bucket in
5256    the memory page dictionary.
5257  */
5258 static int
5259 get_dictionary_bucket_of_page (CORE_ADDR page_start)
5260 {
5261   int hash;
5262
5263   hash = (page_start / memory_page_dictionary.page_size);
5264   hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT;
5265
5266   return hash;
5267 }
5268
5269
5270 /* Given a memory page's starting address, get (i.e., find an existing
5271    or create a new) dictionary entry for the page.  The page will be
5272    write-protected when this function returns, but may have a reference
5273    count of 0 (if the page was newly-added to the dictionary).
5274  */
5275 static memory_page_t *
5276 get_dictionary_entry_of_page (int pid, CORE_ADDR page_start)
5277 {
5278   int bucket;
5279   memory_page_t *page = NULL;
5280   memory_page_t *previous_page = NULL;
5281
5282   /* We're going to be using the dictionary now, than-kew. */
5283   require_memory_page_dictionary ();
5284
5285   /* Try to find an existing dictionary entry for this page.  Hash
5286      on the page's starting address.
5287    */
5288   bucket = get_dictionary_bucket_of_page (page_start);
5289   page = &memory_page_dictionary.buckets[bucket];
5290   while (page != NULL)
5291     {
5292       if (page->page_start == page_start)
5293         break;
5294       previous_page = page;
5295       page = page->next;
5296     }
5297
5298   /* Did we find a dictionary entry for this page?  If not, then
5299      add it to the dictionary now.
5300    */
5301   if (page == NULL)
5302     {
5303       /* Create a new entry. */
5304       page = (memory_page_t *) xmalloc (sizeof (memory_page_t));
5305       page->page_start = page_start;
5306       page->reference_count = 0;
5307       page->next = NULL;
5308       page->previous = NULL;
5309
5310       /* We'll write-protect the page now, if that's allowed. */
5311       page->original_permissions = write_protect_page (pid, page_start);
5312
5313       /* Add the new entry to the dictionary. */
5314       page->previous = previous_page;
5315       previous_page->next = page;
5316
5317       memory_page_dictionary.page_count++;
5318     }
5319
5320   return page;
5321 }
5322
5323
5324 static void
5325 remove_dictionary_entry_of_page (int pid, memory_page_t *page)
5326 {
5327   /* Restore the page's original permissions. */
5328   unwrite_protect_page (pid, page->page_start, page->original_permissions);
5329
5330   /* Kick the page out of the dictionary. */
5331   if (page->previous != NULL)
5332     page->previous->next = page->next;
5333   if (page->next != NULL)
5334     page->next->previous = page->previous;
5335
5336   /* Just in case someone retains a handle to this after it's freed. */
5337   page->page_start = (CORE_ADDR) 0;
5338
5339   memory_page_dictionary.page_count--;
5340
5341   xfree (page);
5342 }
5343
5344
5345 static void
5346 hppa_enable_syscall_events (int pid)
5347 {
5348   int tt_status;
5349   ttevent_t ttrace_events;
5350
5351   /* Get the set of events that are currently enabled. */
5352   tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5353                            pid,
5354                            (TTRACE_ARG_TYPE) & ttrace_events,
5355                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5356                            TT_NIL);
5357   if (errno)
5358     perror_with_name ("ttrace");
5359
5360   /* Add syscall events to that set. */
5361   ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY;
5362   ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN;
5363
5364   tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5365                            pid,
5366                            (TTRACE_ARG_TYPE) & ttrace_events,
5367                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5368                            TT_NIL);
5369   if (errno)
5370     perror_with_name ("ttrace");
5371 }
5372
5373
5374 static void
5375 hppa_disable_syscall_events (int pid)
5376 {
5377   int tt_status;
5378   ttevent_t ttrace_events;
5379
5380   /* Get the set of events that are currently enabled. */
5381   tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5382                            pid,
5383                            (TTRACE_ARG_TYPE) & ttrace_events,
5384                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5385                            TT_NIL);
5386   if (errno)
5387     perror_with_name ("ttrace");
5388
5389   /* Remove syscall events from that set. */
5390   ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY;
5391   ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN;
5392
5393   tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5394                            pid,
5395                            (TTRACE_ARG_TYPE) & ttrace_events,
5396                            (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5397                            TT_NIL);
5398   if (errno)
5399     perror_with_name ("ttrace");
5400 }
5401
5402
5403 /* The address range beginning with START and ending with START+LEN-1
5404    (inclusive) is to be watched via page-protection by a new watchpoint.
5405    Set protection for all pages that overlap that range.
5406
5407    Note that our caller sets TYPE to:
5408    0 for a bp_hardware_watchpoint,
5409    1 for a bp_read_watchpoint,
5410    2 for a bp_access_watchpoint
5411
5412    (Yes, this is intentionally (though lord only knows why) different
5413    from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5414  */
5415 int
5416 hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
5417 {
5418   CORE_ADDR page_start;
5419   int dictionary_was_empty;
5420   int page_size;
5421   int page_id;
5422   LONGEST range_size_in_pages;
5423
5424   if (type != 0)
5425     error ("read or access hardware watchpoints not supported on HP-UX");
5426
5427   /* Examine all pages in the address range. */
5428   require_memory_page_dictionary ();
5429
5430   dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5431
5432   page_size = memory_page_dictionary.page_size;
5433   page_start = (start / page_size) * page_size;
5434   range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5435
5436   for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5437     {
5438       memory_page_t *page;
5439
5440       /* This gets the page entered into the dictionary if it was
5441          not already entered.
5442        */
5443       page = get_dictionary_entry_of_page (pid, page_start);
5444       page->reference_count++;
5445     }
5446
5447   /* Our implementation depends on seeing calls to kernel code, for the
5448      following reason.  Here we ask to be notified of syscalls.
5449
5450      When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5451      Fine.
5452
5453      But when kernel code accesses the page, it doesn't give a SIGBUS.
5454      Rather, the system call that touched the page fails, with errno=EFAULT.
5455      Not good for us.
5456
5457      We could accomodate this "feature" by asking to be notified of syscall
5458      entries & exits; upon getting an entry event, disabling page-protections;
5459      upon getting an exit event, reenabling page-protections and then checking
5460      if any watchpoints triggered.
5461
5462      However, this turns out to be a real performance loser.  syscalls are
5463      usually a frequent occurrence.  Having to unprotect-reprotect all watched
5464      pages, and also to then read all watched memory locations and compare for
5465      triggers, can be quite expensive.
5466
5467      Instead, we'll only ask to be notified of syscall exits.  When we get
5468      one, we'll check whether errno is set.  If not, or if it's not EFAULT,
5469      we can just continue the inferior.
5470
5471      If errno is set upon syscall exit to EFAULT, we must perform some fairly
5472      hackish stuff to determine whether the failure really was due to a
5473      page-protect trap on a watched location.
5474    */
5475   if (dictionary_was_empty)
5476     hppa_enable_syscall_events (pid);
5477
5478   return 1;
5479 }
5480
5481
5482 /* The address range beginning with START and ending with START+LEN-1
5483    (inclusive) was being watched via page-protection by a watchpoint
5484    which has been removed.  Remove protection for all pages that
5485    overlap that range, which are not also being watched by other
5486    watchpoints.
5487  */
5488 int
5489 hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len,
5490                            enum bptype type)
5491 {
5492   CORE_ADDR page_start;
5493   int dictionary_is_empty;
5494   int page_size;
5495   int page_id;
5496   LONGEST range_size_in_pages;
5497
5498   if (type != 0)
5499     error ("read or access hardware watchpoints not supported on HP-UX");
5500
5501   /* Examine all pages in the address range. */
5502   require_memory_page_dictionary ();
5503
5504   page_size = memory_page_dictionary.page_size;
5505   page_start = (start / page_size) * page_size;
5506   range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5507
5508   for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5509     {
5510       memory_page_t *page;
5511
5512       page = get_dictionary_entry_of_page (pid, page_start);
5513       page->reference_count--;
5514
5515       /* Was this the last reference of this page?  If so, then we
5516          must scrub the entry from the dictionary, and also restore
5517          the page's original permissions.
5518        */
5519       if (page->reference_count == 0)
5520         remove_dictionary_entry_of_page (pid, page);
5521     }
5522
5523   dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5524
5525   /* If write protections are currently disallowed, then that implies that
5526      wait_for_inferior believes that the inferior is within a system call.
5527      Since we want to see both syscall entry and return, it's clearly not
5528      good to disable syscall events in this state!
5529
5530      ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5531      "inferior is between syscall events now".  Oh well.
5532    */
5533   if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed)
5534     hppa_disable_syscall_events (pid);
5535
5536   return 1;
5537 }
5538
5539
5540 /* Could we implement a watchpoint of this type via our available
5541    hardware support?
5542
5543    This query does not consider whether a particular address range
5544    could be so watched, but just whether support is generally available
5545    for such things.  See hppa_range_profitable_for_hw_watchpoint for a
5546    query that answers whether a particular range should be watched via
5547    hardware support.
5548  */
5549 int
5550 hppa_can_use_hw_watchpoint (enum bptype type, int cnt, enum bptype ot)
5551 {
5552   return (type == bp_hardware_watchpoint);
5553 }
5554
5555
5556 /* Assuming we could set a hardware watchpoint on this address, do
5557    we think it would be profitable ("a good idea") to do so?  If not,
5558    we can always set a regular (aka single-step & test) watchpoint
5559    on the address...
5560  */
5561 int
5562 hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
5563 {
5564   int range_is_stack_based;
5565   int range_is_accessible;
5566   CORE_ADDR page_start;
5567   int page_size;
5568   int page;
5569   LONGEST range_size_in_pages;
5570
5571   /* ??rehrauer: For now, say that all addresses are potentially
5572      profitable.  Possibly later we'll want to test the address
5573      for "stackness"?
5574    */
5575   range_is_stack_based = 0;
5576
5577   /* If any page in the range is inaccessible, then we cannot
5578      really use hardware watchpointing, even though our client
5579      thinks we can.  In that case, it's actually an error to
5580      attempt to use hw watchpoints, so we'll tell our client
5581      that the range is "unprofitable", and hope that they listen...
5582    */
5583   range_is_accessible = 1;      /* Until proven otherwise. */
5584
5585   /* Examine all pages in the address range. */
5586   errno = 0;
5587   page_size = sysconf (_SC_PAGE_SIZE);
5588
5589   /* If we can't determine page size, we're hosed.  Tell our
5590      client it's unprofitable to use hw watchpoints for this
5591      range.
5592    */
5593   if (errno || (page_size <= 0))
5594     {
5595       errno = 0;
5596       return 0;
5597     }
5598
5599   page_start = (start / page_size) * page_size;
5600   range_size_in_pages = len / (LONGEST) page_size;
5601
5602   for (page = 0; page < range_size_in_pages; page++, page_start += page_size)
5603     {
5604       int tt_status;
5605       int page_permissions;
5606
5607       /* Is this page accessible? */
5608       errno = 0;
5609       tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
5610                                pid,
5611                                (TTRACE_ARG_TYPE) page_start,
5612                                TT_NIL,
5613                                (TTRACE_ARG_TYPE) & page_permissions);
5614       if (errno || (tt_status < 0))
5615         {
5616           errno = 0;
5617           range_is_accessible = 0;
5618           break;
5619         }
5620
5621       /* Yes, go for another... */
5622     }
5623
5624   return (!range_is_stack_based && range_is_accessible);
5625 }
5626
5627
5628 char *
5629 hppa_pid_or_tid_to_str (pid_t id)
5630 {
5631   static char buf[100];         /* Static because address returned. */
5632
5633   /* Does this appear to be a process?  If so, print it that way. */
5634   if (is_process_id (id))
5635     return child_pid_to_str (id);
5636
5637   /* Else, print both the GDB thread number and the system thread id. */
5638   sprintf (buf, "thread %d (", pid_to_thread_id (id));
5639   strcat (buf, hppa_tid_to_str (id));
5640   strcat (buf, ")\0");
5641
5642   return buf;
5643 }
5644 \f
5645
5646 /* If the current pid is not the pid this module reported
5647  * from "ptrace_wait" with the most recent event, then the
5648  * user has switched threads.
5649  *
5650  * If the last reported event was a breakpoint, then return
5651  * the old thread id, else return 0.
5652  */
5653 pid_t
5654 hppa_switched_threads (pid_t gdb_pid)
5655 {
5656   if (gdb_pid == old_gdb_pid)
5657     {
5658       /*
5659        * Core gdb is working with the same pid that it
5660        * was before we reported the last event.  This
5661        * is ok: e.g. we reported hitting a thread-specific
5662        * breakpoint, but we were reporting the wrong
5663        * thread, so the core just ignored the event.
5664        *
5665        * No thread switch has happened.
5666        */
5667       return (pid_t) 0;
5668     }
5669   else if (gdb_pid == reported_pid)
5670     {
5671       /*
5672        * Core gdb is working with the pid we reported, so
5673        * any continue or step will be able to figure out
5674        * that it needs to step over any hit breakpoints
5675        * without our (i.e. PREPARE_TO_PROCEED's) help.
5676        */
5677       return (pid_t) 0;
5678     }
5679   else if (!reported_bpt)
5680     {
5681       /*
5682        * The core switched, but we didn't just report a
5683        * breakpoint, so there's no just-hit breakpoint
5684        * instruction at "reported_pid"'s PC, and thus there
5685        * is no need to step over it.
5686        */
5687       return (pid_t) 0;
5688     }
5689   else
5690     {
5691       /* There's been a real switch, and we reported
5692        * a hit breakpoint.  Let "hppa_prepare_to_proceed"
5693        * know, so it can see whether the breakpoint is
5694        * still active.
5695        */
5696       return reported_pid;
5697     }
5698
5699   /* Keep compiler happy with an obvious return at the end.
5700    */
5701   return (pid_t) 0;
5702 }
5703
5704 void
5705 hppa_ensure_vforking_parent_remains_stopped (int pid)
5706 {
5707   /* Nothing to do when using ttrace.  Only the ptrace-based implementation
5708      must do real work.
5709    */
5710 }
5711
5712
5713 int
5714 hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
5715 {
5716   return 0;                     /* No, the parent vfork is available now. */
5717 }
5718 \f
5719
5720 /* Write a register as a 64bit value.  This may be necessary if the
5721    native OS is too braindamaged to allow some (or all) registers to
5722    be written in 32bit hunks such as hpux11 and the PC queue registers.
5723
5724    This is horribly gross and disgusting.  */
5725  
5726 int
5727 ttrace_write_reg_64 (int gdb_tid, CORE_ADDR dest_addr, CORE_ADDR src_addr)
5728 {
5729   pid_t         pid;
5730   lwpid_t       tid;
5731   int           tt_status;
5732
5733   tid = map_from_gdb_tid (gdb_tid);
5734   pid = get_pid_for (tid);
5735
5736   errno = 0;
5737   tt_status = ttrace (TT_LWP_WUREGS, 
5738                       pid, 
5739                       tid, 
5740                       (TTRACE_ARG_TYPE) dest_addr, 
5741                       8, 
5742                       (TTRACE_ARG_TYPE) src_addr );
5743
5744 #ifdef THREAD_DEBUG
5745   if (errno)
5746     {
5747       /* Don't bother for a known benign error: if you ask for the
5748          first thread state, but there is only one thread and it's
5749          not stopped, ttrace complains.
5750         
5751          We have this inside the #ifdef because our caller will do
5752          this check for real.  */
5753       if( request != TT_PROC_GET_FIRST_LWP_STATE
5754           ||  errno   != EPROTO )
5755         {
5756           if( debug_on )
5757             printf( "TT fail for %s, with pid %d, tid %d, status %d \n",
5758                     get_printable_name_of_ttrace_request (TT_LWP_WUREGS),
5759                     pid, tid, tt_status );
5760         }
5761     }
5762 #endif
5763
5764   return tt_status;
5765 }
5766
5767 void
5768 _initialize_infttrace (void)
5769 {
5770   /* Initialize the ttrace-based hardware watchpoint implementation. */
5771   memory_page_dictionary.page_count = (LONGEST) - 1;
5772   memory_page_dictionary.page_protections_allowed = 1;
5773
5774   errno = 0;
5775   memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE);
5776
5777   /* We do a lot of casts from pointers to TTRACE_ARG_TYPE; make sure
5778      this is okay.  */
5779   if (sizeof (TTRACE_ARG_TYPE) < sizeof (void *))
5780     internal_error (__FILE__, __LINE__, "failed internal consistency check");
5781
5782   if (errno || (memory_page_dictionary.page_size <= 0))
5783     perror_with_name ("sysconf");
5784 }
This page took 0.354217 seconds and 4 git commands to generate.