Add __FILE__ and __LINE__ parameter to internal_error() /
[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, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* FIXME, some 387-specific items of use taken from i387-tdep.c -- ought to be
23 merged back in. */
24
25 #include "defs.h"
26 #include "frame.h"
27 #include "inferior.h"
28 #include "symtab.h"
29 #include "target.h"
30
31 /* FIXME: What is the _INKERNEL define for? */
32 #define _INKERNEL
33 #include <signal.h>
34 #undef _INKERNEL
35 #include <sys/wait.h>
36 #include <sys/param.h>
37 #include <sys/user.h>
38 #include <sys/proc.h>
39 #include <sys/dir.h>
40 #include <sys/ioctl.h>
41 #include "gdb_stat.h"
42 #ifdef _SEQUENT_
43 #include <sys/ptrace.h>
44 #else
45 /* Dynix has only machine/ptrace.h, which is already included by sys/user.h */
46 /* Dynix has no mptrace call */
47 #define mptrace ptrace
48 #endif
49 #include "gdbcore.h"
50 #include <fcntl.h>
51 #include <sgtty.h>
52 #define TERMINAL struct sgttyb
53
54 #include "gdbcore.h"
55
56 void
57 store_inferior_registers (int regno)
58 {
59 struct pt_regset regs;
60 int i;
61
62 /* FIXME: Fetching the registers is a kludge to initialize all elements
63 in the fpu and fpa status. This works for normal debugging, but
64 might cause problems when calling functions in the inferior.
65 At least fpu_control and fpa_pcr (probably more) should be added
66 to the registers array to solve this properly. */
67 mptrace (XPT_RREGS, inferior_pid, (PTRACE_ARG3_TYPE) & regs, 0);
68
69 regs.pr_eax = *(int *) &registers[REGISTER_BYTE (0)];
70 regs.pr_ebx = *(int *) &registers[REGISTER_BYTE (5)];
71 regs.pr_ecx = *(int *) &registers[REGISTER_BYTE (2)];
72 regs.pr_edx = *(int *) &registers[REGISTER_BYTE (1)];
73 regs.pr_esi = *(int *) &registers[REGISTER_BYTE (6)];
74 regs.pr_edi = *(int *) &registers[REGISTER_BYTE (7)];
75 regs.pr_esp = *(int *) &registers[REGISTER_BYTE (14)];
76 regs.pr_ebp = *(int *) &registers[REGISTER_BYTE (15)];
77 regs.pr_eip = *(int *) &registers[REGISTER_BYTE (16)];
78 regs.pr_flags = *(int *) &registers[REGISTER_BYTE (17)];
79 for (i = 0; i < 31; i++)
80 {
81 regs.pr_fpa.fpa_regs[i] =
82 *(int *) &registers[REGISTER_BYTE (FP1_REGNUM + i)];
83 }
84 memcpy (regs.pr_fpu.fpu_stack[0], &registers[REGISTER_BYTE (ST0_REGNUM)], 10);
85 memcpy (regs.pr_fpu.fpu_stack[1], &registers[REGISTER_BYTE (ST1_REGNUM)], 10);
86 memcpy (regs.pr_fpu.fpu_stack[2], &registers[REGISTER_BYTE (ST2_REGNUM)], 10);
87 memcpy (regs.pr_fpu.fpu_stack[3], &registers[REGISTER_BYTE (ST3_REGNUM)], 10);
88 memcpy (regs.pr_fpu.fpu_stack[4], &registers[REGISTER_BYTE (ST4_REGNUM)], 10);
89 memcpy (regs.pr_fpu.fpu_stack[5], &registers[REGISTER_BYTE (ST5_REGNUM)], 10);
90 memcpy (regs.pr_fpu.fpu_stack[6], &registers[REGISTER_BYTE (ST6_REGNUM)], 10);
91 memcpy (regs.pr_fpu.fpu_stack[7], &registers[REGISTER_BYTE (ST7_REGNUM)], 10);
92 mptrace (XPT_WREGS, inferior_pid, (PTRACE_ARG3_TYPE) & regs, 0);
93 }
94
95 void
96 fetch_inferior_registers (int regno)
97 {
98 int i;
99 struct pt_regset regs;
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. */
130 static
131 print_fpu_status (struct pt_regset ep)
132 {
133 int i;
134 int bothstatus;
135 int top;
136 int fpreg;
137 unsigned char *p;
138
139 printf_unfiltered ("80387:");
140 if (ep.pr_fpu.fpu_ip == 0)
141 {
142 printf_unfiltered (" not in use.\n");
143 return;
144 }
145 else
146 {
147 printf_unfiltered ("\n");
148 }
149 if (ep.pr_fpu.fpu_status != 0)
150 {
151 print_387_status_word (ep.pr_fpu.fpu_status);
152 }
153 print_387_control_word (ep.pr_fpu.fpu_control);
154 printf_unfiltered ("last exception: ");
155 printf_unfiltered ("opcode 0x%x; ", ep.pr_fpu.fpu_rsvd4);
156 printf_unfiltered ("pc 0x%x:0x%x; ", ep.pr_fpu.fpu_cs, ep.pr_fpu.fpu_ip);
157 printf_unfiltered ("operand 0x%x:0x%x\n", ep.pr_fpu.fpu_data_offset, ep.pr_fpu.fpu_op_sel);
158
159 top = (ep.pr_fpu.fpu_status >> 11) & 7;
160
161 printf_unfiltered ("regno tag msb lsb value\n");
162 for (fpreg = 7; fpreg >= 0; fpreg--)
163 {
164 double val;
165
166 printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
167
168 switch ((ep.pr_fpu.fpu_tag >> (fpreg * 2)) & 3)
169 {
170 case 0:
171 printf_unfiltered ("valid ");
172 break;
173 case 1:
174 printf_unfiltered ("zero ");
175 break;
176 case 2:
177 printf_unfiltered ("trap ");
178 break;
179 case 3:
180 printf_unfiltered ("empty ");
181 break;
182 }
183 for (i = 9; i >= 0; i--)
184 printf_unfiltered ("%02x", ep.pr_fpu.fpu_stack[fpreg][i]);
185
186 i387_to_double ((char *) ep.pr_fpu.fpu_stack[fpreg], (char *) &val);
187 printf_unfiltered (" %g\n", val);
188 }
189 if (ep.pr_fpu.fpu_rsvd1)
190 warning ("rsvd1 is 0x%x\n", ep.pr_fpu.fpu_rsvd1);
191 if (ep.pr_fpu.fpu_rsvd2)
192 warning ("rsvd2 is 0x%x\n", ep.pr_fpu.fpu_rsvd2);
193 if (ep.pr_fpu.fpu_rsvd3)
194 warning ("rsvd3 is 0x%x\n", ep.pr_fpu.fpu_rsvd3);
195 if (ep.pr_fpu.fpu_rsvd5)
196 warning ("rsvd5 is 0x%x\n", ep.pr_fpu.fpu_rsvd5);
197 }
198
199
200 print_1167_control_word (unsigned int pcr)
201 {
202 int pcr_tmp;
203
204 pcr_tmp = pcr & FPA_PCR_MODE;
205 printf_unfiltered ("\tMODE= %#x; RND= %#x ", pcr_tmp, pcr_tmp & 12);
206 switch (pcr_tmp & 12)
207 {
208 case 0:
209 printf_unfiltered ("RN (Nearest Value)");
210 break;
211 case 1:
212 printf_unfiltered ("RZ (Zero)");
213 break;
214 case 2:
215 printf_unfiltered ("RP (Positive Infinity)");
216 break;
217 case 3:
218 printf_unfiltered ("RM (Negative Infinity)");
219 break;
220 }
221 printf_unfiltered ("; IRND= %d ", pcr_tmp & 2);
222 if (0 == pcr_tmp & 2)
223 {
224 printf_unfiltered ("(same as RND)\n");
225 }
226 else
227 {
228 printf_unfiltered ("(toward zero)\n");
229 }
230 pcr_tmp = pcr & FPA_PCR_EM;
231 printf_unfiltered ("\tEM= %#x", pcr_tmp);
232 if (pcr_tmp & FPA_PCR_EM_DM)
233 printf_unfiltered (" DM");
234 if (pcr_tmp & FPA_PCR_EM_UOM)
235 printf_unfiltered (" UOM");
236 if (pcr_tmp & FPA_PCR_EM_PM)
237 printf_unfiltered (" PM");
238 if (pcr_tmp & FPA_PCR_EM_UM)
239 printf_unfiltered (" UM");
240 if (pcr_tmp & FPA_PCR_EM_OM)
241 printf_unfiltered (" OM");
242 if (pcr_tmp & FPA_PCR_EM_ZM)
243 printf_unfiltered (" ZM");
244 if (pcr_tmp & FPA_PCR_EM_IM)
245 printf_unfiltered (" IM");
246 printf_unfiltered ("\n");
247 pcr_tmp = FPA_PCR_CC;
248 printf_unfiltered ("\tCC= %#x", pcr_tmp);
249 if (pcr_tmp & FPA_PCR_20MHZ)
250 printf_unfiltered (" 20MHZ");
251 if (pcr_tmp & FPA_PCR_CC_Z)
252 printf_unfiltered (" Z");
253 if (pcr_tmp & FPA_PCR_CC_C2)
254 printf_unfiltered (" C2");
255
256 /* Dynix defines FPA_PCR_CC_C0 to 0x100 and ptx defines
257 FPA_PCR_CC_C1 to 0x100. Use whichever is defined and assume
258 the OS knows what it is doing. */
259 #ifdef FPA_PCR_CC_C1
260 if (pcr_tmp & FPA_PCR_CC_C1)
261 printf_unfiltered (" C1");
262 #else
263 if (pcr_tmp & FPA_PCR_CC_C0)
264 printf_unfiltered (" C0");
265 #endif
266
267 switch (pcr_tmp)
268 {
269 case FPA_PCR_CC_Z:
270 printf_unfiltered (" (Equal)");
271 break;
272 #ifdef FPA_PCR_CC_C1
273 case FPA_PCR_CC_C1:
274 #else
275 case FPA_PCR_CC_C0:
276 #endif
277 printf_unfiltered (" (Less than)");
278 break;
279 case 0:
280 printf_unfiltered (" (Greater than)");
281 break;
282 case FPA_PCR_CC_Z |
283 #ifdef FPA_PCR_CC_C1
284 FPA_PCR_CC_C1
285 #else
286 FPA_PCR_CC_C0
287 #endif
288 | FPA_PCR_CC_C2:
289 printf_unfiltered (" (Unordered)");
290 break;
291 default:
292 printf_unfiltered (" (Undefined)");
293 break;
294 }
295 printf_unfiltered ("\n");
296 pcr_tmp = pcr & FPA_PCR_AE;
297 printf_unfiltered ("\tAE= %#x", pcr_tmp);
298 if (pcr_tmp & FPA_PCR_AE_DE)
299 printf_unfiltered (" DE");
300 if (pcr_tmp & FPA_PCR_AE_UOE)
301 printf_unfiltered (" UOE");
302 if (pcr_tmp & FPA_PCR_AE_PE)
303 printf_unfiltered (" PE");
304 if (pcr_tmp & FPA_PCR_AE_UE)
305 printf_unfiltered (" UE");
306 if (pcr_tmp & FPA_PCR_AE_OE)
307 printf_unfiltered (" OE");
308 if (pcr_tmp & FPA_PCR_AE_ZE)
309 printf_unfiltered (" ZE");
310 if (pcr_tmp & FPA_PCR_AE_EE)
311 printf_unfiltered (" EE");
312 if (pcr_tmp & FPA_PCR_AE_IE)
313 printf_unfiltered (" IE");
314 printf_unfiltered ("\n");
315 }
316
317 print_1167_regs (long regs[FPA_NREGS])
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 (__FILE__, __LINE__,
536 "PTS_WATCHPT_HIT\n");
537 break;
538 default:
539 /* stopped by signal */
540 status->kind = TARGET_WAITKIND_STOPPED;
541 status->value.sig = target_signal_from_host (pt.ps_reason);
542 death_by_signal = 0;
543
544 if (0 == (SIGNALS_DFL_SAFE & sigmask (pt.ps_reason)))
545 {
546 break;
547 }
548 /* else default action of signal is to die */
549 #ifdef SVR4_SHARED_LIBS
550 rv = ptrace (PT_GET_PRSTATUS, pid, (char *) &pstatus, 0);
551 if (-1 == rv)
552 error ("child_wait: signal %d PT_GET_PRSTATUS: %s\n",
553 pt.ps_reason, safe_strerror (errno));
554 if (pstatus.pr_cursig != pt.ps_reason)
555 {
556 printf ("pstatus signal %d, pt signal %d\n",
557 pstatus.pr_cursig, pt.ps_reason);
558 }
559 sa_hand = (int) pstatus.pr_action.sa_handler;
560 #else
561 saoff = (unsigned long) &u.u_sa[0] - (unsigned long) &u;
562 saoff += sizeof (struct sigaction) * (pt.ps_reason - 1);
563 errno = 0;
564 sa_hand = ptrace (PT_RUSER, pid, (char *) saoff, 0);
565 if (errno)
566 error ("child_wait: signal %d: RUSER: %s\n",
567 pt.ps_reason, safe_strerror (errno));
568 #endif
569 if ((int) SIG_DFL == sa_hand)
570 {
571 /* we will be dying */
572 death_by_signal = pt.ps_reason;
573 }
574 break;
575 }
576
577 }
578 while (pid != inferior_pid); /* Some other child died or stopped */
579
580 return pid;
581 }
582 #else /* !ATTACH_DETACH */
583 /*
584 * Simple child_wait() based on inftarg.c child_wait() for use until
585 * the MPDEBUGGER child_wait() works properly. This will go away when
586 * that is fixed.
587 */
588 child_wait (int pid, struct target_waitstatus *ourstatus)
589 {
590 int save_errno;
591 int status;
592
593 do
594 {
595 pid = wait (&status);
596 save_errno = errno;
597
598 if (pid == -1)
599 {
600 if (save_errno == EINTR)
601 continue;
602 fprintf (stderr, "Child process unexpectedly missing: %s.\n",
603 safe_strerror (save_errno));
604 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
605 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
606 return -1;
607 }
608 }
609 while (pid != inferior_pid); /* Some other child died or stopped */
610 store_waitstatus (ourstatus, status);
611 return pid;
612 }
613 #endif /* ATTACH_DETACH */
614 \f
615
616
617 /* This function simply calls ptrace with the given arguments.
618 It exists so that all calls to ptrace are isolated in this
619 machine-dependent file. */
620 int
621 call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data)
622 {
623 return ptrace (request, pid, addr, data);
624 }
625
626 int
627 call_mptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data)
628 {
629 return mptrace (request, pid, addr, data);
630 }
631
632 #if defined (DEBUG_PTRACE)
633 /* For the rest of the file, use an extra level of indirection */
634 /* This lets us breakpoint usefully on call_ptrace. */
635 #define ptrace call_ptrace
636 #define mptrace call_mptrace
637 #endif
638
639 void
640 kill_inferior (void)
641 {
642 if (inferior_pid == 0)
643 return;
644
645 /* For MPDEBUGGER, don't use PT_KILL, since the child will stop
646 again with a PTS_EXIT. Just hit him with SIGKILL (so he stops)
647 and detach. */
648
649 kill (inferior_pid, SIGKILL);
650 #ifdef ATTACH_DETACH
651 detach (SIGKILL);
652 #else /* ATTACH_DETACH */
653 ptrace (PT_KILL, inferior_pid, 0, 0);
654 wait ((int *) NULL);
655 #endif /* ATTACH_DETACH */
656 target_mourn_inferior ();
657 }
658
659 /* Resume execution of the inferior process.
660 If STEP is nonzero, single-step it.
661 If SIGNAL is nonzero, give it that signal. */
662
663 void
664 child_resume (int pid, int step, enum target_signal signal)
665 {
666 errno = 0;
667
668 if (pid == -1)
669 pid = inferior_pid;
670
671 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
672 it was. (If GDB wanted it to start some other way, we have already
673 written a new PC value to the child.)
674
675 If this system does not support PT_SSTEP, a higher level function will
676 have called single_step() to transmute the step request into a
677 continue request (by setting breakpoints on all possible successor
678 instructions), so we don't have to worry about that here. */
679
680 if (step)
681 ptrace (PT_SSTEP, pid, (PTRACE_ARG3_TYPE) 1, signal);
682 else
683 ptrace (PT_CONTSIG, pid, (PTRACE_ARG3_TYPE) 1, signal);
684
685 if (errno)
686 perror_with_name ("ptrace");
687 }
688 \f
689 #ifdef ATTACH_DETACH
690 /* Start debugging the process whose number is PID. */
691 int
692 attach (int pid)
693 {
694 sigset_t set;
695 int rv;
696
697 rv = mptrace (XPT_DEBUG, pid, 0, 0);
698 if (-1 == rv)
699 {
700 error ("mptrace(XPT_DEBUG): %s", safe_strerror (errno));
701 }
702 rv = mptrace (XPT_SIGNAL, pid, 0, SIGSTOP);
703 if (-1 == rv)
704 {
705 error ("mptrace(XPT_SIGNAL): %s", safe_strerror (errno));
706 }
707 attach_flag = 1;
708 return pid;
709 }
710
711 void
712 detach (int signo)
713 {
714 int rv;
715
716 rv = mptrace (XPT_UNDEBUG, inferior_pid, 1, signo);
717 if (-1 == rv)
718 {
719 error ("mptrace(XPT_UNDEBUG): %s", safe_strerror (errno));
720 }
721 attach_flag = 0;
722 }
723
724 #endif /* ATTACH_DETACH */
725 \f
726 /* Default the type of the ptrace transfer to int. */
727 #ifndef PTRACE_XFER_TYPE
728 #define PTRACE_XFER_TYPE int
729 #endif
730 \f
731
732 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
733 in the NEW_SUN_PTRACE case.
734 It ought to be straightforward. But it appears that writing did
735 not write the data that I specified. I cannot understand where
736 it got the data that it actually did write. */
737
738 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
739 to debugger memory starting at MYADDR. Copy to inferior if
740 WRITE is nonzero. TARGET is ignored.
741
742 Returns the length copied, which is either the LEN argument or zero.
743 This xfer function does not do partial moves, since child_ops
744 doesn't allow memory operations to cross below us in the target stack
745 anyway. */
746
747 int
748 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
749 struct target_ops *target)
750 {
751 register int i;
752 /* Round starting address down to longword boundary. */
753 register CORE_ADDR addr = memaddr & -sizeof (PTRACE_XFER_TYPE);
754 /* Round ending address up; get number of longwords that makes. */
755 register int count
756 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
757 / sizeof (PTRACE_XFER_TYPE);
758 /* Allocate buffer of that many longwords. */
759 register PTRACE_XFER_TYPE *buffer
760 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
761
762 if (write)
763 {
764 /* Fill start and end extra bytes of buffer with existing memory data. */
765
766 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE))
767 {
768 /* Need part of initial word -- fetch it. */
769 buffer[0] = ptrace (PT_RTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr,
770 0);
771 }
772
773 if (count > 1) /* FIXME, avoid if even boundary */
774 {
775 buffer[count - 1]
776 = ptrace (PT_RTEXT, inferior_pid,
777 ((PTRACE_ARG3_TYPE)
778 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
779 0);
780 }
781
782 /* Copy data to be written over corresponding part of buffer */
783
784 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
785 myaddr,
786 len);
787
788 /* Write the entire buffer. */
789
790 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
791 {
792 errno = 0;
793 ptrace (PT_WDATA, inferior_pid, (PTRACE_ARG3_TYPE) addr,
794 buffer[i]);
795 if (errno)
796 {
797 /* Using the appropriate one (I or D) is necessary for
798 Gould NP1, at least. */
799 errno = 0;
800 ptrace (PT_WTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr,
801 buffer[i]);
802 }
803 if (errno)
804 return 0;
805 }
806 }
807 else
808 {
809 /* Read all the longwords */
810 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
811 {
812 errno = 0;
813 buffer[i] = ptrace (PT_RTEXT, inferior_pid,
814 (PTRACE_ARG3_TYPE) addr, 0);
815 if (errno)
816 return 0;
817 QUIT;
818 }
819
820 /* Copy appropriate bytes out of the buffer. */
821 memcpy (myaddr,
822 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
823 len);
824 }
825 return len;
826 }
827
828
829 void
830 _initialize_symm_nat (void)
831 {
832 #ifdef ATTACH_DETACH
833 /*
834 * the MPDEBUGGER is necessary for process tree debugging and attach
835 * to work, but it alters the behavior of debugged processes, so other
836 * things (at least child_wait()) will have to change to accomodate
837 * that.
838 *
839 * Note that attach is not implemented in dynix 3, and not in ptx
840 * until version 2.1 of the OS.
841 */
842 int rv;
843 sigset_t set;
844 struct sigaction sact;
845
846 rv = mptrace (XPT_MPDEBUGGER, 0, 0, 0);
847 if (-1 == rv)
848 {
849 internal_error (__FILE__, __LINE__,
850 "_initialize_symm_nat(): mptrace(XPT_MPDEBUGGER): %s",
851 safe_strerror (errno));
852 }
853
854 /*
855 * Under MPDEBUGGER, we get SIGCLHD when a traced process does
856 * anything of interest.
857 */
858
859 /*
860 * Block SIGCHLD. We leave it blocked all the time, and then
861 * call sigsuspend() in child_wait() to wait for the child
862 * to do something. None of these ought to fail, but check anyway.
863 */
864 sigemptyset (&set);
865 rv = sigaddset (&set, SIGCHLD);
866 if (-1 == rv)
867 {
868 internal_error (__FILE__, __LINE__,
869 "_initialize_symm_nat(): sigaddset(SIGCHLD): %s",
870 safe_strerror (errno));
871 }
872 rv = sigprocmask (SIG_BLOCK, &set, (sigset_t *) NULL);
873 if (-1 == rv)
874 {
875 internal_error (__FILE__, __LINE__,
876 "_initialize_symm_nat(): sigprocmask(SIG_BLOCK): %s",
877 safe_strerror (errno));
878 }
879
880 sact.sa_handler = sigchld_handler;
881 sigemptyset (&sact.sa_mask);
882 sact.sa_flags = SA_NOCLDWAIT; /* keep the zombies away */
883 rv = sigaction (SIGCHLD, &sact, (struct sigaction *) NULL);
884 if (-1 == rv)
885 {
886 internal_error (__FILE__, __LINE__,
887 "_initialize_symm_nat(): sigaction(SIGCHLD): %s",
888 safe_strerror (errno));
889 }
890 #endif
891 }
This page took 0.04827 seconds and 5 git commands to generate.