user-regs.c 6.7 KB

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