Commit | Line | Data |
---|---|---|
0a30fbc4 | 1 | /* Register support routines for the remote server for GDB. |
32d0add0 | 2 | Copyright (C) 2001-2015 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 SM |
147 | regcache->register_status |
148 | = (unsigned char *) xcalloc (1, tdesc->num_registers); | |
1c79eb8a | 149 | gdb_assert (REG_UNAVAILABLE == 0); |
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 | { |
8d749320 | 174 | struct regcache *regcache = XCNEW (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); |
5822d809 PA |
189 | free (regcache); |
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 PA |
205 | memcpy (dst->register_status, src->register_status, |
206 | src->tdesc->num_registers); | |
1c79eb8a | 207 | #endif |
219f2f23 PA |
208 | dst->registers_valid = src->registers_valid; |
209 | } | |
210 | ||
219f2f23 | 211 | |
fa593d66 PA |
212 | #ifndef IN_PROCESS_AGENT |
213 | ||
0a30fbc4 | 214 | void |
442ea881 | 215 | registers_to_string (struct regcache *regcache, char *buf) |
0a30fbc4 | 216 | { |
442ea881 | 217 | unsigned char *registers = regcache->registers; |
3aee8918 | 218 | const struct target_desc *tdesc = regcache->tdesc; |
1c79eb8a | 219 | int i; |
c04a1aa8 | 220 | |
3aee8918 | 221 | for (i = 0; i < tdesc->num_registers; i++) |
1c79eb8a PA |
222 | { |
223 | if (regcache->register_status[i] == REG_VALID) | |
224 | { | |
e9371aff | 225 | bin2hex (registers, buf, register_size (tdesc, i)); |
3aee8918 | 226 | buf += register_size (tdesc, i) * 2; |
1c79eb8a PA |
227 | } |
228 | else | |
229 | { | |
3aee8918 PA |
230 | memset (buf, 'x', register_size (tdesc, i) * 2); |
231 | buf += register_size (tdesc, i) * 2; | |
1c79eb8a | 232 | } |
3aee8918 | 233 | registers += register_size (tdesc, i); |
1c79eb8a PA |
234 | } |
235 | *buf = '\0'; | |
0a30fbc4 DJ |
236 | } |
237 | ||
238 | void | |
442ea881 | 239 | registers_from_string (struct regcache *regcache, char *buf) |
0a30fbc4 DJ |
240 | { |
241 | int len = strlen (buf); | |
442ea881 | 242 | unsigned char *registers = regcache->registers; |
3aee8918 | 243 | const struct target_desc *tdesc = regcache->tdesc; |
0a30fbc4 | 244 | |
3aee8918 | 245 | if (len != tdesc->registers_size * 2) |
0a30fbc4 | 246 | { |
1b3f6016 | 247 | warning ("Wrong sized register packet (expected %d bytes, got %d)", |
3aee8918 PA |
248 | 2 * tdesc->registers_size, len); |
249 | if (len > tdesc->registers_size * 2) | |
250 | len = tdesc->registers_size * 2; | |
0a30fbc4 | 251 | } |
a7191e8b | 252 | hex2bin (buf, registers, len / 2); |
0a30fbc4 DJ |
253 | } |
254 | ||
255 | struct reg * | |
3aee8918 | 256 | find_register_by_name (const struct target_desc *tdesc, const char *name) |
0a30fbc4 DJ |
257 | { |
258 | int i; | |
259 | ||
3aee8918 PA |
260 | for (i = 0; i < tdesc->num_registers; i++) |
261 | if (strcmp (name, tdesc->reg_defs[i].name) == 0) | |
262 | return &tdesc->reg_defs[i]; | |
38e08fca GB |
263 | internal_error (__FILE__, __LINE__, "Unknown register %s requested", |
264 | name); | |
0a30fbc4 DJ |
265 | } |
266 | ||
267 | int | |
3aee8918 | 268 | find_regno (const struct target_desc *tdesc, const char *name) |
0a30fbc4 DJ |
269 | { |
270 | int i; | |
271 | ||
3aee8918 PA |
272 | for (i = 0; i < tdesc->num_registers; i++) |
273 | if (strcmp (name, tdesc->reg_defs[i].name) == 0) | |
0a30fbc4 | 274 | return i; |
38e08fca GB |
275 | internal_error (__FILE__, __LINE__, "Unknown register %s requested", |
276 | name); | |
0a30fbc4 DJ |
277 | } |
278 | ||
279 | struct reg * | |
3aee8918 | 280 | find_register_by_number (const struct target_desc *tdesc, int n) |
0a30fbc4 | 281 | { |
3aee8918 | 282 | return &tdesc->reg_defs[n]; |
0a30fbc4 DJ |
283 | } |
284 | ||
fa593d66 PA |
285 | #endif |
286 | ||
3aee8918 PA |
287 | #ifndef IN_PROCESS_AGENT |
288 | static void | |
289 | free_register_cache_thread (struct thread_info *thread) | |
290 | { | |
a44892be | 291 | struct regcache *regcache = inferior_regcache_data (thread); |
3aee8918 PA |
292 | |
293 | if (regcache != NULL) | |
294 | { | |
295 | regcache_invalidate_thread (thread); | |
296 | free_register_cache (regcache); | |
297 | set_inferior_regcache_data (thread, NULL); | |
298 | } | |
299 | } | |
300 | ||
301 | static void | |
302 | free_register_cache_thread_one (struct inferior_list_entry *entry) | |
303 | { | |
304 | struct thread_info *thread = (struct thread_info *) entry; | |
305 | ||
306 | free_register_cache_thread (thread); | |
307 | } | |
308 | ||
309 | void | |
310 | regcache_release (void) | |
311 | { | |
312 | /* Flush and release all pre-existing register caches. */ | |
313 | for_each_inferior (&all_threads, free_register_cache_thread_one); | |
314 | } | |
315 | #endif | |
316 | ||
0a30fbc4 | 317 | int |
3aee8918 | 318 | register_cache_size (const struct target_desc *tdesc) |
0a30fbc4 | 319 | { |
3aee8918 PA |
320 | return tdesc->registers_size; |
321 | } | |
322 | ||
323 | int | |
324 | register_size (const struct target_desc *tdesc, int n) | |
325 | { | |
326 | return tdesc->reg_defs[n].size / 8; | |
0a30fbc4 DJ |
327 | } |
328 | ||
8d689ee5 YQ |
329 | /* See common/common-regcache.h. */ |
330 | ||
331 | int | |
332 | regcache_register_size (const struct regcache *regcache, int n) | |
333 | { | |
334 | return register_size (regcache->tdesc, n); | |
335 | } | |
336 | ||
f450004a | 337 | static unsigned char * |
442ea881 | 338 | register_data (struct regcache *regcache, int n, int fetch) |
0a30fbc4 | 339 | { |
3aee8918 | 340 | return regcache->registers + regcache->tdesc->reg_defs[n].offset / 8; |
0a30fbc4 DJ |
341 | } |
342 | ||
1c79eb8a PA |
343 | /* Supply register N, whose contents are stored in BUF, to REGCACHE. |
344 | If BUF is NULL, the register's value is recorded as | |
345 | unavailable. */ | |
346 | ||
58caa3dc | 347 | void |
442ea881 | 348 | supply_register (struct regcache *regcache, int n, const void *buf) |
58caa3dc | 349 | { |
3327ccf7 | 350 | if (buf) |
1c79eb8a | 351 | { |
3aee8918 PA |
352 | memcpy (register_data (regcache, n, 0), buf, |
353 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
354 | #ifndef IN_PROCESS_AGENT |
355 | if (regcache->register_status != NULL) | |
356 | regcache->register_status[n] = REG_VALID; | |
357 | #endif | |
358 | } | |
3327ccf7 | 359 | else |
1c79eb8a | 360 | { |
3aee8918 PA |
361 | memset (register_data (regcache, n, 0), 0, |
362 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
363 | #ifndef IN_PROCESS_AGENT |
364 | if (regcache->register_status != NULL) | |
365 | regcache->register_status[n] = REG_UNAVAILABLE; | |
366 | #endif | |
367 | } | |
58caa3dc DJ |
368 | } |
369 | ||
1c79eb8a PA |
370 | /* Supply register N with value zero to REGCACHE. */ |
371 | ||
372 | void | |
373 | supply_register_zeroed (struct regcache *regcache, int n) | |
374 | { | |
3aee8918 PA |
375 | memset (register_data (regcache, n, 0), 0, |
376 | register_size (regcache->tdesc, n)); | |
1c79eb8a PA |
377 | #ifndef IN_PROCESS_AGENT |
378 | if (regcache->register_status != NULL) | |
379 | regcache->register_status[n] = REG_VALID; | |
380 | #endif | |
381 | } | |
382 | ||
383 | /* Supply the whole register set whose contents are stored in BUF, to | |
384 | REGCACHE. If BUF is NULL, all the registers' values are recorded | |
385 | as unavailable. */ | |
386 | ||
219f2f23 PA |
387 | void |
388 | supply_regblock (struct regcache *regcache, const void *buf) | |
389 | { | |
390 | if (buf) | |
1c79eb8a | 391 | { |
3aee8918 PA |
392 | const struct target_desc *tdesc = regcache->tdesc; |
393 | ||
394 | memcpy (regcache->registers, buf, tdesc->registers_size); | |
1c79eb8a PA |
395 | #ifndef IN_PROCESS_AGENT |
396 | { | |
397 | int i; | |
398 | ||
3aee8918 | 399 | for (i = 0; i < tdesc->num_registers; i++) |
1c79eb8a PA |
400 | regcache->register_status[i] = REG_VALID; |
401 | } | |
402 | #endif | |
403 | } | |
219f2f23 | 404 | else |
1c79eb8a | 405 | { |
3aee8918 PA |
406 | const struct target_desc *tdesc = regcache->tdesc; |
407 | ||
408 | memset (regcache->registers, 0, tdesc->registers_size); | |
1c79eb8a PA |
409 | #ifndef IN_PROCESS_AGENT |
410 | { | |
411 | int i; | |
412 | ||
3aee8918 | 413 | for (i = 0; i < tdesc->num_registers; i++) |
1c79eb8a PA |
414 | regcache->register_status[i] = REG_UNAVAILABLE; |
415 | } | |
416 | #endif | |
417 | } | |
219f2f23 PA |
418 | } |
419 | ||
fa593d66 PA |
420 | #ifndef IN_PROCESS_AGENT |
421 | ||
58caa3dc | 422 | void |
442ea881 PA |
423 | supply_register_by_name (struct regcache *regcache, |
424 | const char *name, const void *buf) | |
58caa3dc | 425 | { |
3aee8918 | 426 | supply_register (regcache, find_regno (regcache->tdesc, name), buf); |
58caa3dc DJ |
427 | } |
428 | ||
fa593d66 PA |
429 | #endif |
430 | ||
58caa3dc | 431 | void |
442ea881 | 432 | collect_register (struct regcache *regcache, int n, void *buf) |
58caa3dc | 433 | { |
3aee8918 PA |
434 | memcpy (buf, register_data (regcache, n, 1), |
435 | register_size (regcache->tdesc, n)); | |
0d62e5e8 DJ |
436 | } |
437 | ||
fa593d66 PA |
438 | #ifndef IN_PROCESS_AGENT |
439 | ||
0d62e5e8 | 440 | void |
442ea881 | 441 | collect_register_as_string (struct regcache *regcache, int n, char *buf) |
0d62e5e8 | 442 | { |
e9371aff TT |
443 | bin2hex (register_data (regcache, n, 1), buf, |
444 | register_size (regcache->tdesc, n)); | |
58caa3dc DJ |
445 | } |
446 | ||
447 | void | |
442ea881 PA |
448 | collect_register_by_name (struct regcache *regcache, |
449 | const char *name, void *buf) | |
58caa3dc | 450 | { |
3aee8918 | 451 | collect_register (regcache, find_regno (regcache->tdesc, name), buf); |
58caa3dc | 452 | } |
219f2f23 PA |
453 | |
454 | /* Special handling for register PC. */ | |
455 | ||
456 | CORE_ADDR | |
457 | regcache_read_pc (struct regcache *regcache) | |
458 | { | |
459 | CORE_ADDR pc_val; | |
460 | ||
461 | if (the_target->read_pc) | |
462 | pc_val = the_target->read_pc (regcache); | |
463 | else | |
464 | internal_error (__FILE__, __LINE__, | |
465 | "regcache_read_pc: Unable to find PC"); | |
466 | ||
467 | return pc_val; | |
468 | } | |
469 | ||
470 | void | |
471 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) | |
472 | { | |
473 | if (the_target->write_pc) | |
474 | the_target->write_pc (regcache, pc); | |
475 | else | |
476 | internal_error (__FILE__, __LINE__, | |
477 | "regcache_write_pc: Unable to update PC"); | |
478 | } | |
fa593d66 PA |
479 | |
480 | #endif |