Automatic date update in version.in
[binutils-gdb.git] / gdbserver / regcache.cc
blob312f14ee9dd88d52b4594c9683c6edc1f1e30712
1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2021 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
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
26 struct regcache *
27 get_thread_regcache (struct thread_info *thread, int fetch)
29 struct regcache *regcache;
31 regcache = thread_regcache_data (thread);
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)
42 struct process_info *proc = get_thread_process (thread);
44 gdb_assert (proc->tdesc != NULL);
46 regcache = new_register_cache (proc->tdesc);
47 set_thread_regcache_data (thread, regcache);
50 if (fetch && regcache->registers_valid == 0)
52 struct thread_info *saved_thread = current_thread;
54 current_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 current_thread = saved_thread;
60 regcache->registers_valid = 1;
63 return regcache;
66 /* See gdbsupport/common-regcache.h. */
68 struct regcache *
69 get_thread_regcache_for_ptid (ptid_t ptid)
71 return get_thread_regcache (find_thread_ptid (ptid), 1);
74 void
75 regcache_invalidate_thread (struct thread_info *thread)
77 struct regcache *regcache;
79 regcache = thread_regcache_data (thread);
81 if (regcache == NULL)
82 return;
84 if (regcache->registers_valid)
86 struct thread_info *saved_thread = current_thread;
88 current_thread = thread;
89 store_inferior_registers (regcache, -1);
90 current_thread = saved_thread;
93 regcache->registers_valid = 0;
96 /* See regcache.h. */
98 void
99 regcache_invalidate_pid (int pid)
101 /* Only invalidate the regcaches of threads of this process. */
102 for_each_thread (pid, regcache_invalidate_thread);
105 /* See regcache.h. */
107 void
108 regcache_invalidate (void)
110 /* Only update the threads of the current process. */
111 int pid = current_thread->id.pid ();
113 regcache_invalidate_pid (pid);
116 #endif
118 struct regcache *
119 init_register_cache (struct regcache *regcache,
120 const struct target_desc *tdesc,
121 unsigned char *regbuf)
123 if (regbuf == NULL)
125 #ifndef IN_PROCESS_AGENT
126 /* Make sure to zero-initialize the register cache when it is
127 created, in case there are registers the target never
128 fetches. This way they'll read as zero instead of
129 garbage. */
130 regcache->tdesc = tdesc;
131 regcache->registers
132 = (unsigned char *) xcalloc (1, tdesc->registers_size);
133 regcache->registers_owned = 1;
134 regcache->register_status
135 = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
136 memset ((void *) regcache->register_status, REG_UNAVAILABLE,
137 tdesc->reg_defs.size ());
138 #else
139 gdb_assert_not_reached ("can't allocate memory from the heap");
140 #endif
142 else
144 regcache->tdesc = tdesc;
145 regcache->registers = regbuf;
146 regcache->registers_owned = 0;
147 #ifndef IN_PROCESS_AGENT
148 regcache->register_status = NULL;
149 #endif
152 regcache->registers_valid = 0;
154 return regcache;
157 #ifndef IN_PROCESS_AGENT
159 struct regcache *
160 new_register_cache (const struct target_desc *tdesc)
162 struct regcache *regcache = new struct regcache;
164 gdb_assert (tdesc->registers_size != 0);
166 return init_register_cache (regcache, tdesc, NULL);
169 void
170 free_register_cache (struct regcache *regcache)
172 if (regcache)
174 if (regcache->registers_owned)
175 free (regcache->registers);
176 free (regcache->register_status);
177 delete regcache;
181 #endif
183 void
184 regcache_cpy (struct regcache *dst, struct regcache *src)
186 gdb_assert (src != NULL && dst != NULL);
187 gdb_assert (src->tdesc == dst->tdesc);
188 gdb_assert (src != dst);
190 memcpy (dst->registers, src->registers, src->tdesc->registers_size);
191 #ifndef IN_PROCESS_AGENT
192 if (dst->register_status != NULL && src->register_status != NULL)
193 memcpy (dst->register_status, src->register_status,
194 src->tdesc->reg_defs.size ());
195 #endif
196 dst->registers_valid = src->registers_valid;
199 /* Return a reference to the description of register N. */
201 static const struct gdb::reg &
202 find_register_by_number (const struct target_desc *tdesc, int n)
204 return tdesc->reg_defs[n];
207 #ifndef IN_PROCESS_AGENT
209 void
210 registers_to_string (struct regcache *regcache, char *buf)
212 unsigned char *registers = regcache->registers;
213 const struct target_desc *tdesc = regcache->tdesc;
215 for (int i = 0; i < tdesc->reg_defs.size (); ++i)
217 if (regcache->register_status[i] == REG_VALID)
219 bin2hex (registers, buf, register_size (tdesc, i));
220 buf += register_size (tdesc, i) * 2;
222 else
224 memset (buf, 'x', register_size (tdesc, i) * 2);
225 buf += register_size (tdesc, i) * 2;
227 registers += register_size (tdesc, i);
229 *buf = '\0';
232 void
233 registers_from_string (struct regcache *regcache, char *buf)
235 int len = strlen (buf);
236 unsigned char *registers = regcache->registers;
237 const struct target_desc *tdesc = regcache->tdesc;
239 if (len != tdesc->registers_size * 2)
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;
246 hex2bin (buf, registers, len / 2);
250 find_regno (const struct target_desc *tdesc, const char *name)
252 for (int i = 0; i < tdesc->reg_defs.size (); ++i)
254 if (strcmp (name, find_register_by_number (tdesc, i).name) == 0)
255 return i;
257 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
258 name);
261 static void
262 free_register_cache_thread (struct thread_info *thread)
264 struct regcache *regcache = thread_regcache_data (thread);
266 if (regcache != NULL)
268 regcache_invalidate_thread (thread);
269 free_register_cache (regcache);
270 set_thread_regcache_data (thread, NULL);
274 void
275 regcache_release (void)
277 /* Flush and release all pre-existing register caches. */
278 for_each_thread (free_register_cache_thread);
280 #endif
283 register_cache_size (const struct target_desc *tdesc)
285 return tdesc->registers_size;
289 register_size (const struct target_desc *tdesc, int n)
291 return find_register_by_number (tdesc, n).size / 8;
294 /* See gdbsupport/common-regcache.h. */
297 regcache_register_size (const struct regcache *regcache, int n)
299 return register_size (regcache->tdesc, n);
302 static unsigned char *
303 register_data (const struct regcache *regcache, int n)
305 return (regcache->registers
306 + find_register_by_number (regcache->tdesc, n).offset / 8);
309 void
310 supply_register (struct regcache *regcache, int n, const void *buf)
312 return regcache->raw_supply (n, buf);
315 /* See gdbsupport/common-regcache.h. */
317 void
318 regcache::raw_supply (int n, const void *buf)
320 if (buf)
322 memcpy (register_data (this, n), buf, register_size (tdesc, n));
323 #ifndef IN_PROCESS_AGENT
324 if (register_status != NULL)
325 register_status[n] = REG_VALID;
326 #endif
328 else
330 memset (register_data (this, n), 0, register_size (tdesc, n));
331 #ifndef IN_PROCESS_AGENT
332 if (register_status != NULL)
333 register_status[n] = REG_UNAVAILABLE;
334 #endif
338 /* Supply register N with value zero to REGCACHE. */
340 void
341 supply_register_zeroed (struct regcache *regcache, int n)
343 memset (register_data (regcache, n), 0,
344 register_size (regcache->tdesc, n));
345 #ifndef IN_PROCESS_AGENT
346 if (regcache->register_status != NULL)
347 regcache->register_status[n] = REG_VALID;
348 #endif
351 #ifndef IN_PROCESS_AGENT
353 /* Supply register called NAME with value zero to REGCACHE. */
355 void
356 supply_register_by_name_zeroed (struct regcache *regcache,
357 const char *name)
359 supply_register_zeroed (regcache, find_regno (regcache->tdesc, name));
362 #endif
364 /* Supply the whole register set whose contents are stored in BUF, to
365 REGCACHE. If BUF is NULL, all the registers' values are recorded
366 as unavailable. */
368 void
369 supply_regblock (struct regcache *regcache, const void *buf)
371 if (buf)
373 const struct target_desc *tdesc = regcache->tdesc;
375 memcpy (regcache->registers, buf, tdesc->registers_size);
376 #ifndef IN_PROCESS_AGENT
378 int i;
380 for (i = 0; i < tdesc->reg_defs.size (); i++)
381 regcache->register_status[i] = REG_VALID;
383 #endif
385 else
387 const struct target_desc *tdesc = regcache->tdesc;
389 memset (regcache->registers, 0, tdesc->registers_size);
390 #ifndef IN_PROCESS_AGENT
392 int i;
394 for (i = 0; i < tdesc->reg_defs.size (); i++)
395 regcache->register_status[i] = REG_UNAVAILABLE;
397 #endif
401 #ifndef IN_PROCESS_AGENT
403 void
404 supply_register_by_name (struct regcache *regcache,
405 const char *name, const void *buf)
407 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
410 #endif
412 void
413 collect_register (struct regcache *regcache, int n, void *buf)
415 regcache->raw_collect (n, buf);
418 /* See gdbsupport/common-regcache.h. */
420 void
421 regcache::raw_collect (int n, void *buf) const
423 memcpy (buf, register_data (this, n), register_size (tdesc, n));
426 enum register_status
427 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
428 ULONGEST *val)
430 int size;
432 gdb_assert (regcache != NULL);
433 gdb_assert (regnum >= 0
434 && regnum < regcache->tdesc->reg_defs.size ());
436 size = register_size (regcache->tdesc, regnum);
438 if (size > (int) sizeof (ULONGEST))
439 error (_("That operation is not available on integers of more than"
440 "%d bytes."),
441 (int) sizeof (ULONGEST));
443 *val = 0;
444 collect_register (regcache, regnum, val);
446 return REG_VALID;
449 #ifndef IN_PROCESS_AGENT
451 /* See regcache.h. */
453 ULONGEST
454 regcache_raw_get_unsigned_by_name (struct regcache *regcache,
455 const char *name)
457 return regcache_raw_get_unsigned (regcache,
458 find_regno (regcache->tdesc, name));
461 void
462 collect_register_as_string (struct regcache *regcache, int n, char *buf)
464 bin2hex (register_data (regcache, n), buf,
465 register_size (regcache->tdesc, n));
468 void
469 collect_register_by_name (struct regcache *regcache,
470 const char *name, void *buf)
472 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
475 /* Special handling for register PC. */
477 CORE_ADDR
478 regcache_read_pc (struct regcache *regcache)
480 return the_target->read_pc (regcache);
483 void
484 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
486 the_target->write_pc (regcache, pc);
489 #endif
491 /* See gdbsupport/common-regcache.h. */
493 enum register_status
494 regcache::get_register_status (int regnum) const
496 #ifndef IN_PROCESS_AGENT
497 gdb_assert (regnum >= 0 && regnum < tdesc->reg_defs.size ());
498 return (enum register_status) (register_status[regnum]);
499 #else
500 return REG_VALID;
501 #endif
504 /* See gdbsupport/common-regcache.h. */
506 bool
507 regcache::raw_compare (int regnum, const void *buf, int offset) const
509 gdb_assert (buf != NULL);
511 const unsigned char *regbuf = register_data (this, regnum);
512 int size = register_size (tdesc, regnum);
513 gdb_assert (size >= offset);
515 return (memcmp (buf, regbuf + offset, size - offset) == 0);