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