15796c8dcSSimon Schubert /* Native-dependent code for the i386.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2001-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
21*ef5ccd6cSJohn Marino #include "i386-nat.h"
225796c8dcSSimon Schubert #include "breakpoint.h"
235796c8dcSSimon Schubert #include "command.h"
245796c8dcSSimon Schubert #include "gdbcmd.h"
255796c8dcSSimon Schubert #include "target.h"
265796c8dcSSimon Schubert #include "gdb_assert.h"
27*ef5ccd6cSJohn Marino #include "inferior.h"
285796c8dcSSimon Schubert
295796c8dcSSimon Schubert /* Support for hardware watchpoints and breakpoints using the i386
305796c8dcSSimon Schubert debug registers.
315796c8dcSSimon Schubert
325796c8dcSSimon Schubert This provides several functions for inserting and removing
335796c8dcSSimon Schubert hardware-assisted breakpoints and watchpoints, testing if one or
345796c8dcSSimon Schubert more of the watchpoints triggered and at what address, checking
355796c8dcSSimon Schubert whether a given region can be watched, etc.
365796c8dcSSimon Schubert
375796c8dcSSimon Schubert The functions below implement debug registers sharing by reference
385796c8dcSSimon Schubert counts, and allow to watch regions up to 16 bytes long. */
395796c8dcSSimon Schubert
405796c8dcSSimon Schubert struct i386_dr_low_type i386_dr_low;
415796c8dcSSimon Schubert
425796c8dcSSimon Schubert
435796c8dcSSimon Schubert /* Support for 8-byte wide hw watchpoints. */
445796c8dcSSimon Schubert #define TARGET_HAS_DR_LEN_8 (i386_dr_low.debug_register_length == 8)
455796c8dcSSimon Schubert
465796c8dcSSimon Schubert /* DR7 Debug Control register fields. */
475796c8dcSSimon Schubert
485796c8dcSSimon Schubert /* How many bits to skip in DR7 to get to R/W and LEN fields. */
495796c8dcSSimon Schubert #define DR_CONTROL_SHIFT 16
505796c8dcSSimon Schubert /* How many bits in DR7 per R/W and LEN field for each watchpoint. */
515796c8dcSSimon Schubert #define DR_CONTROL_SIZE 4
525796c8dcSSimon Schubert
535796c8dcSSimon Schubert /* Watchpoint/breakpoint read/write fields in DR7. */
545796c8dcSSimon Schubert #define DR_RW_EXECUTE (0x0) /* Break on instruction execution. */
555796c8dcSSimon Schubert #define DR_RW_WRITE (0x1) /* Break on data writes. */
565796c8dcSSimon Schubert #define DR_RW_READ (0x3) /* Break on data reads or writes. */
575796c8dcSSimon Schubert
585796c8dcSSimon Schubert /* This is here for completeness. No platform supports this
595796c8dcSSimon Schubert functionality yet (as of March 2001). Note that the DE flag in the
605796c8dcSSimon Schubert CR4 register needs to be set to support this. */
615796c8dcSSimon Schubert #ifndef DR_RW_IORW
625796c8dcSSimon Schubert #define DR_RW_IORW (0x2) /* Break on I/O reads or writes. */
635796c8dcSSimon Schubert #endif
645796c8dcSSimon Schubert
655796c8dcSSimon Schubert /* Watchpoint/breakpoint length fields in DR7. The 2-bit left shift
665796c8dcSSimon Schubert is so we could OR this with the read/write field defined above. */
675796c8dcSSimon Schubert #define DR_LEN_1 (0x0 << 2) /* 1-byte region watch or breakpoint. */
685796c8dcSSimon Schubert #define DR_LEN_2 (0x1 << 2) /* 2-byte region watch. */
695796c8dcSSimon Schubert #define DR_LEN_4 (0x3 << 2) /* 4-byte region watch. */
705796c8dcSSimon Schubert #define DR_LEN_8 (0x2 << 2) /* 8-byte region watch (AMD64). */
715796c8dcSSimon Schubert
725796c8dcSSimon Schubert /* Local and Global Enable flags in DR7.
735796c8dcSSimon Schubert
745796c8dcSSimon Schubert When the Local Enable flag is set, the breakpoint/watchpoint is
755796c8dcSSimon Schubert enabled only for the current task; the processor automatically
765796c8dcSSimon Schubert clears this flag on every task switch. When the Global Enable flag
775796c8dcSSimon Schubert is set, the breakpoint/watchpoint is enabled for all tasks; the
785796c8dcSSimon Schubert processor never clears this flag.
795796c8dcSSimon Schubert
805796c8dcSSimon Schubert Currently, all watchpoint are locally enabled. If you need to
815796c8dcSSimon Schubert enable them globally, read the comment which pertains to this in
825796c8dcSSimon Schubert i386_insert_aligned_watchpoint below. */
835796c8dcSSimon Schubert #define DR_LOCAL_ENABLE_SHIFT 0 /* Extra shift to the local enable bit. */
845796c8dcSSimon Schubert #define DR_GLOBAL_ENABLE_SHIFT 1 /* Extra shift to the global enable bit. */
855796c8dcSSimon Schubert #define DR_ENABLE_SIZE 2 /* Two enable bits per debug register. */
865796c8dcSSimon Schubert
875796c8dcSSimon Schubert /* Local and global exact breakpoint enable flags (a.k.a. slowdown
885796c8dcSSimon Schubert flags). These are only required on i386, to allow detection of the
895796c8dcSSimon Schubert exact instruction which caused a watchpoint to break; i486 and
905796c8dcSSimon Schubert later processors do that automatically. We set these flags for
915796c8dcSSimon Schubert backwards compatibility. */
925796c8dcSSimon Schubert #define DR_LOCAL_SLOWDOWN (0x100)
935796c8dcSSimon Schubert #define DR_GLOBAL_SLOWDOWN (0x200)
945796c8dcSSimon Schubert
955796c8dcSSimon Schubert /* Fields reserved by Intel. This includes the GD (General Detect
965796c8dcSSimon Schubert Enable) flag, which causes a debug exception to be generated when a
975796c8dcSSimon Schubert MOV instruction accesses one of the debug registers.
985796c8dcSSimon Schubert
995796c8dcSSimon Schubert FIXME: My Intel manual says we should use 0xF800, not 0xFC00. */
1005796c8dcSSimon Schubert #define DR_CONTROL_RESERVED (0xFC00)
1015796c8dcSSimon Schubert
1025796c8dcSSimon Schubert /* Auxiliary helper macros. */
1035796c8dcSSimon Schubert
1045796c8dcSSimon Schubert /* A value that masks all fields in DR7 that are reserved by Intel. */
1055796c8dcSSimon Schubert #define I386_DR_CONTROL_MASK (~DR_CONTROL_RESERVED)
1065796c8dcSSimon Schubert
1075796c8dcSSimon Schubert /* The I'th debug register is vacant if its Local and Global Enable
1085796c8dcSSimon Schubert bits are reset in the Debug Control register. */
109a45ae5f8SJohn Marino #define I386_DR_VACANT(state, i) \
110a45ae5f8SJohn Marino (((state)->dr_control_mirror & (3 << (DR_ENABLE_SIZE * (i)))) == 0)
1115796c8dcSSimon Schubert
1125796c8dcSSimon Schubert /* Locally enable the break/watchpoint in the I'th debug register. */
113a45ae5f8SJohn Marino #define I386_DR_LOCAL_ENABLE(state, i) \
114a45ae5f8SJohn Marino do { \
115a45ae5f8SJohn Marino (state)->dr_control_mirror |= \
116a45ae5f8SJohn Marino (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
117a45ae5f8SJohn Marino } while (0)
1185796c8dcSSimon Schubert
1195796c8dcSSimon Schubert /* Globally enable the break/watchpoint in the I'th debug register. */
120a45ae5f8SJohn Marino #define I386_DR_GLOBAL_ENABLE(state, i) \
121a45ae5f8SJohn Marino do { \
122a45ae5f8SJohn Marino (state)->dr_control_mirror |= \
123a45ae5f8SJohn Marino (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i))); \
124a45ae5f8SJohn Marino } while (0)
1255796c8dcSSimon Schubert
1265796c8dcSSimon Schubert /* Disable the break/watchpoint in the I'th debug register. */
127a45ae5f8SJohn Marino #define I386_DR_DISABLE(state, i) \
128a45ae5f8SJohn Marino do { \
129a45ae5f8SJohn Marino (state)->dr_control_mirror &= \
130a45ae5f8SJohn Marino ~(3 << (DR_ENABLE_SIZE * (i))); \
131a45ae5f8SJohn Marino } while (0)
1325796c8dcSSimon Schubert
1335796c8dcSSimon Schubert /* Set in DR7 the RW and LEN fields for the I'th debug register. */
134a45ae5f8SJohn Marino #define I386_DR_SET_RW_LEN(state, i, rwlen) \
1355796c8dcSSimon Schubert do { \
136a45ae5f8SJohn Marino (state)->dr_control_mirror &= \
137a45ae5f8SJohn Marino ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
138a45ae5f8SJohn Marino (state)->dr_control_mirror |= \
139a45ae5f8SJohn Marino ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
1405796c8dcSSimon Schubert } while (0)
1415796c8dcSSimon Schubert
1425796c8dcSSimon Schubert /* Get from DR7 the RW and LEN fields for the I'th debug register. */
143a45ae5f8SJohn Marino #define I386_DR_GET_RW_LEN(dr7, i) \
144a45ae5f8SJohn Marino (((dr7) \
145a45ae5f8SJohn Marino >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))) & 0x0f)
1465796c8dcSSimon Schubert
147cf7f2e2dSJohn Marino /* Mask that this I'th watchpoint has triggered. */
148cf7f2e2dSJohn Marino #define I386_DR_WATCH_MASK(i) (1 << (i))
149cf7f2e2dSJohn Marino
1505796c8dcSSimon Schubert /* Did the watchpoint whose address is in the I'th register break? */
151a45ae5f8SJohn Marino #define I386_DR_WATCH_HIT(dr6, i) ((dr6) & (1 << (i)))
1525796c8dcSSimon Schubert
1535796c8dcSSimon Schubert /* A macro to loop over all debug registers. */
1545796c8dcSSimon Schubert #define ALL_DEBUG_REGISTERS(i) for (i = 0; i < DR_NADDR; i++)
1555796c8dcSSimon Schubert
156*ef5ccd6cSJohn Marino /* Per-process data. We don't bind this to a per-inferior registry
157*ef5ccd6cSJohn Marino because of targets like x86 GNU/Linux that need to keep track of
158*ef5ccd6cSJohn Marino processes that aren't bound to any inferior (e.g., fork children,
159*ef5ccd6cSJohn Marino checkpoints). */
160a45ae5f8SJohn Marino
161*ef5ccd6cSJohn Marino struct i386_process_info
162a45ae5f8SJohn Marino {
163*ef5ccd6cSJohn Marino /* Linked list. */
164*ef5ccd6cSJohn Marino struct i386_process_info *next;
1655796c8dcSSimon Schubert
166*ef5ccd6cSJohn Marino /* The process identifier. */
167*ef5ccd6cSJohn Marino pid_t pid;
168*ef5ccd6cSJohn Marino
169*ef5ccd6cSJohn Marino /* Copy of i386 hardware debug registers. */
170*ef5ccd6cSJohn Marino struct i386_debug_reg_state state;
171a45ae5f8SJohn Marino };
172a45ae5f8SJohn Marino
173*ef5ccd6cSJohn Marino static struct i386_process_info *i386_process_list = NULL;
174a45ae5f8SJohn Marino
175*ef5ccd6cSJohn Marino /* Find process data for process PID. */
176a45ae5f8SJohn Marino
177*ef5ccd6cSJohn Marino static struct i386_process_info *
i386_find_process_pid(pid_t pid)178*ef5ccd6cSJohn Marino i386_find_process_pid (pid_t pid)
179a45ae5f8SJohn Marino {
180*ef5ccd6cSJohn Marino struct i386_process_info *proc;
181*ef5ccd6cSJohn Marino
182*ef5ccd6cSJohn Marino for (proc = i386_process_list; proc; proc = proc->next)
183*ef5ccd6cSJohn Marino if (proc->pid == pid)
184*ef5ccd6cSJohn Marino return proc;
185*ef5ccd6cSJohn Marino
186*ef5ccd6cSJohn Marino return NULL;
187a45ae5f8SJohn Marino }
188a45ae5f8SJohn Marino
189*ef5ccd6cSJohn Marino /* Add process data for process PID. Returns newly allocated info
190*ef5ccd6cSJohn Marino object. */
191*ef5ccd6cSJohn Marino
192*ef5ccd6cSJohn Marino static struct i386_process_info *
i386_add_process(pid_t pid)193*ef5ccd6cSJohn Marino i386_add_process (pid_t pid)
194*ef5ccd6cSJohn Marino {
195*ef5ccd6cSJohn Marino struct i386_process_info *proc;
196*ef5ccd6cSJohn Marino
197*ef5ccd6cSJohn Marino proc = xcalloc (1, sizeof (*proc));
198*ef5ccd6cSJohn Marino proc->pid = pid;
199*ef5ccd6cSJohn Marino
200*ef5ccd6cSJohn Marino proc->next = i386_process_list;
201*ef5ccd6cSJohn Marino i386_process_list = proc;
202*ef5ccd6cSJohn Marino
203*ef5ccd6cSJohn Marino return proc;
204*ef5ccd6cSJohn Marino }
205*ef5ccd6cSJohn Marino
206*ef5ccd6cSJohn Marino /* Get data specific info for process PID, creating it if necessary.
207*ef5ccd6cSJohn Marino Never returns NULL. */
208*ef5ccd6cSJohn Marino
209*ef5ccd6cSJohn Marino static struct i386_process_info *
i386_process_info_get(pid_t pid)210*ef5ccd6cSJohn Marino i386_process_info_get (pid_t pid)
211*ef5ccd6cSJohn Marino {
212*ef5ccd6cSJohn Marino struct i386_process_info *proc;
213*ef5ccd6cSJohn Marino
214*ef5ccd6cSJohn Marino proc = i386_find_process_pid (pid);
215*ef5ccd6cSJohn Marino if (proc == NULL)
216*ef5ccd6cSJohn Marino proc = i386_add_process (pid);
217*ef5ccd6cSJohn Marino
218*ef5ccd6cSJohn Marino return proc;
219*ef5ccd6cSJohn Marino }
220*ef5ccd6cSJohn Marino
221*ef5ccd6cSJohn Marino /* Get debug registers state for process PID. */
222*ef5ccd6cSJohn Marino
223*ef5ccd6cSJohn Marino struct i386_debug_reg_state *
i386_debug_reg_state(pid_t pid)224*ef5ccd6cSJohn Marino i386_debug_reg_state (pid_t pid)
225*ef5ccd6cSJohn Marino {
226*ef5ccd6cSJohn Marino return &i386_process_info_get (pid)->state;
227*ef5ccd6cSJohn Marino }
228*ef5ccd6cSJohn Marino
229*ef5ccd6cSJohn Marino /* See declaration in i386-nat.h. */
230*ef5ccd6cSJohn Marino
231*ef5ccd6cSJohn Marino void
i386_forget_process(pid_t pid)232*ef5ccd6cSJohn Marino i386_forget_process (pid_t pid)
233*ef5ccd6cSJohn Marino {
234*ef5ccd6cSJohn Marino struct i386_process_info *proc, **proc_link;
235*ef5ccd6cSJohn Marino
236*ef5ccd6cSJohn Marino proc = i386_process_list;
237*ef5ccd6cSJohn Marino proc_link = &i386_process_list;
238*ef5ccd6cSJohn Marino
239*ef5ccd6cSJohn Marino while (proc != NULL)
240*ef5ccd6cSJohn Marino {
241*ef5ccd6cSJohn Marino if (proc->pid == pid)
242*ef5ccd6cSJohn Marino {
243*ef5ccd6cSJohn Marino *proc_link = proc->next;
244*ef5ccd6cSJohn Marino
245*ef5ccd6cSJohn Marino xfree (proc);
246*ef5ccd6cSJohn Marino return;
247*ef5ccd6cSJohn Marino }
248*ef5ccd6cSJohn Marino
249*ef5ccd6cSJohn Marino proc_link = &proc->next;
250*ef5ccd6cSJohn Marino proc = *proc_link;
251*ef5ccd6cSJohn Marino }
252*ef5ccd6cSJohn Marino }
2535796c8dcSSimon Schubert
2545796c8dcSSimon Schubert /* Whether or not to print the mirrored debug registers. */
2555796c8dcSSimon Schubert static int maint_show_dr;
2565796c8dcSSimon Schubert
2575796c8dcSSimon Schubert /* Types of operations supported by i386_handle_nonaligned_watchpoint. */
2585796c8dcSSimon Schubert typedef enum { WP_INSERT, WP_REMOVE, WP_COUNT } i386_wp_op_t;
2595796c8dcSSimon Schubert
2605796c8dcSSimon Schubert /* Internal functions. */
2615796c8dcSSimon Schubert
2625796c8dcSSimon Schubert /* Return the value of a 4-bit field for DR7 suitable for watching a
2635796c8dcSSimon Schubert region of LEN bytes for accesses of type TYPE. LEN is assumed to
2645796c8dcSSimon Schubert have the value of 1, 2, or 4. */
2655796c8dcSSimon Schubert static unsigned i386_length_and_rw_bits (int len, enum target_hw_bp_type type);
2665796c8dcSSimon Schubert
2675796c8dcSSimon Schubert /* Insert a watchpoint at address ADDR, which is assumed to be aligned
2685796c8dcSSimon Schubert according to the length of the region to watch. LEN_RW_BITS is the
2695796c8dcSSimon Schubert value of the bit-field from DR7 which describes the length and
2705796c8dcSSimon Schubert access type of the region to be watched by this watchpoint. Return
2715796c8dcSSimon Schubert 0 on success, -1 on failure. */
272a45ae5f8SJohn Marino static int i386_insert_aligned_watchpoint (struct i386_debug_reg_state *state,
273a45ae5f8SJohn Marino CORE_ADDR addr,
2745796c8dcSSimon Schubert unsigned len_rw_bits);
2755796c8dcSSimon Schubert
2765796c8dcSSimon Schubert /* Remove a watchpoint at address ADDR, which is assumed to be aligned
2775796c8dcSSimon Schubert according to the length of the region to watch. LEN_RW_BITS is the
2785796c8dcSSimon Schubert value of the bits from DR7 which describes the length and access
2795796c8dcSSimon Schubert type of the region watched by this watchpoint. Return 0 on
2805796c8dcSSimon Schubert success, -1 on failure. */
281a45ae5f8SJohn Marino static int i386_remove_aligned_watchpoint (struct i386_debug_reg_state *state,
282a45ae5f8SJohn Marino CORE_ADDR addr,
2835796c8dcSSimon Schubert unsigned len_rw_bits);
2845796c8dcSSimon Schubert
2855796c8dcSSimon Schubert /* Insert or remove a (possibly non-aligned) watchpoint, or count the
2865796c8dcSSimon Schubert number of debug registers required to watch a region at address
2875796c8dcSSimon Schubert ADDR whose length is LEN for accesses of type TYPE. Return 0 on
2885796c8dcSSimon Schubert successful insertion or removal, a positive number when queried
2895796c8dcSSimon Schubert about the number of registers, or -1 on failure. If WHAT is not a
2905796c8dcSSimon Schubert valid value, bombs through internal_error. */
291a45ae5f8SJohn Marino static int i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state,
292a45ae5f8SJohn Marino i386_wp_op_t what,
2935796c8dcSSimon Schubert CORE_ADDR addr, int len,
2945796c8dcSSimon Schubert enum target_hw_bp_type type);
2955796c8dcSSimon Schubert
2965796c8dcSSimon Schubert /* Implementation. */
2975796c8dcSSimon Schubert
2985796c8dcSSimon Schubert /* Clear the reference counts and forget everything we knew about the
2995796c8dcSSimon Schubert debug registers. */
3005796c8dcSSimon Schubert
3015796c8dcSSimon Schubert void
i386_cleanup_dregs(void)3025796c8dcSSimon Schubert i386_cleanup_dregs (void)
3035796c8dcSSimon Schubert {
304*ef5ccd6cSJohn Marino /* Starting from scratch has the same effect. */
305*ef5ccd6cSJohn Marino i386_forget_process (ptid_get_pid (inferior_ptid));
3065796c8dcSSimon Schubert }
3075796c8dcSSimon Schubert
3085796c8dcSSimon Schubert /* Print the values of the mirrored debug registers. This is called
3095796c8dcSSimon Schubert when maint_show_dr is non-zero. To set that up, type "maint
3105796c8dcSSimon Schubert show-debug-regs" at GDB's prompt. */
3115796c8dcSSimon Schubert
3125796c8dcSSimon Schubert static void
i386_show_dr(struct i386_debug_reg_state * state,const char * func,CORE_ADDR addr,int len,enum target_hw_bp_type type)313a45ae5f8SJohn Marino i386_show_dr (struct i386_debug_reg_state *state,
314a45ae5f8SJohn Marino const char *func, CORE_ADDR addr,
3155796c8dcSSimon Schubert int len, enum target_hw_bp_type type)
3165796c8dcSSimon Schubert {
317*ef5ccd6cSJohn Marino int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
3185796c8dcSSimon Schubert int i;
3195796c8dcSSimon Schubert
3205796c8dcSSimon Schubert puts_unfiltered (func);
3215796c8dcSSimon Schubert if (addr || len)
3225796c8dcSSimon Schubert printf_unfiltered (" (addr=%lx, len=%d, type=%s)",
3235796c8dcSSimon Schubert /* This code is for ia32, so casting CORE_ADDR
3245796c8dcSSimon Schubert to unsigned long should be okay. */
3255796c8dcSSimon Schubert (unsigned long)addr, len,
3265796c8dcSSimon Schubert type == hw_write ? "data-write"
3275796c8dcSSimon Schubert : (type == hw_read ? "data-read"
3285796c8dcSSimon Schubert : (type == hw_access ? "data-read/write"
3295796c8dcSSimon Schubert : (type == hw_execute ? "instruction-execute"
3305796c8dcSSimon Schubert /* FIXME: if/when I/O read/write
3315796c8dcSSimon Schubert watchpoints are supported, add them
3325796c8dcSSimon Schubert here. */
3335796c8dcSSimon Schubert : "??unknown??"))));
3345796c8dcSSimon Schubert puts_unfiltered (":\n");
3355796c8dcSSimon Schubert printf_unfiltered ("\tCONTROL (DR7): %s STATUS (DR6): %s\n",
336a45ae5f8SJohn Marino phex (state->dr_control_mirror, 8),
337a45ae5f8SJohn Marino phex (state->dr_status_mirror, 8));
3385796c8dcSSimon Schubert ALL_DEBUG_REGISTERS(i)
3395796c8dcSSimon Schubert {
3405796c8dcSSimon Schubert printf_unfiltered ("\
3415796c8dcSSimon Schubert \tDR%d: addr=0x%s, ref.count=%d DR%d: addr=0x%s, ref.count=%d\n",
342a45ae5f8SJohn Marino i, phex (state->dr_mirror[i], addr_size),
343a45ae5f8SJohn Marino state->dr_ref_count[i],
344a45ae5f8SJohn Marino i + 1, phex (state->dr_mirror[i + 1], addr_size),
345a45ae5f8SJohn Marino state->dr_ref_count[i+1]);
3465796c8dcSSimon Schubert i++;
3475796c8dcSSimon Schubert }
3485796c8dcSSimon Schubert }
3495796c8dcSSimon Schubert
3505796c8dcSSimon Schubert /* Return the value of a 4-bit field for DR7 suitable for watching a
3515796c8dcSSimon Schubert region of LEN bytes for accesses of type TYPE. LEN is assumed to
3525796c8dcSSimon Schubert have the value of 1, 2, or 4. */
3535796c8dcSSimon Schubert
3545796c8dcSSimon Schubert static unsigned
i386_length_and_rw_bits(int len,enum target_hw_bp_type type)3555796c8dcSSimon Schubert i386_length_and_rw_bits (int len, enum target_hw_bp_type type)
3565796c8dcSSimon Schubert {
3575796c8dcSSimon Schubert unsigned rw;
3585796c8dcSSimon Schubert
3595796c8dcSSimon Schubert switch (type)
3605796c8dcSSimon Schubert {
3615796c8dcSSimon Schubert case hw_execute:
3625796c8dcSSimon Schubert rw = DR_RW_EXECUTE;
3635796c8dcSSimon Schubert break;
3645796c8dcSSimon Schubert case hw_write:
3655796c8dcSSimon Schubert rw = DR_RW_WRITE;
3665796c8dcSSimon Schubert break;
3675796c8dcSSimon Schubert case hw_read:
368cf7f2e2dSJohn Marino internal_error (__FILE__, __LINE__,
369c50c785cSJohn Marino _("The i386 doesn't support "
370c50c785cSJohn Marino "data-read watchpoints.\n"));
3715796c8dcSSimon Schubert case hw_access:
3725796c8dcSSimon Schubert rw = DR_RW_READ;
3735796c8dcSSimon Schubert break;
3745796c8dcSSimon Schubert #if 0
3755796c8dcSSimon Schubert /* Not yet supported. */
3765796c8dcSSimon Schubert case hw_io_access:
3775796c8dcSSimon Schubert rw = DR_RW_IORW;
3785796c8dcSSimon Schubert break;
3795796c8dcSSimon Schubert #endif
3805796c8dcSSimon Schubert default:
3815796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("\
3825796c8dcSSimon Schubert Invalid hardware breakpoint type %d in i386_length_and_rw_bits.\n"),
3835796c8dcSSimon Schubert (int) type);
3845796c8dcSSimon Schubert }
3855796c8dcSSimon Schubert
3865796c8dcSSimon Schubert switch (len)
3875796c8dcSSimon Schubert {
3885796c8dcSSimon Schubert case 1:
3895796c8dcSSimon Schubert return (DR_LEN_1 | rw);
3905796c8dcSSimon Schubert case 2:
3915796c8dcSSimon Schubert return (DR_LEN_2 | rw);
3925796c8dcSSimon Schubert case 4:
3935796c8dcSSimon Schubert return (DR_LEN_4 | rw);
3945796c8dcSSimon Schubert case 8:
3955796c8dcSSimon Schubert if (TARGET_HAS_DR_LEN_8)
3965796c8dcSSimon Schubert return (DR_LEN_8 | rw);
397c50c785cSJohn Marino /* ELSE FALL THROUGH */
3985796c8dcSSimon Schubert default:
3995796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("\
4005796c8dcSSimon Schubert Invalid hardware breakpoint length %d in i386_length_and_rw_bits.\n"), len);
4015796c8dcSSimon Schubert }
4025796c8dcSSimon Schubert }
4035796c8dcSSimon Schubert
4045796c8dcSSimon Schubert /* Insert a watchpoint at address ADDR, which is assumed to be aligned
4055796c8dcSSimon Schubert according to the length of the region to watch. LEN_RW_BITS is the
4065796c8dcSSimon Schubert value of the bits from DR7 which describes the length and access
4075796c8dcSSimon Schubert type of the region to be watched by this watchpoint. Return 0 on
4085796c8dcSSimon Schubert success, -1 on failure. */
4095796c8dcSSimon Schubert
4105796c8dcSSimon Schubert static int
i386_insert_aligned_watchpoint(struct i386_debug_reg_state * state,CORE_ADDR addr,unsigned len_rw_bits)411a45ae5f8SJohn Marino i386_insert_aligned_watchpoint (struct i386_debug_reg_state *state,
412a45ae5f8SJohn Marino CORE_ADDR addr, unsigned len_rw_bits)
4135796c8dcSSimon Schubert {
4145796c8dcSSimon Schubert int i;
4155796c8dcSSimon Schubert
4165796c8dcSSimon Schubert if (!i386_dr_low.set_addr || !i386_dr_low.set_control)
4175796c8dcSSimon Schubert return -1;
4185796c8dcSSimon Schubert
4195796c8dcSSimon Schubert /* First, look for an occupied debug register with the same address
4205796c8dcSSimon Schubert and the same RW and LEN definitions. If we find one, we can
4215796c8dcSSimon Schubert reuse it for this watchpoint as well (and save a register). */
4225796c8dcSSimon Schubert ALL_DEBUG_REGISTERS(i)
4235796c8dcSSimon Schubert {
424a45ae5f8SJohn Marino if (!I386_DR_VACANT (state, i)
425a45ae5f8SJohn Marino && state->dr_mirror[i] == addr
426a45ae5f8SJohn Marino && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
4275796c8dcSSimon Schubert {
428a45ae5f8SJohn Marino state->dr_ref_count[i]++;
4295796c8dcSSimon Schubert return 0;
4305796c8dcSSimon Schubert }
4315796c8dcSSimon Schubert }
4325796c8dcSSimon Schubert
4335796c8dcSSimon Schubert /* Next, look for a vacant debug register. */
4345796c8dcSSimon Schubert ALL_DEBUG_REGISTERS(i)
4355796c8dcSSimon Schubert {
436a45ae5f8SJohn Marino if (I386_DR_VACANT (state, i))
4375796c8dcSSimon Schubert break;
4385796c8dcSSimon Schubert }
4395796c8dcSSimon Schubert
4405796c8dcSSimon Schubert /* No more debug registers! */
4415796c8dcSSimon Schubert if (i >= DR_NADDR)
4425796c8dcSSimon Schubert return -1;
4435796c8dcSSimon Schubert
4445796c8dcSSimon Schubert /* Now set up the register I to watch our region. */
4455796c8dcSSimon Schubert
4465796c8dcSSimon Schubert /* Record the info in our local mirrored array. */
447a45ae5f8SJohn Marino state->dr_mirror[i] = addr;
448a45ae5f8SJohn Marino state->dr_ref_count[i] = 1;
449a45ae5f8SJohn Marino I386_DR_SET_RW_LEN (state, i, len_rw_bits);
4505796c8dcSSimon Schubert /* Note: we only enable the watchpoint locally, i.e. in the current
4515796c8dcSSimon Schubert task. Currently, no i386 target allows or supports global
4525796c8dcSSimon Schubert watchpoints; however, if any target would want that in the
4535796c8dcSSimon Schubert future, GDB should probably provide a command to control whether
4545796c8dcSSimon Schubert to enable watchpoints globally or locally, and the code below
4555796c8dcSSimon Schubert should use global or local enable and slow-down flags as
4565796c8dcSSimon Schubert appropriate. */
457a45ae5f8SJohn Marino I386_DR_LOCAL_ENABLE (state, i);
458a45ae5f8SJohn Marino state->dr_control_mirror |= DR_LOCAL_SLOWDOWN;
459a45ae5f8SJohn Marino state->dr_control_mirror &= I386_DR_CONTROL_MASK;
460cf7f2e2dSJohn Marino
4615796c8dcSSimon Schubert return 0;
4625796c8dcSSimon Schubert }
4635796c8dcSSimon Schubert
4645796c8dcSSimon Schubert /* Remove a watchpoint at address ADDR, which is assumed to be aligned
4655796c8dcSSimon Schubert according to the length of the region to watch. LEN_RW_BITS is the
4665796c8dcSSimon Schubert value of the bits from DR7 which describes the length and access
4675796c8dcSSimon Schubert type of the region watched by this watchpoint. Return 0 on
4685796c8dcSSimon Schubert success, -1 on failure. */
4695796c8dcSSimon Schubert
4705796c8dcSSimon Schubert static int
i386_remove_aligned_watchpoint(struct i386_debug_reg_state * state,CORE_ADDR addr,unsigned len_rw_bits)471a45ae5f8SJohn Marino i386_remove_aligned_watchpoint (struct i386_debug_reg_state *state,
472a45ae5f8SJohn Marino CORE_ADDR addr, unsigned len_rw_bits)
4735796c8dcSSimon Schubert {
4745796c8dcSSimon Schubert int i, retval = -1;
4755796c8dcSSimon Schubert
4765796c8dcSSimon Schubert ALL_DEBUG_REGISTERS(i)
4775796c8dcSSimon Schubert {
478a45ae5f8SJohn Marino if (!I386_DR_VACANT (state, i)
479a45ae5f8SJohn Marino && state->dr_mirror[i] == addr
480a45ae5f8SJohn Marino && I386_DR_GET_RW_LEN (state->dr_control_mirror, i) == len_rw_bits)
4815796c8dcSSimon Schubert {
482a45ae5f8SJohn Marino if (--state->dr_ref_count[i] == 0) /* no longer in use? */
4835796c8dcSSimon Schubert {
4845796c8dcSSimon Schubert /* Reset our mirror. */
485a45ae5f8SJohn Marino state->dr_mirror[i] = 0;
486a45ae5f8SJohn Marino I386_DR_DISABLE (state, i);
4875796c8dcSSimon Schubert }
4885796c8dcSSimon Schubert retval = 0;
4895796c8dcSSimon Schubert }
4905796c8dcSSimon Schubert }
4915796c8dcSSimon Schubert
4925796c8dcSSimon Schubert return retval;
4935796c8dcSSimon Schubert }
4945796c8dcSSimon Schubert
4955796c8dcSSimon Schubert /* Insert or remove a (possibly non-aligned) watchpoint, or count the
4965796c8dcSSimon Schubert number of debug registers required to watch a region at address
4975796c8dcSSimon Schubert ADDR whose length is LEN for accesses of type TYPE. Return 0 on
4985796c8dcSSimon Schubert successful insertion or removal, a positive number when queried
4995796c8dcSSimon Schubert about the number of registers, or -1 on failure. If WHAT is not a
5005796c8dcSSimon Schubert valid value, bombs through internal_error. */
5015796c8dcSSimon Schubert
5025796c8dcSSimon Schubert 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)503a45ae5f8SJohn Marino i386_handle_nonaligned_watchpoint (struct i386_debug_reg_state *state,
504a45ae5f8SJohn Marino i386_wp_op_t what, CORE_ADDR addr, int len,
5055796c8dcSSimon Schubert enum target_hw_bp_type type)
5065796c8dcSSimon Schubert {
507a45ae5f8SJohn Marino int retval = 0;
5085796c8dcSSimon Schubert int max_wp_len = TARGET_HAS_DR_LEN_8 ? 8 : 4;
5095796c8dcSSimon Schubert
5105796c8dcSSimon Schubert static int size_try_array[8][8] =
5115796c8dcSSimon Schubert {
5125796c8dcSSimon Schubert {1, 1, 1, 1, 1, 1, 1, 1}, /* Trying size one. */
5135796c8dcSSimon Schubert {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size two. */
5145796c8dcSSimon Schubert {2, 1, 2, 1, 2, 1, 2, 1}, /* Trying size three. */
5155796c8dcSSimon Schubert {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size four. */
5165796c8dcSSimon Schubert {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size five. */
5175796c8dcSSimon Schubert {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size six. */
5185796c8dcSSimon Schubert {4, 1, 2, 1, 4, 1, 2, 1}, /* Trying size seven. */
5195796c8dcSSimon Schubert {8, 1, 2, 1, 4, 1, 2, 1}, /* Trying size eight. */
5205796c8dcSSimon Schubert };
5215796c8dcSSimon Schubert
5225796c8dcSSimon Schubert while (len > 0)
5235796c8dcSSimon Schubert {
5245796c8dcSSimon Schubert int align = addr % max_wp_len;
5255796c8dcSSimon Schubert /* Four (eight on AMD64) is the maximum length a debug register
5265796c8dcSSimon Schubert can watch. */
5275796c8dcSSimon Schubert int try = (len > max_wp_len ? (max_wp_len - 1) : len - 1);
5285796c8dcSSimon Schubert int size = size_try_array[try][align];
5295796c8dcSSimon Schubert
5305796c8dcSSimon Schubert if (what == WP_COUNT)
5315796c8dcSSimon Schubert {
5325796c8dcSSimon Schubert /* size_try_array[] is defined such that each iteration
5335796c8dcSSimon Schubert through the loop is guaranteed to produce an address and a
5345796c8dcSSimon Schubert size that can be watched with a single debug register.
5355796c8dcSSimon Schubert Thus, for counting the registers required to watch a
5365796c8dcSSimon Schubert region, we simply need to increment the count on each
5375796c8dcSSimon Schubert iteration. */
5385796c8dcSSimon Schubert retval++;
5395796c8dcSSimon Schubert }
5405796c8dcSSimon Schubert else
5415796c8dcSSimon Schubert {
5425796c8dcSSimon Schubert unsigned len_rw = i386_length_and_rw_bits (size, type);
5435796c8dcSSimon Schubert
5445796c8dcSSimon Schubert if (what == WP_INSERT)
545a45ae5f8SJohn Marino retval = i386_insert_aligned_watchpoint (state, addr, len_rw);
5465796c8dcSSimon Schubert else if (what == WP_REMOVE)
547a45ae5f8SJohn Marino retval = i386_remove_aligned_watchpoint (state, addr, len_rw);
5485796c8dcSSimon Schubert else
5495796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("\
5505796c8dcSSimon Schubert Invalid value %d of operation in i386_handle_nonaligned_watchpoint.\n"),
5515796c8dcSSimon Schubert (int)what);
552a45ae5f8SJohn Marino if (retval)
553a45ae5f8SJohn Marino break;
5545796c8dcSSimon Schubert }
5555796c8dcSSimon Schubert
5565796c8dcSSimon Schubert addr += size;
5575796c8dcSSimon Schubert len -= size;
5585796c8dcSSimon Schubert }
5595796c8dcSSimon Schubert
5605796c8dcSSimon Schubert return retval;
5615796c8dcSSimon Schubert }
5625796c8dcSSimon Schubert
563a45ae5f8SJohn Marino /* Update the inferior's debug registers with the new debug registers
564a45ae5f8SJohn Marino state, in NEW_STATE, and then update our local mirror to match. */
565a45ae5f8SJohn Marino
566a45ae5f8SJohn Marino static void
i386_update_inferior_debug_regs(struct i386_debug_reg_state * new_state)567a45ae5f8SJohn Marino i386_update_inferior_debug_regs (struct i386_debug_reg_state *new_state)
568a45ae5f8SJohn Marino {
569*ef5ccd6cSJohn Marino struct i386_debug_reg_state *state
570*ef5ccd6cSJohn Marino = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
571a45ae5f8SJohn Marino int i;
572a45ae5f8SJohn Marino
573a45ae5f8SJohn Marino ALL_DEBUG_REGISTERS (i)
574a45ae5f8SJohn Marino {
575*ef5ccd6cSJohn Marino if (I386_DR_VACANT (new_state, i) != I386_DR_VACANT (state, i))
576a45ae5f8SJohn Marino i386_dr_low.set_addr (i, new_state->dr_mirror[i]);
577a45ae5f8SJohn Marino else
578*ef5ccd6cSJohn Marino gdb_assert (new_state->dr_mirror[i] == state->dr_mirror[i]);
579a45ae5f8SJohn Marino }
580a45ae5f8SJohn Marino
581*ef5ccd6cSJohn Marino if (new_state->dr_control_mirror != state->dr_control_mirror)
582a45ae5f8SJohn Marino i386_dr_low.set_control (new_state->dr_control_mirror);
583a45ae5f8SJohn Marino
584*ef5ccd6cSJohn Marino *state = *new_state;
585a45ae5f8SJohn Marino }
586a45ae5f8SJohn Marino
5875796c8dcSSimon Schubert /* Insert a watchpoint to watch a memory region which starts at
5885796c8dcSSimon Schubert address ADDR and whose length is LEN bytes. Watch memory accesses
5895796c8dcSSimon Schubert of the type TYPE. Return 0 on success, -1 on failure. */
5905796c8dcSSimon Schubert
5915796c8dcSSimon Schubert static int
i386_insert_watchpoint(CORE_ADDR addr,int len,int type,struct expression * cond)592cf7f2e2dSJohn Marino i386_insert_watchpoint (CORE_ADDR addr, int len, int type,
593cf7f2e2dSJohn Marino struct expression *cond)
5945796c8dcSSimon Schubert {
595*ef5ccd6cSJohn Marino struct i386_debug_reg_state *state
596*ef5ccd6cSJohn Marino = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
5975796c8dcSSimon Schubert int retval;
598a45ae5f8SJohn Marino /* Work on a local copy of the debug registers, and on success,
599a45ae5f8SJohn Marino commit the change back to the inferior. */
600*ef5ccd6cSJohn Marino struct i386_debug_reg_state local_state = *state;
6015796c8dcSSimon Schubert
602cf7f2e2dSJohn Marino if (type == hw_read)
603cf7f2e2dSJohn Marino return 1; /* unsupported */
604cf7f2e2dSJohn Marino
6055796c8dcSSimon Schubert if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8))
6065796c8dcSSimon Schubert || addr % len != 0)
607a45ae5f8SJohn Marino retval = i386_handle_nonaligned_watchpoint (&local_state,
608a45ae5f8SJohn Marino WP_INSERT, addr, len, type);
6095796c8dcSSimon Schubert else
6105796c8dcSSimon Schubert {
6115796c8dcSSimon Schubert unsigned len_rw = i386_length_and_rw_bits (len, type);
6125796c8dcSSimon Schubert
613a45ae5f8SJohn Marino retval = i386_insert_aligned_watchpoint (&local_state,
614a45ae5f8SJohn Marino addr, len_rw);
6155796c8dcSSimon Schubert }
6165796c8dcSSimon Schubert
617a45ae5f8SJohn Marino if (retval == 0)
618a45ae5f8SJohn Marino i386_update_inferior_debug_regs (&local_state);
619a45ae5f8SJohn Marino
6205796c8dcSSimon Schubert if (maint_show_dr)
621*ef5ccd6cSJohn Marino i386_show_dr (state, "insert_watchpoint", addr, len, type);
6225796c8dcSSimon Schubert
6235796c8dcSSimon Schubert return retval;
6245796c8dcSSimon Schubert }
6255796c8dcSSimon Schubert
6265796c8dcSSimon Schubert /* Remove a watchpoint that watched the memory region which starts at
6275796c8dcSSimon Schubert address ADDR, whose length is LEN bytes, and for accesses of the
6285796c8dcSSimon Schubert type TYPE. Return 0 on success, -1 on failure. */
6295796c8dcSSimon Schubert static int
i386_remove_watchpoint(CORE_ADDR addr,int len,int type,struct expression * cond)630cf7f2e2dSJohn Marino i386_remove_watchpoint (CORE_ADDR addr, int len, int type,
631cf7f2e2dSJohn Marino struct expression *cond)
6325796c8dcSSimon Schubert {
633*ef5ccd6cSJohn Marino struct i386_debug_reg_state *state
634*ef5ccd6cSJohn Marino = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
6355796c8dcSSimon Schubert int retval;
636a45ae5f8SJohn Marino /* Work on a local copy of the debug registers, and on success,
637a45ae5f8SJohn Marino commit the change back to the inferior. */
638*ef5ccd6cSJohn Marino struct i386_debug_reg_state local_state = *state;
6395796c8dcSSimon Schubert
6405796c8dcSSimon Schubert if (((len != 1 && len !=2 && len !=4) && !(TARGET_HAS_DR_LEN_8 && len == 8))
6415796c8dcSSimon Schubert || addr % len != 0)
642a45ae5f8SJohn Marino retval = i386_handle_nonaligned_watchpoint (&local_state,
643a45ae5f8SJohn Marino WP_REMOVE, addr, len, type);
6445796c8dcSSimon Schubert else
6455796c8dcSSimon Schubert {
6465796c8dcSSimon Schubert unsigned len_rw = i386_length_and_rw_bits (len, type);
6475796c8dcSSimon Schubert
648a45ae5f8SJohn Marino retval = i386_remove_aligned_watchpoint (&local_state,
649a45ae5f8SJohn Marino addr, len_rw);
6505796c8dcSSimon Schubert }
6515796c8dcSSimon Schubert
652a45ae5f8SJohn Marino if (retval == 0)
653a45ae5f8SJohn Marino i386_update_inferior_debug_regs (&local_state);
654a45ae5f8SJohn Marino
6555796c8dcSSimon Schubert if (maint_show_dr)
656*ef5ccd6cSJohn Marino i386_show_dr (state, "remove_watchpoint", addr, len, type);
6575796c8dcSSimon Schubert
6585796c8dcSSimon Schubert return retval;
6595796c8dcSSimon Schubert }
6605796c8dcSSimon Schubert
6615796c8dcSSimon Schubert /* Return non-zero if we can watch a memory region that starts at
6625796c8dcSSimon Schubert address ADDR and whose length is LEN bytes. */
6635796c8dcSSimon Schubert
6645796c8dcSSimon Schubert static int
i386_region_ok_for_watchpoint(CORE_ADDR addr,int len)6655796c8dcSSimon Schubert i386_region_ok_for_watchpoint (CORE_ADDR addr, int len)
6665796c8dcSSimon Schubert {
667*ef5ccd6cSJohn Marino struct i386_debug_reg_state *state
668*ef5ccd6cSJohn Marino = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
6695796c8dcSSimon Schubert int nregs;
6705796c8dcSSimon Schubert
6715796c8dcSSimon Schubert /* Compute how many aligned watchpoints we would need to cover this
6725796c8dcSSimon Schubert region. */
673*ef5ccd6cSJohn Marino nregs = i386_handle_nonaligned_watchpoint (state,
674a45ae5f8SJohn Marino WP_COUNT, addr, len, hw_write);
6755796c8dcSSimon Schubert return nregs <= DR_NADDR ? 1 : 0;
6765796c8dcSSimon Schubert }
6775796c8dcSSimon Schubert
6785796c8dcSSimon Schubert /* If the inferior has some watchpoint that triggered, set the
6795796c8dcSSimon Schubert address associated with that watchpoint and return non-zero.
6805796c8dcSSimon Schubert Otherwise, return zero. */
6815796c8dcSSimon Schubert
6825796c8dcSSimon Schubert static int
i386_stopped_data_address(struct target_ops * ops,CORE_ADDR * addr_p)6835796c8dcSSimon Schubert i386_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
6845796c8dcSSimon Schubert {
685*ef5ccd6cSJohn Marino struct i386_debug_reg_state *state
686*ef5ccd6cSJohn Marino = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
6875796c8dcSSimon Schubert CORE_ADDR addr = 0;
6885796c8dcSSimon Schubert int i;
6895796c8dcSSimon Schubert int rc = 0;
690*ef5ccd6cSJohn Marino /* The current thread's DR_STATUS. We always need to read this to
691*ef5ccd6cSJohn Marino check whether some watchpoint caused the trap. */
692a45ae5f8SJohn Marino unsigned status;
693*ef5ccd6cSJohn Marino /* We need DR_CONTROL as well, but only iff DR_STATUS indicates a
694*ef5ccd6cSJohn Marino data breakpoint trap. Only fetch it when necessary, to avoid an
695*ef5ccd6cSJohn Marino unnecessary extra syscall when no watchpoint triggered. */
696*ef5ccd6cSJohn Marino int control_p = 0;
697*ef5ccd6cSJohn Marino unsigned control = 0;
6985796c8dcSSimon Schubert
699*ef5ccd6cSJohn Marino /* In non-stop/async, threads can be running while we change the
700*ef5ccd6cSJohn Marino STATE (and friends). Say, we set a watchpoint, and let threads
701*ef5ccd6cSJohn Marino resume. Now, say you delete the watchpoint, or add/remove
702*ef5ccd6cSJohn Marino watchpoints such that STATE changes while threads are running.
703*ef5ccd6cSJohn Marino On targets that support non-stop, inserting/deleting watchpoints
704*ef5ccd6cSJohn Marino updates the STATE only. It does not update the real thread's
705*ef5ccd6cSJohn Marino debug registers; that's only done prior to resume. Instead, if
706*ef5ccd6cSJohn Marino threads are running when the mirror changes, a temporary and
707*ef5ccd6cSJohn Marino transparent stop on all threads is forced so they can get their
708*ef5ccd6cSJohn Marino copy of the debug registers updated on re-resume. Now, say,
709*ef5ccd6cSJohn Marino a thread hit a watchpoint before having been updated with the new
710*ef5ccd6cSJohn Marino STATE contents, and we haven't yet handled the corresponding
711*ef5ccd6cSJohn Marino SIGTRAP. If we trusted STATE below, we'd mistake the real
712*ef5ccd6cSJohn Marino trapped address (from the last time we had updated debug
713*ef5ccd6cSJohn Marino registers in the thread) with whatever was currently in STATE.
714*ef5ccd6cSJohn Marino So to fix this, STATE always represents intention, what we _want_
715*ef5ccd6cSJohn Marino threads to have in debug registers. To get at the address and
716*ef5ccd6cSJohn Marino cause of the trap, we need to read the state the thread still has
717*ef5ccd6cSJohn Marino in its debug registers.
718*ef5ccd6cSJohn Marino
719*ef5ccd6cSJohn Marino In sum, always get the current debug register values the current
720*ef5ccd6cSJohn Marino thread has, instead of trusting the global mirror. If the thread
721*ef5ccd6cSJohn Marino was running when we last changed watchpoints, the mirror no
722*ef5ccd6cSJohn Marino longer represents what was set in this thread's debug
723*ef5ccd6cSJohn Marino registers. */
724*ef5ccd6cSJohn Marino status = i386_dr_low.get_status ();
7255796c8dcSSimon Schubert
7265796c8dcSSimon Schubert ALL_DEBUG_REGISTERS(i)
7275796c8dcSSimon Schubert {
728*ef5ccd6cSJohn Marino if (!I386_DR_WATCH_HIT (status, i))
729*ef5ccd6cSJohn Marino continue;
730*ef5ccd6cSJohn Marino
731*ef5ccd6cSJohn Marino if (!control_p)
7325796c8dcSSimon Schubert {
733*ef5ccd6cSJohn Marino control = i386_dr_low.get_control ();
734*ef5ccd6cSJohn Marino control_p = 1;
735*ef5ccd6cSJohn Marino }
736*ef5ccd6cSJohn Marino
737*ef5ccd6cSJohn Marino /* This second condition makes sure DRi is set up for a data
738*ef5ccd6cSJohn Marino watchpoint, not a hardware breakpoint. The reason is that
739*ef5ccd6cSJohn Marino GDB doesn't call the target_stopped_data_address method
740*ef5ccd6cSJohn Marino except for data watchpoints. In other words, I'm being
741*ef5ccd6cSJohn Marino paranoiac. */
742*ef5ccd6cSJohn Marino if (I386_DR_GET_RW_LEN (control, i) != 0)
743*ef5ccd6cSJohn Marino {
744*ef5ccd6cSJohn Marino addr = i386_dr_low.get_addr (i);
7455796c8dcSSimon Schubert rc = 1;
7465796c8dcSSimon Schubert if (maint_show_dr)
747*ef5ccd6cSJohn Marino i386_show_dr (state, "watchpoint_hit", addr, -1, hw_write);
7485796c8dcSSimon Schubert }
7495796c8dcSSimon Schubert }
7505796c8dcSSimon Schubert if (maint_show_dr && addr == 0)
751*ef5ccd6cSJohn Marino i386_show_dr (state, "stopped_data_addr", 0, 0, hw_write);
7525796c8dcSSimon Schubert
7535796c8dcSSimon Schubert if (rc)
7545796c8dcSSimon Schubert *addr_p = addr;
7555796c8dcSSimon Schubert return rc;
7565796c8dcSSimon Schubert }
7575796c8dcSSimon Schubert
7585796c8dcSSimon Schubert static int
i386_stopped_by_watchpoint(void)7595796c8dcSSimon Schubert i386_stopped_by_watchpoint (void)
7605796c8dcSSimon Schubert {
7615796c8dcSSimon Schubert CORE_ADDR addr = 0;
7625796c8dcSSimon Schubert return i386_stopped_data_address (¤t_target, &addr);
7635796c8dcSSimon Schubert }
7645796c8dcSSimon Schubert
7655796c8dcSSimon Schubert /* Insert a hardware-assisted breakpoint at BP_TGT->placed_address.
7665796c8dcSSimon Schubert Return 0 on success, EBUSY on failure. */
7675796c8dcSSimon Schubert static int
i386_insert_hw_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)7685796c8dcSSimon Schubert i386_insert_hw_breakpoint (struct gdbarch *gdbarch,
7695796c8dcSSimon Schubert struct bp_target_info *bp_tgt)
7705796c8dcSSimon Schubert {
771*ef5ccd6cSJohn Marino struct i386_debug_reg_state *state
772*ef5ccd6cSJohn Marino = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
7735796c8dcSSimon Schubert unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
7745796c8dcSSimon Schubert CORE_ADDR addr = bp_tgt->placed_address;
775a45ae5f8SJohn Marino /* Work on a local copy of the debug registers, and on success,
776a45ae5f8SJohn Marino commit the change back to the inferior. */
777*ef5ccd6cSJohn Marino struct i386_debug_reg_state local_state = *state;
778a45ae5f8SJohn Marino int retval = i386_insert_aligned_watchpoint (&local_state,
779a45ae5f8SJohn Marino addr, len_rw) ? EBUSY : 0;
780a45ae5f8SJohn Marino
781a45ae5f8SJohn Marino if (retval == 0)
782a45ae5f8SJohn Marino i386_update_inferior_debug_regs (&local_state);
7835796c8dcSSimon Schubert
7845796c8dcSSimon Schubert if (maint_show_dr)
785*ef5ccd6cSJohn Marino i386_show_dr (state, "insert_hwbp", addr, 1, hw_execute);
7865796c8dcSSimon Schubert
7875796c8dcSSimon Schubert return retval;
7885796c8dcSSimon Schubert }
7895796c8dcSSimon Schubert
7905796c8dcSSimon Schubert /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
7915796c8dcSSimon Schubert Return 0 on success, -1 on failure. */
7925796c8dcSSimon Schubert
7935796c8dcSSimon Schubert static int
i386_remove_hw_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)7945796c8dcSSimon Schubert i386_remove_hw_breakpoint (struct gdbarch *gdbarch,
7955796c8dcSSimon Schubert struct bp_target_info *bp_tgt)
7965796c8dcSSimon Schubert {
797*ef5ccd6cSJohn Marino struct i386_debug_reg_state *state
798*ef5ccd6cSJohn Marino = i386_debug_reg_state (ptid_get_pid (inferior_ptid));
7995796c8dcSSimon Schubert unsigned len_rw = i386_length_and_rw_bits (1, hw_execute);
8005796c8dcSSimon Schubert CORE_ADDR addr = bp_tgt->placed_address;
801a45ae5f8SJohn Marino /* Work on a local copy of the debug registers, and on success,
802a45ae5f8SJohn Marino commit the change back to the inferior. */
803*ef5ccd6cSJohn Marino struct i386_debug_reg_state local_state = *state;
804a45ae5f8SJohn Marino int retval = i386_remove_aligned_watchpoint (&local_state,
805a45ae5f8SJohn Marino addr, len_rw);
806a45ae5f8SJohn Marino
807a45ae5f8SJohn Marino if (retval == 0)
808a45ae5f8SJohn Marino i386_update_inferior_debug_regs (&local_state);
8095796c8dcSSimon Schubert
8105796c8dcSSimon Schubert if (maint_show_dr)
811*ef5ccd6cSJohn Marino i386_show_dr (state, "remove_hwbp", addr, 1, hw_execute);
8125796c8dcSSimon Schubert
8135796c8dcSSimon Schubert return retval;
8145796c8dcSSimon Schubert }
8155796c8dcSSimon Schubert
8165796c8dcSSimon Schubert /* Returns the number of hardware watchpoints of type TYPE that we can
8175796c8dcSSimon Schubert set. Value is positive if we can set CNT watchpoints, zero if
8185796c8dcSSimon Schubert setting watchpoints of type TYPE is not supported, and negative if
8195796c8dcSSimon Schubert CNT is more than the maximum number of watchpoints of type TYPE
8205796c8dcSSimon Schubert that we can support. TYPE is one of bp_hardware_watchpoint,
8215796c8dcSSimon Schubert bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
8225796c8dcSSimon Schubert CNT is the number of such watchpoints used so far (including this
8235796c8dcSSimon Schubert one). OTHERTYPE is non-zero if other types of watchpoints are
8245796c8dcSSimon Schubert currently enabled.
8255796c8dcSSimon Schubert
8265796c8dcSSimon Schubert We always return 1 here because we don't have enough information
8275796c8dcSSimon Schubert about possible overlap of addresses that they want to watch. As an
8285796c8dcSSimon Schubert extreme example, consider the case where all the watchpoints watch
8295796c8dcSSimon Schubert the same address and the same region length: then we can handle a
8305796c8dcSSimon Schubert virtually unlimited number of watchpoints, due to debug register
8315796c8dcSSimon Schubert sharing implemented via reference counts in i386-nat.c. */
8325796c8dcSSimon Schubert
8335796c8dcSSimon Schubert static int
i386_can_use_hw_breakpoint(int type,int cnt,int othertype)8345796c8dcSSimon Schubert i386_can_use_hw_breakpoint (int type, int cnt, int othertype)
8355796c8dcSSimon Schubert {
8365796c8dcSSimon Schubert return 1;
8375796c8dcSSimon Schubert }
8385796c8dcSSimon Schubert
8395796c8dcSSimon Schubert static void
add_show_debug_regs_command(void)8405796c8dcSSimon Schubert add_show_debug_regs_command (void)
8415796c8dcSSimon Schubert {
8425796c8dcSSimon Schubert /* A maintenance command to enable printing the internal DRi mirror
8435796c8dcSSimon Schubert variables. */
8445796c8dcSSimon Schubert add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
8455796c8dcSSimon Schubert &maint_show_dr, _("\
8465796c8dcSSimon Schubert Set whether to show variables that mirror the x86 debug registers."), _("\
8475796c8dcSSimon Schubert Show whether to show variables that mirror the x86 debug registers."), _("\
8485796c8dcSSimon Schubert Use \"on\" to enable, \"off\" to disable.\n\
8495796c8dcSSimon Schubert If enabled, the debug registers values are shown when GDB inserts\n\
8505796c8dcSSimon Schubert or removes a hardware breakpoint or watchpoint, and when the inferior\n\
8515796c8dcSSimon Schubert triggers a breakpoint or watchpoint."),
8525796c8dcSSimon Schubert NULL,
8535796c8dcSSimon Schubert NULL,
8545796c8dcSSimon Schubert &maintenance_set_cmdlist,
8555796c8dcSSimon Schubert &maintenance_show_cmdlist);
8565796c8dcSSimon Schubert }
8575796c8dcSSimon Schubert
8585796c8dcSSimon Schubert /* There are only two global functions left. */
8595796c8dcSSimon Schubert
8605796c8dcSSimon Schubert void
i386_use_watchpoints(struct target_ops * t)8615796c8dcSSimon Schubert i386_use_watchpoints (struct target_ops *t)
8625796c8dcSSimon Schubert {
8635796c8dcSSimon Schubert /* After a watchpoint trap, the PC points to the instruction after the
8645796c8dcSSimon Schubert one that caused the trap. Therefore we don't need to step over it.
8655796c8dcSSimon Schubert But we do need to reset the status register to avoid another trap. */
8665796c8dcSSimon Schubert t->to_have_continuable_watchpoint = 1;
8675796c8dcSSimon Schubert
8685796c8dcSSimon Schubert t->to_can_use_hw_breakpoint = i386_can_use_hw_breakpoint;
8695796c8dcSSimon Schubert t->to_region_ok_for_hw_watchpoint = i386_region_ok_for_watchpoint;
8705796c8dcSSimon Schubert t->to_stopped_by_watchpoint = i386_stopped_by_watchpoint;
8715796c8dcSSimon Schubert t->to_stopped_data_address = i386_stopped_data_address;
8725796c8dcSSimon Schubert t->to_insert_watchpoint = i386_insert_watchpoint;
8735796c8dcSSimon Schubert t->to_remove_watchpoint = i386_remove_watchpoint;
8745796c8dcSSimon Schubert t->to_insert_hw_breakpoint = i386_insert_hw_breakpoint;
8755796c8dcSSimon Schubert t->to_remove_hw_breakpoint = i386_remove_hw_breakpoint;
8765796c8dcSSimon Schubert }
8775796c8dcSSimon Schubert
8785796c8dcSSimon Schubert void
i386_set_debug_register_length(int len)8795796c8dcSSimon Schubert i386_set_debug_register_length (int len)
8805796c8dcSSimon Schubert {
8815796c8dcSSimon Schubert /* This function should be called only once for each native target. */
8825796c8dcSSimon Schubert gdb_assert (i386_dr_low.debug_register_length == 0);
8835796c8dcSSimon Schubert gdb_assert (len == 4 || len == 8);
8845796c8dcSSimon Schubert i386_dr_low.debug_register_length = len;
8855796c8dcSSimon Schubert add_show_debug_regs_command ();
8865796c8dcSSimon Schubert }
887