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