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