gdb/gdbserver/regcache.c - gdb

Functions defined

Source code

  1. /* Register support routines for the remote server for GDB.
  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "server.h"
  15. #include "regdef.h"
  16. #include "gdbthread.h"
  17. #include "tdesc.h"
  18. #include "rsp-low.h"
  19. #ifndef IN_PROCESS_AGENT

  20. struct regcache *
  21. get_thread_regcache (struct thread_info *thread, int fetch)
  22. {
  23.   struct regcache *regcache;

  24.   regcache = (struct regcache *) inferior_regcache_data (thread);

  25.   /* Threads' regcaches are created lazily, because biarch targets add
  26.      the main thread/lwp before seeing it stop for the first time, and
  27.      it is only after the target sees the thread stop for the first
  28.      time that the target has a chance of determining the process's
  29.      architecture.  IOW, when we first add the process's main thread
  30.      we don't know which architecture/tdesc its regcache should
  31.      have.  */
  32.   if (regcache == NULL)
  33.     {
  34.       struct process_info *proc = get_thread_process (thread);

  35.       gdb_assert (proc->tdesc != NULL);

  36.       regcache = new_register_cache (proc->tdesc);
  37.       set_inferior_regcache_data (thread, regcache);
  38.     }

  39.   if (fetch && regcache->registers_valid == 0)
  40.     {
  41.       struct thread_info *saved_thread = current_thread;

  42.       current_thread = thread;
  43.       /* Invalidate all registers, to prevent stale left-overs.  */
  44.       memset (regcache->register_status, REG_UNAVAILABLE,
  45.               regcache->tdesc->num_registers);
  46.       fetch_inferior_registers (regcache, -1);
  47.       current_thread = saved_thread;
  48.       regcache->registers_valid = 1;
  49.     }

  50.   return regcache;
  51. }

  52. /* See common/common-regcache.h.  */

  53. struct regcache *
  54. get_thread_regcache_for_ptid (ptid_t ptid)
  55. {
  56.   return get_thread_regcache (find_thread_ptid (ptid), 1);
  57. }

  58. void
  59. regcache_invalidate_thread (struct thread_info *thread)
  60. {
  61.   struct regcache *regcache;

  62.   regcache = (struct regcache *) inferior_regcache_data (thread);

  63.   if (regcache == NULL)
  64.     return;

  65.   if (regcache->registers_valid)
  66.     {
  67.       struct thread_info *saved_thread = current_thread;

  68.       current_thread = thread;
  69.       store_inferior_registers (regcache, -1);
  70.       current_thread = saved_thread;
  71.     }

  72.   regcache->registers_valid = 0;
  73. }

  74. static int
  75. regcache_invalidate_one (struct inferior_list_entry *entry,
  76.                          void *pid_p)
  77. {
  78.   struct thread_info *thread = (struct thread_info *) entry;
  79.   int pid = *(int *) pid_p;

  80.   /* Only invalidate the regcaches of threads of this process.  */
  81.   if (ptid_get_pid (entry->id) == pid)
  82.     regcache_invalidate_thread (thread);

  83.   return 0;
  84. }

  85. void
  86. regcache_invalidate (void)
  87. {
  88.   /* Only update the threads of the current process.  */
  89.   int pid = ptid_get_pid (current_thread->entry.id);

  90.   find_inferior (&all_threads, regcache_invalidate_one, &pid);
  91. }

  92. #endif

  93. struct regcache *
  94. init_register_cache (struct regcache *regcache,
  95.                      const struct target_desc *tdesc,
  96.                      unsigned char *regbuf)
  97. {
  98.   if (regbuf == NULL)
  99.     {
  100. #ifndef IN_PROCESS_AGENT
  101.       /* Make sure to zero-initialize the register cache when it is
  102.          created, in case there are registers the target never
  103.          fetches.  This way they'll read as zero instead of
  104.          garbage.  */
  105.       regcache->tdesc = tdesc;
  106.       regcache->registers = xcalloc (1, tdesc->registers_size);
  107.       regcache->registers_owned = 1;
  108.       regcache->register_status = xcalloc (1, tdesc->num_registers);
  109.       gdb_assert (REG_UNAVAILABLE == 0);
  110. #else
  111.       gdb_assert_not_reached ("can't allocate memory from the heap");
  112. #endif
  113.     }
  114.   else
  115.     {
  116.       regcache->tdesc = tdesc;
  117.       regcache->registers = regbuf;
  118.       regcache->registers_owned = 0;
  119. #ifndef IN_PROCESS_AGENT
  120.       regcache->register_status = NULL;
  121. #endif
  122.     }

  123.   regcache->registers_valid = 0;

  124.   return regcache;
  125. }

  126. #ifndef IN_PROCESS_AGENT

  127. struct regcache *
  128. new_register_cache (const struct target_desc *tdesc)
  129. {
  130.   struct regcache *regcache;

  131.   gdb_assert (tdesc->registers_size != 0);

  132.   regcache = xmalloc (sizeof (*regcache));
  133.   return init_register_cache (regcache, tdesc, NULL);
  134. }

  135. void
  136. free_register_cache (struct regcache *regcache)
  137. {
  138.   if (regcache)
  139.     {
  140.       if (regcache->registers_owned)
  141.         free (regcache->registers);
  142.       free (regcache->register_status);
  143.       free (regcache);
  144.     }
  145. }

  146. #endif

  147. void
  148. regcache_cpy (struct regcache *dst, struct regcache *src)
  149. {
  150.   gdb_assert (src != NULL && dst != NULL);
  151.   gdb_assert (src->tdesc == dst->tdesc);
  152.   gdb_assert (src != dst);

  153.   memcpy (dst->registers, src->registers, src->tdesc->registers_size);
  154. #ifndef IN_PROCESS_AGENT
  155.   if (dst->register_status != NULL && src->register_status != NULL)
  156.     memcpy (dst->register_status, src->register_status,
  157.             src->tdesc->num_registers);
  158. #endif
  159.   dst->registers_valid = src->registers_valid;
  160. }


  161. #ifndef IN_PROCESS_AGENT

  162. void
  163. registers_to_string (struct regcache *regcache, char *buf)
  164. {
  165.   unsigned char *registers = regcache->registers;
  166.   const struct target_desc *tdesc = regcache->tdesc;
  167.   int i;

  168.   for (i = 0; i < tdesc->num_registers; i++)
  169.     {
  170.       if (regcache->register_status[i] == REG_VALID)
  171.         {
  172.           bin2hex (registers, buf, register_size (tdesc, i));
  173.           buf += register_size (tdesc, i) * 2;
  174.         }
  175.       else
  176.         {
  177.           memset (buf, 'x', register_size (tdesc, i) * 2);
  178.           buf += register_size (tdesc, i) * 2;
  179.         }
  180.       registers += register_size (tdesc, i);
  181.     }
  182.   *buf = '\0';
  183. }

  184. void
  185. registers_from_string (struct regcache *regcache, char *buf)
  186. {
  187.   int len = strlen (buf);
  188.   unsigned char *registers = regcache->registers;
  189.   const struct target_desc *tdesc = regcache->tdesc;

  190.   if (len != tdesc->registers_size * 2)
  191.     {
  192.       warning ("Wrong sized register packet (expected %d bytes, got %d)",
  193.                2 * tdesc->registers_size, len);
  194.       if (len > tdesc->registers_size * 2)
  195.         len = tdesc->registers_size * 2;
  196.     }
  197.   hex2bin (buf, registers, len / 2);
  198. }

  199. struct reg *
  200. find_register_by_name (const struct target_desc *tdesc, const char *name)
  201. {
  202.   int i;

  203.   for (i = 0; i < tdesc->num_registers; i++)
  204.     if (strcmp (name, tdesc->reg_defs[i].name) == 0)
  205.       return &tdesc->reg_defs[i];
  206.   internal_error (__FILE__, __LINE__, "Unknown register %s requested",
  207.                   name);
  208. }

  209. int
  210. find_regno (const struct target_desc *tdesc, const char *name)
  211. {
  212.   int i;

  213.   for (i = 0; i < tdesc->num_registers; i++)
  214.     if (strcmp (name, tdesc->reg_defs[i].name) == 0)
  215.       return i;
  216.   internal_error (__FILE__, __LINE__, "Unknown register %s requested",
  217.                   name);
  218. }

  219. struct reg *
  220. find_register_by_number (const struct target_desc *tdesc, int n)
  221. {
  222.   return &tdesc->reg_defs[n];
  223. }

  224. #endif

  225. #ifndef IN_PROCESS_AGENT
  226. static void
  227. free_register_cache_thread (struct thread_info *thread)
  228. {
  229.   struct regcache *regcache
  230.     = (struct regcache *) inferior_regcache_data (thread);

  231.   if (regcache != NULL)
  232.     {
  233.       regcache_invalidate_thread (thread);
  234.       free_register_cache (regcache);
  235.       set_inferior_regcache_data (thread, NULL);
  236.     }
  237. }

  238. static void
  239. free_register_cache_thread_one (struct inferior_list_entry *entry)
  240. {
  241.   struct thread_info *thread = (struct thread_info *) entry;

  242.   free_register_cache_thread (thread);
  243. }

  244. void
  245. regcache_release (void)
  246. {
  247.   /* Flush and release all pre-existing register caches.  */
  248.   for_each_inferior (&all_threads, free_register_cache_thread_one);
  249. }
  250. #endif

  251. int
  252. register_cache_size (const struct target_desc *tdesc)
  253. {
  254.   return tdesc->registers_size;
  255. }

  256. int
  257. register_size (const struct target_desc *tdesc, int n)
  258. {
  259.   return tdesc->reg_defs[n].size / 8;
  260. }

  261. static unsigned char *
  262. register_data (struct regcache *regcache, int n, int fetch)
  263. {
  264.   return regcache->registers + regcache->tdesc->reg_defs[n].offset / 8;
  265. }

  266. /* Supply register N, whose contents are stored in BUF, to REGCACHE.
  267.    If BUF is NULL, the register's value is recorded as
  268.    unavailable.  */

  269. void
  270. supply_register (struct regcache *regcache, int n, const void *buf)
  271. {
  272.   if (buf)
  273.     {
  274.       memcpy (register_data (regcache, n, 0), buf,
  275.               register_size (regcache->tdesc, n));
  276. #ifndef IN_PROCESS_AGENT
  277.       if (regcache->register_status != NULL)
  278.         regcache->register_status[n] = REG_VALID;
  279. #endif
  280.     }
  281.   else
  282.     {
  283.       memset (register_data (regcache, n, 0), 0,
  284.               register_size (regcache->tdesc, n));
  285. #ifndef IN_PROCESS_AGENT
  286.       if (regcache->register_status != NULL)
  287.         regcache->register_status[n] = REG_UNAVAILABLE;
  288. #endif
  289.     }
  290. }

  291. /* Supply register N with value zero to REGCACHE.  */

  292. void
  293. supply_register_zeroed (struct regcache *regcache, int n)
  294. {
  295.   memset (register_data (regcache, n, 0), 0,
  296.           register_size (regcache->tdesc, n));
  297. #ifndef IN_PROCESS_AGENT
  298.   if (regcache->register_status != NULL)
  299.     regcache->register_status[n] = REG_VALID;
  300. #endif
  301. }

  302. /* Supply the whole register set whose contents are stored in BUF, to
  303.    REGCACHE.  If BUF is NULL, all the registers' values are recorded
  304.    as unavailable.  */

  305. void
  306. supply_regblock (struct regcache *regcache, const void *buf)
  307. {
  308.   if (buf)
  309.     {
  310.       const struct target_desc *tdesc = regcache->tdesc;

  311.       memcpy (regcache->registers, buf, tdesc->registers_size);
  312. #ifndef IN_PROCESS_AGENT
  313.       {
  314.         int i;

  315.         for (i = 0; i < tdesc->num_registers; i++)
  316.           regcache->register_status[i] = REG_VALID;
  317.       }
  318. #endif
  319.     }
  320.   else
  321.     {
  322.       const struct target_desc *tdesc = regcache->tdesc;

  323.       memset (regcache->registers, 0, tdesc->registers_size);
  324. #ifndef IN_PROCESS_AGENT
  325.       {
  326.         int i;

  327.         for (i = 0; i < tdesc->num_registers; i++)
  328.           regcache->register_status[i] = REG_UNAVAILABLE;
  329.       }
  330. #endif
  331.     }
  332. }

  333. #ifndef IN_PROCESS_AGENT

  334. void
  335. supply_register_by_name (struct regcache *regcache,
  336.                          const char *name, const void *buf)
  337. {
  338.   supply_register (regcache, find_regno (regcache->tdesc, name), buf);
  339. }

  340. #endif

  341. void
  342. collect_register (struct regcache *regcache, int n, void *buf)
  343. {
  344.   memcpy (buf, register_data (regcache, n, 1),
  345.           register_size (regcache->tdesc, n));
  346. }

  347. #ifndef IN_PROCESS_AGENT

  348. void
  349. collect_register_as_string (struct regcache *regcache, int n, char *buf)
  350. {
  351.   bin2hex (register_data (regcache, n, 1), buf,
  352.            register_size (regcache->tdesc, n));
  353. }

  354. void
  355. collect_register_by_name (struct regcache *regcache,
  356.                           const char *name, void *buf)
  357. {
  358.   collect_register (regcache, find_regno (regcache->tdesc, name), buf);
  359. }

  360. /* Special handling for register PC.  */

  361. CORE_ADDR
  362. regcache_read_pc (struct regcache *regcache)
  363. {
  364.   CORE_ADDR pc_val;

  365.   if (the_target->read_pc)
  366.     pc_val = the_target->read_pc (regcache);
  367.   else
  368.     internal_error (__FILE__, __LINE__,
  369.                     "regcache_read_pc: Unable to find PC");

  370.   return pc_val;
  371. }

  372. void
  373. regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
  374. {
  375.   if (the_target->write_pc)
  376.     the_target->write_pc (regcache, pc);
  377.   else
  378.     internal_error (__FILE__, __LINE__,
  379.                     "regcache_write_pc: Unable to update PC");
  380. }

  381. #endif