Commit | Line | Data |
---|---|---|
32178cab | 1 | /* Cache and manage the values of registers for GDB, the GNU debugger. |
3fadccb3 | 2 | |
6aba47ca | 3 | Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001, |
7b6bb8da | 4 | 2002, 2004, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
32178cab MS |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
32178cab MS |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
32178cab MS |
20 | |
21 | #include "defs.h" | |
32178cab MS |
22 | #include "inferior.h" |
23 | #include "target.h" | |
24 | #include "gdbarch.h" | |
705152c5 | 25 | #include "gdbcmd.h" |
4e052eda | 26 | #include "regcache.h" |
b59ff9d5 | 27 | #include "reggroups.h" |
61a0eb5b | 28 | #include "gdb_assert.h" |
b66d6d2e | 29 | #include "gdb_string.h" |
af030b9a | 30 | #include "gdbcmd.h" /* For maintenanceprintlist. */ |
f4c5303c | 31 | #include "observer.h" |
05d1431c | 32 | #include "exceptions.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 AC |
68 | |
69 | /* Offset and size (in 8 bit bytes), of reach register in the | |
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 | ||
333 | 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 | { |
2d522557 | 353 | int valid = cooked_read (cooked_read_context, regnum, buf); |
123f5f96 | 354 | |
5602984a AC |
355 | if (valid) |
356 | regcache_cooked_write (dst, regnum, buf); | |
2d28509a AC |
357 | } |
358 | } | |
359 | } | |
360 | ||
05d1431c | 361 | static enum register_status |
2d522557 | 362 | do_cooked_read (void *src, int regnum, gdb_byte *buf) |
5602984a AC |
363 | { |
364 | struct regcache *regcache = src; | |
123f5f96 | 365 | |
05d1431c | 366 | return regcache_cooked_read (regcache, regnum, buf); |
5602984a AC |
367 | } |
368 | ||
3fadccb3 AC |
369 | void |
370 | regcache_cpy (struct regcache *dst, struct regcache *src) | |
371 | { | |
3fadccb3 AC |
372 | gdb_assert (src != NULL && dst != NULL); |
373 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
374 | gdb_assert (src != dst); | |
2d28509a | 375 | gdb_assert (src->readonly_p || dst->readonly_p); |
6c95b8df | 376 | |
2d28509a | 377 | if (!src->readonly_p) |
5602984a | 378 | regcache_save (dst, do_cooked_read, src); |
2d28509a | 379 | else if (!dst->readonly_p) |
5602984a | 380 | regcache_restore (dst, do_cooked_read, src); |
2d28509a AC |
381 | else |
382 | regcache_cpy_no_passthrough (dst, src); | |
3fadccb3 AC |
383 | } |
384 | ||
385 | void | |
386 | regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src) | |
387 | { | |
3fadccb3 AC |
388 | gdb_assert (src != NULL && dst != NULL); |
389 | gdb_assert (src->descr->gdbarch == dst->descr->gdbarch); | |
390 | /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough | |
ee99023e PA |
391 | move of data into a thread's regcache. Doing this would be silly |
392 | - it would mean that regcache->register_status would be | |
393 | completely invalid. */ | |
99e42fd8 | 394 | gdb_assert (dst->readonly_p && src->readonly_p); |
6c95b8df | 395 | |
99e42fd8 PA |
396 | memcpy (dst->registers, src->registers, |
397 | dst->descr->sizeof_cooked_registers); | |
ee99023e PA |
398 | memcpy (dst->register_status, src->register_status, |
399 | dst->descr->sizeof_cooked_register_status); | |
3fadccb3 AC |
400 | } |
401 | ||
402 | struct regcache * | |
403 | regcache_dup (struct regcache *src) | |
404 | { | |
405 | struct regcache *newbuf; | |
123f5f96 | 406 | |
d37346f0 | 407 | newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src)); |
3fadccb3 AC |
408 | regcache_cpy (newbuf, src); |
409 | return newbuf; | |
410 | } | |
411 | ||
3fadccb3 | 412 | int |
ee99023e | 413 | regcache_register_status (const struct regcache *regcache, int regnum) |
3fadccb3 AC |
414 | { |
415 | gdb_assert (regcache != NULL); | |
6ed7ea50 UW |
416 | gdb_assert (regnum >= 0); |
417 | if (regcache->readonly_p) | |
418 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); | |
419 | else | |
420 | gdb_assert (regnum < regcache->descr->nr_raw_registers); | |
421 | ||
ee99023e | 422 | return regcache->register_status[regnum]; |
3fadccb3 AC |
423 | } |
424 | ||
9c5ea4d9 UW |
425 | void |
426 | regcache_invalidate (struct regcache *regcache, int regnum) | |
427 | { | |
428 | gdb_assert (regcache != NULL); | |
429 | gdb_assert (regnum >= 0); | |
430 | gdb_assert (!regcache->readonly_p); | |
431 | gdb_assert (regnum < regcache->descr->nr_raw_registers); | |
ee99023e | 432 | regcache->register_status[regnum] = REG_UNKNOWN; |
9c5ea4d9 UW |
433 | } |
434 | ||
435 | ||
3fadccb3 | 436 | /* Global structure containing the current regcache. */ |
3fadccb3 | 437 | |
5ebd2499 | 438 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
32178cab MS |
439 | recording if the register values have been changed (eg. by the |
440 | user). Therefore all registers must be written back to the | |
441 | target when appropriate. */ | |
442 | ||
c2250ad1 | 443 | struct regcache_list |
594f7785 | 444 | { |
c2250ad1 UW |
445 | struct regcache *regcache; |
446 | struct regcache_list *next; | |
447 | }; | |
448 | ||
449 | static struct regcache_list *current_regcache; | |
450 | ||
451 | struct regcache * | |
452 | get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch) | |
453 | { | |
454 | struct regcache_list *list; | |
455 | struct regcache *new_regcache; | |
b78974c3 | 456 | struct address_space *aspace; |
594f7785 | 457 | |
c2250ad1 UW |
458 | for (list = current_regcache; list; list = list->next) |
459 | if (ptid_equal (list->regcache->ptid, ptid) | |
460 | && get_regcache_arch (list->regcache) == gdbarch) | |
461 | return list->regcache; | |
594f7785 | 462 | |
b78974c3 PA |
463 | /* For the benefit of "maint print registers" & co when debugging an |
464 | executable, allow dumping the regcache even when there is no | |
465 | thread selected (target_thread_address_space internal-errors if | |
466 | no address space is found). Note that normal user commands will | |
467 | fail higher up on the call stack due to no | |
468 | target_has_registers. */ | |
469 | aspace = (ptid_equal (null_ptid, ptid) | |
470 | ? NULL | |
471 | : target_thread_address_space (ptid)); | |
472 | ||
473 | new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0); | |
c2250ad1 | 474 | new_regcache->ptid = ptid; |
594f7785 | 475 | |
c2250ad1 UW |
476 | list = xmalloc (sizeof (struct regcache_list)); |
477 | list->regcache = new_regcache; | |
478 | list->next = current_regcache; | |
479 | current_regcache = list; | |
594f7785 | 480 | |
c2250ad1 | 481 | return new_regcache; |
594f7785 UW |
482 | } |
483 | ||
c2250ad1 UW |
484 | static ptid_t current_thread_ptid; |
485 | static struct gdbarch *current_thread_arch; | |
486 | ||
487 | struct regcache * | |
488 | get_thread_regcache (ptid_t ptid) | |
489 | { | |
490 | if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid)) | |
491 | { | |
492 | current_thread_ptid = ptid; | |
493 | current_thread_arch = target_thread_architecture (ptid); | |
494 | } | |
495 | ||
496 | return get_thread_arch_regcache (ptid, current_thread_arch); | |
497 | } | |
498 | ||
499 | struct regcache * | |
500 | get_current_regcache (void) | |
594f7785 UW |
501 | { |
502 | return get_thread_regcache (inferior_ptid); | |
503 | } | |
32178cab | 504 | |
32178cab | 505 | |
f4c5303c OF |
506 | /* Observer for the target_changed event. */ |
507 | ||
2c0b251b | 508 | static void |
f4c5303c OF |
509 | regcache_observer_target_changed (struct target_ops *target) |
510 | { | |
511 | registers_changed (); | |
512 | } | |
513 | ||
5231c1fd PA |
514 | /* Update global variables old ptids to hold NEW_PTID if they were |
515 | holding OLD_PTID. */ | |
516 | static void | |
517 | regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) | |
518 | { | |
c2250ad1 UW |
519 | struct regcache_list *list; |
520 | ||
521 | for (list = current_regcache; list; list = list->next) | |
522 | if (ptid_equal (list->regcache->ptid, old_ptid)) | |
523 | list->regcache->ptid = new_ptid; | |
5231c1fd PA |
524 | } |
525 | ||
32178cab MS |
526 | /* Low level examining and depositing of registers. |
527 | ||
528 | The caller is responsible for making sure that the inferior is | |
529 | stopped before calling the fetching routines, or it will get | |
530 | garbage. (a change from GDB version 3, in which the caller got the | |
531 | value from the last stop). */ | |
532 | ||
533 | /* REGISTERS_CHANGED () | |
534 | ||
535 | Indicate that registers may have changed, so invalidate the cache. */ | |
536 | ||
537 | void | |
e66408ed | 538 | registers_changed_ptid (ptid_t ptid) |
32178cab | 539 | { |
e66408ed | 540 | struct regcache_list *list, **list_link; |
041274d8 | 541 | int wildcard = ptid_equal (ptid, minus_one_ptid); |
c2250ad1 | 542 | |
e66408ed PA |
543 | list = current_regcache; |
544 | list_link = ¤t_regcache; | |
545 | while (list) | |
c2250ad1 | 546 | { |
e66408ed PA |
547 | if (ptid_match (list->regcache->ptid, ptid)) |
548 | { | |
549 | struct regcache_list *dead = list; | |
550 | ||
551 | *list_link = list->next; | |
552 | regcache_xfree (list->regcache); | |
553 | list = *list_link; | |
554 | xfree (dead); | |
555 | continue; | |
556 | } | |
557 | ||
558 | list_link = &list->next; | |
559 | list = *list_link; | |
c2250ad1 | 560 | } |
32178cab | 561 | |
041274d8 PA |
562 | if (wildcard || ptid_equal (ptid, current_thread_ptid)) |
563 | { | |
564 | current_thread_ptid = null_ptid; | |
565 | current_thread_arch = NULL; | |
566 | } | |
32178cab | 567 | |
041274d8 PA |
568 | if (wildcard || ptid_equal (ptid, inferior_ptid)) |
569 | { | |
570 | /* We just deleted the regcache of the current thread. Need to | |
571 | forget about any frames we have cached, too. */ | |
572 | reinit_frame_cache (); | |
573 | } | |
574 | } | |
c2250ad1 | 575 | |
041274d8 PA |
576 | void |
577 | registers_changed (void) | |
578 | { | |
579 | registers_changed_ptid (minus_one_ptid); | |
a5d9d57d | 580 | |
32178cab MS |
581 | /* Force cleanup of any alloca areas if using C alloca instead of |
582 | a builtin alloca. This particular call is used to clean up | |
583 | areas allocated by low level target code which may build up | |
584 | during lengthy interactions between gdb and the target before | |
585 | gdb gives control to the user (ie watchpoints). */ | |
586 | alloca (0); | |
32178cab MS |
587 | } |
588 | ||
05d1431c | 589 | enum register_status |
2d522557 | 590 | regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
61a0eb5b | 591 | { |
3fadccb3 AC |
592 | gdb_assert (regcache != NULL && buf != NULL); |
593 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
3fadccb3 AC |
594 | /* Make certain that the register cache is up-to-date with respect |
595 | to the current thread. This switching shouldn't be necessary | |
596 | only there is still only one target side register cache. Sigh! | |
597 | On the bright side, at least there is a regcache object. */ | |
788c8b10 PA |
598 | if (!regcache->readonly_p |
599 | && regcache_register_status (regcache, regnum) == REG_UNKNOWN) | |
3fadccb3 | 600 | { |
788c8b10 | 601 | struct cleanup *old_chain = save_inferior_ptid (); |
123f5f96 | 602 | |
788c8b10 PA |
603 | inferior_ptid = regcache->ptid; |
604 | target_fetch_registers (regcache, regnum); | |
605 | do_cleanups (old_chain); | |
606 | ||
607 | /* A number of targets can't access the whole set of raw | |
608 | registers (because the debug API provides no means to get at | |
609 | them). */ | |
610 | if (regcache->register_status[regnum] == REG_UNKNOWN) | |
611 | regcache->register_status[regnum] = REG_UNAVAILABLE; | |
3fadccb3 | 612 | } |
05d1431c PA |
613 | |
614 | if (regcache->register_status[regnum] != REG_VALID) | |
615 | memset (buf, 0, regcache->descr->sizeof_register[regnum]); | |
616 | else | |
617 | memcpy (buf, register_buffer (regcache, regnum), | |
618 | regcache->descr->sizeof_register[regnum]); | |
619 | ||
620 | return regcache->register_status[regnum]; | |
61a0eb5b AC |
621 | } |
622 | ||
05d1431c | 623 | enum register_status |
28fc6740 AC |
624 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) |
625 | { | |
2d522557 | 626 | gdb_byte *buf; |
05d1431c | 627 | enum register_status status; |
123f5f96 | 628 | |
28fc6740 AC |
629 | gdb_assert (regcache != NULL); |
630 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
631 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
05d1431c PA |
632 | status = regcache_raw_read (regcache, regnum, buf); |
633 | if (status == REG_VALID) | |
634 | *val = extract_signed_integer | |
635 | (buf, regcache->descr->sizeof_register[regnum], | |
636 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
637 | else | |
638 | *val = 0; | |
639 | return status; | |
28fc6740 AC |
640 | } |
641 | ||
05d1431c | 642 | enum register_status |
28fc6740 AC |
643 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, |
644 | ULONGEST *val) | |
645 | { | |
2d522557 | 646 | gdb_byte *buf; |
05d1431c | 647 | enum register_status status; |
123f5f96 | 648 | |
28fc6740 AC |
649 | gdb_assert (regcache != NULL); |
650 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
651 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
05d1431c PA |
652 | status = regcache_raw_read (regcache, regnum, buf); |
653 | if (status == REG_VALID) | |
654 | *val = extract_unsigned_integer | |
655 | (buf, regcache->descr->sizeof_register[regnum], | |
656 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
657 | else | |
658 | *val = 0; | |
659 | return status; | |
28fc6740 AC |
660 | } |
661 | ||
c00dcbe9 MK |
662 | void |
663 | regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) | |
664 | { | |
665 | void *buf; | |
123f5f96 | 666 | |
c00dcbe9 MK |
667 | gdb_assert (regcache != NULL); |
668 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
669 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
670 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], |
671 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
c00dcbe9 MK |
672 | regcache_raw_write (regcache, regnum, buf); |
673 | } | |
674 | ||
675 | void | |
676 | regcache_raw_write_unsigned (struct regcache *regcache, int regnum, | |
677 | ULONGEST val) | |
678 | { | |
679 | void *buf; | |
123f5f96 | 680 | |
c00dcbe9 MK |
681 | gdb_assert (regcache != NULL); |
682 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers); | |
683 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
684 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], |
685 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
c00dcbe9 MK |
686 | regcache_raw_write (regcache, regnum, buf); |
687 | } | |
688 | ||
05d1431c | 689 | enum register_status |
2d522557 | 690 | regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
68365089 | 691 | { |
d138e37a | 692 | gdb_assert (regnum >= 0); |
68365089 AC |
693 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
694 | if (regnum < regcache->descr->nr_raw_registers) | |
05d1431c | 695 | return regcache_raw_read (regcache, regnum, buf); |
2d28509a | 696 | else if (regcache->readonly_p |
05d1431c PA |
697 | && regcache->register_status[regnum] != REG_UNKNOWN) |
698 | { | |
699 | /* Read-only register cache, perhaps the cooked value was | |
700 | cached? */ | |
701 | struct gdbarch *gdbarch = regcache->descr->gdbarch; | |
702 | ||
703 | if (regcache->register_status[regnum] == REG_VALID) | |
704 | memcpy (buf, register_buffer (regcache, regnum), | |
705 | regcache->descr->sizeof_register[regnum]); | |
706 | else | |
707 | memset (buf, 0, regcache->descr->sizeof_register[regnum]); | |
708 | ||
709 | return regcache->register_status[regnum]; | |
710 | } | |
d138e37a | 711 | else |
05d1431c PA |
712 | return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache, |
713 | regnum, buf); | |
61a0eb5b AC |
714 | } |
715 | ||
05d1431c | 716 | enum register_status |
a378f419 AC |
717 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, |
718 | LONGEST *val) | |
719 | { | |
05d1431c | 720 | enum register_status status; |
2d522557 | 721 | gdb_byte *buf; |
123f5f96 | 722 | |
a378f419 | 723 | gdb_assert (regcache != NULL); |
a66a9c23 | 724 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 | 725 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
05d1431c PA |
726 | status = regcache_cooked_read (regcache, regnum, buf); |
727 | if (status == REG_VALID) | |
728 | *val = extract_signed_integer | |
729 | (buf, regcache->descr->sizeof_register[regnum], | |
730 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
731 | else | |
732 | *val = 0; | |
733 | return status; | |
a378f419 AC |
734 | } |
735 | ||
05d1431c | 736 | enum register_status |
a378f419 AC |
737 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, |
738 | ULONGEST *val) | |
739 | { | |
05d1431c | 740 | enum register_status status; |
2d522557 | 741 | gdb_byte *buf; |
123f5f96 | 742 | |
a378f419 | 743 | gdb_assert (regcache != NULL); |
a66a9c23 | 744 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers); |
a378f419 | 745 | buf = alloca (regcache->descr->sizeof_register[regnum]); |
05d1431c PA |
746 | status = regcache_cooked_read (regcache, regnum, buf); |
747 | if (status == REG_VALID) | |
748 | *val = extract_unsigned_integer | |
749 | (buf, regcache->descr->sizeof_register[regnum], | |
750 | gdbarch_byte_order (regcache->descr->gdbarch)); | |
751 | else | |
752 | *val = 0; | |
753 | return status; | |
a378f419 AC |
754 | } |
755 | ||
a66a9c23 AC |
756 | void |
757 | regcache_cooked_write_signed (struct regcache *regcache, int regnum, | |
758 | LONGEST val) | |
759 | { | |
760 | void *buf; | |
123f5f96 | 761 | |
a66a9c23 AC |
762 | gdb_assert (regcache != NULL); |
763 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
764 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
765 | store_signed_integer (buf, regcache->descr->sizeof_register[regnum], |
766 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
a66a9c23 AC |
767 | regcache_cooked_write (regcache, regnum, buf); |
768 | } | |
769 | ||
770 | void | |
771 | regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, | |
772 | ULONGEST val) | |
773 | { | |
774 | void *buf; | |
123f5f96 | 775 | |
a66a9c23 AC |
776 | gdb_assert (regcache != NULL); |
777 | gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers); | |
778 | buf = alloca (regcache->descr->sizeof_register[regnum]); | |
e17a4113 UW |
779 | store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], |
780 | gdbarch_byte_order (regcache->descr->gdbarch), val); | |
a66a9c23 AC |
781 | regcache_cooked_write (regcache, regnum, buf); |
782 | } | |
783 | ||
61a0eb5b | 784 | void |
2d522557 AC |
785 | regcache_raw_write (struct regcache *regcache, int regnum, |
786 | const gdb_byte *buf) | |
61a0eb5b | 787 | { |
594f7785 UW |
788 | struct cleanup *old_chain; |
789 | ||
3fadccb3 AC |
790 | gdb_assert (regcache != NULL && buf != NULL); |
791 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
2d28509a | 792 | gdb_assert (!regcache->readonly_p); |
3fadccb3 | 793 | |
3fadccb3 AC |
794 | /* On the sparc, writing %g0 is a no-op, so we don't even want to |
795 | change the registers array if something writes to this register. */ | |
214e098a | 796 | if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum)) |
3fadccb3 AC |
797 | return; |
798 | ||
3fadccb3 | 799 | /* If we have a valid copy of the register, and new value == old |
0df8b418 | 800 | value, then don't bother doing the actual store. */ |
ee99023e | 801 | if (regcache_register_status (regcache, regnum) == REG_VALID |
3fadccb3 AC |
802 | && (memcmp (register_buffer (regcache, regnum), buf, |
803 | regcache->descr->sizeof_register[regnum]) == 0)) | |
804 | return; | |
805 | ||
594f7785 UW |
806 | old_chain = save_inferior_ptid (); |
807 | inferior_ptid = regcache->ptid; | |
808 | ||
316f2060 | 809 | target_prepare_to_store (regcache); |
3fadccb3 AC |
810 | memcpy (register_buffer (regcache, regnum), buf, |
811 | regcache->descr->sizeof_register[regnum]); | |
ee99023e | 812 | regcache->register_status[regnum] = REG_VALID; |
56be3814 | 813 | target_store_registers (regcache, regnum); |
594f7785 UW |
814 | |
815 | do_cleanups (old_chain); | |
61a0eb5b AC |
816 | } |
817 | ||
68365089 | 818 | void |
2d522557 AC |
819 | regcache_cooked_write (struct regcache *regcache, int regnum, |
820 | const gdb_byte *buf) | |
68365089 | 821 | { |
d138e37a | 822 | gdb_assert (regnum >= 0); |
68365089 AC |
823 | gdb_assert (regnum < regcache->descr->nr_cooked_registers); |
824 | if (regnum < regcache->descr->nr_raw_registers) | |
825 | regcache_raw_write (regcache, regnum, buf); | |
d138e37a | 826 | else |
68365089 | 827 | gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache, |
d8124050 | 828 | regnum, buf); |
61a0eb5b AC |
829 | } |
830 | ||
06c0b04e AC |
831 | /* Perform a partial register transfer using a read, modify, write |
832 | operation. */ | |
833 | ||
834 | typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum, | |
835 | void *buf); | |
836 | typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum, | |
837 | const void *buf); | |
838 | ||
05d1431c | 839 | static enum register_status |
06c0b04e AC |
840 | regcache_xfer_part (struct regcache *regcache, int regnum, |
841 | int offset, int len, void *in, const void *out, | |
05d1431c PA |
842 | enum register_status (*read) (struct regcache *regcache, |
843 | int regnum, | |
844 | gdb_byte *buf), | |
2d522557 AC |
845 | void (*write) (struct regcache *regcache, int regnum, |
846 | const gdb_byte *buf)) | |
06c0b04e AC |
847 | { |
848 | struct regcache_descr *descr = regcache->descr; | |
fc1a4b47 | 849 | gdb_byte reg[MAX_REGISTER_SIZE]; |
123f5f96 | 850 | |
06c0b04e AC |
851 | gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]); |
852 | gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]); | |
853 | /* Something to do? */ | |
854 | if (offset + len == 0) | |
05d1431c | 855 | return REG_VALID; |
0df8b418 | 856 | /* Read (when needed) ... */ |
06c0b04e AC |
857 | if (in != NULL |
858 | || offset > 0 | |
859 | || offset + len < descr->sizeof_register[regnum]) | |
860 | { | |
05d1431c PA |
861 | enum register_status status; |
862 | ||
06c0b04e | 863 | gdb_assert (read != NULL); |
05d1431c PA |
864 | status = read (regcache, regnum, reg); |
865 | if (status != REG_VALID) | |
866 | return status; | |
06c0b04e | 867 | } |
0df8b418 | 868 | /* ... modify ... */ |
06c0b04e AC |
869 | if (in != NULL) |
870 | memcpy (in, reg + offset, len); | |
871 | if (out != NULL) | |
872 | memcpy (reg + offset, out, len); | |
873 | /* ... write (when needed). */ | |
874 | if (out != NULL) | |
875 | { | |
876 | gdb_assert (write != NULL); | |
877 | write (regcache, regnum, reg); | |
878 | } | |
05d1431c PA |
879 | |
880 | return REG_VALID; | |
06c0b04e AC |
881 | } |
882 | ||
05d1431c | 883 | enum register_status |
06c0b04e | 884 | regcache_raw_read_part (struct regcache *regcache, int regnum, |
2d522557 | 885 | int offset, int len, gdb_byte *buf) |
06c0b04e AC |
886 | { |
887 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 888 | |
06c0b04e | 889 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); |
05d1431c PA |
890 | return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, |
891 | regcache_raw_read, regcache_raw_write); | |
06c0b04e AC |
892 | } |
893 | ||
894 | void | |
895 | regcache_raw_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 896 | int offset, int len, const gdb_byte *buf) |
06c0b04e AC |
897 | { |
898 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 899 | |
06c0b04e AC |
900 | gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers); |
901 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
902 | regcache_raw_read, regcache_raw_write); | |
903 | } | |
904 | ||
05d1431c | 905 | enum register_status |
06c0b04e | 906 | regcache_cooked_read_part (struct regcache *regcache, int regnum, |
2d522557 | 907 | int offset, int len, gdb_byte *buf) |
06c0b04e AC |
908 | { |
909 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 910 | |
06c0b04e | 911 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
05d1431c PA |
912 | return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL, |
913 | regcache_cooked_read, regcache_cooked_write); | |
06c0b04e AC |
914 | } |
915 | ||
916 | void | |
917 | regcache_cooked_write_part (struct regcache *regcache, int regnum, | |
2d522557 | 918 | int offset, int len, const gdb_byte *buf) |
06c0b04e AC |
919 | { |
920 | struct regcache_descr *descr = regcache->descr; | |
123f5f96 | 921 | |
06c0b04e AC |
922 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
923 | regcache_xfer_part (regcache, regnum, offset, len, NULL, buf, | |
924 | regcache_cooked_read, regcache_cooked_write); | |
925 | } | |
32178cab | 926 | |
a16d75cc | 927 | /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */ |
9a661b68 MK |
928 | |
929 | void | |
6618125d | 930 | regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf) |
9a661b68 MK |
931 | { |
932 | void *regbuf; | |
933 | size_t size; | |
934 | ||
a16d75cc | 935 | gdb_assert (regcache != NULL); |
9a661b68 MK |
936 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); |
937 | gdb_assert (!regcache->readonly_p); | |
938 | ||
9a661b68 MK |
939 | regbuf = register_buffer (regcache, regnum); |
940 | size = regcache->descr->sizeof_register[regnum]; | |
941 | ||
942 | if (buf) | |
ee99023e PA |
943 | { |
944 | memcpy (regbuf, buf, size); | |
945 | regcache->register_status[regnum] = REG_VALID; | |
946 | } | |
9a661b68 | 947 | else |
ee99023e PA |
948 | { |
949 | /* This memset not strictly necessary, but better than garbage | |
950 | in case the register value manages to escape somewhere (due | |
951 | to a bug, no less). */ | |
952 | memset (regbuf, 0, size); | |
953 | regcache->register_status[regnum] = REG_UNAVAILABLE; | |
954 | } | |
9a661b68 MK |
955 | } |
956 | ||
957 | /* Collect register REGNUM from REGCACHE and store its contents in BUF. */ | |
958 | ||
959 | void | |
6618125d | 960 | regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf) |
9a661b68 MK |
961 | { |
962 | const void *regbuf; | |
963 | size_t size; | |
964 | ||
965 | gdb_assert (regcache != NULL && buf != NULL); | |
966 | gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers); | |
967 | ||
968 | regbuf = register_buffer (regcache, regnum); | |
969 | size = regcache->descr->sizeof_register[regnum]; | |
970 | memcpy (buf, regbuf, size); | |
971 | } | |
972 | ||
193cb69f | 973 | |
515630c5 | 974 | /* Special handling for register PC. */ |
32178cab MS |
975 | |
976 | CORE_ADDR | |
515630c5 | 977 | regcache_read_pc (struct regcache *regcache) |
32178cab | 978 | { |
61a1198a UW |
979 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
980 | ||
32178cab MS |
981 | CORE_ADDR pc_val; |
982 | ||
61a1198a UW |
983 | if (gdbarch_read_pc_p (gdbarch)) |
984 | pc_val = gdbarch_read_pc (gdbarch, regcache); | |
cde9ea48 | 985 | /* Else use per-frame method on get_current_frame. */ |
214e098a | 986 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
cde9ea48 | 987 | { |
61a1198a | 988 | ULONGEST raw_val; |
123f5f96 | 989 | |
05d1431c PA |
990 | if (regcache_cooked_read_unsigned (regcache, |
991 | gdbarch_pc_regnum (gdbarch), | |
992 | &raw_val) == REG_UNAVAILABLE) | |
993 | throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available")); | |
994 | ||
214e098a | 995 | pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val); |
cde9ea48 AC |
996 | } |
997 | else | |
515630c5 UW |
998 | internal_error (__FILE__, __LINE__, |
999 | _("regcache_read_pc: Unable to find PC")); | |
32178cab MS |
1000 | return pc_val; |
1001 | } | |
1002 | ||
32178cab | 1003 | void |
515630c5 | 1004 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) |
32178cab | 1005 | { |
61a1198a UW |
1006 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
1007 | ||
61a1198a UW |
1008 | if (gdbarch_write_pc_p (gdbarch)) |
1009 | gdbarch_write_pc (gdbarch, regcache, pc); | |
214e098a | 1010 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
3e8c568d | 1011 | regcache_cooked_write_unsigned (regcache, |
214e098a | 1012 | gdbarch_pc_regnum (gdbarch), pc); |
61a1198a UW |
1013 | else |
1014 | internal_error (__FILE__, __LINE__, | |
515630c5 | 1015 | _("regcache_write_pc: Unable to update PC")); |
edb3359d DJ |
1016 | |
1017 | /* Writing the PC (for instance, from "load") invalidates the | |
1018 | current frame. */ | |
1019 | reinit_frame_cache (); | |
32178cab MS |
1020 | } |
1021 | ||
32178cab | 1022 | |
705152c5 MS |
1023 | static void |
1024 | reg_flush_command (char *command, int from_tty) | |
1025 | { | |
1026 | /* Force-flush the register cache. */ | |
1027 | registers_changed (); | |
1028 | if (from_tty) | |
a3f17187 | 1029 | printf_filtered (_("Register cache flushed.\n")); |
705152c5 MS |
1030 | } |
1031 | ||
af030b9a AC |
1032 | static void |
1033 | dump_endian_bytes (struct ui_file *file, enum bfd_endian endian, | |
1034 | const unsigned char *buf, long len) | |
1035 | { | |
1036 | int i; | |
123f5f96 | 1037 | |
af030b9a AC |
1038 | switch (endian) |
1039 | { | |
1040 | case BFD_ENDIAN_BIG: | |
1041 | for (i = 0; i < len; i++) | |
1042 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1043 | break; | |
1044 | case BFD_ENDIAN_LITTLE: | |
1045 | for (i = len - 1; i >= 0; i--) | |
1046 | fprintf_unfiltered (file, "%02x", buf[i]); | |
1047 | break; | |
1048 | default: | |
e2e0b3e5 | 1049 | internal_error (__FILE__, __LINE__, _("Bad switch")); |
af030b9a AC |
1050 | } |
1051 | } | |
1052 | ||
1053 | enum regcache_dump_what | |
1054 | { | |
3e43a32a MS |
1055 | regcache_dump_none, regcache_dump_raw, |
1056 | regcache_dump_cooked, regcache_dump_groups | |
af030b9a AC |
1057 | }; |
1058 | ||
1059 | static void | |
1060 | regcache_dump (struct regcache *regcache, struct ui_file *file, | |
1061 | enum regcache_dump_what what_to_dump) | |
1062 | { | |
1063 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); | |
b59ff9d5 | 1064 | struct gdbarch *gdbarch = regcache->descr->gdbarch; |
af030b9a AC |
1065 | int regnum; |
1066 | int footnote_nr = 0; | |
1067 | int footnote_register_size = 0; | |
1068 | int footnote_register_offset = 0; | |
1069 | int footnote_register_type_name_null = 0; | |
1070 | long register_offset = 0; | |
123a958e | 1071 | unsigned char buf[MAX_REGISTER_SIZE]; |
af030b9a AC |
1072 | |
1073 | #if 0 | |
af030b9a AC |
1074 | fprintf_unfiltered (file, "nr_raw_registers %d\n", |
1075 | regcache->descr->nr_raw_registers); | |
1076 | fprintf_unfiltered (file, "nr_cooked_registers %d\n", | |
1077 | regcache->descr->nr_cooked_registers); | |
1078 | fprintf_unfiltered (file, "sizeof_raw_registers %ld\n", | |
1079 | regcache->descr->sizeof_raw_registers); | |
ee99023e PA |
1080 | fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n", |
1081 | regcache->descr->sizeof_raw_register_status); | |
f57d151a | 1082 | fprintf_unfiltered (file, "gdbarch_num_regs %d\n", |
214e098a | 1083 | gdbarch_num_regs (gdbarch)); |
f57d151a | 1084 | fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n", |
214e098a | 1085 | gdbarch_num_pseudo_regs (gdbarch)); |
af030b9a AC |
1086 | #endif |
1087 | ||
1088 | gdb_assert (regcache->descr->nr_cooked_registers | |
214e098a UW |
1089 | == (gdbarch_num_regs (gdbarch) |
1090 | + gdbarch_num_pseudo_regs (gdbarch))); | |
af030b9a AC |
1091 | |
1092 | for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++) | |
1093 | { | |
1094 | /* Name. */ | |
1095 | if (regnum < 0) | |
1096 | fprintf_unfiltered (file, " %-10s", "Name"); | |
1097 | else | |
1098 | { | |
214e098a | 1099 | const char *p = gdbarch_register_name (gdbarch, regnum); |
123f5f96 | 1100 | |
af030b9a AC |
1101 | if (p == NULL) |
1102 | p = ""; | |
1103 | else if (p[0] == '\0') | |
1104 | p = "''"; | |
1105 | fprintf_unfiltered (file, " %-10s", p); | |
1106 | } | |
1107 | ||
1108 | /* Number. */ | |
1109 | if (regnum < 0) | |
1110 | fprintf_unfiltered (file, " %4s", "Nr"); | |
1111 | else | |
1112 | fprintf_unfiltered (file, " %4d", regnum); | |
1113 | ||
1114 | /* Relative number. */ | |
1115 | if (regnum < 0) | |
1116 | fprintf_unfiltered (file, " %4s", "Rel"); | |
214e098a | 1117 | else if (regnum < gdbarch_num_regs (gdbarch)) |
af030b9a AC |
1118 | fprintf_unfiltered (file, " %4d", regnum); |
1119 | else | |
f57d151a | 1120 | fprintf_unfiltered (file, " %4d", |
214e098a | 1121 | (regnum - gdbarch_num_regs (gdbarch))); |
af030b9a AC |
1122 | |
1123 | /* Offset. */ | |
1124 | if (regnum < 0) | |
1125 | fprintf_unfiltered (file, " %6s ", "Offset"); | |
1126 | else | |
1127 | { | |
1128 | fprintf_unfiltered (file, " %6ld", | |
1129 | regcache->descr->register_offset[regnum]); | |
a7e3c2ad | 1130 | if (register_offset != regcache->descr->register_offset[regnum] |
d3b22ed5 AC |
1131 | || (regnum > 0 |
1132 | && (regcache->descr->register_offset[regnum] | |
1133 | != (regcache->descr->register_offset[regnum - 1] | |
1134 | + regcache->descr->sizeof_register[regnum - 1]))) | |
1135 | ) | |
af030b9a AC |
1136 | { |
1137 | if (!footnote_register_offset) | |
1138 | footnote_register_offset = ++footnote_nr; | |
1139 | fprintf_unfiltered (file, "*%d", footnote_register_offset); | |
1140 | } | |
1141 | else | |
1142 | fprintf_unfiltered (file, " "); | |
1143 | register_offset = (regcache->descr->register_offset[regnum] | |
1144 | + regcache->descr->sizeof_register[regnum]); | |
1145 | } | |
1146 | ||
1147 | /* Size. */ | |
1148 | if (regnum < 0) | |
1149 | fprintf_unfiltered (file, " %5s ", "Size"); | |
1150 | else | |
01e1877c AC |
1151 | fprintf_unfiltered (file, " %5ld", |
1152 | regcache->descr->sizeof_register[regnum]); | |
af030b9a AC |
1153 | |
1154 | /* Type. */ | |
b59ff9d5 AC |
1155 | { |
1156 | const char *t; | |
123f5f96 | 1157 | |
b59ff9d5 AC |
1158 | if (regnum < 0) |
1159 | t = "Type"; | |
1160 | else | |
1161 | { | |
1162 | static const char blt[] = "builtin_type"; | |
123f5f96 | 1163 | |
b59ff9d5 AC |
1164 | t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum)); |
1165 | if (t == NULL) | |
1166 | { | |
1167 | char *n; | |
123f5f96 | 1168 | |
b59ff9d5 AC |
1169 | if (!footnote_register_type_name_null) |
1170 | footnote_register_type_name_null = ++footnote_nr; | |
b435e160 | 1171 | n = xstrprintf ("*%d", footnote_register_type_name_null); |
b59ff9d5 AC |
1172 | make_cleanup (xfree, n); |
1173 | t = n; | |
1174 | } | |
1175 | /* Chop a leading builtin_type. */ | |
1176 | if (strncmp (t, blt, strlen (blt)) == 0) | |
1177 | t += strlen (blt); | |
1178 | } | |
1179 | fprintf_unfiltered (file, " %-15s", t); | |
1180 | } | |
1181 | ||
1182 | /* Leading space always present. */ | |
1183 | fprintf_unfiltered (file, " "); | |
af030b9a AC |
1184 | |
1185 | /* Value, raw. */ | |
1186 | if (what_to_dump == regcache_dump_raw) | |
1187 | { | |
1188 | if (regnum < 0) | |
1189 | fprintf_unfiltered (file, "Raw value"); | |
1190 | else if (regnum >= regcache->descr->nr_raw_registers) | |
1191 | fprintf_unfiltered (file, "<cooked>"); | |
ee99023e | 1192 | else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN) |
af030b9a | 1193 | fprintf_unfiltered (file, "<invalid>"); |
ee99023e PA |
1194 | else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE) |
1195 | fprintf_unfiltered (file, "<unavailable>"); | |
af030b9a AC |
1196 | else |
1197 | { | |
1198 | regcache_raw_read (regcache, regnum, buf); | |
1199 | fprintf_unfiltered (file, "0x"); | |
0d20ae72 | 1200 | dump_endian_bytes (file, |
214e098a | 1201 | gdbarch_byte_order (gdbarch), buf, |
01e1877c | 1202 | regcache->descr->sizeof_register[regnum]); |
af030b9a AC |
1203 | } |
1204 | } | |
1205 | ||
1206 | /* Value, cooked. */ | |
1207 | if (what_to_dump == regcache_dump_cooked) | |
1208 | { | |
1209 | if (regnum < 0) | |
1210 | fprintf_unfiltered (file, "Cooked value"); | |
1211 | else | |
1212 | { | |
05d1431c PA |
1213 | enum register_status status; |
1214 | ||
1215 | status = regcache_cooked_read (regcache, regnum, buf); | |
1216 | if (status == REG_UNKNOWN) | |
1217 | fprintf_unfiltered (file, "<invalid>"); | |
1218 | else if (status == REG_UNAVAILABLE) | |
1219 | fprintf_unfiltered (file, "<unavailable>"); | |
1220 | else | |
1221 | { | |
1222 | fprintf_unfiltered (file, "0x"); | |
1223 | dump_endian_bytes (file, | |
1224 | gdbarch_byte_order (gdbarch), buf, | |
1225 | regcache->descr->sizeof_register[regnum]); | |
1226 | } | |
af030b9a AC |
1227 | } |
1228 | } | |
1229 | ||
b59ff9d5 AC |
1230 | /* Group members. */ |
1231 | if (what_to_dump == regcache_dump_groups) | |
1232 | { | |
1233 | if (regnum < 0) | |
1234 | fprintf_unfiltered (file, "Groups"); | |
1235 | else | |
1236 | { | |
b59ff9d5 | 1237 | const char *sep = ""; |
6c7d17ba | 1238 | struct reggroup *group; |
123f5f96 | 1239 | |
6c7d17ba AC |
1240 | for (group = reggroup_next (gdbarch, NULL); |
1241 | group != NULL; | |
1242 | group = reggroup_next (gdbarch, group)) | |
b59ff9d5 | 1243 | { |
6c7d17ba | 1244 | if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) |
b59ff9d5 | 1245 | { |
3e43a32a MS |
1246 | fprintf_unfiltered (file, |
1247 | "%s%s", sep, reggroup_name (group)); | |
b59ff9d5 AC |
1248 | sep = ","; |
1249 | } | |
1250 | } | |
1251 | } | |
1252 | } | |
1253 | ||
af030b9a AC |
1254 | fprintf_unfiltered (file, "\n"); |
1255 | } | |
1256 | ||
1257 | if (footnote_register_size) | |
1258 | fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n", | |
1259 | footnote_register_size); | |
1260 | if (footnote_register_offset) | |
1261 | fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n", | |
1262 | footnote_register_offset); | |
1263 | if (footnote_register_type_name_null) | |
1264 | fprintf_unfiltered (file, | |
1265 | "*%d: Register type's name NULL.\n", | |
1266 | footnote_register_type_name_null); | |
1267 | do_cleanups (cleanups); | |
1268 | } | |
1269 | ||
1270 | static void | |
1271 | regcache_print (char *args, enum regcache_dump_what what_to_dump) | |
1272 | { | |
1273 | if (args == NULL) | |
28c38f10 | 1274 | regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump); |
af030b9a AC |
1275 | else |
1276 | { | |
724b958c | 1277 | struct cleanup *cleanups; |
af030b9a | 1278 | struct ui_file *file = gdb_fopen (args, "w"); |
123f5f96 | 1279 | |
af030b9a | 1280 | if (file == NULL) |
e2e0b3e5 | 1281 | perror_with_name (_("maintenance print architecture")); |
724b958c | 1282 | cleanups = make_cleanup_ui_file_delete (file); |
28c38f10 | 1283 | regcache_dump (get_current_regcache (), file, what_to_dump); |
724b958c | 1284 | do_cleanups (cleanups); |
af030b9a AC |
1285 | } |
1286 | } | |
1287 | ||
1288 | static void | |
1289 | maintenance_print_registers (char *args, int from_tty) | |
1290 | { | |
1291 | regcache_print (args, regcache_dump_none); | |
1292 | } | |
1293 | ||
1294 | static void | |
1295 | maintenance_print_raw_registers (char *args, int from_tty) | |
1296 | { | |
1297 | regcache_print (args, regcache_dump_raw); | |
1298 | } | |
1299 | ||
1300 | static void | |
1301 | maintenance_print_cooked_registers (char *args, int from_tty) | |
1302 | { | |
1303 | regcache_print (args, regcache_dump_cooked); | |
1304 | } | |
1305 | ||
b59ff9d5 AC |
1306 | static void |
1307 | maintenance_print_register_groups (char *args, int from_tty) | |
1308 | { | |
1309 | regcache_print (args, regcache_dump_groups); | |
1310 | } | |
1311 | ||
b9362cc7 AC |
1312 | extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */ |
1313 | ||
32178cab MS |
1314 | void |
1315 | _initialize_regcache (void) | |
1316 | { | |
3e43a32a MS |
1317 | regcache_descr_handle |
1318 | = gdbarch_data_register_post_init (init_regcache_descr); | |
705152c5 | 1319 | |
f4c5303c | 1320 | observer_attach_target_changed (regcache_observer_target_changed); |
5231c1fd | 1321 | observer_attach_thread_ptid_changed (regcache_thread_ptid_changed); |
f4c5303c | 1322 | |
705152c5 | 1323 | add_com ("flushregs", class_maintenance, reg_flush_command, |
1bedd215 | 1324 | _("Force gdb to flush its register cache (maintainer command)")); |
39f77062 | 1325 | |
3e43a32a MS |
1326 | add_cmd ("registers", class_maintenance, maintenance_print_registers, |
1327 | _("Print the internal register configuration.\n" | |
1328 | "Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1329 | add_cmd ("raw-registers", class_maintenance, |
3e43a32a MS |
1330 | maintenance_print_raw_registers, |
1331 | _("Print the internal register configuration " | |
1332 | "including raw values.\n" | |
1333 | "Takes an optional file parameter."), &maintenanceprintlist); | |
af030b9a | 1334 | add_cmd ("cooked-registers", class_maintenance, |
3e43a32a MS |
1335 | maintenance_print_cooked_registers, |
1336 | _("Print the internal register configuration " | |
1337 | "including cooked values.\n" | |
1338 | "Takes an optional file parameter."), &maintenanceprintlist); | |
b59ff9d5 | 1339 | add_cmd ("register-groups", class_maintenance, |
3e43a32a MS |
1340 | maintenance_print_register_groups, |
1341 | _("Print the internal register configuration " | |
1342 | "including each register's group.\n" | |
1343 | "Takes an optional file parameter."), | |
af030b9a AC |
1344 | &maintenanceprintlist); |
1345 | ||
32178cab | 1346 | } |