Commit | Line | Data |
---|---|---|
0a30fbc4 | 1 | /* Register support routines for the remote server for GDB. |
618f726f | 2 | Copyright (C) 2001-2016 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 |
26 | struct regcache * |
27 | get_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 | ||
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 | ||
0d62e5e8 | 74 | void |
3aee8918 | 75 | regcache_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 |
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 | ||
80d82c19 JB |
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 | ||
0d62e5e8 | 120 | void |
442ea881 | 121 | regcache_invalidate (void) |
0d62e5e8 | 122 | { |
3aee8918 | 123 | /* Only update the threads of the current process. */ |
0bfdf32f | 124 | int pid = ptid_get_pid (current_thread->entry.id); |
3aee8918 | 125 | |
80d82c19 | 126 | regcache_invalidate_pid (pid); |
0d62e5e8 DJ |
127 | } |
128 | ||
fa593d66 PA |
129 | #endif |
130 | ||
219f2f23 | 131 | struct regcache * |
3aee8918 PA |
132 | init_register_cache (struct regcache *regcache, |
133 | const struct target_desc *tdesc, | |
134 | unsigned char *regbuf) | |
219f2f23 PA |
135 | { |
136 | if (regbuf == NULL) | |
137 | { | |
87f6c4e3 | 138 | #ifndef IN_PROCESS_AGENT |
219f2f23 PA |
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. */ | |
3aee8918 | 143 | regcache->tdesc = tdesc; |
224c3ddb SM |
144 | regcache->registers |
145 | = (unsigned char *) xcalloc (1, tdesc->registers_size); | |
219f2f23 | 146 | regcache->registers_owned = 1; |
224c3ddb | 147 | regcache->register_status |
68ce2059 AT |
148 | = (unsigned char *) xmalloc (tdesc->num_registers); |
149 | memset ((void *) regcache->register_status, REG_UNAVAILABLE, | |
150 | tdesc->num_registers); | |
fa593d66 | 151 | #else |
38e08fca | 152 | gdb_assert_not_reached ("can't allocate memory from the heap"); |
fa593d66 | 153 | #endif |
87f6c4e3 GB |
154 | } |
155 | else | |
219f2f23 | 156 | { |
3aee8918 | 157 | regcache->tdesc = tdesc; |
219f2f23 PA |
158 | regcache->registers = regbuf; |
159 | regcache->registers_owned = 0; | |
1c79eb8a PA |
160 | #ifndef IN_PROCESS_AGENT |
161 | regcache->register_status = NULL; | |
162 | #endif | |
219f2f23 PA |
163 | } |
164 | ||
165 | regcache->registers_valid = 0; | |
166 | ||
167 | return regcache; | |
168 | } | |
169 | ||
fa593d66 PA |
170 | #ifndef IN_PROCESS_AGENT |
171 | ||
442ea881 | 172 | struct regcache * |
3aee8918 | 173 | new_register_cache (const struct target_desc *tdesc) |
c04a1aa8 | 174 | { |
8d749320 | 175 | struct regcache *regcache = XCNEW (struct regcache); |
c04a1aa8 | 176 | |
3aee8918 | 177 | gdb_assert (tdesc->registers_size != 0); |
5822d809 | 178 | |
3aee8918 | 179 | return init_register_cache (regcache, tdesc, NULL); |
c04a1aa8 DJ |
180 | } |
181 | ||
182 | void | |
442ea881 | 183 | free_register_cache (struct regcache *regcache) |
c04a1aa8 | 184 | { |
5822d809 PA |
185 | if (regcache) |
186 | { | |
fa593d66 PA |
187 | if (regcache->registers_owned) |
188 | free (regcache->registers); | |
1c79eb8a | 189 | free (regcache->register_status); |
5822d809 PA |
190 | free (regcache); |
191 | } | |
c04a1aa8 DJ |
192 | } |
193 | ||
fa593d66 PA |
194 | #endif |
195 | ||
219f2f23 PA |
196 | void |
197 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
198 | { | |
3aee8918 PA |
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); | |
1c79eb8a PA |
204 | #ifndef IN_PROCESS_AGENT |
205 | if (dst->register_status != NULL && src->register_status != NULL) | |
3aee8918 PA |
206 | memcpy (dst->register_status, src->register_status, |
207 | src->tdesc->num_registers); | |
1c79eb8a | 208 | #endif |
219f2f23 PA |
209 | dst->registers_valid = src->registers_valid; |
210 | } | |
211 | ||
219f2f23 | 212 | |
fa593d66 PA |
213 | #ifndef IN_PROCESS_AGENT |
214 | ||
0a30fbc4 | 215 | void |
442ea881 | 216 | registers_to_string (struct regcache *regcache, char *buf) |
0a30fbc4 | 217 | { |
442ea881 | 218 | unsigned char *registers = regcache->registers; |
3aee8918 | 219 | const struct target_desc *tdesc = regcache->tdesc; |
1c79eb8a | 220 | int i; |
c04a1aa8 | 221 | |
3aee8918 | 222 | for (i = 0; i < tdesc->num_registers; i++) |
1c79eb8a PA |
223 | { |
224 | if (regcache->register_status[i] == REG_VALID) | |
225 | { | |
e9371aff | 226 | bin2hex (registers, buf, register_size (tdesc, i)); |
3aee8918 | 227 | buf += register_size (tdesc, i) * 2; |
1c79eb8a PA |
228 | } |
229 | else | |
230 | { | |
3aee8918 PA |
231 | memset (buf, 'x', register_size (tdesc, i) * 2); |
232 | buf += register_size (tdesc, i) * 2; | |
1c79eb8a | 233 | } |
3aee8918 | 234 | registers += register_size (tdesc, i); |
1c79eb8a PA |
235 | } |
236 | *buf = '\0'; | |
0a30fbc4 DJ |
237 | } |
238 | ||
239 | void | |
442ea881 | 240 | registers_from_string (struct regcache *regcache, char *buf) |
0a30fbc4 DJ |
241 | { |
242 | int len = strlen (buf); | |
442ea881 | 243 | unsigned char *registers = regcache->registers; |
3aee8918 | 244 | const struct target_desc *tdesc = regcache->tdesc; |
0a30fbc4 | 245 | |
3aee8918 | 246 | if (len != tdesc->registers_size * 2) |
0a30fbc4 | 247 | { |
1b3f6016 | 248 | warning ("Wrong sized register packet (expected %d bytes, got %d)", |
3aee8918 PA |
249 | 2 * tdesc->registers_size, len); |
250 | if (len > tdesc->registers_size * 2) | |
251 | len = tdesc->registers_size * 2; | |
0a30fbc4 | 252 | } |
a7191e8b | 253 | hex2bin (buf, registers, len / 2); |
0a30fbc4 DJ |
254 | } |
255 | ||
256 | struct reg * | |
3aee8918 | 257 | find_register_by_name (const struct target_desc *tdesc, const char *name) |
0a30fbc4 DJ |
258 | { |
259 | int i; | |
260 | ||
3aee8918 PA |
261 | for (i = 0; i < tdesc->num_registers; i++) |
262 | if (strcmp (name, tdesc->reg_defs[i].name) == 0) | |
263 | return &tdesc->reg_defs[i]; | |
38e08fca GB |
264 | internal_error (__FILE__, __LINE__, "Unknown register %s requested", |
265 | name); | |
0a30fbc4 DJ |
266 | } |
267 | ||
268 | int | |
3aee8918 | 269 | find_regno (const struct target_desc *tdesc, const char *name) |
0a30fbc4 DJ |
270 | { |
271 | int i; | |
272 | ||
3aee8918 PA |
273 | for (i = 0; i < tdesc->num_registers; i++) |
274 | if (strcmp (name, tdesc->reg_defs[i].name) == 0) | |
0a30fbc4 | 275 | return i; |
38e08fca GB |
276 | internal_error (__FILE__, __LINE__, "Unknown register %s requested", |
277 | name); | |
0a30fbc4 DJ |
278 | } |
279 | ||
280 | struct reg * | |
3aee8918 | 281 | find_register_by_number (const struct target_desc *tdesc, int n) |
0a30fbc4 | 282 | { |
3aee8918 | 283 | return &tdesc->reg_defs[n]; |
0a30fbc4 DJ |
284 | } |
285 | ||
fa593d66 PA |
286 | #endif |
287 | ||
3aee8918 PA |
288 | #ifndef IN_PROCESS_AGENT |
289 | static void | |
290 | free_register_cache_thread (struct thread_info *thread) | |
291 | { | |
a44892be | 292 | struct regcache *regcache = inferior_regcache_data (thread); |
3aee8918 PA |
293 | |
294 | if (regcache != NULL) | |
295 | { | |
296 | regcache_invalidate_thread (thread); | |
297 | free_register_cache (regcache); | |
298 | set_inferior_regcache_data (thread, NULL); | |
299 | } | |
300 | } | |
301 | ||
302 | static void | |
303 | free_register_cache_thread_one (struct inferior_list_entry *entry) | |
304 | { | |
305 | struct thread_info *thread = (struct thread_info *) entry; | |
306 | ||
307 | free_register_cache_thread (thread); | |
308 | } | |
309 | ||
310 | void | |
311 | regcache_release (void) | |
312 | { | |
313 | /* Flush and release all pre-existing register caches. */ | |
314 | for_each_inferior (&all_threads, free_register_cache_thread_one); | |
315 | } | |
316 | #endif | |
317 | ||
0a30fbc4 | 318 | int |
3aee8918 | 319 | register_cache_size (const struct target_desc *tdesc) |
0a30fbc4 | 320 | { |
3aee8918 PA |
321 | return tdesc->registers_size; |
322 | } | |
323 | ||
324 | int | |
325 | register_size (const struct target_desc *tdesc, int n) | |
326 | { | |
327 | return tdesc->reg_defs[n].size / 8; | |
0a30fbc4 DJ |
328 | } |
329 | ||
8d689ee5 YQ |
330 | /* See common/common-regcache.h. */ |
331 | ||
332 | int | |
333 | regcache_register_size (const struct regcache *regcache, int n) | |
334 | { | |
335 | return register_size (regcache->tdesc, n); | |
336 | } | |
337 | ||
f450004a | 338 | static unsigned char * |
442ea881 | 339 | register_data (struct regcache *regcache, int n, int fetch) |
0a30fbc4 | 340 | { |
3aee8918 | 341 | return regcache->registers + regcache->tdesc->reg_defs[n].offset / 8; |
0a30fbc4 DJ |
342 | } |
343 | ||
1c79eb8a PA |
344 | /* Supply register N, whose contents are stored in BUF, to REGCACHE. |
345 | If BUF is NULL, the register's value is recorded as | |
346 | unavailable. */ | |
347 | ||
58caa3dc | 348 | void |
442ea881 | 349 | supply_register (struct regcache *regcache, int n, const void *buf) |
58caa3dc | 350 | { |
3327ccf7 | 351 | if (buf) |
1c79eb8a | 352 | { |
3aee8918 PA |
353 | memcpy (register_data (regcache, n, 0), buf, |
354 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
355 | #ifndef IN_PROCESS_AGENT |
356 | if (regcache->register_status != NULL) | |
357 | regcache->register_status[n] = REG_VALID; | |
358 | #endif | |
359 | } | |
3327ccf7 | 360 | else |
1c79eb8a | 361 | { |
3aee8918 PA |
362 | memset (register_data (regcache, n, 0), 0, |
363 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
364 | #ifndef IN_PROCESS_AGENT |
365 | if (regcache->register_status != NULL) | |
366 | regcache->register_status[n] = REG_UNAVAILABLE; | |
367 | #endif | |
368 | } | |
58caa3dc DJ |
369 | } |
370 | ||
1c79eb8a PA |
371 | /* Supply register N with value zero to REGCACHE. */ |
372 | ||
373 | void | |
374 | supply_register_zeroed (struct regcache *regcache, int n) | |
375 | { | |
3aee8918 PA |
376 | memset (register_data (regcache, n, 0), 0, |
377 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
378 | #ifndef IN_PROCESS_AGENT |
379 | if (regcache->register_status != NULL) | |
380 | regcache->register_status[n] = REG_VALID; | |
381 | #endif | |
382 | } | |
383 | ||
384 | /* Supply the whole register set whose contents are stored in BUF, to | |
385 | REGCACHE. If BUF is NULL, all the registers' values are recorded | |
386 | as unavailable. */ | |
387 | ||
219f2f23 PA |
388 | void |
389 | supply_regblock (struct regcache *regcache, const void *buf) | |
390 | { | |
391 | if (buf) | |
1c79eb8a | 392 | { |
3aee8918 PA |
393 | const struct target_desc *tdesc = regcache->tdesc; |
394 | ||
395 | memcpy (regcache->registers, buf, tdesc->registers_size); | |
1c79eb8a PA |
396 | #ifndef IN_PROCESS_AGENT |
397 | { | |
398 | int i; | |
399 | ||
3aee8918 | 400 | for (i = 0; i < tdesc->num_registers; i++) |
1c79eb8a PA |
401 | regcache->register_status[i] = REG_VALID; |
402 | } | |
403 | #endif | |
404 | } | |
219f2f23 | 405 | else |
1c79eb8a | 406 | { |
3aee8918 PA |
407 | const struct target_desc *tdesc = regcache->tdesc; |
408 | ||
409 | memset (regcache->registers, 0, tdesc->registers_size); | |
1c79eb8a PA |
410 | #ifndef IN_PROCESS_AGENT |
411 | { | |
412 | int i; | |
413 | ||
3aee8918 | 414 | for (i = 0; i < tdesc->num_registers; i++) |
1c79eb8a PA |
415 | regcache->register_status[i] = REG_UNAVAILABLE; |
416 | } | |
417 | #endif | |
418 | } | |
219f2f23 PA |
419 | } |
420 | ||
fa593d66 PA |
421 | #ifndef IN_PROCESS_AGENT |
422 | ||
58caa3dc | 423 | void |
442ea881 PA |
424 | supply_register_by_name (struct regcache *regcache, |
425 | const char *name, const void *buf) | |
58caa3dc | 426 | { |
3aee8918 | 427 | supply_register (regcache, find_regno (regcache->tdesc, name), buf); |
58caa3dc DJ |
428 | } |
429 | ||
fa593d66 PA |
430 | #endif |
431 | ||
58caa3dc | 432 | void |
442ea881 | 433 | collect_register (struct regcache *regcache, int n, void *buf) |
58caa3dc | 434 | { |
3aee8918 PA |
435 | memcpy (buf, register_data (regcache, n, 1), |
436 | register_size (regcache->tdesc, n)); | |
0d62e5e8 DJ |
437 | } |
438 | ||
68ce2059 AT |
439 | enum register_status |
440 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, | |
441 | ULONGEST *val) | |
442 | { | |
443 | int size; | |
444 | ||
445 | gdb_assert (regcache != NULL); | |
446 | gdb_assert (regnum >= 0 && regnum < regcache->tdesc->num_registers); | |
447 | ||
448 | size = register_size (regcache->tdesc, regnum); | |
449 | ||
450 | if (size > (int) sizeof (ULONGEST)) | |
451 | error (_("That operation is not available on integers of more than" | |
452 | "%d bytes."), | |
453 | (int) sizeof (ULONGEST)); | |
454 | ||
455 | collect_register (regcache, regnum, val); | |
456 | ||
457 | return REG_VALID; | |
458 | } | |
459 | ||
fa593d66 PA |
460 | #ifndef IN_PROCESS_AGENT |
461 | ||
0d62e5e8 | 462 | void |
442ea881 | 463 | collect_register_as_string (struct regcache *regcache, int n, char *buf) |
0d62e5e8 | 464 | { |
e9371aff TT |
465 | bin2hex (register_data (regcache, n, 1), buf, |
466 | register_size (regcache->tdesc, n)); | |
58caa3dc DJ |
467 | } |
468 | ||
469 | void | |
442ea881 PA |
470 | collect_register_by_name (struct regcache *regcache, |
471 | const char *name, void *buf) | |
58caa3dc | 472 | { |
3aee8918 | 473 | collect_register (regcache, find_regno (regcache->tdesc, name), buf); |
58caa3dc | 474 | } |
219f2f23 PA |
475 | |
476 | /* Special handling for register PC. */ | |
477 | ||
478 | CORE_ADDR | |
479 | regcache_read_pc (struct regcache *regcache) | |
480 | { | |
481 | CORE_ADDR pc_val; | |
482 | ||
483 | if (the_target->read_pc) | |
484 | pc_val = the_target->read_pc (regcache); | |
485 | else | |
486 | internal_error (__FILE__, __LINE__, | |
487 | "regcache_read_pc: Unable to find PC"); | |
488 | ||
489 | return pc_val; | |
490 | } | |
491 | ||
492 | void | |
493 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) | |
494 | { | |
495 | if (the_target->write_pc) | |
496 | the_target->write_pc (regcache, pc); | |
497 | else | |
498 | internal_error (__FILE__, __LINE__, | |
499 | "regcache_write_pc: Unable to update PC"); | |
500 | } | |
fa593d66 PA |
501 | |
502 | #endif |