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 (int regnum, CORE_ADDR addr)
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 (unsigned long control)
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 /* Low-level function vector. */
99 struct i386_dr_low_type i386_dr_low =
101 i386_dr_low_set_control,
102 i386_dr_low_set_addr,
103 i386_dr_low_get_addr,
104 i386_dr_low_get_status,
105 i386_dr_low_get_control,
109 /* Breakpoint/watchpoint support. */
112 i386_supports_z_point_type (char z_type)
116 case Z_PACKET_WRITE_WP:
117 case Z_PACKET_ACCESS_WP:
125 i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
126 int size, struct raw_breakpoint *bp)
130 case raw_bkpt_type_write_wp:
131 case raw_bkpt_type_access_wp:
133 enum target_hw_bp_type hw_type
134 = raw_bkpt_type_to_target_hw_bp_type (type);
136 return i386_dr_insert_watchpoint (&debug_reg_state,
137 hw_type, addr, size);
146 i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
147 int size, struct raw_breakpoint *bp)
151 case raw_bkpt_type_write_wp:
152 case raw_bkpt_type_access_wp:
154 enum target_hw_bp_type hw_type
155 = raw_bkpt_type_to_target_hw_bp_type (type);
157 return i386_dr_remove_watchpoint (&debug_reg_state,
158 hw_type, addr, size);
167 i386_stopped_by_watchpoint (void)
169 return i386_dr_stopped_by_watchpoint (&debug_reg_state);
173 i386_stopped_data_address (void)
176 if (i386_dr_stopped_data_address (&debug_reg_state, &addr))
182 i386_initial_stuff (void)
184 i386_low_init_dregs (&debug_reg_state);
185 debug_registers_changed = 0;
186 debug_registers_used = 0;
190 i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
192 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
193 the system doesn't support extended registers. */
194 static DWORD extended_registers = CONTEXT_EXTENDED_REGISTERS;
197 th->context.ContextFlags = (CONTEXT_FULL
198 | CONTEXT_FLOATING_POINT
199 | CONTEXT_DEBUG_REGISTERS
200 | extended_registers);
202 if (!GetThreadContext (th->h, &th->context))
204 DWORD e = GetLastError ();
206 if (extended_registers && e == ERROR_INVALID_PARAMETER)
208 extended_registers = 0;
212 error ("GetThreadContext failure %ld\n", (long) e);
215 debug_registers_changed = 0;
217 if (th->tid == current_event->dwThreadId)
219 /* Copy dr values from the current thread. */
220 struct i386_debug_reg_state *dr = &debug_reg_state;
221 dr->dr_mirror[0] = th->context.Dr0;
222 dr->dr_mirror[1] = th->context.Dr1;
223 dr->dr_mirror[2] = th->context.Dr2;
224 dr->dr_mirror[3] = th->context.Dr3;
225 dr->dr_status_mirror = th->context.Dr6;
226 dr->dr_control_mirror = th->context.Dr7;
231 i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
233 if (debug_registers_changed)
235 struct i386_debug_reg_state *dr = &debug_reg_state;
236 th->context.Dr0 = dr->dr_mirror[0];
237 th->context.Dr1 = dr->dr_mirror[1];
238 th->context.Dr2 = dr->dr_mirror[2];
239 th->context.Dr3 = dr->dr_mirror[3];
240 /* th->context.Dr6 = dr->dr_status_mirror;
241 FIXME: should we set dr6 also ?? */
242 th->context.Dr7 = dr->dr_control_mirror;
245 SetThreadContext (th->h, &th->context);
249 i386_thread_added (win32_thread_info *th)
251 /* Set the debug registers for the new thread if they are used. */
252 if (debug_registers_used)
254 struct i386_debug_reg_state *dr = &debug_reg_state;
255 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
256 GetThreadContext (th->h, &th->context);
258 th->context.Dr0 = dr->dr_mirror[0];
259 th->context.Dr1 = dr->dr_mirror[1];
260 th->context.Dr2 = dr->dr_mirror[2];
261 th->context.Dr3 = dr->dr_mirror[3];
262 /* th->context.Dr6 = dr->dr_status_mirror;
263 FIXME: should we set dr6 also ?? */
264 th->context.Dr7 = dr->dr_control_mirror;
266 SetThreadContext (th->h, &th->context);
267 th->context.ContextFlags = 0;
272 i386_single_step (win32_thread_info *th)
274 th->context.EFlags |= FLAG_TRACE_BIT;
279 /* An array of offset mappings into a Win32 Context structure.
280 This is a one-to-one mapping which is indexed by gdb's register
281 numbers. It retrieves an offset into the context structure where
282 the 4 byte register is located.
283 An offset value of -1 indicates that Win32 does not provide this
284 register in it's CONTEXT structure. In this case regptr will return
285 a pointer into a dummy register. */
286 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
287 static const int mappings[] = {
288 context_offset (Eax),
289 context_offset (Ecx),
290 context_offset (Edx),
291 context_offset (Ebx),
292 context_offset (Esp),
293 context_offset (Ebp),
294 context_offset (Esi),
295 context_offset (Edi),
296 context_offset (Eip),
297 context_offset (EFlags),
298 context_offset (SegCs),
299 context_offset (SegSs),
300 context_offset (SegDs),
301 context_offset (SegEs),
302 context_offset (SegFs),
303 context_offset (SegGs),
304 context_offset (FloatSave.RegisterArea[0 * 10]),
305 context_offset (FloatSave.RegisterArea[1 * 10]),
306 context_offset (FloatSave.RegisterArea[2 * 10]),
307 context_offset (FloatSave.RegisterArea[3 * 10]),
308 context_offset (FloatSave.RegisterArea[4 * 10]),
309 context_offset (FloatSave.RegisterArea[5 * 10]),
310 context_offset (FloatSave.RegisterArea[6 * 10]),
311 context_offset (FloatSave.RegisterArea[7 * 10]),
312 context_offset (FloatSave.ControlWord),
313 context_offset (FloatSave.StatusWord),
314 context_offset (FloatSave.TagWord),
315 context_offset (FloatSave.ErrorSelector),
316 context_offset (FloatSave.ErrorOffset),
317 context_offset (FloatSave.DataSelector),
318 context_offset (FloatSave.DataOffset),
319 context_offset (FloatSave.ErrorSelector),
321 context_offset (ExtendedRegisters[10 * 16]),
322 context_offset (ExtendedRegisters[11 * 16]),
323 context_offset (ExtendedRegisters[12 * 16]),
324 context_offset (ExtendedRegisters[13 * 16]),
325 context_offset (ExtendedRegisters[14 * 16]),
326 context_offset (ExtendedRegisters[15 * 16]),
327 context_offset (ExtendedRegisters[16 * 16]),
328 context_offset (ExtendedRegisters[17 * 16]),
330 context_offset (ExtendedRegisters[24])
332 #undef context_offset
334 #else /* __x86_64__ */
336 #define context_offset(x) (offsetof (CONTEXT, x))
337 static const int mappings[] =
339 context_offset (Rax),
340 context_offset (Rbx),
341 context_offset (Rcx),
342 context_offset (Rdx),
343 context_offset (Rsi),
344 context_offset (Rdi),
345 context_offset (Rbp),
346 context_offset (Rsp),
349 context_offset (R10),
350 context_offset (R11),
351 context_offset (R12),
352 context_offset (R13),
353 context_offset (R14),
354 context_offset (R15),
355 context_offset (Rip),
356 context_offset (EFlags),
357 context_offset (SegCs),
358 context_offset (SegSs),
359 context_offset (SegDs),
360 context_offset (SegEs),
361 context_offset (SegFs),
362 context_offset (SegGs),
363 context_offset (FloatSave.FloatRegisters[0]),
364 context_offset (FloatSave.FloatRegisters[1]),
365 context_offset (FloatSave.FloatRegisters[2]),
366 context_offset (FloatSave.FloatRegisters[3]),
367 context_offset (FloatSave.FloatRegisters[4]),
368 context_offset (FloatSave.FloatRegisters[5]),
369 context_offset (FloatSave.FloatRegisters[6]),
370 context_offset (FloatSave.FloatRegisters[7]),
371 context_offset (FloatSave.ControlWord),
372 context_offset (FloatSave.StatusWord),
373 context_offset (FloatSave.TagWord),
374 context_offset (FloatSave.ErrorSelector),
375 context_offset (FloatSave.ErrorOffset),
376 context_offset (FloatSave.DataSelector),
377 context_offset (FloatSave.DataOffset),
378 context_offset (FloatSave.ErrorSelector)
380 context_offset (Xmm0),
381 context_offset (Xmm1),
382 context_offset (Xmm2),
383 context_offset (Xmm3),
384 context_offset (Xmm4),
385 context_offset (Xmm5),
386 context_offset (Xmm6),
387 context_offset (Xmm7),
388 context_offset (Xmm8),
389 context_offset (Xmm9),
390 context_offset (Xmm10),
391 context_offset (Xmm11),
392 context_offset (Xmm12),
393 context_offset (Xmm13),
394 context_offset (Xmm14),
395 context_offset (Xmm15),
397 context_offset (FloatSave.MxCsr)
399 #undef context_offset
401 #endif /* __x86_64__ */
403 /* Fetch register from gdbserver regcache data. */
405 i386_fetch_inferior_register (struct regcache *regcache,
406 win32_thread_info *th, int r)
408 char *context_offset = (char *) &th->context + mappings[r];
413 l = *((long *) context_offset) & 0xffff;
414 supply_register (regcache, r, (char *) &l);
416 else if (r == FOP_REGNUM)
418 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
419 supply_register (regcache, r, (char *) &l);
422 supply_register (regcache, r, context_offset);
425 /* Store a new register value into the thread context of TH. */
427 i386_store_inferior_register (struct regcache *regcache,
428 win32_thread_info *th, int r)
430 char *context_offset = (char *) &th->context + mappings[r];
431 collect_register (regcache, r, context_offset);
434 static const unsigned char i386_win32_breakpoint = 0xcc;
435 #define i386_win32_breakpoint_len 1
438 i386_arch_setup (void)
441 init_registers_amd64 ();
442 win32_tdesc = tdesc_amd64;
444 init_registers_i386 ();
445 win32_tdesc = tdesc_i386;
449 struct win32_target_ops the_low_target = {
451 sizeof (mappings) / sizeof (mappings[0]),
453 i386_get_thread_context,
454 i386_set_thread_context,
456 i386_fetch_inferior_register,
457 i386_store_inferior_register,
459 &i386_win32_breakpoint,
460 i386_win32_breakpoint_len,
461 i386_supports_z_point_type,
464 i386_stopped_by_watchpoint,
465 i386_stopped_data_address