]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/regcache.c
* regcache.c (set_register_cache): Invalidate regcaches before
[thirdparty/binutils-gdb.git] / gdb / gdbserver / regcache.c
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.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "server.h"
21 #include "regdef.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 static int register_bytes;
27
28 static struct reg *reg_defs;
29 static int num_registers;
30
31 const char **gdbserver_expedite_regs;
32
33 struct regcache *
34 get_thread_regcache (struct thread_info *thread, int fetch)
35 {
36 struct regcache *regcache;
37
38 regcache = (struct regcache *) inferior_regcache_data (thread);
39
40 if (regcache == NULL)
41 fatal ("no register cache");
42
43 if (fetch && regcache->registers_valid == 0)
44 {
45 struct thread_info *saved_inferior = current_inferior;
46
47 current_inferior = thread;
48 fetch_inferior_registers (regcache, -1);
49 current_inferior = saved_inferior;
50 regcache->registers_valid = 1;
51 }
52
53 return regcache;
54 }
55
56 void
57 regcache_invalidate_one (struct inferior_list_entry *entry)
58 {
59 struct thread_info *thread = (struct thread_info *) entry;
60 struct regcache *regcache;
61
62 regcache = (struct regcache *) inferior_regcache_data (thread);
63
64 if (regcache == NULL)
65 return;
66
67 if (regcache->registers_valid)
68 {
69 struct thread_info *saved_inferior = current_inferior;
70
71 current_inferior = thread;
72 store_inferior_registers (regcache, -1);
73 current_inferior = saved_inferior;
74 }
75
76 regcache->registers_valid = 0;
77 }
78
79 void
80 regcache_invalidate (void)
81 {
82 for_each_inferior (&all_threads, regcache_invalidate_one);
83 }
84
85 struct regcache *
86 init_register_cache (struct regcache *regcache, unsigned char *regbuf)
87 {
88 if (regbuf == NULL)
89 {
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
93 garbage. */
94 regcache->registers = xcalloc (1, register_bytes);
95 regcache->registers_owned = 1;
96 }
97 else
98 {
99 regcache->registers = regbuf;
100 regcache->registers_owned = 0;
101 }
102
103 regcache->registers_valid = 0;
104
105 return regcache;
106 }
107
108 struct regcache *
109 new_register_cache (void)
110 {
111 struct regcache *regcache;
112
113 if (register_bytes == 0)
114 return NULL; /* The architecture hasn't been initialized yet. */
115
116 regcache = xmalloc (sizeof (*regcache));
117 return init_register_cache (regcache, NULL);
118 }
119
120 void
121 free_register_cache (struct regcache *regcache)
122 {
123 if (regcache)
124 {
125 free (regcache->registers);
126 free (regcache);
127 }
128 }
129
130 void
131 regcache_cpy (struct regcache *dst, struct regcache *src)
132 {
133 memcpy (dst->registers, src->registers, register_bytes);
134 dst->registers_valid = src->registers_valid;
135 }
136
137 static void
138 realloc_register_cache (struct inferior_list_entry *thread_p)
139 {
140 struct thread_info *thread = (struct thread_info *) thread_p;
141 struct regcache *regcache
142 = (struct regcache *) inferior_regcache_data (thread);
143
144 if (regcache != NULL)
145 regcache_invalidate_one (thread_p);
146 free_register_cache (regcache);
147 set_inferior_regcache_data (thread, new_register_cache ());
148 }
149
150 void
151 set_register_cache (struct reg *regs, int n)
152 {
153 int offset, i;
154
155 /* Before changing the register cache internal layout, flush the
156 contents of valid caches back to the threads. */
157 regcache_invalidate ();
158
159 reg_defs = regs;
160 num_registers = n;
161
162 offset = 0;
163 for (i = 0; i < n; i++)
164 {
165 regs[i].offset = offset;
166 offset += regs[i].size;
167 }
168
169 register_bytes = offset / 8;
170
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.");
174
175 /* Re-allocate all pre-existing register caches. */
176 for_each_inferior (&all_threads, realloc_register_cache);
177 }
178
179 int
180 register_cache_size (void)
181 {
182 return register_bytes;
183 }
184
185 void
186 registers_to_string (struct regcache *regcache, char *buf)
187 {
188 unsigned char *registers = regcache->registers;
189
190 convert_int_to_ascii (registers, buf, register_bytes);
191 }
192
193 void
194 registers_from_string (struct regcache *regcache, char *buf)
195 {
196 int len = strlen (buf);
197 unsigned char *registers = regcache->registers;
198
199 if (len != register_bytes * 2)
200 {
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;
205 }
206 convert_ascii_to_int (buf, registers, len / 2);
207 }
208
209 struct reg *
210 find_register_by_name (const char *name)
211 {
212 int i;
213
214 for (i = 0; i < num_registers; i++)
215 if (!strcmp (name, reg_defs[i].name))
216 return &reg_defs[i];
217 fatal ("Unknown register %s requested", name);
218 return 0;
219 }
220
221 int
222 find_regno (const char *name)
223 {
224 int i;
225
226 for (i = 0; i < num_registers; i++)
227 if (!strcmp (name, reg_defs[i].name))
228 return i;
229 fatal ("Unknown register %s requested", name);
230 return -1;
231 }
232
233 struct reg *
234 find_register_by_number (int n)
235 {
236 return &reg_defs[n];
237 }
238
239 int
240 register_size (int n)
241 {
242 return reg_defs[n].size / 8;
243 }
244
245 static unsigned char *
246 register_data (struct regcache *regcache, int n, int fetch)
247 {
248 return regcache->registers + (reg_defs[n].offset / 8);
249 }
250
251 void
252 supply_register (struct regcache *regcache, int n, const void *buf)
253 {
254 if (buf)
255 memcpy (register_data (regcache, n, 0), buf, register_size (n));
256 else
257 memset (register_data (regcache, n, 0), 0, register_size (n));
258 }
259
260 void
261 supply_regblock (struct regcache *regcache, const void *buf)
262 {
263 if (buf)
264 memcpy (regcache->registers, buf, register_bytes);
265 else
266 memset (regcache->registers, 0, register_bytes);
267 }
268
269 void
270 supply_register_by_name (struct regcache *regcache,
271 const char *name, const void *buf)
272 {
273 supply_register (regcache, find_regno (name), buf);
274 }
275
276 void
277 collect_register (struct regcache *regcache, int n, void *buf)
278 {
279 memcpy (buf, register_data (regcache, n, 1), register_size (n));
280 }
281
282 void
283 collect_register_as_string (struct regcache *regcache, int n, char *buf)
284 {
285 convert_int_to_ascii (register_data (regcache, n, 1),
286 buf, register_size (n));
287 }
288
289 void
290 collect_register_by_name (struct regcache *regcache,
291 const char *name, void *buf)
292 {
293 collect_register (regcache, find_regno (name), buf);
294 }
295
296 /* Special handling for register PC. */
297
298 CORE_ADDR
299 regcache_read_pc (struct regcache *regcache)
300 {
301 CORE_ADDR pc_val;
302
303 if (the_target->read_pc)
304 pc_val = the_target->read_pc (regcache);
305 else
306 internal_error (__FILE__, __LINE__,
307 "regcache_read_pc: Unable to find PC");
308
309 return pc_val;
310 }
311
312 void
313 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
314 {
315 if (the_target->write_pc)
316 the_target->write_pc (regcache, pc);
317 else
318 internal_error (__FILE__, __LINE__,
319 "regcache_write_pc: Unable to update PC");
320 }