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
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>
36 extern CORE_ADDR text_end;
38 extern int hpux_has_forked (int pid, int *childpid);
39 extern int hpux_has_vforked (int pid, int *childpid);
40 extern int hpux_has_execd (int pid, char **execd_pathname);
41 extern int hpux_has_syscall_event (int pid, enum target_waitkind *kind,
44 static void fetch_register (int);
47 fetch_inferior_registers (int regno)
50 for (regno = 0; regno < NUM_REGS; regno++)
51 fetch_register (regno);
53 fetch_register (regno);
56 /* Our own version of the offsetof macro, since we can't assume ANSI C. */
57 #define HPPAH_OFFSETOF(type, member) ((int) (&((type *) 0)->member))
59 /* Store our register values back into the inferior.
60 If REGNO is -1, do this for all registers.
61 Otherwise, REGNO specifies which register (so we can save time). */
64 store_inferior_registers (int regno)
66 register unsigned int regaddr;
69 unsigned int offset = U_REGS_OFFSET;
74 unsigned int addr, len, offset;
76 if (CANNOT_STORE_REGISTER (regno))
80 len = REGISTER_RAW_SIZE (regno);
82 /* Requests for register zero actually want the save_state's
83 ss_flags member. As RM says: "Oh, what a hack!" */
87 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
88 len = sizeof (ss.ss_flags);
90 /* Note that ss_flags is always an int, no matter what
91 REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA machines
92 are big-endian, put it at the least significant end of the
93 value, and zap the rest of the buffer. */
94 offset = REGISTER_RAW_SIZE (0) - len;
97 /* Floating-point registers come from the ss_fpblock area. */
98 else if (regno >= FP0_REGNUM)
99 addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock)
100 + (REGISTER_BYTE (regno) - REGISTER_BYTE (FP0_REGNUM)));
102 /* Wide registers come from the ss_wide area.
103 I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
104 between ss_wide and ss_narrow than to use the raw register size.
105 But checking ss_flags would require an extra ptrace call for
106 every register reference. Bleah. */
108 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
109 + REGISTER_BYTE (regno));
111 /* Narrow registers come from the ss_narrow area. Note that
112 ss_narrow starts with gr1, not gr0. */
114 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
115 + (REGISTER_BYTE (regno) - REGISTER_BYTE (1)));
117 internal_error (__FILE__, __LINE__,
118 "hppah-nat.c (write_register): unexpected register size");
120 #ifdef GDB_TARGET_IS_HPPA_20W
121 /* Unbelieveable. The PC head and tail must be written in 64bit hunks
122 or we will get an error. Worse yet, the oddball ptrace/ttrace
123 layering will not allow us to perform a 64bit register store.
126 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM && len == 8)
130 temp = *(CORE_ADDR *)&deprecated_registers[REGISTER_BYTE (regno)];
132 /* Set the priv level (stored in the low two bits of the PC. */
135 ttrace_write_reg_64 (PIDGET (inferior_ptid), (CORE_ADDR)addr,
138 /* If we fail to write the PC, give a true error instead of
142 char *err = safe_strerror (errno);
143 char *msg = alloca (strlen (err) + 128);
144 sprintf (msg, "writing `%s' register: %s",
145 REGISTER_NAME (regno), err);
146 perror_with_name (msg);
151 /* Another crock. HPUX complains if you write a nonzero value to
152 the high part of IPSW. What will it take for HP to catch a
153 clue about building sensible interfaces? */
154 if (regno == IPSW_REGNUM && len == 8)
155 *(int *)&deprecated_registers[REGISTER_BYTE (regno)] = 0;
158 for (i = 0; i < len; i += sizeof (int))
161 call_ptrace (PT_WUREGS, PIDGET (inferior_ptid),
162 (PTRACE_ARG3_TYPE) addr + i,
163 *(int *) &deprecated_registers[REGISTER_BYTE (regno) + i]);
166 /* Warning, not error, in case we are attached; sometimes
167 the kernel doesn't let us at the registers. */
168 char *err = safe_strerror (errno);
169 char *msg = alloca (strlen (err) + 128);
170 sprintf (msg, "writing `%s' register: %s",
171 REGISTER_NAME (regno), err);
172 /* If we fail to write the PC, give a true error instead of
174 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
175 perror_with_name (msg);
183 for (regno = 0; regno < NUM_REGS; regno++)
184 store_inferior_registers (regno);
188 /* Fetch a register's value from the process's U area. */
190 fetch_register (int regno)
192 char buf[MAX_REGISTER_RAW_SIZE];
193 unsigned int addr, len, offset;
197 len = REGISTER_RAW_SIZE (regno);
199 /* Requests for register zero actually want the save_state's
200 ss_flags member. As RM says: "Oh, what a hack!" */
204 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
205 len = sizeof (ss.ss_flags);
207 /* Note that ss_flags is always an int, no matter what
208 REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA machines
209 are big-endian, put it at the least significant end of the
210 value, and zap the rest of the buffer. */
211 offset = REGISTER_RAW_SIZE (0) - len;
212 memset (buf, 0, sizeof (buf));
215 /* Floating-point registers come from the ss_fpblock area. */
216 else if (regno >= FP0_REGNUM)
217 addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock)
218 + (REGISTER_BYTE (regno) - REGISTER_BYTE (FP0_REGNUM)));
220 /* Wide registers come from the ss_wide area.
221 I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
222 between ss_wide and ss_narrow than to use the raw register size.
223 But checking ss_flags would require an extra ptrace call for
224 every register reference. Bleah. */
226 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
227 + REGISTER_BYTE (regno));
229 /* Narrow registers come from the ss_narrow area. Note that
230 ss_narrow starts with gr1, not gr0. */
232 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
233 + (REGISTER_BYTE (regno) - REGISTER_BYTE (1)));
236 internal_error (__FILE__, __LINE__,
237 "hppa-nat.c (fetch_register): unexpected register size");
239 for (i = 0; i < len; i += sizeof (int))
242 /* Copy an int from the U area to buf. Fill the least
243 significant end if len != raw_size. */
244 * (int *) &buf[offset + i] =
245 call_ptrace (PT_RUREGS, PIDGET (inferior_ptid),
246 (PTRACE_ARG3_TYPE) addr + i, 0);
249 /* Warning, not error, in case we are attached; sometimes
250 the kernel doesn't let us at the registers. */
251 char *err = safe_strerror (errno);
252 char *msg = alloca (strlen (err) + 128);
253 sprintf (msg, "reading `%s' register: %s",
254 REGISTER_NAME (regno), err);
260 /* If we're reading an address from the instruction address queue,
261 mask out the bottom two bits --- they contain the privilege
263 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
264 buf[len - 1] &= ~0x3;
266 supply_register (regno, buf);
270 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
271 to debugger memory starting at MYADDR. Copy to inferior if
274 Returns the length copied, which is either the LEN argument or zero.
275 This xfer function does not do partial moves, since child_ops
276 doesn't allow memory operations to cross below us in the target stack
277 anyway. TARGET is ignored. */
280 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
281 struct mem_attrib *mem,
282 struct target_ops *target)
285 /* Round starting address down to longword boundary. */
286 register CORE_ADDR addr = memaddr & - (CORE_ADDR)(sizeof (int));
287 /* Round ending address up; get number of longwords that makes. */
289 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
291 /* Allocate buffer of that many longwords.
292 Note -- do not use alloca to allocate this buffer since there is no
293 guarantee of when the buffer will actually be deallocated.
295 This routine can be called over and over with the same call chain;
296 this (in effect) would pile up all those alloca requests until a call
297 to alloca was made from a point higher than this routine in the
299 register int *buffer = (int *) xmalloc (count * sizeof (int));
303 /* Fill start and end extra bytes of buffer with existing memory data. */
304 if (addr != memaddr || len < (int) sizeof (int))
306 /* Need part of initial word -- fetch it. */
307 buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
308 PIDGET (inferior_ptid),
309 (PTRACE_ARG3_TYPE) addr, 0);
312 if (count > 1) /* FIXME, avoid if even boundary */
315 = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
316 PIDGET (inferior_ptid),
317 (PTRACE_ARG3_TYPE) (addr
318 + (count - 1) * sizeof (int)),
322 /* Copy data to be written over corresponding part of buffer */
323 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
325 /* Write the entire buffer. */
326 for (i = 0; i < count; i++, addr += sizeof (int))
330 /* The HP-UX kernel crashes if you use PT_WDUSER to write into the
331 text segment. FIXME -- does it work to write into the data
332 segment using WIUSER, or do these idiots really expect us to
333 figure out which segment the address is in, so we can use a
334 separate system call for it??! */
336 pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER;
337 pt_status = call_ptrace (pt_request,
338 PIDGET (inferior_ptid),
339 (PTRACE_ARG3_TYPE) addr,
342 /* Did we fail? Might we've guessed wrong about which
343 segment this address resides in? Try the other request,
344 and see if that works... */
345 if ((pt_status == -1) && errno)
348 pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER;
349 pt_status = call_ptrace (pt_request,
350 PIDGET (inferior_ptid),
351 (PTRACE_ARG3_TYPE) addr,
354 /* No, we still fail. Okay, time to punt. */
355 if ((pt_status == -1) && errno)
365 /* Read all the longwords */
366 for (i = 0; i < count; i++, addr += sizeof (int))
369 buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
370 PIDGET (inferior_ptid),
371 (PTRACE_ARG3_TYPE) addr, 0);
380 /* Copy appropriate bytes out of the buffer. */
381 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
387 char *saved_child_execd_pathname = NULL;
395 } saved_vfork_state = STATE_NONE;
398 child_follow_fork (int follow_child)
401 struct target_waitstatus last_status;
403 int parent_pid, child_pid;
405 get_last_target_status (&last_ptid, &last_status);
406 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
407 parent_pid = ptid_get_pid (last_ptid);
408 child_pid = last_status.value.related_pid;
410 /* At this point, if we are vforking, breakpoints were already
411 detached from the child in child_wait; and the child has already
412 called execve(). If we are forking, both the parent and child
413 have breakpoints inserted. */
419 detach_breakpoints (child_pid);
420 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
421 SOLIB_REMOVE_INFERIOR_HOOK (child_pid);
425 /* Detach from the child. */
426 printf_unfiltered ("Detaching after fork from %s\n",
427 target_pid_to_str (pid_to_ptid (child_pid)));
428 hppa_require_detach (child_pid, 0);
430 /* The parent and child of a vfork share the same address space.
431 Also, on some targets the order in which vfork and exec events
432 are received for parent in child requires some delicate handling
435 For instance, on ptrace-based HPUX we receive the child's vfork
436 event first, at which time the parent has been suspended by the
437 OS and is essentially untouchable until the child's exit or second
438 exec event arrives. At that time, the parent's vfork event is
439 delivered to us, and that's when we see and decide how to follow
440 the vfork. But to get to that point, we must continue the child
441 until it execs or exits. To do that smoothly, all breakpoints
442 must be removed from the child, in case there are any set between
443 the vfork() and exec() calls. But removing them from the child
444 also removes them from the parent, due to the shared-address-space
445 nature of a vfork'd parent and child. On HPUX, therefore, we must
446 take care to restore the bp's to the parent before we continue it.
447 Else, it's likely that we may not stop in the expected place. (The
448 worst scenario is when the user tries to step over a vfork() call;
449 the step-resume bp must be restored for the step to properly stop
450 in the parent after the call completes!)
452 Sequence of events, as reported to gdb from HPUX:
454 Parent Child Action for gdb to take
455 -------------------------------------------------------
456 1 VFORK Continue child
461 Now that the child has safely exec'd or exited, we must restore
462 the parent's breakpoints before we continue it. Else, we may
463 cause it run past expected stopping points. */
466 reattach_breakpoints (parent_pid);
470 /* Needed to keep the breakpoint lists in sync. */
472 detach_breakpoints (child_pid);
474 /* Before detaching from the parent, remove all breakpoints from it. */
475 remove_breakpoints ();
477 /* Also reset the solib inferior hook from the parent. */
478 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
479 SOLIB_REMOVE_INFERIOR_HOOK (PIDGET (inferior_ptid));
482 /* Detach from the parent. */
483 target_detach (NULL, 1);
485 /* Attach to the child. */
486 printf_unfiltered ("Attaching after fork to %s\n",
487 target_pid_to_str (pid_to_ptid (child_pid)));
488 hppa_require_attach (child_pid);
489 inferior_ptid = pid_to_ptid (child_pid);
491 /* If we vforked, then we've also execed by now. The exec will be
492 reported momentarily. follow_exec () will handle breakpoints, so
493 we don't have to.. */
495 follow_inferior_reset_breakpoints ();
500 /* If we followed the parent, don't try to follow the child's exec. */
501 if (saved_vfork_state != STATE_GOT_PARENT
502 && saved_vfork_state != STATE_FAKE_EXEC)
503 fprintf_unfiltered (gdb_stdout,
504 "hppa: post follow vfork: confused state\n");
506 if (! follow_child || saved_vfork_state == STATE_GOT_PARENT)
507 saved_vfork_state = STATE_NONE;
514 /* Format a process id, given PID. Be sure to terminate
515 this with a null--it's going to be printed via a "%s". */
517 child_pid_to_str (ptid_t ptid)
519 /* Static because address returned */
521 pid_t pid = PIDGET (ptid);
523 /* Extra NUL for paranoia's sake */
524 sprintf (buf, "process %d%c", pid, '\0');
529 /* Format a thread id, given TID. Be sure to terminate
530 this with a null--it's going to be printed via a "%s".
532 Note: This is a core-gdb tid, not the actual system tid.
533 See infttrace.c for details. */
535 hppa_tid_to_str (ptid_t ptid)
537 /* Static because address returned */
539 /* This seems strange, but when I did the ptid conversion, it looked
540 as though a pid was always being passed. - Kevin Buettner */
541 pid_t tid = PIDGET (ptid);
543 /* Extra NULLs for paranoia's sake */
544 sprintf (buf, "system thread %d%c", tid, '\0');
550 /* Enable HACK for ttrace work. In
551 * infttrace.c/require_notification_of_events,
552 * this is set to 0 so that the loop in child_wait
555 int not_same_real_pid = 1;
558 /* Wait for child to do something. Return pid of child, or -1 in case
559 of error; store status through argument pointer OURSTATUS. */
562 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
566 char *execd_pathname = NULL;
570 enum target_waitkind kind;
573 if (saved_vfork_state == STATE_FAKE_EXEC)
575 saved_vfork_state = STATE_NONE;
576 ourstatus->kind = TARGET_WAITKIND_EXECD;
577 ourstatus->value.execd_pathname = saved_child_execd_pathname;
578 return inferior_ptid;
583 set_sigint_trap (); /* Causes SIGINT to be passed on to the
587 pid = ptrace_wait (inferior_ptid, &status);
593 clear_sigint_trap ();
597 if (save_errno == EINTR)
600 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
601 safe_strerror (save_errno));
603 /* Claim it exited with unknown signal. */
604 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
605 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
606 return pid_to_ptid (-1);
611 if (target_has_exited (pid, status, &exit_status))
613 /* ??rehrauer: For now, ignore this. */
617 if (!target_thread_alive (pid_to_ptid (pid)))
619 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
620 return pid_to_ptid (pid);
623 if (hpux_has_forked (pid, &related_pid))
625 /* Ignore the parent's fork event. */
626 if (pid == PIDGET (inferior_ptid))
628 ourstatus->kind = TARGET_WAITKIND_IGNORE;
629 return inferior_ptid;
632 /* If this is the child's fork event, report that the
633 process has forked. */
634 if (related_pid == PIDGET (inferior_ptid))
636 ourstatus->kind = TARGET_WAITKIND_FORKED;
637 ourstatus->value.related_pid = pid;
638 return inferior_ptid;
642 if (hpux_has_vforked (pid, &related_pid))
644 if (pid == PIDGET (inferior_ptid))
646 if (saved_vfork_state == STATE_GOT_CHILD)
647 saved_vfork_state = STATE_GOT_PARENT;
648 else if (saved_vfork_state == STATE_GOT_EXEC)
649 saved_vfork_state = STATE_FAKE_EXEC;
651 fprintf_unfiltered (gdb_stdout,
652 "hppah: parent vfork: confused\n");
654 else if (related_pid == PIDGET (inferior_ptid))
656 if (saved_vfork_state == STATE_NONE)
657 saved_vfork_state = STATE_GOT_CHILD;
659 fprintf_unfiltered (gdb_stdout,
660 "hppah: child vfork: confused\n");
663 fprintf_unfiltered (gdb_stdout,
664 "hppah: unknown vfork: confused\n");
666 if (saved_vfork_state == STATE_GOT_CHILD)
668 child_post_startup_inferior (pid_to_ptid (pid));
669 detach_breakpoints (pid);
670 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
671 SOLIB_REMOVE_INFERIOR_HOOK (pid);
673 child_resume (pid_to_ptid (pid), 0, TARGET_SIGNAL_0);
674 ourstatus->kind = TARGET_WAITKIND_IGNORE;
675 return pid_to_ptid (related_pid);
677 else if (saved_vfork_state == STATE_FAKE_EXEC)
679 ourstatus->kind = TARGET_WAITKIND_VFORKED;
680 ourstatus->value.related_pid = related_pid;
681 return pid_to_ptid (pid);
685 /* We saw the parent's vfork, but we haven't seen the exec yet.
686 Wait for it, for simplicity's sake. It should be pending. */
687 saved_vfork_pid = related_pid;
688 ourstatus->kind = TARGET_WAITKIND_IGNORE;
689 return pid_to_ptid (pid);
693 if (hpux_has_execd (pid, &execd_pathname))
695 /* On HP-UX, events associated with a vforking inferior come in
696 threes: a vfork event for the child (always first), followed
697 a vfork event for the parent and an exec event for the child.
698 The latter two can come in either order. Make sure we get
700 if (saved_vfork_state != STATE_NONE)
702 if (saved_vfork_state == STATE_GOT_CHILD)
704 saved_vfork_state = STATE_GOT_EXEC;
705 /* On HP/UX with ptrace, the child must be resumed before
706 the parent vfork event is delivered. A single-step
708 if (RESUME_EXECD_VFORKING_CHILD_TO_GET_PARENT_VFORK ())
709 target_resume (pid_to_ptid (pid), 1, TARGET_SIGNAL_0);
710 ourstatus->kind = TARGET_WAITKIND_IGNORE;
712 else if (saved_vfork_state == STATE_GOT_PARENT)
714 saved_vfork_state = STATE_FAKE_EXEC;
715 ourstatus->kind = TARGET_WAITKIND_VFORKED;
716 ourstatus->value.related_pid = saved_vfork_pid;
719 fprintf_unfiltered (gdb_stdout,
720 "hppa: exec: unexpected state\n");
722 saved_child_execd_pathname = execd_pathname;
724 return inferior_ptid;
727 /* Are we ignoring initial exec events? (This is likely because
728 we're in the process of starting up the inferior, and another
729 (older) mechanism handles those.) If so, we'll report this
730 as a regular stop, not an exec.
732 if (inferior_ignoring_startup_exec_events)
734 inferior_ignoring_startup_exec_events--;
738 ourstatus->kind = TARGET_WAITKIND_EXECD;
739 ourstatus->value.execd_pathname = execd_pathname;
740 return pid_to_ptid (pid);
744 /* All we must do with these is communicate their occurrence
745 to wait_for_inferior...
747 if (hpux_has_syscall_event (pid, &kind, &syscall_id))
749 ourstatus->kind = kind;
750 ourstatus->value.syscall_id = syscall_id;
751 return pid_to_ptid (pid);
754 /*## } while (pid != PIDGET (inferior_ptid)); ## *//* Some other child died or stopped */
755 /* hack for thread testing */
757 while ((pid != PIDGET (inferior_ptid)) && not_same_real_pid);
760 store_waitstatus (ourstatus, status);
761 return pid_to_ptid (pid);
764 #if !defined (GDB_NATIVE_HPUX_11)
766 /* The following code is a substitute for the infttrace.c versions used
767 with ttrace() in HPUX 11. */
769 /* This value is an arbitrary integer. */
770 #define PT_VERSION 123456
772 /* This semaphore is used to coordinate the child and parent processes
773 after a fork(), and before an exec() by the child. See
774 parent_attach_all for details. */
778 int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */
779 int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */
784 #define SEM_LISTEN (0)
786 static startup_semaphore_t startup_semaphore;
788 extern int parent_attach_all (int, PTRACE_ARG3_TYPE, int);
791 /* This function causes the caller's process to be traced by its
792 parent. This is intended to be called after GDB forks itself,
793 and before the child execs the target.
795 Note that HP-UX ptrace is rather funky in how this is done.
796 If the parent wants to get the initial exec event of a child,
797 it must set the ptrace event mask of the child to include execs.
798 (The child cannot do this itself.) This must be done after the
799 child is forked, but before it execs.
801 To coordinate the parent and child, we implement a semaphore using
802 pipes. After SETTRC'ing itself, the child tells the parent that
803 it is now traceable by the parent, and waits for the parent's
804 acknowledgement. The parent can then set the child's event mask,
805 and notify the child that it can now exec.
807 (The acknowledgement by parent happens as a result of a call to
808 child_acknowledge_created_inferior.) */
811 parent_attach_all (int pid, PTRACE_ARG3_TYPE addr, int data)
815 /* We need a memory home for a constant. */
816 int tc_magic_child = PT_VERSION;
817 int tc_magic_parent = 0;
819 /* The remainder of this function is only useful for HPUX 10.0 and
820 later, as it depends upon the ability to request notification
821 of specific kinds of events by the kernel. */
822 #if defined(PT_SET_EVENT_MASK)
824 /* Notify the parent that we're potentially ready to exec(). */
825 write (startup_semaphore.child_channel[SEM_TALK],
827 sizeof (tc_magic_child));
829 /* Wait for acknowledgement from the parent. */
830 read (startup_semaphore.parent_channel[SEM_LISTEN],
832 sizeof (tc_magic_parent));
833 if (tc_magic_child != tc_magic_parent)
834 warning ("mismatched semaphore magic");
836 /* Discard our copy of the semaphore. */
837 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
838 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
839 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
840 (void) close (startup_semaphore.child_channel[SEM_TALK]);
848 hppa_require_attach (int pid)
853 unsigned int regs_offset;
855 /* Are we already attached? There appears to be no explicit way to
856 answer this via ptrace, so we try something which should be
857 innocuous if we are attached. If that fails, then we assume
858 we're not attached, and so attempt to make it so. */
861 regs_offset = U_REGS_OFFSET;
862 pc_addr = register_addr (PC_REGNUM, regs_offset);
863 pc = call_ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE) pc_addr, 0);
868 pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
873 /* Now we really are attached. */
881 hppa_require_detach (int pid, int signal)
884 call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal);
885 errno = 0; /* Ignore any errors. */
889 /* Since ptrace doesn't support memory page-protection events, which
890 are used to implement "hardware" watchpoints on HP-UX, these are
891 dummy versions, which perform no useful work. */
894 hppa_enable_page_protection_events (int pid)
899 hppa_disable_page_protection_events (int pid)
904 hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
906 error ("Hardware watchpoints not implemented on this platform.");
910 hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len,
913 error ("Hardware watchpoints not implemented on this platform.");
917 hppa_can_use_hw_watchpoint (enum bptype type, int cnt, enum bptype ot)
923 hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
925 error ("Hardware watchpoints not implemented on this platform.");
929 hppa_pid_or_tid_to_str (ptid_t id)
931 /* In the ptrace world, there are only processes. */
932 return child_pid_to_str (id);
935 /* This function has no meaning in a non-threaded world. Thus, we
936 return 0 (FALSE). See the use of "hppa_prepare_to_proceed" in
940 hppa_switched_threads (pid_t pid)
946 hppa_ensure_vforking_parent_remains_stopped (int pid)
948 /* This assumes that the vforked parent is presently stopped, and
949 that the vforked child has just delivered its first exec event.
950 Calling kill() this way will cause the SIGTRAP to be delivered as
951 soon as the parent is resumed, which happens as soon as the
952 vforked child is resumed. See wait_for_inferior for the use of
958 hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
960 return 1; /* Yes, the child must be resumed. */
964 require_notification_of_events (int pid)
966 #if defined(PT_SET_EVENT_MASK)
968 ptrace_event_t ptrace_events;
972 /* Instruct the kernel as to the set of events we wish to be
973 informed of. (This support does not exist before HPUX 10.0.
974 We'll assume if PT_SET_EVENT_MASK has not been defined by
975 <sys/ptrace.h>, then we're being built on pre-10.0.) */
976 memset (&ptrace_events, 0, sizeof (ptrace_events));
978 /* Note: By default, all signals are visible to us. If we wish
979 the kernel to keep certain signals hidden from us, we do it
980 by calling sigdelset (ptrace_events.pe_signals, signal) for
981 each such signal here, before doing PT_SET_EVENT_MASK. */
982 /* RM: The above comment is no longer true. We start with ignoring
983 all signals, and then add the ones we are interested in. We could
984 do it the other way: start by looking at all signals and then
985 deleting the ones that we aren't interested in, except that
986 multiple gdb signals may be mapped to the same host signal
987 (eg. TARGET_SIGNAL_IO and TARGET_SIGNAL_POLL both get mapped to
988 signal 22 on HPUX 10.20) We want to be notified if we are
989 interested in either signal. */
990 sigfillset (&ptrace_events.pe_signals);
992 /* RM: Let's not bother with signals we don't care about */
993 nsigs = (int) TARGET_SIGNAL_LAST;
994 for (signum = nsigs; signum > 0; signum--)
996 if ((signal_stop_state (signum)) ||
997 (signal_print_state (signum)) ||
998 (!signal_pass_state (signum)))
1000 if (target_signal_to_host_p (signum))
1001 sigdelset (&ptrace_events.pe_signals,
1002 target_signal_to_host (signum));
1006 ptrace_events.pe_set_event = 0;
1008 ptrace_events.pe_set_event |= PTRACE_SIGNAL;
1009 ptrace_events.pe_set_event |= PTRACE_EXEC;
1010 ptrace_events.pe_set_event |= PTRACE_FORK;
1011 ptrace_events.pe_set_event |= PTRACE_VFORK;
1012 /* ??rehrauer: Add this one when we're prepared to catch it...
1013 ptrace_events.pe_set_event |= PTRACE_EXIT;
1017 pt_status = call_ptrace (PT_SET_EVENT_MASK,
1019 (PTRACE_ARG3_TYPE) & ptrace_events,
1020 sizeof (ptrace_events));
1022 perror_with_name ("ptrace");
1029 require_notification_of_exec_events (int pid)
1031 #if defined(PT_SET_EVENT_MASK)
1033 ptrace_event_t ptrace_events;
1035 /* Instruct the kernel as to the set of events we wish to be
1036 informed of. (This support does not exist before HPUX 10.0.
1037 We'll assume if PT_SET_EVENT_MASK has not been defined by
1038 <sys/ptrace.h>, then we're being built on pre-10.0.) */
1039 memset (&ptrace_events, 0, sizeof (ptrace_events));
1041 /* Note: By default, all signals are visible to us. If we wish
1042 the kernel to keep certain signals hidden from us, we do it
1043 by calling sigdelset (ptrace_events.pe_signals, signal) for
1044 each such signal here, before doing PT_SET_EVENT_MASK. */
1045 sigemptyset (&ptrace_events.pe_signals);
1047 ptrace_events.pe_set_event = 0;
1049 ptrace_events.pe_set_event |= PTRACE_EXEC;
1050 /* ??rehrauer: Add this one when we're prepared to catch it...
1051 ptrace_events.pe_set_event |= PTRACE_EXIT;
1055 pt_status = call_ptrace (PT_SET_EVENT_MASK,
1057 (PTRACE_ARG3_TYPE) & ptrace_events,
1058 sizeof (ptrace_events));
1060 perror_with_name ("ptrace");
1066 /* This function is called by the parent process, with pid being the
1067 ID of the child process, after the debugger has forked. */
1070 child_acknowledge_created_inferior (int pid)
1072 /* We need a memory home for a constant. */
1073 int tc_magic_parent = PT_VERSION;
1074 int tc_magic_child = 0;
1076 /* The remainder of this function is only useful for HPUX 10.0 and
1077 later, as it depends upon the ability to request notification
1078 of specific kinds of events by the kernel. */
1079 #if defined(PT_SET_EVENT_MASK)
1080 /* Wait for the child to tell us that it has forked. */
1081 read (startup_semaphore.child_channel[SEM_LISTEN],
1083 sizeof (tc_magic_child));
1085 /* Notify the child that it can exec.
1087 In the infttrace.c variant of this function, we set the child's
1088 event mask after the fork but before the exec. In the ptrace
1089 world, it seems we can't set the event mask until after the exec. */
1090 write (startup_semaphore.parent_channel[SEM_TALK],
1092 sizeof (tc_magic_parent));
1094 /* We'd better pause a bit before trying to set the event mask,
1095 though, to ensure that the exec has happened. We don't want to
1096 wait() on the child, because that'll screw up the upper layers
1097 of gdb's execution control that expect to see the exec event.
1099 After an exec, the child is no longer executing gdb code. Hence,
1100 we can't have yet another synchronization via the pipes. We'll
1101 just sleep for a second, and hope that's enough delay... */
1104 /* Instruct the kernel as to the set of events we wish to be
1106 require_notification_of_exec_events (pid);
1108 /* Discard our copy of the semaphore. */
1109 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
1110 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
1111 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
1112 (void) close (startup_semaphore.child_channel[SEM_TALK]);
1117 child_post_startup_inferior (ptid_t ptid)
1119 require_notification_of_events (PIDGET (ptid));
1123 child_post_attach (int pid)
1125 require_notification_of_events (pid);
1129 child_insert_fork_catchpoint (int pid)
1131 /* This request is only available on HPUX 10.0 and later. */
1132 #if !defined(PT_SET_EVENT_MASK)
1133 error ("Unable to catch forks prior to HPUX 10.0");
1135 /* Enable reporting of fork events from the kernel. */
1136 /* ??rehrauer: For the moment, we're always enabling these events,
1137 and just ignoring them if there's no catchpoint to catch them. */
1143 child_remove_fork_catchpoint (int pid)
1145 /* This request is only available on HPUX 10.0 and later. */
1146 #if !defined(PT_SET_EVENT_MASK)
1147 error ("Unable to catch forks prior to HPUX 10.0");
1149 /* Disable reporting of fork events from the kernel. */
1150 /* ??rehrauer: For the moment, we're always enabling these events,
1151 and just ignoring them if there's no catchpoint to catch them. */
1157 child_insert_vfork_catchpoint (int pid)
1159 /* This request is only available on HPUX 10.0 and later. */
1160 #if !defined(PT_SET_EVENT_MASK)
1161 error ("Unable to catch vforks prior to HPUX 10.0");
1163 /* Enable reporting of vfork events from the kernel. */
1164 /* ??rehrauer: For the moment, we're always enabling these events,
1165 and just ignoring them if there's no catchpoint to catch them. */
1171 child_remove_vfork_catchpoint (int pid)
1173 /* This request is only available on HPUX 10.0 and later. */
1174 #if !defined(PT_SET_EVENT_MASK)
1175 error ("Unable to catch vforks prior to HPUX 10.0");
1177 /* Disable reporting of vfork events from the kernel. */
1178 /* ??rehrauer: For the moment, we're always enabling these events,
1179 and just ignoring them if there's no catchpoint to catch them. */
1185 hpux_has_forked (int pid, int *childpid)
1187 /* This request is only available on HPUX 10.0 and later. */
1188 #if !defined(PT_GET_PROCESS_STATE)
1193 ptrace_state_t ptrace_state;
1196 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1198 (PTRACE_ARG3_TYPE) & ptrace_state,
1199 sizeof (ptrace_state));
1201 perror_with_name ("ptrace");
1205 if (ptrace_state.pe_report_event & PTRACE_FORK)
1207 *childpid = ptrace_state.pe_other_pid;
1216 hpux_has_vforked (int pid, int *childpid)
1218 /* This request is only available on HPUX 10.0 and later. */
1219 #if !defined(PT_GET_PROCESS_STATE)
1225 ptrace_state_t ptrace_state;
1228 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1230 (PTRACE_ARG3_TYPE) & ptrace_state,
1231 sizeof (ptrace_state));
1233 perror_with_name ("ptrace");
1237 if (ptrace_state.pe_report_event & PTRACE_VFORK)
1239 *childpid = ptrace_state.pe_other_pid;
1248 child_insert_exec_catchpoint (int pid)
1250 /* This request is only available on HPUX 10.0 and later. */
1251 #if !defined(PT_SET_EVENT_MASK)
1252 error ("Unable to catch execs prior to HPUX 10.0");
1255 /* Enable reporting of exec events from the kernel. */
1256 /* ??rehrauer: For the moment, we're always enabling these events,
1257 and just ignoring them if there's no catchpoint to catch them. */
1263 child_remove_exec_catchpoint (int pid)
1265 /* This request is only available on HPUX 10.0 and later. */
1266 #if !defined(PT_SET_EVENT_MASK)
1267 error ("Unable to catch execs prior to HPUX 10.0");
1270 /* Disable reporting of exec events from the kernel. */
1271 /* ??rehrauer: For the moment, we're always enabling these events,
1272 and just ignoring them if there's no catchpoint to catch them. */
1278 hpux_has_execd (int pid, char **execd_pathname)
1280 /* This request is only available on HPUX 10.0 and later. */
1281 #if !defined(PT_GET_PROCESS_STATE)
1282 *execd_pathname = NULL;
1287 ptrace_state_t ptrace_state;
1290 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1292 (PTRACE_ARG3_TYPE) & ptrace_state,
1293 sizeof (ptrace_state));
1295 perror_with_name ("ptrace");
1299 if (ptrace_state.pe_report_event & PTRACE_EXEC)
1301 char *exec_file = target_pid_to_exec_file (pid);
1302 *execd_pathname = savestring (exec_file, strlen (exec_file));
1311 child_reported_exec_events_per_exec_call (void)
1313 return 2; /* ptrace reports the event twice per call. */
1317 hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
1319 /* This request is only available on HPUX 10.30 and later, via
1320 the ttrace interface. */
1322 *kind = TARGET_WAITKIND_SPURIOUS;
1328 child_pid_to_exec_file (int pid)
1330 static char exec_file_buffer[1024];
1332 CORE_ADDR top_of_stack;
1336 ptid_t saved_inferior_ptid;
1339 #ifdef PT_GET_PROCESS_PATHNAME
1340 /* As of 10.x HP-UX, there's an explicit request to get the pathname. */
1341 pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME,
1343 (PTRACE_ARG3_TYPE) exec_file_buffer,
1344 sizeof (exec_file_buffer) - 1);
1346 return exec_file_buffer;
1349 /* It appears that this request is broken prior to 10.30.
1350 If it fails, try a really, truly amazingly gross hack
1351 that DDE uses, of pawing through the process' data
1352 segment to find the pathname. */
1354 top_of_stack = 0x7b03a000;
1358 /* On the chance that pid != inferior_ptid, set inferior_ptid
1359 to pid, so that (grrrr!) implicit uses of inferior_ptid get
1362 saved_inferior_ptid = inferior_ptid;
1363 inferior_ptid = pid_to_ptid (pid);
1365 /* Try to grab a null-terminated string. */
1368 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
1370 inferior_ptid = saved_inferior_ptid;
1373 for (i = 0; i < 4; i++)
1375 exec_file_buffer[name_index++] = four_chars[i];
1376 done = (four_chars[i] == '\0');
1383 if (exec_file_buffer[0] == '\0')
1385 inferior_ptid = saved_inferior_ptid;
1389 inferior_ptid = saved_inferior_ptid;
1390 return exec_file_buffer;
1394 pre_fork_inferior (void)
1398 status = pipe (startup_semaphore.parent_channel);
1401 warning ("error getting parent pipe for startup semaphore");
1405 status = pipe (startup_semaphore.child_channel);
1408 warning ("error getting child pipe for startup semaphore");
1414 /* Check to see if the given thread is alive.
1416 This is a no-op, as ptrace doesn't support threads, so we just
1420 child_thread_alive (ptid_t ptid)
1425 #endif /* ! GDB_NATIVE_HPUX_11 */