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