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