1 /* Native support code for HPUX PA-RISC.
2 Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 Free Software Foundation, Inc.
6 Contributed by the Center for Software Science at the
7 University of Utah (pa-gdb-bugs@cs.utah.edu).
9 This file is part of GDB.
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.
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.
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. */
30 #include <sys/ptrace.h>
34 #include "gdb_string.h"
35 #include "infttrace.h"
38 #include "hppa-tdep.h"
40 static CORE_ADDR text_end;
43 deprecated_hpux_text_end (struct target_ops *exec_ops)
45 struct section_table *p;
47 /* Set text_end to the highest address of the end of any readonly
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))
56 if (text_end < p->endaddr)
57 text_end = p->endaddr;
62 static void fetch_register (int);
65 fetch_inferior_registers (int regno)
68 for (regno = 0; regno < NUM_REGS; regno++)
69 fetch_register (regno);
71 fetch_register (regno);
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))
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). */
82 store_inferior_registers (int regno)
87 unsigned int offset = U_REGS_OFFSET;
92 unsigned int addr, len, offset;
94 if (CANNOT_STORE_REGISTER (regno))
98 len = register_size (current_gdbarch, regno);
100 /* Requests for register zero actually want the save_state's
101 ss_flags member. As RM says: "Oh, what a hack!" */
105 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
106 len = sizeof (ss.ss_flags);
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;
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)));
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. */
126 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
127 + DEPRECATED_REGISTER_BYTE (regno));
129 /* Narrow registers come from the ss_narrow area. Note that
130 ss_narrow starts with gr1, not gr0. */
132 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
133 + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (1)));
135 internal_error (__FILE__, __LINE__,
136 "hppah-nat.c (write_register): unexpected register size");
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.
144 if (regno == HPPA_PCOQ_HEAD_REGNUM || regno == HPPA_PCOQ_TAIL_REGNUM && len == 8)
148 temp = *(CORE_ADDR *)&deprecated_registers[DEPRECATED_REGISTER_BYTE (regno)];
150 /* Set the priv level (stored in the low two bits of the PC. */
153 ttrace_write_reg_64 (PIDGET (inferior_ptid), (CORE_ADDR)addr,
156 /* If we fail to write the PC, give a true error instead of
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);
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;
176 for (i = 0; i < len; i += sizeof (int))
179 call_ptrace (PT_WUREGS, PIDGET (inferior_ptid),
180 (PTRACE_ARG3_TYPE) addr + i,
181 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (regno) + i]);
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
192 if (regno == HPPA_PCOQ_HEAD_REGNUM || regno == HPPA_PCOQ_TAIL_REGNUM)
193 perror_with_name (msg);
201 for (regno = 0; regno < NUM_REGS; regno++)
202 store_inferior_registers (regno);
206 /* Fetch a register's value from the process's U area. */
208 fetch_register (int regno)
210 char buf[MAX_REGISTER_SIZE];
211 unsigned int addr, len, offset;
215 len = register_size (current_gdbarch, regno);
217 /* Requests for register zero actually want the save_state's
218 ss_flags member. As RM says: "Oh, what a hack!" */
222 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
223 len = sizeof (ss.ss_flags);
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));
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)));
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. */
244 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
245 + DEPRECATED_REGISTER_BYTE (regno));
247 /* Narrow registers come from the ss_narrow area. Note that
248 ss_narrow starts with gr1, not gr0. */
250 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
251 + (DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (1)));
254 internal_error (__FILE__, __LINE__,
255 "hppa-nat.c (fetch_register): unexpected register size");
257 for (i = 0; i < len; i += sizeof (int))
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);
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);
278 /* If we're reading an address from the instruction address queue,
279 mask out the bottom two bits --- they contain the privilege
281 if (regno == HPPA_PCOQ_HEAD_REGNUM || regno == HPPA_PCOQ_TAIL_REGNUM)
282 buf[len - 1] &= ~0x3;
284 regcache_raw_supply (current_regcache, regno, buf);
288 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
289 to debugger memory starting at MYADDR. Copy to inferior if
292 Returns the length copied, which is either the LEN argument or zero.
293 This xfer function does not do partial moves, since child_ops
294 doesn't allow memory operations to cross below us in the target stack
295 anyway. TARGET is ignored. */
298 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
299 struct mem_attrib *mem,
300 struct target_ops *target)
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. */
307 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
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.
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
317 int *buffer = (int *) xmalloc (count * sizeof (int));
321 /* Fill start and end extra bytes of buffer with existing memory data. */
322 if (addr != memaddr || len < (int) sizeof (int))
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);
330 if (count > 1) /* FIXME, avoid if even boundary */
333 = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
334 PIDGET (inferior_ptid),
335 (PTRACE_ARG3_TYPE) (addr
336 + (count - 1) * sizeof (int)),
340 /* Copy data to be written over corresponding part of buffer */
341 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
343 /* Write the entire buffer. */
344 for (i = 0; i < count; i++, addr += sizeof (int))
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??! */
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,
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)
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,
372 /* No, we still fail. Okay, time to punt. */
373 if ((pt_status == -1) && errno)
383 /* Read all the longwords */
384 for (i = 0; i < count; i++, addr += sizeof (int))
387 buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
388 PIDGET (inferior_ptid),
389 (PTRACE_ARG3_TYPE) addr, 0);
398 /* Copy appropriate bytes out of the buffer. */
399 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
405 char *saved_child_execd_pathname = NULL;
413 } saved_vfork_state = STATE_NONE;
416 child_follow_fork (int follow_child)
419 struct target_waitstatus last_status;
421 int parent_pid, child_pid;
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;
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. */
437 detach_breakpoints (child_pid);
438 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
439 SOLIB_REMOVE_INFERIOR_HOOK (child_pid);
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);
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
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!)
470 Sequence of events, as reported to gdb from HPUX:
472 Parent Child Action for gdb to take
473 -------------------------------------------------------
474 1 VFORK Continue child
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. */
484 reattach_breakpoints (parent_pid);
488 /* Needed to keep the breakpoint lists in sync. */
490 detach_breakpoints (child_pid);
492 /* Before detaching from the parent, remove all breakpoints from it. */
493 remove_breakpoints ();
495 /* Also reset the solib inferior hook from the parent. */
496 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
497 SOLIB_REMOVE_INFERIOR_HOOK (PIDGET (inferior_ptid));
500 /* Detach from the parent. */
501 target_detach (NULL, 1);
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);
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.. */
513 follow_inferior_reset_breakpoints ();
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");
524 if (! follow_child || saved_vfork_state == STATE_GOT_PARENT)
525 saved_vfork_state = STATE_NONE;
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". */
535 child_pid_to_str (ptid_t ptid)
537 /* Static because address returned */
539 pid_t pid = PIDGET (ptid);
541 /* Extra NUL for paranoia's sake */
542 sprintf (buf, "process %d%c", pid, '\0');
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".
550 Note: This is a core-gdb tid, not the actual system tid.
551 See infttrace.c for details. */
553 hppa_tid_to_str (ptid_t ptid)
555 /* Static because address returned */
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);
561 /* Extra NULLs for paranoia's sake */
562 sprintf (buf, "system thread %d%c", tid, '\0');
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
573 int not_same_real_pid = 1;
576 /* Wait for child to do something. Return pid of child, or -1 in case
577 of error; store status through argument pointer OURSTATUS. */
580 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
584 char *execd_pathname = NULL;
588 enum target_waitkind kind;
591 if (saved_vfork_state == STATE_FAKE_EXEC)
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;
601 set_sigint_trap (); /* Causes SIGINT to be passed on to the
605 pid = ptrace_wait (inferior_ptid, &status);
611 clear_sigint_trap ();
615 if (save_errno == EINTR)
618 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
619 safe_strerror (save_errno));
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);
629 if (target_has_exited (pid, status, &exit_status))
631 /* ??rehrauer: For now, ignore this. */
635 if (!target_thread_alive (pid_to_ptid (pid)))
637 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
638 return pid_to_ptid (pid);
641 if (hpux_has_forked (pid, &related_pid))
643 /* Ignore the parent's fork event. */
644 if (pid == PIDGET (inferior_ptid))
646 ourstatus->kind = TARGET_WAITKIND_IGNORE;
647 return inferior_ptid;
650 /* If this is the child's fork event, report that the
651 process has forked. */
652 if (related_pid == PIDGET (inferior_ptid))
654 ourstatus->kind = TARGET_WAITKIND_FORKED;
655 ourstatus->value.related_pid = pid;
656 return inferior_ptid;
660 if (hpux_has_vforked (pid, &related_pid))
662 if (pid == PIDGET (inferior_ptid))
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;
669 fprintf_unfiltered (gdb_stdout,
670 "hppah: parent vfork: confused\n");
672 else if (related_pid == PIDGET (inferior_ptid))
674 if (saved_vfork_state == STATE_NONE)
675 saved_vfork_state = STATE_GOT_CHILD;
677 fprintf_unfiltered (gdb_stdout,
678 "hppah: child vfork: confused\n");
681 fprintf_unfiltered (gdb_stdout,
682 "hppah: unknown vfork: confused\n");
684 if (saved_vfork_state == STATE_GOT_CHILD)
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);
691 child_resume (pid_to_ptid (pid), 0, TARGET_SIGNAL_0);
692 ourstatus->kind = TARGET_WAITKIND_IGNORE;
693 return pid_to_ptid (related_pid);
695 else if (saved_vfork_state == STATE_FAKE_EXEC)
697 ourstatus->kind = TARGET_WAITKIND_VFORKED;
698 ourstatus->value.related_pid = related_pid;
699 return pid_to_ptid (pid);
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);
711 if (hpux_has_execd (pid, &execd_pathname))
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
718 if (saved_vfork_state != STATE_NONE)
720 if (saved_vfork_state == STATE_GOT_CHILD)
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
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;
730 else if (saved_vfork_state == STATE_GOT_PARENT)
732 saved_vfork_state = STATE_FAKE_EXEC;
733 ourstatus->kind = TARGET_WAITKIND_VFORKED;
734 ourstatus->value.related_pid = saved_vfork_pid;
737 fprintf_unfiltered (gdb_stdout,
738 "hppa: exec: unexpected state\n");
740 saved_child_execd_pathname = execd_pathname;
742 return inferior_ptid;
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.
750 if (inferior_ignoring_startup_exec_events)
752 inferior_ignoring_startup_exec_events--;
756 ourstatus->kind = TARGET_WAITKIND_EXECD;
757 ourstatus->value.execd_pathname = execd_pathname;
758 return pid_to_ptid (pid);
762 /* All we must do with these is communicate their occurrence
763 to wait_for_inferior...
765 if (hpux_has_syscall_event (pid, &kind, &syscall_id))
767 ourstatus->kind = kind;
768 ourstatus->value.syscall_id = syscall_id;
769 return pid_to_ptid (pid);
772 /*## } while (pid != PIDGET (inferior_ptid)); ## *//* Some other child died or stopped */
773 /* hack for thread testing */
775 while ((pid != PIDGET (inferior_ptid)) && not_same_real_pid);
778 store_waitstatus (ourstatus, status);
779 return pid_to_ptid (pid);
782 #if !defined (GDB_NATIVE_HPUX_11)
784 /* The following code is a substitute for the infttrace.c versions used
785 with ttrace() in HPUX 11. */
787 /* This value is an arbitrary integer. */
788 #define PT_VERSION 123456
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. */
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] */
802 #define SEM_LISTEN (0)
804 static startup_semaphore_t startup_semaphore;
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.
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.
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.
823 (The acknowledgement by parent happens as a result of a call to
824 child_acknowledge_created_inferior.) */
827 parent_attach_all (int pid, PTRACE_ARG3_TYPE addr, int data)
831 /* We need a memory home for a constant. */
832 int tc_magic_child = PT_VERSION;
833 int tc_magic_parent = 0;
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)
840 /* Notify the parent that we're potentially ready to exec(). */
841 write (startup_semaphore.child_channel[SEM_TALK],
843 sizeof (tc_magic_child));
845 /* Wait for acknowledgement from the parent. */
846 read (startup_semaphore.parent_channel[SEM_LISTEN],
848 sizeof (tc_magic_parent));
849 if (tc_magic_child != tc_magic_parent)
850 warning ("mismatched semaphore magic");
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]);
864 hppa_require_attach (int pid)
869 unsigned int regs_offset;
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. */
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);
884 pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
889 /* Now we really are attached. */
897 hppa_require_detach (int pid, int signal)
900 call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal);
901 errno = 0; /* Ignore any errors. */
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. */
910 hppa_enable_page_protection_events (int pid)
915 hppa_disable_page_protection_events (int pid)
920 hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
922 error ("Hardware watchpoints not implemented on this platform.");
926 hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
928 error ("Hardware watchpoints not implemented on this platform.");
932 hppa_can_use_hw_watchpoint (int type, int cnt, int ot)
938 hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
940 error ("Hardware watchpoints not implemented on this platform.");
944 hppa_pid_or_tid_to_str (ptid_t id)
946 /* In the ptrace world, there are only processes. */
947 return child_pid_to_str (id);
951 hppa_ensure_vforking_parent_remains_stopped (int pid)
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
963 hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
965 return 1; /* Yes, the child must be resumed. */
969 require_notification_of_events (int pid)
971 #if defined(PT_SET_EVENT_MASK)
973 ptrace_event_t ptrace_events;
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));
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);
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--)
1001 if ((signal_stop_state (signum)) ||
1002 (signal_print_state (signum)) ||
1003 (!signal_pass_state (signum)))
1005 if (target_signal_to_host_p (signum))
1006 sigdelset (&ptrace_events.pe_signals,
1007 target_signal_to_host (signum));
1011 ptrace_events.pe_set_event = 0;
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;
1022 pt_status = call_ptrace (PT_SET_EVENT_MASK,
1024 (PTRACE_ARG3_TYPE) & ptrace_events,
1025 sizeof (ptrace_events));
1027 perror_with_name ("ptrace");
1034 require_notification_of_exec_events (int pid)
1036 #if defined(PT_SET_EVENT_MASK)
1038 ptrace_event_t ptrace_events;
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));
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);
1052 ptrace_events.pe_set_event = 0;
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;
1060 pt_status = call_ptrace (PT_SET_EVENT_MASK,
1062 (PTRACE_ARG3_TYPE) & ptrace_events,
1063 sizeof (ptrace_events));
1065 perror_with_name ("ptrace");
1071 /* This function is called by the parent process, with pid being the
1072 ID of the child process, after the debugger has forked. */
1075 child_acknowledge_created_inferior (int pid)
1077 /* We need a memory home for a constant. */
1078 int tc_magic_parent = PT_VERSION;
1079 int tc_magic_child = 0;
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],
1088 sizeof (tc_magic_child));
1090 /* Notify the child that it can exec.
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],
1097 sizeof (tc_magic_parent));
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.
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... */
1109 /* Instruct the kernel as to the set of events we wish to be
1111 require_notification_of_exec_events (pid);
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]);
1122 child_post_startup_inferior (ptid_t ptid)
1124 require_notification_of_events (PIDGET (ptid));
1128 child_post_attach (int pid)
1130 require_notification_of_events (pid);
1134 child_insert_fork_catchpoint (int pid)
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");
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. */
1148 child_remove_fork_catchpoint (int pid)
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");
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. */
1162 child_insert_vfork_catchpoint (int pid)
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");
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. */
1176 child_remove_vfork_catchpoint (int pid)
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");
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. */
1190 hpux_has_forked (int pid, int *childpid)
1192 /* This request is only available on HPUX 10.0 and later. */
1193 #if !defined(PT_GET_PROCESS_STATE)
1198 ptrace_state_t ptrace_state;
1201 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1203 (PTRACE_ARG3_TYPE) & ptrace_state,
1204 sizeof (ptrace_state));
1206 perror_with_name ("ptrace");
1210 if (ptrace_state.pe_report_event & PTRACE_FORK)
1212 *childpid = ptrace_state.pe_other_pid;
1221 hpux_has_vforked (int pid, int *childpid)
1223 /* This request is only available on HPUX 10.0 and later. */
1224 #if !defined(PT_GET_PROCESS_STATE)
1230 ptrace_state_t ptrace_state;
1233 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1235 (PTRACE_ARG3_TYPE) & ptrace_state,
1236 sizeof (ptrace_state));
1238 perror_with_name ("ptrace");
1242 if (ptrace_state.pe_report_event & PTRACE_VFORK)
1244 *childpid = ptrace_state.pe_other_pid;
1253 child_insert_exec_catchpoint (int pid)
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");
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. */
1268 child_remove_exec_catchpoint (int pid)
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");
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. */
1283 hpux_has_execd (int pid, char **execd_pathname)
1285 /* This request is only available on HPUX 10.0 and later. */
1286 #if !defined(PT_GET_PROCESS_STATE)
1287 *execd_pathname = NULL;
1292 ptrace_state_t ptrace_state;
1295 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1297 (PTRACE_ARG3_TYPE) & ptrace_state,
1298 sizeof (ptrace_state));
1300 perror_with_name ("ptrace");
1304 if (ptrace_state.pe_report_event & PTRACE_EXEC)
1306 char *exec_file = target_pid_to_exec_file (pid);
1307 *execd_pathname = savestring (exec_file, strlen (exec_file));
1316 child_reported_exec_events_per_exec_call (void)
1318 return 2; /* ptrace reports the event twice per call. */
1322 hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
1324 /* This request is only available on HPUX 10.30 and later, via
1325 the ttrace interface. */
1327 *kind = TARGET_WAITKIND_SPURIOUS;
1333 child_pid_to_exec_file (int pid)
1335 static char exec_file_buffer[1024];
1337 CORE_ADDR top_of_stack;
1341 ptid_t saved_inferior_ptid;
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,
1348 (PTRACE_ARG3_TYPE) exec_file_buffer,
1349 sizeof (exec_file_buffer) - 1);
1351 return exec_file_buffer;
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. */
1359 top_of_stack = 0x7b03a000;
1363 /* On the chance that pid != inferior_ptid, set inferior_ptid
1364 to pid, so that (grrrr!) implicit uses of inferior_ptid get
1367 saved_inferior_ptid = inferior_ptid;
1368 inferior_ptid = pid_to_ptid (pid);
1370 /* Try to grab a null-terminated string. */
1373 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
1375 inferior_ptid = saved_inferior_ptid;
1378 for (i = 0; i < 4; i++)
1380 exec_file_buffer[name_index++] = four_chars[i];
1381 done = (four_chars[i] == '\0');
1388 if (exec_file_buffer[0] == '\0')
1390 inferior_ptid = saved_inferior_ptid;
1394 inferior_ptid = saved_inferior_ptid;
1395 return exec_file_buffer;
1399 pre_fork_inferior (void)
1403 status = pipe (startup_semaphore.parent_channel);
1406 warning ("error getting parent pipe for startup semaphore");
1410 status = pipe (startup_semaphore.child_channel);
1413 warning ("error getting child pipe for startup semaphore");
1419 /* Check to see if the given thread is alive.
1421 This is a no-op, as ptrace doesn't support threads, so we just
1425 child_thread_alive (ptid_t ptid)
1430 #endif /* ! GDB_NATIVE_HPUX_11 */