1 /* Native-dependent code for AArch64.
3 Copyright (C) 2011-2023 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/>. */
23 #include "cli/cli-cmds.h"
24 #include "aarch64-nat.h"
26 #include <unordered_map>
28 /* Hash table storing per-process data. We don't bind this to a
29 per-inferior registry because of targets like x86 GNU/Linux that
30 need to keep track of processes that aren't bound to any inferior
31 (e.g., fork children, checkpoints). */
33 static std::unordered_map
<pid_t
, aarch64_debug_reg_state
>
34 aarch64_debug_process_state
;
36 /* See aarch64-nat.h. */
38 struct aarch64_debug_reg_state
*
39 aarch64_lookup_debug_reg_state (pid_t pid
)
41 auto it
= aarch64_debug_process_state
.find (pid
);
42 if (it
!= aarch64_debug_process_state
.end ())
48 /* See aarch64-nat.h. */
50 struct aarch64_debug_reg_state
*
51 aarch64_get_debug_reg_state (pid_t pid
)
53 return &aarch64_debug_process_state
[pid
];
56 /* See aarch64-nat.h. */
59 aarch64_remove_debug_reg_state (pid_t pid
)
61 aarch64_debug_process_state
.erase (pid
);
64 /* Returns the number of hardware watchpoints of type TYPE that we can
65 set. Value is positive if we can set CNT watchpoints, zero if
66 setting watchpoints of type TYPE is not supported, and negative if
67 CNT is more than the maximum number of watchpoints of type TYPE
68 that we can support. TYPE is one of bp_hardware_watchpoint,
69 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
70 CNT is the number of such watchpoints used so far (including this
71 one). OTHERTYPE is non-zero if other types of watchpoints are
75 aarch64_can_use_hw_breakpoint (enum bptype type
, int cnt
, int othertype
)
77 if (type
== bp_hardware_watchpoint
|| type
== bp_read_watchpoint
78 || type
== bp_access_watchpoint
|| type
== bp_watchpoint
)
80 if (aarch64_num_wp_regs
== 0)
83 else if (type
== bp_hardware_breakpoint
)
85 if (aarch64_num_bp_regs
== 0)
89 gdb_assert_not_reached ("unexpected breakpoint type");
91 /* We always return 1 here because we don't have enough information
92 about possible overlap of addresses that they want to watch. As an
93 extreme example, consider the case where all the watchpoints watch
94 the same address and the same region length: then we can handle a
95 virtually unlimited number of watchpoints, due to debug register
96 sharing implemented via reference counts. */
100 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
101 Return 0 on success, -1 on failure. */
104 aarch64_insert_hw_breakpoint (struct gdbarch
*gdbarch
,
105 struct bp_target_info
*bp_tgt
)
108 CORE_ADDR addr
= bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
110 const enum target_hw_bp_type type
= hw_execute
;
111 struct aarch64_debug_reg_state
*state
112 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
114 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &len
);
117 gdb_printf (gdb_stdlog
,
118 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
119 (unsigned long) addr
, len
);
121 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 1 /* is_insert */,
122 inferior_ptid
, state
);
126 aarch64_show_debug_reg_state (state
,
127 "insert_hw_breakpoint", addr
, len
, type
);
133 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
134 Return 0 on success, -1 on failure. */
137 aarch64_remove_hw_breakpoint (struct gdbarch
*gdbarch
,
138 struct bp_target_info
*bp_tgt
)
141 CORE_ADDR addr
= bp_tgt
->placed_address
;
143 const enum target_hw_bp_type type
= hw_execute
;
144 struct aarch64_debug_reg_state
*state
145 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
147 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &len
);
150 gdb_printf (gdb_stdlog
,
151 "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
152 (unsigned long) addr
, len
);
154 ret
= aarch64_handle_breakpoint (type
, addr
, len
, 0 /* is_insert */,
155 inferior_ptid
, state
);
159 aarch64_show_debug_reg_state (state
,
160 "remove_hw_watchpoint", addr
, len
, type
);
166 /* Insert a watchpoint to watch a memory region which starts at
167 address ADDR and whose length is LEN bytes. Watch memory accesses
168 of the type TYPE. Return 0 on success, -1 on failure. */
171 aarch64_insert_watchpoint (CORE_ADDR addr
, int len
, enum target_hw_bp_type type
,
172 struct expression
*cond
)
175 struct aarch64_debug_reg_state
*state
176 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
179 gdb_printf (gdb_stdlog
,
180 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
181 (unsigned long) addr
, len
);
183 gdb_assert (type
!= hw_execute
);
185 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 1 /* is_insert */,
186 inferior_ptid
, state
);
190 aarch64_show_debug_reg_state (state
,
191 "insert_watchpoint", addr
, len
, type
);
197 /* Remove a watchpoint that watched the memory region which starts at
198 address ADDR, whose length is LEN bytes, and for accesses of the
199 type TYPE. Return 0 on success, -1 on failure. */
202 aarch64_remove_watchpoint (CORE_ADDR addr
, int len
, enum target_hw_bp_type type
,
203 struct expression
*cond
)
206 struct aarch64_debug_reg_state
*state
207 = aarch64_get_debug_reg_state (inferior_ptid
.pid ());
210 gdb_printf (gdb_stdlog
,
211 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
212 (unsigned long) addr
, len
);
214 gdb_assert (type
!= hw_execute
);
216 ret
= aarch64_handle_watchpoint (type
, addr
, len
, 0 /* is_insert */,
217 inferior_ptid
, state
);
221 aarch64_show_debug_reg_state (state
,
222 "remove_watchpoint", addr
, len
, type
);
228 /* See aarch64-nat.h. */
231 aarch64_stopped_data_address (const struct aarch64_debug_reg_state
*state
,
232 CORE_ADDR addr_trap
, CORE_ADDR
*addr_p
)
236 for (i
= aarch64_num_wp_regs
- 1; i
>= 0; --i
)
238 const unsigned int offset
239 = aarch64_watchpoint_offset (state
->dr_ctrl_wp
[i
]);
240 const unsigned int len
= aarch64_watchpoint_length (state
->dr_ctrl_wp
[i
]);
241 const CORE_ADDR addr_watch
= state
->dr_addr_wp
[i
] + offset
;
242 const CORE_ADDR addr_watch_aligned
= align_down (state
->dr_addr_wp
[i
], 8);
243 const CORE_ADDR addr_orig
= state
->dr_addr_orig_wp
[i
];
245 if (state
->dr_ref_count_wp
[i
]
246 && DR_CONTROL_ENABLED (state
->dr_ctrl_wp
[i
])
247 && addr_trap
>= addr_watch_aligned
248 && addr_trap
< addr_watch
+ len
)
250 /* ADDR_TRAP reports the first address of the memory range
251 accessed by the CPU, regardless of what was the memory
252 range watched. Thus, a large CPU access that straddles
253 the ADDR_WATCH..ADDR_WATCH+LEN range may result in an
254 ADDR_TRAP that is lower than the
255 ADDR_WATCH..ADDR_WATCH+LEN range. E.g.:
257 addr: | 4 | 5 | 6 | 7 | 8 |
258 |---- range watched ----|
259 |----------- range accessed ------------|
261 In this case, ADDR_TRAP will be 4.
263 To match a watchpoint known to GDB core, we must never
264 report *ADDR_P outside of any ADDR_WATCH..ADDR_WATCH+LEN
265 range. ADDR_WATCH <= ADDR_TRAP < ADDR_ORIG is a false
266 positive on kernels older than 4.10. See PR
276 /* Define AArch64 maintenance commands. */
279 add_show_debug_regs_command (void)
281 /* A maintenance command to enable printing the internal DRi mirror
283 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance
,
284 &show_debug_regs
, _("\
285 Set whether to show variables that mirror the AArch64 debug registers."), _("\
286 Show whether to show variables that mirror the AArch64 debug registers."), _("\
287 Use \"on\" to enable, \"off\" to disable.\n\
288 If enabled, the debug registers values are shown when GDB inserts\n\
289 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
290 triggers a breakpoint or watchpoint."),
293 &maintenance_set_cmdlist
,
294 &maintenance_show_cmdlist
);
298 aarch64_initialize_hw_point ()
300 add_show_debug_regs_command ();