]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/regcache.c
aarch64 multi-arch part 6: HW breakpoint on unaligned address
[thirdparty/binutils-gdb.git] / gdb / gdbserver / regcache.c
1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2015 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 = inferior_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_inferior_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 regcache->tdesc->num_registers);
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 = inferior_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 void
111 regcache_invalidate (void)
112 {
113 /* Only update the threads of the current process. */
114 int pid = ptid_get_pid (current_thread->entry.id);
115
116 find_inferior (&all_threads, regcache_invalidate_one, &pid);
117 }
118
119 #endif
120
121 struct regcache *
122 init_register_cache (struct regcache *regcache,
123 const struct target_desc *tdesc,
124 unsigned char *regbuf)
125 {
126 if (regbuf == NULL)
127 {
128 #ifndef IN_PROCESS_AGENT
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. */
133 regcache->tdesc = tdesc;
134 regcache->registers
135 = (unsigned char *) xcalloc (1, tdesc->registers_size);
136 regcache->registers_owned = 1;
137 regcache->register_status
138 = (unsigned char *) xcalloc (1, tdesc->num_registers);
139 gdb_assert (REG_UNAVAILABLE == 0);
140 #else
141 gdb_assert_not_reached ("can't allocate memory from the heap");
142 #endif
143 }
144 else
145 {
146 regcache->tdesc = tdesc;
147 regcache->registers = regbuf;
148 regcache->registers_owned = 0;
149 #ifndef IN_PROCESS_AGENT
150 regcache->register_status = NULL;
151 #endif
152 }
153
154 regcache->registers_valid = 0;
155
156 return regcache;
157 }
158
159 #ifndef IN_PROCESS_AGENT
160
161 struct regcache *
162 new_register_cache (const struct target_desc *tdesc)
163 {
164 struct regcache *regcache = XCNEW (struct regcache);
165
166 gdb_assert (tdesc->registers_size != 0);
167
168 return init_register_cache (regcache, tdesc, NULL);
169 }
170
171 void
172 free_register_cache (struct regcache *regcache)
173 {
174 if (regcache)
175 {
176 if (regcache->registers_owned)
177 free (regcache->registers);
178 free (regcache->register_status);
179 free (regcache);
180 }
181 }
182
183 #endif
184
185 void
186 regcache_cpy (struct regcache *dst, struct regcache *src)
187 {
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);
193 #ifndef IN_PROCESS_AGENT
194 if (dst->register_status != NULL && src->register_status != NULL)
195 memcpy (dst->register_status, src->register_status,
196 src->tdesc->num_registers);
197 #endif
198 dst->registers_valid = src->registers_valid;
199 }
200
201
202 #ifndef IN_PROCESS_AGENT
203
204 void
205 registers_to_string (struct regcache *regcache, char *buf)
206 {
207 unsigned char *registers = regcache->registers;
208 const struct target_desc *tdesc = regcache->tdesc;
209 int i;
210
211 for (i = 0; i < tdesc->num_registers; i++)
212 {
213 if (regcache->register_status[i] == REG_VALID)
214 {
215 bin2hex (registers, buf, register_size (tdesc, i));
216 buf += register_size (tdesc, i) * 2;
217 }
218 else
219 {
220 memset (buf, 'x', register_size (tdesc, i) * 2);
221 buf += register_size (tdesc, i) * 2;
222 }
223 registers += register_size (tdesc, i);
224 }
225 *buf = '\0';
226 }
227
228 void
229 registers_from_string (struct regcache *regcache, char *buf)
230 {
231 int len = strlen (buf);
232 unsigned char *registers = regcache->registers;
233 const struct target_desc *tdesc = regcache->tdesc;
234
235 if (len != tdesc->registers_size * 2)
236 {
237 warning ("Wrong sized register packet (expected %d bytes, got %d)",
238 2 * tdesc->registers_size, len);
239 if (len > tdesc->registers_size * 2)
240 len = tdesc->registers_size * 2;
241 }
242 hex2bin (buf, registers, len / 2);
243 }
244
245 struct reg *
246 find_register_by_name (const struct target_desc *tdesc, const char *name)
247 {
248 int i;
249
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];
253 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
254 name);
255 }
256
257 int
258 find_regno (const struct target_desc *tdesc, const char *name)
259 {
260 int i;
261
262 for (i = 0; i < tdesc->num_registers; i++)
263 if (strcmp (name, tdesc->reg_defs[i].name) == 0)
264 return i;
265 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
266 name);
267 }
268
269 struct reg *
270 find_register_by_number (const struct target_desc *tdesc, int n)
271 {
272 return &tdesc->reg_defs[n];
273 }
274
275 #endif
276
277 #ifndef IN_PROCESS_AGENT
278 static void
279 free_register_cache_thread (struct thread_info *thread)
280 {
281 struct regcache *regcache = inferior_regcache_data (thread);
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
291 static void
292 free_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
299 void
300 regcache_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
307 int
308 register_cache_size (const struct target_desc *tdesc)
309 {
310 return tdesc->registers_size;
311 }
312
313 int
314 register_size (const struct target_desc *tdesc, int n)
315 {
316 return tdesc->reg_defs[n].size / 8;
317 }
318
319 /* See common/common-regcache.h. */
320
321 int
322 regcache_register_size (const struct regcache *regcache, int n)
323 {
324 return register_size (regcache->tdesc, n);
325 }
326
327 static unsigned char *
328 register_data (struct regcache *regcache, int n, int fetch)
329 {
330 return regcache->registers + regcache->tdesc->reg_defs[n].offset / 8;
331 }
332
333 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
334 If BUF is NULL, the register's value is recorded as
335 unavailable. */
336
337 void
338 supply_register (struct regcache *regcache, int n, const void *buf)
339 {
340 if (buf)
341 {
342 memcpy (register_data (regcache, n, 0), buf,
343 register_size (regcache->tdesc, n));
344 #ifndef IN_PROCESS_AGENT
345 if (regcache->register_status != NULL)
346 regcache->register_status[n] = REG_VALID;
347 #endif
348 }
349 else
350 {
351 memset (register_data (regcache, n, 0), 0,
352 register_size (regcache->tdesc, n));
353 #ifndef IN_PROCESS_AGENT
354 if (regcache->register_status != NULL)
355 regcache->register_status[n] = REG_UNAVAILABLE;
356 #endif
357 }
358 }
359
360 /* Supply register N with value zero to REGCACHE. */
361
362 void
363 supply_register_zeroed (struct regcache *regcache, int n)
364 {
365 memset (register_data (regcache, n, 0), 0,
366 register_size (regcache->tdesc, n));
367 #ifndef IN_PROCESS_AGENT
368 if (regcache->register_status != NULL)
369 regcache->register_status[n] = REG_VALID;
370 #endif
371 }
372
373 /* Supply the whole register set whose contents are stored in BUF, to
374 REGCACHE. If BUF is NULL, all the registers' values are recorded
375 as unavailable. */
376
377 void
378 supply_regblock (struct regcache *regcache, const void *buf)
379 {
380 if (buf)
381 {
382 const struct target_desc *tdesc = regcache->tdesc;
383
384 memcpy (regcache->registers, buf, tdesc->registers_size);
385 #ifndef IN_PROCESS_AGENT
386 {
387 int i;
388
389 for (i = 0; i < tdesc->num_registers; i++)
390 regcache->register_status[i] = REG_VALID;
391 }
392 #endif
393 }
394 else
395 {
396 const struct target_desc *tdesc = regcache->tdesc;
397
398 memset (regcache->registers, 0, tdesc->registers_size);
399 #ifndef IN_PROCESS_AGENT
400 {
401 int i;
402
403 for (i = 0; i < tdesc->num_registers; i++)
404 regcache->register_status[i] = REG_UNAVAILABLE;
405 }
406 #endif
407 }
408 }
409
410 #ifndef IN_PROCESS_AGENT
411
412 void
413 supply_register_by_name (struct regcache *regcache,
414 const char *name, const void *buf)
415 {
416 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
417 }
418
419 #endif
420
421 void
422 collect_register (struct regcache *regcache, int n, void *buf)
423 {
424 memcpy (buf, register_data (regcache, n, 1),
425 register_size (regcache->tdesc, n));
426 }
427
428 #ifndef IN_PROCESS_AGENT
429
430 void
431 collect_register_as_string (struct regcache *regcache, int n, char *buf)
432 {
433 bin2hex (register_data (regcache, n, 1), buf,
434 register_size (regcache->tdesc, n));
435 }
436
437 void
438 collect_register_by_name (struct regcache *regcache,
439 const char *name, void *buf)
440 {
441 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
442 }
443
444 /* Special handling for register PC. */
445
446 CORE_ADDR
447 regcache_read_pc (struct regcache *regcache)
448 {
449 CORE_ADDR pc_val;
450
451 if (the_target->read_pc)
452 pc_val = the_target->read_pc (regcache);
453 else
454 internal_error (__FILE__, __LINE__,
455 "regcache_read_pc: Unable to find PC");
456
457 return pc_val;
458 }
459
460 void
461 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
462 {
463 if (the_target->write_pc)
464 the_target->write_pc (regcache, pc);
465 else
466 internal_error (__FILE__, __LINE__,
467 "regcache_write_pc: Unable to update PC");
468 }
469
470 #endif