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