Straightforward fatal to internal_error conversions
[deliverable/binutils-gdb.git] / gdb / gdbserver / regcache.c
... / ...
CommitLineData
1/* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2014 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 = (struct 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_inferior = current_inferior;
53
54 current_inferior = thread;
55 fetch_inferior_registers (regcache, -1);
56 current_inferior = saved_inferior;
57 regcache->registers_valid = 1;
58 }
59
60 return regcache;
61}
62
63void
64regcache_invalidate_thread (struct thread_info *thread)
65{
66 struct regcache *regcache;
67
68 regcache = (struct regcache *) inferior_regcache_data (thread);
69
70 if (regcache == NULL)
71 return;
72
73 if (regcache->registers_valid)
74 {
75 struct thread_info *saved_inferior = current_inferior;
76
77 current_inferior = thread;
78 store_inferior_registers (regcache, -1);
79 current_inferior = saved_inferior;
80 }
81
82 regcache->registers_valid = 0;
83}
84
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
99void
100regcache_invalidate (void)
101{
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);
106}
107
108#endif
109
110struct regcache *
111init_register_cache (struct regcache *regcache,
112 const struct target_desc *tdesc,
113 unsigned char *regbuf)
114{
115 if (regbuf == NULL)
116 {
117#ifndef IN_PROCESS_AGENT
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. */
122 regcache->tdesc = tdesc;
123 regcache->registers = xcalloc (1, tdesc->registers_size);
124 regcache->registers_owned = 1;
125 regcache->register_status = xcalloc (1, tdesc->num_registers);
126 gdb_assert (REG_UNAVAILABLE == 0);
127#else
128 gdb_assert_not_reached ("can't allocate memory from the heap");
129#endif
130 }
131 else
132 {
133 regcache->tdesc = tdesc;
134 regcache->registers = regbuf;
135 regcache->registers_owned = 0;
136#ifndef IN_PROCESS_AGENT
137 regcache->register_status = NULL;
138#endif
139 }
140
141 regcache->registers_valid = 0;
142
143 return regcache;
144}
145
146#ifndef IN_PROCESS_AGENT
147
148struct regcache *
149new_register_cache (const struct target_desc *tdesc)
150{
151 struct regcache *regcache;
152
153 gdb_assert (tdesc->registers_size != 0);
154
155 regcache = xmalloc (sizeof (*regcache));
156 return init_register_cache (regcache, tdesc, NULL);
157}
158
159void
160free_register_cache (struct regcache *regcache)
161{
162 if (regcache)
163 {
164 if (regcache->registers_owned)
165 free (regcache->registers);
166 free (regcache->register_status);
167 free (regcache);
168 }
169}
170
171#endif
172
173void
174regcache_cpy (struct regcache *dst, struct regcache *src)
175{
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);
181#ifndef IN_PROCESS_AGENT
182 if (dst->register_status != NULL && src->register_status != NULL)
183 memcpy (dst->register_status, src->register_status,
184 src->tdesc->num_registers);
185#endif
186 dst->registers_valid = src->registers_valid;
187}
188
189
190#ifndef IN_PROCESS_AGENT
191
192void
193registers_to_string (struct regcache *regcache, char *buf)
194{
195 unsigned char *registers = regcache->registers;
196 const struct target_desc *tdesc = regcache->tdesc;
197 int i;
198
199 for (i = 0; i < tdesc->num_registers; i++)
200 {
201 if (regcache->register_status[i] == REG_VALID)
202 {
203 bin2hex (registers, buf, register_size (tdesc, i));
204 buf += register_size (tdesc, i) * 2;
205 }
206 else
207 {
208 memset (buf, 'x', register_size (tdesc, i) * 2);
209 buf += register_size (tdesc, i) * 2;
210 }
211 registers += register_size (tdesc, i);
212 }
213 *buf = '\0';
214}
215
216void
217registers_from_string (struct regcache *regcache, char *buf)
218{
219 int len = strlen (buf);
220 unsigned char *registers = regcache->registers;
221 const struct target_desc *tdesc = regcache->tdesc;
222
223 if (len != tdesc->registers_size * 2)
224 {
225 warning ("Wrong sized register packet (expected %d bytes, got %d)",
226 2 * tdesc->registers_size, len);
227 if (len > tdesc->registers_size * 2)
228 len = tdesc->registers_size * 2;
229 }
230 hex2bin (buf, registers, len / 2);
231}
232
233struct reg *
234find_register_by_name (const struct target_desc *tdesc, const char *name)
235{
236 int i;
237
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];
241 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
242 name);
243}
244
245int
246find_regno (const struct target_desc *tdesc, const char *name)
247{
248 int i;
249
250 for (i = 0; i < tdesc->num_registers; i++)
251 if (strcmp (name, tdesc->reg_defs[i].name) == 0)
252 return i;
253 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
254 name);
255}
256
257struct reg *
258find_register_by_number (const struct target_desc *tdesc, int n)
259{
260 return &tdesc->reg_defs[n];
261}
262
263#endif
264
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
296int
297register_cache_size (const struct target_desc *tdesc)
298{
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;
306}
307
308static unsigned char *
309register_data (struct regcache *regcache, int n, int fetch)
310{
311 return regcache->registers + regcache->tdesc->reg_defs[n].offset / 8;
312}
313
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
318void
319supply_register (struct regcache *regcache, int n, const void *buf)
320{
321 if (buf)
322 {
323 memcpy (register_data (regcache, n, 0), buf,
324 register_size (regcache->tdesc, n));
325#ifndef IN_PROCESS_AGENT
326 if (regcache->register_status != NULL)
327 regcache->register_status[n] = REG_VALID;
328#endif
329 }
330 else
331 {
332 memset (register_data (regcache, n, 0), 0,
333 register_size (regcache->tdesc, n));
334#ifndef IN_PROCESS_AGENT
335 if (regcache->register_status != NULL)
336 regcache->register_status[n] = REG_UNAVAILABLE;
337#endif
338 }
339}
340
341/* Supply register N with value zero to REGCACHE. */
342
343void
344supply_register_zeroed (struct regcache *regcache, int n)
345{
346 memset (register_data (regcache, n, 0), 0,
347 register_size (regcache->tdesc, n));
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
358void
359supply_regblock (struct regcache *regcache, const void *buf)
360{
361 if (buf)
362 {
363 const struct target_desc *tdesc = regcache->tdesc;
364
365 memcpy (regcache->registers, buf, tdesc->registers_size);
366#ifndef IN_PROCESS_AGENT
367 {
368 int i;
369
370 for (i = 0; i < tdesc->num_registers; i++)
371 regcache->register_status[i] = REG_VALID;
372 }
373#endif
374 }
375 else
376 {
377 const struct target_desc *tdesc = regcache->tdesc;
378
379 memset (regcache->registers, 0, tdesc->registers_size);
380#ifndef IN_PROCESS_AGENT
381 {
382 int i;
383
384 for (i = 0; i < tdesc->num_registers; i++)
385 regcache->register_status[i] = REG_UNAVAILABLE;
386 }
387#endif
388 }
389}
390
391#ifndef IN_PROCESS_AGENT
392
393void
394supply_register_by_name (struct regcache *regcache,
395 const char *name, const void *buf)
396{
397 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
398}
399
400#endif
401
402void
403collect_register (struct regcache *regcache, int n, void *buf)
404{
405 memcpy (buf, register_data (regcache, n, 1),
406 register_size (regcache->tdesc, n));
407}
408
409#ifndef IN_PROCESS_AGENT
410
411void
412collect_register_as_string (struct regcache *regcache, int n, char *buf)
413{
414 bin2hex (register_data (regcache, n, 1), buf,
415 register_size (regcache->tdesc, n));
416}
417
418void
419collect_register_by_name (struct regcache *regcache,
420 const char *name, void *buf)
421{
422 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
423}
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}
450
451#endif
This page took 0.023307 seconds and 4 git commands to generate.