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