xref: /netbsd-src/external/gpl3/gdb/dist/gdbserver/regcache.cc (revision 64f917f5a88990e32dd65fcd4348042fa7f852b9)
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 (struct thread_info *thread, int fetch)
29 {
30   struct regcache *regcache;
31 
32   regcache = thread_regcache_data (thread);
33 
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)
42     {
43       struct process_info *proc = get_thread_process (thread);
44 
45       gdb_assert (proc->tdesc != NULL);
46 
47       regcache = new_register_cache (proc->tdesc);
48       set_thread_regcache_data (thread, regcache);
49     }
50 
51   if (fetch && regcache->registers_valid == 0)
52     {
53       scoped_restore_current_thread restore_thread;
54 
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;
61     }
62 
63   return regcache;
64 }
65 
66 /* See gdbsupport/common-regcache.h.  */
67 
68 reg_buffer_common *
69 get_thread_regcache_for_ptid (ptid_t ptid)
70 {
71   return get_thread_regcache (find_thread_ptid (ptid), 1);
72 }
73 
74 void
75 regcache_invalidate_thread (struct thread_info *thread)
76 {
77   struct regcache *regcache;
78 
79   regcache = thread_regcache_data (thread);
80 
81   if (regcache == NULL)
82     return;
83 
84   if (regcache->registers_valid)
85     {
86       scoped_restore_current_thread restore_thread;
87 
88       switch_to_thread (thread);
89       store_inferior_registers (regcache, -1);
90     }
91 
92   regcache->registers_valid = 0;
93 }
94 
95 /* See regcache.h.  */
96 
97 void
98 regcache_invalidate_pid (int pid)
99 {
100   /* Only invalidate the regcaches of threads of this process.  */
101   for_each_thread (pid, regcache_invalidate_thread);
102 }
103 
104 /* See regcache.h.  */
105 
106 void
107 regcache_invalidate (void)
108 {
109   /* Only update the threads of the current process.  */
110   int pid = current_thread->id.pid ();
111 
112   regcache_invalidate_pid (pid);
113 }
114 
115 #endif
116 
117 struct regcache *
118 init_register_cache (struct regcache *regcache,
119 		     const struct target_desc *tdesc,
120 		     unsigned char *regbuf)
121 {
122   if (regbuf == NULL)
123     {
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
140     }
141   else
142     {
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
149     }
150 
151   regcache->registers_valid = 0;
152 
153   return regcache;
154 }
155 
156 #ifndef IN_PROCESS_AGENT
157 
158 struct regcache *
159 new_register_cache (const struct target_desc *tdesc)
160 {
161   struct regcache *regcache = new struct regcache;
162 
163   gdb_assert (tdesc->registers_size != 0);
164 
165   return init_register_cache (regcache, tdesc, NULL);
166 }
167 
168 void
169 free_register_cache (struct regcache *regcache)
170 {
171   if (regcache)
172     {
173       if (regcache->registers_owned)
174 	free (regcache->registers);
175       free (regcache->register_status);
176       delete regcache;
177     }
178 }
179 
180 #endif
181 
182 void
183 regcache_cpy (struct regcache *dst, struct regcache *src)
184 {
185   gdb_assert (src != NULL && dst != NULL);
186   gdb_assert (src->tdesc == dst->tdesc);
187   gdb_assert (src != dst);
188 
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;
196 }
197 
198 /* Return a reference to the description of register N.  */
199 
200 static const struct gdb::reg &
201 find_register_by_number (const struct target_desc *tdesc, int n)
202 {
203   gdb_assert (n >= 0);
204   gdb_assert (n < tdesc->reg_defs.size ());
205 
206   return tdesc->reg_defs[n];
207 }
208 
209 #ifndef IN_PROCESS_AGENT
210 
211 void
212 registers_to_string (struct regcache *regcache, char *buf)
213 {
214   unsigned char *registers = regcache->registers;
215   const struct target_desc *tdesc = regcache->tdesc;
216 
217   for (int i = 0; i < tdesc->reg_defs.size (); ++i)
218     {
219       if (regcache->register_status[i] == REG_VALID)
220 	{
221 	  bin2hex (registers, buf, register_size (tdesc, i));
222 	  buf += register_size (tdesc, i) * 2;
223 	}
224       else
225 	{
226 	  memset (buf, 'x', register_size (tdesc, i) * 2);
227 	  buf += register_size (tdesc, i) * 2;
228 	}
229       registers += register_size (tdesc, i);
230     }
231   *buf = '\0';
232 }
233 
234 void
235 registers_from_string (struct regcache *regcache, char *buf)
236 {
237   int len = strlen (buf);
238   unsigned char *registers = regcache->registers;
239   const struct target_desc *tdesc = regcache->tdesc;
240 
241   if (len != tdesc->registers_size * 2)
242     {
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;
247     }
248   hex2bin (buf, registers, len / 2);
249 }
250 
251 /* See regcache.h */
252 
253 std::optional<int>
254 find_regno_no_throw (const struct target_desc *tdesc, const char *name)
255 {
256   for (int i = 0; i < tdesc->reg_defs.size (); ++i)
257     {
258       if (strcmp (name, find_register_by_number (tdesc, i).name) == 0)
259 	return i;
260     }
261   return {};
262 }
263 
264 int
265 find_regno (const struct target_desc *tdesc, const char *name)
266 {
267   std::optional<int> regnum = find_regno_no_throw (tdesc, name);
268 
269   if (regnum.has_value ())
270     return *regnum;
271 
272   internal_error ("Unknown register %s requested", name);
273 }
274 
275 static void
276 free_register_cache_thread (struct thread_info *thread)
277 {
278   struct regcache *regcache = thread_regcache_data (thread);
279 
280   if (regcache != NULL)
281     {
282       regcache_invalidate_thread (thread);
283       free_register_cache (regcache);
284       set_thread_regcache_data (thread, NULL);
285     }
286 }
287 
288 void
289 regcache_release (void)
290 {
291   /* Flush and release all pre-existing register caches.  */
292   for_each_thread (free_register_cache_thread);
293 }
294 #endif
295 
296 int
297 register_cache_size (const struct target_desc *tdesc)
298 {
299   return tdesc->registers_size;
300 }
301 
302 int
303 register_size (const struct target_desc *tdesc, int n)
304 {
305   return find_register_by_number (tdesc, n).size / 8;
306 }
307 
308 /* See gdbsupport/common-regcache.h.  */
309 
310 int
311 regcache_register_size (const reg_buffer_common *regcache, int n)
312 {
313   return register_size
314     (gdb::checked_static_cast<const struct regcache *> (regcache)->tdesc, n);
315 }
316 
317 static gdb::array_view<gdb_byte>
318 register_data (const struct regcache *regcache, int n)
319 {
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);
323 }
324 
325 void
326 supply_register (struct regcache *regcache, int n, const void *vbuf)
327 {
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));
331 }
332 
333 /* See gdbsupport/common-regcache.h.  */
334 
335 void
336 regcache::raw_supply (int n, gdb::array_view<const gdb_byte> src)
337 {
338   auto dst = register_data (this, n);
339 
340   if (src.data () != nullptr)
341     {
342       copy (src, dst);
343 #ifndef IN_PROCESS_AGENT
344       if (register_status != NULL)
345 	register_status[n] = REG_VALID;
346 #endif
347     }
348   else
349     {
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
355     }
356 }
357 
358 /* Supply register N with value zero to REGCACHE.  */
359 
360 void
361 supply_register_zeroed (struct regcache *regcache, int n)
362 {
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
369 }
370 
371 #ifndef IN_PROCESS_AGENT
372 
373 /* Supply register called NAME with value zero to REGCACHE.  */
374 
375 void
376 supply_register_by_name_zeroed (struct regcache *regcache,
377 				const char *name)
378 {
379   supply_register_zeroed (regcache, find_regno (regcache->tdesc, name));
380 }
381 
382 #endif
383 
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.  */
387 
388 void
389 supply_regblock (struct regcache *regcache, const void *buf)
390 {
391   if (buf)
392     {
393       const struct target_desc *tdesc = regcache->tdesc;
394 
395       memcpy (regcache->registers, buf, tdesc->registers_size);
396 #ifndef IN_PROCESS_AGENT
397       {
398 	int i;
399 
400 	for (i = 0; i < tdesc->reg_defs.size (); i++)
401 	  regcache->register_status[i] = REG_VALID;
402       }
403 #endif
404     }
405   else
406     {
407       const struct target_desc *tdesc = regcache->tdesc;
408 
409       memset (regcache->registers, 0, tdesc->registers_size);
410 #ifndef IN_PROCESS_AGENT
411       {
412 	int i;
413 
414 	for (i = 0; i < tdesc->reg_defs.size (); i++)
415 	  regcache->register_status[i] = REG_UNAVAILABLE;
416       }
417 #endif
418     }
419 }
420 
421 #ifndef IN_PROCESS_AGENT
422 
423 void
424 supply_register_by_name (struct regcache *regcache,
425 			 const char *name, const void *buf)
426 {
427   supply_register (regcache, find_regno (regcache->tdesc, name), buf);
428 }
429 
430 #endif
431 
432 void
433 collect_register (struct regcache *regcache, int n, void *vbuf)
434 {
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));
438 }
439 
440 /* See gdbsupport/common-regcache.h.  */
441 
442 void
443 regcache::raw_collect (int n, gdb::array_view<gdb_byte> dst) const
444 {
445   auto src = register_data (this, n);
446   copy (src, dst);
447 }
448 
449 enum register_status
450 regcache_raw_read_unsigned (reg_buffer_common *reg_buf, int regnum,
451 			    ULONGEST *val)
452 {
453   int size;
454   regcache *regcache = gdb::checked_static_cast<struct regcache *> (reg_buf);
455 
456   gdb_assert (regcache != NULL);
457 
458   size = register_size (regcache->tdesc, regnum);
459 
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));
464 
465   *val = 0;
466   collect_register (regcache, regnum, val);
467 
468   return REG_VALID;
469 }
470 
471 #ifndef IN_PROCESS_AGENT
472 
473 /* See regcache.h.  */
474 
475 ULONGEST
476 regcache_raw_get_unsigned_by_name (struct regcache *regcache,
477 				   const char *name)
478 {
479   return regcache_raw_get_unsigned (regcache,
480 				    find_regno (regcache->tdesc, name));
481 }
482 
483 void
484 collect_register_as_string (struct regcache *regcache, int n, char *buf)
485 {
486   bin2hex (register_data (regcache, n), buf);
487 }
488 
489 void
490 collect_register_by_name (struct regcache *regcache,
491 			  const char *name, void *buf)
492 {
493   collect_register (regcache, find_regno (regcache->tdesc, name), buf);
494 }
495 
496 /* Special handling for register PC.  */
497 
498 CORE_ADDR
499 regcache_read_pc (reg_buffer_common *regcache)
500 {
501   return the_target->read_pc
502     (gdb::checked_static_cast<struct regcache *> (regcache));
503 }
504 
505 void
506 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
507 {
508   the_target->write_pc (regcache, pc);
509 }
510 
511 #endif
512 
513 /* See gdbsupport/common-regcache.h.  */
514 
515 enum register_status
516 regcache::get_register_status (int regnum) const
517 {
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
524 }
525 
526 /* See gdbsupport/common-regcache.h.  */
527 
528 bool
529 regcache::raw_compare (int regnum, const void *buf, int offset) const
530 {
531   gdb_assert (buf != NULL);
532 
533   gdb::array_view<const gdb_byte> regbuf = register_data (this, regnum);
534   gdb_assert (offset < regbuf.size ());
535   regbuf = regbuf.slice (offset);
536 
537   return memcmp (buf, regbuf.data (), regbuf.size ()) == 0;
538 }
539