]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/regcache.c
gdb/gdbserver/
[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->registers_valid)
65 {
66 struct thread_info *saved_inferior = current_inferior;
67
68 current_inferior = thread;
69 store_inferior_registers (regcache, -1);
70 current_inferior = saved_inferior;
71 }
72
73 regcache->registers_valid = 0;
74 }
75
76 void
77 regcache_invalidate (void)
78 {
79 for_each_inferior (&all_threads, regcache_invalidate_one);
80 }
81
82 struct regcache *
83 init_register_cache (struct regcache *regcache, unsigned char *regbuf)
84 {
85 if (regbuf == NULL)
86 {
87 /* Make sure to zero-initialize the register cache when it is
88 created, in case there are registers the target never
89 fetches. This way they'll read as zero instead of
90 garbage. */
91 regcache->registers = xcalloc (1, register_bytes);
92 regcache->registers_owned = 1;
93 }
94 else
95 {
96 regcache->registers = regbuf;
97 regcache->registers_owned = 0;
98 }
99
100 regcache->registers_valid = 0;
101
102 return regcache;
103 }
104
105 struct regcache *
106 new_register_cache (void)
107 {
108 struct regcache *regcache;
109
110 if (register_bytes == 0)
111 return NULL; /* The architecture hasn't been initialized yet. */
112
113 regcache = xmalloc (sizeof (*regcache));
114 return init_register_cache (regcache, NULL);
115 }
116
117 void
118 free_register_cache (struct regcache *regcache)
119 {
120 if (regcache)
121 {
122 free (regcache->registers);
123 free (regcache);
124 }
125 }
126
127 void
128 regcache_cpy (struct regcache *dst, struct regcache *src)
129 {
130 memcpy (dst->registers, src->registers, register_bytes);
131 dst->registers_valid = src->registers_valid;
132 }
133
134 static void
135 realloc_register_cache (struct inferior_list_entry *thread_p)
136 {
137 struct thread_info *thread = (struct thread_info *) thread_p;
138 struct regcache *regcache
139 = (struct regcache *) inferior_regcache_data (thread);
140
141 free_register_cache (regcache);
142 set_inferior_regcache_data (thread, new_register_cache ());
143 }
144
145 void
146 set_register_cache (struct reg *regs, int n)
147 {
148 int offset, i;
149
150 reg_defs = regs;
151 num_registers = n;
152
153 offset = 0;
154 for (i = 0; i < n; i++)
155 {
156 regs[i].offset = offset;
157 offset += regs[i].size;
158 }
159
160 register_bytes = offset / 8;
161
162 /* Make sure PBUFSIZ is large enough to hold a full register packet. */
163 if (2 * register_bytes + 32 > PBUFSIZ)
164 fatal ("Register packet size exceeds PBUFSIZ.");
165
166 /* Re-allocate all pre-existing register caches. */
167 for_each_inferior (&all_threads, realloc_register_cache);
168 }
169
170 int
171 register_cache_size (void)
172 {
173 return register_bytes;
174 }
175
176 void
177 registers_to_string (struct regcache *regcache, char *buf)
178 {
179 unsigned char *registers = regcache->registers;
180
181 convert_int_to_ascii (registers, buf, register_bytes);
182 }
183
184 void
185 registers_from_string (struct regcache *regcache, char *buf)
186 {
187 int len = strlen (buf);
188 unsigned char *registers = regcache->registers;
189
190 if (len != register_bytes * 2)
191 {
192 warning ("Wrong sized register packet (expected %d bytes, got %d)",
193 2*register_bytes, len);
194 if (len > register_bytes * 2)
195 len = register_bytes * 2;
196 }
197 convert_ascii_to_int (buf, registers, len / 2);
198 }
199
200 struct reg *
201 find_register_by_name (const char *name)
202 {
203 int i;
204
205 for (i = 0; i < num_registers; i++)
206 if (!strcmp (name, reg_defs[i].name))
207 return &reg_defs[i];
208 fatal ("Unknown register %s requested", name);
209 return 0;
210 }
211
212 int
213 find_regno (const char *name)
214 {
215 int i;
216
217 for (i = 0; i < num_registers; i++)
218 if (!strcmp (name, reg_defs[i].name))
219 return i;
220 fatal ("Unknown register %s requested", name);
221 return -1;
222 }
223
224 struct reg *
225 find_register_by_number (int n)
226 {
227 return &reg_defs[n];
228 }
229
230 int
231 register_size (int n)
232 {
233 return reg_defs[n].size / 8;
234 }
235
236 static unsigned char *
237 register_data (struct regcache *regcache, int n, int fetch)
238 {
239 return regcache->registers + (reg_defs[n].offset / 8);
240 }
241
242 void
243 supply_register (struct regcache *regcache, int n, const void *buf)
244 {
245 if (buf)
246 memcpy (register_data (regcache, n, 0), buf, register_size (n));
247 else
248 memset (register_data (regcache, n, 0), 0, register_size (n));
249 }
250
251 void
252 supply_regblock (struct regcache *regcache, const void *buf)
253 {
254 if (buf)
255 memcpy (regcache->registers, buf, register_bytes);
256 else
257 memset (regcache->registers, 0, register_bytes);
258 }
259
260 void
261 supply_register_by_name (struct regcache *regcache,
262 const char *name, const void *buf)
263 {
264 supply_register (regcache, find_regno (name), buf);
265 }
266
267 void
268 collect_register (struct regcache *regcache, int n, void *buf)
269 {
270 memcpy (buf, register_data (regcache, n, 1), register_size (n));
271 }
272
273 void
274 collect_register_as_string (struct regcache *regcache, int n, char *buf)
275 {
276 convert_int_to_ascii (register_data (regcache, n, 1),
277 buf, register_size (n));
278 }
279
280 void
281 collect_register_by_name (struct regcache *regcache,
282 const char *name, void *buf)
283 {
284 collect_register (regcache, find_regno (name), buf);
285 }
286
287 /* Special handling for register PC. */
288
289 CORE_ADDR
290 regcache_read_pc (struct regcache *regcache)
291 {
292 CORE_ADDR pc_val;
293
294 if (the_target->read_pc)
295 pc_val = the_target->read_pc (regcache);
296 else
297 internal_error (__FILE__, __LINE__,
298 "regcache_read_pc: Unable to find PC");
299
300 return pc_val;
301 }
302
303 void
304 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
305 {
306 if (the_target->write_pc)
307 the_target->write_pc (regcache, pc);
308 else
309 internal_error (__FILE__, __LINE__,
310 "regcache_write_pc: Unable to update PC");
311 }