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"
25 #include "arch-utils.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. */
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)
46 struct value
*(*xread
) (const frame_info_ptr
&frame
, 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". */
59 struct user_reg
*first
= nullptr;
60 struct user_reg
**last
= nullptr;
64 append_user_reg (struct gdb_user_regs
*regs
, const char *name
,
65 user_reg_read_ftype
*xread
, const void *baton
,
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
);
76 if (regs
->last
== nullptr)
77 regs
->last
= ®s
->first
;
79 regs
->last
= &(*regs
->last
)->next
;
82 /* An array of the builtin user registers. */
84 static struct gdb_user_regs builtin_user_regs
;
87 user_reg_add_builtin (const char *name
, user_reg_read_ftype
*xread
,
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
);
105 regs
= new struct gdb_user_regs
;
107 struct obstack
*obstack
= gdbarch_obstack (gdbarch
);
108 regs
->last
= ®s
->first
;
109 for (user_reg
*reg
= builtin_user_regs
.first
;
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
);
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
,
134 /* Make life easy, set the len to something reasonable. */
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)
152 /* Search the user name space. */
154 struct gdb_user_regs
*regs
= get_user_regs (gdbarch
);
155 struct user_reg
*reg
;
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
;
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
)
186 user_reg_map_regnum_to_name (struct gdbarch
*gdbarch
, int regnum
)
188 int maxregs
= gdbarch_num_cooked_regs (gdbarch
);
192 else if (regnum
< maxregs
)
193 return gdbarch_register_name (gdbarch
, regnum
);
196 struct user_reg
*reg
= usernum_to_user_reg (gdbarch
, regnum
- maxregs
);
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
);
216 maintenance_print_user_registers (const char *args
, int from_tty
)
218 struct gdbarch
*gdbarch
= get_current_arch ();
219 struct user_reg
*reg
;
222 struct gdb_user_regs
*regs
= get_user_regs (gdbarch
);
223 regnum
= gdbarch_num_cooked_regs (gdbarch
);
225 ui_out_emit_table
emitter (current_uiout
, 2, -1, "UserRegs");
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 ();
231 for (reg
= regs
->first
; reg
!= NULL
; reg
= reg
->next
, ++regnum
)
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");
240 void _initialize_user_regs ();
242 _initialize_user_regs ()
244 add_cmd ("user-registers", class_maintenance
,
245 maintenance_print_user_registers
,
246 _("List the names of the current user registers."),
247 &maintenanceprintlist
);