1 /* User visible, per-frame registers, for GDB, the GNU debugger. 2 3 Copyright (C) 2002-2023 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 "defs.h" 23 #include "user-regs.h" 24 #include "gdbtypes.h" 25 #include "frame.h" 26 #include "arch-utils.h" 27 #include "command.h" 28 #include "cli/cli-cmds.h" 29 30 /* A table of user registers. 31 32 User registers have regnum's that live above of the range [0 33 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) 34 (which is controlled by the target). 35 The target should never see a user register's regnum value. 36 37 Always append, never delete. By doing this, the relative regnum 38 (offset from gdbarch_num_regs + gdbarch_num_pseudo_regs) 39 assigned to each user register never changes. */ 40 41 struct user_reg 42 { 43 const char *name; 44 /* Avoid the "read" symbol name as it conflicts with a preprocessor symbol 45 in the NetBSD header for Stack Smashing Protection, that wraps the read(2) 46 syscall. */ 47 struct value *(*xread) (frame_info_ptr frame, const void *baton); 48 const void *baton; 49 struct user_reg *next; 50 }; 51 52 /* This structure is named gdb_user_regs instead of user_regs to avoid 53 conflicts with any "struct user_regs" in system headers. For instance, 54 on ARM GNU/Linux native builds, nm-linux.h includes <signal.h> includes 55 <sys/ucontext.h> includes <sys/procfs.h> includes <sys/user.h>, which 56 declares "struct user_regs". */ 57 58 struct gdb_user_regs 59 { 60 struct user_reg *first = nullptr; 61 struct user_reg **last = nullptr; 62 }; 63 64 static void 65 append_user_reg (struct gdb_user_regs *regs, const char *name, 66 user_reg_read_ftype *xread, const void *baton, 67 struct user_reg *reg) 68 { 69 /* The caller is responsible for allocating memory needed to store 70 the register. By doing this, the function can operate on a 71 register list stored in the common heap or a specific obstack. */ 72 gdb_assert (reg != NULL); 73 reg->name = name; 74 reg->xread = xread; 75 reg->baton = baton; 76 reg->next = NULL; 77 if (regs->last == nullptr) 78 regs->last = ®s->first; 79 (*regs->last) = reg; 80 regs->last = &(*regs->last)->next; 81 } 82 83 /* An array of the builtin user registers. */ 84 85 static struct gdb_user_regs builtin_user_regs; 86 87 void 88 user_reg_add_builtin (const char *name, user_reg_read_ftype *xread, 89 const void *baton) 90 { 91 append_user_reg (&builtin_user_regs, name, xread, baton, 92 XNEW (struct user_reg)); 93 } 94 95 /* Per-architecture user registers. Start with the builtin user 96 registers and then, again, append. */ 97 98 static const registry<gdbarch>::key<gdb_user_regs> user_regs_data; 99 100 static gdb_user_regs * 101 get_user_regs (struct gdbarch *gdbarch) 102 { 103 struct gdb_user_regs *regs = user_regs_data.get (gdbarch); 104 if (regs == nullptr) 105 { 106 regs = new struct gdb_user_regs; 107 108 struct obstack *obstack = gdbarch_obstack (gdbarch); 109 regs->last = ®s->first; 110 for (user_reg *reg = builtin_user_regs.first; 111 reg != NULL; 112 reg = reg->next) 113 append_user_reg (regs, reg->name, reg->xread, reg->baton, 114 OBSTACK_ZALLOC (obstack, struct user_reg)); 115 user_regs_data.set (gdbarch, regs); 116 } 117 118 return regs; 119 } 120 121 void 122 user_reg_add (struct gdbarch *gdbarch, const char *name, 123 user_reg_read_ftype *xread, const void *baton) 124 { 125 struct gdb_user_regs *regs = get_user_regs (gdbarch); 126 gdb_assert (regs != NULL); 127 append_user_reg (regs, name, xread, baton, 128 GDBARCH_OBSTACK_ZALLOC (gdbarch, struct user_reg)); 129 } 130 131 int 132 user_reg_map_name_to_regnum (struct gdbarch *gdbarch, const char *name, 133 int len) 134 { 135 /* Make life easy, set the len to something reasonable. */ 136 if (len < 0) 137 len = strlen (name); 138 139 /* Search register name space first - always let an architecture 140 specific register override the user registers. */ 141 { 142 int maxregs = gdbarch_num_cooked_regs (gdbarch); 143 144 for (int i = 0; i < maxregs; i++) 145 { 146 const char *regname = gdbarch_register_name (gdbarch, i); 147 148 if (len == strlen (regname) && strncmp (regname, name, len) == 0) 149 return i; 150 } 151 } 152 153 /* Search the user name space. */ 154 { 155 struct gdb_user_regs *regs = get_user_regs (gdbarch); 156 struct user_reg *reg; 157 int nr; 158 159 for (nr = 0, reg = regs->first; reg != NULL; reg = reg->next, nr++) 160 { 161 if ((len < 0 && strcmp (reg->name, name)) 162 || (len == strlen (reg->name) 163 && strncmp (reg->name, name, len) == 0)) 164 return gdbarch_num_cooked_regs (gdbarch) + nr; 165 } 166 } 167 168 return -1; 169 } 170 171 static struct user_reg * 172 usernum_to_user_reg (struct gdbarch *gdbarch, int usernum) 173 { 174 struct gdb_user_regs *regs = get_user_regs (gdbarch); 175 struct user_reg *reg; 176 177 for (reg = regs->first; reg != NULL; reg = reg->next) 178 { 179 if (usernum == 0) 180 return reg; 181 usernum--; 182 } 183 return NULL; 184 } 185 186 const char * 187 user_reg_map_regnum_to_name (struct gdbarch *gdbarch, int regnum) 188 { 189 int maxregs = gdbarch_num_cooked_regs (gdbarch); 190 191 if (regnum < 0) 192 return NULL; 193 else if (regnum < maxregs) 194 return gdbarch_register_name (gdbarch, regnum); 195 else 196 { 197 struct user_reg *reg = usernum_to_user_reg (gdbarch, regnum - maxregs); 198 if (reg == NULL) 199 return NULL; 200 else 201 return reg->name; 202 } 203 } 204 205 struct value * 206 value_of_user_reg (int regnum, frame_info_ptr frame) 207 { 208 struct gdbarch *gdbarch = get_frame_arch (frame); 209 int maxregs = gdbarch_num_cooked_regs (gdbarch); 210 struct user_reg *reg = usernum_to_user_reg (gdbarch, regnum - maxregs); 211 212 gdb_assert (reg != NULL); 213 return reg->xread (frame, reg->baton); 214 } 215 216 static void 217 maintenance_print_user_registers (const char *args, int from_tty) 218 { 219 struct gdbarch *gdbarch = get_current_arch (); 220 struct user_reg *reg; 221 int regnum; 222 223 struct gdb_user_regs *regs = get_user_regs (gdbarch); 224 regnum = gdbarch_num_cooked_regs (gdbarch); 225 226 gdb_printf (" %-11s %3s\n", "Name", "Nr"); 227 for (reg = regs->first; reg != NULL; reg = reg->next, ++regnum) 228 gdb_printf (" %-11s %3d\n", reg->name, regnum); 229 } 230 231 void _initialize_user_regs (); 232 void 233 _initialize_user_regs () 234 { 235 add_cmd ("user-registers", class_maintenance, 236 maintenance_print_user_registers, 237 _("List the names of the current user registers."), 238 &maintenanceprintlist); 239 } 240