1 /* Functions specific to running gdb native on a SPARC running SunOS4.
3 Copyright 1989, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2002, 2003 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
29 #include "sparc-tdep.h"
31 #ifdef HAVE_SYS_PARAM_H
32 #include <sys/param.h>
35 #include <sys/ptrace.h>
40 #include <machine/reg.h>
44 /* We don't store all registers immediately when requested, since they
45 get sent over in large chunks anyway. Instead, we accumulate most
46 of the changes and send them over once. "deferred_stores" keeps
47 track of which sets of registers we have locally-changed copies of,
48 so we only need send the groups that have changed. */
54 /* Fetch one or more registers from the inferior. REGNO == -1 to get
55 them all. We actually fetch more than requested, when convenient,
56 marking them as valid so we won't fetch them again. */
59 fetch_inferior_registers (int regno
)
61 struct regs inferior_registers
;
62 struct fp_status inferior_fp_registers
;
66 /* NOTE: cagney/2002-12-03: This code assumes that the currently
67 selected light weight processes' registers can be written
68 directly into the selected thread's register cache. This works
69 fine when given an 1:1 LWP:thread model (such as found on
70 GNU/Linux) but will, likely, have problems when used on an N:1
71 (userland threads) or N:M (userland multiple LWP) model. In the
72 case of the latter two, the LWP's registers do not necessarily
73 belong to the selected thread (the LWP could be in the middle of
74 executing the thread switch code).
76 These functions should instead be paramaterized with an explicit
77 object (struct regcache, struct thread_info?) into which the LWPs
78 registers can be written. */
80 fetch_pid
= TIDGET (inferior_ptid
);
82 fetch_pid
= PIDGET (inferior_ptid
);
84 /* We should never be called with deferred stores, because a prerequisite
85 for writing regs is to have fetched them all (PREPARE_TO_STORE), sigh. */
87 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
91 /* Global and Out regs are fetched directly, as well as the control
92 registers. If we're getting one of the in or local regs,
93 and the stack pointer has not yet been fetched,
94 we have to do that first, since they're found in memory relative
95 to the stack pointer. */
96 if (regno
< O7_REGNUM
/* including -1 */
98 || (!deprecated_register_valid
[SP_REGNUM
] && regno
< I7_REGNUM
))
100 if (0 != ptrace (PTRACE_GETREGS
, fetch_pid
,
101 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0))
102 perror ("ptrace_getregs");
104 deprecated_registers
[REGISTER_BYTE (0)] = 0;
105 memcpy (&deprecated_registers
[REGISTER_BYTE (1)],
106 &inferior_registers
.r_g1
, 15 * REGISTER_RAW_SIZE (G0_REGNUM
));
107 *(int *) &deprecated_registers
[REGISTER_BYTE (PS_REGNUM
)]
108 = inferior_registers
.r_ps
;
109 *(int *) &deprecated_registers
[REGISTER_BYTE (PC_REGNUM
)]
110 = inferior_registers
.r_pc
;
111 *(int *) &deprecated_registers
[REGISTER_BYTE (NPC_REGNUM
)]
112 = inferior_registers
.r_npc
;
113 *(int *) &deprecated_registers
[REGISTER_BYTE (Y_REGNUM
)] = inferior_registers
.r_y
;
115 for (i
= G0_REGNUM
; i
<= O7_REGNUM
; i
++)
116 deprecated_register_valid
[i
] = 1;
117 deprecated_register_valid
[Y_REGNUM
] = 1;
118 deprecated_register_valid
[PS_REGNUM
] = 1;
119 deprecated_register_valid
[PC_REGNUM
] = 1;
120 deprecated_register_valid
[NPC_REGNUM
] = 1;
121 /* If we don't set these valid, read_register_bytes() rereads
122 all the regs every time it is called! FIXME. */
123 deprecated_register_valid
[WIM_REGNUM
] = 1; /* Not true yet, FIXME */
124 deprecated_register_valid
[TBR_REGNUM
] = 1; /* Not true yet, FIXME */
125 deprecated_register_valid
[CPS_REGNUM
] = 1; /* Not true yet, FIXME */
128 /* Floating point registers */
130 regno
== FPS_REGNUM
||
131 (regno
>= FP0_REGNUM
&& regno
<= FP0_REGNUM
+ 31))
133 if (0 != ptrace (PTRACE_GETFPREGS
, fetch_pid
,
134 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
,
136 perror ("ptrace_getfpregs");
137 memcpy (&deprecated_registers
[REGISTER_BYTE (FP0_REGNUM
)],
138 &inferior_fp_registers
, sizeof inferior_fp_registers
.fpu_fr
);
139 memcpy (&deprecated_registers
[REGISTER_BYTE (FPS_REGNUM
)],
140 &inferior_fp_registers
.Fpu_fsr
, sizeof (FPU_FSR_TYPE
));
141 for (i
= FP0_REGNUM
; i
<= FP0_REGNUM
+ 31; i
++)
142 deprecated_register_valid
[i
] = 1;
143 deprecated_register_valid
[FPS_REGNUM
] = 1;
146 /* These regs are saved on the stack by the kernel. Only read them
147 all (16 ptrace calls!) if we really need them. */
150 CORE_ADDR sp
= *(unsigned int *) & deprecated_registers
[REGISTER_BYTE (SP_REGNUM
)];
151 target_read_memory (sp
, &deprecated_registers
[REGISTER_BYTE (L0_REGNUM
)],
152 16 * REGISTER_RAW_SIZE (L0_REGNUM
));
153 for (i
= L0_REGNUM
; i
<= I7_REGNUM
; i
++)
154 deprecated_register_valid
[i
] = 1;
156 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
158 CORE_ADDR sp
= *(unsigned int *) & deprecated_registers
[REGISTER_BYTE (SP_REGNUM
)];
159 i
= REGISTER_BYTE (regno
);
160 if (deprecated_register_valid
[regno
])
161 printf_unfiltered ("register %d valid and read\n", regno
);
162 target_read_memory (sp
+ i
- REGISTER_BYTE (L0_REGNUM
),
163 &deprecated_registers
[i
], REGISTER_RAW_SIZE (regno
));
164 deprecated_register_valid
[regno
] = 1;
168 /* Store our register values back into the inferior.
169 If REGNO is -1, do this for all registers.
170 Otherwise, REGNO specifies which register (so we can save time). */
173 store_inferior_registers (int regno
)
175 struct regs inferior_registers
;
176 struct fp_status inferior_fp_registers
;
177 int wanna_store
= INT_REGS
+ STACK_REGS
+ FP_REGS
;
180 /* NOTE: cagney/2002-12-02: See comment in fetch_inferior_registers
181 about threaded assumptions. */
182 store_pid
= TIDGET (inferior_ptid
);
184 store_pid
= PIDGET (inferior_ptid
);
186 /* First decide which pieces of machine-state we need to modify.
187 Default for regno == -1 case is all pieces. */
190 if (FP0_REGNUM
<= regno
&& regno
< FP0_REGNUM
+ 32)
192 wanna_store
= FP_REGS
;
196 if (regno
== SP_REGNUM
)
197 wanna_store
= INT_REGS
+ STACK_REGS
;
198 else if (regno
< L0_REGNUM
|| regno
> I7_REGNUM
)
199 wanna_store
= INT_REGS
;
200 else if (regno
== FPS_REGNUM
)
201 wanna_store
= FP_REGS
;
203 wanna_store
= STACK_REGS
;
207 /* See if we're forcing the stores to happen now, or deferring. */
210 wanna_store
= deferred_stores
;
215 if (wanna_store
== STACK_REGS
)
217 /* Fall through and just store one stack reg. If we deferred
218 it, we'd have to store them all, or remember more info. */
222 deferred_stores
|= wanna_store
;
227 if (wanna_store
& STACK_REGS
)
229 CORE_ADDR sp
= *(unsigned int *) & deprecated_registers
[REGISTER_BYTE (SP_REGNUM
)];
231 if (regno
< 0 || regno
== SP_REGNUM
)
233 if (!deprecated_register_valid
[L0_REGNUM
+ 5])
234 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
235 target_write_memory (sp
,
236 &deprecated_registers
[REGISTER_BYTE (L0_REGNUM
)],
237 16 * REGISTER_RAW_SIZE (L0_REGNUM
));
241 if (!deprecated_register_valid
[regno
])
242 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
243 target_write_memory (sp
+ REGISTER_BYTE (regno
) - REGISTER_BYTE (L0_REGNUM
),
244 &deprecated_registers
[REGISTER_BYTE (regno
)],
245 REGISTER_RAW_SIZE (regno
));
250 if (wanna_store
& INT_REGS
)
252 if (!deprecated_register_valid
[G1_REGNUM
])
253 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
255 memcpy (&inferior_registers
.r_g1
,
256 &deprecated_registers
[REGISTER_BYTE (G1_REGNUM
)],
257 15 * REGISTER_RAW_SIZE (G1_REGNUM
));
259 inferior_registers
.r_ps
=
260 *(int *) &deprecated_registers
[REGISTER_BYTE (PS_REGNUM
)];
261 inferior_registers
.r_pc
=
262 *(int *) &deprecated_registers
[REGISTER_BYTE (PC_REGNUM
)];
263 inferior_registers
.r_npc
=
264 *(int *) &deprecated_registers
[REGISTER_BYTE (NPC_REGNUM
)];
265 inferior_registers
.r_y
=
266 *(int *) &deprecated_registers
[REGISTER_BYTE (Y_REGNUM
)];
268 if (0 != ptrace (PTRACE_SETREGS
, store_pid
,
269 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0))
270 perror ("ptrace_setregs");
273 if (wanna_store
& FP_REGS
)
275 if (!deprecated_register_valid
[FP0_REGNUM
+ 9])
276 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
277 memcpy (&inferior_fp_registers
,
278 &deprecated_registers
[REGISTER_BYTE (FP0_REGNUM
)],
279 sizeof inferior_fp_registers
.fpu_fr
);
280 memcpy (&inferior_fp_registers
.Fpu_fsr
,
281 &deprecated_registers
[REGISTER_BYTE (FPS_REGNUM
)],
282 sizeof (FPU_FSR_TYPE
));
284 ptrace (PTRACE_SETFPREGS
, store_pid
,
285 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0))
286 perror ("ptrace_setfpregs");
290 /* Provide registers to GDB from a core file.
292 CORE_REG_SECT points to an array of bytes, which are the contents
293 of a `note' from a core file which BFD thinks might contain
294 register contents. CORE_REG_SIZE is its size.
296 WHICH says which register set corelow suspects this is:
297 0 --- the general-purpose register set
298 2 --- the floating-point register set
303 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
304 int which
, CORE_ADDR ignore
)
310 /* Integer registers */
312 #define gregs ((struct regs *)core_reg_sect)
313 /* G0 *always* holds 0. */
314 *(int *) &deprecated_registers
[REGISTER_BYTE (0)] = 0;
316 /* The globals and output registers. */
317 memcpy (&deprecated_registers
[REGISTER_BYTE (G1_REGNUM
)], &gregs
->r_g1
,
318 15 * REGISTER_RAW_SIZE (G1_REGNUM
));
319 *(int *) &deprecated_registers
[REGISTER_BYTE (PS_REGNUM
)] = gregs
->r_ps
;
320 *(int *) &deprecated_registers
[REGISTER_BYTE (PC_REGNUM
)] = gregs
->r_pc
;
321 *(int *) &deprecated_registers
[REGISTER_BYTE (NPC_REGNUM
)] = gregs
->r_npc
;
322 *(int *) &deprecated_registers
[REGISTER_BYTE (Y_REGNUM
)] = gregs
->r_y
;
324 /* My best guess at where to get the locals and input
325 registers is exactly where they usually are, right above
326 the stack pointer. If the core dump was caused by a bus error
327 from blowing away the stack pointer (as is possible) then this
328 won't work, but it's worth the try. */
332 sp
= *(int *) &deprecated_registers
[REGISTER_BYTE (SP_REGNUM
)];
333 if (0 != target_read_memory (sp
,
334 &deprecated_registers
[REGISTER_BYTE (L0_REGNUM
)],
335 16 * REGISTER_RAW_SIZE (L0_REGNUM
)))
337 /* fprintf_unfiltered so user can still use gdb */
338 fprintf_unfiltered (gdb_stderr
,
339 "Couldn't read input and local registers from core file\n");
346 /* Floating point registers */
348 #define fpuregs ((struct fpu *) core_reg_sect)
349 if (core_reg_size
>= sizeof (struct fpu
))
351 memcpy (&deprecated_registers
[REGISTER_BYTE (FP0_REGNUM
)],
352 fpuregs
->fpu_regs
, sizeof (fpuregs
->fpu_regs
));
353 memcpy (&deprecated_registers
[REGISTER_BYTE (FPS_REGNUM
)],
354 &fpuregs
->fpu_fsr
, sizeof (FPU_FSR_TYPE
));
357 fprintf_unfiltered (gdb_stderr
, "Couldn't read float regs from core file\n");
364 return (sizeof (struct user
));
368 /* Register that we are able to handle sparc core file formats.
369 FIXME: is this really bfd_target_unknown_flavour? */
371 static struct core_fns sparc_core_fns
=
373 bfd_target_unknown_flavour
, /* core_flavour */
374 default_check_format
, /* check_format */
375 default_core_sniffer
, /* core_sniffer */
376 fetch_core_registers
, /* core_read_registers */
381 _initialize_core_sparc (void)
383 add_core_fns (&sparc_core_fns
);