]> Git Repo - binutils.git/blame - gdb/regcache.c
Add missing function name to ChangeLog entry.
[binutils.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,
9564ee9f 4 2001, 2002, 2004 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. */
f4c5303c 33#include "observer.h"
32178cab
MS
34
35/*
36 * DATA STRUCTURE
37 *
38 * Here is the actual register cache.
39 */
40
3fadccb3
AC
41/* Per-architecture object describing the layout of a register cache.
42 Computed once when the architecture is created */
43
44struct gdbarch_data *regcache_descr_handle;
45
46struct regcache_descr
47{
48 /* The architecture this descriptor belongs to. */
49 struct gdbarch *gdbarch;
50
bb1db049
AC
51 /* The raw register cache. Each raw (or hard) register is supplied
52 by the target interface. The raw cache should not contain
53 redundant information - if the PC is constructed from two
54 registers then those regigisters and not the PC lives in the raw
55 cache. */
3fadccb3
AC
56 int nr_raw_registers;
57 long sizeof_raw_registers;
58 long sizeof_raw_register_valid_p;
59
d138e37a
AC
60 /* The cooked register space. Each cooked register in the range
61 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
62 register. The remaining [NR_RAW_REGISTERS
02f60eae 63 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
d138e37a 64 both raw registers and memory by the architecture methods
02f60eae 65 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
d138e37a 66 int nr_cooked_registers;
067df2e5
AC
67 long sizeof_cooked_registers;
68 long sizeof_cooked_register_valid_p;
d138e37a
AC
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
bb425013
AC
79 /* Cached table containing the type of each register. */
80 struct type **register_type;
3fadccb3
AC
81};
82
3fadccb3
AC
83static void *
84init_regcache_descr (struct gdbarch *gdbarch)
85{
86 int i;
87 struct regcache_descr *descr;
88 gdb_assert (gdbarch != NULL);
89
bb425013 90 /* Create an initial, zero filled, table. */
116f06ea 91 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
3fadccb3 92 descr->gdbarch = gdbarch;
3fadccb3 93
d138e37a
AC
94 /* Total size of the register space. The raw registers are mapped
95 directly onto the raw register cache while the pseudo's are
3fadccb3 96 either mapped onto raw-registers or memory. */
d138e37a 97 descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
067df2e5 98 descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
3fadccb3 99
bb425013 100 /* Fill in a table of register types. */
116f06ea
AC
101 descr->register_type
102 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
bb425013 103 for (i = 0; i < descr->nr_cooked_registers; i++)
336a3131 104 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
bb425013 105
bb1db049
AC
106 /* Construct a strictly RAW register cache. Don't allow pseudo's
107 into the register cache. */
108 descr->nr_raw_registers = NUM_REGS;
109
110 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
111 array. This pretects GDB from erant code that accesses elements
112 of the global register_valid_p[] array in the range [NUM_REGS
113 .. NUM_REGS + NUM_PSEUDO_REGS). */
114 descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
115
067df2e5 116 /* Lay out the register cache.
3fadccb3 117
bb425013
AC
118 NOTE: cagney/2002-05-22: Only register_type() is used when
119 constructing the register cache. It is assumed that the
120 register's raw size, virtual size and type length are all the
121 same. */
3fadccb3
AC
122
123 {
124 long offset = 0;
116f06ea
AC
125 descr->sizeof_register
126 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
127 descr->register_offset
128 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
d138e37a 129 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3 130 {
bb425013 131 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
3fadccb3
AC
132 descr->register_offset[i] = offset;
133 offset += descr->sizeof_register[i];
123a958e 134 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
3fadccb3
AC
135 }
136 /* Set the real size of the register cache buffer. */
067df2e5 137 descr->sizeof_cooked_registers = offset;
3fadccb3
AC
138 }
139
067df2e5 140 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
ce2826aa 141 the raw registers. Unfortunately some code still accesses the
067df2e5
AC
142 register array directly using the global registers[]. Until that
143 code has been purged, play safe and over allocating the register
144 buffer. Ulgh! */
145 descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
146
3fadccb3
AC
147 return descr;
148}
149
150static struct regcache_descr *
151regcache_descr (struct gdbarch *gdbarch)
152{
153 return gdbarch_data (gdbarch, regcache_descr_handle);
154}
155
bb425013
AC
156/* Utility functions returning useful register attributes stored in
157 the regcache descr. */
158
159struct type *
160register_type (struct gdbarch *gdbarch, int regnum)
161{
162 struct regcache_descr *descr = regcache_descr (gdbarch);
163 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
164 return descr->register_type[regnum];
165}
166
0ed04cce
AC
167/* Utility functions returning useful register attributes stored in
168 the regcache descr. */
169
08a617da
AC
170int
171register_size (struct gdbarch *gdbarch, int regnum)
172{
173 struct regcache_descr *descr = regcache_descr (gdbarch);
174 int size;
175 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
176 size = descr->sizeof_register[regnum];
08a617da
AC
177 return size;
178}
179
3fadccb3
AC
180/* The register cache for storing raw register values. */
181
182struct regcache
183{
184 struct regcache_descr *descr;
51b1fe4e
AC
185 /* The register buffers. A read-only register cache can hold the
186 full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
187 register cache can only hold [0 .. NUM_REGS). */
188 char *registers;
189 char *register_valid_p;
2d28509a
AC
190 /* Is this a read-only cache? A read-only cache is used for saving
191 the target's register state (e.g, across an inferior function
192 call or just before forcing a function return). A read-only
193 cache can only be updated via the methods regcache_dup() and
194 regcache_cpy(). The actual contents are determined by the
195 reggroup_save and reggroup_restore methods. */
196 int readonly_p;
3fadccb3
AC
197};
198
199struct regcache *
200regcache_xmalloc (struct gdbarch *gdbarch)
201{
202 struct regcache_descr *descr;
203 struct regcache *regcache;
204 gdb_assert (gdbarch != NULL);
205 descr = regcache_descr (gdbarch);
206 regcache = XMALLOC (struct regcache);
207 regcache->descr = descr;
51b1fe4e 208 regcache->registers
3fadccb3 209 = XCALLOC (descr->sizeof_raw_registers, char);
51b1fe4e 210 regcache->register_valid_p
3fadccb3 211 = XCALLOC (descr->sizeof_raw_register_valid_p, char);
2d28509a 212 regcache->readonly_p = 1;
3fadccb3
AC
213 return regcache;
214}
215
216void
217regcache_xfree (struct regcache *regcache)
218{
219 if (regcache == NULL)
220 return;
51b1fe4e
AC
221 xfree (regcache->registers);
222 xfree (regcache->register_valid_p);
3fadccb3
AC
223 xfree (regcache);
224}
225
b9362cc7 226static void
36160dc4
AC
227do_regcache_xfree (void *data)
228{
229 regcache_xfree (data);
230}
231
232struct cleanup *
233make_cleanup_regcache_xfree (struct regcache *regcache)
234{
235 return make_cleanup (do_regcache_xfree, regcache);
236}
237
41d35cb0
MK
238/* Return REGCACHE's architecture. */
239
240struct gdbarch *
241get_regcache_arch (const struct regcache *regcache)
242{
243 return regcache->descr->gdbarch;
244}
245
51b1fe4e
AC
246/* Return a pointer to register REGNUM's buffer cache. */
247
248static char *
9a661b68 249register_buffer (const struct regcache *regcache, int regnum)
51b1fe4e
AC
250{
251 return regcache->registers + regcache->descr->register_offset[regnum];
252}
253
2d28509a 254void
5602984a
AC
255regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
256 void *src)
2d28509a
AC
257{
258 struct gdbarch *gdbarch = dst->descr->gdbarch;
123a958e 259 char buf[MAX_REGISTER_SIZE];
2d28509a 260 int regnum;
2d28509a 261 /* The DST should be `read-only', if it wasn't then the save would
5602984a 262 end up trying to write the register values back out to the
2d28509a 263 target. */
2d28509a
AC
264 gdb_assert (dst->readonly_p);
265 /* Clear the dest. */
266 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
267 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
268 /* Copy over any registers (identified by their membership in the
5602984a
AC
269 save_reggroup) and mark them as valid. The full [0 .. NUM_REGS +
270 NUM_PSEUDO_REGS) range is checked since some architectures need
271 to save/restore `cooked' registers that live in memory. */
2d28509a
AC
272 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
273 {
274 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
275 {
5602984a
AC
276 int valid = cooked_read (src, regnum, buf);
277 if (valid)
278 {
279 memcpy (register_buffer (dst, regnum), buf,
280 register_size (gdbarch, regnum));
281 dst->register_valid_p[regnum] = 1;
282 }
2d28509a
AC
283 }
284 }
285}
286
287void
5602984a
AC
288regcache_restore (struct regcache *dst,
289 regcache_cooked_read_ftype *cooked_read,
290 void *src)
2d28509a
AC
291{
292 struct gdbarch *gdbarch = dst->descr->gdbarch;
123a958e 293 char buf[MAX_REGISTER_SIZE];
2d28509a 294 int regnum;
5602984a
AC
295 /* The dst had better not be read-only. If it is, the `restore'
296 doesn't make much sense. */
2d28509a 297 gdb_assert (!dst->readonly_p);
2d28509a 298 /* Copy over any registers, being careful to only restore those that
5602984a
AC
299 were both saved and need to be restored. The full [0 .. NUM_REGS
300 + NUM_PSEUDO_REGS) range is checked since some architectures need
301 to save/restore `cooked' registers that live in memory. */
302 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
2d28509a 303 {
5602984a 304 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
2d28509a 305 {
5602984a
AC
306 int valid = cooked_read (src, regnum, buf);
307 if (valid)
308 regcache_cooked_write (dst, regnum, buf);
2d28509a
AC
309 }
310 }
311}
312
5602984a
AC
313static int
314do_cooked_read (void *src, int regnum, void *buf)
315{
316 struct regcache *regcache = src;
6f4e5a41 317 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
5602984a
AC
318 /* Don't even think about fetching a register from a read-only
319 cache when the register isn't yet valid. There isn't a target
320 from which the register value can be fetched. */
321 return 0;
322 regcache_cooked_read (regcache, regnum, buf);
323 return 1;
324}
325
326
3fadccb3
AC
327void
328regcache_cpy (struct regcache *dst, struct regcache *src)
329{
330 int i;
331 char *buf;
332 gdb_assert (src != NULL && dst != NULL);
333 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
334 gdb_assert (src != dst);
2d28509a
AC
335 gdb_assert (src->readonly_p || dst->readonly_p);
336 if (!src->readonly_p)
5602984a 337 regcache_save (dst, do_cooked_read, src);
2d28509a 338 else if (!dst->readonly_p)
5602984a 339 regcache_restore (dst, do_cooked_read, src);
2d28509a
AC
340 else
341 regcache_cpy_no_passthrough (dst, src);
3fadccb3
AC
342}
343
344void
345regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
346{
347 int i;
348 gdb_assert (src != NULL && dst != NULL);
349 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
350 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
351 move of data into the current_regcache(). Doing this would be
9564ee9f 352 silly - it would mean that valid_p would be completely invalid. */
3fadccb3 353 gdb_assert (dst != current_regcache);
51b1fe4e
AC
354 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
355 memcpy (dst->register_valid_p, src->register_valid_p,
3fadccb3
AC
356 dst->descr->sizeof_raw_register_valid_p);
357}
358
359struct regcache *
360regcache_dup (struct regcache *src)
361{
362 struct regcache *newbuf;
363 gdb_assert (current_regcache != NULL);
364 newbuf = regcache_xmalloc (src->descr->gdbarch);
365 regcache_cpy (newbuf, src);
366 return newbuf;
367}
368
369struct regcache *
370regcache_dup_no_passthrough (struct regcache *src)
371{
372 struct regcache *newbuf;
373 gdb_assert (current_regcache != NULL);
374 newbuf = regcache_xmalloc (src->descr->gdbarch);
375 regcache_cpy_no_passthrough (newbuf, src);
376 return newbuf;
377}
378
379int
380regcache_valid_p (struct regcache *regcache, int regnum)
381{
382 gdb_assert (regcache != NULL);
383 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
51b1fe4e 384 return regcache->register_valid_p[regnum];
3fadccb3
AC
385}
386
3fadccb3
AC
387char *
388deprecated_grub_regcache_for_registers (struct regcache *regcache)
389{
51b1fe4e 390 return regcache->registers;
3fadccb3
AC
391}
392
3fadccb3
AC
393/* Global structure containing the current regcache. */
394/* FIXME: cagney/2002-05-11: The two global arrays registers[] and
8262ee23 395 deprecated_register_valid[] currently point into this structure. */
3fadccb3
AC
396struct regcache *current_regcache;
397
5ebd2499 398/* NOTE: this is a write-through cache. There is no "dirty" bit for
32178cab
MS
399 recording if the register values have been changed (eg. by the
400 user). Therefore all registers must be written back to the
401 target when appropriate. */
402
403/* REGISTERS contains the cached register values (in target byte order). */
404
524d7c18 405char *deprecated_registers;
32178cab 406
8262ee23 407/* DEPRECATED_REGISTER_VALID is 0 if the register needs to be fetched,
32178cab
MS
408 1 if it has been fetched, and
409 -1 if the register value was not available.
c97dcfc7
AC
410
411 "Not available" indicates that the target is not not able to supply
412 the register at this state. The register may become available at a
413 later time (after the next resume). This often occures when GDB is
414 manipulating a target that contains only a snapshot of the entire
415 system being debugged - some of the registers in such a system may
416 not have been saved. */
32178cab 417
8262ee23 418signed char *deprecated_register_valid;
32178cab 419
39f77062 420/* The thread/process associated with the current set of registers. */
32178cab 421
39f77062 422static ptid_t registers_ptid;
32178cab
MS
423
424/*
425 * FUNCTIONS:
426 */
427
428/* REGISTER_CACHED()
429
430 Returns 0 if the value is not in the cache (needs fetch).
431 >0 if the value is in the cache.
432 <0 if the value is permanently unavailable (don't ask again). */
433
434int
435register_cached (int regnum)
436{
8262ee23 437 return deprecated_register_valid[regnum];
32178cab
MS
438}
439
7302a204
ND
440/* Record that REGNUM's value is cached if STATE is >0, uncached but
441 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
442
443void
444set_register_cached (int regnum, int state)
445{
53826de9
AC
446 gdb_assert (regnum >= 0);
447 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
51b1fe4e 448 current_regcache->register_valid_p[regnum] = state;
7302a204
ND
449}
450
f4c5303c
OF
451/* Observer for the target_changed event. */
452
453void
454regcache_observer_target_changed (struct target_ops *target)
455{
456 registers_changed ();
457}
458
32178cab
MS
459/* Low level examining and depositing of registers.
460
461 The caller is responsible for making sure that the inferior is
462 stopped before calling the fetching routines, or it will get
463 garbage. (a change from GDB version 3, in which the caller got the
464 value from the last stop). */
465
466/* REGISTERS_CHANGED ()
467
468 Indicate that registers may have changed, so invalidate the cache. */
469
470void
471registers_changed (void)
472{
473 int i;
32178cab 474
39f77062 475 registers_ptid = pid_to_ptid (-1);
32178cab
MS
476
477 /* Force cleanup of any alloca areas if using C alloca instead of
478 a builtin alloca. This particular call is used to clean up
479 areas allocated by low level target code which may build up
480 during lengthy interactions between gdb and the target before
481 gdb gives control to the user (ie watchpoints). */
482 alloca (0);
483
53826de9 484 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
7302a204 485 set_register_cached (i, 0);
32178cab 486
9a4105ab
AC
487 if (deprecated_registers_changed_hook)
488 deprecated_registers_changed_hook ();
32178cab
MS
489}
490
2b9e5f3f 491/* DEPRECATED_REGISTERS_FETCHED ()
32178cab
MS
492
493 Indicate that all registers have been fetched, so mark them all valid. */
494
31e9866e
AC
495/* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
496 code was blatting the registers[] array and then calling this.
23a6d369 497 Since targets should only be using regcache_raw_supply() the need for
31e9866e 498 this function/hack is eliminated. */
32178cab
MS
499
500void
2b9e5f3f 501deprecated_registers_fetched (void)
32178cab
MS
502{
503 int i;
32178cab 504
a728f042 505 for (i = 0; i < NUM_REGS; i++)
7302a204 506 set_register_cached (i, 1);
fcdc5976 507 /* Do not assume that the pseudo-regs have also been fetched.
31e9866e 508 Fetching all real regs NEVER accounts for pseudo-regs. */
32178cab
MS
509}
510
73937e03
AC
511/* deprecated_read_register_bytes and deprecated_write_register_bytes
512 are generally a *BAD* idea. They are inefficient because they need
513 to check for partial updates, which can only be done by scanning
514 through all of the registers and seeing if the bytes that are being
515 read/written fall inside of an invalid register. [The main reason
516 this is necessary is that register sizes can vary, so a simple
517 index won't suffice.] It is far better to call read_register_gen
518 and write_register_gen if you want to get at the raw register
519 contents, as it only takes a regnum as an argument, and therefore
520 can't do a partial register update.
32178cab
MS
521
522 Prior to the recent fixes to check for partial updates, both read
73937e03
AC
523 and deprecated_write_register_bytes always checked to see if any
524 registers were stale, and then called target_fetch_registers (-1)
525 to update the whole set. This caused really slowed things down for
526 remote targets. */
32178cab
MS
527
528/* Copy INLEN bytes of consecutive data from registers
529 starting with the INREGBYTE'th byte of register data
530 into memory at MYADDR. */
531
532void
73937e03 533deprecated_read_register_bytes (int in_start, char *in_buf, int in_len)
32178cab 534{
61a0eb5b 535 int in_end = in_start + in_len;
5ebd2499 536 int regnum;
d9d9c31f 537 char reg_buf[MAX_REGISTER_SIZE];
32178cab
MS
538
539 /* See if we are trying to read bytes from out-of-date registers. If so,
540 update just those registers. */
541
5ebd2499 542 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
32178cab 543 {
61a0eb5b
AC
544 int reg_start;
545 int reg_end;
546 int reg_len;
547 int start;
548 int end;
549 int byte;
32178cab 550
62700349 551 reg_start = DEPRECATED_REGISTER_BYTE (regnum);
3acba339 552 reg_len = register_size (current_gdbarch, regnum);
61a0eb5b 553 reg_end = reg_start + reg_len;
32178cab 554
61a0eb5b 555 if (reg_end <= in_start || in_end <= reg_start)
5ebd2499 556 /* The range the user wants to read doesn't overlap with regnum. */
32178cab
MS
557 continue;
558
275f450c
AC
559 if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
560 /* Force the cache to fetch the entire register. */
4caf0990 561 deprecated_read_register_gen (regnum, reg_buf);
275f450c
AC
562 else
563 /* Legacy note: even though this register is ``invalid'' we
564 still need to return something. It would appear that some
565 code relies on apparent gaps in the register array also
566 being returned. */
567 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
568 the entire register read/write flow of control. Must
569 resist temptation to return 0xdeadbeef. */
524d7c18 570 memcpy (reg_buf, &deprecated_registers[reg_start], reg_len);
32178cab 571
61a0eb5b
AC
572 /* Legacy note: This function, for some reason, allows a NULL
573 input buffer. If the buffer is NULL, the registers are still
574 fetched, just the final transfer is skipped. */
575 if (in_buf == NULL)
576 continue;
577
578 /* start = max (reg_start, in_start) */
579 if (reg_start > in_start)
580 start = reg_start;
581 else
582 start = in_start;
583
584 /* end = min (reg_end, in_end) */
585 if (reg_end < in_end)
586 end = reg_end;
587 else
588 end = in_end;
589
590 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
591 for (byte = start; byte < end; byte++)
165cd47f 592 {
61a0eb5b 593 in_buf[byte - in_start] = reg_buf[byte - reg_start];
165cd47f 594 }
32178cab 595 }
32178cab
MS
596}
597
61a0eb5b 598void
1aaa5f99 599regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
61a0eb5b 600{
3fadccb3
AC
601 gdb_assert (regcache != NULL && buf != NULL);
602 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
3fadccb3
AC
603 /* Make certain that the register cache is up-to-date with respect
604 to the current thread. This switching shouldn't be necessary
605 only there is still only one target side register cache. Sigh!
606 On the bright side, at least there is a regcache object. */
2d28509a 607 if (!regcache->readonly_p)
3fadccb3
AC
608 {
609 gdb_assert (regcache == current_regcache);
610 if (! ptid_equal (registers_ptid, inferior_ptid))
611 {
612 registers_changed ();
613 registers_ptid = inferior_ptid;
614 }
615 if (!register_cached (regnum))
5c27f28a 616 target_fetch_registers (regnum);
3fadccb3
AC
617 }
618 /* Copy the value directly into the register cache. */
51b1fe4e 619 memcpy (buf, register_buffer (regcache, regnum),
3fadccb3 620 regcache->descr->sizeof_register[regnum]);
61a0eb5b
AC
621}
622
28fc6740
AC
623void
624regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
625{
626 char *buf;
627 gdb_assert (regcache != NULL);
628 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
629 buf = alloca (regcache->descr->sizeof_register[regnum]);
630 regcache_raw_read (regcache, regnum, buf);
631 (*val) = extract_signed_integer (buf,
632 regcache->descr->sizeof_register[regnum]);
633}
634
635void
636regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
637 ULONGEST *val)
638{
639 char *buf;
640 gdb_assert (regcache != NULL);
641 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
642 buf = alloca (regcache->descr->sizeof_register[regnum]);
643 regcache_raw_read (regcache, regnum, buf);
644 (*val) = extract_unsigned_integer (buf,
645 regcache->descr->sizeof_register[regnum]);
646}
647
c00dcbe9
MK
648void
649regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
650{
651 void *buf;
652 gdb_assert (regcache != NULL);
653 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
654 buf = alloca (regcache->descr->sizeof_register[regnum]);
655 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
656 regcache_raw_write (regcache, regnum, buf);
657}
658
659void
660regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
661 ULONGEST val)
662{
663 void *buf;
664 gdb_assert (regcache != NULL);
665 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
666 buf = alloca (regcache->descr->sizeof_register[regnum]);
667 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
668 regcache_raw_write (regcache, regnum, buf);
669}
670
61a0eb5b 671void
4caf0990 672deprecated_read_register_gen (int regnum, char *buf)
61a0eb5b 673{
3fadccb3
AC
674 gdb_assert (current_regcache != NULL);
675 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
68365089
AC
676 regcache_cooked_read (current_regcache, regnum, buf);
677}
678
679void
29e1842b 680regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
68365089 681{
d138e37a 682 gdb_assert (regnum >= 0);
68365089
AC
683 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
684 if (regnum < regcache->descr->nr_raw_registers)
685 regcache_raw_read (regcache, regnum, buf);
2d28509a
AC
686 else if (regcache->readonly_p
687 && regnum < regcache->descr->nr_cooked_registers
688 && regcache->register_valid_p[regnum])
b2fa5097 689 /* Read-only register cache, perhaps the cooked value was cached? */
2d28509a
AC
690 memcpy (buf, register_buffer (regcache, regnum),
691 regcache->descr->sizeof_register[regnum]);
d138e37a 692 else
68365089
AC
693 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
694 regnum, buf);
61a0eb5b
AC
695}
696
a378f419
AC
697void
698regcache_cooked_read_signed (struct regcache *regcache, int regnum,
699 LONGEST *val)
700{
701 char *buf;
702 gdb_assert (regcache != NULL);
a66a9c23 703 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
704 buf = alloca (regcache->descr->sizeof_register[regnum]);
705 regcache_cooked_read (regcache, regnum, buf);
706 (*val) = extract_signed_integer (buf,
707 regcache->descr->sizeof_register[regnum]);
708}
709
710void
711regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
712 ULONGEST *val)
713{
714 char *buf;
715 gdb_assert (regcache != NULL);
a66a9c23 716 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
717 buf = alloca (regcache->descr->sizeof_register[regnum]);
718 regcache_cooked_read (regcache, regnum, buf);
719 (*val) = extract_unsigned_integer (buf,
720 regcache->descr->sizeof_register[regnum]);
721}
722
a66a9c23
AC
723void
724regcache_cooked_write_signed (struct regcache *regcache, int regnum,
725 LONGEST val)
726{
727 void *buf;
728 gdb_assert (regcache != NULL);
729 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
730 buf = alloca (regcache->descr->sizeof_register[regnum]);
731 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
732 regcache_cooked_write (regcache, regnum, buf);
733}
734
735void
736regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
737 ULONGEST val)
738{
739 void *buf;
740 gdb_assert (regcache != NULL);
741 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
742 buf = alloca (regcache->descr->sizeof_register[regnum]);
743 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
744 regcache_cooked_write (regcache, regnum, buf);
745}
746
61a0eb5b 747void
1aaa5f99 748regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
61a0eb5b 749{
3fadccb3
AC
750 gdb_assert (regcache != NULL && buf != NULL);
751 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
2d28509a 752 gdb_assert (!regcache->readonly_p);
3fadccb3 753
3fadccb3
AC
754 /* On the sparc, writing %g0 is a no-op, so we don't even want to
755 change the registers array if something writes to this register. */
756 if (CANNOT_STORE_REGISTER (regnum))
757 return;
758
3fadccb3
AC
759 /* Make certain that the correct cache is selected. */
760 gdb_assert (regcache == current_regcache);
761 if (! ptid_equal (registers_ptid, inferior_ptid))
762 {
763 registers_changed ();
764 registers_ptid = inferior_ptid;
765 }
766
767 /* If we have a valid copy of the register, and new value == old
768 value, then don't bother doing the actual store. */
769 if (regcache_valid_p (regcache, regnum)
770 && (memcmp (register_buffer (regcache, regnum), buf,
771 regcache->descr->sizeof_register[regnum]) == 0))
772 return;
773
774 target_prepare_to_store ();
775 memcpy (register_buffer (regcache, regnum), buf,
776 regcache->descr->sizeof_register[regnum]);
51b1fe4e 777 regcache->register_valid_p[regnum] = 1;
5c27f28a 778 target_store_registers (regnum);
61a0eb5b
AC
779}
780
781void
4caf0990 782deprecated_write_register_gen (int regnum, char *buf)
61a0eb5b 783{
3fadccb3
AC
784 gdb_assert (current_regcache != NULL);
785 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
68365089
AC
786 regcache_cooked_write (current_regcache, regnum, buf);
787}
788
789void
29e1842b 790regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
68365089 791{
d138e37a 792 gdb_assert (regnum >= 0);
68365089
AC
793 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
794 if (regnum < regcache->descr->nr_raw_registers)
795 regcache_raw_write (regcache, regnum, buf);
d138e37a 796 else
68365089 797 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
d8124050 798 regnum, buf);
61a0eb5b
AC
799}
800
32178cab
MS
801/* Copy INLEN bytes of consecutive data from memory at MYADDR
802 into registers starting with the MYREGSTART'th byte of register data. */
803
804void
73937e03 805deprecated_write_register_bytes (int myregstart, char *myaddr, int inlen)
32178cab
MS
806{
807 int myregend = myregstart + inlen;
5ebd2499 808 int regnum;
32178cab
MS
809
810 target_prepare_to_store ();
811
812 /* Scan through the registers updating any that are covered by the
813 range myregstart<=>myregend using write_register_gen, which does
814 nice things like handling threads, and avoiding updates when the
815 new and old contents are the same. */
816
5ebd2499 817 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
32178cab
MS
818 {
819 int regstart, regend;
820
62700349 821 regstart = DEPRECATED_REGISTER_BYTE (regnum);
3acba339 822 regend = regstart + register_size (current_gdbarch, regnum);
32178cab
MS
823
824 /* Is this register completely outside the range the user is writing? */
825 if (myregend <= regstart || regend <= myregstart)
826 /* do nothing */ ;
827
828 /* Is this register completely within the range the user is writing? */
829 else if (myregstart <= regstart && regend <= myregend)
4caf0990 830 deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
32178cab
MS
831
832 /* The register partially overlaps the range being written. */
833 else
834 {
d9d9c31f 835 char regbuf[MAX_REGISTER_SIZE];
32178cab
MS
836 /* What's the overlap between this register's bytes and
837 those the caller wants to write? */
838 int overlapstart = max (regstart, myregstart);
839 int overlapend = min (regend, myregend);
840
841 /* We may be doing a partial update of an invalid register.
842 Update it from the target before scribbling on it. */
4caf0990 843 deprecated_read_register_gen (regnum, regbuf);
32178cab 844
524d7c18 845 memcpy (&deprecated_registers[overlapstart],
32178cab
MS
846 myaddr + (overlapstart - myregstart),
847 overlapend - overlapstart);
848
5c27f28a 849 target_store_registers (regnum);
32178cab
MS
850 }
851 }
852}
853
06c0b04e
AC
854/* Perform a partial register transfer using a read, modify, write
855 operation. */
856
857typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
858 void *buf);
859typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
860 const void *buf);
861
b9362cc7 862static void
06c0b04e
AC
863regcache_xfer_part (struct regcache *regcache, int regnum,
864 int offset, int len, void *in, const void *out,
865 regcache_read_ftype *read, regcache_write_ftype *write)
866{
867 struct regcache_descr *descr = regcache->descr;
123a958e 868 bfd_byte reg[MAX_REGISTER_SIZE];
06c0b04e
AC
869 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
870 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
871 /* Something to do? */
872 if (offset + len == 0)
873 return;
874 /* Read (when needed) ... */
875 if (in != NULL
876 || offset > 0
877 || offset + len < descr->sizeof_register[regnum])
878 {
879 gdb_assert (read != NULL);
880 read (regcache, regnum, reg);
881 }
882 /* ... modify ... */
883 if (in != NULL)
884 memcpy (in, reg + offset, len);
885 if (out != NULL)
886 memcpy (reg + offset, out, len);
887 /* ... write (when needed). */
888 if (out != NULL)
889 {
890 gdb_assert (write != NULL);
891 write (regcache, regnum, reg);
892 }
893}
894
895void
896regcache_raw_read_part (struct regcache *regcache, int regnum,
897 int offset, int len, void *buf)
898{
899 struct regcache_descr *descr = regcache->descr;
900 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
901 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
902 regcache_raw_read, regcache_raw_write);
903}
904
905void
906regcache_raw_write_part (struct regcache *regcache, int regnum,
907 int offset, int len, const void *buf)
908{
909 struct regcache_descr *descr = regcache->descr;
910 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
911 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
912 regcache_raw_read, regcache_raw_write);
913}
914
915void
916regcache_cooked_read_part (struct regcache *regcache, int regnum,
917 int offset, int len, void *buf)
918{
919 struct regcache_descr *descr = regcache->descr;
920 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
921 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
922 regcache_cooked_read, regcache_cooked_write);
923}
924
925void
926regcache_cooked_write_part (struct regcache *regcache, int regnum,
927 int offset, int len, const void *buf)
928{
929 struct regcache_descr *descr = regcache->descr;
930 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
931 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
932 regcache_cooked_read, regcache_cooked_write);
933}
32178cab 934
d3b22ed5
AC
935/* Hack to keep code that view the register buffer as raw bytes
936 working. */
937
938int
939register_offset_hack (struct gdbarch *gdbarch, int regnum)
940{
941 struct regcache_descr *descr = regcache_descr (gdbarch);
942 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
943 return descr->register_offset[regnum];
944}
945
f42accbe
AC
946/* Hack to keep code using register_bytes working. */
947
948int
949deprecated_register_bytes (void)
950{
951 return current_regcache->descr->sizeof_raw_registers;
952}
953
5ebd2499 954/* Return the contents of register REGNUM as an unsigned integer. */
32178cab 955
173155e8 956ULONGEST
5ebd2499 957read_register (int regnum)
32178cab 958{
3acba339 959 char *buf = alloca (register_size (current_gdbarch, regnum));
4caf0990 960 deprecated_read_register_gen (regnum, buf);
3acba339 961 return (extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)));
32178cab
MS
962}
963
173155e8 964ULONGEST
39f77062 965read_register_pid (int regnum, ptid_t ptid)
32178cab 966{
39f77062 967 ptid_t save_ptid;
32178cab
MS
968 int save_pid;
969 CORE_ADDR retval;
970
39f77062 971 if (ptid_equal (ptid, inferior_ptid))
5ebd2499 972 return read_register (regnum);
32178cab 973
39f77062 974 save_ptid = inferior_ptid;
32178cab 975
39f77062 976 inferior_ptid = ptid;
32178cab 977
5ebd2499 978 retval = read_register (regnum);
32178cab 979
39f77062 980 inferior_ptid = save_ptid;
32178cab
MS
981
982 return retval;
983}
984
5ebd2499 985/* Store VALUE into the raw contents of register number REGNUM. */
32178cab
MS
986
987void
5ebd2499 988write_register (int regnum, LONGEST val)
32178cab 989{
61a0eb5b 990 void *buf;
32178cab 991 int size;
3acba339 992 size = register_size (current_gdbarch, regnum);
32178cab
MS
993 buf = alloca (size);
994 store_signed_integer (buf, size, (LONGEST) val);
4caf0990 995 deprecated_write_register_gen (regnum, buf);
32178cab
MS
996}
997
998void
39f77062 999write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
32178cab 1000{
39f77062 1001 ptid_t save_ptid;
32178cab 1002
39f77062 1003 if (ptid_equal (ptid, inferior_ptid))
32178cab 1004 {
5ebd2499 1005 write_register (regnum, val);
32178cab
MS
1006 return;
1007 }
1008
39f77062 1009 save_ptid = inferior_ptid;
32178cab 1010
39f77062 1011 inferior_ptid = ptid;
32178cab 1012
5ebd2499 1013 write_register (regnum, val);
32178cab 1014
39f77062 1015 inferior_ptid = save_ptid;
32178cab
MS
1016}
1017
a16d75cc 1018/* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
9a661b68
MK
1019
1020void
1021regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1022{
1023 void *regbuf;
1024 size_t size;
1025
a16d75cc 1026 gdb_assert (regcache != NULL);
9a661b68
MK
1027 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1028 gdb_assert (!regcache->readonly_p);
1029
1030 /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
1031 CURRENT_REGCACHE specially here. */
1032 if (regcache == current_regcache
1033 && !ptid_equal (registers_ptid, inferior_ptid))
1034 {
1035 registers_changed ();
1036 registers_ptid = inferior_ptid;
1037 }
1038
1039 regbuf = register_buffer (regcache, regnum);
1040 size = regcache->descr->sizeof_register[regnum];
1041
1042 if (buf)
1043 memcpy (regbuf, buf, size);
1044 else
1045 memset (regbuf, 0, size);
1046
1047 /* Mark the register as cached. */
1048 regcache->register_valid_p[regnum] = 1;
1049}
1050
1051/* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1052
1053void
1054regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1055{
1056 const void *regbuf;
1057 size_t size;
1058
1059 gdb_assert (regcache != NULL && buf != NULL);
1060 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1061
1062 regbuf = register_buffer (regcache, regnum);
1063 size = regcache->descr->sizeof_register[regnum];
1064 memcpy (buf, regbuf, size);
1065}
1066
193cb69f 1067
0ba6dca9
AC
1068/* read_pc, write_pc, read_sp, deprecated_read_fp, etc. Special
1069 handling for registers PC, SP, and FP. */
32178cab 1070
cde9ea48
AC
1071/* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc(),
1072 read_sp(), and deprecated_read_fp(), will eventually be replaced by
1073 per-frame methods. Instead of relying on the global INFERIOR_PTID,
1074 they will use the contextual information provided by the FRAME.
1075 These functions do not belong in the register cache. */
32178cab 1076
cde9ea48
AC
1077/* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
1078 write_pc_pid(), write_pc(), and deprecated_read_fp(), all need to
1079 be replaced by something that does not rely on global state. But
1080 what? */
32178cab
MS
1081
1082CORE_ADDR
39f77062 1083read_pc_pid (ptid_t ptid)
32178cab 1084{
39f77062 1085 ptid_t saved_inferior_ptid;
32178cab
MS
1086 CORE_ADDR pc_val;
1087
39f77062
KB
1088 /* In case ptid != inferior_ptid. */
1089 saved_inferior_ptid = inferior_ptid;
1090 inferior_ptid = ptid;
32178cab 1091
cde9ea48
AC
1092 if (TARGET_READ_PC_P ())
1093 pc_val = TARGET_READ_PC (ptid);
1094 /* Else use per-frame method on get_current_frame. */
1095 else if (PC_REGNUM >= 0)
1096 {
1097 CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
6ba34a8d 1098 pc_val = ADDR_BITS_REMOVE (raw_val);
cde9ea48
AC
1099 }
1100 else
1101 internal_error (__FILE__, __LINE__, "read_pc_pid: Unable to find PC");
32178cab 1102
39f77062 1103 inferior_ptid = saved_inferior_ptid;
32178cab
MS
1104 return pc_val;
1105}
1106
1107CORE_ADDR
1108read_pc (void)
1109{
39f77062 1110 return read_pc_pid (inferior_ptid);
32178cab
MS
1111}
1112
32178cab 1113void
39f77062 1114generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
32178cab 1115{
32178cab 1116 if (PC_REGNUM >= 0)
39f77062 1117 write_register_pid (PC_REGNUM, pc, ptid);
afb18d0f
AC
1118 else
1119 internal_error (__FILE__, __LINE__,
1120 "generic_target_write_pc");
32178cab
MS
1121}
1122
1123void
39f77062 1124write_pc_pid (CORE_ADDR pc, ptid_t ptid)
32178cab 1125{
39f77062 1126 ptid_t saved_inferior_ptid;
32178cab 1127
39f77062
KB
1128 /* In case ptid != inferior_ptid. */
1129 saved_inferior_ptid = inferior_ptid;
1130 inferior_ptid = ptid;
32178cab 1131
39f77062 1132 TARGET_WRITE_PC (pc, ptid);
32178cab 1133
39f77062 1134 inferior_ptid = saved_inferior_ptid;
32178cab
MS
1135}
1136
1137void
1138write_pc (CORE_ADDR pc)
1139{
39f77062 1140 write_pc_pid (pc, inferior_ptid);
32178cab
MS
1141}
1142
1143/* Cope with strage ways of getting to the stack and frame pointers */
1144
32178cab
MS
1145CORE_ADDR
1146read_sp (void)
1147{
bd1ce8ba
AC
1148 if (TARGET_READ_SP_P ())
1149 return TARGET_READ_SP ();
a9e5fdc2
AC
1150 else if (gdbarch_unwind_sp_p (current_gdbarch))
1151 return get_frame_sp (get_current_frame ());
bd1ce8ba 1152 else if (SP_REGNUM >= 0)
a9e5fdc2
AC
1153 /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1154 about the architecture so put it at the end. */
bd1ce8ba
AC
1155 return read_register (SP_REGNUM);
1156 internal_error (__FILE__, __LINE__, "read_sp: Unable to find SP");
32178cab
MS
1157}
1158
32178cab 1159void
b46e02f6 1160deprecated_write_sp (CORE_ADDR val)
32178cab 1161{
b46e02f6
AC
1162 gdb_assert (SP_REGNUM >= 0);
1163 write_register (SP_REGNUM, val);
32178cab
MS
1164}
1165
32178cab 1166CORE_ADDR
0ba6dca9 1167deprecated_read_fp (void)
32178cab 1168{
331ae7ed 1169 if (DEPRECATED_FP_REGNUM >= 0)
0ba6dca9
AC
1170 return read_register (DEPRECATED_FP_REGNUM);
1171 else
1172 internal_error (__FILE__, __LINE__, "deprecated_read_fp");
32178cab
MS
1173}
1174
705152c5
MS
1175static void
1176reg_flush_command (char *command, int from_tty)
1177{
1178 /* Force-flush the register cache. */
1179 registers_changed ();
1180 if (from_tty)
1181 printf_filtered ("Register cache flushed.\n");
1182}
1183
32178cab
MS
1184static void
1185build_regcache (void)
3fadccb3
AC
1186{
1187 current_regcache = regcache_xmalloc (current_gdbarch);
2d28509a 1188 current_regcache->readonly_p = 0;
524d7c18 1189 deprecated_registers = deprecated_grub_regcache_for_registers (current_regcache);
b923b08d 1190 deprecated_register_valid = current_regcache->register_valid_p;
3fadccb3
AC
1191}
1192
af030b9a
AC
1193static void
1194dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1195 const unsigned char *buf, long len)
1196{
1197 int i;
1198 switch (endian)
1199 {
1200 case BFD_ENDIAN_BIG:
1201 for (i = 0; i < len; i++)
1202 fprintf_unfiltered (file, "%02x", buf[i]);
1203 break;
1204 case BFD_ENDIAN_LITTLE:
1205 for (i = len - 1; i >= 0; i--)
1206 fprintf_unfiltered (file, "%02x", buf[i]);
1207 break;
1208 default:
1209 internal_error (__FILE__, __LINE__, "Bad switch");
1210 }
1211}
1212
1213enum regcache_dump_what
1214{
b59ff9d5 1215 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
af030b9a
AC
1216};
1217
1218static void
1219regcache_dump (struct regcache *regcache, struct ui_file *file,
1220 enum regcache_dump_what what_to_dump)
1221{
1222 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
b59ff9d5 1223 struct gdbarch *gdbarch = regcache->descr->gdbarch;
af030b9a
AC
1224 int regnum;
1225 int footnote_nr = 0;
1226 int footnote_register_size = 0;
1227 int footnote_register_offset = 0;
1228 int footnote_register_type_name_null = 0;
1229 long register_offset = 0;
123a958e 1230 unsigned char buf[MAX_REGISTER_SIZE];
af030b9a
AC
1231
1232#if 0
af030b9a
AC
1233 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1234 regcache->descr->nr_raw_registers);
1235 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1236 regcache->descr->nr_cooked_registers);
1237 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1238 regcache->descr->sizeof_raw_registers);
1239 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1240 regcache->descr->sizeof_raw_register_valid_p);
af030b9a
AC
1241 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1242 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1243#endif
1244
1245 gdb_assert (regcache->descr->nr_cooked_registers
1246 == (NUM_REGS + NUM_PSEUDO_REGS));
1247
1248 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1249 {
1250 /* Name. */
1251 if (regnum < 0)
1252 fprintf_unfiltered (file, " %-10s", "Name");
1253 else
1254 {
1255 const char *p = REGISTER_NAME (regnum);
1256 if (p == NULL)
1257 p = "";
1258 else if (p[0] == '\0')
1259 p = "''";
1260 fprintf_unfiltered (file, " %-10s", p);
1261 }
1262
1263 /* Number. */
1264 if (regnum < 0)
1265 fprintf_unfiltered (file, " %4s", "Nr");
1266 else
1267 fprintf_unfiltered (file, " %4d", regnum);
1268
1269 /* Relative number. */
1270 if (regnum < 0)
1271 fprintf_unfiltered (file, " %4s", "Rel");
1272 else if (regnum < NUM_REGS)
1273 fprintf_unfiltered (file, " %4d", regnum);
1274 else
1275 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1276
1277 /* Offset. */
1278 if (regnum < 0)
1279 fprintf_unfiltered (file, " %6s ", "Offset");
1280 else
1281 {
1282 fprintf_unfiltered (file, " %6ld",
1283 regcache->descr->register_offset[regnum]);
a7e3c2ad 1284 if (register_offset != regcache->descr->register_offset[regnum]
62700349 1285 || register_offset != DEPRECATED_REGISTER_BYTE (regnum)
d3b22ed5
AC
1286 || (regnum > 0
1287 && (regcache->descr->register_offset[regnum]
1288 != (regcache->descr->register_offset[regnum - 1]
1289 + regcache->descr->sizeof_register[regnum - 1])))
1290 )
af030b9a
AC
1291 {
1292 if (!footnote_register_offset)
1293 footnote_register_offset = ++footnote_nr;
1294 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1295 }
1296 else
1297 fprintf_unfiltered (file, " ");
1298 register_offset = (regcache->descr->register_offset[regnum]
1299 + regcache->descr->sizeof_register[regnum]);
1300 }
1301
1302 /* Size. */
1303 if (regnum < 0)
1304 fprintf_unfiltered (file, " %5s ", "Size");
1305 else
01e1877c
AC
1306 fprintf_unfiltered (file, " %5ld",
1307 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1308
1309 /* Type. */
b59ff9d5
AC
1310 {
1311 const char *t;
1312 if (regnum < 0)
1313 t = "Type";
1314 else
1315 {
1316 static const char blt[] = "builtin_type";
1317 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1318 if (t == NULL)
1319 {
1320 char *n;
1321 if (!footnote_register_type_name_null)
1322 footnote_register_type_name_null = ++footnote_nr;
b435e160 1323 n = xstrprintf ("*%d", footnote_register_type_name_null);
b59ff9d5
AC
1324 make_cleanup (xfree, n);
1325 t = n;
1326 }
1327 /* Chop a leading builtin_type. */
1328 if (strncmp (t, blt, strlen (blt)) == 0)
1329 t += strlen (blt);
1330 }
1331 fprintf_unfiltered (file, " %-15s", t);
1332 }
1333
1334 /* Leading space always present. */
1335 fprintf_unfiltered (file, " ");
af030b9a
AC
1336
1337 /* Value, raw. */
1338 if (what_to_dump == regcache_dump_raw)
1339 {
1340 if (regnum < 0)
1341 fprintf_unfiltered (file, "Raw value");
1342 else if (regnum >= regcache->descr->nr_raw_registers)
1343 fprintf_unfiltered (file, "<cooked>");
1344 else if (!regcache_valid_p (regcache, regnum))
1345 fprintf_unfiltered (file, "<invalid>");
1346 else
1347 {
1348 regcache_raw_read (regcache, regnum, buf);
1349 fprintf_unfiltered (file, "0x");
1350 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
01e1877c 1351 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1352 }
1353 }
1354
1355 /* Value, cooked. */
1356 if (what_to_dump == regcache_dump_cooked)
1357 {
1358 if (regnum < 0)
1359 fprintf_unfiltered (file, "Cooked value");
1360 else
1361 {
1362 regcache_cooked_read (regcache, regnum, buf);
1363 fprintf_unfiltered (file, "0x");
1364 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
01e1877c 1365 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1366 }
1367 }
1368
b59ff9d5
AC
1369 /* Group members. */
1370 if (what_to_dump == regcache_dump_groups)
1371 {
1372 if (regnum < 0)
1373 fprintf_unfiltered (file, "Groups");
1374 else
1375 {
b59ff9d5 1376 const char *sep = "";
6c7d17ba
AC
1377 struct reggroup *group;
1378 for (group = reggroup_next (gdbarch, NULL);
1379 group != NULL;
1380 group = reggroup_next (gdbarch, group))
b59ff9d5 1381 {
6c7d17ba 1382 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
b59ff9d5 1383 {
6c7d17ba 1384 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
b59ff9d5
AC
1385 sep = ",";
1386 }
1387 }
1388 }
1389 }
1390
af030b9a
AC
1391 fprintf_unfiltered (file, "\n");
1392 }
1393
1394 if (footnote_register_size)
1395 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1396 footnote_register_size);
1397 if (footnote_register_offset)
1398 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1399 footnote_register_offset);
1400 if (footnote_register_type_name_null)
1401 fprintf_unfiltered (file,
1402 "*%d: Register type's name NULL.\n",
1403 footnote_register_type_name_null);
1404 do_cleanups (cleanups);
1405}
1406
1407static void
1408regcache_print (char *args, enum regcache_dump_what what_to_dump)
1409{
1410 if (args == NULL)
1411 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1412 else
1413 {
1414 struct ui_file *file = gdb_fopen (args, "w");
1415 if (file == NULL)
1416 perror_with_name ("maintenance print architecture");
1417 regcache_dump (current_regcache, file, what_to_dump);
1418 ui_file_delete (file);
1419 }
1420}
1421
1422static void
1423maintenance_print_registers (char *args, int from_tty)
1424{
1425 regcache_print (args, regcache_dump_none);
1426}
1427
1428static void
1429maintenance_print_raw_registers (char *args, int from_tty)
1430{
1431 regcache_print (args, regcache_dump_raw);
1432}
1433
1434static void
1435maintenance_print_cooked_registers (char *args, int from_tty)
1436{
1437 regcache_print (args, regcache_dump_cooked);
1438}
1439
b59ff9d5
AC
1440static void
1441maintenance_print_register_groups (char *args, int from_tty)
1442{
1443 regcache_print (args, regcache_dump_groups);
1444}
1445
b9362cc7
AC
1446extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1447
32178cab
MS
1448void
1449_initialize_regcache (void)
1450{
030f20e1 1451 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
046a4708
AC
1452 DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
1453 DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_registers);
1454 DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_register_valid);
1455 deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
705152c5 1456
f4c5303c
OF
1457 observer_attach_target_changed (regcache_observer_target_changed);
1458
705152c5
MS
1459 add_com ("flushregs", class_maintenance, reg_flush_command,
1460 "Force gdb to flush its register cache (maintainer command)");
39f77062
KB
1461
1462 /* Initialize the thread/process associated with the current set of
1463 registers. For now, -1 is special, and means `no current process'. */
1464 registers_ptid = pid_to_ptid (-1);
af030b9a
AC
1465
1466 add_cmd ("registers", class_maintenance,
1467 maintenance_print_registers,
1468 "Print the internal register configuration.\
1469Takes an optional file parameter.",
1470 &maintenanceprintlist);
1471 add_cmd ("raw-registers", class_maintenance,
1472 maintenance_print_raw_registers,
1473 "Print the internal register configuration including raw values.\
1474Takes an optional file parameter.",
1475 &maintenanceprintlist);
1476 add_cmd ("cooked-registers", class_maintenance,
1477 maintenance_print_cooked_registers,
1478 "Print the internal register configuration including cooked values.\
b59ff9d5
AC
1479Takes an optional file parameter.",
1480 &maintenanceprintlist);
1481 add_cmd ("register-groups", class_maintenance,
1482 maintenance_print_register_groups,
1483 "Print the internal register configuration including each register's group.\
af030b9a
AC
1484Takes an optional file parameter.",
1485 &maintenanceprintlist);
1486
32178cab 1487}
This page took 1.108272 seconds and 4 git commands to generate.