Correct shell compatibility issue detected with pkgsrc.
[deliverable/binutils-gdb.git] / gdb / gdbserver / regcache.c
1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2017 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 #ifndef IN_PROCESS_AGENT
25
26 struct regcache *
27 get_thread_regcache (struct thread_info *thread, int fetch)
28 {
29 struct regcache *regcache;
30
31 regcache = thread_regcache_data (thread);
32
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. */
40 if (regcache == NULL)
41 {
42 struct process_info *proc = get_thread_process (thread);
43
44 gdb_assert (proc->tdesc != NULL);
45
46 regcache = new_register_cache (proc->tdesc);
47 set_thread_regcache_data (thread, regcache);
48 }
49
50 if (fetch && regcache->registers_valid == 0)
51 {
52 struct thread_info *saved_thread = current_thread;
53
54 current_thread = thread;
55 /* Invalidate all registers, to prevent stale left-overs. */
56 memset (regcache->register_status, REG_UNAVAILABLE,
57 VEC_length (tdesc_reg_p, regcache->tdesc->reg_defs));
58 fetch_inferior_registers (regcache, -1);
59 current_thread = saved_thread;
60 regcache->registers_valid = 1;
61 }
62
63 return regcache;
64 }
65
66 /* See common/common-regcache.h. */
67
68 struct regcache *
69 get_thread_regcache_for_ptid (ptid_t ptid)
70 {
71 return get_thread_regcache (find_thread_ptid (ptid), 1);
72 }
73
74 void
75 regcache_invalidate_thread (struct thread_info *thread)
76 {
77 struct regcache *regcache;
78
79 regcache = thread_regcache_data (thread);
80
81 if (regcache == NULL)
82 return;
83
84 if (regcache->registers_valid)
85 {
86 struct thread_info *saved_thread = current_thread;
87
88 current_thread = thread;
89 store_inferior_registers (regcache, -1);
90 current_thread = saved_thread;
91 }
92
93 regcache->registers_valid = 0;
94 }
95
96 static int
97 regcache_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
110 /* See regcache.h. */
111
112 void
113 regcache_invalidate_pid (int pid)
114 {
115 find_inferior (&all_threads, regcache_invalidate_one, &pid);
116 }
117
118 /* See regcache.h. */
119
120 void
121 regcache_invalidate (void)
122 {
123 /* Only update the threads of the current process. */
124 int pid = ptid_get_pid (current_thread->entry.id);
125
126 regcache_invalidate_pid (pid);
127 }
128
129 #endif
130
131 struct regcache *
132 init_register_cache (struct regcache *regcache,
133 const struct target_desc *tdesc,
134 unsigned char *regbuf)
135 {
136 if (regbuf == NULL)
137 {
138 #ifndef IN_PROCESS_AGENT
139 /* Make sure to zero-initialize the register cache when it is
140 created, in case there are registers the target never
141 fetches. This way they'll read as zero instead of
142 garbage. */
143 regcache->tdesc = tdesc;
144 regcache->registers
145 = (unsigned char *) xcalloc (1, tdesc->registers_size);
146 regcache->registers_owned = 1;
147 regcache->register_status
148 = (unsigned char *) xmalloc (VEC_length (tdesc_reg_p, tdesc->reg_defs));
149 memset ((void *) regcache->register_status, REG_UNAVAILABLE,
150 VEC_length (tdesc_reg_p, tdesc->reg_defs));
151 #else
152 gdb_assert_not_reached ("can't allocate memory from the heap");
153 #endif
154 }
155 else
156 {
157 regcache->tdesc = tdesc;
158 regcache->registers = regbuf;
159 regcache->registers_owned = 0;
160 #ifndef IN_PROCESS_AGENT
161 regcache->register_status = NULL;
162 #endif
163 }
164
165 regcache->registers_valid = 0;
166
167 return regcache;
168 }
169
170 #ifndef IN_PROCESS_AGENT
171
172 struct regcache *
173 new_register_cache (const struct target_desc *tdesc)
174 {
175 struct regcache *regcache = XCNEW (struct regcache);
176
177 gdb_assert (tdesc->registers_size != 0);
178
179 return init_register_cache (regcache, tdesc, NULL);
180 }
181
182 void
183 free_register_cache (struct regcache *regcache)
184 {
185 if (regcache)
186 {
187 if (regcache->registers_owned)
188 free (regcache->registers);
189 free (regcache->register_status);
190 free (regcache);
191 }
192 }
193
194 #endif
195
196 void
197 regcache_cpy (struct regcache *dst, struct regcache *src)
198 {
199 gdb_assert (src != NULL && dst != NULL);
200 gdb_assert (src->tdesc == dst->tdesc);
201 gdb_assert (src != dst);
202
203 memcpy (dst->registers, src->registers, src->tdesc->registers_size);
204 #ifndef IN_PROCESS_AGENT
205 if (dst->register_status != NULL && src->register_status != NULL)
206 memcpy (dst->register_status, src->register_status,
207 VEC_length (tdesc_reg_p, src->tdesc->reg_defs));
208 #endif
209 dst->registers_valid = src->registers_valid;
210 }
211
212
213 #ifndef IN_PROCESS_AGENT
214
215 void
216 registers_to_string (struct regcache *regcache, char *buf)
217 {
218 unsigned char *registers = regcache->registers;
219 const struct target_desc *tdesc = regcache->tdesc;
220 int i;
221 reg *reg;
222
223 for (i = 0; VEC_iterate (tdesc_reg_p, tdesc->reg_defs, i, reg); i++)
224 {
225 if (regcache->register_status[i] == REG_VALID)
226 {
227 bin2hex (registers, buf, register_size (tdesc, i));
228 buf += register_size (tdesc, i) * 2;
229 }
230 else
231 {
232 memset (buf, 'x', register_size (tdesc, i) * 2);
233 buf += register_size (tdesc, i) * 2;
234 }
235 registers += register_size (tdesc, i);
236 }
237 *buf = '\0';
238 }
239
240 void
241 registers_from_string (struct regcache *regcache, char *buf)
242 {
243 int len = strlen (buf);
244 unsigned char *registers = regcache->registers;
245 const struct target_desc *tdesc = regcache->tdesc;
246
247 if (len != tdesc->registers_size * 2)
248 {
249 warning ("Wrong sized register packet (expected %d bytes, got %d)",
250 2 * tdesc->registers_size, len);
251 if (len > tdesc->registers_size * 2)
252 len = tdesc->registers_size * 2;
253 }
254 hex2bin (buf, registers, len / 2);
255 }
256
257 int
258 find_regno (const struct target_desc *tdesc, const char *name)
259 {
260 int i;
261 reg *reg;
262
263 for (i = 0; VEC_iterate (tdesc_reg_p, tdesc->reg_defs, i, reg); i++)
264 if (strcmp (name, reg->name) == 0)
265 return i;
266 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
267 name);
268 }
269
270 #endif
271
272 struct reg *
273 find_register_by_number (const struct target_desc *tdesc, int n)
274 {
275 return VEC_index (tdesc_reg_p, tdesc->reg_defs, n);
276 }
277
278 #ifndef IN_PROCESS_AGENT
279 static void
280 free_register_cache_thread (struct thread_info *thread)
281 {
282 struct regcache *regcache = thread_regcache_data (thread);
283
284 if (regcache != NULL)
285 {
286 regcache_invalidate_thread (thread);
287 free_register_cache (regcache);
288 set_thread_regcache_data (thread, NULL);
289 }
290 }
291
292 static void
293 free_register_cache_thread_one (struct inferior_list_entry *entry)
294 {
295 struct thread_info *thread = (struct thread_info *) entry;
296
297 free_register_cache_thread (thread);
298 }
299
300 void
301 regcache_release (void)
302 {
303 /* Flush and release all pre-existing register caches. */
304 for_each_inferior (&all_threads, free_register_cache_thread_one);
305 }
306 #endif
307
308 int
309 register_cache_size (const struct target_desc *tdesc)
310 {
311 return tdesc->registers_size;
312 }
313
314 int
315 register_size (const struct target_desc *tdesc, int n)
316 {
317 return find_register_by_number (tdesc, n)->size / 8;
318 }
319
320 /* See common/common-regcache.h. */
321
322 int
323 regcache_register_size (const struct regcache *regcache, int n)
324 {
325 return register_size (regcache->tdesc, n);
326 }
327
328 static unsigned char *
329 register_data (struct regcache *regcache, int n, int fetch)
330 {
331 return (regcache->registers
332 + find_register_by_number (regcache->tdesc, n)->offset / 8);
333 }
334
335 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
336 If BUF is NULL, the register's value is recorded as
337 unavailable. */
338
339 void
340 supply_register (struct regcache *regcache, int n, const void *buf)
341 {
342 if (buf)
343 {
344 memcpy (register_data (regcache, n, 0), buf,
345 register_size (regcache->tdesc, n));
346 #ifndef IN_PROCESS_AGENT
347 if (regcache->register_status != NULL)
348 regcache->register_status[n] = REG_VALID;
349 #endif
350 }
351 else
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_UNAVAILABLE;
358 #endif
359 }
360 }
361
362 /* Supply register N with value zero to REGCACHE. */
363
364 void
365 supply_register_zeroed (struct regcache *regcache, int n)
366 {
367 memset (register_data (regcache, n, 0), 0,
368 register_size (regcache->tdesc, n));
369 #ifndef IN_PROCESS_AGENT
370 if (regcache->register_status != NULL)
371 regcache->register_status[n] = REG_VALID;
372 #endif
373 }
374
375 /* Supply the whole register set whose contents are stored in BUF, to
376 REGCACHE. If BUF is NULL, all the registers' values are recorded
377 as unavailable. */
378
379 void
380 supply_regblock (struct regcache *regcache, const void *buf)
381 {
382 if (buf)
383 {
384 const struct target_desc *tdesc = regcache->tdesc;
385
386 memcpy (regcache->registers, buf, tdesc->registers_size);
387 #ifndef IN_PROCESS_AGENT
388 {
389 int i;
390
391 for (i = 0; i < VEC_length (tdesc_reg_p, tdesc->reg_defs); i++)
392 regcache->register_status[i] = REG_VALID;
393 }
394 #endif
395 }
396 else
397 {
398 const struct target_desc *tdesc = regcache->tdesc;
399
400 memset (regcache->registers, 0, tdesc->registers_size);
401 #ifndef IN_PROCESS_AGENT
402 {
403 int i;
404
405 for (i = 0; i < VEC_length (tdesc_reg_p, tdesc->reg_defs); i++)
406 regcache->register_status[i] = REG_UNAVAILABLE;
407 }
408 #endif
409 }
410 }
411
412 #ifndef IN_PROCESS_AGENT
413
414 void
415 supply_register_by_name (struct regcache *regcache,
416 const char *name, const void *buf)
417 {
418 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
419 }
420
421 #endif
422
423 void
424 collect_register (struct regcache *regcache, int n, void *buf)
425 {
426 memcpy (buf, register_data (regcache, n, 1),
427 register_size (regcache->tdesc, n));
428 }
429
430 enum register_status
431 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
432 ULONGEST *val)
433 {
434 int size;
435
436 gdb_assert (regcache != NULL);
437 gdb_assert (regnum >= 0
438 && regnum < VEC_length (tdesc_reg_p, regcache->tdesc->reg_defs));
439
440 size = register_size (regcache->tdesc, regnum);
441
442 if (size > (int) sizeof (ULONGEST))
443 error (_("That operation is not available on integers of more than"
444 "%d bytes."),
445 (int) sizeof (ULONGEST));
446
447 *val = 0;
448 collect_register (regcache, regnum, val);
449
450 return REG_VALID;
451 }
452
453 #ifndef IN_PROCESS_AGENT
454
455 void
456 collect_register_as_string (struct regcache *regcache, int n, char *buf)
457 {
458 bin2hex (register_data (regcache, n, 1), buf,
459 register_size (regcache->tdesc, n));
460 }
461
462 void
463 collect_register_by_name (struct regcache *regcache,
464 const char *name, void *buf)
465 {
466 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
467 }
468
469 /* Special handling for register PC. */
470
471 CORE_ADDR
472 regcache_read_pc (struct regcache *regcache)
473 {
474 CORE_ADDR pc_val;
475
476 if (the_target->read_pc)
477 pc_val = the_target->read_pc (regcache);
478 else
479 internal_error (__FILE__, __LINE__,
480 "regcache_read_pc: Unable to find PC");
481
482 return pc_val;
483 }
484
485 void
486 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
487 {
488 if (the_target->write_pc)
489 the_target->write_pc (regcache, pc);
490 else
491 internal_error (__FILE__, __LINE__,
492 "regcache_write_pc: Unable to update PC");
493 }
494
495 #endif
This page took 0.047203 seconds and 4 git commands to generate.