-/* Print the values of the mirrored debug registers. */
-
-void
-i386_dr_show (struct i386_debug_reg_state *state,
- const char *func, CORE_ADDR addr,
- int len, enum target_hw_bp_type type)
-{
- int i;
-
- debug_printf ("%s", func);
- if (addr || len)
- debug_printf (" (addr=%s, len=%d, type=%s)",
- phex (addr, 8), len,
- type == hw_write ? "data-write"
- : (type == hw_read ? "data-read"
- : (type == hw_access ? "data-read/write"
- : (type == hw_execute ? "instruction-execute"
- /* FIXME: if/when I/O read/write
- watchpoints are supported, add them
- here. */
- : "??unknown??"))));
- debug_printf (":\n");
- debug_printf ("\tCONTROL (DR7): %s STATUS (DR6): %s\n",
- phex (state->dr_control_mirror, 8),
- phex (state->dr_status_mirror, 8));
- ALL_DEBUG_REGISTERS (i)
- {
- debug_printf ("\
-\tDR%d: addr=0x%s, ref.count=%d DR%d: addr=0x%s, ref.count=%d\n",
- i, phex (state->dr_mirror[i],
- i386_get_debug_register_length ()),
- state->dr_ref_count[i],
- i + 1, phex (state->dr_mirror[i + 1],
- i386_get_debug_register_length ()),
- state->dr_ref_count[i + 1]);
- i++;
- }
-}
-
-/* Return the value of a 4-bit field for DR7 suitable for watching a
- region of LEN bytes for accesses of type TYPE. LEN is assumed to
- have the value of 1, 2, or 4. */
-
-unsigned
-i386_dr_length_and_rw_bits (int len, enum target_hw_bp_type type)
-{
- unsigned rw;
-
- switch (type)
- {
- case hw_execute:
- rw = DR_RW_EXECUTE;
- break;
- case hw_write:
- rw = DR_RW_WRITE;
- break;
- case hw_read:
- internal_error (__FILE__, __LINE__,
- _("The i386 doesn't support "
- "data-read watchpoints.\n"));
- case hw_access:
- rw = DR_RW_READ;
- break;
-#if 0
- /* Not yet supported. */
- case hw_io_access:
- rw = DR_RW_IORW;
- break;
-#endif
- default:
- internal_error (__FILE__, __LINE__, _("\
-Invalid hardware breakpoint type %d in i386_dr_length_and_rw_bits.\n"),
- (int) type);
- }
-
- switch (len)
- {
- case 1:
- return (DR_LEN_1 | rw);
- case 2:
- return (DR_LEN_2 | rw);
- case 4:
- return (DR_LEN_4 | rw);
- case 8:
- if (TARGET_HAS_DR_LEN_8)
- return (DR_LEN_8 | rw);
- /* ELSE FALL THROUGH */
- default:
- internal_error (__FILE__, __LINE__, _("\
-Invalid hardware breakpoint length %d in i386_dr_length_and_rw_bits.\n"), len);
- }
-}
-
-/* Insert a watchpoint at address ADDR, which is assumed to be aligned
- according to the length of the region to watch. LEN_RW_BITS is the
- value of the bits from DR7 which describes the length and access
- type of the region to be watched by this watchpoint. Return 0 on
- success, -1 on failure. */
-
-int
-i386_dr_insert_aligned_watchpoint (struct i386_debug_reg_state *state,
- CORE_ADDR addr, unsigned len_rw_bits)
-{
- int i;
-
- if (!i386_dr_low_can_set_addr () || !i386_dr_low_can_set_control ())
- return -1;
-
- /* First, look for an occupied debug register with the same address
- and the same RW and LEN definitions. If we find one, we can
- reuse it for this watchpoint as well (and save a register). */
- ALL_DEBUG_REGISTERS (i)
- {
- if (!I386_DR_VACANT (state, i)
- && state->dr_mirror[i] == addr
- && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
- {
- state->dr_ref_count[i]++;
- return 0;
- }
- }
-
- /* Next, look for a vacant debug register. */
- ALL_DEBUG_REGISTERS (i)
- {
- if (I386_DR_VACANT (state, i))
- break;
- }
-
- /* No more debug registers! */
- if (i >= DR_NADDR)
- return -1;
-
- /* Now set up the register I to watch our region. */
-
- /* Record the info in our local mirrored array. */
- state->dr_mirror[i] = addr;
- state->dr_ref_count[i] = 1;
- I386_DR_SET_RW_LEN (state, i, len_rw_bits);
- /* Note: we only enable the watchpoint locally, i.e. in the current
- task. Currently, no i386 target allows or supports global
- watchpoints; however, if any target would want that in the
- future, GDB should probably provide a command to control whether
- to enable watchpoints globally or locally, and the code below
- should use global or local enable and slow-down flags as
- appropriate. */
- I386_DR_LOCAL_ENABLE (state, i);
- state->dr_control_mirror |= DR_LOCAL_SLOWDOWN;
- state->dr_control_mirror &= I386_DR_CONTROL_MASK;
-
- return 0;
-}
-
-/* Remove a watchpoint at address ADDR, which is assumed to be aligned
- according to the length of the region to watch. LEN_RW_BITS is the
- value of the bits from DR7 which describes the length and access
- type of the region watched by this watchpoint. Return 0 on
- success, -1 on failure. */
-
-int
-i386_dr_remove_aligned_watchpoint (struct i386_debug_reg_state *state,
- CORE_ADDR addr, unsigned len_rw_bits)
-{
- int i, retval = -1;
-
- ALL_DEBUG_REGISTERS (i)
- {
- if (!I386_DR_VACANT (state, i)
- && state->dr_mirror[i] == addr
- && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
- {
- if (--state->dr_ref_count[i] == 0) /* No longer in use? */
- {
- /* Reset our mirror. */
- state->dr_mirror[i] = 0;
- I386_DR_DISABLE (state, i);
- }
- retval = 0;
- }
- }
-
- return retval;
-}
-
-/* Insert or remove a (possibly non-aligned) watchpoint, or count the
- number of debug registers required to watch a region at address
- ADDR whose length is LEN for accesses of type TYPE. Return 0 on
- successful insertion or removal, a positive number when queried
- about the number of registers, or -1 on failure. If WHAT is not a
- valid value, bombs through internal_error. */
-
-static int
-i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state,
- i386_wp_op_t what, CORE_ADDR addr, int len,
- enum target_hw_bp_type type)
-{
- int retval = 0;
- int max_wp_len = TARGET_HAS_DR_LEN_8 ? 8 : 4;
-
- static const int size_try_array[8][8] =
- {
- {1, 1, 1, 1, 1, 1, 1, 1}, /* Trying size one. */
- {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size two. */
- {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size three. */
- {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size four. */
- {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size five. */
- {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size six. */
- {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size seven. */
- {8, 1, 2, 1, 4, 1, 2, 1}, /* Trying size eight. */
- };
-
- while (len > 0)
- {
- int align = addr % max_wp_len;
- /* Four (eight on AMD64) is the maximum length a debug register
- can watch. */
- int try = (len > max_wp_len ? (max_wp_len - 1) : len - 1);
- int size = size_try_array[try][align];
-
- if (what == WP_COUNT)
- {
- /* size_try_array[] is defined such that each iteration
- through the loop is guaranteed to produce an address and a
- size that can be watched with a single debug register.
- Thus, for counting the registers required to watch a
- region, we simply need to increment the count on each
- iteration. */
- retval++;
- }
- else
- {
- unsigned len_rw = i386_dr_length_and_rw_bits (size, type);
-
- if (what == WP_INSERT)
- retval = i386_dr_insert_aligned_watchpoint (state, addr, len_rw);
- else if (what == WP_REMOVE)
- retval = i386_dr_remove_aligned_watchpoint (state, addr, len_rw);
- else
- internal_error (__FILE__, __LINE__, _("\
-Invalid value %d of operation in i386_handle_nonaligned_watchpoint.\n"),
- (int) what);
- if (retval)
- break;
- }
-
- addr += size;
- len -= size;
- }
-
- return retval;
-}
-
-/* Update the inferior debug registers state, in STATE, with the
- new debug registers state, in NEW_STATE. */
-
-void
-i386_dr_update_inferior_debug_regs (struct i386_debug_reg_state *state,
- struct i386_debug_reg_state *new_state)
-{
- int i;
-
- ALL_DEBUG_REGISTERS (i)
- {
- if (I386_DR_VACANT (new_state, i) != I386_DR_VACANT (state, i))
- i386_dr_low_set_addr (new_state, i);
- else
- gdb_assert (new_state->dr_mirror[i] == state->dr_mirror[i]);
- }
-
- if (new_state->dr_control_mirror != state->dr_control_mirror)
- i386_dr_low_set_control (new_state);
-
- *state = *new_state;
-}
-