1130803Smarcel/* User visible, per-frame registers, for GDB, the GNU debugger. 2130803Smarcel 3130803Smarcel Copyright 2002, 2003, 2004 Free Software Foundation, Inc. 4130803Smarcel 5130803Smarcel Contributed by Red Hat. 6130803Smarcel 7130803Smarcel This file is part of GDB. 8130803Smarcel 9130803Smarcel This program is free software; you can redistribute it and/or modify 10130803Smarcel it under the terms of the GNU General Public License as published by 11130803Smarcel the Free Software Foundation; either version 2 of the License, or 12130803Smarcel (at your option) any later version. 13130803Smarcel 14130803Smarcel This program is distributed in the hope that it will be useful, 15130803Smarcel but WITHOUT ANY WARRANTY; without even the implied warranty of 16130803Smarcel MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17130803Smarcel GNU General Public License for more details. 18130803Smarcel 19130803Smarcel You should have received a copy of the GNU General Public License 20130803Smarcel along with this program; if not, write to the Free Software 21130803Smarcel Foundation, Inc., 59 Temple Place - Suite 330, 22130803Smarcel Boston, MA 02111-1307, USA. */ 23130803Smarcel 24130803Smarcel#include "defs.h" 25130803Smarcel#include "user-regs.h" 26130803Smarcel#include "gdbtypes.h" 27130803Smarcel#include "gdb_string.h" 28130803Smarcel#include "gdb_assert.h" 29130803Smarcel#include "frame.h" 30130803Smarcel 31130803Smarcel/* A table of user registers. 32130803Smarcel 33130803Smarcel User registers have regnum's that live above of the range [0 34130803Smarcel .. NUM_REGS + NUM_PSEUDO_REGS) (which is controlled by the target). 35130803Smarcel The target should never see a user register's regnum value. 36130803Smarcel 37130803Smarcel Always append, never delete. By doing this, the relative regnum 38130803Smarcel (offset from NUM_REGS + NUM_PSEUDO_REGS) assigned to each user 39130803Smarcel register never changes. */ 40130803Smarcel 41130803Smarcelstruct user_reg 42130803Smarcel{ 43130803Smarcel const char *name; 44130803Smarcel struct value *(*read) (struct frame_info * frame); 45130803Smarcel struct user_reg *next; 46130803Smarcel}; 47130803Smarcel 48130803Smarcel/* This structure is named gdb_user_regs instead of user_regs to avoid 49130803Smarcel conflicts with any "struct user_regs" in system headers. For instance, 50130803Smarcel on ARM GNU/Linux native builds, nm-linux.h includes <signal.h> includes 51130803Smarcel <sys/ucontext.h> includes <sys/procfs.h> includes <sys/user.h>, which 52130803Smarcel declares "struct user_regs". */ 53130803Smarcel 54130803Smarcelstruct gdb_user_regs 55130803Smarcel{ 56130803Smarcel struct user_reg *first; 57130803Smarcel struct user_reg **last; 58130803Smarcel}; 59130803Smarcel 60130803Smarcelstatic void 61130803Smarcelappend_user_reg (struct gdb_user_regs *regs, const char *name, 62130803Smarcel user_reg_read_ftype *read, struct user_reg *reg) 63130803Smarcel{ 64130803Smarcel /* The caller is responsible for allocating memory needed to store 65130803Smarcel the register. By doing this, the function can operate on a 66130803Smarcel register list stored in the common heap or a specific obstack. */ 67130803Smarcel gdb_assert (reg != NULL); 68130803Smarcel reg->name = name; 69130803Smarcel reg->read = read; 70130803Smarcel reg->next = NULL; 71130803Smarcel (*regs->last) = reg; 72130803Smarcel regs->last = &(*regs->last)->next; 73130803Smarcel} 74130803Smarcel 75130803Smarcel/* An array of the builtin user registers. */ 76130803Smarcel 77130803Smarcelstatic struct gdb_user_regs builtin_user_regs = { NULL, &builtin_user_regs.first }; 78130803Smarcel 79130803Smarcelvoid 80130803Smarceluser_reg_add_builtin (const char *name, user_reg_read_ftype *read) 81130803Smarcel{ 82130803Smarcel append_user_reg (&builtin_user_regs, name, read, 83130803Smarcel XMALLOC (struct user_reg)); 84130803Smarcel} 85130803Smarcel 86130803Smarcel/* Per-architecture user registers. Start with the builtin user 87130803Smarcel registers and then, again, append. */ 88130803Smarcel 89130803Smarcelstatic struct gdbarch_data *user_regs_data; 90130803Smarcel 91130803Smarcelstatic void * 92130803Smarceluser_regs_init (struct gdbarch *gdbarch) 93130803Smarcel{ 94130803Smarcel struct user_reg *reg; 95130803Smarcel struct gdb_user_regs *regs = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct gdb_user_regs); 96130803Smarcel regs->last = ®s->first; 97130803Smarcel for (reg = builtin_user_regs.first; reg != NULL; reg = reg->next) 98130803Smarcel append_user_reg (regs, reg->name, reg->read, 99130803Smarcel GDBARCH_OBSTACK_ZALLOC (gdbarch, struct user_reg)); 100130803Smarcel return regs; 101130803Smarcel} 102130803Smarcel 103130803Smarcelvoid 104130803Smarceluser_reg_add (struct gdbarch *gdbarch, const char *name, 105130803Smarcel user_reg_read_ftype *read) 106130803Smarcel{ 107130803Smarcel struct gdb_user_regs *regs = gdbarch_data (gdbarch, user_regs_data); 108130803Smarcel if (regs == NULL) 109130803Smarcel { 110130803Smarcel /* ULGH, called during architecture initialization. Patch 111130803Smarcel things up. */ 112130803Smarcel regs = user_regs_init (gdbarch); 113130803Smarcel set_gdbarch_data (gdbarch, user_regs_data, regs); 114130803Smarcel } 115130803Smarcel append_user_reg (regs, name, read, 116130803Smarcel GDBARCH_OBSTACK_ZALLOC (gdbarch, struct user_reg)); 117130803Smarcel} 118130803Smarcel 119130803Smarcelint 120130803Smarceluser_reg_map_name_to_regnum (struct gdbarch *gdbarch, const char *name, 121130803Smarcel int len) 122130803Smarcel{ 123130803Smarcel /* Make life easy, set the len to something reasonable. */ 124130803Smarcel if (len < 0) 125130803Smarcel len = strlen (name); 126130803Smarcel 127130803Smarcel /* Search register name space first - always let an architecture 128130803Smarcel specific register override the user registers. */ 129130803Smarcel { 130130803Smarcel int i; 131130803Smarcel int maxregs = (gdbarch_num_regs (gdbarch) 132130803Smarcel + gdbarch_num_pseudo_regs (gdbarch)); 133130803Smarcel for (i = 0; i < maxregs; i++) 134130803Smarcel { 135130803Smarcel const char *regname = gdbarch_register_name (gdbarch, i); 136130803Smarcel if (regname != NULL && len == strlen (regname) 137130803Smarcel && strncmp (regname, name, len) == 0) 138130803Smarcel { 139130803Smarcel return i; 140130803Smarcel } 141130803Smarcel } 142130803Smarcel } 143130803Smarcel 144130803Smarcel /* Search the user name space. */ 145130803Smarcel { 146130803Smarcel struct gdb_user_regs *regs = gdbarch_data (gdbarch, user_regs_data); 147130803Smarcel struct user_reg *reg; 148130803Smarcel int nr; 149130803Smarcel for (nr = 0, reg = regs->first; reg != NULL; reg = reg->next, nr++) 150130803Smarcel { 151130803Smarcel if ((len < 0 && strcmp (reg->name, name)) 152130803Smarcel || (len == strlen (reg->name) 153130803Smarcel && strncmp (reg->name, name, len) == 0)) 154130803Smarcel return NUM_REGS + NUM_PSEUDO_REGS + nr; 155130803Smarcel } 156130803Smarcel } 157130803Smarcel 158130803Smarcel return -1; 159130803Smarcel} 160130803Smarcel 161130803Smarcelstatic struct user_reg * 162130803Smarcelusernum_to_user_reg (struct gdbarch *gdbarch, int usernum) 163130803Smarcel{ 164130803Smarcel struct gdb_user_regs *regs = gdbarch_data (gdbarch, user_regs_data); 165130803Smarcel struct user_reg *reg; 166130803Smarcel for (reg = regs->first; reg != NULL; reg = reg->next) 167130803Smarcel { 168130803Smarcel if (usernum == 0) 169130803Smarcel return reg; 170130803Smarcel usernum--; 171130803Smarcel } 172130803Smarcel return NULL; 173130803Smarcel} 174130803Smarcel 175130803Smarcelconst char * 176130803Smarceluser_reg_map_regnum_to_name (struct gdbarch *gdbarch, int regnum) 177130803Smarcel{ 178130803Smarcel int maxregs = (gdbarch_num_regs (gdbarch) 179130803Smarcel + gdbarch_num_pseudo_regs (gdbarch)); 180130803Smarcel if (regnum < 0) 181130803Smarcel return NULL; 182130803Smarcel else if (regnum < maxregs) 183130803Smarcel return gdbarch_register_name (gdbarch, regnum); 184130803Smarcel else 185130803Smarcel { 186130803Smarcel struct user_reg *reg = usernum_to_user_reg (gdbarch, regnum - maxregs); 187130803Smarcel if (reg == NULL) 188130803Smarcel return NULL; 189130803Smarcel else 190130803Smarcel return reg->name; 191130803Smarcel } 192130803Smarcel} 193130803Smarcel 194130803Smarcelstruct value * 195130803Smarcelvalue_of_user_reg (int regnum, struct frame_info *frame) 196130803Smarcel{ 197130803Smarcel struct gdbarch *gdbarch = get_frame_arch (frame); 198130803Smarcel int maxregs = (gdbarch_num_regs (gdbarch) 199130803Smarcel + gdbarch_num_pseudo_regs (gdbarch)); 200130803Smarcel struct user_reg *reg = usernum_to_user_reg (gdbarch, regnum - maxregs); 201130803Smarcel gdb_assert (reg != NULL); 202130803Smarcel return reg->read (frame); 203130803Smarcel} 204130803Smarcel 205130803Smarcelextern initialize_file_ftype _initialize_user_regs; /* -Wmissing-prototypes */ 206130803Smarcel 207130803Smarcelvoid 208130803Smarcel_initialize_user_regs (void) 209130803Smarcel{ 210130803Smarcel user_regs_data = register_gdbarch_data (user_regs_init); 211130803Smarcel} 212