1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2022 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/>. */
21 #include "gdbthread.h"
23 #include "gdbsupport/rsp-low.h"
24 #ifndef IN_PROCESS_AGENT
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
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 scoped_restore_current_thread restore_thread
;
54 switch_to_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 regcache
->registers_valid
= 1;
65 /* See gdbsupport/common-regcache.h. */
68 get_thread_regcache_for_ptid (ptid_t ptid
)
70 return get_thread_regcache (find_thread_ptid (ptid
), 1);
74 regcache_invalidate_thread (struct thread_info
*thread
)
76 struct regcache
*regcache
;
78 regcache
= thread_regcache_data (thread
);
83 if (regcache
->registers_valid
)
85 scoped_restore_current_thread restore_thread
;
87 switch_to_thread (thread
);
88 store_inferior_registers (regcache
, -1);
91 regcache
->registers_valid
= 0;
97 regcache_invalidate_pid (int pid
)
99 /* Only invalidate the regcaches of threads of this process. */
100 for_each_thread (pid
, regcache_invalidate_thread
);
103 /* See regcache.h. */
106 regcache_invalidate (void)
108 /* Only update the threads of the current process. */
109 int pid
= current_thread
->id
.pid ();
111 regcache_invalidate_pid (pid
);
117 init_register_cache (struct regcache
*regcache
,
118 const struct target_desc
*tdesc
,
119 unsigned char *regbuf
)
123 #ifndef IN_PROCESS_AGENT
124 /* Make sure to zero-initialize the register cache when it is
125 created, in case there are registers the target never
126 fetches. This way they'll read as zero instead of
128 regcache
->tdesc
= tdesc
;
130 = (unsigned char *) xcalloc (1, tdesc
->registers_size
);
131 regcache
->registers_owned
= 1;
132 regcache
->register_status
133 = (unsigned char *) xmalloc (tdesc
->reg_defs
.size ());
134 memset ((void *) regcache
->register_status
, REG_UNAVAILABLE
,
135 tdesc
->reg_defs
.size ());
137 gdb_assert_not_reached ("can't allocate memory from the heap");
142 regcache
->tdesc
= tdesc
;
143 regcache
->registers
= regbuf
;
144 regcache
->registers_owned
= 0;
145 #ifndef IN_PROCESS_AGENT
146 regcache
->register_status
= NULL
;
150 regcache
->registers_valid
= 0;
155 #ifndef IN_PROCESS_AGENT
158 new_register_cache (const struct target_desc
*tdesc
)
160 struct regcache
*regcache
= new struct regcache
;
162 gdb_assert (tdesc
->registers_size
!= 0);
164 return init_register_cache (regcache
, tdesc
, NULL
);
168 free_register_cache (struct regcache
*regcache
)
172 if (regcache
->registers_owned
)
173 free (regcache
->registers
);
174 free (regcache
->register_status
);
182 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
184 gdb_assert (src
!= NULL
&& dst
!= NULL
);
185 gdb_assert (src
->tdesc
== dst
->tdesc
);
186 gdb_assert (src
!= dst
);
188 memcpy (dst
->registers
, src
->registers
, src
->tdesc
->registers_size
);
189 #ifndef IN_PROCESS_AGENT
190 if (dst
->register_status
!= NULL
&& src
->register_status
!= NULL
)
191 memcpy (dst
->register_status
, src
->register_status
,
192 src
->tdesc
->reg_defs
.size ());
194 dst
->registers_valid
= src
->registers_valid
;
197 /* Return a reference to the description of register N. */
199 static const struct gdb::reg
&
200 find_register_by_number (const struct target_desc
*tdesc
, int n
)
202 return tdesc
->reg_defs
[n
];
205 #ifndef IN_PROCESS_AGENT
208 registers_to_string (struct regcache
*regcache
, char *buf
)
210 unsigned char *registers
= regcache
->registers
;
211 const struct target_desc
*tdesc
= regcache
->tdesc
;
213 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
215 if (regcache
->register_status
[i
] == REG_VALID
)
217 bin2hex (registers
, buf
, register_size (tdesc
, i
));
218 buf
+= register_size (tdesc
, i
) * 2;
222 memset (buf
, 'x', register_size (tdesc
, i
) * 2);
223 buf
+= register_size (tdesc
, i
) * 2;
225 registers
+= register_size (tdesc
, i
);
231 registers_from_string (struct regcache
*regcache
, char *buf
)
233 int len
= strlen (buf
);
234 unsigned char *registers
= regcache
->registers
;
235 const struct target_desc
*tdesc
= regcache
->tdesc
;
237 if (len
!= tdesc
->registers_size
* 2)
239 warning ("Wrong sized register packet (expected %d bytes, got %d)",
240 2 * tdesc
->registers_size
, len
);
241 if (len
> tdesc
->registers_size
* 2)
242 len
= tdesc
->registers_size
* 2;
244 hex2bin (buf
, registers
, len
/ 2);
248 find_regno (const struct target_desc
*tdesc
, const char *name
)
250 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
252 if (strcmp (name
, find_register_by_number (tdesc
, i
).name
) == 0)
255 internal_error ("Unknown register %s requested",
260 free_register_cache_thread (struct thread_info
*thread
)
262 struct regcache
*regcache
= thread_regcache_data (thread
);
264 if (regcache
!= NULL
)
266 regcache_invalidate_thread (thread
);
267 free_register_cache (regcache
);
268 set_thread_regcache_data (thread
, NULL
);
273 regcache_release (void)
275 /* Flush and release all pre-existing register caches. */
276 for_each_thread (free_register_cache_thread
);
281 register_cache_size (const struct target_desc
*tdesc
)
283 return tdesc
->registers_size
;
287 register_size (const struct target_desc
*tdesc
, int n
)
289 return find_register_by_number (tdesc
, n
).size
/ 8;
292 /* See gdbsupport/common-regcache.h. */
295 regcache_register_size (const struct regcache
*regcache
, int n
)
297 return register_size (regcache
->tdesc
, n
);
300 static unsigned char *
301 register_data (const struct regcache
*regcache
, int n
)
303 return (regcache
->registers
304 + find_register_by_number (regcache
->tdesc
, n
).offset
/ 8);
308 supply_register (struct regcache
*regcache
, int n
, const void *buf
)
310 return regcache
->raw_supply (n
, buf
);
313 /* See gdbsupport/common-regcache.h. */
316 regcache::raw_supply (int n
, const void *buf
)
320 memcpy (register_data (this, n
), buf
, register_size (tdesc
, n
));
321 #ifndef IN_PROCESS_AGENT
322 if (register_status
!= NULL
)
323 register_status
[n
] = REG_VALID
;
328 memset (register_data (this, n
), 0, register_size (tdesc
, n
));
329 #ifndef IN_PROCESS_AGENT
330 if (register_status
!= NULL
)
331 register_status
[n
] = REG_UNAVAILABLE
;
336 /* Supply register N with value zero to REGCACHE. */
339 supply_register_zeroed (struct regcache
*regcache
, int n
)
341 memset (register_data (regcache
, n
), 0,
342 register_size (regcache
->tdesc
, n
));
343 #ifndef IN_PROCESS_AGENT
344 if (regcache
->register_status
!= NULL
)
345 regcache
->register_status
[n
] = REG_VALID
;
349 #ifndef IN_PROCESS_AGENT
351 /* Supply register called NAME with value zero to REGCACHE. */
354 supply_register_by_name_zeroed (struct regcache
*regcache
,
357 supply_register_zeroed (regcache
, find_regno (regcache
->tdesc
, name
));
362 /* Supply the whole register set whose contents are stored in BUF, to
363 REGCACHE. If BUF is NULL, all the registers' values are recorded
367 supply_regblock (struct regcache
*regcache
, const void *buf
)
371 const struct target_desc
*tdesc
= regcache
->tdesc
;
373 memcpy (regcache
->registers
, buf
, tdesc
->registers_size
);
374 #ifndef IN_PROCESS_AGENT
378 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
379 regcache
->register_status
[i
] = REG_VALID
;
385 const struct target_desc
*tdesc
= regcache
->tdesc
;
387 memset (regcache
->registers
, 0, tdesc
->registers_size
);
388 #ifndef IN_PROCESS_AGENT
392 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
393 regcache
->register_status
[i
] = REG_UNAVAILABLE
;
399 #ifndef IN_PROCESS_AGENT
402 supply_register_by_name (struct regcache
*regcache
,
403 const char *name
, const void *buf
)
405 supply_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
411 collect_register (struct regcache
*regcache
, int n
, void *buf
)
413 regcache
->raw_collect (n
, buf
);
416 /* See gdbsupport/common-regcache.h. */
419 regcache::raw_collect (int n
, void *buf
) const
421 memcpy (buf
, register_data (this, n
), register_size (tdesc
, n
));
425 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
430 gdb_assert (regcache
!= NULL
);
431 gdb_assert (regnum
>= 0
432 && regnum
< regcache
->tdesc
->reg_defs
.size ());
434 size
= register_size (regcache
->tdesc
, regnum
);
436 if (size
> (int) sizeof (ULONGEST
))
437 error (_("That operation is not available on integers of more than"
439 (int) sizeof (ULONGEST
));
442 collect_register (regcache
, regnum
, val
);
447 #ifndef IN_PROCESS_AGENT
449 /* See regcache.h. */
452 regcache_raw_get_unsigned_by_name (struct regcache
*regcache
,
455 return regcache_raw_get_unsigned (regcache
,
456 find_regno (regcache
->tdesc
, name
));
460 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
462 bin2hex (register_data (regcache
, n
), buf
,
463 register_size (regcache
->tdesc
, n
));
467 collect_register_by_name (struct regcache
*regcache
,
468 const char *name
, void *buf
)
470 collect_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
473 /* Special handling for register PC. */
476 regcache_read_pc (struct regcache
*regcache
)
478 return the_target
->read_pc (regcache
);
482 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
484 the_target
->write_pc (regcache
, pc
);
489 /* See gdbsupport/common-regcache.h. */
492 regcache::get_register_status (int regnum
) const
494 #ifndef IN_PROCESS_AGENT
495 gdb_assert (regnum
>= 0 && regnum
< tdesc
->reg_defs
.size ());
496 return (enum register_status
) (register_status
[regnum
]);
502 /* See gdbsupport/common-regcache.h. */
505 regcache::raw_compare (int regnum
, const void *buf
, int offset
) const
507 gdb_assert (buf
!= NULL
);
509 const unsigned char *regbuf
= register_data (this, regnum
);
510 int size
= register_size (tdesc
, regnum
);
511 gdb_assert (size
>= offset
);
513 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);