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