1 /* Copyright (C) 2007-2014 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "win32-low.h"
22 #ifndef CONTEXT_EXTENDED_REGISTERS
23 #define CONTEXT_EXTENDED_REGISTERS 0
29 #define FLAG_TRACE_BIT 0x100
32 /* Defined in auto-generated file reg-amd64.c. */
33 void init_registers_amd64 (void);
34 extern const struct target_desc *tdesc_amd64;
36 /* Defined in auto-generated file reg-i386.c. */
37 void init_registers_i386 (void);
38 extern const struct target_desc *tdesc_i386;
41 static struct i386_debug_reg_state debug_reg_state;
43 static int debug_registers_changed = 0;
44 static int debug_registers_used = 0;
46 /* Update the inferior's debug register REGNUM from STATE. */
49 i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
51 if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
52 fatal ("Invalid debug register %d", regnum);
54 /* debug_reg_state.dr_mirror is already set.
55 Just notify i386_set_thread_context, i386_thread_added
56 that the registers need to be updated. */
57 debug_registers_changed = 1;
58 debug_registers_used = 1;
62 i386_dr_low_get_addr (int regnum)
64 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
66 return debug_reg_state.dr_mirror[regnum];
69 /* Update the inferior's DR7 debug control register from STATE. */
72 i386_dr_low_set_control (const struct i386_debug_reg_state *state)
74 /* debug_reg_state.dr_control_mirror is already set.
75 Just notify i386_set_thread_context, i386_thread_added
76 that the registers need to be updated. */
77 debug_registers_changed = 1;
78 debug_registers_used = 1;
82 i386_dr_low_get_control (void)
84 return debug_reg_state.dr_control_mirror;
87 /* Get the value of the DR6 debug status register from the inferior
88 and record it in STATE. */
91 i386_dr_low_get_status (void)
93 /* We don't need to do anything here, the last call to thread_rec for
94 current_event.dwThreadId id has already set it. */
95 return debug_reg_state.dr_status_mirror;
98 /* Watchpoint support. */
101 i386_insert_point (char type, CORE_ADDR addr, int len)
109 enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
111 return i386_low_insert_watchpoint (&debug_reg_state,
121 i386_remove_point (char type, CORE_ADDR addr, int len)
129 enum target_hw_bp_type hw_type = Z_packet_to_target_hw_bp_type (type);
131 return i386_low_remove_watchpoint (&debug_reg_state,
141 i386_stopped_by_watchpoint (void)
143 return i386_low_stopped_by_watchpoint (&debug_reg_state);
147 i386_stopped_data_address (void)
150 if (i386_low_stopped_data_address (&debug_reg_state, &addr))
156 i386_initial_stuff (void)
158 i386_low_init_dregs (&debug_reg_state);
159 debug_registers_changed = 0;
160 debug_registers_used = 0;
164 i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
166 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
167 the system doesn't support extended registers. */
168 static DWORD extended_registers = CONTEXT_EXTENDED_REGISTERS;
171 th->context.ContextFlags = (CONTEXT_FULL
172 | CONTEXT_FLOATING_POINT
173 | CONTEXT_DEBUG_REGISTERS
174 | extended_registers);
176 if (!GetThreadContext (th->h, &th->context))
178 DWORD e = GetLastError ();
180 if (extended_registers && e == ERROR_INVALID_PARAMETER)
182 extended_registers = 0;
186 error ("GetThreadContext failure %ld\n", (long) e);
189 debug_registers_changed = 0;
191 if (th->tid == current_event->dwThreadId)
193 /* Copy dr values from the current thread. */
194 struct i386_debug_reg_state *dr = &debug_reg_state;
195 dr->dr_mirror[0] = th->context.Dr0;
196 dr->dr_mirror[1] = th->context.Dr1;
197 dr->dr_mirror[2] = th->context.Dr2;
198 dr->dr_mirror[3] = th->context.Dr3;
199 dr->dr_status_mirror = th->context.Dr6;
200 dr->dr_control_mirror = th->context.Dr7;
205 i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
207 if (debug_registers_changed)
209 struct i386_debug_reg_state *dr = &debug_reg_state;
210 th->context.Dr0 = dr->dr_mirror[0];
211 th->context.Dr1 = dr->dr_mirror[1];
212 th->context.Dr2 = dr->dr_mirror[2];
213 th->context.Dr3 = dr->dr_mirror[3];
214 /* th->context.Dr6 = dr->dr_status_mirror;
215 FIXME: should we set dr6 also ?? */
216 th->context.Dr7 = dr->dr_control_mirror;
219 SetThreadContext (th->h, &th->context);
223 i386_thread_added (win32_thread_info *th)
225 /* Set the debug registers for the new thread if they are used. */
226 if (debug_registers_used)
228 struct i386_debug_reg_state *dr = &debug_reg_state;
229 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
230 GetThreadContext (th->h, &th->context);
232 th->context.Dr0 = dr->dr_mirror[0];
233 th->context.Dr1 = dr->dr_mirror[1];
234 th->context.Dr2 = dr->dr_mirror[2];
235 th->context.Dr3 = dr->dr_mirror[3];
236 /* th->context.Dr6 = dr->dr_status_mirror;
237 FIXME: should we set dr6 also ?? */
238 th->context.Dr7 = dr->dr_control_mirror;
240 SetThreadContext (th->h, &th->context);
241 th->context.ContextFlags = 0;
246 i386_single_step (win32_thread_info *th)
248 th->context.EFlags |= FLAG_TRACE_BIT;
253 /* An array of offset mappings into a Win32 Context structure.
254 This is a one-to-one mapping which is indexed by gdb's register
255 numbers. It retrieves an offset into the context structure where
256 the 4 byte register is located.
257 An offset value of -1 indicates that Win32 does not provide this
258 register in it's CONTEXT structure. In this case regptr will return
259 a pointer into a dummy register. */
260 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
261 static const int mappings[] = {
262 context_offset (Eax),
263 context_offset (Ecx),
264 context_offset (Edx),
265 context_offset (Ebx),
266 context_offset (Esp),
267 context_offset (Ebp),
268 context_offset (Esi),
269 context_offset (Edi),
270 context_offset (Eip),
271 context_offset (EFlags),
272 context_offset (SegCs),
273 context_offset (SegSs),
274 context_offset (SegDs),
275 context_offset (SegEs),
276 context_offset (SegFs),
277 context_offset (SegGs),
278 context_offset (FloatSave.RegisterArea[0 * 10]),
279 context_offset (FloatSave.RegisterArea[1 * 10]),
280 context_offset (FloatSave.RegisterArea[2 * 10]),
281 context_offset (FloatSave.RegisterArea[3 * 10]),
282 context_offset (FloatSave.RegisterArea[4 * 10]),
283 context_offset (FloatSave.RegisterArea[5 * 10]),
284 context_offset (FloatSave.RegisterArea[6 * 10]),
285 context_offset (FloatSave.RegisterArea[7 * 10]),
286 context_offset (FloatSave.ControlWord),
287 context_offset (FloatSave.StatusWord),
288 context_offset (FloatSave.TagWord),
289 context_offset (FloatSave.ErrorSelector),
290 context_offset (FloatSave.ErrorOffset),
291 context_offset (FloatSave.DataSelector),
292 context_offset (FloatSave.DataOffset),
293 context_offset (FloatSave.ErrorSelector),
295 context_offset (ExtendedRegisters[10 * 16]),
296 context_offset (ExtendedRegisters[11 * 16]),
297 context_offset (ExtendedRegisters[12 * 16]),
298 context_offset (ExtendedRegisters[13 * 16]),
299 context_offset (ExtendedRegisters[14 * 16]),
300 context_offset (ExtendedRegisters[15 * 16]),
301 context_offset (ExtendedRegisters[16 * 16]),
302 context_offset (ExtendedRegisters[17 * 16]),
304 context_offset (ExtendedRegisters[24])
306 #undef context_offset
308 #else /* __x86_64__ */
310 #define context_offset(x) (offsetof (CONTEXT, x))
311 static const int mappings[] =
313 context_offset (Rax),
314 context_offset (Rbx),
315 context_offset (Rcx),
316 context_offset (Rdx),
317 context_offset (Rsi),
318 context_offset (Rdi),
319 context_offset (Rbp),
320 context_offset (Rsp),
323 context_offset (R10),
324 context_offset (R11),
325 context_offset (R12),
326 context_offset (R13),
327 context_offset (R14),
328 context_offset (R15),
329 context_offset (Rip),
330 context_offset (EFlags),
331 context_offset (SegCs),
332 context_offset (SegSs),
333 context_offset (SegDs),
334 context_offset (SegEs),
335 context_offset (SegFs),
336 context_offset (SegGs),
337 context_offset (FloatSave.FloatRegisters[0]),
338 context_offset (FloatSave.FloatRegisters[1]),
339 context_offset (FloatSave.FloatRegisters[2]),
340 context_offset (FloatSave.FloatRegisters[3]),
341 context_offset (FloatSave.FloatRegisters[4]),
342 context_offset (FloatSave.FloatRegisters[5]),
343 context_offset (FloatSave.FloatRegisters[6]),
344 context_offset (FloatSave.FloatRegisters[7]),
345 context_offset (FloatSave.ControlWord),
346 context_offset (FloatSave.StatusWord),
347 context_offset (FloatSave.TagWord),
348 context_offset (FloatSave.ErrorSelector),
349 context_offset (FloatSave.ErrorOffset),
350 context_offset (FloatSave.DataSelector),
351 context_offset (FloatSave.DataOffset),
352 context_offset (FloatSave.ErrorSelector)
354 context_offset (Xmm0),
355 context_offset (Xmm1),
356 context_offset (Xmm2),
357 context_offset (Xmm3),
358 context_offset (Xmm4),
359 context_offset (Xmm5),
360 context_offset (Xmm6),
361 context_offset (Xmm7),
362 context_offset (Xmm8),
363 context_offset (Xmm9),
364 context_offset (Xmm10),
365 context_offset (Xmm11),
366 context_offset (Xmm12),
367 context_offset (Xmm13),
368 context_offset (Xmm14),
369 context_offset (Xmm15),
371 context_offset (FloatSave.MxCsr)
373 #undef context_offset
375 #endif /* __x86_64__ */
377 /* Fetch register from gdbserver regcache data. */
379 i386_fetch_inferior_register (struct regcache *regcache,
380 win32_thread_info *th, int r)
382 char *context_offset = (char *) &th->context + mappings[r];
387 l = *((long *) context_offset) & 0xffff;
388 supply_register (regcache, r, (char *) &l);
390 else if (r == FOP_REGNUM)
392 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
393 supply_register (regcache, r, (char *) &l);
396 supply_register (regcache, r, context_offset);
399 /* Store a new register value into the thread context of TH. */
401 i386_store_inferior_register (struct regcache *regcache,
402 win32_thread_info *th, int r)
404 char *context_offset = (char *) &th->context + mappings[r];
405 collect_register (regcache, r, context_offset);
408 static const unsigned char i386_win32_breakpoint = 0xcc;
409 #define i386_win32_breakpoint_len 1
412 i386_arch_setup (void)
415 init_registers_amd64 ();
416 win32_tdesc = tdesc_amd64;
418 init_registers_i386 ();
419 win32_tdesc = tdesc_i386;
423 struct win32_target_ops the_low_target = {
425 sizeof (mappings) / sizeof (mappings[0]),
427 i386_get_thread_context,
428 i386_set_thread_context,
430 i386_fetch_inferior_register,
431 i386_store_inferior_register,
433 &i386_win32_breakpoint,
434 i386_win32_breakpoint_len,
437 i386_stopped_by_watchpoint,
438 i386_stopped_data_address