]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/regcache.c
Aarch64 SVE: Support changing vector lengths in gdbserver
[thirdparty/binutils-gdb.git] / gdb / gdbserver / regcache.c
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 /* 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))
34 {
35 /* Clear regcache. */
36 free_register_cache (thread_regcache_data (thread));
37 set_thread_regcache_data (thread, NULL);
38 regcache = NULL;
39
40 target_arch_setup ();
41 }
42
43 regcache = thread_regcache_data (thread);
44
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
51 have. */
52 if (regcache == NULL)
53 {
54 struct process_info *proc = get_thread_process (thread);
55
56 gdb_assert (proc->tdesc != NULL);
57
58 regcache = new_register_cache (proc->tdesc);
59 set_thread_regcache_data (thread, regcache);
60 }
61
62 if (fetch && regcache->registers_valid == 0)
63 {
64 struct thread_info *saved_thread = current_thread;
65
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;
73 }
74
75 return regcache;
76 }
77
78 /* See common/common-regcache.h. */
79
80 struct regcache *
81 get_thread_regcache_for_ptid (ptid_t ptid)
82 {
83 return get_thread_regcache (find_thread_ptid (ptid), 1);
84 }
85
86 void
87 regcache_invalidate_thread (struct thread_info *thread)
88 {
89 struct regcache *regcache;
90
91 regcache = thread_regcache_data (thread);
92
93 if (regcache == NULL)
94 return;
95
96 if (regcache->registers_valid)
97 {
98 struct thread_info *saved_thread = current_thread;
99
100 current_thread = thread;
101 store_inferior_registers (regcache, -1);
102 current_thread = saved_thread;
103 }
104
105 regcache->registers_valid = 0;
106 }
107
108 /* See regcache.h. */
109
110 void
111 regcache_invalidate_pid (int pid)
112 {
113 /* Only invalidate the regcaches of threads of this process. */
114 for_each_thread (pid, regcache_invalidate_thread);
115 }
116
117 /* See regcache.h. */
118
119 void
120 regcache_invalidate (void)
121 {
122 /* Only update the threads of the current process. */
123 int pid = current_thread->id.pid ();
124
125 regcache_invalidate_pid (pid);
126 }
127
128 #endif
129
130 struct regcache *
131 init_register_cache (struct regcache *regcache,
132 const struct target_desc *tdesc,
133 unsigned char *regbuf)
134 {
135 if (regbuf == NULL)
136 {
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
141 garbage. */
142 regcache->tdesc = tdesc;
143 regcache->registers
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 ());
150 #else
151 gdb_assert_not_reached ("can't allocate memory from the heap");
152 #endif
153 }
154 else
155 {
156 regcache->tdesc = tdesc;
157 regcache->registers = regbuf;
158 regcache->registers_owned = 0;
159 #ifndef IN_PROCESS_AGENT
160 regcache->register_status = NULL;
161 #endif
162 }
163
164 regcache->registers_valid = 0;
165
166 return regcache;
167 }
168
169 #ifndef IN_PROCESS_AGENT
170
171 struct regcache *
172 new_register_cache (const struct target_desc *tdesc)
173 {
174 struct regcache *regcache = new struct regcache;
175
176 gdb_assert (tdesc->registers_size != 0);
177
178 return init_register_cache (regcache, tdesc, NULL);
179 }
180
181 void
182 free_register_cache (struct regcache *regcache)
183 {
184 if (regcache)
185 {
186 if (regcache->registers_owned)
187 free (regcache->registers);
188 free (regcache->register_status);
189 delete regcache;
190 }
191 }
192
193 #endif
194
195 void
196 regcache_cpy (struct regcache *dst, struct regcache *src)
197 {
198 gdb_assert (src != NULL && dst != NULL);
199 gdb_assert (src->tdesc == dst->tdesc);
200 gdb_assert (src != dst);
201
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 ());
207 #endif
208 dst->registers_valid = src->registers_valid;
209 }
210
211 /* Return a reference to the description of register N. */
212
213 static const struct reg &
214 find_register_by_number (const struct target_desc *tdesc, int n)
215 {
216 return tdesc->reg_defs[n];
217 }
218
219 #ifndef IN_PROCESS_AGENT
220
221 void
222 registers_to_string (struct regcache *regcache, char *buf)
223 {
224 unsigned char *registers = regcache->registers;
225 const struct target_desc *tdesc = regcache->tdesc;
226
227 for (int i = 0; i < tdesc->reg_defs.size (); ++i)
228 {
229 if (regcache->register_status[i] == REG_VALID)
230 {
231 bin2hex (registers, buf, register_size (tdesc, i));
232 buf += register_size (tdesc, i) * 2;
233 }
234 else
235 {
236 memset (buf, 'x', register_size (tdesc, i) * 2);
237 buf += register_size (tdesc, i) * 2;
238 }
239 registers += register_size (tdesc, i);
240 }
241 *buf = '\0';
242 }
243
244 void
245 registers_from_string (struct regcache *regcache, char *buf)
246 {
247 int len = strlen (buf);
248 unsigned char *registers = regcache->registers;
249 const struct target_desc *tdesc = regcache->tdesc;
250
251 if (len != tdesc->registers_size * 2)
252 {
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;
257 }
258 hex2bin (buf, registers, len / 2);
259 }
260
261 int
262 find_regno (const struct target_desc *tdesc, const char *name)
263 {
264 for (int i = 0; i < tdesc->reg_defs.size (); ++i)
265 {
266 if (strcmp (name, find_register_by_number (tdesc, i).name) == 0)
267 return i;
268 }
269 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
270 name);
271 }
272
273 static void
274 free_register_cache_thread (struct thread_info *thread)
275 {
276 struct regcache *regcache = thread_regcache_data (thread);
277
278 if (regcache != NULL)
279 {
280 regcache_invalidate_thread (thread);
281 free_register_cache (regcache);
282 set_thread_regcache_data (thread, NULL);
283 }
284 }
285
286 void
287 regcache_release (void)
288 {
289 /* Flush and release all pre-existing register caches. */
290 for_each_thread (free_register_cache_thread);
291 }
292 #endif
293
294 int
295 register_cache_size (const struct target_desc *tdesc)
296 {
297 return tdesc->registers_size;
298 }
299
300 int
301 register_size (const struct target_desc *tdesc, int n)
302 {
303 return find_register_by_number (tdesc, n).size / 8;
304 }
305
306 /* See common/common-regcache.h. */
307
308 int
309 regcache_register_size (const struct regcache *regcache, int n)
310 {
311 return register_size (regcache->tdesc, n);
312 }
313
314 static unsigned char *
315 register_data (const struct regcache *regcache, int n, int fetch)
316 {
317 return (regcache->registers
318 + find_register_by_number (regcache->tdesc, n).offset / 8);
319 }
320
321 void
322 supply_register (struct regcache *regcache, int n, const void *buf)
323 {
324 return regcache->raw_supply (n, buf);
325 }
326
327 /* See common/common-regcache.h. */
328
329 void
330 regcache::raw_supply (int n, const void *buf)
331 {
332 if (buf)
333 {
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;
338 #endif
339 }
340 else
341 {
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;
346 #endif
347 }
348 }
349
350 /* Supply register N with value zero to REGCACHE. */
351
352 void
353 supply_register_zeroed (struct regcache *regcache, int n)
354 {
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;
360 #endif
361 }
362
363 #ifndef IN_PROCESS_AGENT
364
365 /* Supply register called NAME with value zero to REGCACHE. */
366
367 void
368 supply_register_by_name_zeroed (struct regcache *regcache,
369 const char *name)
370 {
371 supply_register_zeroed (regcache, find_regno (regcache->tdesc, name));
372 }
373
374 #endif
375
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
378 as unavailable. */
379
380 void
381 supply_regblock (struct regcache *regcache, const void *buf)
382 {
383 if (buf)
384 {
385 const struct target_desc *tdesc = regcache->tdesc;
386
387 memcpy (regcache->registers, buf, tdesc->registers_size);
388 #ifndef IN_PROCESS_AGENT
389 {
390 int i;
391
392 for (i = 0; i < tdesc->reg_defs.size (); i++)
393 regcache->register_status[i] = REG_VALID;
394 }
395 #endif
396 }
397 else
398 {
399 const struct target_desc *tdesc = regcache->tdesc;
400
401 memset (regcache->registers, 0, tdesc->registers_size);
402 #ifndef IN_PROCESS_AGENT
403 {
404 int i;
405
406 for (i = 0; i < tdesc->reg_defs.size (); i++)
407 regcache->register_status[i] = REG_UNAVAILABLE;
408 }
409 #endif
410 }
411 }
412
413 #ifndef IN_PROCESS_AGENT
414
415 void
416 supply_register_by_name (struct regcache *regcache,
417 const char *name, const void *buf)
418 {
419 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
420 }
421
422 #endif
423
424 void
425 collect_register (struct regcache *regcache, int n, void *buf)
426 {
427 regcache->raw_collect (n, buf);
428 }
429
430 /* See common/common-regcache.h. */
431
432 void
433 regcache::raw_collect (int n, void *buf) const
434 {
435 memcpy (buf, register_data (this, n, 1), register_size (tdesc, n));
436 }
437
438 enum register_status
439 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
440 ULONGEST *val)
441 {
442 int size;
443
444 gdb_assert (regcache != NULL);
445 gdb_assert (regnum >= 0
446 && regnum < regcache->tdesc->reg_defs.size ());
447
448 size = register_size (regcache->tdesc, regnum);
449
450 if (size > (int) sizeof (ULONGEST))
451 error (_("That operation is not available on integers of more than"
452 "%d bytes."),
453 (int) sizeof (ULONGEST));
454
455 *val = 0;
456 collect_register (regcache, regnum, val);
457
458 return REG_VALID;
459 }
460
461 #ifndef IN_PROCESS_AGENT
462
463 /* See regcache.h. */
464
465 ULONGEST
466 regcache_raw_get_unsigned_by_name (struct regcache *regcache,
467 const char *name)
468 {
469 return regcache_raw_get_unsigned (regcache,
470 find_regno (regcache->tdesc, name));
471 }
472
473 void
474 collect_register_as_string (struct regcache *regcache, int n, char *buf)
475 {
476 bin2hex (register_data (regcache, n, 1), buf,
477 register_size (regcache->tdesc, n));
478 }
479
480 void
481 collect_register_by_name (struct regcache *regcache,
482 const char *name, void *buf)
483 {
484 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
485 }
486
487 /* Special handling for register PC. */
488
489 CORE_ADDR
490 regcache_read_pc (struct regcache *regcache)
491 {
492 CORE_ADDR pc_val;
493
494 if (the_target->read_pc)
495 pc_val = the_target->read_pc (regcache);
496 else
497 internal_error (__FILE__, __LINE__,
498 "regcache_read_pc: Unable to find PC");
499
500 return pc_val;
501 }
502
503 void
504 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
505 {
506 if (the_target->write_pc)
507 the_target->write_pc (regcache, pc);
508 else
509 internal_error (__FILE__, __LINE__,
510 "regcache_write_pc: Unable to update PC");
511 }
512
513 #endif
514
515 /* See common/common-regcache.h. */
516
517 enum register_status
518 regcache::get_register_status (int regnum) const
519 {
520 #ifndef IN_PROCESS_AGENT
521 gdb_assert (regnum >= 0 && regnum < tdesc->reg_defs.size ());
522 return (enum register_status) (register_status[regnum]);
523 #else
524 return REG_VALID;
525 #endif
526 }
527
528 /* See common/common-regcache.h. */
529
530 bool
531 regcache::raw_compare (int regnum, const void *buf, int offset) const
532 {
533 gdb_assert (buf != NULL);
534
535 const unsigned char *regbuf = register_data (this, regnum, 1);
536 int size = register_size (tdesc, regnum);
537 gdb_assert (size >= offset);
538
539 return (memcmp (buf, regbuf + offset, size - offset) == 0);
540 }