]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/symm-nat.c
import gdb-19990422 snapshot
[thirdparty/binutils-gdb.git] / gdb / symm-nat.c
CommitLineData
c906108c
SS
1/* Sequent Symmetry host interface, for GDB when running under Unix.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1994 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20/* FIXME, some 387-specific items of use taken from i387-tdep.c -- ought to be
21 merged back in. */
22
23#include "defs.h"
24#include "frame.h"
25#include "inferior.h"
26#include "symtab.h"
27#include "target.h"
28
29/* FIXME: What is the _INKERNEL define for? */
30#define _INKERNEL
31#include <signal.h>
32#undef _INKERNEL
33#include <sys/wait.h>
34#include <sys/param.h>
35#include <sys/user.h>
36#include <sys/proc.h>
37#include <sys/dir.h>
38#include <sys/ioctl.h>
39#include "gdb_stat.h"
40#ifdef _SEQUENT_
41#include <sys/ptrace.h>
42#else
43/* Dynix has only machine/ptrace.h, which is already included by sys/user.h */
44/* Dynix has no mptrace call */
45#define mptrace ptrace
46#endif
47#include "gdbcore.h"
48#include <fcntl.h>
49#include <sgtty.h>
50#define TERMINAL struct sgttyb
51
52#include "gdbcore.h"
53
54void
55store_inferior_registers(regno)
56int regno;
57{
58 struct pt_regset regs;
59 int i;
c906108c
SS
60
61 /* FIXME: Fetching the registers is a kludge to initialize all elements
62 in the fpu and fpa status. This works for normal debugging, but
63 might cause problems when calling functions in the inferior.
64 At least fpu_control and fpa_pcr (probably more) should be added
65 to the registers array to solve this properly. */
66 mptrace (XPT_RREGS, inferior_pid, (PTRACE_ARG3_TYPE) &regs, 0);
67
68 regs.pr_eax = *(int *)&registers[REGISTER_BYTE(0)];
69 regs.pr_ebx = *(int *)&registers[REGISTER_BYTE(5)];
70 regs.pr_ecx = *(int *)&registers[REGISTER_BYTE(2)];
71 regs.pr_edx = *(int *)&registers[REGISTER_BYTE(1)];
72 regs.pr_esi = *(int *)&registers[REGISTER_BYTE(6)];
73 regs.pr_edi = *(int *)&registers[REGISTER_BYTE(7)];
74 regs.pr_esp = *(int *)&registers[REGISTER_BYTE(14)];
75 regs.pr_ebp = *(int *)&registers[REGISTER_BYTE(15)];
76 regs.pr_eip = *(int *)&registers[REGISTER_BYTE(16)];
77 regs.pr_flags = *(int *)&registers[REGISTER_BYTE(17)];
78 for (i = 0; i < 31; i++)
79 {
80 regs.pr_fpa.fpa_regs[i] =
81 *(int *)&registers[REGISTER_BYTE(FP1_REGNUM+i)];
82 }
83 memcpy (regs.pr_fpu.fpu_stack[0], &registers[REGISTER_BYTE(ST0_REGNUM)], 10);
84 memcpy (regs.pr_fpu.fpu_stack[1], &registers[REGISTER_BYTE(ST1_REGNUM)], 10);
85 memcpy (regs.pr_fpu.fpu_stack[2], &registers[REGISTER_BYTE(ST2_REGNUM)], 10);
86 memcpy (regs.pr_fpu.fpu_stack[3], &registers[REGISTER_BYTE(ST3_REGNUM)], 10);
87 memcpy (regs.pr_fpu.fpu_stack[4], &registers[REGISTER_BYTE(ST4_REGNUM)], 10);
88 memcpy (regs.pr_fpu.fpu_stack[5], &registers[REGISTER_BYTE(ST5_REGNUM)], 10);
89 memcpy (regs.pr_fpu.fpu_stack[6], &registers[REGISTER_BYTE(ST6_REGNUM)], 10);
90 memcpy (regs.pr_fpu.fpu_stack[7], &registers[REGISTER_BYTE(ST7_REGNUM)], 10);
91 mptrace (XPT_WREGS, inferior_pid, (PTRACE_ARG3_TYPE) &regs, 0);
92}
93
94void
95fetch_inferior_registers (regno)
96 int regno;
97{
98 int i;
99 struct pt_regset regs;
c906108c
SS
100
101 registers_fetched ();
102
103 mptrace (XPT_RREGS, inferior_pid, (PTRACE_ARG3_TYPE) &regs, 0);
104 *(int *)&registers[REGISTER_BYTE(EAX_REGNUM)] = regs.pr_eax;
105 *(int *)&registers[REGISTER_BYTE(EBX_REGNUM)] = regs.pr_ebx;
106 *(int *)&registers[REGISTER_BYTE(ECX_REGNUM)] = regs.pr_ecx;
107 *(int *)&registers[REGISTER_BYTE(EDX_REGNUM)] = regs.pr_edx;
108 *(int *)&registers[REGISTER_BYTE(ESI_REGNUM)] = regs.pr_esi;
109 *(int *)&registers[REGISTER_BYTE(EDI_REGNUM)] = regs.pr_edi;
110 *(int *)&registers[REGISTER_BYTE(EBP_REGNUM)] = regs.pr_ebp;
111 *(int *)&registers[REGISTER_BYTE(ESP_REGNUM)] = regs.pr_esp;
112 *(int *)&registers[REGISTER_BYTE(EIP_REGNUM)] = regs.pr_eip;
113 *(int *)&registers[REGISTER_BYTE(EFLAGS_REGNUM)] = regs.pr_flags;
114 for (i = 0; i < FPA_NREGS; i++)
115 {
116 *(int *)&registers[REGISTER_BYTE(FP1_REGNUM+i)] =
117 regs.pr_fpa.fpa_regs[i];
118 }
119 memcpy (&registers[REGISTER_BYTE(ST0_REGNUM)], regs.pr_fpu.fpu_stack[0], 10);
120 memcpy (&registers[REGISTER_BYTE(ST1_REGNUM)], regs.pr_fpu.fpu_stack[1], 10);
121 memcpy (&registers[REGISTER_BYTE(ST2_REGNUM)], regs.pr_fpu.fpu_stack[2], 10);
122 memcpy (&registers[REGISTER_BYTE(ST3_REGNUM)], regs.pr_fpu.fpu_stack[3], 10);
123 memcpy (&registers[REGISTER_BYTE(ST4_REGNUM)], regs.pr_fpu.fpu_stack[4], 10);
124 memcpy (&registers[REGISTER_BYTE(ST5_REGNUM)], regs.pr_fpu.fpu_stack[5], 10);
125 memcpy (&registers[REGISTER_BYTE(ST6_REGNUM)], regs.pr_fpu.fpu_stack[6], 10);
126 memcpy (&registers[REGISTER_BYTE(ST7_REGNUM)], regs.pr_fpu.fpu_stack[7], 10);
127}
128\f
129/* FIXME: This should be merged with i387-tdep.c as well. */
130static
131print_fpu_status(ep)
132struct pt_regset ep;
133{
134 int i;
135 int bothstatus;
136 int top;
137 int fpreg;
138 unsigned char *p;
139
140 printf_unfiltered("80387:");
141 if (ep.pr_fpu.fpu_ip == 0) {
142 printf_unfiltered(" not in use.\n");
143 return;
144 } else {
145 printf_unfiltered("\n");
146 }
147 if (ep.pr_fpu.fpu_status != 0) {
148 print_387_status_word (ep.pr_fpu.fpu_status);
149 }
150 print_387_control_word (ep.pr_fpu.fpu_control);
151 printf_unfiltered ("last exception: ");
152 printf_unfiltered ("opcode 0x%x; ", ep.pr_fpu.fpu_rsvd4);
153 printf_unfiltered ("pc 0x%x:0x%x; ", ep.pr_fpu.fpu_cs, ep.pr_fpu.fpu_ip);
154 printf_unfiltered ("operand 0x%x:0x%x\n", ep.pr_fpu.fpu_data_offset, ep.pr_fpu.fpu_op_sel);
155
156 top = (ep.pr_fpu.fpu_status >> 11) & 7;
157
158 printf_unfiltered ("regno tag msb lsb value\n");
159 for (fpreg = 7; fpreg >= 0; fpreg--)
160 {
161 double val;
162
163 printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
164
165 switch ((ep.pr_fpu.fpu_tag >> (fpreg * 2)) & 3)
166 {
167 case 0: printf_unfiltered ("valid "); break;
168 case 1: printf_unfiltered ("zero "); break;
169 case 2: printf_unfiltered ("trap "); break;
170 case 3: printf_unfiltered ("empty "); break;
171 }
172 for (i = 9; i >= 0; i--)
173 printf_unfiltered ("%02x", ep.pr_fpu.fpu_stack[fpreg][i]);
174
175 i387_to_double ((char *)ep.pr_fpu.fpu_stack[fpreg], (char *)&val);
176 printf_unfiltered (" %g\n", val);
177 }
178 if (ep.pr_fpu.fpu_rsvd1)
179 warning ("rsvd1 is 0x%x\n", ep.pr_fpu.fpu_rsvd1);
180 if (ep.pr_fpu.fpu_rsvd2)
181 warning ("rsvd2 is 0x%x\n", ep.pr_fpu.fpu_rsvd2);
182 if (ep.pr_fpu.fpu_rsvd3)
183 warning ("rsvd3 is 0x%x\n", ep.pr_fpu.fpu_rsvd3);
184 if (ep.pr_fpu.fpu_rsvd5)
185 warning ("rsvd5 is 0x%x\n", ep.pr_fpu.fpu_rsvd5);
186}
187
188
189print_1167_control_word(pcr)
190unsigned int pcr;
191
192{
193 int pcr_tmp;
194
195 pcr_tmp = pcr & FPA_PCR_MODE;
196 printf_unfiltered("\tMODE= %#x; RND= %#x ", pcr_tmp, pcr_tmp & 12);
197 switch (pcr_tmp & 12) {
198 case 0:
199 printf_unfiltered("RN (Nearest Value)");
200 break;
201 case 1:
202 printf_unfiltered("RZ (Zero)");
203 break;
204 case 2:
205 printf_unfiltered("RP (Positive Infinity)");
206 break;
207 case 3:
208 printf_unfiltered("RM (Negative Infinity)");
209 break;
210 }
211 printf_unfiltered("; IRND= %d ", pcr_tmp & 2);
212 if (0 == pcr_tmp & 2) {
213 printf_unfiltered("(same as RND)\n");
214 } else {
215 printf_unfiltered("(toward zero)\n");
216 }
217 pcr_tmp = pcr & FPA_PCR_EM;
218 printf_unfiltered("\tEM= %#x", pcr_tmp);
219 if (pcr_tmp & FPA_PCR_EM_DM) printf_unfiltered(" DM");
220 if (pcr_tmp & FPA_PCR_EM_UOM) printf_unfiltered(" UOM");
221 if (pcr_tmp & FPA_PCR_EM_PM) printf_unfiltered(" PM");
222 if (pcr_tmp & FPA_PCR_EM_UM) printf_unfiltered(" UM");
223 if (pcr_tmp & FPA_PCR_EM_OM) printf_unfiltered(" OM");
224 if (pcr_tmp & FPA_PCR_EM_ZM) printf_unfiltered(" ZM");
225 if (pcr_tmp & FPA_PCR_EM_IM) printf_unfiltered(" IM");
226 printf_unfiltered("\n");
227 pcr_tmp = FPA_PCR_CC;
228 printf_unfiltered("\tCC= %#x", pcr_tmp);
229 if (pcr_tmp & FPA_PCR_20MHZ) printf_unfiltered(" 20MHZ");
230 if (pcr_tmp & FPA_PCR_CC_Z) printf_unfiltered(" Z");
231 if (pcr_tmp & FPA_PCR_CC_C2) printf_unfiltered(" C2");
232
233 /* Dynix defines FPA_PCR_CC_C0 to 0x100 and ptx defines
234 FPA_PCR_CC_C1 to 0x100. Use whichever is defined and assume
235 the OS knows what it is doing. */
236#ifdef FPA_PCR_CC_C1
237 if (pcr_tmp & FPA_PCR_CC_C1) printf_unfiltered(" C1");
238#else
239 if (pcr_tmp & FPA_PCR_CC_C0) printf_unfiltered(" C0");
240#endif
241
242 switch (pcr_tmp)
243 {
244 case FPA_PCR_CC_Z:
245 printf_unfiltered(" (Equal)");
246 break;
247#ifdef FPA_PCR_CC_C1
248 case FPA_PCR_CC_C1:
249#else
250 case FPA_PCR_CC_C0:
251#endif
252 printf_unfiltered(" (Less than)");
253 break;
254 case 0:
255 printf_unfiltered(" (Greater than)");
256 break;
257 case FPA_PCR_CC_Z |
258#ifdef FPA_PCR_CC_C1
259 FPA_PCR_CC_C1
260#else
261 FPA_PCR_CC_C0
262#endif
263 | FPA_PCR_CC_C2:
264 printf_unfiltered(" (Unordered)");
265 break;
266 default:
267 printf_unfiltered(" (Undefined)");
268 break;
269 }
270 printf_unfiltered("\n");
271 pcr_tmp = pcr & FPA_PCR_AE;
272 printf_unfiltered("\tAE= %#x", pcr_tmp);
273 if (pcr_tmp & FPA_PCR_AE_DE) printf_unfiltered(" DE");
274 if (pcr_tmp & FPA_PCR_AE_UOE) printf_unfiltered(" UOE");
275 if (pcr_tmp & FPA_PCR_AE_PE) printf_unfiltered(" PE");
276 if (pcr_tmp & FPA_PCR_AE_UE) printf_unfiltered(" UE");
277 if (pcr_tmp & FPA_PCR_AE_OE) printf_unfiltered(" OE");
278 if (pcr_tmp & FPA_PCR_AE_ZE) printf_unfiltered(" ZE");
279 if (pcr_tmp & FPA_PCR_AE_EE) printf_unfiltered(" EE");
280 if (pcr_tmp & FPA_PCR_AE_IE) printf_unfiltered(" IE");
281 printf_unfiltered("\n");
282}
283
284print_1167_regs(regs)
285long regs[FPA_NREGS];
286
287{
288 int i;
289
290 union {
291 double d;
292 long l[2];
293 } xd;
294 union {
295 float f;
296 long l;
297 } xf;
298
299
300 for (i = 0; i < FPA_NREGS; i++) {
301 xf.l = regs[i];
302 printf_unfiltered("%%fp%d: raw= %#x, single= %f", i+1, regs[i], xf.f);
303 if (!(i & 1)) {
304 printf_unfiltered("\n");
305 } else {
306 xd.l[1] = regs[i];
307 xd.l[0] = regs[i+1];
308 printf_unfiltered(", double= %f\n", xd.d);
309 }
310 }
311}
312
313print_fpa_status(ep)
314struct pt_regset ep;
315
316{
317
318 printf_unfiltered("WTL 1167:");
319 if (ep.pr_fpa.fpa_pcr !=0) {
320 printf_unfiltered("\n");
321 print_1167_control_word(ep.pr_fpa.fpa_pcr);
322 print_1167_regs(ep.pr_fpa.fpa_regs);
323 } else {
324 printf_unfiltered(" not in use.\n");
325 }
326}
327
328#if 0 /* disabled because it doesn't go through the target vector. */
329i386_float_info ()
330{
331 char ubuf[UPAGES*NBPG];
332 struct pt_regset regset;
333
334 if (have_inferior_p())
335 {
336 PTRACE_READ_REGS (inferior_pid, (PTRACE_ARG3_TYPE) &regset);
337 }
338 else
339 {
340 int corechan = bfd_cache_lookup (core_bfd);
341 if (lseek (corechan, 0, 0) < 0)
342 {
343 perror ("seek on core file");
344 }
345 if (myread (corechan, ubuf, UPAGES*NBPG) < 0)
346 {
347 perror ("read on core file");
348 }
349 /* only interested in the floating point registers */
350 regset.pr_fpu = ((struct user *) ubuf)->u_fpusave;
351 regset.pr_fpa = ((struct user *) ubuf)->u_fpasave;
352 }
353 print_fpu_status(regset);
354 print_fpa_status(regset);
355}
356#endif
357
358static volatile int got_sigchld;
359
360/*ARGSUSED*/
361/* This will eventually be more interesting. */
362void
363sigchld_handler(signo)
364 int signo;
365{
366 got_sigchld++;
367}
368
369/*
370 * Signals for which the default action does not cause the process
371 * to die. See <sys/signal.h> for where this came from (alas, we
372 * can't use those macros directly)
373 */
374#ifndef sigmask
375#define sigmask(s) (1 << ((s) - 1))
376#endif
377#define SIGNALS_DFL_SAFE sigmask(SIGSTOP) | sigmask(SIGTSTP) | \
378 sigmask(SIGTTIN) | sigmask(SIGTTOU) | sigmask(SIGCHLD) | \
379 sigmask(SIGCONT) | sigmask(SIGWINCH) | sigmask(SIGPWR) | \
380 sigmask(SIGURG) | sigmask(SIGPOLL)
381
382#ifdef ATTACH_DETACH
383/*
384 * Thanks to XPT_MPDEBUGGER, we have to mange child_wait().
385 */
386int
387child_wait(pid, status)
388 int pid;
389 struct target_waitstatus *status;
390{
391 int save_errno, rv, xvaloff, saoff, sa_hand;
392 struct pt_stop pt;
393 struct user u;
394 sigset_t set;
395 /* Host signal number for a signal which the inferior terminates with, or
396 0 if it hasn't terminated due to a signal. */
397 static int death_by_signal = 0;
398#ifdef SVR4_SHARED_LIBS /* use this to distinguish ptx 2 vs ptx 4 */
399 prstatus_t pstatus;
400#endif
401
402 do {
403 set_sigint_trap(); /* Causes SIGINT to be passed on to the
404 attached process. */
405 save_errno = errno;
406
407 got_sigchld = 0;
408
409 sigemptyset(&set);
410
411 while (got_sigchld == 0) {
412 sigsuspend(&set);
413 }
414
415 clear_sigint_trap();
416
417 rv = mptrace(XPT_STOPSTAT, 0, (char *)&pt, 0);
418 if (-1 == rv) {
419 printf("XPT_STOPSTAT: errno %d\n", errno); /* DEBUG */
420 continue;
421 }
422
423 pid = pt.ps_pid;
424
425 if (pid != inferior_pid) {
426 /* NOTE: the mystery fork in csh/tcsh needs to be ignored.
427 * We should not return new children for the initial run
428 * of a process until it has done the exec.
429 */
430 /* inferior probably forked; send it on its way */
431 rv = mptrace(XPT_UNDEBUG, pid, 0, 0);
432 if (-1 == rv) {
433 printf("child_wait: XPT_UNDEBUG: pid %d: %s\n", pid,
434 safe_strerror(errno));
435 }
436 continue;
437 }
438 /* FIXME: Do we deal with fork notification correctly? */
439 switch (pt.ps_reason) {
440 case PTS_FORK:
441 /* multi proc: treat like PTS_EXEC */
442 /*
443 * Pretend this didn't happen, since gdb isn't set up
444 * to deal with stops on fork.
445 */
446 rv = ptrace(PT_CONTSIG, pid, 1, 0);
447 if (-1 == rv) {
448 printf("PTS_FORK: PT_CONTSIG: error %d\n", errno);
449 }
450 continue;
451 case PTS_EXEC:
452 /*
453 * Pretend this is a SIGTRAP.
454 */
455 status->kind = TARGET_WAITKIND_STOPPED;
456 status->value.sig = TARGET_SIGNAL_TRAP;
457 break;
458 case PTS_EXIT:
459 /*
460 * Note: we stop before the exit actually occurs. Extract
461 * the exit code from the uarea. If we're stopped in the
462 * exit() system call, the exit code will be in
463 * u.u_ap[0]. An exit due to an uncaught signal will have
464 * something else in here, see the comment in the default:
465 * case, below. Finally,let the process exit.
466 */
467 if (death_by_signal)
468 {
469 status->kind = TARGET_WAITKIND_SIGNALED;
470 status->value.sig = target_signal_from_host (death_by_signal);
471 death_by_signal = 0;
472 break;
473 }
474 xvaloff = (unsigned long)&u.u_ap[0] - (unsigned long)&u;
475 errno = 0;
476 rv = ptrace(PT_RUSER, pid, (char *)xvaloff, 0);
477 status->kind = TARGET_WAITKIND_EXITED;
478 status->value.integer = rv;
479 /*
480 * addr & data to mptrace() don't matter here, since
481 * the process is already dead.
482 */
483 rv = mptrace(XPT_UNDEBUG, pid, 0, 0);
484 if (-1 == rv) {
485 printf("child_wait: PTS_EXIT: XPT_UNDEBUG: pid %d error %d\n", pid,
486 errno);
487 }
488 break;
489 case PTS_WATCHPT_HIT:
490 fatal("PTS_WATCHPT_HIT\n");
491 break;
492 default:
493 /* stopped by signal */
494 status->kind = TARGET_WAITKIND_STOPPED;
495 status->value.sig = target_signal_from_host (pt.ps_reason);
496 death_by_signal = 0;
497
498 if (0 == (SIGNALS_DFL_SAFE & sigmask(pt.ps_reason))) {
499 break;
500 }
501 /* else default action of signal is to die */
502#ifdef SVR4_SHARED_LIBS
503 rv = ptrace(PT_GET_PRSTATUS, pid, (char *)&pstatus, 0);
504 if (-1 == rv)
505 error("child_wait: signal %d PT_GET_PRSTATUS: %s\n",
506 pt.ps_reason, safe_strerror(errno));
507 if (pstatus.pr_cursig != pt.ps_reason) {
508 printf("pstatus signal %d, pt signal %d\n",
509 pstatus.pr_cursig, pt.ps_reason);
510 }
511 sa_hand = (int)pstatus.pr_action.sa_handler;
512#else
513 saoff = (unsigned long)&u.u_sa[0] - (unsigned long)&u;
514 saoff += sizeof(struct sigaction) * (pt.ps_reason - 1);
515 errno = 0;
516 sa_hand = ptrace(PT_RUSER, pid, (char *)saoff, 0);
517 if (errno)
518 error("child_wait: signal %d: RUSER: %s\n",
519 pt.ps_reason, safe_strerror(errno));
520#endif
521 if ((int)SIG_DFL == sa_hand) {
522 /* we will be dying */
523 death_by_signal = pt.ps_reason;
524 }
525 break;
526 }
527
528 } while (pid != inferior_pid); /* Some other child died or stopped */
529
530 return pid;
531}
532#else /* !ATTACH_DETACH */
533/*
534 * Simple child_wait() based on inftarg.c child_wait() for use until
535 * the MPDEBUGGER child_wait() works properly. This will go away when
536 * that is fixed.
537 */
538child_wait (pid, ourstatus)
539 int pid;
540 struct target_waitstatus *ourstatus;
541{
542 int save_errno;
543 int status;
544
545 do {
546 pid = wait (&status);
547 save_errno = errno;
548
549 if (pid == -1)
550 {
551 if (save_errno == EINTR)
552 continue;
553 fprintf (stderr, "Child process unexpectedly missing: %s.\n",
554 safe_strerror (save_errno));
555 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
556 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
557 return -1;
558 }
559 } while (pid != inferior_pid); /* Some other child died or stopped */
560 store_waitstatus (ourstatus, status);
561 return pid;
562}
563#endif /* ATTACH_DETACH */
564
565
566\f
567/* This function simply calls ptrace with the given arguments.
568 It exists so that all calls to ptrace are isolated in this
569 machine-dependent file. */
570int
571call_ptrace (request, pid, addr, data)
572 int request, pid;
573 PTRACE_ARG3_TYPE addr;
574 int data;
575{
576 return ptrace (request, pid, addr, data);
577}
578
579int
580call_mptrace(request, pid, addr, data)
581 int request, pid;
582 PTRACE_ARG3_TYPE addr;
583 int data;
584{
585 return mptrace(request, pid, addr, data);
586}
587
588#if defined (DEBUG_PTRACE)
589/* For the rest of the file, use an extra level of indirection */
590/* This lets us breakpoint usefully on call_ptrace. */
591#define ptrace call_ptrace
592#define mptrace call_mptrace
593#endif
594
595void
596kill_inferior ()
597{
598 if (inferior_pid == 0)
599 return;
600
601 /* For MPDEBUGGER, don't use PT_KILL, since the child will stop
602 again with a PTS_EXIT. Just hit him with SIGKILL (so he stops)
603 and detach. */
604
605 kill (inferior_pid, SIGKILL);
606#ifdef ATTACH_DETACH
607 detach(SIGKILL);
608#else /* ATTACH_DETACH */
609 ptrace(PT_KILL, inferior_pid, 0, 0);
610 wait((int *)NULL);
611#endif /* ATTACH_DETACH */
612 target_mourn_inferior ();
613}
614
615/* Resume execution of the inferior process.
616 If STEP is nonzero, single-step it.
617 If SIGNAL is nonzero, give it that signal. */
618
619void
620child_resume (pid, step, signal)
621 int pid;
622 int step;
623 enum target_signal signal;
624{
625 errno = 0;
626
627 if (pid == -1)
628 pid = inferior_pid;
629
630 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
631 it was. (If GDB wanted it to start some other way, we have already
632 written a new PC value to the child.)
633
634 If this system does not support PT_SSTEP, a higher level function will
635 have called single_step() to transmute the step request into a
636 continue request (by setting breakpoints on all possible successor
637 instructions), so we don't have to worry about that here. */
638
639 if (step)
640 ptrace (PT_SSTEP, pid, (PTRACE_ARG3_TYPE) 1, signal);
641 else
642 ptrace (PT_CONTSIG, pid, (PTRACE_ARG3_TYPE) 1, signal);
643
644 if (errno)
645 perror_with_name ("ptrace");
646}
647\f
648#ifdef ATTACH_DETACH
649/* Start debugging the process whose number is PID. */
650int
651attach (pid)
652 int pid;
653{
654 sigset_t set;
655 int rv;
656
657 rv = mptrace(XPT_DEBUG, pid, 0, 0);
658 if (-1 == rv) {
659 error("mptrace(XPT_DEBUG): %s", safe_strerror(errno));
660 }
661 rv = mptrace(XPT_SIGNAL, pid, 0, SIGSTOP);
662 if (-1 == rv) {
663 error("mptrace(XPT_SIGNAL): %s", safe_strerror(errno));
664 }
665 attach_flag = 1;
666 return pid;
667}
668
669void
670detach (signo)
671 int signo;
672{
673 int rv;
674
675 rv = mptrace(XPT_UNDEBUG, inferior_pid, 1, signo);
676 if (-1 == rv) {
677 error("mptrace(XPT_UNDEBUG): %s", safe_strerror(errno));
678 }
679 attach_flag = 0;
680}
681
682#endif /* ATTACH_DETACH */
683\f
684/* Default the type of the ptrace transfer to int. */
685#ifndef PTRACE_XFER_TYPE
686#define PTRACE_XFER_TYPE int
687#endif
688
689\f
690/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
691 in the NEW_SUN_PTRACE case.
692 It ought to be straightforward. But it appears that writing did
693 not write the data that I specified. I cannot understand where
694 it got the data that it actually did write. */
695
696/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
697 to debugger memory starting at MYADDR. Copy to inferior if
698 WRITE is nonzero.
699
700 Returns the length copied, which is either the LEN argument or zero.
701 This xfer function does not do partial moves, since child_ops
702 doesn't allow memory operations to cross below us in the target stack
703 anyway. */
704
705int
706child_xfer_memory (memaddr, myaddr, len, write, target)
707 CORE_ADDR memaddr;
708 char *myaddr;
709 int len;
710 int write;
711 struct target_ops *target; /* ignored */
712{
713 register int i;
714 /* Round starting address down to longword boundary. */
715 register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE);
716 /* Round ending address up; get number of longwords that makes. */
717 register int count
718 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
719 / sizeof (PTRACE_XFER_TYPE);
720 /* Allocate buffer of that many longwords. */
721 register PTRACE_XFER_TYPE *buffer
722 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
723
724 if (write)
725 {
726 /* Fill start and end extra bytes of buffer with existing memory data. */
727
728 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) {
729 /* Need part of initial word -- fetch it. */
730 buffer[0] = ptrace (PT_RTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr,
731 0);
732 }
733
734 if (count > 1) /* FIXME, avoid if even boundary */
735 {
736 buffer[count - 1]
737 = ptrace (PT_RTEXT, inferior_pid,
738 ((PTRACE_ARG3_TYPE)
739 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
740 0);
741 }
742
743 /* Copy data to be written over corresponding part of buffer */
744
745 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
746 myaddr,
747 len);
748
749 /* Write the entire buffer. */
750
751 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
752 {
753 errno = 0;
754 ptrace (PT_WDATA, inferior_pid, (PTRACE_ARG3_TYPE) addr,
755 buffer[i]);
756 if (errno)
757 {
758 /* Using the appropriate one (I or D) is necessary for
759 Gould NP1, at least. */
760 errno = 0;
761 ptrace (PT_WTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr,
762 buffer[i]);
763 }
764 if (errno)
765 return 0;
766 }
767 }
768 else
769 {
770 /* Read all the longwords */
771 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
772 {
773 errno = 0;
774 buffer[i] = ptrace (PT_RTEXT, inferior_pid,
775 (PTRACE_ARG3_TYPE) addr, 0);
776 if (errno)
777 return 0;
778 QUIT;
779 }
780
781 /* Copy appropriate bytes out of the buffer. */
782 memcpy (myaddr,
783 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
784 len);
785 }
786 return len;
787}
788
789
790void
791_initialize_symm_nat ()
792{
793#ifdef ATTACH_DETACH
794/*
795 * the MPDEBUGGER is necessary for process tree debugging and attach
796 * to work, but it alters the behavior of debugged processes, so other
797 * things (at least child_wait()) will have to change to accomodate
798 * that.
799 *
800 * Note that attach is not implemented in dynix 3, and not in ptx
801 * until version 2.1 of the OS.
802 */
803 int rv;
804 sigset_t set;
805 struct sigaction sact;
806
807 rv = mptrace(XPT_MPDEBUGGER, 0, 0, 0);
808 if (-1 == rv) {
809 fatal("_initialize_symm_nat(): mptrace(XPT_MPDEBUGGER): %s",
810 safe_strerror(errno));
811 }
812
813 /*
814 * Under MPDEBUGGER, we get SIGCLHD when a traced process does
815 * anything of interest.
816 */
817
818 /*
819 * Block SIGCHLD. We leave it blocked all the time, and then
820 * call sigsuspend() in child_wait() to wait for the child
821 * to do something. None of these ought to fail, but check anyway.
822 */
823 sigemptyset(&set);
824 rv = sigaddset(&set, SIGCHLD);
825 if (-1 == rv) {
826 fatal("_initialize_symm_nat(): sigaddset(SIGCHLD): %s",
827 safe_strerror(errno));
828 }
829 rv = sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
830 if (-1 == rv) {
831 fatal("_initialize_symm_nat(): sigprocmask(SIG_BLOCK): %s",
832 safe_strerror(errno));
833 }
834
835 sact.sa_handler = sigchld_handler;
836 sigemptyset(&sact.sa_mask);
837 sact.sa_flags = SA_NOCLDWAIT; /* keep the zombies away */
838 rv = sigaction(SIGCHLD, &sact, (struct sigaction *)NULL);
839 if (-1 == rv) {
840 fatal("_initialize_symm_nat(): sigaction(SIGCHLD): %s",
841 safe_strerror(errno));
842 }
843#endif
844}