1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
21 #include "gdbthread.h"
27 #ifndef IN_PROCESS_AGENT
30 get_thread_regcache (struct thread_info *thread, int fetch)
32 struct regcache *regcache;
34 regcache = (struct regcache *) inferior_regcache_data (thread);
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
45 struct process_info *proc = get_thread_process (thread);
47 if (proc->tdesc == NULL)
48 fatal ("no target description");
50 regcache = new_register_cache (proc->tdesc);
51 set_inferior_regcache_data (thread, regcache);
54 if (fetch && regcache->registers_valid == 0)
56 struct thread_info *saved_inferior = current_inferior;
58 current_inferior = thread;
59 fetch_inferior_registers (regcache, -1);
60 current_inferior = saved_inferior;
61 regcache->registers_valid = 1;
68 regcache_invalidate_thread (struct thread_info *thread)
70 struct regcache *regcache;
72 regcache = (struct regcache *) inferior_regcache_data (thread);
77 if (regcache->registers_valid)
79 struct thread_info *saved_inferior = current_inferior;
81 current_inferior = thread;
82 store_inferior_registers (regcache, -1);
83 current_inferior = saved_inferior;
86 regcache->registers_valid = 0;
90 regcache_invalidate_one (struct inferior_list_entry *entry,
93 struct thread_info *thread = (struct thread_info *) entry;
94 int pid = *(int *) pid_p;
96 /* Only invalidate the regcaches of threads of this process. */
97 if (ptid_get_pid (entry->id) == pid)
98 regcache_invalidate_thread (thread);
104 regcache_invalidate (void)
106 /* Only update the threads of the current process. */
107 int pid = ptid_get_pid (current_inferior->entry.id);
109 find_inferior (&all_threads, regcache_invalidate_one, &pid);
115 init_register_cache (struct regcache *regcache,
116 const struct target_desc *tdesc,
117 unsigned char *regbuf)
119 #ifndef IN_PROCESS_AGENT
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
126 regcache->tdesc = tdesc;
127 regcache->registers = xcalloc (1, tdesc->registers_size);
128 regcache->registers_owned = 1;
129 regcache->register_status = xcalloc (1, tdesc->num_registers);
130 gdb_assert (REG_UNAVAILABLE == 0);
135 fatal ("init_register_cache: can't allocate memory from the heap");
139 regcache->tdesc = tdesc;
140 regcache->registers = regbuf;
141 regcache->registers_owned = 0;
142 #ifndef IN_PROCESS_AGENT
143 regcache->register_status = NULL;
147 regcache->registers_valid = 0;
152 #ifndef IN_PROCESS_AGENT
155 new_register_cache (const struct target_desc *tdesc)
157 struct regcache *regcache;
159 gdb_assert (tdesc->registers_size != 0);
161 regcache = xmalloc (sizeof (*regcache));
162 return init_register_cache (regcache, tdesc, NULL);
166 free_register_cache (struct regcache *regcache)
170 if (regcache->registers_owned)
171 free (regcache->registers);
172 free (regcache->register_status);
180 regcache_cpy (struct regcache *dst, struct regcache *src)
182 gdb_assert (src != NULL && dst != NULL);
183 gdb_assert (src->tdesc == dst->tdesc);
184 gdb_assert (src != dst);
186 memcpy (dst->registers, src->registers, src->tdesc->registers_size);
187 #ifndef IN_PROCESS_AGENT
188 if (dst->register_status != NULL && src->register_status != NULL)
189 memcpy (dst->register_status, src->register_status,
190 src->tdesc->num_registers);
192 dst->registers_valid = src->registers_valid;
196 #ifndef IN_PROCESS_AGENT
199 registers_to_string (struct regcache *regcache, char *buf)
201 unsigned char *registers = regcache->registers;
202 const struct target_desc *tdesc = regcache->tdesc;
205 for (i = 0; i < tdesc->num_registers; i++)
207 if (regcache->register_status[i] == REG_VALID)
209 convert_int_to_ascii (registers, buf,
210 register_size (tdesc, i));
211 buf += register_size (tdesc, i) * 2;
215 memset (buf, 'x', register_size (tdesc, i) * 2);
216 buf += register_size (tdesc, i) * 2;
218 registers += register_size (tdesc, i);
224 registers_from_string (struct regcache *regcache, char *buf)
226 int len = strlen (buf);
227 unsigned char *registers = regcache->registers;
228 const struct target_desc *tdesc = regcache->tdesc;
230 if (len != tdesc->registers_size * 2)
232 warning ("Wrong sized register packet (expected %d bytes, got %d)",
233 2 * tdesc->registers_size, len);
234 if (len > tdesc->registers_size * 2)
235 len = tdesc->registers_size * 2;
237 convert_ascii_to_int (buf, registers, len / 2);
241 find_register_by_name (const struct target_desc *tdesc, const char *name)
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];
248 fatal ("Unknown register %s requested", name);
253 find_regno (const struct target_desc *tdesc, const char *name)
257 for (i = 0; i < tdesc->num_registers; i++)
258 if (strcmp (name, tdesc->reg_defs[i].name) == 0)
260 fatal ("Unknown register %s requested", name);
265 find_register_by_number (const struct target_desc *tdesc, int n)
267 return &tdesc->reg_defs[n];
272 #ifndef IN_PROCESS_AGENT
274 free_register_cache_thread (struct thread_info *thread)
276 struct regcache *regcache
277 = (struct regcache *) inferior_regcache_data (thread);
279 if (regcache != NULL)
281 regcache_invalidate_thread (thread);
282 free_register_cache (regcache);
283 set_inferior_regcache_data (thread, NULL);
288 free_register_cache_thread_one (struct inferior_list_entry *entry)
290 struct thread_info *thread = (struct thread_info *) entry;
292 free_register_cache_thread (thread);
296 regcache_release (void)
298 /* Flush and release all pre-existing register caches. */
299 for_each_inferior (&all_threads, free_register_cache_thread_one);
304 register_cache_size (const struct target_desc *tdesc)
306 return tdesc->registers_size;
310 register_size (const struct target_desc *tdesc, int n)
312 return tdesc->reg_defs[n].size / 8;
315 static unsigned char *
316 register_data (struct regcache *regcache, int n, int fetch)
318 return regcache->registers + regcache->tdesc->reg_defs[n].offset / 8;
321 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
322 If BUF is NULL, the register's value is recorded as
326 supply_register (struct regcache *regcache, int n, const void *buf)
330 memcpy (register_data (regcache, n, 0), buf,
331 register_size (regcache->tdesc, n));
332 #ifndef IN_PROCESS_AGENT
333 if (regcache->register_status != NULL)
334 regcache->register_status[n] = REG_VALID;
339 memset (register_data (regcache, n, 0), 0,
340 register_size (regcache->tdesc, n));
341 #ifndef IN_PROCESS_AGENT
342 if (regcache->register_status != NULL)
343 regcache->register_status[n] = REG_UNAVAILABLE;
348 /* Supply register N with value zero to REGCACHE. */
351 supply_register_zeroed (struct regcache *regcache, int n)
353 memset (register_data (regcache, n, 0), 0,
354 register_size (regcache->tdesc, n));
355 #ifndef IN_PROCESS_AGENT
356 if (regcache->register_status != NULL)
357 regcache->register_status[n] = REG_VALID;
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
366 supply_regblock (struct regcache *regcache, const void *buf)
370 const struct target_desc *tdesc = regcache->tdesc;
372 memcpy (regcache->registers, buf, tdesc->registers_size);
373 #ifndef IN_PROCESS_AGENT
377 for (i = 0; i < tdesc->num_registers; i++)
378 regcache->register_status[i] = REG_VALID;
384 const struct target_desc *tdesc = regcache->tdesc;
386 memset (regcache->registers, 0, tdesc->registers_size);
387 #ifndef IN_PROCESS_AGENT
391 for (i = 0; i < tdesc->num_registers; i++)
392 regcache->register_status[i] = REG_UNAVAILABLE;
398 #ifndef IN_PROCESS_AGENT
401 supply_register_by_name (struct regcache *regcache,
402 const char *name, const void *buf)
404 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
410 collect_register (struct regcache *regcache, int n, void *buf)
412 memcpy (buf, register_data (regcache, n, 1),
413 register_size (regcache->tdesc, n));
416 #ifndef IN_PROCESS_AGENT
419 collect_register_as_string (struct regcache *regcache, int n, char *buf)
421 convert_int_to_ascii (register_data (regcache, n, 1), buf,
422 register_size (regcache->tdesc, n));
426 collect_register_by_name (struct regcache *regcache,
427 const char *name, void *buf)
429 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
432 /* Special handling for register PC. */
435 regcache_read_pc (struct regcache *regcache)
439 if (the_target->read_pc)
440 pc_val = the_target->read_pc (regcache);
442 internal_error (__FILE__, __LINE__,
443 "regcache_read_pc: Unable to find PC");
449 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
451 if (the_target->write_pc)
452 the_target->write_pc (regcache, pc);
454 internal_error (__FILE__, __LINE__,
455 "regcache_write_pc: Unable to update PC");