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