*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / sparc-nat.c
CommitLineData
c906108c 1/* Functions specific to running gdb native on a SPARC running SunOS4.
b6ba6518
KB
2 Copyright 1989, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b
JM
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. */
c906108c
SS
21
22#include "defs.h"
23#include "inferior.h"
24#include "target.h"
25#include "gdbcore.h"
4e052eda 26#include "regcache.h"
c906108c 27
7781cd62
DB
28#ifdef HAVE_SYS_PARAM_H
29#include <sys/param.h>
30#endif
c906108c
SS
31#include <signal.h>
32#include <sys/ptrace.h>
33#include <sys/wait.h>
34#ifdef __linux__
35#include <asm/reg.h>
36#else
37#include <machine/reg.h>
38#endif
39#include <sys/user.h>
40
41/* We don't store all registers immediately when requested, since they
42 get sent over in large chunks anyway. Instead, we accumulate most
43 of the changes and send them over once. "deferred_stores" keeps
44 track of which sets of registers we have locally-changed copies of,
45 so we only need send the groups that have changed. */
46
47#define INT_REGS 1
48#define STACK_REGS 2
49#define FP_REGS 4
50
c906108c
SS
51/* Fetch one or more registers from the inferior. REGNO == -1 to get
52 them all. We actually fetch more than requested, when convenient,
53 marking them as valid so we won't fetch them again. */
54
55void
fba45db2 56fetch_inferior_registers (int regno)
c906108c
SS
57{
58 struct regs inferior_registers;
59 struct fp_status inferior_fp_registers;
60 int i;
61
62 /* We should never be called with deferred stores, because a prerequisite
63 for writing regs is to have fetched them all (PREPARE_TO_STORE), sigh. */
c5aa993b 64 if (deferred_stores)
e1e9e218 65 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
66
67 DO_DEFERRED_STORES;
68
69 /* Global and Out regs are fetched directly, as well as the control
70 registers. If we're getting one of the in or local regs,
71 and the stack pointer has not yet been fetched,
72 we have to do that first, since they're found in memory relative
73 to the stack pointer. */
c5aa993b 74 if (regno < O7_REGNUM /* including -1 */
c906108c 75 || regno >= Y_REGNUM
8262ee23 76 || (!deprecated_register_valid[SP_REGNUM] && regno < I7_REGNUM))
c906108c 77 {
39f77062 78 if (0 != ptrace (PTRACE_GETREGS, PIDGET (inferior_ptid),
c5aa993b
JM
79 (PTRACE_ARG3_TYPE) & inferior_registers, 0))
80 perror ("ptrace_getregs");
81
524d7c18
AC
82 deprecated_registers[REGISTER_BYTE (0)] = 0;
83 memcpy (&deprecated_registers[REGISTER_BYTE (1)],
84 &inferior_registers.r_g1, 15 * REGISTER_RAW_SIZE (G0_REGNUM));
85 *(int *) &deprecated_registers[REGISTER_BYTE (PS_REGNUM)]
86 = inferior_registers.r_ps;
87 *(int *) &deprecated_registers[REGISTER_BYTE (PC_REGNUM)]
88 = inferior_registers.r_pc;
89 *(int *) &deprecated_registers[REGISTER_BYTE (NPC_REGNUM)]
90 = inferior_registers.r_npc;
c5aa993b 91 *(int *) &registers[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
c906108c
SS
92
93 for (i = G0_REGNUM; i <= O7_REGNUM; i++)
8262ee23
AC
94 deprecated_register_valid[i] = 1;
95 deprecated_register_valid[Y_REGNUM] = 1;
96 deprecated_register_valid[PS_REGNUM] = 1;
97 deprecated_register_valid[PC_REGNUM] = 1;
98 deprecated_register_valid[NPC_REGNUM] = 1;
c906108c 99 /* If we don't set these valid, read_register_bytes() rereads
c5aa993b 100 all the regs every time it is called! FIXME. */
8262ee23
AC
101 deprecated_register_valid[WIM_REGNUM] = 1; /* Not true yet, FIXME */
102 deprecated_register_valid[TBR_REGNUM] = 1; /* Not true yet, FIXME */
103 deprecated_register_valid[CPS_REGNUM] = 1; /* Not true yet, FIXME */
c906108c
SS
104 }
105
106 /* Floating point registers */
107 if (regno == -1 ||
108 regno == FPS_REGNUM ||
109 (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31))
110 {
39f77062 111 if (0 != ptrace (PTRACE_GETFPREGS, PIDGET (inferior_ptid),
c5aa993b 112 (PTRACE_ARG3_TYPE) & inferior_fp_registers,
c906108c 113 0))
c5aa993b 114 perror ("ptrace_getfpregs");
524d7c18
AC
115 memcpy (&deprecated_registers[REGISTER_BYTE (FP0_REGNUM)],
116 &inferior_fp_registers, sizeof inferior_fp_registers.fpu_fr);
117 memcpy (&deprecated_registers[REGISTER_BYTE (FPS_REGNUM)],
118 &inferior_fp_registers.Fpu_fsr, sizeof (FPU_FSR_TYPE));
c5aa993b 119 for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
8262ee23
AC
120 deprecated_register_valid[i] = 1;
121 deprecated_register_valid[FPS_REGNUM] = 1;
c906108c
SS
122 }
123
124 /* These regs are saved on the stack by the kernel. Only read them
125 all (16 ptrace calls!) if we really need them. */
126 if (regno == -1)
127 {
524d7c18
AC
128 CORE_ADDR sp = *(unsigned int *) & deprecated_registers[REGISTER_BYTE (SP_REGNUM)];
129 target_read_memory (sp, &deprecated_registers[REGISTER_BYTE (L0_REGNUM)],
c5aa993b 130 16 * REGISTER_RAW_SIZE (L0_REGNUM));
c906108c 131 for (i = L0_REGNUM; i <= I7_REGNUM; i++)
8262ee23 132 deprecated_register_valid[i] = 1;
c906108c
SS
133 }
134 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
135 {
524d7c18 136 CORE_ADDR sp = *(unsigned int *) & deprecated_registers[REGISTER_BYTE (SP_REGNUM)];
c906108c 137 i = REGISTER_BYTE (regno);
8262ee23 138 if (deprecated_register_valid[regno])
c5aa993b 139 printf_unfiltered ("register %d valid and read\n", regno);
c906108c 140 target_read_memory (sp + i - REGISTER_BYTE (L0_REGNUM),
524d7c18 141 &deprecated_registers[i], REGISTER_RAW_SIZE (regno));
8262ee23 142 deprecated_register_valid[regno] = 1;
c906108c
SS
143 }
144}
145
146/* Store our register values back into the inferior.
147 If REGNO is -1, do this for all registers.
148 Otherwise, REGNO specifies which register (so we can save time). */
149
150void
fba45db2 151store_inferior_registers (int regno)
c906108c
SS
152{
153 struct regs inferior_registers;
154 struct fp_status inferior_fp_registers;
155 int wanna_store = INT_REGS + STACK_REGS + FP_REGS;
156
157 /* First decide which pieces of machine-state we need to modify.
158 Default for regno == -1 case is all pieces. */
159 if (regno >= 0)
e1925118
DM
160 {
161 if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32)
162 {
c906108c 163 wanna_store = FP_REGS;
e1925118
DM
164 }
165 else
166 {
167 if (regno == SP_REGNUM)
168 wanna_store = INT_REGS + STACK_REGS;
169 else if (regno < L0_REGNUM || regno > I7_REGNUM)
170 wanna_store = INT_REGS;
171 else if (regno == FPS_REGNUM)
172 wanna_store = FP_REGS;
173 else
174 wanna_store = STACK_REGS;
175 }
176 }
c906108c
SS
177
178 /* See if we're forcing the stores to happen now, or deferring. */
179 if (regno == -2)
180 {
181 wanna_store = deferred_stores;
182 deferred_stores = 0;
183 }
184 else
185 {
186 if (wanna_store == STACK_REGS)
187 {
188 /* Fall through and just store one stack reg. If we deferred
189 it, we'd have to store them all, or remember more info. */
190 }
191 else
192 {
193 deferred_stores |= wanna_store;
194 return;
195 }
196 }
197
198 if (wanna_store & STACK_REGS)
199 {
524d7c18 200 CORE_ADDR sp = *(unsigned int *) & deprecated_registers[REGISTER_BYTE (SP_REGNUM)];
c906108c
SS
201
202 if (regno < 0 || regno == SP_REGNUM)
203 {
8262ee23 204 if (!deprecated_register_valid[L0_REGNUM + 5])
e1e9e218 205 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c5aa993b 206 target_write_memory (sp,
524d7c18 207 &deprecated_registers[REGISTER_BYTE (L0_REGNUM)],
c5aa993b 208 16 * REGISTER_RAW_SIZE (L0_REGNUM));
c906108c
SS
209 }
210 else
211 {
8262ee23 212 if (!deprecated_register_valid[regno])
e1e9e218 213 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c 214 target_write_memory (sp + REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM),
524d7c18 215 &deprecated_registers[REGISTER_BYTE (regno)],
c906108c
SS
216 REGISTER_RAW_SIZE (regno));
217 }
c5aa993b 218
c906108c
SS
219 }
220
221 if (wanna_store & INT_REGS)
222 {
8262ee23 223 if (!deprecated_register_valid[G1_REGNUM])
e1e9e218 224 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c 225
524d7c18
AC
226 memcpy (&inferior_registers.r_g1,
227 &deprecated_registers[REGISTER_BYTE (G1_REGNUM)],
c906108c
SS
228 15 * REGISTER_RAW_SIZE (G1_REGNUM));
229
230 inferior_registers.r_ps =
524d7c18 231 *(int *) &deprecated_registers[REGISTER_BYTE (PS_REGNUM)];
c906108c 232 inferior_registers.r_pc =
524d7c18 233 *(int *) &deprecated_registers[REGISTER_BYTE (PC_REGNUM)];
c906108c 234 inferior_registers.r_npc =
524d7c18 235 *(int *) &deprecated_registers[REGISTER_BYTE (NPC_REGNUM)];
c906108c 236 inferior_registers.r_y =
524d7c18 237 *(int *) &deprecated_registers[REGISTER_BYTE (Y_REGNUM)];
c906108c 238
39f77062 239 if (0 != ptrace (PTRACE_SETREGS, PIDGET (inferior_ptid),
c5aa993b
JM
240 (PTRACE_ARG3_TYPE) & inferior_registers, 0))
241 perror ("ptrace_setregs");
c906108c
SS
242 }
243
244 if (wanna_store & FP_REGS)
245 {
8262ee23 246 if (!deprecated_register_valid[FP0_REGNUM + 9])
e1e9e218 247 internal_error (__FILE__, __LINE__, "failed internal consistency check");
524d7c18
AC
248 memcpy (&inferior_fp_registers,
249 &deprecated_registers[REGISTER_BYTE (FP0_REGNUM)],
c906108c 250 sizeof inferior_fp_registers.fpu_fr);
c5aa993b 251 memcpy (&inferior_fp_registers.Fpu_fsr,
524d7c18
AC
252 &deprecated_registers[REGISTER_BYTE (FPS_REGNUM)],
253 sizeof (FPU_FSR_TYPE));
c906108c 254 if (0 !=
39f77062 255 ptrace (PTRACE_SETFPREGS, PIDGET (inferior_ptid),
c5aa993b
JM
256 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0))
257 perror ("ptrace_setfpregs");
c906108c
SS
258 }
259}
260
c67b4c45
KB
261/* Provide registers to GDB from a core file.
262
263 CORE_REG_SECT points to an array of bytes, which are the contents
264 of a `note' from a core file which BFD thinks might contain
265 register contents. CORE_REG_SIZE is its size.
266
267 WHICH says which register set corelow suspects this is:
268 0 --- the general-purpose register set
269 2 --- the floating-point register set
270
271 IGNORE is unused. */
c906108c
SS
272
273static void
c67b4c45
KB
274fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
275 int which, CORE_ADDR ignore)
c906108c
SS
276{
277
c5aa993b
JM
278 if (which == 0)
279 {
c906108c 280
c5aa993b 281 /* Integer registers */
c906108c
SS
282
283#define gregs ((struct regs *)core_reg_sect)
c5aa993b 284 /* G0 *always* holds 0. */
524d7c18 285 *(int *) &deprecated_registers[REGISTER_BYTE (0)] = 0;
c906108c 286
c5aa993b 287 /* The globals and output registers. */
524d7c18 288 memcpy (&deprecated_registers[REGISTER_BYTE (G1_REGNUM)], &gregs->r_g1,
c5aa993b 289 15 * REGISTER_RAW_SIZE (G1_REGNUM));
524d7c18
AC
290 *(int *) &deprecated_registers[REGISTER_BYTE (PS_REGNUM)] = gregs->r_ps;
291 *(int *) &deprecated_registers[REGISTER_BYTE (PC_REGNUM)] = gregs->r_pc;
292 *(int *) &deprecated_registers[REGISTER_BYTE (NPC_REGNUM)] = gregs->r_npc;
293 *(int *) &deprecated_registers[REGISTER_BYTE (Y_REGNUM)] = gregs->r_y;
c5aa993b
JM
294
295 /* My best guess at where to get the locals and input
296 registers is exactly where they usually are, right above
297 the stack pointer. If the core dump was caused by a bus error
298 from blowing away the stack pointer (as is possible) then this
299 won't work, but it's worth the try. */
300 {
301 int sp;
302
524d7c18
AC
303 sp = *(int *) &deprecated_registers[REGISTER_BYTE (SP_REGNUM)];
304 if (0 != target_read_memory (sp,
305 &deprecated_registers[REGISTER_BYTE (L0_REGNUM)],
c5aa993b
JM
306 16 * REGISTER_RAW_SIZE (L0_REGNUM)))
307 {
308 /* fprintf_unfiltered so user can still use gdb */
309 fprintf_unfiltered (gdb_stderr,
310 "Couldn't read input and local registers from core file\n");
311 }
312 }
c906108c 313 }
c5aa993b
JM
314 else if (which == 2)
315 {
c906108c 316
c5aa993b 317 /* Floating point registers */
c906108c
SS
318
319#define fpuregs ((struct fpu *) core_reg_sect)
c5aa993b
JM
320 if (core_reg_size >= sizeof (struct fpu))
321 {
524d7c18
AC
322 memcpy (&deprecated_registers[REGISTER_BYTE (FP0_REGNUM)],
323 fpuregs->fpu_regs, sizeof (fpuregs->fpu_regs));
324 memcpy (&deprecated_registers[REGISTER_BYTE (FPS_REGNUM)],
325 &fpuregs->fpu_fsr, sizeof (FPU_FSR_TYPE));
c5aa993b
JM
326 }
327 else
328 fprintf_unfiltered (gdb_stderr, "Couldn't read float regs from core file\n");
329 }
c906108c
SS
330}
331
332int
fba45db2 333kernel_u_size (void)
c906108c
SS
334{
335 return (sizeof (struct user));
336}
c906108c 337\f
c5aa993b 338
c906108c
SS
339/* Register that we are able to handle sparc core file formats.
340 FIXME: is this really bfd_target_unknown_flavour? */
341
342static struct core_fns sparc_core_fns =
343{
2acceee2
JM
344 bfd_target_unknown_flavour, /* core_flavour */
345 default_check_format, /* check_format */
346 default_core_sniffer, /* core_sniffer */
347 fetch_core_registers, /* core_read_registers */
348 NULL /* next */
c906108c
SS
349};
350
351void
fba45db2 352_initialize_core_sparc (void)
c906108c
SS
353{
354 add_core_fns (&sparc_core_fns);
355}
This page took 0.294692 seconds and 4 git commands to generate.