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