]>
Commit | Line | Data |
---|---|---|
0a30fbc4 | 1 | /* Register support routines for the remote server for GDB. |
e2882c85 | 2 | Copyright (C) 2001-2018 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 | |
e8f3f204 AH |
31 | /* Check the target descriptor is still valid for the current target. If |
32 | not, then clear it and create a new one. */ | |
33 | if (!target_validate_tdesc (thread)) | |
34 | { | |
35 | /* Clear regcache. */ | |
36 | free_register_cache (thread_regcache_data (thread)); | |
37 | set_thread_regcache_data (thread, NULL); | |
38 | regcache = NULL; | |
39 | ||
40 | target_arch_setup (); | |
41 | } | |
42 | ||
6afd337d | 43 | regcache = thread_regcache_data (thread); |
c04a1aa8 | 44 | |
3aee8918 PA |
45 | /* Threads' regcaches are created lazily, because biarch targets add |
46 | the main thread/lwp before seeing it stop for the first time, and | |
47 | it is only after the target sees the thread stop for the first | |
48 | time that the target has a chance of determining the process's | |
49 | architecture. IOW, when we first add the process's main thread | |
50 | we don't know which architecture/tdesc its regcache should | |
51 | have. */ | |
c04a1aa8 | 52 | if (regcache == NULL) |
3aee8918 PA |
53 | { |
54 | struct process_info *proc = get_thread_process (thread); | |
55 | ||
38e08fca | 56 | gdb_assert (proc->tdesc != NULL); |
3aee8918 PA |
57 | |
58 | regcache = new_register_cache (proc->tdesc); | |
6afd337d | 59 | set_thread_regcache_data (thread, regcache); |
3aee8918 | 60 | } |
c04a1aa8 | 61 | |
0d62e5e8 DJ |
62 | if (fetch && regcache->registers_valid == 0) |
63 | { | |
0bfdf32f | 64 | struct thread_info *saved_thread = current_thread; |
442ea881 | 65 | |
0bfdf32f | 66 | current_thread = thread; |
098dbe61 | 67 | /* Invalidate all registers, to prevent stale left-overs. */ |
c4dfafab SDJ |
68 | memset (regcache->register_status, REG_UNAVAILABLE, |
69 | regcache->tdesc->reg_defs.size ()); | |
442ea881 | 70 | fetch_inferior_registers (regcache, -1); |
0bfdf32f | 71 | current_thread = saved_thread; |
0d62e5e8 DJ |
72 | regcache->registers_valid = 1; |
73 | } | |
74 | ||
c04a1aa8 DJ |
75 | return regcache; |
76 | } | |
77 | ||
361c8ade GB |
78 | /* See common/common-regcache.h. */ |
79 | ||
80 | struct regcache * | |
81 | get_thread_regcache_for_ptid (ptid_t ptid) | |
82 | { | |
83 | return get_thread_regcache (find_thread_ptid (ptid), 1); | |
84 | } | |
85 | ||
0d62e5e8 | 86 | void |
3aee8918 | 87 | regcache_invalidate_thread (struct thread_info *thread) |
0d62e5e8 | 88 | { |
442ea881 | 89 | struct regcache *regcache; |
0d62e5e8 | 90 | |
6afd337d | 91 | regcache = thread_regcache_data (thread); |
0d62e5e8 | 92 | |
45ba0d02 PA |
93 | if (regcache == NULL) |
94 | return; | |
95 | ||
0d62e5e8 DJ |
96 | if (regcache->registers_valid) |
97 | { | |
0bfdf32f | 98 | struct thread_info *saved_thread = current_thread; |
0d62e5e8 | 99 | |
0bfdf32f | 100 | current_thread = thread; |
442ea881 | 101 | store_inferior_registers (regcache, -1); |
0bfdf32f | 102 | current_thread = saved_thread; |
0d62e5e8 DJ |
103 | } |
104 | ||
105 | regcache->registers_valid = 0; | |
106 | } | |
107 | ||
80d82c19 JB |
108 | /* See regcache.h. */ |
109 | ||
110 | void | |
111 | regcache_invalidate_pid (int pid) | |
112 | { | |
634a3254 SM |
113 | /* Only invalidate the regcaches of threads of this process. */ |
114 | for_each_thread (pid, regcache_invalidate_thread); | |
80d82c19 JB |
115 | } |
116 | ||
117 | /* See regcache.h. */ | |
118 | ||
0d62e5e8 | 119 | void |
442ea881 | 120 | regcache_invalidate (void) |
0d62e5e8 | 121 | { |
3aee8918 | 122 | /* Only update the threads of the current process. */ |
9c80ecd6 | 123 | int pid = current_thread->id.pid (); |
3aee8918 | 124 | |
80d82c19 | 125 | regcache_invalidate_pid (pid); |
0d62e5e8 DJ |
126 | } |
127 | ||
fa593d66 PA |
128 | #endif |
129 | ||
219f2f23 | 130 | struct regcache * |
3aee8918 PA |
131 | init_register_cache (struct regcache *regcache, |
132 | const struct target_desc *tdesc, | |
133 | unsigned char *regbuf) | |
219f2f23 PA |
134 | { |
135 | if (regbuf == NULL) | |
136 | { | |
87f6c4e3 | 137 | #ifndef IN_PROCESS_AGENT |
219f2f23 PA |
138 | /* Make sure to zero-initialize the register cache when it is |
139 | created, in case there are registers the target never | |
140 | fetches. This way they'll read as zero instead of | |
141 | garbage. */ | |
3aee8918 | 142 | regcache->tdesc = tdesc; |
224c3ddb SM |
143 | regcache->registers |
144 | = (unsigned char *) xcalloc (1, tdesc->registers_size); | |
219f2f23 | 145 | regcache->registers_owned = 1; |
224c3ddb | 146 | regcache->register_status |
c4dfafab SDJ |
147 | = (unsigned char *) xmalloc (tdesc->reg_defs.size ()); |
148 | memset ((void *) regcache->register_status, REG_UNAVAILABLE, | |
149 | tdesc->reg_defs.size ()); | |
fa593d66 | 150 | #else |
38e08fca | 151 | gdb_assert_not_reached ("can't allocate memory from the heap"); |
fa593d66 | 152 | #endif |
87f6c4e3 GB |
153 | } |
154 | else | |
219f2f23 | 155 | { |
3aee8918 | 156 | regcache->tdesc = tdesc; |
219f2f23 PA |
157 | regcache->registers = regbuf; |
158 | regcache->registers_owned = 0; | |
1c79eb8a PA |
159 | #ifndef IN_PROCESS_AGENT |
160 | regcache->register_status = NULL; | |
161 | #endif | |
219f2f23 PA |
162 | } |
163 | ||
164 | regcache->registers_valid = 0; | |
165 | ||
166 | return regcache; | |
167 | } | |
168 | ||
fa593d66 PA |
169 | #ifndef IN_PROCESS_AGENT |
170 | ||
442ea881 | 171 | struct regcache * |
3aee8918 | 172 | new_register_cache (const struct target_desc *tdesc) |
c04a1aa8 | 173 | { |
9c861883 | 174 | struct regcache *regcache = new struct regcache; |
c04a1aa8 | 175 | |
3aee8918 | 176 | gdb_assert (tdesc->registers_size != 0); |
5822d809 | 177 | |
3aee8918 | 178 | return init_register_cache (regcache, tdesc, NULL); |
c04a1aa8 DJ |
179 | } |
180 | ||
181 | void | |
442ea881 | 182 | free_register_cache (struct regcache *regcache) |
c04a1aa8 | 183 | { |
5822d809 PA |
184 | if (regcache) |
185 | { | |
fa593d66 PA |
186 | if (regcache->registers_owned) |
187 | free (regcache->registers); | |
1c79eb8a | 188 | free (regcache->register_status); |
9c861883 | 189 | delete regcache; |
5822d809 | 190 | } |
c04a1aa8 DJ |
191 | } |
192 | ||
fa593d66 PA |
193 | #endif |
194 | ||
219f2f23 PA |
195 | void |
196 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
197 | { | |
3aee8918 PA |
198 | gdb_assert (src != NULL && dst != NULL); |
199 | gdb_assert (src->tdesc == dst->tdesc); | |
200 | gdb_assert (src != dst); | |
201 | ||
202 | memcpy (dst->registers, src->registers, src->tdesc->registers_size); | |
1c79eb8a PA |
203 | #ifndef IN_PROCESS_AGENT |
204 | if (dst->register_status != NULL && src->register_status != NULL) | |
3aee8918 | 205 | memcpy (dst->register_status, src->register_status, |
c4dfafab | 206 | src->tdesc->reg_defs.size ()); |
1c79eb8a | 207 | #endif |
219f2f23 PA |
208 | dst->registers_valid = src->registers_valid; |
209 | } | |
210 | ||
5cd3e386 | 211 | /* Return a reference to the description of register N. */ |
dff7492c | 212 | |
5cd3e386 | 213 | static const struct reg & |
dff7492c AH |
214 | find_register_by_number (const struct target_desc *tdesc, int n) |
215 | { | |
216 | return tdesc->reg_defs[n]; | |
217 | } | |
219f2f23 | 218 | |
fa593d66 PA |
219 | #ifndef IN_PROCESS_AGENT |
220 | ||
0a30fbc4 | 221 | void |
442ea881 | 222 | registers_to_string (struct regcache *regcache, char *buf) |
0a30fbc4 | 223 | { |
442ea881 | 224 | unsigned char *registers = regcache->registers; |
3aee8918 | 225 | const struct target_desc *tdesc = regcache->tdesc; |
c04a1aa8 | 226 | |
c4dfafab | 227 | for (int i = 0; i < tdesc->reg_defs.size (); ++i) |
1c79eb8a PA |
228 | { |
229 | if (regcache->register_status[i] == REG_VALID) | |
230 | { | |
e9371aff | 231 | bin2hex (registers, buf, register_size (tdesc, i)); |
3aee8918 | 232 | buf += register_size (tdesc, i) * 2; |
1c79eb8a PA |
233 | } |
234 | else | |
235 | { | |
3aee8918 PA |
236 | memset (buf, 'x', register_size (tdesc, i) * 2); |
237 | buf += register_size (tdesc, i) * 2; | |
1c79eb8a | 238 | } |
3aee8918 | 239 | registers += register_size (tdesc, i); |
1c79eb8a PA |
240 | } |
241 | *buf = '\0'; | |
0a30fbc4 DJ |
242 | } |
243 | ||
244 | void | |
442ea881 | 245 | registers_from_string (struct regcache *regcache, char *buf) |
0a30fbc4 DJ |
246 | { |
247 | int len = strlen (buf); | |
442ea881 | 248 | unsigned char *registers = regcache->registers; |
3aee8918 | 249 | const struct target_desc *tdesc = regcache->tdesc; |
0a30fbc4 | 250 | |
3aee8918 | 251 | if (len != tdesc->registers_size * 2) |
0a30fbc4 | 252 | { |
1b3f6016 | 253 | warning ("Wrong sized register packet (expected %d bytes, got %d)", |
3aee8918 PA |
254 | 2 * tdesc->registers_size, len); |
255 | if (len > tdesc->registers_size * 2) | |
256 | len = tdesc->registers_size * 2; | |
0a30fbc4 | 257 | } |
a7191e8b | 258 | hex2bin (buf, registers, len / 2); |
0a30fbc4 DJ |
259 | } |
260 | ||
0a30fbc4 | 261 | int |
3aee8918 | 262 | find_regno (const struct target_desc *tdesc, const char *name) |
0a30fbc4 | 263 | { |
c4dfafab SDJ |
264 | for (int i = 0; i < tdesc->reg_defs.size (); ++i) |
265 | { | |
5cd3e386 | 266 | if (strcmp (name, find_register_by_number (tdesc, i).name) == 0) |
c4dfafab SDJ |
267 | return i; |
268 | } | |
38e08fca GB |
269 | internal_error (__FILE__, __LINE__, "Unknown register %s requested", |
270 | name); | |
0a30fbc4 DJ |
271 | } |
272 | ||
3aee8918 PA |
273 | static void |
274 | free_register_cache_thread (struct thread_info *thread) | |
275 | { | |
6afd337d | 276 | struct regcache *regcache = thread_regcache_data (thread); |
3aee8918 PA |
277 | |
278 | if (regcache != NULL) | |
279 | { | |
280 | regcache_invalidate_thread (thread); | |
281 | free_register_cache (regcache); | |
6afd337d | 282 | set_thread_regcache_data (thread, NULL); |
3aee8918 PA |
283 | } |
284 | } | |
285 | ||
3aee8918 PA |
286 | void |
287 | regcache_release (void) | |
288 | { | |
289 | /* Flush and release all pre-existing register caches. */ | |
f0045347 | 290 | for_each_thread (free_register_cache_thread); |
3aee8918 PA |
291 | } |
292 | #endif | |
293 | ||
0a30fbc4 | 294 | int |
3aee8918 | 295 | register_cache_size (const struct target_desc *tdesc) |
0a30fbc4 | 296 | { |
3aee8918 PA |
297 | return tdesc->registers_size; |
298 | } | |
299 | ||
300 | int | |
301 | register_size (const struct target_desc *tdesc, int n) | |
302 | { | |
5cd3e386 | 303 | return find_register_by_number (tdesc, n).size / 8; |
0a30fbc4 DJ |
304 | } |
305 | ||
8d689ee5 YQ |
306 | /* See common/common-regcache.h. */ |
307 | ||
308 | int | |
309 | regcache_register_size (const struct regcache *regcache, int n) | |
310 | { | |
311 | return register_size (regcache->tdesc, n); | |
312 | } | |
313 | ||
f450004a | 314 | static unsigned char * |
9c861883 | 315 | register_data (const struct regcache *regcache, int n, int fetch) |
0a30fbc4 | 316 | { |
f7000548 | 317 | return (regcache->registers |
5cd3e386 | 318 | + find_register_by_number (regcache->tdesc, n).offset / 8); |
0a30fbc4 DJ |
319 | } |
320 | ||
58caa3dc | 321 | void |
442ea881 | 322 | supply_register (struct regcache *regcache, int n, const void *buf) |
9c861883 AH |
323 | { |
324 | return regcache->raw_supply (n, buf); | |
325 | } | |
326 | ||
327 | /* See common/common-regcache.h. */ | |
328 | ||
329 | void | |
330 | regcache::raw_supply (int n, const void *buf) | |
58caa3dc | 331 | { |
3327ccf7 | 332 | if (buf) |
1c79eb8a | 333 | { |
9c861883 | 334 | memcpy (register_data (this, n, 0), buf, register_size (tdesc, n)); |
1c79eb8a | 335 | #ifndef IN_PROCESS_AGENT |
9c861883 AH |
336 | if (register_status != NULL) |
337 | register_status[n] = REG_VALID; | |
1c79eb8a PA |
338 | #endif |
339 | } | |
3327ccf7 | 340 | else |
1c79eb8a | 341 | { |
9c861883 | 342 | memset (register_data (this, n, 0), 0, register_size (tdesc, n)); |
1c79eb8a | 343 | #ifndef IN_PROCESS_AGENT |
9c861883 AH |
344 | if (register_status != NULL) |
345 | register_status[n] = REG_UNAVAILABLE; | |
1c79eb8a PA |
346 | #endif |
347 | } | |
58caa3dc DJ |
348 | } |
349 | ||
1c79eb8a PA |
350 | /* Supply register N with value zero to REGCACHE. */ |
351 | ||
352 | void | |
353 | supply_register_zeroed (struct regcache *regcache, int n) | |
354 | { | |
3aee8918 PA |
355 | memset (register_data (regcache, n, 0), 0, |
356 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
357 | #ifndef IN_PROCESS_AGENT |
358 | if (regcache->register_status != NULL) | |
359 | regcache->register_status[n] = REG_VALID; | |
360 | #endif | |
361 | } | |
362 | ||
8ee22052 AB |
363 | #ifndef IN_PROCESS_AGENT |
364 | ||
365 | /* Supply register called NAME with value zero to REGCACHE. */ | |
366 | ||
367 | void | |
368 | supply_register_by_name_zeroed (struct regcache *regcache, | |
369 | const char *name) | |
370 | { | |
371 | supply_register_zeroed (regcache, find_regno (regcache->tdesc, name)); | |
372 | } | |
373 | ||
374 | #endif | |
375 | ||
1c79eb8a PA |
376 | /* Supply the whole register set whose contents are stored in BUF, to |
377 | REGCACHE. If BUF is NULL, all the registers' values are recorded | |
378 | as unavailable. */ | |
379 | ||
219f2f23 PA |
380 | void |
381 | supply_regblock (struct regcache *regcache, const void *buf) | |
382 | { | |
383 | if (buf) | |
1c79eb8a | 384 | { |
3aee8918 PA |
385 | const struct target_desc *tdesc = regcache->tdesc; |
386 | ||
387 | memcpy (regcache->registers, buf, tdesc->registers_size); | |
1c79eb8a PA |
388 | #ifndef IN_PROCESS_AGENT |
389 | { | |
390 | int i; | |
391 | ||
c4dfafab | 392 | for (i = 0; i < tdesc->reg_defs.size (); i++) |
1c79eb8a PA |
393 | regcache->register_status[i] = REG_VALID; |
394 | } | |
395 | #endif | |
396 | } | |
219f2f23 | 397 | else |
1c79eb8a | 398 | { |
3aee8918 PA |
399 | const struct target_desc *tdesc = regcache->tdesc; |
400 | ||
401 | memset (regcache->registers, 0, tdesc->registers_size); | |
1c79eb8a PA |
402 | #ifndef IN_PROCESS_AGENT |
403 | { | |
404 | int i; | |
405 | ||
c4dfafab | 406 | for (i = 0; i < tdesc->reg_defs.size (); i++) |
1c79eb8a PA |
407 | regcache->register_status[i] = REG_UNAVAILABLE; |
408 | } | |
409 | #endif | |
410 | } | |
219f2f23 PA |
411 | } |
412 | ||
fa593d66 PA |
413 | #ifndef IN_PROCESS_AGENT |
414 | ||
58caa3dc | 415 | void |
442ea881 PA |
416 | supply_register_by_name (struct regcache *regcache, |
417 | const char *name, const void *buf) | |
58caa3dc | 418 | { |
3aee8918 | 419 | supply_register (regcache, find_regno (regcache->tdesc, name), buf); |
58caa3dc DJ |
420 | } |
421 | ||
fa593d66 PA |
422 | #endif |
423 | ||
58caa3dc | 424 | void |
442ea881 | 425 | collect_register (struct regcache *regcache, int n, void *buf) |
58caa3dc | 426 | { |
9c861883 AH |
427 | regcache->raw_collect (n, buf); |
428 | } | |
429 | ||
430 | /* See common/common-regcache.h. */ | |
431 | ||
432 | void | |
433 | regcache::raw_collect (int n, void *buf) const | |
434 | { | |
435 | memcpy (buf, register_data (this, n, 1), register_size (tdesc, n)); | |
0d62e5e8 DJ |
436 | } |
437 | ||
68ce2059 AT |
438 | enum register_status |
439 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, | |
440 | ULONGEST *val) | |
441 | { | |
442 | int size; | |
443 | ||
444 | gdb_assert (regcache != NULL); | |
f7000548 | 445 | gdb_assert (regnum >= 0 |
c4dfafab | 446 | && regnum < regcache->tdesc->reg_defs.size ()); |
68ce2059 AT |
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 | ||
9f6a71b4 | 455 | *val = 0; |
68ce2059 AT |
456 | collect_register (regcache, regnum, val); |
457 | ||
458 | return REG_VALID; | |
459 | } | |
460 | ||
fa593d66 PA |
461 | #ifndef IN_PROCESS_AGENT |
462 | ||
cb197132 PA |
463 | /* See regcache.h. */ |
464 | ||
465 | ULONGEST | |
466 | regcache_raw_get_unsigned_by_name (struct regcache *regcache, | |
467 | const char *name) | |
468 | { | |
469 | return regcache_raw_get_unsigned (regcache, | |
470 | find_regno (regcache->tdesc, name)); | |
471 | } | |
472 | ||
0d62e5e8 | 473 | void |
442ea881 | 474 | collect_register_as_string (struct regcache *regcache, int n, char *buf) |
0d62e5e8 | 475 | { |
e9371aff TT |
476 | bin2hex (register_data (regcache, n, 1), buf, |
477 | register_size (regcache->tdesc, n)); | |
58caa3dc DJ |
478 | } |
479 | ||
480 | void | |
442ea881 PA |
481 | collect_register_by_name (struct regcache *regcache, |
482 | const char *name, void *buf) | |
58caa3dc | 483 | { |
3aee8918 | 484 | collect_register (regcache, find_regno (regcache->tdesc, name), buf); |
58caa3dc | 485 | } |
219f2f23 PA |
486 | |
487 | /* Special handling for register PC. */ | |
488 | ||
489 | CORE_ADDR | |
490 | regcache_read_pc (struct regcache *regcache) | |
491 | { | |
492 | CORE_ADDR pc_val; | |
493 | ||
494 | if (the_target->read_pc) | |
495 | pc_val = the_target->read_pc (regcache); | |
496 | else | |
497 | internal_error (__FILE__, __LINE__, | |
498 | "regcache_read_pc: Unable to find PC"); | |
499 | ||
500 | return pc_val; | |
501 | } | |
502 | ||
503 | void | |
504 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) | |
505 | { | |
506 | if (the_target->write_pc) | |
507 | the_target->write_pc (regcache, pc); | |
508 | else | |
509 | internal_error (__FILE__, __LINE__, | |
510 | "regcache_write_pc: Unable to update PC"); | |
511 | } | |
fa593d66 PA |
512 | |
513 | #endif | |
9c861883 AH |
514 | |
515 | /* See common/common-regcache.h. */ | |
516 | ||
517 | enum register_status | |
518 | regcache::get_register_status (int regnum) const | |
519 | { | |
520 | #ifndef IN_PROCESS_AGENT | |
521 | gdb_assert (regnum >= 0 && regnum < tdesc->reg_defs.size ()); | |
522 | return (enum register_status) (register_status[regnum]); | |
523 | #else | |
524 | return REG_VALID; | |
525 | #endif | |
526 | } | |
f868386e AH |
527 | |
528 | /* See common/common-regcache.h. */ | |
529 | ||
530 | bool | |
531 | regcache::raw_compare (int regnum, const void *buf, int offset) const | |
532 | { | |
533 | gdb_assert (buf != NULL); | |
534 | ||
535 | const unsigned char *regbuf = register_data (this, regnum, 1); | |
536 | int size = register_size (tdesc, regnum); | |
537 | gdb_assert (size >= offset); | |
538 | ||
539 | return (memcmp (buf, regbuf + offset, size - offset) == 0); | |
540 | } |