Commit | Line | Data |
---|---|---|
da59e081 | 1 | /* Low level interface to i386 running the GNU Hurd. |
f6052080 | 2 | |
6aba47ca | 3 | Copyright (C) 1992, 1995, 1996, 1998, 2000, 2001, 2004, 2007 |
b6ba6518 | 4 | Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
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 | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 11 | (at your option) any later version. |
c906108c | 12 | |
c5aa993b JM |
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. | |
c906108c | 17 | |
c5aa993b | 18 | You should have received a copy of the GNU General Public License |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
20 | |
21 | #include "defs.h" | |
22 | #include "inferior.h" | |
23 | #include "floatformat.h" | |
4e052eda | 24 | #include "regcache.h" |
c906108c | 25 | |
780a49fa | 26 | #include "gdb_assert.h" |
c906108c | 27 | #include <errno.h> |
383d750b | 28 | #include <stdio.h> |
3303a276 | 29 | #include "gdb_string.h" |
c906108c SS |
30 | |
31 | #include <mach.h> | |
da59e081 | 32 | #include <mach_error.h> |
c906108c SS |
33 | #include <mach/message.h> |
34 | #include <mach/exception.h> | |
c906108c | 35 | |
4604bcad MK |
36 | #include "i386-tdep.h" |
37 | ||
c906108c | 38 | #include "gnu-nat.h" |
e750d25e | 39 | #include "i387-tdep.h" |
c906108c | 40 | |
57e76fac MS |
41 | #ifdef HAVE_SYS_PROCFS_H |
42 | # include <sys/procfs.h> | |
43 | # include "gregset.h" | |
44 | #endif | |
c906108c | 45 | |
da59e081 JM |
46 | /* Offset to the thread_state_t location where REG is stored. */ |
47 | #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg) | |
c906108c | 48 | |
383d750b MK |
49 | /* At REG_OFFSET[N] is the offset to the thread_state_t location where |
50 | the GDB register N is stored. */ | |
c5aa993b | 51 | static int reg_offset[] = |
c906108c | 52 | { |
c5aa993b JM |
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) | |
c906108c SS |
57 | }; |
58 | ||
da59e081 | 59 | #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum]) |
7f7fe91e | 60 | #define CREG_ADDR(state, regnum) ((const char *)(state) + reg_offset[regnum]) |
da59e081 | 61 | |
c906108c | 62 | \f |
3303a276 MK |
63 | /* Get the whole floating-point state of THREAD and record the values |
64 | of the corresponding (pseudo) registers. */ | |
65 | ||
da59e081 | 66 | static void |
56be3814 | 67 | fetch_fpregs (struct regcache *regcache, struct proc *thread) |
c906108c | 68 | { |
da59e081 JM |
69 | mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT; |
70 | struct i386_float_state state; | |
da59e081 | 71 | error_t err; |
c906108c | 72 | |
da59e081 JM |
73 | err = thread_get_state (thread->port, i386_FLOAT_STATE, |
74 | (thread_state_t) &state, &count); | |
75 | if (err) | |
c906108c | 76 | { |
8a3fe4f8 | 77 | warning (_("Couldn't fetch floating-point state from %s"), |
da59e081 JM |
78 | proc_string (thread)); |
79 | return; | |
c906108c | 80 | } |
da59e081 | 81 | |
383d750b | 82 | if (!state.initialized) |
c906108c | 83 | { |
3303a276 | 84 | /* The floating-point state isn't initialized. */ |
56be3814 | 85 | i387_supply_fsave (regcache, -1, NULL); |
3303a276 MK |
86 | } |
87 | else | |
88 | { | |
89 | /* Supply the floating-point registers. */ | |
56be3814 | 90 | i387_supply_fsave (regcache, -1, state.hw_state); |
c906108c SS |
91 | } |
92 | } | |
da59e081 | 93 | |
57e76fac MS |
94 | #ifdef HAVE_SYS_PROCFS_H |
95 | /* These two calls are used by the core-regset.c code for | |
96 | reading ELF core files. */ | |
97 | void | |
7f7fe91e | 98 | supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregs) |
57e76fac MS |
99 | { |
100 | int i; | |
65e78234 | 101 | for (i = 0; i < I386_NUM_GREGS; i++) |
7f7fe91e | 102 | regcache_raw_supply (regcache, i, CREG_ADDR (gregs, i)); |
57e76fac MS |
103 | } |
104 | ||
105 | void | |
7f7fe91e | 106 | supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs) |
57e76fac | 107 | { |
7f7fe91e | 108 | i387_supply_fsave (regcache, -1, fpregs); |
57e76fac MS |
109 | } |
110 | #endif | |
111 | ||
da59e081 | 112 | /* Fetch register REGNO, or all regs if REGNO is -1. */ |
c906108c | 113 | void |
56be3814 | 114 | gnu_fetch_registers (struct regcache *regcache, int regno) |
c906108c SS |
115 | { |
116 | struct proc *thread; | |
c5aa993b | 117 | |
da59e081 JM |
118 | /* Make sure we know about new threads. */ |
119 | inf_update_procs (current_inferior); | |
c906108c | 120 | |
39f77062 | 121 | thread = inf_tid_to_thread (current_inferior, PIDGET (inferior_ptid)); |
c5aa993b | 122 | if (!thread) |
8a3fe4f8 | 123 | error (_("Can't fetch registers from thread %d: No such thread"), |
39f77062 | 124 | PIDGET (inferior_ptid)); |
c906108c | 125 | |
65e78234 | 126 | if (regno < I386_NUM_GREGS || regno == -1) |
c906108c | 127 | { |
da59e081 | 128 | thread_state_t state; |
383d750b | 129 | |
da59e081 JM |
130 | /* This does the dirty work for us. */ |
131 | state = proc_get_state (thread, 0); | |
132 | if (!state) | |
c906108c | 133 | { |
8a3fe4f8 | 134 | warning (_("Couldn't fetch registers from %s"), |
da59e081 JM |
135 | proc_string (thread)); |
136 | return; | |
c906108c SS |
137 | } |
138 | ||
da59e081 | 139 | if (regno == -1) |
c906108c | 140 | { |
da59e081 | 141 | int i; |
383d750b | 142 | |
da59e081 | 143 | proc_debug (thread, "fetching all register"); |
383d750b | 144 | |
65e78234 | 145 | for (i = 0; i < I386_NUM_GREGS; i++) |
56be3814 | 146 | regcache_raw_supply (regcache, i, REG_ADDR (state, i)); |
da59e081 | 147 | thread->fetched_regs = ~0; |
c906108c SS |
148 | } |
149 | else | |
150 | { | |
c9f4d572 UW |
151 | proc_debug (thread, "fetching register %s", |
152 | gdbarch_register_name (current_gdbarch, regno)); | |
383d750b | 153 | |
56be3814 | 154 | regcache_raw_supply (regcache, regno, |
23a6d369 | 155 | REG_ADDR (state, regno)); |
da59e081 | 156 | thread->fetched_regs |= (1 << regno); |
c906108c SS |
157 | } |
158 | } | |
da59e081 | 159 | |
65e78234 | 160 | if (regno >= I386_NUM_GREGS || regno == -1) |
da59e081 JM |
161 | { |
162 | proc_debug (thread, "fetching floating-point registers"); | |
383d750b | 163 | |
56be3814 | 164 | fetch_fpregs (regcache, thread); |
da59e081 | 165 | } |
c906108c SS |
166 | } |
167 | \f | |
da59e081 JM |
168 | |
169 | /* Store the whole floating-point state into THREAD using information | |
170 | from the corresponding (pseudo) registers. */ | |
171 | static void | |
56be3814 | 172 | store_fpregs (const struct regcache *regcache, struct proc *thread, int regno) |
da59e081 JM |
173 | { |
174 | mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT; | |
175 | struct i386_float_state state; | |
176 | error_t err; | |
c5aa993b | 177 | |
da59e081 JM |
178 | err = thread_get_state (thread->port, i386_FLOAT_STATE, |
179 | (thread_state_t) &state, &count); | |
180 | if (err) | |
c906108c | 181 | { |
8a3fe4f8 | 182 | warning (_("Couldn't fetch floating-point state from %s"), |
da59e081 JM |
183 | proc_string (thread)); |
184 | return; | |
c906108c | 185 | } |
c5aa993b | 186 | |
383d750b | 187 | /* FIXME: kettenis/2001-07-15: Is this right? Should we somehow |
8262ee23 | 188 | take into account DEPRECATED_REGISTER_VALID like the old code did? */ |
56be3814 | 189 | i387_collect_fsave (regcache, regno, state.hw_state); |
383d750b | 190 | |
da59e081 JM |
191 | err = thread_set_state (thread->port, i386_FLOAT_STATE, |
192 | (thread_state_t) &state, i386_FLOAT_STATE_COUNT); | |
193 | if (err) | |
c906108c | 194 | { |
8a3fe4f8 | 195 | warning (_("Couldn't store floating-point state into %s"), |
da59e081 JM |
196 | proc_string (thread)); |
197 | return; | |
c906108c | 198 | } |
c906108c | 199 | } |
c5aa993b | 200 | |
da59e081 JM |
201 | /* Store at least register REGNO, or all regs if REGNO == -1. */ |
202 | void | |
56be3814 | 203 | gnu_store_registers (struct regcache *regcache, int regno) |
c5aa993b | 204 | { |
da59e081 | 205 | struct proc *thread; |
c906108c | 206 | |
da59e081 JM |
207 | /* Make sure we know about new threads. */ |
208 | inf_update_procs (current_inferior); | |
c5aa993b | 209 | |
39f77062 | 210 | thread = inf_tid_to_thread (current_inferior, PIDGET (inferior_ptid)); |
c906108c | 211 | if (!thread) |
8a3fe4f8 | 212 | error (_("Couldn't store registers into thread %d: No such thread"), |
39f77062 | 213 | PIDGET (inferior_ptid)); |
c906108c | 214 | |
65e78234 | 215 | if (regno < I386_NUM_GREGS || regno == -1) |
c906108c | 216 | { |
da59e081 JM |
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; | |
fb557744 | 221 | int trace; |
c906108c | 222 | |
da59e081 JM |
223 | if (!was_aborted && was_valid) |
224 | memcpy (&old_state, &thread->state, sizeof (old_state)); | |
c906108c | 225 | |
da59e081 JM |
226 | state = proc_get_state (thread, 1); |
227 | if (!state) | |
228 | { | |
8a3fe4f8 | 229 | warning (_("Couldn't store registers into %s"), proc_string (thread)); |
da59e081 JM |
230 | return; |
231 | } | |
c906108c | 232 | |
fb557744 MK |
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; | |
236 | ||
da59e081 JM |
237 | if (!was_aborted && was_valid) |
238 | /* See which registers have changed after aborting the thread. */ | |
239 | { | |
240 | int check_regno; | |
241 | ||
65e78234 | 242 | for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++) |
da59e081 JM |
243 | if ((thread->fetched_regs & (1 << check_regno)) |
244 | && memcpy (REG_ADDR (&old_state, check_regno), | |
245 | REG_ADDR (state, check_regno), | |
3acba339 | 246 | register_size (current_gdbarch, check_regno))) |
da59e081 JM |
247 | /* Register CHECK_REGNO has changed! Ack! */ |
248 | { | |
8a3fe4f8 | 249 | warning (_("Register %s changed after the thread was aborted"), |
c9f4d572 | 250 | gdbarch_register_name (current_gdbarch, check_regno)); |
da59e081 | 251 | if (regno >= 0 && regno != check_regno) |
383d750b | 252 | /* Update GDB's copy of the register. */ |
f6052080 | 253 | regcache_raw_supply (regcache, check_regno, |
23a6d369 | 254 | REG_ADDR (state, check_regno)); |
da59e081 | 255 | else |
8a3fe4f8 | 256 | warning (_("... also writing this register! Suspicious...")); |
da59e081 JM |
257 | } |
258 | } | |
c906108c | 259 | |
da59e081 JM |
260 | if (regno == -1) |
261 | { | |
262 | int i; | |
383d750b | 263 | |
da59e081 | 264 | proc_debug (thread, "storing all registers"); |
c5aa993b | 265 | |
65e78234 | 266 | for (i = 0; i < I386_NUM_GREGS; i++) |
f6052080 | 267 | if (regcache_valid_p (regcache, i)) |
9b8607ce | 268 | regcache_raw_collect (regcache, i, REG_ADDR (state, i)); |
da59e081 JM |
269 | } |
270 | else | |
271 | { | |
c9f4d572 UW |
272 | proc_debug (thread, "storing register %s", |
273 | gdbarch_register_name (current_gdbarch, regno)); | |
c906108c | 274 | |
f6052080 | 275 | gdb_assert (regcache_valid_p (regcache, regno)); |
ecac404d | 276 | regcache_raw_collect (regcache, regno, REG_ADDR (state, regno)); |
da59e081 | 277 | } |
fb557744 MK |
278 | |
279 | /* Restore the T bit. */ | |
280 | ((struct i386_thread_state *)state)->efl &= ~0x100; | |
281 | ((struct i386_thread_state *)state)->efl |= trace; | |
c906108c | 282 | } |
c5aa993b | 283 | |
65e78234 | 284 | if (regno >= I386_NUM_GREGS || regno == -1) |
da59e081 JM |
285 | { |
286 | proc_debug (thread, "storing floating-point registers"); | |
383d750b | 287 | |
56be3814 | 288 | store_fpregs (regcache, thread, regno); |
da59e081 | 289 | } |
c906108c | 290 | } |