Remove regcache_raw_collect
[deliverable/binutils-gdb.git] / gdb / i386-gnu-nat.c
1 /* Low level interface to i386 running the GNU Hurd.
2
3 Copyright (C) 1992-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* Mach/Hurd headers are not yet ready for C++ compilation. */
21 extern "C"
22 {
23 #include <mach.h>
24 #include <mach_error.h>
25 #include <mach/message.h>
26 #include <mach/exception.h>
27 }
28
29 #include "defs.h"
30 #include "x86-nat.h"
31 #include "inferior.h"
32 #include "floatformat.h"
33 #include "regcache.h"
34
35 #include "i386-tdep.h"
36
37 #include "gnu-nat.h"
38 #include "inf-child.h"
39 #include "i387-tdep.h"
40
41 /* Offset to the thread_state_t location where REG is stored. */
42 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
43
44 /* At REG_OFFSET[N] is the offset to the thread_state_t location where
45 the GDB register N is stored. */
46 static int reg_offset[] =
47 {
48 REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
49 REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
50 REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
51 REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
52 };
53
54 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
55
56 \f
57
58 /* The i386 GNU Hurd target. */
59
60 #ifdef i386_DEBUG_STATE
61 using gnu_base_target = x86_nat_target<gnu_nat_target>;
62 #else
63 using gnu_base_target = gnu_nat_target;
64 #endif
65
66 struct i386_gnu_nat_target final : public gnu_base_target
67 {
68 void fetch_registers (struct regcache *, int) override;
69 void store_registers (struct regcache *, int) override;
70 };
71
72 static i386_gnu_nat_target the_i386_gnu_nat_target;
73
74 /* Get the whole floating-point state of THREAD and record the values
75 of the corresponding (pseudo) registers. */
76
77 static void
78 fetch_fpregs (struct regcache *regcache, struct proc *thread)
79 {
80 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
81 struct i386_float_state state;
82 kern_return_t err;
83
84 err = thread_get_state (thread->port, i386_FLOAT_STATE,
85 (thread_state_t) &state, &count);
86 if (err)
87 {
88 warning (_("Couldn't fetch floating-point state from %s"),
89 proc_string (thread));
90 return;
91 }
92
93 if (!state.initialized)
94 {
95 /* The floating-point state isn't initialized. */
96 i387_supply_fsave (regcache, -1, NULL);
97 }
98 else
99 {
100 /* Supply the floating-point registers. */
101 i387_supply_fsave (regcache, -1, state.hw_state);
102 }
103 }
104
105 /* Fetch register REGNO, or all regs if REGNO is -1. */
106 static void
107 gnu_fetch_registers (struct target_ops *ops,
108 struct regcache *regcache, int regno)
109 {
110 struct proc *thread;
111 ptid_t ptid = regcache->ptid ();
112
113 /* Make sure we know about new threads. */
114 inf_update_procs (gnu_current_inf);
115
116 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid));
117 if (!thread)
118 error (_("Can't fetch registers from thread %s: No such thread"),
119 target_pid_to_str (ptid));
120
121 if (regno < I386_NUM_GREGS || regno == -1)
122 {
123 thread_state_t state;
124
125 /* This does the dirty work for us. */
126 state = proc_get_state (thread, 0);
127 if (!state)
128 {
129 warning (_("Couldn't fetch registers from %s"),
130 proc_string (thread));
131 return;
132 }
133
134 if (regno == -1)
135 {
136 int i;
137
138 proc_debug (thread, "fetching all register");
139
140 for (i = 0; i < I386_NUM_GREGS; i++)
141 regcache->raw_supply (i, REG_ADDR (state, i));
142 thread->fetched_regs = ~0;
143 }
144 else
145 {
146 proc_debug (thread, "fetching register %s",
147 gdbarch_register_name (regcache->arch (),
148 regno));
149
150 regcache->raw_supply (regno, REG_ADDR (state, regno));
151 thread->fetched_regs |= (1 << regno);
152 }
153 }
154
155 if (regno >= I386_NUM_GREGS || regno == -1)
156 {
157 proc_debug (thread, "fetching floating-point registers");
158
159 fetch_fpregs (regcache, thread);
160 }
161 }
162 \f
163
164 /* Store the whole floating-point state into THREAD using information
165 from the corresponding (pseudo) registers. */
166 static void
167 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
168 {
169 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
170 struct i386_float_state state;
171 kern_return_t err;
172
173 err = thread_get_state (thread->port, i386_FLOAT_STATE,
174 (thread_state_t) &state, &count);
175 if (err)
176 {
177 warning (_("Couldn't fetch floating-point state from %s"),
178 proc_string (thread));
179 return;
180 }
181
182 /* FIXME: kettenis/2001-07-15: Is this right? Should we somehow
183 take into account DEPRECATED_REGISTER_VALID like the old code did? */
184 i387_collect_fsave (regcache, regno, state.hw_state);
185
186 err = thread_set_state (thread->port, i386_FLOAT_STATE,
187 (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
188 if (err)
189 {
190 warning (_("Couldn't store floating-point state into %s"),
191 proc_string (thread));
192 return;
193 }
194 }
195
196 /* Store at least register REGNO, or all regs if REGNO == -1. */
197 static void
198 gnu_store_registers (struct target_ops *ops,
199 struct regcache *regcache, int regno)
200 {
201 struct proc *thread;
202 struct gdbarch *gdbarch = regcache->arch ();
203 ptid_t ptid = regcache->ptid ();
204
205 /* Make sure we know about new threads. */
206 inf_update_procs (gnu_current_inf);
207
208 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid));
209 if (!thread)
210 error (_("Couldn't store registers into thread %s: No such thread"),
211 target_pid_to_str (ptid));
212
213 if (regno < I386_NUM_GREGS || regno == -1)
214 {
215 thread_state_t state;
216 thread_state_data_t old_state;
217 int was_aborted = thread->aborted;
218 int was_valid = thread->state_valid;
219 int trace;
220
221 if (!was_aborted && was_valid)
222 memcpy (&old_state, &thread->state, sizeof (old_state));
223
224 state = proc_get_state (thread, 1);
225 if (!state)
226 {
227 warning (_("Couldn't store registers into %s"),
228 proc_string (thread));
229 return;
230 }
231
232 /* Save the T bit. We might try to restore the %eflags register
233 below, but changing the T bit would seriously confuse GDB. */
234 trace = ((struct i386_thread_state *)state)->efl & 0x100;
235
236 if (!was_aborted && was_valid)
237 /* See which registers have changed after aborting the thread. */
238 {
239 int check_regno;
240
241 for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
242 if ((thread->fetched_regs & (1 << check_regno))
243 && memcpy (REG_ADDR (&old_state, check_regno),
244 REG_ADDR (state, check_regno),
245 register_size (gdbarch, check_regno)))
246 /* Register CHECK_REGNO has changed! Ack! */
247 {
248 warning (_("Register %s changed after the thread was aborted"),
249 gdbarch_register_name (gdbarch, check_regno));
250 if (regno >= 0 && regno != check_regno)
251 /* Update GDB's copy of the register. */
252 regcache->raw_supply (check_regno,
253 REG_ADDR (state, check_regno));
254 else
255 warning (_("... also writing this register! "
256 "Suspicious..."));
257 }
258 }
259
260 if (regno == -1)
261 {
262 int i;
263
264 proc_debug (thread, "storing all registers");
265
266 for (i = 0; i < I386_NUM_GREGS; i++)
267 if (REG_VALID == regcache->get_register_status (i))
268 regcache->raw_collect (i, REG_ADDR (state, i));
269 }
270 else
271 {
272 proc_debug (thread, "storing register %s",
273 gdbarch_register_name (gdbarch, regno));
274
275 gdb_assert (REG_VALID == regcache->get_register_status (regno));
276 regcache->raw_collect (regno, REG_ADDR (state, regno));
277 }
278
279 /* Restore the T bit. */
280 ((struct i386_thread_state *)state)->efl &= ~0x100;
281 ((struct i386_thread_state *)state)->efl |= trace;
282 }
283
284 if (regno >= I386_NUM_GREGS || regno == -1)
285 {
286 proc_debug (thread, "storing floating-point registers");
287
288 store_fpregs (regcache, thread, regno);
289 }
290 }
291
292 \f
293 /* Support for debug registers. */
294
295 #ifdef i386_DEBUG_STATE
296 /* Get debug registers for thread THREAD. */
297
298 static void
299 i386_gnu_dr_get (struct i386_debug_state *regs, struct proc *thread)
300 {
301 mach_msg_type_number_t count = i386_DEBUG_STATE_COUNT;
302 kern_return_t err;
303
304 err = thread_get_state (thread->port, i386_DEBUG_STATE,
305 (thread_state_t) regs, &count);
306 if (err != 0 || count != i386_DEBUG_STATE_COUNT)
307 warning (_("Couldn't fetch debug state from %s"),
308 proc_string (thread));
309 }
310
311 /* Set debug registers for thread THREAD. */
312
313 static void
314 i386_gnu_dr_set (const struct i386_debug_state *regs, struct proc *thread)
315 {
316 kern_return_t err;
317
318 err = thread_set_state (thread->port, i386_DEBUG_STATE,
319 (thread_state_t) regs, i386_DEBUG_STATE_COUNT);
320 if (err != 0)
321 warning (_("Couldn't store debug state into %s"),
322 proc_string (thread));
323 }
324
325 /* Set DR_CONTROL in THREAD. */
326
327 static void
328 i386_gnu_dr_set_control_one (struct proc *thread, void *arg)
329 {
330 unsigned long *control = (unsigned long *) arg;
331 struct i386_debug_state regs;
332
333 i386_gnu_dr_get (&regs, thread);
334 regs.dr[DR_CONTROL] = *control;
335 i386_gnu_dr_set (&regs, thread);
336 }
337
338 /* Set DR_CONTROL to CONTROL in all threads. */
339
340 static void
341 i386_gnu_dr_set_control (unsigned long control)
342 {
343 inf_update_procs (gnu_current_inf);
344 inf_threads (gnu_current_inf, i386_gnu_dr_set_control_one, &control);
345 }
346
347 /* Parameters to set a debugging address. */
348
349 struct reg_addr
350 {
351 int regnum; /* Register number (zero based). */
352 CORE_ADDR addr; /* Address. */
353 };
354
355 /* Set address REGNUM (zero based) to ADDR in THREAD. */
356
357 static void
358 i386_gnu_dr_set_addr_one (struct proc *thread, void *arg)
359 {
360 struct reg_addr *reg_addr = (struct reg_addr *) arg;
361 struct i386_debug_state regs;
362
363 i386_gnu_dr_get (&regs, thread);
364 regs.dr[reg_addr->regnum] = reg_addr->addr;
365 i386_gnu_dr_set (&regs, thread);
366 }
367
368 /* Set address REGNUM (zero based) to ADDR in all threads. */
369
370 static void
371 i386_gnu_dr_set_addr (int regnum, CORE_ADDR addr)
372 {
373 struct reg_addr reg_addr;
374
375 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
376
377 reg_addr.regnum = regnum;
378 reg_addr.addr = addr;
379
380 inf_update_procs (gnu_current_inf);
381 inf_threads (gnu_current_inf, i386_gnu_dr_set_addr_one, &reg_addr);
382 }
383
384 /* Get debug register REGNUM value from only the one LWP of PTID. */
385
386 static unsigned long
387 i386_gnu_dr_get_reg (ptid_t ptid, int regnum)
388 {
389 struct i386_debug_state regs;
390 struct proc *thread;
391
392 /* Make sure we know about new threads. */
393 inf_update_procs (gnu_current_inf);
394
395 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid));
396 i386_gnu_dr_get (&regs, thread);
397
398 return regs.dr[regnum];
399 }
400
401 /* Return the inferior's debug register REGNUM. */
402
403 static CORE_ADDR
404 i386_gnu_dr_get_addr (int regnum)
405 {
406 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
407
408 return i386_gnu_dr_get_reg (inferior_ptid, regnum);
409 }
410
411 /* Get DR_STATUS from only the one thread of INFERIOR_PTID. */
412
413 static unsigned long
414 i386_gnu_dr_get_status (void)
415 {
416 return i386_gnu_dr_get_reg (inferior_ptid, DR_STATUS);
417 }
418
419 /* Return the inferior's DR7 debug control register. */
420
421 static unsigned long
422 i386_gnu_dr_get_control (void)
423 {
424 return i386_gnu_dr_get_reg (inferior_ptid, DR_CONTROL);
425 }
426 #endif /* i386_DEBUG_STATE */
427
428 void
429 _initialize_i386gnu_nat (void)
430 {
431 #ifdef i386_DEBUG_STATE
432 x86_dr_low.set_control = i386_gnu_dr_set_control;
433 gdb_assert (DR_FIRSTADDR == 0 && DR_LASTADDR < i386_DEBUG_STATE_COUNT);
434 x86_dr_low.set_addr = i386_gnu_dr_set_addr;
435 x86_dr_low.get_addr = i386_gnu_dr_get_addr;
436 x86_dr_low.get_status = i386_gnu_dr_get_status;
437 x86_dr_low.get_control = i386_gnu_dr_get_control;
438 x86_set_debug_register_length (4);
439 #endif /* i386_DEBUG_STATE */
440
441 /* Register the target. */
442 add_inf_child_target (&the_i386_gnu_nat_target);
443 }
This page took 0.055718 seconds and 4 git commands to generate.