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