2002-11-02 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / regcache.c
CommitLineData
32178cab 1/* Cache and manage the values of registers for GDB, the GNU debugger.
3fadccb3
AC
2
3 Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4 2001, 2002 Free Software Foundation, Inc.
32178cab
MS
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23#include "defs.h"
32178cab
MS
24#include "inferior.h"
25#include "target.h"
26#include "gdbarch.h"
705152c5 27#include "gdbcmd.h"
4e052eda 28#include "regcache.h"
61a0eb5b 29#include "gdb_assert.h"
b66d6d2e 30#include "gdb_string.h"
af030b9a 31#include "gdbcmd.h" /* For maintenanceprintlist. */
32178cab
MS
32
33/*
34 * DATA STRUCTURE
35 *
36 * Here is the actual register cache.
37 */
38
3fadccb3
AC
39/* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created */
41
42struct gdbarch_data *regcache_descr_handle;
43
44struct regcache_descr
45{
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch;
48
49 /* Is this a ``legacy'' register cache? Such caches reserve space
50 for raw and pseudo registers and allow access to both. */
51 int legacy_p;
52
53 /* The raw register cache. This should contain just [0
54 .. NUM_RAW_REGISTERS). However, for older targets, it contains
55 space for the full [0 .. NUM_RAW_REGISTERS +
56 NUM_PSEUDO_REGISTERS). */
57 int nr_raw_registers;
58 long sizeof_raw_registers;
59 long sizeof_raw_register_valid_p;
60
d138e37a
AC
61 /* The cooked register space. Each cooked register in the range
62 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
63 register. The remaining [NR_RAW_REGISTERS
64 .. NR_COOKED_REGISTERS) (a.k.a. pseudo regiters) are mapped onto
65 both raw registers and memory by the architecture methods
66 gdbarch_register_read and gdbarch_register_write. */
67 int nr_cooked_registers;
68
69 /* Offset and size (in 8 bit bytes), of reach register in the
70 register cache. All registers (including those in the range
71 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
72 Assigning all registers an offset makes it possible to keep
73 legacy code, such as that found in read_register_bytes() and
74 write_register_bytes() working. */
3fadccb3 75 long *register_offset;
3fadccb3 76 long *sizeof_register;
3fadccb3 77
d138e37a
AC
78 /* Useful constant. Largest of all the registers. */
79 long max_register_size;
bb425013
AC
80
81 /* Cached table containing the type of each register. */
82 struct type **register_type;
3fadccb3
AC
83};
84
bb425013
AC
85void
86init_legacy_regcache_descr (struct gdbarch *gdbarch,
87 struct regcache_descr *descr)
3fadccb3
AC
88{
89 int i;
3fadccb3
AC
90 /* FIXME: cagney/2002-05-11: gdbarch_data() should take that
91 ``gdbarch'' as a parameter. */
92 gdb_assert (gdbarch != NULL);
93
3fadccb3
AC
94 /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
95 in the register buffer. Unfortunatly some architectures do. */
d138e37a
AC
96 descr->nr_raw_registers = descr->nr_cooked_registers;
97 descr->sizeof_raw_register_valid_p = descr->nr_cooked_registers;
3fadccb3
AC
98
99 /* FIXME: cagney/2002-05-11: Instead of using REGISTER_BYTE() this
100 code should compute the offets et.al. at runtime. This currently
101 isn't possible because some targets overlap register locations -
102 see the mess in read_register_bytes() and write_register_bytes()
103 registers. */
d138e37a
AC
104 descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
105 descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
3fadccb3 106 descr->max_register_size = 0;
d138e37a 107 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3
AC
108 {
109 descr->register_offset[i] = REGISTER_BYTE (i);
110 descr->sizeof_register[i] = REGISTER_RAW_SIZE (i);
111 if (descr->max_register_size < REGISTER_RAW_SIZE (i))
112 descr->max_register_size = REGISTER_RAW_SIZE (i);
0ed04cce
AC
113 if (descr->max_register_size < REGISTER_VIRTUAL_SIZE (i))
114 descr->max_register_size = REGISTER_VIRTUAL_SIZE (i);
3fadccb3
AC
115 }
116
117 /* Come up with the real size of the registers buffer. */
118 descr->sizeof_raw_registers = REGISTER_BYTES; /* OK use. */
d138e37a 119 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3
AC
120 {
121 long regend;
122 /* Keep extending the buffer so that there is always enough
123 space for all registers. The comparison is necessary since
124 legacy code is free to put registers in random places in the
125 buffer separated by holes. Once REGISTER_BYTE() is killed
126 this can be greatly simplified. */
127 /* FIXME: cagney/2001-12-04: This code shouldn't need to use
128 REGISTER_BYTE(). Unfortunatly, legacy code likes to lay the
129 buffer out so that certain registers just happen to overlap.
130 Ulgh! New targets use gdbarch's register read/write and
131 entirely avoid this uglyness. */
132 regend = descr->register_offset[i] + descr->sizeof_register[i];
133 if (descr->sizeof_raw_registers < regend)
134 descr->sizeof_raw_registers = regend;
135 }
3fadccb3
AC
136}
137
138static void *
139init_regcache_descr (struct gdbarch *gdbarch)
140{
141 int i;
142 struct regcache_descr *descr;
143 gdb_assert (gdbarch != NULL);
144
bb425013
AC
145 /* Create an initial, zero filled, table. */
146 descr = XCALLOC (1, struct regcache_descr);
3fadccb3 147 descr->gdbarch = gdbarch;
3fadccb3 148
d138e37a
AC
149 /* Total size of the register space. The raw registers are mapped
150 directly onto the raw register cache while the pseudo's are
3fadccb3 151 either mapped onto raw-registers or memory. */
d138e37a 152 descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
3fadccb3 153
bb425013
AC
154 /* Fill in a table of register types. */
155 descr->register_type = XCALLOC (descr->nr_cooked_registers,
156 struct type *);
157 for (i = 0; i < descr->nr_cooked_registers; i++)
158 {
159 descr->register_type[i] = REGISTER_VIRTUAL_TYPE (i);
160 }
161
162 /* If an old style architecture, fill in the remainder of the
163 register cache descriptor using the register macros. */
164 if (!gdbarch_pseudo_register_read_p (gdbarch)
165 && !gdbarch_pseudo_register_write_p (gdbarch))
166 {
167 descr->legacy_p = 1;
168 init_legacy_regcache_descr (gdbarch, descr);
169 return descr;
170 }
171
3fadccb3
AC
172 /* Construct a strictly RAW register cache. Don't allow pseudo's
173 into the register cache. */
174 descr->nr_raw_registers = NUM_REGS;
53826de9
AC
175
176 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
177 array. This pretects GDB from erant code that accesses elements
178 of the global register_valid_p[] array in the range [NUM_REGS
179 .. NUM_REGS + NUM_PSEUDO_REGS). */
180 descr->sizeof_raw_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
3fadccb3
AC
181
182 /* Lay out the register cache. The pseud-registers are included in
183 the layout even though their value isn't stored in the register
184 cache. Some code, via read_register_bytes() access a register
185 using an offset/length rather than a register number.
186
bb425013
AC
187 NOTE: cagney/2002-05-22: Only register_type() is used when
188 constructing the register cache. It is assumed that the
189 register's raw size, virtual size and type length are all the
190 same. */
3fadccb3
AC
191
192 {
193 long offset = 0;
d138e37a
AC
194 descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
195 descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
3fadccb3 196 descr->max_register_size = 0;
d138e37a 197 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3 198 {
bb425013 199 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
3fadccb3
AC
200 descr->register_offset[i] = offset;
201 offset += descr->sizeof_register[i];
202 if (descr->max_register_size < descr->sizeof_register[i])
203 descr->max_register_size = descr->sizeof_register[i];
204 }
205 /* Set the real size of the register cache buffer. */
206 /* FIXME: cagney/2002-05-22: Should only need to allocate space
207 for the raw registers. Unfortunatly some code still accesses
208 the register array directly using the global registers[].
209 Until that code has been purged, play safe and over allocating
210 the register buffer. Ulgh! */
211 descr->sizeof_raw_registers = offset;
212 /* = descr->register_offset[descr->nr_raw_registers]; */
213 }
214
215#if 0
216 /* Sanity check. Confirm that the assumptions about gdbarch are
217 true. The REGCACHE_DESCR_HANDLE is set before doing the checks
218 so that targets using the generic methods supplied by regcache
219 don't go into infinite recursion trying to, again, create the
220 regcache. */
221 set_gdbarch_data (gdbarch, regcache_descr_handle, descr);
d138e37a 222 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3
AC
223 {
224 gdb_assert (descr->sizeof_register[i] == REGISTER_RAW_SIZE (i));
225 gdb_assert (descr->sizeof_register[i] == REGISTER_VIRTUAL_SIZE (i));
226 gdb_assert (descr->register_offset[i] == REGISTER_BYTE (i));
227 }
228 /* gdb_assert (descr->sizeof_raw_registers == REGISTER_BYTES (i)); */
229#endif
230
231 return descr;
232}
233
234static struct regcache_descr *
235regcache_descr (struct gdbarch *gdbarch)
236{
237 return gdbarch_data (gdbarch, regcache_descr_handle);
238}
239
240static void
241xfree_regcache_descr (struct gdbarch *gdbarch, void *ptr)
242{
243 struct regcache_descr *descr = ptr;
244 if (descr == NULL)
245 return;
246 xfree (descr->register_offset);
247 xfree (descr->sizeof_register);
248 descr->register_offset = NULL;
249 descr->sizeof_register = NULL;
250 xfree (descr);
251}
252
bb425013
AC
253/* Utility functions returning useful register attributes stored in
254 the regcache descr. */
255
256struct type *
257register_type (struct gdbarch *gdbarch, int regnum)
258{
259 struct regcache_descr *descr = regcache_descr (gdbarch);
260 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
261 return descr->register_type[regnum];
262}
263
0ed04cce
AC
264/* Utility functions returning useful register attributes stored in
265 the regcache descr. */
266
267int
268max_register_size (struct gdbarch *gdbarch)
269{
270 struct regcache_descr *descr = regcache_descr (gdbarch);
271 return descr->max_register_size;
272}
273
3fadccb3
AC
274/* The register cache for storing raw register values. */
275
276struct regcache
277{
278 struct regcache_descr *descr;
279 char *raw_registers;
280 char *raw_register_valid_p;
281 /* If a value isn't in the cache should the corresponding target be
282 queried for a value. */
283 int passthrough_p;
284};
285
286struct regcache *
287regcache_xmalloc (struct gdbarch *gdbarch)
288{
289 struct regcache_descr *descr;
290 struct regcache *regcache;
291 gdb_assert (gdbarch != NULL);
292 descr = regcache_descr (gdbarch);
293 regcache = XMALLOC (struct regcache);
294 regcache->descr = descr;
295 regcache->raw_registers
296 = XCALLOC (descr->sizeof_raw_registers, char);
297 regcache->raw_register_valid_p
298 = XCALLOC (descr->sizeof_raw_register_valid_p, char);
299 regcache->passthrough_p = 0;
300 return regcache;
301}
302
303void
304regcache_xfree (struct regcache *regcache)
305{
306 if (regcache == NULL)
307 return;
308 xfree (regcache->raw_registers);
309 xfree (regcache->raw_register_valid_p);
310 xfree (regcache);
311}
312
36160dc4
AC
313void
314do_regcache_xfree (void *data)
315{
316 regcache_xfree (data);
317}
318
319struct cleanup *
320make_cleanup_regcache_xfree (struct regcache *regcache)
321{
322 return make_cleanup (do_regcache_xfree, regcache);
323}
324
3fadccb3
AC
325void
326regcache_cpy (struct regcache *dst, struct regcache *src)
327{
328 int i;
329 char *buf;
330 gdb_assert (src != NULL && dst != NULL);
331 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
332 gdb_assert (src != dst);
333 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
334 It keeps the existing code working where things rely on going
335 through to the register cache. */
336 if (src == current_regcache && src->descr->legacy_p)
337 {
338 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
339 untangle fetch. */
340 read_register_bytes (0, dst->raw_registers, REGISTER_BYTES);
341 return;
342 }
343 /* FIXME: cagney/2002-05-17: To say this bit is bad is being polite.
344 It keeps the existing code working where things rely on going
345 through to the register cache. */
346 if (dst == current_regcache && dst->descr->legacy_p)
347 {
348 /* ULGH!!!! Old way. Use REGISTER bytes and let code below
349 untangle fetch. */
350 write_register_bytes (0, src->raw_registers, REGISTER_BYTES);
351 return;
352 }
353 buf = alloca (src->descr->max_register_size);
354 for (i = 0; i < src->descr->nr_raw_registers; i++)
355 {
356 /* Should we worry about the valid bit here? */
0818c12a
AC
357 regcache_raw_read (src, i, buf);
358 regcache_raw_write (dst, i, buf);
3fadccb3
AC
359 }
360}
361
362void
363regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
364{
365 int i;
366 gdb_assert (src != NULL && dst != NULL);
367 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
368 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
369 move of data into the current_regcache(). Doing this would be
370 silly - it would mean that valid_p would be completly invalid. */
371 gdb_assert (dst != current_regcache);
372 memcpy (dst->raw_registers, src->raw_registers,
373 dst->descr->sizeof_raw_registers);
374 memcpy (dst->raw_register_valid_p, src->raw_register_valid_p,
375 dst->descr->sizeof_raw_register_valid_p);
376}
377
378struct regcache *
379regcache_dup (struct regcache *src)
380{
381 struct regcache *newbuf;
382 gdb_assert (current_regcache != NULL);
383 newbuf = regcache_xmalloc (src->descr->gdbarch);
384 regcache_cpy (newbuf, src);
385 return newbuf;
386}
387
388struct regcache *
389regcache_dup_no_passthrough (struct regcache *src)
390{
391 struct regcache *newbuf;
392 gdb_assert (current_regcache != NULL);
393 newbuf = regcache_xmalloc (src->descr->gdbarch);
394 regcache_cpy_no_passthrough (newbuf, src);
395 return newbuf;
396}
397
398int
399regcache_valid_p (struct regcache *regcache, int regnum)
400{
401 gdb_assert (regcache != NULL);
402 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
403 return regcache->raw_register_valid_p[regnum];
404}
405
3fadccb3
AC
406char *
407deprecated_grub_regcache_for_registers (struct regcache *regcache)
408{
409 return regcache->raw_registers;
410}
411
412char *
413deprecated_grub_regcache_for_register_valid (struct regcache *regcache)
414{
415 return regcache->raw_register_valid_p;
416}
417
418/* Global structure containing the current regcache. */
419/* FIXME: cagney/2002-05-11: The two global arrays registers[] and
420 register_valid[] currently point into this structure. */
421struct regcache *current_regcache;
422
5ebd2499 423/* NOTE: this is a write-through cache. There is no "dirty" bit for
32178cab
MS
424 recording if the register values have been changed (eg. by the
425 user). Therefore all registers must be written back to the
426 target when appropriate. */
427
428/* REGISTERS contains the cached register values (in target byte order). */
429
430char *registers;
431
432/* REGISTER_VALID is 0 if the register needs to be fetched,
433 1 if it has been fetched, and
434 -1 if the register value was not available.
c97dcfc7
AC
435
436 "Not available" indicates that the target is not not able to supply
437 the register at this state. The register may become available at a
438 later time (after the next resume). This often occures when GDB is
439 manipulating a target that contains only a snapshot of the entire
440 system being debugged - some of the registers in such a system may
441 not have been saved. */
32178cab
MS
442
443signed char *register_valid;
444
39f77062 445/* The thread/process associated with the current set of registers. */
32178cab 446
39f77062 447static ptid_t registers_ptid;
32178cab
MS
448
449/*
450 * FUNCTIONS:
451 */
452
453/* REGISTER_CACHED()
454
455 Returns 0 if the value is not in the cache (needs fetch).
456 >0 if the value is in the cache.
457 <0 if the value is permanently unavailable (don't ask again). */
458
459int
460register_cached (int regnum)
461{
462 return register_valid[regnum];
463}
464
7302a204
ND
465/* Record that REGNUM's value is cached if STATE is >0, uncached but
466 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
467
468void
469set_register_cached (int regnum, int state)
470{
53826de9
AC
471 gdb_assert (regnum >= 0);
472 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
473 current_regcache->raw_register_valid_p[regnum] = state;
7302a204
ND
474}
475
2dc4e391
DT
476/* REGISTER_CHANGED
477
478 invalidate a single register REGNUM in the cache */
479void
480register_changed (int regnum)
481{
7302a204
ND
482 set_register_cached (regnum, 0);
483}
484
485/* If REGNUM >= 0, return a pointer to register REGNUM's cache buffer area,
486 else return a pointer to the start of the cache buffer. */
487
193cb69f 488static char *
3fadccb3 489register_buffer (struct regcache *regcache, int regnum)
7302a204 490{
3fadccb3 491 return regcache->raw_registers + regcache->descr->register_offset[regnum];
7302a204
ND
492}
493
494/* Return whether register REGNUM is a real register. */
495
496static int
497real_register (int regnum)
498{
499 return regnum >= 0 && regnum < NUM_REGS;
500}
501
32178cab
MS
502/* Low level examining and depositing of registers.
503
504 The caller is responsible for making sure that the inferior is
505 stopped before calling the fetching routines, or it will get
506 garbage. (a change from GDB version 3, in which the caller got the
507 value from the last stop). */
508
509/* REGISTERS_CHANGED ()
510
511 Indicate that registers may have changed, so invalidate the cache. */
512
513void
514registers_changed (void)
515{
516 int i;
32178cab 517
39f77062 518 registers_ptid = pid_to_ptid (-1);
32178cab
MS
519
520 /* Force cleanup of any alloca areas if using C alloca instead of
521 a builtin alloca. This particular call is used to clean up
522 areas allocated by low level target code which may build up
523 during lengthy interactions between gdb and the target before
524 gdb gives control to the user (ie watchpoints). */
525 alloca (0);
526
53826de9 527 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
7302a204 528 set_register_cached (i, 0);
32178cab
MS
529
530 if (registers_changed_hook)
531 registers_changed_hook ();
532}
533
534/* REGISTERS_FETCHED ()
535
536 Indicate that all registers have been fetched, so mark them all valid. */
537
31e9866e
AC
538/* NOTE: cagney/2001-12-04: This function does not set valid on the
539 pseudo-register range since pseudo registers are always supplied
540 using supply_register(). */
541/* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
542 code was blatting the registers[] array and then calling this.
543 Since targets should only be using supply_register() the need for
544 this function/hack is eliminated. */
32178cab
MS
545
546void
547registers_fetched (void)
548{
549 int i;
32178cab 550
a728f042 551 for (i = 0; i < NUM_REGS; i++)
7302a204 552 set_register_cached (i, 1);
fcdc5976 553 /* Do not assume that the pseudo-regs have also been fetched.
31e9866e 554 Fetching all real regs NEVER accounts for pseudo-regs. */
32178cab
MS
555}
556
557/* read_register_bytes and write_register_bytes are generally a *BAD*
558 idea. They are inefficient because they need to check for partial
559 updates, which can only be done by scanning through all of the
560 registers and seeing if the bytes that are being read/written fall
561 inside of an invalid register. [The main reason this is necessary
562 is that register sizes can vary, so a simple index won't suffice.]
563 It is far better to call read_register_gen and write_register_gen
564 if you want to get at the raw register contents, as it only takes a
5ebd2499 565 regnum as an argument, and therefore can't do a partial register
32178cab
MS
566 update.
567
568 Prior to the recent fixes to check for partial updates, both read
569 and write_register_bytes always checked to see if any registers
570 were stale, and then called target_fetch_registers (-1) to update
571 the whole set. This caused really slowed things down for remote
572 targets. */
573
574/* Copy INLEN bytes of consecutive data from registers
575 starting with the INREGBYTE'th byte of register data
576 into memory at MYADDR. */
577
578void
61a0eb5b 579read_register_bytes (int in_start, char *in_buf, int in_len)
32178cab 580{
61a0eb5b 581 int in_end = in_start + in_len;
5ebd2499 582 int regnum;
61a0eb5b 583 char *reg_buf = alloca (MAX_REGISTER_RAW_SIZE);
32178cab
MS
584
585 /* See if we are trying to read bytes from out-of-date registers. If so,
586 update just those registers. */
587
5ebd2499 588 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
32178cab 589 {
61a0eb5b
AC
590 int reg_start;
591 int reg_end;
592 int reg_len;
593 int start;
594 int end;
595 int byte;
32178cab 596
61a0eb5b
AC
597 reg_start = REGISTER_BYTE (regnum);
598 reg_len = REGISTER_RAW_SIZE (regnum);
599 reg_end = reg_start + reg_len;
32178cab 600
61a0eb5b 601 if (reg_end <= in_start || in_end <= reg_start)
5ebd2499 602 /* The range the user wants to read doesn't overlap with regnum. */
32178cab
MS
603 continue;
604
275f450c
AC
605 if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
606 /* Force the cache to fetch the entire register. */
4caf0990 607 deprecated_read_register_gen (regnum, reg_buf);
275f450c
AC
608 else
609 /* Legacy note: even though this register is ``invalid'' we
610 still need to return something. It would appear that some
611 code relies on apparent gaps in the register array also
612 being returned. */
613 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
614 the entire register read/write flow of control. Must
615 resist temptation to return 0xdeadbeef. */
616 memcpy (reg_buf, registers + reg_start, reg_len);
32178cab 617
61a0eb5b
AC
618 /* Legacy note: This function, for some reason, allows a NULL
619 input buffer. If the buffer is NULL, the registers are still
620 fetched, just the final transfer is skipped. */
621 if (in_buf == NULL)
622 continue;
623
624 /* start = max (reg_start, in_start) */
625 if (reg_start > in_start)
626 start = reg_start;
627 else
628 start = in_start;
629
630 /* end = min (reg_end, in_end) */
631 if (reg_end < in_end)
632 end = reg_end;
633 else
634 end = in_end;
635
636 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
637 for (byte = start; byte < end; byte++)
165cd47f 638 {
61a0eb5b 639 in_buf[byte - in_start] = reg_buf[byte - reg_start];
165cd47f 640 }
32178cab 641 }
32178cab
MS
642}
643
5ebd2499
ND
644/* Read register REGNUM into memory at MYADDR, which must be large
645 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
32178cab
MS
646 register is known to be the size of a CORE_ADDR or smaller,
647 read_register can be used instead. */
648
61a0eb5b
AC
649static void
650legacy_read_register_gen (int regnum, char *myaddr)
32178cab 651{
61a0eb5b 652 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
39f77062 653 if (! ptid_equal (registers_ptid, inferior_ptid))
32178cab
MS
654 {
655 registers_changed ();
39f77062 656 registers_ptid = inferior_ptid;
32178cab
MS
657 }
658
7302a204 659 if (!register_cached (regnum))
5c27f28a 660 target_fetch_registers (regnum);
7302a204 661
3fadccb3 662 memcpy (myaddr, register_buffer (current_regcache, regnum),
5ebd2499 663 REGISTER_RAW_SIZE (regnum));
32178cab
MS
664}
665
61a0eb5b 666void
1aaa5f99 667regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
61a0eb5b 668{
3fadccb3
AC
669 gdb_assert (regcache != NULL && buf != NULL);
670 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
671 if (regcache->descr->legacy_p
672 && regcache->passthrough_p)
673 {
674 gdb_assert (regcache == current_regcache);
675 /* For moment, just use underlying legacy code. Ulgh!!! This
676 silently and very indirectly updates the regcache's regcache
677 via the global register_valid[]. */
678 legacy_read_register_gen (regnum, buf);
679 return;
680 }
681 /* Make certain that the register cache is up-to-date with respect
682 to the current thread. This switching shouldn't be necessary
683 only there is still only one target side register cache. Sigh!
684 On the bright side, at least there is a regcache object. */
685 if (regcache->passthrough_p)
686 {
687 gdb_assert (regcache == current_regcache);
688 if (! ptid_equal (registers_ptid, inferior_ptid))
689 {
690 registers_changed ();
691 registers_ptid = inferior_ptid;
692 }
693 if (!register_cached (regnum))
5c27f28a 694 target_fetch_registers (regnum);
3fadccb3
AC
695 }
696 /* Copy the value directly into the register cache. */
697 memcpy (buf, (regcache->raw_registers
698 + regcache->descr->register_offset[regnum]),
699 regcache->descr->sizeof_register[regnum]);
61a0eb5b
AC
700}
701
28fc6740
AC
702void
703regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
704{
705 char *buf;
706 gdb_assert (regcache != NULL);
707 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
708 buf = alloca (regcache->descr->sizeof_register[regnum]);
709 regcache_raw_read (regcache, regnum, buf);
710 (*val) = extract_signed_integer (buf,
711 regcache->descr->sizeof_register[regnum]);
712}
713
714void
715regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
716 ULONGEST *val)
717{
718 char *buf;
719 gdb_assert (regcache != NULL);
720 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
721 buf = alloca (regcache->descr->sizeof_register[regnum]);
722 regcache_raw_read (regcache, regnum, buf);
723 (*val) = extract_unsigned_integer (buf,
724 regcache->descr->sizeof_register[regnum]);
725}
726
c00dcbe9
MK
727void
728regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
729{
730 void *buf;
731 gdb_assert (regcache != NULL);
732 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
733 buf = alloca (regcache->descr->sizeof_register[regnum]);
734 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
735 regcache_raw_write (regcache, regnum, buf);
736}
737
738void
739regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
740 ULONGEST val)
741{
742 void *buf;
743 gdb_assert (regcache != NULL);
744 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
745 buf = alloca (regcache->descr->sizeof_register[regnum]);
746 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
747 regcache_raw_write (regcache, regnum, buf);
748}
749
61a0eb5b 750void
4caf0990 751deprecated_read_register_gen (int regnum, char *buf)
61a0eb5b 752{
3fadccb3
AC
753 gdb_assert (current_regcache != NULL);
754 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
755 if (current_regcache->descr->legacy_p)
61a0eb5b
AC
756 {
757 legacy_read_register_gen (regnum, buf);
758 return;
759 }
68365089
AC
760 regcache_cooked_read (current_regcache, regnum, buf);
761}
762
763void
29e1842b 764regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
68365089 765{
d138e37a 766 gdb_assert (regnum >= 0);
68365089
AC
767 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
768 if (regnum < regcache->descr->nr_raw_registers)
769 regcache_raw_read (regcache, regnum, buf);
d138e37a 770 else
68365089
AC
771 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
772 regnum, buf);
61a0eb5b
AC
773}
774
a378f419
AC
775void
776regcache_cooked_read_signed (struct regcache *regcache, int regnum,
777 LONGEST *val)
778{
779 char *buf;
780 gdb_assert (regcache != NULL);
781 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
782 buf = alloca (regcache->descr->sizeof_register[regnum]);
783 regcache_cooked_read (regcache, regnum, buf);
784 (*val) = extract_signed_integer (buf,
785 regcache->descr->sizeof_register[regnum]);
786}
787
788void
789regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
790 ULONGEST *val)
791{
792 char *buf;
793 gdb_assert (regcache != NULL);
794 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
795 buf = alloca (regcache->descr->sizeof_register[regnum]);
796 regcache_cooked_read (regcache, regnum, buf);
797 (*val) = extract_unsigned_integer (buf,
798 regcache->descr->sizeof_register[regnum]);
799}
800
5ebd2499
ND
801/* Write register REGNUM at MYADDR to the target. MYADDR points at
802 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
32178cab 803
61a0eb5b 804static void
1aaa5f99 805legacy_write_register_gen (int regnum, const void *myaddr)
32178cab
MS
806{
807 int size;
61a0eb5b 808 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
32178cab
MS
809
810 /* On the sparc, writing %g0 is a no-op, so we don't even want to
811 change the registers array if something writes to this register. */
5ebd2499 812 if (CANNOT_STORE_REGISTER (regnum))
32178cab
MS
813 return;
814
39f77062 815 if (! ptid_equal (registers_ptid, inferior_ptid))
32178cab
MS
816 {
817 registers_changed ();
39f77062 818 registers_ptid = inferior_ptid;
32178cab
MS
819 }
820
5ebd2499 821 size = REGISTER_RAW_SIZE (regnum);
32178cab 822
7302a204 823 if (real_register (regnum))
1297a2f0
MS
824 {
825 /* If we have a valid copy of the register, and new value == old
826 value, then don't bother doing the actual store. */
827 if (register_cached (regnum)
3fadccb3
AC
828 && (memcmp (register_buffer (current_regcache, regnum), myaddr, size)
829 == 0))
1297a2f0
MS
830 return;
831 else
832 target_prepare_to_store ();
833 }
32178cab 834
3fadccb3 835 memcpy (register_buffer (current_regcache, regnum), myaddr, size);
32178cab 836
7302a204 837 set_register_cached (regnum, 1);
5c27f28a 838 target_store_registers (regnum);
32178cab
MS
839}
840
61a0eb5b 841void
1aaa5f99 842regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
61a0eb5b 843{
3fadccb3
AC
844 gdb_assert (regcache != NULL && buf != NULL);
845 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
846
847 if (regcache->passthrough_p
848 && regcache->descr->legacy_p)
849 {
850 /* For moment, just use underlying legacy code. Ulgh!!! This
851 silently and very indirectly updates the regcache's buffers
852 via the globals register_valid[] and registers[]. */
853 gdb_assert (regcache == current_regcache);
854 legacy_write_register_gen (regnum, buf);
855 return;
856 }
857
858 /* On the sparc, writing %g0 is a no-op, so we don't even want to
859 change the registers array if something writes to this register. */
860 if (CANNOT_STORE_REGISTER (regnum))
861 return;
862
863 /* Handle the simple case first -> not write through so just store
864 value in cache. */
865 if (!regcache->passthrough_p)
866 {
867 memcpy ((regcache->raw_registers
868 + regcache->descr->register_offset[regnum]), buf,
869 regcache->descr->sizeof_register[regnum]);
870 regcache->raw_register_valid_p[regnum] = 1;
871 return;
872 }
873
874 /* Make certain that the correct cache is selected. */
875 gdb_assert (regcache == current_regcache);
876 if (! ptid_equal (registers_ptid, inferior_ptid))
877 {
878 registers_changed ();
879 registers_ptid = inferior_ptid;
880 }
881
882 /* If we have a valid copy of the register, and new value == old
883 value, then don't bother doing the actual store. */
884 if (regcache_valid_p (regcache, regnum)
885 && (memcmp (register_buffer (regcache, regnum), buf,
886 regcache->descr->sizeof_register[regnum]) == 0))
887 return;
888
889 target_prepare_to_store ();
890 memcpy (register_buffer (regcache, regnum), buf,
891 regcache->descr->sizeof_register[regnum]);
892 regcache->raw_register_valid_p[regnum] = 1;
5c27f28a 893 target_store_registers (regnum);
61a0eb5b
AC
894}
895
896void
4caf0990 897deprecated_write_register_gen (int regnum, char *buf)
61a0eb5b 898{
3fadccb3
AC
899 gdb_assert (current_regcache != NULL);
900 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
901 if (current_regcache->descr->legacy_p)
61a0eb5b
AC
902 {
903 legacy_write_register_gen (regnum, buf);
904 return;
905 }
68365089
AC
906 regcache_cooked_write (current_regcache, regnum, buf);
907}
908
909void
29e1842b 910regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
68365089 911{
d138e37a 912 gdb_assert (regnum >= 0);
68365089
AC
913 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
914 if (regnum < regcache->descr->nr_raw_registers)
915 regcache_raw_write (regcache, regnum, buf);
d138e37a 916 else
68365089 917 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
d8124050 918 regnum, buf);
61a0eb5b
AC
919}
920
32178cab
MS
921/* Copy INLEN bytes of consecutive data from memory at MYADDR
922 into registers starting with the MYREGSTART'th byte of register data. */
923
924void
925write_register_bytes (int myregstart, char *myaddr, int inlen)
926{
927 int myregend = myregstart + inlen;
5ebd2499 928 int regnum;
32178cab
MS
929
930 target_prepare_to_store ();
931
932 /* Scan through the registers updating any that are covered by the
933 range myregstart<=>myregend using write_register_gen, which does
934 nice things like handling threads, and avoiding updates when the
935 new and old contents are the same. */
936
5ebd2499 937 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
32178cab
MS
938 {
939 int regstart, regend;
940
5ebd2499
ND
941 regstart = REGISTER_BYTE (regnum);
942 regend = regstart + REGISTER_RAW_SIZE (regnum);
32178cab
MS
943
944 /* Is this register completely outside the range the user is writing? */
945 if (myregend <= regstart || regend <= myregstart)
946 /* do nothing */ ;
947
948 /* Is this register completely within the range the user is writing? */
949 else if (myregstart <= regstart && regend <= myregend)
4caf0990 950 deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
32178cab
MS
951
952 /* The register partially overlaps the range being written. */
953 else
954 {
e6cbd02a 955 char *regbuf = (char*) alloca (MAX_REGISTER_RAW_SIZE);
32178cab
MS
956 /* What's the overlap between this register's bytes and
957 those the caller wants to write? */
958 int overlapstart = max (regstart, myregstart);
959 int overlapend = min (regend, myregend);
960
961 /* We may be doing a partial update of an invalid register.
962 Update it from the target before scribbling on it. */
4caf0990 963 deprecated_read_register_gen (regnum, regbuf);
32178cab
MS
964
965 memcpy (registers + overlapstart,
966 myaddr + (overlapstart - myregstart),
967 overlapend - overlapstart);
968
5c27f28a 969 target_store_registers (regnum);
32178cab
MS
970 }
971 }
972}
973
06c0b04e
AC
974/* Perform a partial register transfer using a read, modify, write
975 operation. */
976
977typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
978 void *buf);
979typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
980 const void *buf);
981
982void
983regcache_xfer_part (struct regcache *regcache, int regnum,
984 int offset, int len, void *in, const void *out,
985 regcache_read_ftype *read, regcache_write_ftype *write)
986{
987 struct regcache_descr *descr = regcache->descr;
988 bfd_byte *reg = alloca (descr->max_register_size);
989 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
990 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
991 /* Something to do? */
992 if (offset + len == 0)
993 return;
994 /* Read (when needed) ... */
995 if (in != NULL
996 || offset > 0
997 || offset + len < descr->sizeof_register[regnum])
998 {
999 gdb_assert (read != NULL);
1000 read (regcache, regnum, reg);
1001 }
1002 /* ... modify ... */
1003 if (in != NULL)
1004 memcpy (in, reg + offset, len);
1005 if (out != NULL)
1006 memcpy (reg + offset, out, len);
1007 /* ... write (when needed). */
1008 if (out != NULL)
1009 {
1010 gdb_assert (write != NULL);
1011 write (regcache, regnum, reg);
1012 }
1013}
1014
1015void
1016regcache_raw_read_part (struct regcache *regcache, int regnum,
1017 int offset, int len, void *buf)
1018{
1019 struct regcache_descr *descr = regcache->descr;
1020 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1021 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1022 regcache_raw_read, regcache_raw_write);
1023}
1024
1025void
1026regcache_raw_write_part (struct regcache *regcache, int regnum,
1027 int offset, int len, const void *buf)
1028{
1029 struct regcache_descr *descr = regcache->descr;
1030 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1031 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1032 regcache_raw_read, regcache_raw_write);
1033}
1034
1035void
1036regcache_cooked_read_part (struct regcache *regcache, int regnum,
1037 int offset, int len, void *buf)
1038{
1039 struct regcache_descr *descr = regcache->descr;
1040 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1041 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1042 regcache_cooked_read, regcache_cooked_write);
1043}
1044
1045void
1046regcache_cooked_write_part (struct regcache *regcache, int regnum,
1047 int offset, int len, const void *buf)
1048{
1049 struct regcache_descr *descr = regcache->descr;
1050 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1051 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1052 regcache_cooked_read, regcache_cooked_write);
1053}
32178cab 1054
d3b22ed5
AC
1055/* Hack to keep code that view the register buffer as raw bytes
1056 working. */
1057
1058int
1059register_offset_hack (struct gdbarch *gdbarch, int regnum)
1060{
1061 struct regcache_descr *descr = regcache_descr (gdbarch);
1062 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1063 return descr->register_offset[regnum];
1064}
1065
1066static void
1067cooked_xfer_using_offset_hack (struct regcache *regcache,
1068 int buf_start, int buf_len, void *in_b,
1069 const void *out_b)
1070{
1071 struct regcache_descr *descr = regcache->descr;
1072 struct gdbarch *gdbarch = descr->gdbarch;
1073 bfd_byte *in_buf = in_b;
1074 const bfd_byte *out_buf = out_b;
1075 int buf_end = buf_start + buf_len;
1076 int regnum;
1077 char *reg_buf = alloca (descr->max_register_size);
1078
1079 /* NOTE: cagney/2002-08-17: This code assumes that the register
1080 offsets are strictly increasing and do not overlap. If this
1081 isn't the case then the bug is in the target architecture and NOT
1082 this code. */
1083
1084 /* NOTE: cagney/2002-08-17: This code assumes that only the
1085 registers covered by BUF_START:BUF_LEN should be transfered. If,
1086 for some reason, there is a gap between two registers, then that
1087 gap isn't transfered. (The gap shouldn't be there but that is
1088 another story.) */
1089
1090 /* Iterate through all registers looking for those that lie within
1091 BUF_START:BUF_LEN. */
1092
1093 for (regnum = 0; regnum < descr->nr_cooked_registers; regnum++)
1094 {
1095 /* The register's location. */
1096 int reg_start = descr->register_offset[regnum];
1097 int reg_len = descr->sizeof_register[regnum];
1098 int reg_end = reg_start + reg_len;
1099
1100 /* The START, END and LEN that falls within the current
1101 register. */
1102 int xfer_start;
1103 int xfer_end;
1104 int xfer_len;
1105
1106 /* start = max (reg_start, buf_start) */
1107 if (reg_start > buf_start)
1108 xfer_start = reg_start;
1109 else
1110 xfer_start = buf_start;
1111
1112 /* end = min (reg_end, buf_end) */
1113 if (reg_end < buf_end)
1114 xfer_end = reg_end;
1115 else
1116 xfer_end = buf_end;
1117
1118 /* The number of bytes to transfer. If there isn't anything to
1119 transfer (the end is before the start) this will be -ve. */
1120 xfer_len = xfer_end - xfer_start;
1121
1122 if (xfer_len > 0)
1123 regcache_xfer_part (regcache, regnum, xfer_start - reg_start,
1124 xfer_len, in_b, out_b, regcache_cooked_read,
1125 regcache_cooked_write);
1126 }
1127}
1128
1129void
1130regcache_cooked_read_using_offset_hack (struct regcache *regcache,
1131 int buf_start, int buf_len, void *b)
1132{
1133 cooked_xfer_using_offset_hack (regcache, buf_start, buf_len, b, NULL);
1134}
1135
1136void
1137regcache_cooked_write_using_offset_hack (struct regcache *regcache,
1138 int buf_start, int buf_len,
1139 const void *b)
1140{
1141 cooked_xfer_using_offset_hack (regcache, buf_start, buf_len, NULL, b);
1142}
1143
5ebd2499 1144/* Return the contents of register REGNUM as an unsigned integer. */
32178cab 1145
173155e8 1146ULONGEST
5ebd2499 1147read_register (int regnum)
32178cab 1148{
61a0eb5b 1149 char *buf = alloca (REGISTER_RAW_SIZE (regnum));
4caf0990 1150 deprecated_read_register_gen (regnum, buf);
61a0eb5b 1151 return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum)));
32178cab
MS
1152}
1153
173155e8 1154ULONGEST
39f77062 1155read_register_pid (int regnum, ptid_t ptid)
32178cab 1156{
39f77062 1157 ptid_t save_ptid;
32178cab
MS
1158 int save_pid;
1159 CORE_ADDR retval;
1160
39f77062 1161 if (ptid_equal (ptid, inferior_ptid))
5ebd2499 1162 return read_register (regnum);
32178cab 1163
39f77062 1164 save_ptid = inferior_ptid;
32178cab 1165
39f77062 1166 inferior_ptid = ptid;
32178cab 1167
5ebd2499 1168 retval = read_register (regnum);
32178cab 1169
39f77062 1170 inferior_ptid = save_ptid;
32178cab
MS
1171
1172 return retval;
1173}
1174
5ebd2499 1175/* Return the contents of register REGNUM as a signed integer. */
173155e8
AC
1176
1177LONGEST
5ebd2499 1178read_signed_register (int regnum)
173155e8 1179{
61a0eb5b 1180 void *buf = alloca (REGISTER_RAW_SIZE (regnum));
4caf0990 1181 deprecated_read_register_gen (regnum, buf);
61a0eb5b 1182 return (extract_signed_integer (buf, REGISTER_RAW_SIZE (regnum)));
173155e8
AC
1183}
1184
1185LONGEST
39f77062 1186read_signed_register_pid (int regnum, ptid_t ptid)
173155e8 1187{
39f77062 1188 ptid_t save_ptid;
173155e8
AC
1189 LONGEST retval;
1190
39f77062 1191 if (ptid_equal (ptid, inferior_ptid))
5ebd2499 1192 return read_signed_register (regnum);
173155e8 1193
39f77062 1194 save_ptid = inferior_ptid;
173155e8 1195
39f77062 1196 inferior_ptid = ptid;
173155e8 1197
5ebd2499 1198 retval = read_signed_register (regnum);
173155e8 1199
39f77062 1200 inferior_ptid = save_ptid;
173155e8
AC
1201
1202 return retval;
1203}
1204
5ebd2499 1205/* Store VALUE into the raw contents of register number REGNUM. */
32178cab
MS
1206
1207void
5ebd2499 1208write_register (int regnum, LONGEST val)
32178cab 1209{
61a0eb5b 1210 void *buf;
32178cab 1211 int size;
5ebd2499 1212 size = REGISTER_RAW_SIZE (regnum);
32178cab
MS
1213 buf = alloca (size);
1214 store_signed_integer (buf, size, (LONGEST) val);
4caf0990 1215 deprecated_write_register_gen (regnum, buf);
32178cab
MS
1216}
1217
1218void
39f77062 1219write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
32178cab 1220{
39f77062 1221 ptid_t save_ptid;
32178cab 1222
39f77062 1223 if (ptid_equal (ptid, inferior_ptid))
32178cab 1224 {
5ebd2499 1225 write_register (regnum, val);
32178cab
MS
1226 return;
1227 }
1228
39f77062 1229 save_ptid = inferior_ptid;
32178cab 1230
39f77062 1231 inferior_ptid = ptid;
32178cab 1232
5ebd2499 1233 write_register (regnum, val);
32178cab 1234
39f77062 1235 inferior_ptid = save_ptid;
32178cab
MS
1236}
1237
1238/* SUPPLY_REGISTER()
1239
5ebd2499 1240 Record that register REGNUM contains VAL. This is used when the
32178cab
MS
1241 value is obtained from the inferior or core dump, so there is no
1242 need to store the value there.
1243
1244 If VAL is a NULL pointer, then it's probably an unsupported register.
5ebd2499 1245 We just set its value to all zeros. We might want to record this
32178cab
MS
1246 fact, and report it to the users of read_register and friends. */
1247
1248void
1aaa5f99 1249supply_register (int regnum, const void *val)
32178cab
MS
1250{
1251#if 1
39f77062 1252 if (! ptid_equal (registers_ptid, inferior_ptid))
32178cab
MS
1253 {
1254 registers_changed ();
39f77062 1255 registers_ptid = inferior_ptid;
32178cab
MS
1256 }
1257#endif
1258
7302a204 1259 set_register_cached (regnum, 1);
32178cab 1260 if (val)
3fadccb3 1261 memcpy (register_buffer (current_regcache, regnum), val,
5ebd2499 1262 REGISTER_RAW_SIZE (regnum));
32178cab 1263 else
3fadccb3 1264 memset (register_buffer (current_regcache, regnum), '\000',
5ebd2499 1265 REGISTER_RAW_SIZE (regnum));
32178cab
MS
1266
1267 /* On some architectures, e.g. HPPA, there are a few stray bits in
1268 some registers, that the rest of the code would like to ignore. */
1269
61a0eb5b
AC
1270 /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
1271 going to be deprecated. Instead architectures will leave the raw
1272 register value as is and instead clean things up as they pass
d8124050 1273 through the method gdbarch_pseudo_register_read() clean up the
61a0eb5b
AC
1274 values. */
1275
4ee3352d 1276#ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
0b434a00
AC
1277 DEPRECATED_CLEAN_UP_REGISTER_VALUE \
1278 (regnum, register_buffer (current_regcache, regnum));
32178cab
MS
1279#endif
1280}
1281
193cb69f
AC
1282void
1283regcache_collect (int regnum, void *buf)
1284{
3fadccb3
AC
1285 memcpy (buf, register_buffer (current_regcache, regnum),
1286 REGISTER_RAW_SIZE (regnum));
193cb69f
AC
1287}
1288
1289
8227c0ff
AC
1290/* read_pc, write_pc, read_sp, write_sp, read_fp, etc. Special
1291 handling for registers PC, SP, and FP. */
32178cab 1292
4e052eda
AC
1293/* NOTE: cagney/2001-02-18: The functions generic_target_read_pc(),
1294 read_pc_pid(), read_pc(), generic_target_write_pc(),
1295 write_pc_pid(), write_pc(), generic_target_read_sp(), read_sp(),
8227c0ff
AC
1296 generic_target_write_sp(), write_sp(), generic_target_read_fp() and
1297 read_fp(), will eventually be moved out of the reg-cache into
1298 either frame.[hc] or to the multi-arch framework. The are not part
1299 of the raw register cache. */
4e052eda 1300
32178cab
MS
1301/* This routine is getting awfully cluttered with #if's. It's probably
1302 time to turn this into READ_PC and define it in the tm.h file.
1303 Ditto for write_pc.
1304
1305 1999-06-08: The following were re-written so that it assumes the
8e1a459b 1306 existence of a TARGET_READ_PC et.al. macro. A default generic
32178cab
MS
1307 version of that macro is made available where needed.
1308
1309 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
1310 by the multi-arch framework, it will eventually be possible to
1311 eliminate the intermediate read_pc_pid(). The client would call
1312 TARGET_READ_PC directly. (cagney). */
1313
32178cab 1314CORE_ADDR
39f77062 1315generic_target_read_pc (ptid_t ptid)
32178cab
MS
1316{
1317#ifdef PC_REGNUM
1318 if (PC_REGNUM >= 0)
1319 {
39f77062 1320 CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, ptid));
32178cab
MS
1321 return pc_val;
1322 }
1323#endif
8e65ff28
AC
1324 internal_error (__FILE__, __LINE__,
1325 "generic_target_read_pc");
32178cab
MS
1326 return 0;
1327}
1328
1329CORE_ADDR
39f77062 1330read_pc_pid (ptid_t ptid)
32178cab 1331{
39f77062 1332 ptid_t saved_inferior_ptid;
32178cab
MS
1333 CORE_ADDR pc_val;
1334
39f77062
KB
1335 /* In case ptid != inferior_ptid. */
1336 saved_inferior_ptid = inferior_ptid;
1337 inferior_ptid = ptid;
32178cab 1338
39f77062 1339 pc_val = TARGET_READ_PC (ptid);
32178cab 1340
39f77062 1341 inferior_ptid = saved_inferior_ptid;
32178cab
MS
1342 return pc_val;
1343}
1344
1345CORE_ADDR
1346read_pc (void)
1347{
39f77062 1348 return read_pc_pid (inferior_ptid);
32178cab
MS
1349}
1350
32178cab 1351void
39f77062 1352generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
32178cab
MS
1353{
1354#ifdef PC_REGNUM
1355 if (PC_REGNUM >= 0)
39f77062 1356 write_register_pid (PC_REGNUM, pc, ptid);
32178cab 1357 if (NPC_REGNUM >= 0)
39f77062 1358 write_register_pid (NPC_REGNUM, pc + 4, ptid);
32178cab 1359#else
8e65ff28
AC
1360 internal_error (__FILE__, __LINE__,
1361 "generic_target_write_pc");
32178cab
MS
1362#endif
1363}
1364
1365void
39f77062 1366write_pc_pid (CORE_ADDR pc, ptid_t ptid)
32178cab 1367{
39f77062 1368 ptid_t saved_inferior_ptid;
32178cab 1369
39f77062
KB
1370 /* In case ptid != inferior_ptid. */
1371 saved_inferior_ptid = inferior_ptid;
1372 inferior_ptid = ptid;
32178cab 1373
39f77062 1374 TARGET_WRITE_PC (pc, ptid);
32178cab 1375
39f77062 1376 inferior_ptid = saved_inferior_ptid;
32178cab
MS
1377}
1378
1379void
1380write_pc (CORE_ADDR pc)
1381{
39f77062 1382 write_pc_pid (pc, inferior_ptid);
32178cab
MS
1383}
1384
1385/* Cope with strage ways of getting to the stack and frame pointers */
1386
32178cab
MS
1387CORE_ADDR
1388generic_target_read_sp (void)
1389{
1390#ifdef SP_REGNUM
1391 if (SP_REGNUM >= 0)
1392 return read_register (SP_REGNUM);
1393#endif
8e65ff28
AC
1394 internal_error (__FILE__, __LINE__,
1395 "generic_target_read_sp");
32178cab
MS
1396}
1397
1398CORE_ADDR
1399read_sp (void)
1400{
1401 return TARGET_READ_SP ();
1402}
1403
32178cab
MS
1404void
1405generic_target_write_sp (CORE_ADDR val)
1406{
1407#ifdef SP_REGNUM
1408 if (SP_REGNUM >= 0)
1409 {
1410 write_register (SP_REGNUM, val);
1411 return;
1412 }
1413#endif
8e65ff28
AC
1414 internal_error (__FILE__, __LINE__,
1415 "generic_target_write_sp");
32178cab
MS
1416}
1417
1418void
1419write_sp (CORE_ADDR val)
1420{
1421 TARGET_WRITE_SP (val);
1422}
1423
32178cab
MS
1424CORE_ADDR
1425generic_target_read_fp (void)
1426{
1427#ifdef FP_REGNUM
1428 if (FP_REGNUM >= 0)
1429 return read_register (FP_REGNUM);
1430#endif
8e65ff28
AC
1431 internal_error (__FILE__, __LINE__,
1432 "generic_target_read_fp");
32178cab
MS
1433}
1434
1435CORE_ADDR
1436read_fp (void)
1437{
1438 return TARGET_READ_FP ();
1439}
1440
705152c5
MS
1441/* ARGSUSED */
1442static void
1443reg_flush_command (char *command, int from_tty)
1444{
1445 /* Force-flush the register cache. */
1446 registers_changed ();
1447 if (from_tty)
1448 printf_filtered ("Register cache flushed.\n");
1449}
1450
32178cab
MS
1451static void
1452build_regcache (void)
3fadccb3
AC
1453{
1454 current_regcache = regcache_xmalloc (current_gdbarch);
1455 current_regcache->passthrough_p = 1;
1456 registers = deprecated_grub_regcache_for_registers (current_regcache);
1457 register_valid = deprecated_grub_regcache_for_register_valid (current_regcache);
1458}
1459
af030b9a
AC
1460static void
1461dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1462 const unsigned char *buf, long len)
1463{
1464 int i;
1465 switch (endian)
1466 {
1467 case BFD_ENDIAN_BIG:
1468 for (i = 0; i < len; i++)
1469 fprintf_unfiltered (file, "%02x", buf[i]);
1470 break;
1471 case BFD_ENDIAN_LITTLE:
1472 for (i = len - 1; i >= 0; i--)
1473 fprintf_unfiltered (file, "%02x", buf[i]);
1474 break;
1475 default:
1476 internal_error (__FILE__, __LINE__, "Bad switch");
1477 }
1478}
1479
1480enum regcache_dump_what
1481{
1482 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked
1483};
1484
1485static void
1486regcache_dump (struct regcache *regcache, struct ui_file *file,
1487 enum regcache_dump_what what_to_dump)
1488{
1489 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1490 int regnum;
1491 int footnote_nr = 0;
1492 int footnote_register_size = 0;
1493 int footnote_register_offset = 0;
1494 int footnote_register_type_name_null = 0;
1495 long register_offset = 0;
1496 unsigned char *buf = alloca (regcache->descr->max_register_size);
1497
1498#if 0
1499 fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p);
1500 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1501 regcache->descr->nr_raw_registers);
1502 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1503 regcache->descr->nr_cooked_registers);
1504 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1505 regcache->descr->sizeof_raw_registers);
1506 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1507 regcache->descr->sizeof_raw_register_valid_p);
1508 fprintf_unfiltered (file, "max_register_size %ld\n",
1509 regcache->descr->max_register_size);
1510 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1511 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1512#endif
1513
1514 gdb_assert (regcache->descr->nr_cooked_registers
1515 == (NUM_REGS + NUM_PSEUDO_REGS));
1516
1517 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1518 {
1519 /* Name. */
1520 if (regnum < 0)
1521 fprintf_unfiltered (file, " %-10s", "Name");
1522 else
1523 {
1524 const char *p = REGISTER_NAME (regnum);
1525 if (p == NULL)
1526 p = "";
1527 else if (p[0] == '\0')
1528 p = "''";
1529 fprintf_unfiltered (file, " %-10s", p);
1530 }
1531
1532 /* Number. */
1533 if (regnum < 0)
1534 fprintf_unfiltered (file, " %4s", "Nr");
1535 else
1536 fprintf_unfiltered (file, " %4d", regnum);
1537
1538 /* Relative number. */
1539 if (regnum < 0)
1540 fprintf_unfiltered (file, " %4s", "Rel");
1541 else if (regnum < NUM_REGS)
1542 fprintf_unfiltered (file, " %4d", regnum);
1543 else
1544 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1545
1546 /* Offset. */
1547 if (regnum < 0)
1548 fprintf_unfiltered (file, " %6s ", "Offset");
1549 else
1550 {
1551 fprintf_unfiltered (file, " %6ld",
1552 regcache->descr->register_offset[regnum]);
a7e3c2ad 1553 if (register_offset != regcache->descr->register_offset[regnum]
d3b22ed5
AC
1554 || register_offset != REGISTER_BYTE (regnum)
1555 || (regnum > 0
1556 && (regcache->descr->register_offset[regnum]
1557 != (regcache->descr->register_offset[regnum - 1]
1558 + regcache->descr->sizeof_register[regnum - 1])))
1559 )
af030b9a
AC
1560 {
1561 if (!footnote_register_offset)
1562 footnote_register_offset = ++footnote_nr;
1563 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1564 }
1565 else
1566 fprintf_unfiltered (file, " ");
1567 register_offset = (regcache->descr->register_offset[regnum]
1568 + regcache->descr->sizeof_register[regnum]);
1569 }
1570
1571 /* Size. */
1572 if (regnum < 0)
1573 fprintf_unfiltered (file, " %5s ", "Size");
1574 else
1575 {
1576 fprintf_unfiltered (file, " %5ld",
1577 regcache->descr->sizeof_register[regnum]);
1578 if ((regcache->descr->sizeof_register[regnum]
1579 != REGISTER_RAW_SIZE (regnum))
1580 || (regcache->descr->sizeof_register[regnum]
1581 != REGISTER_VIRTUAL_SIZE (regnum))
1582 || (regcache->descr->sizeof_register[regnum]
bb425013
AC
1583 != TYPE_LENGTH (register_type (regcache->descr->gdbarch,
1584 regnum)))
af030b9a
AC
1585 )
1586 {
1587 if (!footnote_register_size)
1588 footnote_register_size = ++footnote_nr;
1589 fprintf_unfiltered (file, "*%d", footnote_register_size);
1590 }
1591 else
1592 fprintf_unfiltered (file, " ");
1593 }
1594
1595 /* Type. */
1596 if (regnum < 0)
1597 fprintf_unfiltered (file, " %-20s", "Type");
1598 else
1599 {
1600 static const char blt[] = "builtin_type";
bb425013
AC
1601 const char *t = TYPE_NAME (register_type (regcache->descr->gdbarch,
1602 regnum));
af030b9a
AC
1603 if (t == NULL)
1604 {
1605 char *n;
1606 if (!footnote_register_type_name_null)
1607 footnote_register_type_name_null = ++footnote_nr;
1608 xasprintf (&n, "*%d", footnote_register_type_name_null);
1609 make_cleanup (xfree, n);
1610 t = n;
1611 }
1612 /* Chop a leading builtin_type. */
1613 if (strncmp (t, blt, strlen (blt)) == 0)
1614 t += strlen (blt);
1615 fprintf_unfiltered (file, " %-20s", t);
1616 }
1617
1618 /* Value, raw. */
1619 if (what_to_dump == regcache_dump_raw)
1620 {
1621 if (regnum < 0)
1622 fprintf_unfiltered (file, "Raw value");
1623 else if (regnum >= regcache->descr->nr_raw_registers)
1624 fprintf_unfiltered (file, "<cooked>");
1625 else if (!regcache_valid_p (regcache, regnum))
1626 fprintf_unfiltered (file, "<invalid>");
1627 else
1628 {
1629 regcache_raw_read (regcache, regnum, buf);
1630 fprintf_unfiltered (file, "0x");
1631 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1632 REGISTER_RAW_SIZE (regnum));
1633 }
1634 }
1635
1636 /* Value, cooked. */
1637 if (what_to_dump == regcache_dump_cooked)
1638 {
1639 if (regnum < 0)
1640 fprintf_unfiltered (file, "Cooked value");
1641 else
1642 {
1643 regcache_cooked_read (regcache, regnum, buf);
1644 fprintf_unfiltered (file, "0x");
1645 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1646 REGISTER_VIRTUAL_SIZE (regnum));
1647 }
1648 }
1649
1650 fprintf_unfiltered (file, "\n");
1651 }
1652
1653 if (footnote_register_size)
1654 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1655 footnote_register_size);
1656 if (footnote_register_offset)
1657 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1658 footnote_register_offset);
1659 if (footnote_register_type_name_null)
1660 fprintf_unfiltered (file,
1661 "*%d: Register type's name NULL.\n",
1662 footnote_register_type_name_null);
1663 do_cleanups (cleanups);
1664}
1665
1666static void
1667regcache_print (char *args, enum regcache_dump_what what_to_dump)
1668{
1669 if (args == NULL)
1670 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1671 else
1672 {
1673 struct ui_file *file = gdb_fopen (args, "w");
1674 if (file == NULL)
1675 perror_with_name ("maintenance print architecture");
1676 regcache_dump (current_regcache, file, what_to_dump);
1677 ui_file_delete (file);
1678 }
1679}
1680
1681static void
1682maintenance_print_registers (char *args, int from_tty)
1683{
1684 regcache_print (args, regcache_dump_none);
1685}
1686
1687static void
1688maintenance_print_raw_registers (char *args, int from_tty)
1689{
1690 regcache_print (args, regcache_dump_raw);
1691}
1692
1693static void
1694maintenance_print_cooked_registers (char *args, int from_tty)
1695{
1696 regcache_print (args, regcache_dump_cooked);
1697}
1698
32178cab
MS
1699void
1700_initialize_regcache (void)
1701{
3fadccb3
AC
1702 regcache_descr_handle = register_gdbarch_data (init_regcache_descr,
1703 xfree_regcache_descr);
1704 REGISTER_GDBARCH_SWAP (current_regcache);
32178cab
MS
1705 register_gdbarch_swap (&registers, sizeof (registers), NULL);
1706 register_gdbarch_swap (&register_valid, sizeof (register_valid), NULL);
1707 register_gdbarch_swap (NULL, 0, build_regcache);
705152c5
MS
1708
1709 add_com ("flushregs", class_maintenance, reg_flush_command,
1710 "Force gdb to flush its register cache (maintainer command)");
39f77062
KB
1711
1712 /* Initialize the thread/process associated with the current set of
1713 registers. For now, -1 is special, and means `no current process'. */
1714 registers_ptid = pid_to_ptid (-1);
af030b9a
AC
1715
1716 add_cmd ("registers", class_maintenance,
1717 maintenance_print_registers,
1718 "Print the internal register configuration.\
1719Takes an optional file parameter.",
1720 &maintenanceprintlist);
1721 add_cmd ("raw-registers", class_maintenance,
1722 maintenance_print_raw_registers,
1723 "Print the internal register configuration including raw values.\
1724Takes an optional file parameter.",
1725 &maintenanceprintlist);
1726 add_cmd ("cooked-registers", class_maintenance,
1727 maintenance_print_cooked_registers,
1728 "Print the internal register configuration including cooked values.\
1729Takes an optional file parameter.",
1730 &maintenanceprintlist);
1731
32178cab 1732}
This page took 0.291948 seconds and 4 git commands to generate.