GDBserver
regcache.c
Go to the documentation of this file.
1 /* Register support routines for the remote server for GDB.
2  Copyright (C) 2001-2018 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 "server.h"
20 #include "regdef.h"
21 #include "gdbthread.h"
22 #include "tdesc.h"
23 #include "rsp-low.h"
24 #ifndef IN_PROCESS_AGENT
25 
26 struct regcache *
27 get_thread_regcache (struct thread_info *thread, int fetch)
28 {
29  struct regcache *regcache;
30 
31  regcache = thread_regcache_data (thread);
32 
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)
41  {
42  struct process_info *proc = get_thread_process (thread);
43 
44  gdb_assert (proc->tdesc != NULL);
45 
48  }
49 
50  if (fetch && regcache->registers_valid == 0)
51  {
52  struct thread_info *saved_thread = current_thread;
53 
54  current_thread = thread;
55  /* Invalidate all registers, to prevent stale left-overs. */
57  regcache->tdesc->reg_defs.size ());
59  current_thread = saved_thread;
61  }
62 
63  return regcache;
64 }
65 
66 /* See common/common-regcache.h. */
67 
68 struct regcache *
70 {
71  return get_thread_regcache (find_thread_ptid (ptid), 1);
72 }
73 
74 void
76 {
77  struct regcache *regcache;
78 
79  regcache = thread_regcache_data (thread);
80 
81  if (regcache == NULL)
82  return;
83 
85  {
86  struct thread_info *saved_thread = current_thread;
87 
88  current_thread = thread;
90  current_thread = saved_thread;
91  }
92 
94 }
95 
96 /* See regcache.h. */
97 
98 void
100 {
101  /* Only invalidate the regcaches of threads of this process. */
103 }
104 
105 /* See regcache.h. */
106 
107 void
109 {
110  /* Only update the threads of the current process. */
111  int pid = current_thread->id.pid ();
112 
114 }
115 
116 #endif
117 
118 struct regcache *
120  const struct target_desc *tdesc,
121  unsigned char *regbuf)
122 {
123  if (regbuf == NULL)
124  {
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;
132  = (unsigned char *) xcalloc (1, tdesc->registers_size);
135  = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
137  tdesc->reg_defs.size ());
138 #else
139  gdb_assert_not_reached ("can't allocate memory from the heap");
140 #endif
141  }
142  else
143  {
144  regcache->tdesc = tdesc;
145  regcache->registers = regbuf;
147 #ifndef IN_PROCESS_AGENT
148  regcache->register_status = NULL;
149 #endif
150  }
151 
153 
154  return regcache;
155 }
156 
157 #ifndef IN_PROCESS_AGENT
158 
159 struct regcache *
161 {
162  struct regcache *regcache = XCNEW (struct regcache);
163 
165 
166  return init_register_cache (regcache, tdesc, NULL);
167 }
168 
169 void
171 {
172  if (regcache)
173  {
177  free (regcache);
178  }
179 }
180 
181 #endif
182 
183 void
184 regcache_cpy (struct regcache *dst, struct regcache *src)
185 {
186  gdb_assert (src != NULL && dst != NULL);
187  gdb_assert (src->tdesc == dst->tdesc);
188  gdb_assert (src != dst);
189 
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;
197 }
198 
199 
200 #ifndef IN_PROCESS_AGENT
201 
202 void
204 {
205  unsigned char *registers = regcache->registers;
206  const struct target_desc *tdesc = regcache->tdesc;
207 
208  for (int i = 0; i < tdesc->reg_defs.size (); ++i)
209  {
211  {
212  bin2hex (registers, buf, register_size (tdesc, i));
213  buf += register_size (tdesc, i) * 2;
214  }
215  else
216  {
217  memset (buf, 'x', register_size (tdesc, i) * 2);
218  buf += register_size (tdesc, i) * 2;
219  }
220  registers += register_size (tdesc, i);
221  }
222  *buf = '\0';
223 }
224 
225 void
227 {
228  int len = strlen (buf);
229  unsigned char *registers = regcache->registers;
230  const struct target_desc *tdesc = regcache->tdesc;
231 
232  if (len != tdesc->registers_size * 2)
233  {
234  warning ("Wrong sized register packet (expected %d bytes, got %d)",
235  2 * tdesc->registers_size, len);
236  if (len > tdesc->registers_size * 2)
237  len = tdesc->registers_size * 2;
238  }
239  hex2bin (buf, registers, len / 2);
240 }
241 
242 int
243 find_regno (const struct target_desc *tdesc, const char *name)
244 {
245  for (int i = 0; i < tdesc->reg_defs.size (); ++i)
246  {
247  struct reg *reg = tdesc->reg_defs[i];
248 
249  if (strcmp (name, reg->name) == 0)
250  return i;
251  }
252  internal_error (__FILE__, __LINE__, "Unknown register %s requested",
253  name);
254 }
255 
256 #endif
257 
258 struct reg *
259 find_register_by_number (const struct target_desc *tdesc, int n)
260 {
261  return tdesc->reg_defs[n];
262 }
263 
264 #ifndef IN_PROCESS_AGENT
265 static void
267 {
268  struct regcache *regcache = thread_regcache_data (thread);
269 
270  if (regcache != NULL)
271  {
274  set_thread_regcache_data (thread, NULL);
275  }
276 }
277 
278 void
280 {
281  /* Flush and release all pre-existing register caches. */
283 }
284 #endif
285 
286 int
288 {
289  return tdesc->registers_size;
290 }
291 
292 int
293 register_size (const struct target_desc *tdesc, int n)
294 {
295  return find_register_by_number (tdesc, n)->size / 8;
296 }
297 
298 /* See common/common-regcache.h. */
299 
300 int
302 {
303  return register_size (regcache->tdesc, n);
304 }
305 
306 static unsigned char *
307 register_data (struct regcache *regcache, int n, int fetch)
308 {
309  return (regcache->registers
310  + find_register_by_number (regcache->tdesc, n)->offset / 8);
311 }
312 
313 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
314  If BUF is NULL, the register's value is recorded as
315  unavailable. */
316 
317 void
318 supply_register (struct regcache *regcache, int n, const void *buf)
319 {
320  if (buf)
321  {
322  memcpy (register_data (regcache, n, 0), buf,
324 #ifndef IN_PROCESS_AGENT
325  if (regcache->register_status != NULL)
327 #endif
328  }
329  else
330  {
331  memset (register_data (regcache, n, 0), 0,
333 #ifndef IN_PROCESS_AGENT
334  if (regcache->register_status != NULL)
336 #endif
337  }
338 }
339 
340 /* Supply register N with value zero to REGCACHE. */
341 
342 void
344 {
345  memset (register_data (regcache, n, 0), 0,
347 #ifndef IN_PROCESS_AGENT
348  if (regcache->register_status != NULL)
350 #endif
351 }
352 
353 /* Supply the whole register set whose contents are stored in BUF, to
354  REGCACHE. If BUF is NULL, all the registers' values are recorded
355  as unavailable. */
356 
357 void
358 supply_regblock (struct regcache *regcache, const void *buf)
359 {
360  if (buf)
361  {
362  const struct target_desc *tdesc = regcache->tdesc;
363 
364  memcpy (regcache->registers, buf, tdesc->registers_size);
365 #ifndef IN_PROCESS_AGENT
366  {
367  int i;
368 
369  for (i = 0; i < tdesc->reg_defs.size (); i++)
371  }
372 #endif
373  }
374  else
375  {
376  const struct target_desc *tdesc = regcache->tdesc;
377 
378  memset (regcache->registers, 0, tdesc->registers_size);
379 #ifndef IN_PROCESS_AGENT
380  {
381  int i;
382 
383  for (i = 0; i < tdesc->reg_defs.size (); i++)
385  }
386 #endif
387  }
388 }
389 
390 #ifndef IN_PROCESS_AGENT
391 
392 void
394  const char *name, const void *buf)
395 {
397 }
398 
399 #endif
400 
401 void
402 collect_register (struct regcache *regcache, int n, void *buf)
403 {
404  memcpy (buf, register_data (regcache, n, 1),
406 }
407 
408 enum register_status
410  ULONGEST *val)
411 {
412  int size;
413 
414  gdb_assert (regcache != NULL);
415  gdb_assert (regnum >= 0
416  && regnum < regcache->tdesc->reg_defs.size ());
417 
418  size = register_size (regcache->tdesc, regnum);
419 
420  if (size > (int) sizeof (ULONGEST))
421  error (_("That operation is not available on integers of more than"
422  "%d bytes."),
423  (int) sizeof (ULONGEST));
424 
425  *val = 0;
427 
428  return REG_VALID;
429 }
430 
431 #ifndef IN_PROCESS_AGENT
432 
433 void
434 collect_register_as_string (struct regcache *regcache, int n, char *buf)
435 {
436  bin2hex (register_data (regcache, n, 1), buf,
438 }
439 
440 void
442  const char *name, void *buf)
443 {
445 }
446 
447 /* Special handling for register PC. */
448 
449 CORE_ADDR
451 {
452  CORE_ADDR pc_val;
453 
454  if (the_target->read_pc)
455  pc_val = the_target->read_pc (regcache);
456  else
457  internal_error (__FILE__, __LINE__,
458  "regcache_read_pc: Unable to find PC");
459 
460  return pc_val;
461 }
462 
463 void
465 {
466  if (the_target->write_pc)
468  else
469  internal_error (__FILE__, __LINE__,
470  "regcache_write_pc: Unable to update PC");
471 }
472 
473 #endif
const struct target_desc * tdesc
Definition: regcache.h:34
int registers_valid
Definition: regcache.h:41
CORE_ADDR(* read_pc)(struct regcache *regcache)
Definition: target.h:321
struct thread_info * current_thread
Definition: inferiors.c:28
void collect_register(struct regcache *regcache, int n, void *buf)
Definition: regcache.c:402
constexpr int pid() const
Definition: ptid.h:53
struct thread_info * find_thread_ptid(ptid_t ptid)
Definition: inferiors.c:64
bfd_vma CORE_ADDR
Definition: common-types.h:41
struct regcache * new_register_cache(const struct target_desc *tdesc)
Definition: regcache.c:160
void set_thread_regcache_data(struct thread_info *thread, struct regcache *data)
Definition: inferiors.c:123
void supply_register_by_name(struct regcache *regcache, const char *name, const void *buf)
Definition: regcache.c:393
CORE_ADDR regcache_read_pc(struct regcache *regcache)
Definition: regcache.c:450
void supply_register_zeroed(struct regcache *regcache, int n)
Definition: regcache.c:343
void warning(const char *fmt,...)
Definition: errors.c:26
struct reg * find_register_by_number(const struct target_desc *tdesc, int n)
Definition: regcache.c:259
void regcache_release(void)
Definition: regcache.c:279
int bin2hex(const gdb_byte *bin, char *hex, int count)
Definition: rsp-low.c:173
void * memset(T *s, int c, size_t n)=delete
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
struct regcache * thread_regcache_data(struct thread_info *thread)
Definition: inferiors.c:117
int regcache_register_size(const struct regcache *regcache, int n)
Definition: regcache.c:301
const char * name
Definition: tracepoint.c:180
register_status
int registers_owned
Definition: regcache.h:42
regnum
#define _(String)
Definition: gdb_locale.h:35
static void for_each_thread(Func func)
Definition: gdbthread.h:142
void regcache_write_pc(struct regcache *regcache, CORE_ADDR pc)
Definition: regcache.c:464
struct target_ops * the_target
Definition: target.c:24
const struct target_desc * tdesc
Definition: inferiors.h:67
unsigned char * registers
Definition: regcache.h:43
static void free_register_cache_thread(struct thread_info *thread)
Definition: regcache.c:266
unsigned char * register_status
Definition: regcache.h:46
void collect_register_by_name(struct regcache *regcache, const char *name, void *buf)
Definition: regcache.c:441
void regcache_invalidate(void)
Definition: regcache.c:108
void collect_register_as_string(struct regcache *regcache, int n, char *buf)
Definition: regcache.c:434
Definition: ptid.h:35
#define gdb_assert_not_reached(message)
Definition: gdb_assert.h:55
int register_size(const struct target_desc *tdesc, int n)
Definition: regcache.c:293
void free(T *ptr)=delete
void regcache_cpy(struct regcache *dst, struct regcache *src)
Definition: regcache.c:184
int register_cache_size(const struct target_desc *tdesc)
Definition: regcache.c:287
enum register_status regcache_raw_read_unsigned(struct regcache *regcache, int regnum, ULONGEST *val)
Definition: regcache.c:409
std::vector< struct reg * > reg_defs
Definition: tdesc.h:37
int registers_size
Definition: tdesc.h:40
#define gdb_assert(expr)
Definition: gdb_assert.h:32
struct regcache * init_register_cache(struct regcache *regcache, const struct target_desc *tdesc, unsigned char *regbuf)
Definition: regcache.c:119
ptid_t id
Definition: gdbthread.h:33
int find_regno(const struct target_desc *tdesc, const char *name)
Definition: regcache.c:243
#define XCNEW(T)
Definition: poison.h:121
#define fetch_inferior_registers(regcache, regno)
Definition: target.h:529
void regcache_invalidate_thread(struct thread_info *thread)
Definition: regcache.c:75
void registers_to_string(struct regcache *regcache, char *buf)
Definition: regcache.c:203
struct regcache * get_thread_regcache(struct thread_info *thread, int fetch)
Definition: regcache.c:27
void free_register_cache(struct regcache *regcache)
Definition: regcache.c:170
void regcache_invalidate_pid(int pid)
Definition: regcache.c:99
static unsigned char * register_data(struct regcache *regcache, int n, int fetch)
Definition: regcache.c:307
unsigned long long ULONGEST
Definition: common-types.h:53
PTR xmalloc(size_t size)
Definition: common-utils.c:35
int hex2bin(const char *hex, gdb_byte *bin, int count)
Definition: rsp-low.c:115
void supply_register(struct regcache *regcache, int n, const void *buf)
Definition: regcache.c:318
struct regcache * get_thread_regcache_for_ptid(ptid_t ptid)
Definition: regcache.c:69
PTR xcalloc(size_t number, size_t size)
Definition: common-utils.c:72
#define store_inferior_registers(regcache, regno)
Definition: target.h:532
void(* write_pc)(struct regcache *regcache, CORE_ADDR pc)
Definition: target.h:324
void error(const char *fmt,...)
Definition: errors.c:38
struct process_info * get_thread_process(const struct thread_info *thread)
Definition: inferiors.c:204
void registers_from_string(struct regcache *regcache, char *buf)
Definition: regcache.c:226
void supply_regblock(struct regcache *regcache, const void *buf)
Definition: regcache.c:358