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