]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/hppah-nat.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / hppah-nat.c
CommitLineData
c906108c 1/* Native support code for HPUX PA-RISC.
b83266a0 2 Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1998, 1999
c906108c
SS
3 Free Software Foundation, Inc.
4
5 Contributed by the Center for Software Science at the
6 University of Utah (pa-gdb-bugs@cs.utah.edu).
7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
c906108c 14
c5aa993b
JM
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
c906108c 19
c5aa993b
JM
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
c906108c
SS
24
25
26#include "defs.h"
27#include "inferior.h"
28#include "target.h"
29#include <sys/ptrace.h>
30#include "gdbcore.h"
31#include <wait.h>
32#include <signal.h>
33
34extern CORE_ADDR text_end;
35
36static void fetch_register PARAMS ((int));
37
38void
39fetch_inferior_registers (regno)
40 int regno;
41{
42 if (regno == -1)
43 for (regno = 0; regno < NUM_REGS; regno++)
44 fetch_register (regno);
45 else
46 fetch_register (regno);
47}
48
49/* Store our register values back into the inferior.
50 If REGNO is -1, do this for all registers.
51 Otherwise, REGNO specifies which register (so we can save time). */
52
53void
54store_inferior_registers (regno)
55 int regno;
56{
57 register unsigned int regaddr;
58 char buf[80];
c906108c
SS
59 register int i;
60 unsigned int offset = U_REGS_OFFSET;
61 int scratch;
62
63 if (regno >= 0)
64 {
65 if (CANNOT_STORE_REGISTER (regno))
66 return;
67 regaddr = register_addr (regno, offset);
68 errno = 0;
69 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
b83266a0
SS
70 {
71 scratch = *(int *) &registers[REGISTER_BYTE (regno)] | 0x3;
72 call_ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
73 scratch);
74 if (errno != 0)
75 {
c906108c 76 /* Error, even if attached. Failing to write these two
c5aa993b 77 registers is pretty serious. */
b83266a0
SS
78 sprintf (buf, "writing register number %d", regno);
79 perror_with_name (buf);
80 }
81 }
c906108c 82 else
c5aa993b 83 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
c906108c
SS
84 {
85 errno = 0;
86 call_ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
b83266a0 87 *(int *) &registers[REGISTER_BYTE (regno) + i]);
c906108c
SS
88 if (errno != 0)
89 {
90 /* Warning, not error, in case we are attached; sometimes the
91 kernel doesn't let us at the registers. */
92 char *err = safe_strerror (errno);
93 char *msg = alloca (strlen (err) + 128);
94 sprintf (msg, "writing register %s: %s",
95 REGISTER_NAME (regno), err);
96 warning (msg);
97 return;
98 }
c5aa993b 99 regaddr += sizeof (int);
c906108c
SS
100 }
101 }
102 else
103 for (regno = 0; regno < NUM_REGS; regno++)
104 store_inferior_registers (regno);
105}
106
107/* Fetch one register. */
108
109static void
110fetch_register (regno)
111 int regno;
112{
113 register unsigned int regaddr;
114 char buf[MAX_REGISTER_RAW_SIZE];
115 register int i;
116
117 /* Offset of registers within the u area. */
118 unsigned int offset;
119
120 offset = U_REGS_OFFSET;
121
122 regaddr = register_addr (regno, offset);
123 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
124 {
125 errno = 0;
126 *(int *) &buf[i] = call_ptrace (PT_RUREGS, inferior_pid,
b83266a0 127 (PTRACE_ARG3_TYPE) regaddr, 0);
c906108c
SS
128 regaddr += sizeof (int);
129 if (errno != 0)
130 {
131 /* Warning, not error, in case we are attached; sometimes the
b83266a0 132 kernel doesn't let us at the registers. */
c906108c
SS
133 char *err = safe_strerror (errno);
134 char *msg = alloca (strlen (err) + 128);
135 sprintf (msg, "reading register %s: %s", REGISTER_NAME (regno), err);
136 warning (msg);
137 goto error_exit;
138 }
139 }
140 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
141 buf[3] &= ~0x3;
142 supply_register (regno, buf);
c5aa993b 143error_exit:;
c906108c
SS
144}
145
146/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
147 to debugger memory starting at MYADDR. Copy to inferior if
148 WRITE is nonzero.
c5aa993b 149
c906108c
SS
150 Returns the length copied, which is either the LEN argument or zero.
151 This xfer function does not do partial moves, since child_ops
152 doesn't allow memory operations to cross below us in the target stack
153 anyway. */
154
155int
156child_xfer_memory (memaddr, myaddr, len, write, target)
157 CORE_ADDR memaddr;
158 char *myaddr;
159 int len;
160 int write;
c5aa993b 161 struct target_ops *target; /* ignored */
c906108c
SS
162{
163 register int i;
164 /* Round starting address down to longword boundary. */
c5aa993b 165 register CORE_ADDR addr = memaddr & -sizeof (int);
c906108c
SS
166 /* Round ending address up; get number of longwords that makes. */
167 register int count
c5aa993b 168 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
c906108c 169
b83266a0
SS
170 /* Allocate buffer of that many longwords.
171 Note -- do not use alloca to allocate this buffer since there is no
172 guarantee of when the buffer will actually be deallocated.
173
174 This routine can be called over and over with the same call chain;
175 this (in effect) would pile up all those alloca requests until a call
176 to alloca was made from a point higher than this routine in the
177 call chain. */
c906108c
SS
178 register int *buffer = (int *) xmalloc (count * sizeof (int));
179
180 if (write)
181 {
182 /* Fill start and end extra bytes of buffer with existing memory data. */
c5aa993b 183 if (addr != memaddr || len < (int) sizeof (int))
b83266a0
SS
184 {
185 /* Need part of initial word -- fetch it. */
c5aa993b 186 buffer[0] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
b83266a0
SS
187 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
188 }
c906108c
SS
189
190 if (count > 1) /* FIXME, avoid if even boundary */
191 {
192 buffer[count - 1]
b83266a0
SS
193 = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
194 inferior_pid,
195 (PTRACE_ARG3_TYPE) (addr
196 + (count - 1) * sizeof (int)),
197 0);
c906108c
SS
198 }
199
200 /* Copy data to be written over corresponding part of buffer */
c906108c
SS
201 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
202
203 /* Write the entire buffer. */
c906108c
SS
204 for (i = 0; i < count; i++, addr += sizeof (int))
205 {
b83266a0
SS
206 int pt_status;
207 int pt_request;
208 /* The HP-UX kernel crashes if you use PT_WDUSER to write into the
209 text segment. FIXME -- does it work to write into the data
210 segment using WIUSER, or do these idiots really expect us to
211 figure out which segment the address is in, so we can use a
212 separate system call for it??! */
c906108c 213 errno = 0;
b83266a0 214 pt_request = (addr < text_end) ? PT_WIUSER : PT_WDUSER;
c906108c 215 pt_status = call_ptrace (pt_request,
c5aa993b 216 inferior_pid,
b83266a0
SS
217 (PTRACE_ARG3_TYPE) addr,
218 buffer[i]);
219
220 /* Did we fail? Might we've guessed wrong about which
221 segment this address resides in? Try the other request,
222 and see if that works... */
223 if ((pt_status == -1) && errno)
224 {
225 errno = 0;
226 pt_request = (pt_request == PT_WIUSER) ? PT_WDUSER : PT_WIUSER;
227 pt_status = call_ptrace (pt_request,
c5aa993b 228 inferior_pid,
b83266a0
SS
229 (PTRACE_ARG3_TYPE) addr,
230 buffer[i]);
231
232 /* No, we still fail. Okay, time to punt. */
233 if ((pt_status == -1) && errno)
234 {
c5aa993b 235 free (buffer);
b83266a0
SS
236 return 0;
237 }
238 }
c906108c
SS
239 }
240 }
241 else
242 {
243 /* Read all the longwords */
244 for (i = 0; i < count; i++, addr += sizeof (int))
245 {
246 errno = 0;
c5aa993b 247 buffer[i] = call_ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
b83266a0
SS
248 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
249 if (errno)
250 {
c5aa993b 251 free (buffer);
b83266a0
SS
252 return 0;
253 }
c906108c
SS
254 QUIT;
255 }
256
257 /* Copy appropriate bytes out of the buffer. */
258 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
259 }
c5aa993b 260 free (buffer);
c906108c
SS
261 return len;
262}
263
264
265void
266child_post_follow_inferior_by_clone ()
267{
b83266a0 268 int status;
c906108c
SS
269
270 /* This function is used when following both the parent and child
271 of a fork. In this case, the debugger clones itself. The original
272 debugger follows the parent, the clone follows the child. The
273 original detaches from the child, delivering a SIGSTOP to it to
274 keep it from running away until the clone can attach itself.
275
276 At this point, the clone has attached to the child. Because of
277 the SIGSTOP, we must now deliver a SIGCONT to the child, or it
278 won't behave properly. */
279 status = kill (inferior_pid, SIGCONT);
280}
281
282
283void
284child_post_follow_vfork (parent_pid, followed_parent, child_pid, followed_child)
b83266a0
SS
285 int parent_pid;
286 int followed_parent;
287 int child_pid;
288 int followed_child;
c906108c 289{
c906108c
SS
290 /* Are we a debugger that followed the parent of a vfork? If so,
291 then recall that the child's vfork event was delivered to us
292 first. And, that the parent was suspended by the OS until the
293 child's exec or exit events were received.
294
295 Upon receiving that child vfork, then, we were forced to remove
296 all breakpoints in the child and continue it so that it could
297 reach the exec or exit point.
298
299 But also recall that the parent and child of a vfork share the
300 same address space. Thus, removing bp's in the child also
301 removed them from the parent.
302
303 Now that the child has safely exec'd or exited, we must restore
304 the parent's breakpoints before we continue it. Else, we may
305 cause it run past expected stopping points. */
306 if (followed_parent)
307 {
308 reattach_breakpoints (parent_pid);
309 }
310
311 /* Are we a debugger that followed the child of a vfork? If so,
312 then recall that we don't actually acquire control of the child
b83266a0 313 until after it has exec'd or exited. */
c906108c
SS
314 if (followed_child)
315 {
316 /* If the child has exited, then there's nothing for us to do.
c5aa993b
JM
317 In the case of an exec event, we'll let that be handled by
318 the normal mechanism that notices and handles exec events, in
319 resume(). */
c906108c
SS
320 }
321}
322
b83266a0
SS
323/* Format a process id, given PID. Be sure to terminate
324 this with a null--it's going to be printed via a "%s". */
c906108c 325char *
b83266a0 326hppa_pid_to_str (pid)
c5aa993b 327 pid_t pid;
c906108c 328{
c5aa993b
JM
329 /* Static because address returned */
330 static char buf[30];
c906108c 331
c5aa993b
JM
332 /* Extra NULLs for paranoia's sake */
333 sprintf (buf, "process %d\0\0\0\0", pid);
334
335 return buf;
c906108c
SS
336}
337
b83266a0
SS
338/* Format a thread id, given TID. Be sure to terminate
339 this with a null--it's going to be printed via a "%s".
340
341 Note: This is a core-gdb tid, not the actual system tid.
c5aa993b 342 See infttrace.c for details. */
c906108c 343char *
b83266a0 344hppa_tid_to_str (tid)
c5aa993b 345 pid_t tid;
c906108c 346{
c5aa993b
JM
347 /* Static because address returned */
348 static char buf[30];
349
350 /* Extra NULLs for paranoia's sake */
351 sprintf (buf, "system thread %d\0\0\0\0", tid);
c906108c 352
c5aa993b 353 return buf;
c906108c
SS
354}
355
356#if !defined (GDB_NATIVE_HPUX_11)
357
358/* The following code is a substitute for the infttrace.c versions used
359 with ttrace() in HPUX 11. */
360
361/* This value is an arbitrary integer. */
362#define PT_VERSION 123456
363
364/* This semaphore is used to coordinate the child and parent processes
365 after a fork(), and before an exec() by the child. See
366 parent_attach_all for details. */
367
c5aa993b
JM
368typedef struct
369{
370 int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */
371 int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */
372}
373startup_semaphore_t;
c906108c
SS
374
375#define SEM_TALK (1)
376#define SEM_LISTEN (0)
377
c5aa993b 378static startup_semaphore_t startup_semaphore;
c906108c
SS
379
380extern int parent_attach_all PARAMS ((int, PTRACE_ARG3_TYPE, int));
381
382#ifdef PT_SETTRC
383/* This function causes the caller's process to be traced by its
384 parent. This is intended to be called after GDB forks itself,
385 and before the child execs the target.
386
387 Note that HP-UX ptrace is rather funky in how this is done.
388 If the parent wants to get the initial exec event of a child,
389 it must set the ptrace event mask of the child to include execs.
390 (The child cannot do this itself.) This must be done after the
391 child is forked, but before it execs.
392
393 To coordinate the parent and child, we implement a semaphore using
394 pipes. After SETTRC'ing itself, the child tells the parent that
395 it is now traceable by the parent, and waits for the parent's
396 acknowledgement. The parent can then set the child's event mask,
397 and notify the child that it can now exec.
398
399 (The acknowledgement by parent happens as a result of a call to
400 child_acknowledge_created_inferior.) */
401
402int
403parent_attach_all (pid, addr, data)
404 int pid;
405 PTRACE_ARG3_TYPE addr;
406 int data;
407{
408 int pt_status = 0;
409
410 /* We need a memory home for a constant. */
411 int tc_magic_child = PT_VERSION;
412 int tc_magic_parent = 0;
413
414 /* The remainder of this function is only useful for HPUX 10.0 and
415 later, as it depends upon the ability to request notification
416 of specific kinds of events by the kernel. */
417#if defined(PT_SET_EVENT_MASK)
418
419 /* Notify the parent that we're potentially ready to exec(). */
420 write (startup_semaphore.child_channel[SEM_TALK],
b83266a0
SS
421 &tc_magic_child,
422 sizeof (tc_magic_child));
c906108c
SS
423
424 /* Wait for acknowledgement from the parent. */
425 read (startup_semaphore.parent_channel[SEM_LISTEN],
b83266a0
SS
426 &tc_magic_parent,
427 sizeof (tc_magic_parent));
c906108c 428 if (tc_magic_child != tc_magic_parent)
c5aa993b 429 warning ("mismatched semaphore magic");
c906108c
SS
430
431 /* Discard our copy of the semaphore. */
432 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
433 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
434 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
435 (void) close (startup_semaphore.child_channel[SEM_TALK]);
436#endif
c5aa993b 437
c906108c
SS
438 return 0;
439}
440#endif
441
442int
443hppa_require_attach (pid)
444 int pid;
445{
446 int pt_status;
b83266a0
SS
447 CORE_ADDR pc;
448 CORE_ADDR pc_addr;
c906108c
SS
449 unsigned int regs_offset;
450
451 /* Are we already attached? There appears to be no explicit way to
452 answer this via ptrace, so we try something which should be
453 innocuous if we are attached. If that fails, then we assume
454 we're not attached, and so attempt to make it so. */
455
456 errno = 0;
457 regs_offset = U_REGS_OFFSET;
458 pc_addr = register_addr (PC_REGNUM, regs_offset);
459 pc = call_ptrace (PT_READ_U, pid, (PTRACE_ARG3_TYPE) pc_addr, 0);
460
461 if (errno)
462 {
463 errno = 0;
464 pt_status = call_ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
465
466 if (errno)
b83266a0 467 return -1;
c906108c
SS
468
469 /* Now we really are attached. */
470 errno = 0;
471 }
472 attach_flag = 1;
473 return pid;
474}
475
476int
477hppa_require_detach (pid, signal)
c5aa993b
JM
478 int pid;
479 int signal;
c906108c
SS
480{
481 errno = 0;
482 call_ptrace (PT_DETACH, pid, (PTRACE_ARG3_TYPE) 1, signal);
c5aa993b 483 errno = 0; /* Ignore any errors. */
c906108c
SS
484 return pid;
485}
486
487/* Since ptrace doesn't support memory page-protection events, which
488 are used to implement "hardware" watchpoints on HP-UX, these are
489 dummy versions, which perform no useful work. */
490
491void
492hppa_enable_page_protection_events (pid)
493 int pid;
494{
495}
496
497void
498hppa_disable_page_protection_events (pid)
499 int pid;
500{
501}
502
503int
504hppa_insert_hw_watchpoint (pid, start, len, type)
505 int pid;
506 CORE_ADDR start;
507 LONGEST len;
508 int type;
509{
510 error ("Hardware watchpoints not implemented on this platform.");
511}
512
513int
514hppa_remove_hw_watchpoint (pid, start, len, type)
515 int pid;
516 CORE_ADDR start;
517 LONGEST len;
518 enum bptype type;
519{
520 error ("Hardware watchpoints not implemented on this platform.");
521}
522
523int
524hppa_can_use_hw_watchpoint (type, cnt, ot)
525 enum bptype type;
526 int cnt;
527 enum bptype ot;
528{
529 return 0;
530}
531
532int
533hppa_range_profitable_for_hw_watchpoint (pid, start, len)
534 int pid;
535 CORE_ADDR start;
536 LONGEST len;
537{
538 error ("Hardware watchpoints not implemented on this platform.");
539}
540
541char *
542hppa_pid_or_tid_to_str (id)
543 pid_t id;
544{
545 /* In the ptrace world, there are only processes. */
546 return hppa_pid_to_str (id);
547}
548
549/* This function has no meaning in a non-threaded world. Thus, we
550 return 0 (FALSE). See the use of "hppa_prepare_to_proceed" in
551 hppa-tdep.c. */
552
553pid_t
554hppa_switched_threads (pid)
555 pid_t pid;
556{
557 return (pid_t) 0;
558}
559
560void
561hppa_ensure_vforking_parent_remains_stopped (pid)
562 int pid;
563{
564 /* This assumes that the vforked parent is presently stopped, and
565 that the vforked child has just delivered its first exec event.
566 Calling kill() this way will cause the SIGTRAP to be delivered as
567 soon as the parent is resumed, which happens as soon as the
568 vforked child is resumed. See wait_for_inferior for the use of
569 this function. */
570 kill (pid, SIGTRAP);
571}
572
573int
574hppa_resume_execd_vforking_child_to_get_parent_vfork ()
575{
c5aa993b 576 return 1; /* Yes, the child must be resumed. */
c906108c
SS
577}
578
579void
580require_notification_of_events (pid)
b83266a0 581 int pid;
c906108c
SS
582{
583#if defined(PT_SET_EVENT_MASK)
584 int pt_status;
585 ptrace_event_t ptrace_events;
586
587 /* Instruct the kernel as to the set of events we wish to be
588 informed of. (This support does not exist before HPUX 10.0.
589 We'll assume if PT_SET_EVENT_MASK has not been defined by
b83266a0 590 <sys/ptrace.h>, then we're being built on pre-10.0.) */
c906108c
SS
591 memset (&ptrace_events, 0, sizeof (ptrace_events));
592
593 /* Note: By default, all signals are visible to us. If we wish
594 the kernel to keep certain signals hidden from us, we do it
595 by calling sigdelset (ptrace_events.pe_signals, signal) for
b83266a0 596 each such signal here, before doing PT_SET_EVENT_MASK. */
c906108c
SS
597 sigemptyset (&ptrace_events.pe_signals);
598
599 ptrace_events.pe_set_event = 0;
600
601 ptrace_events.pe_set_event |= PTRACE_SIGNAL;
602 ptrace_events.pe_set_event |= PTRACE_EXEC;
603 ptrace_events.pe_set_event |= PTRACE_FORK;
604 ptrace_events.pe_set_event |= PTRACE_VFORK;
605 /* ??rehrauer: Add this one when we're prepared to catch it...
c5aa993b
JM
606 ptrace_events.pe_set_event |= PTRACE_EXIT;
607 */
c906108c
SS
608
609 errno = 0;
610 pt_status = call_ptrace (PT_SET_EVENT_MASK,
c5aa993b
JM
611 pid,
612 (PTRACE_ARG3_TYPE) & ptrace_events,
613 sizeof (ptrace_events));
c906108c
SS
614 if (errno)
615 perror_with_name ("ptrace");
616 if (pt_status < 0)
617 return;
618#endif
619}
620
621void
622require_notification_of_exec_events (pid)
b83266a0 623 int pid;
c906108c
SS
624{
625#if defined(PT_SET_EVENT_MASK)
626 int pt_status;
627 ptrace_event_t ptrace_events;
628
629 /* Instruct the kernel as to the set of events we wish to be
630 informed of. (This support does not exist before HPUX 10.0.
631 We'll assume if PT_SET_EVENT_MASK has not been defined by
b83266a0 632 <sys/ptrace.h>, then we're being built on pre-10.0.) */
c906108c
SS
633 memset (&ptrace_events, 0, sizeof (ptrace_events));
634
635 /* Note: By default, all signals are visible to us. If we wish
636 the kernel to keep certain signals hidden from us, we do it
637 by calling sigdelset (ptrace_events.pe_signals, signal) for
b83266a0 638 each such signal here, before doing PT_SET_EVENT_MASK. */
c906108c
SS
639 sigemptyset (&ptrace_events.pe_signals);
640
641 ptrace_events.pe_set_event = 0;
642
643 ptrace_events.pe_set_event |= PTRACE_EXEC;
644 /* ??rehrauer: Add this one when we're prepared to catch it...
c5aa993b
JM
645 ptrace_events.pe_set_event |= PTRACE_EXIT;
646 */
c906108c
SS
647
648 errno = 0;
649 pt_status = call_ptrace (PT_SET_EVENT_MASK,
c5aa993b
JM
650 pid,
651 (PTRACE_ARG3_TYPE) & ptrace_events,
652 sizeof (ptrace_events));
c906108c
SS
653 if (errno)
654 perror_with_name ("ptrace");
655 if (pt_status < 0)
656 return;
657#endif
658}
659
660/* This function is called by the parent process, with pid being the
661 ID of the child process, after the debugger has forked. */
662
663void
664child_acknowledge_created_inferior (pid)
b83266a0 665 int pid;
c906108c
SS
666{
667 /* We need a memory home for a constant. */
668 int tc_magic_parent = PT_VERSION;
669 int tc_magic_child = 0;
670
b83266a0
SS
671 /* The remainder of this function is only useful for HPUX 10.0 and
672 later, as it depends upon the ability to request notification
673 of specific kinds of events by the kernel. */
674#if defined(PT_SET_EVENT_MASK)
c906108c
SS
675 /* Wait for the child to tell us that it has forked. */
676 read (startup_semaphore.child_channel[SEM_LISTEN],
b83266a0 677 &tc_magic_child,
c5aa993b 678 sizeof (tc_magic_child));
c906108c
SS
679
680 /* Notify the child that it can exec.
681
682 In the infttrace.c variant of this function, we set the child's
683 event mask after the fork but before the exec. In the ptrace
684 world, it seems we can't set the event mask until after the exec. */
c906108c 685 write (startup_semaphore.parent_channel[SEM_TALK],
b83266a0
SS
686 &tc_magic_parent,
687 sizeof (tc_magic_parent));
c906108c
SS
688
689 /* We'd better pause a bit before trying to set the event mask,
690 though, to ensure that the exec has happened. We don't want to
691 wait() on the child, because that'll screw up the upper layers
692 of gdb's execution control that expect to see the exec event.
693
694 After an exec, the child is no longer executing gdb code. Hence,
695 we can't have yet another synchronization via the pipes. We'll
696 just sleep for a second, and hope that's enough delay... */
c906108c
SS
697 sleep (1);
698
699 /* Instruct the kernel as to the set of events we wish to be
700 informed of. */
c906108c
SS
701 require_notification_of_exec_events (pid);
702
703 /* Discard our copy of the semaphore. */
704 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
705 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
706 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
707 (void) close (startup_semaphore.child_channel[SEM_TALK]);
b83266a0 708#endif
c906108c
SS
709}
710
711void
712child_post_startup_inferior (pid)
b83266a0 713 int pid;
c906108c
SS
714{
715 require_notification_of_events (pid);
716}
717
718void
719child_post_attach (pid)
b83266a0 720 int pid;
c906108c
SS
721{
722 require_notification_of_events (pid);
723}
724
725int
726child_insert_fork_catchpoint (pid)
b83266a0 727 int pid;
c906108c
SS
728{
729 /* This request is only available on HPUX 10.0 and later. */
730#if !defined(PT_SET_EVENT_MASK)
731 error ("Unable to catch forks prior to HPUX 10.0");
732#else
733 /* Enable reporting of fork events from the kernel. */
734 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 735 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
736 return 0;
737#endif
738}
739
740int
741child_remove_fork_catchpoint (pid)
b83266a0 742 int pid;
c906108c
SS
743{
744 /* This request is only available on HPUX 10.0 and later. */
745#if !defined(PT_SET_EVENT_MASK)
746 error ("Unable to catch forks prior to HPUX 10.0");
747#else
748 /* Disable reporting of fork events from the kernel. */
749 /* ??rehrauer: For the moment, we're always enabling these events,
750 and just ignoring them if there's no catchpoint to catch them. */
751 return 0;
752#endif
753}
754
755int
756child_insert_vfork_catchpoint (pid)
b83266a0 757 int pid;
c906108c
SS
758{
759 /* This request is only available on HPUX 10.0 and later. */
760#if !defined(PT_SET_EVENT_MASK)
761 error ("Unable to catch vforks prior to HPUX 10.0");
762#else
763 /* Enable reporting of vfork events from the kernel. */
764 /* ??rehrauer: For the moment, we're always enabling these events,
765 and just ignoring them if there's no catchpoint to catch them. */
766 return 0;
767#endif
768}
769
770int
771child_remove_vfork_catchpoint (pid)
b83266a0 772 int pid;
c906108c
SS
773{
774 /* This request is only available on HPUX 10.0 and later. */
775#if !defined(PT_SET_EVENT_MASK)
776 error ("Unable to catch vforks prior to HPUX 10.0");
777#else
778 /* Disable reporting of vfork events from the kernel. */
779 /* ??rehrauer: For the moment, we're always enabling these events,
780 and just ignoring them if there's no catchpoint to catch them. */
781 return 0;
782#endif
783}
784
785int
786child_has_forked (pid, childpid)
b83266a0
SS
787 int pid;
788 int *childpid;
c906108c
SS
789{
790 /* This request is only available on HPUX 10.0 and later. */
791#if !defined(PT_GET_PROCESS_STATE)
792 *childpid = 0;
793 return 0;
794#else
795 int pt_status;
c5aa993b 796 ptrace_state_t ptrace_state;
c906108c
SS
797
798 errno = 0;
799 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 800 pid,
c5aa993b 801 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 802 sizeof (ptrace_state));
c906108c
SS
803 if (errno)
804 perror_with_name ("ptrace");
805 if (pt_status < 0)
806 return 0;
807
808 if (ptrace_state.pe_report_event & PTRACE_FORK)
809 {
810 *childpid = ptrace_state.pe_other_pid;
811 return 1;
812 }
813
814 return 0;
815#endif
816}
817
818int
819child_has_vforked (pid, childpid)
b83266a0
SS
820 int pid;
821 int *childpid;
c906108c
SS
822{
823 /* This request is only available on HPUX 10.0 and later. */
824#if !defined(PT_GET_PROCESS_STATE)
825 *childpid = 0;
826 return 0;
827
828#else
829 int pt_status;
c5aa993b 830 ptrace_state_t ptrace_state;
c906108c
SS
831
832 errno = 0;
833 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 834 pid,
c5aa993b 835 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 836 sizeof (ptrace_state));
c906108c
SS
837 if (errno)
838 perror_with_name ("ptrace");
839 if (pt_status < 0)
840 return 0;
841
842 if (ptrace_state.pe_report_event & PTRACE_VFORK)
843 {
844 *childpid = ptrace_state.pe_other_pid;
845 return 1;
846 }
847
848 return 0;
849#endif
850}
851
852int
853child_can_follow_vfork_prior_to_exec ()
854{
855 /* ptrace doesn't allow this. */
856 return 0;
857}
858
859int
860child_insert_exec_catchpoint (pid)
b83266a0 861 int pid;
c906108c 862{
b83266a0 863 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
864#if !defined(PT_SET_EVENT_MASK)
865 error ("Unable to catch execs prior to HPUX 10.0");
866
867#else
b83266a0 868 /* Enable reporting of exec events from the kernel. */
c906108c 869 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 870 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
871 return 0;
872#endif
873}
874
875int
876child_remove_exec_catchpoint (pid)
b83266a0 877 int pid;
c906108c 878{
b83266a0 879 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
880#if !defined(PT_SET_EVENT_MASK)
881 error ("Unable to catch execs prior to HPUX 10.0");
882
883#else
884 /* Disable reporting of exec events from the kernel. */
885 /* ??rehrauer: For the moment, we're always enabling these events,
b83266a0 886 and just ignoring them if there's no catchpoint to catch them. */
c906108c
SS
887 return 0;
888#endif
889}
890
891int
892child_has_execd (pid, execd_pathname)
b83266a0
SS
893 int pid;
894 char **execd_pathname;
c906108c 895{
b83266a0 896 /* This request is only available on HPUX 10.0 and later. */
c906108c
SS
897#if !defined(PT_GET_PROCESS_STATE)
898 *execd_pathname = NULL;
899 return 0;
900
901#else
902 int pt_status;
c5aa993b 903 ptrace_state_t ptrace_state;
c906108c
SS
904
905 errno = 0;
906 pt_status = call_ptrace (PT_GET_PROCESS_STATE,
b83266a0 907 pid,
c5aa993b 908 (PTRACE_ARG3_TYPE) & ptrace_state,
b83266a0 909 sizeof (ptrace_state));
c906108c
SS
910 if (errno)
911 perror_with_name ("ptrace");
912 if (pt_status < 0)
913 return 0;
914
915 if (ptrace_state.pe_report_event & PTRACE_EXEC)
916 {
c5aa993b 917 char *exec_file = target_pid_to_exec_file (pid);
c906108c
SS
918 *execd_pathname = savestring (exec_file, strlen (exec_file));
919 return 1;
920 }
921
922 return 0;
923#endif
924}
925
926int
927child_reported_exec_events_per_exec_call ()
928{
c5aa993b 929 return 2; /* ptrace reports the event twice per call. */
c906108c
SS
930}
931
932int
933child_has_syscall_event (pid, kind, syscall_id)
934 int pid;
935 enum target_waitkind *kind;
936 int *syscall_id;
937{
938 /* This request is only available on HPUX 10.30 and later, via
939 the ttrace interface. */
940
941 *kind = TARGET_WAITKIND_SPURIOUS;
942 *syscall_id = -1;
943 return 0;
944}
945
946char *
947child_pid_to_exec_file (pid)
b83266a0 948 int pid;
c906108c 949{
b83266a0 950 static char exec_file_buffer[1024];
c906108c 951 int pt_status;
b83266a0
SS
952 CORE_ADDR top_of_stack;
953 char four_chars[4];
c906108c
SS
954 int name_index;
955 int i;
956 int saved_inferior_pid;
b83266a0 957 boolean done;
c5aa993b 958
c906108c
SS
959#ifdef PT_GET_PROCESS_PATHNAME
960 /* As of 10.x HP-UX, there's an explicit request to get the pathname. */
961 pt_status = call_ptrace (PT_GET_PROCESS_PATHNAME,
b83266a0
SS
962 pid,
963 (PTRACE_ARG3_TYPE) exec_file_buffer,
964 sizeof (exec_file_buffer) - 1);
c906108c
SS
965 if (pt_status == 0)
966 return exec_file_buffer;
967#endif
968
969 /* It appears that this request is broken prior to 10.30.
970 If it fails, try a really, truly amazingly gross hack
971 that DDE uses, of pawing through the process' data
972 segment to find the pathname. */
973
974 top_of_stack = 0x7b03a000;
975 name_index = 0;
976 done = 0;
977
978 /* On the chance that pid != inferior_pid, set inferior_pid
979 to pid, so that (grrrr!) implicit uses of inferior_pid get
980 the right id. */
981
982 saved_inferior_pid = inferior_pid;
983 inferior_pid = pid;
984
985 /* Try to grab a null-terminated string. */
c5aa993b 986 while (!done)
c906108c
SS
987 {
988 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
989 {
990 inferior_pid = saved_inferior_pid;
991 return NULL;
992 }
993 for (i = 0; i < 4; i++)
994 {
995 exec_file_buffer[name_index++] = four_chars[i];
996 done = (four_chars[i] == '\0');
997 if (done)
998 break;
999 }
1000 top_of_stack += 4;
1001 }
1002
1003 if (exec_file_buffer[0] == '\0')
1004 {
1005 inferior_pid = saved_inferior_pid;
1006 return NULL;
1007 }
1008
1009 inferior_pid = saved_inferior_pid;
1010 return exec_file_buffer;
1011}
1012
1013void
1014pre_fork_inferior ()
1015{
1016 int status;
1017
1018 status = pipe (startup_semaphore.parent_channel);
1019 if (status < 0)
1020 {
1021 warning ("error getting parent pipe for startup semaphore");
1022 return;
1023 }
1024
1025 status = pipe (startup_semaphore.child_channel);
1026 if (status < 0)
1027 {
1028 warning ("error getting child pipe for startup semaphore");
1029 return;
1030 }
1031}
c906108c 1032\f
c5aa993b 1033
c906108c
SS
1034/* Check to see if the given thread is alive.
1035
1036 This is a no-op, as ptrace doesn't support threads, so we just
1037 return "TRUE". */
1038
1039int
1040child_thread_alive (pid)
1041 int pid;
1042{
c5aa993b 1043 return 1;
c906108c
SS
1044}
1045
1046#endif /* ! GDB_NATIVE_HPUX_11 */