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_inferior
= current_inferior
;
54 current_inferior
= thread
;
55 fetch_inferior_registers (regcache
, -1);
56 current_inferior
= saved_inferior
;
57 regcache
->registers_valid
= 1;
64 regcache_invalidate_thread (struct thread_info
*thread
)
66 struct regcache
*regcache
;
68 regcache
= (struct regcache
*) inferior_regcache_data (thread
);
73 if (regcache
->registers_valid
)
75 struct thread_info
*saved_inferior
= current_inferior
;
77 current_inferior
= thread
;
78 store_inferior_registers (regcache
, -1);
79 current_inferior
= saved_inferior
;
82 regcache
->registers_valid
= 0;
86 regcache_invalidate_one (struct inferior_list_entry
*entry
,
89 struct thread_info
*thread
= (struct thread_info
*) entry
;
90 int pid
= *(int *) pid_p
;
92 /* Only invalidate the regcaches of threads of this process. */
93 if (ptid_get_pid (entry
->id
) == pid
)
94 regcache_invalidate_thread (thread
);
100 regcache_invalidate (void)
102 /* Only update the threads of the current process. */
103 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
105 find_inferior (&all_threads
, regcache_invalidate_one
, &pid
);
111 init_register_cache (struct regcache
*regcache
,
112 const struct target_desc
*tdesc
,
113 unsigned char *regbuf
)
117 #ifndef IN_PROCESS_AGENT
118 /* Make sure to zero-initialize the register cache when it is
119 created, in case there are registers the target never
120 fetches. This way they'll read as zero instead of
122 regcache
->tdesc
= tdesc
;
123 regcache
->registers
= xcalloc (1, tdesc
->registers_size
);
124 regcache
->registers_owned
= 1;
125 regcache
->register_status
= xcalloc (1, tdesc
->num_registers
);
126 gdb_assert (REG_UNAVAILABLE
== 0);
128 gdb_assert_not_reached ("can't allocate memory from the heap");
133 regcache
->tdesc
= tdesc
;
134 regcache
->registers
= regbuf
;
135 regcache
->registers_owned
= 0;
136 #ifndef IN_PROCESS_AGENT
137 regcache
->register_status
= NULL
;
141 regcache
->registers_valid
= 0;
146 #ifndef IN_PROCESS_AGENT
149 new_register_cache (const struct target_desc
*tdesc
)
151 struct regcache
*regcache
;
153 gdb_assert (tdesc
->registers_size
!= 0);
155 regcache
= xmalloc (sizeof (*regcache
));
156 return init_register_cache (regcache
, tdesc
, NULL
);
160 free_register_cache (struct regcache
*regcache
)
164 if (regcache
->registers_owned
)
165 free (regcache
->registers
);
166 free (regcache
->register_status
);
174 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
176 gdb_assert (src
!= NULL
&& dst
!= NULL
);
177 gdb_assert (src
->tdesc
== dst
->tdesc
);
178 gdb_assert (src
!= dst
);
180 memcpy (dst
->registers
, src
->registers
, src
->tdesc
->registers_size
);
181 #ifndef IN_PROCESS_AGENT
182 if (dst
->register_status
!= NULL
&& src
->register_status
!= NULL
)
183 memcpy (dst
->register_status
, src
->register_status
,
184 src
->tdesc
->num_registers
);
186 dst
->registers_valid
= src
->registers_valid
;
190 #ifndef IN_PROCESS_AGENT
193 registers_to_string (struct regcache
*regcache
, char *buf
)
195 unsigned char *registers
= regcache
->registers
;
196 const struct target_desc
*tdesc
= regcache
->tdesc
;
199 for (i
= 0; i
< tdesc
->num_registers
; i
++)
201 if (regcache
->register_status
[i
] == REG_VALID
)
203 bin2hex (registers
, buf
, register_size (tdesc
, i
));
204 buf
+= register_size (tdesc
, i
) * 2;
208 memset (buf
, 'x', register_size (tdesc
, i
) * 2);
209 buf
+= register_size (tdesc
, i
) * 2;
211 registers
+= register_size (tdesc
, i
);
217 registers_from_string (struct regcache
*regcache
, char *buf
)
219 int len
= strlen (buf
);
220 unsigned char *registers
= regcache
->registers
;
221 const struct target_desc
*tdesc
= regcache
->tdesc
;
223 if (len
!= tdesc
->registers_size
* 2)
225 warning ("Wrong sized register packet (expected %d bytes, got %d)",
226 2 * tdesc
->registers_size
, len
);
227 if (len
> tdesc
->registers_size
* 2)
228 len
= tdesc
->registers_size
* 2;
230 hex2bin (buf
, registers
, len
/ 2);
234 find_register_by_name (const struct target_desc
*tdesc
, const char *name
)
238 for (i
= 0; i
< tdesc
->num_registers
; i
++)
239 if (strcmp (name
, tdesc
->reg_defs
[i
].name
) == 0)
240 return &tdesc
->reg_defs
[i
];
241 internal_error (__FILE__
, __LINE__
, "Unknown register %s requested",
246 find_regno (const struct target_desc
*tdesc
, const char *name
)
250 for (i
= 0; i
< tdesc
->num_registers
; i
++)
251 if (strcmp (name
, tdesc
->reg_defs
[i
].name
) == 0)
253 internal_error (__FILE__
, __LINE__
, "Unknown register %s requested",
258 find_register_by_number (const struct target_desc
*tdesc
, int n
)
260 return &tdesc
->reg_defs
[n
];
265 #ifndef IN_PROCESS_AGENT
267 free_register_cache_thread (struct thread_info
*thread
)
269 struct regcache
*regcache
270 = (struct regcache
*) inferior_regcache_data (thread
);
272 if (regcache
!= NULL
)
274 regcache_invalidate_thread (thread
);
275 free_register_cache (regcache
);
276 set_inferior_regcache_data (thread
, NULL
);
281 free_register_cache_thread_one (struct inferior_list_entry
*entry
)
283 struct thread_info
*thread
= (struct thread_info
*) entry
;
285 free_register_cache_thread (thread
);
289 regcache_release (void)
291 /* Flush and release all pre-existing register caches. */
292 for_each_inferior (&all_threads
, free_register_cache_thread_one
);
297 register_cache_size (const struct target_desc
*tdesc
)
299 return tdesc
->registers_size
;
303 register_size (const struct target_desc
*tdesc
, int n
)
305 return tdesc
->reg_defs
[n
].size
/ 8;
308 static unsigned char *
309 register_data (struct regcache
*regcache
, int n
, int fetch
)
311 return regcache
->registers
+ regcache
->tdesc
->reg_defs
[n
].offset
/ 8;
314 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
315 If BUF is NULL, the register's value is recorded as
319 supply_register (struct regcache
*regcache
, int n
, const void *buf
)
323 memcpy (register_data (regcache
, n
, 0), buf
,
324 register_size (regcache
->tdesc
, n
));
325 #ifndef IN_PROCESS_AGENT
326 if (regcache
->register_status
!= NULL
)
327 regcache
->register_status
[n
] = REG_VALID
;
332 memset (register_data (regcache
, n
, 0), 0,
333 register_size (regcache
->tdesc
, n
));
334 #ifndef IN_PROCESS_AGENT
335 if (regcache
->register_status
!= NULL
)
336 regcache
->register_status
[n
] = REG_UNAVAILABLE
;
341 /* Supply register N with value zero to REGCACHE. */
344 supply_register_zeroed (struct regcache
*regcache
, int n
)
346 memset (register_data (regcache
, n
, 0), 0,
347 register_size (regcache
->tdesc
, n
));
348 #ifndef IN_PROCESS_AGENT
349 if (regcache
->register_status
!= NULL
)
350 regcache
->register_status
[n
] = REG_VALID
;
354 /* Supply the whole register set whose contents are stored in BUF, to
355 REGCACHE. If BUF is NULL, all the registers' values are recorded
359 supply_regblock (struct regcache
*regcache
, const void *buf
)
363 const struct target_desc
*tdesc
= regcache
->tdesc
;
365 memcpy (regcache
->registers
, buf
, tdesc
->registers_size
);
366 #ifndef IN_PROCESS_AGENT
370 for (i
= 0; i
< tdesc
->num_registers
; i
++)
371 regcache
->register_status
[i
] = REG_VALID
;
377 const struct target_desc
*tdesc
= regcache
->tdesc
;
379 memset (regcache
->registers
, 0, tdesc
->registers_size
);
380 #ifndef IN_PROCESS_AGENT
384 for (i
= 0; i
< tdesc
->num_registers
; i
++)
385 regcache
->register_status
[i
] = REG_UNAVAILABLE
;
391 #ifndef IN_PROCESS_AGENT
394 supply_register_by_name (struct regcache
*regcache
,
395 const char *name
, const void *buf
)
397 supply_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
403 collect_register (struct regcache
*regcache
, int n
, void *buf
)
405 memcpy (buf
, register_data (regcache
, n
, 1),
406 register_size (regcache
->tdesc
, n
));
409 #ifndef IN_PROCESS_AGENT
412 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
414 bin2hex (register_data (regcache
, n
, 1), buf
,
415 register_size (regcache
->tdesc
, n
));
419 collect_register_by_name (struct regcache
*regcache
,
420 const char *name
, void *buf
)
422 collect_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
425 /* Special handling for register PC. */
428 regcache_read_pc (struct regcache
*regcache
)
432 if (the_target
->read_pc
)
433 pc_val
= the_target
->read_pc (regcache
);
435 internal_error (__FILE__
, __LINE__
,
436 "regcache_read_pc: Unable to find PC");
442 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
444 if (the_target
->write_pc
)
445 the_target
->write_pc (regcache
, pc
);
447 internal_error (__FILE__
, __LINE__
,
448 "regcache_write_pc: Unable to update PC");