daily update
[binutils-gdb.git] / gdb / gdbserver / regcache.c
1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19 #include "server.h"
20 #include "regdef.h"
21 #include "gdbthread.h"
22 #include "tdesc.h"
23 #include "rsp-low.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #ifndef IN_PROCESS_AGENT
29
30 struct regcache *
31 get_thread_regcache (struct thread_info *thread, int fetch)
32 {
33 struct regcache *regcache;
34
35 regcache = (struct regcache *) inferior_regcache_data (thread);
36
37 /* Threads' regcaches are created lazily, because biarch targets add
38 the main thread/lwp before seeing it stop for the first time, and
39 it is only after the target sees the thread stop for the first
40 time that the target has a chance of determining the process's
41 architecture. IOW, when we first add the process's main thread
42 we don't know which architecture/tdesc its regcache should
43 have. */
44 if (regcache == NULL)
45 {
46 struct process_info *proc = get_thread_process (thread);
47
48 if (proc->tdesc == NULL)
49 fatal ("no target description");
50
51 regcache = new_register_cache (proc->tdesc);
52 set_inferior_regcache_data (thread, regcache);
53 }
54
55 if (fetch && regcache->registers_valid == 0)
56 {
57 struct thread_info *saved_inferior = current_inferior;
58
59 current_inferior = thread;
60 fetch_inferior_registers (regcache, -1);
61 current_inferior = saved_inferior;
62 regcache->registers_valid = 1;
63 }
64
65 return regcache;
66 }
67
68 void
69 regcache_invalidate_thread (struct thread_info *thread)
70 {
71 struct regcache *regcache;
72
73 regcache = (struct regcache *) inferior_regcache_data (thread);
74
75 if (regcache == NULL)
76 return;
77
78 if (regcache->registers_valid)
79 {
80 struct thread_info *saved_inferior = current_inferior;
81
82 current_inferior = thread;
83 store_inferior_registers (regcache, -1);
84 current_inferior = saved_inferior;
85 }
86
87 regcache->registers_valid = 0;
88 }
89
90 static int
91 regcache_invalidate_one (struct inferior_list_entry *entry,
92 void *pid_p)
93 {
94 struct thread_info *thread = (struct thread_info *) entry;
95 int pid = *(int *) pid_p;
96
97 /* Only invalidate the regcaches of threads of this process. */
98 if (ptid_get_pid (entry->id) == pid)
99 regcache_invalidate_thread (thread);
100
101 return 0;
102 }
103
104 void
105 regcache_invalidate (void)
106 {
107 /* Only update the threads of the current process. */
108 int pid = ptid_get_pid (current_inferior->entry.id);
109
110 find_inferior (&all_threads, regcache_invalidate_one, &pid);
111 }
112
113 #endif
114
115 struct regcache *
116 init_register_cache (struct regcache *regcache,
117 const struct target_desc *tdesc,
118 unsigned char *regbuf)
119 {
120 #ifndef IN_PROCESS_AGENT
121 if (regbuf == NULL)
122 {
123 /* Make sure to zero-initialize the register cache when it is
124 created, in case there are registers the target never
125 fetches. This way they'll read as zero instead of
126 garbage. */
127 regcache->tdesc = tdesc;
128 regcache->registers = xcalloc (1, tdesc->registers_size);
129 regcache->registers_owned = 1;
130 regcache->register_status = xcalloc (1, tdesc->num_registers);
131 gdb_assert (REG_UNAVAILABLE == 0);
132 }
133 else
134 #else
135 if (regbuf == NULL)
136 fatal ("init_register_cache: can't allocate memory from the heap");
137 else
138 #endif
139 {
140 regcache->tdesc = tdesc;
141 regcache->registers = regbuf;
142 regcache->registers_owned = 0;
143 #ifndef IN_PROCESS_AGENT
144 regcache->register_status = NULL;
145 #endif
146 }
147
148 regcache->registers_valid = 0;
149
150 return regcache;
151 }
152
153 #ifndef IN_PROCESS_AGENT
154
155 struct regcache *
156 new_register_cache (const struct target_desc *tdesc)
157 {
158 struct regcache *regcache;
159
160 gdb_assert (tdesc->registers_size != 0);
161
162 regcache = xmalloc (sizeof (*regcache));
163 return init_register_cache (regcache, tdesc, NULL);
164 }
165
166 void
167 free_register_cache (struct regcache *regcache)
168 {
169 if (regcache)
170 {
171 if (regcache->registers_owned)
172 free (regcache->registers);
173 free (regcache->register_status);
174 free (regcache);
175 }
176 }
177
178 #endif
179
180 void
181 regcache_cpy (struct regcache *dst, struct regcache *src)
182 {
183 gdb_assert (src != NULL && dst != NULL);
184 gdb_assert (src->tdesc == dst->tdesc);
185 gdb_assert (src != dst);
186
187 memcpy (dst->registers, src->registers, src->tdesc->registers_size);
188 #ifndef IN_PROCESS_AGENT
189 if (dst->register_status != NULL && src->register_status != NULL)
190 memcpy (dst->register_status, src->register_status,
191 src->tdesc->num_registers);
192 #endif
193 dst->registers_valid = src->registers_valid;
194 }
195
196
197 #ifndef IN_PROCESS_AGENT
198
199 void
200 registers_to_string (struct regcache *regcache, char *buf)
201 {
202 unsigned char *registers = regcache->registers;
203 const struct target_desc *tdesc = regcache->tdesc;
204 int i;
205
206 for (i = 0; i < tdesc->num_registers; i++)
207 {
208 if (regcache->register_status[i] == REG_VALID)
209 {
210 bin2hex (registers, buf, register_size (tdesc, i));
211 buf += register_size (tdesc, i) * 2;
212 }
213 else
214 {
215 memset (buf, 'x', register_size (tdesc, i) * 2);
216 buf += register_size (tdesc, i) * 2;
217 }
218 registers += register_size (tdesc, i);
219 }
220 *buf = '\0';
221 }
222
223 void
224 registers_from_string (struct regcache *regcache, char *buf)
225 {
226 int len = strlen (buf);
227 unsigned char *registers = regcache->registers;
228 const struct target_desc *tdesc = regcache->tdesc;
229
230 if (len != tdesc->registers_size * 2)
231 {
232 warning ("Wrong sized register packet (expected %d bytes, got %d)",
233 2 * tdesc->registers_size, len);
234 if (len > tdesc->registers_size * 2)
235 len = tdesc->registers_size * 2;
236 }
237 hex2bin (buf, registers, len / 2);
238 }
239
240 struct reg *
241 find_register_by_name (const struct target_desc *tdesc, const char *name)
242 {
243 int i;
244
245 for (i = 0; i < tdesc->num_registers; i++)
246 if (strcmp (name, tdesc->reg_defs[i].name) == 0)
247 return &tdesc->reg_defs[i];
248 fatal ("Unknown register %s requested", name);
249 return 0;
250 }
251
252 int
253 find_regno (const struct target_desc *tdesc, const char *name)
254 {
255 int i;
256
257 for (i = 0; i < tdesc->num_registers; i++)
258 if (strcmp (name, tdesc->reg_defs[i].name) == 0)
259 return i;
260 fatal ("Unknown register %s requested", name);
261 return -1;
262 }
263
264 struct reg *
265 find_register_by_number (const struct target_desc *tdesc, int n)
266 {
267 return &tdesc->reg_defs[n];
268 }
269
270 #endif
271
272 #ifndef IN_PROCESS_AGENT
273 static void
274 free_register_cache_thread (struct thread_info *thread)
275 {
276 struct regcache *regcache
277 = (struct regcache *) inferior_regcache_data (thread);
278
279 if (regcache != NULL)
280 {
281 regcache_invalidate_thread (thread);
282 free_register_cache (regcache);
283 set_inferior_regcache_data (thread, NULL);
284 }
285 }
286
287 static void
288 free_register_cache_thread_one (struct inferior_list_entry *entry)
289 {
290 struct thread_info *thread = (struct thread_info *) entry;
291
292 free_register_cache_thread (thread);
293 }
294
295 void
296 regcache_release (void)
297 {
298 /* Flush and release all pre-existing register caches. */
299 for_each_inferior (&all_threads, free_register_cache_thread_one);
300 }
301 #endif
302
303 int
304 register_cache_size (const struct target_desc *tdesc)
305 {
306 return tdesc->registers_size;
307 }
308
309 int
310 register_size (const struct target_desc *tdesc, int n)
311 {
312 return tdesc->reg_defs[n].size / 8;
313 }
314
315 static unsigned char *
316 register_data (struct regcache *regcache, int n, int fetch)
317 {
318 return regcache->registers + regcache->tdesc->reg_defs[n].offset / 8;
319 }
320
321 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
322 If BUF is NULL, the register's value is recorded as
323 unavailable. */
324
325 void
326 supply_register (struct regcache *regcache, int n, const void *buf)
327 {
328 if (buf)
329 {
330 memcpy (register_data (regcache, n, 0), buf,
331 register_size (regcache->tdesc, n));
332 #ifndef IN_PROCESS_AGENT
333 if (regcache->register_status != NULL)
334 regcache->register_status[n] = REG_VALID;
335 #endif
336 }
337 else
338 {
339 memset (register_data (regcache, n, 0), 0,
340 register_size (regcache->tdesc, n));
341 #ifndef IN_PROCESS_AGENT
342 if (regcache->register_status != NULL)
343 regcache->register_status[n] = REG_UNAVAILABLE;
344 #endif
345 }
346 }
347
348 /* Supply register N with value zero to REGCACHE. */
349
350 void
351 supply_register_zeroed (struct regcache *regcache, int n)
352 {
353 memset (register_data (regcache, n, 0), 0,
354 register_size (regcache->tdesc, n));
355 #ifndef IN_PROCESS_AGENT
356 if (regcache->register_status != NULL)
357 regcache->register_status[n] = REG_VALID;
358 #endif
359 }
360
361 /* Supply the whole register set whose contents are stored in BUF, to
362 REGCACHE. If BUF is NULL, all the registers' values are recorded
363 as unavailable. */
364
365 void
366 supply_regblock (struct regcache *regcache, const void *buf)
367 {
368 if (buf)
369 {
370 const struct target_desc *tdesc = regcache->tdesc;
371
372 memcpy (regcache->registers, buf, tdesc->registers_size);
373 #ifndef IN_PROCESS_AGENT
374 {
375 int i;
376
377 for (i = 0; i < tdesc->num_registers; i++)
378 regcache->register_status[i] = REG_VALID;
379 }
380 #endif
381 }
382 else
383 {
384 const struct target_desc *tdesc = regcache->tdesc;
385
386 memset (regcache->registers, 0, tdesc->registers_size);
387 #ifndef IN_PROCESS_AGENT
388 {
389 int i;
390
391 for (i = 0; i < tdesc->num_registers; i++)
392 regcache->register_status[i] = REG_UNAVAILABLE;
393 }
394 #endif
395 }
396 }
397
398 #ifndef IN_PROCESS_AGENT
399
400 void
401 supply_register_by_name (struct regcache *regcache,
402 const char *name, const void *buf)
403 {
404 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
405 }
406
407 #endif
408
409 void
410 collect_register (struct regcache *regcache, int n, void *buf)
411 {
412 memcpy (buf, register_data (regcache, n, 1),
413 register_size (regcache->tdesc, n));
414 }
415
416 #ifndef IN_PROCESS_AGENT
417
418 void
419 collect_register_as_string (struct regcache *regcache, int n, char *buf)
420 {
421 bin2hex (register_data (regcache, n, 1), buf,
422 register_size (regcache->tdesc, n));
423 }
424
425 void
426 collect_register_by_name (struct regcache *regcache,
427 const char *name, void *buf)
428 {
429 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
430 }
431
432 /* Special handling for register PC. */
433
434 CORE_ADDR
435 regcache_read_pc (struct regcache *regcache)
436 {
437 CORE_ADDR pc_val;
438
439 if (the_target->read_pc)
440 pc_val = the_target->read_pc (regcache);
441 else
442 internal_error (__FILE__, __LINE__,
443 "regcache_read_pc: Unable to find PC");
444
445 return pc_val;
446 }
447
448 void
449 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
450 {
451 if (the_target->write_pc)
452 the_target->write_pc (regcache, pc);
453 else
454 internal_error (__FILE__, __LINE__,
455 "regcache_write_pc: Unable to update PC");
456 }
457
458 #endif