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