| 1 | /* Native-dependent code for modern i386 BSD's. |
| 2 | Copyright 2000, 2001 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, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "inferior.h" |
| 23 | #include "regcache.h" |
| 24 | |
| 25 | #include "gdb_assert.h" |
| 26 | #include <signal.h> |
| 27 | #include <stddef.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/ptrace.h> |
| 30 | #include <machine/reg.h> |
| 31 | #include <machine/frame.h> |
| 32 | |
| 33 | #ifdef HAVE_SYS_PROCFS_H |
| 34 | #include <sys/procfs.h> |
| 35 | #endif |
| 36 | |
| 37 | #ifndef HAVE_GREGSET_T |
| 38 | typedef struct reg gregset_t; |
| 39 | #endif |
| 40 | |
| 41 | #ifndef HAVE_FPREGSET_T |
| 42 | typedef struct fpreg fpregset_t; |
| 43 | #endif |
| 44 | |
| 45 | #include "gregset.h" |
| 46 | \f |
| 47 | |
| 48 | /* In older BSD versions we cannot get at some of the segment |
| 49 | registers. FreeBSD for example didn't support the %fs and %gs |
| 50 | registers until the 3.0 release. We have autoconf checks for their |
| 51 | presence, and deal gracefully with their absence. */ |
| 52 | |
| 53 | /* Registers we shouldn't try to fetch. */ |
| 54 | #if !defined (CANNOT_FETCH_REGISTER) |
| 55 | #define CANNOT_FETCH_REGISTER(regno) cannot_fetch_register (regno) |
| 56 | #endif |
| 57 | |
| 58 | /* Registers we shouldn't try to store. */ |
| 59 | #if !defined (CANNOT_STORE_REGISTER) |
| 60 | #define CANNOT_STORE_REGISTER(regno) cannot_fetch_register (regno) |
| 61 | #endif |
| 62 | |
| 63 | /* Offset to the gregset_t location where REG is stored. */ |
| 64 | #define REG_OFFSET(reg) offsetof (gregset_t, reg) |
| 65 | |
| 66 | /* At reg_offset[REGNO] you'll find the offset to the gregset_t |
| 67 | location where the GDB register REGNO is stored. Unsupported |
| 68 | registers are marked with `-1'. */ |
| 69 | static int reg_offset[] = |
| 70 | { |
| 71 | REG_OFFSET (r_eax), |
| 72 | REG_OFFSET (r_ecx), |
| 73 | REG_OFFSET (r_edx), |
| 74 | REG_OFFSET (r_edx), |
| 75 | REG_OFFSET (r_esp), |
| 76 | REG_OFFSET (r_ebp), |
| 77 | REG_OFFSET (r_esi), |
| 78 | REG_OFFSET (r_edi), |
| 79 | REG_OFFSET (r_eip), |
| 80 | REG_OFFSET (r_eflags), |
| 81 | REG_OFFSET (r_cs), |
| 82 | REG_OFFSET (r_ss), |
| 83 | REG_OFFSET (r_ds), |
| 84 | REG_OFFSET (r_es), |
| 85 | #ifdef HAVE_STRUCT_REG_R_FS |
| 86 | REG_OFFSET (r_fs), |
| 87 | #else |
| 88 | -1, |
| 89 | #endif |
| 90 | #ifdef HAVE_STRUCT_REG_R_GS |
| 91 | REG_OFFSET (r_gs) |
| 92 | #else |
| 93 | -1 |
| 94 | #endif |
| 95 | }; |
| 96 | |
| 97 | #define REG_ADDR(regset, regno) ((char *) (regset) + reg_offset[regno]) |
| 98 | |
| 99 | /* Return nonzero if we shouldn't try to fetch register REGNO. */ |
| 100 | |
| 101 | static int |
| 102 | cannot_fetch_register (int regno) |
| 103 | { |
| 104 | return (reg_offset[regno] == -1); |
| 105 | } |
| 106 | \f |
| 107 | |
| 108 | /* Transfering the registers between GDB, inferiors and core files. */ |
| 109 | |
| 110 | /* Fill GDB's register array with the general-purpose register values |
| 111 | in *GREGSETP. */ |
| 112 | |
| 113 | void |
| 114 | supply_gregset (gregset_t *gregsetp) |
| 115 | { |
| 116 | int i; |
| 117 | |
| 118 | for (i = 0; i < NUM_GREGS; i++) |
| 119 | { |
| 120 | if (CANNOT_FETCH_REGISTER (i)) |
| 121 | supply_register (i, NULL); |
| 122 | else |
| 123 | supply_register (i, REG_ADDR (gregsetp, i)); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* Fill register REGNO (if it is a general-purpose register) in |
| 128 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, |
| 129 | do this for all registers. */ |
| 130 | |
| 131 | void |
| 132 | fill_gregset (gregset_t *gregsetp, int regno) |
| 133 | { |
| 134 | int i; |
| 135 | |
| 136 | for (i = 0; i < NUM_GREGS; i++) |
| 137 | if ((regno == -1 || regno == i) && ! CANNOT_STORE_REGISTER (i)) |
| 138 | memcpy (REG_ADDR (gregsetp, i), ®isters[REGISTER_BYTE (i)], |
| 139 | REGISTER_RAW_SIZE (i)); |
| 140 | } |
| 141 | |
| 142 | #include "i387-nat.h" |
| 143 | |
| 144 | /* Fill GDB's register array with the floating-point register values |
| 145 | in *FPREGSETP. */ |
| 146 | |
| 147 | void |
| 148 | supply_fpregset (fpregset_t *fpregsetp) |
| 149 | { |
| 150 | i387_supply_fsave ((char *) fpregsetp); |
| 151 | } |
| 152 | |
| 153 | /* Fill register REGNO (if it is a floating-point register) in |
| 154 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, |
| 155 | do this for all registers. */ |
| 156 | |
| 157 | void |
| 158 | fill_fpregset (fpregset_t *fpregsetp, int regno) |
| 159 | { |
| 160 | i387_fill_fsave ((char *) fpregsetp, regno); |
| 161 | } |
| 162 | |
| 163 | /* Fetch register REGNO from the inferior. If REGNO is -1, do this |
| 164 | for all registers (including the floating point registers). */ |
| 165 | |
| 166 | void |
| 167 | fetch_inferior_registers (int regno) |
| 168 | { |
| 169 | gregset_t gregs; |
| 170 | |
| 171 | if (ptrace (PT_GETREGS, PIDGET (inferior_ptid), |
| 172 | (PTRACE_ARG3_TYPE) &gregs, 0) == -1) |
| 173 | perror_with_name ("Couldn't get registers"); |
| 174 | |
| 175 | supply_gregset (&gregs); |
| 176 | |
| 177 | if (regno == -1 || regno >= FP0_REGNUM) |
| 178 | { |
| 179 | fpregset_t fpregs; |
| 180 | |
| 181 | if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid), |
| 182 | (PTRACE_ARG3_TYPE) &fpregs, 0) == -1) |
| 183 | perror_with_name ("Couldn't get floating point status"); |
| 184 | |
| 185 | supply_fpregset (&fpregs); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* Store register REGNO back into the inferior. If REGNO is -1, do |
| 190 | this for all registers (including the floating point registers). */ |
| 191 | |
| 192 | void |
| 193 | store_inferior_registers (int regno) |
| 194 | { |
| 195 | gregset_t gregs; |
| 196 | |
| 197 | if (ptrace (PT_GETREGS, PIDGET (inferior_ptid), |
| 198 | (PTRACE_ARG3_TYPE) &gregs, 0) == -1) |
| 199 | perror_with_name ("Couldn't get registers"); |
| 200 | |
| 201 | fill_gregset (&gregs, regno); |
| 202 | |
| 203 | if (ptrace (PT_SETREGS, PIDGET (inferior_ptid), |
| 204 | (PTRACE_ARG3_TYPE) &gregs, 0) == -1) |
| 205 | perror_with_name ("Couldn't write registers"); |
| 206 | |
| 207 | if (regno == -1 || regno >= FP0_REGNUM) |
| 208 | { |
| 209 | fpregset_t fpregs; |
| 210 | |
| 211 | if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid), |
| 212 | (PTRACE_ARG3_TYPE) &fpregs, 0) == -1) |
| 213 | perror_with_name ("Couldn't get floating point status"); |
| 214 | |
| 215 | fill_fpregset (&fpregs, regno); |
| 216 | |
| 217 | if (ptrace (PT_SETFPREGS, PIDGET (inferior_ptid), |
| 218 | (PTRACE_ARG3_TYPE) &fpregs, 0) == -1) |
| 219 | perror_with_name ("Couldn't write floating point status"); |
| 220 | } |
| 221 | } |
| 222 | \f |
| 223 | |
| 224 | /* Support for debug registers. */ |
| 225 | |
| 226 | #ifdef HAVE_PT_GETDBREGS |
| 227 | |
| 228 | /* Not all versions of FreeBSD/i386 that support the debug registers |
| 229 | have this macro. */ |
| 230 | #ifndef DBREG_DRX |
| 231 | #define DBREG_DRX(d, x) ((&d->dr0)[x]) |
| 232 | #endif |
| 233 | |
| 234 | static void |
| 235 | i386bsd_dr_set (int regnum, unsigned int value) |
| 236 | { |
| 237 | struct dbreg dbregs; |
| 238 | |
| 239 | if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid), |
| 240 | (PTRACE_ARG3_TYPE) &dbregs, 0) == -1) |
| 241 | perror_with_name ("Couldn't get debug registers"); |
| 242 | |
| 243 | /* For some mysterious reason, some of the reserved bits in the |
| 244 | debug control register get set. Mask these off, otherwise the |
| 245 | ptrace call below will fail. */ |
| 246 | dbregs.dr7 &= ~(0x0000fc00); |
| 247 | |
| 248 | DBREG_DRX ((&dbregs), regnum) = value; |
| 249 | |
| 250 | if (ptrace (PT_SETDBREGS, PIDGET (inferior_ptid), |
| 251 | (PTRACE_ARG3_TYPE) &dbregs, 0) == -1) |
| 252 | perror_with_name ("Couldn't write debug registers"); |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | i386bsd_dr_set_control (unsigned long control) |
| 257 | { |
| 258 | i386bsd_dr_set (7, control); |
| 259 | } |
| 260 | |
| 261 | void |
| 262 | i386bsd_dr_set_addr (int regnum, CORE_ADDR addr) |
| 263 | { |
| 264 | gdb_assert (regnum >= 0 && regnum <= 4); |
| 265 | |
| 266 | i386bsd_dr_set (regnum, addr); |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | i386bsd_dr_reset_addr (int regnum) |
| 271 | { |
| 272 | gdb_assert (regnum >= 0 && regnum <= 4); |
| 273 | |
| 274 | i386bsd_dr_set (regnum, 0); |
| 275 | } |
| 276 | |
| 277 | unsigned long |
| 278 | i386bsd_dr_get_status (void) |
| 279 | { |
| 280 | struct dbreg dbregs; |
| 281 | |
| 282 | /* FIXME: kettenis/2001-03-31: Calling perror_with_name if the |
| 283 | ptrace call fails breaks debugging remote targets. The correct |
| 284 | way to fix this is to add the hardware breakpoint and watchpoint |
| 285 | stuff to the target vector. For now, just return zero if the |
| 286 | ptrace call fails. */ |
| 287 | if (ptrace (PT_GETDBREGS, PIDGET (inferior_ptid), |
| 288 | (PTRACE_ARG3_TYPE) & dbregs, 0) == -1) |
| 289 | #if 0 |
| 290 | perror_with_name ("Couldn't read debug registers"); |
| 291 | #else |
| 292 | return 0; |
| 293 | #endif |
| 294 | |
| 295 | return dbregs.dr6; |
| 296 | } |
| 297 | |
| 298 | #endif /* PT_GETDBREGS */ |
| 299 | \f |
| 300 | |
| 301 | /* Support for the user struct. */ |
| 302 | |
| 303 | /* Return the address register REGNO. BLOCKEND is the value of |
| 304 | u.u_ar0, which should point to the registers. */ |
| 305 | |
| 306 | CORE_ADDR |
| 307 | register_u_addr (CORE_ADDR blockend, int regno) |
| 308 | { |
| 309 | return (CORE_ADDR) REG_ADDR (blockend, regno); |
| 310 | } |
| 311 | |
| 312 | #include <sys/param.h> |
| 313 | #include <sys/user.h> |
| 314 | |
| 315 | /* Return the size of the user struct. */ |
| 316 | |
| 317 | int |
| 318 | kernel_u_size (void) |
| 319 | { |
| 320 | return (sizeof (struct user)); |
| 321 | } |
| 322 | \f |
| 323 | /* See i386bsd-tdep.c. */ |
| 324 | extern int i386bsd_sigcontext_pc_offset; |
| 325 | |
| 326 | void |
| 327 | _initialize_i386bsd_nat (void) |
| 328 | { |
| 329 | /* To support the recognition of signal handlers, i386bsd-tdep.c |
| 330 | hardcodes some constants. Inclusion of this file means that we |
| 331 | are compiling a native debugger, which means that we can use the |
| 332 | system header files and sysctl(3) to get at the relevant |
| 333 | information. */ |
| 334 | |
| 335 | /* Override the default value for the offset of the program counter |
| 336 | in the sigcontext structure. */ |
| 337 | i386bsd_sigcontext_pc_offset = offsetof (struct sigcontext, sc_pc); |
| 338 | } |