| 1 | /* Functions specific to running gdb native on a SPARC running SunOS4. |
| 2 | |
| 3 | Copyright 1989, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, |
| 4 | 2001, 2002, 2003 Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "inferior.h" |
| 25 | #include "target.h" |
| 26 | #include "gdbcore.h" |
| 27 | #include "regcache.h" |
| 28 | |
| 29 | #ifdef HAVE_SYS_PARAM_H |
| 30 | #include <sys/param.h> |
| 31 | #endif |
| 32 | #include <signal.h> |
| 33 | #include <sys/ptrace.h> |
| 34 | #include "gdb_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 | |
| 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 |
| 57 | fetch_inferior_registers (int regno) |
| 58 | { |
| 59 | struct regs inferior_registers; |
| 60 | struct fp_status inferior_fp_registers; |
| 61 | int i; |
| 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); |
| 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. */ |
| 84 | if (deferred_stores) |
| 85 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
| 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. */ |
| 94 | if (regno < O7_REGNUM /* including -1 */ |
| 95 | || regno >= Y_REGNUM |
| 96 | || (!deprecated_register_valid[SP_REGNUM] && regno < I7_REGNUM)) |
| 97 | { |
| 98 | if (0 != ptrace (PTRACE_GETREGS, fetch_pid, |
| 99 | (PTRACE_ARG3_TYPE) & inferior_registers, 0)) |
| 100 | perror ("ptrace_getregs"); |
| 101 | |
| 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; |
| 111 | *(int *) ®isters[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y; |
| 112 | |
| 113 | for (i = G0_REGNUM; i <= O7_REGNUM; i++) |
| 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; |
| 119 | /* If we don't set these valid, read_register_bytes() rereads |
| 120 | all the regs every time it is called! FIXME. */ |
| 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 */ |
| 124 | } |
| 125 | |
| 126 | /* Floating point registers */ |
| 127 | if (regno == -1 || |
| 128 | regno == FPS_REGNUM || |
| 129 | (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31)) |
| 130 | { |
| 131 | if (0 != ptrace (PTRACE_GETFPREGS, fetch_pid, |
| 132 | (PTRACE_ARG3_TYPE) & inferior_fp_registers, |
| 133 | 0)) |
| 134 | perror ("ptrace_getfpregs"); |
| 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)); |
| 139 | for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++) |
| 140 | deprecated_register_valid[i] = 1; |
| 141 | deprecated_register_valid[FPS_REGNUM] = 1; |
| 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 | { |
| 148 | CORE_ADDR sp = *(unsigned int *) & deprecated_registers[REGISTER_BYTE (SP_REGNUM)]; |
| 149 | target_read_memory (sp, &deprecated_registers[REGISTER_BYTE (L0_REGNUM)], |
| 150 | 16 * REGISTER_RAW_SIZE (L0_REGNUM)); |
| 151 | for (i = L0_REGNUM; i <= I7_REGNUM; i++) |
| 152 | deprecated_register_valid[i] = 1; |
| 153 | } |
| 154 | else if (regno >= L0_REGNUM && regno <= I7_REGNUM) |
| 155 | { |
| 156 | CORE_ADDR sp = *(unsigned int *) & deprecated_registers[REGISTER_BYTE (SP_REGNUM)]; |
| 157 | i = REGISTER_BYTE (regno); |
| 158 | if (deprecated_register_valid[regno]) |
| 159 | printf_unfiltered ("register %d valid and read\n", regno); |
| 160 | target_read_memory (sp + i - REGISTER_BYTE (L0_REGNUM), |
| 161 | &deprecated_registers[i], REGISTER_RAW_SIZE (regno)); |
| 162 | deprecated_register_valid[regno] = 1; |
| 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 |
| 171 | store_inferior_registers (int regno) |
| 172 | { |
| 173 | struct regs inferior_registers; |
| 174 | struct fp_status inferior_fp_registers; |
| 175 | int wanna_store = INT_REGS + STACK_REGS + FP_REGS; |
| 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); |
| 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) |
| 187 | { |
| 188 | if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32) |
| 189 | { |
| 190 | wanna_store = FP_REGS; |
| 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 | } |
| 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 | { |
| 227 | CORE_ADDR sp = *(unsigned int *) & deprecated_registers[REGISTER_BYTE (SP_REGNUM)]; |
| 228 | |
| 229 | if (regno < 0 || regno == SP_REGNUM) |
| 230 | { |
| 231 | if (!deprecated_register_valid[L0_REGNUM + 5]) |
| 232 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
| 233 | target_write_memory (sp, |
| 234 | &deprecated_registers[REGISTER_BYTE (L0_REGNUM)], |
| 235 | 16 * REGISTER_RAW_SIZE (L0_REGNUM)); |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | if (!deprecated_register_valid[regno]) |
| 240 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
| 241 | target_write_memory (sp + REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM), |
| 242 | &deprecated_registers[REGISTER_BYTE (regno)], |
| 243 | REGISTER_RAW_SIZE (regno)); |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | |
| 248 | if (wanna_store & INT_REGS) |
| 249 | { |
| 250 | if (!deprecated_register_valid[G1_REGNUM]) |
| 251 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
| 252 | |
| 253 | memcpy (&inferior_registers.r_g1, |
| 254 | &deprecated_registers[REGISTER_BYTE (G1_REGNUM)], |
| 255 | 15 * REGISTER_RAW_SIZE (G1_REGNUM)); |
| 256 | |
| 257 | inferior_registers.r_ps = |
| 258 | *(int *) &deprecated_registers[REGISTER_BYTE (PS_REGNUM)]; |
| 259 | inferior_registers.r_pc = |
| 260 | *(int *) &deprecated_registers[REGISTER_BYTE (PC_REGNUM)]; |
| 261 | inferior_registers.r_npc = |
| 262 | *(int *) &deprecated_registers[REGISTER_BYTE (NPC_REGNUM)]; |
| 263 | inferior_registers.r_y = |
| 264 | *(int *) &deprecated_registers[REGISTER_BYTE (Y_REGNUM)]; |
| 265 | |
| 266 | if (0 != ptrace (PTRACE_SETREGS, store_pid, |
| 267 | (PTRACE_ARG3_TYPE) & inferior_registers, 0)) |
| 268 | perror ("ptrace_setregs"); |
| 269 | } |
| 270 | |
| 271 | if (wanna_store & FP_REGS) |
| 272 | { |
| 273 | if (!deprecated_register_valid[FP0_REGNUM + 9]) |
| 274 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
| 275 | memcpy (&inferior_fp_registers, |
| 276 | &deprecated_registers[REGISTER_BYTE (FP0_REGNUM)], |
| 277 | sizeof inferior_fp_registers.fpu_fr); |
| 278 | memcpy (&inferior_fp_registers.Fpu_fsr, |
| 279 | &deprecated_registers[REGISTER_BYTE (FPS_REGNUM)], |
| 280 | sizeof (FPU_FSR_TYPE)); |
| 281 | if (0 != |
| 282 | ptrace (PTRACE_SETFPREGS, store_pid, |
| 283 | (PTRACE_ARG3_TYPE) & inferior_fp_registers, 0)) |
| 284 | perror ("ptrace_setfpregs"); |
| 285 | } |
| 286 | } |
| 287 | |
| 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. */ |
| 299 | |
| 300 | static void |
| 301 | fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, |
| 302 | int which, CORE_ADDR ignore) |
| 303 | { |
| 304 | |
| 305 | if (which == 0) |
| 306 | { |
| 307 | |
| 308 | /* Integer registers */ |
| 309 | |
| 310 | #define gregs ((struct regs *)core_reg_sect) |
| 311 | /* G0 *always* holds 0. */ |
| 312 | *(int *) &deprecated_registers[REGISTER_BYTE (0)] = 0; |
| 313 | |
| 314 | /* The globals and output registers. */ |
| 315 | memcpy (&deprecated_registers[REGISTER_BYTE (G1_REGNUM)], &gregs->r_g1, |
| 316 | 15 * REGISTER_RAW_SIZE (G1_REGNUM)); |
| 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; |
| 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 | |
| 330 | sp = *(int *) &deprecated_registers[REGISTER_BYTE (SP_REGNUM)]; |
| 331 | if (0 != target_read_memory (sp, |
| 332 | &deprecated_registers[REGISTER_BYTE (L0_REGNUM)], |
| 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 | } |
| 340 | } |
| 341 | else if (which == 2) |
| 342 | { |
| 343 | |
| 344 | /* Floating point registers */ |
| 345 | |
| 346 | #define fpuregs ((struct fpu *) core_reg_sect) |
| 347 | if (core_reg_size >= sizeof (struct fpu)) |
| 348 | { |
| 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)); |
| 353 | } |
| 354 | else |
| 355 | fprintf_unfiltered (gdb_stderr, "Couldn't read float regs from core file\n"); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | int |
| 360 | kernel_u_size (void) |
| 361 | { |
| 362 | return (sizeof (struct user)); |
| 363 | } |
| 364 | \f |
| 365 | |
| 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 | { |
| 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 */ |
| 376 | }; |
| 377 | |
| 378 | void |
| 379 | _initialize_core_sparc (void) |
| 380 | { |
| 381 | add_core_fns (&sparc_core_fns); |
| 382 | } |