1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2014 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"
24 #ifndef IN_PROCESS_AGENT
27 get_thread_regcache (struct thread_info *thread, int fetch)
29 struct regcache *regcache;
31 regcache = (struct regcache *) inferior_regcache_data (thread);
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
42 struct process_info *proc = get_thread_process (thread);
44 gdb_assert (proc->tdesc != NULL);
46 regcache = new_register_cache (proc->tdesc);
47 set_inferior_regcache_data (thread, regcache);
50 if (fetch && regcache->registers_valid == 0)
52 struct thread_info *saved_thread = current_thread;
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;
66 /* See common/common-regcache.h. */
69 get_thread_regcache_for_ptid (ptid_t ptid)
71 return get_thread_regcache (find_thread_ptid (ptid), 1);
75 regcache_invalidate_thread (struct thread_info *thread)
77 struct regcache *regcache;
79 regcache = (struct regcache *) inferior_regcache_data (thread);
84 if (regcache->registers_valid)
86 struct thread_info *saved_thread = current_thread;
88 current_thread = thread;
89 store_inferior_registers (regcache, -1);
90 current_thread = saved_thread;
93 regcache->registers_valid = 0;
97 regcache_invalidate_one (struct inferior_list_entry *entry,
100 struct thread_info *thread = (struct thread_info *) entry;
101 int pid = *(int *) pid_p;
103 /* Only invalidate the regcaches of threads of this process. */
104 if (ptid_get_pid (entry->id) == pid)
105 regcache_invalidate_thread (thread);
111 regcache_invalidate (void)
113 /* Only update the threads of the current process. */
114 int pid = ptid_get_pid (current_thread->entry.id);
116 find_inferior (&all_threads, regcache_invalidate_one, &pid);
122 init_register_cache (struct regcache *regcache,
123 const struct target_desc *tdesc,
124 unsigned char *regbuf)
128 #ifndef IN_PROCESS_AGENT
129 /* Make sure to zero-initialize the register cache when it is
130 created, in case there are registers the target never
131 fetches. This way they'll read as zero instead of
133 regcache->tdesc = tdesc;
134 regcache->registers = xcalloc (1, tdesc->registers_size);
135 regcache->registers_owned = 1;
136 regcache->register_status = xcalloc (1, tdesc->num_registers);
137 gdb_assert (REG_UNAVAILABLE == 0);
139 gdb_assert_not_reached ("can't allocate memory from the heap");
144 regcache->tdesc = tdesc;
145 regcache->registers = regbuf;
146 regcache->registers_owned = 0;
147 #ifndef IN_PROCESS_AGENT
148 regcache->register_status = NULL;
152 regcache->registers_valid = 0;
157 #ifndef IN_PROCESS_AGENT
160 new_register_cache (const struct target_desc *tdesc)
162 struct regcache *regcache;
164 gdb_assert (tdesc->registers_size != 0);
166 regcache = xmalloc (sizeof (*regcache));
167 return init_register_cache (regcache, tdesc, NULL);
171 free_register_cache (struct regcache *regcache)
175 if (regcache->registers_owned)
176 free (regcache->registers);
177 free (regcache->register_status);
185 regcache_cpy (struct regcache *dst, struct regcache *src)
187 gdb_assert (src != NULL && dst != NULL);
188 gdb_assert (src->tdesc == dst->tdesc);
189 gdb_assert (src != dst);
191 memcpy (dst->registers, src->registers, src->tdesc->registers_size);
192 #ifndef IN_PROCESS_AGENT
193 if (dst->register_status != NULL && src->register_status != NULL)
194 memcpy (dst->register_status, src->register_status,
195 src->tdesc->num_registers);
197 dst->registers_valid = src->registers_valid;
201 #ifndef IN_PROCESS_AGENT
204 registers_to_string (struct regcache *regcache, char *buf)
206 unsigned char *registers = regcache->registers;
207 const struct target_desc *tdesc = regcache->tdesc;
210 for (i = 0; i < tdesc->num_registers; i++)
212 if (regcache->register_status[i] == REG_VALID)
214 bin2hex (registers, buf, register_size (tdesc, i));
215 buf += register_size (tdesc, i) * 2;
219 memset (buf, 'x', register_size (tdesc, i) * 2);
220 buf += register_size (tdesc, i) * 2;
222 registers += register_size (tdesc, i);
228 registers_from_string (struct regcache *regcache, char *buf)
230 int len = strlen (buf);
231 unsigned char *registers = regcache->registers;
232 const struct target_desc *tdesc = regcache->tdesc;
234 if (len != tdesc->registers_size * 2)
236 warning ("Wrong sized register packet (expected %d bytes, got %d)",
237 2 * tdesc->registers_size, len);
238 if (len > tdesc->registers_size * 2)
239 len = tdesc->registers_size * 2;
241 hex2bin (buf, registers, len / 2);
245 find_register_by_name (const struct target_desc *tdesc, const char *name)
249 for (i = 0; i < tdesc->num_registers; i++)
250 if (strcmp (name, tdesc->reg_defs[i].name) == 0)
251 return &tdesc->reg_defs[i];
252 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
257 find_regno (const struct target_desc *tdesc, const char *name)
261 for (i = 0; i < tdesc->num_registers; i++)
262 if (strcmp (name, tdesc->reg_defs[i].name) == 0)
264 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
269 find_register_by_number (const struct target_desc *tdesc, int n)
271 return &tdesc->reg_defs[n];
276 #ifndef IN_PROCESS_AGENT
278 free_register_cache_thread (struct thread_info *thread)
280 struct regcache *regcache
281 = (struct regcache *) inferior_regcache_data (thread);
283 if (regcache != NULL)
285 regcache_invalidate_thread (thread);
286 free_register_cache (regcache);
287 set_inferior_regcache_data (thread, NULL);
292 free_register_cache_thread_one (struct inferior_list_entry *entry)
294 struct thread_info *thread = (struct thread_info *) entry;
296 free_register_cache_thread (thread);
300 regcache_release (void)
302 /* Flush and release all pre-existing register caches. */
303 for_each_inferior (&all_threads, free_register_cache_thread_one);
308 register_cache_size (const struct target_desc *tdesc)
310 return tdesc->registers_size;
314 register_size (const struct target_desc *tdesc, int n)
316 return tdesc->reg_defs[n].size / 8;
319 static unsigned char *
320 register_data (struct regcache *regcache, int n, int fetch)
322 return regcache->registers + regcache->tdesc->reg_defs[n].offset / 8;
325 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
326 If BUF is NULL, the register's value is recorded as
330 supply_register (struct regcache *regcache, int n, const void *buf)
334 memcpy (register_data (regcache, n, 0), buf,
335 register_size (regcache->tdesc, n));
336 #ifndef IN_PROCESS_AGENT
337 if (regcache->register_status != NULL)
338 regcache->register_status[n] = REG_VALID;
343 memset (register_data (regcache, n, 0), 0,
344 register_size (regcache->tdesc, n));
345 #ifndef IN_PROCESS_AGENT
346 if (regcache->register_status != NULL)
347 regcache->register_status[n] = REG_UNAVAILABLE;
352 /* Supply register N with value zero to REGCACHE. */
355 supply_register_zeroed (struct regcache *regcache, int n)
357 memset (register_data (regcache, n, 0), 0,
358 register_size (regcache->tdesc, n));
359 #ifndef IN_PROCESS_AGENT
360 if (regcache->register_status != NULL)
361 regcache->register_status[n] = REG_VALID;
365 /* Supply the whole register set whose contents are stored in BUF, to
366 REGCACHE. If BUF is NULL, all the registers' values are recorded
370 supply_regblock (struct regcache *regcache, const void *buf)
374 const struct target_desc *tdesc = regcache->tdesc;
376 memcpy (regcache->registers, buf, tdesc->registers_size);
377 #ifndef IN_PROCESS_AGENT
381 for (i = 0; i < tdesc->num_registers; i++)
382 regcache->register_status[i] = REG_VALID;
388 const struct target_desc *tdesc = regcache->tdesc;
390 memset (regcache->registers, 0, tdesc->registers_size);
391 #ifndef IN_PROCESS_AGENT
395 for (i = 0; i < tdesc->num_registers; i++)
396 regcache->register_status[i] = REG_UNAVAILABLE;
402 #ifndef IN_PROCESS_AGENT
405 supply_register_by_name (struct regcache *regcache,
406 const char *name, const void *buf)
408 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
414 collect_register (struct regcache *regcache, int n, void *buf)
416 memcpy (buf, register_data (regcache, n, 1),
417 register_size (regcache->tdesc, n));
420 #ifndef IN_PROCESS_AGENT
423 collect_register_as_string (struct regcache *regcache, int n, char *buf)
425 bin2hex (register_data (regcache, n, 1), buf,
426 register_size (regcache->tdesc, n));
430 collect_register_by_name (struct regcache *regcache,
431 const char *name, void *buf)
433 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
436 /* Special handling for register PC. */
439 regcache_read_pc (struct regcache *regcache)
443 if (the_target->read_pc)
444 pc_val = the_target->read_pc (regcache);
446 internal_error (__FILE__, __LINE__,
447 "regcache_read_pc: Unable to find PC");
453 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
455 if (the_target->write_pc)
456 the_target->write_pc (regcache, pc);
458 internal_error (__FILE__, __LINE__,
459 "regcache_write_pc: Unable to update PC");