| 1 | /* Native support for Sun 386i's for GDB, the GNU debugger. |
| 2 | Copyright (C) 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc. |
| 3 | Changes for sun386i by Jean Daniel Fekete (jdf@litp.univ-p6-7.fr), |
| 4 | C2V Paris, April 89. |
| 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 21 | |
| 22 | #if defined (GDB_TARGET_IS_SUN386) |
| 23 | |
| 24 | #include "defs.h" |
| 25 | #include "frame.h" |
| 26 | #include "inferior.h" |
| 27 | #include "gdbcore.h" |
| 28 | |
| 29 | #include <sys/param.h> |
| 30 | #include <sys/dir.h> |
| 31 | #include <sys/user.h> |
| 32 | #include <signal.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <fcntl.h> |
| 35 | |
| 36 | #include <sys/ptrace.h> |
| 37 | #include <machine/reg.h> |
| 38 | |
| 39 | #include <sys/file.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <sys/core.h> |
| 42 | |
| 43 | \f |
| 44 | /* Machine-dependent code which would otherwise be in core.c */ |
| 45 | /* Work with core files, for GDB. */ |
| 46 | |
| 47 | \f |
| 48 | void |
| 49 | core_file_command (filename, from_tty) |
| 50 | char *filename; |
| 51 | int from_tty; |
| 52 | { |
| 53 | int val; |
| 54 | extern char registers[]; |
| 55 | |
| 56 | /* Discard all vestiges of any previous core file |
| 57 | and mark data and stack spaces as empty. */ |
| 58 | |
| 59 | if (corefile) |
| 60 | free (corefile); |
| 61 | corefile = 0; |
| 62 | |
| 63 | if (corechan >= 0) |
| 64 | close (corechan); |
| 65 | corechan = -1; |
| 66 | |
| 67 | data_start = 0; |
| 68 | data_end = 0; |
| 69 | stack_start = STACK_END_ADDR; |
| 70 | stack_end = STACK_END_ADDR; |
| 71 | |
| 72 | /* Now, if a new core file was specified, open it and digest it. */ |
| 73 | |
| 74 | if (filename) |
| 75 | { |
| 76 | filename = tilde_expand (filename); |
| 77 | make_cleanup (free, filename); |
| 78 | |
| 79 | if (have_inferior_p ()) |
| 80 | error ("To look at a core file, you must kill the program with \"kill\"."); |
| 81 | corechan = open (filename, O_RDONLY, 0); |
| 82 | if (corechan < 0) |
| 83 | perror_with_name (filename); |
| 84 | |
| 85 | { |
| 86 | struct core corestr; |
| 87 | |
| 88 | val = myread (corechan, &corestr, sizeof corestr); |
| 89 | if (val < 0) |
| 90 | perror_with_name (filename); |
| 91 | if (corestr.c_magic != CORE_MAGIC) |
| 92 | error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)", |
| 93 | filename, corestr.c_magic, (int) CORE_MAGIC); |
| 94 | else if (sizeof (struct core) != corestr.c_len) |
| 95 | error ("\"%s\" has an invalid struct core length (%d, expected %d)", |
| 96 | filename, corestr.c_len, (int) sizeof (struct core)); |
| 97 | |
| 98 | data_start = exec_data_start; |
| 99 | data_end = data_start + corestr.c_dsize; |
| 100 | stack_start = stack_end - corestr.c_ssize; |
| 101 | data_offset = sizeof corestr; |
| 102 | stack_offset = sizeof corestr + corestr.c_dsize; |
| 103 | |
| 104 | memcpy (registers, &corestr.c_regs, sizeof corestr.c_regs); |
| 105 | |
| 106 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], |
| 107 | corestr.c_fpu.f_fpstatus.f_st, |
| 108 | sizeof corestr.c_fpu.f_fpstatus.f_st); |
| 109 | memcpy (®isters[REGISTER_BYTE (FPC_REGNUM)], |
| 110 | &corestr.c_fpu.f_fpstatus.f_ctrl, |
| 111 | sizeof corestr.c_fpu.f_fpstatus - |
| 112 | sizeof corestr.c_fpu.f_fpstatus.f_st); |
| 113 | |
| 114 | /* the struct aouthdr of sun coff is not the struct exec stored |
| 115 | in the core file. */ |
| 116 | memcpy (&core_aouthdr, &corestr.c_aouthdr, sizeof (struct exec)); |
| 117 | #ifndef COFF_ENCAPSULATE |
| 118 | core_aouthdr.magic = corestr.c_aouthdr.a_info; |
| 119 | core_aouthdr.vstamp = /*SUNVERSION*/ 31252; |
| 120 | #endif |
| 121 | printf_unfiltered ("Core file is from \"%s\".\n", corestr.c_cmdname); |
| 122 | if (corestr.c_signo > 0) |
| 123 | printf_unfiltered ("Program terminated with signal %d, %s.\n", |
| 124 | corestr.c_signo, safe_strsignal (corestr.c_signo)); |
| 125 | } |
| 126 | if (filename[0] == '/') |
| 127 | corefile = savestring (filename, strlen (filename)); |
| 128 | else |
| 129 | { |
| 130 | corefile = concat (current_directory, "/", filename, NULL); |
| 131 | } |
| 132 | |
| 133 | set_current_frame ( create_new_frame (read_register (FP_REGNUM), |
| 134 | read_pc ())); |
| 135 | select_frame (get_current_frame (), 0); |
| 136 | |
| 137 | validate_files (); |
| 138 | } |
| 139 | else if (from_tty) |
| 140 | printf_unfiltered ("No core file now.\n"); |
| 141 | } |
| 142 | |
| 143 | i387_to_double (from, to) |
| 144 | char *from; |
| 145 | char *to; |
| 146 | { |
| 147 | long *lp; |
| 148 | /* push extended mode on 387 stack, then pop in double mode |
| 149 | * |
| 150 | * first, set exception masks so no error is generated - |
| 151 | * number will be rounded to inf or 0, if necessary |
| 152 | */ |
| 153 | asm ("pushl %eax"); /* grab a stack slot */ |
| 154 | asm ("fstcw (%esp)"); /* get 387 control word */ |
| 155 | asm ("movl (%esp),%eax"); /* save old value */ |
| 156 | asm ("orl $0x3f,%eax"); /* mask all exceptions */ |
| 157 | asm ("pushl %eax"); |
| 158 | asm ("fldcw (%esp)"); /* load new value into 387 */ |
| 159 | |
| 160 | asm ("movl 8(%ebp),%eax"); |
| 161 | asm ("fldt (%eax)"); /* push extended number on 387 stack */ |
| 162 | asm ("fwait"); |
| 163 | asm ("movl 12(%ebp),%eax"); |
| 164 | asm ("fstpl (%eax)"); /* pop double */ |
| 165 | asm ("fwait"); |
| 166 | |
| 167 | asm ("popl %eax"); /* flush modified control word */ |
| 168 | asm ("fnclex"); /* clear exceptions */ |
| 169 | asm ("fldcw (%esp)"); /* restore original control word */ |
| 170 | asm ("popl %eax"); /* flush saved copy */ |
| 171 | } |
| 172 | |
| 173 | double_to_i387 (from, to) |
| 174 | char *from; |
| 175 | char *to; |
| 176 | { |
| 177 | /* push double mode on 387 stack, then pop in extended mode |
| 178 | * no errors are possible because every 64-bit pattern |
| 179 | * can be converted to an extended |
| 180 | */ |
| 181 | asm ("movl 8(%ebp),%eax"); |
| 182 | asm ("fldl (%eax)"); |
| 183 | asm ("fwait"); |
| 184 | asm ("movl 12(%ebp),%eax"); |
| 185 | asm ("fstpt (%eax)"); |
| 186 | asm ("fwait"); |
| 187 | } |
| 188 | #else /* Not sun386 target. */ |
| 189 | |
| 190 | /* These functions shouldn't be called when we're cross-debugging. */ |
| 191 | |
| 192 | /* ARGSUSED */ |
| 193 | void |
| 194 | fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr) |
| 195 | char *core_reg_sect; |
| 196 | unsigned core_reg_size; |
| 197 | int which; |
| 198 | unsigned int reg_addr; /* Unused in this version */ |
| 199 | { |
| 200 | } |
| 201 | |
| 202 | #endif /* Not sun386 target. */ |
| 203 | |
| 204 | void |
| 205 | fetch_inferior_registers (regno) |
| 206 | int regno; |
| 207 | { |
| 208 | struct regs inferior_registers; |
| 209 | struct fp_state inferior_fp_registers; |
| 210 | extern char registers[]; |
| 211 | |
| 212 | registers_fetched (); |
| 213 | |
| 214 | ptrace (PTRACE_GETREGS, inferior_pid, |
| 215 | (PTRACE_ARG3_TYPE) &inferior_registers); |
| 216 | ptrace (PTRACE_GETFPREGS, inferior_pid, |
| 217 | (PTRACE_ARG3_TYPE) &inferior_fp_registers); |
| 218 | |
| 219 | memcpy (registers, &inferior_registers, sizeof inferior_registers); |
| 220 | |
| 221 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)],inferior_fp_registers.f_st, |
| 222 | sizeof inferior_fp_registers.f_st); |
| 223 | memcpy (®isters[REGISTER_BYTE (FPC_REGNUM)], |
| 224 | &inferior_fp_registers.f_ctrl, |
| 225 | sizeof inferior_fp_registers - sizeof inferior_fp_registers.f_st); |
| 226 | } |
| 227 | |
| 228 | /* Store our register values back into the inferior. |
| 229 | If REGNO is -1, do this for all registers. |
| 230 | Otherwise, REGNO specifies which register (so we can save time). */ |
| 231 | |
| 232 | void |
| 233 | store_inferior_registers (regno) |
| 234 | int regno; |
| 235 | { |
| 236 | struct regs inferior_registers; |
| 237 | struct fp_state inferior_fp_registers; |
| 238 | extern char registers[]; |
| 239 | |
| 240 | memcpy (&inferior_registers, registers, 20 * 4); |
| 241 | |
| 242 | memcpy (inferior_fp_registers.f_st, |
| 243 | ®isters[REGISTER_BYTE (FP0_REGNUM)], |
| 244 | sizeof inferior_fp_registers.f_st); |
| 245 | memcpy (&inferior_fp_registers.f_ctrl, |
| 246 | ®isters[REGISTER_BYTE (FPC_REGNUM)], |
| 247 | sizeof inferior_fp_registers - sizeof inferior_fp_registers.f_st); |
| 248 | |
| 249 | #ifdef PTRACE_FP_BUG |
| 250 | if (regno == FP_REGNUM || regno == -1) |
| 251 | /* Storing the frame pointer requires a gross hack, in which an |
| 252 | instruction that moves eax into ebp gets single-stepped. */ |
| 253 | { |
| 254 | int stack = inferior_registers.r_reg[SP_REGNUM]; |
| 255 | int stuff = ptrace (PTRACE_PEEKDATA, inferior_pid, |
| 256 | (PTRACE_ARG3_TYPE) stack); |
| 257 | int reg = inferior_registers.r_reg[EAX]; |
| 258 | inferior_registers.r_reg[EAX] = |
| 259 | inferior_registers.r_reg[FP_REGNUM]; |
| 260 | ptrace (PTRACE_SETREGS, inferior_pid, |
| 261 | (PTRACE_ARG3_TYPE) &inferior_registers); |
| 262 | ptrace (PTRACE_POKEDATA, inferior_pid, (PTRACE_ARG3_TYPE) stack, |
| 263 | 0xc589); |
| 264 | ptrace (PTRACE_SINGLESTEP, inferior_pid, (PTRACE_ARG3_TYPE) stack, |
| 265 | 0); |
| 266 | wait (0); |
| 267 | ptrace (PTRACE_POKEDATA, inferior_pid, (PTRACE_ARG3_TYPE) stack, |
| 268 | stuff); |
| 269 | inferior_registers.r_reg[EAX] = reg; |
| 270 | } |
| 271 | #endif |
| 272 | ptrace (PTRACE_SETREGS, inferior_pid, |
| 273 | (PTRACE_ARG3_TYPE) &inferior_registers); |
| 274 | ptrace (PTRACE_SETFPREGS, inferior_pid, |
| 275 | (PTRACE_ARG3_TYPE) &inferior_fp_registers); |
| 276 | } |
| 277 | |