GDB (API)
/home/stan/gdb/src/gdb/reggroups.c
Go to the documentation of this file.
00001 /* Register groupings for GDB, the GNU debugger.
00002 
00003    Copyright (C) 2002-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Red Hat.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "arch-utils.h"
00024 #include "reggroups.h"
00025 #include "gdbtypes.h"
00026 #include "gdb_assert.h"
00027 #include "regcache.h"
00028 #include "command.h"
00029 #include "gdbcmd.h"             /* For maintenanceprintlist.  */
00030 
00031 /* Individual register groups.  */
00032 
00033 struct reggroup
00034 {
00035   const char *name;
00036   enum reggroup_type type;
00037 };
00038 
00039 struct reggroup *
00040 reggroup_new (const char *name, enum reggroup_type type)
00041 {
00042   struct reggroup *group = XMALLOC (struct reggroup);
00043 
00044   group->name = name;
00045   group->type = type;
00046   return group;
00047 }
00048 
00049 /* Register group attributes.  */
00050 
00051 const char *
00052 reggroup_name (struct reggroup *group)
00053 {
00054   return group->name;
00055 }
00056 
00057 enum reggroup_type
00058 reggroup_type (struct reggroup *group)
00059 {
00060   return group->type;
00061 }
00062 
00063 /* A linked list of groups for the given architecture.  */
00064 
00065 struct reggroup_el
00066 {
00067   struct reggroup *group;
00068   struct reggroup_el *next;
00069 };
00070 
00071 struct reggroups
00072 {
00073   struct reggroup_el *first;
00074   struct reggroup_el **last;
00075 };
00076 
00077 static struct gdbarch_data *reggroups_data;
00078 
00079 static void *
00080 reggroups_init (struct gdbarch *gdbarch)
00081 {
00082   struct reggroups *groups = GDBARCH_OBSTACK_ZALLOC (gdbarch,
00083                                                      struct reggroups);
00084 
00085   groups->last = &groups->first;
00086   return groups;
00087 }
00088 
00089 /* Add a register group (with attribute values) to the pre-defined
00090    list.  */
00091 
00092 static void
00093 add_group (struct reggroups *groups, struct reggroup *group,
00094            struct reggroup_el *el)
00095 {
00096   gdb_assert (group != NULL);
00097   el->group = group;
00098   el->next = NULL;
00099   (*groups->last) = el;
00100   groups->last = &el->next;
00101 }
00102 
00103 void
00104 reggroup_add (struct gdbarch *gdbarch, struct reggroup *group)
00105 {
00106   struct reggroups *groups = gdbarch_data (gdbarch, reggroups_data);
00107 
00108   if (groups == NULL)
00109     {
00110       /* ULGH, called during architecture initialization.  Patch
00111          things up.  */
00112       groups = reggroups_init (gdbarch);
00113       deprecated_set_gdbarch_data (gdbarch, reggroups_data, groups);
00114     }
00115   add_group (groups, group,
00116              GDBARCH_OBSTACK_ZALLOC (gdbarch, struct reggroup_el));
00117 }
00118 
00119 /* The default register groups for an architecture.  */
00120 
00121 static struct reggroups default_groups = { NULL, &default_groups.first };
00122 
00123 /* A register group iterator.  */
00124 
00125 struct reggroup *
00126 reggroup_next (struct gdbarch *gdbarch, struct reggroup *last)
00127 {
00128   struct reggroups *groups;
00129   struct reggroup_el *el;
00130 
00131   /* Don't allow this function to be called during architecture
00132      creation.  If there are no groups, use the default groups list.  */
00133   groups = gdbarch_data (gdbarch, reggroups_data);
00134   gdb_assert (groups != NULL);
00135   if (groups->first == NULL)
00136     groups = &default_groups;
00137 
00138   /* Return the first/next reggroup.  */
00139   if (last == NULL)
00140     return groups->first->group;
00141   for (el = groups->first; el != NULL; el = el->next)
00142     {
00143       if (el->group == last)
00144         {
00145           if (el->next != NULL)
00146             return el->next->group;
00147           else
00148             return NULL;
00149         }
00150     }
00151   return NULL;
00152 }
00153 
00154 /* Is REGNUM a member of REGGROUP?  */
00155 int
00156 default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
00157                              struct reggroup *group)
00158 {
00159   int vector_p;
00160   int float_p;
00161   int raw_p;
00162 
00163   if (gdbarch_register_name (gdbarch, regnum) == NULL
00164       || *gdbarch_register_name (gdbarch, regnum) == '\0')
00165     return 0;
00166   if (group == all_reggroup)
00167     return 1;
00168   vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
00169   float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
00170   raw_p = regnum < gdbarch_num_regs (gdbarch);
00171   if (group == float_reggroup)
00172     return float_p;
00173   if (group == vector_reggroup)
00174     return vector_p;
00175   if (group == general_reggroup)
00176     return (!vector_p && !float_p);
00177   if (group == save_reggroup || group == restore_reggroup)
00178     return raw_p;
00179   return 0;   
00180 }
00181 
00182 /* Dump out a table of register groups for the current architecture.  */
00183 
00184 static void
00185 reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
00186 {
00187   struct reggroup *group = NULL;
00188 
00189   do
00190     {
00191       /* Group name.  */
00192       {
00193         const char *name;
00194 
00195         if (group == NULL)
00196           name = "Group";
00197         else
00198           name = reggroup_name (group);
00199         fprintf_unfiltered (file, " %-10s", name);
00200       }
00201       
00202       /* Group type.  */
00203       {
00204         const char *type;
00205 
00206         if (group == NULL)
00207           type = "Type";
00208         else
00209           {
00210             switch (reggroup_type (group))
00211               {
00212               case USER_REGGROUP:
00213                 type = "user";
00214                 break;
00215               case INTERNAL_REGGROUP:
00216                 type = "internal";
00217                 break;
00218               default:
00219                 internal_error (__FILE__, __LINE__, _("bad switch"));
00220               }
00221           }
00222         fprintf_unfiltered (file, " %-10s", type);
00223       }
00224 
00225       /* Note: If you change this, be sure to also update the
00226          documentation.  */
00227       
00228       fprintf_unfiltered (file, "\n");
00229 
00230       group = reggroup_next (gdbarch, group);
00231     }
00232   while (group != NULL);
00233 }
00234 
00235 static void
00236 maintenance_print_reggroups (char *args, int from_tty)
00237 {
00238   struct gdbarch *gdbarch = get_current_arch ();
00239 
00240   if (args == NULL)
00241     reggroups_dump (gdbarch, gdb_stdout);
00242   else
00243     {
00244       struct cleanup *cleanups;
00245       struct ui_file *file = gdb_fopen (args, "w");
00246 
00247       if (file == NULL)
00248         perror_with_name (_("maintenance print reggroups"));
00249       cleanups = make_cleanup_ui_file_delete (file);
00250       reggroups_dump (gdbarch, file);
00251       do_cleanups (cleanups);
00252     }
00253 }
00254 
00255 /* Pre-defined register groups.  */
00256 static struct reggroup general_group = { "general", USER_REGGROUP };
00257 static struct reggroup float_group = { "float", USER_REGGROUP };
00258 static struct reggroup system_group = { "system", USER_REGGROUP };
00259 static struct reggroup vector_group = { "vector", USER_REGGROUP };
00260 static struct reggroup all_group = { "all", USER_REGGROUP };
00261 static struct reggroup save_group = { "save", INTERNAL_REGGROUP };
00262 static struct reggroup restore_group = { "restore", INTERNAL_REGGROUP };
00263 
00264 struct reggroup *const general_reggroup = &general_group;
00265 struct reggroup *const float_reggroup = &float_group;
00266 struct reggroup *const system_reggroup = &system_group;
00267 struct reggroup *const vector_reggroup = &vector_group;
00268 struct reggroup *const all_reggroup = &all_group;
00269 struct reggroup *const save_reggroup = &save_group;
00270 struct reggroup *const restore_reggroup = &restore_group;
00271 
00272 extern initialize_file_ftype _initialize_reggroup; /* -Wmissing-prototypes */
00273 
00274 void
00275 _initialize_reggroup (void)
00276 {
00277   reggroups_data = gdbarch_data_register_post_init (reggroups_init);
00278 
00279   /* The pre-defined list of groups.  */
00280   add_group (&default_groups, general_reggroup, XMALLOC (struct reggroup_el));
00281   add_group (&default_groups, float_reggroup, XMALLOC (struct reggroup_el));
00282   add_group (&default_groups, system_reggroup, XMALLOC (struct reggroup_el));
00283   add_group (&default_groups, vector_reggroup, XMALLOC (struct reggroup_el));
00284   add_group (&default_groups, all_reggroup, XMALLOC (struct reggroup_el));
00285   add_group (&default_groups, save_reggroup, XMALLOC (struct reggroup_el));
00286   add_group (&default_groups, restore_reggroup, XMALLOC (struct reggroup_el));
00287 
00288   add_cmd ("reggroups", class_maintenance,
00289            maintenance_print_reggroups, _("\
00290 Print the internal register group names.\n\
00291 Takes an optional file parameter."),
00292            &maintenanceprintlist);
00293 
00294 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines