Home | History | Annotate | Line # | Download | only in gdbserver
regcache.cc revision 1.1.1.2
      1 /* Register support routines for the remote server for GDB.
      2    Copyright (C) 2001-2023 Free Software Foundation, Inc.
      3 
      4    This file is part of GDB.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18 
     19 #include "server.h"
     20 #include "regdef.h"
     21 #include "gdbthread.h"
     22 #include "tdesc.h"
     23 #include "gdbsupport/rsp-low.h"
     24 #ifndef IN_PROCESS_AGENT
     25 
     26 struct regcache *
     27 get_thread_regcache (struct thread_info *thread, int fetch)
     28 {
     29   struct regcache *regcache;
     30 
     31   regcache = thread_regcache_data (thread);
     32 
     33   /* Threads' regcaches are created lazily, because biarch targets add
     34      the main thread/lwp before seeing it stop for the first time, and
     35      it is only after the target sees the thread stop for the first
     36      time that the target has a chance of determining the process's
     37      architecture.  IOW, when we first add the process's main thread
     38      we don't know which architecture/tdesc its regcache should
     39      have.  */
     40   if (regcache == NULL)
     41     {
     42       struct process_info *proc = get_thread_process (thread);
     43 
     44       gdb_assert (proc->tdesc != NULL);
     45 
     46       regcache = new_register_cache (proc->tdesc);
     47       set_thread_regcache_data (thread, regcache);
     48     }
     49 
     50   if (fetch && regcache->registers_valid == 0)
     51     {
     52       scoped_restore_current_thread restore_thread;
     53 
     54       switch_to_thread (thread);
     55       /* Invalidate all registers, to prevent stale left-overs.  */
     56       memset (regcache->register_status, REG_UNAVAILABLE,
     57 	      regcache->tdesc->reg_defs.size ());
     58       fetch_inferior_registers (regcache, -1);
     59       regcache->registers_valid = 1;
     60     }
     61 
     62   return regcache;
     63 }
     64 
     65 /* See gdbsupport/common-regcache.h.  */
     66 
     67 struct regcache *
     68 get_thread_regcache_for_ptid (ptid_t ptid)
     69 {
     70   return get_thread_regcache (find_thread_ptid (ptid), 1);
     71 }
     72 
     73 void
     74 regcache_invalidate_thread (struct thread_info *thread)
     75 {
     76   struct regcache *regcache;
     77 
     78   regcache = thread_regcache_data (thread);
     79 
     80   if (regcache == NULL)
     81     return;
     82 
     83   if (regcache->registers_valid)
     84     {
     85       scoped_restore_current_thread restore_thread;
     86 
     87       switch_to_thread (thread);
     88       store_inferior_registers (regcache, -1);
     89     }
     90 
     91   regcache->registers_valid = 0;
     92 }
     93 
     94 /* See regcache.h.  */
     95 
     96 void
     97 regcache_invalidate_pid (int pid)
     98 {
     99   /* Only invalidate the regcaches of threads of this process.  */
    100   for_each_thread (pid, regcache_invalidate_thread);
    101 }
    102 
    103 /* See regcache.h.  */
    104 
    105 void
    106 regcache_invalidate (void)
    107 {
    108   /* Only update the threads of the current process.  */
    109   int pid = current_thread->id.pid ();
    110 
    111   regcache_invalidate_pid (pid);
    112 }
    113 
    114 #endif
    115 
    116 struct regcache *
    117 init_register_cache (struct regcache *regcache,
    118 		     const struct target_desc *tdesc,
    119 		     unsigned char *regbuf)
    120 {
    121   if (regbuf == NULL)
    122     {
    123 #ifndef IN_PROCESS_AGENT
    124       /* Make sure to zero-initialize the register cache when it is
    125 	 created, in case there are registers the target never
    126 	 fetches.  This way they'll read as zero instead of
    127 	 garbage.  */
    128       regcache->tdesc = tdesc;
    129       regcache->registers
    130 	= (unsigned char *) xcalloc (1, tdesc->registers_size);
    131       regcache->registers_owned = 1;
    132       regcache->register_status
    133 	= (unsigned char *) xmalloc (tdesc->reg_defs.size ());
    134       memset ((void *) regcache->register_status, REG_UNAVAILABLE,
    135 	      tdesc->reg_defs.size ());
    136 #else
    137       gdb_assert_not_reached ("can't allocate memory from the heap");
    138 #endif
    139     }
    140   else
    141     {
    142       regcache->tdesc = tdesc;
    143       regcache->registers = regbuf;
    144       regcache->registers_owned = 0;
    145 #ifndef IN_PROCESS_AGENT
    146       regcache->register_status = NULL;
    147 #endif
    148     }
    149 
    150   regcache->registers_valid = 0;
    151 
    152   return regcache;
    153 }
    154 
    155 #ifndef IN_PROCESS_AGENT
    156 
    157 struct regcache *
    158 new_register_cache (const struct target_desc *tdesc)
    159 {
    160   struct regcache *regcache = new struct regcache;
    161 
    162   gdb_assert (tdesc->registers_size != 0);
    163 
    164   return init_register_cache (regcache, tdesc, NULL);
    165 }
    166 
    167 void
    168 free_register_cache (struct regcache *regcache)
    169 {
    170   if (regcache)
    171     {
    172       if (regcache->registers_owned)
    173 	free (regcache->registers);
    174       free (regcache->register_status);
    175       delete regcache;
    176     }
    177 }
    178 
    179 #endif
    180 
    181 void
    182 regcache_cpy (struct regcache *dst, struct regcache *src)
    183 {
    184   gdb_assert (src != NULL && dst != NULL);
    185   gdb_assert (src->tdesc == dst->tdesc);
    186   gdb_assert (src != dst);
    187 
    188   memcpy (dst->registers, src->registers, src->tdesc->registers_size);
    189 #ifndef IN_PROCESS_AGENT
    190   if (dst->register_status != NULL && src->register_status != NULL)
    191     memcpy (dst->register_status, src->register_status,
    192 	    src->tdesc->reg_defs.size ());
    193 #endif
    194   dst->registers_valid = src->registers_valid;
    195 }
    196 
    197 /* Return a reference to the description of register N.  */
    198 
    199 static const struct gdb::reg &
    200 find_register_by_number (const struct target_desc *tdesc, int n)
    201 {
    202   return tdesc->reg_defs[n];
    203 }
    204 
    205 #ifndef IN_PROCESS_AGENT
    206 
    207 void
    208 registers_to_string (struct regcache *regcache, char *buf)
    209 {
    210   unsigned char *registers = regcache->registers;
    211   const struct target_desc *tdesc = regcache->tdesc;
    212 
    213   for (int i = 0; i < tdesc->reg_defs.size (); ++i)
    214     {
    215       if (regcache->register_status[i] == REG_VALID)
    216 	{
    217 	  bin2hex (registers, buf, register_size (tdesc, i));
    218 	  buf += register_size (tdesc, i) * 2;
    219 	}
    220       else
    221 	{
    222 	  memset (buf, 'x', register_size (tdesc, i) * 2);
    223 	  buf += register_size (tdesc, i) * 2;
    224 	}
    225       registers += register_size (tdesc, i);
    226     }
    227   *buf = '\0';
    228 }
    229 
    230 void
    231 registers_from_string (struct regcache *regcache, char *buf)
    232 {
    233   int len = strlen (buf);
    234   unsigned char *registers = regcache->registers;
    235   const struct target_desc *tdesc = regcache->tdesc;
    236 
    237   if (len != tdesc->registers_size * 2)
    238     {
    239       warning ("Wrong sized register packet (expected %d bytes, got %d)",
    240 	       2 * tdesc->registers_size, len);
    241       if (len > tdesc->registers_size * 2)
    242 	len = tdesc->registers_size * 2;
    243     }
    244   hex2bin (buf, registers, len / 2);
    245 }
    246 
    247 /* See regcache.h */
    248 
    249 gdb::optional<int>
    250 find_regno_no_throw (const struct target_desc *tdesc, const char *name)
    251 {
    252   for (int i = 0; i < tdesc->reg_defs.size (); ++i)
    253     {
    254       if (strcmp (name, find_register_by_number (tdesc, i).name) == 0)
    255 	return i;
    256     }
    257   return {};
    258 }
    259 
    260 int
    261 find_regno (const struct target_desc *tdesc, const char *name)
    262 {
    263   gdb::optional<int> regnum = find_regno_no_throw (tdesc, name);
    264 
    265   if (regnum.has_value ())
    266     return *regnum;
    267 
    268   internal_error ("Unknown register %s requested", name);
    269 }
    270 
    271 static void
    272 free_register_cache_thread (struct thread_info *thread)
    273 {
    274   struct regcache *regcache = thread_regcache_data (thread);
    275 
    276   if (regcache != NULL)
    277     {
    278       regcache_invalidate_thread (thread);
    279       free_register_cache (regcache);
    280       set_thread_regcache_data (thread, NULL);
    281     }
    282 }
    283 
    284 void
    285 regcache_release (void)
    286 {
    287   /* Flush and release all pre-existing register caches.  */
    288   for_each_thread (free_register_cache_thread);
    289 }
    290 #endif
    291 
    292 int
    293 register_cache_size (const struct target_desc *tdesc)
    294 {
    295   return tdesc->registers_size;
    296 }
    297 
    298 int
    299 register_size (const struct target_desc *tdesc, int n)
    300 {
    301   return find_register_by_number (tdesc, n).size / 8;
    302 }
    303 
    304 /* See gdbsupport/common-regcache.h.  */
    305 
    306 int
    307 regcache_register_size (const struct regcache *regcache, int n)
    308 {
    309   return register_size (regcache->tdesc, n);
    310 }
    311 
    312 static unsigned char *
    313 register_data (const struct regcache *regcache, int n)
    314 {
    315   return (regcache->registers
    316 	  + find_register_by_number (regcache->tdesc, n).offset / 8);
    317 }
    318 
    319 void
    320 supply_register (struct regcache *regcache, int n, const void *buf)
    321 {
    322   return regcache->raw_supply (n, buf);
    323 }
    324 
    325 /* See gdbsupport/common-regcache.h.  */
    326 
    327 void
    328 regcache::raw_supply (int n, const void *buf)
    329 {
    330   if (buf)
    331     {
    332       memcpy (register_data (this, n), buf, register_size (tdesc, n));
    333 #ifndef IN_PROCESS_AGENT
    334       if (register_status != NULL)
    335 	register_status[n] = REG_VALID;
    336 #endif
    337     }
    338   else
    339     {
    340       memset (register_data (this, n), 0, register_size (tdesc, n));
    341 #ifndef IN_PROCESS_AGENT
    342       if (register_status != NULL)
    343 	register_status[n] = REG_UNAVAILABLE;
    344 #endif
    345     }
    346 }
    347 
    348 /* Supply register N with value zero to REGCACHE.  */
    349 
    350 void
    351 supply_register_zeroed (struct regcache *regcache, int n)
    352 {
    353   memset (register_data (regcache, n), 0,
    354 	  register_size (regcache->tdesc, n));
    355 #ifndef IN_PROCESS_AGENT
    356   if (regcache->register_status != NULL)
    357     regcache->register_status[n] = REG_VALID;
    358 #endif
    359 }
    360 
    361 #ifndef IN_PROCESS_AGENT
    362 
    363 /* Supply register called NAME with value zero to REGCACHE.  */
    364 
    365 void
    366 supply_register_by_name_zeroed (struct regcache *regcache,
    367 				const char *name)
    368 {
    369   supply_register_zeroed (regcache, find_regno (regcache->tdesc, name));
    370 }
    371 
    372 #endif
    373 
    374 /* Supply the whole register set whose contents are stored in BUF, to
    375    REGCACHE.  If BUF is NULL, all the registers' values are recorded
    376    as unavailable.  */
    377 
    378 void
    379 supply_regblock (struct regcache *regcache, const void *buf)
    380 {
    381   if (buf)
    382     {
    383       const struct target_desc *tdesc = regcache->tdesc;
    384 
    385       memcpy (regcache->registers, buf, tdesc->registers_size);
    386 #ifndef IN_PROCESS_AGENT
    387       {
    388 	int i;
    389 
    390 	for (i = 0; i < tdesc->reg_defs.size (); i++)
    391 	  regcache->register_status[i] = REG_VALID;
    392       }
    393 #endif
    394     }
    395   else
    396     {
    397       const struct target_desc *tdesc = regcache->tdesc;
    398 
    399       memset (regcache->registers, 0, tdesc->registers_size);
    400 #ifndef IN_PROCESS_AGENT
    401       {
    402 	int i;
    403 
    404 	for (i = 0; i < tdesc->reg_defs.size (); i++)
    405 	  regcache->register_status[i] = REG_UNAVAILABLE;
    406       }
    407 #endif
    408     }
    409 }
    410 
    411 #ifndef IN_PROCESS_AGENT
    412 
    413 void
    414 supply_register_by_name (struct regcache *regcache,
    415 			 const char *name, const void *buf)
    416 {
    417   supply_register (regcache, find_regno (regcache->tdesc, name), buf);
    418 }
    419 
    420 #endif
    421 
    422 void
    423 collect_register (struct regcache *regcache, int n, void *buf)
    424 {
    425   regcache->raw_collect (n, buf);
    426 }
    427 
    428 /* See gdbsupport/common-regcache.h.  */
    429 
    430 void
    431 regcache::raw_collect (int n, void *buf) const
    432 {
    433   memcpy (buf, register_data (this, n), register_size (tdesc, n));
    434 }
    435 
    436 enum register_status
    437 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
    438 			    ULONGEST *val)
    439 {
    440   int size;
    441 
    442   gdb_assert (regcache != NULL);
    443   gdb_assert (regnum >= 0
    444 	      && regnum < regcache->tdesc->reg_defs.size ());
    445 
    446   size = register_size (regcache->tdesc, regnum);
    447 
    448   if (size > (int) sizeof (ULONGEST))
    449     error (_("That operation is not available on integers of more than"
    450 	    "%d bytes."),
    451 	  (int) sizeof (ULONGEST));
    452 
    453   *val = 0;
    454   collect_register (regcache, regnum, val);
    455 
    456   return REG_VALID;
    457 }
    458 
    459 #ifndef IN_PROCESS_AGENT
    460 
    461 /* See regcache.h.  */
    462 
    463 ULONGEST
    464 regcache_raw_get_unsigned_by_name (struct regcache *regcache,
    465 				   const char *name)
    466 {
    467   return regcache_raw_get_unsigned (regcache,
    468 				    find_regno (regcache->tdesc, name));
    469 }
    470 
    471 void
    472 collect_register_as_string (struct regcache *regcache, int n, char *buf)
    473 {
    474   bin2hex (register_data (regcache, n), buf,
    475 	   register_size (regcache->tdesc, n));
    476 }
    477 
    478 void
    479 collect_register_by_name (struct regcache *regcache,
    480 			  const char *name, void *buf)
    481 {
    482   collect_register (regcache, find_regno (regcache->tdesc, name), buf);
    483 }
    484 
    485 /* Special handling for register PC.  */
    486 
    487 CORE_ADDR
    488 regcache_read_pc (struct regcache *regcache)
    489 {
    490   return the_target->read_pc (regcache);
    491 }
    492 
    493 void
    494 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
    495 {
    496   the_target->write_pc (regcache, pc);
    497 }
    498 
    499 #endif
    500 
    501 /* See gdbsupport/common-regcache.h.  */
    502 
    503 enum register_status
    504 regcache::get_register_status (int regnum) const
    505 {
    506 #ifndef IN_PROCESS_AGENT
    507   gdb_assert (regnum >= 0 && regnum < tdesc->reg_defs.size ());
    508   return (enum register_status) (register_status[regnum]);
    509 #else
    510   return REG_VALID;
    511 #endif
    512 }
    513 
    514 /* See gdbsupport/common-regcache.h.  */
    515 
    516 bool
    517 regcache::raw_compare (int regnum, const void *buf, int offset) const
    518 {
    519   gdb_assert (buf != NULL);
    520 
    521   const unsigned char *regbuf = register_data (this, regnum);
    522   int size = register_size (tdesc, regnum);
    523   gdb_assert (size >= offset);
    524 
    525   return (memcmp (buf, regbuf + offset, size - offset) == 0);
    526 }
    527