| 1 | /* Native dependent code for Mach 386's for GDB, the GNU debugger. |
| 2 | Copyright (C) 1986, 1987, 1989, 1991, 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., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "frame.h" |
| 23 | #include "inferior.h" |
| 24 | #include "gdbcore.h" |
| 25 | |
| 26 | #include <sys/param.h> |
| 27 | #include <sys/dir.h> |
| 28 | #include <sys/user.h> |
| 29 | #include <signal.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <fcntl.h> |
| 32 | |
| 33 | #include <sys/ptrace.h> |
| 34 | #include <machine/reg.h> |
| 35 | |
| 36 | #include <sys/file.h> |
| 37 | #include "gdb_stat.h" |
| 38 | #include <sys/core.h> |
| 39 | |
| 40 | static void fetch_core_registers (char *, unsigned, int, CORE_ADDR); |
| 41 | |
| 42 | void |
| 43 | fetch_inferior_registers (int regno) |
| 44 | { |
| 45 | struct regs inferior_registers; |
| 46 | struct fp_state inferior_fp_registers; |
| 47 | |
| 48 | registers_fetched (); |
| 49 | |
| 50 | ptrace (PTRACE_GETREGS, inferior_pid, |
| 51 | (PTRACE_ARG3_TYPE) & inferior_registers); |
| 52 | ptrace (PTRACE_GETFPREGS, inferior_pid, |
| 53 | (PTRACE_ARG3_TYPE) & inferior_fp_registers); |
| 54 | |
| 55 | memcpy (registers, &inferior_registers, sizeof inferior_registers); |
| 56 | |
| 57 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], |
| 58 | inferior_fp_registers.f_st, |
| 59 | sizeof inferior_fp_registers.f_st); |
| 60 | memcpy (®isters[REGISTER_BYTE (FPC_REGNUM)], |
| 61 | &inferior_fp_registers.f_ctrl, |
| 62 | sizeof inferior_fp_registers - sizeof inferior_fp_registers.f_st); |
| 63 | } |
| 64 | |
| 65 | /* Store our register values back into the inferior. |
| 66 | If REGNO is -1, do this for all registers. |
| 67 | Otherwise, REGNO specifies which register (so we can save time). */ |
| 68 | |
| 69 | void |
| 70 | store_inferior_registers (int regno) |
| 71 | { |
| 72 | struct regs inferior_registers; |
| 73 | struct fp_state inferior_fp_registers; |
| 74 | |
| 75 | memcpy (&inferior_registers, registers, 20 * 4); |
| 76 | |
| 77 | memcpy (inferior_fp_registers.f_st, ®isters[REGISTER_BYTE (FP0_REGNUM)], |
| 78 | sizeof inferior_fp_registers.f_st); |
| 79 | memcpy (&inferior_fp_registers.f_ctrl, |
| 80 | ®isters[REGISTER_BYTE (FPC_REGNUM)], |
| 81 | sizeof inferior_fp_registers - sizeof inferior_fp_registers.f_st); |
| 82 | |
| 83 | #ifdef PTRACE_FP_BUG |
| 84 | if (regno == FP_REGNUM || regno == -1) |
| 85 | /* Storing the frame pointer requires a gross hack, in which an |
| 86 | instruction that moves eax into ebp gets single-stepped. */ |
| 87 | { |
| 88 | int stack = inferior_registers.r_reg[SP_REGNUM]; |
| 89 | int stuff = ptrace (PTRACE_PEEKDATA, inferior_pid, |
| 90 | (PTRACE_ARG3_TYPE) stack); |
| 91 | int reg = inferior_registers.r_reg[EAX]; |
| 92 | inferior_registers.r_reg[EAX] = |
| 93 | inferior_registers.r_reg[FP_REGNUM]; |
| 94 | ptrace (PTRACE_SETREGS, inferior_pid, |
| 95 | (PTRACE_ARG3_TYPE) & inferior_registers); |
| 96 | ptrace (PTRACE_POKEDATA, inferior_pid, (PTRACE_ARG3_TYPE) stack, 0xc589); |
| 97 | ptrace (PTRACE_SINGLESTEP, inferior_pid, (PTRACE_ARG3_TYPE) stack, 0); |
| 98 | wait (0); |
| 99 | ptrace (PTRACE_POKEDATA, inferior_pid, (PTRACE_ARG3_TYPE) stack, stuff); |
| 100 | inferior_registers.r_reg[EAX] = reg; |
| 101 | } |
| 102 | #endif |
| 103 | ptrace (PTRACE_SETREGS, inferior_pid, |
| 104 | (PTRACE_ARG3_TYPE) & inferior_registers); |
| 105 | ptrace (PTRACE_SETFPREGS, inferior_pid, |
| 106 | (PTRACE_ARG3_TYPE) & inferior_fp_registers); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | /* Provide registers to GDB from a core file. |
| 112 | |
| 113 | CORE_REG_SECT points to an array of bytes, which were obtained from |
| 114 | a core file which BFD thinks might contain register contents. |
| 115 | CORE_REG_SIZE is its size. |
| 116 | |
| 117 | WHICH says which register set corelow suspects this is: |
| 118 | 0 --- the general-purpose register set |
| 119 | 2 --- the floating-point register set |
| 120 | |
| 121 | REG_ADDR isn't used. */ |
| 122 | |
| 123 | static void |
| 124 | fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, |
| 125 | int which, CORE_ADDR reg_addr) |
| 126 | { |
| 127 | int val; |
| 128 | |
| 129 | switch (which) |
| 130 | { |
| 131 | case 0: |
| 132 | case 1: |
| 133 | memcpy (registers, core_reg_sect, core_reg_size); |
| 134 | break; |
| 135 | |
| 136 | case 2: |
| 137 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], |
| 138 | core_reg_sect, |
| 139 | core_reg_size); /* FIXME, probably bogus */ |
| 140 | #ifdef FPC_REGNUM |
| 141 | memcpy (®isters[REGISTER_BYTE (FPC_REGNUM)], |
| 142 | &corestr.c_fpu.f_fpstatus.f_ctrl, |
| 143 | sizeof corestr.c_fpu.f_fpstatus - |
| 144 | sizeof corestr.c_fpu.f_fpstatus.f_st); |
| 145 | #endif |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | \f |
| 150 | |
| 151 | /* Register that we are able to handle i386mach core file formats. |
| 152 | FIXME: is this really bfd_target_unknown_flavour? */ |
| 153 | |
| 154 | static struct core_fns i386mach_core_fns = |
| 155 | { |
| 156 | bfd_target_unknown_flavour, /* core_flavour */ |
| 157 | default_check_format, /* check_format */ |
| 158 | default_core_sniffer, /* core_sniffer */ |
| 159 | fetch_core_registers, /* core_read_registers */ |
| 160 | NULL /* next */ |
| 161 | }; |
| 162 | |
| 163 | void |
| 164 | _initialize_core_i386mach (void) |
| 165 | { |
| 166 | add_core_fns (&i386mach_core_fns); |
| 167 | } |