* libtool.m4 (LD): Append -melf* option to LD on IRIX with GNU ld.
[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"
c906108c
SS
34#include <signal.h>
35
36extern CORE_ADDR text_end;
37
47932f85
DJ
38extern int hpux_has_forked (int pid, int *childpid);
39extern int hpux_has_vforked (int pid, int *childpid);
40extern int hpux_has_execd (int pid, char **execd_pathname);
41extern int hpux_has_syscall_event (int pid, enum target_waitkind *kind,
42 int *syscall_id);
43
a14ed312 44static void fetch_register (int);
c906108c
SS
45
46void
fba45db2 47fetch_inferior_registers (int regno)
c906108c
SS
48{
49 if (regno == -1)
50 for (regno = 0; regno < NUM_REGS; regno++)
51 fetch_register (regno);
52 else
53 fetch_register (regno);
54}
55
7be570e7
JM
56/* Our own version of the offsetof macro, since we can't assume ANSI C. */
57#define HPPAH_OFFSETOF(type, member) ((int) (&((type *) 0)->member))
58
c906108c
SS
59/* Store our register values back into the inferior.
60 If REGNO is -1, do this for all registers.
61 Otherwise, REGNO specifies which register (so we can save time). */
62
63void
fba45db2 64store_inferior_registers (int regno)
c906108c
SS
65{
66 register unsigned int regaddr;
67 char buf[80];
c906108c
SS
68 register int i;
69 unsigned int offset = U_REGS_OFFSET;
70 int scratch;
71
72 if (regno >= 0)
73 {
7be570e7
JM
74 unsigned int addr, len, offset;
75
c906108c
SS
76 if (CANNOT_STORE_REGISTER (regno))
77 return;
7be570e7
JM
78
79 offset = 0;
80 len = REGISTER_RAW_SIZE (regno);
81
82 /* Requests for register zero actually want the save_state's
83 ss_flags member. As RM says: "Oh, what a hack!" */
84 if (regno == 0)
b83266a0 85 {
7be570e7
JM
86 save_state_t ss;
87 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
88 len = sizeof (ss.ss_flags);
89
90 /* Note that ss_flags is always an int, no matter what
91 REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA machines
92 are big-endian, put it at the least significant end of the
93 value, and zap the rest of the buffer. */
94 offset = REGISTER_RAW_SIZE (0) - len;
95 }
96
97 /* Floating-point registers come from the ss_fpblock area. */
98 else if (regno >= FP0_REGNUM)
99 addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock)
100 + (REGISTER_BYTE (regno) - REGISTER_BYTE (FP0_REGNUM)));
101
102 /* Wide registers come from the ss_wide area.
103 I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
104 between ss_wide and ss_narrow than to use the raw register size.
105 But checking ss_flags would require an extra ptrace call for
106 every register reference. Bleah. */
107 else if (len == 8)
108 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
109 + REGISTER_BYTE (regno));
110
111 /* Narrow registers come from the ss_narrow area. Note that
112 ss_narrow starts with gr1, not gr0. */
113 else if (len == 4)
114 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
115 + (REGISTER_BYTE (regno) - REGISTER_BYTE (1)));
116 else
8e65ff28
AC
117 internal_error (__FILE__, __LINE__,
118 "hppah-nat.c (write_register): unexpected register size");
7be570e7
JM
119
120#ifdef GDB_TARGET_IS_HPPA_20W
121 /* Unbelieveable. The PC head and tail must be written in 64bit hunks
122 or we will get an error. Worse yet, the oddball ptrace/ttrace
123 layering will not allow us to perform a 64bit register store.
124
125 What a crock. */
126 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM && len == 8)
127 {
128 CORE_ADDR temp;
129
524d7c18 130 temp = *(CORE_ADDR *)&deprecated_registers[REGISTER_BYTE (regno)];
7be570e7
JM
131
132 /* Set the priv level (stored in the low two bits of the PC. */
133 temp |= 0x3;
134
39f77062
KB
135 ttrace_write_reg_64 (PIDGET (inferior_ptid), (CORE_ADDR)addr,
136 (CORE_ADDR)&temp);
7be570e7
JM
137
138 /* If we fail to write the PC, give a true error instead of
139 just a warning. */
b83266a0
SS
140 if (errno != 0)
141 {
7be570e7
JM
142 char *err = safe_strerror (errno);
143 char *msg = alloca (strlen (err) + 128);
144 sprintf (msg, "writing `%s' register: %s",
145 REGISTER_NAME (regno), err);
146 perror_with_name (msg);
b83266a0 147 }
7be570e7 148 return;
b83266a0 149 }
53a5351d
JM
150
151 /* Another crock. HPUX complains if you write a nonzero value to
152 the high part of IPSW. What will it take for HP to catch a
153 clue about building sensible interfaces? */
154 if (regno == IPSW_REGNUM && len == 8)
524d7c18 155 *(int *)&deprecated_registers[REGISTER_BYTE (regno)] = 0;
7be570e7
JM
156#endif
157
158 for (i = 0; i < len; i += sizeof (int))
159 {
160 errno = 0;
39f77062
KB
161 call_ptrace (PT_WUREGS, PIDGET (inferior_ptid),
162 (PTRACE_ARG3_TYPE) addr + i,
524d7c18 163 *(int *) &deprecated_registers[REGISTER_BYTE (regno) + i]);
7be570e7
JM
164 if (errno != 0)
165 {
166 /* Warning, not error, in case we are attached; sometimes
167 the kernel doesn't let us at the registers. */
168 char *err = safe_strerror (errno);
169 char *msg = alloca (strlen (err) + 128);
53a5351d 170 sprintf (msg, "writing `%s' register: %s",
7be570e7
JM
171 REGISTER_NAME (regno), err);
172 /* If we fail to write the PC, give a true error instead of
173 just a warning. */
174 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
175 perror_with_name (msg);
176 else
c906108c 177 warning (msg);
7be570e7
JM
178 return;
179 }
180 }
c906108c
SS
181 }
182 else
183 for (regno = 0; regno < NUM_REGS; regno++)
184 store_inferior_registers (regno);
185}
186
c906108c 187
adf40b2e 188/* Fetch a register's value from the process's U area. */
c906108c 189static void
fba45db2 190fetch_register (int regno)
c906108c 191{
6789195b 192 char *buf = alloca (max_register_size (current_gdbarch));
adf40b2e
JM
193 unsigned int addr, len, offset;
194 int i;
c906108c 195
adf40b2e
JM
196 offset = 0;
197 len = REGISTER_RAW_SIZE (regno);
198
199 /* Requests for register zero actually want the save_state's
200 ss_flags member. As RM says: "Oh, what a hack!" */
201 if (regno == 0)
202 {
203 save_state_t ss;
204 addr = HPPAH_OFFSETOF (save_state_t, ss_flags);
205 len = sizeof (ss.ss_flags);
206
207 /* Note that ss_flags is always an int, no matter what
208 REGISTER_RAW_SIZE(0) says. Assuming all HP-UX PA machines
209 are big-endian, put it at the least significant end of the
210 value, and zap the rest of the buffer. */
211 offset = REGISTER_RAW_SIZE (0) - len;
212 memset (buf, 0, sizeof (buf));
213 }
c906108c 214
adf40b2e
JM
215 /* Floating-point registers come from the ss_fpblock area. */
216 else if (regno >= FP0_REGNUM)
217 addr = (HPPAH_OFFSETOF (save_state_t, ss_fpblock)
218 + (REGISTER_BYTE (regno) - REGISTER_BYTE (FP0_REGNUM)));
219
220 /* Wide registers come from the ss_wide area.
221 I think it's more PC to test (ss_flags & SS_WIDEREGS) to select
222 between ss_wide and ss_narrow than to use the raw register size.
223 But checking ss_flags would require an extra ptrace call for
224 every register reference. Bleah. */
225 else if (len == 8)
226 addr = (HPPAH_OFFSETOF (save_state_t, ss_wide)
227 + REGISTER_BYTE (regno));
228
229 /* Narrow registers come from the ss_narrow area. Note that
230 ss_narrow starts with gr1, not gr0. */
231 else if (len == 4)
232 addr = (HPPAH_OFFSETOF (save_state_t, ss_narrow)
233 + (REGISTER_BYTE (regno) - REGISTER_BYTE (1)));
c906108c 234
adf40b2e 235 else
8e65ff28
AC
236 internal_error (__FILE__, __LINE__,
237 "hppa-nat.c (fetch_register): unexpected register size");
adf40b2e
JM
238
239 for (i = 0; i < len; i += sizeof (int))
c906108c
SS
240 {
241 errno = 0;
adf40b2e
JM
242 /* Copy an int from the U area to buf. Fill the least
243 significant end if len != raw_size. */
244 * (int *) &buf[offset + i] =
39f77062 245 call_ptrace (PT_RUREGS, PIDGET (inferior_ptid),
adf40b2e 246 (PTRACE_ARG3_TYPE) addr + i, 0);
c906108c
SS
247 if (errno != 0)
248 {
adf40b2e
JM
249 /* Warning, not error, in case we are attached; sometimes
250 the kernel doesn't let us at the registers. */
c906108c
SS
251 char *err = safe_strerror (errno);
252 char *msg = alloca (strlen (err) + 128);
adf40b2e
JM
253 sprintf (msg, "reading `%s' register: %s",
254 REGISTER_NAME (regno), err);
c906108c 255 warning (msg);
adf40b2e 256 return;
c906108c
SS
257 }
258 }
adf40b2e
JM
259
260 /* If we're reading an address from the instruction address queue,
261 mask out the bottom two bits --- they contain the privilege
262 level. */
c906108c 263 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
adf40b2e
JM
264 buf[len - 1] &= ~0x3;
265
c906108c 266 supply_register (regno, buf);
c906108c
SS
267}
268
adf40b2e 269
c906108c
SS
270/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
271 to debugger memory starting at MYADDR. Copy to inferior if
272 WRITE is nonzero.
c5aa993b 273
c906108c
SS
274 Returns the length copied, which is either the LEN argument or zero.
275 This xfer function does not do partial moves, since child_ops
276 doesn't allow memory operations to cross below us in the target stack
8fef05cc 277 anyway. TARGET is ignored. */
c906108c
SS
278
279int
8fef05cc 280child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
240be855 281 struct mem_attrib *mem,
8fef05cc 282 struct target_ops *target)
c906108c
SS
283{
284 register int i;
285 /* Round starting address down to longword boundary. */
a0b3c4fd 286 register CORE_ADDR addr = memaddr & - (CORE_ADDR)(sizeof (int));
c906108c
SS
287 /* Round ending address up; get number of longwords that makes. */
288 register int count
c5aa993b 289 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
c906108c 290
b83266a0
SS
291 /* Allocate buffer of that many longwords.
292 Note -- do not use alloca to allocate this buffer since there is no
293 guarantee of when the buffer will actually be deallocated.
294
295 This routine can be called over and over with the same call chain;
296 this (in effect) would pile up all those alloca requests until a call
297 to alloca was made from a point higher than this routine in the
298 call chain. */
c906108c
SS
299 register int *buffer = (int *) xmalloc (count * sizeof (int));
300
301 if (write)
302 {
303 /* Fill start and end extra bytes of buffer with existing memory data. */
c5aa993b 304 if (addr != memaddr || len < (int) sizeof (int))
b83266a0
SS
305 {
306 /* Need part of initial word -- fetch it. */
c5aa993b 307 buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
39f77062
KB
308 PIDGET (inferior_ptid),
309 (PTRACE_ARG3_TYPE) addr, 0);
b83266a0 310 }
c906108c
SS
311
312 if (count > 1) /* FIXME, avoid if even boundary */
313 {
314 buffer[count - 1]
b83266a0 315 = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
39f77062 316 PIDGET (inferior_ptid),
b83266a0
SS
317 (PTRACE_ARG3_TYPE) (addr
318 + (count - 1) * sizeof (int)),
319 0);
c906108c
SS
320 }
321
322 /* Copy data to be written over corresponding part of buffer */
c906108c
SS
323 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
324
325 /* Write the entire buffer. */
c906108c
SS
326 for (i = 0; i < count; i++, addr += sizeof (int))
327 {
b83266a0
SS
328 int pt_status;
329 int pt_request;
330 /* The HP-UX kernel crashes if you use PT_WDUSER to write into the
331 text segment. FIXME -- does it work to write into the data
332 segment using WIUSER, or do these idiots really expect us to
333 figure out which segment the address is in, so we can use a
334 separate system call for it??! */
c906108c 335 errno = 0;
b83266a0 336 pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER;
c906108c 337 pt_status = call_ptrace (pt_request,
39f77062 338 PIDGET (inferior_ptid),
b83266a0
SS
339 (PTRACE_ARG3_TYPE) addr,
340 buffer[i]);
341
342 /* Did we fail? Might we've guessed wrong about which
343 segment this address resides in? Try the other request,
344 and see if that works... */
345 if ((pt_status == -1) && errno)
346 {
347 errno = 0;
348 pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER;
349 pt_status = call_ptrace (pt_request,
39f77062 350 PIDGET (inferior_ptid),
b83266a0
SS
351 (PTRACE_ARG3_TYPE) addr,
352 buffer[i]);
353
354 /* No, we still fail. Okay, time to punt. */
355 if ((pt_status == -1) && errno)
356 {
b8c9b27d 357 xfree (buffer);
b83266a0
SS
358 return 0;
359 }
360 }
c906108c
SS
361 }
362 }
363 else
364 {
365 /* Read all the longwords */
366 for (i = 0; i < count; i++, addr += sizeof (int))
367 {
368 errno = 0;
c5aa993b 369 buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
39f77062
KB
370 PIDGET (inferior_ptid),
371 (PTRACE_ARG3_TYPE) addr, 0);
b83266a0
SS
372 if (errno)
373 {
b8c9b27d 374 xfree (buffer);
b83266a0
SS
375 return 0;
376 }
c906108c
SS
377 QUIT;
378 }
379
380 /* Copy appropriate bytes out of the buffer. */
381 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
382 }
b8c9b27d 383 xfree (buffer);
c906108c
SS
384 return len;
385}
386
7d2830a3 387char *saved_child_execd_pathname = NULL;
6604731b 388int saved_vfork_pid;
7d2830a3
DJ
389enum {
390 STATE_NONE,
391 STATE_GOT_CHILD,
392 STATE_GOT_EXEC,
393 STATE_GOT_PARENT,
394 STATE_FAKE_EXEC
395} saved_vfork_state = STATE_NONE;
c906108c 396
6604731b
DJ
397int
398child_follow_fork (int follow_child)
c906108c 399{
6604731b
DJ
400 ptid_t last_ptid;
401 struct target_waitstatus last_status;
402 int has_vforked;
403 int parent_pid, child_pid;
404
405 get_last_target_status (&last_ptid, &last_status);
406 has_vforked = (last_status.kind == TARGET_WAITKIND_VFORKED);
407 parent_pid = ptid_get_pid (last_ptid);
408 child_pid = last_status.value.related_pid;
409
410 /* At this point, if we are vforking, breakpoints were already
411 detached from the child in child_wait; and the child has already
412 called execve(). If we are forking, both the parent and child
413 have breakpoints inserted. */
414
415 if (! follow_child)
c906108c 416 {
6604731b
DJ
417 if (! has_vforked)
418 {
419 detach_breakpoints (child_pid);
420#ifdef SOLIB_REMOVE_INFERIOR_HOOK
421 SOLIB_REMOVE_INFERIOR_HOOK (child_pid);
422#endif
423 }
424
425 /* Detach from the child. */
4c9ba7e0
DJ
426 printf_unfiltered ("Detaching after fork from %s\n",
427 target_pid_to_str (pid_to_ptid (child_pid)));
428 hppa_require_detach (child_pid, 0);
6604731b
DJ
429
430 /* The parent and child of a vfork share the same address space.
431 Also, on some targets the order in which vfork and exec events
432 are received for parent in child requires some delicate handling
433 of the events.
434
435 For instance, on ptrace-based HPUX we receive the child's vfork
436 event first, at which time the parent has been suspended by the
437 OS and is essentially untouchable until the child's exit or second
438 exec event arrives. At that time, the parent's vfork event is
439 delivered to us, and that's when we see and decide how to follow
440 the vfork. But to get to that point, we must continue the child
441 until it execs or exits. To do that smoothly, all breakpoints
442 must be removed from the child, in case there are any set between
443 the vfork() and exec() calls. But removing them from the child
444 also removes them from the parent, due to the shared-address-space
445 nature of a vfork'd parent and child. On HPUX, therefore, we must
446 take care to restore the bp's to the parent before we continue it.
447 Else, it's likely that we may not stop in the expected place. (The
448 worst scenario is when the user tries to step over a vfork() call;
449 the step-resume bp must be restored for the step to properly stop
450 in the parent after the call completes!)
451
452 Sequence of events, as reported to gdb from HPUX:
453
454 Parent Child Action for gdb to take
455 -------------------------------------------------------
456 1 VFORK Continue child
457 2 EXEC
458 3 EXEC or EXIT
459 4 VFORK
460
461 Now that the child has safely exec'd or exited, we must restore
462 the parent's breakpoints before we continue it. Else, we may
463 cause it run past expected stopping points. */
464
465 if (has_vforked)
466 reattach_breakpoints (parent_pid);
c906108c 467 }
6604731b
DJ
468 else
469 {
6604731b
DJ
470 /* Needed to keep the breakpoint lists in sync. */
471 if (! has_vforked)
472 detach_breakpoints (child_pid);
7d2830a3 473
6604731b
DJ
474 /* Before detaching from the parent, remove all breakpoints from it. */
475 remove_breakpoints ();
476
477 /* Also reset the solib inferior hook from the parent. */
478#ifdef SOLIB_REMOVE_INFERIOR_HOOK
479 SOLIB_REMOVE_INFERIOR_HOOK (PIDGET (inferior_ptid));
480#endif
7d2830a3 481
6604731b
DJ
482 /* Detach from the parent. */
483 target_detach (NULL, 1);
484
485 /* Attach to the child. */
4c9ba7e0
DJ
486 printf_unfiltered ("Attaching after fork to %s\n",
487 target_pid_to_str (pid_to_ptid (child_pid)));
488 hppa_require_attach (child_pid);
6604731b 489 inferior_ptid = pid_to_ptid (child_pid);
6604731b
DJ
490
491 /* If we vforked, then we've also execed by now. The exec will be
492 reported momentarily. follow_exec () will handle breakpoints, so
493 we don't have to.. */
494 if (!has_vforked)
495 follow_inferior_reset_breakpoints ();
496 }
497
498 if (has_vforked)
c906108c 499 {
6604731b
DJ
500 /* If we followed the parent, don't try to follow the child's exec. */
501 if (saved_vfork_state != STATE_GOT_PARENT
502 && saved_vfork_state != STATE_FAKE_EXEC)
503 fprintf_unfiltered (gdb_stdout,
504 "hppa: post follow vfork: confused state\n");
505
506 if (! follow_child || saved_vfork_state == STATE_GOT_PARENT)
507 saved_vfork_state = STATE_NONE;
508 else
509 return 1;
c906108c 510 }
6604731b 511 return 0;
c906108c
SS
512}
513
b83266a0
SS
514/* Format a process id, given PID. Be sure to terminate
515 this with a null--it's going to be printed via a "%s". */
c906108c 516char *
39f77062 517child_pid_to_str (ptid_t ptid)
c906108c 518{
c5aa993b
JM
519 /* Static because address returned */
520 static char buf[30];
39f77062 521 pid_t pid = PIDGET (ptid);
c906108c 522
ce414844
AC
523 /* Extra NUL for paranoia's sake */
524 sprintf (buf, "process %d%c", pid, '\0');
c5aa993b
JM
525
526 return buf;
c906108c
SS
527}
528
b83266a0
SS
529/* Format a thread id, given TID. Be sure to terminate
530 this with a null--it's going to be printed via a "%s".
531
532 Note: This is a core-gdb tid, not the actual system tid.
c5aa993b 533 See infttrace.c for details. */
c906108c 534char *
39f77062 535hppa_tid_to_str (ptid_t ptid)
c906108c 536{
c5aa993b
JM
537 /* Static because address returned */
538 static char buf[30];
39f77062
KB
539 /* This seems strange, but when I did the ptid conversion, it looked
540 as though a pid was always being passed. - Kevin Buettner */
541 pid_t tid = PIDGET (ptid);
c5aa993b
JM
542
543 /* Extra NULLs for paranoia's sake */
ce414844 544 sprintf (buf, "system thread %d%c", tid, '\0');
c906108c 545
c5aa993b 546 return buf;
c906108c
SS
547}
548
47932f85
DJ
549/*## */
550/* Enable HACK for ttrace work. In
551 * infttrace.c/require_notification_of_events,
552 * this is set to 0 so that the loop in child_wait
553 * won't loop.
554 */
555int not_same_real_pid = 1;
556/*## */
557
47932f85
DJ
558/* Wait for child to do something. Return pid of child, or -1 in case
559 of error; store status through argument pointer OURSTATUS. */
560
561ptid_t
562child_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
563{
564 int save_errno;
565 int status;
566 char *execd_pathname = NULL;
567 int exit_status;
568 int related_pid;
569 int syscall_id;
570 enum target_waitkind kind;
571 int pid;
572
7d2830a3
DJ
573 if (saved_vfork_state == STATE_FAKE_EXEC)
574 {
575 saved_vfork_state = STATE_NONE;
576 ourstatus->kind = TARGET_WAITKIND_EXECD;
577 ourstatus->value.execd_pathname = saved_child_execd_pathname;
578 return inferior_ptid;
579 }
580
47932f85
DJ
581 do
582 {
583 set_sigint_trap (); /* Causes SIGINT to be passed on to the
584 attached process. */
585 set_sigio_trap ();
586
587 pid = ptrace_wait (inferior_ptid, &status);
588
589 save_errno = errno;
590
591 clear_sigio_trap ();
592
593 clear_sigint_trap ();
594
595 if (pid == -1)
596 {
597 if (save_errno == EINTR)
598 continue;
599
600 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
601 safe_strerror (save_errno));
602
603 /* Claim it exited with unknown signal. */
604 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
605 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
606 return pid_to_ptid (-1);
607 }
608
609 /* Did it exit?
610 */
611 if (target_has_exited (pid, status, &exit_status))
612 {
613 /* ??rehrauer: For now, ignore this. */
614 continue;
615 }
616
617 if (!target_thread_alive (pid_to_ptid (pid)))
618 {
619 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
620 return pid_to_ptid (pid);
621 }
622
8e7d2c16 623 if (hpux_has_forked (pid, &related_pid))
47932f85 624 {
8e7d2c16
DJ
625 /* Ignore the parent's fork event. */
626 if (pid == PIDGET (inferior_ptid))
627 {
628 ourstatus->kind = TARGET_WAITKIND_IGNORE;
629 return inferior_ptid;
630 }
631
632 /* If this is the child's fork event, report that the
633 process has forked. */
634 if (related_pid == PIDGET (inferior_ptid))
635 {
636 ourstatus->kind = TARGET_WAITKIND_FORKED;
637 ourstatus->value.related_pid = pid;
638 return inferior_ptid;
639 }
47932f85
DJ
640 }
641
7d2830a3 642 if (hpux_has_vforked (pid, &related_pid))
47932f85 643 {
7d2830a3
DJ
644 if (pid == PIDGET (inferior_ptid))
645 {
646 if (saved_vfork_state == STATE_GOT_CHILD)
647 saved_vfork_state = STATE_GOT_PARENT;
648 else if (saved_vfork_state == STATE_GOT_EXEC)
649 saved_vfork_state = STATE_FAKE_EXEC;
650 else
651 fprintf_unfiltered (gdb_stdout,
652 "hppah: parent vfork: confused\n");
653 }
654 else if (related_pid == PIDGET (inferior_ptid))
655 {
656 if (saved_vfork_state == STATE_NONE)
657 saved_vfork_state = STATE_GOT_CHILD;
658 else
659 fprintf_unfiltered (gdb_stdout,
660 "hppah: child vfork: confused\n");
661 }
662 else
663 fprintf_unfiltered (gdb_stdout,
664 "hppah: unknown vfork: confused\n");
665
666 if (saved_vfork_state == STATE_GOT_CHILD)
667 {
668 child_post_startup_inferior (pid_to_ptid (pid));
6604731b
DJ
669 detach_breakpoints (pid);
670#ifdef SOLIB_REMOVE_INFERIOR_HOOK
671 SOLIB_REMOVE_INFERIOR_HOOK (pid);
672#endif
673 child_resume (pid_to_ptid (pid), 0, TARGET_SIGNAL_0);
674 ourstatus->kind = TARGET_WAITKIND_IGNORE;
675 return pid_to_ptid (related_pid);
7d2830a3 676 }
6604731b 677 else if (saved_vfork_state == STATE_FAKE_EXEC)
7d2830a3
DJ
678 {
679 ourstatus->kind = TARGET_WAITKIND_VFORKED;
680 ourstatus->value.related_pid = related_pid;
681 return pid_to_ptid (pid);
682 }
6604731b
DJ
683 else
684 {
685 /* We saw the parent's vfork, but we haven't seen the exec yet.
686 Wait for it, for simplicity's sake. It should be pending. */
687 saved_vfork_pid = related_pid;
688 ourstatus->kind = TARGET_WAITKIND_IGNORE;
689 return pid_to_ptid (pid);
690 }
47932f85
DJ
691 }
692
693 if (hpux_has_execd (pid, &execd_pathname))
694 {
7d2830a3
DJ
695 /* On HP-UX, events associated with a vforking inferior come in
696 threes: a vfork event for the child (always first), followed
697 a vfork event for the parent and an exec event for the child.
6604731b
DJ
698 The latter two can come in either order. Make sure we get
699 both. */
700 if (saved_vfork_state != STATE_NONE)
7d2830a3 701 {
6604731b
DJ
702 if (saved_vfork_state == STATE_GOT_CHILD)
703 {
704 saved_vfork_state = STATE_GOT_EXEC;
705 /* On HP/UX with ptrace, the child must be resumed before
706 the parent vfork event is delivered. A single-step
707 suffices. */
708 if (RESUME_EXECD_VFORKING_CHILD_TO_GET_PARENT_VFORK ())
709 target_resume (pid_to_ptid (pid), 1, TARGET_SIGNAL_0);
710 ourstatus->kind = TARGET_WAITKIND_IGNORE;
711 }
712 else if (saved_vfork_state == STATE_GOT_PARENT)
713 {
714 saved_vfork_state = STATE_FAKE_EXEC;
715 ourstatus->kind = TARGET_WAITKIND_VFORKED;
716 ourstatus->value.related_pid = saved_vfork_pid;
717 }
718 else
719 fprintf_unfiltered (gdb_stdout,
720 "hppa: exec: unexpected state\n");
7d2830a3 721
6604731b 722 saved_child_execd_pathname = execd_pathname;
7d2830a3 723
7d2830a3
DJ
724 return inferior_ptid;
725 }
726
47932f85
DJ
727 /* Are we ignoring initial exec events? (This is likely because
728 we're in the process of starting up the inferior, and another
729 (older) mechanism handles those.) If so, we'll report this
730 as a regular stop, not an exec.
731 */
732 if (inferior_ignoring_startup_exec_events)
733 {
734 inferior_ignoring_startup_exec_events--;
735 }
736 else
737 {
738 ourstatus->kind = TARGET_WAITKIND_EXECD;
739 ourstatus->value.execd_pathname = execd_pathname;
740 return pid_to_ptid (pid);
741 }
742 }
743
744 /* All we must do with these is communicate their occurrence
745 to wait_for_inferior...
746 */
747 if (hpux_has_syscall_event (pid, &kind, &syscall_id))
748 {
749 ourstatus->kind = kind;
750 ourstatus->value.syscall_id = syscall_id;
751 return pid_to_ptid (pid);
752 }
753
754 /*## } while (pid != PIDGET (inferior_ptid)); ## *//* Some other child died or stopped */
755/* hack for thread testing */
756 }
757 while ((pid != PIDGET (inferior_ptid)) && not_same_real_pid);
758/*## */
759
760 store_waitstatus (ourstatus, status);
761 return pid_to_ptid (pid);
762}
763
c906108c
SS
764#if !defined (GDB_NATIVE_HPUX_11)
765
766/* The following code is a substitute for the infttrace.c versions used
767 with ttrace() in HPUX 11. */
768
769/* This value is an arbitrary integer. */
770#define PT_VERSION 123456
771
772/* This semaphore is used to coordinate the child and parent processes
773 after a fork(), and before an exec() by the child. See
774 parent_attach_all for details. */
775
c5aa993b
JM
776typedef struct
777{
778 int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */
779 int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */
780}
781startup_semaphore_t;
c906108c
SS
782
783#define SEM_TALK (1)
784#define SEM_LISTEN (0)
785
c5aa993b 786static startup_semaphore_t startup_semaphore;
c906108c 787
a14ed312 788extern int parent_attach_all (int, PTRACE_ARG3_TYPE, int);
c906108c
SS
789
790#ifdef PT_SETTRC
791/* This function causes the caller's process to be traced by its
792 parent. This is intended to be called after GDB forks itself,
793 and before the child execs the target.
794
795 Note that HP-UX ptrace is rather funky in how this is done.
796 If the parent wants to get the initial exec event of a child,
797 it must set the ptrace event mask of the child to include execs.
798 (The child cannot do this itself.) This must be done after the
799 child is forked, but before it execs.
800
801 To coordinate the parent and child, we implement a semaphore using
802 pipes. After SETTRC'ing itself, the child tells the parent that
803 it is now traceable by the parent, and waits for the parent's
804 acknowledgement. The parent can then set the child's event mask,
805 and notify the child that it can now exec.
806
807 (The acknowledgement by parent happens as a result of a call to
808 child_acknowledge_created_inferior.) */
809
810int
fba45db2 811parent_attach_all (int pid, PTRACE_ARG3_TYPE addr, int data)
c906108c
SS
812{
813 int pt_status = 0;
814
815 /* We need a memory home for a constant. */
816 int tc_magic_child = PT_VERSION;
817 int tc_magic_parent = 0;
818
819 /* The remainder of this function is only useful for HPUX 10.0 and
820 later, as it depends upon the ability to request notification
821 of specific kinds of events by the kernel. */
822#if defined(PT_SET_EVENT_MASK)
823
824 /* Notify the parent that we're potentially ready to exec(). */
825 write (startup_semaphore.child_channel[SEM_TALK],
b83266a0
SS
826 &tc_magic_child,
827 sizeof (tc_magic_child));
c906108c
SS
828
829 /* Wait for acknowledgement from the parent. */
830 read (startup_semaphore.parent_channel[SEM_LISTEN],
b83266a0
SS
831 &tc_magic_parent,
832 sizeof (tc_magic_parent));
c906108c 833 if (tc_magic_child != tc_magic_parent)
c5aa993b 834 warning ("mismatched semaphore magic");
c906108c
SS
835
836 /* Discard our copy of the semaphore. */
837 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
838 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
839 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
840 (void) close (startup_semaphore.child_channel[SEM_TALK]);
841#endif
c5aa993b 842
c906108c
SS
843 return 0;
844}
845#endif
846
847int
fba45db2 848hppa_require_attach (int pid)
c906108c
SS
849{
850 int pt_status;
b83266a0
SS
851 CORE_ADDR pc;
852 CORE_ADDR pc_addr;
c906108c
SS
853 unsigned int regs_offset;
854
855 /* Are we already attached? There appears to be no explicit way to
856 answer this via ptrace, so we try something which should be
857 innocuous if we are attached. If that fails, then we assume
858 we're not attached, and so attempt to make it so. */
859
860 errno = 0;
861 regs_offset = U_REGS_OFFSET;
862 pc_addr = register_addr (PC_REGNUM, regs_offset);
863 pc = call_ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE) pc_addr, 0);
864
865 if (errno)
866 {
867 errno = 0;
868 pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
869
870 if (errno)
b83266a0 871 return -1;
c906108c
SS
872
873 /* Now we really are attached. */
874 errno = 0;
875 }
876 attach_flag = 1;
877 return pid;
878}
879
880int
fba45db2 881hppa_require_detach (int pid, int signal)
c906108c
SS
882{
883 errno = 0;
884 call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal);
c5aa993b 885 errno = 0; /* Ignore any errors. */
c906108c
SS
886 return pid;
887}
888
889/* Since ptrace doesn't support memory page-protection events, which
890 are used to implement "hardware" watchpoints on HP-UX, these are
891 dummy versions, which perform no useful work. */
892
893void
fba45db2 894hppa_enable_page_protection_events (int pid)
c906108c
SS
895{
896}
897
898void
fba45db2 899hppa_disable_page_protection_events (int pid)
c906108c
SS
900{
901}
902
903int
fba45db2 904hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
c906108c
SS
905{
906 error ("Hardware watchpoints not implemented on this platform.");
907}
908
909int
fba45db2
KB
910hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len,
911 enum bptype type)
c906108c
SS
912{
913 error ("Hardware watchpoints not implemented on this platform.");
914}
915
916int
fba45db2 917hppa_can_use_hw_watchpoint (enum bptype type, int cnt, enum bptype ot)
c906108c
SS
918{
919 return 0;
920}
921
922int
fba45db2 923hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
c906108c
SS
924{
925 error ("Hardware watchpoints not implemented on this platform.");
926}
927
928char *
39f77062 929hppa_pid_or_tid_to_str (ptid_t id)
c906108c
SS
930{
931 /* In the ptrace world, there are only processes. */
ed9a39eb 932 return child_pid_to_str (id);
c906108c
SS
933}
934
935/* This function has no meaning in a non-threaded world. Thus, we
936 return 0 (FALSE). See the use of "hppa_prepare_to_proceed" in
937 hppa-tdep.c. */
938
939pid_t
fba45db2 940hppa_switched_threads (pid_t pid)
c906108c
SS
941{
942 return (pid_t) 0;
943}
944
945void
fba45db2 946hppa_ensure_vforking_parent_remains_stopped (int pid)
c906108c
SS
947{
948 /* This assumes that the vforked parent is presently stopped, and
949 that the vforked child has just delivered its first exec event.
950 Calling kill() this way will cause the SIGTRAP to be delivered as
951 soon as the parent is resumed, which happens as soon as the
952 vforked child is resumed. See wait_for_inferior for the use of
953 this function. */
954 kill (pid, SIGTRAP);
955}
956
957int
fba45db2 958hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
c906108c 959{
c5aa993b 960 return 1; /* Yes, the child must be resumed. */
c906108c
SS
961}
962
963void
fba45db2 964require_notification_of_events (int pid)
c906108c
SS
965{
966#if defined(PT_SET_EVENT_MASK)
967 int pt_status;
968 ptrace_event_t ptrace_events;
c2d11a7d
JM
969 int nsigs;
970 int signum;
c906108c
SS
971
972 /* Instruct the kernel as to the set of events we wish to be
973 informed of. (This support does not exist before HPUX 10.0.
974 We'll assume if PT_SET_EVENT_MASK has not been defined by
b83266a0 975 <sys/ptrace.h>, then we're being built on pre-10.0.) */
c906108c
SS
976 memset (&ptrace_events, 0, sizeof (ptrace_events));
977
978 /* Note: By default, all signals are visible to us. If we wish
979 the kernel to keep certain signals hidden from us, we do it
980 by calling sigdelset (ptrace_events.pe_signals, signal) for
b83266a0 981 each such signal here, before doing PT_SET_EVENT_MASK. */
c2d11a7d
JM
982 /* RM: The above comment is no longer true. We start with ignoring
983 all signals, and then add the ones we are interested in. We could
984 do it the other way: start by looking at all signals and then
985 deleting the ones that we aren't interested in, except that
986 multiple gdb signals may be mapped to the same host signal
987 (eg. TARGET_SIGNAL_IO and TARGET_SIGNAL_POLL both get mapped to
988 signal 22 on HPUX 10.20) We want to be notified if we are
989 interested in either signal. */
990 sigfillset (&ptrace_events.pe_signals);
991
992 /* RM: Let's not bother with signals we don't care about */
993 nsigs = (int) TARGET_SIGNAL_LAST;
994 for (signum = nsigs; signum > 0; signum--)
995 {
996 if ((signal_stop_state (signum)) ||
997 (signal_print_state (signum)) ||
998 (!signal_pass_state (signum)))
999 {
1000 if (target_signal_to_host_p (signum))
1001 sigdelset (&ptrace_events.pe_signals,
1002 target_signal_to_host (signum));
1003 }
1004 }
c906108c
SS
1005
1006 ptrace_events.pe_set_event = 0;
1007
1008 ptrace_events.pe_set_event |= PTRACE_SIGNAL;
1009 ptrace_events.pe_set_event |= PTRACE_EXEC;
1010 ptrace_events.pe_set_event |= PTRACE_FORK;
1011 ptrace_events.pe_set_event |= PTRACE_VFORK;
1012 /* ??rehrauer: Add this one when we're prepared to catch it...
c5aa993b
JM
1013 ptrace_events.pe_set_event |= PTRACE_EXIT;
1014 */
c906108c
SS
1015
1016 errno = 0;
1017 pt_status = call_ptrace (PT_SET_EVENT_MASK,
c5aa993b
JM
1018 pid,
1019 (PTRACE_ARG3_TYPE) & ptrace_events,
1020 sizeof (ptrace_events));
c906108c
SS
1021 if (errno)
1022 perror_with_name ("ptrace");
1023 if (pt_status < 0)
1024 return;
1025#endif
1026}
1027
1028void
fba45db2 1029require_notification_of_exec_events (int pid)
c906108c
SS
1030{
1031#if defined(PT_SET_EVENT_MASK)
1032 int pt_status;
1033 ptrace_event_t ptrace_events;
1034
1035 /* Instruct the kernel as to the set of events we wish to be
1036 informed of. (This support does not exist before HPUX 10.0.
1037 We'll assume if PT_SET_EVENT_MASK has not been defined by
b83266a0 1038 <sys/ptrace.h>, then we're being built on pre-10.0.) */
c906108c
SS
1039 memset (&ptrace_events, 0, sizeof (ptrace_events));
1040
1041 /* Note: By default, all signals are visible to us. If we wish
1042 the kernel to keep certain signals hidden from us, we do it
1043 by calling sigdelset (ptrace_events.pe_signals, signal) for
b83266a0 1044 each such signal here, before doing PT_SET_EVENT_MASK. */
c906108c
SS
1045 sigemptyset (&ptrace_events.pe_signals);
1046
1047 ptrace_events.pe_set_event = 0;
1048
1049 ptrace_events.pe_set_event |= PTRACE_EXEC;
1050 /* ??rehrauer: Add this one when we're prepared to catch it...
c5aa993b
JM
1051 ptrace_events.pe_set_event |= PTRACE_EXIT;
1052 */
c906108c
SS
1053
1054 errno = 0;
1055 pt_status = call_ptrace (PT_SET_EVENT_MASK,
c5aa993b
JM
1056 pid,
1057 (PTRACE_ARG3_TYPE) & ptrace_events,
1058 sizeof (ptrace_events));
c906108c
SS
1059 if (errno)
1060 perror_with_name ("ptrace");
1061 if (pt_status < 0)
1062 return;
1063#endif
1064}
1065
1066/* This function is called by the parent process, with pid being the
1067 ID of the child process, after the debugger has forked. */
1068
1069void
fba45db2 1070child_acknowledge_created_inferior (int pid)
c906108c
SS
1071{
1072 /* We need a memory home for a constant. */
1073 int tc_magic_parent = PT_VERSION;
1074 int tc_magic_child = 0;
1075
b83266a0
SS
1076 /* The remainder of this function is only useful for HPUX 10.0 and
1077 later, as it depends upon the ability to request notification
1078 of specific kinds of events by the kernel. */
1079#if defined(PT_SET_EVENT_MASK)
c906108c
SS
1080 /* Wait for the child to tell us that it has forked. */
1081 read (startup_semaphore.child_channel[SEM_LISTEN],
b83266a0 1082 &tc_magic_child,
c5aa993b 1083 sizeof (tc_magic_child));
c906108c
SS
1084
1085 /* Notify the child that it can exec.
1086
1087 In the infttrace.c variant of this function, we set the child's
1088 event mask after the fork but before the exec. In the ptrace
1089 world, it seems we can't set the event mask until after the exec. */
c906108c 1090 write (startup_semaphore.parent_channel[SEM_TALK],
b83266a0
SS
1091 &tc_magic_parent,
1092 sizeof (tc_magic_parent));
c906108c
SS
1093
1094 /* We'd better pause a bit before trying to set the event mask,
1095 though, to ensure that the exec has happened. We don't want to
1096 wait() on the child, because that'll screw up the upper layers
1097 of gdb's execution control that expect to see the exec event.
1098
1099 After an exec, the child is no longer executing gdb code. Hence,
1100 we can't have yet another synchronization via the pipes. We'll
1101 just sleep for a second, and hope that's enough delay... */
c906108c
SS
1102 sleep (1);
1103
1104 /* Instruct the kernel as to the set of events we wish to be
1105 informed of. */
c906108c
SS
1106 require_notification_of_exec_events (pid);
1107
1108 /* Discard our copy of the semaphore. */
1109 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
1110 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
1111 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
1112 (void) close (startup_semaphore.child_channel[SEM_TALK]);
b83266a0 1113#endif
c906108c
SS
1114}
1115
1116void
39f77062 1117child_post_startup_inferior (ptid_t ptid)
c906108c 1118{
39f77062 1119 require_notification_of_events (PIDGET (ptid));
c906108c
SS
1120}
1121
1122void
fba45db2 1123child_post_attach (int pid)
c906108c
SS
1124{
1125 require_notification_of_events (pid);
1126}
1127
1128int
fba45db2 1129child_insert_fork_catchpoint (int pid)
c906108c
SS
1130{
1131 /* This request is only available on HPUX 10.0 and later. */
1132#if !defined(PT_SET_EVENT_MASK)
1133 error ("Unable to catch forks prior to HPUX 10.0");
1134#else
1135 /* Enable reporting of fork events from the kernel. */
1136 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 1137 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
1138 return 0;
1139#endif
1140}
1141
1142int
fba45db2 1143child_remove_fork_catchpoint (int pid)
c906108c
SS
1144{
1145 /* This request is only available on HPUX 10.0 and later. */
1146#if !defined(PT_SET_EVENT_MASK)
1147 error ("Unable to catch forks prior to HPUX 10.0");
1148#else
1149 /* Disable reporting of fork events from the kernel. */
1150 /* ??rehrauer: For the moment, we're always enabling these events,
1151 and just ignoring them if there's no catchpoint to catch them. */
1152 return 0;
1153#endif
1154}
1155
1156int
fba45db2 1157child_insert_vfork_catchpoint (int pid)
c906108c
SS
1158{
1159 /* This request is only available on HPUX 10.0 and later. */
1160#if !defined(PT_SET_EVENT_MASK)
1161 error ("Unable to catch vforks prior to HPUX 10.0");
1162#else
1163 /* Enable reporting of vfork events from the kernel. */
1164 /* ??rehrauer: For the moment, we're always enabling these events,
1165 and just ignoring them if there's no catchpoint to catch them. */
1166 return 0;
1167#endif
1168}
1169
1170int
fba45db2 1171child_remove_vfork_catchpoint (int pid)
c906108c
SS
1172{
1173 /* This request is only available on HPUX 10.0 and later. */
1174#if !defined(PT_SET_EVENT_MASK)
1175 error ("Unable to catch vforks prior to HPUX 10.0");
1176#else
1177 /* Disable reporting of vfork events from the kernel. */
1178 /* ??rehrauer: For the moment, we're always enabling these events,
1179 and just ignoring them if there's no catchpoint to catch them. */
1180 return 0;
1181#endif
1182}
1183
1184int
47932f85 1185hpux_has_forked (int pid, int *childpid)
c906108c
SS
1186{
1187 /* This request is only available on HPUX 10.0 and later. */
1188#if !defined(PT_GET_PROCESS_STATE)
1189 *childpid = 0;
1190 return 0;
1191#else
1192 int pt_status;
c5aa993b 1193 ptrace_state_t ptrace_state;
c906108c
SS
1194
1195 errno = 0;
1196 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 1197 pid,
c5aa993b 1198 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 1199 sizeof (ptrace_state));
c906108c
SS
1200 if (errno)
1201 perror_with_name ("ptrace");
1202 if (pt_status < 0)
1203 return 0;
1204
1205 if (ptrace_state.pe_report_event & PTRACE_FORK)
1206 {
1207 *childpid = ptrace_state.pe_other_pid;
1208 return 1;
1209 }
1210
1211 return 0;
1212#endif
1213}
1214
1215int
47932f85 1216hpux_has_vforked (int pid, int *childpid)
c906108c
SS
1217{
1218 /* This request is only available on HPUX 10.0 and later. */
1219#if !defined(PT_GET_PROCESS_STATE)
1220 *childpid = 0;
1221 return 0;
1222
1223#else
1224 int pt_status;
c5aa993b 1225 ptrace_state_t ptrace_state;
c906108c
SS
1226
1227 errno = 0;
1228 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 1229 pid,
c5aa993b 1230 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 1231 sizeof (ptrace_state));
c906108c
SS
1232 if (errno)
1233 perror_with_name ("ptrace");
1234 if (pt_status < 0)
1235 return 0;
1236
1237 if (ptrace_state.pe_report_event & PTRACE_VFORK)
1238 {
1239 *childpid = ptrace_state.pe_other_pid;
1240 return 1;
1241 }
1242
1243 return 0;
1244#endif
1245}
1246
c906108c 1247int
fba45db2 1248child_insert_exec_catchpoint (int pid)
c906108c 1249{
b83266a0 1250 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
1251#if !defined(PT_SET_EVENT_MASK)
1252 error ("Unable to catch execs prior to HPUX 10.0");
1253
1254#else
b83266a0 1255 /* Enable reporting of exec events from the kernel. */
c906108c 1256 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 1257 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
1258 return 0;
1259#endif
1260}
1261
1262int
fba45db2 1263child_remove_exec_catchpoint (int pid)
c906108c 1264{
b83266a0 1265 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
1266#if !defined(PT_SET_EVENT_MASK)
1267 error ("Unable to catch execs prior to HPUX 10.0");
1268
1269#else
1270 /* Disable reporting of exec events from the kernel. */
1271 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 1272 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
1273 return 0;
1274#endif
1275}
1276
1277int
47932f85 1278hpux_has_execd (int pid, char **execd_pathname)
c906108c 1279{
b83266a0 1280 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
1281#if !defined(PT_GET_PROCESS_STATE)
1282 *execd_pathname = NULL;
1283 return 0;
1284
1285#else
1286 int pt_status;
c5aa993b 1287 ptrace_state_t ptrace_state;
c906108c
SS
1288
1289 errno = 0;
1290 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 1291 pid,
c5aa993b 1292 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 1293 sizeof (ptrace_state));
c906108c
SS
1294 if (errno)
1295 perror_with_name ("ptrace");
1296 if (pt_status < 0)
1297 return 0;
1298
1299 if (ptrace_state.pe_report_event & PTRACE_EXEC)
1300 {
c5aa993b 1301 char *exec_file = target_pid_to_exec_file (pid);
c906108c
SS
1302 *execd_pathname = savestring (exec_file, strlen (exec_file));
1303 return 1;
1304 }
1305
1306 return 0;
1307#endif
1308}
1309
1310int
fba45db2 1311child_reported_exec_events_per_exec_call (void)
c906108c 1312{
c5aa993b 1313 return 2; /* ptrace reports the event twice per call. */
c906108c
SS
1314}
1315
1316int
47932f85 1317hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
c906108c
SS
1318{
1319 /* This request is only available on HPUX 10.30 and later, via
1320 the ttrace interface. */
1321
1322 *kind = TARGET_WAITKIND_SPURIOUS;
1323 *syscall_id = -1;
1324 return 0;
1325}
1326
1327char *
fba45db2 1328child_pid_to_exec_file (int pid)
c906108c 1329{
b83266a0 1330 static char exec_file_buffer[1024];
c906108c 1331 int pt_status;
b83266a0
SS
1332 CORE_ADDR top_of_stack;
1333 char four_chars[4];
c906108c
SS
1334 int name_index;
1335 int i;
39f77062 1336 ptid_t saved_inferior_ptid;
52287340 1337 int done;
c5aa993b 1338
c906108c
SS
1339#ifdef PT_GET_PROCESS_PATHNAME
1340 /* As of 10.x HP-UX, there's an explicit request to get the pathname. */
1341 pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME,
b83266a0
SS
1342 pid,
1343 (PTRACE_ARG3_TYPE) exec_file_buffer,
1344 sizeof (exec_file_buffer) - 1);
c906108c
SS
1345 if (pt_status == 0)
1346 return exec_file_buffer;
1347#endif
1348
1349 /* It appears that this request is broken prior to 10.30.
1350 If it fails, try a really, truly amazingly gross hack
1351 that DDE uses, of pawing through the process' data
1352 segment to find the pathname. */
1353
1354 top_of_stack = 0x7b03a000;
1355 name_index = 0;
1356 done = 0;
1357
39f77062
KB
1358 /* On the chance that pid != inferior_ptid, set inferior_ptid
1359 to pid, so that (grrrr!) implicit uses of inferior_ptid get
c906108c
SS
1360 the right id. */
1361
39f77062
KB
1362 saved_inferior_ptid = inferior_ptid;
1363 inferior_ptid = pid_to_ptid (pid);
c906108c
SS
1364
1365 /* Try to grab a null-terminated string. */
c5aa993b 1366 while (!done)
c906108c
SS
1367 {
1368 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
1369 {
39f77062 1370 inferior_ptid = saved_inferior_ptid;
c906108c
SS
1371 return NULL;
1372 }
1373 for (i = 0; i < 4; i++)
1374 {
1375 exec_file_buffer[name_index++] = four_chars[i];
1376 done = (four_chars[i] == '\0');
1377 if (done)
1378 break;
1379 }
1380 top_of_stack += 4;
1381 }
1382
1383 if (exec_file_buffer[0] == '\0')
1384 {
39f77062 1385 inferior_ptid = saved_inferior_ptid;
c906108c
SS
1386 return NULL;
1387 }
1388
39f77062 1389 inferior_ptid = saved_inferior_ptid;
c906108c
SS
1390 return exec_file_buffer;
1391}
1392
1393void
fba45db2 1394pre_fork_inferior (void)
c906108c
SS
1395{
1396 int status;
1397
1398 status = pipe (startup_semaphore.parent_channel);
1399 if (status < 0)
1400 {
1401 warning ("error getting parent pipe for startup semaphore");
1402 return;
1403 }
1404
1405 status = pipe (startup_semaphore.child_channel);
1406 if (status < 0)
1407 {
1408 warning ("error getting child pipe for startup semaphore");
1409 return;
1410 }
1411}
c906108c 1412\f
c5aa993b 1413
c906108c
SS
1414/* Check to see if the given thread is alive.
1415
1416 This is a no-op, as ptrace doesn't support threads, so we just
1417 return "TRUE". */
1418
1419int
39f77062 1420child_thread_alive (ptid_t ptid)
c906108c 1421{
c5aa993b 1422 return 1;
c906108c
SS
1423}
1424
1425#endif /* ! GDB_NATIVE_HPUX_11 */
This page took 0.301625 seconds and 4 git commands to generate.