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