1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2002, 2004-2005, 2007-2012 Free Software
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdbthread.h"
27 static int register_bytes
;
29 static struct reg
*reg_defs
;
30 static int num_registers
;
32 const char **gdbserver_expedite_regs
;
34 #ifndef IN_PROCESS_AGENT
37 get_thread_regcache (struct thread_info
*thread
, int fetch
)
39 struct regcache
*regcache
;
41 regcache
= (struct regcache
*) inferior_regcache_data (thread
);
44 fatal ("no register cache");
46 if (fetch
&& regcache
->registers_valid
== 0)
48 struct thread_info
*saved_inferior
= current_inferior
;
50 current_inferior
= thread
;
51 fetch_inferior_registers (regcache
, -1);
52 current_inferior
= saved_inferior
;
53 regcache
->registers_valid
= 1;
60 regcache_invalidate_one (struct inferior_list_entry
*entry
)
62 struct thread_info
*thread
= (struct thread_info
*) entry
;
63 struct regcache
*regcache
;
65 regcache
= (struct regcache
*) inferior_regcache_data (thread
);
70 if (regcache
->registers_valid
)
72 struct thread_info
*saved_inferior
= current_inferior
;
74 current_inferior
= thread
;
75 store_inferior_registers (regcache
, -1);
76 current_inferior
= saved_inferior
;
79 regcache
->registers_valid
= 0;
83 regcache_invalidate (void)
85 for_each_inferior (&all_threads
, regcache_invalidate_one
);
91 init_register_cache (struct regcache
*regcache
, unsigned char *regbuf
)
93 #ifndef IN_PROCESS_AGENT
96 /* Make sure to zero-initialize the register cache when it is
97 created, in case there are registers the target never
98 fetches. This way they'll read as zero instead of
100 regcache
->registers
= xcalloc (1, register_bytes
);
101 regcache
->registers_owned
= 1;
102 regcache
->register_status
= xcalloc (1, num_registers
);
103 gdb_assert (REG_UNAVAILABLE
== 0);
108 fatal ("init_register_cache: can't allocate memory from the heap");
112 regcache
->registers
= regbuf
;
113 regcache
->registers_owned
= 0;
114 #ifndef IN_PROCESS_AGENT
115 regcache
->register_status
= NULL
;
119 regcache
->registers_valid
= 0;
124 #ifndef IN_PROCESS_AGENT
127 new_register_cache (void)
129 struct regcache
*regcache
;
131 if (register_bytes
== 0)
132 return NULL
; /* The architecture hasn't been initialized yet. */
134 regcache
= xmalloc (sizeof (*regcache
));
135 return init_register_cache (regcache
, NULL
);
139 free_register_cache (struct regcache
*regcache
)
143 if (regcache
->registers_owned
)
144 free (regcache
->registers
);
145 free (regcache
->register_status
);
153 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
155 memcpy (dst
->registers
, src
->registers
, register_bytes
);
156 #ifndef IN_PROCESS_AGENT
157 if (dst
->register_status
!= NULL
&& src
->register_status
!= NULL
)
158 memcpy (dst
->register_status
, src
->register_status
, num_registers
);
160 dst
->registers_valid
= src
->registers_valid
;
163 #ifndef IN_PROCESS_AGENT
165 realloc_register_cache (struct inferior_list_entry
*thread_p
)
167 struct thread_info
*thread
= (struct thread_info
*) thread_p
;
168 struct regcache
*regcache
169 = (struct regcache
*) inferior_regcache_data (thread
);
171 if (regcache
!= NULL
)
172 regcache_invalidate_one (thread_p
);
173 free_register_cache (regcache
);
174 set_inferior_regcache_data (thread
, new_register_cache ());
179 set_register_cache (struct reg
*regs
, int n
)
183 #ifndef IN_PROCESS_AGENT
184 /* Before changing the register cache internal layout, flush the
185 contents of valid caches back to the threads. */
186 regcache_invalidate ();
193 for (i
= 0; i
< n
; i
++)
195 regs
[i
].offset
= offset
;
196 offset
+= regs
[i
].size
;
199 register_bytes
= offset
/ 8;
201 /* Make sure PBUFSIZ is large enough to hold a full register packet. */
202 if (2 * register_bytes
+ 32 > PBUFSIZ
)
203 fatal ("Register packet size exceeds PBUFSIZ.");
205 #ifndef IN_PROCESS_AGENT
206 /* Re-allocate all pre-existing register caches. */
207 for_each_inferior (&all_threads
, realloc_register_cache
);
212 register_cache_size (void)
214 return register_bytes
;
217 #ifndef IN_PROCESS_AGENT
220 registers_to_string (struct regcache
*regcache
, char *buf
)
222 unsigned char *registers
= regcache
->registers
;
225 for (i
= 0; i
< num_registers
; i
++)
227 if (regcache
->register_status
[i
] == REG_VALID
)
229 convert_int_to_ascii (registers
, buf
, register_size (i
));
230 buf
+= register_size (i
) * 2;
234 memset (buf
, 'x', register_size (i
) * 2);
235 buf
+= register_size (i
) * 2;
237 registers
+= register_size (i
);
243 registers_from_string (struct regcache
*regcache
, char *buf
)
245 int len
= strlen (buf
);
246 unsigned char *registers
= regcache
->registers
;
248 if (len
!= register_bytes
* 2)
250 warning ("Wrong sized register packet (expected %d bytes, got %d)",
251 2*register_bytes
, len
);
252 if (len
> register_bytes
* 2)
253 len
= register_bytes
* 2;
255 convert_ascii_to_int (buf
, registers
, len
/ 2);
259 find_register_by_name (const char *name
)
263 for (i
= 0; i
< num_registers
; i
++)
264 if (!strcmp (name
, reg_defs
[i
].name
))
266 fatal ("Unknown register %s requested", name
);
271 find_regno (const char *name
)
275 for (i
= 0; i
< num_registers
; i
++)
276 if (!strcmp (name
, reg_defs
[i
].name
))
278 fatal ("Unknown register %s requested", name
);
283 find_register_by_number (int n
)
291 register_size (int n
)
293 return reg_defs
[n
].size
/ 8;
296 static unsigned char *
297 register_data (struct regcache
*regcache
, int n
, int fetch
)
299 return regcache
->registers
+ (reg_defs
[n
].offset
/ 8);
302 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
303 If BUF is NULL, the register's value is recorded as
307 supply_register (struct regcache
*regcache
, int n
, const void *buf
)
311 memcpy (register_data (regcache
, n
, 0), buf
, register_size (n
));
312 #ifndef IN_PROCESS_AGENT
313 if (regcache
->register_status
!= NULL
)
314 regcache
->register_status
[n
] = REG_VALID
;
319 memset (register_data (regcache
, n
, 0), 0, register_size (n
));
320 #ifndef IN_PROCESS_AGENT
321 if (regcache
->register_status
!= NULL
)
322 regcache
->register_status
[n
] = REG_UNAVAILABLE
;
327 /* Supply register N with value zero to REGCACHE. */
330 supply_register_zeroed (struct regcache
*regcache
, int n
)
332 memset (register_data (regcache
, n
, 0), 0, register_size (n
));
333 #ifndef IN_PROCESS_AGENT
334 if (regcache
->register_status
!= NULL
)
335 regcache
->register_status
[n
] = REG_VALID
;
339 /* Supply the whole register set whose contents are stored in BUF, to
340 REGCACHE. If BUF is NULL, all the registers' values are recorded
344 supply_regblock (struct regcache
*regcache
, const void *buf
)
348 memcpy (regcache
->registers
, buf
, register_bytes
);
349 #ifndef IN_PROCESS_AGENT
353 for (i
= 0; i
< num_registers
; i
++)
354 regcache
->register_status
[i
] = REG_VALID
;
360 memset (regcache
->registers
, 0, register_bytes
);
361 #ifndef IN_PROCESS_AGENT
365 for (i
= 0; i
< num_registers
; i
++)
366 regcache
->register_status
[i
] = REG_UNAVAILABLE
;
372 #ifndef IN_PROCESS_AGENT
375 supply_register_by_name (struct regcache
*regcache
,
376 const char *name
, const void *buf
)
378 supply_register (regcache
, find_regno (name
), buf
);
384 collect_register (struct regcache
*regcache
, int n
, void *buf
)
386 memcpy (buf
, register_data (regcache
, n
, 1), register_size (n
));
389 #ifndef IN_PROCESS_AGENT
392 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
394 convert_int_to_ascii (register_data (regcache
, n
, 1),
395 buf
, register_size (n
));
399 collect_register_by_name (struct regcache
*regcache
,
400 const char *name
, void *buf
)
402 collect_register (regcache
, find_regno (name
), buf
);
405 /* Special handling for register PC. */
408 regcache_read_pc (struct regcache
*regcache
)
412 if (the_target
->read_pc
)
413 pc_val
= the_target
->read_pc (regcache
);
415 internal_error (__FILE__
, __LINE__
,
416 "regcache_read_pc: Unable to find PC");
422 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
424 if (the_target
->write_pc
)
425 the_target
->write_pc (regcache
, pc
);
427 internal_error (__FILE__
, __LINE__
,
428 "regcache_write_pc: Unable to update PC");
This page took 0.038254 seconds and 4 git commands to generate.