1 /* Copyright (C) 2007-2020 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"
21 #include "gdbsupport/x86-xstate.h"
23 #include "arch/amd64.h"
25 #include "arch/i386.h"
27 #include "x86-tdesc.h"
29 using namespace windows_nat
;
31 #ifndef CONTEXT_EXTENDED_REGISTERS
32 #define CONTEXT_EXTENDED_REGISTERS 0
38 #define FLAG_TRACE_BIT 0x100
40 static struct x86_debug_reg_state debug_reg_state
;
43 update_debug_registers (thread_info
*thread
)
45 windows_thread_info
*th
= (windows_thread_info
*) thread_target_data (thread
);
47 /* The actual update is done later just before resuming the lwp,
48 we just mark that the registers need updating. */
49 th
->debug_registers_changed
= true;
52 /* Update the inferior's debug register REGNUM from STATE. */
55 x86_dr_low_set_addr (int regnum
, CORE_ADDR addr
)
57 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
59 /* Only update the threads of this process. */
60 for_each_thread (current_thread
->id
.pid (), update_debug_registers
);
63 /* Update the inferior's DR7 debug control register from STATE. */
66 x86_dr_low_set_control (unsigned long control
)
68 /* Only update the threads of this process. */
69 for_each_thread (current_thread
->id
.pid (), update_debug_registers
);
72 /* Return the current value of a DR register of the current thread's
76 win32_get_current_dr (int dr
)
78 windows_thread_info
*th
79 = (windows_thread_info
*) thread_target_data (current_thread
);
81 win32_require_context (th
);
85 return th->context.Dr ## DR
99 gdb_assert_not_reached ("unhandled dr");
103 x86_dr_low_get_addr (int regnum
)
105 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
107 return win32_get_current_dr (regnum
- DR_FIRSTADDR
);
111 x86_dr_low_get_control (void)
113 return win32_get_current_dr (7);
116 /* Get the value of the DR6 debug status register from the inferior
117 and record it in STATE. */
120 x86_dr_low_get_status (void)
122 return win32_get_current_dr (6);
125 /* Low-level function vector. */
126 struct x86_dr_low_type x86_dr_low
=
128 x86_dr_low_set_control
,
131 x86_dr_low_get_status
,
132 x86_dr_low_get_control
,
136 /* Breakpoint/watchpoint support. */
139 i386_supports_z_point_type (char z_type
)
143 case Z_PACKET_WRITE_WP
:
144 case Z_PACKET_ACCESS_WP
:
152 i386_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
153 int size
, struct raw_breakpoint
*bp
)
157 case raw_bkpt_type_write_wp
:
158 case raw_bkpt_type_access_wp
:
160 enum target_hw_bp_type hw_type
161 = raw_bkpt_type_to_target_hw_bp_type (type
);
163 return x86_dr_insert_watchpoint (&debug_reg_state
,
164 hw_type
, addr
, size
);
173 i386_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
174 int size
, struct raw_breakpoint
*bp
)
178 case raw_bkpt_type_write_wp
:
179 case raw_bkpt_type_access_wp
:
181 enum target_hw_bp_type hw_type
182 = raw_bkpt_type_to_target_hw_bp_type (type
);
184 return x86_dr_remove_watchpoint (&debug_reg_state
,
185 hw_type
, addr
, size
);
194 x86_stopped_by_watchpoint (void)
196 return x86_dr_stopped_by_watchpoint (&debug_reg_state
);
200 x86_stopped_data_address (void)
203 if (x86_dr_stopped_data_address (&debug_reg_state
, &addr
))
209 i386_initial_stuff (void)
211 x86_low_init_dregs (&debug_reg_state
);
215 i386_get_thread_context (windows_thread_info
*th
)
217 /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
218 the system doesn't support extended registers. */
219 static DWORD extended_registers
= CONTEXT_EXTENDED_REGISTERS
;
222 th
->context
.ContextFlags
= (CONTEXT_FULL
223 | CONTEXT_FLOATING_POINT
224 | CONTEXT_DEBUG_REGISTERS
225 | extended_registers
);
227 if (!GetThreadContext (th
->h
, &th
->context
))
229 DWORD e
= GetLastError ();
231 if (extended_registers
&& e
== ERROR_INVALID_PARAMETER
)
233 extended_registers
= 0;
237 error ("GetThreadContext failure %ld\n", (long) e
);
242 i386_prepare_to_resume (windows_thread_info
*th
)
244 if (th
->debug_registers_changed
)
246 struct x86_debug_reg_state
*dr
= &debug_reg_state
;
248 win32_require_context (th
);
250 th
->context
.Dr0
= dr
->dr_mirror
[0];
251 th
->context
.Dr1
= dr
->dr_mirror
[1];
252 th
->context
.Dr2
= dr
->dr_mirror
[2];
253 th
->context
.Dr3
= dr
->dr_mirror
[3];
254 /* th->context.Dr6 = dr->dr_status_mirror;
255 FIXME: should we set dr6 also ?? */
256 th
->context
.Dr7
= dr
->dr_control_mirror
;
258 th
->debug_registers_changed
= false;
263 i386_thread_added (windows_thread_info
*th
)
265 th
->debug_registers_changed
= true;
269 i386_single_step (windows_thread_info
*th
)
271 th
->context
.EFlags
|= FLAG_TRACE_BIT
;
276 /* An array of offset mappings into a Win32 Context structure.
277 This is a one-to-one mapping which is indexed by gdb's register
278 numbers. It retrieves an offset into the context structure where
279 the 4 byte register is located.
280 An offset value of -1 indicates that Win32 does not provide this
281 register in it's CONTEXT structure. In this case regptr will return
282 a pointer into a dummy register. */
283 #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x))
284 static const int mappings
[] = {
285 context_offset (Eax
),
286 context_offset (Ecx
),
287 context_offset (Edx
),
288 context_offset (Ebx
),
289 context_offset (Esp
),
290 context_offset (Ebp
),
291 context_offset (Esi
),
292 context_offset (Edi
),
293 context_offset (Eip
),
294 context_offset (EFlags
),
295 context_offset (SegCs
),
296 context_offset (SegSs
),
297 context_offset (SegDs
),
298 context_offset (SegEs
),
299 context_offset (SegFs
),
300 context_offset (SegGs
),
301 context_offset (FloatSave
.RegisterArea
[0 * 10]),
302 context_offset (FloatSave
.RegisterArea
[1 * 10]),
303 context_offset (FloatSave
.RegisterArea
[2 * 10]),
304 context_offset (FloatSave
.RegisterArea
[3 * 10]),
305 context_offset (FloatSave
.RegisterArea
[4 * 10]),
306 context_offset (FloatSave
.RegisterArea
[5 * 10]),
307 context_offset (FloatSave
.RegisterArea
[6 * 10]),
308 context_offset (FloatSave
.RegisterArea
[7 * 10]),
309 context_offset (FloatSave
.ControlWord
),
310 context_offset (FloatSave
.StatusWord
),
311 context_offset (FloatSave
.TagWord
),
312 context_offset (FloatSave
.ErrorSelector
),
313 context_offset (FloatSave
.ErrorOffset
),
314 context_offset (FloatSave
.DataSelector
),
315 context_offset (FloatSave
.DataOffset
),
316 context_offset (FloatSave
.ErrorSelector
),
318 context_offset (ExtendedRegisters
[10 * 16]),
319 context_offset (ExtendedRegisters
[11 * 16]),
320 context_offset (ExtendedRegisters
[12 * 16]),
321 context_offset (ExtendedRegisters
[13 * 16]),
322 context_offset (ExtendedRegisters
[14 * 16]),
323 context_offset (ExtendedRegisters
[15 * 16]),
324 context_offset (ExtendedRegisters
[16 * 16]),
325 context_offset (ExtendedRegisters
[17 * 16]),
327 context_offset (ExtendedRegisters
[24])
329 #undef context_offset
331 #else /* __x86_64__ */
333 #define context_offset(x) (offsetof (CONTEXT, x))
334 static const int mappings
[] =
336 context_offset (Rax
),
337 context_offset (Rbx
),
338 context_offset (Rcx
),
339 context_offset (Rdx
),
340 context_offset (Rsi
),
341 context_offset (Rdi
),
342 context_offset (Rbp
),
343 context_offset (Rsp
),
346 context_offset (R10
),
347 context_offset (R11
),
348 context_offset (R12
),
349 context_offset (R13
),
350 context_offset (R14
),
351 context_offset (R15
),
352 context_offset (Rip
),
353 context_offset (EFlags
),
354 context_offset (SegCs
),
355 context_offset (SegSs
),
356 context_offset (SegDs
),
357 context_offset (SegEs
),
358 context_offset (SegFs
),
359 context_offset (SegGs
),
360 context_offset (FloatSave
.FloatRegisters
[0]),
361 context_offset (FloatSave
.FloatRegisters
[1]),
362 context_offset (FloatSave
.FloatRegisters
[2]),
363 context_offset (FloatSave
.FloatRegisters
[3]),
364 context_offset (FloatSave
.FloatRegisters
[4]),
365 context_offset (FloatSave
.FloatRegisters
[5]),
366 context_offset (FloatSave
.FloatRegisters
[6]),
367 context_offset (FloatSave
.FloatRegisters
[7]),
368 context_offset (FloatSave
.ControlWord
),
369 context_offset (FloatSave
.StatusWord
),
370 context_offset (FloatSave
.TagWord
),
371 context_offset (FloatSave
.ErrorSelector
),
372 context_offset (FloatSave
.ErrorOffset
),
373 context_offset (FloatSave
.DataSelector
),
374 context_offset (FloatSave
.DataOffset
),
375 context_offset (FloatSave
.ErrorSelector
)
377 context_offset (Xmm0
),
378 context_offset (Xmm1
),
379 context_offset (Xmm2
),
380 context_offset (Xmm3
),
381 context_offset (Xmm4
),
382 context_offset (Xmm5
),
383 context_offset (Xmm6
),
384 context_offset (Xmm7
),
385 context_offset (Xmm8
),
386 context_offset (Xmm9
),
387 context_offset (Xmm10
),
388 context_offset (Xmm11
),
389 context_offset (Xmm12
),
390 context_offset (Xmm13
),
391 context_offset (Xmm14
),
392 context_offset (Xmm15
),
394 context_offset (FloatSave
.MxCsr
)
396 #undef context_offset
398 #endif /* __x86_64__ */
400 /* Fetch register from gdbserver regcache data. */
402 i386_fetch_inferior_register (struct regcache
*regcache
,
403 windows_thread_info
*th
, int r
)
405 char *context_offset
= (char *) &th
->context
+ mappings
[r
];
410 l
= *((long *) context_offset
) & 0xffff;
411 supply_register (regcache
, r
, (char *) &l
);
413 else if (r
== FOP_REGNUM
)
415 l
= (*((long *) context_offset
) >> 16) & ((1 << 11) - 1);
416 supply_register (regcache
, r
, (char *) &l
);
419 supply_register (regcache
, r
, context_offset
);
422 /* Store a new register value into the thread context of TH. */
424 i386_store_inferior_register (struct regcache
*regcache
,
425 windows_thread_info
*th
, int r
)
427 char *context_offset
= (char *) &th
->context
+ mappings
[r
];
428 collect_register (regcache
, r
, context_offset
);
431 static const unsigned char i386_win32_breakpoint
= 0xcc;
432 #define i386_win32_breakpoint_len 1
435 i386_arch_setup (void)
437 struct target_desc
*tdesc
;
440 tdesc
= amd64_create_target_description (X86_XSTATE_SSE_MASK
, false,
442 const char **expedite_regs
= amd64_expedite_regs
;
444 tdesc
= i386_create_target_description (X86_XSTATE_SSE_MASK
, false, false);
445 const char **expedite_regs
= i386_expedite_regs
;
448 init_target_desc (tdesc
, expedite_regs
);
453 /* Implement win32_target_ops "get_pc" method. */
456 i386_win32_get_pc (struct regcache
*regcache
)
458 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
464 collect_register_by_name (regcache
, "rip", &pc
);
465 return (CORE_ADDR
) pc
;
471 collect_register_by_name (regcache
, "eip", &pc
);
472 return (CORE_ADDR
) pc
;
476 /* Implement win32_target_ops "set_pc" method. */
479 i386_win32_set_pc (struct regcache
*regcache
, CORE_ADDR pc
)
481 bool use_64bit
= register_size (regcache
->tdesc
, 0) == 8;
487 supply_register_by_name (regcache
, "rip", &newpc
);
493 supply_register_by_name (regcache
, "eip", &newpc
);
497 struct win32_target_ops the_low_target
= {
499 sizeof (mappings
) / sizeof (mappings
[0]),
501 i386_get_thread_context
,
502 i386_prepare_to_resume
,
504 i386_fetch_inferior_register
,
505 i386_store_inferior_register
,
507 &i386_win32_breakpoint
,
508 i386_win32_breakpoint_len
,
512 i386_supports_z_point_type
,
515 x86_stopped_by_watchpoint
,
516 x86_stopped_data_address