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