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>
34 #include "gdb_string.h"
37 extern CORE_ADDR text_end;
39 extern int hpux_has_forked (int pid, int *childpid);
40 extern int hpux_has_vforked (int pid, int *childpid);
41 extern int hpux_has_execd (int pid, char **execd_pathname);
42 extern int hpux_has_syscall_event (int pid, enum target_waitkind *kind,
45 static void fetch_register (int);
48 fetch_inferior_registers (int regno)
51 for (regno = 0; regno < NUM_REGS; regno++)
52 fetch_register (regno);
54 fetch_register (regno);
57 /* Our own version of the offsetof macro, since we can't assume ANSI C. */
58 #define HPPAH_OFFSETOF(type, member) ((int) (&((type *) 0)->member))
60 /* Store our register values back into the inferior.
61 If REGNO is -1, do this for all registers.
62 Otherwise, REGNO specifies which register (so we can save time). */
65 store_inferior_registers (int regno)
70 unsigned int offset = U_REGS_OFFSET;
75 unsigned int addr, len, offset;
77 if (CANNOT_STORE_REGISTER (regno))
81 len = REGISTER_RAW_SIZE (regno);
83 /* Requests for register zero actually want the save_state's
84 ss_flags member. As RM says: "Oh, what a hack!" */
88 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
89 len = sizeof (ss.ss_flags);
91 /* Note that ss_flags is always an int, no matter what
92 REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA machines
93 are big-endian, put it at the least significant end of the
94 value, and zap the rest of the buffer. */
95 offset = REGISTER_RAW_SIZE (0) - len;
98 /* Floating-point registers come from the ss_fpblock area. */
99 else if (regno >= FP0_REGNUM)
100 addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock)
101 + (REGISTER_BYTE (regno) - REGISTER_BYTE (FP0_REGNUM)));
103 /* Wide registers come from the ss_wide area.
104 I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
105 between ss_wide and ss_narrow than to use the raw register size.
106 But checking ss_flags would require an extra ptrace call for
107 every register reference. Bleah. */
109 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
110 + REGISTER_BYTE (regno));
112 /* Narrow registers come from the ss_narrow area. Note that
113 ss_narrow starts with gr1, not gr0. */
115 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
116 + (REGISTER_BYTE (regno) - REGISTER_BYTE (1)));
118 internal_error (__FILE__, __LINE__,
119 "hppah-nat.c (write_register): unexpected register size");
121 #ifdef GDB_TARGET_IS_HPPA_20W
122 /* Unbelieveable. The PC head and tail must be written in 64bit hunks
123 or we will get an error. Worse yet, the oddball ptrace/ttrace
124 layering will not allow us to perform a 64bit register store.
127 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM && len == 8)
131 temp = *(CORE_ADDR *)&deprecated_registers[REGISTER_BYTE (regno)];
133 /* Set the priv level (stored in the low two bits of the PC. */
136 ttrace_write_reg_64 (PIDGET (inferior_ptid), (CORE_ADDR)addr,
139 /* If we fail to write the PC, give a true error instead of
143 char *err = safe_strerror (errno);
144 char *msg = alloca (strlen (err) + 128);
145 sprintf (msg, "writing `%s' register: %s",
146 REGISTER_NAME (regno), err);
147 perror_with_name (msg);
152 /* Another crock. HPUX complains if you write a nonzero value to
153 the high part of IPSW. What will it take for HP to catch a
154 clue about building sensible interfaces? */
155 if (regno == IPSW_REGNUM && len == 8)
156 *(int *)&deprecated_registers[REGISTER_BYTE (regno)] = 0;
159 for (i = 0; i < len; i += sizeof (int))
162 call_ptrace (PT_WUREGS, PIDGET (inferior_ptid),
163 (PTRACE_ARG3_TYPE) addr + i,
164 *(int *) &deprecated_registers[REGISTER_BYTE (regno) + i]);
167 /* Warning, not error, in case we are attached; sometimes
168 the kernel doesn't let us at the registers. */
169 char *err = safe_strerror (errno);
170 char *msg = alloca (strlen (err) + 128);
171 sprintf (msg, "writing `%s' register: %s",
172 REGISTER_NAME (regno), err);
173 /* If we fail to write the PC, give a true error instead of
175 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
176 perror_with_name (msg);
184 for (regno = 0; regno < NUM_REGS; regno++)
185 store_inferior_registers (regno);
189 /* Fetch a register's value from the process's U area. */
191 fetch_register (int regno)
193 char buf[MAX_REGISTER_SIZE];
194 unsigned int addr, len, offset;
198 len = REGISTER_RAW_SIZE (regno);
200 /* Requests for register zero actually want the save_state's
201 ss_flags member. As RM says: "Oh, what a hack!" */
205 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
206 len = sizeof (ss.ss_flags);
208 /* Note that ss_flags is always an int, no matter what
209 REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA machines
210 are big-endian, put it at the least significant end of the
211 value, and zap the rest of the buffer. */
212 offset = REGISTER_RAW_SIZE (0) - len;
213 memset (buf, 0, sizeof (buf));
216 /* Floating-point registers come from the ss_fpblock area. */
217 else if (regno >= FP0_REGNUM)
218 addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock)
219 + (REGISTER_BYTE (regno) - REGISTER_BYTE (FP0_REGNUM)));
221 /* Wide registers come from the ss_wide area.
222 I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
223 between ss_wide and ss_narrow than to use the raw register size.
224 But checking ss_flags would require an extra ptrace call for
225 every register reference. Bleah. */
227 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
228 + REGISTER_BYTE (regno));
230 /* Narrow registers come from the ss_narrow area. Note that
231 ss_narrow starts with gr1, not gr0. */
233 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
234 + (REGISTER_BYTE (regno) - REGISTER_BYTE (1)));
237 internal_error (__FILE__, __LINE__,
238 "hppa-nat.c (fetch_register): unexpected register size");
240 for (i = 0; i < len; i += sizeof (int))
243 /* Copy an int from the U area to buf. Fill the least
244 significant end if len != raw_size. */
245 * (int *) &buf[offset + i] =
246 call_ptrace (PT_RUREGS, PIDGET (inferior_ptid),
247 (PTRACE_ARG3_TYPE) addr + i, 0);
250 /* Warning, not error, in case we are attached; sometimes
251 the kernel doesn't let us at the registers. */
252 char *err = safe_strerror (errno);
253 char *msg = alloca (strlen (err) + 128);
254 sprintf (msg, "reading `%s' register: %s",
255 REGISTER_NAME (regno), err);
261 /* If we're reading an address from the instruction address queue,
262 mask out the bottom two bits --- they contain the privilege
264 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
265 buf[len - 1] &= ~0x3;
267 supply_register (regno, buf);
271 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
272 to debugger memory starting at MYADDR. Copy to inferior if
275 Returns the length copied, which is either the LEN argument or zero.
276 This xfer function does not do partial moves, since child_ops
277 doesn't allow memory operations to cross below us in the target stack
278 anyway. TARGET is ignored. */
281 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
282 struct mem_attrib *mem,
283 struct target_ops *target)
286 /* Round starting address down to longword boundary. */
287 CORE_ADDR addr = memaddr & - (CORE_ADDR)(sizeof (int));
288 /* Round ending address up; get number of longwords that makes. */
290 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
292 /* Allocate buffer of that many longwords.
293 Note -- do not use alloca to allocate this buffer since there is no
294 guarantee of when the buffer will actually be deallocated.
296 This routine can be called over and over with the same call chain;
297 this (in effect) would pile up all those alloca requests until a call
298 to alloca was made from a point higher than this routine in the
300 int *buffer = (int *) xmalloc (count * sizeof (int));
304 /* Fill start and end extra bytes of buffer with existing memory data. */
305 if (addr != memaddr || len < (int) sizeof (int))
307 /* Need part of initial word -- fetch it. */
308 buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
309 PIDGET (inferior_ptid),
310 (PTRACE_ARG3_TYPE) addr, 0);
313 if (count > 1) /* FIXME, avoid if even boundary */
316 = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
317 PIDGET (inferior_ptid),
318 (PTRACE_ARG3_TYPE) (addr
319 + (count - 1) * sizeof (int)),
323 /* Copy data to be written over corresponding part of buffer */
324 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
326 /* Write the entire buffer. */
327 for (i = 0; i < count; i++, addr += sizeof (int))
331 /* The HP-UX kernel crashes if you use PT_WDUSER to write into the
332 text segment. FIXME -- does it work to write into the data
333 segment using WIUSER, or do these idiots really expect us to
334 figure out which segment the address is in, so we can use a
335 separate system call for it??! */
337 pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER;
338 pt_status = call_ptrace (pt_request,
339 PIDGET (inferior_ptid),
340 (PTRACE_ARG3_TYPE) addr,
343 /* Did we fail? Might we've guessed wrong about which
344 segment this address resides in? Try the other request,
345 and see if that works... */
346 if ((pt_status == -1) && errno)
349 pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER;
350 pt_status = call_ptrace (pt_request,
351 PIDGET (inferior_ptid),
352 (PTRACE_ARG3_TYPE) addr,
355 /* No, we still fail. Okay, time to punt. */
356 if ((pt_status == -1) && errno)
366 /* Read all the longwords */
367 for (i = 0; i < count; i++, addr += sizeof (int))
370 buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
371 PIDGET (inferior_ptid),
372 (PTRACE_ARG3_TYPE) addr, 0);
381 /* Copy appropriate bytes out of the buffer. */
382 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
388 char *saved_child_execd_pathname = NULL;
396 } saved_vfork_state = STATE_NONE;
399 child_follow_fork (int follow_child)
402 struct target_waitstatus last_status;
404 int parent_pid, child_pid;
406 get_last_target_status (&last_ptid, &last_status);
407 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
408 parent_pid = ptid_get_pid (last_ptid);
409 child_pid = last_status.value.related_pid;
411 /* At this point, if we are vforking, breakpoints were already
412 detached from the child in child_wait; and the child has already
413 called execve(). If we are forking, both the parent and child
414 have breakpoints inserted. */
420 detach_breakpoints (child_pid);
421 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
422 SOLIB_REMOVE_INFERIOR_HOOK (child_pid);
426 /* Detach from the child. */
427 printf_unfiltered ("Detaching after fork from %s\n",
428 target_pid_to_str (pid_to_ptid (child_pid)));
429 hppa_require_detach (child_pid, 0);
431 /* The parent and child of a vfork share the same address space.
432 Also, on some targets the order in which vfork and exec events
433 are received for parent in child requires some delicate handling
436 For instance, on ptrace-based HPUX we receive the child's vfork
437 event first, at which time the parent has been suspended by the
438 OS and is essentially untouchable until the child's exit or second
439 exec event arrives. At that time, the parent's vfork event is
440 delivered to us, and that's when we see and decide how to follow
441 the vfork. But to get to that point, we must continue the child
442 until it execs or exits. To do that smoothly, all breakpoints
443 must be removed from the child, in case there are any set between
444 the vfork() and exec() calls. But removing them from the child
445 also removes them from the parent, due to the shared-address-space
446 nature of a vfork'd parent and child. On HPUX, therefore, we must
447 take care to restore the bp's to the parent before we continue it.
448 Else, it's likely that we may not stop in the expected place. (The
449 worst scenario is when the user tries to step over a vfork() call;
450 the step-resume bp must be restored for the step to properly stop
451 in the parent after the call completes!)
453 Sequence of events, as reported to gdb from HPUX:
455 Parent Child Action for gdb to take
456 -------------------------------------------------------
457 1 VFORK Continue child
462 Now that the child has safely exec'd or exited, we must restore
463 the parent's breakpoints before we continue it. Else, we may
464 cause it run past expected stopping points. */
467 reattach_breakpoints (parent_pid);
471 /* Needed to keep the breakpoint lists in sync. */
473 detach_breakpoints (child_pid);
475 /* Before detaching from the parent, remove all breakpoints from it. */
476 remove_breakpoints ();
478 /* Also reset the solib inferior hook from the parent. */
479 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
480 SOLIB_REMOVE_INFERIOR_HOOK (PIDGET (inferior_ptid));
483 /* Detach from the parent. */
484 target_detach (NULL, 1);
486 /* Attach to the child. */
487 printf_unfiltered ("Attaching after fork to %s\n",
488 target_pid_to_str (pid_to_ptid (child_pid)));
489 hppa_require_attach (child_pid);
490 inferior_ptid = pid_to_ptid (child_pid);
492 /* If we vforked, then we've also execed by now. The exec will be
493 reported momentarily. follow_exec () will handle breakpoints, so
494 we don't have to.. */
496 follow_inferior_reset_breakpoints ();
501 /* If we followed the parent, don't try to follow the child's exec. */
502 if (saved_vfork_state != STATE_GOT_PARENT
503 && saved_vfork_state != STATE_FAKE_EXEC)
504 fprintf_unfiltered (gdb_stdout,
505 "hppa: post follow vfork: confused state\n");
507 if (! follow_child || saved_vfork_state == STATE_GOT_PARENT)
508 saved_vfork_state = STATE_NONE;
515 /* Format a process id, given PID. Be sure to terminate
516 this with a null--it's going to be printed via a "%s". */
518 child_pid_to_str (ptid_t ptid)
520 /* Static because address returned */
522 pid_t pid = PIDGET (ptid);
524 /* Extra NUL for paranoia's sake */
525 sprintf (buf, "process %d%c", pid, '\0');
530 /* Format a thread id, given TID. Be sure to terminate
531 this with a null--it's going to be printed via a "%s".
533 Note: This is a core-gdb tid, not the actual system tid.
534 See infttrace.c for details. */
536 hppa_tid_to_str (ptid_t ptid)
538 /* Static because address returned */
540 /* This seems strange, but when I did the ptid conversion, it looked
541 as though a pid was always being passed. - Kevin Buettner */
542 pid_t tid = PIDGET (ptid);
544 /* Extra NULLs for paranoia's sake */
545 sprintf (buf, "system thread %d%c", tid, '\0');
551 /* Enable HACK for ttrace work. In
552 * infttrace.c/require_notification_of_events,
553 * this is set to 0 so that the loop in child_wait
556 int not_same_real_pid = 1;
559 /* Wait for child to do something. Return pid of child, or -1 in case
560 of error; store status through argument pointer OURSTATUS. */
563 child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
567 char *execd_pathname = NULL;
571 enum target_waitkind kind;
574 if (saved_vfork_state == STATE_FAKE_EXEC)
576 saved_vfork_state = STATE_NONE;
577 ourstatus->kind = TARGET_WAITKIND_EXECD;
578 ourstatus->value.execd_pathname = saved_child_execd_pathname;
579 return inferior_ptid;
584 set_sigint_trap (); /* Causes SIGINT to be passed on to the
588 pid = ptrace_wait (inferior_ptid, &status);
594 clear_sigint_trap ();
598 if (save_errno == EINTR)
601 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
602 safe_strerror (save_errno));
604 /* Claim it exited with unknown signal. */
605 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
606 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
607 return pid_to_ptid (-1);
612 if (target_has_exited (pid, status, &exit_status))
614 /* ??rehrauer: For now, ignore this. */
618 if (!target_thread_alive (pid_to_ptid (pid)))
620 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
621 return pid_to_ptid (pid);
624 if (hpux_has_forked (pid, &related_pid))
626 /* Ignore the parent's fork event. */
627 if (pid == PIDGET (inferior_ptid))
629 ourstatus->kind = TARGET_WAITKIND_IGNORE;
630 return inferior_ptid;
633 /* If this is the child's fork event, report that the
634 process has forked. */
635 if (related_pid == PIDGET (inferior_ptid))
637 ourstatus->kind = TARGET_WAITKIND_FORKED;
638 ourstatus->value.related_pid = pid;
639 return inferior_ptid;
643 if (hpux_has_vforked (pid, &related_pid))
645 if (pid == PIDGET (inferior_ptid))
647 if (saved_vfork_state == STATE_GOT_CHILD)
648 saved_vfork_state = STATE_GOT_PARENT;
649 else if (saved_vfork_state == STATE_GOT_EXEC)
650 saved_vfork_state = STATE_FAKE_EXEC;
652 fprintf_unfiltered (gdb_stdout,
653 "hppah: parent vfork: confused\n");
655 else if (related_pid == PIDGET (inferior_ptid))
657 if (saved_vfork_state == STATE_NONE)
658 saved_vfork_state = STATE_GOT_CHILD;
660 fprintf_unfiltered (gdb_stdout,
661 "hppah: child vfork: confused\n");
664 fprintf_unfiltered (gdb_stdout,
665 "hppah: unknown vfork: confused\n");
667 if (saved_vfork_state == STATE_GOT_CHILD)
669 child_post_startup_inferior (pid_to_ptid (pid));
670 detach_breakpoints (pid);
671 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
672 SOLIB_REMOVE_INFERIOR_HOOK (pid);
674 child_resume (pid_to_ptid (pid), 0, TARGET_SIGNAL_0);
675 ourstatus->kind = TARGET_WAITKIND_IGNORE;
676 return pid_to_ptid (related_pid);
678 else if (saved_vfork_state == STATE_FAKE_EXEC)
680 ourstatus->kind = TARGET_WAITKIND_VFORKED;
681 ourstatus->value.related_pid = related_pid;
682 return pid_to_ptid (pid);
686 /* We saw the parent's vfork, but we haven't seen the exec yet.
687 Wait for it, for simplicity's sake. It should be pending. */
688 saved_vfork_pid = related_pid;
689 ourstatus->kind = TARGET_WAITKIND_IGNORE;
690 return pid_to_ptid (pid);
694 if (hpux_has_execd (pid, &execd_pathname))
696 /* On HP-UX, events associated with a vforking inferior come in
697 threes: a vfork event for the child (always first), followed
698 a vfork event for the parent and an exec event for the child.
699 The latter two can come in either order. Make sure we get
701 if (saved_vfork_state != STATE_NONE)
703 if (saved_vfork_state == STATE_GOT_CHILD)
705 saved_vfork_state = STATE_GOT_EXEC;
706 /* On HP/UX with ptrace, the child must be resumed before
707 the parent vfork event is delivered. A single-step
709 if (RESUME_EXECD_VFORKING_CHILD_TO_GET_PARENT_VFORK ())
710 target_resume (pid_to_ptid (pid), 1, TARGET_SIGNAL_0);
711 ourstatus->kind = TARGET_WAITKIND_IGNORE;
713 else if (saved_vfork_state == STATE_GOT_PARENT)
715 saved_vfork_state = STATE_FAKE_EXEC;
716 ourstatus->kind = TARGET_WAITKIND_VFORKED;
717 ourstatus->value.related_pid = saved_vfork_pid;
720 fprintf_unfiltered (gdb_stdout,
721 "hppa: exec: unexpected state\n");
723 saved_child_execd_pathname = execd_pathname;
725 return inferior_ptid;
728 /* Are we ignoring initial exec events? (This is likely because
729 we're in the process of starting up the inferior, and another
730 (older) mechanism handles those.) If so, we'll report this
731 as a regular stop, not an exec.
733 if (inferior_ignoring_startup_exec_events)
735 inferior_ignoring_startup_exec_events--;
739 ourstatus->kind = TARGET_WAITKIND_EXECD;
740 ourstatus->value.execd_pathname = execd_pathname;
741 return pid_to_ptid (pid);
745 /* All we must do with these is communicate their occurrence
746 to wait_for_inferior...
748 if (hpux_has_syscall_event (pid, &kind, &syscall_id))
750 ourstatus->kind = kind;
751 ourstatus->value.syscall_id = syscall_id;
752 return pid_to_ptid (pid);
755 /*## } while (pid != PIDGET (inferior_ptid)); ## *//* Some other child died or stopped */
756 /* hack for thread testing */
758 while ((pid != PIDGET (inferior_ptid)) && not_same_real_pid);
761 store_waitstatus (ourstatus, status);
762 return pid_to_ptid (pid);
765 #if !defined (GDB_NATIVE_HPUX_11)
767 /* The following code is a substitute for the infttrace.c versions used
768 with ttrace() in HPUX 11. */
770 /* This value is an arbitrary integer. */
771 #define PT_VERSION 123456
773 /* This semaphore is used to coordinate the child and parent processes
774 after a fork(), and before an exec() by the child. See
775 parent_attach_all for details. */
779 int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */
780 int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */
785 #define SEM_LISTEN (0)
787 static startup_semaphore_t startup_semaphore;
790 /* This function causes the caller's process to be traced by its
791 parent. This is intended to be called after GDB forks itself,
792 and before the child execs the target.
794 Note that HP-UX ptrace is rather funky in how this is done.
795 If the parent wants to get the initial exec event of a child,
796 it must set the ptrace event mask of the child to include execs.
797 (The child cannot do this itself.) This must be done after the
798 child is forked, but before it execs.
800 To coordinate the parent and child, we implement a semaphore using
801 pipes. After SETTRC'ing itself, the child tells the parent that
802 it is now traceable by the parent, and waits for the parent's
803 acknowledgement. The parent can then set the child's event mask,
804 and notify the child that it can now exec.
806 (The acknowledgement by parent happens as a result of a call to
807 child_acknowledge_created_inferior.) */
810 parent_attach_all (int pid, PTRACE_ARG3_TYPE addr, int data)
814 /* We need a memory home for a constant. */
815 int tc_magic_child = PT_VERSION;
816 int tc_magic_parent = 0;
818 /* The remainder of this function is only useful for HPUX 10.0 and
819 later, as it depends upon the ability to request notification
820 of specific kinds of events by the kernel. */
821 #if defined(PT_SET_EVENT_MASK)
823 /* Notify the parent that we're potentially ready to exec(). */
824 write (startup_semaphore.child_channel[SEM_TALK],
826 sizeof (tc_magic_child));
828 /* Wait for acknowledgement from the parent. */
829 read (startup_semaphore.parent_channel[SEM_LISTEN],
831 sizeof (tc_magic_parent));
832 if (tc_magic_child != tc_magic_parent)
833 warning ("mismatched semaphore magic");
835 /* Discard our copy of the semaphore. */
836 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
837 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
838 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
839 (void) close (startup_semaphore.child_channel[SEM_TALK]);
847 hppa_require_attach (int pid)
852 unsigned int regs_offset;
854 /* Are we already attached? There appears to be no explicit way to
855 answer this via ptrace, so we try something which should be
856 innocuous if we are attached. If that fails, then we assume
857 we're not attached, and so attempt to make it so. */
860 regs_offset = U_REGS_OFFSET;
861 pc_addr = register_addr (PC_REGNUM, regs_offset);
862 pc = call_ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE) pc_addr, 0);
867 pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
872 /* Now we really are attached. */
880 hppa_require_detach (int pid, int signal)
883 call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal);
884 errno = 0; /* Ignore any errors. */
888 /* Since ptrace doesn't support memory page-protection events, which
889 are used to implement "hardware" watchpoints on HP-UX, these are
890 dummy versions, which perform no useful work. */
893 hppa_enable_page_protection_events (int pid)
898 hppa_disable_page_protection_events (int pid)
903 hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
905 error ("Hardware watchpoints not implemented on this platform.");
909 hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
911 error ("Hardware watchpoints not implemented on this platform.");
915 hppa_can_use_hw_watchpoint (int type, int cnt, int ot)
921 hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
923 error ("Hardware watchpoints not implemented on this platform.");
927 hppa_pid_or_tid_to_str (ptid_t id)
929 /* In the ptrace world, there are only processes. */
930 return child_pid_to_str (id);
934 hppa_ensure_vforking_parent_remains_stopped (int pid)
936 /* This assumes that the vforked parent is presently stopped, and
937 that the vforked child has just delivered its first exec event.
938 Calling kill() this way will cause the SIGTRAP to be delivered as
939 soon as the parent is resumed, which happens as soon as the
940 vforked child is resumed. See wait_for_inferior for the use of
946 hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
948 return 1; /* Yes, the child must be resumed. */
952 require_notification_of_events (int pid)
954 #if defined(PT_SET_EVENT_MASK)
956 ptrace_event_t ptrace_events;
960 /* Instruct the kernel as to the set of events we wish to be
961 informed of. (This support does not exist before HPUX 10.0.
962 We'll assume if PT_SET_EVENT_MASK has not been defined by
963 <sys/ptrace.h>, then we're being built on pre-10.0.) */
964 memset (&ptrace_events, 0, sizeof (ptrace_events));
966 /* Note: By default, all signals are visible to us. If we wish
967 the kernel to keep certain signals hidden from us, we do it
968 by calling sigdelset (ptrace_events.pe_signals, signal) for
969 each such signal here, before doing PT_SET_EVENT_MASK. */
970 /* RM: The above comment is no longer true. We start with ignoring
971 all signals, and then add the ones we are interested in. We could
972 do it the other way: start by looking at all signals and then
973 deleting the ones that we aren't interested in, except that
974 multiple gdb signals may be mapped to the same host signal
975 (eg. TARGET_SIGNAL_IO and TARGET_SIGNAL_POLL both get mapped to
976 signal 22 on HPUX 10.20) We want to be notified if we are
977 interested in either signal. */
978 sigfillset (&ptrace_events.pe_signals);
980 /* RM: Let's not bother with signals we don't care about */
981 nsigs = (int) TARGET_SIGNAL_LAST;
982 for (signum = nsigs; signum > 0; signum--)
984 if ((signal_stop_state (signum)) ||
985 (signal_print_state (signum)) ||
986 (!signal_pass_state (signum)))
988 if (target_signal_to_host_p (signum))
989 sigdelset (&ptrace_events.pe_signals,
990 target_signal_to_host (signum));
994 ptrace_events.pe_set_event = 0;
996 ptrace_events.pe_set_event |= PTRACE_SIGNAL;
997 ptrace_events.pe_set_event |= PTRACE_EXEC;
998 ptrace_events.pe_set_event |= PTRACE_FORK;
999 ptrace_events.pe_set_event |= PTRACE_VFORK;
1000 /* ??rehrauer: Add this one when we're prepared to catch it...
1001 ptrace_events.pe_set_event |= PTRACE_EXIT;
1005 pt_status = call_ptrace (PT_SET_EVENT_MASK,
1007 (PTRACE_ARG3_TYPE) & ptrace_events,
1008 sizeof (ptrace_events));
1010 perror_with_name ("ptrace");
1017 require_notification_of_exec_events (int pid)
1019 #if defined(PT_SET_EVENT_MASK)
1021 ptrace_event_t ptrace_events;
1023 /* Instruct the kernel as to the set of events we wish to be
1024 informed of. (This support does not exist before HPUX 10.0.
1025 We'll assume if PT_SET_EVENT_MASK has not been defined by
1026 <sys/ptrace.h>, then we're being built on pre-10.0.) */
1027 memset (&ptrace_events, 0, sizeof (ptrace_events));
1029 /* Note: By default, all signals are visible to us. If we wish
1030 the kernel to keep certain signals hidden from us, we do it
1031 by calling sigdelset (ptrace_events.pe_signals, signal) for
1032 each such signal here, before doing PT_SET_EVENT_MASK. */
1033 sigemptyset (&ptrace_events.pe_signals);
1035 ptrace_events.pe_set_event = 0;
1037 ptrace_events.pe_set_event |= PTRACE_EXEC;
1038 /* ??rehrauer: Add this one when we're prepared to catch it...
1039 ptrace_events.pe_set_event |= PTRACE_EXIT;
1043 pt_status = call_ptrace (PT_SET_EVENT_MASK,
1045 (PTRACE_ARG3_TYPE) & ptrace_events,
1046 sizeof (ptrace_events));
1048 perror_with_name ("ptrace");
1054 /* This function is called by the parent process, with pid being the
1055 ID of the child process, after the debugger has forked. */
1058 child_acknowledge_created_inferior (int pid)
1060 /* We need a memory home for a constant. */
1061 int tc_magic_parent = PT_VERSION;
1062 int tc_magic_child = 0;
1064 /* The remainder of this function is only useful for HPUX 10.0 and
1065 later, as it depends upon the ability to request notification
1066 of specific kinds of events by the kernel. */
1067 #if defined(PT_SET_EVENT_MASK)
1068 /* Wait for the child to tell us that it has forked. */
1069 read (startup_semaphore.child_channel[SEM_LISTEN],
1071 sizeof (tc_magic_child));
1073 /* Notify the child that it can exec.
1075 In the infttrace.c variant of this function, we set the child's
1076 event mask after the fork but before the exec. In the ptrace
1077 world, it seems we can't set the event mask until after the exec. */
1078 write (startup_semaphore.parent_channel[SEM_TALK],
1080 sizeof (tc_magic_parent));
1082 /* We'd better pause a bit before trying to set the event mask,
1083 though, to ensure that the exec has happened. We don't want to
1084 wait() on the child, because that'll screw up the upper layers
1085 of gdb's execution control that expect to see the exec event.
1087 After an exec, the child is no longer executing gdb code. Hence,
1088 we can't have yet another synchronization via the pipes. We'll
1089 just sleep for a second, and hope that's enough delay... */
1092 /* Instruct the kernel as to the set of events we wish to be
1094 require_notification_of_exec_events (pid);
1096 /* Discard our copy of the semaphore. */
1097 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
1098 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
1099 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
1100 (void) close (startup_semaphore.child_channel[SEM_TALK]);
1105 child_post_startup_inferior (ptid_t ptid)
1107 require_notification_of_events (PIDGET (ptid));
1111 child_post_attach (int pid)
1113 require_notification_of_events (pid);
1117 child_insert_fork_catchpoint (int pid)
1119 /* This request is only available on HPUX 10.0 and later. */
1120 #if !defined(PT_SET_EVENT_MASK)
1121 error ("Unable to catch forks prior to HPUX 10.0");
1123 /* Enable reporting of fork events from the kernel. */
1124 /* ??rehrauer: For the moment, we're always enabling these events,
1125 and just ignoring them if there's no catchpoint to catch them. */
1131 child_remove_fork_catchpoint (int pid)
1133 /* This request is only available on HPUX 10.0 and later. */
1134 #if !defined(PT_SET_EVENT_MASK)
1135 error ("Unable to catch forks prior to HPUX 10.0");
1137 /* Disable reporting of fork events from the kernel. */
1138 /* ??rehrauer: For the moment, we're always enabling these events,
1139 and just ignoring them if there's no catchpoint to catch them. */
1145 child_insert_vfork_catchpoint (int pid)
1147 /* This request is only available on HPUX 10.0 and later. */
1148 #if !defined(PT_SET_EVENT_MASK)
1149 error ("Unable to catch vforks prior to HPUX 10.0");
1151 /* Enable reporting of vfork events from the kernel. */
1152 /* ??rehrauer: For the moment, we're always enabling these events,
1153 and just ignoring them if there's no catchpoint to catch them. */
1159 child_remove_vfork_catchpoint (int pid)
1161 /* This request is only available on HPUX 10.0 and later. */
1162 #if !defined(PT_SET_EVENT_MASK)
1163 error ("Unable to catch vforks prior to HPUX 10.0");
1165 /* Disable reporting of vfork events from the kernel. */
1166 /* ??rehrauer: For the moment, we're always enabling these events,
1167 and just ignoring them if there's no catchpoint to catch them. */
1173 hpux_has_forked (int pid, int *childpid)
1175 /* This request is only available on HPUX 10.0 and later. */
1176 #if !defined(PT_GET_PROCESS_STATE)
1181 ptrace_state_t ptrace_state;
1184 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1186 (PTRACE_ARG3_TYPE) & ptrace_state,
1187 sizeof (ptrace_state));
1189 perror_with_name ("ptrace");
1193 if (ptrace_state.pe_report_event & PTRACE_FORK)
1195 *childpid = ptrace_state.pe_other_pid;
1204 hpux_has_vforked (int pid, int *childpid)
1206 /* This request is only available on HPUX 10.0 and later. */
1207 #if !defined(PT_GET_PROCESS_STATE)
1213 ptrace_state_t ptrace_state;
1216 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1218 (PTRACE_ARG3_TYPE) & ptrace_state,
1219 sizeof (ptrace_state));
1221 perror_with_name ("ptrace");
1225 if (ptrace_state.pe_report_event & PTRACE_VFORK)
1227 *childpid = ptrace_state.pe_other_pid;
1236 child_insert_exec_catchpoint (int pid)
1238 /* This request is only available on HPUX 10.0 and later. */
1239 #if !defined(PT_SET_EVENT_MASK)
1240 error ("Unable to catch execs prior to HPUX 10.0");
1243 /* Enable reporting of exec events from the kernel. */
1244 /* ??rehrauer: For the moment, we're always enabling these events,
1245 and just ignoring them if there's no catchpoint to catch them. */
1251 child_remove_exec_catchpoint (int pid)
1253 /* This request is only available on HPUX 10.0 and later. */
1254 #if !defined(PT_SET_EVENT_MASK)
1255 error ("Unable to catch execs prior to HPUX 10.0");
1258 /* Disable reporting of exec events from the kernel. */
1259 /* ??rehrauer: For the moment, we're always enabling these events,
1260 and just ignoring them if there's no catchpoint to catch them. */
1266 hpux_has_execd (int pid, char **execd_pathname)
1268 /* This request is only available on HPUX 10.0 and later. */
1269 #if !defined(PT_GET_PROCESS_STATE)
1270 *execd_pathname = NULL;
1275 ptrace_state_t ptrace_state;
1278 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
1280 (PTRACE_ARG3_TYPE) & ptrace_state,
1281 sizeof (ptrace_state));
1283 perror_with_name ("ptrace");
1287 if (ptrace_state.pe_report_event & PTRACE_EXEC)
1289 char *exec_file = target_pid_to_exec_file (pid);
1290 *execd_pathname = savestring (exec_file, strlen (exec_file));
1299 child_reported_exec_events_per_exec_call (void)
1301 return 2; /* ptrace reports the event twice per call. */
1305 hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
1307 /* This request is only available on HPUX 10.30 and later, via
1308 the ttrace interface. */
1310 *kind = TARGET_WAITKIND_SPURIOUS;
1316 child_pid_to_exec_file (int pid)
1318 static char exec_file_buffer[1024];
1320 CORE_ADDR top_of_stack;
1324 ptid_t saved_inferior_ptid;
1327 #ifdef PT_GET_PROCESS_PATHNAME
1328 /* As of 10.x HP-UX, there's an explicit request to get the pathname. */
1329 pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME,
1331 (PTRACE_ARG3_TYPE) exec_file_buffer,
1332 sizeof (exec_file_buffer) - 1);
1334 return exec_file_buffer;
1337 /* It appears that this request is broken prior to 10.30.
1338 If it fails, try a really, truly amazingly gross hack
1339 that DDE uses, of pawing through the process' data
1340 segment to find the pathname. */
1342 top_of_stack = 0x7b03a000;
1346 /* On the chance that pid != inferior_ptid, set inferior_ptid
1347 to pid, so that (grrrr!) implicit uses of inferior_ptid get
1350 saved_inferior_ptid = inferior_ptid;
1351 inferior_ptid = pid_to_ptid (pid);
1353 /* Try to grab a null-terminated string. */
1356 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
1358 inferior_ptid = saved_inferior_ptid;
1361 for (i = 0; i < 4; i++)
1363 exec_file_buffer[name_index++] = four_chars[i];
1364 done = (four_chars[i] == '\0');
1371 if (exec_file_buffer[0] == '\0')
1373 inferior_ptid = saved_inferior_ptid;
1377 inferior_ptid = saved_inferior_ptid;
1378 return exec_file_buffer;
1382 pre_fork_inferior (void)
1386 status = pipe (startup_semaphore.parent_channel);
1389 warning ("error getting parent pipe for startup semaphore");
1393 status = pipe (startup_semaphore.child_channel);
1396 warning ("error getting child pipe for startup semaphore");
1402 /* Check to see if the given thread is alive.
1404 This is a no-op, as ptrace doesn't support threads, so we just
1408 child_thread_alive (ptid_t ptid)
1413 #endif /* ! GDB_NATIVE_HPUX_11 */