2003-07-22 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / hppah-nat.c
CommitLineData
c906108c 1/* Native support code for HPUX PA-RISC.
b6ba6518
KB
2 Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3 1998, 1999, 2000, 2001
c906108c
SS
4 Free Software Foundation, Inc.
5
6 Contributed by the Center for Software Science at the
7 University of Utah (pa-gdb-bugs@cs.utah.edu).
8
c5aa993b 9 This file is part of GDB.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
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.
c906108c 20
c5aa993b
JM
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. */
c906108c
SS
25
26
27#include "defs.h"
28#include "inferior.h"
29#include "target.h"
30#include <sys/ptrace.h>
31#include "gdbcore.h"
a526d713 32#include "gdb_wait.h"
4e052eda 33#include "regcache.h"
65e82032 34#include "gdb_string.h"
c906108c
SS
35#include <signal.h>
36
37extern CORE_ADDR text_end;
38
47932f85
DJ
39extern int hpux_has_forked (int pid, int *childpid);
40extern int hpux_has_vforked (int pid, int *childpid);
41extern int hpux_has_execd (int pid, char **execd_pathname);
42extern int hpux_has_syscall_event (int pid, enum target_waitkind *kind,
43 int *syscall_id);
44
a14ed312 45static void fetch_register (int);
c906108c
SS
46
47void
fba45db2 48fetch_inferior_registers (int regno)
c906108c
SS
49{
50 if (regno == -1)
51 for (regno = 0; regno < NUM_REGS; regno++)
52 fetch_register (regno);
53 else
54 fetch_register (regno);
55}
56
7be570e7
JM
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))
59
c906108c
SS
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). */
63
64void
fba45db2 65store_inferior_registers (int regno)
c906108c
SS
66{
67 register unsigned int regaddr;
68 char buf[80];
c906108c
SS
69 register int i;
70 unsigned int offset = U_REGS_OFFSET;
71 int scratch;
72
73 if (regno >= 0)
74 {
7be570e7
JM
75 unsigned int addr, len, offset;
76
c906108c
SS
77 if (CANNOT_STORE_REGISTER (regno))
78 return;
7be570e7
JM
79
80 offset = 0;
81 len = REGISTER_RAW_SIZE (regno);
82
83 /* Requests for register zero actually want the save_state's
84 ss_flags member. As RM says: "Oh, what a hack!" */
85 if (regno == 0)
b83266a0 86 {
7be570e7
JM
87 save_state_t ss;
88 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
89 len = sizeof (ss.ss_flags);
90
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;
96 }
97
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)));
102
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. */
108 else if (len == 8)
109 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
110 + REGISTER_BYTE (regno));
111
112 /* Narrow registers come from the ss_narrow area. Note that
113 ss_narrow starts with gr1, not gr0. */
114 else if (len == 4)
115 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
116 + (REGISTER_BYTE (regno) - REGISTER_BYTE (1)));
117 else
8e65ff28
AC
118 internal_error (__FILE__, __LINE__,
119 "hppah-nat.c (write_register): unexpected register size");
7be570e7
JM
120
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.
125
126 What a crock. */
127 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM && len == 8)
128 {
129 CORE_ADDR temp;
130
524d7c18 131 temp = *(CORE_ADDR *)&deprecated_registers[REGISTER_BYTE (regno)];
7be570e7
JM
132
133 /* Set the priv level (stored in the low two bits of the PC. */
134 temp |= 0x3;
135
39f77062
KB
136 ttrace_write_reg_64 (PIDGET (inferior_ptid), (CORE_ADDR)addr,
137 (CORE_ADDR)&temp);
7be570e7
JM
138
139 /* If we fail to write the PC, give a true error instead of
140 just a warning. */
b83266a0
SS
141 if (errno != 0)
142 {
7be570e7
JM
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);
b83266a0 148 }
7be570e7 149 return;
b83266a0 150 }
53a5351d
JM
151
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)
524d7c18 156 *(int *)&deprecated_registers[REGISTER_BYTE (regno)] = 0;
7be570e7
JM
157#endif
158
159 for (i = 0; i < len; i += sizeof (int))
160 {
161 errno = 0;
39f77062
KB
162 call_ptrace (PT_WUREGS, PIDGET (inferior_ptid),
163 (PTRACE_ARG3_TYPE) addr + i,
524d7c18 164 *(int *) &deprecated_registers[REGISTER_BYTE (regno) + i]);
7be570e7
JM
165 if (errno != 0)
166 {
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);
53a5351d 171 sprintf (msg, "writing `%s' register: %s",
7be570e7
JM
172 REGISTER_NAME (regno), err);
173 /* If we fail to write the PC, give a true error instead of
174 just a warning. */
175 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
176 perror_with_name (msg);
177 else
c906108c 178 warning (msg);
7be570e7
JM
179 return;
180 }
181 }
c906108c
SS
182 }
183 else
184 for (regno = 0; regno < NUM_REGS; regno++)
185 store_inferior_registers (regno);
186}
187
c906108c 188
adf40b2e 189/* Fetch a register's value from the process's U area. */
c906108c 190static void
fba45db2 191fetch_register (int regno)
c906108c 192{
123a958e 193 char buf[MAX_REGISTER_SIZE];
adf40b2e
JM
194 unsigned int addr, len, offset;
195 int i;
c906108c 196
adf40b2e
JM
197 offset = 0;
198 len = REGISTER_RAW_SIZE (regno);
199
200 /* Requests for register zero actually want the save_state's
201 ss_flags member. As RM says: "Oh, what a hack!" */
202 if (regno == 0)
203 {
204 save_state_t ss;
205 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
206 len = sizeof (ss.ss_flags);
207
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));
214 }
c906108c 215
adf40b2e
JM
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)));
220
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. */
226 else if (len == 8)
227 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
228 + REGISTER_BYTE (regno));
229
230 /* Narrow registers come from the ss_narrow area. Note that
231 ss_narrow starts with gr1, not gr0. */
232 else if (len == 4)
233 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
234 + (REGISTER_BYTE (regno) - REGISTER_BYTE (1)));
c906108c 235
adf40b2e 236 else
8e65ff28
AC
237 internal_error (__FILE__, __LINE__,
238 "hppa-nat.c (fetch_register): unexpected register size");
adf40b2e
JM
239
240 for (i = 0; i < len; i += sizeof (int))
c906108c
SS
241 {
242 errno = 0;
adf40b2e
JM
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] =
39f77062 246 call_ptrace (PT_RUREGS, PIDGET (inferior_ptid),
adf40b2e 247 (PTRACE_ARG3_TYPE) addr + i, 0);
c906108c
SS
248 if (errno != 0)
249 {
adf40b2e
JM
250 /* Warning, not error, in case we are attached; sometimes
251 the kernel doesn't let us at the registers. */
c906108c
SS
252 char *err = safe_strerror (errno);
253 char *msg = alloca (strlen (err) + 128);
adf40b2e
JM
254 sprintf (msg, "reading `%s' register: %s",
255 REGISTER_NAME (regno), err);
c906108c 256 warning (msg);
adf40b2e 257 return;
c906108c
SS
258 }
259 }
adf40b2e
JM
260
261 /* If we're reading an address from the instruction address queue,
262 mask out the bottom two bits --- they contain the privilege
263 level. */
c906108c 264 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
adf40b2e
JM
265 buf[len - 1] &= ~0x3;
266
c906108c 267 supply_register (regno, buf);
c906108c
SS
268}
269
adf40b2e 270
c906108c
SS
271/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
272 to debugger memory starting at MYADDR. Copy to inferior if
273 WRITE is nonzero.
c5aa993b 274
c906108c
SS
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
8fef05cc 278 anyway. TARGET is ignored. */
c906108c
SS
279
280int
8fef05cc 281child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
240be855 282 struct mem_attrib *mem,
8fef05cc 283 struct target_ops *target)
c906108c
SS
284{
285 register int i;
286 /* Round starting address down to longword boundary. */
a0b3c4fd 287 register CORE_ADDR addr = memaddr & - (CORE_ADDR)(sizeof (int));
c906108c
SS
288 /* Round ending address up; get number of longwords that makes. */
289 register int count
c5aa993b 290 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
c906108c 291
b83266a0
SS
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.
295
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
299 call chain. */
c906108c
SS
300 register int *buffer = (int *) xmalloc (count * sizeof (int));
301
302 if (write)
303 {
304 /* Fill start and end extra bytes of buffer with existing memory data. */
c5aa993b 305 if (addr != memaddr || len < (int) sizeof (int))
b83266a0
SS
306 {
307 /* Need part of initial word -- fetch it. */
c5aa993b 308 buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
39f77062
KB
309 PIDGET (inferior_ptid),
310 (PTRACE_ARG3_TYPE) addr, 0);
b83266a0 311 }
c906108c
SS
312
313 if (count > 1) /* FIXME, avoid if even boundary */
314 {
315 buffer[count - 1]
b83266a0 316 = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
39f77062 317 PIDGET (inferior_ptid),
b83266a0
SS
318 (PTRACE_ARG3_TYPE) (addr
319 + (count - 1) * sizeof (int)),
320 0);
c906108c
SS
321 }
322
323 /* Copy data to be written over corresponding part of buffer */
c906108c
SS
324 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
325
326 /* Write the entire buffer. */
c906108c
SS
327 for (i = 0; i < count; i++, addr += sizeof (int))
328 {
b83266a0
SS
329 int pt_status;
330 int pt_request;
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??! */
c906108c 336 errno = 0;
b83266a0 337 pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER;
c906108c 338 pt_status = call_ptrace (pt_request,
39f77062 339 PIDGET (inferior_ptid),
b83266a0
SS
340 (PTRACE_ARG3_TYPE) addr,
341 buffer[i]);
342
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)
347 {
348 errno = 0;
349 pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER;
350 pt_status = call_ptrace (pt_request,
39f77062 351 PIDGET (inferior_ptid),
b83266a0
SS
352 (PTRACE_ARG3_TYPE) addr,
353 buffer[i]);
354
355 /* No, we still fail. Okay, time to punt. */
356 if ((pt_status == -1) && errno)
357 {
b8c9b27d 358 xfree (buffer);
b83266a0
SS
359 return 0;
360 }
361 }
c906108c
SS
362 }
363 }
364 else
365 {
366 /* Read all the longwords */
367 for (i = 0; i < count; i++, addr += sizeof (int))
368 {
369 errno = 0;
c5aa993b 370 buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
39f77062
KB
371 PIDGET (inferior_ptid),
372 (PTRACE_ARG3_TYPE) addr, 0);
b83266a0
SS
373 if (errno)
374 {
b8c9b27d 375 xfree (buffer);
b83266a0
SS
376 return 0;
377 }
c906108c
SS
378 QUIT;
379 }
380
381 /* Copy appropriate bytes out of the buffer. */
382 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
383 }
b8c9b27d 384 xfree (buffer);
c906108c
SS
385 return len;
386}
387
7d2830a3 388char *saved_child_execd_pathname = NULL;
6604731b 389int saved_vfork_pid;
7d2830a3
DJ
390enum {
391 STATE_NONE,
392 STATE_GOT_CHILD,
393 STATE_GOT_EXEC,
394 STATE_GOT_PARENT,
395 STATE_FAKE_EXEC
396} saved_vfork_state = STATE_NONE;
c906108c 397
6604731b
DJ
398int
399child_follow_fork (int follow_child)
c906108c 400{
6604731b
DJ
401 ptid_t last_ptid;
402 struct target_waitstatus last_status;
403 int has_vforked;
404 int parent_pid, child_pid;
405
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;
410
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. */
415
416 if (! follow_child)
c906108c 417 {
6604731b
DJ
418 if (! has_vforked)
419 {
420 detach_breakpoints (child_pid);
421#ifdef SOLIB_REMOVE_INFERIOR_HOOK
422 SOLIB_REMOVE_INFERIOR_HOOK (child_pid);
423#endif
424 }
425
426 /* Detach from the child. */
4c9ba7e0
DJ
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);
6604731b
DJ
430
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
434 of the events.
435
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!)
452
453 Sequence of events, as reported to gdb from HPUX:
454
455 Parent Child Action for gdb to take
456 -------------------------------------------------------
457 1 VFORK Continue child
458 2 EXEC
459 3 EXEC or EXIT
460 4 VFORK
461
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. */
465
466 if (has_vforked)
467 reattach_breakpoints (parent_pid);
c906108c 468 }
6604731b
DJ
469 else
470 {
6604731b
DJ
471 /* Needed to keep the breakpoint lists in sync. */
472 if (! has_vforked)
473 detach_breakpoints (child_pid);
7d2830a3 474
6604731b
DJ
475 /* Before detaching from the parent, remove all breakpoints from it. */
476 remove_breakpoints ();
477
478 /* Also reset the solib inferior hook from the parent. */
479#ifdef SOLIB_REMOVE_INFERIOR_HOOK
480 SOLIB_REMOVE_INFERIOR_HOOK (PIDGET (inferior_ptid));
481#endif
7d2830a3 482
6604731b
DJ
483 /* Detach from the parent. */
484 target_detach (NULL, 1);
485
486 /* Attach to the child. */
4c9ba7e0
DJ
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);
6604731b 490 inferior_ptid = pid_to_ptid (child_pid);
6604731b
DJ
491
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.. */
495 if (!has_vforked)
496 follow_inferior_reset_breakpoints ();
497 }
498
499 if (has_vforked)
c906108c 500 {
6604731b
DJ
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");
506
507 if (! follow_child || saved_vfork_state == STATE_GOT_PARENT)
508 saved_vfork_state = STATE_NONE;
509 else
510 return 1;
c906108c 511 }
6604731b 512 return 0;
c906108c
SS
513}
514
b83266a0
SS
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". */
c906108c 517char *
39f77062 518child_pid_to_str (ptid_t ptid)
c906108c 519{
c5aa993b
JM
520 /* Static because address returned */
521 static char buf[30];
39f77062 522 pid_t pid = PIDGET (ptid);
c906108c 523
ce414844
AC
524 /* Extra NUL for paranoia's sake */
525 sprintf (buf, "process %d%c", pid, '\0');
c5aa993b
JM
526
527 return buf;
c906108c
SS
528}
529
b83266a0
SS
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".
532
533 Note: This is a core-gdb tid, not the actual system tid.
c5aa993b 534 See infttrace.c for details. */
c906108c 535char *
39f77062 536hppa_tid_to_str (ptid_t ptid)
c906108c 537{
c5aa993b
JM
538 /* Static because address returned */
539 static char buf[30];
39f77062
KB
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);
c5aa993b
JM
543
544 /* Extra NULLs for paranoia's sake */
ce414844 545 sprintf (buf, "system thread %d%c", tid, '\0');
c906108c 546
c5aa993b 547 return buf;
c906108c
SS
548}
549
47932f85
DJ
550/*## */
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
554 * won't loop.
555 */
556int not_same_real_pid = 1;
557/*## */
558
47932f85
DJ
559/* Wait for child to do something. Return pid of child, or -1 in case
560 of error; store status through argument pointer OURSTATUS. */
561
562ptid_t
563child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
564{
565 int save_errno;
566 int status;
567 char *execd_pathname = NULL;
568 int exit_status;
569 int related_pid;
570 int syscall_id;
571 enum target_waitkind kind;
572 int pid;
573
7d2830a3
DJ
574 if (saved_vfork_state == STATE_FAKE_EXEC)
575 {
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;
580 }
581
47932f85
DJ
582 do
583 {
584 set_sigint_trap (); /* Causes SIGINT to be passed on to the
585 attached process. */
586 set_sigio_trap ();
587
588 pid = ptrace_wait (inferior_ptid, &status);
589
590 save_errno = errno;
591
592 clear_sigio_trap ();
593
594 clear_sigint_trap ();
595
596 if (pid == -1)
597 {
598 if (save_errno == EINTR)
599 continue;
600
601 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
602 safe_strerror (save_errno));
603
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);
608 }
609
610 /* Did it exit?
611 */
612 if (target_has_exited (pid, status, &exit_status))
613 {
614 /* ??rehrauer: For now, ignore this. */
615 continue;
616 }
617
618 if (!target_thread_alive (pid_to_ptid (pid)))
619 {
620 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
621 return pid_to_ptid (pid);
622 }
623
8e7d2c16 624 if (hpux_has_forked (pid, &related_pid))
47932f85 625 {
8e7d2c16
DJ
626 /* Ignore the parent's fork event. */
627 if (pid == PIDGET (inferior_ptid))
628 {
629 ourstatus->kind = TARGET_WAITKIND_IGNORE;
630 return inferior_ptid;
631 }
632
633 /* If this is the child's fork event, report that the
634 process has forked. */
635 if (related_pid == PIDGET (inferior_ptid))
636 {
637 ourstatus->kind = TARGET_WAITKIND_FORKED;
638 ourstatus->value.related_pid = pid;
639 return inferior_ptid;
640 }
47932f85
DJ
641 }
642
7d2830a3 643 if (hpux_has_vforked (pid, &related_pid))
47932f85 644 {
7d2830a3
DJ
645 if (pid == PIDGET (inferior_ptid))
646 {
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;
651 else
652 fprintf_unfiltered (gdb_stdout,
653 "hppah: parent vfork: confused\n");
654 }
655 else if (related_pid == PIDGET (inferior_ptid))
656 {
657 if (saved_vfork_state == STATE_NONE)
658 saved_vfork_state = STATE_GOT_CHILD;
659 else
660 fprintf_unfiltered (gdb_stdout,
661 "hppah: child vfork: confused\n");
662 }
663 else
664 fprintf_unfiltered (gdb_stdout,
665 "hppah: unknown vfork: confused\n");
666
667 if (saved_vfork_state == STATE_GOT_CHILD)
668 {
669 child_post_startup_inferior (pid_to_ptid (pid));
6604731b
DJ
670 detach_breakpoints (pid);
671#ifdef SOLIB_REMOVE_INFERIOR_HOOK
672 SOLIB_REMOVE_INFERIOR_HOOK (pid);
673#endif
674 child_resume (pid_to_ptid (pid), 0, TARGET_SIGNAL_0);
675 ourstatus->kind = TARGET_WAITKIND_IGNORE;
676 return pid_to_ptid (related_pid);
7d2830a3 677 }
6604731b 678 else if (saved_vfork_state == STATE_FAKE_EXEC)
7d2830a3
DJ
679 {
680 ourstatus->kind = TARGET_WAITKIND_VFORKED;
681 ourstatus->value.related_pid = related_pid;
682 return pid_to_ptid (pid);
683 }
6604731b
DJ
684 else
685 {
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);
691 }
47932f85
DJ
692 }
693
694 if (hpux_has_execd (pid, &execd_pathname))
695 {
7d2830a3
DJ
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.
6604731b
DJ
699 The latter two can come in either order. Make sure we get
700 both. */
701 if (saved_vfork_state != STATE_NONE)
7d2830a3 702 {
6604731b
DJ
703 if (saved_vfork_state == STATE_GOT_CHILD)
704 {
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
708 suffices. */
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;
712 }
713 else if (saved_vfork_state == STATE_GOT_PARENT)
714 {
715 saved_vfork_state = STATE_FAKE_EXEC;
716 ourstatus->kind = TARGET_WAITKIND_VFORKED;
717 ourstatus->value.related_pid = saved_vfork_pid;
718 }
719 else
720 fprintf_unfiltered (gdb_stdout,
721 "hppa: exec: unexpected state\n");
7d2830a3 722
6604731b 723 saved_child_execd_pathname = execd_pathname;
7d2830a3 724
7d2830a3
DJ
725 return inferior_ptid;
726 }
727
47932f85
DJ
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.
732 */
733 if (inferior_ignoring_startup_exec_events)
734 {
735 inferior_ignoring_startup_exec_events--;
736 }
737 else
738 {
739 ourstatus->kind = TARGET_WAITKIND_EXECD;
740 ourstatus->value.execd_pathname = execd_pathname;
741 return pid_to_ptid (pid);
742 }
743 }
744
745 /* All we must do with these is communicate their occurrence
746 to wait_for_inferior...
747 */
748 if (hpux_has_syscall_event (pid, &kind, &syscall_id))
749 {
750 ourstatus->kind = kind;
751 ourstatus->value.syscall_id = syscall_id;
752 return pid_to_ptid (pid);
753 }
754
755 /*## } while (pid != PIDGET (inferior_ptid)); ## *//* Some other child died or stopped */
756/* hack for thread testing */
757 }
758 while ((pid != PIDGET (inferior_ptid)) && not_same_real_pid);
759/*## */
760
761 store_waitstatus (ourstatus, status);
762 return pid_to_ptid (pid);
763}
764
c906108c
SS
765#if !defined (GDB_NATIVE_HPUX_11)
766
767/* The following code is a substitute for the infttrace.c versions used
768 with ttrace() in HPUX 11. */
769
770/* This value is an arbitrary integer. */
771#define PT_VERSION 123456
772
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. */
776
c5aa993b
JM
777typedef struct
778{
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] */
781}
782startup_semaphore_t;
c906108c
SS
783
784#define SEM_TALK (1)
785#define SEM_LISTEN (0)
786
c5aa993b 787static startup_semaphore_t startup_semaphore;
c906108c 788
c906108c
SS
789#ifdef PT_SETTRC
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.
793
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.
799
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.
805
806 (The acknowledgement by parent happens as a result of a call to
807 child_acknowledge_created_inferior.) */
808
809int
fba45db2 810parent_attach_all (int pid, PTRACE_ARG3_TYPE addr, int data)
c906108c
SS
811{
812 int pt_status = 0;
813
814 /* We need a memory home for a constant. */
815 int tc_magic_child = PT_VERSION;
816 int tc_magic_parent = 0;
817
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)
822
823 /* Notify the parent that we're potentially ready to exec(). */
824 write (startup_semaphore.child_channel[SEM_TALK],
b83266a0
SS
825 &tc_magic_child,
826 sizeof (tc_magic_child));
c906108c
SS
827
828 /* Wait for acknowledgement from the parent. */
829 read (startup_semaphore.parent_channel[SEM_LISTEN],
b83266a0
SS
830 &tc_magic_parent,
831 sizeof (tc_magic_parent));
c906108c 832 if (tc_magic_child != tc_magic_parent)
c5aa993b 833 warning ("mismatched semaphore magic");
c906108c
SS
834
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]);
840#endif
c5aa993b 841
c906108c
SS
842 return 0;
843}
844#endif
845
846int
fba45db2 847hppa_require_attach (int pid)
c906108c
SS
848{
849 int pt_status;
b83266a0
SS
850 CORE_ADDR pc;
851 CORE_ADDR pc_addr;
c906108c
SS
852 unsigned int regs_offset;
853
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. */
858
859 errno = 0;
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);
863
864 if (errno)
865 {
866 errno = 0;
867 pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
868
869 if (errno)
b83266a0 870 return -1;
c906108c
SS
871
872 /* Now we really are attached. */
873 errno = 0;
874 }
875 attach_flag = 1;
876 return pid;
877}
878
879int
fba45db2 880hppa_require_detach (int pid, int signal)
c906108c
SS
881{
882 errno = 0;
883 call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal);
c5aa993b 884 errno = 0; /* Ignore any errors. */
c906108c
SS
885 return pid;
886}
887
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. */
891
892void
fba45db2 893hppa_enable_page_protection_events (int pid)
c906108c
SS
894{
895}
896
897void
fba45db2 898hppa_disable_page_protection_events (int pid)
c906108c
SS
899{
900}
901
902int
fba45db2 903hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
c906108c
SS
904{
905 error ("Hardware watchpoints not implemented on this platform.");
906}
907
908int
65e82032 909hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
c906108c
SS
910{
911 error ("Hardware watchpoints not implemented on this platform.");
912}
913
914int
65e82032 915hppa_can_use_hw_watchpoint (int type, int cnt, int ot)
c906108c
SS
916{
917 return 0;
918}
919
920int
fba45db2 921hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
c906108c
SS
922{
923 error ("Hardware watchpoints not implemented on this platform.");
924}
925
926char *
39f77062 927hppa_pid_or_tid_to_str (ptid_t id)
c906108c
SS
928{
929 /* In the ptrace world, there are only processes. */
ed9a39eb 930 return child_pid_to_str (id);
c906108c
SS
931}
932
c906108c 933void
fba45db2 934hppa_ensure_vforking_parent_remains_stopped (int pid)
c906108c
SS
935{
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
941 this function. */
942 kill (pid, SIGTRAP);
943}
944
945int
fba45db2 946hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
c906108c 947{
c5aa993b 948 return 1; /* Yes, the child must be resumed. */
c906108c
SS
949}
950
951void
fba45db2 952require_notification_of_events (int pid)
c906108c
SS
953{
954#if defined(PT_SET_EVENT_MASK)
955 int pt_status;
956 ptrace_event_t ptrace_events;
c2d11a7d
JM
957 int nsigs;
958 int signum;
c906108c
SS
959
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
b83266a0 963 <sys/ptrace.h>, then we're being built on pre-10.0.) */
c906108c
SS
964 memset (&ptrace_events, 0, sizeof (ptrace_events));
965
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
b83266a0 969 each such signal here, before doing PT_SET_EVENT_MASK. */
c2d11a7d
JM
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);
979
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--)
983 {
984 if ((signal_stop_state (signum)) ||
985 (signal_print_state (signum)) ||
986 (!signal_pass_state (signum)))
987 {
988 if (target_signal_to_host_p (signum))
989 sigdelset (&ptrace_events.pe_signals,
990 target_signal_to_host (signum));
991 }
992 }
c906108c
SS
993
994 ptrace_events.pe_set_event = 0;
995
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...
c5aa993b
JM
1001 ptrace_events.pe_set_event |= PTRACE_EXIT;
1002 */
c906108c
SS
1003
1004 errno = 0;
1005 pt_status = call_ptrace (PT_SET_EVENT_MASK,
c5aa993b
JM
1006 pid,
1007 (PTRACE_ARG3_TYPE) & ptrace_events,
1008 sizeof (ptrace_events));
c906108c
SS
1009 if (errno)
1010 perror_with_name ("ptrace");
1011 if (pt_status < 0)
1012 return;
1013#endif
1014}
1015
1016void
fba45db2 1017require_notification_of_exec_events (int pid)
c906108c
SS
1018{
1019#if defined(PT_SET_EVENT_MASK)
1020 int pt_status;
1021 ptrace_event_t ptrace_events;
1022
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
b83266a0 1026 <sys/ptrace.h>, then we're being built on pre-10.0.) */
c906108c
SS
1027 memset (&ptrace_events, 0, sizeof (ptrace_events));
1028
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
b83266a0 1032 each such signal here, before doing PT_SET_EVENT_MASK. */
c906108c
SS
1033 sigemptyset (&ptrace_events.pe_signals);
1034
1035 ptrace_events.pe_set_event = 0;
1036
1037 ptrace_events.pe_set_event |= PTRACE_EXEC;
1038 /* ??rehrauer: Add this one when we're prepared to catch it...
c5aa993b
JM
1039 ptrace_events.pe_set_event |= PTRACE_EXIT;
1040 */
c906108c
SS
1041
1042 errno = 0;
1043 pt_status = call_ptrace (PT_SET_EVENT_MASK,
c5aa993b
JM
1044 pid,
1045 (PTRACE_ARG3_TYPE) & ptrace_events,
1046 sizeof (ptrace_events));
c906108c
SS
1047 if (errno)
1048 perror_with_name ("ptrace");
1049 if (pt_status < 0)
1050 return;
1051#endif
1052}
1053
1054/* This function is called by the parent process, with pid being the
1055 ID of the child process, after the debugger has forked. */
1056
1057void
fba45db2 1058child_acknowledge_created_inferior (int pid)
c906108c
SS
1059{
1060 /* We need a memory home for a constant. */
1061 int tc_magic_parent = PT_VERSION;
1062 int tc_magic_child = 0;
1063
b83266a0
SS
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)
c906108c
SS
1068 /* Wait for the child to tell us that it has forked. */
1069 read (startup_semaphore.child_channel[SEM_LISTEN],
b83266a0 1070 &tc_magic_child,
c5aa993b 1071 sizeof (tc_magic_child));
c906108c
SS
1072
1073 /* Notify the child that it can exec.
1074
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. */
c906108c 1078 write (startup_semaphore.parent_channel[SEM_TALK],
b83266a0
SS
1079 &tc_magic_parent,
1080 sizeof (tc_magic_parent));
c906108c
SS
1081
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.
1086
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... */
c906108c
SS
1090 sleep (1);
1091
1092 /* Instruct the kernel as to the set of events we wish to be
1093 informed of. */
c906108c
SS
1094 require_notification_of_exec_events (pid);
1095
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]);
b83266a0 1101#endif
c906108c
SS
1102}
1103
1104void
39f77062 1105child_post_startup_inferior (ptid_t ptid)
c906108c 1106{
39f77062 1107 require_notification_of_events (PIDGET (ptid));
c906108c
SS
1108}
1109
1110void
fba45db2 1111child_post_attach (int pid)
c906108c
SS
1112{
1113 require_notification_of_events (pid);
1114}
1115
1116int
fba45db2 1117child_insert_fork_catchpoint (int pid)
c906108c
SS
1118{
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");
1122#else
1123 /* Enable reporting of fork events from the kernel. */
1124 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 1125 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
1126 return 0;
1127#endif
1128}
1129
1130int
fba45db2 1131child_remove_fork_catchpoint (int pid)
c906108c
SS
1132{
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");
1136#else
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. */
1140 return 0;
1141#endif
1142}
1143
1144int
fba45db2 1145child_insert_vfork_catchpoint (int pid)
c906108c
SS
1146{
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");
1150#else
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. */
1154 return 0;
1155#endif
1156}
1157
1158int
fba45db2 1159child_remove_vfork_catchpoint (int pid)
c906108c
SS
1160{
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");
1164#else
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. */
1168 return 0;
1169#endif
1170}
1171
1172int
47932f85 1173hpux_has_forked (int pid, int *childpid)
c906108c
SS
1174{
1175 /* This request is only available on HPUX 10.0 and later. */
1176#if !defined(PT_GET_PROCESS_STATE)
1177 *childpid = 0;
1178 return 0;
1179#else
1180 int pt_status;
c5aa993b 1181 ptrace_state_t ptrace_state;
c906108c
SS
1182
1183 errno = 0;
1184 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 1185 pid,
c5aa993b 1186 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 1187 sizeof (ptrace_state));
c906108c
SS
1188 if (errno)
1189 perror_with_name ("ptrace");
1190 if (pt_status < 0)
1191 return 0;
1192
1193 if (ptrace_state.pe_report_event & PTRACE_FORK)
1194 {
1195 *childpid = ptrace_state.pe_other_pid;
1196 return 1;
1197 }
1198
1199 return 0;
1200#endif
1201}
1202
1203int
47932f85 1204hpux_has_vforked (int pid, int *childpid)
c906108c
SS
1205{
1206 /* This request is only available on HPUX 10.0 and later. */
1207#if !defined(PT_GET_PROCESS_STATE)
1208 *childpid = 0;
1209 return 0;
1210
1211#else
1212 int pt_status;
c5aa993b 1213 ptrace_state_t ptrace_state;
c906108c
SS
1214
1215 errno = 0;
1216 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 1217 pid,
c5aa993b 1218 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 1219 sizeof (ptrace_state));
c906108c
SS
1220 if (errno)
1221 perror_with_name ("ptrace");
1222 if (pt_status < 0)
1223 return 0;
1224
1225 if (ptrace_state.pe_report_event & PTRACE_VFORK)
1226 {
1227 *childpid = ptrace_state.pe_other_pid;
1228 return 1;
1229 }
1230
1231 return 0;
1232#endif
1233}
1234
c906108c 1235int
fba45db2 1236child_insert_exec_catchpoint (int pid)
c906108c 1237{
b83266a0 1238 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
1239#if !defined(PT_SET_EVENT_MASK)
1240 error ("Unable to catch execs prior to HPUX 10.0");
1241
1242#else
b83266a0 1243 /* Enable reporting of exec events from the kernel. */
c906108c 1244 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 1245 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
1246 return 0;
1247#endif
1248}
1249
1250int
fba45db2 1251child_remove_exec_catchpoint (int pid)
c906108c 1252{
b83266a0 1253 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
1254#if !defined(PT_SET_EVENT_MASK)
1255 error ("Unable to catch execs prior to HPUX 10.0");
1256
1257#else
1258 /* Disable reporting of exec events from the kernel. */
1259 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 1260 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
1261 return 0;
1262#endif
1263}
1264
1265int
47932f85 1266hpux_has_execd (int pid, char **execd_pathname)
c906108c 1267{
b83266a0 1268 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
1269#if !defined(PT_GET_PROCESS_STATE)
1270 *execd_pathname = NULL;
1271 return 0;
1272
1273#else
1274 int pt_status;
c5aa993b 1275 ptrace_state_t ptrace_state;
c906108c
SS
1276
1277 errno = 0;
1278 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 1279 pid,
c5aa993b 1280 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 1281 sizeof (ptrace_state));
c906108c
SS
1282 if (errno)
1283 perror_with_name ("ptrace");
1284 if (pt_status < 0)
1285 return 0;
1286
1287 if (ptrace_state.pe_report_event & PTRACE_EXEC)
1288 {
c5aa993b 1289 char *exec_file = target_pid_to_exec_file (pid);
c906108c
SS
1290 *execd_pathname = savestring (exec_file, strlen (exec_file));
1291 return 1;
1292 }
1293
1294 return 0;
1295#endif
1296}
1297
1298int
fba45db2 1299child_reported_exec_events_per_exec_call (void)
c906108c 1300{
c5aa993b 1301 return 2; /* ptrace reports the event twice per call. */
c906108c
SS
1302}
1303
1304int
47932f85 1305hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
c906108c
SS
1306{
1307 /* This request is only available on HPUX 10.30 and later, via
1308 the ttrace interface. */
1309
1310 *kind = TARGET_WAITKIND_SPURIOUS;
1311 *syscall_id = -1;
1312 return 0;
1313}
1314
1315char *
fba45db2 1316child_pid_to_exec_file (int pid)
c906108c 1317{
b83266a0 1318 static char exec_file_buffer[1024];
c906108c 1319 int pt_status;
b83266a0
SS
1320 CORE_ADDR top_of_stack;
1321 char four_chars[4];
c906108c
SS
1322 int name_index;
1323 int i;
39f77062 1324 ptid_t saved_inferior_ptid;
52287340 1325 int done;
c5aa993b 1326
c906108c
SS
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,
b83266a0
SS
1330 pid,
1331 (PTRACE_ARG3_TYPE) exec_file_buffer,
1332 sizeof (exec_file_buffer) - 1);
c906108c
SS
1333 if (pt_status == 0)
1334 return exec_file_buffer;
1335#endif
1336
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. */
1341
1342 top_of_stack = 0x7b03a000;
1343 name_index = 0;
1344 done = 0;
1345
39f77062
KB
1346 /* On the chance that pid != inferior_ptid, set inferior_ptid
1347 to pid, so that (grrrr!) implicit uses of inferior_ptid get
c906108c
SS
1348 the right id. */
1349
39f77062
KB
1350 saved_inferior_ptid = inferior_ptid;
1351 inferior_ptid = pid_to_ptid (pid);
c906108c
SS
1352
1353 /* Try to grab a null-terminated string. */
c5aa993b 1354 while (!done)
c906108c
SS
1355 {
1356 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
1357 {
39f77062 1358 inferior_ptid = saved_inferior_ptid;
c906108c
SS
1359 return NULL;
1360 }
1361 for (i = 0; i < 4; i++)
1362 {
1363 exec_file_buffer[name_index++] = four_chars[i];
1364 done = (four_chars[i] == '\0');
1365 if (done)
1366 break;
1367 }
1368 top_of_stack += 4;
1369 }
1370
1371 if (exec_file_buffer[0] == '\0')
1372 {
39f77062 1373 inferior_ptid = saved_inferior_ptid;
c906108c
SS
1374 return NULL;
1375 }
1376
39f77062 1377 inferior_ptid = saved_inferior_ptid;
c906108c
SS
1378 return exec_file_buffer;
1379}
1380
1381void
fba45db2 1382pre_fork_inferior (void)
c906108c
SS
1383{
1384 int status;
1385
1386 status = pipe (startup_semaphore.parent_channel);
1387 if (status < 0)
1388 {
1389 warning ("error getting parent pipe for startup semaphore");
1390 return;
1391 }
1392
1393 status = pipe (startup_semaphore.child_channel);
1394 if (status < 0)
1395 {
1396 warning ("error getting child pipe for startup semaphore");
1397 return;
1398 }
1399}
c906108c 1400\f
c5aa993b 1401
c906108c
SS
1402/* Check to see if the given thread is alive.
1403
1404 This is a no-op, as ptrace doesn't support threads, so we just
1405 return "TRUE". */
1406
1407int
39f77062 1408child_thread_alive (ptid_t ptid)
c906108c 1409{
c5aa993b 1410 return 1;
c906108c
SS
1411}
1412
1413#endif /* ! GDB_NATIVE_HPUX_11 */
This page took 0.357401 seconds and 4 git commands to generate.