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"
28 #ifndef IN_PROCESS_AGENT
31 get_thread_regcache (struct thread_info
*thread
, int fetch
)
33 struct regcache
*regcache
;
35 regcache
= (struct regcache
*) inferior_regcache_data (thread
);
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
46 struct process_info
*proc
= get_thread_process (thread
);
48 if (proc
->tdesc
== NULL
)
49 fatal ("no target description");
51 regcache
= new_register_cache (proc
->tdesc
);
52 set_inferior_regcache_data (thread
, regcache
);
55 if (fetch
&& regcache
->registers_valid
== 0)
57 struct thread_info
*saved_inferior
= current_inferior
;
59 current_inferior
= thread
;
60 fetch_inferior_registers (regcache
, -1);
61 current_inferior
= saved_inferior
;
62 regcache
->registers_valid
= 1;
69 regcache_invalidate_thread (struct thread_info
*thread
)
71 struct regcache
*regcache
;
73 regcache
= (struct regcache
*) inferior_regcache_data (thread
);
78 if (regcache
->registers_valid
)
80 struct thread_info
*saved_inferior
= current_inferior
;
82 current_inferior
= thread
;
83 store_inferior_registers (regcache
, -1);
84 current_inferior
= saved_inferior
;
87 regcache
->registers_valid
= 0;
91 regcache_invalidate_one (struct inferior_list_entry
*entry
,
94 struct thread_info
*thread
= (struct thread_info
*) entry
;
95 int pid
= *(int *) pid_p
;
97 /* Only invalidate the regcaches of threads of this process. */
98 if (ptid_get_pid (entry
->id
) == pid
)
99 regcache_invalidate_thread (thread
);
105 regcache_invalidate (void)
107 /* Only update the threads of the current process. */
108 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
110 find_inferior (&all_threads
, regcache_invalidate_one
, &pid
);
116 init_register_cache (struct regcache
*regcache
,
117 const struct target_desc
*tdesc
,
118 unsigned char *regbuf
)
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
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);
133 fatal ("init_register_cache: can't allocate memory from the heap");
138 regcache
->tdesc
= tdesc
;
139 regcache
->registers
= regbuf
;
140 regcache
->registers_owned
= 0;
141 #ifndef IN_PROCESS_AGENT
142 regcache
->register_status
= NULL
;
146 regcache
->registers_valid
= 0;
151 #ifndef IN_PROCESS_AGENT
154 new_register_cache (const struct target_desc
*tdesc
)
156 struct regcache
*regcache
;
158 gdb_assert (tdesc
->registers_size
!= 0);
160 regcache
= xmalloc (sizeof (*regcache
));
161 return init_register_cache (regcache
, tdesc
, NULL
);
165 free_register_cache (struct regcache
*regcache
)
169 if (regcache
->registers_owned
)
170 free (regcache
->registers
);
171 free (regcache
->register_status
);
179 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
181 gdb_assert (src
!= NULL
&& dst
!= NULL
);
182 gdb_assert (src
->tdesc
== dst
->tdesc
);
183 gdb_assert (src
!= dst
);
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
);
191 dst
->registers_valid
= src
->registers_valid
;
195 #ifndef IN_PROCESS_AGENT
198 registers_to_string (struct regcache
*regcache
, char *buf
)
200 unsigned char *registers
= regcache
->registers
;
201 const struct target_desc
*tdesc
= regcache
->tdesc
;
204 for (i
= 0; i
< tdesc
->num_registers
; i
++)
206 if (regcache
->register_status
[i
] == REG_VALID
)
208 bin2hex (registers
, buf
, register_size (tdesc
, i
));
209 buf
+= register_size (tdesc
, i
) * 2;
213 memset (buf
, 'x', register_size (tdesc
, i
) * 2);
214 buf
+= register_size (tdesc
, i
) * 2;
216 registers
+= register_size (tdesc
, i
);
222 registers_from_string (struct regcache
*regcache
, char *buf
)
224 int len
= strlen (buf
);
225 unsigned char *registers
= regcache
->registers
;
226 const struct target_desc
*tdesc
= regcache
->tdesc
;
228 if (len
!= tdesc
->registers_size
* 2)
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;
235 hex2bin (buf
, registers
, len
/ 2);
239 find_register_by_name (const struct target_desc
*tdesc
, const char *name
)
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
);
251 find_regno (const struct target_desc
*tdesc
, const char *name
)
255 for (i
= 0; i
< tdesc
->num_registers
; i
++)
256 if (strcmp (name
, tdesc
->reg_defs
[i
].name
) == 0)
258 fatal ("Unknown register %s requested", name
);
263 find_register_by_number (const struct target_desc
*tdesc
, int n
)
265 return &tdesc
->reg_defs
[n
];
270 #ifndef IN_PROCESS_AGENT
272 free_register_cache_thread (struct thread_info
*thread
)
274 struct regcache
*regcache
275 = (struct regcache
*) inferior_regcache_data (thread
);
277 if (regcache
!= NULL
)
279 regcache_invalidate_thread (thread
);
280 free_register_cache (regcache
);
281 set_inferior_regcache_data (thread
, NULL
);
286 free_register_cache_thread_one (struct inferior_list_entry
*entry
)
288 struct thread_info
*thread
= (struct thread_info
*) entry
;
290 free_register_cache_thread (thread
);
294 regcache_release (void)
296 /* Flush and release all pre-existing register caches. */
297 for_each_inferior (&all_threads
, free_register_cache_thread_one
);
302 register_cache_size (const struct target_desc
*tdesc
)
304 return tdesc
->registers_size
;
308 register_size (const struct target_desc
*tdesc
, int n
)
310 return tdesc
->reg_defs
[n
].size
/ 8;
313 static unsigned char *
314 register_data (struct regcache
*regcache
, int n
, int fetch
)
316 return regcache
->registers
+ regcache
->tdesc
->reg_defs
[n
].offset
/ 8;
319 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
320 If BUF is NULL, the register's value is recorded as
324 supply_register (struct regcache
*regcache
, int n
, const void *buf
)
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
;
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
;
346 /* Supply register N with value zero to REGCACHE. */
349 supply_register_zeroed (struct regcache
*regcache
, int n
)
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
;
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
364 supply_regblock (struct regcache
*regcache
, const void *buf
)
368 const struct target_desc
*tdesc
= regcache
->tdesc
;
370 memcpy (regcache
->registers
, buf
, tdesc
->registers_size
);
371 #ifndef IN_PROCESS_AGENT
375 for (i
= 0; i
< tdesc
->num_registers
; i
++)
376 regcache
->register_status
[i
] = REG_VALID
;
382 const struct target_desc
*tdesc
= regcache
->tdesc
;
384 memset (regcache
->registers
, 0, tdesc
->registers_size
);
385 #ifndef IN_PROCESS_AGENT
389 for (i
= 0; i
< tdesc
->num_registers
; i
++)
390 regcache
->register_status
[i
] = REG_UNAVAILABLE
;
396 #ifndef IN_PROCESS_AGENT
399 supply_register_by_name (struct regcache
*regcache
,
400 const char *name
, const void *buf
)
402 supply_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
408 collect_register (struct regcache
*regcache
, int n
, void *buf
)
410 memcpy (buf
, register_data (regcache
, n
, 1),
411 register_size (regcache
->tdesc
, n
));
414 #ifndef IN_PROCESS_AGENT
417 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
419 bin2hex (register_data (regcache
, n
, 1), buf
,
420 register_size (regcache
->tdesc
, n
));
424 collect_register_by_name (struct regcache
*regcache
,
425 const char *name
, void *buf
)
427 collect_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
430 /* Special handling for register PC. */
433 regcache_read_pc (struct regcache
*regcache
)
437 if (the_target
->read_pc
)
438 pc_val
= the_target
->read_pc (regcache
);
440 internal_error (__FILE__
, __LINE__
,
441 "regcache_read_pc: Unable to find PC");
447 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
449 if (the_target
->write_pc
)
450 the_target
->write_pc (regcache
, pc
);
452 internal_error (__FILE__
, __LINE__
,
453 "regcache_write_pc: Unable to update PC");