reggroups.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Register groupings 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 "arch-utils.h"
  17. #include "reggroups.h"
  18. #include "gdbtypes.h"
  19. #include "regcache.h"
  20. #include "command.h"
  21. #include "gdbcmd.h" /* For maintenanceprintlist. */
  22. #include "gdbsupport/gdb_obstack.h"
  23. /* See reggroups.h. */
  24. const reggroup *
  25. reggroup_new (const char *name, enum reggroup_type type)
  26. {
  27. return new reggroup (name, type);
  28. }
  29. /* See reggroups.h. */
  30. const reggroup *
  31. reggroup_gdbarch_new (struct gdbarch *gdbarch, const char *name,
  32. enum reggroup_type type)
  33. {
  34. name = gdbarch_obstack_strdup (gdbarch, name);
  35. return obstack_new<struct reggroup> (gdbarch_obstack (gdbarch),
  36. name, type);
  37. }
  38. /* A container holding all the register groups for a particular
  39. architecture. */
  40. struct reggroups
  41. {
  42. /* Add GROUP to the list of register groups. */
  43. void add (const reggroup *group)
  44. {
  45. gdb_assert (group != nullptr);
  46. gdb_assert (std::find (m_groups.begin(), m_groups.end(), group)
  47. == m_groups.end());
  48. m_groups.push_back (group);
  49. }
  50. /* The number of register groups. */
  51. std::vector<struct reggroup *>::size_type
  52. size () const
  53. {
  54. return m_groups.size ();
  55. }
  56. /* Return a reference to the list of all groups. */
  57. const std::vector<const struct reggroup *> &
  58. groups () const
  59. {
  60. return m_groups;
  61. }
  62. private:
  63. /* The register groups. */
  64. std::vector<const struct reggroup *> m_groups;
  65. };
  66. /* Key used to lookup register group data from a gdbarch. */
  67. static struct gdbarch_data *reggroups_data;
  68. /* See reggroups.h. */
  69. void
  70. reggroup_add (struct gdbarch *gdbarch, const reggroup *group)
  71. {
  72. struct reggroups *groups
  73. = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
  74. gdb_assert (groups != nullptr);
  75. gdb_assert (group != nullptr);
  76. groups->add (group);
  77. }
  78. /* Called to initialize the per-gdbarch register group information. */
  79. static void *
  80. reggroups_init (struct obstack *obstack)
  81. {
  82. struct reggroups *groups = obstack_new<struct reggroups> (obstack);
  83. /* Add the default groups. */
  84. groups->add (general_reggroup);
  85. groups->add (float_reggroup);
  86. groups->add (system_reggroup);
  87. groups->add (vector_reggroup);
  88. groups->add (all_reggroup);
  89. groups->add (save_reggroup);
  90. groups->add (restore_reggroup);
  91. return groups;
  92. }
  93. /* See reggroups.h. */
  94. const std::vector<const reggroup *> &
  95. gdbarch_reggroups (struct gdbarch *gdbarch)
  96. {
  97. struct reggroups *groups
  98. = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
  99. gdb_assert (groups != nullptr);
  100. gdb_assert (groups->size () > 0);
  101. return groups->groups ();
  102. }
  103. /* See reggroups.h. */
  104. int
  105. default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  106. const struct reggroup *group)
  107. {
  108. int vector_p;
  109. int float_p;
  110. int raw_p;
  111. if (gdbarch_register_name (gdbarch, regnum) == NULL
  112. || *gdbarch_register_name (gdbarch, regnum) == '\0')
  113. return 0;
  114. if (group == all_reggroup)
  115. return 1;
  116. vector_p = register_type (gdbarch, regnum)->is_vector ();
  117. float_p = (register_type (gdbarch, regnum)->code () == TYPE_CODE_FLT
  118. || (register_type (gdbarch, regnum)->code ()
  119. == TYPE_CODE_DECFLOAT));
  120. raw_p = regnum < gdbarch_num_regs (gdbarch);
  121. if (group == float_reggroup)
  122. return float_p;
  123. if (group == vector_reggroup)
  124. return vector_p;
  125. if (group == general_reggroup)
  126. return (!vector_p && !float_p);
  127. if (group == save_reggroup || group == restore_reggroup)
  128. return raw_p;
  129. return 0;
  130. }
  131. /* See reggroups.h. */
  132. const reggroup *
  133. reggroup_find (struct gdbarch *gdbarch, const char *name)
  134. {
  135. for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
  136. {
  137. if (strcmp (name, group->name ()) == 0)
  138. return group;
  139. }
  140. return NULL;
  141. }
  142. /* Dump out a table of register groups for the current architecture. */
  143. static void
  144. reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
  145. {
  146. static constexpr const char *fmt = " %-10s %-10s\n";
  147. gdb_printf (file, fmt, "Group", "Type");
  148. for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
  149. {
  150. /* Group name. */
  151. const char *name = group->name ();
  152. /* Group type. */
  153. const char *type;
  154. switch (group->type ())
  155. {
  156. case USER_REGGROUP:
  157. type = "user";
  158. break;
  159. case INTERNAL_REGGROUP:
  160. type = "internal";
  161. break;
  162. default:
  163. internal_error (__FILE__, __LINE__, _("bad switch"));
  164. }
  165. /* Note: If you change this, be sure to also update the
  166. documentation. */
  167. gdb_printf (file, fmt, name, type);
  168. }
  169. }
  170. /* Implement 'maintenance print reggroups' command. */
  171. static void
  172. maintenance_print_reggroups (const char *args, int from_tty)
  173. {
  174. struct gdbarch *gdbarch = get_current_arch ();
  175. if (args == NULL)
  176. reggroups_dump (gdbarch, gdb_stdout);
  177. else
  178. {
  179. stdio_file file;
  180. if (!file.open (args, "w"))
  181. perror_with_name (_("maintenance print reggroups"));
  182. reggroups_dump (gdbarch, &file);
  183. }
  184. }
  185. /* Pre-defined register groups. */
  186. static const reggroup general_group = { "general", USER_REGGROUP };
  187. static const reggroup float_group = { "float", USER_REGGROUP };
  188. static const reggroup system_group = { "system", USER_REGGROUP };
  189. static const reggroup vector_group = { "vector", USER_REGGROUP };
  190. static const reggroup all_group = { "all", USER_REGGROUP };
  191. static const reggroup save_group = { "save", INTERNAL_REGGROUP };
  192. static const reggroup restore_group = { "restore", INTERNAL_REGGROUP };
  193. const reggroup *const general_reggroup = &general_group;
  194. const reggroup *const float_reggroup = &float_group;
  195. const reggroup *const system_reggroup = &system_group;
  196. const reggroup *const vector_reggroup = &vector_group;
  197. const reggroup *const all_reggroup = &all_group;
  198. const reggroup *const save_reggroup = &save_group;
  199. const reggroup *const restore_reggroup = &restore_group;
  200. void _initialize_reggroup ();
  201. void
  202. _initialize_reggroup ()
  203. {
  204. reggroups_data = gdbarch_data_register_pre_init (reggroups_init);
  205. add_cmd ("reggroups", class_maintenance,
  206. maintenance_print_reggroups, _("\
  207. Print the internal register group names.\n\
  208. Takes an optional file parameter."),
  209. &maintenanceprintlist);
  210. }