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