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