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