10SN/A/* User visible, per-frame registers, for GDB, the GNU debugger.
210387SN/A
30SN/A   Copyright (C) 2002-2023 Free Software Foundation, Inc.
40SN/A
50SN/A   Contributed by Red Hat.
60SN/A
72362SN/A   This file is part of GDB.
80SN/A
92362SN/A   This program is free software; you can redistribute it and/or modify
100SN/A   it under the terms of the GNU General Public License as published by
110SN/A   the Free Software Foundation; either version 3 of the License, or
120SN/A   (at your option) any later version.
130SN/A
140SN/A   This program is distributed in the hope that it will be useful,
150SN/A   but WITHOUT ANY WARRANTY; without even the implied warranty of
160SN/A   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
170SN/A   GNU General Public License for more details.
180SN/A
190SN/A   You should have received a copy of the GNU General Public License
200SN/A   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
212362SN/A
222362SN/A#include "defs.h"
232362SN/A#include "user-regs.h"
240SN/A#include "gdbtypes.h"
250SN/A#include "frame.h"
260SN/A#include "arch-utils.h"
270SN/A#include "command.h"
281859SN/A#include "cli/cli-cmds.h"
291859SN/A
300SN/A/* A table of user registers.
310SN/A
320SN/A   User registers have regnum's that live above of the range [0
330SN/A   .. gdbarch_num_regs + gdbarch_num_pseudo_regs)
340SN/A   (which is controlled by the target).
350SN/A   The target should never see a user register's regnum value.
360SN/A
370SN/A   Always append, never delete.  By doing this, the relative regnum
380SN/A   (offset from gdbarch_num_regs + gdbarch_num_pseudo_regs)
390SN/A    assigned to each user  register never changes.  */
400SN/A
410SN/Astruct user_reg
420SN/A{
430SN/A  const char *name;
440SN/A  /* Avoid the "read" symbol name as it conflicts with a preprocessor symbol
450SN/A     in the NetBSD header for Stack Smashing Protection, that wraps the read(2)
460SN/A     syscall.  */
471859SN/A  struct value *(*xread) (frame_info_ptr frame, const void *baton);
480SN/A  const void *baton;
4912839Sprr  struct user_reg *next;
500SN/A};
510SN/A
520SN/A/* This structure is named gdb_user_regs instead of user_regs to avoid
530SN/A   conflicts with any "struct user_regs" in system headers.  For instance,
5410387SN/A   on ARM GNU/Linux native builds, nm-linux.h includes <signal.h> includes
5510387SN/A   <sys/ucontext.h> includes <sys/procfs.h> includes <sys/user.h>, which
5610387SN/A   declares "struct user_regs".  */
5710387SN/A
5810387SN/Astruct gdb_user_regs
5910387SN/A{
6010387SN/A  struct user_reg *first = nullptr;
610SN/A  struct user_reg **last = nullptr;
621859SN/A};
631859SN/A
641859SN/Astatic void
651859SN/Aappend_user_reg (struct gdb_user_regs *regs, const char *name,
661859SN/A		 user_reg_read_ftype *xread, const void *baton,
671859SN/A		 struct user_reg *reg)
681859SN/A{
690SN/A  /* The caller is responsible for allocating memory needed to store
700SN/A     the register.  By doing this, the function can operate on a
710SN/A     register list stored in the common heap or a specific obstack.  */
720SN/A  gdb_assert (reg != NULL);
730SN/A  reg->name = name;
740SN/A  reg->xread = xread;
750SN/A  reg->baton = baton;
760SN/A  reg->next = NULL;
770SN/A  if (regs->last == nullptr)
780SN/A    regs->last = &regs->first;
790SN/A  (*regs->last) = reg;
800SN/A  regs->last = &(*regs->last)->next;
810SN/A}
820SN/A
830SN/A/* An array of the builtin user registers.  */
840SN/A
850SN/Astatic struct gdb_user_regs builtin_user_regs;
860SN/A
870SN/Avoid
880SN/Auser_reg_add_builtin (const char *name, user_reg_read_ftype *xread,
890SN/A		      const void *baton)
900SN/A{
910SN/A  append_user_reg (&builtin_user_regs, name, xread, baton,
920SN/A		   XNEW (struct user_reg));
930SN/A}
940SN/A
950SN/A/* Per-architecture user registers.  Start with the builtin user
960SN/A   registers and then, again, append.  */
970SN/A
980SN/Astatic const registry<gdbarch>::key<gdb_user_regs> user_regs_data;
990SN/A
1000SN/Astatic gdb_user_regs *
1010SN/Aget_user_regs (struct gdbarch *gdbarch)
1020SN/A{
1030SN/A  struct gdb_user_regs *regs = user_regs_data.get (gdbarch);
1040SN/A  if (regs == nullptr)
1050SN/A    {
1060SN/A      regs = new struct gdb_user_regs;
1070SN/A
1080SN/A      struct obstack *obstack = gdbarch_obstack (gdbarch);
1090SN/A      regs->last = &regs->first;
1100SN/A      for (user_reg *reg = builtin_user_regs.first;
1110SN/A	   reg != NULL;
1120SN/A	   reg = reg->next)
1130SN/A	append_user_reg (regs, reg->name, reg->xread, reg->baton,
1140SN/A			 OBSTACK_ZALLOC (obstack, struct user_reg));
1150SN/A      user_regs_data.set (gdbarch, regs);
1160SN/A    }
1170SN/A
1180SN/A  return regs;
1190SN/A}
1200SN/A
1210SN/Avoid
1220SN/Auser_reg_add (struct gdbarch *gdbarch, const char *name,
1230SN/A	      user_reg_read_ftype *xread, const void *baton)
1240SN/A{
1250SN/A  struct gdb_user_regs *regs = get_user_regs (gdbarch);
1260SN/A  gdb_assert (regs != NULL);
1270SN/A  append_user_reg (regs, name, xread, baton,
1280SN/A		   GDBARCH_OBSTACK_ZALLOC (gdbarch, struct user_reg));
1290SN/A}
1300SN/A
1310SN/Aint
1320SN/Auser_reg_map_name_to_regnum (struct gdbarch *gdbarch, const char *name,
1330SN/A			     int len)
1340SN/A{
1350SN/A  /* Make life easy, set the len to something reasonable.  */
1360SN/A  if (len < 0)
13710387SN/A    len = strlen (name);
13810387SN/A
13910387SN/A  /* Search register name space first - always let an architecture
14010387SN/A     specific register override the user registers.  */
14110387SN/A  {
14210387SN/A    int maxregs = gdbarch_num_cooked_regs (gdbarch);
14310387SN/A
1440SN/A    for (int i = 0; i < maxregs; i++)
1450SN/A      {
1460SN/A	const char *regname = gdbarch_register_name (gdbarch, i);
1470SN/A
1480SN/A	if (len == strlen (regname) && strncmp (regname, name, len) == 0)
1490SN/A	  return i;
150614SN/A      }
1510SN/A  }
152614SN/A
1530SN/A  /* Search the user name space.  */
1540SN/A  {
155614SN/A    struct gdb_user_regs *regs = get_user_regs (gdbarch);
1560SN/A    struct user_reg *reg;
1570SN/A    int nr;
158614SN/A
1590SN/A    for (nr = 0, reg = regs->first; reg != NULL; reg = reg->next, nr++)
1600SN/A      {
1610SN/A	if ((len < 0 && strcmp (reg->name, name))
162614SN/A	    || (len == strlen (reg->name)
1630SN/A		&& strncmp (reg->name, name, len) == 0))
164614SN/A	  return gdbarch_num_cooked_regs (gdbarch) + nr;
1650SN/A      }
1660SN/A  }
167614SN/A
1680SN/A  return -1;
1690SN/A}
170614SN/A
1710SN/Astatic struct user_reg *
1720SN/Ausernum_to_user_reg (struct gdbarch *gdbarch, int usernum)
1730SN/A{
174614SN/A  struct gdb_user_regs *regs = get_user_regs (gdbarch);
1750SN/A  struct user_reg *reg;
1760SN/A
1770SN/A  for (reg = regs->first; reg != NULL; reg = reg->next)
1780SN/A    {
1790SN/A      if (usernum == 0)
1800SN/A	return reg;
1810SN/A      usernum--;
1820SN/A    }
1830SN/A  return NULL;
1840SN/A}
1850SN/A
1860SN/Aconst char *
1870SN/Auser_reg_map_regnum_to_name (struct gdbarch *gdbarch, int regnum)
1880SN/A{
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
205struct value *
206value_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
216static void
217maintenance_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
231void _initialize_user_regs ();
232void
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