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 /* This routine handles some oddball cases for Sparc registers and LynxOS.
175 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
176 It also handles knows where to find the I & L regs on the stack. */
179 fetch_inferior_registers (regno)
184 #define WHATREGS_FLOAT 1
185 #define WHATREGS_GEN 2
186 #define WHATREGS_STACK 4
189 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
190 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
191 whatregs = WHATREGS_STACK;
192 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
193 whatregs = WHATREGS_FLOAT;
195 whatregs = WHATREGS_GEN;
197 if (whatregs & WHATREGS_GEN)
199 struct econtext ec; /* general regs */
200 char buf[MAX_REGISTER_RAW_SIZE];
205 retval = ptrace (PTRACE_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &ec,
208 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
210 memset (buf, 0, REGISTER_RAW_SIZE (G0_REGNUM));
211 supply_register (G0_REGNUM, buf);
212 supply_register (TBR_REGNUM, (char *)&ec.tbr);
214 memcpy (®isters[REGISTER_BYTE (G1_REGNUM)], &ec.g1,
215 4 * REGISTER_RAW_SIZE (G1_REGNUM));
216 for (i = G1_REGNUM; i <= G1_REGNUM + 3; i++)
217 register_valid[i] = 1;
219 supply_register (PS_REGNUM, (char *)&ec.psr);
220 supply_register (Y_REGNUM, (char *)&ec.y);
221 supply_register (PC_REGNUM, (char *)&ec.pc);
222 supply_register (NPC_REGNUM, (char *)&ec.npc);
223 supply_register (WIM_REGNUM, (char *)&ec.wim);
225 memcpy (®isters[REGISTER_BYTE (O0_REGNUM)], ec.o,
226 8 * REGISTER_RAW_SIZE (O0_REGNUM));
227 for (i = O0_REGNUM; i <= O0_REGNUM + 7; i++)
228 register_valid[i] = 1;
231 if (whatregs & WHATREGS_STACK)
236 sp = read_register (SP_REGNUM);
238 target_xfer_memory (sp + FRAME_SAVED_I0,
239 ®isters[REGISTER_BYTE(I0_REGNUM)],
240 8 * REGISTER_RAW_SIZE (I0_REGNUM), 0);
241 for (i = I0_REGNUM; i <= I7_REGNUM; i++)
242 register_valid[i] = 1;
244 target_xfer_memory (sp + FRAME_SAVED_L0,
245 ®isters[REGISTER_BYTE(L0_REGNUM)],
246 8 * REGISTER_RAW_SIZE (L0_REGNUM), 0);
247 for (i = L0_REGNUM; i <= L0_REGNUM + 7; i++)
248 register_valid[i] = 1;
251 if (whatregs & WHATREGS_FLOAT)
253 struct fcontext fc; /* fp regs */
258 retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc,
261 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
263 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], fc.f.fregs,
264 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
265 for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
266 register_valid[i] = 1;
268 supply_register (FPS_REGNUM, (char *)&fc.fsr);
272 /* This routine handles storing of the I & L regs for the Sparc. The trick
273 here is that they actually live on the stack. The really tricky part is
274 that when changing the stack pointer, the I & L regs must be written to
275 where the new SP points, otherwise the regs will be incorrect when the
276 process is started up again. We assume that the I & L regs are valid at
280 store_inferior_registers (regno)
286 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
287 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
288 whatregs = WHATREGS_STACK;
289 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
290 whatregs = WHATREGS_FLOAT;
291 else if (regno == SP_REGNUM)
292 whatregs = WHATREGS_STACK | WHATREGS_GEN;
294 whatregs = WHATREGS_GEN;
296 if (whatregs & WHATREGS_GEN)
298 struct econtext ec; /* general regs */
301 ec.tbr = read_register (TBR_REGNUM);
302 memcpy (&ec.g1, ®isters[REGISTER_BYTE (G1_REGNUM)],
303 4 * REGISTER_RAW_SIZE (G1_REGNUM));
305 ec.psr = read_register (PS_REGNUM);
306 ec.y = read_register (Y_REGNUM);
307 ec.pc = read_register (PC_REGNUM);
308 ec.npc = read_register (NPC_REGNUM);
309 ec.wim = read_register (WIM_REGNUM);
311 memcpy (ec.o, ®isters[REGISTER_BYTE (O0_REGNUM)],
312 8 * REGISTER_RAW_SIZE (O0_REGNUM));
315 retval = ptrace (PTRACE_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &ec,
318 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
321 if (whatregs & WHATREGS_STACK)
326 sp = read_register (SP_REGNUM);
328 if (regno == -1 || regno == SP_REGNUM)
330 if (!register_valid[L0_REGNUM+5])
332 target_xfer_memory (sp + FRAME_SAVED_I0,
333 ®isters[REGISTER_BYTE (I0_REGNUM)],
334 8 * REGISTER_RAW_SIZE (I0_REGNUM), 1);
336 target_xfer_memory (sp + FRAME_SAVED_L0,
337 ®isters[REGISTER_BYTE (L0_REGNUM)],
338 8 * REGISTER_RAW_SIZE (L0_REGNUM), 1);
340 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
342 if (!register_valid[regno])
344 if (regno >= L0_REGNUM && regno <= L0_REGNUM + 7)
345 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM)
348 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (I0_REGNUM)
350 target_xfer_memory (sp + regoffset, ®isters[REGISTER_BYTE (regno)],
351 REGISTER_RAW_SIZE (regno), 1);
355 if (whatregs & WHATREGS_FLOAT)
357 struct fcontext fc; /* fp regs */
360 /* We read fcontext first so that we can get good values for fq_t... */
362 retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc,
365 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
367 memcpy (fc.f.fregs, ®isters[REGISTER_BYTE (FP0_REGNUM)],
368 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
370 fc.fsr = read_register (FPS_REGNUM);
373 retval = ptrace (PTRACE_SETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) &fc,
376 perror_with_name ("Sparc fetch_inferior_registers(ptrace)");
383 /* Return the offset relative to the start of the per-thread data to the
384 saved context block. */
391 int ecpoff = offsetof(st_t, ecp);
395 stblock = (CORE_ADDR) ptrace (PTRACE_THREADUSER, pid, (PTRACE_ARG3_TYPE)0,
398 perror_with_name ("registers_addr(PTRACE_THREADUSER)");
400 ecp = (CORE_ADDR) ptrace (PTRACE_PEEKTHREAD, pid, (PTRACE_ARG3_TYPE)ecpoff,
403 perror_with_name ("registers_addr(PTRACE_PEEKTHREAD)");
405 return ecp - stblock;
408 /* Fetch one or more registers from the inferior. REGNO == -1 to get
409 them all. We actually fetch more than requested, when convenient,
410 marking them as valid so we won't fetch them again. */
413 fetch_inferior_registers (regno)
423 reghi = NUM_REGS - 1;
426 reglo = reghi = regno;
428 ecp = registers_addr (inferior_pid);
430 for (regno = reglo; regno <= reghi; regno++)
432 char buf[MAX_REGISTER_RAW_SIZE];
433 int ptrace_fun = PTRACE_PEEKTHREAD;
435 #ifdef PTRACE_PEEKUSP
436 ptrace_fun = regno == SP_REGNUM ? PTRACE_PEEKUSP : PTRACE_PEEKTHREAD;
439 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
444 reg = ptrace (ptrace_fun, inferior_pid,
445 (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), 0);
447 perror_with_name ("fetch_inferior_registers(ptrace)");
449 *(int *)&buf[i] = reg;
451 supply_register (regno, buf);
455 /* Store our register values back into the inferior.
456 If REGNO is -1, do this for all registers.
457 Otherwise, REGNO specifies which register (so we can save time). */
460 store_inferior_registers (regno)
470 reghi = NUM_REGS - 1;
473 reglo = reghi = regno;
475 ecp = registers_addr (inferior_pid);
477 for (regno = reglo; regno <= reghi; regno++)
479 int ptrace_fun = PTRACE_POKEUSER;
481 #ifdef PTRACE_POKEUSP
482 ptrace_fun = regno == SP_REGNUM ? PTRACE_POKEUSP : PTRACE_POKEUSER;
485 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
489 reg = *(unsigned int *)®isters[REGISTER_BYTE (regno) + i];
492 ptrace (ptrace_fun, inferior_pid,
493 (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), reg);
495 perror_with_name ("PTRACE_POKEUSER");
499 #endif /* ifndef SPARC */
501 /* Wait for child to do something. Return pid of child, or -1 in case
502 of error; store status through argument pointer OURSTATUS. */
505 child_wait (pid, ourstatus)
507 struct target_waitstatus *ourstatus;
518 set_sigint_trap(); /* Causes SIGINT to be passed on to the
520 pid = wait (&status);
522 /* Swap halves of status so that the rest of GDB can understand it */
523 status = (status << 16) | ((unsigned)status >> 16);
533 if (save_errno == EINTR)
535 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
536 safe_strerror (save_errno));
537 /* Claim it exited with unknown signal. */
538 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
539 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
543 if (pid != PIDGET (inferior_pid)) /* Some other process?!? */
546 /* thread = WIFTID (status);*/
547 thread = status >> 16;
549 /* Initial thread value can only be acquired via wait, so we have to
550 resort to this hack. */
552 if (TIDGET (inferior_pid) == 0)
554 inferior_pid = BUILDPID (inferior_pid, thread);
555 add_thread (inferior_pid);
558 pid = BUILDPID (pid, thread);
560 store_waitstatus (ourstatus, status);
566 /* Convert a Lynx process ID to a string. Returns the string in a static
570 lynx_pid_to_str (pid)
575 sprintf (buf, "process %d thread %d", PIDGET (pid), TIDGET (pid));
580 /* Extract the register values out of the core file and store
581 them where `read_register' will find them.
583 CORE_REG_SECT points to the register values themselves, read into memory.
584 CORE_REG_SIZE is the size of that area.
585 WHICH says which set of registers we are handling (0 = int, 2 = float
586 on machines where they are discontiguous).
587 REG_ADDR is the offset from u.u_ar0 to the register values relative to
588 core_reg_sect. This is used with old-fashioned core files to
589 locate the registers in a large upage-plus-stack ".reg" section.
590 Original upage address X is at location core_reg_sect+x+reg_addr.
594 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
596 unsigned core_reg_size;
603 for (regno = 0; regno < NUM_REGS; regno++)
604 supply_register (regno, core_reg_sect + offsetof (st_t, ec)
608 /* Fetching this register causes all of the I & L regs to be read from the
609 stack and validated. */
611 fetch_inferior_registers (I0_REGNUM);