2003-09-17 Andrew Cagney <cagney@redhat.com>
[external/binutils.git] / gdb / sparc-nat.c
1 /* Functions specific to running gdb native on a SPARC running SunOS4.
2
3    Copyright 1989, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4    2001, 2002, 2003 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
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.
12
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.
17
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.  */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "target.h"
26 #include "gdbcore.h"
27 #include "regcache.h"
28
29 #include "sparc-tdep.h"
30
31 #ifdef HAVE_SYS_PARAM_H
32 #include <sys/param.h>
33 #endif
34 #include <signal.h>
35 #include <sys/ptrace.h>
36 #include "gdb_wait.h"
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
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
58 void
59 fetch_inferior_registers (int regno)
60 {
61   struct regs inferior_registers;
62   struct fp_status inferior_fp_registers;
63   int i;
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);
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.  */
86   if (deferred_stores)
87     internal_error (__FILE__, __LINE__, "failed internal consistency check");
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.  */
96   if (regno < O7_REGNUM         /* including -1 */
97       || regno >= Y_REGNUM
98       || (!deprecated_register_valid[SP_REGNUM] && regno < I7_REGNUM))
99     {
100       if (0 != ptrace (PTRACE_GETREGS, fetch_pid,
101                        (PTRACE_ARG3_TYPE) & inferior_registers, 0))
102         perror ("ptrace_getregs");
103
104       deprecated_registers[DEPRECATED_REGISTER_BYTE (0)] = 0;
105       memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (1)],
106               &inferior_registers.r_g1, 15 * REGISTER_RAW_SIZE (G0_REGNUM));
107       *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PS_REGNUM)]
108         = inferior_registers.r_ps;
109       *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PC_REGNUM)]
110         = inferior_registers.r_pc;
111       *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (NPC_REGNUM)]
112         = inferior_registers.r_npc;
113       *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
114
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 */
126     }
127
128   /* Floating point registers */
129   if (regno == -1 ||
130       regno == FPS_REGNUM ||
131       (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31))
132     {
133       if (0 != ptrace (PTRACE_GETFPREGS, fetch_pid,
134                        (PTRACE_ARG3_TYPE) & inferior_fp_registers,
135                        0))
136         perror ("ptrace_getfpregs");
137       memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (FP0_REGNUM)],
138               &inferior_fp_registers, sizeof inferior_fp_registers.fpu_fr);
139       memcpy (&deprecated_registers[DEPRECATED_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;
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     {
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)],
152                           16 * REGISTER_RAW_SIZE (L0_REGNUM));
153       for (i = L0_REGNUM; i <= I7_REGNUM; i++)
154         deprecated_register_valid[i] = 1;
155     }
156   else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
157     {
158       CORE_ADDR sp = *(unsigned int *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (SP_REGNUM)];
159       i = DEPRECATED_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 - DEPRECATED_REGISTER_BYTE (L0_REGNUM),
163                           &deprecated_registers[i], REGISTER_RAW_SIZE (regno));
164       deprecated_register_valid[regno] = 1;
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
172 void
173 store_inferior_registers (int regno)
174 {
175   struct regs inferior_registers;
176   struct fp_status inferior_fp_registers;
177   int wanna_store = INT_REGS + STACK_REGS + FP_REGS;
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);
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)
189     {
190       if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32)
191         {
192           wanna_store = FP_REGS;
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     }
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     {
229       CORE_ADDR sp = *(unsigned int *) & deprecated_registers[DEPRECATED_REGISTER_BYTE (SP_REGNUM)];
230
231       if (regno < 0 || regno == SP_REGNUM)
232         {
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[DEPRECATED_REGISTER_BYTE (L0_REGNUM)],
237                                16 * REGISTER_RAW_SIZE (L0_REGNUM));
238         }
239       else
240         {
241           if (!deprecated_register_valid[regno])
242             internal_error (__FILE__, __LINE__, "failed internal consistency check");
243           target_write_memory (sp + DEPRECATED_REGISTER_BYTE (regno) - DEPRECATED_REGISTER_BYTE (L0_REGNUM),
244                                &deprecated_registers[DEPRECATED_REGISTER_BYTE (regno)],
245                                REGISTER_RAW_SIZE (regno));
246         }
247
248     }
249
250   if (wanna_store & INT_REGS)
251     {
252       if (!deprecated_register_valid[G1_REGNUM])
253         internal_error (__FILE__, __LINE__, "failed internal consistency check");
254
255       memcpy (&inferior_registers.r_g1,
256               &deprecated_registers[DEPRECATED_REGISTER_BYTE (G1_REGNUM)],
257               15 * REGISTER_RAW_SIZE (G1_REGNUM));
258
259       inferior_registers.r_ps =
260         *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PS_REGNUM)];
261       inferior_registers.r_pc =
262         *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PC_REGNUM)];
263       inferior_registers.r_npc =
264         *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (NPC_REGNUM)];
265       inferior_registers.r_y =
266         *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (Y_REGNUM)];
267
268       if (0 != ptrace (PTRACE_SETREGS, store_pid,
269                        (PTRACE_ARG3_TYPE) & inferior_registers, 0))
270         perror ("ptrace_setregs");
271     }
272
273   if (wanna_store & FP_REGS)
274     {
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[DEPRECATED_REGISTER_BYTE (FP0_REGNUM)],
279               sizeof inferior_fp_registers.fpu_fr);
280       memcpy (&inferior_fp_registers.Fpu_fsr,
281               &deprecated_registers[DEPRECATED_REGISTER_BYTE (FPS_REGNUM)],
282               sizeof (FPU_FSR_TYPE));
283       if (0 !=
284           ptrace (PTRACE_SETFPREGS, store_pid,
285                   (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0))
286         perror ("ptrace_setfpregs");
287     }
288 }
289
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.  */
301
302 static void
303 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
304                       int which, CORE_ADDR ignore)
305 {
306
307   if (which == 0)
308     {
309
310       /* Integer registers */
311
312 #define gregs ((struct regs *)core_reg_sect)
313       /* G0 *always* holds 0.  */
314       *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (0)] = 0;
315
316       /* The globals and output registers.  */
317       memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (G1_REGNUM)], &gregs->r_g1,
318               15 * REGISTER_RAW_SIZE (G1_REGNUM));
319       *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PS_REGNUM)] = gregs->r_ps;
320       *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (PC_REGNUM)] = gregs->r_pc;
321       *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (NPC_REGNUM)] = gregs->r_npc;
322       *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (Y_REGNUM)] = gregs->r_y;
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
332         sp = *(int *) &deprecated_registers[DEPRECATED_REGISTER_BYTE (SP_REGNUM)];
333         if (0 != target_read_memory (sp,
334                                      &deprecated_registers[DEPRECATED_REGISTER_BYTE (L0_REGNUM)],
335                                      16 * REGISTER_RAW_SIZE (L0_REGNUM)))
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       }
342     }
343   else if (which == 2)
344     {
345
346       /* Floating point registers */
347
348 #define fpuregs  ((struct fpu *) core_reg_sect)
349       if (core_reg_size >= sizeof (struct fpu))
350         {
351           memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (FP0_REGNUM)],
352                   fpuregs->fpu_regs, sizeof (fpuregs->fpu_regs));
353           memcpy (&deprecated_registers[DEPRECATED_REGISTER_BYTE (FPS_REGNUM)],
354                   &fpuregs->fpu_fsr, sizeof (FPU_FSR_TYPE));
355         }
356       else
357         fprintf_unfiltered (gdb_stderr, "Couldn't read float regs from core file\n");
358     }
359 }
360
361 int
362 kernel_u_size (void)
363 {
364   return (sizeof (struct user));
365 }
366 \f
367
368 /* Register that we are able to handle sparc core file formats.
369    FIXME: is this really bfd_target_unknown_flavour? */
370
371 static struct core_fns sparc_core_fns =
372 {
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 */
378 };
379
380 void
381 _initialize_core_sparc (void)
382 {
383   add_core_fns (&sparc_core_fns);
384 }