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