1 /* Low level interface to i386 running the GNU Hurd.
3 Copyright (C) 1992, 1995, 1996, 1998, 2000, 2001, 2004, 2007
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "floatformat.h"
26 #include "gdb_assert.h"
29 #include "gdb_string.h"
32 #include <mach_error.h>
33 #include <mach/message.h>
34 #include <mach/exception.h>
36 #include "i386-tdep.h"
39 #include "i387-tdep.h"
41 #ifdef HAVE_SYS_PROCFS_H
42 # include <sys/procfs.h>
46 /* Offset to the thread_state_t location where REG is stored. */
47 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
49 /* At REG_OFFSET[N] is the offset to the thread_state_t location where
50 the GDB register N is stored. */
51 static int reg_offset[] =
53 REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
54 REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
55 REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
56 REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
59 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
60 #define CREG_ADDR(state, regnum) ((const char *)(state) + reg_offset[regnum])
63 /* Get the whole floating-point state of THREAD and record the values
64 of the corresponding (pseudo) registers. */
67 fetch_fpregs (struct regcache *regcache, struct proc *thread)
69 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
70 struct i386_float_state state;
73 err = thread_get_state (thread->port, i386_FLOAT_STATE,
74 (thread_state_t) &state, &count);
77 warning (_("Couldn't fetch floating-point state from %s"),
78 proc_string (thread));
82 if (!state.initialized)
84 /* The floating-point state isn't initialized. */
85 i387_supply_fsave (regcache, -1, NULL);
89 /* Supply the floating-point registers. */
90 i387_supply_fsave (regcache, -1, state.hw_state);
94 #ifdef HAVE_SYS_PROCFS_H
95 /* These two calls are used by the core-regset.c code for
96 reading ELF core files. */
98 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregs)
101 for (i = 0; i < I386_NUM_GREGS; i++)
102 regcache_raw_supply (regcache, i, CREG_ADDR (gregs, i));
106 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
108 i387_supply_fsave (regcache, -1, fpregs);
112 /* Fetch register REGNO, or all regs if REGNO is -1. */
114 gnu_fetch_registers (struct regcache *regcache, int regno)
118 /* Make sure we know about new threads. */
119 inf_update_procs (current_inferior);
121 thread = inf_tid_to_thread (current_inferior, PIDGET (inferior_ptid));
123 error (_("Can't fetch registers from thread %d: No such thread"),
124 PIDGET (inferior_ptid));
126 if (regno < I386_NUM_GREGS || regno == -1)
128 thread_state_t state;
130 /* This does the dirty work for us. */
131 state = proc_get_state (thread, 0);
134 warning (_("Couldn't fetch registers from %s"),
135 proc_string (thread));
143 proc_debug (thread, "fetching all register");
145 for (i = 0; i < I386_NUM_GREGS; i++)
146 regcache_raw_supply (regcache, i, REG_ADDR (state, i));
147 thread->fetched_regs = ~0;
151 proc_debug (thread, "fetching register %s",
152 gdbarch_register_name (current_gdbarch, regno));
154 regcache_raw_supply (regcache, regno,
155 REG_ADDR (state, regno));
156 thread->fetched_regs |= (1 << regno);
160 if (regno >= I386_NUM_GREGS || regno == -1)
162 proc_debug (thread, "fetching floating-point registers");
164 fetch_fpregs (regcache, thread);
169 /* Store the whole floating-point state into THREAD using information
170 from the corresponding (pseudo) registers. */
172 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
174 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
175 struct i386_float_state state;
178 err = thread_get_state (thread->port, i386_FLOAT_STATE,
179 (thread_state_t) &state, &count);
182 warning (_("Couldn't fetch floating-point state from %s"),
183 proc_string (thread));
187 /* FIXME: kettenis/2001-07-15: Is this right? Should we somehow
188 take into account DEPRECATED_REGISTER_VALID like the old code did? */
189 i387_collect_fsave (regcache, regno, state.hw_state);
191 err = thread_set_state (thread->port, i386_FLOAT_STATE,
192 (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
195 warning (_("Couldn't store floating-point state into %s"),
196 proc_string (thread));
201 /* Store at least register REGNO, or all regs if REGNO == -1. */
203 gnu_store_registers (struct regcache *regcache, int regno)
207 /* Make sure we know about new threads. */
208 inf_update_procs (current_inferior);
210 thread = inf_tid_to_thread (current_inferior, PIDGET (inferior_ptid));
212 error (_("Couldn't store registers into thread %d: No such thread"),
213 PIDGET (inferior_ptid));
215 if (regno < I386_NUM_GREGS || regno == -1)
217 thread_state_t state;
218 thread_state_data_t old_state;
219 int was_aborted = thread->aborted;
220 int was_valid = thread->state_valid;
223 if (!was_aborted && was_valid)
224 memcpy (&old_state, &thread->state, sizeof (old_state));
226 state = proc_get_state (thread, 1);
229 warning (_("Couldn't store registers into %s"), proc_string (thread));
233 /* Save the T bit. We might try to restore the %eflags register
234 below, but changing the T bit would seriously confuse GDB. */
235 trace = ((struct i386_thread_state *)state)->efl & 0x100;
237 if (!was_aborted && was_valid)
238 /* See which registers have changed after aborting the thread. */
242 for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
243 if ((thread->fetched_regs & (1 << check_regno))
244 && memcpy (REG_ADDR (&old_state, check_regno),
245 REG_ADDR (state, check_regno),
246 register_size (current_gdbarch, check_regno)))
247 /* Register CHECK_REGNO has changed! Ack! */
249 warning (_("Register %s changed after the thread was aborted"),
250 gdbarch_register_name (current_gdbarch, check_regno));
251 if (regno >= 0 && regno != check_regno)
252 /* Update GDB's copy of the register. */
253 regcache_raw_supply (regcache, check_regno,
254 REG_ADDR (state, check_regno));
256 warning (_("... also writing this register! Suspicious..."));
264 proc_debug (thread, "storing all registers");
266 for (i = 0; i < I386_NUM_GREGS; i++)
267 if (regcache_valid_p (regcache, i))
268 regcache_raw_collect (regcache, i, REG_ADDR (state, i));
272 proc_debug (thread, "storing register %s",
273 gdbarch_register_name (current_gdbarch, regno));
275 gdb_assert (regcache_valid_p (regcache, regno));
276 regcache_raw_collect (regcache, regno, REG_ADDR (state, regno));
279 /* Restore the T bit. */
280 ((struct i386_thread_state *)state)->efl &= ~0x100;
281 ((struct i386_thread_state *)state)->efl |= trace;
284 if (regno >= I386_NUM_GREGS || regno == -1)
286 proc_debug (thread, "storing floating-point registers");
288 store_fpregs (regcache, thread, regno);