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 */
101 static int regmap[] =
103 X(iregs[0]), /* r0 */
136 X(fregs[0]), /* f0 */
169 X(srr0), /* IAR (PC) */
170 X(srr1), /* MSR (PS) */
182 /* This routine handles some oddball cases for Sparc registers and LynxOS.
183 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
184 It also handles knows where to find the I & L regs on the stack. */
187 fetch_inferior_registers (regno)
192 #define WHATREGS_FLOAT 1
193 #define WHATREGS_GEN 2
194 #define WHATREGS_STACK 4
197 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
198 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
199 whatregs = WHATREGS_STACK;
200 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
201 whatregs = WHATREGS_FLOAT;
203 whatregs = WHATREGS_GEN;
205 if (whatregs & WHATREGS_GEN)
207 struct econtext ec; /* general regs */
208 char buf[MAX_REGISTER_RAW_SIZE];
213 retval = ptrace (PTRACE_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &ec,
216 perror_with_name ("ptrace(PTRACE_GETREGS)");
218 memset (buf, 0, REGISTER_RAW_SIZE (G0_REGNUM));
219 supply_register (G0_REGNUM, buf);
220 supply_register (TBR_REGNUM, (char *)&ec.tbr);
222 memcpy (®isters[REGISTER_BYTE (G1_REGNUM)], &ec.g1,
223 4 * REGISTER_RAW_SIZE (G1_REGNUM));
224 for (i = G1_REGNUM; i <= G1_REGNUM + 3; i++)
225 register_valid[i] = 1;
227 supply_register (PS_REGNUM, (char *)&ec.psr);
228 supply_register (Y_REGNUM, (char *)&ec.y);
229 supply_register (PC_REGNUM, (char *)&ec.pc);
230 supply_register (NPC_REGNUM, (char *)&ec.npc);
231 supply_register (WIM_REGNUM, (char *)&ec.wim);
233 memcpy (®isters[REGISTER_BYTE (O0_REGNUM)], ec.o,
234 8 * REGISTER_RAW_SIZE (O0_REGNUM));
235 for (i = O0_REGNUM; i <= O0_REGNUM + 7; i++)
236 register_valid[i] = 1;
239 if (whatregs & WHATREGS_STACK)
244 sp = read_register (SP_REGNUM);
246 target_xfer_memory (sp + FRAME_SAVED_I0,
247 ®isters[REGISTER_BYTE(I0_REGNUM)],
248 8 * REGISTER_RAW_SIZE (I0_REGNUM), 0);
249 for (i = I0_REGNUM; i <= I7_REGNUM; i++)
250 register_valid[i] = 1;
252 target_xfer_memory (sp + FRAME_SAVED_L0,
253 ®isters[REGISTER_BYTE(L0_REGNUM)],
254 8 * REGISTER_RAW_SIZE (L0_REGNUM), 0);
255 for (i = L0_REGNUM; i <= L0_REGNUM + 7; i++)
256 register_valid[i] = 1;
259 if (whatregs & WHATREGS_FLOAT)
261 struct fcontext fc; /* fp regs */
266 retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc,
269 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
271 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], fc.f.fregs,
272 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
273 for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
274 register_valid[i] = 1;
276 supply_register (FPS_REGNUM, (char *)&fc.fsr);
280 /* This routine handles storing of the I & L regs for the Sparc. The trick
281 here is that they actually live on the stack. The really tricky part is
282 that when changing the stack pointer, the I & L regs must be written to
283 where the new SP points, otherwise the regs will be incorrect when the
284 process is started up again. We assume that the I & L regs are valid at
288 store_inferior_registers (regno)
294 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
295 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
296 whatregs = WHATREGS_STACK;
297 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
298 whatregs = WHATREGS_FLOAT;
299 else if (regno == SP_REGNUM)
300 whatregs = WHATREGS_STACK | WHATREGS_GEN;
302 whatregs = WHATREGS_GEN;
304 if (whatregs & WHATREGS_GEN)
306 struct econtext ec; /* general regs */
309 ec.tbr = read_register (TBR_REGNUM);
310 memcpy (&ec.g1, ®isters[REGISTER_BYTE (G1_REGNUM)],
311 4 * REGISTER_RAW_SIZE (G1_REGNUM));
313 ec.psr = read_register (PS_REGNUM);
314 ec.y = read_register (Y_REGNUM);
315 ec.pc = read_register (PC_REGNUM);
316 ec.npc = read_register (NPC_REGNUM);
317 ec.wim = read_register (WIM_REGNUM);
319 memcpy (ec.o, ®isters[REGISTER_BYTE (O0_REGNUM)],
320 8 * REGISTER_RAW_SIZE (O0_REGNUM));
323 retval = ptrace (PTRACE_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &ec,
326 perror_with_name ("ptrace(PTRACE_SETREGS)");
329 if (whatregs & WHATREGS_STACK)
334 sp = read_register (SP_REGNUM);
336 if (regno == -1 || regno == SP_REGNUM)
338 if (!register_valid[L0_REGNUM+5])
340 target_xfer_memory (sp + FRAME_SAVED_I0,
341 ®isters[REGISTER_BYTE (I0_REGNUM)],
342 8 * REGISTER_RAW_SIZE (I0_REGNUM), 1);
344 target_xfer_memory (sp + FRAME_SAVED_L0,
345 ®isters[REGISTER_BYTE (L0_REGNUM)],
346 8 * REGISTER_RAW_SIZE (L0_REGNUM), 1);
348 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
350 if (!register_valid[regno])
352 if (regno >= L0_REGNUM && regno <= L0_REGNUM + 7)
353 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM)
356 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (I0_REGNUM)
358 target_xfer_memory (sp + regoffset, ®isters[REGISTER_BYTE (regno)],
359 REGISTER_RAW_SIZE (regno), 1);
363 if (whatregs & WHATREGS_FLOAT)
365 struct fcontext fc; /* fp regs */
368 /* We read fcontext first so that we can get good values for fq_t... */
370 retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc,
373 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
375 memcpy (fc.f.fregs, ®isters[REGISTER_BYTE (FP0_REGNUM)],
376 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
378 fc.fsr = read_register (FPS_REGNUM);
381 retval = ptrace (PTRACE_SETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc,
384 perror_with_name ("ptrace(PTRACE_SETFPREGS)");
389 #if defined (I386) || defined (M68K) || defined (rs6000)
391 /* Return the offset relative to the start of the per-thread data to the
392 saved context block. */
399 int ecpoff = offsetof(st_t, ecp);
403 stblock = (CORE_ADDR) ptrace (PTRACE_THREADUSER, pid, (PTRACE_ARG3_TYPE)0,
406 perror_with_name ("ptrace(PTRACE_THREADUSER)");
408 ecp = (CORE_ADDR) ptrace (PTRACE_PEEKTHREAD, pid, (PTRACE_ARG3_TYPE)ecpoff,
411 perror_with_name ("ptrace(PTRACE_PEEKTHREAD)");
413 return ecp - stblock;
416 /* Fetch one or more registers from the inferior. REGNO == -1 to get
417 them all. We actually fetch more than requested, when convenient,
418 marking them as valid so we won't fetch them again. */
421 fetch_inferior_registers (regno)
431 reghi = NUM_REGS - 1;
434 reglo = reghi = regno;
436 ecp = registers_addr (inferior_pid);
438 for (regno = reglo; regno <= reghi; regno++)
440 char buf[MAX_REGISTER_RAW_SIZE];
441 int ptrace_fun = PTRACE_PEEKTHREAD;
444 ptrace_fun = regno == SP_REGNUM ? PTRACE_PEEKUSP : PTRACE_PEEKTHREAD;
447 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
452 reg = ptrace (ptrace_fun, inferior_pid,
453 (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), 0);
455 perror_with_name ("ptrace(PTRACE_PEEKUSP)");
457 *(int *)&buf[i] = reg;
459 supply_register (regno, buf);
463 /* Store our register values back into the inferior.
464 If REGNO is -1, do this for all registers.
465 Otherwise, REGNO specifies which register (so we can save time). */
468 store_inferior_registers (regno)
478 reghi = NUM_REGS - 1;
481 reglo = reghi = regno;
483 ecp = registers_addr (inferior_pid);
485 for (regno = reglo; regno <= reghi; regno++)
487 int ptrace_fun = PTRACE_POKEUSER;
489 if (CANNOT_STORE_REGISTER (regno))
493 ptrace_fun = regno == SP_REGNUM ? PTRACE_POKEUSP : PTRACE_POKEUSER;
496 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
500 reg = *(unsigned int *)®isters[REGISTER_BYTE (regno) + i];
503 ptrace (ptrace_fun, inferior_pid,
504 (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), reg);
506 perror_with_name ("ptrace(PTRACE_POKEUSP)");
510 #endif /* defined (I386) || defined (M68K) || defined (rs6000) */
512 /* Wait for child to do something. Return pid of child, or -1 in case
513 of error; store status through argument pointer OURSTATUS. */
516 child_wait (pid, ourstatus)
518 struct target_waitstatus *ourstatus;
529 set_sigint_trap(); /* Causes SIGINT to be passed on to the
531 pid = wait (&status);
533 /* Swap halves of status so that the rest of GDB can understand it */
534 status = (status << 16) | ((unsigned)status >> 16);
544 if (save_errno == EINTR)
546 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
547 safe_strerror (save_errno));
548 /* Claim it exited with unknown signal. */
549 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
550 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
554 if (pid != PIDGET (inferior_pid)) /* Some other process?!? */
557 /* thread = WIFTID (status);*/
558 thread = status >> 16;
560 /* Initial thread value can only be acquired via wait, so we have to
561 resort to this hack. */
563 if (TIDGET (inferior_pid) == 0)
565 inferior_pid = BUILDPID (inferior_pid, thread);
566 add_thread (inferior_pid);
569 pid = BUILDPID (pid, thread);
571 store_waitstatus (ourstatus, status);
577 /* Convert a Lynx process ID to a string. Returns the string in a static
581 lynx_pid_to_str (pid)
586 sprintf (buf, "process %d thread %d", PIDGET (pid), TIDGET (pid));
591 /* Extract the register values out of the core file and store
592 them where `read_register' will find them.
594 CORE_REG_SECT points to the register values themselves, read into memory.
595 CORE_REG_SIZE is the size of that area.
596 WHICH says which set of registers we are handling (0 = int, 2 = float
597 on machines where they are discontiguous).
598 REG_ADDR is the offset from u.u_ar0 to the register values relative to
599 core_reg_sect. This is used with old-fashioned core files to
600 locate the registers in a large upage-plus-stack ".reg" section.
601 Original upage address X is at location core_reg_sect+x+reg_addr.
605 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
607 unsigned core_reg_size;
614 for (regno = 0; regno < NUM_REGS; regno++)
615 supply_register (regno, core_reg_sect + offsetof (st_t, ec)
619 /* Fetching this register causes all of the I & L regs to be read from the
620 stack and validated. */
622 fetch_inferior_registers (I0_REGNUM);