Add DW_TAG_compile_unit DIE to Dummy CUs
[binutils-gdb.git] / gdbserver / regcache.cc
blob1bb71d10328873027e649dc472fbb00a4523978a
1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2024 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 "regdef.h"
20 #include "gdbthread.h"
21 #include "tdesc.h"
22 #include "gdbsupport/rsp-low.h"
23 #include "gdbsupport/gdb-checked-static-cast.h"
25 #ifndef IN_PROCESS_AGENT
27 struct regcache *
28 get_thread_regcache (struct thread_info *thread, int fetch)
30 struct regcache *regcache;
32 regcache = thread_regcache_data (thread);
34 /* Threads' regcaches are created lazily, because biarch targets add
35 the main thread/lwp before seeing it stop for the first time, and
36 it is only after the target sees the thread stop for the first
37 time that the target has a chance of determining the process's
38 architecture. IOW, when we first add the process's main thread
39 we don't know which architecture/tdesc its regcache should
40 have. */
41 if (regcache == NULL)
43 struct process_info *proc = get_thread_process (thread);
45 gdb_assert (proc->tdesc != NULL);
47 regcache = new_register_cache (proc->tdesc);
48 set_thread_regcache_data (thread, regcache);
51 if (fetch && regcache->registers_valid == 0)
53 scoped_restore_current_thread restore_thread;
55 switch_to_thread (thread);
56 /* Invalidate all registers, to prevent stale left-overs. */
57 memset (regcache->register_status, REG_UNAVAILABLE,
58 regcache->tdesc->reg_defs.size ());
59 fetch_inferior_registers (regcache, -1);
60 regcache->registers_valid = 1;
63 return regcache;
66 /* See gdbsupport/common-regcache.h. */
68 reg_buffer_common *
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 scoped_restore_current_thread restore_thread;
88 switch_to_thread (thread);
89 store_inferior_registers (regcache, -1);
92 regcache->registers_valid = 0;
95 /* See regcache.h. */
97 void
98 regcache_invalidate_pid (int pid)
100 /* Only invalidate the regcaches of threads of this process. */
101 for_each_thread (pid, regcache_invalidate_thread);
104 /* See regcache.h. */
106 void
107 regcache_invalidate (void)
109 /* Only update the threads of the current process. */
110 int pid = current_thread->id.pid ();
112 regcache_invalidate_pid (pid);
115 #endif
117 struct regcache *
118 init_register_cache (struct regcache *regcache,
119 const struct target_desc *tdesc,
120 unsigned char *regbuf)
122 if (regbuf == NULL)
124 #ifndef IN_PROCESS_AGENT
125 /* Make sure to zero-initialize the register cache when it is
126 created, in case there are registers the target never
127 fetches. This way they'll read as zero instead of
128 garbage. */
129 regcache->tdesc = tdesc;
130 regcache->registers
131 = (unsigned char *) xcalloc (1, tdesc->registers_size);
132 regcache->registers_owned = 1;
133 regcache->register_status
134 = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
135 memset ((void *) regcache->register_status, REG_UNAVAILABLE,
136 tdesc->reg_defs.size ());
137 #else
138 gdb_assert_not_reached ("can't allocate memory from the heap");
139 #endif
141 else
143 regcache->tdesc = tdesc;
144 regcache->registers = regbuf;
145 regcache->registers_owned = 0;
146 #ifndef IN_PROCESS_AGENT
147 regcache->register_status = NULL;
148 #endif
151 regcache->registers_valid = 0;
153 return regcache;
156 #ifndef IN_PROCESS_AGENT
158 struct regcache *
159 new_register_cache (const struct target_desc *tdesc)
161 struct regcache *regcache = new struct regcache;
163 gdb_assert (tdesc->registers_size != 0);
165 return init_register_cache (regcache, tdesc, NULL);
168 void
169 free_register_cache (struct regcache *regcache)
171 if (regcache)
173 if (regcache->registers_owned)
174 free (regcache->registers);
175 free (regcache->register_status);
176 delete regcache;
180 #endif
182 void
183 regcache_cpy (struct regcache *dst, struct regcache *src)
185 gdb_assert (src != NULL && dst != NULL);
186 gdb_assert (src->tdesc == dst->tdesc);
187 gdb_assert (src != dst);
189 memcpy (dst->registers, src->registers, src->tdesc->registers_size);
190 #ifndef IN_PROCESS_AGENT
191 if (dst->register_status != NULL && src->register_status != NULL)
192 memcpy (dst->register_status, src->register_status,
193 src->tdesc->reg_defs.size ());
194 #endif
195 dst->registers_valid = src->registers_valid;
198 /* Return a reference to the description of register N. */
200 static const struct gdb::reg &
201 find_register_by_number (const struct target_desc *tdesc, int n)
203 gdb_assert (n >= 0);
204 gdb_assert (n < tdesc->reg_defs.size ());
206 return tdesc->reg_defs[n];
209 #ifndef IN_PROCESS_AGENT
211 void
212 registers_to_string (struct regcache *regcache, char *buf)
214 unsigned char *registers = regcache->registers;
215 const struct target_desc *tdesc = regcache->tdesc;
217 for (int i = 0; i < tdesc->reg_defs.size (); ++i)
219 if (regcache->register_status[i] == REG_VALID)
221 bin2hex (registers, buf, register_size (tdesc, i));
222 buf += register_size (tdesc, i) * 2;
224 else
226 memset (buf, 'x', register_size (tdesc, i) * 2);
227 buf += register_size (tdesc, i) * 2;
229 registers += register_size (tdesc, i);
231 *buf = '\0';
234 void
235 registers_from_string (struct regcache *regcache, char *buf)
237 int len = strlen (buf);
238 unsigned char *registers = regcache->registers;
239 const struct target_desc *tdesc = regcache->tdesc;
241 if (len != tdesc->registers_size * 2)
243 warning ("Wrong sized register packet (expected %d bytes, got %d)",
244 2 * tdesc->registers_size, len);
245 if (len > tdesc->registers_size * 2)
246 len = tdesc->registers_size * 2;
248 hex2bin (buf, registers, len / 2);
251 /* See regcache.h */
253 std::optional<int>
254 find_regno_no_throw (const struct target_desc *tdesc, const char *name)
256 for (int i = 0; i < tdesc->reg_defs.size (); ++i)
258 if (strcmp (name, find_register_by_number (tdesc, i).name) == 0)
259 return i;
261 return {};
265 find_regno (const struct target_desc *tdesc, const char *name)
267 std::optional<int> regnum = find_regno_no_throw (tdesc, name);
269 if (regnum.has_value ())
270 return *regnum;
272 internal_error ("Unknown register %s requested", name);
275 static void
276 free_register_cache_thread (struct thread_info *thread)
278 struct regcache *regcache = thread_regcache_data (thread);
280 if (regcache != NULL)
282 regcache_invalidate_thread (thread);
283 free_register_cache (regcache);
284 set_thread_regcache_data (thread, NULL);
288 void
289 regcache_release (void)
291 /* Flush and release all pre-existing register caches. */
292 for_each_thread (free_register_cache_thread);
294 #endif
297 register_cache_size (const struct target_desc *tdesc)
299 return tdesc->registers_size;
303 register_size (const struct target_desc *tdesc, int n)
305 return find_register_by_number (tdesc, n).size / 8;
308 /* See gdbsupport/common-regcache.h. */
311 regcache_register_size (const reg_buffer_common *regcache, int n)
313 return register_size
314 (gdb::checked_static_cast<const struct regcache *> (regcache)->tdesc, n);
317 static gdb::array_view<gdb_byte>
318 register_data (const struct regcache *regcache, int n)
320 const gdb::reg &reg = find_register_by_number (regcache->tdesc, n);
321 return gdb::make_array_view (regcache->registers + reg.offset / 8,
322 reg.size / 8);
325 void
326 supply_register (struct regcache *regcache, int n, const void *vbuf)
328 const gdb::reg &reg = find_register_by_number (regcache->tdesc, n);
329 const gdb_byte *buf = static_cast<const gdb_byte *> (vbuf);
330 return regcache->raw_supply (n, gdb::make_array_view (buf, reg.size / 8));
333 /* See gdbsupport/common-regcache.h. */
335 void
336 regcache::raw_supply (int n, gdb::array_view<const gdb_byte> src)
338 auto dst = register_data (this, n);
340 if (src.data () != nullptr)
342 copy (src, dst);
343 #ifndef IN_PROCESS_AGENT
344 if (register_status != NULL)
345 register_status[n] = REG_VALID;
346 #endif
348 else
350 memset (dst.data (), 0, dst.size ());
351 #ifndef IN_PROCESS_AGENT
352 if (register_status != NULL)
353 register_status[n] = REG_UNAVAILABLE;
354 #endif
358 /* Supply register N with value zero to REGCACHE. */
360 void
361 supply_register_zeroed (struct regcache *regcache, int n)
363 auto dst = register_data (regcache, n);
364 memset (dst.data (), 0, dst.size ());
365 #ifndef IN_PROCESS_AGENT
366 if (regcache->register_status != NULL)
367 regcache->register_status[n] = REG_VALID;
368 #endif
371 #ifndef IN_PROCESS_AGENT
373 /* Supply register called NAME with value zero to REGCACHE. */
375 void
376 supply_register_by_name_zeroed (struct regcache *regcache,
377 const char *name)
379 supply_register_zeroed (regcache, find_regno (regcache->tdesc, name));
382 #endif
384 /* Supply the whole register set whose contents are stored in BUF, to
385 REGCACHE. If BUF is NULL, all the registers' values are recorded
386 as unavailable. */
388 void
389 supply_regblock (struct regcache *regcache, const void *buf)
391 if (buf)
393 const struct target_desc *tdesc = regcache->tdesc;
395 memcpy (regcache->registers, buf, tdesc->registers_size);
396 #ifndef IN_PROCESS_AGENT
398 int i;
400 for (i = 0; i < tdesc->reg_defs.size (); i++)
401 regcache->register_status[i] = REG_VALID;
403 #endif
405 else
407 const struct target_desc *tdesc = regcache->tdesc;
409 memset (regcache->registers, 0, tdesc->registers_size);
410 #ifndef IN_PROCESS_AGENT
412 int i;
414 for (i = 0; i < tdesc->reg_defs.size (); i++)
415 regcache->register_status[i] = REG_UNAVAILABLE;
417 #endif
421 #ifndef IN_PROCESS_AGENT
423 void
424 supply_register_by_name (struct regcache *regcache,
425 const char *name, const void *buf)
427 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
430 #endif
432 void
433 collect_register (struct regcache *regcache, int n, void *vbuf)
435 const gdb::reg &reg = find_register_by_number (regcache->tdesc, n);
436 gdb_byte *buf = static_cast<gdb_byte *> (vbuf);
437 regcache->raw_collect (n, gdb::make_array_view (buf, reg.size / 8));
440 /* See gdbsupport/common-regcache.h. */
442 void
443 regcache::raw_collect (int n, gdb::array_view<gdb_byte> dst) const
445 auto src = register_data (this, n);
446 copy (src, dst);
449 enum register_status
450 regcache_raw_read_unsigned (reg_buffer_common *reg_buf, int regnum,
451 ULONGEST *val)
453 int size;
454 regcache *regcache = gdb::checked_static_cast<struct regcache *> (reg_buf);
456 gdb_assert (regcache != NULL);
458 size = register_size (regcache->tdesc, regnum);
460 if (size > (int) sizeof (ULONGEST))
461 error (_("That operation is not available on integers of more than"
462 "%d bytes."),
463 (int) sizeof (ULONGEST));
465 *val = 0;
466 collect_register (regcache, regnum, val);
468 return REG_VALID;
471 #ifndef IN_PROCESS_AGENT
473 /* See regcache.h. */
475 ULONGEST
476 regcache_raw_get_unsigned_by_name (struct regcache *regcache,
477 const char *name)
479 return regcache_raw_get_unsigned (regcache,
480 find_regno (regcache->tdesc, name));
483 void
484 collect_register_as_string (struct regcache *regcache, int n, char *buf)
486 bin2hex (register_data (regcache, n), buf);
489 void
490 collect_register_by_name (struct regcache *regcache,
491 const char *name, void *buf)
493 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
496 /* Special handling for register PC. */
498 CORE_ADDR
499 regcache_read_pc (reg_buffer_common *regcache)
501 return the_target->read_pc
502 (gdb::checked_static_cast<struct regcache *> (regcache));
505 void
506 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
508 the_target->write_pc (regcache, pc);
511 #endif
513 /* See gdbsupport/common-regcache.h. */
515 enum register_status
516 regcache::get_register_status (int regnum) const
518 #ifndef IN_PROCESS_AGENT
519 gdb_assert (regnum >= 0 && regnum < tdesc->reg_defs.size ());
520 return (enum register_status) (register_status[regnum]);
521 #else
522 return REG_VALID;
523 #endif
526 /* See gdbsupport/common-regcache.h. */
528 bool
529 regcache::raw_compare (int regnum, const void *buf, int offset) const
531 gdb_assert (buf != NULL);
533 gdb::array_view<const gdb_byte> regbuf = register_data (this, regnum);
534 gdb_assert (offset < regbuf.size ());
535 regbuf = regbuf.slice (offset);
537 return memcmp (buf, regbuf.data (), regbuf.size ()) == 0;