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