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