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