Fix building Loongarch BFD with a 32-bit compiler
[binutils-gdb.git] / gdb / user-regs.c
blobac04f63dbb805a9f1263b8b5f7d991f313665c97
1 /* User visible, per-frame registers, for GDB, the GNU debugger.
3 Copyright (C) 2002-2024 Free Software Foundation, Inc.
5 Contributed by Red Hat.
7 This file is part of GDB.
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.
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.
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/>. */
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"
29 /* A table of user registers.
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.
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. */
40 struct user_reg
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;
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". */
57 struct gdb_user_regs
59 struct user_reg *first = nullptr;
60 struct user_reg **last = nullptr;
63 static void
64 append_user_reg (struct gdb_user_regs *regs, const char *name,
65 user_reg_read_ftype *xread, const void *baton,
66 struct user_reg *reg)
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;
82 /* An array of the builtin user registers. */
84 static struct gdb_user_regs builtin_user_regs;
86 void
87 user_reg_add_builtin (const char *name, user_reg_read_ftype *xread,
88 const void *baton)
90 append_user_reg (&builtin_user_regs, name, xread, baton,
91 XNEW (struct user_reg));
94 /* Per-architecture user registers. Start with the builtin user
95 registers and then, again, append. */
97 static const registry<gdbarch>::key<gdb_user_regs> user_regs_data;
99 static gdb_user_regs *
100 get_user_regs (struct gdbarch *gdbarch)
102 struct gdb_user_regs *regs = user_regs_data.get (gdbarch);
103 if (regs == nullptr)
105 regs = new struct gdb_user_regs;
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);
117 return regs;
120 void
121 user_reg_add (struct gdbarch *gdbarch, const char *name,
122 user_reg_read_ftype *xread, const void *baton)
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));
131 user_reg_map_name_to_regnum (struct gdbarch *gdbarch, const char *name,
132 int len)
134 /* Make life easy, set the len to something reasonable. */
135 if (len < 0)
136 len = strlen (name);
138 /* Search register name space first - always let an architecture
139 specific register override the user registers. */
141 int maxregs = gdbarch_num_cooked_regs (gdbarch);
143 for (int i = 0; i < maxregs; i++)
145 const char *regname = gdbarch_register_name (gdbarch, i);
147 if (len == strlen (regname) && strncmp (regname, name, len) == 0)
148 return i;
152 /* Search the user name space. */
154 struct gdb_user_regs *regs = get_user_regs (gdbarch);
155 struct user_reg *reg;
156 int nr;
158 for (nr = 0, reg = regs->first; reg != NULL; reg = reg->next, nr++)
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;
167 return -1;
170 static struct user_reg *
171 usernum_to_user_reg (struct gdbarch *gdbarch, int usernum)
173 struct gdb_user_regs *regs = get_user_regs (gdbarch);
174 struct user_reg *reg;
176 for (reg = regs->first; reg != NULL; reg = reg->next)
178 if (usernum == 0)
179 return reg;
180 usernum--;
182 return NULL;
185 const char *
186 user_reg_map_regnum_to_name (struct gdbarch *gdbarch, int regnum)
188 int maxregs = gdbarch_num_cooked_regs (gdbarch);
190 if (regnum < 0)
191 return NULL;
192 else if (regnum < maxregs)
193 return gdbarch_register_name (gdbarch, regnum);
194 else
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;
204 struct value *
205 value_of_user_reg (int regnum, const frame_info_ptr &frame)
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);
211 gdb_assert (reg != NULL);
212 return reg->xread (frame, reg->baton);
215 static void
216 maintenance_print_user_registers (const char *args, int from_tty)
218 struct gdbarch *gdbarch = get_current_arch ();
219 struct user_reg *reg;
220 int regnum;
222 struct gdb_user_regs *regs = get_user_regs (gdbarch);
223 regnum = gdbarch_num_cooked_regs (gdbarch);
225 gdb_printf (" %-11s %3s\n", "Name", "Nr");
226 for (reg = regs->first; reg != NULL; reg = reg->next, ++regnum)
227 gdb_printf (" %-11s %3d\n", reg->name, regnum);
230 void _initialize_user_regs ();
231 void
232 _initialize_user_regs ()
234 add_cmd ("user-registers", class_maintenance,
235 maintenance_print_user_registers,
236 _("List the names of the current user registers."),
237 &maintenanceprintlist);