| 1 | /* Native-dependent code for BSD Unix running on i386's, for GDB. |
| 2 | Copyright 1988, 1989, 1991, 1992, 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 | |
| 22 | #ifdef FETCH_INFERIOR_REGISTERS |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/ptrace.h> |
| 25 | #include <machine/reg.h> |
| 26 | #include <machine/frame.h> |
| 27 | #include "inferior.h" |
| 28 | |
| 29 | void |
| 30 | fetch_inferior_registers(regno) |
| 31 | int regno; |
| 32 | { |
| 33 | struct reg inferior_registers; |
| 34 | |
| 35 | ptrace (PT_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &inferior_registers, 0); |
| 36 | memcpy (®isters[REGISTER_BYTE (0)], &inferior_registers, 4*NUM_REGS); |
| 37 | registers_fetched (); |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | store_inferior_registers(regno) |
| 42 | int regno; |
| 43 | { |
| 44 | struct reg inferior_registers; |
| 45 | |
| 46 | memcpy (&inferior_registers, ®isters[REGISTER_BYTE (0)], 4*NUM_REGS); |
| 47 | ptrace (PT_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) &inferior_registers, 0); |
| 48 | } |
| 49 | |
| 50 | struct md_core { |
| 51 | struct reg intreg; |
| 52 | struct fpreg freg; |
| 53 | }; |
| 54 | |
| 55 | void |
| 56 | fetch_core_registers (core_reg_sect, core_reg_size, which, ignore) |
| 57 | char *core_reg_sect; |
| 58 | unsigned core_reg_size; |
| 59 | int which; |
| 60 | CORE_ADDR ignore; |
| 61 | { |
| 62 | struct md_core *core_reg = (struct md_core *)core_reg_sect; |
| 63 | |
| 64 | /* integer registers */ |
| 65 | memcpy(®isters[REGISTER_BYTE (0)], &core_reg->intreg, |
| 66 | sizeof(struct reg)); |
| 67 | /* floating point registers */ |
| 68 | /* XXX */ |
| 69 | } |
| 70 | |
| 71 | #else |
| 72 | |
| 73 | #include <machine/reg.h> |
| 74 | |
| 75 | /* this table must line up with REGISTER_NAMES in tm-i386.h */ |
| 76 | /* symbols like 'tEAX' come from <machine/reg.h> */ |
| 77 | static int tregmap[] = |
| 78 | { |
| 79 | tEAX, tECX, tEDX, tEBX, |
| 80 | tESP, tEBP, tESI, tEDI, |
| 81 | tEIP, tEFLAGS, tCS, tSS |
| 82 | }; |
| 83 | |
| 84 | #ifdef sEAX |
| 85 | static int sregmap[] = |
| 86 | { |
| 87 | sEAX, sECX, sEDX, sEBX, |
| 88 | sESP, sEBP, sESI, sEDI, |
| 89 | sEIP, sEFLAGS, sCS, sSS |
| 90 | }; |
| 91 | #else /* No sEAX */ |
| 92 | |
| 93 | /* FreeBSD has decided to collapse the s* and t* symbols. So if the s* |
| 94 | ones aren't around, use the t* ones for sregmap too. */ |
| 95 | |
| 96 | static int sregmap[] = |
| 97 | { |
| 98 | tEAX, tECX, tEDX, tEBX, |
| 99 | tESP, tEBP, tESI, tEDI, |
| 100 | tEIP, tEFLAGS, tCS, tSS |
| 101 | }; |
| 102 | #endif /* No sEAX */ |
| 103 | |
| 104 | /* blockend is the value of u.u_ar0, and points to the |
| 105 | place where ES is stored. */ |
| 106 | |
| 107 | int |
| 108 | i386_register_u_addr (blockend, regnum) |
| 109 | int blockend; |
| 110 | int regnum; |
| 111 | { |
| 112 | /* The following condition is a kludge to get at the proper register map |
| 113 | depending upon the state of pcb_flag. |
| 114 | The proper condition would be |
| 115 | if (u.u_pcb.pcb_flag & FM_TRAP) |
| 116 | but that would require a ptrace call here and wouldn't work |
| 117 | for corefiles. */ |
| 118 | |
| 119 | if (blockend < 0x1fcc) |
| 120 | return (blockend + 4 * tregmap[regnum]); |
| 121 | else |
| 122 | return (blockend + 4 * sregmap[regnum]); |
| 123 | } |
| 124 | |
| 125 | #endif /* !FETCH_INFERIOR_REGISTERS */ |
| 126 | |
| 127 | #ifdef FLOAT_INFO |
| 128 | #include "language.h" /* for local_hex_string */ |
| 129 | #include "floatformat.h" |
| 130 | |
| 131 | #include <sys/param.h> |
| 132 | #include <sys/dir.h> |
| 133 | #include <signal.h> |
| 134 | #include <sys/ioctl.h> |
| 135 | #include <fcntl.h> |
| 136 | |
| 137 | #include <a.out.h> |
| 138 | |
| 139 | #include <sys/time.h> |
| 140 | #include <sys/resource.h> |
| 141 | #include <sys/uio.h> |
| 142 | #define curpcb Xcurpcb /* XXX avoid leaking declaration from pcb.h */ |
| 143 | #include <sys/user.h> |
| 144 | #undef curpcb |
| 145 | #include <sys/file.h> |
| 146 | #include "gdb_stat.h" |
| 147 | #include <sys/ptrace.h> |
| 148 | |
| 149 | extern void print_387_control_word (); /* i387-tdep.h */ |
| 150 | extern void print_387_status_word (); |
| 151 | |
| 152 | #define fpstate save87 |
| 153 | #define U_FPSTATE(u) u.u_pcb.pcb_savefpu |
| 154 | |
| 155 | struct env387 |
| 156 | { |
| 157 | unsigned short control; |
| 158 | unsigned short r0; |
| 159 | unsigned short status; |
| 160 | unsigned short r1; |
| 161 | unsigned short tag; |
| 162 | unsigned short r2; |
| 163 | unsigned long eip; |
| 164 | unsigned short code_seg; |
| 165 | unsigned short opcode; |
| 166 | unsigned long operand; |
| 167 | unsigned short operand_seg; |
| 168 | unsigned short r3; |
| 169 | unsigned char regs[8][10]; |
| 170 | }; |
| 171 | |
| 172 | static void |
| 173 | print_387_status (status, ep) |
| 174 | unsigned short status; |
| 175 | struct env387 *ep; |
| 176 | { |
| 177 | int i; |
| 178 | int bothstatus; |
| 179 | int top; |
| 180 | int fpreg; |
| 181 | |
| 182 | bothstatus = ((status != 0) && (ep->status != 0)); |
| 183 | if (status != 0) |
| 184 | { |
| 185 | if (bothstatus) |
| 186 | printf_unfiltered ("u: "); |
| 187 | print_387_status_word ((unsigned int)status); |
| 188 | } |
| 189 | |
| 190 | if (ep->status != 0) |
| 191 | { |
| 192 | if (bothstatus) |
| 193 | printf_unfiltered ("e: "); |
| 194 | print_387_status_word ((unsigned int)ep->status); |
| 195 | } |
| 196 | |
| 197 | print_387_control_word ((unsigned int)ep->control); |
| 198 | printf_unfiltered ("last exception: "); |
| 199 | printf_unfiltered ("opcode %s; ", local_hex_string(ep->opcode)); |
| 200 | printf_unfiltered ("pc %s:", local_hex_string(ep->code_seg)); |
| 201 | printf_unfiltered ("%s; ", local_hex_string(ep->eip)); |
| 202 | printf_unfiltered ("operand %s", local_hex_string(ep->operand_seg)); |
| 203 | printf_unfiltered (":%s\n", local_hex_string(ep->operand)); |
| 204 | |
| 205 | top = (ep->status >> 11) & 7; |
| 206 | |
| 207 | printf_unfiltered ("regno tag msb lsb value\n"); |
| 208 | for (fpreg = 7; fpreg >= 0; fpreg--) |
| 209 | { |
| 210 | double val; |
| 211 | |
| 212 | printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg); |
| 213 | |
| 214 | switch ((ep->tag >> (fpreg * 2)) & 3) |
| 215 | { |
| 216 | case 0: printf_unfiltered ("valid "); break; |
| 217 | case 1: printf_unfiltered ("zero "); break; |
| 218 | case 2: printf_unfiltered ("trap "); break; |
| 219 | case 3: printf_unfiltered ("empty "); break; |
| 220 | } |
| 221 | for (i = 9; i >= 0; i--) |
| 222 | printf_unfiltered ("%02x", ep->regs[fpreg][i]); |
| 223 | |
| 224 | floatformat_to_double(&floatformat_i387_ext, (char *) ep->regs[fpreg], |
| 225 | &val); |
| 226 | printf_unfiltered (" %g\n", val); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | i386_float_info () |
| 231 | { |
| 232 | struct user u; /* just for address computations */ |
| 233 | int i; |
| 234 | /* fpstate defined in <sys/user.h> */ |
| 235 | struct fpstate *fpstatep; |
| 236 | char buf[sizeof (struct fpstate) + 2 * sizeof (int)]; |
| 237 | unsigned int uaddr; |
| 238 | char fpvalid; |
| 239 | unsigned int rounded_addr; |
| 240 | unsigned int rounded_size; |
| 241 | /*extern int corechan;*/ |
| 242 | int skip; |
| 243 | extern int inferior_pid; |
| 244 | |
| 245 | uaddr = (char *)&U_FPSTATE(u) - (char *)&u; |
| 246 | if (inferior_pid) |
| 247 | { |
| 248 | int *ip; |
| 249 | |
| 250 | rounded_addr = uaddr & -sizeof (int); |
| 251 | rounded_size = (((uaddr + sizeof (struct fpstate)) - uaddr) + |
| 252 | sizeof (int) - 1) / sizeof (int); |
| 253 | skip = uaddr - rounded_addr; |
| 254 | |
| 255 | ip = (int *)buf; |
| 256 | for (i = 0; i < rounded_size; i++) |
| 257 | { |
| 258 | *ip++ = ptrace (PT_READ_U, inferior_pid, (caddr_t)rounded_addr, 0); |
| 259 | rounded_addr += sizeof (int); |
| 260 | } |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | printf("float info: can't do a core file (yet)\n"); |
| 265 | return; |
| 266 | #if 0 |
| 267 | if (lseek (corechan, uaddr, 0) < 0) |
| 268 | perror_with_name ("seek on core file"); |
| 269 | if (myread (corechan, buf, sizeof (struct fpstate)) < 0) |
| 270 | perror_with_name ("read from core file"); |
| 271 | skip = 0; |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | print_387_status (0, (struct env387 *)buf); |
| 276 | } |
| 277 | |
| 278 | int |
| 279 | kernel_u_size () |
| 280 | { |
| 281 | return (sizeof (struct user)); |
| 282 | } |
| 283 | |
| 284 | #endif |