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 | |
6afd337d | 31 | regcache = thread_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); | |
6afd337d | 47 | set_thread_regcache_data (thread, regcache); |
3aee8918 | 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 | 55 | /* Invalidate all registers, to prevent stale left-overs. */ |
c4dfafab SDJ |
56 | memset (regcache->register_status, REG_UNAVAILABLE, |
57 | regcache->tdesc->reg_defs.size ()); | |
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 | |
6afd337d | 79 | regcache = thread_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 | ||
80d82c19 JB |
96 | /* See regcache.h. */ |
97 | ||
98 | void | |
99 | regcache_invalidate_pid (int pid) | |
100 | { | |
634a3254 SM |
101 | /* Only invalidate the regcaches of threads of this process. */ |
102 | for_each_thread (pid, regcache_invalidate_thread); | |
80d82c19 JB |
103 | } |
104 | ||
105 | /* See regcache.h. */ | |
106 | ||
0d62e5e8 | 107 | void |
442ea881 | 108 | regcache_invalidate (void) |
0d62e5e8 | 109 | { |
3aee8918 | 110 | /* Only update the threads of the current process. */ |
9c80ecd6 | 111 | int pid = current_thread->id.pid (); |
3aee8918 | 112 | |
80d82c19 | 113 | regcache_invalidate_pid (pid); |
0d62e5e8 DJ |
114 | } |
115 | ||
fa593d66 PA |
116 | #endif |
117 | ||
219f2f23 | 118 | struct regcache * |
3aee8918 PA |
119 | init_register_cache (struct regcache *regcache, |
120 | const struct target_desc *tdesc, | |
121 | unsigned char *regbuf) | |
219f2f23 PA |
122 | { |
123 | if (regbuf == NULL) | |
124 | { | |
87f6c4e3 | 125 | #ifndef IN_PROCESS_AGENT |
219f2f23 PA |
126 | /* Make sure to zero-initialize the register cache when it is |
127 | created, in case there are registers the target never | |
128 | fetches. This way they'll read as zero instead of | |
129 | garbage. */ | |
3aee8918 | 130 | regcache->tdesc = tdesc; |
224c3ddb SM |
131 | regcache->registers |
132 | = (unsigned char *) xcalloc (1, tdesc->registers_size); | |
219f2f23 | 133 | regcache->registers_owned = 1; |
224c3ddb | 134 | regcache->register_status |
c4dfafab SDJ |
135 | = (unsigned char *) xmalloc (tdesc->reg_defs.size ()); |
136 | memset ((void *) regcache->register_status, REG_UNAVAILABLE, | |
137 | tdesc->reg_defs.size ()); | |
fa593d66 | 138 | #else |
38e08fca | 139 | gdb_assert_not_reached ("can't allocate memory from the heap"); |
fa593d66 | 140 | #endif |
87f6c4e3 GB |
141 | } |
142 | else | |
219f2f23 | 143 | { |
3aee8918 | 144 | regcache->tdesc = tdesc; |
219f2f23 PA |
145 | regcache->registers = regbuf; |
146 | regcache->registers_owned = 0; | |
1c79eb8a PA |
147 | #ifndef IN_PROCESS_AGENT |
148 | regcache->register_status = NULL; | |
149 | #endif | |
219f2f23 PA |
150 | } |
151 | ||
152 | regcache->registers_valid = 0; | |
153 | ||
154 | return regcache; | |
155 | } | |
156 | ||
fa593d66 PA |
157 | #ifndef IN_PROCESS_AGENT |
158 | ||
442ea881 | 159 | struct regcache * |
3aee8918 | 160 | new_register_cache (const struct target_desc *tdesc) |
c04a1aa8 | 161 | { |
8d749320 | 162 | struct regcache *regcache = XCNEW (struct regcache); |
c04a1aa8 | 163 | |
3aee8918 | 164 | gdb_assert (tdesc->registers_size != 0); |
5822d809 | 165 | |
3aee8918 | 166 | return init_register_cache (regcache, tdesc, NULL); |
c04a1aa8 DJ |
167 | } |
168 | ||
169 | void | |
442ea881 | 170 | free_register_cache (struct regcache *regcache) |
c04a1aa8 | 171 | { |
5822d809 PA |
172 | if (regcache) |
173 | { | |
fa593d66 PA |
174 | if (regcache->registers_owned) |
175 | free (regcache->registers); | |
1c79eb8a | 176 | free (regcache->register_status); |
5822d809 PA |
177 | free (regcache); |
178 | } | |
c04a1aa8 DJ |
179 | } |
180 | ||
fa593d66 PA |
181 | #endif |
182 | ||
219f2f23 PA |
183 | void |
184 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
185 | { | |
3aee8918 PA |
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); | |
1c79eb8a PA |
191 | #ifndef IN_PROCESS_AGENT |
192 | if (dst->register_status != NULL && src->register_status != NULL) | |
3aee8918 | 193 | memcpy (dst->register_status, src->register_status, |
c4dfafab | 194 | src->tdesc->reg_defs.size ()); |
1c79eb8a | 195 | #endif |
219f2f23 PA |
196 | dst->registers_valid = src->registers_valid; |
197 | } | |
198 | ||
219f2f23 | 199 | |
fa593d66 PA |
200 | #ifndef IN_PROCESS_AGENT |
201 | ||
0a30fbc4 | 202 | void |
442ea881 | 203 | registers_to_string (struct regcache *regcache, char *buf) |
0a30fbc4 | 204 | { |
442ea881 | 205 | unsigned char *registers = regcache->registers; |
3aee8918 | 206 | const struct target_desc *tdesc = regcache->tdesc; |
c04a1aa8 | 207 | |
c4dfafab | 208 | for (int i = 0; i < tdesc->reg_defs.size (); ++i) |
1c79eb8a PA |
209 | { |
210 | if (regcache->register_status[i] == REG_VALID) | |
211 | { | |
e9371aff | 212 | bin2hex (registers, buf, register_size (tdesc, i)); |
3aee8918 | 213 | buf += register_size (tdesc, i) * 2; |
1c79eb8a PA |
214 | } |
215 | else | |
216 | { | |
3aee8918 PA |
217 | memset (buf, 'x', register_size (tdesc, i) * 2); |
218 | buf += register_size (tdesc, i) * 2; | |
1c79eb8a | 219 | } |
3aee8918 | 220 | registers += register_size (tdesc, i); |
1c79eb8a PA |
221 | } |
222 | *buf = '\0'; | |
0a30fbc4 DJ |
223 | } |
224 | ||
225 | void | |
442ea881 | 226 | registers_from_string (struct regcache *regcache, char *buf) |
0a30fbc4 DJ |
227 | { |
228 | int len = strlen (buf); | |
442ea881 | 229 | unsigned char *registers = regcache->registers; |
3aee8918 | 230 | const struct target_desc *tdesc = regcache->tdesc; |
0a30fbc4 | 231 | |
3aee8918 | 232 | if (len != tdesc->registers_size * 2) |
0a30fbc4 | 233 | { |
1b3f6016 | 234 | warning ("Wrong sized register packet (expected %d bytes, got %d)", |
3aee8918 PA |
235 | 2 * tdesc->registers_size, len); |
236 | if (len > tdesc->registers_size * 2) | |
237 | len = tdesc->registers_size * 2; | |
0a30fbc4 | 238 | } |
a7191e8b | 239 | hex2bin (buf, registers, len / 2); |
0a30fbc4 DJ |
240 | } |
241 | ||
0a30fbc4 | 242 | int |
3aee8918 | 243 | find_regno (const struct target_desc *tdesc, const char *name) |
0a30fbc4 | 244 | { |
c4dfafab SDJ |
245 | for (int i = 0; i < tdesc->reg_defs.size (); ++i) |
246 | { | |
247 | struct reg *reg = tdesc->reg_defs[i]; | |
0a30fbc4 | 248 | |
c4dfafab SDJ |
249 | if (strcmp (name, reg->name) == 0) |
250 | return i; | |
251 | } | |
38e08fca GB |
252 | internal_error (__FILE__, __LINE__, "Unknown register %s requested", |
253 | name); | |
0a30fbc4 DJ |
254 | } |
255 | ||
f7000548 YQ |
256 | #endif |
257 | ||
0a30fbc4 | 258 | struct reg * |
3aee8918 | 259 | find_register_by_number (const struct target_desc *tdesc, int n) |
0a30fbc4 | 260 | { |
c4dfafab | 261 | return tdesc->reg_defs[n]; |
0a30fbc4 DJ |
262 | } |
263 | ||
3aee8918 PA |
264 | #ifndef IN_PROCESS_AGENT |
265 | static void | |
266 | free_register_cache_thread (struct thread_info *thread) | |
267 | { | |
6afd337d | 268 | struct regcache *regcache = thread_regcache_data (thread); |
3aee8918 PA |
269 | |
270 | if (regcache != NULL) | |
271 | { | |
272 | regcache_invalidate_thread (thread); | |
273 | free_register_cache (regcache); | |
6afd337d | 274 | set_thread_regcache_data (thread, NULL); |
3aee8918 PA |
275 | } |
276 | } | |
277 | ||
3aee8918 PA |
278 | void |
279 | regcache_release (void) | |
280 | { | |
281 | /* Flush and release all pre-existing register caches. */ | |
f0045347 | 282 | for_each_thread (free_register_cache_thread); |
3aee8918 PA |
283 | } |
284 | #endif | |
285 | ||
0a30fbc4 | 286 | int |
3aee8918 | 287 | register_cache_size (const struct target_desc *tdesc) |
0a30fbc4 | 288 | { |
3aee8918 PA |
289 | return tdesc->registers_size; |
290 | } | |
291 | ||
292 | int | |
293 | register_size (const struct target_desc *tdesc, int n) | |
294 | { | |
f7000548 | 295 | return find_register_by_number (tdesc, n)->size / 8; |
0a30fbc4 DJ |
296 | } |
297 | ||
8d689ee5 YQ |
298 | /* See common/common-regcache.h. */ |
299 | ||
300 | int | |
301 | regcache_register_size (const struct regcache *regcache, int n) | |
302 | { | |
303 | return register_size (regcache->tdesc, n); | |
304 | } | |
305 | ||
f450004a | 306 | static unsigned char * |
442ea881 | 307 | register_data (struct regcache *regcache, int n, int fetch) |
0a30fbc4 | 308 | { |
f7000548 YQ |
309 | return (regcache->registers |
310 | + find_register_by_number (regcache->tdesc, n)->offset / 8); | |
0a30fbc4 DJ |
311 | } |
312 | ||
1c79eb8a PA |
313 | /* Supply register N, whose contents are stored in BUF, to REGCACHE. |
314 | If BUF is NULL, the register's value is recorded as | |
315 | unavailable. */ | |
316 | ||
58caa3dc | 317 | void |
442ea881 | 318 | supply_register (struct regcache *regcache, int n, const void *buf) |
58caa3dc | 319 | { |
3327ccf7 | 320 | if (buf) |
1c79eb8a | 321 | { |
3aee8918 PA |
322 | memcpy (register_data (regcache, n, 0), buf, |
323 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
324 | #ifndef IN_PROCESS_AGENT |
325 | if (regcache->register_status != NULL) | |
326 | regcache->register_status[n] = REG_VALID; | |
327 | #endif | |
328 | } | |
3327ccf7 | 329 | else |
1c79eb8a | 330 | { |
3aee8918 PA |
331 | memset (register_data (regcache, n, 0), 0, |
332 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
333 | #ifndef IN_PROCESS_AGENT |
334 | if (regcache->register_status != NULL) | |
335 | regcache->register_status[n] = REG_UNAVAILABLE; | |
336 | #endif | |
337 | } | |
58caa3dc DJ |
338 | } |
339 | ||
1c79eb8a PA |
340 | /* Supply register N with value zero to REGCACHE. */ |
341 | ||
342 | void | |
343 | supply_register_zeroed (struct regcache *regcache, int n) | |
344 | { | |
3aee8918 PA |
345 | memset (register_data (regcache, n, 0), 0, |
346 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
347 | #ifndef IN_PROCESS_AGENT |
348 | if (regcache->register_status != NULL) | |
349 | regcache->register_status[n] = REG_VALID; | |
350 | #endif | |
351 | } | |
352 | ||
353 | /* Supply the whole register set whose contents are stored in BUF, to | |
354 | REGCACHE. If BUF is NULL, all the registers' values are recorded | |
355 | as unavailable. */ | |
356 | ||
219f2f23 PA |
357 | void |
358 | supply_regblock (struct regcache *regcache, const void *buf) | |
359 | { | |
360 | if (buf) | |
1c79eb8a | 361 | { |
3aee8918 PA |
362 | const struct target_desc *tdesc = regcache->tdesc; |
363 | ||
364 | memcpy (regcache->registers, buf, tdesc->registers_size); | |
1c79eb8a PA |
365 | #ifndef IN_PROCESS_AGENT |
366 | { | |
367 | int i; | |
368 | ||
c4dfafab | 369 | for (i = 0; i < tdesc->reg_defs.size (); i++) |
1c79eb8a PA |
370 | regcache->register_status[i] = REG_VALID; |
371 | } | |
372 | #endif | |
373 | } | |
219f2f23 | 374 | else |
1c79eb8a | 375 | { |
3aee8918 PA |
376 | const struct target_desc *tdesc = regcache->tdesc; |
377 | ||
378 | memset (regcache->registers, 0, tdesc->registers_size); | |
1c79eb8a PA |
379 | #ifndef IN_PROCESS_AGENT |
380 | { | |
381 | int i; | |
382 | ||
c4dfafab | 383 | for (i = 0; i < tdesc->reg_defs.size (); i++) |
1c79eb8a PA |
384 | regcache->register_status[i] = REG_UNAVAILABLE; |
385 | } | |
386 | #endif | |
387 | } | |
219f2f23 PA |
388 | } |
389 | ||
fa593d66 PA |
390 | #ifndef IN_PROCESS_AGENT |
391 | ||
58caa3dc | 392 | void |
442ea881 PA |
393 | supply_register_by_name (struct regcache *regcache, |
394 | const char *name, const void *buf) | |
58caa3dc | 395 | { |
3aee8918 | 396 | supply_register (regcache, find_regno (regcache->tdesc, name), buf); |
58caa3dc DJ |
397 | } |
398 | ||
fa593d66 PA |
399 | #endif |
400 | ||
58caa3dc | 401 | void |
442ea881 | 402 | collect_register (struct regcache *regcache, int n, void *buf) |
58caa3dc | 403 | { |
3aee8918 PA |
404 | memcpy (buf, register_data (regcache, n, 1), |
405 | register_size (regcache->tdesc, n)); | |
0d62e5e8 DJ |
406 | } |
407 | ||
68ce2059 AT |
408 | enum register_status |
409 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, | |
410 | ULONGEST *val) | |
411 | { | |
412 | int size; | |
413 | ||
414 | gdb_assert (regcache != NULL); | |
f7000548 | 415 | gdb_assert (regnum >= 0 |
c4dfafab | 416 | && regnum < regcache->tdesc->reg_defs.size ()); |
68ce2059 AT |
417 | |
418 | size = register_size (regcache->tdesc, regnum); | |
419 | ||
420 | if (size > (int) sizeof (ULONGEST)) | |
421 | error (_("That operation is not available on integers of more than" | |
422 | "%d bytes."), | |
423 | (int) sizeof (ULONGEST)); | |
424 | ||
9f6a71b4 | 425 | *val = 0; |
68ce2059 AT |
426 | collect_register (regcache, regnum, val); |
427 | ||
428 | return REG_VALID; | |
429 | } | |
430 | ||
fa593d66 PA |
431 | #ifndef IN_PROCESS_AGENT |
432 | ||
0d62e5e8 | 433 | void |
442ea881 | 434 | collect_register_as_string (struct regcache *regcache, int n, char *buf) |
0d62e5e8 | 435 | { |
e9371aff TT |
436 | bin2hex (register_data (regcache, n, 1), buf, |
437 | register_size (regcache->tdesc, n)); | |
58caa3dc DJ |
438 | } |
439 | ||
440 | void | |
442ea881 PA |
441 | collect_register_by_name (struct regcache *regcache, |
442 | const char *name, void *buf) | |
58caa3dc | 443 | { |
3aee8918 | 444 | collect_register (regcache, find_regno (regcache->tdesc, name), buf); |
58caa3dc | 445 | } |
219f2f23 PA |
446 | |
447 | /* Special handling for register PC. */ | |
448 | ||
449 | CORE_ADDR | |
450 | regcache_read_pc (struct regcache *regcache) | |
451 | { | |
452 | CORE_ADDR pc_val; | |
453 | ||
454 | if (the_target->read_pc) | |
455 | pc_val = the_target->read_pc (regcache); | |
456 | else | |
457 | internal_error (__FILE__, __LINE__, | |
458 | "regcache_read_pc: Unable to find PC"); | |
459 | ||
460 | return pc_val; | |
461 | } | |
462 | ||
463 | void | |
464 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) | |
465 | { | |
466 | if (the_target->write_pc) | |
467 | the_target->write_pc (regcache, pc); | |
468 | else | |
469 | internal_error (__FILE__, __LINE__, | |
470 | "regcache_write_pc: Unable to update PC"); | |
471 | } | |
fa593d66 PA |
472 | |
473 | #endif |