]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - gdb/user-regs.c
gas: introduce .errif and .warnif
[thirdparty/binutils-gdb.git] / gdb / user-regs.c
... / ...
CommitLineData
1/* User visible, per-frame registers, for GDB, the GNU debugger.
2
3 Copyright (C) 2002-2025 Free Software Foundation, Inc.
4
5 Contributed by Red Hat.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#include "user-regs.h"
23#include "gdbtypes.h"
24#include "frame.h"
25#include "arch-utils.h"
26#include "command.h"
27#include "cli/cli-cmds.h"
28
29/* A table of user registers.
30
31 User registers have regnum's that live above of the range [0
32 .. gdbarch_num_regs + gdbarch_num_pseudo_regs)
33 (which is controlled by the target).
34 The target should never see a user register's regnum value.
35
36 Always append, never delete. By doing this, the relative regnum
37 (offset from gdbarch_num_regs + gdbarch_num_pseudo_regs)
38 assigned to each user register never changes. */
39
40struct user_reg
41{
42 const char *name;
43 /* Avoid the "read" symbol name as it conflicts with a preprocessor symbol
44 in the NetBSD header for Stack Smashing Protection, that wraps the read(2)
45 syscall. */
46 struct value *(*xread) (const frame_info_ptr &frame, const void *baton);
47 const void *baton;
48 struct user_reg *next;
49};
50
51/* This structure is named gdb_user_regs instead of user_regs to avoid
52 conflicts with any "struct user_regs" in system headers. For instance,
53 on ARM GNU/Linux native builds, nm-linux.h includes <signal.h> includes
54 <sys/ucontext.h> includes <sys/procfs.h> includes <sys/user.h>, which
55 declares "struct user_regs". */
56
57struct gdb_user_regs
58{
59 struct user_reg *first = nullptr;
60 struct user_reg **last = nullptr;
61};
62
63static void
64append_user_reg (struct gdb_user_regs *regs, const char *name,
65 user_reg_read_ftype *xread, const void *baton,
66 struct user_reg *reg)
67{
68 /* The caller is responsible for allocating memory needed to store
69 the register. By doing this, the function can operate on a
70 register list stored in the common heap or a specific obstack. */
71 gdb_assert (reg != NULL);
72 reg->name = name;
73 reg->xread = xread;
74 reg->baton = baton;
75 reg->next = NULL;
76 if (regs->last == nullptr)
77 regs->last = &regs->first;
78 (*regs->last) = reg;
79 regs->last = &(*regs->last)->next;
80}
81
82/* An array of the builtin user registers. */
83
84static struct gdb_user_regs builtin_user_regs;
85
86void
87user_reg_add_builtin (const char *name, user_reg_read_ftype *xread,
88 const void *baton)
89{
90 append_user_reg (&builtin_user_regs, name, xread, baton,
91 XNEW (struct user_reg));
92}
93
94/* Per-architecture user registers. Start with the builtin user
95 registers and then, again, append. */
96
97static const registry<gdbarch>::key<gdb_user_regs> user_regs_data;
98
99static gdb_user_regs *
100get_user_regs (struct gdbarch *gdbarch)
101{
102 struct gdb_user_regs *regs = user_regs_data.get (gdbarch);
103 if (regs == nullptr)
104 {
105 regs = new struct gdb_user_regs;
106
107 struct obstack *obstack = gdbarch_obstack (gdbarch);
108 regs->last = &regs->first;
109 for (user_reg *reg = builtin_user_regs.first;
110 reg != NULL;
111 reg = reg->next)
112 append_user_reg (regs, reg->name, reg->xread, reg->baton,
113 OBSTACK_ZALLOC (obstack, struct user_reg));
114 user_regs_data.set (gdbarch, regs);
115 }
116
117 return regs;
118}
119
120void
121user_reg_add (struct gdbarch *gdbarch, const char *name,
122 user_reg_read_ftype *xread, const void *baton)
123{
124 struct gdb_user_regs *regs = get_user_regs (gdbarch);
125 gdb_assert (regs != NULL);
126 append_user_reg (regs, name, xread, baton,
127 GDBARCH_OBSTACK_ZALLOC (gdbarch, struct user_reg));
128}
129
130int
131user_reg_map_name_to_regnum (struct gdbarch *gdbarch, const char *name,
132 int len)
133{
134 /* Make life easy, set the len to something reasonable. */
135 if (len < 0)
136 len = strlen (name);
137
138 /* Search register name space first - always let an architecture
139 specific register override the user registers. */
140 {
141 int maxregs = gdbarch_num_cooked_regs (gdbarch);
142
143 for (int i = 0; i < maxregs; i++)
144 {
145 const char *regname = gdbarch_register_name (gdbarch, i);
146
147 if (len == strlen (regname) && strncmp (regname, name, len) == 0)
148 return i;
149 }
150 }
151
152 /* Search the user name space. */
153 {
154 struct gdb_user_regs *regs = get_user_regs (gdbarch);
155 struct user_reg *reg;
156 int nr;
157
158 for (nr = 0, reg = regs->first; reg != NULL; reg = reg->next, nr++)
159 {
160 if ((len < 0 && strcmp (reg->name, name))
161 || (len == strlen (reg->name)
162 && strncmp (reg->name, name, len) == 0))
163 return gdbarch_num_cooked_regs (gdbarch) + nr;
164 }
165 }
166
167 return -1;
168}
169
170static struct user_reg *
171usernum_to_user_reg (struct gdbarch *gdbarch, int usernum)
172{
173 struct gdb_user_regs *regs = get_user_regs (gdbarch);
174 struct user_reg *reg;
175
176 for (reg = regs->first; reg != NULL; reg = reg->next)
177 {
178 if (usernum == 0)
179 return reg;
180 usernum--;
181 }
182 return NULL;
183}
184
185const char *
186user_reg_map_regnum_to_name (struct gdbarch *gdbarch, int regnum)
187{
188 int maxregs = gdbarch_num_cooked_regs (gdbarch);
189
190 if (regnum < 0)
191 return NULL;
192 else if (regnum < maxregs)
193 return gdbarch_register_name (gdbarch, regnum);
194 else
195 {
196 struct user_reg *reg = usernum_to_user_reg (gdbarch, regnum - maxregs);
197 if (reg == NULL)
198 return NULL;
199 else
200 return reg->name;
201 }
202}
203
204struct value *
205value_of_user_reg (int regnum, const frame_info_ptr &frame)
206{
207 struct gdbarch *gdbarch = get_frame_arch (frame);
208 int maxregs = gdbarch_num_cooked_regs (gdbarch);
209 struct user_reg *reg = usernum_to_user_reg (gdbarch, regnum - maxregs);
210
211 gdb_assert (reg != NULL);
212 return reg->xread (frame, reg->baton);
213}
214
215static void
216maintenance_print_user_registers (const char *args, int from_tty)
217{
218 struct gdbarch *gdbarch = get_current_arch ();
219 struct user_reg *reg;
220 int regnum;
221
222 struct gdb_user_regs *regs = get_user_regs (gdbarch);
223 regnum = gdbarch_num_cooked_regs (gdbarch);
224
225 ui_out_emit_table emitter (current_uiout, 2, -1, "UserRegs");
226
227 current_uiout->table_header (11, ui_left, "name", "Name");
228 current_uiout->table_header (3, ui_left, "regnum", "Nr");
229 current_uiout->table_body ();
230
231 for (reg = regs->first; reg != NULL; reg = reg->next, ++regnum)
232 {
233 ui_out_emit_tuple tuple_emitter (current_uiout, nullptr);
234 current_uiout->field_string ("name", reg->name);
235 current_uiout->field_signed ("regnum", regnum);
236 current_uiout->text ("\n");
237 }
238}
239
240INIT_GDB_FILE (user_regs)
241{
242 add_cmd ("user-registers", class_maintenance,
243 maintenance_print_user_registers,
244 _("List the names of the current user registers."),
245 &maintenanceprintlist);
246}