GDB (xrefs)
/tmp/gdb-7.10/gdb/reggroups.c
Go to the documentation of this file.
1 /* Register groupings for GDB, the GNU debugger.
2 
3  Copyright (C) 2002-2015 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 "arch-utils.h"
24 #include "reggroups.h"
25 #include "gdbtypes.h"
26 #include "regcache.h"
27 #include "command.h"
28 #include "gdbcmd.h" /* For maintenanceprintlist. */
29 
30 /* Individual register groups. */
31 
32 struct reggroup
33 {
34  const char *name;
36 };
37 
38 struct reggroup *
39 reggroup_new (const char *name, enum reggroup_type type)
40 {
41  struct reggroup *group = XNEW (struct reggroup);
42 
43  group->name = name;
44  group->type = type;
45  return group;
46 }
47 
48 /* Register group attributes. */
49 
50 const char *
51 reggroup_name (struct reggroup *group)
52 {
53  return group->name;
54 }
55 
56 enum reggroup_type
57 reggroup_type (struct reggroup *group)
58 {
59  return group->type;
60 }
61 
62 /* A linked list of groups for the given architecture. */
63 
65 {
66  struct reggroup *group;
67  struct reggroup_el *next;
68 };
69 
70 struct reggroups
71 {
72  struct reggroup_el *first;
73  struct reggroup_el **last;
74 };
75 
77 
78 static void *
80 {
81  struct reggroups *groups = GDBARCH_OBSTACK_ZALLOC (gdbarch,
82  struct reggroups);
83 
84  groups->last = &groups->first;
85  return groups;
86 }
87 
88 /* Add a register group (with attribute values) to the pre-defined
89  list. */
90 
91 static void
92 add_group (struct reggroups *groups, struct reggroup *group,
93  struct reggroup_el *el)
94 {
95  gdb_assert (group != NULL);
96  el->group = group;
97  el->next = NULL;
98  (*groups->last) = el;
99  groups->last = &el->next;
100 }
101 
102 void
103 reggroup_add (struct gdbarch *gdbarch, struct reggroup *group)
104 {
105  struct reggroups *groups = gdbarch_data (gdbarch, reggroups_data);
106 
107  if (groups == NULL)
108  {
109  /* ULGH, called during architecture initialization. Patch
110  things up. */
111  groups = reggroups_init (gdbarch);
112  deprecated_set_gdbarch_data (gdbarch, reggroups_data, groups);
113  }
114  add_group (groups, group,
115  GDBARCH_OBSTACK_ZALLOC (gdbarch, struct reggroup_el));
116 }
117 
118 /* The default register groups for an architecture. */
119 
120 static struct reggroups default_groups = { NULL, &default_groups.first };
121 
122 /* A register group iterator. */
123 
124 struct reggroup *
125 reggroup_next (struct gdbarch *gdbarch, struct reggroup *last)
126 {
127  struct reggroups *groups;
128  struct reggroup_el *el;
129 
130  /* Don't allow this function to be called during architecture
131  creation. If there are no groups, use the default groups list. */
132  groups = gdbarch_data (gdbarch, reggroups_data);
133  gdb_assert (groups != NULL);
134  if (groups->first == NULL)
135  groups = &default_groups;
136 
137  /* Return the first/next reggroup. */
138  if (last == NULL)
139  return groups->first->group;
140  for (el = groups->first; el != NULL; el = el->next)
141  {
142  if (el->group == last)
143  {
144  if (el->next != NULL)
145  return el->next->group;
146  else
147  return NULL;
148  }
149  }
150  return NULL;
151 }
152 
153 /* See reggroups.h. */
154 
155 struct reggroup *
156 reggroup_prev (struct gdbarch *gdbarch, struct reggroup *curr)
157 {
158  struct reggroups *groups;
159  struct reggroup_el *el;
160  struct reggroup *prev;
161 
162  /* Don't allow this function to be called during architecture
163  creation. If there are no groups, use the default groups list. */
164  groups = gdbarch_data (gdbarch, reggroups_data);
165  gdb_assert (groups != NULL);
166  if (groups->first == NULL)
167  groups = &default_groups;
168 
169  prev = NULL;
170  for (el = groups->first; el != NULL; el = el->next)
171  {
172  gdb_assert (el->group != NULL);
173  if (el->group == curr)
174  return prev;
175  prev = el->group;
176  }
177  if (curr == NULL)
178  return prev;
179  return NULL;
180 }
181 
182 /* Is REGNUM a member of REGGROUP? */
183 int
185  struct reggroup *group)
186 {
187  int vector_p;
188  int float_p;
189  int raw_p;
190 
191  if (gdbarch_register_name (gdbarch, regnum) == NULL
192  || *gdbarch_register_name (gdbarch, regnum) == '\0')
193  return 0;
194  if (group == all_reggroup)
195  return 1;
196  vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
197  float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
198  raw_p = regnum < gdbarch_num_regs (gdbarch);
199  if (group == float_reggroup)
200  return float_p;
201  if (group == vector_reggroup)
202  return vector_p;
203  if (group == general_reggroup)
204  return (!vector_p && !float_p);
205  if (group == save_reggroup || group == restore_reggroup)
206  return raw_p;
207  return 0;
208 }
209 
210 /* Dump out a table of register groups for the current architecture. */
211 
212 static void
213 reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
214 {
215  struct reggroup *group = NULL;
216 
217  do
218  {
219  /* Group name. */
220  {
221  const char *name;
222 
223  if (group == NULL)
224  name = "Group";
225  else
226  name = reggroup_name (group);
227  fprintf_unfiltered (file, " %-10s", name);
228  }
229 
230  /* Group type. */
231  {
232  const char *type;
233 
234  if (group == NULL)
235  type = "Type";
236  else
237  {
238  switch (reggroup_type (group))
239  {
240  case USER_REGGROUP:
241  type = "user";
242  break;
243  case INTERNAL_REGGROUP:
244  type = "internal";
245  break;
246  default:
247  internal_error (__FILE__, __LINE__, _("bad switch"));
248  }
249  }
250  fprintf_unfiltered (file, " %-10s", type);
251  }
252 
253  /* Note: If you change this, be sure to also update the
254  documentation. */
255 
256  fprintf_unfiltered (file, "\n");
257 
258  group = reggroup_next (gdbarch, group);
259  }
260  while (group != NULL);
261 }
262 
263 static void
264 maintenance_print_reggroups (char *args, int from_tty)
265 {
266  struct gdbarch *gdbarch = get_current_arch ();
267 
268  if (args == NULL)
269  reggroups_dump (gdbarch, gdb_stdout);
270  else
271  {
272  struct cleanup *cleanups;
273  struct ui_file *file = gdb_fopen (args, "w");
274 
275  if (file == NULL)
276  perror_with_name (_("maintenance print reggroups"));
277  cleanups = make_cleanup_ui_file_delete (file);
278  reggroups_dump (gdbarch, file);
279  do_cleanups (cleanups);
280  }
281 }
282 
283 /* Pre-defined register groups. */
284 static struct reggroup general_group = { "general", USER_REGGROUP };
285 static struct reggroup float_group = { "float", USER_REGGROUP };
286 static struct reggroup system_group = { "system", USER_REGGROUP };
287 static struct reggroup vector_group = { "vector", USER_REGGROUP };
288 static struct reggroup all_group = { "all", USER_REGGROUP };
289 static struct reggroup save_group = { "save", INTERNAL_REGGROUP };
290 static struct reggroup restore_group = { "restore", INTERNAL_REGGROUP };
291 
292 struct reggroup *const general_reggroup = &general_group;
293 struct reggroup *const float_reggroup = &float_group;
294 struct reggroup *const system_reggroup = &system_group;
295 struct reggroup *const vector_reggroup = &vector_group;
296 struct reggroup *const all_reggroup = &all_group;
297 struct reggroup *const save_reggroup = &save_group;
298 struct reggroup *const restore_reggroup = &restore_group;
299 
300 extern initialize_file_ftype _initialize_reggroup; /* -Wmissing-prototypes */
301 
302 void
304 {
306 
307  /* The pre-defined list of groups. */
308  add_group (&default_groups, general_reggroup, XNEW (struct reggroup_el));
309  add_group (&default_groups, float_reggroup, XNEW (struct reggroup_el));
310  add_group (&default_groups, system_reggroup, XNEW (struct reggroup_el));
311  add_group (&default_groups, vector_reggroup, XNEW (struct reggroup_el));
312  add_group (&default_groups, all_reggroup, XNEW (struct reggroup_el));
313  add_group (&default_groups, save_reggroup, XNEW (struct reggroup_el));
314  add_group (&default_groups, restore_reggroup, XNEW (struct reggroup_el));
315 
316  add_cmd ("reggroups", class_maintenance,
318 Print the internal register group names.\n\
319 Takes an optional file parameter."),
321 
322 }
void reggroup_add(struct gdbarch *gdbarch, struct reggroup *group)
Definition: reggroups.c:103
struct reggroup_el ** last
Definition: reggroups.c:73
struct reggroup * reggroup_new(const char *name, enum reggroup_type type)
Definition: reggroups.c:39
static struct reggroup save_group
Definition: reggroups.c:289
static struct reggroup system_group
Definition: reggroups.c:286
static struct reggroup all_group
Definition: reggroups.c:288
struct reggroup * reggroup_prev(struct gdbarch *gdbarch, struct reggroup *curr)
Definition: reggroups.c:156
static struct reggroup vector_group
Definition: reggroups.c:287
const char * name
Definition: reggroups.c:34
void * gdbarch_data(struct gdbarch *gdbarch, struct gdbarch_data *data)
Definition: gdbarch.c:4845
struct ui_file * gdb_stdout
Definition: main.c:71
struct ui_file * gdb_fopen(const char *name, const char *mode)
Definition: ui-file.c:723
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
static void maintenance_print_reggroups(char *args, int from_tty)
Definition: reggroups.c:264
static struct reggroup restore_group
Definition: reggroups.c:290
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:1898
#define _(String)
Definition: gdb_locale.h:40
#define GDBARCH_OBSTACK_ZALLOC(GDBARCH, TYPE)
Definition: gdbarch.h:1615
const char *const name
Definition: aarch64-tdep.c:68
reggroup_type
Definition: reggroups.h:28
static struct gdbarch_data * reggroups_data
Definition: reggroups.c:76
void initialize_file_ftype(void)
Definition: defs.h:281
static struct reggroups default_groups
Definition: reggroups.c:120
struct reggroup_el * first
Definition: reggroups.c:72
static void reggroups_dump(struct gdbarch *gdbarch, struct ui_file *file)
Definition: reggroups.c:213
enum reggroup_type type
Definition: reggroups.c:35
initialize_file_ftype _initialize_reggroup
struct type * register_type(struct gdbarch *gdbarch, int regnum)
Definition: regcache.c:157
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2361
#define TYPE_VECTOR(t)
Definition: gdbtypes.h:287
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, cmd_cfunc_ftype *fun, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:192
struct reggroup * group
Definition: reggroups.c:66
Definition: gdbtypes.h:749
struct gdbarch * get_current_arch(void)
Definition: arch-utils.c:781
static const char * type
Definition: language.c:103
#define gdb_assert(expr)
Definition: gdb_assert.h:33
const char * gdbarch_register_name(struct gdbarch *gdbarch, int regnr)
Definition: gdbarch.c:2117
int regnum
Definition: aarch64-tdep.c:69
struct reggroup_el * next
Definition: reggroups.c:67
struct cmd_list_element * maintenanceprintlist
Definition: cli-cmds.c:167
static struct reggroup float_group
Definition: reggroups.c:285
enum reggroup_type reggroup_type(struct reggroup *group)
Definition: reggroups.c:57
static void add_group(struct reggroups *groups, struct reggroup *group, struct reggroup_el *el)
Definition: reggroups.c:92
static void * reggroups_init(struct gdbarch *gdbarch)
Definition: reggroups.c:79
void void void void void void void void void perror_with_name(const char *string) ATTRIBUTE_NORETURN
Definition: utils.c:979
static struct reggroup general_group
Definition: reggroups.c:284
struct cleanup * make_cleanup_ui_file_delete(struct ui_file *arg)
Definition: utils.c:237
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1240
void deprecated_set_gdbarch_data(struct gdbarch *gdbarch, struct gdbarch_data *data, void *pointer)
Definition: gdbarch.c:4831
int default_register_reggroup_p(struct gdbarch *gdbarch, int regnum, struct reggroup *group)
Definition: reggroups.c:184
struct reggroup * reggroup_next(struct gdbarch *gdbarch, struct reggroup *last)
Definition: reggroups.c:125
struct gdbarch_data * gdbarch_data_register_post_init(gdbarch_data_post_init_ftype *post_init)
Definition: gdbarch.c:4812
const char * reggroup_name(struct reggroup *group)
Definition: reggroups.c:51
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:175