* alpha-tdep.c (alpha_supply_int_regs, alpha_fill_int_regs): New.
[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
933/* This function has no meaning in a non-threaded world. Thus, we
934 return 0 (FALSE). See the use of "hppa_prepare_to_proceed" in
935 hppa-tdep.c. */
936
937pid_t
fba45db2 938hppa_switched_threads (pid_t pid)
c906108c
SS
939{
940 return (pid_t) 0;
941}
942
943void
fba45db2 944hppa_ensure_vforking_parent_remains_stopped (int pid)
c906108c
SS
945{
946 /* This assumes that the vforked parent is presently stopped, and
947 that the vforked child has just delivered its first exec event.
948 Calling kill() this way will cause the SIGTRAP to be delivered as
949 soon as the parent is resumed, which happens as soon as the
950 vforked child is resumed. See wait_for_inferior for the use of
951 this function. */
952 kill (pid, SIGTRAP);
953}
954
955int
fba45db2 956hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
c906108c 957{
c5aa993b 958 return 1; /* Yes, the child must be resumed. */
c906108c
SS
959}
960
961void
fba45db2 962require_notification_of_events (int pid)
c906108c
SS
963{
964#if defined(PT_SET_EVENT_MASK)
965 int pt_status;
966 ptrace_event_t ptrace_events;
c2d11a7d
JM
967 int nsigs;
968 int signum;
c906108c
SS
969
970 /* Instruct the kernel as to the set of events we wish to be
971 informed of. (This support does not exist before HPUX 10.0.
972 We'll assume if PT_SET_EVENT_MASK has not been defined by
b83266a0 973 <sys/ptrace.h>, then we're being built on pre-10.0.) */
c906108c
SS
974 memset (&ptrace_events, 0, sizeof (ptrace_events));
975
976 /* Note: By default, all signals are visible to us. If we wish
977 the kernel to keep certain signals hidden from us, we do it
978 by calling sigdelset (ptrace_events.pe_signals, signal) for
b83266a0 979 each such signal here, before doing PT_SET_EVENT_MASK. */
c2d11a7d
JM
980 /* RM: The above comment is no longer true. We start with ignoring
981 all signals, and then add the ones we are interested in. We could
982 do it the other way: start by looking at all signals and then
983 deleting the ones that we aren't interested in, except that
984 multiple gdb signals may be mapped to the same host signal
985 (eg. TARGET_SIGNAL_IO and TARGET_SIGNAL_POLL both get mapped to
986 signal 22 on HPUX 10.20) We want to be notified if we are
987 interested in either signal. */
988 sigfillset (&ptrace_events.pe_signals);
989
990 /* RM: Let's not bother with signals we don't care about */
991 nsigs = (int) TARGET_SIGNAL_LAST;
992 for (signum = nsigs; signum > 0; signum--)
993 {
994 if ((signal_stop_state (signum)) ||
995 (signal_print_state (signum)) ||
996 (!signal_pass_state (signum)))
997 {
998 if (target_signal_to_host_p (signum))
999 sigdelset (&ptrace_events.pe_signals,
1000 target_signal_to_host (signum));
1001 }
1002 }
c906108c
SS
1003
1004 ptrace_events.pe_set_event = 0;
1005
1006 ptrace_events.pe_set_event |= PTRACE_SIGNAL;
1007 ptrace_events.pe_set_event |= PTRACE_EXEC;
1008 ptrace_events.pe_set_event |= PTRACE_FORK;
1009 ptrace_events.pe_set_event |= PTRACE_VFORK;
1010 /* ??rehrauer: Add this one when we're prepared to catch it...
c5aa993b
JM
1011 ptrace_events.pe_set_event |= PTRACE_EXIT;
1012 */
c906108c
SS
1013
1014 errno = 0;
1015 pt_status = call_ptrace (PT_SET_EVENT_MASK,
c5aa993b
JM
1016 pid,
1017 (PTRACE_ARG3_TYPE) & ptrace_events,
1018 sizeof (ptrace_events));
c906108c
SS
1019 if (errno)
1020 perror_with_name ("ptrace");
1021 if (pt_status < 0)
1022 return;
1023#endif
1024}
1025
1026void
fba45db2 1027require_notification_of_exec_events (int pid)
c906108c
SS
1028{
1029#if defined(PT_SET_EVENT_MASK)
1030 int pt_status;
1031 ptrace_event_t ptrace_events;
1032
1033 /* Instruct the kernel as to the set of events we wish to be
1034 informed of. (This support does not exist before HPUX 10.0.
1035 We'll assume if PT_SET_EVENT_MASK has not been defined by
b83266a0 1036 <sys/ptrace.h>, then we're being built on pre-10.0.) */
c906108c
SS
1037 memset (&ptrace_events, 0, sizeof (ptrace_events));
1038
1039 /* Note: By default, all signals are visible to us. If we wish
1040 the kernel to keep certain signals hidden from us, we do it
1041 by calling sigdelset (ptrace_events.pe_signals, signal) for
b83266a0 1042 each such signal here, before doing PT_SET_EVENT_MASK. */
c906108c
SS
1043 sigemptyset (&ptrace_events.pe_signals);
1044
1045 ptrace_events.pe_set_event = 0;
1046
1047 ptrace_events.pe_set_event |= PTRACE_EXEC;
1048 /* ??rehrauer: Add this one when we're prepared to catch it...
c5aa993b
JM
1049 ptrace_events.pe_set_event |= PTRACE_EXIT;
1050 */
c906108c
SS
1051
1052 errno = 0;
1053 pt_status = call_ptrace (PT_SET_EVENT_MASK,
c5aa993b
JM
1054 pid,
1055 (PTRACE_ARG3_TYPE) & ptrace_events,
1056 sizeof (ptrace_events));
c906108c
SS
1057 if (errno)
1058 perror_with_name ("ptrace");
1059 if (pt_status < 0)
1060 return;
1061#endif
1062}
1063
1064/* This function is called by the parent process, with pid being the
1065 ID of the child process, after the debugger has forked. */
1066
1067void
fba45db2 1068child_acknowledge_created_inferior (int pid)
c906108c
SS
1069{
1070 /* We need a memory home for a constant. */
1071 int tc_magic_parent = PT_VERSION;
1072 int tc_magic_child = 0;
1073
b83266a0
SS
1074 /* The remainder of this function is only useful for HPUX 10.0 and
1075 later, as it depends upon the ability to request notification
1076 of specific kinds of events by the kernel. */
1077#if defined(PT_SET_EVENT_MASK)
c906108c
SS
1078 /* Wait for the child to tell us that it has forked. */
1079 read (startup_semaphore.child_channel[SEM_LISTEN],
b83266a0 1080 &tc_magic_child,
c5aa993b 1081 sizeof (tc_magic_child));
c906108c
SS
1082
1083 /* Notify the child that it can exec.
1084
1085 In the infttrace.c variant of this function, we set the child's
1086 event mask after the fork but before the exec. In the ptrace
1087 world, it seems we can't set the event mask until after the exec. */
c906108c 1088 write (startup_semaphore.parent_channel[SEM_TALK],
b83266a0
SS
1089 &tc_magic_parent,
1090 sizeof (tc_magic_parent));
c906108c
SS
1091
1092 /* We'd better pause a bit before trying to set the event mask,
1093 though, to ensure that the exec has happened. We don't want to
1094 wait() on the child, because that'll screw up the upper layers
1095 of gdb's execution control that expect to see the exec event.
1096
1097 After an exec, the child is no longer executing gdb code. Hence,
1098 we can't have yet another synchronization via the pipes. We'll
1099 just sleep for a second, and hope that's enough delay... */
c906108c
SS
1100 sleep (1);
1101
1102 /* Instruct the kernel as to the set of events we wish to be
1103 informed of. */
c906108c
SS
1104 require_notification_of_exec_events (pid);
1105
1106 /* Discard our copy of the semaphore. */
1107 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
1108 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
1109 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
1110 (void) close (startup_semaphore.child_channel[SEM_TALK]);
b83266a0 1111#endif
c906108c
SS
1112}
1113
1114void
39f77062 1115child_post_startup_inferior (ptid_t ptid)
c906108c 1116{
39f77062 1117 require_notification_of_events (PIDGET (ptid));
c906108c
SS
1118}
1119
1120void
fba45db2 1121child_post_attach (int pid)
c906108c
SS
1122{
1123 require_notification_of_events (pid);
1124}
1125
1126int
fba45db2 1127child_insert_fork_catchpoint (int pid)
c906108c
SS
1128{
1129 /* This request is only available on HPUX 10.0 and later. */
1130#if !defined(PT_SET_EVENT_MASK)
1131 error ("Unable to catch forks prior to HPUX 10.0");
1132#else
1133 /* Enable reporting of fork events from the kernel. */
1134 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 1135 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
1136 return 0;
1137#endif
1138}
1139
1140int
fba45db2 1141child_remove_fork_catchpoint (int pid)
c906108c
SS
1142{
1143 /* This request is only available on HPUX 10.0 and later. */
1144#if !defined(PT_SET_EVENT_MASK)
1145 error ("Unable to catch forks prior to HPUX 10.0");
1146#else
1147 /* Disable reporting of fork events from the kernel. */
1148 /* ??rehrauer: For the moment, we're always enabling these events,
1149 and just ignoring them if there's no catchpoint to catch them. */
1150 return 0;
1151#endif
1152}
1153
1154int
fba45db2 1155child_insert_vfork_catchpoint (int pid)
c906108c
SS
1156{
1157 /* This request is only available on HPUX 10.0 and later. */
1158#if !defined(PT_SET_EVENT_MASK)
1159 error ("Unable to catch vforks prior to HPUX 10.0");
1160#else
1161 /* Enable reporting of vfork events from the kernel. */
1162 /* ??rehrauer: For the moment, we're always enabling these events,
1163 and just ignoring them if there's no catchpoint to catch them. */
1164 return 0;
1165#endif
1166}
1167
1168int
fba45db2 1169child_remove_vfork_catchpoint (int pid)
c906108c
SS
1170{
1171 /* This request is only available on HPUX 10.0 and later. */
1172#if !defined(PT_SET_EVENT_MASK)
1173 error ("Unable to catch vforks prior to HPUX 10.0");
1174#else
1175 /* Disable reporting of vfork events from the kernel. */
1176 /* ??rehrauer: For the moment, we're always enabling these events,
1177 and just ignoring them if there's no catchpoint to catch them. */
1178 return 0;
1179#endif
1180}
1181
1182int
47932f85 1183hpux_has_forked (int pid, int *childpid)
c906108c
SS
1184{
1185 /* This request is only available on HPUX 10.0 and later. */
1186#if !defined(PT_GET_PROCESS_STATE)
1187 *childpid = 0;
1188 return 0;
1189#else
1190 int pt_status;
c5aa993b 1191 ptrace_state_t ptrace_state;
c906108c
SS
1192
1193 errno = 0;
1194 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 1195 pid,
c5aa993b 1196 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 1197 sizeof (ptrace_state));
c906108c
SS
1198 if (errno)
1199 perror_with_name ("ptrace");
1200 if (pt_status < 0)
1201 return 0;
1202
1203 if (ptrace_state.pe_report_event & PTRACE_FORK)
1204 {
1205 *childpid = ptrace_state.pe_other_pid;
1206 return 1;
1207 }
1208
1209 return 0;
1210#endif
1211}
1212
1213int
47932f85 1214hpux_has_vforked (int pid, int *childpid)
c906108c
SS
1215{
1216 /* This request is only available on HPUX 10.0 and later. */
1217#if !defined(PT_GET_PROCESS_STATE)
1218 *childpid = 0;
1219 return 0;
1220
1221#else
1222 int pt_status;
c5aa993b 1223 ptrace_state_t ptrace_state;
c906108c
SS
1224
1225 errno = 0;
1226 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 1227 pid,
c5aa993b 1228 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 1229 sizeof (ptrace_state));
c906108c
SS
1230 if (errno)
1231 perror_with_name ("ptrace");
1232 if (pt_status < 0)
1233 return 0;
1234
1235 if (ptrace_state.pe_report_event & PTRACE_VFORK)
1236 {
1237 *childpid = ptrace_state.pe_other_pid;
1238 return 1;
1239 }
1240
1241 return 0;
1242#endif
1243}
1244
c906108c 1245int
fba45db2 1246child_insert_exec_catchpoint (int pid)
c906108c 1247{
b83266a0 1248 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
1249#if !defined(PT_SET_EVENT_MASK)
1250 error ("Unable to catch execs prior to HPUX 10.0");
1251
1252#else
b83266a0 1253 /* Enable reporting of exec events from the kernel. */
c906108c 1254 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 1255 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
1256 return 0;
1257#endif
1258}
1259
1260int
fba45db2 1261child_remove_exec_catchpoint (int pid)
c906108c 1262{
b83266a0 1263 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
1264#if !defined(PT_SET_EVENT_MASK)
1265 error ("Unable to catch execs prior to HPUX 10.0");
1266
1267#else
1268 /* Disable reporting of exec events from the kernel. */
1269 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 1270 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
1271 return 0;
1272#endif
1273}
1274
1275int
47932f85 1276hpux_has_execd (int pid, char **execd_pathname)
c906108c 1277{
b83266a0 1278 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
1279#if !defined(PT_GET_PROCESS_STATE)
1280 *execd_pathname = NULL;
1281 return 0;
1282
1283#else
1284 int pt_status;
c5aa993b 1285 ptrace_state_t ptrace_state;
c906108c
SS
1286
1287 errno = 0;
1288 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 1289 pid,
c5aa993b 1290 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 1291 sizeof (ptrace_state));
c906108c
SS
1292 if (errno)
1293 perror_with_name ("ptrace");
1294 if (pt_status < 0)
1295 return 0;
1296
1297 if (ptrace_state.pe_report_event & PTRACE_EXEC)
1298 {
c5aa993b 1299 char *exec_file = target_pid_to_exec_file (pid);
c906108c
SS
1300 *execd_pathname = savestring (exec_file, strlen (exec_file));
1301 return 1;
1302 }
1303
1304 return 0;
1305#endif
1306}
1307
1308int
fba45db2 1309child_reported_exec_events_per_exec_call (void)
c906108c 1310{
c5aa993b 1311 return 2; /* ptrace reports the event twice per call. */
c906108c
SS
1312}
1313
1314int
47932f85 1315hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
c906108c
SS
1316{
1317 /* This request is only available on HPUX 10.30 and later, via
1318 the ttrace interface. */
1319
1320 *kind = TARGET_WAITKIND_SPURIOUS;
1321 *syscall_id = -1;
1322 return 0;
1323}
1324
1325char *
fba45db2 1326child_pid_to_exec_file (int pid)
c906108c 1327{
b83266a0 1328 static char exec_file_buffer[1024];
c906108c 1329 int pt_status;
b83266a0
SS
1330 CORE_ADDR top_of_stack;
1331 char four_chars[4];
c906108c
SS
1332 int name_index;
1333 int i;
39f77062 1334 ptid_t saved_inferior_ptid;
52287340 1335 int done;
c5aa993b 1336
c906108c
SS
1337#ifdef PT_GET_PROCESS_PATHNAME
1338 /* As of 10.x HP-UX, there's an explicit request to get the pathname. */
1339 pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME,
b83266a0
SS
1340 pid,
1341 (PTRACE_ARG3_TYPE) exec_file_buffer,
1342 sizeof (exec_file_buffer) - 1);
c906108c
SS
1343 if (pt_status == 0)
1344 return exec_file_buffer;
1345#endif
1346
1347 /* It appears that this request is broken prior to 10.30.
1348 If it fails, try a really, truly amazingly gross hack
1349 that DDE uses, of pawing through the process' data
1350 segment to find the pathname. */
1351
1352 top_of_stack = 0x7b03a000;
1353 name_index = 0;
1354 done = 0;
1355
39f77062
KB
1356 /* On the chance that pid != inferior_ptid, set inferior_ptid
1357 to pid, so that (grrrr!) implicit uses of inferior_ptid get
c906108c
SS
1358 the right id. */
1359
39f77062
KB
1360 saved_inferior_ptid = inferior_ptid;
1361 inferior_ptid = pid_to_ptid (pid);
c906108c
SS
1362
1363 /* Try to grab a null-terminated string. */
c5aa993b 1364 while (!done)
c906108c
SS
1365 {
1366 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
1367 {
39f77062 1368 inferior_ptid = saved_inferior_ptid;
c906108c
SS
1369 return NULL;
1370 }
1371 for (i = 0; i < 4; i++)
1372 {
1373 exec_file_buffer[name_index++] = four_chars[i];
1374 done = (four_chars[i] == '\0');
1375 if (done)
1376 break;
1377 }
1378 top_of_stack += 4;
1379 }
1380
1381 if (exec_file_buffer[0] == '\0')
1382 {
39f77062 1383 inferior_ptid = saved_inferior_ptid;
c906108c
SS
1384 return NULL;
1385 }
1386
39f77062 1387 inferior_ptid = saved_inferior_ptid;
c906108c
SS
1388 return exec_file_buffer;
1389}
1390
1391void
fba45db2 1392pre_fork_inferior (void)
c906108c
SS
1393{
1394 int status;
1395
1396 status = pipe (startup_semaphore.parent_channel);
1397 if (status < 0)
1398 {
1399 warning ("error getting parent pipe for startup semaphore");
1400 return;
1401 }
1402
1403 status = pipe (startup_semaphore.child_channel);
1404 if (status < 0)
1405 {
1406 warning ("error getting child pipe for startup semaphore");
1407 return;
1408 }
1409}
c906108c 1410\f
c5aa993b 1411
c906108c
SS
1412/* Check to see if the given thread is alive.
1413
1414 This is a no-op, as ptrace doesn't support threads, so we just
1415 return "TRUE". */
1416
1417int
39f77062 1418child_thread_alive (ptid_t ptid)
c906108c 1419{
c5aa993b 1420 return 1;
c906108c
SS
1421}
1422
1423#endif /* ! GDB_NATIVE_HPUX_11 */
This page took 0.344203 seconds and 4 git commands to generate.