[aarch64] Check region OK for HW watchpoint in GDBserver
[deliverable/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 = xcalloc (1, tdesc->registers_size);
135 regcache->registers_owned = 1;
136 regcache->register_status = xcalloc (1, tdesc->num_registers);
137 gdb_assert (REG_UNAVAILABLE == 0);
138 #else
139 gdb_assert_not_reached ("can't allocate memory from the heap");
140 #endif
141 }
142 else
143 {
144 regcache->tdesc = tdesc;
145 regcache->registers = regbuf;
146 regcache->registers_owned = 0;
147 #ifndef IN_PROCESS_AGENT
148 regcache->register_status = NULL;
149 #endif
150 }
151
152 regcache->registers_valid = 0;
153
154 return regcache;
155 }
156
157 #ifndef IN_PROCESS_AGENT
158
159 struct regcache *
160 new_register_cache (const struct target_desc *tdesc)
161 {
162 struct regcache *regcache = XCNEW (struct regcache);
163
164 gdb_assert (tdesc->registers_size != 0);
165
166 return init_register_cache (regcache, tdesc, NULL);
167 }
168
169 void
170 free_register_cache (struct regcache *regcache)
171 {
172 if (regcache)
173 {
174 if (regcache->registers_owned)
175 free (regcache->registers);
176 free (regcache->register_status);
177 free (regcache);
178 }
179 }
180
181 #endif
182
183 void
184 regcache_cpy (struct regcache *dst, struct regcache *src)
185 {
186 gdb_assert (src != NULL && dst != NULL);
187 gdb_assert (src->tdesc == dst->tdesc);
188 gdb_assert (src != dst);
189
190 memcpy (dst->registers, src->registers, src->tdesc->registers_size);
191 #ifndef IN_PROCESS_AGENT
192 if (dst->register_status != NULL && src->register_status != NULL)
193 memcpy (dst->register_status, src->register_status,
194 src->tdesc->num_registers);
195 #endif
196 dst->registers_valid = src->registers_valid;
197 }
198
199
200 #ifndef IN_PROCESS_AGENT
201
202 void
203 registers_to_string (struct regcache *regcache, char *buf)
204 {
205 unsigned char *registers = regcache->registers;
206 const struct target_desc *tdesc = regcache->tdesc;
207 int i;
208
209 for (i = 0; i < tdesc->num_registers; i++)
210 {
211 if (regcache->register_status[i] == REG_VALID)
212 {
213 bin2hex (registers, buf, register_size (tdesc, i));
214 buf += register_size (tdesc, i) * 2;
215 }
216 else
217 {
218 memset (buf, 'x', register_size (tdesc, i) * 2);
219 buf += register_size (tdesc, i) * 2;
220 }
221 registers += register_size (tdesc, i);
222 }
223 *buf = '\0';
224 }
225
226 void
227 registers_from_string (struct regcache *regcache, char *buf)
228 {
229 int len = strlen (buf);
230 unsigned char *registers = regcache->registers;
231 const struct target_desc *tdesc = regcache->tdesc;
232
233 if (len != tdesc->registers_size * 2)
234 {
235 warning ("Wrong sized register packet (expected %d bytes, got %d)",
236 2 * tdesc->registers_size, len);
237 if (len > tdesc->registers_size * 2)
238 len = tdesc->registers_size * 2;
239 }
240 hex2bin (buf, registers, len / 2);
241 }
242
243 struct reg *
244 find_register_by_name (const struct target_desc *tdesc, const char *name)
245 {
246 int i;
247
248 for (i = 0; i < tdesc->num_registers; i++)
249 if (strcmp (name, tdesc->reg_defs[i].name) == 0)
250 return &tdesc->reg_defs[i];
251 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
252 name);
253 }
254
255 int
256 find_regno (const struct target_desc *tdesc, const char *name)
257 {
258 int i;
259
260 for (i = 0; i < tdesc->num_registers; i++)
261 if (strcmp (name, tdesc->reg_defs[i].name) == 0)
262 return i;
263 internal_error (__FILE__, __LINE__, "Unknown register %s requested",
264 name);
265 }
266
267 struct reg *
268 find_register_by_number (const struct target_desc *tdesc, int n)
269 {
270 return &tdesc->reg_defs[n];
271 }
272
273 #endif
274
275 #ifndef IN_PROCESS_AGENT
276 static void
277 free_register_cache_thread (struct thread_info *thread)
278 {
279 struct regcache *regcache = inferior_regcache_data (thread);
280
281 if (regcache != NULL)
282 {
283 regcache_invalidate_thread (thread);
284 free_register_cache (regcache);
285 set_inferior_regcache_data (thread, NULL);
286 }
287 }
288
289 static void
290 free_register_cache_thread_one (struct inferior_list_entry *entry)
291 {
292 struct thread_info *thread = (struct thread_info *) entry;
293
294 free_register_cache_thread (thread);
295 }
296
297 void
298 regcache_release (void)
299 {
300 /* Flush and release all pre-existing register caches. */
301 for_each_inferior (&all_threads, free_register_cache_thread_one);
302 }
303 #endif
304
305 int
306 register_cache_size (const struct target_desc *tdesc)
307 {
308 return tdesc->registers_size;
309 }
310
311 int
312 register_size (const struct target_desc *tdesc, int n)
313 {
314 return tdesc->reg_defs[n].size / 8;
315 }
316
317 static unsigned char *
318 register_data (struct regcache *regcache, int n, int fetch)
319 {
320 return regcache->registers + regcache->tdesc->reg_defs[n].offset / 8;
321 }
322
323 /* Supply register N, whose contents are stored in BUF, to REGCACHE.
324 If BUF is NULL, the register's value is recorded as
325 unavailable. */
326
327 void
328 supply_register (struct regcache *regcache, int n, const void *buf)
329 {
330 if (buf)
331 {
332 memcpy (register_data (regcache, n, 0), buf,
333 register_size (regcache->tdesc, n));
334 #ifndef IN_PROCESS_AGENT
335 if (regcache->register_status != NULL)
336 regcache->register_status[n] = REG_VALID;
337 #endif
338 }
339 else
340 {
341 memset (register_data (regcache, n, 0), 0,
342 register_size (regcache->tdesc, n));
343 #ifndef IN_PROCESS_AGENT
344 if (regcache->register_status != NULL)
345 regcache->register_status[n] = REG_UNAVAILABLE;
346 #endif
347 }
348 }
349
350 /* Supply register N with value zero to REGCACHE. */
351
352 void
353 supply_register_zeroed (struct regcache *regcache, int n)
354 {
355 memset (register_data (regcache, n, 0), 0,
356 register_size (regcache->tdesc, n));
357 #ifndef IN_PROCESS_AGENT
358 if (regcache->register_status != NULL)
359 regcache->register_status[n] = REG_VALID;
360 #endif
361 }
362
363 /* Supply the whole register set whose contents are stored in BUF, to
364 REGCACHE. If BUF is NULL, all the registers' values are recorded
365 as unavailable. */
366
367 void
368 supply_regblock (struct regcache *regcache, const void *buf)
369 {
370 if (buf)
371 {
372 const struct target_desc *tdesc = regcache->tdesc;
373
374 memcpy (regcache->registers, buf, tdesc->registers_size);
375 #ifndef IN_PROCESS_AGENT
376 {
377 int i;
378
379 for (i = 0; i < tdesc->num_registers; i++)
380 regcache->register_status[i] = REG_VALID;
381 }
382 #endif
383 }
384 else
385 {
386 const struct target_desc *tdesc = regcache->tdesc;
387
388 memset (regcache->registers, 0, tdesc->registers_size);
389 #ifndef IN_PROCESS_AGENT
390 {
391 int i;
392
393 for (i = 0; i < tdesc->num_registers; i++)
394 regcache->register_status[i] = REG_UNAVAILABLE;
395 }
396 #endif
397 }
398 }
399
400 #ifndef IN_PROCESS_AGENT
401
402 void
403 supply_register_by_name (struct regcache *regcache,
404 const char *name, const void *buf)
405 {
406 supply_register (regcache, find_regno (regcache->tdesc, name), buf);
407 }
408
409 #endif
410
411 void
412 collect_register (struct regcache *regcache, int n, void *buf)
413 {
414 memcpy (buf, register_data (regcache, n, 1),
415 register_size (regcache->tdesc, n));
416 }
417
418 #ifndef IN_PROCESS_AGENT
419
420 void
421 collect_register_as_string (struct regcache *regcache, int n, char *buf)
422 {
423 bin2hex (register_data (regcache, n, 1), buf,
424 register_size (regcache->tdesc, n));
425 }
426
427 void
428 collect_register_by_name (struct regcache *regcache,
429 const char *name, void *buf)
430 {
431 collect_register (regcache, find_regno (regcache->tdesc, name), buf);
432 }
433
434 /* Special handling for register PC. */
435
436 CORE_ADDR
437 regcache_read_pc (struct regcache *regcache)
438 {
439 CORE_ADDR pc_val;
440
441 if (the_target->read_pc)
442 pc_val = the_target->read_pc (regcache);
443 else
444 internal_error (__FILE__, __LINE__,
445 "regcache_read_pc: Unable to find PC");
446
447 return pc_val;
448 }
449
450 void
451 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
452 {
453 if (the_target->write_pc)
454 the_target->write_pc (regcache, pc);
455 else
456 internal_error (__FILE__, __LINE__,
457 "regcache_write_pc: Unable to update PC");
458 }
459
460 #endif
This page took 0.038591 seconds and 4 git commands to generate.