1 /* Functions specific to running gdb native on a SPARC running SunOS4. 2 Copyright 1989, 1992, 1993, 1994, 1996 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 19 20 #include "defs.h" 21 #include "inferior.h" 22 #include "target.h" 23 #include "gdbcore.h" 24 25 #include <signal.h> 26 #include <sys/ptrace.h> 27 #include <sys/wait.h> 28 #include <machine/reg.h> 29 #include <sys/user.h> 30 31 /* We don't store all registers immediately when requested, since they 32 get sent over in large chunks anyway. Instead, we accumulate most 33 of the changes and send them over once. "deferred_stores" keeps 34 track of which sets of registers we have locally-changed copies of, 35 so we only need send the groups that have changed. */ 36 37 #define INT_REGS 1 38 #define STACK_REGS 2 39 #define FP_REGS 4 40 41 static void 42 fetch_core_registers PARAMS ((char *, unsigned int, int, CORE_ADDR)); 43 44 /* Fetch one or more registers from the inferior. REGNO == -1 to get 45 them all. We actually fetch more than requested, when convenient, 46 marking them as valid so we won't fetch them again. */ 47 48 void 49 fetch_inferior_registers (regno) 50 int regno; 51 { 52 struct regs inferior_registers; 53 struct fp_status inferior_fp_registers; 54 int i; 55 56 /* We should never be called with deferred stores, because a prerequisite 57 for writing regs is to have fetched them all (PREPARE_TO_STORE), sigh. */ 58 if (deferred_stores) abort(); 59 60 DO_DEFERRED_STORES; 61 62 /* Global and Out regs are fetched directly, as well as the control 63 registers. If we're getting one of the in or local regs, 64 and the stack pointer has not yet been fetched, 65 we have to do that first, since they're found in memory relative 66 to the stack pointer. */ 67 if (regno < O7_REGNUM /* including -1 */ 68 || regno >= Y_REGNUM 69 || (!register_valid[SP_REGNUM] && regno < I7_REGNUM)) 70 { 71 if (0 != ptrace (PTRACE_GETREGS, inferior_pid, 72 (PTRACE_ARG3_TYPE) &inferior_registers, 0)) 73 perror("ptrace_getregs"); 74 75 registers[REGISTER_BYTE (0)] = 0; 76 memcpy (®isters[REGISTER_BYTE (1)], &inferior_registers.r_g1, 77 15 * REGISTER_RAW_SIZE (G0_REGNUM)); 78 *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps; 79 *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc; 80 *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc; 81 *(int *)®isters[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y; 82 83 for (i = G0_REGNUM; i <= O7_REGNUM; i++) 84 register_valid[i] = 1; 85 register_valid[Y_REGNUM] = 1; 86 register_valid[PS_REGNUM] = 1; 87 register_valid[PC_REGNUM] = 1; 88 register_valid[NPC_REGNUM] = 1; 89 /* If we don't set these valid, read_register_bytes() rereads 90 all the regs every time it is called! FIXME. */ 91 register_valid[WIM_REGNUM] = 1; /* Not true yet, FIXME */ 92 register_valid[TBR_REGNUM] = 1; /* Not true yet, FIXME */ 93 register_valid[CPS_REGNUM] = 1; /* Not true yet, FIXME */ 94 } 95 96 /* Floating point registers */ 97 if (regno == -1 || 98 regno == FPS_REGNUM || 99 (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31)) 100 { 101 if (0 != ptrace (PTRACE_GETFPREGS, inferior_pid, 102 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 103 0)) 104 perror("ptrace_getfpregs"); 105 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers, 106 sizeof inferior_fp_registers.fpu_fr); 107 memcpy (®isters[REGISTER_BYTE (FPS_REGNUM)], 108 &inferior_fp_registers.Fpu_fsr, 109 sizeof (FPU_FSR_TYPE)); 110 for (i = FP0_REGNUM; i <= FP0_REGNUM+31; i++) 111 register_valid[i] = 1; 112 register_valid[FPS_REGNUM] = 1; 113 } 114 115 /* These regs are saved on the stack by the kernel. Only read them 116 all (16 ptrace calls!) if we really need them. */ 117 if (regno == -1) 118 { 119 target_xfer_memory (*(CORE_ADDR*)®isters[REGISTER_BYTE (SP_REGNUM)], 120 ®isters[REGISTER_BYTE (L0_REGNUM)], 121 16*REGISTER_RAW_SIZE (L0_REGNUM), 0); 122 for (i = L0_REGNUM; i <= I7_REGNUM; i++) 123 register_valid[i] = 1; 124 } 125 else if (regno >= L0_REGNUM && regno <= I7_REGNUM) 126 { 127 CORE_ADDR sp = *(CORE_ADDR*)®isters[REGISTER_BYTE (SP_REGNUM)]; 128 i = REGISTER_BYTE (regno); 129 if (register_valid[regno]) 130 printf_unfiltered("register %d valid and read\n", regno); 131 target_xfer_memory (sp + i - REGISTER_BYTE (L0_REGNUM), 132 ®isters[i], REGISTER_RAW_SIZE (regno), 0); 133 register_valid[regno] = 1; 134 } 135 } 136 137 /* Store our register values back into the inferior. 138 If REGNO is -1, do this for all registers. 139 Otherwise, REGNO specifies which register (so we can save time). */ 140 141 void 142 store_inferior_registers (regno) 143 int regno; 144 { 145 struct regs inferior_registers; 146 struct fp_status inferior_fp_registers; 147 int wanna_store = INT_REGS + STACK_REGS + FP_REGS; 148 149 /* First decide which pieces of machine-state we need to modify. 150 Default for regno == -1 case is all pieces. */ 151 if (regno >= 0) 152 if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32) 153 { 154 wanna_store = FP_REGS; 155 } 156 else 157 { 158 if (regno == SP_REGNUM) 159 wanna_store = INT_REGS + STACK_REGS; 160 else if (regno < L0_REGNUM || regno > I7_REGNUM) 161 wanna_store = INT_REGS; 162 else if (regno == FPS_REGNUM) 163 wanna_store = FP_REGS; 164 else 165 wanna_store = STACK_REGS; 166 } 167 168 /* See if we're forcing the stores to happen now, or deferring. */ 169 if (regno == -2) 170 { 171 wanna_store = deferred_stores; 172 deferred_stores = 0; 173 } 174 else 175 { 176 if (wanna_store == STACK_REGS) 177 { 178 /* Fall through and just store one stack reg. If we deferred 179 it, we'd have to store them all, or remember more info. */ 180 } 181 else 182 { 183 deferred_stores |= wanna_store; 184 return; 185 } 186 } 187 188 if (wanna_store & STACK_REGS) 189 { 190 CORE_ADDR sp = *(CORE_ADDR *)®isters[REGISTER_BYTE (SP_REGNUM)]; 191 192 if (regno < 0 || regno == SP_REGNUM) 193 { 194 if (!register_valid[L0_REGNUM+5]) abort(); 195 target_xfer_memory (sp, 196 ®isters[REGISTER_BYTE (L0_REGNUM)], 197 16*REGISTER_RAW_SIZE (L0_REGNUM), 1); 198 } 199 else 200 { 201 if (!register_valid[regno]) abort(); 202 target_xfer_memory (sp + REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM), 203 ®isters[REGISTER_BYTE (regno)], 204 REGISTER_RAW_SIZE (regno), 1); 205 } 206 207 } 208 209 if (wanna_store & INT_REGS) 210 { 211 if (!register_valid[G1_REGNUM]) abort(); 212 213 memcpy (&inferior_registers.r_g1, ®isters[REGISTER_BYTE (G1_REGNUM)], 214 15 * REGISTER_RAW_SIZE (G1_REGNUM)); 215 216 inferior_registers.r_ps = 217 *(int *)®isters[REGISTER_BYTE (PS_REGNUM)]; 218 inferior_registers.r_pc = 219 *(int *)®isters[REGISTER_BYTE (PC_REGNUM)]; 220 inferior_registers.r_npc = 221 *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)]; 222 inferior_registers.r_y = 223 *(int *)®isters[REGISTER_BYTE (Y_REGNUM)]; 224 225 if (0 != ptrace (PTRACE_SETREGS, inferior_pid, 226 (PTRACE_ARG3_TYPE) &inferior_registers, 0)) 227 perror("ptrace_setregs"); 228 } 229 230 if (wanna_store & FP_REGS) 231 { 232 if (!register_valid[FP0_REGNUM+9]) abort(); 233 memcpy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)], 234 sizeof inferior_fp_registers.fpu_fr); 235 memcpy (&inferior_fp_registers.Fpu_fsr, 236 ®isters[REGISTER_BYTE (FPS_REGNUM)], sizeof (FPU_FSR_TYPE)); 237 if (0 != 238 ptrace (PTRACE_SETFPREGS, inferior_pid, 239 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0)) 240 perror("ptrace_setfpregs"); 241 } 242 } 243 244 245 static void 246 fetch_core_registers (core_reg_sect, core_reg_size, which, ignore) 247 char *core_reg_sect; 248 unsigned core_reg_size; 249 int which; 250 CORE_ADDR ignore; /* reg addr, unused in this version */ 251 { 252 253 if (which == 0) { 254 255 /* Integer registers */ 256 257 #define gregs ((struct regs *)core_reg_sect) 258 /* G0 *always* holds 0. */ 259 *(int *)®isters[REGISTER_BYTE (0)] = 0; 260 261 /* The globals and output registers. */ 262 memcpy (®isters[REGISTER_BYTE (G1_REGNUM)], &gregs->r_g1, 263 15 * REGISTER_RAW_SIZE (G1_REGNUM)); 264 *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = gregs->r_ps; 265 *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = gregs->r_pc; 266 *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)] = gregs->r_npc; 267 *(int *)®isters[REGISTER_BYTE (Y_REGNUM)] = gregs->r_y; 268 269 /* My best guess at where to get the locals and input 270 registers is exactly where they usually are, right above 271 the stack pointer. If the core dump was caused by a bus error 272 from blowing away the stack pointer (as is possible) then this 273 won't work, but it's worth the try. */ 274 { 275 int sp; 276 277 sp = *(int *)®isters[REGISTER_BYTE (SP_REGNUM)]; 278 if (0 != target_read_memory (sp, ®isters[REGISTER_BYTE (L0_REGNUM)], 279 16 * REGISTER_RAW_SIZE (L0_REGNUM))) 280 { 281 /* fprintf_unfiltered so user can still use gdb */ 282 fprintf_unfiltered (gdb_stderr, 283 "Couldn't read input and local registers from core file\n"); 284 } 285 } 286 } else if (which == 2) { 287 288 /* Floating point registers */ 289 290 #define fpuregs ((struct fpu *) core_reg_sect) 291 if (core_reg_size >= sizeof (struct fpu)) 292 { 293 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], fpuregs->fpu_regs, 294 sizeof (fpuregs->fpu_regs)); 295 memcpy (®isters[REGISTER_BYTE (FPS_REGNUM)], &fpuregs->fpu_fsr, 296 sizeof (FPU_FSR_TYPE)); 297 } 298 else 299 fprintf_unfiltered (gdb_stderr, "Couldn't read float regs from core file\n"); 300 } 301 } 302 303 int 304 kernel_u_size () 305 { 306 return (sizeof (struct user)); 307 } 308 309 310 /* Register that we are able to handle sparc core file formats. 311 FIXME: is this really bfd_target_unknown_flavour? */ 312 313 static struct core_fns sparc_core_fns = 314 { 315 bfd_target_unknown_flavour, 316 fetch_core_registers, 317 NULL 318 }; 319 320 void 321 _initialize_core_sparc () 322 { 323 add_core_fns (&sparc_core_fns); 324 } 325