1 /* Native-dependent code for LynxOS.
2 Copyright 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
25 #include <sys/ptrace.h>
29 static unsigned long registers_addr PARAMS ((int pid));
31 #define X(ENTRY)(offsetof(struct econtext, ENTRY))
34 /* Mappings from tm-i386v.h */
52 X(ecode), /* Lynx doesn't give us either fs or gs, so */
53 X(fault), /* we just substitute these two in the hopes
54 that they are useful. */
59 /* Mappings from tm-m68k.h */
78 offsetof (st_t, usp) - offsetof (st_t, ec), /* sp */
82 X(fregs[0*3]), /* fp0 */
83 X(fregs[1*3]), /* fp1 */
84 X(fregs[2*3]), /* fp2 */
85 X(fregs[3*3]), /* fp3 */
86 X(fregs[4*3]), /* fp4 */
87 X(fregs[5*3]), /* fp5 */
88 X(fregs[6*3]), /* fp6 */
89 X(fregs[7*3]), /* fp7 */
91 X(fcregs[0]), /* fpcontrol */
92 X(fcregs[1]), /* fpstatus */
93 X(fcregs[2]), /* fpiaddr */
95 X(fault), /* fpflags */
100 /* Mappings from tm-sparc.h */
102 #define FX(ENTRY)(offsetof(struct fcontext, ENTRY))
104 static int regmap[] =
111 -1, /* g5->g7 aren't saved by Lynx */
124 -1,-1,-1,-1,-1,-1,-1,-1, /* l0 -> l7 */
126 -1,-1,-1,-1,-1,-1,-1,-1, /* i0 -> i7 */
128 FX(f.fregs[0]), /* f0 */
174 static int regmap[] =
176 X(iregs[0]), /* r0 */
209 X(fregs[0]), /* f0 */
242 X(srr0), /* IAR (PC) */
243 X(srr1), /* MSR (PS) */
255 /* This routine handles some oddball cases for Sparc registers and LynxOS.
256 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
257 It also handles knows where to find the I & L regs on the stack. */
260 fetch_inferior_registers (regno)
265 #define WHATREGS_FLOAT 1
266 #define WHATREGS_GEN 2
267 #define WHATREGS_STACK 4
270 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
271 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
272 whatregs = WHATREGS_STACK;
273 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
274 whatregs = WHATREGS_FLOAT;
276 whatregs = WHATREGS_GEN;
278 if (whatregs & WHATREGS_GEN)
280 struct econtext ec; /* general regs */
281 char buf[MAX_REGISTER_RAW_SIZE];
286 retval = ptrace (PTRACE_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &ec,
289 perror_with_name ("ptrace(PTRACE_GETREGS)");
291 memset (buf, 0, REGISTER_RAW_SIZE (G0_REGNUM));
292 supply_register (G0_REGNUM, buf);
293 supply_register (TBR_REGNUM, (char *)&ec.tbr);
295 memcpy (®isters[REGISTER_BYTE (G1_REGNUM)], &ec.g1,
296 4 * REGISTER_RAW_SIZE (G1_REGNUM));
297 for (i = G1_REGNUM; i <= G1_REGNUM + 3; i++)
298 register_valid[i] = 1;
300 supply_register (PS_REGNUM, (char *)&ec.psr);
301 supply_register (Y_REGNUM, (char *)&ec.y);
302 supply_register (PC_REGNUM, (char *)&ec.pc);
303 supply_register (NPC_REGNUM, (char *)&ec.npc);
304 supply_register (WIM_REGNUM, (char *)&ec.wim);
306 memcpy (®isters[REGISTER_BYTE (O0_REGNUM)], ec.o,
307 8 * REGISTER_RAW_SIZE (O0_REGNUM));
308 for (i = O0_REGNUM; i <= O0_REGNUM + 7; i++)
309 register_valid[i] = 1;
312 if (whatregs & WHATREGS_STACK)
317 sp = read_register (SP_REGNUM);
319 target_xfer_memory (sp + FRAME_SAVED_I0,
320 ®isters[REGISTER_BYTE(I0_REGNUM)],
321 8 * REGISTER_RAW_SIZE (I0_REGNUM), 0);
322 for (i = I0_REGNUM; i <= I7_REGNUM; i++)
323 register_valid[i] = 1;
325 target_xfer_memory (sp + FRAME_SAVED_L0,
326 ®isters[REGISTER_BYTE(L0_REGNUM)],
327 8 * REGISTER_RAW_SIZE (L0_REGNUM), 0);
328 for (i = L0_REGNUM; i <= L0_REGNUM + 7; i++)
329 register_valid[i] = 1;
332 if (whatregs & WHATREGS_FLOAT)
334 struct fcontext fc; /* fp regs */
339 retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc,
342 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
344 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], fc.f.fregs,
345 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
346 for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
347 register_valid[i] = 1;
349 supply_register (FPS_REGNUM, (char *)&fc.fsr);
353 /* This routine handles storing of the I & L regs for the Sparc. The trick
354 here is that they actually live on the stack. The really tricky part is
355 that when changing the stack pointer, the I & L regs must be written to
356 where the new SP points, otherwise the regs will be incorrect when the
357 process is started up again. We assume that the I & L regs are valid at
361 store_inferior_registers (regno)
367 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
368 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
369 whatregs = WHATREGS_STACK;
370 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
371 whatregs = WHATREGS_FLOAT;
372 else if (regno == SP_REGNUM)
373 whatregs = WHATREGS_STACK | WHATREGS_GEN;
375 whatregs = WHATREGS_GEN;
377 if (whatregs & WHATREGS_GEN)
379 struct econtext ec; /* general regs */
382 ec.tbr = read_register (TBR_REGNUM);
383 memcpy (&ec.g1, ®isters[REGISTER_BYTE (G1_REGNUM)],
384 4 * REGISTER_RAW_SIZE (G1_REGNUM));
386 ec.psr = read_register (PS_REGNUM);
387 ec.y = read_register (Y_REGNUM);
388 ec.pc = read_register (PC_REGNUM);
389 ec.npc = read_register (NPC_REGNUM);
390 ec.wim = read_register (WIM_REGNUM);
392 memcpy (ec.o, ®isters[REGISTER_BYTE (O0_REGNUM)],
393 8 * REGISTER_RAW_SIZE (O0_REGNUM));
396 retval = ptrace (PTRACE_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &ec,
399 perror_with_name ("ptrace(PTRACE_SETREGS)");
402 if (whatregs & WHATREGS_STACK)
407 sp = read_register (SP_REGNUM);
409 if (regno == -1 || regno == SP_REGNUM)
411 if (!register_valid[L0_REGNUM+5])
413 target_xfer_memory (sp + FRAME_SAVED_I0,
414 ®isters[REGISTER_BYTE (I0_REGNUM)],
415 8 * REGISTER_RAW_SIZE (I0_REGNUM), 1);
417 target_xfer_memory (sp + FRAME_SAVED_L0,
418 ®isters[REGISTER_BYTE (L0_REGNUM)],
419 8 * REGISTER_RAW_SIZE (L0_REGNUM), 1);
421 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
423 if (!register_valid[regno])
425 if (regno >= L0_REGNUM && regno <= L0_REGNUM + 7)
426 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM)
429 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (I0_REGNUM)
431 target_xfer_memory (sp + regoffset, ®isters[REGISTER_BYTE (regno)],
432 REGISTER_RAW_SIZE (regno), 1);
436 if (whatregs & WHATREGS_FLOAT)
438 struct fcontext fc; /* fp regs */
441 /* We read fcontext first so that we can get good values for fq_t... */
443 retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc,
446 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
448 memcpy (fc.f.fregs, ®isters[REGISTER_BYTE (FP0_REGNUM)],
449 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
451 fc.fsr = read_register (FPS_REGNUM);
454 retval = ptrace (PTRACE_SETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc,
457 perror_with_name ("ptrace(PTRACE_SETFPREGS)");
462 #if defined (I386) || defined (M68K) || defined (rs6000)
464 /* Return the offset relative to the start of the per-thread data to the
465 saved context block. */
472 int ecpoff = offsetof(st_t, ecp);
476 stblock = (CORE_ADDR) ptrace (PTRACE_THREADUSER, pid, (PTRACE_ARG3_TYPE)0,
479 perror_with_name ("ptrace(PTRACE_THREADUSER)");
481 ecp = (CORE_ADDR) ptrace (PTRACE_PEEKTHREAD, pid, (PTRACE_ARG3_TYPE)ecpoff,
484 perror_with_name ("ptrace(PTRACE_PEEKTHREAD)");
486 return ecp - stblock;
489 /* Fetch one or more registers from the inferior. REGNO == -1 to get
490 them all. We actually fetch more than requested, when convenient,
491 marking them as valid so we won't fetch them again. */
494 fetch_inferior_registers (regno)
504 reghi = NUM_REGS - 1;
507 reglo = reghi = regno;
509 ecp = registers_addr (inferior_pid);
511 for (regno = reglo; regno <= reghi; regno++)
513 char buf[MAX_REGISTER_RAW_SIZE];
514 int ptrace_fun = PTRACE_PEEKTHREAD;
517 ptrace_fun = regno == SP_REGNUM ? PTRACE_PEEKUSP : PTRACE_PEEKTHREAD;
520 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
525 reg = ptrace (ptrace_fun, inferior_pid,
526 (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), 0);
528 perror_with_name ("ptrace(PTRACE_PEEKUSP)");
530 *(int *)&buf[i] = reg;
532 supply_register (regno, buf);
536 /* Store our register values back into the inferior.
537 If REGNO is -1, do this for all registers.
538 Otherwise, REGNO specifies which register (so we can save time). */
540 /* Registers we shouldn't try to store. */
541 #if !defined (CANNOT_STORE_REGISTER)
542 #define CANNOT_STORE_REGISTER(regno) 0
546 store_inferior_registers (regno)
556 reghi = NUM_REGS - 1;
559 reglo = reghi = regno;
561 ecp = registers_addr (inferior_pid);
563 for (regno = reglo; regno <= reghi; regno++)
565 int ptrace_fun = PTRACE_POKEUSER;
567 if (CANNOT_STORE_REGISTER (regno))
571 ptrace_fun = regno == SP_REGNUM ? PTRACE_POKEUSP : PTRACE_POKEUSER;
574 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
578 reg = *(unsigned int *)®isters[REGISTER_BYTE (regno) + i];
581 ptrace (ptrace_fun, inferior_pid,
582 (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), reg);
584 perror_with_name ("ptrace(PTRACE_POKEUSP)");
588 #endif /* defined (I386) || defined (M68K) || defined (rs6000) */
590 /* Wait for child to do something. Return pid of child, or -1 in case
591 of error; store status through argument pointer OURSTATUS. */
594 child_wait (pid, ourstatus)
596 struct target_waitstatus *ourstatus;
607 set_sigint_trap(); /* Causes SIGINT to be passed on to the
609 pid = wait (&status);
611 /* Swap halves of status so that the rest of GDB can understand it */
612 status = (status << 16) | ((unsigned)status >> 16);
622 if (save_errno == EINTR)
624 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
625 safe_strerror (save_errno));
626 /* Claim it exited with unknown signal. */
627 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
628 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
632 if (pid != PIDGET (inferior_pid)) /* Some other process?!? */
635 /* thread = WIFTID (status);*/
636 thread = status >> 16;
638 /* Initial thread value can only be acquired via wait, so we have to
639 resort to this hack. */
641 if (TIDGET (inferior_pid) == 0)
643 inferior_pid = BUILDPID (inferior_pid, thread);
644 add_thread (inferior_pid);
647 pid = BUILDPID (pid, thread);
649 store_waitstatus (ourstatus, status);
655 /* Resume execution of the inferior process.
656 If STEP is nonzero, single-step it.
657 If SIGNAL is nonzero, give it that signal. */
660 child_resume (pid, step, signal)
663 enum target_signal signal;
668 /* Resume all threads. */
669 /* I think this only gets used in the non-threaded case, where "resume
670 all threads" and "resume inferior_pid" are the same. */
673 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
674 it was. (If GDB wanted it to start some other way, we have already
675 written a new PC value to the child.)
677 If this system does not support PT_STEP, a higher level function will
678 have called single_step() to transmute the step request into a
679 continue request (by setting breakpoints on all possible successor
680 instructions), so we don't have to worry about that here. */
683 ptrace (PTRACE_SINGLESTEP_ONE, pid, (PTRACE_ARG3_TYPE) 1,
684 target_signal_to_host (signal));
686 ptrace (PTRACE_CONT_ONE, pid, (PTRACE_ARG3_TYPE) 1,
687 target_signal_to_host (signal));
690 perror_with_name ("ptrace");
693 /* Convert a Lynx process ID to a string. Returns the string in a static
697 lynx_pid_to_str (pid)
702 sprintf (buf, "process %d thread %d", PIDGET (pid), TIDGET (pid));
707 /* Extract the register values out of the core file and store
708 them where `read_register' will find them.
710 CORE_REG_SECT points to the register values themselves, read into memory.
711 CORE_REG_SIZE is the size of that area.
712 WHICH says which set of registers we are handling (0 = int, 2 = float
713 on machines where they are discontiguous).
714 REG_ADDR is the offset from u.u_ar0 to the register values relative to
715 core_reg_sect. This is used with old-fashioned core files to
716 locate the registers in a large upage-plus-stack ".reg" section.
717 Original upage address X is at location core_reg_sect+x+reg_addr.
721 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
723 unsigned core_reg_size;
730 for (regno = 0; regno < NUM_REGS; regno++)
731 supply_register (regno, core_reg_sect + offsetof (st_t, ec)
735 /* Fetching this register causes all of the I & L regs to be read from the
736 stack and validated. */
738 fetch_inferior_registers (I0_REGNUM);