Re: nios2: Remove binutils support for Nios II target
[binutils-gdb.git] / gdb / aarch64-nat.c
blob1ba9c4c19a7de340c95051e0dc8cc1a93fef483e
1 /* Native-dependent code for AArch64.
3 Copyright (C) 2011-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 "gdbarch.h"
21 #include "inferior.h"
22 #include "cli/cli-cmds.h"
23 #include "aarch64-nat.h"
25 #include <unordered_map>
27 /* Hash table storing per-process data. We don't bind this to a
28 per-inferior registry because of targets like x86 GNU/Linux that
29 need to keep track of processes that aren't bound to any inferior
30 (e.g., fork children, checkpoints). */
32 static std::unordered_map<pid_t, aarch64_debug_reg_state>
33 aarch64_debug_process_state;
35 /* See aarch64-nat.h. */
37 struct aarch64_debug_reg_state *
38 aarch64_lookup_debug_reg_state (pid_t pid)
40 auto it = aarch64_debug_process_state.find (pid);
41 if (it != aarch64_debug_process_state.end ())
42 return &it->second;
44 return nullptr;
47 /* See aarch64-nat.h. */
49 struct aarch64_debug_reg_state *
50 aarch64_get_debug_reg_state (pid_t pid)
52 return &aarch64_debug_process_state[pid];
55 /* See aarch64-nat.h. */
57 void
58 aarch64_remove_debug_reg_state (pid_t pid)
60 aarch64_debug_process_state.erase (pid);
63 /* Returns the number of hardware watchpoints of type TYPE that we can
64 set. Value is positive if we can set CNT watchpoints, zero if
65 setting watchpoints of type TYPE is not supported, and negative if
66 CNT is more than the maximum number of watchpoints of type TYPE
67 that we can support. TYPE is one of bp_hardware_watchpoint,
68 bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
69 CNT is the number of such watchpoints used so far (including this
70 one). OTHERTYPE is non-zero if other types of watchpoints are
71 currently enabled. */
73 int
74 aarch64_can_use_hw_breakpoint (enum bptype type, int cnt, int othertype)
76 if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
77 || type == bp_access_watchpoint || type == bp_watchpoint)
79 if (aarch64_num_wp_regs == 0)
80 return 0;
82 else if (type == bp_hardware_breakpoint)
84 if (aarch64_num_bp_regs == 0)
85 return 0;
87 else
88 gdb_assert_not_reached ("unexpected breakpoint type");
90 /* We always return 1 here because we don't have enough information
91 about possible overlap of addresses that they want to watch. As an
92 extreme example, consider the case where all the watchpoints watch
93 the same address and the same region length: then we can handle a
94 virtually unlimited number of watchpoints, due to debug register
95 sharing implemented via reference counts. */
96 return 1;
99 /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
100 Return 0 on success, -1 on failure. */
103 aarch64_insert_hw_breakpoint (struct gdbarch *gdbarch,
104 struct bp_target_info *bp_tgt)
106 int ret;
107 CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
108 int len;
109 const enum target_hw_bp_type type = hw_execute;
110 struct aarch64_debug_reg_state *state
111 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
113 gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
115 if (show_debug_regs)
116 gdb_printf (gdb_stdlog,
117 "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
118 (unsigned long) addr, len);
120 ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */,
121 inferior_ptid, state);
123 if (show_debug_regs)
125 aarch64_show_debug_reg_state (state,
126 "insert_hw_breakpoint", addr, len, type);
129 return ret;
132 /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
133 Return 0 on success, -1 on failure. */
136 aarch64_remove_hw_breakpoint (struct gdbarch *gdbarch,
137 struct bp_target_info *bp_tgt)
139 int ret;
140 CORE_ADDR addr = bp_tgt->placed_address;
141 int len = 4;
142 const enum target_hw_bp_type type = hw_execute;
143 struct aarch64_debug_reg_state *state
144 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
146 gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
148 if (show_debug_regs)
149 gdb_printf (gdb_stdlog,
150 "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n",
151 (unsigned long) addr, len);
153 ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */,
154 inferior_ptid, state);
156 if (show_debug_regs)
158 aarch64_show_debug_reg_state (state,
159 "remove_hw_watchpoint", addr, len, type);
162 return ret;
165 /* Insert a watchpoint to watch a memory region which starts at
166 address ADDR and whose length is LEN bytes. Watch memory accesses
167 of the type TYPE. Return 0 on success, -1 on failure. */
170 aarch64_insert_watchpoint (CORE_ADDR addr, int len, enum target_hw_bp_type type,
171 struct expression *cond)
173 int ret;
174 struct aarch64_debug_reg_state *state
175 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
177 if (show_debug_regs)
178 gdb_printf (gdb_stdlog,
179 "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n",
180 (unsigned long) addr, len);
182 gdb_assert (type != hw_execute);
184 ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */,
185 inferior_ptid, state);
187 if (show_debug_regs)
189 aarch64_show_debug_reg_state (state,
190 "insert_watchpoint", addr, len, type);
193 return ret;
196 /* Remove a watchpoint that watched the memory region which starts at
197 address ADDR, whose length is LEN bytes, and for accesses of the
198 type TYPE. Return 0 on success, -1 on failure. */
201 aarch64_remove_watchpoint (CORE_ADDR addr, int len, enum target_hw_bp_type type,
202 struct expression *cond)
204 int ret;
205 struct aarch64_debug_reg_state *state
206 = aarch64_get_debug_reg_state (inferior_ptid.pid ());
208 if (show_debug_regs)
209 gdb_printf (gdb_stdlog,
210 "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n",
211 (unsigned long) addr, len);
213 gdb_assert (type != hw_execute);
215 ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */,
216 inferior_ptid, state);
218 if (show_debug_regs)
220 aarch64_show_debug_reg_state (state,
221 "remove_watchpoint", addr, len, type);
224 return ret;
227 /* Define AArch64 maintenance commands. */
229 static void
230 add_show_debug_regs_command (void)
232 /* A maintenance command to enable printing the internal DRi mirror
233 variables. */
234 add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
235 &show_debug_regs, _("\
236 Set whether to show variables that mirror the AArch64 debug registers."), _("\
237 Show whether to show variables that mirror the AArch64 debug registers."), _("\
238 Use \"on\" to enable, \"off\" to disable.\n\
239 If enabled, the debug registers values are shown when GDB inserts\n\
240 or removes a hardware breakpoint or watchpoint, and when the inferior\n\
241 triggers a breakpoint or watchpoint."),
242 NULL,
243 NULL,
244 &maintenance_set_cmdlist,
245 &maintenance_show_cmdlist);
248 void
249 aarch64_initialize_hw_point ()
251 add_show_debug_regs_command ();