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