1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001, 2002, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
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/>. */
26 static int register_bytes
;
28 static struct reg
*reg_defs
;
29 static int num_registers
;
31 const char **gdbserver_expedite_regs
;
34 get_thread_regcache (struct thread_info
*thread
, int fetch
)
36 struct regcache
*regcache
;
38 regcache
= (struct regcache
*) inferior_regcache_data (thread
);
41 fatal ("no register cache");
43 if (fetch
&& regcache
->registers_valid
== 0)
45 struct thread_info
*saved_inferior
= current_inferior
;
47 current_inferior
= thread
;
48 fetch_inferior_registers (regcache
, -1);
49 current_inferior
= saved_inferior
;
50 regcache
->registers_valid
= 1;
57 regcache_invalidate_one (struct inferior_list_entry
*entry
)
59 struct thread_info
*thread
= (struct thread_info
*) entry
;
60 struct regcache
*regcache
;
62 regcache
= (struct regcache
*) inferior_regcache_data (thread
);
67 if (regcache
->registers_valid
)
69 struct thread_info
*saved_inferior
= current_inferior
;
71 current_inferior
= thread
;
72 store_inferior_registers (regcache
, -1);
73 current_inferior
= saved_inferior
;
76 regcache
->registers_valid
= 0;
80 regcache_invalidate (void)
82 for_each_inferior (&all_threads
, regcache_invalidate_one
);
86 init_register_cache (struct regcache
*regcache
, unsigned char *regbuf
)
90 /* Make sure to zero-initialize the register cache when it is
91 created, in case there are registers the target never
92 fetches. This way they'll read as zero instead of
94 regcache
->registers
= xcalloc (1, register_bytes
);
95 regcache
->registers_owned
= 1;
99 regcache
->registers
= regbuf
;
100 regcache
->registers_owned
= 0;
103 regcache
->registers_valid
= 0;
109 new_register_cache (void)
111 struct regcache
*regcache
;
113 if (register_bytes
== 0)
114 return NULL
; /* The architecture hasn't been initialized yet. */
116 regcache
= xmalloc (sizeof (*regcache
));
117 return init_register_cache (regcache
, NULL
);
121 free_register_cache (struct regcache
*regcache
)
125 free (regcache
->registers
);
131 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
133 memcpy (dst
->registers
, src
->registers
, register_bytes
);
134 dst
->registers_valid
= src
->registers_valid
;
138 realloc_register_cache (struct inferior_list_entry
*thread_p
)
140 struct thread_info
*thread
= (struct thread_info
*) thread_p
;
141 struct regcache
*regcache
142 = (struct regcache
*) inferior_regcache_data (thread
);
144 if (regcache
!= NULL
)
145 regcache_invalidate_one (thread_p
);
146 free_register_cache (regcache
);
147 set_inferior_regcache_data (thread
, new_register_cache ());
151 set_register_cache (struct reg
*regs
, int n
)
155 /* Before changing the register cache internal layout, flush the
156 contents of valid caches back to the threads. */
157 regcache_invalidate ();
163 for (i
= 0; i
< n
; i
++)
165 regs
[i
].offset
= offset
;
166 offset
+= regs
[i
].size
;
169 register_bytes
= offset
/ 8;
171 /* Make sure PBUFSIZ is large enough to hold a full register packet. */
172 if (2 * register_bytes
+ 32 > PBUFSIZ
)
173 fatal ("Register packet size exceeds PBUFSIZ.");
175 /* Re-allocate all pre-existing register caches. */
176 for_each_inferior (&all_threads
, realloc_register_cache
);
180 register_cache_size (void)
182 return register_bytes
;
186 registers_to_string (struct regcache
*regcache
, char *buf
)
188 unsigned char *registers
= regcache
->registers
;
190 convert_int_to_ascii (registers
, buf
, register_bytes
);
194 registers_from_string (struct regcache
*regcache
, char *buf
)
196 int len
= strlen (buf
);
197 unsigned char *registers
= regcache
->registers
;
199 if (len
!= register_bytes
* 2)
201 warning ("Wrong sized register packet (expected %d bytes, got %d)",
202 2*register_bytes
, len
);
203 if (len
> register_bytes
* 2)
204 len
= register_bytes
* 2;
206 convert_ascii_to_int (buf
, registers
, len
/ 2);
210 find_register_by_name (const char *name
)
214 for (i
= 0; i
< num_registers
; i
++)
215 if (!strcmp (name
, reg_defs
[i
].name
))
217 fatal ("Unknown register %s requested", name
);
222 find_regno (const char *name
)
226 for (i
= 0; i
< num_registers
; i
++)
227 if (!strcmp (name
, reg_defs
[i
].name
))
229 fatal ("Unknown register %s requested", name
);
234 find_register_by_number (int n
)
240 register_size (int n
)
242 return reg_defs
[n
].size
/ 8;
245 static unsigned char *
246 register_data (struct regcache
*regcache
, int n
, int fetch
)
248 return regcache
->registers
+ (reg_defs
[n
].offset
/ 8);
252 supply_register (struct regcache
*regcache
, int n
, const void *buf
)
255 memcpy (register_data (regcache
, n
, 0), buf
, register_size (n
));
257 memset (register_data (regcache
, n
, 0), 0, register_size (n
));
261 supply_regblock (struct regcache
*regcache
, const void *buf
)
264 memcpy (regcache
->registers
, buf
, register_bytes
);
266 memset (regcache
->registers
, 0, register_bytes
);
270 supply_register_by_name (struct regcache
*regcache
,
271 const char *name
, const void *buf
)
273 supply_register (regcache
, find_regno (name
), buf
);
277 collect_register (struct regcache
*regcache
, int n
, void *buf
)
279 memcpy (buf
, register_data (regcache
, n
, 1), register_size (n
));
283 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
285 convert_int_to_ascii (register_data (regcache
, n
, 1),
286 buf
, register_size (n
));
290 collect_register_by_name (struct regcache
*regcache
,
291 const char *name
, void *buf
)
293 collect_register (regcache
, find_regno (name
), buf
);
296 /* Special handling for register PC. */
299 regcache_read_pc (struct regcache
*regcache
)
303 if (the_target
->read_pc
)
304 pc_val
= the_target
->read_pc (regcache
);
306 internal_error (__FILE__
, __LINE__
,
307 "regcache_read_pc: Unable to find PC");
313 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
315 if (the_target
->write_pc
)
316 the_target
->write_pc (regcache
, pc
);
318 internal_error (__FILE__
, __LINE__
,
319 "regcache_write_pc: Unable to update PC");