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