1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2017 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
= thread_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_thread_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
->reg_defs
.size ());
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
= thread_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;
99 regcache_invalidate_pid (int pid
)
101 /* Only invalidate the regcaches of threads of this process. */
102 for_each_thread (pid
, regcache_invalidate_thread
);
105 /* See regcache.h. */
108 regcache_invalidate (void)
110 /* Only update the threads of the current process. */
111 int pid
= current_thread
->id
.pid ();
113 regcache_invalidate_pid (pid
);
119 init_register_cache (struct regcache
*regcache
,
120 const struct target_desc
*tdesc
,
121 unsigned char *regbuf
)
125 #ifndef IN_PROCESS_AGENT
126 /* Make sure to zero-initialize the register cache when it is
127 created, in case there are registers the target never
128 fetches. This way they'll read as zero instead of
130 regcache
->tdesc
= tdesc
;
132 = (unsigned char *) xcalloc (1, tdesc
->registers_size
);
133 regcache
->registers_owned
= 1;
134 regcache
->register_status
135 = (unsigned char *) xmalloc (tdesc
->reg_defs
.size ());
136 memset ((void *) regcache
->register_status
, REG_UNAVAILABLE
,
137 tdesc
->reg_defs
.size ());
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
= XCNEW (struct regcache
);
164 gdb_assert (tdesc
->registers_size
!= 0);
166 return init_register_cache (regcache
, tdesc
, NULL
);
170 free_register_cache (struct regcache
*regcache
)
174 if (regcache
->registers_owned
)
175 free (regcache
->registers
);
176 free (regcache
->register_status
);
184 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
186 gdb_assert (src
!= NULL
&& dst
!= NULL
);
187 gdb_assert (src
->tdesc
== dst
->tdesc
);
188 gdb_assert (src
!= dst
);
190 memcpy (dst
->registers
, src
->registers
, src
->tdesc
->registers_size
);
191 #ifndef IN_PROCESS_AGENT
192 if (dst
->register_status
!= NULL
&& src
->register_status
!= NULL
)
193 memcpy (dst
->register_status
, src
->register_status
,
194 src
->tdesc
->reg_defs
.size ());
196 dst
->registers_valid
= src
->registers_valid
;
200 #ifndef IN_PROCESS_AGENT
203 registers_to_string (struct regcache
*regcache
, char *buf
)
205 unsigned char *registers
= regcache
->registers
;
206 const struct target_desc
*tdesc
= regcache
->tdesc
;
208 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
210 struct reg
*reg
= tdesc
->reg_defs
[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_regno (const struct target_desc
*tdesc
, const char *name
)
247 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
249 struct reg
*reg
= tdesc
->reg_defs
[i
];
251 if (strcmp (name
, reg
->name
) == 0)
254 internal_error (__FILE__
, __LINE__
, "Unknown register %s requested",
261 find_register_by_number (const struct target_desc
*tdesc
, int n
)
263 return tdesc
->reg_defs
[n
];
266 #ifndef IN_PROCESS_AGENT
268 free_register_cache_thread (struct thread_info
*thread
)
270 struct regcache
*regcache
= thread_regcache_data (thread
);
272 if (regcache
!= NULL
)
274 regcache_invalidate_thread (thread
);
275 free_register_cache (regcache
);
276 set_thread_regcache_data (thread
, NULL
);
281 regcache_release (void)
283 /* Flush and release all pre-existing register caches. */
284 for_each_inferior (&all_threads
, free_register_cache_thread
);
289 register_cache_size (const struct target_desc
*tdesc
)
291 return tdesc
->registers_size
;
295 register_size (const struct target_desc
*tdesc
, int n
)
297 return find_register_by_number (tdesc
, n
)->size
/ 8;
300 /* See common/common-regcache.h. */
303 regcache_register_size (const struct regcache
*regcache
, int n
)
305 return register_size (regcache
->tdesc
, n
);
308 static unsigned char *
309 register_data (struct regcache
*regcache
, int n
, int fetch
)
311 return (regcache
->registers
312 + find_register_by_number (regcache
->tdesc
, n
)->offset
/ 8);
315 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
316 If BUF is NULL, the register's value is recorded as
320 supply_register (struct regcache
*regcache
, int n
, const void *buf
)
324 memcpy (register_data (regcache
, n
, 0), buf
,
325 register_size (regcache
->tdesc
, n
));
326 #ifndef IN_PROCESS_AGENT
327 if (regcache
->register_status
!= NULL
)
328 regcache
->register_status
[n
] = REG_VALID
;
333 memset (register_data (regcache
, n
, 0), 0,
334 register_size (regcache
->tdesc
, n
));
335 #ifndef IN_PROCESS_AGENT
336 if (regcache
->register_status
!= NULL
)
337 regcache
->register_status
[n
] = REG_UNAVAILABLE
;
342 /* Supply register N with value zero to REGCACHE. */
345 supply_register_zeroed (struct regcache
*regcache
, int n
)
347 memset (register_data (regcache
, n
, 0), 0,
348 register_size (regcache
->tdesc
, n
));
349 #ifndef IN_PROCESS_AGENT
350 if (regcache
->register_status
!= NULL
)
351 regcache
->register_status
[n
] = REG_VALID
;
355 /* Supply the whole register set whose contents are stored in BUF, to
356 REGCACHE. If BUF is NULL, all the registers' values are recorded
360 supply_regblock (struct regcache
*regcache
, const void *buf
)
364 const struct target_desc
*tdesc
= regcache
->tdesc
;
366 memcpy (regcache
->registers
, buf
, tdesc
->registers_size
);
367 #ifndef IN_PROCESS_AGENT
371 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
372 regcache
->register_status
[i
] = REG_VALID
;
378 const struct target_desc
*tdesc
= regcache
->tdesc
;
380 memset (regcache
->registers
, 0, tdesc
->registers_size
);
381 #ifndef IN_PROCESS_AGENT
385 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
386 regcache
->register_status
[i
] = REG_UNAVAILABLE
;
392 #ifndef IN_PROCESS_AGENT
395 supply_register_by_name (struct regcache
*regcache
,
396 const char *name
, const void *buf
)
398 supply_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
404 collect_register (struct regcache
*regcache
, int n
, void *buf
)
406 memcpy (buf
, register_data (regcache
, n
, 1),
407 register_size (regcache
->tdesc
, n
));
411 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
416 gdb_assert (regcache
!= NULL
);
417 gdb_assert (regnum
>= 0
418 && regnum
< regcache
->tdesc
->reg_defs
.size ());
420 size
= register_size (regcache
->tdesc
, regnum
);
422 if (size
> (int) sizeof (ULONGEST
))
423 error (_("That operation is not available on integers of more than"
425 (int) sizeof (ULONGEST
));
428 collect_register (regcache
, regnum
, val
);
433 #ifndef IN_PROCESS_AGENT
436 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
438 bin2hex (register_data (regcache
, n
, 1), buf
,
439 register_size (regcache
->tdesc
, n
));
443 collect_register_by_name (struct regcache
*regcache
,
444 const char *name
, void *buf
)
446 collect_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
449 /* Special handling for register PC. */
452 regcache_read_pc (struct regcache
*regcache
)
456 if (the_target
->read_pc
)
457 pc_val
= the_target
->read_pc (regcache
);
459 internal_error (__FILE__
, __LINE__
,
460 "regcache_read_pc: Unable to find PC");
466 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
468 if (the_target
->write_pc
)
469 the_target
->write_pc (regcache
, pc
);
471 internal_error (__FILE__
, __LINE__
,
472 "regcache_write_pc: Unable to update PC");