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