* source.c (print_source_lines): for DOS interoperability; don't
[binutils-gdb.git] / gdb / sparc-xdep.c
1 /* Host-dependent code for SPARC host systems, for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* This code only compiles when we have the definitions in tm-sparc.h. */
21
22 #define TM_FILE_OVERRIDE
23 #include "defs.h"
24 #include "tm-sparc.h"
25
26 #include "inferior.h"
27 #include "target.h"
28
29 #include <sys/param.h>
30 #include <sys/ptrace.h>
31 #include <machine/reg.h>
32
33 #include "gdbcore.h"
34 #include <sys/core.h>
35
36 /* We don't store all registers immediately when requested, since they
37 get sent over in large chunks anyway. Instead, we accumulate most
38 of the changes and send them over once. "deferred_stores" keeps
39 track of which sets of registers we have locally-changed copies of,
40 so we only need send the groups that have changed. */
41
42 #define INT_REGS 1
43 #define STACK_REGS 2
44 #define FP_REGS 4
45
46 int deferred_stores = 0; /* Cumulates stores we want to do eventually. */
47
48 /* Fetch one or more registers from the inferior. REGNO == -1 to get
49 them all. We actually fetch more than requested, when convenient,
50 marking them as valid so we won't fetch them again. */
51 void
52 fetch_inferior_registers (regno)
53 int regno;
54 {
55 struct regs inferior_registers;
56 struct fp_status inferior_fp_registers;
57 int i;
58
59 /* We should never be called with deferred stores, because a prerequisite
60 for writing regs is to have fetched them all (PREPARE_TO_STORE), sigh. */
61 if (deferred_stores) abort();
62
63 DO_DEFERRED_STORES;
64
65 /* Global and Out regs are fetched directly, as well as the control
66 registers. If we're getting one of the in or local regs,
67 and the stack pointer has not yet been fetched,
68 we have to do that first, since they're found in memory relative
69 to the stack pointer. */
70 if (regno < O7_REGNUM /* including -1 */
71 || regno >= Y_REGNUM
72 || (!register_valid[SP_REGNUM] && regno < I7_REGNUM))
73 {
74 if (0 != ptrace (PTRACE_GETREGS, inferior_pid,
75 (PTRACE_ARG3_TYPE) &inferior_registers, 0))
76 perror("ptrace_getregs");
77
78 registers[REGISTER_BYTE (0)] = 0;
79 bcopy (&inferior_registers.r_g1, &registers[REGISTER_BYTE (1)], 15 * REGISTER_RAW_SIZE (G0_REGNUM));
80 *(int *)&registers[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
81 *(int *)&registers[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
82 *(int *)&registers[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc;
83 *(int *)&registers[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
84
85 for (i = G0_REGNUM; i <= O7_REGNUM; i++)
86 register_valid[i] = 1;
87 register_valid[Y_REGNUM] = 1;
88 register_valid[PS_REGNUM] = 1;
89 register_valid[PC_REGNUM] = 1;
90 register_valid[NPC_REGNUM] = 1;
91 /* If we don't set these valid, read_register_bytes() rereads
92 all the regs every time it is called! FIXME. */
93 register_valid[WIM_REGNUM] = 1; /* Not true yet, FIXME */
94 register_valid[TBR_REGNUM] = 1; /* Not true yet, FIXME */
95 register_valid[FPS_REGNUM] = 1; /* Not true yet, FIXME */
96 register_valid[CPS_REGNUM] = 1; /* Not true yet, FIXME */
97 }
98
99 /* Floating point registers */
100 if (regno == -1 || (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31))
101 {
102 if (0 != ptrace (PTRACE_GETFPREGS, inferior_pid,
103 (PTRACE_ARG3_TYPE) &inferior_fp_registers,
104 0))
105 perror("ptrace_getfpregs");
106 bcopy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
107 sizeof inferior_fp_registers.fpu_fr);
108 /* bcopy (&inferior_fp_registers.Fpu_fsr,
109 &registers[REGISTER_BYTE (FPS_REGNUM)],
110 sizeof (FPU_FSR_TYPE)); FIXME??? -- gnu@cyg */
111 for (i = FP0_REGNUM; i <= FP0_REGNUM+31; i++)
112 register_valid[i] = 1;
113 register_valid[FPS_REGNUM] = 1;
114 }
115
116 /* These regs are saved on the stack by the kernel. Only read them
117 all (16 ptrace calls!) if we really need them. */
118 if (regno == -1)
119 {
120 target_xfer_memory (*(CORE_ADDR*)&registers[REGISTER_BYTE (SP_REGNUM)],
121 &registers[REGISTER_BYTE (L0_REGNUM)],
122 16*REGISTER_RAW_SIZE (L0_REGNUM), 0);
123 for (i = L0_REGNUM; i <= I7_REGNUM; i++)
124 register_valid[i] = 1;
125 }
126 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
127 {
128 CORE_ADDR sp = *(CORE_ADDR*)&registers[REGISTER_BYTE (SP_REGNUM)];
129 i = REGISTER_BYTE (regno);
130 if (register_valid[regno])
131 printf("register %d valid and read\n", regno);
132 target_xfer_memory (sp + i - REGISTER_BYTE (L0_REGNUM),
133 &registers[i], REGISTER_RAW_SIZE (regno), 0);
134 register_valid[regno] = 1;
135 }
136 }
137
138 /* Store our register values back into the inferior.
139 If REGNO is -1, do this for all registers.
140 Otherwise, REGNO specifies which register (so we can save time). */
141
142 void
143 store_inferior_registers (regno)
144 int regno;
145 {
146 struct regs inferior_registers;
147 struct fp_status inferior_fp_registers;
148 int wanna_store = INT_REGS + STACK_REGS + FP_REGS;
149
150 /* First decide which pieces of machine-state we need to modify.
151 Default for regno == -1 case is all pieces. */
152 if (regno >= 0)
153 if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32)
154 {
155 wanna_store = FP_REGS;
156 }
157 else
158 {
159 if (regno == SP_REGNUM)
160 wanna_store = INT_REGS + STACK_REGS;
161 else if (regno < L0_REGNUM || regno > I7_REGNUM)
162 wanna_store = INT_REGS;
163 else
164 wanna_store = STACK_REGS;
165 }
166
167 /* See if we're forcing the stores to happen now, or deferring. */
168 if (regno == -2)
169 {
170 wanna_store = deferred_stores;
171 deferred_stores = 0;
172 }
173 else
174 {
175 if (wanna_store == STACK_REGS)
176 {
177 /* Fall through and just store one stack reg. If we deferred
178 it, we'd have to store them all, or remember more info. */
179 }
180 else
181 {
182 deferred_stores |= wanna_store;
183 return;
184 }
185 }
186
187 if (wanna_store & STACK_REGS)
188 {
189 CORE_ADDR sp = *(CORE_ADDR *)&registers[REGISTER_BYTE (SP_REGNUM)];
190
191 if (regno < 0 || regno == SP_REGNUM)
192 {
193 if (!register_valid[L0_REGNUM+5]) abort();
194 target_xfer_memory (sp,
195 &registers[REGISTER_BYTE (L0_REGNUM)],
196 16*REGISTER_RAW_SIZE (L0_REGNUM), 1);
197 }
198 else
199 {
200 if (!register_valid[regno]) abort();
201 target_xfer_memory (sp + REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM),
202 &registers[REGISTER_BYTE (regno)],
203 REGISTER_RAW_SIZE (regno), 1);
204 }
205
206 }
207
208 if (wanna_store & INT_REGS)
209 {
210 if (!register_valid[G1_REGNUM]) abort();
211
212 bcopy (&registers[REGISTER_BYTE (G1_REGNUM)],
213 &inferior_registers.r_g1, 15 * REGISTER_RAW_SIZE (G1_REGNUM));
214
215 inferior_registers.r_ps =
216 *(int *)&registers[REGISTER_BYTE (PS_REGNUM)];
217 inferior_registers.r_pc =
218 *(int *)&registers[REGISTER_BYTE (PC_REGNUM)];
219 inferior_registers.r_npc =
220 *(int *)&registers[REGISTER_BYTE (NPC_REGNUM)];
221 inferior_registers.r_y =
222 *(int *)&registers[REGISTER_BYTE (Y_REGNUM)];
223
224 if (0 != ptrace (PTRACE_SETREGS, inferior_pid,
225 (PTRACE_ARG3_TYPE) &inferior_registers, 0))
226 perror("ptrace_setregs");
227 }
228
229 if (wanna_store & FP_REGS)
230 {
231 if (!register_valid[FP0_REGNUM+9]) abort();
232 bcopy (&registers[REGISTER_BYTE (FP0_REGNUM)],
233 &inferior_fp_registers,
234 sizeof inferior_fp_registers.fpu_fr);
235
236 /* bcopy (&registers[REGISTER_BYTE (FPS_REGNUM)],
237 &inferior_fp_registers.Fpu_fsr,
238 sizeof (FPU_FSR_TYPE));
239 ****/
240 if (0 !=
241 ptrace (PTRACE_SETFPREGS, inferior_pid,
242 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0))
243 perror("ptrace_setfpregs");
244 }
245 }
246 \f
247 void
248 fetch_core_registers (core_reg_sect, core_reg_size, which, ignore)
249 char *core_reg_sect;
250 unsigned core_reg_size;
251 int which;
252 unsigned int ignore; /* reg addr, unused in this version */
253 {
254
255 if (which == 0) {
256
257 /* Integer registers */
258
259 #define gregs ((struct regs *)core_reg_sect)
260 /* G0 *always* holds 0. */
261 *(int *)&registers[REGISTER_BYTE (0)] = 0;
262
263 /* The globals and output registers. */
264 bcopy (&gregs->r_g1,
265 &registers[REGISTER_BYTE (G1_REGNUM)],
266 15 * REGISTER_RAW_SIZE (G1_REGNUM));
267 *(int *)&registers[REGISTER_BYTE (PS_REGNUM)] = gregs->r_ps;
268 *(int *)&registers[REGISTER_BYTE (PC_REGNUM)] = gregs->r_pc;
269 *(int *)&registers[REGISTER_BYTE (NPC_REGNUM)] = gregs->r_npc;
270 *(int *)&registers[REGISTER_BYTE (Y_REGNUM)] = gregs->r_y;
271
272 /* My best guess at where to get the locals and input
273 registers is exactly where they usually are, right above
274 the stack pointer. If the core dump was caused by a bus error
275 from blowing away the stack pointer (as is possible) then this
276 won't work, but it's worth the try. */
277 {
278 int sp;
279
280 sp = *(int *)&registers[REGISTER_BYTE (SP_REGNUM)];
281 if (0 != target_read_memory (sp, &registers[REGISTER_BYTE (L0_REGNUM)],
282 16 * REGISTER_RAW_SIZE (L0_REGNUM)))
283 {
284 /* fprintf so user can still use gdb */
285 fprintf (stderr,
286 "Couldn't read input and local registers from core file\n");
287 }
288 }
289 } else if (which == 2) {
290
291 /* Floating point registers */
292
293 #define fpuregs ((struct fpu *) core_reg_sect)
294 if (core_reg_size >= sizeof (struct fpu))
295 {
296 bcopy (fpuregs->fpu_regs,
297 &registers[REGISTER_BYTE (FP0_REGNUM)],
298 sizeof (fpuregs->fpu_regs));
299 bcopy (&fpuregs->fpu_fsr,
300 &registers[REGISTER_BYTE (FPS_REGNUM)],
301 sizeof (FPU_FSR_TYPE));
302 }
303 else
304 fprintf (stderr, "Couldn't read float regs from core file\n");
305 }
306 }