1 /* Debug register code for x86 (i386 and x86-64).
3 Copyright (C) 2001-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "gdbsupport/common-defs.h"
21 #include "x86-dregs.h"
22 #include "gdbsupport/break-common.h"
24 /* Support for hardware watchpoints and breakpoints using the x86
27 This provides several functions for inserting and removing
28 hardware-assisted breakpoints and watchpoints, testing if one or
29 more of the watchpoints triggered and at what address, checking
30 whether a given region can be watched, etc.
32 The functions below implement debug registers sharing by reference
33 counts, and allow to watch regions up to 16 bytes long. */
35 /* Accessor macros for low-level function vector. */
37 /* Can we update the inferior's debug registers? */
40 x86_dr_low_can_set_addr ()
42 return x86_dr_low
.set_addr
!= nullptr;
45 /* Update the inferior's debug register REGNUM from STATE. */
48 x86_dr_low_set_addr (struct x86_debug_reg_state
*new_state
, int i
)
50 x86_dr_low
.set_addr (i
, new_state
->dr_mirror
[i
]);
53 /* Return the inferior's debug register REGNUM. */
56 x86_dr_low_get_addr (int i
)
58 return x86_dr_low
.get_addr (i
);
61 /* Can we update the inferior's DR7 control register? */
64 x86_dr_low_can_set_control ()
66 return x86_dr_low
.set_control
!= nullptr;
69 /* Update the inferior's DR7 debug control register from STATE. */
72 x86_dr_low_set_control (struct x86_debug_reg_state
*new_state
)
74 x86_dr_low
.set_control (new_state
->dr_control_mirror
);
77 /* Return the value of the inferior's DR7 debug control register. */
80 x86_dr_low_get_control ()
82 return x86_dr_low
.get_control ();
85 /* Return the value of the inferior's DR6 debug status register. */
88 x86_dr_low_get_status ()
90 return x86_dr_low
.get_status ();
93 /* Return the debug register size, in bytes. */
96 x86_get_debug_register_length ()
98 return x86_dr_low
.debug_register_length
;
101 /* Support for 8-byte wide hw watchpoints. */
102 #define TARGET_HAS_DR_LEN_8 (x86_get_debug_register_length () == 8)
104 /* DR7 Debug Control register fields. */
106 /* How many bits to skip in DR7 to get to R/W and LEN fields. */
107 #define DR_CONTROL_SHIFT 16
108 /* How many bits in DR7 per R/W and LEN field for each watchpoint. */
109 #define DR_CONTROL_SIZE 4
111 /* Watchpoint/breakpoint read/write fields in DR7. */
112 #define DR_RW_EXECUTE (0x0) /* Break on instruction execution. */
113 #define DR_RW_WRITE (0x1) /* Break on data writes. */
114 #define DR_RW_READ (0x3) /* Break on data reads or writes. */
116 /* This is here for completeness. No platform supports this
117 functionality yet (as of March 2001). Note that the DE flag in the
118 CR4 register needs to be set to support this. */
120 #define DR_RW_IORW (0x2) /* Break on I/O reads or writes. */
123 /* Watchpoint/breakpoint length fields in DR7. The 2-bit left shift
124 is so we could OR this with the read/write field defined above. */
125 #define DR_LEN_1 (0x0 << 2) /* 1-byte region watch or breakpoint. */
126 #define DR_LEN_2 (0x1 << 2) /* 2-byte region watch. */
127 #define DR_LEN_4 (0x3 << 2) /* 4-byte region watch. */
128 #define DR_LEN_8 (0x2 << 2) /* 8-byte region watch (AMD64). */
130 /* Local and Global Enable flags in DR7.
132 When the Local Enable flag is set, the breakpoint/watchpoint is
133 enabled only for the current task; the processor automatically
134 clears this flag on every task switch. When the Global Enable flag
135 is set, the breakpoint/watchpoint is enabled for all tasks; the
136 processor never clears this flag.
138 Currently, all watchpoint are locally enabled. If you need to
139 enable them globally, read the comment which pertains to this in
140 x86_insert_aligned_watchpoint below. */
141 #define DR_LOCAL_ENABLE_SHIFT 0 /* Extra shift to the local enable bit. */
142 #define DR_GLOBAL_ENABLE_SHIFT 1 /* Extra shift to the global enable bit. */
143 #define DR_ENABLE_SIZE 2 /* Two enable bits per debug register. */
145 /* Local and global exact breakpoint enable flags (a.k.a. slowdown
146 flags). These are only required on i386, to allow detection of the
147 exact instruction which caused a watchpoint to break; i486 and
148 later processors do that automatically. We set these flags for
149 backwards compatibility. */
150 #define DR_LOCAL_SLOWDOWN (0x100)
151 #define DR_GLOBAL_SLOWDOWN (0x200)
153 /* Fields reserved by Intel. This includes the GD (General Detect
154 Enable) flag, which causes a debug exception to be generated when a
155 MOV instruction accesses one of the debug registers.
157 FIXME: My Intel manual says we should use 0xF800, not 0xFC00. */
158 #define DR_CONTROL_RESERVED (0xFC00)
160 /* Auxiliary helper macros. */
162 /* A value that masks all fields in DR7 that are reserved by Intel. */
163 #define X86_DR_CONTROL_MASK (~DR_CONTROL_RESERVED)
165 /* The I'th debug register is vacant if its Local and Global Enable
166 bits are reset in the Debug Control register. */
167 #define X86_DR_VACANT(state, i) \
168 (((state)->dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0)
170 /* Locally enable the break/watchpoint in the I'th debug register. */
171 #define X86_DR_LOCAL_ENABLE(state, i) \
173 (state)->dr_control_mirror |= \
174 (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
177 /* Globally enable the break/watchpoint in the I'th debug register. */
178 #define X86_DR_GLOBAL_ENABLE(state, i) \
180 (state)->dr_control_mirror |= \
181 (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
184 /* Disable the break/watchpoint in the I'th debug register. */
185 #define X86_DR_DISABLE(state, i) \
187 (state)->dr_control_mirror &= \
188 ~(3 << (DR_ENABLE_SIZE * (i))); \
191 /* Set in DR7 the RW and LEN fields for the I'th debug register. */
192 #define X86_DR_SET_RW_LEN(state, i, rwlen) \
194 (state)->dr_control_mirror &= \
195 ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
196 (state)->dr_control_mirror |= \
197 ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
200 /* Get from DR7 the RW and LEN fields for the I'th debug register. */
201 #define X86_DR_GET_RW_LEN(dr7, i) \
203 >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)
205 /* Did the watchpoint whose address is in the I'th register break? */
206 #define X86_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i)))
208 /* Types of operations supported by x86_handle_nonaligned_watchpoint. */
209 enum x86_wp_op_t
{ WP_INSERT
, WP_REMOVE
, WP_COUNT
};
211 /* Print the values of the mirrored debug registers. */
214 x86_show_dr (struct x86_debug_reg_state
*state
,
215 const char *func
, CORE_ADDR addr
,
216 int len
, enum target_hw_bp_type type
)
220 debug_printf ("%s", func
);
222 debug_printf (" (addr=%s, len=%d, type=%s)",
224 type
== hw_write
? "data-write"
225 : (type
== hw_read
? "data-read"
226 : (type
== hw_access
? "data-read/write"
227 : (type
== hw_execute
? "instruction-execute"
228 /* FIXME: if/when I/O read/write
229 watchpoints are supported, add them
232 debug_printf (":\n");
234 debug_printf ("\tCONTROL (DR7): 0x%s\n", phex (state
->dr_control_mirror
, 8));
235 debug_printf ("\tSTATUS (DR6): 0x%s\n", phex (state
->dr_status_mirror
, 8));
237 ALL_DEBUG_ADDRESS_REGISTERS (i
)
239 debug_printf ("\tDR%d: addr=0x%s, ref.count=%d\n",
240 i
, phex (state
->dr_mirror
[i
],
241 x86_get_debug_register_length ()),
242 state
->dr_ref_count
[i
]);
246 /* Return the value of a 4-bit field for DR7 suitable for watching a
247 region of LEN bytes for accesses of type TYPE. LEN is assumed to
248 have the value of 1, 2, or 4. */
251 x86_length_and_rw_bits (int len
, enum target_hw_bp_type type
)
264 internal_error (__FILE__
, __LINE__
,
265 _("The i386 doesn't support "
266 "data-read watchpoints.\n"));
271 /* Not yet supported. */
277 internal_error (__FILE__
, __LINE__
, _("\
278 Invalid hardware breakpoint type %d in x86_length_and_rw_bits.\n"),
285 return (DR_LEN_1
| rw
);
287 return (DR_LEN_2
| rw
);
289 return (DR_LEN_4
| rw
);
291 if (TARGET_HAS_DR_LEN_8
)
292 return (DR_LEN_8
| rw
);
295 internal_error (__FILE__
, __LINE__
, _("\
296 Invalid hardware breakpoint length %d in x86_length_and_rw_bits.\n"), len
);
300 /* Insert a watchpoint at address ADDR, which is assumed to be aligned
301 according to the length of the region to watch. LEN_RW_BITS is the
302 value of the bits from DR7 which describes the length and access
303 type of the region to be watched by this watchpoint. Return 0 on
304 success, -1 on failure. */
307 x86_insert_aligned_watchpoint (struct x86_debug_reg_state
*state
,
308 CORE_ADDR addr
, unsigned len_rw_bits
)
312 if (!x86_dr_low_can_set_addr () || !x86_dr_low_can_set_control ())
315 /* First, look for an occupied debug register with the same address
316 and the same RW and LEN definitions. If we find one, we can
317 reuse it for this watchpoint as well (and save a register). */
318 ALL_DEBUG_ADDRESS_REGISTERS (i
)
320 if (!X86_DR_VACANT (state
, i
)
321 && state
->dr_mirror
[i
] == addr
322 && X86_DR_GET_RW_LEN (state
->dr_control_mirror
, i
) == len_rw_bits
)
324 state
->dr_ref_count
[i
]++;
329 /* Next, look for a vacant debug register. */
330 ALL_DEBUG_ADDRESS_REGISTERS (i
)
332 if (X86_DR_VACANT (state
, i
))
336 /* No more debug registers! */
340 /* Now set up the register I to watch our region. */
342 /* Record the info in our local mirrored array. */
343 state
->dr_mirror
[i
] = addr
;
344 state
->dr_ref_count
[i
] = 1;
345 X86_DR_SET_RW_LEN (state
, i
, len_rw_bits
);
346 /* Note: we only enable the watchpoint locally, i.e. in the current
347 task. Currently, no x86 target allows or supports global
348 watchpoints; however, if any target would want that in the
349 future, GDB should probably provide a command to control whether
350 to enable watchpoints globally or locally, and the code below
351 should use global or local enable and slow-down flags as
353 X86_DR_LOCAL_ENABLE (state
, i
);
354 state
->dr_control_mirror
|= DR_LOCAL_SLOWDOWN
;
355 state
->dr_control_mirror
&= X86_DR_CONTROL_MASK
;
360 /* Remove a watchpoint at address ADDR, which is assumed to be aligned
361 according to the length of the region to watch. LEN_RW_BITS is the
362 value of the bits from DR7 which describes the length and access
363 type of the region watched by this watchpoint. Return 0 on
364 success, -1 on failure. */
367 x86_remove_aligned_watchpoint (struct x86_debug_reg_state
*state
,
368 CORE_ADDR addr
, unsigned len_rw_bits
)
373 ALL_DEBUG_ADDRESS_REGISTERS (i
)
375 if (!X86_DR_VACANT (state
, i
)
376 && state
->dr_mirror
[i
] == addr
377 && X86_DR_GET_RW_LEN (state
->dr_control_mirror
, i
) == len_rw_bits
)
379 if (--state
->dr_ref_count
[i
] == 0) /* No longer in use? */
381 /* Reset our mirror. */
382 state
->dr_mirror
[i
] = 0;
383 X86_DR_DISABLE (state
, i
);
384 /* Even though not strictly necessary, clear out all
385 bits in DR_CONTROL related to this debug register.
386 Debug output is clearer when we don't have stale bits
387 in place. This also allows the assertion below. */
388 X86_DR_SET_RW_LEN (state
, i
, 0);
393 if (!X86_DR_VACANT (state
, i
))
399 /* Even though not strictly necessary, clear out all of
400 DR_CONTROL, so that when we have no debug registers in use,
401 we end up with DR_CONTROL == 0. The Linux support relies on
402 this for an optimization. Plus, it makes for clearer debug
404 state
->dr_control_mirror
&= ~DR_LOCAL_SLOWDOWN
;
406 gdb_assert (state
->dr_control_mirror
== 0);
411 /* Insert or remove a (possibly non-aligned) watchpoint, or count the
412 number of debug registers required to watch a region at address
413 ADDR whose length is LEN for accesses of type TYPE. Return 0 on
414 successful insertion or removal, a positive number when queried
415 about the number of registers, or -1 on failure. If WHAT is not a
416 valid value, bombs through internal_error. */
419 x86_handle_nonaligned_watchpoint (struct x86_debug_reg_state
*state
,
420 x86_wp_op_t what
, CORE_ADDR addr
, int len
,
421 enum target_hw_bp_type type
)
424 int max_wp_len
= TARGET_HAS_DR_LEN_8
? 8 : 4;
426 static const int size_try_array
[8][8] =
428 {1, 1, 1, 1, 1, 1, 1, 1}, /* Trying size one. */
429 {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size two. */
430 {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size three. */
431 {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size four. */
432 {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size five. */
433 {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size six. */
434 {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size seven. */
435 {8, 1, 2, 1, 4, 1, 2, 1}, /* Trying size eight. */
440 int align
= addr
% max_wp_len
;
441 /* Four (eight on AMD64) is the maximum length a debug register
443 int attempt
= (len
> max_wp_len
? (max_wp_len
- 1) : len
- 1);
444 int size
= size_try_array
[attempt
][align
];
446 if (what
== WP_COUNT
)
448 /* size_try_array[] is defined such that each iteration
449 through the loop is guaranteed to produce an address and a
450 size that can be watched with a single debug register.
451 Thus, for counting the registers required to watch a
452 region, we simply need to increment the count on each
458 unsigned len_rw
= x86_length_and_rw_bits (size
, type
);
460 if (what
== WP_INSERT
)
461 retval
= x86_insert_aligned_watchpoint (state
, addr
, len_rw
);
462 else if (what
== WP_REMOVE
)
463 retval
= x86_remove_aligned_watchpoint (state
, addr
, len_rw
);
465 internal_error (__FILE__
, __LINE__
, _("\
466 Invalid value %d of operation in x86_handle_nonaligned_watchpoint.\n"),
479 /* Update the inferior debug registers state, in STATE, with the
480 new debug registers state, in NEW_STATE. */
483 x86_update_inferior_debug_regs (struct x86_debug_reg_state
*state
,
484 struct x86_debug_reg_state
*new_state
)
488 ALL_DEBUG_ADDRESS_REGISTERS (i
)
490 if (X86_DR_VACANT (new_state
, i
) != X86_DR_VACANT (state
, i
))
491 x86_dr_low_set_addr (new_state
, i
);
493 gdb_assert (new_state
->dr_mirror
[i
] == state
->dr_mirror
[i
]);
496 if (new_state
->dr_control_mirror
!= state
->dr_control_mirror
)
497 x86_dr_low_set_control (new_state
);
502 /* Insert a watchpoint to watch a memory region which starts at
503 address ADDR and whose length is LEN bytes. Watch memory accesses
504 of the type TYPE. Return 0 on success, -1 on failure. */
507 x86_dr_insert_watchpoint (struct x86_debug_reg_state
*state
,
508 enum target_hw_bp_type type
,
509 CORE_ADDR addr
, int len
)
512 /* Work on a local copy of the debug registers, and on success,
513 commit the change back to the inferior. */
514 struct x86_debug_reg_state local_state
= *state
;
517 return 1; /* unsupported */
519 if (((len
!= 1 && len
!= 2 && len
!= 4)
520 && !(TARGET_HAS_DR_LEN_8
&& len
== 8))
523 retval
= x86_handle_nonaligned_watchpoint (&local_state
,
529 unsigned len_rw
= x86_length_and_rw_bits (len
, type
);
531 retval
= x86_insert_aligned_watchpoint (&local_state
,
536 x86_update_inferior_debug_regs (state
, &local_state
);
539 x86_show_dr (state
, "insert_watchpoint", addr
, len
, type
);
544 /* Remove a watchpoint that watched the memory region which starts at
545 address ADDR, whose length is LEN bytes, and for accesses of the
546 type TYPE. Return 0 on success, -1 on failure. */
549 x86_dr_remove_watchpoint (struct x86_debug_reg_state
*state
,
550 enum target_hw_bp_type type
,
551 CORE_ADDR addr
, int len
)
554 /* Work on a local copy of the debug registers, and on success,
555 commit the change back to the inferior. */
556 struct x86_debug_reg_state local_state
= *state
;
558 if (((len
!= 1 && len
!= 2 && len
!= 4)
559 && !(TARGET_HAS_DR_LEN_8
&& len
== 8))
562 retval
= x86_handle_nonaligned_watchpoint (&local_state
,
568 unsigned len_rw
= x86_length_and_rw_bits (len
, type
);
570 retval
= x86_remove_aligned_watchpoint (&local_state
,
575 x86_update_inferior_debug_regs (state
, &local_state
);
578 x86_show_dr (state
, "remove_watchpoint", addr
, len
, type
);
583 /* Return non-zero if we can watch a memory region that starts at
584 address ADDR and whose length is LEN bytes. */
587 x86_dr_region_ok_for_watchpoint (struct x86_debug_reg_state
*state
,
588 CORE_ADDR addr
, int len
)
592 /* Compute how many aligned watchpoints we would need to cover this
594 nregs
= x86_handle_nonaligned_watchpoint (state
, WP_COUNT
,
595 addr
, len
, hw_write
);
596 return nregs
<= DR_NADDR
? 1 : 0;
599 /* If the inferior has some break/watchpoint that triggered, set the
600 address associated with that break/watchpoint and return non-zero.
601 Otherwise, return zero. */
604 x86_dr_stopped_data_address (struct x86_debug_reg_state
*state
,
610 /* The current thread's DR_STATUS. We always need to read this to
611 check whether some watchpoint caused the trap. */
613 /* We need DR_CONTROL as well, but only iff DR_STATUS indicates a
614 data breakpoint trap. Only fetch it when necessary, to avoid an
615 unnecessary extra syscall when no watchpoint triggered. */
617 unsigned control
= 0;
619 /* In non-stop/async, threads can be running while we change the
620 global dr_mirror (and friends). Say, we set a watchpoint, and
621 let threads resume. Now, say you delete the watchpoint, or
622 add/remove watchpoints such that dr_mirror changes while threads
623 are running. On targets that support non-stop,
624 inserting/deleting watchpoints updates the global dr_mirror only.
625 It does not update the real thread's debug registers; that's only
626 done prior to resume. Instead, if threads are running when the
627 mirror changes, a temporary and transparent stop on all threads
628 is forced so they can get their copy of the debug registers
629 updated on re-resume. Now, say, a thread hit a watchpoint before
630 having been updated with the new dr_mirror contents, and we
631 haven't yet handled the corresponding SIGTRAP. If we trusted
632 dr_mirror below, we'd mistake the real trapped address (from the
633 last time we had updated debug registers in the thread) with
634 whatever was currently in dr_mirror. So to fix this, dr_mirror
635 always represents intention, what we _want_ threads to have in
636 debug registers. To get at the address and cause of the trap, we
637 need to read the state the thread still has in its debug
640 In sum, always get the current debug register values the current
641 thread has, instead of trusting the global mirror. If the thread
642 was running when we last changed watchpoints, the mirror no
643 longer represents what was set in this thread's debug
645 status
= x86_dr_low_get_status ();
647 ALL_DEBUG_ADDRESS_REGISTERS (i
)
649 if (!X86_DR_WATCH_HIT (status
, i
))
654 control
= x86_dr_low_get_control ();
658 /* This second condition makes sure DRi is set up for a data
659 watchpoint, not a hardware breakpoint. The reason is that
660 GDB doesn't call the target_stopped_data_address method
661 except for data watchpoints. In other words, I'm being
663 if (X86_DR_GET_RW_LEN (control
, i
) != 0)
665 addr
= x86_dr_low_get_addr (i
);
668 x86_show_dr (state
, "watchpoint_hit", addr
, -1, hw_write
);
672 if (show_debug_regs
&& addr
== 0)
673 x86_show_dr (state
, "stopped_data_addr", 0, 0, hw_write
);
680 /* Return non-zero if the inferior has some watchpoint that triggered.
681 Otherwise return zero. */
684 x86_dr_stopped_by_watchpoint (struct x86_debug_reg_state
*state
)
687 return x86_dr_stopped_data_address (state
, &addr
);
690 /* Return non-zero if the inferior has some hardware breakpoint that
691 triggered. Otherwise return zero. */
694 x86_dr_stopped_by_hw_breakpoint (struct x86_debug_reg_state
*state
)
699 /* The current thread's DR_STATUS. We always need to read this to
700 check whether some watchpoint caused the trap. */
702 /* We need DR_CONTROL as well, but only iff DR_STATUS indicates a
703 breakpoint trap. Only fetch it when necessary, to avoid an
704 unnecessary extra syscall when no watchpoint triggered. */
706 unsigned control
= 0;
708 /* As above, always read the current thread's debug registers rather
709 than trusting dr_mirror. */
710 status
= x86_dr_low_get_status ();
712 ALL_DEBUG_ADDRESS_REGISTERS (i
)
714 if (!X86_DR_WATCH_HIT (status
, i
))
719 control
= x86_dr_low_get_control ();
723 if (X86_DR_GET_RW_LEN (control
, i
) == 0)
725 addr
= x86_dr_low_get_addr (i
);
728 x86_show_dr (state
, "watchpoint_hit", addr
, -1, hw_execute
);