| 1 | /* Functions specific to running gdb native on a Sun 4 running sunos4. |
| 2 | Copyright (C) 1989, 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 | #include "defs.h" |
| 21 | #include "inferior.h" |
| 22 | #include "target.h" |
| 23 | |
| 24 | #include <signal.h> |
| 25 | #include <sys/ptrace.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <machine/reg.h> |
| 28 | |
| 29 | /* We don't store all registers immediately when requested, since they |
| 30 | get sent over in large chunks anyway. Instead, we accumulate most |
| 31 | of the changes and send them over once. "deferred_stores" keeps |
| 32 | track of which sets of registers we have locally-changed copies of, |
| 33 | so we only need send the groups that have changed. */ |
| 34 | |
| 35 | #define INT_REGS 1 |
| 36 | #define STACK_REGS 2 |
| 37 | #define FP_REGS 4 |
| 38 | |
| 39 | /* Fetch one or more registers from the inferior. REGNO == -1 to get |
| 40 | them all. We actually fetch more than requested, when convenient, |
| 41 | marking them as valid so we won't fetch them again. */ |
| 42 | |
| 43 | void |
| 44 | fetch_inferior_registers (regno) |
| 45 | int regno; |
| 46 | { |
| 47 | struct regs inferior_registers; |
| 48 | struct fp_status inferior_fp_registers; |
| 49 | int i; |
| 50 | |
| 51 | /* We should never be called with deferred stores, because a prerequisite |
| 52 | for writing regs is to have fetched them all (PREPARE_TO_STORE), sigh. */ |
| 53 | if (deferred_stores) abort(); |
| 54 | |
| 55 | DO_DEFERRED_STORES; |
| 56 | |
| 57 | /* Global and Out regs are fetched directly, as well as the control |
| 58 | registers. If we're getting one of the in or local regs, |
| 59 | and the stack pointer has not yet been fetched, |
| 60 | we have to do that first, since they're found in memory relative |
| 61 | to the stack pointer. */ |
| 62 | if (regno < O7_REGNUM /* including -1 */ |
| 63 | || regno >= Y_REGNUM |
| 64 | || (!register_valid[SP_REGNUM] && regno < I7_REGNUM)) |
| 65 | { |
| 66 | if (0 != ptrace (PTRACE_GETREGS, inferior_pid, |
| 67 | (PTRACE_ARG3_TYPE) &inferior_registers, 0)) |
| 68 | perror("ptrace_getregs"); |
| 69 | |
| 70 | registers[REGISTER_BYTE (0)] = 0; |
| 71 | memcpy (®isters[REGISTER_BYTE (1)], &inferior_registers.r_g1, |
| 72 | 15 * REGISTER_RAW_SIZE (G0_REGNUM)); |
| 73 | *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps; |
| 74 | *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc; |
| 75 | *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc; |
| 76 | *(int *)®isters[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y; |
| 77 | |
| 78 | for (i = G0_REGNUM; i <= O7_REGNUM; i++) |
| 79 | register_valid[i] = 1; |
| 80 | register_valid[Y_REGNUM] = 1; |
| 81 | register_valid[PS_REGNUM] = 1; |
| 82 | register_valid[PC_REGNUM] = 1; |
| 83 | register_valid[NPC_REGNUM] = 1; |
| 84 | /* If we don't set these valid, read_register_bytes() rereads |
| 85 | all the regs every time it is called! FIXME. */ |
| 86 | register_valid[WIM_REGNUM] = 1; /* Not true yet, FIXME */ |
| 87 | register_valid[TBR_REGNUM] = 1; /* Not true yet, FIXME */ |
| 88 | register_valid[FPS_REGNUM] = 1; /* Not true yet, FIXME */ |
| 89 | register_valid[CPS_REGNUM] = 1; /* Not true yet, FIXME */ |
| 90 | } |
| 91 | |
| 92 | /* Floating point registers */ |
| 93 | if (regno == -1 || (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31)) |
| 94 | { |
| 95 | if (0 != ptrace (PTRACE_GETFPREGS, inferior_pid, |
| 96 | (PTRACE_ARG3_TYPE) &inferior_fp_registers, |
| 97 | 0)) |
| 98 | perror("ptrace_getfpregs"); |
| 99 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers, |
| 100 | sizeof inferior_fp_registers.fpu_fr); |
| 101 | /* memcpy (®isters[REGISTER_BYTE (FPS_REGNUM)], |
| 102 | &inferior_fp_registers.Fpu_fsr, |
| 103 | sizeof (FPU_FSR_TYPE)); FIXME??? -- gnu@cyg */ |
| 104 | for (i = FP0_REGNUM; i <= FP0_REGNUM+31; i++) |
| 105 | register_valid[i] = 1; |
| 106 | register_valid[FPS_REGNUM] = 1; |
| 107 | } |
| 108 | |
| 109 | /* These regs are saved on the stack by the kernel. Only read them |
| 110 | all (16 ptrace calls!) if we really need them. */ |
| 111 | if (regno == -1) |
| 112 | { |
| 113 | target_xfer_memory (*(CORE_ADDR*)®isters[REGISTER_BYTE (SP_REGNUM)], |
| 114 | ®isters[REGISTER_BYTE (L0_REGNUM)], |
| 115 | 16*REGISTER_RAW_SIZE (L0_REGNUM), 0); |
| 116 | for (i = L0_REGNUM; i <= I7_REGNUM; i++) |
| 117 | register_valid[i] = 1; |
| 118 | } |
| 119 | else if (regno >= L0_REGNUM && regno <= I7_REGNUM) |
| 120 | { |
| 121 | CORE_ADDR sp = *(CORE_ADDR*)®isters[REGISTER_BYTE (SP_REGNUM)]; |
| 122 | i = REGISTER_BYTE (regno); |
| 123 | if (register_valid[regno]) |
| 124 | printf("register %d valid and read\n", regno); |
| 125 | target_xfer_memory (sp + i - REGISTER_BYTE (L0_REGNUM), |
| 126 | ®isters[i], REGISTER_RAW_SIZE (regno), 0); |
| 127 | register_valid[regno] = 1; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Store our register values back into the inferior. |
| 132 | If REGNO is -1, do this for all registers. |
| 133 | Otherwise, REGNO specifies which register (so we can save time). */ |
| 134 | |
| 135 | void |
| 136 | store_inferior_registers (regno) |
| 137 | int regno; |
| 138 | { |
| 139 | struct regs inferior_registers; |
| 140 | struct fp_status inferior_fp_registers; |
| 141 | int wanna_store = INT_REGS + STACK_REGS + FP_REGS; |
| 142 | |
| 143 | /* First decide which pieces of machine-state we need to modify. |
| 144 | Default for regno == -1 case is all pieces. */ |
| 145 | if (regno >= 0) |
| 146 | if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32) |
| 147 | { |
| 148 | wanna_store = FP_REGS; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | if (regno == SP_REGNUM) |
| 153 | wanna_store = INT_REGS + STACK_REGS; |
| 154 | else if (regno < L0_REGNUM || regno > I7_REGNUM) |
| 155 | wanna_store = INT_REGS; |
| 156 | else |
| 157 | wanna_store = STACK_REGS; |
| 158 | } |
| 159 | |
| 160 | /* See if we're forcing the stores to happen now, or deferring. */ |
| 161 | if (regno == -2) |
| 162 | { |
| 163 | wanna_store = deferred_stores; |
| 164 | deferred_stores = 0; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | if (wanna_store == STACK_REGS) |
| 169 | { |
| 170 | /* Fall through and just store one stack reg. If we deferred |
| 171 | it, we'd have to store them all, or remember more info. */ |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | deferred_stores |= wanna_store; |
| 176 | return; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (wanna_store & STACK_REGS) |
| 181 | { |
| 182 | CORE_ADDR sp = *(CORE_ADDR *)®isters[REGISTER_BYTE (SP_REGNUM)]; |
| 183 | |
| 184 | if (regno < 0 || regno == SP_REGNUM) |
| 185 | { |
| 186 | if (!register_valid[L0_REGNUM+5]) abort(); |
| 187 | target_xfer_memory (sp, |
| 188 | ®isters[REGISTER_BYTE (L0_REGNUM)], |
| 189 | 16*REGISTER_RAW_SIZE (L0_REGNUM), 1); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | if (!register_valid[regno]) abort(); |
| 194 | target_xfer_memory (sp + REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM), |
| 195 | ®isters[REGISTER_BYTE (regno)], |
| 196 | REGISTER_RAW_SIZE (regno), 1); |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |
| 201 | if (wanna_store & INT_REGS) |
| 202 | { |
| 203 | if (!register_valid[G1_REGNUM]) abort(); |
| 204 | |
| 205 | memcpy (&inferior_registers.r_g1, ®isters[REGISTER_BYTE (G1_REGNUM)], |
| 206 | 15 * REGISTER_RAW_SIZE (G1_REGNUM)); |
| 207 | |
| 208 | inferior_registers.r_ps = |
| 209 | *(int *)®isters[REGISTER_BYTE (PS_REGNUM)]; |
| 210 | inferior_registers.r_pc = |
| 211 | *(int *)®isters[REGISTER_BYTE (PC_REGNUM)]; |
| 212 | inferior_registers.r_npc = |
| 213 | *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)]; |
| 214 | inferior_registers.r_y = |
| 215 | *(int *)®isters[REGISTER_BYTE (Y_REGNUM)]; |
| 216 | |
| 217 | if (0 != ptrace (PTRACE_SETREGS, inferior_pid, |
| 218 | (PTRACE_ARG3_TYPE) &inferior_registers, 0)) |
| 219 | perror("ptrace_setregs"); |
| 220 | } |
| 221 | |
| 222 | if (wanna_store & FP_REGS) |
| 223 | { |
| 224 | if (!register_valid[FP0_REGNUM+9]) abort(); |
| 225 | /* Initialize inferior_fp_registers members that gdb doesn't set |
| 226 | by reading them from the inferior. */ |
| 227 | if (0 != |
| 228 | ptrace (PTRACE_GETFPREGS, inferior_pid, |
| 229 | (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0)) |
| 230 | perror("ptrace_getfpregs"); |
| 231 | memcpy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)], |
| 232 | sizeof inferior_fp_registers.fpu_fr); |
| 233 | |
| 234 | /* memcpy (&inferior_fp_registers.Fpu_fsr, |
| 235 | ®isters[REGISTER_BYTE (FPS_REGNUM)], sizeof (FPU_FSR_TYPE)); |
| 236 | ****/ |
| 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 | 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 | unsigned int 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 so user can still use gdb */ |
| 282 | fprintf (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 (stderr, "Couldn't read float regs from core file\n"); |
| 300 | } |
| 301 | } |
| 302 | |