2003-11-22 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / sparc-nat.c
CommitLineData
c906108c 1/* Functions specific to running gdb native on a SPARC running SunOS4.
2555fe1a
AC
2
3 Copyright 1989, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2002, 2003 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
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. */
c906108c
SS
22
23#include "defs.h"
24#include "inferior.h"
25#include "target.h"
26#include "gdbcore.h"
4e052eda 27#include "regcache.h"
c906108c 28
c139e7d9
DJ
29#include "sparc-tdep.h"
30
7781cd62
DB
31#ifdef HAVE_SYS_PARAM_H
32#include <sys/param.h>
33#endif
c906108c
SS
34#include <signal.h>
35#include <sys/ptrace.h>
2555fe1a 36#include "gdb_wait.h"
c906108c
SS
37#ifdef __linux__
38#include <asm/reg.h>
39#else
40#include <machine/reg.h>
41#endif
42#include <sys/user.h>
43
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. */
49
50#define INT_REGS 1
51#define STACK_REGS 2
52#define FP_REGS 4
53
c906108c
SS
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. */
57
58void
fba45db2 59fetch_inferior_registers (int regno)
c906108c
SS
60{
61 struct regs inferior_registers;
62 struct fp_status inferior_fp_registers;
63 int i;
02ae7771
AC
64 int fetch_pid;
65
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).
75
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. */
79
80 fetch_pid = TIDGET (inferior_ptid);
81 if (fetch_pid == 0)
82 fetch_pid = PIDGET (inferior_ptid);
c906108c
SS
83
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. */
c5aa993b 86 if (deferred_stores)
e1e9e218 87 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
88
89 DO_DEFERRED_STORES;
90
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. */
c5aa993b 96 if (regno < O7_REGNUM /* including -1 */
c906108c 97 || regno >= Y_REGNUM
8262ee23 98 || (!deprecated_register_valid[SP_REGNUM] && regno < I7_REGNUM))
c906108c 99 {
02ae7771 100 if (0 != ptrace (PTRACE_GETREGS, fetch_pid,
c5aa993b
JM
101 (PTRACE_ARG3_TYPE) & inferior_registers, 0))
102 perror ("ptrace_getregs");
103
62700349
AC
104 deprecated_registers[DEPRECATED_REGISTER_BYTE (0)] = 0;
105 memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (1)],
12c266ea 106 &inferior_registers.r_g1, 15 * DEPRECATED_REGISTER_RAW_SIZE (G0_REGNUM));
62700349 107 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PS_REGNUM)]
524d7c18 108 = inferior_registers.r_ps;
62700349 109 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PC_REGNUM)]
524d7c18 110 = inferior_registers.r_pc;
efe59759 111 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (DEPRECATED_NPC_REGNUM)]
524d7c18 112 = inferior_registers.r_npc;
62700349 113 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
c906108c
SS
114
115 for (i = G0_REGNUM; i <= O7_REGNUM; i++)
8262ee23
AC
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;
efe59759 120 deprecated_register_valid[DEPRECATED_NPC_REGNUM] = 1;
c906108c 121 /* If we don't set these valid, read_register_bytes() rereads
c5aa993b 122 all the regs every time it is called! FIXME. */
8262ee23
AC
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 */
c906108c
SS
126 }
127
128 /* Floating point registers */
129 if (regno == -1 ||
130 regno == FPS_REGNUM ||
131 (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31))
132 {
02ae7771 133 if (0 != ptrace (PTRACE_GETFPREGS, fetch_pid,
c5aa993b 134 (PTRACE_ARG3_TYPE) & inferior_fp_registers,
c906108c 135 0))
c5aa993b 136 perror ("ptrace_getfpregs");
62700349 137 memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (FP0_REGNUM)],
524d7c18 138 &inferior_fp_registers, sizeof inferior_fp_registers.fpu_fr);
62700349 139 memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (FPS_REGNUM)],
524d7c18 140 &inferior_fp_registers.Fpu_fsr, sizeof (FPU_FSR_TYPE));
c5aa993b 141 for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
8262ee23
AC
142 deprecated_register_valid[i] = 1;
143 deprecated_register_valid[FPS_REGNUM] = 1;
c906108c
SS
144 }
145
146 /* These regs are saved on the stack by the kernel. Only read them
147 all (16 ptrace calls!) if we really need them. */
148 if (regno == -1)
149 {
62700349
AC
150 CORE_ADDR sp = *(unsigned int *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (SP_REGNUM)];
151 target_read_memory (sp, &deprecated_registers[DEPRECATED_REGISTER_BYTE (L0_REGNUM)],
12c266ea 152 16 * DEPRECATED_REGISTER_RAW_SIZE (L0_REGNUM));
c906108c 153 for (i = L0_REGNUM; i <= I7_REGNUM; i++)
8262ee23 154 deprecated_register_valid[i] = 1;
c906108c
SS
155 }
156 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
157 {
62700349
AC
158 CORE_ADDR sp = *(unsigned int *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (SP_REGNUM)];
159 i = DEPRECATED_REGISTER_BYTE (regno);
8262ee23 160 if (deprecated_register_valid[regno])
c5aa993b 161 printf_unfiltered ("register %d valid and read\n", regno);
62700349 162 target_read_memory (sp + i - DEPRECATED_REGISTER_BYTE (L0_REGNUM),
12c266ea 163 &deprecated_registers[i], DEPRECATED_REGISTER_RAW_SIZE (regno));
8262ee23 164 deprecated_register_valid[regno] = 1;
c906108c
SS
165 }
166}
167
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). */
171
172void
fba45db2 173store_inferior_registers (int regno)
c906108c
SS
174{
175 struct regs inferior_registers;
176 struct fp_status inferior_fp_registers;
177 int wanna_store = INT_REGS + STACK_REGS + FP_REGS;
02ae7771
AC
178 int store_pid;
179
180 /* NOTE: cagney/2002-12-02: See comment in fetch_inferior_registers
181 about threaded assumptions. */
182 store_pid = TIDGET (inferior_ptid);
183 if (store_pid == 0)
184 store_pid = PIDGET (inferior_ptid);
c906108c
SS
185
186 /* First decide which pieces of machine-state we need to modify.
187 Default for regno == -1 case is all pieces. */
188 if (regno >= 0)
e1925118
DM
189 {
190 if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32)
191 {
c906108c 192 wanna_store = FP_REGS;
e1925118
DM
193 }
194 else
195 {
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;
202 else
203 wanna_store = STACK_REGS;
204 }
205 }
c906108c
SS
206
207 /* See if we're forcing the stores to happen now, or deferring. */
208 if (regno == -2)
209 {
210 wanna_store = deferred_stores;
211 deferred_stores = 0;
212 }
213 else
214 {
215 if (wanna_store == STACK_REGS)
216 {
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. */
219 }
220 else
221 {
222 deferred_stores |= wanna_store;
223 return;
224 }
225 }
226
227 if (wanna_store & STACK_REGS)
228 {
62700349 229 CORE_ADDR sp = *(unsigned int *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (SP_REGNUM)];
c906108c
SS
230
231 if (regno < 0 || regno == SP_REGNUM)
232 {
8262ee23 233 if (!deprecated_register_valid[L0_REGNUM + 5])
e1e9e218 234 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c5aa993b 235 target_write_memory (sp,
62700349 236 &deprecated_registers[DEPRECATED_REGISTER_BYTE (L0_REGNUM)],
12c266ea 237 16 * DEPRECATED_REGISTER_RAW_SIZE (L0_REGNUM));
c906108c
SS
238 }
239 else
240 {
8262ee23 241 if (!deprecated_register_valid[regno])
e1e9e218 242 internal_error (__FILE__, __LINE__, "failed internal consistency check");
62700349
AC
243 target_write_memory (sp + DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (L0_REGNUM),
244 &deprecated_registers[DEPRECATED_REGISTER_BYTE (regno)],
12c266ea 245 DEPRECATED_REGISTER_RAW_SIZE (regno));
c906108c 246 }
c5aa993b 247
c906108c
SS
248 }
249
250 if (wanna_store & INT_REGS)
251 {
8262ee23 252 if (!deprecated_register_valid[G1_REGNUM])
e1e9e218 253 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c 254
524d7c18 255 memcpy (&inferior_registers.r_g1,
62700349 256 &deprecated_registers[DEPRECATED_REGISTER_BYTE (G1_REGNUM)],
12c266ea 257 15 * DEPRECATED_REGISTER_RAW_SIZE (G1_REGNUM));
c906108c
SS
258
259 inferior_registers.r_ps =
62700349 260 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PS_REGNUM)];
c906108c 261 inferior_registers.r_pc =
62700349 262 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PC_REGNUM)];
c906108c 263 inferior_registers.r_npc =
efe59759 264 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (DEPRECATED_NPC_REGNUM)];
c906108c 265 inferior_registers.r_y =
62700349 266 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (Y_REGNUM)];
c906108c 267
02ae7771 268 if (0 != ptrace (PTRACE_SETREGS, store_pid,
c5aa993b
JM
269 (PTRACE_ARG3_TYPE) & inferior_registers, 0))
270 perror ("ptrace_setregs");
c906108c
SS
271 }
272
273 if (wanna_store & FP_REGS)
274 {
8262ee23 275 if (!deprecated_register_valid[FP0_REGNUM + 9])
e1e9e218 276 internal_error (__FILE__, __LINE__, "failed internal consistency check");
524d7c18 277 memcpy (&inferior_fp_registers,
62700349 278 &deprecated_registers[DEPRECATED_REGISTER_BYTE (FP0_REGNUM)],
c906108c 279 sizeof inferior_fp_registers.fpu_fr);
c5aa993b 280 memcpy (&inferior_fp_registers.Fpu_fsr,
62700349 281 &deprecated_registers[DEPRECATED_REGISTER_BYTE (FPS_REGNUM)],
524d7c18 282 sizeof (FPU_FSR_TYPE));
c906108c 283 if (0 !=
02ae7771 284 ptrace (PTRACE_SETFPREGS, store_pid,
c5aa993b
JM
285 (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0))
286 perror ("ptrace_setfpregs");
c906108c
SS
287 }
288}
289
c67b4c45
KB
290/* Provide registers to GDB from a core file.
291
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.
295
296 WHICH says which register set corelow suspects this is:
297 0 --- the general-purpose register set
298 2 --- the floating-point register set
299
300 IGNORE is unused. */
c906108c
SS
301
302static void
c67b4c45
KB
303fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
304 int which, CORE_ADDR ignore)
c906108c
SS
305{
306
c5aa993b
JM
307 if (which == 0)
308 {
c906108c 309
c5aa993b 310 /* Integer registers */
c906108c
SS
311
312#define gregs ((struct regs *)core_reg_sect)
c5aa993b 313 /* G0 *always* holds 0. */
62700349 314 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (0)] = 0;
c906108c 315
c5aa993b 316 /* The globals and output registers. */
62700349 317 memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (G1_REGNUM)], &gregs->r_g1,
12c266ea 318 15 * DEPRECATED_REGISTER_RAW_SIZE (G1_REGNUM));
62700349
AC
319 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PS_REGNUM)] = gregs->r_ps;
320 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PC_REGNUM)] = gregs->r_pc;
efe59759 321 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (DEPRECATED_NPC_REGNUM)] = gregs->r_npc;
62700349 322 *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (Y_REGNUM)] = gregs->r_y;
c5aa993b
JM
323
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. */
329 {
330 int sp;
331
62700349 332 sp = *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (SP_REGNUM)];
524d7c18 333 if (0 != target_read_memory (sp,
62700349 334 &deprecated_registers[DEPRECATED_REGISTER_BYTE (L0_REGNUM)],
12c266ea 335 16 * DEPRECATED_REGISTER_RAW_SIZE (L0_REGNUM)))
c5aa993b
JM
336 {
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");
340 }
341 }
c906108c 342 }
c5aa993b
JM
343 else if (which == 2)
344 {
c906108c 345
c5aa993b 346 /* Floating point registers */
c906108c
SS
347
348#define fpuregs ((struct fpu *) core_reg_sect)
c5aa993b
JM
349 if (core_reg_size >= sizeof (struct fpu))
350 {
62700349 351 memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (FP0_REGNUM)],
524d7c18 352 fpuregs->fpu_regs, sizeof (fpuregs->fpu_regs));
62700349 353 memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (FPS_REGNUM)],
524d7c18 354 &fpuregs->fpu_fsr, sizeof (FPU_FSR_TYPE));
c5aa993b
JM
355 }
356 else
357 fprintf_unfiltered (gdb_stderr, "Couldn't read float regs from core file\n");
358 }
c906108c
SS
359}
360
361int
fba45db2 362kernel_u_size (void)
c906108c
SS
363{
364 return (sizeof (struct user));
365}
c906108c 366\f
c5aa993b 367
c906108c
SS
368/* Register that we are able to handle sparc core file formats.
369 FIXME: is this really bfd_target_unknown_flavour? */
370
371static struct core_fns sparc_core_fns =
372{
2acceee2
JM
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 */
377 NULL /* next */
c906108c
SS
378};
379
380void
fba45db2 381_initialize_core_sparc (void)
c906108c
SS
382{
383 add_core_fns (&sparc_core_fns);
384}
This page took 0.399128 seconds and 4 git commands to generate.