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 x86_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 x86_dr_low_set_addr (int regnum, CORE_ADDR addr)
51 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
53 /* debug_reg_state.dr_mirror is already set.
54 Just notify i386_set_thread_context, i386_thread_added
55 that the registers need to be updated. */
56 debug_registers_changed = 1;
57 debug_registers_used = 1;
61 x86_dr_low_get_addr (int regnum)
63 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
65 return debug_reg_state.dr_mirror[regnum];
68 /* Update the inferior's DR7 debug control register from STATE. */
71 x86_dr_low_set_control (unsigned long control)
73 /* debug_reg_state.dr_control_mirror is already set.
74 Just notify i386_set_thread_context, i386_thread_added
75 that the registers need to be updated. */
76 debug_registers_changed = 1;
77 debug_registers_used = 1;
81 x86_dr_low_get_control (void)
83 return debug_reg_state.dr_control_mirror;
86 /* Get the value of the DR6 debug status register from the inferior
87 and record it in STATE. */
90 x86_dr_low_get_status (void)
92 /* We don't need to do anything here, the last call to thread_rec for
93 current_event.dwThreadId id has already set it. */
94 return debug_reg_state.dr_status_mirror;
97 /* Low-level function vector. */
98 struct x86_dr_low_type x86_dr_low =
100 x86_dr_low_set_control,
103 x86_dr_low_get_status,
104 x86_dr_low_get_control,
108 /* Breakpoint/watchpoint support. */
111 i386_supports_z_point_type (char z_type)
115 case Z_PACKET_WRITE_WP:
116 case Z_PACKET_ACCESS_WP:
124 i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
125 int size, struct raw_breakpoint *bp)
129 case raw_bkpt_type_write_wp:
130 case raw_bkpt_type_access_wp:
132 enum target_hw_bp_type hw_type
133 = raw_bkpt_type_to_target_hw_bp_type (type);
135 return x86_dr_insert_watchpoint (&debug_reg_state,
136 hw_type, addr, size);
145 i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
146 int size, struct raw_breakpoint *bp)
150 case raw_bkpt_type_write_wp:
151 case raw_bkpt_type_access_wp:
153 enum target_hw_bp_type hw_type
154 = raw_bkpt_type_to_target_hw_bp_type (type);
156 return x86_dr_remove_watchpoint (&debug_reg_state,
157 hw_type, addr, size);
166 x86_stopped_by_watchpoint (void)
168 return x86_dr_stopped_by_watchpoint (&debug_reg_state);
172 x86_stopped_data_address (void)
175 if (x86_dr_stopped_data_address (&debug_reg_state, &addr))
181 i386_initial_stuff (void)
183 x86_low_init_dregs (&debug_reg_state);
184 debug_registers_changed = 0;
185 debug_registers_used = 0;
189 i386_get_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
191 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
192 the system doesn't support extended registers. */
193 static DWORD extended_registers = CONTEXT_EXTENDED_REGISTERS;
196 th->context.ContextFlags = (CONTEXT_FULL
197 | CONTEXT_FLOATING_POINT
198 | CONTEXT_DEBUG_REGISTERS
199 | extended_registers);
201 if (!GetThreadContext (th->h, &th->context))
203 DWORD e = GetLastError ();
205 if (extended_registers && e == ERROR_INVALID_PARAMETER)
207 extended_registers = 0;
211 error ("GetThreadContext failure %ld\n", (long) e);
214 debug_registers_changed = 0;
216 if (th->tid == current_event->dwThreadId)
218 /* Copy dr values from the current thread. */
219 struct x86_debug_reg_state *dr = &debug_reg_state;
220 dr->dr_mirror[0] = th->context.Dr0;
221 dr->dr_mirror[1] = th->context.Dr1;
222 dr->dr_mirror[2] = th->context.Dr2;
223 dr->dr_mirror[3] = th->context.Dr3;
224 dr->dr_status_mirror = th->context.Dr6;
225 dr->dr_control_mirror = th->context.Dr7;
230 i386_set_thread_context (win32_thread_info *th, DEBUG_EVENT* current_event)
232 if (debug_registers_changed)
234 struct x86_debug_reg_state *dr = &debug_reg_state;
235 th->context.Dr0 = dr->dr_mirror[0];
236 th->context.Dr1 = dr->dr_mirror[1];
237 th->context.Dr2 = dr->dr_mirror[2];
238 th->context.Dr3 = dr->dr_mirror[3];
239 /* th->context.Dr6 = dr->dr_status_mirror;
240 FIXME: should we set dr6 also ?? */
241 th->context.Dr7 = dr->dr_control_mirror;
244 SetThreadContext (th->h, &th->context);
248 i386_thread_added (win32_thread_info *th)
250 /* Set the debug registers for the new thread if they are used. */
251 if (debug_registers_used)
253 struct x86_debug_reg_state *dr = &debug_reg_state;
254 th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
255 GetThreadContext (th->h, &th->context);
257 th->context.Dr0 = dr->dr_mirror[0];
258 th->context.Dr1 = dr->dr_mirror[1];
259 th->context.Dr2 = dr->dr_mirror[2];
260 th->context.Dr3 = dr->dr_mirror[3];
261 /* th->context.Dr6 = dr->dr_status_mirror;
262 FIXME: should we set dr6 also ?? */
263 th->context.Dr7 = dr->dr_control_mirror;
265 SetThreadContext (th->h, &th->context);
266 th->context.ContextFlags = 0;
271 i386_single_step (win32_thread_info *th)
273 th->context.EFlags |= FLAG_TRACE_BIT;
278 /* An array of offset mappings into a Win32 Context structure.
279 This is a one-to-one mapping which is indexed by gdb's register
280 numbers. It retrieves an offset into the context structure where
281 the 4 byte register is located.
282 An offset value of -1 indicates that Win32 does not provide this
283 register in it's CONTEXT structure. In this case regptr will return
284 a pointer into a dummy register. */
285 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
286 static const int mappings[] = {
287 context_offset (Eax),
288 context_offset (Ecx),
289 context_offset (Edx),
290 context_offset (Ebx),
291 context_offset (Esp),
292 context_offset (Ebp),
293 context_offset (Esi),
294 context_offset (Edi),
295 context_offset (Eip),
296 context_offset (EFlags),
297 context_offset (SegCs),
298 context_offset (SegSs),
299 context_offset (SegDs),
300 context_offset (SegEs),
301 context_offset (SegFs),
302 context_offset (SegGs),
303 context_offset (FloatSave.RegisterArea[0 * 10]),
304 context_offset (FloatSave.RegisterArea[1 * 10]),
305 context_offset (FloatSave.RegisterArea[2 * 10]),
306 context_offset (FloatSave.RegisterArea[3 * 10]),
307 context_offset (FloatSave.RegisterArea[4 * 10]),
308 context_offset (FloatSave.RegisterArea[5 * 10]),
309 context_offset (FloatSave.RegisterArea[6 * 10]),
310 context_offset (FloatSave.RegisterArea[7 * 10]),
311 context_offset (FloatSave.ControlWord),
312 context_offset (FloatSave.StatusWord),
313 context_offset (FloatSave.TagWord),
314 context_offset (FloatSave.ErrorSelector),
315 context_offset (FloatSave.ErrorOffset),
316 context_offset (FloatSave.DataSelector),
317 context_offset (FloatSave.DataOffset),
318 context_offset (FloatSave.ErrorSelector),
320 context_offset (ExtendedRegisters[10 * 16]),
321 context_offset (ExtendedRegisters[11 * 16]),
322 context_offset (ExtendedRegisters[12 * 16]),
323 context_offset (ExtendedRegisters[13 * 16]),
324 context_offset (ExtendedRegisters[14 * 16]),
325 context_offset (ExtendedRegisters[15 * 16]),
326 context_offset (ExtendedRegisters[16 * 16]),
327 context_offset (ExtendedRegisters[17 * 16]),
329 context_offset (ExtendedRegisters[24])
331 #undef context_offset
333 #else /* __x86_64__ */
335 #define context_offset(x) (offsetof (CONTEXT, x))
336 static const int mappings[] =
338 context_offset (Rax),
339 context_offset (Rbx),
340 context_offset (Rcx),
341 context_offset (Rdx),
342 context_offset (Rsi),
343 context_offset (Rdi),
344 context_offset (Rbp),
345 context_offset (Rsp),
348 context_offset (R10),
349 context_offset (R11),
350 context_offset (R12),
351 context_offset (R13),
352 context_offset (R14),
353 context_offset (R15),
354 context_offset (Rip),
355 context_offset (EFlags),
356 context_offset (SegCs),
357 context_offset (SegSs),
358 context_offset (SegDs),
359 context_offset (SegEs),
360 context_offset (SegFs),
361 context_offset (SegGs),
362 context_offset (FloatSave.FloatRegisters[0]),
363 context_offset (FloatSave.FloatRegisters[1]),
364 context_offset (FloatSave.FloatRegisters[2]),
365 context_offset (FloatSave.FloatRegisters[3]),
366 context_offset (FloatSave.FloatRegisters[4]),
367 context_offset (FloatSave.FloatRegisters[5]),
368 context_offset (FloatSave.FloatRegisters[6]),
369 context_offset (FloatSave.FloatRegisters[7]),
370 context_offset (FloatSave.ControlWord),
371 context_offset (FloatSave.StatusWord),
372 context_offset (FloatSave.TagWord),
373 context_offset (FloatSave.ErrorSelector),
374 context_offset (FloatSave.ErrorOffset),
375 context_offset (FloatSave.DataSelector),
376 context_offset (FloatSave.DataOffset),
377 context_offset (FloatSave.ErrorSelector)
379 context_offset (Xmm0),
380 context_offset (Xmm1),
381 context_offset (Xmm2),
382 context_offset (Xmm3),
383 context_offset (Xmm4),
384 context_offset (Xmm5),
385 context_offset (Xmm6),
386 context_offset (Xmm7),
387 context_offset (Xmm8),
388 context_offset (Xmm9),
389 context_offset (Xmm10),
390 context_offset (Xmm11),
391 context_offset (Xmm12),
392 context_offset (Xmm13),
393 context_offset (Xmm14),
394 context_offset (Xmm15),
396 context_offset (FloatSave.MxCsr)
398 #undef context_offset
400 #endif /* __x86_64__ */
402 /* Fetch register from gdbserver regcache data. */
404 i386_fetch_inferior_register (struct regcache *regcache,
405 win32_thread_info *th, int r)
407 char *context_offset = (char *) &th->context + mappings[r];
412 l = *((long *) context_offset) & 0xffff;
413 supply_register (regcache, r, (char *) &l);
415 else if (r == FOP_REGNUM)
417 l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
418 supply_register (regcache, r, (char *) &l);
421 supply_register (regcache, r, context_offset);
424 /* Store a new register value into the thread context of TH. */
426 i386_store_inferior_register (struct regcache *regcache,
427 win32_thread_info *th, int r)
429 char *context_offset = (char *) &th->context + mappings[r];
430 collect_register (regcache, r, context_offset);
433 static const unsigned char i386_win32_breakpoint = 0xcc;
434 #define i386_win32_breakpoint_len 1
437 i386_arch_setup (void)
440 init_registers_amd64 ();
441 win32_tdesc = tdesc_amd64;
443 init_registers_i386 ();
444 win32_tdesc = tdesc_i386;
448 struct win32_target_ops the_low_target = {
450 sizeof (mappings) / sizeof (mappings[0]),
452 i386_get_thread_context,
453 i386_set_thread_context,
455 i386_fetch_inferior_register,
456 i386_store_inferior_register,
458 &i386_win32_breakpoint,
459 i386_win32_breakpoint_len,
460 i386_supports_z_point_type,
463 x86_stopped_by_watchpoint,
464 x86_stopped_data_address