Define YYOBJ in terms of YYFILES
[deliverable/binutils-gdb.git] / gdb / gdbserver / regcache.c
CommitLineData
0a30fbc4 1/* Register support routines for the remote server for GDB.
61baf725 2 Copyright (C) 2001-2017 Free Software Foundation, Inc.
0a30fbc4
DJ
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
0a30fbc4
DJ
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
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
0a30fbc4
DJ
18
19#include "server.h"
20#include "regdef.h"
623b6bdf 21#include "gdbthread.h"
3aee8918 22#include "tdesc.h"
9c3d6531 23#include "rsp-low.h"
fa593d66
PA
24#ifndef IN_PROCESS_AGENT
25
442ea881
PA
26struct regcache *
27get_thread_regcache (struct thread_info *thread, int fetch)
c04a1aa8 28{
442ea881 29 struct regcache *regcache;
c04a1aa8 30
6afd337d 31 regcache = thread_regcache_data (thread);
c04a1aa8 32
3aee8918
PA
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. */
c04a1aa8 40 if (regcache == NULL)
3aee8918
PA
41 {
42 struct process_info *proc = get_thread_process (thread);
43
38e08fca 44 gdb_assert (proc->tdesc != NULL);
3aee8918
PA
45
46 regcache = new_register_cache (proc->tdesc);
6afd337d 47 set_thread_regcache_data (thread, regcache);
3aee8918 48 }
c04a1aa8 49
0d62e5e8
DJ
50 if (fetch && regcache->registers_valid == 0)
51 {
0bfdf32f 52 struct thread_info *saved_thread = current_thread;
442ea881 53
0bfdf32f 54 current_thread = thread;
098dbe61 55 /* Invalidate all registers, to prevent stale left-overs. */
c4dfafab
SDJ
56 memset (regcache->register_status, REG_UNAVAILABLE,
57 regcache->tdesc->reg_defs.size ());
442ea881 58 fetch_inferior_registers (regcache, -1);
0bfdf32f 59 current_thread = saved_thread;
0d62e5e8
DJ
60 regcache->registers_valid = 1;
61 }
62
c04a1aa8
DJ
63 return regcache;
64}
65
361c8ade
GB
66/* See common/common-regcache.h. */
67
68struct regcache *
69get_thread_regcache_for_ptid (ptid_t ptid)
70{
71 return get_thread_regcache (find_thread_ptid (ptid), 1);
72}
73
0d62e5e8 74void
3aee8918 75regcache_invalidate_thread (struct thread_info *thread)
0d62e5e8 76{
442ea881 77 struct regcache *regcache;
0d62e5e8 78
6afd337d 79 regcache = thread_regcache_data (thread);
0d62e5e8 80
45ba0d02
PA
81 if (regcache == NULL)
82 return;
83
0d62e5e8
DJ
84 if (regcache->registers_valid)
85 {
0bfdf32f 86 struct thread_info *saved_thread = current_thread;
0d62e5e8 87
0bfdf32f 88 current_thread = thread;
442ea881 89 store_inferior_registers (regcache, -1);
0bfdf32f 90 current_thread = saved_thread;
0d62e5e8
DJ
91 }
92
93 regcache->registers_valid = 0;
94}
95
3aee8918 96static int
9c80ecd6 97regcache_invalidate_one (thread_info *thread, void *pid_p)
3aee8918 98{
3aee8918
PA
99 int pid = *(int *) pid_p;
100
101 /* Only invalidate the regcaches of threads of this process. */
9c80ecd6 102 if (thread->id.pid () == pid)
3aee8918
PA
103 regcache_invalidate_thread (thread);
104
105 return 0;
106}
107
80d82c19
JB
108/* See regcache.h. */
109
110void
111regcache_invalidate_pid (int pid)
112{
113 find_inferior (&all_threads, regcache_invalidate_one, &pid);
114}
115
116/* See regcache.h. */
117
0d62e5e8 118void
442ea881 119regcache_invalidate (void)
0d62e5e8 120{
3aee8918 121 /* Only update the threads of the current process. */
9c80ecd6 122 int pid = current_thread->id.pid ();
3aee8918 123
80d82c19 124 regcache_invalidate_pid (pid);
0d62e5e8
DJ
125}
126
fa593d66
PA
127#endif
128
219f2f23 129struct regcache *
3aee8918
PA
130init_register_cache (struct regcache *regcache,
131 const struct target_desc *tdesc,
132 unsigned char *regbuf)
219f2f23
PA
133{
134 if (regbuf == NULL)
135 {
87f6c4e3 136#ifndef IN_PROCESS_AGENT
219f2f23
PA
137 /* Make sure to zero-initialize the register cache when it is
138 created, in case there are registers the target never
139 fetches. This way they'll read as zero instead of
140 garbage. */
3aee8918 141 regcache->tdesc = tdesc;
224c3ddb
SM
142 regcache->registers
143 = (unsigned char *) xcalloc (1, tdesc->registers_size);
219f2f23 144 regcache->registers_owned = 1;
224c3ddb 145 regcache->register_status
c4dfafab
SDJ
146 = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
147 memset ((void *) regcache->register_status, REG_UNAVAILABLE,
148 tdesc->reg_defs.size ());
fa593d66 149#else
38e08fca 150 gdb_assert_not_reached ("can't allocate memory from the heap");
fa593d66 151#endif
87f6c4e3
GB
152 }
153 else
219f2f23 154 {
3aee8918 155 regcache->tdesc = tdesc;
219f2f23
PA
156 regcache->registers = regbuf;
157 regcache->registers_owned = 0;
1c79eb8a
PA
158#ifndef IN_PROCESS_AGENT
159 regcache->register_status = NULL;
160#endif
219f2f23
PA
161 }
162
163 regcache->registers_valid = 0;
164
165 return regcache;
166}
167
fa593d66
PA
168#ifndef IN_PROCESS_AGENT
169
442ea881 170struct regcache *
3aee8918 171new_register_cache (const struct target_desc *tdesc)
c04a1aa8 172{
8d749320 173 struct regcache *regcache = XCNEW (struct regcache);
c04a1aa8 174
3aee8918 175 gdb_assert (tdesc->registers_size != 0);
5822d809 176
3aee8918 177 return init_register_cache (regcache, tdesc, NULL);
c04a1aa8
DJ
178}
179
180void
442ea881 181free_register_cache (struct regcache *regcache)
c04a1aa8 182{
5822d809
PA
183 if (regcache)
184 {
fa593d66
PA
185 if (regcache->registers_owned)
186 free (regcache->registers);
1c79eb8a 187 free (regcache->register_status);
5822d809
PA
188 free (regcache);
189 }
c04a1aa8
DJ
190}
191
fa593d66
PA
192#endif
193
219f2f23
PA
194void
195regcache_cpy (struct regcache *dst, struct regcache *src)
196{
3aee8918
PA
197 gdb_assert (src != NULL && dst != NULL);
198 gdb_assert (src->tdesc == dst->tdesc);
199 gdb_assert (src != dst);
200
201 memcpy (dst->registers, src->registers, src->tdesc->registers_size);
1c79eb8a
PA
202#ifndef IN_PROCESS_AGENT
203 if (dst->register_status != NULL && src->register_status != NULL)
3aee8918 204 memcpy (dst->register_status, src->register_status,
c4dfafab 205 src->tdesc->reg_defs.size ());
1c79eb8a 206#endif
219f2f23
PA
207 dst->registers_valid = src->registers_valid;
208}
209
219f2f23 210
fa593d66
PA
211#ifndef IN_PROCESS_AGENT
212
0a30fbc4 213void
442ea881 214registers_to_string (struct regcache *regcache, char *buf)
0a30fbc4 215{
442ea881 216 unsigned char *registers = regcache->registers;
3aee8918 217 const struct target_desc *tdesc = regcache->tdesc;
c04a1aa8 218
c4dfafab 219 for (int i = 0; i < tdesc->reg_defs.size (); ++i)
1c79eb8a 220 {
c4dfafab
SDJ
221 struct reg *reg = tdesc->reg_defs[i];
222
1c79eb8a
PA
223 if (regcache->register_status[i] == REG_VALID)
224 {
e9371aff 225 bin2hex (registers, buf, register_size (tdesc, i));
3aee8918 226 buf += register_size (tdesc, i) * 2;
1c79eb8a
PA
227 }
228 else
229 {
3aee8918
PA
230 memset (buf, 'x', register_size (tdesc, i) * 2);
231 buf += register_size (tdesc, i) * 2;
1c79eb8a 232 }
3aee8918 233 registers += register_size (tdesc, i);
1c79eb8a
PA
234 }
235 *buf = '\0';
0a30fbc4
DJ
236}
237
238void
442ea881 239registers_from_string (struct regcache *regcache, char *buf)
0a30fbc4
DJ
240{
241 int len = strlen (buf);
442ea881 242 unsigned char *registers = regcache->registers;
3aee8918 243 const struct target_desc *tdesc = regcache->tdesc;
0a30fbc4 244
3aee8918 245 if (len != tdesc->registers_size * 2)
0a30fbc4 246 {
1b3f6016 247 warning ("Wrong sized register packet (expected %d bytes, got %d)",
3aee8918
PA
248 2 * tdesc->registers_size, len);
249 if (len > tdesc->registers_size * 2)
250 len = tdesc->registers_size * 2;
0a30fbc4 251 }
a7191e8b 252 hex2bin (buf, registers, len / 2);
0a30fbc4
DJ
253}
254
0a30fbc4 255int
3aee8918 256find_regno (const struct target_desc *tdesc, const char *name)
0a30fbc4 257{
c4dfafab
SDJ
258 for (int i = 0; i < tdesc->reg_defs.size (); ++i)
259 {
260 struct reg *reg = tdesc->reg_defs[i];
0a30fbc4 261
c4dfafab
SDJ
262 if (strcmp (name, reg->name) == 0)
263 return i;
264 }
38e08fca
GB
265 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
266 name);
0a30fbc4
DJ
267}
268
f7000548
YQ
269#endif
270
0a30fbc4 271struct reg *
3aee8918 272find_register_by_number (const struct target_desc *tdesc, int n)
0a30fbc4 273{
c4dfafab 274 return tdesc->reg_defs[n];
0a30fbc4
DJ
275}
276
3aee8918
PA
277#ifndef IN_PROCESS_AGENT
278static void
279free_register_cache_thread (struct thread_info *thread)
280{
6afd337d 281 struct regcache *regcache = thread_regcache_data (thread);
3aee8918
PA
282
283 if (regcache != NULL)
284 {
285 regcache_invalidate_thread (thread);
286 free_register_cache (regcache);
6afd337d 287 set_thread_regcache_data (thread, NULL);
3aee8918
PA
288 }
289}
290
3aee8918
PA
291void
292regcache_release (void)
293{
294 /* Flush and release all pre-existing register caches. */
9c80ecd6 295 for_each_inferior (&all_threads, free_register_cache_thread);
3aee8918
PA
296}
297#endif
298
0a30fbc4 299int
3aee8918 300register_cache_size (const struct target_desc *tdesc)
0a30fbc4 301{
3aee8918
PA
302 return tdesc->registers_size;
303}
304
305int
306register_size (const struct target_desc *tdesc, int n)
307{
f7000548 308 return find_register_by_number (tdesc, n)->size / 8;
0a30fbc4
DJ
309}
310
8d689ee5
YQ
311/* See common/common-regcache.h. */
312
313int
314regcache_register_size (const struct regcache *regcache, int n)
315{
316 return register_size (regcache->tdesc, n);
317}
318
f450004a 319static unsigned char *
442ea881 320register_data (struct regcache *regcache, int n, int fetch)
0a30fbc4 321{
f7000548
YQ
322 return (regcache->registers
323 + find_register_by_number (regcache->tdesc, n)->offset / 8);
0a30fbc4
DJ
324}
325
1c79eb8a
PA
326/* Supply register N, whose contents are stored in BUF, to REGCACHE.
327 If BUF is NULL, the register's value is recorded as
328 unavailable. */
329
58caa3dc 330void
442ea881 331supply_register (struct regcache *regcache, int n, const void *buf)
58caa3dc 332{
3327ccf7 333 if (buf)
1c79eb8a 334 {
3aee8918
PA
335 memcpy (register_data (regcache, n, 0), buf,
336 register_size (regcache->tdesc, n));
1c79eb8a
PA
337#ifndef IN_PROCESS_AGENT
338 if (regcache->register_status != NULL)
339 regcache->register_status[n] = REG_VALID;
340#endif
341 }
3327ccf7 342 else
1c79eb8a 343 {
3aee8918
PA
344 memset (register_data (regcache, n, 0), 0,
345 register_size (regcache->tdesc, n));
1c79eb8a
PA
346#ifndef IN_PROCESS_AGENT
347 if (regcache->register_status != NULL)
348 regcache->register_status[n] = REG_UNAVAILABLE;
349#endif
350 }
58caa3dc
DJ
351}
352
1c79eb8a
PA
353/* Supply register N with value zero to REGCACHE. */
354
355void
356supply_register_zeroed (struct regcache *regcache, int n)
357{
3aee8918
PA
358 memset (register_data (regcache, n, 0), 0,
359 register_size (regcache->tdesc, n));
1c79eb8a
PA
360#ifndef IN_PROCESS_AGENT
361 if (regcache->register_status != NULL)
362 regcache->register_status[n] = REG_VALID;
363#endif
364}
365
366/* Supply the whole register set whose contents are stored in BUF, to
367 REGCACHE. If BUF is NULL, all the registers' values are recorded
368 as unavailable. */
369
219f2f23
PA
370void
371supply_regblock (struct regcache *regcache, const void *buf)
372{
373 if (buf)
1c79eb8a 374 {
3aee8918
PA
375 const struct target_desc *tdesc = regcache->tdesc;
376
377 memcpy (regcache->registers, buf, tdesc->registers_size);
1c79eb8a
PA
378#ifndef IN_PROCESS_AGENT
379 {
380 int i;
381
c4dfafab 382 for (i = 0; i < tdesc->reg_defs.size (); i++)
1c79eb8a
PA
383 regcache->register_status[i] = REG_VALID;
384 }
385#endif
386 }
219f2f23 387 else
1c79eb8a 388 {
3aee8918
PA
389 const struct target_desc *tdesc = regcache->tdesc;
390
391 memset (regcache->registers, 0, tdesc->registers_size);
1c79eb8a
PA
392#ifndef IN_PROCESS_AGENT
393 {
394 int i;
395
c4dfafab 396 for (i = 0; i < tdesc->reg_defs.size (); i++)
1c79eb8a
PA
397 regcache->register_status[i] = REG_UNAVAILABLE;
398 }
399#endif
400 }
219f2f23
PA
401}
402
fa593d66
PA
403#ifndef IN_PROCESS_AGENT
404
58caa3dc 405void
442ea881
PA
406supply_register_by_name (struct regcache *regcache,
407 const char *name, const void *buf)
58caa3dc 408{
3aee8918 409 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
58caa3dc
DJ
410}
411
fa593d66
PA
412#endif
413
58caa3dc 414void
442ea881 415collect_register (struct regcache *regcache, int n, void *buf)
58caa3dc 416{
3aee8918
PA
417 memcpy (buf, register_data (regcache, n, 1),
418 register_size (regcache->tdesc, n));
0d62e5e8
DJ
419}
420
68ce2059
AT
421enum register_status
422regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
423 ULONGEST *val)
424{
425 int size;
426
427 gdb_assert (regcache != NULL);
f7000548 428 gdb_assert (regnum >= 0
c4dfafab 429 && regnum < regcache->tdesc->reg_defs.size ());
68ce2059
AT
430
431 size = register_size (regcache->tdesc, regnum);
432
433 if (size > (int) sizeof (ULONGEST))
434 error (_("That operation is not available on integers of more than"
435 "%d bytes."),
436 (int) sizeof (ULONGEST));
437
9f6a71b4 438 *val = 0;
68ce2059
AT
439 collect_register (regcache, regnum, val);
440
441 return REG_VALID;
442}
443
fa593d66
PA
444#ifndef IN_PROCESS_AGENT
445
0d62e5e8 446void
442ea881 447collect_register_as_string (struct regcache *regcache, int n, char *buf)
0d62e5e8 448{
e9371aff
TT
449 bin2hex (register_data (regcache, n, 1), buf,
450 register_size (regcache->tdesc, n));
58caa3dc
DJ
451}
452
453void
442ea881
PA
454collect_register_by_name (struct regcache *regcache,
455 const char *name, void *buf)
58caa3dc 456{
3aee8918 457 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
58caa3dc 458}
219f2f23
PA
459
460/* Special handling for register PC. */
461
462CORE_ADDR
463regcache_read_pc (struct regcache *regcache)
464{
465 CORE_ADDR pc_val;
466
467 if (the_target->read_pc)
468 pc_val = the_target->read_pc (regcache);
469 else
470 internal_error (__FILE__, __LINE__,
471 "regcache_read_pc: Unable to find PC");
472
473 return pc_val;
474}
475
476void
477regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
478{
479 if (the_target->write_pc)
480 the_target->write_pc (regcache, pc);
481 else
482 internal_error (__FILE__, __LINE__,
483 "regcache_write_pc: Unable to update PC");
484}
fa593d66
PA
485
486#endif
This page took 1.128331 seconds and 4 git commands to generate.