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