1
1
/* Cache and manage the values of registers for GDB, the GNU debugger.
3
Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4
2001, 2002, 2004 Free Software Foundation, Inc.
3
Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4
2002, 2004, 2007 Free Software Foundation, Inc.
6
6
This file is part of GDB.
8
8
This program is free software; you can redistribute it and/or modify
9
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
10
the Free Software Foundation; either version 3 of the License, or
11
11
(at your option) any later version.
13
13
This program is distributed in the hope that it will be useful,
106
107
/* Construct a strictly RAW register cache. Don't allow pseudo's
107
108
into the register cache. */
108
descr->nr_raw_registers = NUM_REGS;
109
descr->nr_raw_registers = gdbarch_num_regs (current_gdbarch);
110
111
/* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
111
112
array. This pretects GDB from erant code that accesses elements
112
of the global register_valid_p[] array in the range [NUM_REGS
113
.. NUM_REGS + NUM_PSEUDO_REGS). */
113
of the global register_valid_p[] array in the range
114
[gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs). */
114
115
descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
116
117
/* Lay out the register cache.
270
277
memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
271
278
memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
272
279
/* Copy over any registers (identified by their membership in the
273
save_reggroup) and mark them as valid. The full [0 .. NUM_REGS +
274
NUM_PSEUDO_REGS) range is checked since some architectures need
280
save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
281
gdbarch_num_pseudo_regs) range is checked since some architectures need
275
282
to save/restore `cooked' registers that live in memory. */
276
283
for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
352
359
gdb_assert (src != NULL && dst != NULL);
353
360
gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
354
361
/* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
355
move of data into the current_regcache(). Doing this would be
362
move of data into the current regcache. Doing this would be
356
363
silly - it would mean that valid_p would be completely invalid. */
357
gdb_assert (dst != current_regcache);
364
gdb_assert (dst->readonly_p);
358
365
memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
359
366
memcpy (dst->register_valid_p, src->register_valid_p,
360
367
dst->descr->sizeof_raw_register_valid_p);
374
380
regcache_dup_no_passthrough (struct regcache *src)
376
382
struct regcache *newbuf;
377
gdb_assert (current_regcache != NULL);
378
383
newbuf = regcache_xmalloc (src->descr->gdbarch);
379
384
regcache_cpy_no_passthrough (newbuf, src);
384
regcache_valid_p (struct regcache *regcache, int regnum)
389
regcache_valid_p (const struct regcache *regcache, int regnum)
386
391
gdb_assert (regcache != NULL);
387
gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
392
gdb_assert (regnum >= 0);
393
if (regcache->readonly_p)
394
gdb_assert (regnum < regcache->descr->nr_cooked_registers);
396
gdb_assert (regnum < regcache->descr->nr_raw_registers);
388
398
return regcache->register_valid_p[regnum];
392
deprecated_grub_regcache_for_registers (struct regcache *regcache)
402
regcache_invalidate (struct regcache *regcache, int regnum)
394
return regcache->registers;
404
gdb_assert (regcache != NULL);
405
gdb_assert (regnum >= 0);
406
gdb_assert (!regcache->readonly_p);
407
gdb_assert (regnum < regcache->descr->nr_raw_registers);
408
regcache->register_valid_p[regnum] = 0;
397
412
/* Global structure containing the current regcache. */
398
413
/* FIXME: cagney/2002-05-11: The two global arrays registers[] and
399
414
deprecated_register_valid[] currently point into this structure. */
400
struct regcache *current_regcache;
415
static struct regcache *current_regcache;
402
417
/* NOTE: this is a write-through cache. There is no "dirty" bit for
403
418
recording if the register values have been changed (eg. by the
404
419
user). Therefore all registers must be written back to the
405
420
target when appropriate. */
407
/* The thread/process associated with the current set of registers. */
409
static ptid_t registers_ptid;
417
Returns 0 if the value is not in the cache (needs fetch).
418
>0 if the value is in the cache.
419
<0 if the value is permanently unavailable (don't ask again). */
422
register_cached (int regnum)
424
return current_regcache->register_valid_p[regnum];
427
/* Record that REGNUM's value is cached if STATE is >0, uncached but
428
fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
431
set_register_cached (int regnum, int state)
433
gdb_assert (regnum >= 0);
434
gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
435
current_regcache->register_valid_p[regnum] = state;
422
struct regcache *get_thread_regcache (ptid_t ptid)
424
/* NOTE: uweigand/2007-05-05: We need to detect the thread's
425
current architecture at this point. */
426
struct gdbarch *thread_gdbarch = current_gdbarch;
428
if (current_regcache && ptid_equal (current_regcache->ptid, ptid)
429
&& get_regcache_arch (current_regcache) == thread_gdbarch)
430
return current_regcache;
432
if (current_regcache)
433
regcache_xfree (current_regcache);
435
current_regcache = regcache_xmalloc (thread_gdbarch);
436
current_regcache->readonly_p = 0;
437
current_regcache->ptid = ptid;
439
return current_regcache;
442
struct regcache *get_current_regcache (void)
444
return get_thread_regcache (inferior_ptid);
438
448
/* Observer for the target_changed event. */
467
478
during lengthy interactions between gdb and the target before
468
479
gdb gives control to the user (ie watchpoints). */
471
for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
472
set_register_cached (i, 0);
474
if (deprecated_registers_changed_hook)
475
deprecated_registers_changed_hook ();
478
/* DEPRECATED_REGISTERS_FETCHED ()
480
Indicate that all registers have been fetched, so mark them all valid. */
482
/* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
483
code was blatting the registers[] array and then calling this.
484
Since targets should only be using regcache_raw_supply() the need for
485
this function/hack is eliminated. */
488
deprecated_registers_fetched (void)
492
for (i = 0; i < NUM_REGS; i++)
493
set_register_cached (i, 1);
494
/* Do not assume that the pseudo-regs have also been fetched.
495
Fetching all real regs NEVER accounts for pseudo-regs. */
498
/* deprecated_read_register_bytes and deprecated_write_register_bytes
499
are generally a *BAD* idea. They are inefficient because they need
500
to check for partial updates, which can only be done by scanning
501
through all of the registers and seeing if the bytes that are being
502
read/written fall inside of an invalid register. [The main reason
503
this is necessary is that register sizes can vary, so a simple
504
index won't suffice.] It is far better to call read_register_gen
505
and write_register_gen if you want to get at the raw register
506
contents, as it only takes a regnum as an argument, and therefore
507
can't do a partial register update.
509
Prior to the recent fixes to check for partial updates, both read
510
and deprecated_write_register_bytes always checked to see if any
511
registers were stale, and then called target_fetch_registers (-1)
512
to update the whole set. This caused really slowed things down for
515
/* Copy INLEN bytes of consecutive data from registers
516
starting with the INREGBYTE'th byte of register data
517
into memory at MYADDR. */
520
deprecated_read_register_bytes (int in_start, gdb_byte *in_buf, int in_len)
522
int in_end = in_start + in_len;
524
gdb_byte reg_buf[MAX_REGISTER_SIZE];
526
/* See if we are trying to read bytes from out-of-date registers. If so,
527
update just those registers. */
529
for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
538
reg_start = DEPRECATED_REGISTER_BYTE (regnum);
539
reg_len = register_size (current_gdbarch, regnum);
540
reg_end = reg_start + reg_len;
542
if (reg_end <= in_start || in_end <= reg_start)
543
/* The range the user wants to read doesn't overlap with regnum. */
546
if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
547
/* Force the cache to fetch the entire register. */
548
deprecated_read_register_gen (regnum, reg_buf);
550
/* Legacy note: This function, for some reason, allows a NULL
551
input buffer. If the buffer is NULL, the registers are still
552
fetched, just the final transfer is skipped. */
556
/* start = max (reg_start, in_start) */
557
if (reg_start > in_start)
562
/* end = min (reg_end, in_end) */
563
if (reg_end < in_end)
568
/* Transfer just the bytes common to both IN_BUF and REG_BUF */
569
for (byte = start; byte < end; byte++)
571
in_buf[byte - in_start] = reg_buf[byte - reg_start];
577
485
regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
584
492
On the bright side, at least there is a regcache object. */
585
493
if (!regcache->readonly_p)
587
gdb_assert (regcache == current_regcache);
588
if (! ptid_equal (registers_ptid, inferior_ptid))
495
if (!regcache_valid_p (regcache, regnum))
590
registers_changed ();
591
registers_ptid = inferior_ptid;
497
struct cleanup *old_chain = save_inferior_ptid ();
498
inferior_ptid = regcache->ptid;
499
target_fetch_registers (regcache, regnum);
500
do_cleanups (old_chain);
593
if (!register_cached (regnum))
594
target_fetch_registers (regnum);
596
503
/* FIXME: cagney/2004-08-07: At present a number of targets
597
504
forget (or didn't know that they needed) to set this leading to
735
634
regcache_raw_write (struct regcache *regcache, int regnum,
736
635
const gdb_byte *buf)
637
struct cleanup *old_chain;
738
639
gdb_assert (regcache != NULL && buf != NULL);
739
640
gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
740
641
gdb_assert (!regcache->readonly_p);
742
643
/* On the sparc, writing %g0 is a no-op, so we don't even want to
743
644
change the registers array if something writes to this register. */
744
if (CANNOT_STORE_REGISTER (regnum))
645
if (gdbarch_cannot_store_register (current_gdbarch, regnum))
747
/* Make certain that the correct cache is selected. */
748
gdb_assert (regcache == current_regcache);
749
if (! ptid_equal (registers_ptid, inferior_ptid))
751
registers_changed ();
752
registers_ptid = inferior_ptid;
755
648
/* If we have a valid copy of the register, and new value == old
756
649
value, then don't bother doing the actual store. */
757
650
if (regcache_valid_p (regcache, regnum)
759
652
regcache->descr->sizeof_register[regnum]) == 0))
762
target_prepare_to_store ();
655
old_chain = save_inferior_ptid ();
656
inferior_ptid = regcache->ptid;
658
target_prepare_to_store (regcache);
763
659
memcpy (register_buffer (regcache, regnum), buf,
764
660
regcache->descr->sizeof_register[regnum]);
765
661
regcache->register_valid_p[regnum] = 1;
766
target_store_registers (regnum);
662
target_store_registers (regcache, regnum);
770
deprecated_write_register_gen (int regnum, gdb_byte *buf)
772
gdb_assert (current_regcache != NULL);
773
gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
774
regcache_cooked_write (current_regcache, regnum, buf);
664
do_cleanups (old_chain);
790
/* Copy INLEN bytes of consecutive data from memory at MYADDR
791
into registers starting with the MYREGSTART'th byte of register data. */
794
deprecated_write_register_bytes (int myregstart, gdb_byte *myaddr, int inlen)
796
int myregend = myregstart + inlen;
799
target_prepare_to_store ();
801
/* Scan through the registers updating any that are covered by the
802
range myregstart<=>myregend using write_register_gen, which does
803
nice things like handling threads, and avoiding updates when the
804
new and old contents are the same. */
806
for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
808
int regstart, regend;
810
regstart = DEPRECATED_REGISTER_BYTE (regnum);
811
regend = regstart + register_size (current_gdbarch, regnum);
813
/* Is this register completely outside the range the user is writing? */
814
if (myregend <= regstart || regend <= myregstart)
817
/* Is this register completely within the range the user is writing? */
818
else if (myregstart <= regstart && regend <= myregend)
819
deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
821
/* The register partially overlaps the range being written. */
824
gdb_byte regbuf[MAX_REGISTER_SIZE];
825
/* What's the overlap between this register's bytes and
826
those the caller wants to write? */
827
int overlapstart = max (regstart, myregstart);
828
int overlapend = min (regend, myregend);
830
/* We may be doing a partial update of an invalid register.
831
Update it from the target before scribbling on it. */
832
deprecated_read_register_gen (regnum, regbuf);
834
target_store_registers (regnum);
839
680
/* Perform a partial register transfer using a read, modify, write
931
772
return descr->register_offset[regnum];
934
/* Hack to keep code using register_bytes working. */
937
deprecated_register_bytes (void)
939
return current_regcache->descr->sizeof_raw_registers;
942
/* Return the contents of register REGNUM as an unsigned integer. */
945
read_register (int regnum)
947
gdb_byte *buf = alloca (register_size (current_gdbarch, regnum));
948
deprecated_read_register_gen (regnum, buf);
949
return (extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)));
953
read_register_pid (int regnum, ptid_t ptid)
959
if (ptid_equal (ptid, inferior_ptid))
960
return read_register (regnum);
962
save_ptid = inferior_ptid;
964
inferior_ptid = ptid;
966
retval = read_register (regnum);
968
inferior_ptid = save_ptid;
973
/* Store VALUE into the raw contents of register number REGNUM. */
976
write_register (int regnum, LONGEST val)
980
size = register_size (current_gdbarch, regnum);
982
store_signed_integer (buf, size, (LONGEST) val);
983
deprecated_write_register_gen (regnum, buf);
987
write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
991
if (ptid_equal (ptid, inferior_ptid))
993
write_register (regnum, val);
997
save_ptid = inferior_ptid;
999
inferior_ptid = ptid;
1001
write_register (regnum, val);
1003
inferior_ptid = save_ptid;
1006
776
/* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1070
830
read_pc_pid (ptid_t ptid)
1072
ptid_t saved_inferior_ptid;
832
struct regcache *regcache = get_thread_regcache (ptid);
833
struct gdbarch *gdbarch = get_regcache_arch (regcache);
1073
835
CORE_ADDR pc_val;
1075
/* In case ptid != inferior_ptid. */
1076
saved_inferior_ptid = inferior_ptid;
1077
inferior_ptid = ptid;
1079
if (TARGET_READ_PC_P ())
1080
pc_val = TARGET_READ_PC (ptid);
837
if (gdbarch_read_pc_p (gdbarch))
838
pc_val = gdbarch_read_pc (gdbarch, regcache);
1081
839
/* Else use per-frame method on get_current_frame. */
1082
else if (PC_REGNUM >= 0)
840
else if (gdbarch_pc_regnum (current_gdbarch) >= 0)
1084
CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
1085
pc_val = ADDR_BITS_REMOVE (raw_val);
843
regcache_cooked_read_unsigned (regcache,
844
gdbarch_pc_regnum (current_gdbarch),
846
pc_val = gdbarch_addr_bits_remove (current_gdbarch, raw_val);
1088
849
internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
1090
inferior_ptid = saved_inferior_ptid;
1101
generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
861
write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1104
write_register_pid (PC_REGNUM, pc, ptid);
863
struct regcache *regcache = get_thread_regcache (ptid);
864
struct gdbarch *gdbarch = get_regcache_arch (regcache);
866
if (gdbarch_write_pc_p (gdbarch))
867
gdbarch_write_pc (gdbarch, regcache, pc);
868
else if (gdbarch_pc_regnum (current_gdbarch) >= 0)
869
regcache_cooked_write_unsigned (regcache,
870
gdbarch_pc_regnum (current_gdbarch), pc);
1106
872
internal_error (__FILE__, __LINE__,
1107
_("generic_target_write_pc"));
1111
write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1113
ptid_t saved_inferior_ptid;
1115
/* In case ptid != inferior_ptid. */
1116
saved_inferior_ptid = inferior_ptid;
1117
inferior_ptid = ptid;
1119
TARGET_WRITE_PC (pc, ptid);
1121
inferior_ptid = saved_inferior_ptid;
873
_("write_pc_pid: Unable to update PC"));
1127
879
write_pc_pid (pc, inferior_ptid);
1130
/* Cope with strage ways of getting to the stack and frame pointers */
1135
if (TARGET_READ_SP_P ())
1136
return TARGET_READ_SP ();
1137
else if (gdbarch_unwind_sp_p (current_gdbarch))
1138
return get_frame_sp (get_current_frame ());
1139
else if (SP_REGNUM >= 0)
1140
/* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1141
about the architecture so put it at the end. */
1142
return read_register (SP_REGNUM);
1143
internal_error (__FILE__, __LINE__, _("read_sp: Unable to find SP"));
1147
884
reg_flush_command (char *command, int from_tty)
1207
937
regcache->descr->sizeof_raw_registers);
1208
938
fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1209
939
regcache->descr->sizeof_raw_register_valid_p);
1210
fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1211
fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
940
fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
941
gdbarch_num_regs (current_gdbarch));
942
fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
943
gdbarch_num_pseudo_regs (current_gdbarch));
1214
946
gdb_assert (regcache->descr->nr_cooked_registers
1215
== (NUM_REGS + NUM_PSEUDO_REGS));
947
== (gdbarch_num_regs (current_gdbarch)
948
+ gdbarch_num_pseudo_regs (current_gdbarch)));
1217
950
for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1377
1112
regcache_print (char *args, enum regcache_dump_what what_to_dump)
1379
1114
if (args == NULL)
1380
regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1115
regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1383
1118
struct ui_file *file = gdb_fopen (args, "w");
1384
1119
if (file == NULL)
1385
1120
perror_with_name (_("maintenance print architecture"));
1386
regcache_dump (current_regcache, file, what_to_dump);
1121
regcache_dump (get_current_regcache (), file, what_to_dump);
1387
1122
ui_file_delete (file);
1418
1153
_initialize_regcache (void)
1420
1155
regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1421
DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
1422
deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
1424
1157
observer_attach_target_changed (regcache_observer_target_changed);
1426
1159
add_com ("flushregs", class_maintenance, reg_flush_command,
1427
1160
_("Force gdb to flush its register cache (maintainer command)"));
1429
/* Initialize the thread/process associated with the current set of
1430
registers. For now, -1 is special, and means `no current process'. */
1431
registers_ptid = pid_to_ptid (-1);
1433
1162
add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1434
1163
Print the internal register configuration.\n\
1435
1164
Takes an optional file parameter."), &maintenanceprintlist);