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