Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Functions specific to running gdb native on a SPARC running SunOS4. |
02ae7771 AC |
2 | Copyright 1989, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001, |
3 | 2002 | |
b6ba6518 | 4 | 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 | |
7781cd62 DB |
29 | #ifdef HAVE_SYS_PARAM_H |
30 | #include <sys/param.h> | |
31 | #endif | |
c906108c SS |
32 | #include <signal.h> |
33 | #include <sys/ptrace.h> | |
34 | #include <sys/wait.h> | |
35 | #ifdef __linux__ | |
36 | #include <asm/reg.h> | |
37 | #else | |
38 | #include <machine/reg.h> | |
39 | #endif | |
40 | #include <sys/user.h> | |
41 | ||
42 | /* We don't store all registers immediately when requested, since they | |
43 | get sent over in large chunks anyway. Instead, we accumulate most | |
44 | of the changes and send them over once. "deferred_stores" keeps | |
45 | track of which sets of registers we have locally-changed copies of, | |
46 | so we only need send the groups that have changed. */ | |
47 | ||
48 | #define INT_REGS 1 | |
49 | #define STACK_REGS 2 | |
50 | #define FP_REGS 4 | |
51 | ||
c906108c SS |
52 | /* Fetch one or more registers from the inferior. REGNO == -1 to get |
53 | them all. We actually fetch more than requested, when convenient, | |
54 | marking them as valid so we won't fetch them again. */ | |
55 | ||
56 | void | |
fba45db2 | 57 | fetch_inferior_registers (int regno) |
c906108c SS |
58 | { |
59 | struct regs inferior_registers; | |
60 | struct fp_status inferior_fp_registers; | |
61 | int i; | |
02ae7771 AC |
62 | int fetch_pid; |
63 | ||
64 | /* NOTE: cagney/2002-12-03: This code assumes that the currently | |
65 | selected light weight processes' registers can be written | |
66 | directly into the selected thread's register cache. This works | |
67 | fine when given an 1:1 LWP:thread model (such as found on | |
68 | GNU/Linux) but will, likely, have problems when used on an N:1 | |
69 | (userland threads) or N:M (userland multiple LWP) model. In the | |
70 | case of the latter two, the LWP's registers do not necessarily | |
71 | belong to the selected thread (the LWP could be in the middle of | |
72 | executing the thread switch code). | |
73 | ||
74 | These functions should instead be paramaterized with an explicit | |
75 | object (struct regcache, struct thread_info?) into which the LWPs | |
76 | registers can be written. */ | |
77 | ||
78 | fetch_pid = TIDGET (inferior_ptid); | |
79 | if (fetch_pid == 0) | |
80 | fetch_pid = PIDGET (inferior_ptid); | |
c906108c SS |
81 | |
82 | /* We should never be called with deferred stores, because a prerequisite | |
83 | for writing regs is to have fetched them all (PREPARE_TO_STORE), sigh. */ | |
c5aa993b | 84 | if (deferred_stores) |
e1e9e218 | 85 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c SS |
86 | |
87 | DO_DEFERRED_STORES; | |
88 | ||
89 | /* Global and Out regs are fetched directly, as well as the control | |
90 | registers. If we're getting one of the in or local regs, | |
91 | and the stack pointer has not yet been fetched, | |
92 | we have to do that first, since they're found in memory relative | |
93 | to the stack pointer. */ | |
c5aa993b | 94 | if (regno < O7_REGNUM /* including -1 */ |
c906108c | 95 | || regno >= Y_REGNUM |
8262ee23 | 96 | || (!deprecated_register_valid[SP_REGNUM] && regno < I7_REGNUM)) |
c906108c | 97 | { |
02ae7771 | 98 | if (0 != ptrace (PTRACE_GETREGS, fetch_pid, |
c5aa993b JM |
99 | (PTRACE_ARG3_TYPE) & inferior_registers, 0)) |
100 | perror ("ptrace_getregs"); | |
101 | ||
524d7c18 AC |
102 | deprecated_registers[REGISTER_BYTE (0)] = 0; |
103 | memcpy (&deprecated_registers[REGISTER_BYTE (1)], | |
104 | &inferior_registers.r_g1, 15 * REGISTER_RAW_SIZE (G0_REGNUM)); | |
105 | *(int *) &deprecated_registers[REGISTER_BYTE (PS_REGNUM)] | |
106 | = inferior_registers.r_ps; | |
107 | *(int *) &deprecated_registers[REGISTER_BYTE (PC_REGNUM)] | |
108 | = inferior_registers.r_pc; | |
109 | *(int *) &deprecated_registers[REGISTER_BYTE (NPC_REGNUM)] | |
110 | = inferior_registers.r_npc; | |
c5aa993b | 111 | *(int *) ®isters[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y; |
c906108c SS |
112 | |
113 | for (i = G0_REGNUM; i <= O7_REGNUM; i++) | |
8262ee23 AC |
114 | deprecated_register_valid[i] = 1; |
115 | deprecated_register_valid[Y_REGNUM] = 1; | |
116 | deprecated_register_valid[PS_REGNUM] = 1; | |
117 | deprecated_register_valid[PC_REGNUM] = 1; | |
118 | deprecated_register_valid[NPC_REGNUM] = 1; | |
c906108c | 119 | /* If we don't set these valid, read_register_bytes() rereads |
c5aa993b | 120 | all the regs every time it is called! FIXME. */ |
8262ee23 AC |
121 | deprecated_register_valid[WIM_REGNUM] = 1; /* Not true yet, FIXME */ |
122 | deprecated_register_valid[TBR_REGNUM] = 1; /* Not true yet, FIXME */ | |
123 | deprecated_register_valid[CPS_REGNUM] = 1; /* Not true yet, FIXME */ | |
c906108c SS |
124 | } |
125 | ||
126 | /* Floating point registers */ | |
127 | if (regno == -1 || | |
128 | regno == FPS_REGNUM || | |
129 | (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31)) | |
130 | { | |
02ae7771 | 131 | if (0 != ptrace (PTRACE_GETFPREGS, fetch_pid, |
c5aa993b | 132 | (PTRACE_ARG3_TYPE) & inferior_fp_registers, |
c906108c | 133 | 0)) |
c5aa993b | 134 | perror ("ptrace_getfpregs"); |
524d7c18 AC |
135 | memcpy (&deprecated_registers[REGISTER_BYTE (FP0_REGNUM)], |
136 | &inferior_fp_registers, sizeof inferior_fp_registers.fpu_fr); | |
137 | memcpy (&deprecated_registers[REGISTER_BYTE (FPS_REGNUM)], | |
138 | &inferior_fp_registers.Fpu_fsr, sizeof (FPU_FSR_TYPE)); | |
c5aa993b | 139 | for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++) |
8262ee23 AC |
140 | deprecated_register_valid[i] = 1; |
141 | deprecated_register_valid[FPS_REGNUM] = 1; | |
c906108c SS |
142 | } |
143 | ||
144 | /* These regs are saved on the stack by the kernel. Only read them | |
145 | all (16 ptrace calls!) if we really need them. */ | |
146 | if (regno == -1) | |
147 | { | |
524d7c18 AC |
148 | CORE_ADDR sp = *(unsigned int *) & deprecated_registers[REGISTER_BYTE (SP_REGNUM)]; |
149 | target_read_memory (sp, &deprecated_registers[REGISTER_BYTE (L0_REGNUM)], | |
c5aa993b | 150 | 16 * REGISTER_RAW_SIZE (L0_REGNUM)); |
c906108c | 151 | for (i = L0_REGNUM; i <= I7_REGNUM; i++) |
8262ee23 | 152 | deprecated_register_valid[i] = 1; |
c906108c SS |
153 | } |
154 | else if (regno >= L0_REGNUM && regno <= I7_REGNUM) | |
155 | { | |
524d7c18 | 156 | CORE_ADDR sp = *(unsigned int *) & deprecated_registers[REGISTER_BYTE (SP_REGNUM)]; |
c906108c | 157 | i = REGISTER_BYTE (regno); |
8262ee23 | 158 | if (deprecated_register_valid[regno]) |
c5aa993b | 159 | printf_unfiltered ("register %d valid and read\n", regno); |
c906108c | 160 | target_read_memory (sp + i - REGISTER_BYTE (L0_REGNUM), |
524d7c18 | 161 | &deprecated_registers[i], REGISTER_RAW_SIZE (regno)); |
8262ee23 | 162 | deprecated_register_valid[regno] = 1; |
c906108c SS |
163 | } |
164 | } | |
165 | ||
166 | /* Store our register values back into the inferior. | |
167 | If REGNO is -1, do this for all registers. | |
168 | Otherwise, REGNO specifies which register (so we can save time). */ | |
169 | ||
170 | void | |
fba45db2 | 171 | store_inferior_registers (int regno) |
c906108c SS |
172 | { |
173 | struct regs inferior_registers; | |
174 | struct fp_status inferior_fp_registers; | |
175 | int wanna_store = INT_REGS + STACK_REGS + FP_REGS; | |
02ae7771 AC |
176 | int store_pid; |
177 | ||
178 | /* NOTE: cagney/2002-12-02: See comment in fetch_inferior_registers | |
179 | about threaded assumptions. */ | |
180 | store_pid = TIDGET (inferior_ptid); | |
181 | if (store_pid == 0) | |
182 | store_pid = PIDGET (inferior_ptid); | |
c906108c SS |
183 | |
184 | /* First decide which pieces of machine-state we need to modify. | |
185 | Default for regno == -1 case is all pieces. */ | |
186 | if (regno >= 0) | |
e1925118 DM |
187 | { |
188 | if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32) | |
189 | { | |
c906108c | 190 | wanna_store = FP_REGS; |
e1925118 DM |
191 | } |
192 | else | |
193 | { | |
194 | if (regno == SP_REGNUM) | |
195 | wanna_store = INT_REGS + STACK_REGS; | |
196 | else if (regno < L0_REGNUM || regno > I7_REGNUM) | |
197 | wanna_store = INT_REGS; | |
198 | else if (regno == FPS_REGNUM) | |
199 | wanna_store = FP_REGS; | |
200 | else | |
201 | wanna_store = STACK_REGS; | |
202 | } | |
203 | } | |
c906108c SS |
204 | |
205 | /* See if we're forcing the stores to happen now, or deferring. */ | |
206 | if (regno == -2) | |
207 | { | |
208 | wanna_store = deferred_stores; | |
209 | deferred_stores = 0; | |
210 | } | |
211 | else | |
212 | { | |
213 | if (wanna_store == STACK_REGS) | |
214 | { | |
215 | /* Fall through and just store one stack reg. If we deferred | |
216 | it, we'd have to store them all, or remember more info. */ | |
217 | } | |
218 | else | |
219 | { | |
220 | deferred_stores |= wanna_store; | |
221 | return; | |
222 | } | |
223 | } | |
224 | ||
225 | if (wanna_store & STACK_REGS) | |
226 | { | |
524d7c18 | 227 | CORE_ADDR sp = *(unsigned int *) & deprecated_registers[REGISTER_BYTE (SP_REGNUM)]; |
c906108c SS |
228 | |
229 | if (regno < 0 || regno == SP_REGNUM) | |
230 | { | |
8262ee23 | 231 | if (!deprecated_register_valid[L0_REGNUM + 5]) |
e1e9e218 | 232 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c5aa993b | 233 | target_write_memory (sp, |
524d7c18 | 234 | &deprecated_registers[REGISTER_BYTE (L0_REGNUM)], |
c5aa993b | 235 | 16 * REGISTER_RAW_SIZE (L0_REGNUM)); |
c906108c SS |
236 | } |
237 | else | |
238 | { | |
8262ee23 | 239 | if (!deprecated_register_valid[regno]) |
e1e9e218 | 240 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c | 241 | target_write_memory (sp + REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM), |
524d7c18 | 242 | &deprecated_registers[REGISTER_BYTE (regno)], |
c906108c SS |
243 | REGISTER_RAW_SIZE (regno)); |
244 | } | |
c5aa993b | 245 | |
c906108c SS |
246 | } |
247 | ||
248 | if (wanna_store & INT_REGS) | |
249 | { | |
8262ee23 | 250 | if (!deprecated_register_valid[G1_REGNUM]) |
e1e9e218 | 251 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c | 252 | |
524d7c18 AC |
253 | memcpy (&inferior_registers.r_g1, |
254 | &deprecated_registers[REGISTER_BYTE (G1_REGNUM)], | |
c906108c SS |
255 | 15 * REGISTER_RAW_SIZE (G1_REGNUM)); |
256 | ||
257 | inferior_registers.r_ps = | |
524d7c18 | 258 | *(int *) &deprecated_registers[REGISTER_BYTE (PS_REGNUM)]; |
c906108c | 259 | inferior_registers.r_pc = |
524d7c18 | 260 | *(int *) &deprecated_registers[REGISTER_BYTE (PC_REGNUM)]; |
c906108c | 261 | inferior_registers.r_npc = |
524d7c18 | 262 | *(int *) &deprecated_registers[REGISTER_BYTE (NPC_REGNUM)]; |
c906108c | 263 | inferior_registers.r_y = |
524d7c18 | 264 | *(int *) &deprecated_registers[REGISTER_BYTE (Y_REGNUM)]; |
c906108c | 265 | |
02ae7771 | 266 | if (0 != ptrace (PTRACE_SETREGS, store_pid, |
c5aa993b JM |
267 | (PTRACE_ARG3_TYPE) & inferior_registers, 0)) |
268 | perror ("ptrace_setregs"); | |
c906108c SS |
269 | } |
270 | ||
271 | if (wanna_store & FP_REGS) | |
272 | { | |
8262ee23 | 273 | if (!deprecated_register_valid[FP0_REGNUM + 9]) |
e1e9e218 | 274 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
524d7c18 AC |
275 | memcpy (&inferior_fp_registers, |
276 | &deprecated_registers[REGISTER_BYTE (FP0_REGNUM)], | |
c906108c | 277 | sizeof inferior_fp_registers.fpu_fr); |
c5aa993b | 278 | memcpy (&inferior_fp_registers.Fpu_fsr, |
524d7c18 AC |
279 | &deprecated_registers[REGISTER_BYTE (FPS_REGNUM)], |
280 | sizeof (FPU_FSR_TYPE)); | |
c906108c | 281 | if (0 != |
02ae7771 | 282 | ptrace (PTRACE_SETFPREGS, store_pid, |
c5aa993b JM |
283 | (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0)) |
284 | perror ("ptrace_setfpregs"); | |
c906108c SS |
285 | } |
286 | } | |
287 | ||
c67b4c45 KB |
288 | /* Provide registers to GDB from a core file. |
289 | ||
290 | CORE_REG_SECT points to an array of bytes, which are the contents | |
291 | of a `note' from a core file which BFD thinks might contain | |
292 | register contents. CORE_REG_SIZE is its size. | |
293 | ||
294 | WHICH says which register set corelow suspects this is: | |
295 | 0 --- the general-purpose register set | |
296 | 2 --- the floating-point register set | |
297 | ||
298 | IGNORE is unused. */ | |
c906108c SS |
299 | |
300 | static void | |
c67b4c45 KB |
301 | fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, |
302 | int which, CORE_ADDR ignore) | |
c906108c SS |
303 | { |
304 | ||
c5aa993b JM |
305 | if (which == 0) |
306 | { | |
c906108c | 307 | |
c5aa993b | 308 | /* Integer registers */ |
c906108c SS |
309 | |
310 | #define gregs ((struct regs *)core_reg_sect) | |
c5aa993b | 311 | /* G0 *always* holds 0. */ |
524d7c18 | 312 | *(int *) &deprecated_registers[REGISTER_BYTE (0)] = 0; |
c906108c | 313 | |
c5aa993b | 314 | /* The globals and output registers. */ |
524d7c18 | 315 | memcpy (&deprecated_registers[REGISTER_BYTE (G1_REGNUM)], &gregs->r_g1, |
c5aa993b | 316 | 15 * REGISTER_RAW_SIZE (G1_REGNUM)); |
524d7c18 AC |
317 | *(int *) &deprecated_registers[REGISTER_BYTE (PS_REGNUM)] = gregs->r_ps; |
318 | *(int *) &deprecated_registers[REGISTER_BYTE (PC_REGNUM)] = gregs->r_pc; | |
319 | *(int *) &deprecated_registers[REGISTER_BYTE (NPC_REGNUM)] = gregs->r_npc; | |
320 | *(int *) &deprecated_registers[REGISTER_BYTE (Y_REGNUM)] = gregs->r_y; | |
c5aa993b JM |
321 | |
322 | /* My best guess at where to get the locals and input | |
323 | registers is exactly where they usually are, right above | |
324 | the stack pointer. If the core dump was caused by a bus error | |
325 | from blowing away the stack pointer (as is possible) then this | |
326 | won't work, but it's worth the try. */ | |
327 | { | |
328 | int sp; | |
329 | ||
524d7c18 AC |
330 | sp = *(int *) &deprecated_registers[REGISTER_BYTE (SP_REGNUM)]; |
331 | if (0 != target_read_memory (sp, | |
332 | &deprecated_registers[REGISTER_BYTE (L0_REGNUM)], | |
c5aa993b JM |
333 | 16 * REGISTER_RAW_SIZE (L0_REGNUM))) |
334 | { | |
335 | /* fprintf_unfiltered so user can still use gdb */ | |
336 | fprintf_unfiltered (gdb_stderr, | |
337 | "Couldn't read input and local registers from core file\n"); | |
338 | } | |
339 | } | |
c906108c | 340 | } |
c5aa993b JM |
341 | else if (which == 2) |
342 | { | |
c906108c | 343 | |
c5aa993b | 344 | /* Floating point registers */ |
c906108c SS |
345 | |
346 | #define fpuregs ((struct fpu *) core_reg_sect) | |
c5aa993b JM |
347 | if (core_reg_size >= sizeof (struct fpu)) |
348 | { | |
524d7c18 AC |
349 | memcpy (&deprecated_registers[REGISTER_BYTE (FP0_REGNUM)], |
350 | fpuregs->fpu_regs, sizeof (fpuregs->fpu_regs)); | |
351 | memcpy (&deprecated_registers[REGISTER_BYTE (FPS_REGNUM)], | |
352 | &fpuregs->fpu_fsr, sizeof (FPU_FSR_TYPE)); | |
c5aa993b JM |
353 | } |
354 | else | |
355 | fprintf_unfiltered (gdb_stderr, "Couldn't read float regs from core file\n"); | |
356 | } | |
c906108c SS |
357 | } |
358 | ||
359 | int | |
fba45db2 | 360 | kernel_u_size (void) |
c906108c SS |
361 | { |
362 | return (sizeof (struct user)); | |
363 | } | |
c906108c | 364 | \f |
c5aa993b | 365 | |
c906108c SS |
366 | /* Register that we are able to handle sparc core file formats. |
367 | FIXME: is this really bfd_target_unknown_flavour? */ | |
368 | ||
369 | static struct core_fns sparc_core_fns = | |
370 | { | |
2acceee2 JM |
371 | bfd_target_unknown_flavour, /* core_flavour */ |
372 | default_check_format, /* check_format */ | |
373 | default_core_sniffer, /* core_sniffer */ | |
374 | fetch_core_registers, /* core_read_registers */ | |
375 | NULL /* next */ | |
c906108c SS |
376 | }; |
377 | ||
378 | void | |
fba45db2 | 379 | _initialize_core_sparc (void) |
c906108c SS |
380 | { |
381 | add_core_fns (&sparc_core_fns); | |
382 | } |