1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2018 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"
24 #ifndef IN_PROCESS_AGENT
27 get_thread_regcache (struct thread_info
*thread
, int fetch
)
29 struct regcache
*regcache
;
31 /* Check the target descriptor is still valid for the current target. If
32 not, then clear it and create a new one. */
33 if (!target_validate_tdesc (thread
))
36 free_register_cache (thread_regcache_data (thread
));
37 set_thread_regcache_data (thread
, NULL
);
43 regcache
= thread_regcache_data (thread
);
45 /* Threads' regcaches are created lazily, because biarch targets add
46 the main thread/lwp before seeing it stop for the first time, and
47 it is only after the target sees the thread stop for the first
48 time that the target has a chance of determining the process's
49 architecture. IOW, when we first add the process's main thread
50 we don't know which architecture/tdesc its regcache should
54 struct process_info
*proc
= get_thread_process (thread
);
56 gdb_assert (proc
->tdesc
!= NULL
);
58 regcache
= new_register_cache (proc
->tdesc
);
59 set_thread_regcache_data (thread
, regcache
);
62 if (fetch
&& regcache
->registers_valid
== 0)
64 struct thread_info
*saved_thread
= current_thread
;
66 current_thread
= thread
;
67 /* Invalidate all registers, to prevent stale left-overs. */
68 memset (regcache
->register_status
, REG_UNAVAILABLE
,
69 regcache
->tdesc
->reg_defs
.size ());
70 fetch_inferior_registers (regcache
, -1);
71 current_thread
= saved_thread
;
72 regcache
->registers_valid
= 1;
78 /* See common/common-regcache.h. */
81 get_thread_regcache_for_ptid (ptid_t ptid
)
83 return get_thread_regcache (find_thread_ptid (ptid
), 1);
87 regcache_invalidate_thread (struct thread_info
*thread
)
89 struct regcache
*regcache
;
91 regcache
= thread_regcache_data (thread
);
96 if (regcache
->registers_valid
)
98 struct thread_info
*saved_thread
= current_thread
;
100 current_thread
= thread
;
101 store_inferior_registers (regcache
, -1);
102 current_thread
= saved_thread
;
105 regcache
->registers_valid
= 0;
108 /* See regcache.h. */
111 regcache_invalidate_pid (int pid
)
113 /* Only invalidate the regcaches of threads of this process. */
114 for_each_thread (pid
, regcache_invalidate_thread
);
117 /* See regcache.h. */
120 regcache_invalidate (void)
122 /* Only update the threads of the current process. */
123 int pid
= current_thread
->id
.pid ();
125 regcache_invalidate_pid (pid
);
131 init_register_cache (struct regcache
*regcache
,
132 const struct target_desc
*tdesc
,
133 unsigned char *regbuf
)
137 #ifndef IN_PROCESS_AGENT
138 /* Make sure to zero-initialize the register cache when it is
139 created, in case there are registers the target never
140 fetches. This way they'll read as zero instead of
142 regcache
->tdesc
= tdesc
;
144 = (unsigned char *) xcalloc (1, tdesc
->registers_size
);
145 regcache
->registers_owned
= 1;
146 regcache
->register_status
147 = (unsigned char *) xmalloc (tdesc
->reg_defs
.size ());
148 memset ((void *) regcache
->register_status
, REG_UNAVAILABLE
,
149 tdesc
->reg_defs
.size ());
151 gdb_assert_not_reached ("can't allocate memory from the heap");
156 regcache
->tdesc
= tdesc
;
157 regcache
->registers
= regbuf
;
158 regcache
->registers_owned
= 0;
159 #ifndef IN_PROCESS_AGENT
160 regcache
->register_status
= NULL
;
164 regcache
->registers_valid
= 0;
169 #ifndef IN_PROCESS_AGENT
172 new_register_cache (const struct target_desc
*tdesc
)
174 struct regcache
*regcache
= new struct regcache
;
176 gdb_assert (tdesc
->registers_size
!= 0);
178 return init_register_cache (regcache
, tdesc
, NULL
);
182 free_register_cache (struct regcache
*regcache
)
186 if (regcache
->registers_owned
)
187 free (regcache
->registers
);
188 free (regcache
->register_status
);
196 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
198 gdb_assert (src
!= NULL
&& dst
!= NULL
);
199 gdb_assert (src
->tdesc
== dst
->tdesc
);
200 gdb_assert (src
!= dst
);
202 memcpy (dst
->registers
, src
->registers
, src
->tdesc
->registers_size
);
203 #ifndef IN_PROCESS_AGENT
204 if (dst
->register_status
!= NULL
&& src
->register_status
!= NULL
)
205 memcpy (dst
->register_status
, src
->register_status
,
206 src
->tdesc
->reg_defs
.size ());
208 dst
->registers_valid
= src
->registers_valid
;
211 /* Return a reference to the description of register N. */
213 static const struct reg
&
214 find_register_by_number (const struct target_desc
*tdesc
, int n
)
216 return tdesc
->reg_defs
[n
];
219 #ifndef IN_PROCESS_AGENT
222 registers_to_string (struct regcache
*regcache
, char *buf
)
224 unsigned char *registers
= regcache
->registers
;
225 const struct target_desc
*tdesc
= regcache
->tdesc
;
227 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
229 if (regcache
->register_status
[i
] == REG_VALID
)
231 bin2hex (registers
, buf
, register_size (tdesc
, i
));
232 buf
+= register_size (tdesc
, i
) * 2;
236 memset (buf
, 'x', register_size (tdesc
, i
) * 2);
237 buf
+= register_size (tdesc
, i
) * 2;
239 registers
+= register_size (tdesc
, i
);
245 registers_from_string (struct regcache
*regcache
, char *buf
)
247 int len
= strlen (buf
);
248 unsigned char *registers
= regcache
->registers
;
249 const struct target_desc
*tdesc
= regcache
->tdesc
;
251 if (len
!= tdesc
->registers_size
* 2)
253 warning ("Wrong sized register packet (expected %d bytes, got %d)",
254 2 * tdesc
->registers_size
, len
);
255 if (len
> tdesc
->registers_size
* 2)
256 len
= tdesc
->registers_size
* 2;
258 hex2bin (buf
, registers
, len
/ 2);
262 find_regno (const struct target_desc
*tdesc
, const char *name
)
264 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
266 if (strcmp (name
, find_register_by_number (tdesc
, i
).name
) == 0)
269 internal_error (__FILE__
, __LINE__
, "Unknown register %s requested",
274 free_register_cache_thread (struct thread_info
*thread
)
276 struct regcache
*regcache
= thread_regcache_data (thread
);
278 if (regcache
!= NULL
)
280 regcache_invalidate_thread (thread
);
281 free_register_cache (regcache
);
282 set_thread_regcache_data (thread
, NULL
);
287 regcache_release (void)
289 /* Flush and release all pre-existing register caches. */
290 for_each_thread (free_register_cache_thread
);
295 register_cache_size (const struct target_desc
*tdesc
)
297 return tdesc
->registers_size
;
301 register_size (const struct target_desc
*tdesc
, int n
)
303 return find_register_by_number (tdesc
, n
).size
/ 8;
306 /* See common/common-regcache.h. */
309 regcache_register_size (const struct regcache
*regcache
, int n
)
311 return register_size (regcache
->tdesc
, n
);
314 static unsigned char *
315 register_data (const struct regcache
*regcache
, int n
, int fetch
)
317 return (regcache
->registers
318 + find_register_by_number (regcache
->tdesc
, n
).offset
/ 8);
322 supply_register (struct regcache
*regcache
, int n
, const void *buf
)
324 return regcache
->raw_supply (n
, buf
);
327 /* See common/common-regcache.h. */
330 regcache::raw_supply (int n
, const void *buf
)
334 memcpy (register_data (this, n
, 0), buf
, register_size (tdesc
, n
));
335 #ifndef IN_PROCESS_AGENT
336 if (register_status
!= NULL
)
337 register_status
[n
] = REG_VALID
;
342 memset (register_data (this, n
, 0), 0, register_size (tdesc
, n
));
343 #ifndef IN_PROCESS_AGENT
344 if (register_status
!= NULL
)
345 register_status
[n
] = REG_UNAVAILABLE
;
350 /* Supply register N with value zero to REGCACHE. */
353 supply_register_zeroed (struct regcache
*regcache
, int n
)
355 memset (register_data (regcache
, n
, 0), 0,
356 register_size (regcache
->tdesc
, n
));
357 #ifndef IN_PROCESS_AGENT
358 if (regcache
->register_status
!= NULL
)
359 regcache
->register_status
[n
] = REG_VALID
;
363 #ifndef IN_PROCESS_AGENT
365 /* Supply register called NAME with value zero to REGCACHE. */
368 supply_register_by_name_zeroed (struct regcache
*regcache
,
371 supply_register_zeroed (regcache
, find_regno (regcache
->tdesc
, name
));
376 /* Supply the whole register set whose contents are stored in BUF, to
377 REGCACHE. If BUF is NULL, all the registers' values are recorded
381 supply_regblock (struct regcache
*regcache
, const void *buf
)
385 const struct target_desc
*tdesc
= regcache
->tdesc
;
387 memcpy (regcache
->registers
, buf
, tdesc
->registers_size
);
388 #ifndef IN_PROCESS_AGENT
392 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
393 regcache
->register_status
[i
] = REG_VALID
;
399 const struct target_desc
*tdesc
= regcache
->tdesc
;
401 memset (regcache
->registers
, 0, tdesc
->registers_size
);
402 #ifndef IN_PROCESS_AGENT
406 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
407 regcache
->register_status
[i
] = REG_UNAVAILABLE
;
413 #ifndef IN_PROCESS_AGENT
416 supply_register_by_name (struct regcache
*regcache
,
417 const char *name
, const void *buf
)
419 supply_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
425 collect_register (struct regcache
*regcache
, int n
, void *buf
)
427 regcache
->raw_collect (n
, buf
);
430 /* See common/common-regcache.h. */
433 regcache::raw_collect (int n
, void *buf
) const
435 memcpy (buf
, register_data (this, n
, 1), register_size (tdesc
, n
));
439 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
444 gdb_assert (regcache
!= NULL
);
445 gdb_assert (regnum
>= 0
446 && regnum
< regcache
->tdesc
->reg_defs
.size ());
448 size
= register_size (regcache
->tdesc
, regnum
);
450 if (size
> (int) sizeof (ULONGEST
))
451 error (_("That operation is not available on integers of more than"
453 (int) sizeof (ULONGEST
));
456 collect_register (regcache
, regnum
, val
);
461 #ifndef IN_PROCESS_AGENT
463 /* See regcache.h. */
466 regcache_raw_get_unsigned_by_name (struct regcache
*regcache
,
469 return regcache_raw_get_unsigned (regcache
,
470 find_regno (regcache
->tdesc
, name
));
474 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
476 bin2hex (register_data (regcache
, n
, 1), buf
,
477 register_size (regcache
->tdesc
, n
));
481 collect_register_by_name (struct regcache
*regcache
,
482 const char *name
, void *buf
)
484 collect_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
487 /* Special handling for register PC. */
490 regcache_read_pc (struct regcache
*regcache
)
494 if (the_target
->read_pc
)
495 pc_val
= the_target
->read_pc (regcache
);
497 internal_error (__FILE__
, __LINE__
,
498 "regcache_read_pc: Unable to find PC");
504 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
506 if (the_target
->write_pc
)
507 the_target
->write_pc (regcache
, pc
);
509 internal_error (__FILE__
, __LINE__
,
510 "regcache_write_pc: Unable to update PC");
515 /* See common/common-regcache.h. */
518 regcache::get_register_status (int regnum
) const
520 #ifndef IN_PROCESS_AGENT
521 gdb_assert (regnum
>= 0 && regnum
< tdesc
->reg_defs
.size ());
522 return (enum register_status
) (register_status
[regnum
]);
528 /* See common/common-regcache.h. */
531 regcache::raw_compare (int regnum
, const void *buf
, int offset
) const
533 gdb_assert (buf
!= NULL
);
535 const unsigned char *regbuf
= register_data (this, regnum
, 1);
536 int size
= register_size (tdesc
, regnum
);
537 gdb_assert (size
>= offset
);
539 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);