import gdb-1999-08-09 snapshot
[external/binutils.git] / gdb / hppah-nat.c
1 /* Native support code for HPUX PA-RISC.
2    Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1998, 1999
3    Free Software Foundation, Inc.
4
5    Contributed by the Center for Software Science at the
6    University of Utah (pa-gdb-bugs@cs.utah.edu).
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24
25
26 #include "defs.h"
27 #include "inferior.h"
28 #include "target.h"
29 #include <sys/ptrace.h>
30 #include "gdbcore.h"
31 #include <wait.h>
32 #include <signal.h>
33
34 extern CORE_ADDR text_end;
35
36 static void fetch_register PARAMS ((int));
37
38 void
39 fetch_inferior_registers (regno)
40      int regno;
41 {
42   if (regno == -1)
43     for (regno = 0; regno < NUM_REGS; regno++)
44       fetch_register (regno);
45   else
46     fetch_register (regno);
47 }
48
49 /* Store our register values back into the inferior.
50    If REGNO is -1, do this for all registers.
51    Otherwise, REGNO specifies which register (so we can save time).  */
52
53 void
54 store_inferior_registers (regno)
55      int regno;
56 {
57   register unsigned int regaddr;
58   char buf[80];
59   register int i;
60   unsigned int offset = U_REGS_OFFSET;
61   int scratch;
62
63   if (regno >= 0)
64     {
65       if (CANNOT_STORE_REGISTER (regno))
66         return;
67       regaddr = register_addr (regno, offset);
68       errno = 0;
69       if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
70         {
71           scratch = *(int *) &registers[REGISTER_BYTE (regno)] | 0x3;
72           call_ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
73                        scratch);
74           if (errno != 0)
75             {
76               /* Error, even if attached.  Failing to write these two
77                  registers is pretty serious.  */
78               sprintf (buf, "writing register number %d", regno);
79               perror_with_name (buf);
80             }
81         }
82       else
83         for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
84           {
85             errno = 0;
86             call_ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
87                          *(int *) &registers[REGISTER_BYTE (regno) + i]);
88             if (errno != 0)
89               {
90                 /* Warning, not error, in case we are attached; sometimes the
91                    kernel doesn't let us at the registers.  */
92                 char *err = safe_strerror (errno);
93                 char *msg = alloca (strlen (err) + 128);
94                 sprintf (msg, "writing register %s: %s",
95                          REGISTER_NAME (regno), err);
96                 warning (msg);
97                 return;
98               }
99             regaddr += sizeof (int);
100           }
101     }
102   else
103     for (regno = 0; regno < NUM_REGS; regno++)
104       store_inferior_registers (regno);
105 }
106
107
108 /* Our own version of the offsetof macro, since we can't assume ANSI C.  */
109 #define HPPAH_OFFSETOF(type, member) ((int) (&((type *) 0)->member))
110
111 /* Fetch a register's value from the process's U area.  */
112 static void
113 fetch_register (regno)
114      int regno;
115 {
116   char buf[MAX_REGISTER_RAW_SIZE];
117   unsigned int addr, len, offset;
118   int i;
119
120   offset = 0;
121   len = REGISTER_RAW_SIZE (regno);
122
123   /* Requests for register zero actually want the save_state's
124      ss_flags member.  As RM says: "Oh, what a hack!"  */
125   if (regno == 0)
126     {
127       save_state_t ss;
128       addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
129       len = sizeof (ss.ss_flags);
130
131       /* Note that ss_flags is always an int, no matter what
132          REGISTER_RAW_SIZE(0) says.  Assuming all HP-UX PA machines
133          are big-endian, put it at the least significant end of the
134          value, and zap the rest of the buffer.  */
135       offset = REGISTER_RAW_SIZE (0) - len;
136       memset (buf, 0, sizeof (buf));
137     }
138
139   /* Floating-point registers come from the ss_fpblock area.  */
140   else if (regno >= FP0_REGNUM)
141     addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock) 
142             + (REGISTER_BYTE (regno) - REGISTER_BYTE (FP0_REGNUM)));
143
144   /* Wide registers come from the ss_wide area.
145      I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
146      between ss_wide and ss_narrow than to use the raw register size.
147      But checking ss_flags would require an extra ptrace call for
148      every register reference.  Bleah.  */
149   else if (len == 8)
150     addr = (HPPAH_OFFSETOF (save_state_t, ss_wide) 
151             + REGISTER_BYTE (regno));
152
153   /* Narrow registers come from the ss_narrow area.  Note that
154      ss_narrow starts with gr1, not gr0.  */
155   else if (len == 4)
156     addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
157             + (REGISTER_BYTE (regno) - REGISTER_BYTE (1)));
158
159   else
160     internal_error ("hppa-nat.c (fetch_register): unexpected register size");
161
162   for (i = 0; i < len; i += sizeof (int))
163     {
164       errno = 0;
165       /* Copy an int from the U area to buf.  Fill the least
166          significant end if len != raw_size.  */
167       * (int *) &buf[offset + i] =
168           call_ptrace (PT_RUREGS, inferior_pid,
169                        (PTRACE_ARG3_TYPE) addr + i, 0);
170       if (errno != 0)
171         {
172           /* Warning, not error, in case we are attached; sometimes
173              the kernel doesn't let us at the registers. */
174           char *err = safe_strerror (errno);
175           char *msg = alloca (strlen (err) + 128);
176           sprintf (msg, "reading `%s' register: %s",
177                    REGISTER_NAME (regno), err);
178           warning (msg);
179           return;
180         }
181     }
182
183   /* If we're reading an address from the instruction address queue,
184      mask out the bottom two bits --- they contain the privilege
185      level.  */
186   if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
187     buf[len - 1] &= ~0x3;
188
189   supply_register (regno, buf);
190 }
191
192
193 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
194    to debugger memory starting at MYADDR.   Copy to inferior if
195    WRITE is nonzero.
196
197    Returns the length copied, which is either the LEN argument or zero.
198    This xfer function does not do partial moves, since child_ops
199    doesn't allow memory operations to cross below us in the target stack
200    anyway.  */
201
202 int
203 child_xfer_memory (memaddr, myaddr, len, write, target)
204      CORE_ADDR memaddr;
205      char *myaddr;
206      int len;
207      int write;
208      struct target_ops *target; /* ignored */
209 {
210   register int i;
211   /* Round starting address down to longword boundary.  */
212   register CORE_ADDR addr = memaddr & - (CORE_ADDR)(sizeof (int));
213   /* Round ending address up; get number of longwords that makes.  */
214   register int count
215   = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
216
217   /* Allocate buffer of that many longwords.
218      Note -- do not use alloca to allocate this buffer since there is no
219      guarantee of when the buffer will actually be deallocated.
220
221      This routine can be called over and over with the same call chain;
222      this (in effect) would pile up all those alloca requests until a call
223      to alloca was made from a point higher than this routine in the
224      call chain.  */
225   register int *buffer = (int *) xmalloc (count * sizeof (int));
226
227   if (write)
228     {
229       /* Fill start and end extra bytes of buffer with existing memory data.  */
230       if (addr != memaddr || len < (int) sizeof (int))
231         {
232           /* Need part of initial word -- fetch it.  */
233           buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
234                                    inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
235         }
236
237       if (count > 1)            /* FIXME, avoid if even boundary */
238         {
239           buffer[count - 1]
240             = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
241                            inferior_pid,
242                            (PTRACE_ARG3_TYPE) (addr
243                                                + (count - 1) * sizeof (int)),
244                            0);
245         }
246
247       /* Copy data to be written over corresponding part of buffer */
248       memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
249
250       /* Write the entire buffer.  */
251       for (i = 0; i < count; i++, addr += sizeof (int))
252         {
253           int pt_status;
254           int pt_request;
255           /* The HP-UX kernel crashes if you use PT_WDUSER to write into the
256              text segment.  FIXME -- does it work to write into the data
257              segment using WIUSER, or do these idiots really expect us to
258              figure out which segment the address is in, so we can use a
259              separate system call for it??!  */
260           errno = 0;
261           pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER;
262           pt_status = call_ptrace (pt_request,
263                                    inferior_pid,
264                                    (PTRACE_ARG3_TYPE) addr,
265                                    buffer[i]);
266
267           /* Did we fail?  Might we've guessed wrong about which
268              segment this address resides in?  Try the other request,
269              and see if that works...  */
270           if ((pt_status == -1) && errno)
271             {
272               errno = 0;
273               pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER;
274               pt_status = call_ptrace (pt_request,
275                                        inferior_pid,
276                                        (PTRACE_ARG3_TYPE) addr,
277                                        buffer[i]);
278
279               /* No, we still fail.  Okay, time to punt. */
280               if ((pt_status == -1) && errno)
281                 {
282                   free (buffer);
283                   return 0;
284                 }
285             }
286         }
287     }
288   else
289     {
290       /* Read all the longwords */
291       for (i = 0; i < count; i++, addr += sizeof (int))
292         {
293           errno = 0;
294           buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
295                                    inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
296           if (errno)
297             {
298               free (buffer);
299               return 0;
300             }
301           QUIT;
302         }
303
304       /* Copy appropriate bytes out of the buffer.  */
305       memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
306     }
307   free (buffer);
308   return len;
309 }
310
311
312 void
313 child_post_follow_inferior_by_clone ()
314 {
315   int status;
316
317   /* This function is used when following both the parent and child
318      of a fork.  In this case, the debugger clones itself.  The original
319      debugger follows the parent, the clone follows the child.  The
320      original detaches from the child, delivering a SIGSTOP to it to
321      keep it from running away until the clone can attach itself.
322
323      At this point, the clone has attached to the child.  Because of
324      the SIGSTOP, we must now deliver a SIGCONT to the child, or it
325      won't behave properly. */
326   status = kill (inferior_pid, SIGCONT);
327 }
328
329
330 void
331 child_post_follow_vfork (parent_pid, followed_parent, child_pid, followed_child)
332      int parent_pid;
333      int followed_parent;
334      int child_pid;
335      int followed_child;
336 {
337   /* Are we a debugger that followed the parent of a vfork?  If so,
338      then recall that the child's vfork event was delivered to us
339      first.  And, that the parent was suspended by the OS until the
340      child's exec or exit events were received.
341
342      Upon receiving that child vfork, then, we were forced to remove
343      all breakpoints in the child and continue it so that it could
344      reach the exec or exit point.
345
346      But also recall that the parent and child of a vfork share the
347      same address space.  Thus, removing bp's in the child also
348      removed them from the parent.
349
350      Now that the child has safely exec'd or exited, we must restore
351      the parent's breakpoints before we continue it.  Else, we may
352      cause it run past expected stopping points. */
353   if (followed_parent)
354     {
355       reattach_breakpoints (parent_pid);
356     }
357
358   /* Are we a debugger that followed the child of a vfork?  If so,
359      then recall that we don't actually acquire control of the child
360      until after it has exec'd or exited.  */
361   if (followed_child)
362     {
363       /* If the child has exited, then there's nothing for us to do.
364          In the case of an exec event, we'll let that be handled by
365          the normal mechanism that notices and handles exec events, in
366          resume(). */
367     }
368 }
369
370 /* Format a process id, given PID.  Be sure to terminate
371    this with a null--it's going to be printed via a "%s".  */
372 char *
373 hppa_pid_to_str (pid)
374      pid_t pid;
375 {
376   /* Static because address returned */
377   static char buf[30];
378
379   /* Extra NULLs for paranoia's sake */
380   sprintf (buf, "process %d\0\0\0\0", pid);
381
382   return buf;
383 }
384
385 /* Format a thread id, given TID.  Be sure to terminate
386    this with a null--it's going to be printed via a "%s".
387
388    Note: This is a core-gdb tid, not the actual system tid.
389    See infttrace.c for details.  */
390 char *
391 hppa_tid_to_str (tid)
392      pid_t tid;
393 {
394   /* Static because address returned */
395   static char buf[30];
396
397   /* Extra NULLs for paranoia's sake */
398   sprintf (buf, "system thread %d\0\0\0\0", tid);
399
400   return buf;
401 }
402
403 #if !defined (GDB_NATIVE_HPUX_11)
404
405 /* The following code is a substitute for the infttrace.c versions used
406    with ttrace() in HPUX 11.  */
407
408 /* This value is an arbitrary integer. */
409 #define PT_VERSION 123456
410
411 /* This semaphore is used to coordinate the child and parent processes
412    after a fork(), and before an exec() by the child.  See
413    parent_attach_all for details.  */
414
415 typedef struct
416 {
417   int parent_channel[2];        /* Parent "talks" to [1], child "listens" to [0] */
418   int child_channel[2];         /* Child "talks" to [1], parent "listens" to [0] */
419 }
420 startup_semaphore_t;
421
422 #define SEM_TALK (1)
423 #define SEM_LISTEN (0)
424
425 static startup_semaphore_t startup_semaphore;
426
427 extern int parent_attach_all PARAMS ((int, PTRACE_ARG3_TYPE, int));
428
429 #ifdef PT_SETTRC
430 /* This function causes the caller's process to be traced by its
431    parent.  This is intended to be called after GDB forks itself,
432    and before the child execs the target.
433
434    Note that HP-UX ptrace is rather funky in how this is done.
435    If the parent wants to get the initial exec event of a child,
436    it must set the ptrace event mask of the child to include execs.
437    (The child cannot do this itself.)  This must be done after the
438    child is forked, but before it execs.
439
440    To coordinate the parent and child, we implement a semaphore using
441    pipes.  After SETTRC'ing itself, the child tells the parent that
442    it is now traceable by the parent, and waits for the parent's
443    acknowledgement.  The parent can then set the child's event mask,
444    and notify the child that it can now exec.
445
446    (The acknowledgement by parent happens as a result of a call to
447    child_acknowledge_created_inferior.)  */
448
449 int
450 parent_attach_all (pid, addr, data)
451      int pid;
452      PTRACE_ARG3_TYPE addr;
453      int data;
454 {
455   int pt_status = 0;
456
457   /* We need a memory home for a constant.  */
458   int tc_magic_child = PT_VERSION;
459   int tc_magic_parent = 0;
460
461   /* The remainder of this function is only useful for HPUX 10.0 and
462      later, as it depends upon the ability to request notification
463      of specific kinds of events by the kernel.  */
464 #if defined(PT_SET_EVENT_MASK)
465
466   /* Notify the parent that we're potentially ready to exec(). */
467   write (startup_semaphore.child_channel[SEM_TALK],
468          &tc_magic_child,
469          sizeof (tc_magic_child));
470
471   /* Wait for acknowledgement from the parent. */
472   read (startup_semaphore.parent_channel[SEM_LISTEN],
473         &tc_magic_parent,
474         sizeof (tc_magic_parent));
475   if (tc_magic_child != tc_magic_parent)
476     warning ("mismatched semaphore magic");
477
478   /* Discard our copy of the semaphore. */
479   (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
480   (void) close (startup_semaphore.parent_channel[SEM_TALK]);
481   (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
482   (void) close (startup_semaphore.child_channel[SEM_TALK]);
483 #endif
484
485   return 0;
486 }
487 #endif
488
489 int
490 hppa_require_attach (pid)
491      int pid;
492 {
493   int pt_status;
494   CORE_ADDR pc;
495   CORE_ADDR pc_addr;
496   unsigned int regs_offset;
497
498   /* Are we already attached?  There appears to be no explicit way to
499      answer this via ptrace, so we try something which should be
500      innocuous if we are attached.  If that fails, then we assume
501      we're not attached, and so attempt to make it so. */
502
503   errno = 0;
504   regs_offset = U_REGS_OFFSET;
505   pc_addr = register_addr (PC_REGNUM, regs_offset);
506   pc = call_ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE) pc_addr, 0);
507
508   if (errno)
509     {
510       errno = 0;
511       pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
512
513       if (errno)
514         return -1;
515
516       /* Now we really are attached. */
517       errno = 0;
518     }
519   attach_flag = 1;
520   return pid;
521 }
522
523 int
524 hppa_require_detach (pid, signal)
525      int pid;
526      int signal;
527 {
528   errno = 0;
529   call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal);
530   errno = 0;                    /* Ignore any errors. */
531   return pid;
532 }
533
534 /* Since ptrace doesn't support memory page-protection events, which
535    are used to implement "hardware" watchpoints on HP-UX, these are
536    dummy versions, which perform no useful work.  */
537
538 void
539 hppa_enable_page_protection_events (pid)
540      int pid;
541 {
542 }
543
544 void
545 hppa_disable_page_protection_events (pid)
546      int pid;
547 {
548 }
549
550 int
551 hppa_insert_hw_watchpoint (pid, start, len, type)
552      int pid;
553      CORE_ADDR start;
554      LONGEST len;
555      int type;
556 {
557   error ("Hardware watchpoints not implemented on this platform.");
558 }
559
560 int
561 hppa_remove_hw_watchpoint (pid, start, len, type)
562      int pid;
563      CORE_ADDR start;
564      LONGEST len;
565      enum bptype type;
566 {
567   error ("Hardware watchpoints not implemented on this platform.");
568 }
569
570 int
571 hppa_can_use_hw_watchpoint (type, cnt, ot)
572      enum bptype type;
573      int cnt;
574      enum bptype ot;
575 {
576   return 0;
577 }
578
579 int
580 hppa_range_profitable_for_hw_watchpoint (pid, start, len)
581      int pid;
582      CORE_ADDR start;
583      LONGEST len;
584 {
585   error ("Hardware watchpoints not implemented on this platform.");
586 }
587
588 char *
589 hppa_pid_or_tid_to_str (id)
590      pid_t id;
591 {
592   /* In the ptrace world, there are only processes. */
593   return hppa_pid_to_str (id);
594 }
595
596 /* This function has no meaning in a non-threaded world.  Thus, we
597    return 0 (FALSE).  See the use of "hppa_prepare_to_proceed" in
598    hppa-tdep.c. */
599
600 pid_t
601 hppa_switched_threads (pid)
602      pid_t pid;
603 {
604   return (pid_t) 0;
605 }
606
607 void
608 hppa_ensure_vforking_parent_remains_stopped (pid)
609      int pid;
610 {
611   /* This assumes that the vforked parent is presently stopped, and
612      that the vforked child has just delivered its first exec event.
613      Calling kill() this way will cause the SIGTRAP to be delivered as
614      soon as the parent is resumed, which happens as soon as the
615      vforked child is resumed.  See wait_for_inferior for the use of
616      this function.  */
617   kill (pid, SIGTRAP);
618 }
619
620 int
621 hppa_resume_execd_vforking_child_to_get_parent_vfork ()
622 {
623   return 1;                     /* Yes, the child must be resumed. */
624 }
625
626 void
627 require_notification_of_events (pid)
628      int pid;
629 {
630 #if defined(PT_SET_EVENT_MASK)
631   int pt_status;
632   ptrace_event_t ptrace_events;
633
634   /* Instruct the kernel as to the set of events we wish to be
635      informed of.  (This support does not exist before HPUX 10.0.
636      We'll assume if PT_SET_EVENT_MASK has not been defined by
637      <sys/ptrace.h>, then we're being built on pre-10.0.)  */
638   memset (&ptrace_events, 0, sizeof (ptrace_events));
639
640   /* Note: By default, all signals are visible to us.  If we wish
641      the kernel to keep certain signals hidden from us, we do it
642      by calling sigdelset (ptrace_events.pe_signals, signal) for
643      each such signal here, before doing PT_SET_EVENT_MASK.  */
644   sigemptyset (&ptrace_events.pe_signals);
645
646   ptrace_events.pe_set_event = 0;
647
648   ptrace_events.pe_set_event |= PTRACE_SIGNAL;
649   ptrace_events.pe_set_event |= PTRACE_EXEC;
650   ptrace_events.pe_set_event |= PTRACE_FORK;
651   ptrace_events.pe_set_event |= PTRACE_VFORK;
652   /* ??rehrauer: Add this one when we're prepared to catch it...
653      ptrace_events.pe_set_event |= PTRACE_EXIT;
654    */
655
656   errno = 0;
657   pt_status = call_ptrace (PT_SET_EVENT_MASK,
658                            pid,
659                            (PTRACE_ARG3_TYPE) & ptrace_events,
660                            sizeof (ptrace_events));
661   if (errno)
662     perror_with_name ("ptrace");
663   if (pt_status < 0)
664     return;
665 #endif
666 }
667
668 void
669 require_notification_of_exec_events (pid)
670      int pid;
671 {
672 #if defined(PT_SET_EVENT_MASK)
673   int pt_status;
674   ptrace_event_t ptrace_events;
675
676   /* Instruct the kernel as to the set of events we wish to be
677      informed of.  (This support does not exist before HPUX 10.0.
678      We'll assume if PT_SET_EVENT_MASK has not been defined by
679      <sys/ptrace.h>, then we're being built on pre-10.0.)  */
680   memset (&ptrace_events, 0, sizeof (ptrace_events));
681
682   /* Note: By default, all signals are visible to us.  If we wish
683      the kernel to keep certain signals hidden from us, we do it
684      by calling sigdelset (ptrace_events.pe_signals, signal) for
685      each such signal here, before doing PT_SET_EVENT_MASK.  */
686   sigemptyset (&ptrace_events.pe_signals);
687
688   ptrace_events.pe_set_event = 0;
689
690   ptrace_events.pe_set_event |= PTRACE_EXEC;
691   /* ??rehrauer: Add this one when we're prepared to catch it...
692      ptrace_events.pe_set_event |= PTRACE_EXIT;
693    */
694
695   errno = 0;
696   pt_status = call_ptrace (PT_SET_EVENT_MASK,
697                            pid,
698                            (PTRACE_ARG3_TYPE) & ptrace_events,
699                            sizeof (ptrace_events));
700   if (errno)
701     perror_with_name ("ptrace");
702   if (pt_status < 0)
703     return;
704 #endif
705 }
706
707 /* This function is called by the parent process, with pid being the
708    ID of the child process, after the debugger has forked.  */
709
710 void
711 child_acknowledge_created_inferior (pid)
712      int pid;
713 {
714   /* We need a memory home for a constant.  */
715   int tc_magic_parent = PT_VERSION;
716   int tc_magic_child = 0;
717
718   /* The remainder of this function is only useful for HPUX 10.0 and
719      later, as it depends upon the ability to request notification
720      of specific kinds of events by the kernel.  */
721 #if defined(PT_SET_EVENT_MASK)
722   /* Wait for the child to tell us that it has forked. */
723   read (startup_semaphore.child_channel[SEM_LISTEN],
724         &tc_magic_child,
725         sizeof (tc_magic_child));
726
727   /* Notify the child that it can exec.
728
729      In the infttrace.c variant of this function, we set the child's
730      event mask after the fork but before the exec.  In the ptrace
731      world, it seems we can't set the event mask until after the exec.  */
732   write (startup_semaphore.parent_channel[SEM_TALK],
733          &tc_magic_parent,
734          sizeof (tc_magic_parent));
735
736   /* We'd better pause a bit before trying to set the event mask,
737      though, to ensure that the exec has happened.  We don't want to
738      wait() on the child, because that'll screw up the upper layers
739      of gdb's execution control that expect to see the exec event.
740
741      After an exec, the child is no longer executing gdb code.  Hence,
742      we can't have yet another synchronization via the pipes.  We'll
743      just sleep for a second, and hope that's enough delay...  */
744   sleep (1);
745
746   /* Instruct the kernel as to the set of events we wish to be
747      informed of.  */
748   require_notification_of_exec_events (pid);
749
750   /* Discard our copy of the semaphore. */
751   (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
752   (void) close (startup_semaphore.parent_channel[SEM_TALK]);
753   (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
754   (void) close (startup_semaphore.child_channel[SEM_TALK]);
755 #endif
756 }
757
758 void
759 child_post_startup_inferior (pid)
760      int pid;
761 {
762   require_notification_of_events (pid);
763 }
764
765 void
766 child_post_attach (pid)
767      int pid;
768 {
769   require_notification_of_events (pid);
770 }
771
772 int
773 child_insert_fork_catchpoint (pid)
774      int pid;
775 {
776   /* This request is only available on HPUX 10.0 and later.  */
777 #if !defined(PT_SET_EVENT_MASK)
778   error ("Unable to catch forks prior to HPUX 10.0");
779 #else
780   /* Enable reporting of fork events from the kernel. */
781   /* ??rehrauer: For the moment, we're always enabling these events,
782      and just ignoring them if there's no catchpoint to catch them.  */
783   return 0;
784 #endif
785 }
786
787 int
788 child_remove_fork_catchpoint (pid)
789      int pid;
790 {
791   /* This request is only available on HPUX 10.0 and later.  */
792 #if !defined(PT_SET_EVENT_MASK)
793   error ("Unable to catch forks prior to HPUX 10.0");
794 #else
795   /* Disable reporting of fork events from the kernel. */
796   /* ??rehrauer: For the moment, we're always enabling these events,
797      and just ignoring them if there's no catchpoint to catch them.  */
798   return 0;
799 #endif
800 }
801
802 int
803 child_insert_vfork_catchpoint (pid)
804      int pid;
805 {
806   /* This request is only available on HPUX 10.0 and later.  */
807 #if !defined(PT_SET_EVENT_MASK)
808   error ("Unable to catch vforks prior to HPUX 10.0");
809 #else
810   /* Enable reporting of vfork events from the kernel. */
811   /* ??rehrauer: For the moment, we're always enabling these events,
812      and just ignoring them if there's no catchpoint to catch them.  */
813   return 0;
814 #endif
815 }
816
817 int
818 child_remove_vfork_catchpoint (pid)
819      int pid;
820 {
821   /* This request is only available on HPUX 10.0 and later.  */
822 #if !defined(PT_SET_EVENT_MASK)
823   error ("Unable to catch vforks prior to HPUX 10.0");
824 #else
825   /* Disable reporting of vfork events from the kernel. */
826   /* ??rehrauer: For the moment, we're always enabling these events,
827      and just ignoring them if there's no catchpoint to catch them.  */
828   return 0;
829 #endif
830 }
831
832 int
833 child_has_forked (pid, childpid)
834      int pid;
835      int *childpid;
836 {
837   /* This request is only available on HPUX 10.0 and later.  */
838 #if !defined(PT_GET_PROCESS_STATE)
839   *childpid = 0;
840   return 0;
841 #else
842   int pt_status;
843   ptrace_state_t ptrace_state;
844
845   errno = 0;
846   pt_status = call_ptrace (PT_GET_PROCESS_STATE,
847                            pid,
848                            (PTRACE_ARG3_TYPE) & ptrace_state,
849                            sizeof (ptrace_state));
850   if (errno)
851     perror_with_name ("ptrace");
852   if (pt_status < 0)
853     return 0;
854
855   if (ptrace_state.pe_report_event & PTRACE_FORK)
856     {
857       *childpid = ptrace_state.pe_other_pid;
858       return 1;
859     }
860
861   return 0;
862 #endif
863 }
864
865 int
866 child_has_vforked (pid, childpid)
867      int pid;
868      int *childpid;
869 {
870   /* This request is only available on HPUX 10.0 and later.  */
871 #if !defined(PT_GET_PROCESS_STATE)
872   *childpid = 0;
873   return 0;
874
875 #else
876   int pt_status;
877   ptrace_state_t ptrace_state;
878
879   errno = 0;
880   pt_status = call_ptrace (PT_GET_PROCESS_STATE,
881                            pid,
882                            (PTRACE_ARG3_TYPE) & ptrace_state,
883                            sizeof (ptrace_state));
884   if (errno)
885     perror_with_name ("ptrace");
886   if (pt_status < 0)
887     return 0;
888
889   if (ptrace_state.pe_report_event & PTRACE_VFORK)
890     {
891       *childpid = ptrace_state.pe_other_pid;
892       return 1;
893     }
894
895   return 0;
896 #endif
897 }
898
899 int
900 child_can_follow_vfork_prior_to_exec ()
901 {
902   /* ptrace doesn't allow this. */
903   return 0;
904 }
905
906 int
907 child_insert_exec_catchpoint (pid)
908      int pid;
909 {
910   /* This request is only available on HPUX 10.0 and later.   */
911 #if !defined(PT_SET_EVENT_MASK)
912   error ("Unable to catch execs prior to HPUX 10.0");
913
914 #else
915   /* Enable reporting of exec events from the kernel.  */
916   /* ??rehrauer: For the moment, we're always enabling these events,
917      and just ignoring them if there's no catchpoint to catch them.  */
918   return 0;
919 #endif
920 }
921
922 int
923 child_remove_exec_catchpoint (pid)
924      int pid;
925 {
926   /* This request is only available on HPUX 10.0 and later.  */
927 #if !defined(PT_SET_EVENT_MASK)
928   error ("Unable to catch execs prior to HPUX 10.0");
929
930 #else
931   /* Disable reporting of exec events from the kernel. */
932   /* ??rehrauer: For the moment, we're always enabling these events,
933      and just ignoring them if there's no catchpoint to catch them.  */
934   return 0;
935 #endif
936 }
937
938 int
939 child_has_execd (pid, execd_pathname)
940      int pid;
941      char **execd_pathname;
942 {
943   /* This request is only available on HPUX 10.0 and later.  */
944 #if !defined(PT_GET_PROCESS_STATE)
945   *execd_pathname = NULL;
946   return 0;
947
948 #else
949   int pt_status;
950   ptrace_state_t ptrace_state;
951
952   errno = 0;
953   pt_status = call_ptrace (PT_GET_PROCESS_STATE,
954                            pid,
955                            (PTRACE_ARG3_TYPE) & ptrace_state,
956                            sizeof (ptrace_state));
957   if (errno)
958     perror_with_name ("ptrace");
959   if (pt_status < 0)
960     return 0;
961
962   if (ptrace_state.pe_report_event & PTRACE_EXEC)
963     {
964       char *exec_file = target_pid_to_exec_file (pid);
965       *execd_pathname = savestring (exec_file, strlen (exec_file));
966       return 1;
967     }
968
969   return 0;
970 #endif
971 }
972
973 int
974 child_reported_exec_events_per_exec_call ()
975 {
976   return 2;                     /* ptrace reports the event twice per call. */
977 }
978
979 int
980 child_has_syscall_event (pid, kind, syscall_id)
981      int pid;
982      enum target_waitkind *kind;
983      int *syscall_id;
984 {
985   /* This request is only available on HPUX 10.30 and later, via
986      the ttrace interface.  */
987
988   *kind = TARGET_WAITKIND_SPURIOUS;
989   *syscall_id = -1;
990   return 0;
991 }
992
993 char *
994 child_pid_to_exec_file (pid)
995      int pid;
996 {
997   static char exec_file_buffer[1024];
998   int pt_status;
999   CORE_ADDR top_of_stack;
1000   char four_chars[4];
1001   int name_index;
1002   int i;
1003   int saved_inferior_pid;
1004   boolean done;
1005
1006 #ifdef PT_GET_PROCESS_PATHNAME
1007   /* As of 10.x HP-UX, there's an explicit request to get the pathname. */
1008   pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME,
1009                            pid,
1010                            (PTRACE_ARG3_TYPE) exec_file_buffer,
1011                            sizeof (exec_file_buffer) - 1);
1012   if (pt_status == 0)
1013     return exec_file_buffer;
1014 #endif
1015
1016   /* It appears that this request is broken prior to 10.30.
1017      If it fails, try a really, truly amazingly gross hack
1018      that DDE uses, of pawing through the process' data
1019      segment to find the pathname.  */
1020
1021   top_of_stack = 0x7b03a000;
1022   name_index = 0;
1023   done = 0;
1024
1025   /* On the chance that pid != inferior_pid, set inferior_pid
1026      to pid, so that (grrrr!) implicit uses of inferior_pid get
1027      the right id.  */
1028
1029   saved_inferior_pid = inferior_pid;
1030   inferior_pid = pid;
1031
1032   /* Try to grab a null-terminated string. */
1033   while (!done)
1034     {
1035       if (target_read_memory (top_of_stack, four_chars, 4) != 0)
1036         {
1037           inferior_pid = saved_inferior_pid;
1038           return NULL;
1039         }
1040       for (i = 0; i < 4; i++)
1041         {
1042           exec_file_buffer[name_index++] = four_chars[i];
1043           done = (four_chars[i] == '\0');
1044           if (done)
1045             break;
1046         }
1047       top_of_stack += 4;
1048     }
1049
1050   if (exec_file_buffer[0] == '\0')
1051     {
1052       inferior_pid = saved_inferior_pid;
1053       return NULL;
1054     }
1055
1056   inferior_pid = saved_inferior_pid;
1057   return exec_file_buffer;
1058 }
1059
1060 void
1061 pre_fork_inferior ()
1062 {
1063   int status;
1064
1065   status = pipe (startup_semaphore.parent_channel);
1066   if (status < 0)
1067     {
1068       warning ("error getting parent pipe for startup semaphore");
1069       return;
1070     }
1071
1072   status = pipe (startup_semaphore.child_channel);
1073   if (status < 0)
1074     {
1075       warning ("error getting child pipe for startup semaphore");
1076       return;
1077     }
1078 }
1079 \f
1080
1081 /* Check to see if the given thread is alive.
1082
1083    This is a no-op, as ptrace doesn't support threads, so we just
1084    return "TRUE".  */
1085
1086 int
1087 child_thread_alive (pid)
1088      int pid;
1089 {
1090   return 1;
1091 }
1092
1093 #endif /* ! GDB_NATIVE_HPUX_11 */