* regcache.c: Change "write-back" comment to "write-through".
[deliverable/binutils-gdb.git] / gdb / regcache.c
CommitLineData
32178cab
MS
1/* Cache and manage the values of registers for GDB, the GNU debugger.
2 Copyright 1986, 87, 89, 91, 94, 95, 96, 1998, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "frame.h"
24#include "inferior.h"
25#include "target.h"
26#include "gdbarch.h"
705152c5 27#include "gdbcmd.h"
32178cab
MS
28
29/*
30 * DATA STRUCTURE
31 *
32 * Here is the actual register cache.
33 */
34
5ebd2499 35/* NOTE: this is a write-through cache. There is no "dirty" bit for
32178cab
MS
36 recording if the register values have been changed (eg. by the
37 user). Therefore all registers must be written back to the
38 target when appropriate. */
39
40/* REGISTERS contains the cached register values (in target byte order). */
41
42char *registers;
43
44/* REGISTER_VALID is 0 if the register needs to be fetched,
45 1 if it has been fetched, and
46 -1 if the register value was not available.
47 "Not available" means don't try to fetch it again. */
48
49signed char *register_valid;
50
51/* The thread/process associated with the current set of registers.
52 For now, -1 is special, and means `no current process'. */
53
54static int registers_pid = -1;
55
56/*
57 * FUNCTIONS:
58 */
59
60/* REGISTER_CACHED()
61
62 Returns 0 if the value is not in the cache (needs fetch).
63 >0 if the value is in the cache.
64 <0 if the value is permanently unavailable (don't ask again). */
65
66int
67register_cached (int regnum)
68{
69 return register_valid[regnum];
70}
71
2dc4e391
DT
72/* REGISTER_CHANGED
73
74 invalidate a single register REGNUM in the cache */
75void
76register_changed (int regnum)
77{
78 register_valid[regnum] = 0;
79}
80
32178cab
MS
81/* FIND_SAVED_REGISTER ()
82
83 Return the address in which frame FRAME's value of register REGNUM
84 has been saved in memory. Or return zero if it has not been saved.
85 If REGNUM specifies the SP, the value we return is actually
86 the SP value, not an address where it was saved. */
87
88CORE_ADDR
89find_saved_register (struct frame_info *frame, int regnum)
90{
91 register struct frame_info *frame1 = NULL;
92 register CORE_ADDR addr = 0;
93
94 if (frame == NULL) /* No regs saved if want current frame */
95 return 0;
96
97#ifdef HAVE_REGISTER_WINDOWS
98 /* We assume that a register in a register window will only be saved
99 in one place (since the name changes and/or disappears as you go
100 towards inner frames), so we only call get_frame_saved_regs on
101 the current frame. This is directly in contradiction to the
102 usage below, which assumes that registers used in a frame must be
103 saved in a lower (more interior) frame. This change is a result
104 of working on a register window machine; get_frame_saved_regs
105 always returns the registers saved within a frame, within the
106 context (register namespace) of that frame. */
107
108 /* However, note that we don't want this to return anything if
109 nothing is saved (if there's a frame inside of this one). Also,
110 callers to this routine asking for the stack pointer want the
111 stack pointer saved for *this* frame; this is returned from the
112 next frame. */
113
114 if (REGISTER_IN_WINDOW_P (regnum))
115 {
116 frame1 = get_next_frame (frame);
117 if (!frame1)
118 return 0; /* Registers of this frame are active. */
119
120 /* Get the SP from the next frame in; it will be this
121 current frame. */
122 if (regnum != SP_REGNUM)
123 frame1 = frame;
124
125 FRAME_INIT_SAVED_REGS (frame1);
126 return frame1->saved_regs[regnum]; /* ... which might be zero */
127 }
128#endif /* HAVE_REGISTER_WINDOWS */
129
130 /* Note that this next routine assumes that registers used in
131 frame x will be saved only in the frame that x calls and
132 frames interior to it. This is not true on the sparc, but the
133 above macro takes care of it, so we should be all right. */
134 while (1)
135 {
136 QUIT;
137 frame1 = get_prev_frame (frame1);
138 if (frame1 == 0 || frame1 == frame)
139 break;
140 FRAME_INIT_SAVED_REGS (frame1);
141 if (frame1->saved_regs[regnum])
142 addr = frame1->saved_regs[regnum];
143 }
144
145 return addr;
146}
147
148/* DEFAULT_GET_SAVED_REGISTER ()
149
150 Find register number REGNUM relative to FRAME and put its (raw,
151 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
152 variable was optimized out (and thus can't be fetched). Set *LVAL
153 to lval_memory, lval_register, or not_lval, depending on whether
154 the value was fetched from memory, from a register, or in a strange
155 and non-modifiable way (e.g. a frame pointer which was calculated
156 rather than fetched). Set *ADDRP to the address, either in memory
157 on as a REGISTER_BYTE offset into the registers array.
158
159 Note that this implementation never sets *LVAL to not_lval. But
160 it can be replaced by defining GET_SAVED_REGISTER and supplying
161 your own.
162
163 The argument RAW_BUFFER must point to aligned memory. */
164
165static void
166default_get_saved_register (char *raw_buffer,
167 int *optimized,
168 CORE_ADDR *addrp,
169 struct frame_info *frame,
170 int regnum,
171 enum lval_type *lval)
172{
173 CORE_ADDR addr;
174
175 if (!target_has_registers)
176 error ("No registers.");
177
178 /* Normal systems don't optimize out things with register numbers. */
179 if (optimized != NULL)
180 *optimized = 0;
181 addr = find_saved_register (frame, regnum);
182 if (addr != 0)
183 {
184 if (lval != NULL)
185 *lval = lval_memory;
186 if (regnum == SP_REGNUM)
187 {
188 if (raw_buffer != NULL)
189 {
190 /* Put it back in target format. */
191 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
192 (LONGEST) addr);
193 }
194 if (addrp != NULL)
195 *addrp = 0;
196 return;
197 }
198 if (raw_buffer != NULL)
199 target_read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
200 }
201 else
202 {
203 if (lval != NULL)
204 *lval = lval_register;
205 addr = REGISTER_BYTE (regnum);
206 if (raw_buffer != NULL)
207 read_register_gen (regnum, raw_buffer);
208 }
209 if (addrp != NULL)
210 *addrp = addr;
211}
212
f3d21924
AC
213#if !defined (GET_SAVED_REGISTER)
214#define GET_SAVED_REGISTER(raw_buffer, optimized, addrp, frame, regnum, lval) \
215 default_get_saved_register(raw_buffer, optimized, addrp, frame, regnum, lval)
216#endif
217
32178cab
MS
218void
219get_saved_register (char *raw_buffer,
220 int *optimized,
221 CORE_ADDR *addrp,
222 struct frame_info *frame,
223 int regnum,
224 enum lval_type *lval)
225{
226 GET_SAVED_REGISTER (raw_buffer, optimized, addrp, frame, regnum, lval);
227}
228
229/* READ_RELATIVE_REGISTER_RAW_BYTES_FOR_FRAME
230
231 Copy the bytes of register REGNUM, relative to the input stack frame,
232 into our memory at MYADDR, in target byte order.
233 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
234
235 Returns 1 if could not be read, 0 if could. */
236
237/* FIXME: This function increases the confusion between FP_REGNUM
238 and the virtual/pseudo-frame pointer. */
239
240static int
241read_relative_register_raw_bytes_for_frame (int regnum,
242 char *myaddr,
243 struct frame_info *frame)
244{
245 int optim;
246 if (regnum == FP_REGNUM && frame)
247 {
248 /* Put it back in target format. */
249 store_address (myaddr, REGISTER_RAW_SIZE (FP_REGNUM),
250 (LONGEST) FRAME_FP (frame));
251
252 return 0;
253 }
254
255 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, frame,
256 regnum, (enum lval_type *) NULL);
257
258 if (register_valid[regnum] < 0)
259 return 1; /* register value not available */
260
261 return optim;
262}
263
264/* READ_RELATIVE_REGISTER_RAW_BYTES
265
266 Copy the bytes of register REGNUM, relative to the current stack
267 frame, into our memory at MYADDR, in target byte order.
268 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
269
270 Returns 1 if could not be read, 0 if could. */
271
272int
273read_relative_register_raw_bytes (int regnum, char *myaddr)
274{
275 return read_relative_register_raw_bytes_for_frame (regnum, myaddr,
276 selected_frame);
277}
278
279
280/* Low level examining and depositing of registers.
281
282 The caller is responsible for making sure that the inferior is
283 stopped before calling the fetching routines, or it will get
284 garbage. (a change from GDB version 3, in which the caller got the
285 value from the last stop). */
286
287/* REGISTERS_CHANGED ()
288
289 Indicate that registers may have changed, so invalidate the cache. */
290
291void
292registers_changed (void)
293{
294 int i;
32178cab
MS
295
296 registers_pid = -1;
297
298 /* Force cleanup of any alloca areas if using C alloca instead of
299 a builtin alloca. This particular call is used to clean up
300 areas allocated by low level target code which may build up
301 during lengthy interactions between gdb and the target before
302 gdb gives control to the user (ie watchpoints). */
303 alloca (0);
304
fcdc5976
MS
305 for (i = 0; i < ARCH_NUM_REGS; i++)
306 register_valid[i] = 0;
307
308 /* Assume that if all the hardware regs have changed,
309 then so have the pseudo-registers. */
310 for (i = NUM_REGS; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
32178cab
MS
311 register_valid[i] = 0;
312
313 if (registers_changed_hook)
314 registers_changed_hook ();
315}
316
317/* REGISTERS_FETCHED ()
318
319 Indicate that all registers have been fetched, so mark them all valid. */
320
321
322void
323registers_fetched (void)
324{
325 int i;
32178cab 326
fcdc5976 327 for (i = 0; i < ARCH_NUM_REGS; i++)
32178cab 328 register_valid[i] = 1;
fcdc5976
MS
329 /* Do not assume that the pseudo-regs have also been fetched.
330 Fetching all real regs might not account for all pseudo-regs. */
32178cab
MS
331}
332
333/* read_register_bytes and write_register_bytes are generally a *BAD*
334 idea. They are inefficient because they need to check for partial
335 updates, which can only be done by scanning through all of the
336 registers and seeing if the bytes that are being read/written fall
337 inside of an invalid register. [The main reason this is necessary
338 is that register sizes can vary, so a simple index won't suffice.]
339 It is far better to call read_register_gen and write_register_gen
340 if you want to get at the raw register contents, as it only takes a
5ebd2499 341 regnum as an argument, and therefore can't do a partial register
32178cab
MS
342 update.
343
344 Prior to the recent fixes to check for partial updates, both read
345 and write_register_bytes always checked to see if any registers
346 were stale, and then called target_fetch_registers (-1) to update
347 the whole set. This caused really slowed things down for remote
348 targets. */
349
350/* Copy INLEN bytes of consecutive data from registers
351 starting with the INREGBYTE'th byte of register data
352 into memory at MYADDR. */
353
354void
355read_register_bytes (int inregbyte, char *myaddr, int inlen)
356{
357 int inregend = inregbyte + inlen;
5ebd2499 358 int regnum;
32178cab
MS
359
360 if (registers_pid != inferior_pid)
361 {
362 registers_changed ();
363 registers_pid = inferior_pid;
364 }
365
366 /* See if we are trying to read bytes from out-of-date registers. If so,
367 update just those registers. */
368
5ebd2499 369 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
32178cab
MS
370 {
371 int regstart, regend;
372
5ebd2499 373 if (register_valid[regnum])
32178cab
MS
374 continue;
375
5ebd2499 376 if (REGISTER_NAME (regnum) == NULL || *REGISTER_NAME (regnum) == '\0')
32178cab
MS
377 continue;
378
5ebd2499
ND
379 regstart = REGISTER_BYTE (regnum);
380 regend = regstart + REGISTER_RAW_SIZE (regnum);
32178cab
MS
381
382 if (regend <= inregbyte || inregend <= regstart)
5ebd2499 383 /* The range the user wants to read doesn't overlap with regnum. */
32178cab
MS
384 continue;
385
fcdc5976 386 /* We've found an uncached register where at least one byte will be read.
32178cab 387 Update it from the target. */
5ebd2499
ND
388 if (regnum < NUM_REGS)
389 target_fetch_registers (regnum);
390 else if (regnum < NUM_REGS + NUM_PSEUDO_REGS)
391 FETCH_PSEUDO_REGISTER (regnum);
32178cab 392
5ebd2499 393 if (!register_valid[regnum])
165cd47f
MS
394 {
395 /* Sometimes pseudoregs are never marked valid, so that they
396 will be fetched every time (it can be complicated to know
397 if a pseudoreg is valid, while "fetching" them can be cheap).
398 */
5ebd2499
ND
399 if (regnum < NUM_REGS)
400 error ("read_register_bytes: Couldn't update register %d.",
401 regnum);
165cd47f 402 }
32178cab
MS
403 }
404
405 if (myaddr != NULL)
406 memcpy (myaddr, &registers[inregbyte], inlen);
407}
408
5ebd2499
ND
409/* Read register REGNUM into memory at MYADDR, which must be large
410 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
32178cab
MS
411 register is known to be the size of a CORE_ADDR or smaller,
412 read_register can be used instead. */
413
414void
5ebd2499 415read_register_gen (int regnum, char *myaddr)
32178cab
MS
416{
417 if (registers_pid != inferior_pid)
418 {
419 registers_changed ();
420 registers_pid = inferior_pid;
421 }
422
5ebd2499 423 if (!register_valid[regnum])
fcdc5976 424 {
5ebd2499
ND
425 if (regnum < NUM_REGS)
426 target_fetch_registers (regnum);
427 else if (regnum < NUM_REGS + NUM_PSEUDO_REGS)
428 FETCH_PSEUDO_REGISTER (regnum);
fcdc5976 429 }
5ebd2499
ND
430 memcpy (myaddr, &registers[REGISTER_BYTE (regnum)],
431 REGISTER_RAW_SIZE (regnum));
32178cab
MS
432}
433
5ebd2499
ND
434/* Write register REGNUM at MYADDR to the target. MYADDR points at
435 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
32178cab
MS
436
437/* Registers we shouldn't try to store. */
438#if !defined (CANNOT_STORE_REGISTER)
5ebd2499 439#define CANNOT_STORE_REGISTER(regnum) 0
32178cab
MS
440#endif
441
442void
5ebd2499 443write_register_gen (int regnum, char *myaddr)
32178cab
MS
444{
445 int size;
446
447 /* On the sparc, writing %g0 is a no-op, so we don't even want to
448 change the registers array if something writes to this register. */
5ebd2499 449 if (CANNOT_STORE_REGISTER (regnum))
32178cab
MS
450 return;
451
452 if (registers_pid != inferior_pid)
453 {
454 registers_changed ();
455 registers_pid = inferior_pid;
456 }
457
5ebd2499 458 size = REGISTER_RAW_SIZE (regnum);
32178cab
MS
459
460 /* If we have a valid copy of the register, and new value == old value,
461 then don't bother doing the actual store. */
462
5ebd2499
ND
463 if (register_valid[regnum]
464 && memcmp (&registers[REGISTER_BYTE (regnum)], myaddr, size) == 0)
32178cab
MS
465 return;
466
5ebd2499 467 if (regnum < NUM_REGS)
fcdc5976 468 target_prepare_to_store ();
32178cab 469
5ebd2499 470 memcpy (&registers[REGISTER_BYTE (regnum)], myaddr, size);
32178cab 471
5ebd2499 472 register_valid[regnum] = 1;
32178cab 473
5ebd2499
ND
474 if (regnum < NUM_REGS)
475 target_store_registers (regnum);
476 else if (regnum < NUM_REGS + NUM_PSEUDO_REGS)
477 STORE_PSEUDO_REGISTER (regnum);
32178cab
MS
478}
479
480/* Copy INLEN bytes of consecutive data from memory at MYADDR
481 into registers starting with the MYREGSTART'th byte of register data. */
482
483void
484write_register_bytes (int myregstart, char *myaddr, int inlen)
485{
486 int myregend = myregstart + inlen;
5ebd2499 487 int regnum;
32178cab
MS
488
489 target_prepare_to_store ();
490
491 /* Scan through the registers updating any that are covered by the
492 range myregstart<=>myregend using write_register_gen, which does
493 nice things like handling threads, and avoiding updates when the
494 new and old contents are the same. */
495
5ebd2499 496 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
32178cab
MS
497 {
498 int regstart, regend;
499
5ebd2499
ND
500 regstart = REGISTER_BYTE (regnum);
501 regend = regstart + REGISTER_RAW_SIZE (regnum);
32178cab
MS
502
503 /* Is this register completely outside the range the user is writing? */
504 if (myregend <= regstart || regend <= myregstart)
505 /* do nothing */ ;
506
507 /* Is this register completely within the range the user is writing? */
508 else if (myregstart <= regstart && regend <= myregend)
5ebd2499 509 write_register_gen (regnum, myaddr + (regstart - myregstart));
32178cab
MS
510
511 /* The register partially overlaps the range being written. */
512 else
513 {
514 char regbuf[MAX_REGISTER_RAW_SIZE];
515 /* What's the overlap between this register's bytes and
516 those the caller wants to write? */
517 int overlapstart = max (regstart, myregstart);
518 int overlapend = min (regend, myregend);
519
520 /* We may be doing a partial update of an invalid register.
521 Update it from the target before scribbling on it. */
5ebd2499 522 read_register_gen (regnum, regbuf);
32178cab
MS
523
524 memcpy (registers + overlapstart,
525 myaddr + (overlapstart - myregstart),
526 overlapend - overlapstart);
527
5ebd2499
ND
528 if (regnum < NUM_REGS)
529 target_store_registers (regnum);
530 else if (regnum < NUM_REGS + NUM_PSEUDO_REGS)
531 STORE_PSEUDO_REGISTER (regnum);
32178cab
MS
532 }
533 }
534}
535
536
5ebd2499 537/* Return the contents of register REGNUM as an unsigned integer. */
32178cab 538
173155e8 539ULONGEST
5ebd2499 540read_register (int regnum)
32178cab
MS
541{
542 if (registers_pid != inferior_pid)
543 {
544 registers_changed ();
545 registers_pid = inferior_pid;
546 }
547
5ebd2499 548 if (!register_valid[regnum])
fcdc5976 549 {
5ebd2499
ND
550 if (regnum < NUM_REGS)
551 target_fetch_registers (regnum);
552 else if (regnum < NUM_REGS + NUM_PSEUDO_REGS)
553 FETCH_PSEUDO_REGISTER (regnum);
fcdc5976 554 }
32178cab 555
5ebd2499
ND
556 return (extract_unsigned_integer (&registers[REGISTER_BYTE (regnum)],
557 REGISTER_RAW_SIZE (regnum)));
32178cab
MS
558}
559
173155e8 560ULONGEST
5ebd2499 561read_register_pid (int regnum, int pid)
32178cab
MS
562{
563 int save_pid;
564 CORE_ADDR retval;
565
566 if (pid == inferior_pid)
5ebd2499 567 return read_register (regnum);
32178cab
MS
568
569 save_pid = inferior_pid;
570
571 inferior_pid = pid;
572
5ebd2499 573 retval = read_register (regnum);
32178cab
MS
574
575 inferior_pid = save_pid;
576
577 return retval;
578}
579
5ebd2499 580/* Return the contents of register REGNUM as a signed integer. */
173155e8
AC
581
582LONGEST
5ebd2499 583read_signed_register (int regnum)
173155e8
AC
584{
585 if (registers_pid != inferior_pid)
586 {
587 registers_changed ();
588 registers_pid = inferior_pid;
589 }
590
5ebd2499
ND
591 if (!register_valid[regnum])
592 target_fetch_registers (regnum);
173155e8 593
5ebd2499
ND
594 return (extract_signed_integer (&registers[REGISTER_BYTE (regnum)],
595 REGISTER_RAW_SIZE (regnum)));
173155e8
AC
596}
597
598LONGEST
5ebd2499 599read_signed_register_pid (int regnum, int pid)
173155e8
AC
600{
601 int save_pid;
602 LONGEST retval;
603
604 if (pid == inferior_pid)
5ebd2499 605 return read_signed_register (regnum);
173155e8
AC
606
607 save_pid = inferior_pid;
608
609 inferior_pid = pid;
610
5ebd2499 611 retval = read_signed_register (regnum);
173155e8
AC
612
613 inferior_pid = save_pid;
614
615 return retval;
616}
617
5ebd2499 618/* Store VALUE into the raw contents of register number REGNUM. */
32178cab
MS
619
620void
5ebd2499 621write_register (int regnum, LONGEST val)
32178cab
MS
622{
623 PTR buf;
624 int size;
625
626 /* On the sparc, writing %g0 is a no-op, so we don't even want to
627 change the registers array if something writes to this register. */
5ebd2499 628 if (CANNOT_STORE_REGISTER (regnum))
32178cab
MS
629 return;
630
631 if (registers_pid != inferior_pid)
632 {
633 registers_changed ();
634 registers_pid = inferior_pid;
635 }
636
5ebd2499 637 size = REGISTER_RAW_SIZE (regnum);
32178cab
MS
638 buf = alloca (size);
639 store_signed_integer (buf, size, (LONGEST) val);
640
641 /* If we have a valid copy of the register, and new value == old value,
642 then don't bother doing the actual store. */
643
5ebd2499
ND
644 if (register_valid[regnum]
645 && memcmp (&registers[REGISTER_BYTE (regnum)], buf, size) == 0)
32178cab
MS
646 return;
647
5ebd2499 648 if (regnum < NUM_REGS)
fcdc5976 649 target_prepare_to_store ();
32178cab 650
5ebd2499 651 memcpy (&registers[REGISTER_BYTE (regnum)], buf, size);
32178cab 652
5ebd2499 653 register_valid[regnum] = 1;
32178cab 654
5ebd2499
ND
655 if (regnum < NUM_REGS)
656 target_store_registers (regnum);
657 else if (regnum < NUM_REGS + NUM_PSEUDO_REGS)
658 STORE_PSEUDO_REGISTER (regnum);
32178cab
MS
659}
660
661void
5ebd2499 662write_register_pid (int regnum, CORE_ADDR val, int pid)
32178cab
MS
663{
664 int save_pid;
665
666 if (pid == inferior_pid)
667 {
5ebd2499 668 write_register (regnum, val);
32178cab
MS
669 return;
670 }
671
672 save_pid = inferior_pid;
673
674 inferior_pid = pid;
675
5ebd2499 676 write_register (regnum, val);
32178cab
MS
677
678 inferior_pid = save_pid;
679}
680
681/* SUPPLY_REGISTER()
682
5ebd2499 683 Record that register REGNUM contains VAL. This is used when the
32178cab
MS
684 value is obtained from the inferior or core dump, so there is no
685 need to store the value there.
686
687 If VAL is a NULL pointer, then it's probably an unsupported register.
5ebd2499 688 We just set its value to all zeros. We might want to record this
32178cab
MS
689 fact, and report it to the users of read_register and friends. */
690
691void
5ebd2499 692supply_register (int regnum, char *val)
32178cab
MS
693{
694#if 1
695 if (registers_pid != inferior_pid)
696 {
697 registers_changed ();
698 registers_pid = inferior_pid;
699 }
700#endif
701
5ebd2499 702 register_valid[regnum] = 1;
32178cab 703 if (val)
5ebd2499
ND
704 memcpy (&registers[REGISTER_BYTE (regnum)], val,
705 REGISTER_RAW_SIZE (regnum));
32178cab 706 else
5ebd2499
ND
707 memset (&registers[REGISTER_BYTE (regnum)], '\000',
708 REGISTER_RAW_SIZE (regnum));
32178cab
MS
709
710 /* On some architectures, e.g. HPPA, there are a few stray bits in
711 some registers, that the rest of the code would like to ignore. */
712
713#ifdef CLEAN_UP_REGISTER_VALUE
5ebd2499 714 CLEAN_UP_REGISTER_VALUE (regnum, &registers[REGISTER_BYTE (regnum)]);
32178cab
MS
715#endif
716}
717
718/* read_pc, write_pc, read_sp, write_sp, read_fp, write_fp, etc.
719 Special handling for registers PC, SP, and FP. */
720
721/* This routine is getting awfully cluttered with #if's. It's probably
722 time to turn this into READ_PC and define it in the tm.h file.
723 Ditto for write_pc.
724
725 1999-06-08: The following were re-written so that it assumes the
8e1a459b 726 existence of a TARGET_READ_PC et.al. macro. A default generic
32178cab
MS
727 version of that macro is made available where needed.
728
729 Since the ``TARGET_READ_PC'' et.al. macro is going to be controlled
730 by the multi-arch framework, it will eventually be possible to
731 eliminate the intermediate read_pc_pid(). The client would call
732 TARGET_READ_PC directly. (cagney). */
733
32178cab
MS
734CORE_ADDR
735generic_target_read_pc (int pid)
736{
737#ifdef PC_REGNUM
738 if (PC_REGNUM >= 0)
739 {
740 CORE_ADDR pc_val = ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, pid));
741 return pc_val;
742 }
743#endif
744 internal_error ("generic_target_read_pc");
745 return 0;
746}
747
748CORE_ADDR
749read_pc_pid (int pid)
750{
751 int saved_inferior_pid;
752 CORE_ADDR pc_val;
753
754 /* In case pid != inferior_pid. */
755 saved_inferior_pid = inferior_pid;
756 inferior_pid = pid;
757
758 pc_val = TARGET_READ_PC (pid);
759
760 inferior_pid = saved_inferior_pid;
761 return pc_val;
762}
763
764CORE_ADDR
765read_pc (void)
766{
767 return read_pc_pid (inferior_pid);
768}
769
32178cab
MS
770void
771generic_target_write_pc (CORE_ADDR pc, int pid)
772{
773#ifdef PC_REGNUM
774 if (PC_REGNUM >= 0)
775 write_register_pid (PC_REGNUM, pc, pid);
776 if (NPC_REGNUM >= 0)
777 write_register_pid (NPC_REGNUM, pc + 4, pid);
778 if (NNPC_REGNUM >= 0)
779 write_register_pid (NNPC_REGNUM, pc + 8, pid);
780#else
781 internal_error ("generic_target_write_pc");
782#endif
783}
784
785void
786write_pc_pid (CORE_ADDR pc, int pid)
787{
788 int saved_inferior_pid;
789
790 /* In case pid != inferior_pid. */
791 saved_inferior_pid = inferior_pid;
792 inferior_pid = pid;
793
794 TARGET_WRITE_PC (pc, pid);
795
796 inferior_pid = saved_inferior_pid;
797}
798
799void
800write_pc (CORE_ADDR pc)
801{
802 write_pc_pid (pc, inferior_pid);
803}
804
805/* Cope with strage ways of getting to the stack and frame pointers */
806
32178cab
MS
807CORE_ADDR
808generic_target_read_sp (void)
809{
810#ifdef SP_REGNUM
811 if (SP_REGNUM >= 0)
812 return read_register (SP_REGNUM);
813#endif
814 internal_error ("generic_target_read_sp");
815}
816
817CORE_ADDR
818read_sp (void)
819{
820 return TARGET_READ_SP ();
821}
822
32178cab
MS
823void
824generic_target_write_sp (CORE_ADDR val)
825{
826#ifdef SP_REGNUM
827 if (SP_REGNUM >= 0)
828 {
829 write_register (SP_REGNUM, val);
830 return;
831 }
832#endif
833 internal_error ("generic_target_write_sp");
834}
835
836void
837write_sp (CORE_ADDR val)
838{
839 TARGET_WRITE_SP (val);
840}
841
32178cab
MS
842CORE_ADDR
843generic_target_read_fp (void)
844{
845#ifdef FP_REGNUM
846 if (FP_REGNUM >= 0)
847 return read_register (FP_REGNUM);
848#endif
849 internal_error ("generic_target_read_fp");
850}
851
852CORE_ADDR
853read_fp (void)
854{
855 return TARGET_READ_FP ();
856}
857
32178cab
MS
858void
859generic_target_write_fp (CORE_ADDR val)
860{
861#ifdef FP_REGNUM
862 if (FP_REGNUM >= 0)
863 {
864 write_register (FP_REGNUM, val);
865 return;
866 }
867#endif
868 internal_error ("generic_target_write_fp");
869}
870
871void
872write_fp (CORE_ADDR val)
873{
874 TARGET_WRITE_FP (val);
875}
876
705152c5
MS
877/* ARGSUSED */
878static void
879reg_flush_command (char *command, int from_tty)
880{
881 /* Force-flush the register cache. */
882 registers_changed ();
883 if (from_tty)
884 printf_filtered ("Register cache flushed.\n");
885}
886
887
32178cab
MS
888static void
889build_regcache (void)
890{
891 /* We allocate some extra slop since we do a lot of memcpy's around
892 `registers', and failing-soft is better than failing hard. */
893 int sizeof_registers = REGISTER_BYTES + /* SLOP */ 256;
fcdc5976
MS
894 int sizeof_register_valid =
895 (NUM_REGS + NUM_PSEUDO_REGS) * sizeof (*register_valid);
32178cab
MS
896 registers = xmalloc (sizeof_registers);
897 memset (registers, 0, sizeof_registers);
898 register_valid = xmalloc (sizeof_register_valid);
899 memset (register_valid, 0, sizeof_register_valid);
900}
901
902void
903_initialize_regcache (void)
904{
905 build_regcache ();
906
907 register_gdbarch_swap (&registers, sizeof (registers), NULL);
908 register_gdbarch_swap (&register_valid, sizeof (register_valid), NULL);
909 register_gdbarch_swap (NULL, 0, build_regcache);
705152c5
MS
910
911 add_com ("flushregs", class_maintenance, reg_flush_command,
912 "Force gdb to flush its register cache (maintainer command)");
32178cab 913}
This page took 0.093497 seconds and 4 git commands to generate.