1 /* Functions specific to running gdb native on a SPARC running SunOS4.
2 Copyright 1989, 1992, 1993, 1994, 1996 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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
27 #include <sys/ptrace.h>
32 #include <machine/reg.h>
36 /* We don't store all registers immediately when requested, since they
37 get sent over in large chunks anyway. Instead, we accumulate most
38 of the changes and send them over once. "deferred_stores" keeps
39 track of which sets of registers we have locally-changed copies of,
40 so we only need send the groups that have changed. */
47 fetch_core_registers PARAMS ((char *, unsigned int, int, CORE_ADDR));
49 /* Fetch one or more registers from the inferior. REGNO == -1 to get
50 them all. We actually fetch more than requested, when convenient,
51 marking them as valid so we won't fetch them again. */
54 fetch_inferior_registers (regno)
57 struct regs inferior_registers;
58 struct fp_status inferior_fp_registers;
61 /* We should never be called with deferred stores, because a prerequisite
62 for writing regs is to have fetched them all (PREPARE_TO_STORE), sigh. */
68 /* Global and Out regs are fetched directly, as well as the control
69 registers. If we're getting one of the in or local regs,
70 and the stack pointer has not yet been fetched,
71 we have to do that first, since they're found in memory relative
72 to the stack pointer. */
73 if (regno < O7_REGNUM /* including -1 */
75 || (!register_valid[SP_REGNUM] && regno < I7_REGNUM))
77 if (0 != ptrace (PTRACE_GETREGS, inferior_pid,
78 (PTRACE_ARG3_TYPE) & inferior_registers, 0))
79 perror ("ptrace_getregs");
81 registers[REGISTER_BYTE (0)] = 0;
82 memcpy (®isters[REGISTER_BYTE (1)], &inferior_registers.r_g1,
83 15 * REGISTER_RAW_SIZE (G0_REGNUM));
84 *(int *) ®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
85 *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
86 *(int *) ®isters[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc;
87 *(int *) ®isters[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
89 for (i = G0_REGNUM; i <= O7_REGNUM; i++)
90 register_valid[i] = 1;
91 register_valid[Y_REGNUM] = 1;
92 register_valid[PS_REGNUM] = 1;
93 register_valid[PC_REGNUM] = 1;
94 register_valid[NPC_REGNUM] = 1;
95 /* If we don't set these valid, read_register_bytes() rereads
96 all the regs every time it is called! FIXME. */
97 register_valid[WIM_REGNUM] = 1; /* Not true yet, FIXME */
98 register_valid[TBR_REGNUM] = 1; /* Not true yet, FIXME */
99 register_valid[CPS_REGNUM] = 1; /* Not true yet, FIXME */
102 /* Floating point registers */
104 regno == FPS_REGNUM ||
105 (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31))
107 if (0 != ptrace (PTRACE_GETFPREGS, inferior_pid,
108 (PTRACE_ARG3_TYPE) & inferior_fp_registers,
110 perror ("ptrace_getfpregs");
111 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
112 sizeof inferior_fp_registers.fpu_fr);
113 memcpy (®isters[REGISTER_BYTE (FPS_REGNUM)],
114 &inferior_fp_registers.Fpu_fsr,
115 sizeof (FPU_FSR_TYPE));
116 for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
117 register_valid[i] = 1;
118 register_valid[FPS_REGNUM] = 1;
121 /* These regs are saved on the stack by the kernel. Only read them
122 all (16 ptrace calls!) if we really need them. */
125 target_read_memory (*(CORE_ADDR *) & registers[REGISTER_BYTE (SP_REGNUM)],
126 ®isters[REGISTER_BYTE (L0_REGNUM)],
127 16 * REGISTER_RAW_SIZE (L0_REGNUM));
128 for (i = L0_REGNUM; i <= I7_REGNUM; i++)
129 register_valid[i] = 1;
131 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
133 CORE_ADDR sp = *(CORE_ADDR *) & registers[REGISTER_BYTE (SP_REGNUM)];
134 i = REGISTER_BYTE (regno);
135 if (register_valid[regno])
136 printf_unfiltered ("register %d valid and read\n", regno);
137 target_read_memory (sp + i - REGISTER_BYTE (L0_REGNUM),
138 ®isters[i], REGISTER_RAW_SIZE (regno));
139 register_valid[regno] = 1;
143 /* Store our register values back into the inferior.
144 If REGNO is -1, do this for all registers.
145 Otherwise, REGNO specifies which register (so we can save time). */
148 store_inferior_registers (regno)
151 struct regs inferior_registers;
152 struct fp_status inferior_fp_registers;
153 int wanna_store = INT_REGS + STACK_REGS + FP_REGS;
155 /* First decide which pieces of machine-state we need to modify.
156 Default for regno == -1 case is all pieces. */
158 if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32)
160 wanna_store = FP_REGS;
164 if (regno == SP_REGNUM)
165 wanna_store = INT_REGS + STACK_REGS;
166 else if (regno < L0_REGNUM || regno > I7_REGNUM)
167 wanna_store = INT_REGS;
168 else if (regno == FPS_REGNUM)
169 wanna_store = FP_REGS;
171 wanna_store = STACK_REGS;
174 /* See if we're forcing the stores to happen now, or deferring. */
177 wanna_store = deferred_stores;
182 if (wanna_store == STACK_REGS)
184 /* Fall through and just store one stack reg. If we deferred
185 it, we'd have to store them all, or remember more info. */
189 deferred_stores |= wanna_store;
194 if (wanna_store & STACK_REGS)
196 CORE_ADDR sp = *(CORE_ADDR *) & registers[REGISTER_BYTE (SP_REGNUM)];
198 if (regno < 0 || regno == SP_REGNUM)
200 if (!register_valid[L0_REGNUM + 5])
202 target_write_memory (sp,
203 ®isters[REGISTER_BYTE (L0_REGNUM)],
204 16 * REGISTER_RAW_SIZE (L0_REGNUM));
208 if (!register_valid[regno])
210 target_write_memory (sp + REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM),
211 ®isters[REGISTER_BYTE (regno)],
212 REGISTER_RAW_SIZE (regno));
217 if (wanna_store & INT_REGS)
219 if (!register_valid[G1_REGNUM])
222 memcpy (&inferior_registers.r_g1, ®isters[REGISTER_BYTE (G1_REGNUM)],
223 15 * REGISTER_RAW_SIZE (G1_REGNUM));
225 inferior_registers.r_ps =
226 *(int *) ®isters[REGISTER_BYTE (PS_REGNUM)];
227 inferior_registers.r_pc =
228 *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)];
229 inferior_registers.r_npc =
230 *(int *) ®isters[REGISTER_BYTE (NPC_REGNUM)];
231 inferior_registers.r_y =
232 *(int *) ®isters[REGISTER_BYTE (Y_REGNUM)];
234 if (0 != ptrace (PTRACE_SETREGS, inferior_pid,
235 (PTRACE_ARG3_TYPE) & inferior_registers, 0))
236 perror ("ptrace_setregs");
239 if (wanna_store & FP_REGS)
241 if (!register_valid[FP0_REGNUM + 9])
243 memcpy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)],
244 sizeof inferior_fp_registers.fpu_fr);
245 memcpy (&inferior_fp_registers.Fpu_fsr,
246 ®isters[REGISTER_BYTE (FPS_REGNUM)], sizeof (FPU_FSR_TYPE));
248 ptrace (PTRACE_SETFPREGS, inferior_pid,
249 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0))
250 perror ("ptrace_setfpregs");
256 fetch_core_registers (core_reg_sect, core_reg_size, which, ignore)
258 unsigned core_reg_size;
260 CORE_ADDR ignore; /* reg addr, unused in this version */
266 /* Integer registers */
268 #define gregs ((struct regs *)core_reg_sect)
269 /* G0 *always* holds 0. */
270 *(int *) ®isters[REGISTER_BYTE (0)] = 0;
272 /* The globals and output registers. */
273 memcpy (®isters[REGISTER_BYTE (G1_REGNUM)], &gregs->r_g1,
274 15 * REGISTER_RAW_SIZE (G1_REGNUM));
275 *(int *) ®isters[REGISTER_BYTE (PS_REGNUM)] = gregs->r_ps;
276 *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)] = gregs->r_pc;
277 *(int *) ®isters[REGISTER_BYTE (NPC_REGNUM)] = gregs->r_npc;
278 *(int *) ®isters[REGISTER_BYTE (Y_REGNUM)] = gregs->r_y;
280 /* My best guess at where to get the locals and input
281 registers is exactly where they usually are, right above
282 the stack pointer. If the core dump was caused by a bus error
283 from blowing away the stack pointer (as is possible) then this
284 won't work, but it's worth the try. */
288 sp = *(int *) ®isters[REGISTER_BYTE (SP_REGNUM)];
289 if (0 != target_read_memory (sp, ®isters[REGISTER_BYTE (L0_REGNUM)],
290 16 * REGISTER_RAW_SIZE (L0_REGNUM)))
292 /* fprintf_unfiltered so user can still use gdb */
293 fprintf_unfiltered (gdb_stderr,
294 "Couldn't read input and local registers from core file\n");
301 /* Floating point registers */
303 #define fpuregs ((struct fpu *) core_reg_sect)
304 if (core_reg_size >= sizeof (struct fpu))
306 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], fpuregs->fpu_regs,
307 sizeof (fpuregs->fpu_regs));
308 memcpy (®isters[REGISTER_BYTE (FPS_REGNUM)], &fpuregs->fpu_fsr,
309 sizeof (FPU_FSR_TYPE));
312 fprintf_unfiltered (gdb_stderr, "Couldn't read float regs from core file\n");
319 return (sizeof (struct user));
323 /* Register that we are able to handle sparc core file formats.
324 FIXME: is this really bfd_target_unknown_flavour? */
326 static struct core_fns sparc_core_fns =
328 bfd_target_unknown_flavour,
329 fetch_core_registers,
334 _initialize_core_sparc ()
336 add_core_fns (&sparc_core_fns);