Commit | Line | Data |
---|---|---|
32178cab | 1 | /* Cache and manage the values of registers for GDB, the GNU debugger. |
3fadccb3 | 2 | |
28e7fd62 | 3 | Copyright (C) 1986-2013 Free Software Foundation, Inc. |
32178cab MS |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
32178cab MS |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
32178cab MS |
19 | |
20 | #include "defs.h" | |
32178cab MS |
21 | #include "inferior.h" |
22 | #include "target.h" | |
23 | #include "gdbarch.h" | |
705152c5 | 24 | #include "gdbcmd.h" |
4e052eda | 25 | #include "regcache.h" |
b59ff9d5 | 26 | #include "reggroups.h" |
61a0eb5b | 27 | #include "gdb_assert.h" |
b66d6d2e | 28 | #include "gdb_string.h" |
af030b9a | 29 | #include "gdbcmd.h" /* For maintenanceprintlist. */ |
f4c5303c | 30 | #include "observer.h" |
05d1431c | 31 | #include "exceptions.h" |
c21236dc | 32 | #include "remote.h" |
32178cab MS |
33 | |
34 | /* | |
35 | * DATA STRUCTURE | |
36 | * | |
37 | * Here is the actual register cache. | |
38 | */ | |
39 | ||
3fadccb3 | 40 | /* Per-architecture object describing the layout of a register cache. |
0df8b418 | 41 | Computed once when the architecture is created. */ |
3fadccb3 AC |
42 | |
43 | struct gdbarch_data *regcache_descr_handle; | |
44 | ||
45 | struct regcache_descr | |
46 | { | |
47 | /* The architecture this descriptor belongs to. */ | |
48 | struct gdbarch *gdbarch; | |
49 | ||
bb1db049 AC |
50 | /* The raw register cache. Each raw (or hard) register is supplied |
51 | by the target interface. The raw cache should not contain | |
52 | redundant information - if the PC is constructed from two | |
d2f0b918 | 53 | registers then those registers and not the PC lives in the raw |
bb1db049 | 54 | cache. */ |
3fadccb3 AC |
55 | int nr_raw_registers; |
56 | long sizeof_raw_registers; | |
ee99023e | 57 | long sizeof_raw_register_status; |
3fadccb3 | 58 | |
d138e37a AC |
59 | /* The cooked register space. Each cooked register in the range |
60 | [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw | |
61 | register. The remaining [NR_RAW_REGISTERS | |
02f60eae | 62 | .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto |
d138e37a | 63 | both raw registers and memory by the architecture methods |
02f60eae | 64 | gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */ |
d138e37a | 65 | int nr_cooked_registers; |
067df2e5 | 66 | long sizeof_cooked_registers; |
ee99023e | 67 | long sizeof_cooked_register_status; |
d138e37a | 68 | |
86d31898 | 69 | /* Offset and size (in 8 bit bytes), of each register in the |
d138e37a | 70 | register cache. All registers (including those in the range |
99e42fd8 PA |
71 | [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an |
72 | offset. */ | |
3fadccb3 | 73 | long *register_offset; |
3fadccb3 | 74 | long *sizeof_register; |
3fadccb3 | 75 | |
bb425013 AC |
76 | /* Cached table containing the type of each register. */ |
77 | struct type **register_type; | |
3fadccb3 AC |
78 | }; |
79 | ||
3fadccb3 AC |
80 | static void * |
81 | init_regcache_descr (struct gdbarch *gdbarch) | |
82 | { | |
83 | int i; | |
84 | struct regcache_descr *descr; | |
85 | gdb_assert (gdbarch != NULL); | |
86 | ||
bb425013 | 87 | /* Create an initial, zero filled, table. */ |
116f06ea | 88 | descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr); |
3fadccb3 | 89 | descr->gdbarch = gdbarch; |
3fadccb3 | 90 | |
d138e37a AC |
91 | /* Total size of the register space. The raw registers are mapped |
92 | directly onto the raw register cache while the pseudo's are | |
3fadccb3 | 93 | either mapped onto raw-registers or memory. */ |
214e098a UW |
94 | descr->nr_cooked_registers = gdbarch_num_regs (gdbarch) |
95 | + gdbarch_num_pseudo_regs (gdbarch); | |
ee99023e PA |
96 | descr->sizeof_cooked_register_status |
97 | = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); | |
3fadccb3 | 98 | |
bb425013 | 99 | /* Fill in a table of register types. */ |
116f06ea | 100 | descr->register_type |
3e43a32a MS |
101 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, |
102 | struct type *); | |
bb425013 | 103 | for (i = 0; i < descr->nr_cooked_registers; i++) |
336a3131 | 104 | descr->register_type[i] = gdbarch_register_type (gdbarch, i); |
bb425013 | 105 | |
bb1db049 AC |
106 | /* Construct a strictly RAW register cache. Don't allow pseudo's |
107 | into the register cache. */ | |
214e098a | 108 | descr->nr_raw_registers = gdbarch_num_regs (gdbarch); |
ee99023e | 109 | descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch); |
bb1db049 | 110 | |
067df2e5 | 111 | /* Lay out the register cache. |
3fadccb3 | 112 | |
bb425013 AC |
113 | NOTE: cagney/2002-05-22: Only register_type() is used when |
114 | constructing the register cache. It is assumed that the | |
115 | register's raw size, virtual size and type length are all the | |
116 | same. */ | |
3fadccb3 AC |
117 | |
118 | { | |
119 | long offset = 0; | |
123f5f96 | 120 | |
116f06ea AC |
121 | descr->sizeof_register |
122 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
123 | descr->register_offset | |
124 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); | |
99e42fd8 PA |
125 | for (i = 0; i < descr->nr_raw_registers; i++) |
126 | { | |
127 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); | |
128 | descr->register_offset[i] = offset; | |
129 | offset += descr->sizeof_register[i]; | |
130 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); | |
131 | } | |
132 | /* Set the real size of the raw register cache buffer. */ | |
133 | descr->sizeof_raw_registers = offset; | |
134 | ||
135 | for (; i < descr->nr_cooked_registers; i++) | |
3fadccb3 | 136 | { |
bb425013 | 137 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); |
3fadccb3 AC |
138 | descr->register_offset[i] = offset; |
139 | offset += descr->sizeof_register[i]; | |
123a958e | 140 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); |
3fadccb3 | 141 | } |
99e42fd8 | 142 | /* Set the real size of the readonly register cache buffer. */ |
067df2e5 | 143 | descr->sizeof_cooked_registers = offset; |
3fadccb3 AC |
144 | } |
145 | ||
3fadccb3 AC |
146 | return descr; |
147 | } | |
148 | ||
149 | static struct regcache_descr * | |
150 | regcache_descr (struct gdbarch *gdbarch) | |
151 | { | |
152 | return gdbarch_data (gdbarch, regcache_descr_handle); | |
153 | } | |
154 | ||
bb425013 AC |
155 | /* Utility functions returning useful register attributes stored in |
156 | the regcache descr. */ | |
157 | ||
158 | struct type * | |
159 | register_type (struct gdbarch *gdbarch, int regnum) | |
160 | { | |
161 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
123f5f96 | 162 | |
bb425013 AC |
163 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
164 | return descr->register_type[regnum]; | |
165 | } | |
166 | ||
0ed04cce AC |
167 | /* Utility functions returning useful register attributes stored in |
168 | the regcache descr. */ | |
169 | ||
08a617da AC |
170 | int |
171 | register_size (struct gdbarch *gdbarch, int regnum) | |
172 | { | |
173 | struct regcache_descr *descr = regcache_descr (gdbarch); | |
174 | int size; | |
123f5f96 | 175 | |
f57d151a | 176 | gdb_assert (regnum >= 0 |
214e098a UW |
177 | && regnum < (gdbarch_num_regs (gdbarch) |
178 | + gdbarch_num_pseudo_regs (gdbarch))); | |
08a617da | 179 | size = descr->sizeof_register[regnum]; |
08a617da AC |
180 | return size; |
181 | } | |
182 | ||
3fadccb3 AC |
183 | /* The register cache for storing raw register values. */ |
184 | ||
185 | struct regcache | |
186 | { | |
187 | struct regcache_descr *descr; | |
6c95b8df PA |
188 | |
189 | /* The address space of this register cache (for registers where it | |
190 | makes sense, like PC or SP). */ | |
191 | struct address_space *aspace; | |
192 | ||
51b1fe4e | 193 | /* The register buffers. A read-only register cache can hold the |
f57d151a UW |
194 | full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write |
195 | register cache can only hold [0 .. gdbarch_num_regs). */ | |
2d522557 | 196 | gdb_byte *registers; |
ee99023e PA |
197 | /* Register cache status. */ |
198 | signed char *register_status; | |
2d28509a AC |
199 | /* Is this a read-only cache? A read-only cache is used for saving |
200 | the target's register state (e.g, across an inferior function | |
201 | call or just before forcing a function return). A read-only | |
202 | cache can only be updated via the methods regcache_dup() and | |
203 | regcache_cpy(). The actual contents are determined by the | |
204 | reggroup_save and reggroup_restore methods. */ | |
205 | int readonly_p; | |
594f7785 UW |
206 | /* If this is a read-write cache, which thread's registers is |
207 | it connected to? */ | |
208 | ptid_t ptid; | |
3fadccb3 AC |
209 | }; |
210 | ||
99e42fd8 PA |
211 | static struct regcache * |
212 | regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace, | |
213 | int readonly_p) | |
3fadccb3 AC |
214 | { |
215 | struct regcache_descr *descr; | |
216 | struct regcache *regcache; | |
123f5f96 | 217 | |
3fadccb3 AC |
218 | gdb_assert (gdbarch != NULL); |
219 | descr = regcache_descr (gdbarch); | |
220 | regcache = XMALLOC (struct regcache); | |
221 | regcache->descr = descr; | |
99e42fd8 PA |
222 | regcache->readonly_p = readonly_p; |
223 | if (readonly_p) | |
224 | { | |
225 | regcache->registers | |
226 | = XCALLOC (descr->sizeof_cooked_registers, gdb_byte); | |
ee99023e PA |
227 | regcache->register_status |
228 | = XCALLOC (descr->sizeof_cooked_register_status, gdb_byte); | |
99e42fd8 PA |
229 | } |
230 | else | |
231 | { | |
232 | regcache->registers | |
233 | = XCALLOC (descr->sizeof_raw_registers, gdb_byte); | |
ee99023e PA |
234 | regcache->register_status |
235 | = XCALLOC (descr->sizeof_raw_register_status, gdb_byte); | |
99e42fd8 | 236 | } |
d37346f0 | 237 | regcache->aspace = aspace; |
594f7785 | 238 | regcache->ptid = minus_one_ptid; |
3fadccb3 AC |
239 | return regcache; |
240 | } | |
241 | ||
99e42fd8 PA |
242 | struct regcache * |
243 | regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace) | |
244 | { | |
245 | return regcache_xmalloc_1 (gdbarch, aspace, 1); | |
246 | } | |
247 | ||
3fadccb3 AC |
248 | void |
249 | regcache_xfree (struct regcache *regcache) | |
250 | { | |
251 | if (regcache == NULL) | |
252 | return; | |
51b1fe4e | 253 | xfree (regcache->registers); |
ee99023e | 254 | xfree (regcache->register_status); |
3fadccb3 AC |
255 | xfree (regcache); |
256 | } | |
257 | ||
b9362cc7 | 258 | static void |
36160dc4 AC |
259 | do_regcache_xfree (void *data) |
260 | { | |
261 | regcache_xfree (data); | |
262 | } | |
263 | ||
264 | struct cleanup * | |
265 | make_cleanup_regcache_xfree (struct regcache *regcache) | |
266 | { | |
267 | return make_cleanup (do_regcache_xfree, regcache); | |
268 | } | |
269 | ||
41d35cb0 MK |
270 | /* Return REGCACHE's architecture. */ |
271 | ||
272 | struct gdbarch * | |
273 | get_regcache_arch (const struct regcache *regcache) | |
274 | { | |
275 | return regcache->descr->gdbarch; | |
276 | } | |
277 | ||
6c95b8df PA |
278 | struct address_space * |
279 | get_regcache_aspace (const struct regcache *regcache) | |
280 | { | |
281 | return regcache->aspace; | |
282 | } | |
283 | ||
51b1fe4e AC |
284 | /* Return a pointer to register REGNUM's buffer cache. */ |
285 | ||
2d522557 | 286 | static gdb_byte * |
9a661b68 | 287 | register_buffer (const struct regcache *regcache, int regnum) |
51b1fe4e AC |
288 | { |
289 | return regcache->registers + regcache->descr->register_offset[regnum]; | |
290 | } | |
291 | ||
2d28509a | 292 | void |
5602984a AC |
293 | regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read, |
294 | void *src) | |
2d28509a AC |
295 | { |
296 | struct gdbarch *gdbarch = dst->descr->gdbarch; | |
2d522557 | 297 | gdb_byte buf[MAX_REGISTER_SIZE]; |
2d28509a | 298 | int regnum; |
123f5f96 | 299 | |
2d28509a | 300 | /* The DST should be `read-only', if it wasn't then the save would |
5602984a | 301 | end up trying to write the register values back out to the |
2d28509a | 302 | target. */ |
2d28509a AC |
303 | gdb_assert (dst->readonly_p); |
304 | /* Clear the dest. */ | |
305 | memset (dst->registers, 0, dst->descr->sizeof_cooked_registers); | |
ee99023e PA |
306 | memset (dst->register_status, 0, |
307 | dst->descr->sizeof_cooked_register_status); | |
2d28509a | 308 | /* Copy over any registers (identified by their membership in the |
f57d151a UW |
309 | save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs + |
310 | gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a | 311 | to save/restore `cooked' registers that live in memory. */ |
2d28509a AC |
312 | for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++) |
313 | { | |
314 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) | |
315 | { | |
05d1431c | 316 | enum register_status status = cooked_read (src, regnum, buf); |
123f5f96 | 317 | |
05d1431c PA |
318 | if (status == REG_VALID) |
319 | memcpy (register_buffer (dst, regnum), buf, | |
320 | register_size (gdbarch, regnum)); | |
321 | else | |
5602984a | 322 | { |
05d1431c PA |
323 | gdb_assert (status != REG_UNKNOWN); |
324 | ||
325 | memset (register_buffer (dst, regnum), 0, | |
5602984a | 326 | register_size (gdbarch, regnum)); |
5602984a | 327 | } |
05d1431c | 328 | dst->register_status[regnum] = status; |
2d28509a AC |
329 | } |
330 | } | |
331 | } | |
332 | ||
349d1385 | 333 | static void |
5602984a AC |
334 | regcache_restore (struct regcache *dst, |
335 | regcache_cooked_read_ftype *cooked_read, | |
2d522557 | 336 | void *cooked_read_context) |
2d28509a AC |
337 | { |
338 | struct gdbarch *gdbarch = dst->descr->gdbarch; | |
2d522557 | 339 | gdb_byte buf[MAX_REGISTER_SIZE]; |
2d28509a | 340 | int regnum; |
123f5f96 | 341 | |
5602984a AC |
342 | /* The dst had better not be read-only. If it is, the `restore' |
343 | doesn't make much sense. */ | |
2d28509a | 344 | gdb_assert (!dst->readonly_p); |
2d28509a | 345 | /* Copy over any registers, being careful to only restore those that |
f57d151a UW |
346 | were both saved and need to be restored. The full [0 .. gdbarch_num_regs |
347 | + gdbarch_num_pseudo_regs) range is checked since some architectures need | |
5602984a AC |
348 | to save/restore `cooked' registers that live in memory. */ |
349 | for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++) | |
2d28509a | 350 | { |
5602984a | 351 | if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)) |
2d28509a | 352 | { |
349d1385 | 353 | enum register_status status; |
123f5f96 | 354 | |
349d1385 DM |
355 | status = cooked_read (cooked_read_context, regnum, buf); |
356 | if (status == REG_VALID) | |
5602984a | 357 | regcache_cooked_write (dst, regnum, buf); |
2d28509a AC |
358 | } |
359 | } | |
360 | } | |
361 | ||
05d1431c | 362 | static enum register_status |
2d522557 | 363 | do_cooked_read (void *src, int regnum, gdb_byte *buf) |
5602984a AC |
364 | { |
365 | struct regcache *regcache = src; | |
123f5f96 | 366 | |
05d1431c | 367 | return regcache_cooked_read (regcache, regnum, buf); |
5602984a AC |
368 | } |
369 | ||
3fadccb3 AC |
370 | void |
371 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
372 | { | |
3fadccb3 AC |
373 | gdb_assert (src != NULL && dst != NULL); |
374 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
375 | gdb_assert (src != dst); | |
2d28509a | 376 | gdb_assert (src->readonly_p || dst->readonly_p); |
6c95b8df | 377 | |
2d28509a | 378 | if (!src->readonly_p) |
5602984a | 379 | regcache_save (dst, do_cooked_read, src); |
2d28509a | 380 | else if (!dst->readonly_p) |
5602984a | 381 | regcache_restore (dst, do_cooked_read, src); |
2d28509a AC |
382 | else |
383 | regcache_cpy_no_passthrough (dst, src); | |
3fadccb3 AC |
384 | } |
385 | ||
386 | void | |
387 | regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src) | |
388 | { | |
3fadccb3 AC |
389 | gdb_assert (src != NULL && dst != NULL); |
390 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
391 | /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough | |
ee99023e PA |
392 | move of data into a thread's regcache. Doing this would be silly |
393 | - it would mean that regcache->register_status would be | |
394 | completely invalid. */ | |
99e42fd8 | 395 | gdb_assert (dst->readonly_p && src->readonly_p); |
6c95b8df | 396 | |
99e42fd8 PA |
397 | memcpy (dst->registers, src->registers, |
398 | dst->descr->sizeof_cooked_registers); | |
ee99023e PA |
399 | memcpy (dst->register_status, src->register_status, |
400 | dst->descr->sizeof_cooked_register_status); | |
3fadccb3 AC |
401 | } |
402 | ||
403 | struct regcache * | |
404 | regcache_dup (struct regcache *src) | |
405 | { | |
406 | struct regcache *newbuf; | |
123f5f96 | 407 | |
d37346f0 | 408 | newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src)); |
3fadccb3 AC |
409 | regcache_cpy (newbuf, src); |
410 | return newbuf; | |
411 | } | |
412 | ||
39181896 | 413 | enum register_status |
ee99023e | 414 | regcache_register_status (const struct regcache *regcache, int regnum) |
3fadccb3 AC |
415 | { |
416 | gdb_assert (regcache != NULL); | |
6ed7ea50 UW |
417 | gdb_assert (regnum >= 0); |
418 | if (regcache->readonly_p) | |
419 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); | |
420 | else | |
421 | gdb_assert (regnum < regcache->descr->nr_raw_registers); | |
422 | ||
ee99023e | 423 | return regcache->register_status[regnum]; |
3fadccb3 AC |
424 | } |
425 | ||
9c5ea4d9 UW |
426 | void |
427 | regcache_invalidate (struct regcache *regcache, int regnum) | |
428 | { | |
429 | gdb_assert (regcache != NULL); | |
430 | gdb_assert (regnum >= 0); | |
431 | gdb_assert (!regcache->readonly_p); | |
432 | gdb_assert (regnum < regcache->descr->nr_raw_registers); | |
ee99023e | 433 | regcache->register_status[regnum] = REG_UNKNOWN; |
9c5ea4d9 UW |
434 | } |
435 | ||
436 | ||
3fadccb3 | 437 | /* Global structure containing the current regcache. */ |
3fadccb3 | 438 | |
5ebd2499 | 439 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
32178cab MS |
440 | recording if the register values have been changed (eg. by the |
441 | user). Therefore all registers must be written back to the | |
442 | target when appropriate. */ | |
443 | ||
c2250ad1 | 444 | struct regcache_list |
594f7785 | 445 | { |
c2250ad1 UW |
446 | struct regcache *regcache; |
447 | struct regcache_list *next; | |
448 | }; | |
449 | ||
450 | static struct regcache_list *current_regcache; | |
451 | ||
452 | struct regcache * | |
e2d96639 YQ |
453 | get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch, |
454 | struct address_space *aspace) | |
c2250ad1 UW |
455 | { |
456 | struct regcache_list *list; | |
457 | struct regcache *new_regcache; | |
594f7785 | 458 | |
c2250ad1 UW |
459 | for (list = current_regcache; list; list = list->next) |
460 | if (ptid_equal (list->regcache->ptid, ptid) | |
461 | && get_regcache_arch (list->regcache) == gdbarch) | |
462 | return list->regcache; | |
594f7785 | 463 | |
e2d96639 YQ |
464 | new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0); |
465 | new_regcache->ptid = ptid; | |
466 | ||
467 | list = xmalloc (sizeof (struct regcache_list)); | |
468 | list->regcache = new_regcache; | |
469 | list->next = current_regcache; | |
470 | current_regcache = list; | |
471 | ||
472 | return new_regcache; | |
473 | } | |
474 | ||
475 | struct regcache * | |
476 | get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch) | |
477 | { | |
478 | struct address_space *aspace; | |
479 | ||
b78974c3 PA |
480 | /* For the benefit of "maint print registers" & co when debugging an |
481 | executable, allow dumping the regcache even when there is no | |
482 | thread selected (target_thread_address_space internal-errors if | |
483 | no address space is found). Note that normal user commands will | |
484 | fail higher up on the call stack due to no | |
485 | target_has_registers. */ | |
486 | aspace = (ptid_equal (null_ptid, ptid) | |
487 | ? NULL | |
488 | : target_thread_address_space (ptid)); | |
489 | ||
e2d96639 | 490 | return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace); |
594f7785 UW |
491 | } |
492 | ||
c2250ad1 UW |
493 | static ptid_t current_thread_ptid; |
494 | static struct gdbarch *current_thread_arch; | |
495 | ||
496 | struct regcache * | |
497 | get_thread_regcache (ptid_t ptid) | |
498 | { | |
499 | if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid)) | |
500 | { | |
501 | current_thread_ptid = ptid; | |
502 | current_thread_arch = target_thread_architecture (ptid); | |
503 | } | |
504 | ||
505 | return get_thread_arch_regcache (ptid, current_thread_arch); | |
506 | } | |
507 | ||
508 | struct regcache * | |
509 | get_current_regcache (void) | |
594f7785 UW |
510 | { |
511 | return get_thread_regcache (inferior_ptid); | |
512 | } | |
32178cab | 513 | |
32178cab | 514 | |
f4c5303c OF |
515 | /* Observer for the target_changed event. */ |
516 | ||
2c0b251b | 517 | static void |
f4c5303c OF |
518 | regcache_observer_target_changed (struct target_ops *target) |
519 | { | |
520 | registers_changed (); | |
521 | } | |
522 | ||
5231c1fd PA |
523 | /* Update global variables old ptids to hold NEW_PTID if they were |
524 | holding OLD_PTID. */ | |
525 | static void | |
526 | regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) | |
527 | { | |
c2250ad1 UW |
528 | struct regcache_list *list; |
529 | ||
530 | for (list = current_regcache; list; list = list->next) | |
531 | if (ptid_equal (list->regcache->ptid, old_ptid)) | |
532 | list->regcache->ptid = new_ptid; | |
5231c1fd PA |
533 | } |
534 | ||
32178cab MS |
535 | /* Low level examining and depositing of registers. |
536 | ||
537 | The caller is responsible for making sure that the inferior is | |
538 | stopped before calling the fetching routines, or it will get | |
539 | garbage. (a change from GDB version 3, in which the caller got the | |
540 | value from the last stop). */ | |
541 | ||
542 | /* REGISTERS_CHANGED () | |
543 | ||
544 | Indicate that registers may have changed, so invalidate the cache. */ | |
545 | ||
546 | void | |
e66408ed | 547 | registers_changed_ptid (ptid_t ptid) |
32178cab | 548 | { |
e66408ed | 549 | struct regcache_list *list, **list_link; |
c2250ad1 | 550 | |
e66408ed PA |
551 | list = current_regcache; |
552 | list_link = ¤t_regcache; | |
553 | while (list) | |
c2250ad1 | 554 | { |
e66408ed PA |
555 | if (ptid_match (list->regcache->ptid, ptid)) |
556 | { | |
557 | struct regcache_list *dead = list; | |
558 | ||
559 | *list_link = list->next; | |
560 | regcache_xfree (list->regcache); | |
561 | list = *list_link; | |
562 | xfree (dead); | |
563 | continue; | |
564 | } | |
565 | ||
566 | list_link = &list->next; | |
567 | list = *list_link; | |
c2250ad1 | 568 | } |
32178cab | 569 | |
c34fd852 | 570 | if (ptid_match (current_thread_ptid, ptid)) |
041274d8 PA |
571 | { |
572 | current_thread_ptid = null_ptid; | |
573 | current_thread_arch = NULL; | |
574 | } | |
32178cab | 575 | |
c34fd852 | 576 | if (ptid_match (inferior_ptid, ptid)) |
041274d8 PA |
577 | { |
578 | /* We just deleted the regcache of the current thread. Need to | |
579 | forget about any frames we have cached, too. */ | |
580 | reinit_frame_cache (); | |
581 | } | |
582 | } | |
c2250ad1 | 583 | |
041274d8 PA |
584 | void |
585 | registers_changed (void) | |
586 | { | |
587 | registers_changed_ptid (minus_one_ptid); | |
a5d9d57d | 588 | |
32178cab MS |
589 | /* Force cleanup of any alloca areas if using C alloca instead of |
590 | a builtin alloca. This particular call is used to clean up | |
591 | areas allocated by low level target code which may build up | |
592 | during lengthy interactions between gdb and the target before | |
593 | gdb gives control to the user (ie watchpoints). */ | |
594 | alloca (0); | |
32178cab MS |
595 | } |
596 | ||
05d1431c | 597 | enum register_status |
2d522557 | 598 | regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
61a0eb5b | 599 | { |
3fadccb3 AC |
600 | gdb_assert (regcache != NULL && buf != NULL); |
601 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
3fadccb3 AC |
602 | /* Make certain that the register cache is up-to-date with respect |
603 | to the current thread. This switching shouldn't be necessary | |
604 | only there is still only one target side register cache. Sigh! | |
605 | On the bright side, at least there is a regcache object. */ | |
788c8b10 PA |
606 | if (!regcache->readonly_p |
607 | && regcache_register_status (regcache, regnum) == REG_UNKNOWN) | |
3fadccb3 | 608 | { |
788c8b10 | 609 | struct cleanup *old_chain = save_inferior_ptid (); |
123f5f96 | 610 | |
788c8b10 PA |
611 | inferior_ptid = regcache->ptid; |
612 | target_fetch_registers (regcache, regnum); | |
613 | do_cleanups (old_chain); | |
614 | ||
615 | /* A number of targets can't access the whole set of raw | |
616 | registers (because the debug API provides no means to get at | |
617 | them). */ | |
618 | if (regcache->register_status[regnum] == REG_UNKNOWN) | |
619 | regcache->register_status[regnum] = REG_UNAVAILABLE; | |
3fadccb3 | 620 | } |
05d1431c PA |
621 | |
622 | if (regcache->register_status[regnum] != REG_VALID) | |
623 | memset (buf, 0, regcache->descr->sizeof_register[regnum]); | |
624 | else | |
625 | memcpy (buf, register_buffer (regcache, regnum), | |
626 | regcache->descr->sizeof_register[regnum]); | |
627 | ||
628 | return regcache->register_status[regnum]; | |
61a0eb5b AC |
629 | } |
630 | ||
05d1431c | 631 | enum register_status |
28fc6740 AC |
632 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) |
633 | { | |
2d522557 | 634 | gdb_byte *buf; |
05d1431c | 635 | enum register_status status; |
123f5f96 | 636 | |
28fc6740 AC |
637 | gdb_assert (regcache != NULL); |
638 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
639 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
05d1431c PA |
640 | status = regcache_raw_read (regcache, regnum, buf); |
641 | if (status == REG_VALID) | |
642 | *val = extract_signed_integer | |
643 | (buf, regcache->descr->sizeof_register[regnum], | |
644 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
645 | else | |
646 | *val = 0; | |
647 | return status; | |
28fc6740 AC |
648 | } |
649 | ||
05d1431c | 650 | enum register_status |
28fc6740 AC |
651 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, |
652 | ULONGEST *val) | |
653 | { | |
2d522557 | 654 | gdb_byte *buf; |
05d1431c | 655 | enum register_status status; |
123f5f96 | 656 | |
28fc6740 AC |
657 | gdb_assert (regcache != NULL); |
658 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
659 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
05d1431c PA |
660 | status = regcache_raw_read (regcache, regnum, buf); |
661 | if (status == REG_VALID) | |
662 | *val = extract_unsigned_integer | |
663 | (buf, regcache->descr->sizeof_register[regnum], | |
664 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
665 | else | |
666 | *val = 0; | |
667 | return status; | |
28fc6740 AC |
668 | } |
669 | ||
c00dcbe9 MK |
670 | void |
671 | regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) | |
672 | { | |
673 | void *buf; | |
123f5f96 | 674 | |
c00dcbe9 MK |
675 | gdb_assert (regcache != NULL); |
676 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
677 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
678 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], |
679 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
c00dcbe9 MK |
680 | regcache_raw_write (regcache, regnum, buf); |
681 | } | |
682 | ||
683 | void | |
684 | regcache_raw_write_unsigned (struct regcache *regcache, int regnum, | |
685 | ULONGEST val) | |
686 | { | |
687 | void *buf; | |
123f5f96 | 688 | |
c00dcbe9 MK |
689 | gdb_assert (regcache != NULL); |
690 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
691 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
692 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], |
693 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
c00dcbe9 MK |
694 | regcache_raw_write (regcache, regnum, buf); |
695 | } | |
696 | ||
05d1431c | 697 | enum register_status |
2d522557 | 698 | regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
68365089 | 699 | { |
d138e37a | 700 | gdb_assert (regnum >= 0); |
68365089 AC |
701 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
702 | if (regnum < regcache->descr->nr_raw_registers) | |
05d1431c | 703 | return regcache_raw_read (regcache, regnum, buf); |
2d28509a | 704 | else if (regcache->readonly_p |
05d1431c PA |
705 | && regcache->register_status[regnum] != REG_UNKNOWN) |
706 | { | |
707 | /* Read-only register cache, perhaps the cooked value was | |
708 | cached? */ | |
709 | struct gdbarch *gdbarch = regcache->descr->gdbarch; | |
710 | ||
711 | if (regcache->register_status[regnum] == REG_VALID) | |
712 | memcpy (buf, register_buffer (regcache, regnum), | |
713 | regcache->descr->sizeof_register[regnum]); | |
714 | else | |
715 | memset (buf, 0, regcache->descr->sizeof_register[regnum]); | |
716 | ||
717 | return regcache->register_status[regnum]; | |
718 | } | |
3543a589 TT |
719 | else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch)) |
720 | { | |
721 | struct value *mark, *computed; | |
722 | enum register_status result = REG_VALID; | |
723 | ||
724 | mark = value_mark (); | |
725 | ||
726 | computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch, | |
727 | regcache, regnum); | |
728 | if (value_entirely_available (computed)) | |
729 | memcpy (buf, value_contents_raw (computed), | |
730 | regcache->descr->sizeof_register[regnum]); | |
731 | else | |
732 | { | |
733 | memset (buf, 0, regcache->descr->sizeof_register[regnum]); | |
734 | result = REG_UNAVAILABLE; | |
735 | } | |
736 | ||
737 | value_free_to_mark (mark); | |
738 | ||
739 | return result; | |
740 | } | |
d138e37a | 741 | else |
05d1431c PA |
742 | return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache, |
743 | regnum, buf); | |
61a0eb5b AC |
744 | } |
745 | ||
3543a589 TT |
746 | struct value * |
747 | regcache_cooked_read_value (struct regcache *regcache, int regnum) | |
748 | { | |
749 | gdb_assert (regnum >= 0); | |
750 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); | |
751 | ||
752 | if (regnum < regcache->descr->nr_raw_registers | |
753 | || (regcache->readonly_p | |
754 | && regcache->register_status[regnum] != REG_UNKNOWN) | |
755 | || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch)) | |
756 | { | |
757 | struct value *result; | |
758 | ||
759 | result = allocate_value (register_type (regcache->descr->gdbarch, | |
760 | regnum)); | |
761 | VALUE_LVAL (result) = lval_register; | |
762 | VALUE_REGNUM (result) = regnum; | |
763 | ||
764 | /* It is more efficient in general to do this delegation in this | |
765 | direction than in the other one, even though the value-based | |
766 | API is preferred. */ | |
767 | if (regcache_cooked_read (regcache, regnum, | |
768 | value_contents_raw (result)) == REG_UNAVAILABLE) | |
769 | mark_value_bytes_unavailable (result, 0, | |
770 | TYPE_LENGTH (value_type (result))); | |
771 | ||
772 | return result; | |
773 | } | |
774 | else | |
775 | return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch, | |
776 | regcache, regnum); | |
777 | } | |
778 | ||
05d1431c | 779 | enum register_status |
a378f419 AC |
780 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, |
781 | LONGEST *val) | |
782 | { | |
05d1431c | 783 | enum register_status status; |
2d522557 | 784 | gdb_byte *buf; |
123f5f96 | 785 | |
a378f419 | 786 | gdb_assert (regcache != NULL); |
a66a9c23 | 787 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 | 788 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
05d1431c PA |
789 | status = regcache_cooked_read (regcache, regnum, buf); |
790 | if (status == REG_VALID) | |
791 | *val = extract_signed_integer | |
792 | (buf, regcache->descr->sizeof_register[regnum], | |
793 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
794 | else | |
795 | *val = 0; | |
796 | return status; | |
a378f419 AC |
797 | } |
798 | ||
05d1431c | 799 | enum register_status |
a378f419 AC |
800 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, |
801 | ULONGEST *val) | |
802 | { | |
05d1431c | 803 | enum register_status status; |
2d522557 | 804 | gdb_byte *buf; |
123f5f96 | 805 | |
a378f419 | 806 | gdb_assert (regcache != NULL); |
a66a9c23 | 807 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 | 808 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
05d1431c PA |
809 | status = regcache_cooked_read (regcache, regnum, buf); |
810 | if (status == REG_VALID) | |
811 | *val = extract_unsigned_integer | |
812 | (buf, regcache->descr->sizeof_register[regnum], | |
813 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
814 | else | |
815 | *val = 0; | |
816 | return status; | |
a378f419 AC |
817 | } |
818 | ||
a66a9c23 AC |
819 | void |
820 | regcache_cooked_write_signed (struct regcache *regcache, int regnum, | |
821 | LONGEST val) | |
822 | { | |
823 | void *buf; | |
123f5f96 | 824 | |
a66a9c23 AC |
825 | gdb_assert (regcache != NULL); |
826 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
827 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
828 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], |
829 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
a66a9c23 AC |
830 | regcache_cooked_write (regcache, regnum, buf); |
831 | } | |
832 | ||
833 | void | |
834 | regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, | |
835 | ULONGEST val) | |
836 | { | |
837 | void *buf; | |
123f5f96 | 838 | |
a66a9c23 AC |
839 | gdb_assert (regcache != NULL); |
840 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
841 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
842 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], |
843 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
a66a9c23 AC |
844 | regcache_cooked_write (regcache, regnum, buf); |
845 | } | |
846 | ||
61a0eb5b | 847 | void |
2d522557 AC |
848 | regcache_raw_write (struct regcache *regcache, int regnum, |
849 | const gdb_byte *buf) | |
61a0eb5b | 850 | { |
594f7785 UW |
851 | struct cleanup *old_chain; |
852 | ||
3fadccb3 AC |
853 | gdb_assert (regcache != NULL && buf != NULL); |
854 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
2d28509a | 855 | gdb_assert (!regcache->readonly_p); |
3fadccb3 | 856 | |
3fadccb3 AC |
857 | /* On the sparc, writing %g0 is a no-op, so we don't even want to |
858 | change the registers array if something writes to this register. */ | |
214e098a | 859 | if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum)) |
3fadccb3 AC |
860 | return; |
861 | ||
3fadccb3 | 862 | /* If we have a valid copy of the register, and new value == old |
0df8b418 | 863 | value, then don't bother doing the actual store. */ |
ee99023e | 864 | if (regcache_register_status (regcache, regnum) == REG_VALID |
3fadccb3 AC |
865 | && (memcmp (register_buffer (regcache, regnum), buf, |
866 | regcache->descr->sizeof_register[regnum]) == 0)) | |
867 | return; | |
868 | ||
594f7785 UW |
869 | old_chain = save_inferior_ptid (); |
870 | inferior_ptid = regcache->ptid; | |
871 | ||
316f2060 | 872 | target_prepare_to_store (regcache); |
3fadccb3 AC |
873 | memcpy (register_buffer (regcache, regnum), buf, |
874 | regcache->descr->sizeof_register[regnum]); | |
ee99023e | 875 | regcache->register_status[regnum] = REG_VALID; |
56be3814 | 876 | target_store_registers (regcache, regnum); |
594f7785 UW |
877 | |
878 | do_cleanups (old_chain); | |
61a0eb5b AC |
879 | } |
880 | ||
68365089 | 881 | void |
2d522557 AC |
882 | regcache_cooked_write (struct regcache *regcache, int regnum, |
883 | const gdb_byte *buf) | |
68365089 | 884 | { |
d138e37a | 885 | gdb_assert (regnum >= 0); |
68365089 AC |
886 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
887 | if (regnum < regcache->descr->nr_raw_registers) | |
888 | regcache_raw_write (regcache, regnum, buf); | |
d138e37a | 889 | else |
68365089 | 890 | gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache, |
d8124050 | 891 | regnum, buf); |
61a0eb5b AC |
892 | } |
893 | ||
06c0b04e AC |
894 | /* Perform a partial register transfer using a read, modify, write |
895 | operation. */ | |
896 | ||
897 | typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum, | |
898 | void *buf); | |
899 | typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum, | |
900 | const void *buf); | |
901 | ||
05d1431c | 902 | static enum register_status |
06c0b04e AC |
903 | regcache_xfer_part (struct regcache *regcache, int regnum, |
904 | int offset, int len, void *in, const void *out, | |
05d1431c PA |
905 | enum register_status (*read) (struct regcache *regcache, |
906 | int regnum, | |
907 | gdb_byte *buf), | |
2d522557 AC |
908 | void (*write) (struct regcache *regcache, int regnum, |
909 | const gdb_byte *buf)) | |
06c0b04e AC |
910 | { |
911 | struct regcache_descr *descr = regcache->descr; | |
fc1a4b47 | 912 | gdb_byte reg[MAX_REGISTER_SIZE]; |
123f5f96 | 913 | |
06c0b04e AC |
914 | gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]); |
915 | gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]); | |
916 | /* Something to do? */ | |
917 | if (offset + len == 0) | |
05d1431c | 918 | return REG_VALID; |
0df8b418 | 919 | /* Read (when needed) ... */ |
06c0b04e AC |
920 | if (in != NULL |
921 | || offset > 0 | |
922 | || offset + len < descr->sizeof_register[regnum]) | |
923 | { | |
05d1431c PA |
924 | enum register_status status; |
925 | ||
06c0b04e | 926 | gdb_assert (read != NULL); |
05d1431c PA |
927 | status = read (regcache, regnum, reg); |
928 | if (status != REG_VALID) | |
929 | return status; | |
06c0b04e | 930 | } |
0df8b418 | 931 | /* ... modify ... */ |
06c0b04e AC |
932 | if (in != NULL) |
933 | memcpy (in, reg + offset, len); | |
934 | if (out != NULL) | |
935 | memcpy (reg + offset, out, len); | |
936 | /* ... write (when needed). */ | |
937 | if (out != NULL) | |
938 | { | |
939 | gdb_assert (write != NULL); | |
940 | write (regcache, regnum, reg); | |
941 | } | |
05d1431c PA |
942 | |
943 | return REG_VALID; | |
06c0b04e AC |
944 | } |
945 | ||
05d1431c | 946 | enum register_status |
06c0b04e | 947 | regcache_raw_read_part (struct regcache *regcache, int regnum, |
2d522557 | 948 | int offset, int len, gdb_byte *buf) |
06c0b04e AC |
949 | { |
950 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 951 | |
06c0b04e | 952 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); |
05d1431c PA |
953 | return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, |
954 | regcache_raw_read, regcache_raw_write); | |
06c0b04e AC |
955 | } |
956 | ||
957 | void | |
958 | regcache_raw_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 959 | int offset, int len, const gdb_byte *buf) |
06c0b04e AC |
960 | { |
961 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 962 | |
06c0b04e AC |
963 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); |
964 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
965 | regcache_raw_read, regcache_raw_write); | |
966 | } | |
967 | ||
05d1431c | 968 | enum register_status |
06c0b04e | 969 | regcache_cooked_read_part (struct regcache *regcache, int regnum, |
2d522557 | 970 | int offset, int len, gdb_byte *buf) |
06c0b04e AC |
971 | { |
972 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 973 | |
06c0b04e | 974 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
05d1431c PA |
975 | return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, |
976 | regcache_cooked_read, regcache_cooked_write); | |
06c0b04e AC |
977 | } |
978 | ||
979 | void | |
980 | regcache_cooked_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 981 | int offset, int len, const gdb_byte *buf) |
06c0b04e AC |
982 | { |
983 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 984 | |
06c0b04e AC |
985 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
986 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
987 | regcache_cooked_read, regcache_cooked_write); | |
988 | } | |
32178cab | 989 | |
a16d75cc | 990 | /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */ |
9a661b68 MK |
991 | |
992 | void | |
6618125d | 993 | regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf) |
9a661b68 MK |
994 | { |
995 | void *regbuf; | |
996 | size_t size; | |
997 | ||
a16d75cc | 998 | gdb_assert (regcache != NULL); |
9a661b68 MK |
999 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); |
1000 | gdb_assert (!regcache->readonly_p); | |
1001 | ||
9a661b68 MK |
1002 | regbuf = register_buffer (regcache, regnum); |
1003 | size = regcache->descr->sizeof_register[regnum]; | |
1004 | ||
1005 | if (buf) | |
ee99023e PA |
1006 | { |
1007 | memcpy (regbuf, buf, size); | |
1008 | regcache->register_status[regnum] = REG_VALID; | |
1009 | } | |
9a661b68 | 1010 | else |
ee99023e PA |
1011 | { |
1012 | /* This memset not strictly necessary, but better than garbage | |
1013 | in case the register value manages to escape somewhere (due | |
1014 | to a bug, no less). */ | |
1015 | memset (regbuf, 0, size); | |
1016 | regcache->register_status[regnum] = REG_UNAVAILABLE; | |
1017 | } | |
9a661b68 MK |
1018 | } |
1019 | ||
1020 | /* Collect register REGNUM from REGCACHE and store its contents in BUF. */ | |
1021 | ||
1022 | void | |
6618125d | 1023 | regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf) |
9a661b68 MK |
1024 | { |
1025 | const void *regbuf; | |
1026 | size_t size; | |
1027 | ||
1028 | gdb_assert (regcache != NULL && buf != NULL); | |
1029 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
1030 | ||
1031 | regbuf = register_buffer (regcache, regnum); | |
1032 | size = regcache->descr->sizeof_register[regnum]; | |
1033 | memcpy (buf, regbuf, size); | |
1034 | } | |
1035 | ||
193cb69f | 1036 | |
515630c5 | 1037 | /* Special handling for register PC. */ |
32178cab MS |
1038 | |
1039 | CORE_ADDR | |
515630c5 | 1040 | regcache_read_pc (struct regcache *regcache) |
32178cab | 1041 | { |
61a1198a UW |
1042 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
1043 | ||
32178cab MS |
1044 | CORE_ADDR pc_val; |
1045 | ||
61a1198a UW |
1046 | if (gdbarch_read_pc_p (gdbarch)) |
1047 | pc_val = gdbarch_read_pc (gdbarch, regcache); | |
cde9ea48 | 1048 | /* Else use per-frame method on get_current_frame. */ |
214e098a | 1049 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
cde9ea48 | 1050 | { |
61a1198a | 1051 | ULONGEST raw_val; |
123f5f96 | 1052 | |
05d1431c PA |
1053 | if (regcache_cooked_read_unsigned (regcache, |
1054 | gdbarch_pc_regnum (gdbarch), | |
1055 | &raw_val) == REG_UNAVAILABLE) | |
1056 | throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available")); | |
1057 | ||
214e098a | 1058 | pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val); |
cde9ea48 AC |
1059 | } |
1060 | else | |
515630c5 UW |
1061 | internal_error (__FILE__, __LINE__, |
1062 | _("regcache_read_pc: Unable to find PC")); | |
32178cab MS |
1063 | return pc_val; |
1064 | } | |
1065 | ||
32178cab | 1066 | void |
515630c5 | 1067 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) |
32178cab | 1068 | { |
61a1198a UW |
1069 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
1070 | ||
61a1198a UW |
1071 | if (gdbarch_write_pc_p (gdbarch)) |
1072 | gdbarch_write_pc (gdbarch, regcache, pc); | |
214e098a | 1073 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
3e8c568d | 1074 | regcache_cooked_write_unsigned (regcache, |
214e098a | 1075 | gdbarch_pc_regnum (gdbarch), pc); |
61a1198a UW |
1076 | else |
1077 | internal_error (__FILE__, __LINE__, | |
515630c5 | 1078 | _("regcache_write_pc: Unable to update PC")); |
edb3359d DJ |
1079 | |
1080 | /* Writing the PC (for instance, from "load") invalidates the | |
1081 | current frame. */ | |
1082 | reinit_frame_cache (); | |
32178cab MS |
1083 | } |
1084 | ||
32178cab | 1085 | |
705152c5 MS |
1086 | static void |
1087 | reg_flush_command (char *command, int from_tty) | |
1088 | { | |
1089 | /* Force-flush the register cache. */ | |
1090 | registers_changed (); | |
1091 | if (from_tty) | |
a3f17187 | 1092 | printf_filtered (_("Register cache flushed.\n")); |
705152c5 MS |
1093 | } |
1094 | ||
af030b9a AC |
1095 | static void |
1096 | dump_endian_bytes (struct ui_file *file, enum bfd_endian endian, | |
1097 | const unsigned char *buf, long len) | |
1098 | { | |
1099 | int i; | |
123f5f96 | 1100 | |
af030b9a AC |
1101 | switch (endian) |
1102 | { | |
1103 | case BFD_ENDIAN_BIG: | |
1104 | for (i = 0; i < len; i++) | |
1105 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1106 | break; | |
1107 | case BFD_ENDIAN_LITTLE: | |
1108 | for (i = len - 1; i >= 0; i--) | |
1109 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1110 | break; | |
1111 | default: | |
e2e0b3e5 | 1112 | internal_error (__FILE__, __LINE__, _("Bad switch")); |
af030b9a AC |
1113 | } |
1114 | } | |
1115 | ||
1116 | enum regcache_dump_what | |
1117 | { | |
3e43a32a | 1118 | regcache_dump_none, regcache_dump_raw, |
c21236dc PA |
1119 | regcache_dump_cooked, regcache_dump_groups, |
1120 | regcache_dump_remote | |
af030b9a AC |
1121 | }; |
1122 | ||
1123 | static void | |
1124 | regcache_dump (struct regcache *regcache, struct ui_file *file, | |
1125 | enum regcache_dump_what what_to_dump) | |
1126 | { | |
1127 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); | |
b59ff9d5 | 1128 | struct gdbarch *gdbarch = regcache->descr->gdbarch; |
af030b9a AC |
1129 | int regnum; |
1130 | int footnote_nr = 0; | |
1131 | int footnote_register_size = 0; | |
1132 | int footnote_register_offset = 0; | |
1133 | int footnote_register_type_name_null = 0; | |
1134 | long register_offset = 0; | |
123a958e | 1135 | unsigned char buf[MAX_REGISTER_SIZE]; |
af030b9a AC |
1136 | |
1137 | #if 0 | |
af030b9a AC |
1138 | fprintf_unfiltered (file, "nr_raw_registers %d\n", |
1139 | regcache->descr->nr_raw_registers); | |
1140 | fprintf_unfiltered (file, "nr_cooked_registers %d\n", | |
1141 | regcache->descr->nr_cooked_registers); | |
1142 | fprintf_unfiltered (file, "sizeof_raw_registers %ld\n", | |
1143 | regcache->descr->sizeof_raw_registers); | |
ee99023e PA |
1144 | fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n", |
1145 | regcache->descr->sizeof_raw_register_status); | |
f57d151a | 1146 | fprintf_unfiltered (file, "gdbarch_num_regs %d\n", |
214e098a | 1147 | gdbarch_num_regs (gdbarch)); |
f57d151a | 1148 | fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n", |
214e098a | 1149 | gdbarch_num_pseudo_regs (gdbarch)); |
af030b9a AC |
1150 | #endif |
1151 | ||
1152 | gdb_assert (regcache->descr->nr_cooked_registers | |
214e098a UW |
1153 | == (gdbarch_num_regs (gdbarch) |
1154 | + gdbarch_num_pseudo_regs (gdbarch))); | |
af030b9a AC |
1155 | |
1156 | for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++) | |
1157 | { | |
1158 | /* Name. */ | |
1159 | if (regnum < 0) | |
1160 | fprintf_unfiltered (file, " %-10s", "Name"); | |
1161 | else | |
1162 | { | |
214e098a | 1163 | const char *p = gdbarch_register_name (gdbarch, regnum); |
123f5f96 | 1164 | |
af030b9a AC |
1165 | if (p == NULL) |
1166 | p = ""; | |
1167 | else if (p[0] == '\0') | |
1168 | p = "''"; | |
1169 | fprintf_unfiltered (file, " %-10s", p); | |
1170 | } | |
1171 | ||
1172 | /* Number. */ | |
1173 | if (regnum < 0) | |
1174 | fprintf_unfiltered (file, " %4s", "Nr"); | |
1175 | else | |
1176 | fprintf_unfiltered (file, " %4d", regnum); | |
1177 | ||
1178 | /* Relative number. */ | |
1179 | if (regnum < 0) | |
1180 | fprintf_unfiltered (file, " %4s", "Rel"); | |
214e098a | 1181 | else if (regnum < gdbarch_num_regs (gdbarch)) |
af030b9a AC |
1182 | fprintf_unfiltered (file, " %4d", regnum); |
1183 | else | |
f57d151a | 1184 | fprintf_unfiltered (file, " %4d", |
214e098a | 1185 | (regnum - gdbarch_num_regs (gdbarch))); |
af030b9a AC |
1186 | |
1187 | /* Offset. */ | |
1188 | if (regnum < 0) | |
1189 | fprintf_unfiltered (file, " %6s ", "Offset"); | |
1190 | else | |
1191 | { | |
1192 | fprintf_unfiltered (file, " %6ld", | |
1193 | regcache->descr->register_offset[regnum]); | |
a7e3c2ad | 1194 | if (register_offset != regcache->descr->register_offset[regnum] |
d3b22ed5 AC |
1195 | || (regnum > 0 |
1196 | && (regcache->descr->register_offset[regnum] | |
1197 | != (regcache->descr->register_offset[regnum - 1] | |
1198 | + regcache->descr->sizeof_register[regnum - 1]))) | |
1199 | ) | |
af030b9a AC |
1200 | { |
1201 | if (!footnote_register_offset) | |
1202 | footnote_register_offset = ++footnote_nr; | |
1203 | fprintf_unfiltered (file, "*%d", footnote_register_offset); | |
1204 | } | |
1205 | else | |
1206 | fprintf_unfiltered (file, " "); | |
1207 | register_offset = (regcache->descr->register_offset[regnum] | |
1208 | + regcache->descr->sizeof_register[regnum]); | |
1209 | } | |
1210 | ||
1211 | /* Size. */ | |
1212 | if (regnum < 0) | |
1213 | fprintf_unfiltered (file, " %5s ", "Size"); | |
1214 | else | |
01e1877c AC |
1215 | fprintf_unfiltered (file, " %5ld", |
1216 | regcache->descr->sizeof_register[regnum]); | |
af030b9a AC |
1217 | |
1218 | /* Type. */ | |
b59ff9d5 AC |
1219 | { |
1220 | const char *t; | |
123f5f96 | 1221 | |
b59ff9d5 AC |
1222 | if (regnum < 0) |
1223 | t = "Type"; | |
1224 | else | |
1225 | { | |
1226 | static const char blt[] = "builtin_type"; | |
123f5f96 | 1227 | |
b59ff9d5 AC |
1228 | t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum)); |
1229 | if (t == NULL) | |
1230 | { | |
1231 | char *n; | |
123f5f96 | 1232 | |
b59ff9d5 AC |
1233 | if (!footnote_register_type_name_null) |
1234 | footnote_register_type_name_null = ++footnote_nr; | |
b435e160 | 1235 | n = xstrprintf ("*%d", footnote_register_type_name_null); |
b59ff9d5 AC |
1236 | make_cleanup (xfree, n); |
1237 | t = n; | |
1238 | } | |
1239 | /* Chop a leading builtin_type. */ | |
1240 | if (strncmp (t, blt, strlen (blt)) == 0) | |
1241 | t += strlen (blt); | |
1242 | } | |
1243 | fprintf_unfiltered (file, " %-15s", t); | |
1244 | } | |
1245 | ||
1246 | /* Leading space always present. */ | |
1247 | fprintf_unfiltered (file, " "); | |
af030b9a AC |
1248 | |
1249 | /* Value, raw. */ | |
1250 | if (what_to_dump == regcache_dump_raw) | |
1251 | { | |
1252 | if (regnum < 0) | |
1253 | fprintf_unfiltered (file, "Raw value"); | |
1254 | else if (regnum >= regcache->descr->nr_raw_registers) | |
1255 | fprintf_unfiltered (file, "<cooked>"); | |
ee99023e | 1256 | else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN) |
af030b9a | 1257 | fprintf_unfiltered (file, "<invalid>"); |
ee99023e PA |
1258 | else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE) |
1259 | fprintf_unfiltered (file, "<unavailable>"); | |
af030b9a AC |
1260 | else |
1261 | { | |
1262 | regcache_raw_read (regcache, regnum, buf); | |
1263 | fprintf_unfiltered (file, "0x"); | |
0d20ae72 | 1264 | dump_endian_bytes (file, |
214e098a | 1265 | gdbarch_byte_order (gdbarch), buf, |
01e1877c | 1266 | regcache->descr->sizeof_register[regnum]); |
af030b9a AC |
1267 | } |
1268 | } | |
1269 | ||
1270 | /* Value, cooked. */ | |
1271 | if (what_to_dump == regcache_dump_cooked) | |
1272 | { | |
1273 | if (regnum < 0) | |
1274 | fprintf_unfiltered (file, "Cooked value"); | |
1275 | else | |
1276 | { | |
05d1431c PA |
1277 | enum register_status status; |
1278 | ||
1279 | status = regcache_cooked_read (regcache, regnum, buf); | |
1280 | if (status == REG_UNKNOWN) | |
1281 | fprintf_unfiltered (file, "<invalid>"); | |
1282 | else if (status == REG_UNAVAILABLE) | |
1283 | fprintf_unfiltered (file, "<unavailable>"); | |
1284 | else | |
1285 | { | |
1286 | fprintf_unfiltered (file, "0x"); | |
1287 | dump_endian_bytes (file, | |
1288 | gdbarch_byte_order (gdbarch), buf, | |
1289 | regcache->descr->sizeof_register[regnum]); | |
1290 | } | |
af030b9a AC |
1291 | } |
1292 | } | |
1293 | ||
b59ff9d5 AC |
1294 | /* Group members. */ |
1295 | if (what_to_dump == regcache_dump_groups) | |
1296 | { | |
1297 | if (regnum < 0) | |
1298 | fprintf_unfiltered (file, "Groups"); | |
1299 | else | |
1300 | { | |
b59ff9d5 | 1301 | const char *sep = ""; |
6c7d17ba | 1302 | struct reggroup *group; |
123f5f96 | 1303 | |
6c7d17ba AC |
1304 | for (group = reggroup_next (gdbarch, NULL); |
1305 | group != NULL; | |
1306 | group = reggroup_next (gdbarch, group)) | |
b59ff9d5 | 1307 | { |
6c7d17ba | 1308 | if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) |
b59ff9d5 | 1309 | { |
3e43a32a MS |
1310 | fprintf_unfiltered (file, |
1311 | "%s%s", sep, reggroup_name (group)); | |
b59ff9d5 AC |
1312 | sep = ","; |
1313 | } | |
1314 | } | |
1315 | } | |
1316 | } | |
1317 | ||
c21236dc PA |
1318 | /* Remote packet configuration. */ |
1319 | if (what_to_dump == regcache_dump_remote) | |
1320 | { | |
1321 | if (regnum < 0) | |
1322 | { | |
1323 | fprintf_unfiltered (file, "Rmt Nr g/G Offset"); | |
1324 | } | |
1325 | else if (regnum < regcache->descr->nr_raw_registers) | |
1326 | { | |
1327 | int pnum, poffset; | |
1328 | ||
1329 | if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum, | |
1330 | &pnum, &poffset)) | |
1331 | fprintf_unfiltered (file, "%7d %11d", pnum, poffset); | |
1332 | } | |
1333 | } | |
1334 | ||
af030b9a AC |
1335 | fprintf_unfiltered (file, "\n"); |
1336 | } | |
1337 | ||
1338 | if (footnote_register_size) | |
1339 | fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n", | |
1340 | footnote_register_size); | |
1341 | if (footnote_register_offset) | |
1342 | fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n", | |
1343 | footnote_register_offset); | |
1344 | if (footnote_register_type_name_null) | |
1345 | fprintf_unfiltered (file, | |
1346 | "*%d: Register type's name NULL.\n", | |
1347 | footnote_register_type_name_null); | |
1348 | do_cleanups (cleanups); | |
1349 | } | |
1350 | ||
1351 | static void | |
1352 | regcache_print (char *args, enum regcache_dump_what what_to_dump) | |
1353 | { | |
1354 | if (args == NULL) | |
28c38f10 | 1355 | regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump); |
af030b9a AC |
1356 | else |
1357 | { | |
724b958c | 1358 | struct cleanup *cleanups; |
af030b9a | 1359 | struct ui_file *file = gdb_fopen (args, "w"); |
123f5f96 | 1360 | |
af030b9a | 1361 | if (file == NULL) |
e2e0b3e5 | 1362 | perror_with_name (_("maintenance print architecture")); |
724b958c | 1363 | cleanups = make_cleanup_ui_file_delete (file); |
28c38f10 | 1364 | regcache_dump (get_current_regcache (), file, what_to_dump); |
724b958c | 1365 | do_cleanups (cleanups); |
af030b9a AC |
1366 | } |
1367 | } | |
1368 | ||
1369 | static void | |
1370 | maintenance_print_registers (char *args, int from_tty) | |
1371 | { | |
1372 | regcache_print (args, regcache_dump_none); | |
1373 | } | |
1374 | ||
1375 | static void | |
1376 | maintenance_print_raw_registers (char *args, int from_tty) | |
1377 | { | |
1378 | regcache_print (args, regcache_dump_raw); | |
1379 | } | |
1380 | ||
1381 | static void | |
1382 | maintenance_print_cooked_registers (char *args, int from_tty) | |
1383 | { | |
1384 | regcache_print (args, regcache_dump_cooked); | |
1385 | } | |
1386 | ||
b59ff9d5 AC |
1387 | static void |
1388 | maintenance_print_register_groups (char *args, int from_tty) | |
1389 | { | |
1390 | regcache_print (args, regcache_dump_groups); | |
1391 | } | |
1392 | ||
c21236dc PA |
1393 | static void |
1394 | maintenance_print_remote_registers (char *args, int from_tty) | |
1395 | { | |
1396 | regcache_print (args, regcache_dump_remote); | |
1397 | } | |
1398 | ||
b9362cc7 AC |
1399 | extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */ |
1400 | ||
32178cab MS |
1401 | void |
1402 | _initialize_regcache (void) | |
1403 | { | |
3e43a32a MS |
1404 | regcache_descr_handle |
1405 | = gdbarch_data_register_post_init (init_regcache_descr); | |
705152c5 | 1406 | |
f4c5303c | 1407 | observer_attach_target_changed (regcache_observer_target_changed); |
5231c1fd | 1408 | observer_attach_thread_ptid_changed (regcache_thread_ptid_changed); |
f4c5303c | 1409 | |
705152c5 | 1410 | add_com ("flushregs", class_maintenance, reg_flush_command, |
1bedd215 | 1411 | _("Force gdb to flush its register cache (maintainer command)")); |
39f77062 | 1412 | |
3e43a32a MS |
1413 | add_cmd ("registers", class_maintenance, maintenance_print_registers, |
1414 | _("Print the internal register configuration.\n" | |
1415 | "Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1416 | add_cmd ("raw-registers", class_maintenance, |
3e43a32a MS |
1417 | maintenance_print_raw_registers, |
1418 | _("Print the internal register configuration " | |
1419 | "including raw values.\n" | |
1420 | "Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1421 | add_cmd ("cooked-registers", class_maintenance, |
3e43a32a MS |
1422 | maintenance_print_cooked_registers, |
1423 | _("Print the internal register configuration " | |
1424 | "including cooked values.\n" | |
1425 | "Takes an optional file parameter."), &maintenanceprintlist); | |
b59ff9d5 | 1426 | add_cmd ("register-groups", class_maintenance, |
3e43a32a MS |
1427 | maintenance_print_register_groups, |
1428 | _("Print the internal register configuration " | |
1429 | "including each register's group.\n" | |
1430 | "Takes an optional file parameter."), | |
af030b9a | 1431 | &maintenanceprintlist); |
c21236dc PA |
1432 | add_cmd ("remote-registers", class_maintenance, |
1433 | maintenance_print_remote_registers, _("\ | |
1434 | Print the internal register configuration including each register's\n\ | |
1435 | remote register number and buffer offset in the g/G packets.\n\ | |
1436 | Takes an optional file parameter."), | |
1437 | &maintenanceprintlist); | |
af030b9a | 1438 | |
32178cab | 1439 | } |