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