2004-07-28 Andrew Cagney <cagney@gnu.org>
[platform/upstream/binutils.git] / gdb / hppah-nat.c
1 /* Native support code for HPUX PA-RISC.
2    Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3    1998, 1999, 2000, 2001
4    Free Software Foundation, Inc.
5
6    Contributed by the Center for Software Science at the
7    University of Utah (pa-gdb-bugs@cs.utah.edu).
8
9    This file is part of GDB.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330,
24    Boston, MA 02111-1307, USA.  */
25
26
27 #include "defs.h"
28 #include "inferior.h"
29 #include "target.h"
30 #include <sys/ptrace.h>
31 #include "gdbcore.h"
32 #include "gdb_wait.h"
33 #include "regcache.h"
34 #include "gdb_string.h"
35 #include "infttrace.h"
36 #include <signal.h>
37
38 #include "hppa-tdep.h"
39
40 static CORE_ADDR text_end;
41
42 void
43 deprecated_hpux_text_end (struct target_ops *exec_ops)
44 {
45   struct section_table *p;
46
47   /* Set text_end to the highest address of the end of any readonly
48      code section.  */
49   /* FIXME: The comment above does not match the code.  The code
50      checks for sections with are either code *or* readonly.  */
51   text_end = (CORE_ADDR) 0;
52   for (p = exec_ops->to_sections; p < exec_ops->to_sections_end; p++)
53     if (bfd_get_section_flags (p->bfd, p->the_bfd_section)
54         & (SEC_CODE | SEC_READONLY))
55       {
56         if (text_end < p->endaddr)
57           text_end = p->endaddr;
58       }
59 }
60
61
62 static void fetch_register (int);
63
64 void
65 fetch_inferior_registers (int regno)
66 {
67   if (regno == -1)
68     for (regno = 0; regno < NUM_REGS; regno++)
69       fetch_register (regno);
70   else
71     fetch_register (regno);
72 }
73
74 /* Our own version of the offsetof macro, since we can't assume ANSI C.  */
75 #define HPPAH_OFFSETOF(type, member) ((int) (&((type *) 0)->member))
76
77 /* Store our register values back into the inferior.
78    If REGNO is -1, do this for all registers.
79    Otherwise, REGNO specifies which register (so we can save time).  */
80
81 void
82 store_inferior_registers (int regno)
83 {
84   unsigned int regaddr;
85   char buf[80];
86   int i;
87   unsigned int offset = U_REGS_OFFSET;
88   int scratch;
89
90   if (regno >= 0)
91     {
92       unsigned int addr, len, offset;
93
94       if (CANNOT_STORE_REGISTER (regno))
95         return;
96
97       offset = 0;
98       len = DEPRECATED_REGISTER_RAW_SIZE (regno);
99
100       /* Requests for register zero actually want the save_state's
101          ss_flags member.  As RM says: "Oh, what a hack!"  */
102       if (regno == 0)
103         {
104           save_state_t ss;
105           addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
106           len = sizeof (ss.ss_flags);
107
108           /* Note that ss_flags is always an int, no matter what
109              DEPRECATED_REGISTER_RAW_SIZE(0) says.  Assuming all HP-UX
110              PA machines are big-endian, put it at the least
111              significant end of the value, and zap the rest of the
112              buffer.  */
113           offset = DEPRECATED_REGISTER_RAW_SIZE (0) - len;
114         }
115
116       /* Floating-point registers come from the ss_fpblock area.  */
117       else if (regno >= HPPA_FP0_REGNUM)
118         addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock) 
119                 + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (HPPA_FP0_REGNUM)));
120
121       /* Wide registers come from the ss_wide area.
122          I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
123          between ss_wide and ss_narrow than to use the raw register size.
124          But checking ss_flags would require an extra ptrace call for
125          every register reference.  Bleah.  */
126       else if (len == 8)
127         addr = (HPPAH_OFFSETOF (save_state_t, ss_wide) 
128                 + DEPRECATED_REGISTER_BYTE (regno));
129
130       /* Narrow registers come from the ss_narrow area.  Note that
131          ss_narrow starts with gr1, not gr0.  */
132       else if (len == 4)
133         addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
134                 + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (1)));
135       else
136         internal_error (__FILE__, __LINE__,
137                         "hppah-nat.c (write_register): unexpected register size");
138
139 #ifdef GDB_TARGET_IS_HPPA_20W
140       /* Unbelieveable.  The PC head and tail must be written in 64bit hunks
141          or we will get an error.  Worse yet, the oddball ptrace/ttrace
142          layering will not allow us to perform a 64bit register store.
143
144          What a crock.  */
145       if (regno == HPPA_PCOQ_HEAD_REGNUM || regno == HPPA_PCOQ_TAIL_REGNUM && len == 8)
146         {
147           CORE_ADDR temp;
148
149           temp = *(CORE_ADDR *)&deprecated_registers[DEPRECATED_REGISTER_BYTE (regno)];
150
151           /* Set the priv level (stored in the low two bits of the PC.  */
152           temp |= 0x3;
153
154           ttrace_write_reg_64 (PIDGET (inferior_ptid), (CORE_ADDR)addr,
155                                (CORE_ADDR)&temp);
156
157           /* If we fail to write the PC, give a true error instead of
158              just a warning.  */
159           if (errno != 0)
160             {
161               char *err = safe_strerror (errno);
162               char *msg = alloca (strlen (err) + 128);
163               sprintf (msg, "writing `%s' register: %s",
164                         REGISTER_NAME (regno), err);
165               perror_with_name (msg);
166             }
167           return;
168         }
169
170       /* Another crock.  HPUX complains if you write a nonzero value to
171          the high part of IPSW.  What will it take for HP to catch a
172          clue about building sensible interfaces?  */
173      if (regno == HPPA_IPSW_REGNUM && len == 8)
174         *(int *)&deprecated_registers[DEPRECATED_REGISTER_BYTE (regno)] = 0;
175 #endif
176
177       for (i = 0; i < len; i += sizeof (int))
178         {
179           errno = 0;
180           call_ptrace (PT_WUREGS, PIDGET (inferior_ptid),
181                        (PTRACE_ARG3_TYPE) addr + i,
182                        *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (regno) + i]);
183           if (errno != 0)
184             {
185               /* Warning, not error, in case we are attached; sometimes
186                  the kernel doesn't let us at the registers. */
187               char *err = safe_strerror (errno);
188               char *msg = alloca (strlen (err) + 128);
189               sprintf (msg, "writing `%s' register: %s",
190                         REGISTER_NAME (regno), err);
191               /* If we fail to write the PC, give a true error instead of
192                  just a warning.  */
193               if (regno == HPPA_PCOQ_HEAD_REGNUM || regno == HPPA_PCOQ_TAIL_REGNUM)
194                 perror_with_name (msg);
195               else
196                 warning (msg);
197               return;
198             }
199         }
200     }
201   else
202     for (regno = 0; regno < NUM_REGS; regno++)
203       store_inferior_registers (regno);
204 }
205
206
207 /* Fetch a register's value from the process's U area.  */
208 static void
209 fetch_register (int regno)
210 {
211   char buf[MAX_REGISTER_SIZE];
212   unsigned int addr, len, offset;
213   int i;
214
215   offset = 0;
216   len = DEPRECATED_REGISTER_RAW_SIZE (regno);
217
218   /* Requests for register zero actually want the save_state's
219      ss_flags member.  As RM says: "Oh, what a hack!"  */
220   if (regno == 0)
221     {
222       save_state_t ss;
223       addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
224       len = sizeof (ss.ss_flags);
225
226       /* Note that ss_flags is always an int, no matter what
227          DEPRECATED_REGISTER_RAW_SIZE(0) says.  Assuming all HP-UX PA
228          machines are big-endian, put it at the least significant end
229          of the value, and zap the rest of the buffer.  */
230       offset = DEPRECATED_REGISTER_RAW_SIZE (0) - len;
231       memset (buf, 0, sizeof (buf));
232     }
233
234   /* Floating-point registers come from the ss_fpblock area.  */
235   else if (regno >= HPPA_FP0_REGNUM)
236     addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock) 
237             + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (HPPA_FP0_REGNUM)));
238
239   /* Wide registers come from the ss_wide area.
240      I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
241      between ss_wide and ss_narrow than to use the raw register size.
242      But checking ss_flags would require an extra ptrace call for
243      every register reference.  Bleah.  */
244   else if (len == 8)
245     addr = (HPPAH_OFFSETOF (save_state_t, ss_wide) 
246             + DEPRECATED_REGISTER_BYTE (regno));
247
248   /* Narrow registers come from the ss_narrow area.  Note that
249      ss_narrow starts with gr1, not gr0.  */
250   else if (len == 4)
251     addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
252             + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (1)));
253
254   else
255     internal_error (__FILE__, __LINE__,
256                     "hppa-nat.c (fetch_register): unexpected register size");
257
258   for (i = 0; i < len; i += sizeof (int))
259     {
260       errno = 0;
261       /* Copy an int from the U area to buf.  Fill the least
262          significant end if len != raw_size.  */
263       * (int *) &buf[offset + i] =
264           call_ptrace (PT_RUREGS, PIDGET (inferior_ptid),
265                        (PTRACE_ARG3_TYPE) addr + i, 0);
266       if (errno != 0)
267         {
268           /* Warning, not error, in case we are attached; sometimes
269              the kernel doesn't let us at the registers. */
270           char *err = safe_strerror (errno);
271           char *msg = alloca (strlen (err) + 128);
272           sprintf (msg, "reading `%s' register: %s",
273                    REGISTER_NAME (regno), err);
274           warning (msg);
275           return;
276         }
277     }
278
279   /* If we're reading an address from the instruction address queue,
280      mask out the bottom two bits --- they contain the privilege
281      level.  */
282   if (regno == HPPA_PCOQ_HEAD_REGNUM || regno == HPPA_PCOQ_TAIL_REGNUM)
283     buf[len - 1] &= ~0x3;
284
285   regcache_raw_supply (current_regcache, regno, buf);
286 }
287
288
289 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
290    to debugger memory starting at MYADDR.   Copy to inferior if
291    WRITE is nonzero.
292
293    Returns the length copied, which is either the LEN argument or zero.
294    This xfer function does not do partial moves, since child_ops
295    doesn't allow memory operations to cross below us in the target stack
296    anyway.  TARGET is ignored.  */
297
298 int
299 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
300                    struct mem_attrib *mem,
301                    struct target_ops *target)
302 {
303   int i;
304   /* Round starting address down to longword boundary.  */
305   CORE_ADDR addr = memaddr & - (CORE_ADDR)(sizeof (int));
306   /* Round ending address up; get number of longwords that makes.  */
307   int count
308   = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
309
310   /* Allocate buffer of that many longwords.
311      Note -- do not use alloca to allocate this buffer since there is no
312      guarantee of when the buffer will actually be deallocated.
313
314      This routine can be called over and over with the same call chain;
315      this (in effect) would pile up all those alloca requests until a call
316      to alloca was made from a point higher than this routine in the
317      call chain.  */
318   int *buffer = (int *) xmalloc (count * sizeof (int));
319
320   if (write)
321     {
322       /* Fill start and end extra bytes of buffer with existing memory data.  */
323       if (addr != memaddr || len < (int) sizeof (int))
324         {
325           /* Need part of initial word -- fetch it.  */
326           buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
327                                    PIDGET (inferior_ptid),
328                                    (PTRACE_ARG3_TYPE) addr, 0);
329         }
330
331       if (count > 1)            /* FIXME, avoid if even boundary */
332         {
333           buffer[count - 1]
334             = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
335                            PIDGET (inferior_ptid),
336                            (PTRACE_ARG3_TYPE) (addr
337                                                + (count - 1) * sizeof (int)),
338                            0);
339         }
340
341       /* Copy data to be written over corresponding part of buffer */
342       memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
343
344       /* Write the entire buffer.  */
345       for (i = 0; i < count; i++, addr += sizeof (int))
346         {
347           int pt_status;
348           int pt_request;
349           /* The HP-UX kernel crashes if you use PT_WDUSER to write into the
350              text segment.  FIXME -- does it work to write into the data
351              segment using WIUSER, or do these idiots really expect us to
352              figure out which segment the address is in, so we can use a
353              separate system call for it??!  */
354           errno = 0;
355           pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER;
356           pt_status = call_ptrace (pt_request,
357                                    PIDGET (inferior_ptid),
358                                    (PTRACE_ARG3_TYPE) addr,
359                                    buffer[i]);
360
361           /* Did we fail?  Might we've guessed wrong about which
362              segment this address resides in?  Try the other request,
363              and see if that works...  */
364           if ((pt_status == -1) && errno)
365             {
366               errno = 0;
367               pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER;
368               pt_status = call_ptrace (pt_request,
369                                        PIDGET (inferior_ptid),
370                                        (PTRACE_ARG3_TYPE) addr,
371                                        buffer[i]);
372
373               /* No, we still fail.  Okay, time to punt. */
374               if ((pt_status == -1) && errno)
375                 {
376                   xfree (buffer);
377                   return 0;
378                 }
379             }
380         }
381     }
382   else
383     {
384       /* Read all the longwords */
385       for (i = 0; i < count; i++, addr += sizeof (int))
386         {
387           errno = 0;
388           buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
389                                    PIDGET (inferior_ptid),
390                                    (PTRACE_ARG3_TYPE) addr, 0);
391           if (errno)
392             {
393               xfree (buffer);
394               return 0;
395             }
396           QUIT;
397         }
398
399       /* Copy appropriate bytes out of the buffer.  */
400       memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
401     }
402   xfree (buffer);
403   return len;
404 }
405
406 char *saved_child_execd_pathname = NULL;
407 int saved_vfork_pid;
408 enum {
409   STATE_NONE,
410   STATE_GOT_CHILD,
411   STATE_GOT_EXEC,
412   STATE_GOT_PARENT,
413   STATE_FAKE_EXEC
414 } saved_vfork_state = STATE_NONE;
415
416 int
417 child_follow_fork (int follow_child)
418 {
419   ptid_t last_ptid;
420   struct target_waitstatus last_status;
421   int has_vforked;
422   int parent_pid, child_pid;
423
424   get_last_target_status (&last_ptid, &last_status);
425   has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
426   parent_pid = ptid_get_pid (last_ptid);
427   child_pid = last_status.value.related_pid;
428
429   /* At this point, if we are vforking, breakpoints were already
430      detached from the child in child_wait; and the child has already
431      called execve().  If we are forking, both the parent and child
432      have breakpoints inserted.  */
433
434   if (! follow_child)
435     {
436       if (! has_vforked)
437         {
438           detach_breakpoints (child_pid);
439 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
440           SOLIB_REMOVE_INFERIOR_HOOK (child_pid);
441 #endif
442         }
443
444       /* Detach from the child. */
445       printf_unfiltered ("Detaching after fork from %s\n",
446                          target_pid_to_str (pid_to_ptid (child_pid)));
447       hppa_require_detach (child_pid, 0);
448
449       /* The parent and child of a vfork share the same address space.
450          Also, on some targets the order in which vfork and exec events
451          are received for parent in child requires some delicate handling
452          of the events.
453
454          For instance, on ptrace-based HPUX we receive the child's vfork
455          event first, at which time the parent has been suspended by the
456          OS and is essentially untouchable until the child's exit or second
457          exec event arrives.  At that time, the parent's vfork event is
458          delivered to us, and that's when we see and decide how to follow
459          the vfork.  But to get to that point, we must continue the child
460          until it execs or exits.  To do that smoothly, all breakpoints
461          must be removed from the child, in case there are any set between
462          the vfork() and exec() calls.  But removing them from the child
463          also removes them from the parent, due to the shared-address-space
464          nature of a vfork'd parent and child.  On HPUX, therefore, we must
465          take care to restore the bp's to the parent before we continue it.
466          Else, it's likely that we may not stop in the expected place.  (The
467          worst scenario is when the user tries to step over a vfork() call;
468          the step-resume bp must be restored for the step to properly stop
469          in the parent after the call completes!)
470
471          Sequence of events, as reported to gdb from HPUX:
472
473          Parent        Child           Action for gdb to take
474          -------------------------------------------------------
475          1                VFORK               Continue child
476          2                EXEC
477          3                EXEC or EXIT
478          4  VFORK
479
480          Now that the child has safely exec'd or exited, we must restore
481          the parent's breakpoints before we continue it.  Else, we may
482          cause it run past expected stopping points.  */
483
484       if (has_vforked)
485         reattach_breakpoints (parent_pid);
486     }
487   else
488     {
489       /* Needed to keep the breakpoint lists in sync.  */
490       if (! has_vforked)
491         detach_breakpoints (child_pid);
492
493       /* Before detaching from the parent, remove all breakpoints from it. */
494       remove_breakpoints ();
495
496       /* Also reset the solib inferior hook from the parent. */
497 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
498       SOLIB_REMOVE_INFERIOR_HOOK (PIDGET (inferior_ptid));
499 #endif
500
501       /* Detach from the parent. */
502       target_detach (NULL, 1);
503
504       /* Attach to the child. */
505       printf_unfiltered ("Attaching after fork to %s\n",
506                          target_pid_to_str (pid_to_ptid (child_pid)));
507       hppa_require_attach (child_pid);
508       inferior_ptid = pid_to_ptid (child_pid);
509
510       /* If we vforked, then we've also execed by now.  The exec will be
511          reported momentarily.  follow_exec () will handle breakpoints, so
512          we don't have to..  */
513       if (!has_vforked)
514         follow_inferior_reset_breakpoints ();
515     }
516
517   if (has_vforked)
518     {
519       /* If we followed the parent, don't try to follow the child's exec.  */
520       if (saved_vfork_state != STATE_GOT_PARENT
521           && saved_vfork_state != STATE_FAKE_EXEC)
522         fprintf_unfiltered (gdb_stdout,
523                             "hppa: post follow vfork: confused state\n");
524
525       if (! follow_child || saved_vfork_state == STATE_GOT_PARENT)
526         saved_vfork_state = STATE_NONE;
527       else
528         return 1;
529     }
530   return 0;
531 }
532
533 /* Format a process id, given PID.  Be sure to terminate
534    this with a null--it's going to be printed via a "%s".  */
535 char *
536 child_pid_to_str (ptid_t ptid)
537 {
538   /* Static because address returned */
539   static char buf[30];
540   pid_t pid = PIDGET (ptid);
541
542   /* Extra NUL for paranoia's sake */
543   sprintf (buf, "process %d%c", pid, '\0');
544
545   return buf;
546 }
547
548 /* Format a thread id, given TID.  Be sure to terminate
549    this with a null--it's going to be printed via a "%s".
550
551    Note: This is a core-gdb tid, not the actual system tid.
552    See infttrace.c for details.  */
553 char *
554 hppa_tid_to_str (ptid_t ptid)
555 {
556   /* Static because address returned */
557   static char buf[30];
558   /* This seems strange, but when I did the ptid conversion, it looked
559      as though a pid was always being passed.  - Kevin Buettner  */
560   pid_t tid = PIDGET (ptid);
561
562   /* Extra NULLs for paranoia's sake */
563   sprintf (buf, "system thread %d%c", tid, '\0');
564
565   return buf;
566 }
567
568 /*## */
569 /* Enable HACK for ttrace work.  In
570  * infttrace.c/require_notification_of_events,
571  * this is set to 0 so that the loop in child_wait
572  * won't loop.
573  */
574 int not_same_real_pid = 1;
575 /*## */
576
577 /* Wait for child to do something.  Return pid of child, or -1 in case
578    of error; store status through argument pointer OURSTATUS.  */
579
580 ptid_t
581 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
582 {
583   int save_errno;
584   int status;
585   char *execd_pathname = NULL;
586   int exit_status;
587   int related_pid;
588   int syscall_id;
589   enum target_waitkind kind;
590   int pid;
591
592   if (saved_vfork_state == STATE_FAKE_EXEC)
593     {
594       saved_vfork_state = STATE_NONE;
595       ourstatus->kind = TARGET_WAITKIND_EXECD;
596       ourstatus->value.execd_pathname = saved_child_execd_pathname;
597       return inferior_ptid;
598     }
599
600   do
601     {
602       set_sigint_trap ();       /* Causes SIGINT to be passed on to the
603                                    attached process. */
604       set_sigio_trap ();
605
606       pid = ptrace_wait (inferior_ptid, &status);
607
608       save_errno = errno;
609
610       clear_sigio_trap ();
611
612       clear_sigint_trap ();
613
614       if (pid == -1)
615         {
616           if (save_errno == EINTR)
617             continue;
618
619           fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
620                               safe_strerror (save_errno));
621
622           /* Claim it exited with unknown signal.  */
623           ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
624           ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
625           return pid_to_ptid (-1);
626         }
627
628       /* Did it exit?
629        */
630       if (target_has_exited (pid, status, &exit_status))
631         {
632           /* ??rehrauer: For now, ignore this. */
633           continue;
634         }
635
636       if (!target_thread_alive (pid_to_ptid (pid)))
637         {
638           ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
639           return pid_to_ptid (pid);
640         }
641
642       if (hpux_has_forked (pid, &related_pid))
643         {
644           /* Ignore the parent's fork event.  */
645           if (pid == PIDGET (inferior_ptid))
646             {
647               ourstatus->kind = TARGET_WAITKIND_IGNORE;
648               return inferior_ptid;
649             }
650
651           /* If this is the child's fork event, report that the
652              process has forked.  */
653           if (related_pid == PIDGET (inferior_ptid))
654             {
655               ourstatus->kind = TARGET_WAITKIND_FORKED;
656               ourstatus->value.related_pid = pid;
657               return inferior_ptid;
658             }
659         }
660
661       if (hpux_has_vforked (pid, &related_pid))
662         {
663           if (pid == PIDGET (inferior_ptid))
664             {
665               if (saved_vfork_state == STATE_GOT_CHILD)
666                 saved_vfork_state = STATE_GOT_PARENT;
667               else if (saved_vfork_state == STATE_GOT_EXEC)
668                 saved_vfork_state = STATE_FAKE_EXEC;
669               else
670                 fprintf_unfiltered (gdb_stdout,
671                                     "hppah: parent vfork: confused\n");
672             }
673           else if (related_pid == PIDGET (inferior_ptid))
674             {
675               if (saved_vfork_state == STATE_NONE)
676                 saved_vfork_state = STATE_GOT_CHILD;
677               else
678                 fprintf_unfiltered (gdb_stdout,
679                                     "hppah: child vfork: confused\n");
680             }
681           else
682             fprintf_unfiltered (gdb_stdout,
683                                 "hppah: unknown vfork: confused\n");
684
685           if (saved_vfork_state == STATE_GOT_CHILD)
686             {
687               child_post_startup_inferior (pid_to_ptid (pid));
688               detach_breakpoints (pid);
689 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
690               SOLIB_REMOVE_INFERIOR_HOOK (pid);
691 #endif
692               child_resume (pid_to_ptid (pid), 0, TARGET_SIGNAL_0);
693               ourstatus->kind = TARGET_WAITKIND_IGNORE;
694               return pid_to_ptid (related_pid);
695             }
696           else if (saved_vfork_state == STATE_FAKE_EXEC)
697             {
698               ourstatus->kind = TARGET_WAITKIND_VFORKED;
699               ourstatus->value.related_pid = related_pid;
700               return pid_to_ptid (pid);
701             }
702           else
703             {
704               /* We saw the parent's vfork, but we haven't seen the exec yet.
705                  Wait for it, for simplicity's sake.  It should be pending.  */
706               saved_vfork_pid = related_pid;
707               ourstatus->kind = TARGET_WAITKIND_IGNORE;
708               return pid_to_ptid (pid);
709             }
710         }
711
712       if (hpux_has_execd (pid, &execd_pathname))
713         {
714           /* On HP-UX, events associated with a vforking inferior come in
715              threes: a vfork event for the child (always first), followed
716              a vfork event for the parent and an exec event for the child.
717              The latter two can come in either order.  Make sure we get
718              both.  */
719           if (saved_vfork_state != STATE_NONE)
720             {
721               if (saved_vfork_state == STATE_GOT_CHILD)
722                 {
723                   saved_vfork_state = STATE_GOT_EXEC;
724                   /* On HP/UX with ptrace, the child must be resumed before
725                      the parent vfork event is delivered.  A single-step
726                      suffices.  */
727                   if (RESUME_EXECD_VFORKING_CHILD_TO_GET_PARENT_VFORK ())
728                     target_resume (pid_to_ptid (pid), 1, TARGET_SIGNAL_0);
729                   ourstatus->kind = TARGET_WAITKIND_IGNORE;
730                 }
731               else if (saved_vfork_state == STATE_GOT_PARENT)
732                 {
733                   saved_vfork_state = STATE_FAKE_EXEC;
734                   ourstatus->kind = TARGET_WAITKIND_VFORKED;
735                   ourstatus->value.related_pid = saved_vfork_pid;
736                 }
737               else
738                 fprintf_unfiltered (gdb_stdout,
739                                     "hppa: exec: unexpected state\n");
740
741               saved_child_execd_pathname = execd_pathname;
742
743               return inferior_ptid;
744             }
745           
746           /* Are we ignoring initial exec events?  (This is likely because
747              we're in the process of starting up the inferior, and another
748              (older) mechanism handles those.)  If so, we'll report this
749              as a regular stop, not an exec.
750            */
751           if (inferior_ignoring_startup_exec_events)
752             {
753               inferior_ignoring_startup_exec_events--;
754             }
755           else
756             {
757               ourstatus->kind = TARGET_WAITKIND_EXECD;
758               ourstatus->value.execd_pathname = execd_pathname;
759               return pid_to_ptid (pid);
760             }
761         }
762
763       /* All we must do with these is communicate their occurrence
764          to wait_for_inferior...
765        */
766       if (hpux_has_syscall_event (pid, &kind, &syscall_id))
767         {
768           ourstatus->kind = kind;
769           ourstatus->value.syscall_id = syscall_id;
770           return pid_to_ptid (pid);
771         }
772
773       /*##  } while (pid != PIDGET (inferior_ptid)); ## *//* Some other child died or stopped */
774 /* hack for thread testing */
775     }
776   while ((pid != PIDGET (inferior_ptid)) && not_same_real_pid);
777 /*## */
778
779   store_waitstatus (ourstatus, status);
780   return pid_to_ptid (pid);
781 }
782
783 #if !defined (GDB_NATIVE_HPUX_11)
784
785 /* The following code is a substitute for the infttrace.c versions used
786    with ttrace() in HPUX 11.  */
787
788 /* This value is an arbitrary integer. */
789 #define PT_VERSION 123456
790
791 /* This semaphore is used to coordinate the child and parent processes
792    after a fork(), and before an exec() by the child.  See
793    parent_attach_all for details.  */
794
795 typedef struct
796 {
797   int parent_channel[2];        /* Parent "talks" to [1], child "listens" to [0] */
798   int child_channel[2];         /* Child "talks" to [1], parent "listens" to [0] */
799 }
800 startup_semaphore_t;
801
802 #define SEM_TALK (1)
803 #define SEM_LISTEN (0)
804
805 static startup_semaphore_t startup_semaphore;
806
807 #ifdef PT_SETTRC
808 /* This function causes the caller's process to be traced by its
809    parent.  This is intended to be called after GDB forks itself,
810    and before the child execs the target.
811
812    Note that HP-UX ptrace is rather funky in how this is done.
813    If the parent wants to get the initial exec event of a child,
814    it must set the ptrace event mask of the child to include execs.
815    (The child cannot do this itself.)  This must be done after the
816    child is forked, but before it execs.
817
818    To coordinate the parent and child, we implement a semaphore using
819    pipes.  After SETTRC'ing itself, the child tells the parent that
820    it is now traceable by the parent, and waits for the parent's
821    acknowledgement.  The parent can then set the child's event mask,
822    and notify the child that it can now exec.
823
824    (The acknowledgement by parent happens as a result of a call to
825    child_acknowledge_created_inferior.)  */
826
827 int
828 parent_attach_all (int pid, PTRACE_ARG3_TYPE addr, int data)
829 {
830   int pt_status = 0;
831
832   /* We need a memory home for a constant.  */
833   int tc_magic_child = PT_VERSION;
834   int tc_magic_parent = 0;
835
836   /* The remainder of this function is only useful for HPUX 10.0 and
837      later, as it depends upon the ability to request notification
838      of specific kinds of events by the kernel.  */
839 #if defined(PT_SET_EVENT_MASK)
840
841   /* Notify the parent that we're potentially ready to exec(). */
842   write (startup_semaphore.child_channel[SEM_TALK],
843          &tc_magic_child,
844          sizeof (tc_magic_child));
845
846   /* Wait for acknowledgement from the parent. */
847   read (startup_semaphore.parent_channel[SEM_LISTEN],
848         &tc_magic_parent,
849         sizeof (tc_magic_parent));
850   if (tc_magic_child != tc_magic_parent)
851     warning ("mismatched semaphore magic");
852
853   /* Discard our copy of the semaphore. */
854   (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
855   (void) close (startup_semaphore.parent_channel[SEM_TALK]);
856   (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
857   (void) close (startup_semaphore.child_channel[SEM_TALK]);
858 #endif
859
860   return 0;
861 }
862 #endif
863
864 int
865 hppa_require_attach (int pid)
866 {
867   int pt_status;
868   CORE_ADDR pc;
869   CORE_ADDR pc_addr;
870   unsigned int regs_offset;
871
872   /* Are we already attached?  There appears to be no explicit way to
873      answer this via ptrace, so we try something which should be
874      innocuous if we are attached.  If that fails, then we assume
875      we're not attached, and so attempt to make it so. */
876
877   errno = 0;
878   regs_offset = U_REGS_OFFSET;
879   pc_addr = register_addr (PC_REGNUM, regs_offset);
880   pc = call_ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE) pc_addr, 0);
881
882   if (errno)
883     {
884       errno = 0;
885       pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
886
887       if (errno)
888         return -1;
889
890       /* Now we really are attached. */
891       errno = 0;
892     }
893   attach_flag = 1;
894   return pid;
895 }
896
897 int
898 hppa_require_detach (int pid, int signal)
899 {
900   errno = 0;
901   call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal);
902   errno = 0;                    /* Ignore any errors. */
903   return pid;
904 }
905
906 /* Since ptrace doesn't support memory page-protection events, which
907    are used to implement "hardware" watchpoints on HP-UX, these are
908    dummy versions, which perform no useful work.  */
909
910 void
911 hppa_enable_page_protection_events (int pid)
912 {
913 }
914
915 void
916 hppa_disable_page_protection_events (int pid)
917 {
918 }
919
920 int
921 hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
922 {
923   error ("Hardware watchpoints not implemented on this platform.");
924 }
925
926 int
927 hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
928 {
929   error ("Hardware watchpoints not implemented on this platform.");
930 }
931
932 int
933 hppa_can_use_hw_watchpoint (int type, int cnt, int ot)
934 {
935   return 0;
936 }
937
938 int
939 hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
940 {
941   error ("Hardware watchpoints not implemented on this platform.");
942 }
943
944 char *
945 hppa_pid_or_tid_to_str (ptid_t id)
946 {
947   /* In the ptrace world, there are only processes. */
948   return child_pid_to_str (id);
949 }
950
951 void
952 hppa_ensure_vforking_parent_remains_stopped (int pid)
953 {
954   /* This assumes that the vforked parent is presently stopped, and
955      that the vforked child has just delivered its first exec event.
956      Calling kill() this way will cause the SIGTRAP to be delivered as
957      soon as the parent is resumed, which happens as soon as the
958      vforked child is resumed.  See wait_for_inferior for the use of
959      this function.  */
960   kill (pid, SIGTRAP);
961 }
962
963 int
964 hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
965 {
966   return 1;                     /* Yes, the child must be resumed. */
967 }
968
969 void
970 require_notification_of_events (int pid)
971 {
972 #if defined(PT_SET_EVENT_MASK)
973   int pt_status;
974   ptrace_event_t ptrace_events;
975   int nsigs;
976   int signum;
977
978   /* Instruct the kernel as to the set of events we wish to be
979      informed of.  (This support does not exist before HPUX 10.0.
980      We'll assume if PT_SET_EVENT_MASK has not been defined by
981      <sys/ptrace.h>, then we're being built on pre-10.0.)  */
982   memset (&ptrace_events, 0, sizeof (ptrace_events));
983
984   /* Note: By default, all signals are visible to us.  If we wish
985      the kernel to keep certain signals hidden from us, we do it
986      by calling sigdelset (ptrace_events.pe_signals, signal) for
987      each such signal here, before doing PT_SET_EVENT_MASK.  */
988   /* RM: The above comment is no longer true. We start with ignoring
989      all signals, and then add the ones we are interested in. We could
990      do it the other way: start by looking at all signals and then
991      deleting the ones that we aren't interested in, except that
992      multiple gdb signals may be mapped to the same host signal
993      (eg. TARGET_SIGNAL_IO and TARGET_SIGNAL_POLL both get mapped to
994      signal 22 on HPUX 10.20) We want to be notified if we are
995      interested in either signal.  */
996   sigfillset (&ptrace_events.pe_signals);
997
998   /* RM: Let's not bother with signals we don't care about */
999   nsigs = (int) TARGET_SIGNAL_LAST;
1000   for (signum = nsigs; signum > 0; signum--)
1001     {
1002       if ((signal_stop_state (signum)) ||
1003           (signal_print_state (signum)) ||
1004           (!signal_pass_state (signum)))
1005         {
1006           if (target_signal_to_host_p (signum))
1007             sigdelset (&ptrace_events.pe_signals,
1008                        target_signal_to_host (signum));
1009         }
1010     }
1011
1012   ptrace_events.pe_set_event = 0;
1013
1014   ptrace_events.pe_set_event |= PTRACE_SIGNAL;
1015   ptrace_events.pe_set_event |= PTRACE_EXEC;
1016   ptrace_events.pe_set_event |= PTRACE_FORK;
1017   ptrace_events.pe_set_event |= PTRACE_VFORK;
1018   /* ??rehrauer: Add this one when we're prepared to catch it...
1019      ptrace_events.pe_set_event |= PTRACE_EXIT;
1020    */
1021
1022   errno = 0;
1023   pt_status = call_ptrace (PT_SET_EVENT_MASK,
1024                            pid,
1025                            (PTRACE_ARG3_TYPE) & ptrace_events,
1026                            sizeof (ptrace_events));
1027   if (errno)
1028     perror_with_name ("ptrace");
1029   if (pt_status < 0)
1030     return;
1031 #endif
1032 }
1033
1034 void
1035 require_notification_of_exec_events (int pid)
1036 {
1037 #if defined(PT_SET_EVENT_MASK)
1038   int pt_status;
1039   ptrace_event_t ptrace_events;
1040
1041   /* Instruct the kernel as to the set of events we wish to be
1042      informed of.  (This support does not exist before HPUX 10.0.
1043      We'll assume if PT_SET_EVENT_MASK has not been defined by
1044      <sys/ptrace.h>, then we're being built on pre-10.0.)  */
1045   memset (&ptrace_events, 0, sizeof (ptrace_events));
1046
1047   /* Note: By default, all signals are visible to us.  If we wish
1048      the kernel to keep certain signals hidden from us, we do it
1049      by calling sigdelset (ptrace_events.pe_signals, signal) for
1050      each such signal here, before doing PT_SET_EVENT_MASK.  */
1051   sigemptyset (&ptrace_events.pe_signals);
1052
1053   ptrace_events.pe_set_event = 0;
1054
1055   ptrace_events.pe_set_event |= PTRACE_EXEC;
1056   /* ??rehrauer: Add this one when we're prepared to catch it...
1057      ptrace_events.pe_set_event |= PTRACE_EXIT;
1058    */
1059
1060   errno = 0;
1061   pt_status = call_ptrace (PT_SET_EVENT_MASK,
1062                            pid,
1063                            (PTRACE_ARG3_TYPE) & ptrace_events,
1064                            sizeof (ptrace_events));
1065   if (errno)
1066     perror_with_name ("ptrace");
1067   if (pt_status < 0)
1068     return;
1069 #endif
1070 }
1071
1072 /* This function is called by the parent process, with pid being the
1073    ID of the child process, after the debugger has forked.  */
1074
1075 void
1076 child_acknowledge_created_inferior (int pid)
1077 {
1078   /* We need a memory home for a constant.  */
1079   int tc_magic_parent = PT_VERSION;
1080   int tc_magic_child = 0;
1081
1082   /* The remainder of this function is only useful for HPUX 10.0 and
1083      later, as it depends upon the ability to request notification
1084      of specific kinds of events by the kernel.  */
1085 #if defined(PT_SET_EVENT_MASK)
1086   /* Wait for the child to tell us that it has forked. */
1087   read (startup_semaphore.child_channel[SEM_LISTEN],
1088         &tc_magic_child,
1089         sizeof (tc_magic_child));
1090
1091   /* Notify the child that it can exec.
1092
1093      In the infttrace.c variant of this function, we set the child's
1094      event mask after the fork but before the exec.  In the ptrace
1095      world, it seems we can't set the event mask until after the exec.  */
1096   write (startup_semaphore.parent_channel[SEM_TALK],
1097          &tc_magic_parent,
1098          sizeof (tc_magic_parent));
1099
1100   /* We'd better pause a bit before trying to set the event mask,
1101      though, to ensure that the exec has happened.  We don't want to
1102      wait() on the child, because that'll screw up the upper layers
1103      of gdb's execution control that expect to see the exec event.
1104
1105      After an exec, the child is no longer executing gdb code.  Hence,
1106      we can't have yet another synchronization via the pipes.  We'll
1107      just sleep for a second, and hope that's enough delay...  */
1108   sleep (1);
1109
1110   /* Instruct the kernel as to the set of events we wish to be
1111      informed of.  */
1112   require_notification_of_exec_events (pid);
1113
1114   /* Discard our copy of the semaphore. */
1115   (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
1116   (void) close (startup_semaphore.parent_channel[SEM_TALK]);
1117   (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
1118   (void) close (startup_semaphore.child_channel[SEM_TALK]);
1119 #endif
1120 }
1121
1122 void
1123 child_post_startup_inferior (ptid_t ptid)
1124 {
1125   require_notification_of_events (PIDGET (ptid));
1126 }
1127
1128 void
1129 child_post_attach (int pid)
1130 {
1131   require_notification_of_events (pid);
1132 }
1133
1134 int
1135 child_insert_fork_catchpoint (int pid)
1136 {
1137   /* This request is only available on HPUX 10.0 and later.  */
1138 #if !defined(PT_SET_EVENT_MASK)
1139   error ("Unable to catch forks prior to HPUX 10.0");
1140 #else
1141   /* Enable reporting of fork events from the kernel. */
1142   /* ??rehrauer: For the moment, we're always enabling these events,
1143      and just ignoring them if there's no catchpoint to catch them.  */
1144   return 0;
1145 #endif
1146 }
1147
1148 int
1149 child_remove_fork_catchpoint (int pid)
1150 {
1151   /* This request is only available on HPUX 10.0 and later.  */
1152 #if !defined(PT_SET_EVENT_MASK)
1153   error ("Unable to catch forks prior to HPUX 10.0");
1154 #else
1155   /* Disable reporting of fork events from the kernel. */
1156   /* ??rehrauer: For the moment, we're always enabling these events,
1157      and just ignoring them if there's no catchpoint to catch them.  */
1158   return 0;
1159 #endif
1160 }
1161
1162 int
1163 child_insert_vfork_catchpoint (int pid)
1164 {
1165   /* This request is only available on HPUX 10.0 and later.  */
1166 #if !defined(PT_SET_EVENT_MASK)
1167   error ("Unable to catch vforks prior to HPUX 10.0");
1168 #else
1169   /* Enable reporting of vfork events from the kernel. */
1170   /* ??rehrauer: For the moment, we're always enabling these events,
1171      and just ignoring them if there's no catchpoint to catch them.  */
1172   return 0;
1173 #endif
1174 }
1175
1176 int
1177 child_remove_vfork_catchpoint (int pid)
1178 {
1179   /* This request is only available on HPUX 10.0 and later.  */
1180 #if !defined(PT_SET_EVENT_MASK)
1181   error ("Unable to catch vforks prior to HPUX 10.0");
1182 #else
1183   /* Disable reporting of vfork events from the kernel. */
1184   /* ??rehrauer: For the moment, we're always enabling these events,
1185      and just ignoring them if there's no catchpoint to catch them.  */
1186   return 0;
1187 #endif
1188 }
1189
1190 int
1191 hpux_has_forked (int pid, int *childpid)
1192 {
1193   /* This request is only available on HPUX 10.0 and later.  */
1194 #if !defined(PT_GET_PROCESS_STATE)
1195   *childpid = 0;
1196   return 0;
1197 #else
1198   int pt_status;
1199   ptrace_state_t ptrace_state;
1200
1201   errno = 0;
1202   pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1203                            pid,
1204                            (PTRACE_ARG3_TYPE) & ptrace_state,
1205                            sizeof (ptrace_state));
1206   if (errno)
1207     perror_with_name ("ptrace");
1208   if (pt_status < 0)
1209     return 0;
1210
1211   if (ptrace_state.pe_report_event & PTRACE_FORK)
1212     {
1213       *childpid = ptrace_state.pe_other_pid;
1214       return 1;
1215     }
1216
1217   return 0;
1218 #endif
1219 }
1220
1221 int
1222 hpux_has_vforked (int pid, int *childpid)
1223 {
1224   /* This request is only available on HPUX 10.0 and later.  */
1225 #if !defined(PT_GET_PROCESS_STATE)
1226   *childpid = 0;
1227   return 0;
1228
1229 #else
1230   int pt_status;
1231   ptrace_state_t ptrace_state;
1232
1233   errno = 0;
1234   pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1235                            pid,
1236                            (PTRACE_ARG3_TYPE) & ptrace_state,
1237                            sizeof (ptrace_state));
1238   if (errno)
1239     perror_with_name ("ptrace");
1240   if (pt_status < 0)
1241     return 0;
1242
1243   if (ptrace_state.pe_report_event & PTRACE_VFORK)
1244     {
1245       *childpid = ptrace_state.pe_other_pid;
1246       return 1;
1247     }
1248
1249   return 0;
1250 #endif
1251 }
1252
1253 int
1254 child_insert_exec_catchpoint (int pid)
1255 {
1256   /* This request is only available on HPUX 10.0 and later.   */
1257 #if !defined(PT_SET_EVENT_MASK)
1258   error ("Unable to catch execs prior to HPUX 10.0");
1259
1260 #else
1261   /* Enable reporting of exec events from the kernel.  */
1262   /* ??rehrauer: For the moment, we're always enabling these events,
1263      and just ignoring them if there's no catchpoint to catch them.  */
1264   return 0;
1265 #endif
1266 }
1267
1268 int
1269 child_remove_exec_catchpoint (int pid)
1270 {
1271   /* This request is only available on HPUX 10.0 and later.  */
1272 #if !defined(PT_SET_EVENT_MASK)
1273   error ("Unable to catch execs prior to HPUX 10.0");
1274
1275 #else
1276   /* Disable reporting of exec events from the kernel. */
1277   /* ??rehrauer: For the moment, we're always enabling these events,
1278      and just ignoring them if there's no catchpoint to catch them.  */
1279   return 0;
1280 #endif
1281 }
1282
1283 int
1284 hpux_has_execd (int pid, char **execd_pathname)
1285 {
1286   /* This request is only available on HPUX 10.0 and later.  */
1287 #if !defined(PT_GET_PROCESS_STATE)
1288   *execd_pathname = NULL;
1289   return 0;
1290
1291 #else
1292   int pt_status;
1293   ptrace_state_t ptrace_state;
1294
1295   errno = 0;
1296   pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1297                            pid,
1298                            (PTRACE_ARG3_TYPE) & ptrace_state,
1299                            sizeof (ptrace_state));
1300   if (errno)
1301     perror_with_name ("ptrace");
1302   if (pt_status < 0)
1303     return 0;
1304
1305   if (ptrace_state.pe_report_event & PTRACE_EXEC)
1306     {
1307       char *exec_file = target_pid_to_exec_file (pid);
1308       *execd_pathname = savestring (exec_file, strlen (exec_file));
1309       return 1;
1310     }
1311
1312   return 0;
1313 #endif
1314 }
1315
1316 int
1317 child_reported_exec_events_per_exec_call (void)
1318 {
1319   return 2;                     /* ptrace reports the event twice per call. */
1320 }
1321
1322 int
1323 hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
1324 {
1325   /* This request is only available on HPUX 10.30 and later, via
1326      the ttrace interface.  */
1327
1328   *kind = TARGET_WAITKIND_SPURIOUS;
1329   *syscall_id = -1;
1330   return 0;
1331 }
1332
1333 char *
1334 child_pid_to_exec_file (int pid)
1335 {
1336   static char exec_file_buffer[1024];
1337   int pt_status;
1338   CORE_ADDR top_of_stack;
1339   char four_chars[4];
1340   int name_index;
1341   int i;
1342   ptid_t saved_inferior_ptid;
1343   int done;
1344
1345 #ifdef PT_GET_PROCESS_PATHNAME
1346   /* As of 10.x HP-UX, there's an explicit request to get the pathname. */
1347   pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME,
1348                            pid,
1349                            (PTRACE_ARG3_TYPE) exec_file_buffer,
1350                            sizeof (exec_file_buffer) - 1);
1351   if (pt_status == 0)
1352     return exec_file_buffer;
1353 #endif
1354
1355   /* It appears that this request is broken prior to 10.30.
1356      If it fails, try a really, truly amazingly gross hack
1357      that DDE uses, of pawing through the process' data
1358      segment to find the pathname.  */
1359
1360   top_of_stack = 0x7b03a000;
1361   name_index = 0;
1362   done = 0;
1363
1364   /* On the chance that pid != inferior_ptid, set inferior_ptid
1365      to pid, so that (grrrr!) implicit uses of inferior_ptid get
1366      the right id.  */
1367
1368   saved_inferior_ptid = inferior_ptid;
1369   inferior_ptid = pid_to_ptid (pid);
1370
1371   /* Try to grab a null-terminated string. */
1372   while (!done)
1373     {
1374       if (target_read_memory (top_of_stack, four_chars, 4) != 0)
1375         {
1376           inferior_ptid = saved_inferior_ptid;
1377           return NULL;
1378         }
1379       for (i = 0; i < 4; i++)
1380         {
1381           exec_file_buffer[name_index++] = four_chars[i];
1382           done = (four_chars[i] == '\0');
1383           if (done)
1384             break;
1385         }
1386       top_of_stack += 4;
1387     }
1388
1389   if (exec_file_buffer[0] == '\0')
1390     {
1391       inferior_ptid = saved_inferior_ptid;
1392       return NULL;
1393     }
1394
1395   inferior_ptid = saved_inferior_ptid;
1396   return exec_file_buffer;
1397 }
1398
1399 void
1400 pre_fork_inferior (void)
1401 {
1402   int status;
1403
1404   status = pipe (startup_semaphore.parent_channel);
1405   if (status < 0)
1406     {
1407       warning ("error getting parent pipe for startup semaphore");
1408       return;
1409     }
1410
1411   status = pipe (startup_semaphore.child_channel);
1412   if (status < 0)
1413     {
1414       warning ("error getting child pipe for startup semaphore");
1415       return;
1416     }
1417 }
1418 \f
1419
1420 /* Check to see if the given thread is alive.
1421
1422    This is a no-op, as ptrace doesn't support threads, so we just
1423    return "TRUE".  */
1424
1425 int
1426 child_thread_alive (ptid_t ptid)
1427 {
1428   return 1;
1429 }
1430
1431 #endif /* ! GDB_NATIVE_HPUX_11 */