GDB (API)
/home/stan/gdb/src/gdb/user-regs.c
Go to the documentation of this file.
00001 /* User visible, per-frame registers, 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 "user-regs.h"
00024 #include "gdbtypes.h"
00025 #include "gdb_string.h"
00026 #include "gdb_assert.h"
00027 #include "frame.h"
00028 
00029 /* A table of user registers.
00030 
00031    User registers have regnum's that live above of the range [0
00032    .. gdbarch_num_regs + gdbarch_num_pseudo_regs)
00033    (which is controlled by the target).
00034    The target should never see a user register's regnum value.
00035 
00036    Always append, never delete.  By doing this, the relative regnum
00037    (offset from gdbarch_num_regs + gdbarch_num_pseudo_regs)
00038     assigned to each user  register never changes.  */
00039 
00040 struct user_reg
00041 {
00042   const char *name;
00043   struct value *(*read) (struct frame_info * frame, const void *baton);
00044   const void *baton;
00045   struct user_reg *next;
00046 };
00047 
00048 /* This structure is named gdb_user_regs instead of user_regs to avoid
00049    conflicts with any "struct user_regs" in system headers.  For instance,
00050    on ARM GNU/Linux native builds, nm-linux.h includes <signal.h> includes
00051    <sys/ucontext.h> includes <sys/procfs.h> includes <sys/user.h>, which
00052    declares "struct user_regs".  */
00053 
00054 struct gdb_user_regs
00055 {
00056   struct user_reg *first;
00057   struct user_reg **last;
00058 };
00059 
00060 static void
00061 append_user_reg (struct gdb_user_regs *regs, const char *name,
00062                  user_reg_read_ftype *read, const void *baton,
00063                  struct user_reg *reg)
00064 {
00065   /* The caller is responsible for allocating memory needed to store
00066      the register.  By doing this, the function can operate on a
00067      register list stored in the common heap or a specific obstack.  */
00068   gdb_assert (reg != NULL);
00069   reg->name = name;
00070   reg->read = read;
00071   reg->baton = baton;
00072   reg->next = NULL;
00073   (*regs->last) = reg;
00074   regs->last = &(*regs->last)->next;
00075 }
00076 
00077 /* An array of the builtin user registers.  */
00078 
00079 static struct gdb_user_regs builtin_user_regs = {
00080   NULL, &builtin_user_regs.first
00081 };
00082 
00083 void
00084 user_reg_add_builtin (const char *name, user_reg_read_ftype *read,
00085                       const void *baton)
00086 {
00087   append_user_reg (&builtin_user_regs, name, read, baton,
00088                    XMALLOC (struct user_reg));
00089 }
00090 
00091 /* Per-architecture user registers.  Start with the builtin user
00092    registers and then, again, append.  */
00093 
00094 static struct gdbarch_data *user_regs_data;
00095 
00096 static void *
00097 user_regs_init (struct gdbarch *gdbarch)
00098 {
00099   struct user_reg *reg;
00100   struct gdb_user_regs *regs 
00101     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct gdb_user_regs);
00102 
00103   regs->last = &regs->first;
00104   for (reg = builtin_user_regs.first; reg != NULL; reg = reg->next)
00105     append_user_reg (regs, reg->name, reg->read, reg->baton,
00106                      GDBARCH_OBSTACK_ZALLOC (gdbarch, struct user_reg));
00107   return regs;
00108 }
00109 
00110 void
00111 user_reg_add (struct gdbarch *gdbarch, const char *name,
00112               user_reg_read_ftype *read, const void *baton)
00113 {
00114   struct gdb_user_regs *regs = gdbarch_data (gdbarch, user_regs_data);
00115 
00116   if (regs == NULL)
00117     {
00118       /* ULGH, called during architecture initialization.  Patch
00119          things up.  */
00120       regs = user_regs_init (gdbarch);
00121       deprecated_set_gdbarch_data (gdbarch, user_regs_data, regs);
00122     }
00123   append_user_reg (regs, name, read, baton,
00124                    GDBARCH_OBSTACK_ZALLOC (gdbarch, struct user_reg));
00125 }
00126 
00127 int
00128 user_reg_map_name_to_regnum (struct gdbarch *gdbarch, const char *name,
00129                              int len)
00130 {
00131   /* Make life easy, set the len to something reasonable.  */
00132   if (len < 0)
00133     len = strlen (name);
00134 
00135   /* Search register name space first - always let an architecture
00136      specific register override the user registers.  */
00137   {
00138     int i;
00139     int maxregs = (gdbarch_num_regs (gdbarch)
00140                    + gdbarch_num_pseudo_regs (gdbarch));
00141 
00142     for (i = 0; i < maxregs; i++)
00143       {
00144         const char *regname = gdbarch_register_name (gdbarch, i);
00145 
00146         if (regname != NULL && len == strlen (regname)
00147             && strncmp (regname, name, len) == 0)
00148           {
00149             return i;
00150           }
00151       }
00152   }
00153 
00154   /* Search the user name space.  */
00155   {
00156     struct gdb_user_regs *regs = gdbarch_data (gdbarch, user_regs_data);
00157     struct user_reg *reg;
00158     int nr;
00159 
00160     for (nr = 0, reg = regs->first; reg != NULL; reg = reg->next, nr++)
00161       {
00162         if ((len < 0 && strcmp (reg->name, name))
00163             || (len == strlen (reg->name)
00164                 && strncmp (reg->name, name, len) == 0))
00165           return gdbarch_num_regs (gdbarch)
00166                  + gdbarch_num_pseudo_regs (gdbarch) + nr;
00167       }
00168   }
00169 
00170   return -1;
00171 }
00172 
00173 static struct user_reg *
00174 usernum_to_user_reg (struct gdbarch *gdbarch, int usernum)
00175 {
00176   struct gdb_user_regs *regs = gdbarch_data (gdbarch, user_regs_data);
00177   struct user_reg *reg;
00178 
00179   for (reg = regs->first; reg != NULL; reg = reg->next)
00180     {
00181       if (usernum == 0)
00182         return reg;
00183       usernum--;
00184     }
00185   return NULL;
00186 }
00187 
00188 const char *
00189 user_reg_map_regnum_to_name (struct gdbarch *gdbarch, int regnum)
00190 {
00191   int maxregs = (gdbarch_num_regs (gdbarch)
00192                  + gdbarch_num_pseudo_regs (gdbarch));
00193 
00194   if (regnum < 0)
00195     return NULL;
00196   else if (regnum < maxregs)
00197     return gdbarch_register_name (gdbarch, regnum);
00198   else
00199     {
00200       struct user_reg *reg = usernum_to_user_reg (gdbarch, regnum - maxregs);
00201       if (reg == NULL)
00202         return NULL;
00203       else
00204         return reg->name;
00205     }
00206 }
00207 
00208 struct value *
00209 value_of_user_reg (int regnum, struct frame_info *frame)
00210 {
00211   struct gdbarch *gdbarch = get_frame_arch (frame);
00212   int maxregs = (gdbarch_num_regs (gdbarch)
00213                  + gdbarch_num_pseudo_regs (gdbarch));
00214   struct user_reg *reg = usernum_to_user_reg (gdbarch, regnum - maxregs);
00215 
00216   gdb_assert (reg != NULL);
00217   return reg->read (frame, reg->baton);
00218 }
00219 
00220 extern initialize_file_ftype _initialize_user_regs; /* -Wmissing-prototypes */
00221 
00222 void
00223 _initialize_user_regs (void)
00224 {
00225   user_regs_data = gdbarch_data_register_post_init (user_regs_init);
00226 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines