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"
37 extern int hpux_has_forked (int pid
, int *childpid
);
38 extern int hpux_has_vforked (int pid
, int *childpid
);
39 extern int hpux_has_execd (int pid
, char **execd_pathname
);
40 extern int hpux_has_syscall_event (int pid
, enum target_waitkind
*kind
,
43 static CORE_ADDR text_end
;
46 deprecated_hpux_text_end (struct target_ops
*exec_ops
)
48 struct section_table
*p
;
50 /* Set text_end to the highest address of the end of any readonly
52 /* FIXME: The comment above does not match the code. The code
53 checks for sections with are either code *or* readonly. */
54 text_end
= (CORE_ADDR
) 0;
55 for (p
= exec_ops
->to_sections
; p
< exec_ops
->to_sections_end
; p
++)
56 if (bfd_get_section_flags (p
->bfd
, p
->the_bfd_section
)
57 & (SEC_CODE
| SEC_READONLY
))
59 if (text_end
< p
->endaddr
)
60 text_end
= p
->endaddr
;
65 static void fetch_register (int);
68 fetch_inferior_registers (int regno
)
71 for (regno
= 0; regno
< NUM_REGS
; regno
++)
72 fetch_register (regno
);
74 fetch_register (regno
);
77 /* Our own version of the offsetof macro, since we can't assume ANSI C. */
78 #define HPPAH_OFFSETOF(type, member) ((int) (&((type *) 0)->member))
80 /* Store our register values back into the inferior.
81 If REGNO is -1, do this for all registers.
82 Otherwise, REGNO specifies which register (so we can save time). */
85 store_inferior_registers (int regno
)
90 unsigned int offset
= U_REGS_OFFSET
;
95 unsigned int addr
, len
, offset
;
97 if (CANNOT_STORE_REGISTER (regno
))
101 len
= DEPRECATED_REGISTER_RAW_SIZE (regno
);
103 /* Requests for register zero actually want the save_state's
104 ss_flags member. As RM says: "Oh, what a hack!" */
108 addr
= HPPAH_OFFSETOF (save_state_t
, ss_flags
);
109 len
= sizeof (ss
.ss_flags
);
111 /* Note that ss_flags is always an int, no matter what
112 DEPRECATED_REGISTER_RAW_SIZE(0) says. Assuming all HP-UX
113 PA machines are big-endian, put it at the least
114 significant end of the value, and zap the rest of the
116 offset
= DEPRECATED_REGISTER_RAW_SIZE (0) - len
;
119 /* Floating-point registers come from the ss_fpblock area. */
120 else if (regno
>= FP0_REGNUM
)
121 addr
= (HPPAH_OFFSETOF (save_state_t
, ss_fpblock
)
122 + (DEPRECATED_REGISTER_BYTE (regno
) - DEPRECATED_REGISTER_BYTE (FP0_REGNUM
)));
124 /* Wide registers come from the ss_wide area.
125 I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
126 between ss_wide and ss_narrow than to use the raw register size.
127 But checking ss_flags would require an extra ptrace call for
128 every register reference. Bleah. */
130 addr
= (HPPAH_OFFSETOF (save_state_t
, ss_wide
)
131 + DEPRECATED_REGISTER_BYTE (regno
));
133 /* Narrow registers come from the ss_narrow area. Note that
134 ss_narrow starts with gr1, not gr0. */
136 addr
= (HPPAH_OFFSETOF (save_state_t
, ss_narrow
)
137 + (DEPRECATED_REGISTER_BYTE (regno
) - DEPRECATED_REGISTER_BYTE (1)));
139 internal_error (__FILE__
, __LINE__
,
140 "hppah-nat.c (write_register): unexpected register size");
142 #ifdef GDB_TARGET_IS_HPPA_20W
143 /* Unbelieveable. The PC head and tail must be written in 64bit hunks
144 or we will get an error. Worse yet, the oddball ptrace/ttrace
145 layering will not allow us to perform a 64bit register store.
148 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
&& len
== 8)
152 temp
= *(CORE_ADDR
*)&deprecated_registers
[DEPRECATED_REGISTER_BYTE (regno
)];
154 /* Set the priv level (stored in the low two bits of the PC. */
157 ttrace_write_reg_64 (PIDGET (inferior_ptid
), (CORE_ADDR
)addr
,
160 /* If we fail to write the PC, give a true error instead of
164 char *err
= safe_strerror (errno
);
165 char *msg
= alloca (strlen (err
) + 128);
166 sprintf (msg
, "writing `%s' register: %s",
167 REGISTER_NAME (regno
), err
);
168 perror_with_name (msg
);
173 /* Another crock. HPUX complains if you write a nonzero value to
174 the high part of IPSW. What will it take for HP to catch a
175 clue about building sensible interfaces? */
176 if (regno
== IPSW_REGNUM
&& len
== 8)
177 *(int *)&deprecated_registers
[DEPRECATED_REGISTER_BYTE (regno
)] = 0;
180 for (i
= 0; i
< len
; i
+= sizeof (int))
183 call_ptrace (PT_WUREGS
, PIDGET (inferior_ptid
),
184 (PTRACE_ARG3_TYPE
) addr
+ i
,
185 *(int *) &deprecated_registers
[DEPRECATED_REGISTER_BYTE (regno
) + i
]);
188 /* Warning, not error, in case we are attached; sometimes
189 the kernel doesn't let us at the registers. */
190 char *err
= safe_strerror (errno
);
191 char *msg
= alloca (strlen (err
) + 128);
192 sprintf (msg
, "writing `%s' register: %s",
193 REGISTER_NAME (regno
), err
);
194 /* If we fail to write the PC, give a true error instead of
196 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
197 perror_with_name (msg
);
205 for (regno
= 0; regno
< NUM_REGS
; regno
++)
206 store_inferior_registers (regno
);
210 /* Fetch a register's value from the process's U area. */
212 fetch_register (int regno
)
214 char buf
[MAX_REGISTER_SIZE
];
215 unsigned int addr
, len
, offset
;
219 len
= DEPRECATED_REGISTER_RAW_SIZE (regno
);
221 /* Requests for register zero actually want the save_state's
222 ss_flags member. As RM says: "Oh, what a hack!" */
226 addr
= HPPAH_OFFSETOF (save_state_t
, ss_flags
);
227 len
= sizeof (ss
.ss_flags
);
229 /* Note that ss_flags is always an int, no matter what
230 DEPRECATED_REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA
231 machines are big-endian, put it at the least significant end
232 of the value, and zap the rest of the buffer. */
233 offset
= DEPRECATED_REGISTER_RAW_SIZE (0) - len
;
234 memset (buf
, 0, sizeof (buf
));
237 /* Floating-point registers come from the ss_fpblock area. */
238 else if (regno
>= FP0_REGNUM
)
239 addr
= (HPPAH_OFFSETOF (save_state_t
, ss_fpblock
)
240 + (DEPRECATED_REGISTER_BYTE (regno
) - DEPRECATED_REGISTER_BYTE (FP0_REGNUM
)));
242 /* Wide registers come from the ss_wide area.
243 I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
244 between ss_wide and ss_narrow than to use the raw register size.
245 But checking ss_flags would require an extra ptrace call for
246 every register reference. Bleah. */
248 addr
= (HPPAH_OFFSETOF (save_state_t
, ss_wide
)
249 + DEPRECATED_REGISTER_BYTE (regno
));
251 /* Narrow registers come from the ss_narrow area. Note that
252 ss_narrow starts with gr1, not gr0. */
254 addr
= (HPPAH_OFFSETOF (save_state_t
, ss_narrow
)
255 + (DEPRECATED_REGISTER_BYTE (regno
) - DEPRECATED_REGISTER_BYTE (1)));
258 internal_error (__FILE__
, __LINE__
,
259 "hppa-nat.c (fetch_register): unexpected register size");
261 for (i
= 0; i
< len
; i
+= sizeof (int))
264 /* Copy an int from the U area to buf. Fill the least
265 significant end if len != raw_size. */
266 * (int *) &buf
[offset
+ i
] =
267 call_ptrace (PT_RUREGS
, PIDGET (inferior_ptid
),
268 (PTRACE_ARG3_TYPE
) addr
+ i
, 0);
271 /* Warning, not error, in case we are attached; sometimes
272 the kernel doesn't let us at the registers. */
273 char *err
= safe_strerror (errno
);
274 char *msg
= alloca (strlen (err
) + 128);
275 sprintf (msg
, "reading `%s' register: %s",
276 REGISTER_NAME (regno
), err
);
282 /* If we're reading an address from the instruction address queue,
283 mask out the bottom two bits --- they contain the privilege
285 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
286 buf
[len
- 1] &= ~0x3;
288 supply_register (regno
, buf
);
292 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
293 to debugger memory starting at MYADDR. Copy to inferior if
296 Returns the length copied, which is either the LEN argument or zero.
297 This xfer function does not do partial moves, since child_ops
298 doesn't allow memory operations to cross below us in the target stack
299 anyway. TARGET is ignored. */
302 child_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
303 struct mem_attrib
*mem
,
304 struct target_ops
*target
)
307 /* Round starting address down to longword boundary. */
308 CORE_ADDR addr
= memaddr
& - (CORE_ADDR
)(sizeof (int));
309 /* Round ending address up; get number of longwords that makes. */
311 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
313 /* Allocate buffer of that many longwords.
314 Note -- do not use alloca to allocate this buffer since there is no
315 guarantee of when the buffer will actually be deallocated.
317 This routine can be called over and over with the same call chain;
318 this (in effect) would pile up all those alloca requests until a call
319 to alloca was made from a point higher than this routine in the
321 int *buffer
= (int *) xmalloc (count
* sizeof (int));
325 /* Fill start and end extra bytes of buffer with existing memory data. */
326 if (addr
!= memaddr
|| len
< (int) sizeof (int))
328 /* Need part of initial word -- fetch it. */
329 buffer
[0] = call_ptrace (addr
< text_end
? PT_RIUSER
: PT_RDUSER
,
330 PIDGET (inferior_ptid
),
331 (PTRACE_ARG3_TYPE
) addr
, 0);
334 if (count
> 1) /* FIXME, avoid if even boundary */
337 = call_ptrace (addr
< text_end
? PT_RIUSER
: PT_RDUSER
,
338 PIDGET (inferior_ptid
),
339 (PTRACE_ARG3_TYPE
) (addr
340 + (count
- 1) * sizeof (int)),
344 /* Copy data to be written over corresponding part of buffer */
345 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
347 /* Write the entire buffer. */
348 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
352 /* The HP-UX kernel crashes if you use PT_WDUSER to write into the
353 text segment. FIXME -- does it work to write into the data
354 segment using WIUSER, or do these idiots really expect us to
355 figure out which segment the address is in, so we can use a
356 separate system call for it??! */
358 pt_request
= (addr
< text_end
) ? PT_WIUSER
: PT_WDUSER
;
359 pt_status
= call_ptrace (pt_request
,
360 PIDGET (inferior_ptid
),
361 (PTRACE_ARG3_TYPE
) addr
,
364 /* Did we fail? Might we've guessed wrong about which
365 segment this address resides in? Try the other request,
366 and see if that works... */
367 if ((pt_status
== -1) && errno
)
370 pt_request
= (pt_request
== PT_WIUSER
) ? PT_WDUSER
: PT_WIUSER
;
371 pt_status
= call_ptrace (pt_request
,
372 PIDGET (inferior_ptid
),
373 (PTRACE_ARG3_TYPE
) addr
,
376 /* No, we still fail. Okay, time to punt. */
377 if ((pt_status
== -1) && errno
)
387 /* Read all the longwords */
388 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
391 buffer
[i
] = call_ptrace (addr
< text_end
? PT_RIUSER
: PT_RDUSER
,
392 PIDGET (inferior_ptid
),
393 (PTRACE_ARG3_TYPE
) addr
, 0);
402 /* Copy appropriate bytes out of the buffer. */
403 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
409 char *saved_child_execd_pathname
= NULL
;
417 } saved_vfork_state
= STATE_NONE
;
420 child_follow_fork (int follow_child
)
423 struct target_waitstatus last_status
;
425 int parent_pid
, child_pid
;
427 get_last_target_status (&last_ptid
, &last_status
);
428 has_vforked
= (last_status
.kind
== TARGET_WAITKIND_VFORKED
);
429 parent_pid
= ptid_get_pid (last_ptid
);
430 child_pid
= last_status
.value
.related_pid
;
432 /* At this point, if we are vforking, breakpoints were already
433 detached from the child in child_wait; and the child has already
434 called execve(). If we are forking, both the parent and child
435 have breakpoints inserted. */
441 detach_breakpoints (child_pid
);
442 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
443 SOLIB_REMOVE_INFERIOR_HOOK (child_pid
);
447 /* Detach from the child. */
448 printf_unfiltered ("Detaching after fork from %s\n",
449 target_pid_to_str (pid_to_ptid (child_pid
)));
450 hppa_require_detach (child_pid
, 0);
452 /* The parent and child of a vfork share the same address space.
453 Also, on some targets the order in which vfork and exec events
454 are received for parent in child requires some delicate handling
457 For instance, on ptrace-based HPUX we receive the child's vfork
458 event first, at which time the parent has been suspended by the
459 OS and is essentially untouchable until the child's exit or second
460 exec event arrives. At that time, the parent's vfork event is
461 delivered to us, and that's when we see and decide how to follow
462 the vfork. But to get to that point, we must continue the child
463 until it execs or exits. To do that smoothly, all breakpoints
464 must be removed from the child, in case there are any set between
465 the vfork() and exec() calls. But removing them from the child
466 also removes them from the parent, due to the shared-address-space
467 nature of a vfork'd parent and child. On HPUX, therefore, we must
468 take care to restore the bp's to the parent before we continue it.
469 Else, it's likely that we may not stop in the expected place. (The
470 worst scenario is when the user tries to step over a vfork() call;
471 the step-resume bp must be restored for the step to properly stop
472 in the parent after the call completes!)
474 Sequence of events, as reported to gdb from HPUX:
476 Parent Child Action for gdb to take
477 -------------------------------------------------------
478 1 VFORK Continue child
483 Now that the child has safely exec'd or exited, we must restore
484 the parent's breakpoints before we continue it. Else, we may
485 cause it run past expected stopping points. */
488 reattach_breakpoints (parent_pid
);
492 /* Needed to keep the breakpoint lists in sync. */
494 detach_breakpoints (child_pid
);
496 /* Before detaching from the parent, remove all breakpoints from it. */
497 remove_breakpoints ();
499 /* Also reset the solib inferior hook from the parent. */
500 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
501 SOLIB_REMOVE_INFERIOR_HOOK (PIDGET (inferior_ptid
));
504 /* Detach from the parent. */
505 target_detach (NULL
, 1);
507 /* Attach to the child. */
508 printf_unfiltered ("Attaching after fork to %s\n",
509 target_pid_to_str (pid_to_ptid (child_pid
)));
510 hppa_require_attach (child_pid
);
511 inferior_ptid
= pid_to_ptid (child_pid
);
513 /* If we vforked, then we've also execed by now. The exec will be
514 reported momentarily. follow_exec () will handle breakpoints, so
515 we don't have to.. */
517 follow_inferior_reset_breakpoints ();
522 /* If we followed the parent, don't try to follow the child's exec. */
523 if (saved_vfork_state
!= STATE_GOT_PARENT
524 && saved_vfork_state
!= STATE_FAKE_EXEC
)
525 fprintf_unfiltered (gdb_stdout
,
526 "hppa: post follow vfork: confused state\n");
528 if (! follow_child
|| saved_vfork_state
== STATE_GOT_PARENT
)
529 saved_vfork_state
= STATE_NONE
;
536 /* Format a process id, given PID. Be sure to terminate
537 this with a null--it's going to be printed via a "%s". */
539 child_pid_to_str (ptid_t ptid
)
541 /* Static because address returned */
543 pid_t pid
= PIDGET (ptid
);
545 /* Extra NUL for paranoia's sake */
546 sprintf (buf
, "process %d%c", pid
, '\0');
551 /* Format a thread id, given TID. Be sure to terminate
552 this with a null--it's going to be printed via a "%s".
554 Note: This is a core-gdb tid, not the actual system tid.
555 See infttrace.c for details. */
557 hppa_tid_to_str (ptid_t ptid
)
559 /* Static because address returned */
561 /* This seems strange, but when I did the ptid conversion, it looked
562 as though a pid was always being passed. - Kevin Buettner */
563 pid_t tid
= PIDGET (ptid
);
565 /* Extra NULLs for paranoia's sake */
566 sprintf (buf
, "system thread %d%c", tid
, '\0');
572 /* Enable HACK for ttrace work. In
573 * infttrace.c/require_notification_of_events,
574 * this is set to 0 so that the loop in child_wait
577 int not_same_real_pid
= 1;
580 /* Wait for child to do something. Return pid of child, or -1 in case
581 of error; store status through argument pointer OURSTATUS. */
584 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
588 char *execd_pathname
= NULL
;
592 enum target_waitkind kind
;
595 if (saved_vfork_state
== STATE_FAKE_EXEC
)
597 saved_vfork_state
= STATE_NONE
;
598 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
599 ourstatus
->value
.execd_pathname
= saved_child_execd_pathname
;
600 return inferior_ptid
;
605 set_sigint_trap (); /* Causes SIGINT to be passed on to the
609 pid
= ptrace_wait (inferior_ptid
, &status
);
615 clear_sigint_trap ();
619 if (save_errno
== EINTR
)
622 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
623 safe_strerror (save_errno
));
625 /* Claim it exited with unknown signal. */
626 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
627 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
628 return pid_to_ptid (-1);
633 if (target_has_exited (pid
, status
, &exit_status
))
635 /* ??rehrauer: For now, ignore this. */
639 if (!target_thread_alive (pid_to_ptid (pid
)))
641 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
642 return pid_to_ptid (pid
);
645 if (hpux_has_forked (pid
, &related_pid
))
647 /* Ignore the parent's fork event. */
648 if (pid
== PIDGET (inferior_ptid
))
650 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
651 return inferior_ptid
;
654 /* If this is the child's fork event, report that the
655 process has forked. */
656 if (related_pid
== PIDGET (inferior_ptid
))
658 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
659 ourstatus
->value
.related_pid
= pid
;
660 return inferior_ptid
;
664 if (hpux_has_vforked (pid
, &related_pid
))
666 if (pid
== PIDGET (inferior_ptid
))
668 if (saved_vfork_state
== STATE_GOT_CHILD
)
669 saved_vfork_state
= STATE_GOT_PARENT
;
670 else if (saved_vfork_state
== STATE_GOT_EXEC
)
671 saved_vfork_state
= STATE_FAKE_EXEC
;
673 fprintf_unfiltered (gdb_stdout
,
674 "hppah: parent vfork: confused\n");
676 else if (related_pid
== PIDGET (inferior_ptid
))
678 if (saved_vfork_state
== STATE_NONE
)
679 saved_vfork_state
= STATE_GOT_CHILD
;
681 fprintf_unfiltered (gdb_stdout
,
682 "hppah: child vfork: confused\n");
685 fprintf_unfiltered (gdb_stdout
,
686 "hppah: unknown vfork: confused\n");
688 if (saved_vfork_state
== STATE_GOT_CHILD
)
690 child_post_startup_inferior (pid_to_ptid (pid
));
691 detach_breakpoints (pid
);
692 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
693 SOLIB_REMOVE_INFERIOR_HOOK (pid
);
695 child_resume (pid_to_ptid (pid
), 0, TARGET_SIGNAL_0
);
696 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
697 return pid_to_ptid (related_pid
);
699 else if (saved_vfork_state
== STATE_FAKE_EXEC
)
701 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
702 ourstatus
->value
.related_pid
= related_pid
;
703 return pid_to_ptid (pid
);
707 /* We saw the parent's vfork, but we haven't seen the exec yet.
708 Wait for it, for simplicity's sake. It should be pending. */
709 saved_vfork_pid
= related_pid
;
710 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
711 return pid_to_ptid (pid
);
715 if (hpux_has_execd (pid
, &execd_pathname
))
717 /* On HP-UX, events associated with a vforking inferior come in
718 threes: a vfork event for the child (always first), followed
719 a vfork event for the parent and an exec event for the child.
720 The latter two can come in either order. Make sure we get
722 if (saved_vfork_state
!= STATE_NONE
)
724 if (saved_vfork_state
== STATE_GOT_CHILD
)
726 saved_vfork_state
= STATE_GOT_EXEC
;
727 /* On HP/UX with ptrace, the child must be resumed before
728 the parent vfork event is delivered. A single-step
730 if (RESUME_EXECD_VFORKING_CHILD_TO_GET_PARENT_VFORK ())
731 target_resume (pid_to_ptid (pid
), 1, TARGET_SIGNAL_0
);
732 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
734 else if (saved_vfork_state
== STATE_GOT_PARENT
)
736 saved_vfork_state
= STATE_FAKE_EXEC
;
737 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
738 ourstatus
->value
.related_pid
= saved_vfork_pid
;
741 fprintf_unfiltered (gdb_stdout
,
742 "hppa: exec: unexpected state\n");
744 saved_child_execd_pathname
= execd_pathname
;
746 return inferior_ptid
;
749 /* Are we ignoring initial exec events? (This is likely because
750 we're in the process of starting up the inferior, and another
751 (older) mechanism handles those.) If so, we'll report this
752 as a regular stop, not an exec.
754 if (inferior_ignoring_startup_exec_events
)
756 inferior_ignoring_startup_exec_events
--;
760 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
761 ourstatus
->value
.execd_pathname
= execd_pathname
;
762 return pid_to_ptid (pid
);
766 /* All we must do with these is communicate their occurrence
767 to wait_for_inferior...
769 if (hpux_has_syscall_event (pid
, &kind
, &syscall_id
))
771 ourstatus
->kind
= kind
;
772 ourstatus
->value
.syscall_id
= syscall_id
;
773 return pid_to_ptid (pid
);
776 /*## } while (pid != PIDGET (inferior_ptid)); ## *//* Some other child died or stopped */
777 /* hack for thread testing */
779 while ((pid
!= PIDGET (inferior_ptid
)) && not_same_real_pid
);
782 store_waitstatus (ourstatus
, status
);
783 return pid_to_ptid (pid
);
786 #if !defined (GDB_NATIVE_HPUX_11)
788 /* The following code is a substitute for the infttrace.c versions used
789 with ttrace() in HPUX 11. */
791 /* This value is an arbitrary integer. */
792 #define PT_VERSION 123456
794 /* This semaphore is used to coordinate the child and parent processes
795 after a fork(), and before an exec() by the child. See
796 parent_attach_all for details. */
800 int parent_channel
[2]; /* Parent "talks" to [1], child "listens" to [0] */
801 int child_channel
[2]; /* Child "talks" to [1], parent "listens" to [0] */
806 #define SEM_LISTEN (0)
808 static startup_semaphore_t startup_semaphore
;
811 /* This function causes the caller's process to be traced by its
812 parent. This is intended to be called after GDB forks itself,
813 and before the child execs the target.
815 Note that HP-UX ptrace is rather funky in how this is done.
816 If the parent wants to get the initial exec event of a child,
817 it must set the ptrace event mask of the child to include execs.
818 (The child cannot do this itself.) This must be done after the
819 child is forked, but before it execs.
821 To coordinate the parent and child, we implement a semaphore using
822 pipes. After SETTRC'ing itself, the child tells the parent that
823 it is now traceable by the parent, and waits for the parent's
824 acknowledgement. The parent can then set the child's event mask,
825 and notify the child that it can now exec.
827 (The acknowledgement by parent happens as a result of a call to
828 child_acknowledge_created_inferior.) */
831 parent_attach_all (int pid
, PTRACE_ARG3_TYPE addr
, int data
)
835 /* We need a memory home for a constant. */
836 int tc_magic_child
= PT_VERSION
;
837 int tc_magic_parent
= 0;
839 /* The remainder of this function is only useful for HPUX 10.0 and
840 later, as it depends upon the ability to request notification
841 of specific kinds of events by the kernel. */
842 #if defined(PT_SET_EVENT_MASK)
844 /* Notify the parent that we're potentially ready to exec(). */
845 write (startup_semaphore
.child_channel
[SEM_TALK
],
847 sizeof (tc_magic_child
));
849 /* Wait for acknowledgement from the parent. */
850 read (startup_semaphore
.parent_channel
[SEM_LISTEN
],
852 sizeof (tc_magic_parent
));
853 if (tc_magic_child
!= tc_magic_parent
)
854 warning ("mismatched semaphore magic");
856 /* Discard our copy of the semaphore. */
857 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
858 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
859 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
860 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
868 hppa_require_attach (int pid
)
873 unsigned int regs_offset
;
875 /* Are we already attached? There appears to be no explicit way to
876 answer this via ptrace, so we try something which should be
877 innocuous if we are attached. If that fails, then we assume
878 we're not attached, and so attempt to make it so. */
881 regs_offset
= U_REGS_OFFSET
;
882 pc_addr
= register_addr (PC_REGNUM
, regs_offset
);
883 pc
= call_ptrace (PT_READ_U
, pid
, (PTRACE_ARG3_TYPE
) pc_addr
, 0);
888 pt_status
= call_ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
893 /* Now we really are attached. */
901 hppa_require_detach (int pid
, int signal
)
904 call_ptrace (PT_DETACH
, pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
905 errno
= 0; /* Ignore any errors. */
909 /* Since ptrace doesn't support memory page-protection events, which
910 are used to implement "hardware" watchpoints on HP-UX, these are
911 dummy versions, which perform no useful work. */
914 hppa_enable_page_protection_events (int pid
)
919 hppa_disable_page_protection_events (int pid
)
924 hppa_insert_hw_watchpoint (int pid
, CORE_ADDR start
, LONGEST len
, int type
)
926 error ("Hardware watchpoints not implemented on this platform.");
930 hppa_remove_hw_watchpoint (int pid
, CORE_ADDR start
, LONGEST len
, int type
)
932 error ("Hardware watchpoints not implemented on this platform.");
936 hppa_can_use_hw_watchpoint (int type
, int cnt
, int ot
)
942 hppa_range_profitable_for_hw_watchpoint (int pid
, CORE_ADDR start
, LONGEST len
)
944 error ("Hardware watchpoints not implemented on this platform.");
948 hppa_pid_or_tid_to_str (ptid_t id
)
950 /* In the ptrace world, there are only processes. */
951 return child_pid_to_str (id
);
955 hppa_ensure_vforking_parent_remains_stopped (int pid
)
957 /* This assumes that the vforked parent is presently stopped, and
958 that the vforked child has just delivered its first exec event.
959 Calling kill() this way will cause the SIGTRAP to be delivered as
960 soon as the parent is resumed, which happens as soon as the
961 vforked child is resumed. See wait_for_inferior for the use of
967 hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
969 return 1; /* Yes, the child must be resumed. */
973 require_notification_of_events (int pid
)
975 #if defined(PT_SET_EVENT_MASK)
977 ptrace_event_t ptrace_events
;
981 /* Instruct the kernel as to the set of events we wish to be
982 informed of. (This support does not exist before HPUX 10.0.
983 We'll assume if PT_SET_EVENT_MASK has not been defined by
984 <sys/ptrace.h>, then we're being built on pre-10.0.) */
985 memset (&ptrace_events
, 0, sizeof (ptrace_events
));
987 /* Note: By default, all signals are visible to us. If we wish
988 the kernel to keep certain signals hidden from us, we do it
989 by calling sigdelset (ptrace_events.pe_signals, signal) for
990 each such signal here, before doing PT_SET_EVENT_MASK. */
991 /* RM: The above comment is no longer true. We start with ignoring
992 all signals, and then add the ones we are interested in. We could
993 do it the other way: start by looking at all signals and then
994 deleting the ones that we aren't interested in, except that
995 multiple gdb signals may be mapped to the same host signal
996 (eg. TARGET_SIGNAL_IO and TARGET_SIGNAL_POLL both get mapped to
997 signal 22 on HPUX 10.20) We want to be notified if we are
998 interested in either signal. */
999 sigfillset (&ptrace_events
.pe_signals
);
1001 /* RM: Let's not bother with signals we don't care about */
1002 nsigs
= (int) TARGET_SIGNAL_LAST
;
1003 for (signum
= nsigs
; signum
> 0; signum
--)
1005 if ((signal_stop_state (signum
)) ||
1006 (signal_print_state (signum
)) ||
1007 (!signal_pass_state (signum
)))
1009 if (target_signal_to_host_p (signum
))
1010 sigdelset (&ptrace_events
.pe_signals
,
1011 target_signal_to_host (signum
));
1015 ptrace_events
.pe_set_event
= 0;
1017 ptrace_events
.pe_set_event
|= PTRACE_SIGNAL
;
1018 ptrace_events
.pe_set_event
|= PTRACE_EXEC
;
1019 ptrace_events
.pe_set_event
|= PTRACE_FORK
;
1020 ptrace_events
.pe_set_event
|= PTRACE_VFORK
;
1021 /* ??rehrauer: Add this one when we're prepared to catch it...
1022 ptrace_events.pe_set_event |= PTRACE_EXIT;
1026 pt_status
= call_ptrace (PT_SET_EVENT_MASK
,
1028 (PTRACE_ARG3_TYPE
) & ptrace_events
,
1029 sizeof (ptrace_events
));
1031 perror_with_name ("ptrace");
1038 require_notification_of_exec_events (int pid
)
1040 #if defined(PT_SET_EVENT_MASK)
1042 ptrace_event_t ptrace_events
;
1044 /* Instruct the kernel as to the set of events we wish to be
1045 informed of. (This support does not exist before HPUX 10.0.
1046 We'll assume if PT_SET_EVENT_MASK has not been defined by
1047 <sys/ptrace.h>, then we're being built on pre-10.0.) */
1048 memset (&ptrace_events
, 0, sizeof (ptrace_events
));
1050 /* Note: By default, all signals are visible to us. If we wish
1051 the kernel to keep certain signals hidden from us, we do it
1052 by calling sigdelset (ptrace_events.pe_signals, signal) for
1053 each such signal here, before doing PT_SET_EVENT_MASK. */
1054 sigemptyset (&ptrace_events
.pe_signals
);
1056 ptrace_events
.pe_set_event
= 0;
1058 ptrace_events
.pe_set_event
|= PTRACE_EXEC
;
1059 /* ??rehrauer: Add this one when we're prepared to catch it...
1060 ptrace_events.pe_set_event |= PTRACE_EXIT;
1064 pt_status
= call_ptrace (PT_SET_EVENT_MASK
,
1066 (PTRACE_ARG3_TYPE
) & ptrace_events
,
1067 sizeof (ptrace_events
));
1069 perror_with_name ("ptrace");
1075 /* This function is called by the parent process, with pid being the
1076 ID of the child process, after the debugger has forked. */
1079 child_acknowledge_created_inferior (int pid
)
1081 /* We need a memory home for a constant. */
1082 int tc_magic_parent
= PT_VERSION
;
1083 int tc_magic_child
= 0;
1085 /* The remainder of this function is only useful for HPUX 10.0 and
1086 later, as it depends upon the ability to request notification
1087 of specific kinds of events by the kernel. */
1088 #if defined(PT_SET_EVENT_MASK)
1089 /* Wait for the child to tell us that it has forked. */
1090 read (startup_semaphore
.child_channel
[SEM_LISTEN
],
1092 sizeof (tc_magic_child
));
1094 /* Notify the child that it can exec.
1096 In the infttrace.c variant of this function, we set the child's
1097 event mask after the fork but before the exec. In the ptrace
1098 world, it seems we can't set the event mask until after the exec. */
1099 write (startup_semaphore
.parent_channel
[SEM_TALK
],
1101 sizeof (tc_magic_parent
));
1103 /* We'd better pause a bit before trying to set the event mask,
1104 though, to ensure that the exec has happened. We don't want to
1105 wait() on the child, because that'll screw up the upper layers
1106 of gdb's execution control that expect to see the exec event.
1108 After an exec, the child is no longer executing gdb code. Hence,
1109 we can't have yet another synchronization via the pipes. We'll
1110 just sleep for a second, and hope that's enough delay... */
1113 /* Instruct the kernel as to the set of events we wish to be
1115 require_notification_of_exec_events (pid
);
1117 /* Discard our copy of the semaphore. */
1118 (void) close (startup_semaphore
.parent_channel
[SEM_LISTEN
]);
1119 (void) close (startup_semaphore
.parent_channel
[SEM_TALK
]);
1120 (void) close (startup_semaphore
.child_channel
[SEM_LISTEN
]);
1121 (void) close (startup_semaphore
.child_channel
[SEM_TALK
]);
1126 child_post_startup_inferior (ptid_t ptid
)
1128 require_notification_of_events (PIDGET (ptid
));
1132 child_post_attach (int pid
)
1134 require_notification_of_events (pid
);
1138 child_insert_fork_catchpoint (int pid
)
1140 /* This request is only available on HPUX 10.0 and later. */
1141 #if !defined(PT_SET_EVENT_MASK)
1142 error ("Unable to catch forks prior to HPUX 10.0");
1144 /* Enable reporting of fork events from the kernel. */
1145 /* ??rehrauer: For the moment, we're always enabling these events,
1146 and just ignoring them if there's no catchpoint to catch them. */
1152 child_remove_fork_catchpoint (int pid
)
1154 /* This request is only available on HPUX 10.0 and later. */
1155 #if !defined(PT_SET_EVENT_MASK)
1156 error ("Unable to catch forks prior to HPUX 10.0");
1158 /* Disable reporting of fork events from the kernel. */
1159 /* ??rehrauer: For the moment, we're always enabling these events,
1160 and just ignoring them if there's no catchpoint to catch them. */
1166 child_insert_vfork_catchpoint (int pid
)
1168 /* This request is only available on HPUX 10.0 and later. */
1169 #if !defined(PT_SET_EVENT_MASK)
1170 error ("Unable to catch vforks prior to HPUX 10.0");
1172 /* Enable reporting of vfork events from the kernel. */
1173 /* ??rehrauer: For the moment, we're always enabling these events,
1174 and just ignoring them if there's no catchpoint to catch them. */
1180 child_remove_vfork_catchpoint (int pid
)
1182 /* This request is only available on HPUX 10.0 and later. */
1183 #if !defined(PT_SET_EVENT_MASK)
1184 error ("Unable to catch vforks prior to HPUX 10.0");
1186 /* Disable reporting of vfork events from the kernel. */
1187 /* ??rehrauer: For the moment, we're always enabling these events,
1188 and just ignoring them if there's no catchpoint to catch them. */
1194 hpux_has_forked (int pid
, int *childpid
)
1196 /* This request is only available on HPUX 10.0 and later. */
1197 #if !defined(PT_GET_PROCESS_STATE)
1202 ptrace_state_t ptrace_state
;
1205 pt_status
= call_ptrace (PT_GET_PROCESS_STATE
,
1207 (PTRACE_ARG3_TYPE
) & ptrace_state
,
1208 sizeof (ptrace_state
));
1210 perror_with_name ("ptrace");
1214 if (ptrace_state
.pe_report_event
& PTRACE_FORK
)
1216 *childpid
= ptrace_state
.pe_other_pid
;
1225 hpux_has_vforked (int pid
, int *childpid
)
1227 /* This request is only available on HPUX 10.0 and later. */
1228 #if !defined(PT_GET_PROCESS_STATE)
1234 ptrace_state_t ptrace_state
;
1237 pt_status
= call_ptrace (PT_GET_PROCESS_STATE
,
1239 (PTRACE_ARG3_TYPE
) & ptrace_state
,
1240 sizeof (ptrace_state
));
1242 perror_with_name ("ptrace");
1246 if (ptrace_state
.pe_report_event
& PTRACE_VFORK
)
1248 *childpid
= ptrace_state
.pe_other_pid
;
1257 child_insert_exec_catchpoint (int pid
)
1259 /* This request is only available on HPUX 10.0 and later. */
1260 #if !defined(PT_SET_EVENT_MASK)
1261 error ("Unable to catch execs prior to HPUX 10.0");
1264 /* Enable reporting of exec events from the kernel. */
1265 /* ??rehrauer: For the moment, we're always enabling these events,
1266 and just ignoring them if there's no catchpoint to catch them. */
1272 child_remove_exec_catchpoint (int pid
)
1274 /* This request is only available on HPUX 10.0 and later. */
1275 #if !defined(PT_SET_EVENT_MASK)
1276 error ("Unable to catch execs prior to HPUX 10.0");
1279 /* Disable reporting of exec events from the kernel. */
1280 /* ??rehrauer: For the moment, we're always enabling these events,
1281 and just ignoring them if there's no catchpoint to catch them. */
1287 hpux_has_execd (int pid
, char **execd_pathname
)
1289 /* This request is only available on HPUX 10.0 and later. */
1290 #if !defined(PT_GET_PROCESS_STATE)
1291 *execd_pathname
= NULL
;
1296 ptrace_state_t ptrace_state
;
1299 pt_status
= call_ptrace (PT_GET_PROCESS_STATE
,
1301 (PTRACE_ARG3_TYPE
) & ptrace_state
,
1302 sizeof (ptrace_state
));
1304 perror_with_name ("ptrace");
1308 if (ptrace_state
.pe_report_event
& PTRACE_EXEC
)
1310 char *exec_file
= target_pid_to_exec_file (pid
);
1311 *execd_pathname
= savestring (exec_file
, strlen (exec_file
));
1320 child_reported_exec_events_per_exec_call (void)
1322 return 2; /* ptrace reports the event twice per call. */
1326 hpux_has_syscall_event (int pid
, enum target_waitkind
*kind
, int *syscall_id
)
1328 /* This request is only available on HPUX 10.30 and later, via
1329 the ttrace interface. */
1331 *kind
= TARGET_WAITKIND_SPURIOUS
;
1337 child_pid_to_exec_file (int pid
)
1339 static char exec_file_buffer
[1024];
1341 CORE_ADDR top_of_stack
;
1345 ptid_t saved_inferior_ptid
;
1348 #ifdef PT_GET_PROCESS_PATHNAME
1349 /* As of 10.x HP-UX, there's an explicit request to get the pathname. */
1350 pt_status
= call_ptrace (PT_GET_PROCESS_PATHNAME
,
1352 (PTRACE_ARG3_TYPE
) exec_file_buffer
,
1353 sizeof (exec_file_buffer
) - 1);
1355 return exec_file_buffer
;
1358 /* It appears that this request is broken prior to 10.30.
1359 If it fails, try a really, truly amazingly gross hack
1360 that DDE uses, of pawing through the process' data
1361 segment to find the pathname. */
1363 top_of_stack
= 0x7b03a000;
1367 /* On the chance that pid != inferior_ptid, set inferior_ptid
1368 to pid, so that (grrrr!) implicit uses of inferior_ptid get
1371 saved_inferior_ptid
= inferior_ptid
;
1372 inferior_ptid
= pid_to_ptid (pid
);
1374 /* Try to grab a null-terminated string. */
1377 if (target_read_memory (top_of_stack
, four_chars
, 4) != 0)
1379 inferior_ptid
= saved_inferior_ptid
;
1382 for (i
= 0; i
< 4; i
++)
1384 exec_file_buffer
[name_index
++] = four_chars
[i
];
1385 done
= (four_chars
[i
] == '\0');
1392 if (exec_file_buffer
[0] == '\0')
1394 inferior_ptid
= saved_inferior_ptid
;
1398 inferior_ptid
= saved_inferior_ptid
;
1399 return exec_file_buffer
;
1403 pre_fork_inferior (void)
1407 status
= pipe (startup_semaphore
.parent_channel
);
1410 warning ("error getting parent pipe for startup semaphore");
1414 status
= pipe (startup_semaphore
.child_channel
);
1417 warning ("error getting child pipe for startup semaphore");
1423 /* Check to see if the given thread is alive.
1425 This is a no-op, as ptrace doesn't support threads, so we just
1429 child_thread_alive (ptid_t ptid
)
1434 #endif /* ! GDB_NATIVE_HPUX_11 */