| 1 | /* Low level interface to i386 running the GNU Hurd. |
| 2 | |
| 3 | Copyright 1992, 1995, 1996, 1998, 2000, 2001, 2004 |
| 4 | 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 "floatformat.h" |
| 26 | #include "regcache.h" |
| 27 | |
| 28 | #include "gdb_assert.h" |
| 29 | #include <errno.h> |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | #include <mach.h> |
| 33 | #include <mach_error.h> |
| 34 | #include <mach/message.h> |
| 35 | #include <mach/exception.h> |
| 36 | |
| 37 | #include "i386-tdep.h" |
| 38 | |
| 39 | #include "gnu-nat.h" |
| 40 | #include "i387-tdep.h" |
| 41 | |
| 42 | #ifdef HAVE_SYS_PROCFS_H |
| 43 | # include <sys/procfs.h> |
| 44 | # include "gregset.h" |
| 45 | #endif |
| 46 | |
| 47 | /* Offset to the thread_state_t location where REG is stored. */ |
| 48 | #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg) |
| 49 | |
| 50 | /* At REG_OFFSET[N] is the offset to the thread_state_t location where |
| 51 | the GDB register N is stored. */ |
| 52 | static int reg_offset[] = |
| 53 | { |
| 54 | REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx), |
| 55 | REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi), |
| 56 | REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss), |
| 57 | REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs) |
| 58 | }; |
| 59 | |
| 60 | #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum]) |
| 61 | |
| 62 | \f |
| 63 | /* Get the whole floating-point state of THREAD and record the |
| 64 | values of the corresponding (pseudo) registers. */ |
| 65 | static void |
| 66 | fetch_fpregs (struct proc *thread) |
| 67 | { |
| 68 | mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT; |
| 69 | struct i386_float_state state; |
| 70 | error_t err; |
| 71 | |
| 72 | err = thread_get_state (thread->port, i386_FLOAT_STATE, |
| 73 | (thread_state_t) &state, &count); |
| 74 | if (err) |
| 75 | { |
| 76 | warning ("Couldn't fetch floating-point state from %s", |
| 77 | proc_string (thread)); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (!state.initialized) |
| 82 | /* The floating-point state isn't initialized. */ |
| 83 | { |
| 84 | int i; |
| 85 | |
| 86 | for (i = FP0_REGNUM; i <= FOP_REGNUM; i++) |
| 87 | regcache_raw_supply (current_regcache, i, NULL); |
| 88 | |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | /* Supply the floating-point registers. */ |
| 93 | i387_supply_fsave (current_regcache, -1, state.hw_state); |
| 94 | } |
| 95 | |
| 96 | #ifdef HAVE_SYS_PROCFS_H |
| 97 | /* These two calls are used by the core-regset.c code for |
| 98 | reading ELF core files. */ |
| 99 | void |
| 100 | supply_gregset (gdb_gregset_t *gregs) |
| 101 | { |
| 102 | int i; |
| 103 | for (i = 0; i < I386_NUM_GREGS; i++) |
| 104 | regcache_raw_supply (current_regcache, i, REG_ADDR (gregs, i)); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | supply_fpregset (gdb_fpregset_t *fpregs) |
| 109 | { |
| 110 | i387_supply_fsave (current_regcache, -1, fpregs); |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | /* Fetch register REGNO, or all regs if REGNO is -1. */ |
| 115 | void |
| 116 | gnu_fetch_registers (int regno) |
| 117 | { |
| 118 | struct proc *thread; |
| 119 | |
| 120 | /* Make sure we know about new threads. */ |
| 121 | inf_update_procs (current_inferior); |
| 122 | |
| 123 | thread = inf_tid_to_thread (current_inferior, PIDGET (inferior_ptid)); |
| 124 | if (!thread) |
| 125 | error ("Can't fetch registers from thread %d: No such thread", |
| 126 | PIDGET (inferior_ptid)); |
| 127 | |
| 128 | if (regno < I386_NUM_GREGS || regno == -1) |
| 129 | { |
| 130 | thread_state_t state; |
| 131 | |
| 132 | /* This does the dirty work for us. */ |
| 133 | state = proc_get_state (thread, 0); |
| 134 | if (!state) |
| 135 | { |
| 136 | warning ("Couldn't fetch registers from %s", |
| 137 | proc_string (thread)); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if (regno == -1) |
| 142 | { |
| 143 | int i; |
| 144 | |
| 145 | proc_debug (thread, "fetching all register"); |
| 146 | |
| 147 | for (i = 0; i < I386_NUM_GREGS; i++) |
| 148 | regcache_raw_supply (current_regcache, i, REG_ADDR (state, i)); |
| 149 | thread->fetched_regs = ~0; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | proc_debug (thread, "fetching register %s", REGISTER_NAME (regno)); |
| 154 | |
| 155 | regcache_raw_supply (current_regcache, regno, |
| 156 | REG_ADDR (state, regno)); |
| 157 | thread->fetched_regs |= (1 << regno); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (regno >= I386_NUM_GREGS || regno == -1) |
| 162 | { |
| 163 | proc_debug (thread, "fetching floating-point registers"); |
| 164 | |
| 165 | fetch_fpregs (thread); |
| 166 | } |
| 167 | } |
| 168 | \f |
| 169 | |
| 170 | /* Store the whole floating-point state into THREAD using information |
| 171 | from the corresponding (pseudo) registers. */ |
| 172 | static void |
| 173 | store_fpregs (struct proc *thread, int regno) |
| 174 | { |
| 175 | mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT; |
| 176 | struct i386_float_state state; |
| 177 | error_t err; |
| 178 | |
| 179 | err = thread_get_state (thread->port, i386_FLOAT_STATE, |
| 180 | (thread_state_t) &state, &count); |
| 181 | if (err) |
| 182 | { |
| 183 | warning ("Couldn't fetch floating-point state from %s", |
| 184 | proc_string (thread)); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | /* FIXME: kettenis/2001-07-15: Is this right? Should we somehow |
| 189 | take into account DEPRECATED_REGISTER_VALID like the old code did? */ |
| 190 | i387_fill_fsave (state.hw_state, regno); |
| 191 | |
| 192 | err = thread_set_state (thread->port, i386_FLOAT_STATE, |
| 193 | (thread_state_t) &state, i386_FLOAT_STATE_COUNT); |
| 194 | if (err) |
| 195 | { |
| 196 | warning ("Couldn't store floating-point state into %s", |
| 197 | proc_string (thread)); |
| 198 | return; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* Store at least register REGNO, or all regs if REGNO == -1. */ |
| 203 | void |
| 204 | gnu_store_registers (int regno) |
| 205 | { |
| 206 | struct regcache *regcache = current_regcache; |
| 207 | struct proc *thread; |
| 208 | |
| 209 | /* Make sure we know about new threads. */ |
| 210 | inf_update_procs (current_inferior); |
| 211 | |
| 212 | thread = inf_tid_to_thread (current_inferior, PIDGET (inferior_ptid)); |
| 213 | if (!thread) |
| 214 | error ("Couldn't store registers into thread %d: No such thread", |
| 215 | PIDGET (inferior_ptid)); |
| 216 | |
| 217 | if (regno < I386_NUM_GREGS || regno == -1) |
| 218 | { |
| 219 | thread_state_t state; |
| 220 | thread_state_data_t old_state; |
| 221 | int was_aborted = thread->aborted; |
| 222 | int was_valid = thread->state_valid; |
| 223 | int trace; |
| 224 | |
| 225 | if (!was_aborted && was_valid) |
| 226 | memcpy (&old_state, &thread->state, sizeof (old_state)); |
| 227 | |
| 228 | state = proc_get_state (thread, 1); |
| 229 | if (!state) |
| 230 | { |
| 231 | warning ("Couldn't store registers into %s", proc_string (thread)); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | /* Save the T bit. We might try to restore the %eflags register |
| 236 | below, but changing the T bit would seriously confuse GDB. */ |
| 237 | trace = ((struct i386_thread_state *)state)->efl & 0x100; |
| 238 | |
| 239 | if (!was_aborted && was_valid) |
| 240 | /* See which registers have changed after aborting the thread. */ |
| 241 | { |
| 242 | int check_regno; |
| 243 | |
| 244 | for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++) |
| 245 | if ((thread->fetched_regs & (1 << check_regno)) |
| 246 | && memcpy (REG_ADDR (&old_state, check_regno), |
| 247 | REG_ADDR (state, check_regno), |
| 248 | register_size (current_gdbarch, check_regno))) |
| 249 | /* Register CHECK_REGNO has changed! Ack! */ |
| 250 | { |
| 251 | warning ("Register %s changed after the thread was aborted", |
| 252 | REGISTER_NAME (check_regno)); |
| 253 | if (regno >= 0 && regno != check_regno) |
| 254 | /* Update GDB's copy of the register. */ |
| 255 | regcache_raw_supply (regcache, check_regno, |
| 256 | REG_ADDR (state, check_regno)); |
| 257 | else |
| 258 | warning ("... also writing this register! Suspicious..."); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (regno == -1) |
| 263 | { |
| 264 | int i; |
| 265 | |
| 266 | proc_debug (thread, "storing all registers"); |
| 267 | |
| 268 | for (i = 0; i < I386_NUM_GREGS; i++) |
| 269 | if (regcache_valid_p (regcache, i)) |
| 270 | regcache_raw_collect (regcache, i, REG_ADDR (state, i)); |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | proc_debug (thread, "storing register %s", REGISTER_NAME (regno)); |
| 275 | |
| 276 | gdb_assert (regcache_valid_p (regcache, regno)); |
| 277 | regcache_raw_collect (regcache, regno, REG_ADDR (state, regno)); |
| 278 | } |
| 279 | |
| 280 | /* Restore the T bit. */ |
| 281 | ((struct i386_thread_state *)state)->efl &= ~0x100; |
| 282 | ((struct i386_thread_state *)state)->efl |= trace; |
| 283 | } |
| 284 | |
| 285 | if (regno >= I386_NUM_GREGS || regno == -1) |
| 286 | { |
| 287 | proc_debug (thread, "storing floating-point registers"); |
| 288 | |
| 289 | store_fpregs (thread, regno); |
| 290 | } |
| 291 | } |