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