1 /* Low-level debug register code for GNU/Linux x86 (i386 and x86-64).
3 Copyright (C) 1999-2024 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 "nat/gdb_ptrace.h"
22 #include "target/waitstatus.h"
23 #include "nat/x86-linux.h"
24 #include "nat/x86-dregs.h"
25 #include "nat/x86-linux-dregs.h"
27 /* Return the offset of REGNUM in the u_debugreg field of struct
31 u_debugreg_offset (int regnum
)
33 return (offsetof (struct user
, u_debugreg
)
34 + sizeof (((struct user
*) 0)->u_debugreg
[0]) * regnum
);
37 /* Get debug register REGNUM value from the LWP specified by PTID. */
40 x86_linux_dr_get (ptid_t ptid
, int regnum
)
45 gdb_assert (ptid
.lwp_p ());
49 value
= ptrace (PTRACE_PEEKUSER
, tid
, u_debugreg_offset (regnum
), 0);
51 perror_with_name (_("Couldn't read debug register"));
56 /* Set debug register REGNUM to VALUE in the LWP specified by PTID. */
59 x86_linux_dr_set (ptid_t ptid
, int regnum
, unsigned long value
)
63 gdb_assert (ptid
.lwp_p ());
67 ptrace (PTRACE_POKEUSER
, tid
, u_debugreg_offset (regnum
), value
);
69 perror_with_name (_("Couldn't write debug register"));
72 /* Callback for iterate_over_lwps. Mark that our local mirror of
73 LWP's debug registers has been changed, and cause LWP to stop if
74 it isn't already. Values are written from our local mirror to
75 the actual debug registers immediately prior to LWP resuming. */
78 update_debug_registers_callback (struct lwp_info
*lwp
)
80 lwp_set_debug_registers_changed (lwp
, 1);
82 if (!lwp_is_stopped (lwp
))
85 /* Continue the iteration. */
89 /* See nat/x86-linux-dregs.h. */
92 x86_linux_dr_get_addr (int regnum
)
94 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
96 return x86_linux_dr_get (current_lwp_ptid (), regnum
);
99 /* See nat/x86-linux-dregs.h. */
102 x86_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
104 ptid_t pid_ptid
= ptid_t (current_lwp_ptid ().pid ());
106 gdb_assert (DR_FIRSTADDR
<= regnum
&& regnum
<= DR_LASTADDR
);
108 iterate_over_lwps (pid_ptid
, update_debug_registers_callback
);
111 /* See nat/x86-linux-dregs.h. */
114 x86_linux_dr_get_control (void)
116 return x86_linux_dr_get (current_lwp_ptid (), DR_CONTROL
);
119 /* See nat/x86-linux-dregs.h. */
122 x86_linux_dr_set_control (unsigned long control
)
124 ptid_t pid_ptid
= ptid_t (current_lwp_ptid ().pid ());
126 iterate_over_lwps (pid_ptid
, update_debug_registers_callback
);
129 /* See nat/x86-linux-dregs.h. */
132 x86_linux_dr_get_status (void)
134 return x86_linux_dr_get (current_lwp_ptid (), DR_STATUS
);
137 /* See nat/x86-linux-dregs.h. */
140 x86_linux_update_debug_registers (struct lwp_info
*lwp
)
142 ptid_t ptid
= ptid_of_lwp (lwp
);
143 int clear_status
= 0;
145 gdb_assert (lwp_is_stopped (lwp
));
147 if (lwp_debug_registers_changed (lwp
))
149 struct x86_debug_reg_state
*state
150 = x86_debug_reg_state (ptid
.pid ());
153 /* Prior to Linux kernel 2.6.33 commit
154 72f674d203cd230426437cdcf7dd6f681dad8b0d, setting DR0-3 to
155 a value that did not match what was enabled in DR_CONTROL
156 resulted in EINVAL. To avoid this we zero DR_CONTROL before
157 writing address registers, only writing DR_CONTROL's actual
158 value once all the addresses are in place. */
159 x86_linux_dr_set (ptid
, DR_CONTROL
, 0);
161 ALL_DEBUG_ADDRESS_REGISTERS (i
)
162 if (state
->dr_ref_count
[i
] > 0)
164 x86_linux_dr_set (ptid
, i
, state
->dr_mirror
[i
]);
166 /* If we're setting a watchpoint, any change the inferior
167 has made to its debug registers needs to be discarded
168 to avoid x86_stopped_data_address getting confused. */
172 /* If DR_CONTROL is supposed to be zero then it's already set. */
173 if (state
->dr_control_mirror
!= 0)
174 x86_linux_dr_set (ptid
, DR_CONTROL
, state
->dr_control_mirror
);
176 lwp_set_debug_registers_changed (lwp
, 0);
180 || lwp_stop_reason (lwp
) == TARGET_STOPPED_BY_WATCHPOINT
)
181 x86_linux_dr_set (ptid
, DR_STATUS
, 0);