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;
97 regcache_invalidate_one (thread_info
*thread
, void *pid_p
)
99 int pid
= *(int *) pid_p
;
101 /* Only invalidate the regcaches of threads of this process. */
102 if (thread
->id
.pid () == pid
)
103 regcache_invalidate_thread (thread
);
108 /* See regcache.h. */
111 regcache_invalidate_pid (int pid
)
113 find_inferior (&all_threads
, regcache_invalidate_one
, &pid
);
116 /* See regcache.h. */
119 regcache_invalidate (void)
121 /* Only update the threads of the current process. */
122 int pid
= current_thread
->id
.pid ();
124 regcache_invalidate_pid (pid
);
130 init_register_cache (struct regcache
*regcache
,
131 const struct target_desc
*tdesc
,
132 unsigned char *regbuf
)
136 #ifndef IN_PROCESS_AGENT
137 /* Make sure to zero-initialize the register cache when it is
138 created, in case there are registers the target never
139 fetches. This way they'll read as zero instead of
141 regcache
->tdesc
= tdesc
;
143 = (unsigned char *) xcalloc (1, tdesc
->registers_size
);
144 regcache
->registers_owned
= 1;
145 regcache
->register_status
146 = (unsigned char *) xmalloc (tdesc
->reg_defs
.size ());
147 memset ((void *) regcache
->register_status
, REG_UNAVAILABLE
,
148 tdesc
->reg_defs
.size ());
150 gdb_assert_not_reached ("can't allocate memory from the heap");
155 regcache
->tdesc
= tdesc
;
156 regcache
->registers
= regbuf
;
157 regcache
->registers_owned
= 0;
158 #ifndef IN_PROCESS_AGENT
159 regcache
->register_status
= NULL
;
163 regcache
->registers_valid
= 0;
168 #ifndef IN_PROCESS_AGENT
171 new_register_cache (const struct target_desc
*tdesc
)
173 struct regcache
*regcache
= XCNEW (struct regcache
);
175 gdb_assert (tdesc
->registers_size
!= 0);
177 return init_register_cache (regcache
, tdesc
, NULL
);
181 free_register_cache (struct regcache
*regcache
)
185 if (regcache
->registers_owned
)
186 free (regcache
->registers
);
187 free (regcache
->register_status
);
195 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
197 gdb_assert (src
!= NULL
&& dst
!= NULL
);
198 gdb_assert (src
->tdesc
== dst
->tdesc
);
199 gdb_assert (src
!= dst
);
201 memcpy (dst
->registers
, src
->registers
, src
->tdesc
->registers_size
);
202 #ifndef IN_PROCESS_AGENT
203 if (dst
->register_status
!= NULL
&& src
->register_status
!= NULL
)
204 memcpy (dst
->register_status
, src
->register_status
,
205 src
->tdesc
->reg_defs
.size ());
207 dst
->registers_valid
= src
->registers_valid
;
211 #ifndef IN_PROCESS_AGENT
214 registers_to_string (struct regcache
*regcache
, char *buf
)
216 unsigned char *registers
= regcache
->registers
;
217 const struct target_desc
*tdesc
= regcache
->tdesc
;
219 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
221 struct reg
*reg
= tdesc
->reg_defs
[i
];
223 if (regcache
->register_status
[i
] == REG_VALID
)
225 bin2hex (registers
, buf
, register_size (tdesc
, i
));
226 buf
+= register_size (tdesc
, i
) * 2;
230 memset (buf
, 'x', register_size (tdesc
, i
) * 2);
231 buf
+= register_size (tdesc
, i
) * 2;
233 registers
+= register_size (tdesc
, i
);
239 registers_from_string (struct regcache
*regcache
, char *buf
)
241 int len
= strlen (buf
);
242 unsigned char *registers
= regcache
->registers
;
243 const struct target_desc
*tdesc
= regcache
->tdesc
;
245 if (len
!= tdesc
->registers_size
* 2)
247 warning ("Wrong sized register packet (expected %d bytes, got %d)",
248 2 * tdesc
->registers_size
, len
);
249 if (len
> tdesc
->registers_size
* 2)
250 len
= tdesc
->registers_size
* 2;
252 hex2bin (buf
, registers
, len
/ 2);
256 find_regno (const struct target_desc
*tdesc
, const char *name
)
258 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
260 struct reg
*reg
= tdesc
->reg_defs
[i
];
262 if (strcmp (name
, reg
->name
) == 0)
265 internal_error (__FILE__
, __LINE__
, "Unknown register %s requested",
272 find_register_by_number (const struct target_desc
*tdesc
, int n
)
274 return tdesc
->reg_defs
[n
];
277 #ifndef IN_PROCESS_AGENT
279 free_register_cache_thread (struct thread_info
*thread
)
281 struct regcache
*regcache
= thread_regcache_data (thread
);
283 if (regcache
!= NULL
)
285 regcache_invalidate_thread (thread
);
286 free_register_cache (regcache
);
287 set_thread_regcache_data (thread
, NULL
);
292 regcache_release (void)
294 /* Flush and release all pre-existing register caches. */
295 for_each_inferior (&all_threads
, free_register_cache_thread
);
300 register_cache_size (const struct target_desc
*tdesc
)
302 return tdesc
->registers_size
;
306 register_size (const struct target_desc
*tdesc
, int n
)
308 return find_register_by_number (tdesc
, n
)->size
/ 8;
311 /* See common/common-regcache.h. */
314 regcache_register_size (const struct regcache
*regcache
, int n
)
316 return register_size (regcache
->tdesc
, n
);
319 static unsigned char *
320 register_data (struct regcache
*regcache
, int n
, int fetch
)
322 return (regcache
->registers
323 + find_register_by_number (regcache
->tdesc
, n
)->offset
/ 8);
326 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
327 If BUF is NULL, the register's value is recorded as
331 supply_register (struct regcache
*regcache
, int n
, const void *buf
)
335 memcpy (register_data (regcache
, n
, 0), buf
,
336 register_size (regcache
->tdesc
, n
));
337 #ifndef IN_PROCESS_AGENT
338 if (regcache
->register_status
!= NULL
)
339 regcache
->register_status
[n
] = REG_VALID
;
344 memset (register_data (regcache
, n
, 0), 0,
345 register_size (regcache
->tdesc
, n
));
346 #ifndef IN_PROCESS_AGENT
347 if (regcache
->register_status
!= NULL
)
348 regcache
->register_status
[n
] = REG_UNAVAILABLE
;
353 /* Supply register N with value zero to REGCACHE. */
356 supply_register_zeroed (struct regcache
*regcache
, int n
)
358 memset (register_data (regcache
, n
, 0), 0,
359 register_size (regcache
->tdesc
, n
));
360 #ifndef IN_PROCESS_AGENT
361 if (regcache
->register_status
!= NULL
)
362 regcache
->register_status
[n
] = REG_VALID
;
366 /* Supply the whole register set whose contents are stored in BUF, to
367 REGCACHE. If BUF is NULL, all the registers' values are recorded
371 supply_regblock (struct regcache
*regcache
, const void *buf
)
375 const struct target_desc
*tdesc
= regcache
->tdesc
;
377 memcpy (regcache
->registers
, buf
, tdesc
->registers_size
);
378 #ifndef IN_PROCESS_AGENT
382 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
383 regcache
->register_status
[i
] = REG_VALID
;
389 const struct target_desc
*tdesc
= regcache
->tdesc
;
391 memset (regcache
->registers
, 0, tdesc
->registers_size
);
392 #ifndef IN_PROCESS_AGENT
396 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
397 regcache
->register_status
[i
] = REG_UNAVAILABLE
;
403 #ifndef IN_PROCESS_AGENT
406 supply_register_by_name (struct regcache
*regcache
,
407 const char *name
, const void *buf
)
409 supply_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
415 collect_register (struct regcache
*regcache
, int n
, void *buf
)
417 memcpy (buf
, register_data (regcache
, n
, 1),
418 register_size (regcache
->tdesc
, n
));
422 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
427 gdb_assert (regcache
!= NULL
);
428 gdb_assert (regnum
>= 0
429 && regnum
< regcache
->tdesc
->reg_defs
.size ());
431 size
= register_size (regcache
->tdesc
, regnum
);
433 if (size
> (int) sizeof (ULONGEST
))
434 error (_("That operation is not available on integers of more than"
436 (int) sizeof (ULONGEST
));
439 collect_register (regcache
, regnum
, val
);
444 #ifndef IN_PROCESS_AGENT
447 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
449 bin2hex (register_data (regcache
, n
, 1), buf
,
450 register_size (regcache
->tdesc
, n
));
454 collect_register_by_name (struct regcache
*regcache
,
455 const char *name
, void *buf
)
457 collect_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
460 /* Special handling for register PC. */
463 regcache_read_pc (struct regcache
*regcache
)
467 if (the_target
->read_pc
)
468 pc_val
= the_target
->read_pc (regcache
);
470 internal_error (__FILE__
, __LINE__
,
471 "regcache_read_pc: Unable to find PC");
477 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
479 if (the_target
->write_pc
)
480 the_target
->write_pc (regcache
, pc
);
482 internal_error (__FILE__
, __LINE__
,
483 "regcache_write_pc: Unable to update PC");