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