new for ptx
[deliverable/binutils-gdb.git] / gdb / symm-nat.c
CommitLineData
56eec3c7 1/* Sequent Symmetry host interface, for GDB when running under Unix.
82a2edfb 2 Copyright 1986, 1987, 1989, 1991, 1992, 1994 Free Software Foundation, Inc.
56eec3c7
JK
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., 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"
82a2edfb 27#include "target.h"
56eec3c7 28
82a2edfb
JK
29/* FIXME: What is the _INKERNEL define for? */
30#define _INKERNEL
56eec3c7 31#include <signal.h>
82a2edfb
JK
32#undef _INKERNEL
33#include <sys/wait.h>
56eec3c7
JK
34#include <sys/param.h>
35#include <sys/user.h>
82a2edfb 36#include <sys/proc.h>
56eec3c7
JK
37#include <sys/dir.h>
38#include <sys/ioctl.h>
39#include <sys/stat.h>
82a2edfb 40#include <sys/ptrace.h>
56eec3c7
JK
41#include "gdbcore.h"
42#include <fcntl.h>
43#include <sgtty.h>
44#define TERMINAL struct sgttyb
45
46#include "gdbcore.h"
47
48void
49store_inferior_registers(regno)
50int 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 }
82a2edfb 71 mptrace (XPT_WREGS, inferior_pid, (PTRACE_ARG3_TYPE) &regs, 0);
56eec3c7
JK
72}
73
74void
75fetch_inferior_registers (regno)
76 int regno;
77{
78 int i;
79 struct pt_regset regs;
80 extern char registers[];
81
82 registers_fetched ();
83
82a2edfb 84 mptrace (XPT_RREGS, (pid), (regaddr), 0);
56eec3c7
JK
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. */
111static
112print_fpu_status(ep)
113struct pt_regset ep;
114{
115 int i;
116 int bothstatus;
117 int top;
118 int fpreg;
119 unsigned char *p;
120
199b2450 121 printf_unfiltered("80387:");
56eec3c7 122 if (ep.pr_fpu.fpu_ip == 0) {
199b2450 123 printf_unfiltered(" not in use.\n");
56eec3c7
JK
124 return;
125 } else {
199b2450 126 printf_unfiltered("\n");
56eec3c7
JK
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);
199b2450
TL
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);
56eec3c7
JK
136
137 top = (ep.pr_fpu.fpu_status >> 11) & 7;
138
199b2450 139 printf_unfiltered ("regno tag msb lsb value\n");
56eec3c7
JK
140 for (fpreg = 7; fpreg >= 0; fpreg--)
141 {
142 double val;
143
199b2450 144 printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
56eec3c7
JK
145
146 switch ((ep.pr_fpu.fpu_tag >> (fpreg * 2)) & 3)
147 {
199b2450
TL
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;
56eec3c7
JK
152 }
153 for (i = 9; i >= 0; i--)
199b2450 154 printf_unfiltered ("%02x", ep.pr_fpu.fpu_stack[fpreg][i]);
56eec3c7 155
82a2edfb 156 i387_to_double ((char *)ep.pr_fpu.fpu_stack[fpreg], (char *)&val);
199b2450 157 printf_unfiltered (" %g\n", val);
56eec3c7
JK
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
170print_1167_control_word(pcr)
171unsigned int pcr;
172
173{
174 int pcr_tmp;
175
176 pcr_tmp = pcr & FPA_PCR_MODE;
199b2450 177 printf_unfiltered("\tMODE= %#x; RND= %#x ", pcr_tmp, pcr_tmp & 12);
56eec3c7
JK
178 switch (pcr_tmp & 12) {
179 case 0:
199b2450 180 printf_unfiltered("RN (Nearest Value)");
56eec3c7
JK
181 break;
182 case 1:
199b2450 183 printf_unfiltered("RZ (Zero)");
56eec3c7
JK
184 break;
185 case 2:
199b2450 186 printf_unfiltered("RP (Positive Infinity)");
56eec3c7
JK
187 break;
188 case 3:
199b2450 189 printf_unfiltered("RM (Negative Infinity)");
56eec3c7
JK
190 break;
191 }
199b2450 192 printf_unfiltered("; IRND= %d ", pcr_tmp & 2);
56eec3c7 193 if (0 == pcr_tmp & 2) {
199b2450 194 printf_unfiltered("(same as RND)\n");
56eec3c7 195 } else {
199b2450 196 printf_unfiltered("(toward zero)\n");
56eec3c7
JK
197 }
198 pcr_tmp = pcr & FPA_PCR_EM;
199b2450
TL
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");
56eec3c7 208 pcr_tmp = FPA_PCR_CC;
199b2450
TL
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");
82a2edfb
JK
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
199b2450 218 if (pcr_tmp & FPA_PCR_CC_C1) printf_unfiltered(" C1");
82a2edfb
JK
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:
199b2450 227 printf_unfiltered(" (Equal)");
56eec3c7 228 break;
82a2edfb
JK
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
199b2450 235 printf_unfiltered(" (Less than)");
56eec3c7 236 break;
82a2edfb 237 case 0:
199b2450 238 printf_unfiltered(" (Greater than)");
56eec3c7 239 break;
82a2edfb
JK
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:
199b2450 247 printf_unfiltered(" (Unordered)");
56eec3c7 248 break;
82a2edfb 249 default:
199b2450 250 printf_unfiltered(" (Undefined)");
56eec3c7 251 break;
82a2edfb 252 }
199b2450 253 printf_unfiltered("\n");
56eec3c7 254 pcr_tmp = pcr & FPA_PCR_AE;
199b2450
TL
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");
56eec3c7
JK
265}
266
267print_1167_regs(regs)
268long 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];
199b2450 285 printf_unfiltered("%%fp%d: raw= %#x, single= %f", i+1, regs[i], xf.f);
56eec3c7 286 if (!(i & 1)) {
199b2450 287 printf_unfiltered("\n");
56eec3c7
JK
288 } else {
289 xd.l[1] = regs[i];
290 xd.l[0] = regs[i+1];
199b2450 291 printf_unfiltered(", double= %f\n", xd.d);
56eec3c7
JK
292 }
293 }
294}
295
296print_fpa_status(ep)
297struct pt_regset ep;
298
299{
300
199b2450 301 printf_unfiltered("WTL 1167:");
56eec3c7 302 if (ep.pr_fpa.fpa_pcr !=0) {
199b2450 303 printf_unfiltered("\n");
56eec3c7
JK
304 print_1167_control_word(ep.pr_fpa.fpa_pcr);
305 print_1167_regs(ep.pr_fpa.fpa_regs);
306 } else {
199b2450 307 printf_unfiltered(" not in use.\n");
56eec3c7
JK
308 }
309}
310
82a2edfb 311#if 0 /* disabled because it doesn't go through the target vector. */
56eec3c7
JK
312i386_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}
82a2edfb
JK
339#endif
340
341static volatile int got_sigchld;
342
343/*ARGSUSED*/
344/* This will eventually be more interesting. */
345void
346sigchld_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
366/*
367 * Thanks to XPT_MPDEBUGGER, we have to mange child_wait().
368 */
369int
370child_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
518
519\f
520/* This function simply calls ptrace with the given arguments.
521 It exists so that all calls to ptrace are isolated in this
522 machine-dependent file. */
523int
524call_ptrace (request, pid, addr, data)
525 int request, pid;
526 PTRACE_ARG3_TYPE addr;
527 int data;
528{
529 return ptrace (request, pid, addr, data);
530}
531
532int
533call_mptrace(request, pid, addr, data)
534 int request, pid;
535 PTRACE_ARG3_TYPE addr;
536 int data;
537{
538 return mptrace(request, pid, addr, data);
539}
540
541#if defined (DEBUG_PTRACE)
542/* For the rest of the file, use an extra level of indirection */
543/* This lets us breakpoint usefully on call_ptrace. */
544#define ptrace call_ptrace
545#define mptrace call_mptrace
546#endif
547
548void
549kill_inferior ()
550{
551 if (inferior_pid == 0)
552 return;
553 /*
554 * Don't use PT_KILL, since the child will stop again with a PTS_EXIT.
555 * Just hit him with SIGKILL (so he stops) and detach.
556 */
557 kill (inferior_pid, SIGKILL);
558 detach(SIGKILL);
559 target_mourn_inferior ();
560}
561
562/* Resume execution of the inferior process.
563 If STEP is nonzero, single-step it.
564 If SIGNAL is nonzero, give it that signal. */
565
566void
567child_resume (pid, step, signal)
568 int pid;
569 int step;
570 int signal;
571{
572 errno = 0;
573
574 if (pid == -1)
575 pid = inferior_pid;
576
577 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
578 it was. (If GDB wanted it to start some other way, we have already
579 written a new PC value to the child.)
580
581 If this system does not support PT_SSTEP, a higher level function will
582 have called single_step() to transmute the step request into a
583 continue request (by setting breakpoints on all possible successor
584 instructions), so we don't have to worry about that here. */
585
586 if (step)
587 ptrace (PT_SSTEP, pid, (PTRACE_ARG3_TYPE) 1, signal);
588 else
589 ptrace (PT_CONTSIG, pid, (PTRACE_ARG3_TYPE) 1, signal);
590
591 if (errno)
592 perror_with_name ("ptrace");
593}
594\f
595#ifdef ATTACH_DETACH
596/* Start debugging the process whose number is PID. */
597int
598attach (pid)
599 int pid;
600{
601 sigset_t set;
602 int rv;
603
604 rv = mptrace(XPT_DEBUG, pid, 0, 0);
605 if (-1 == rv) {
606 error("mptrace(XPT_DEBUG): %s", safe_strerror(errno));
607 }
608 rv = mptrace(XPT_SIGNAL, pid, 0, SIGSTOP);
609 if (-1 == rv) {
610 error("mptrace(XPT_SIGNAL): %s", safe_strerror(errno));
611 }
612 attach_flag = 1;
613 return pid;
614}
615
616void
617detach (signo)
618 int signo;
619{
620 int rv;
621
622 rv = mptrace(XPT_UNDEBUG, inferior_pid, 1, signo);
623 if (-1 == rv) {
624 error("mptrace(XPT_UNDEBUG): %s", safe_strerror(errno));
625 }
626 attach_flag = 0;
627}
628
629#endif /* ATTACH_DETACH */
630\f
631/* Default the type of the ptrace transfer to int. */
632#ifndef PTRACE_XFER_TYPE
633#define PTRACE_XFER_TYPE int
634#endif
635
636\f
637/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
638 in the NEW_SUN_PTRACE case.
639 It ought to be straightforward. But it appears that writing did
640 not write the data that I specified. I cannot understand where
641 it got the data that it actually did write. */
642
643/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
644 to debugger memory starting at MYADDR. Copy to inferior if
645 WRITE is nonzero.
646
647 Returns the length copied, which is either the LEN argument or zero.
648 This xfer function does not do partial moves, since child_ops
649 doesn't allow memory operations to cross below us in the target stack
650 anyway. */
651
652int
653child_xfer_memory (memaddr, myaddr, len, write, target)
654 CORE_ADDR memaddr;
655 char *myaddr;
656 int len;
657 int write;
658 struct target_ops *target; /* ignored */
659{
660 register int i;
661 /* Round starting address down to longword boundary. */
662 register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE);
663 /* Round ending address up; get number of longwords that makes. */
664 register int count
665 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
666 / sizeof (PTRACE_XFER_TYPE);
667 /* Allocate buffer of that many longwords. */
668 register PTRACE_XFER_TYPE *buffer
669 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
670
671 if (write)
672 {
673 /* Fill start and end extra bytes of buffer with existing memory data. */
674
675 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) {
676 /* Need part of initial word -- fetch it. */
677 buffer[0] = ptrace (PT_RTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr,
678 0);
679 }
680
681 if (count > 1) /* FIXME, avoid if even boundary */
682 {
683 buffer[count - 1]
684 = ptrace (PT_RTEXT, inferior_pid,
685 ((PTRACE_ARG3_TYPE)
686 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
687 0);
688 }
689
690 /* Copy data to be written over corresponding part of buffer */
691
692 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
693 myaddr,
694 len);
695
696 /* Write the entire buffer. */
697
698 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
699 {
700 errno = 0;
701 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
702 buffer[i]);
703 if (errno)
704 {
705 /* Using the appropriate one (I or D) is necessary for
706 Gould NP1, at least. */
707 errno = 0;
708 ptrace (PT_WTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr,
709 buffer[i]);
710 }
711 if (errno)
712 return 0;
713 }
714 }
715 else
716 {
717 /* Read all the longwords */
718 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
719 {
720 errno = 0;
721 buffer[i] = ptrace (PT_RTEXT, inferior_pid,
722 (PTRACE_ARG3_TYPE) addr, 0);
723 if (errno)
724 return 0;
725 QUIT;
726 }
727
728 /* Copy appropriate bytes out of the buffer. */
729 memcpy (myaddr,
730 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
731 len);
732 }
733 return len;
734}
735
736
737void
738_initialize_symm_nat ()
739{
740/*
741 * the MPDEBUGGER is necessary for process tree debugging and attach
742 * to work, but it alters the behavior of debugged processes, so other
743 * things (at least child_wait()) will have to change to accomodate
744 * that.
745 *
746 * Note that attach is not implemented in dynix 3, and not in ptx
747 * until version 2.1 of the OS.
748 */
749 int rv;
750 sigset_t set;
751 struct sigaction sact;
752
753 rv = mptrace(XPT_MPDEBUGGER, 0, 0, 0);
754 if (-1 == rv) {
755 fatal("_initialize_symm_nat(): mptrace(XPT_MPDEBUGGER): %s",
756 safe_strerror(errno));
757 }
758
759 /*
760 * Under MPDEBUGGER, we get SIGCLHD when a traced process does
761 * anything of interest.
762 */
763
764 /*
765 * Block SIGCHLD. We leave it blocked all the time, and then
766 * call sigsuspend() in child_wait() to wait for the child
767 * to do something. None of these ought to fail, but check anyway.
768 */
769 sigemptyset(&set);
770 rv = sigaddset(&set, SIGCHLD);
771 if (-1 == rv) {
772 fatal("_initialize_symm_nat(): sigaddset(SIGCHLD): %s",
773 safe_strerror(errno));
774 }
775 rv = sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
776 if (-1 == rv) {
777 fatal("_initialize_symm_nat(): sigprocmask(SIG_BLOCK): %s",
778 safe_strerror(errno));
779 }
780
781 sact.sa_handler = sigchld_handler;
782 sigemptyset(&sact.sa_mask);
783 sact.sa_flags = SA_NOCLDWAIT; /* keep the zombies away */
784 rv = sigaction(SIGCHLD, &sact, (struct sigaction *)NULL);
785 if (-1 == rv) {
786 fatal("_initialize_symm_nat(): sigaction(SIGCHLD): %s",
787 safe_strerror(errno));
788 }
789}
This page took 0.105854 seconds and 4 git commands to generate.