]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/findvar.c
* Add native support for long double data type.
[thirdparty/binutils-gdb.git] / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "frame.h"
24 #include "value.h"
25 #include "gdbcore.h"
26 #include "inferior.h"
27 #include "target.h"
28 #include "gdb_string.h"
29
30 /* Registers we shouldn't try to store. */
31 #if !defined (CANNOT_STORE_REGISTER)
32 #define CANNOT_STORE_REGISTER(regno) 0
33 #endif
34
35 static void write_register_pid PARAMS ((int regno, LONGEST val, int pid));
36
37 /* Basic byte-swapping routines. GDB has needed these for a long time...
38 All extract a target-format integer at ADDR which is LEN bytes long. */
39
40 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
41 /* 8 bit characters are a pretty safe assumption these days, so we
42 assume it throughout all these swapping routines. If we had to deal with
43 9 bit characters, we would need to make len be in bits and would have
44 to re-write these routines... */
45 you lose
46 #endif
47
48 LONGEST
49 extract_signed_integer (addr, len)
50 PTR addr;
51 int len;
52 {
53 LONGEST retval;
54 unsigned char *p;
55 unsigned char *startaddr = (unsigned char *)addr;
56 unsigned char *endaddr = startaddr + len;
57
58 if (len > sizeof (LONGEST))
59 error ("\
60 That operation is not available on integers of more than %d bytes.",
61 sizeof (LONGEST));
62
63 /* Start at the most significant end of the integer, and work towards
64 the least significant. */
65 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
66 {
67 p = startaddr;
68 /* Do the sign extension once at the start. */
69 retval = ((LONGEST)*p ^ 0x80) - 0x80;
70 for (++p; p < endaddr; ++p)
71 retval = (retval << 8) | *p;
72 }
73 else
74 {
75 p = endaddr - 1;
76 /* Do the sign extension once at the start. */
77 retval = ((LONGEST)*p ^ 0x80) - 0x80;
78 for (--p; p >= startaddr; --p)
79 retval = (retval << 8) | *p;
80 }
81 return retval;
82 }
83
84 unsigned LONGEST
85 extract_unsigned_integer (addr, len)
86 PTR addr;
87 int len;
88 {
89 unsigned LONGEST retval;
90 unsigned char *p;
91 unsigned char *startaddr = (unsigned char *)addr;
92 unsigned char *endaddr = startaddr + len;
93
94 if (len > sizeof (unsigned LONGEST))
95 error ("\
96 That operation is not available on integers of more than %d bytes.",
97 sizeof (unsigned LONGEST));
98
99 /* Start at the most significant end of the integer, and work towards
100 the least significant. */
101 retval = 0;
102 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
103 {
104 for (p = startaddr; p < endaddr; ++p)
105 retval = (retval << 8) | *p;
106 }
107 else
108 {
109 for (p = endaddr - 1; p >= startaddr; --p)
110 retval = (retval << 8) | *p;
111 }
112 return retval;
113 }
114
115 CORE_ADDR
116 extract_address (addr, len)
117 PTR addr;
118 int len;
119 {
120 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
121 whether we want this to be true eventually. */
122 return extract_unsigned_integer (addr, len);
123 }
124
125 void
126 store_signed_integer (addr, len, val)
127 PTR addr;
128 int len;
129 LONGEST val;
130 {
131 unsigned char *p;
132 unsigned char *startaddr = (unsigned char *)addr;
133 unsigned char *endaddr = startaddr + len;
134
135 /* Start at the least significant end of the integer, and work towards
136 the most significant. */
137 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
138 {
139 for (p = endaddr - 1; p >= startaddr; --p)
140 {
141 *p = val & 0xff;
142 val >>= 8;
143 }
144 }
145 else
146 {
147 for (p = startaddr; p < endaddr; ++p)
148 {
149 *p = val & 0xff;
150 val >>= 8;
151 }
152 }
153 }
154
155 void
156 store_unsigned_integer (addr, len, val)
157 PTR addr;
158 int len;
159 unsigned LONGEST val;
160 {
161 unsigned char *p;
162 unsigned char *startaddr = (unsigned char *)addr;
163 unsigned char *endaddr = startaddr + len;
164
165 /* Start at the least significant end of the integer, and work towards
166 the most significant. */
167 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
168 {
169 for (p = endaddr - 1; p >= startaddr; --p)
170 {
171 *p = val & 0xff;
172 val >>= 8;
173 }
174 }
175 else
176 {
177 for (p = startaddr; p < endaddr; ++p)
178 {
179 *p = val & 0xff;
180 val >>= 8;
181 }
182 }
183 }
184
185 void
186 store_address (addr, len, val)
187 PTR addr;
188 int len;
189 CORE_ADDR val;
190 {
191 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
192 whether we want this to be true eventually. */
193 store_unsigned_integer (addr, len, (LONGEST)val);
194 }
195 \f
196 /* Swap LEN bytes at BUFFER between target and host byte-order. */
197 #define SWAP_FLOATING(buffer,len) \
198 do \
199 { \
200 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
201 { \
202 char tmp; \
203 char *p = (char *)(buffer); \
204 char *q = ((char *)(buffer)) + len - 1; \
205 for (; p < q; p++, q--) \
206 { \
207 tmp = *q; \
208 *q = *p; \
209 *p = tmp; \
210 } \
211 } \
212 } \
213 while (0)
214
215 /* There are various problems with the extract_floating and store_floating
216 routines.
217
218 1. These routines only handle byte-swapping, not conversion of
219 formats. So if host is IEEE floating and target is VAX floating,
220 or vice-versa, it loses. This means that we can't (yet) use these
221 routines for extendeds. Extendeds are handled by
222 REGISTER_CONVERTIBLE. What we want is to use floatformat.h, but that
223 doesn't yet handle VAX floating at all.
224
225 2. We can't deal with it if there is more than one floating point
226 format in use. This has to be fixed at the unpack_double level.
227
228 3. We probably should have a LONGEST_DOUBLE or DOUBLEST or whatever
229 we want to call it which is long double where available. */
230
231 DOUBLEST
232 extract_floating (addr, len)
233 PTR addr;
234 int len;
235 {
236 if (len == sizeof (float))
237 {
238 float retval;
239 memcpy (&retval, addr, sizeof (retval));
240 SWAP_FLOATING (&retval, sizeof (retval));
241 return retval;
242 }
243 else if (len == sizeof (double))
244 {
245 double retval;
246 memcpy (&retval, addr, sizeof (retval));
247 SWAP_FLOATING (&retval, sizeof (retval));
248 return retval;
249 }
250 else if (len == sizeof (long double))
251 {
252 long double retval;
253 memcpy (&retval, addr, sizeof (retval));
254 SWAP_FLOATING (&retval, sizeof (retval));
255 return retval;
256 }
257 else
258 {
259 error ("Can't deal with a floating point number of %d bytes.", len);
260 }
261 }
262
263 void
264 store_floating (addr, len, val)
265 PTR addr;
266 int len;
267 DOUBLEST val;
268 {
269 if (len == sizeof (float))
270 {
271 float floatval = val;
272 SWAP_FLOATING (&floatval, sizeof (floatval));
273 memcpy (addr, &floatval, sizeof (floatval));
274 }
275 else if (len == sizeof (double))
276 {
277 double doubleval = val;
278
279 SWAP_FLOATING (&doubleval, sizeof (doubleval));
280 memcpy (addr, &doubleval, sizeof (doubleval));
281 }
282 else if (len == sizeof (long double))
283 {
284 SWAP_FLOATING (&val, sizeof (val));
285 memcpy (addr, &val, sizeof (val));
286 }
287 else
288 {
289 error ("Can't deal with a floating point number of %d bytes.", len);
290 }
291 }
292 \f
293 #if !defined (GET_SAVED_REGISTER)
294
295 /* Return the address in which frame FRAME's value of register REGNUM
296 has been saved in memory. Or return zero if it has not been saved.
297 If REGNUM specifies the SP, the value we return is actually
298 the SP value, not an address where it was saved. */
299
300 CORE_ADDR
301 find_saved_register (frame, regnum)
302 struct frame_info *frame;
303 int regnum;
304 {
305 struct frame_saved_regs saved_regs;
306
307 register struct frame_info *frame1 = NULL;
308 register CORE_ADDR addr = 0;
309
310 if (frame == NULL) /* No regs saved if want current frame */
311 return 0;
312
313 #ifdef HAVE_REGISTER_WINDOWS
314 /* We assume that a register in a register window will only be saved
315 in one place (since the name changes and/or disappears as you go
316 towards inner frames), so we only call get_frame_saved_regs on
317 the current frame. This is directly in contradiction to the
318 usage below, which assumes that registers used in a frame must be
319 saved in a lower (more interior) frame. This change is a result
320 of working on a register window machine; get_frame_saved_regs
321 always returns the registers saved within a frame, within the
322 context (register namespace) of that frame. */
323
324 /* However, note that we don't want this to return anything if
325 nothing is saved (if there's a frame inside of this one). Also,
326 callers to this routine asking for the stack pointer want the
327 stack pointer saved for *this* frame; this is returned from the
328 next frame. */
329
330 if (REGISTER_IN_WINDOW_P(regnum))
331 {
332 frame1 = get_next_frame (frame);
333 if (!frame1) return 0; /* Registers of this frame are active. */
334
335 /* Get the SP from the next frame in; it will be this
336 current frame. */
337 if (regnum != SP_REGNUM)
338 frame1 = frame;
339
340 get_frame_saved_regs (frame1, &saved_regs);
341 return saved_regs.regs[regnum]; /* ... which might be zero */
342 }
343 #endif /* HAVE_REGISTER_WINDOWS */
344
345 /* Note that this next routine assumes that registers used in
346 frame x will be saved only in the frame that x calls and
347 frames interior to it. This is not true on the sparc, but the
348 above macro takes care of it, so we should be all right. */
349 while (1)
350 {
351 QUIT;
352 frame1 = get_prev_frame (frame1);
353 if (frame1 == 0 || frame1 == frame)
354 break;
355 get_frame_saved_regs (frame1, &saved_regs);
356 if (saved_regs.regs[regnum])
357 addr = saved_regs.regs[regnum];
358 }
359
360 return addr;
361 }
362
363 /* Find register number REGNUM relative to FRAME and put its (raw,
364 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
365 variable was optimized out (and thus can't be fetched). Set *LVAL
366 to lval_memory, lval_register, or not_lval, depending on whether
367 the value was fetched from memory, from a register, or in a strange
368 and non-modifiable way (e.g. a frame pointer which was calculated
369 rather than fetched). Set *ADDRP to the address, either in memory
370 on as a REGISTER_BYTE offset into the registers array.
371
372 Note that this implementation never sets *LVAL to not_lval. But
373 it can be replaced by defining GET_SAVED_REGISTER and supplying
374 your own.
375
376 The argument RAW_BUFFER must point to aligned memory. */
377
378 void
379 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
380 char *raw_buffer;
381 int *optimized;
382 CORE_ADDR *addrp;
383 struct frame_info *frame;
384 int regnum;
385 enum lval_type *lval;
386 {
387 CORE_ADDR addr;
388
389 if (!target_has_registers)
390 error ("No registers.");
391
392 /* Normal systems don't optimize out things with register numbers. */
393 if (optimized != NULL)
394 *optimized = 0;
395 addr = find_saved_register (frame, regnum);
396 if (addr != 0)
397 {
398 if (lval != NULL)
399 *lval = lval_memory;
400 if (regnum == SP_REGNUM)
401 {
402 if (raw_buffer != NULL)
403 {
404 /* Put it back in target format. */
405 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), addr);
406 }
407 if (addrp != NULL)
408 *addrp = 0;
409 return;
410 }
411 if (raw_buffer != NULL)
412 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
413 }
414 else
415 {
416 if (lval != NULL)
417 *lval = lval_register;
418 addr = REGISTER_BYTE (regnum);
419 if (raw_buffer != NULL)
420 read_register_gen (regnum, raw_buffer);
421 }
422 if (addrp != NULL)
423 *addrp = addr;
424 }
425 #endif /* GET_SAVED_REGISTER. */
426
427 /* Copy the bytes of register REGNUM, relative to the current stack frame,
428 into our memory at MYADDR, in target byte order.
429 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
430
431 Returns 1 if could not be read, 0 if could. */
432
433 int
434 read_relative_register_raw_bytes (regnum, myaddr)
435 int regnum;
436 char *myaddr;
437 {
438 int optim;
439 if (regnum == FP_REGNUM && selected_frame)
440 {
441 /* Put it back in target format. */
442 store_address (myaddr, REGISTER_RAW_SIZE(FP_REGNUM),
443 FRAME_FP(selected_frame));
444 return 0;
445 }
446
447 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
448 regnum, (enum lval_type *)NULL);
449 return optim;
450 }
451
452 /* Return a `value' with the contents of register REGNUM
453 in its virtual format, with the type specified by
454 REGISTER_VIRTUAL_TYPE. */
455
456 value_ptr
457 value_of_register (regnum)
458 int regnum;
459 {
460 CORE_ADDR addr;
461 int optim;
462 register value_ptr reg_val;
463 char raw_buffer[MAX_REGISTER_RAW_SIZE];
464 enum lval_type lval;
465
466 get_saved_register (raw_buffer, &optim, &addr,
467 selected_frame, regnum, &lval);
468
469 reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
470
471 /* Convert raw data to virtual format if necessary. */
472
473 #ifdef REGISTER_CONVERTIBLE
474 if (REGISTER_CONVERTIBLE (regnum))
475 {
476 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
477 raw_buffer, VALUE_CONTENTS_RAW (reg_val));
478 }
479 else
480 #endif
481 memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
482 REGISTER_RAW_SIZE (regnum));
483 VALUE_LVAL (reg_val) = lval;
484 VALUE_ADDRESS (reg_val) = addr;
485 VALUE_REGNO (reg_val) = regnum;
486 VALUE_OPTIMIZED_OUT (reg_val) = optim;
487 return reg_val;
488 }
489 \f
490 /* Low level examining and depositing of registers.
491
492 The caller is responsible for making
493 sure that the inferior is stopped before calling the fetching routines,
494 or it will get garbage. (a change from GDB version 3, in which
495 the caller got the value from the last stop). */
496
497 /* Contents of the registers in target byte order.
498 We allocate some extra slop since we do a lot of memcpy's around `registers',
499 and failing-soft is better than failing hard. */
500 char registers[REGISTER_BYTES + /* SLOP */ 256];
501
502 /* Nonzero if that register has been fetched. */
503 char register_valid[NUM_REGS];
504
505 /* The thread/process associated with the current set of registers. For now,
506 -1 is special, and means `no current process'. */
507 int registers_pid = -1;
508
509 /* Indicate that registers may have changed, so invalidate the cache. */
510
511 void
512 registers_changed ()
513 {
514 int i;
515 int numregs = ARCH_NUM_REGS;
516
517 registers_pid = -1;
518
519 for (i = 0; i < numregs; i++)
520 register_valid[i] = 0;
521
522 if (registers_changed_hook)
523 registers_changed_hook ();
524 }
525
526 /* Indicate that all registers have been fetched, so mark them all valid. */
527 void
528 registers_fetched ()
529 {
530 int i;
531 int numregs = ARCH_NUM_REGS;
532 for (i = 0; i < numregs; i++)
533 register_valid[i] = 1;
534 }
535
536 /* read_register_bytes and write_register_bytes are generally a *BAD* idea.
537 They are inefficient because they need to check for partial updates, which
538 can only be done by scanning through all of the registers and seeing if the
539 bytes that are being read/written fall inside of an invalid register. [The
540 main reason this is necessary is that register sizes can vary, so a simple
541 index won't suffice.] It is far better to call read_register_gen if you
542 want to get at the raw register contents, as it only takes a regno as an
543 argument, and therefore can't do a partial register update. It would also
544 be good to have a write_register_gen for similar reasons.
545
546 Prior to the recent fixes to check for partial updates, both read and
547 write_register_bytes always checked to see if any registers were stale, and
548 then called target_fetch_registers (-1) to update the whole set. This
549 caused really slowed things down for remote targets. */
550
551 /* Copy INLEN bytes of consecutive data from registers
552 starting with the INREGBYTE'th byte of register data
553 into memory at MYADDR. */
554
555 void
556 read_register_bytes (inregbyte, myaddr, inlen)
557 int inregbyte;
558 char *myaddr;
559 int inlen;
560 {
561 int inregend = inregbyte + inlen;
562 int regno;
563
564 if (registers_pid != inferior_pid)
565 {
566 registers_changed ();
567 registers_pid = inferior_pid;
568 }
569
570 /* See if we are trying to read bytes from out-of-date registers. If so,
571 update just those registers. */
572
573 for (regno = 0; regno < NUM_REGS; regno++)
574 {
575 int regstart, regend;
576 int startin, endin;
577
578 if (register_valid[regno])
579 continue;
580
581 regstart = REGISTER_BYTE (regno);
582 regend = regstart + REGISTER_RAW_SIZE (regno);
583
584 startin = regstart >= inregbyte && regstart < inregend;
585 endin = regend > inregbyte && regend <= inregend;
586
587 if (!startin && !endin)
588 continue;
589
590 /* We've found an invalid register where at least one byte will be read.
591 Update it from the target. */
592
593 target_fetch_registers (regno);
594
595 if (!register_valid[regno])
596 error ("read_register_bytes: Couldn't update register %d.", regno);
597 }
598
599 if (myaddr != NULL)
600 memcpy (myaddr, &registers[inregbyte], inlen);
601 }
602
603 /* Read register REGNO into memory at MYADDR, which must be large enough
604 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
605 If the register is known to be the size of a CORE_ADDR or smaller,
606 read_register can be used instead. */
607 void
608 read_register_gen (regno, myaddr)
609 int regno;
610 char *myaddr;
611 {
612 if (registers_pid != inferior_pid)
613 {
614 registers_changed ();
615 registers_pid = inferior_pid;
616 }
617
618 if (!register_valid[regno])
619 target_fetch_registers (regno);
620 memcpy (myaddr, &registers[REGISTER_BYTE (regno)],
621 REGISTER_RAW_SIZE (regno));
622 }
623
624 /* Write register REGNO at MYADDR to the target. MYADDR points at
625 REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */
626
627 void
628 write_register_gen (regno, myaddr)
629 int regno;
630 char *myaddr;
631 {
632 int size;
633
634 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
635 the registers array if something writes to this register. */
636 if (CANNOT_STORE_REGISTER (regno))
637 return;
638
639 if (registers_pid != inferior_pid)
640 {
641 registers_changed ();
642 registers_pid = inferior_pid;
643 }
644
645 size = REGISTER_RAW_SIZE(regno);
646
647 /* If we have a valid copy of the register, and new value == old value,
648 then don't bother doing the actual store. */
649
650 if (register_valid [regno]
651 && memcmp (&registers[REGISTER_BYTE (regno)], myaddr, size) == 0)
652 return;
653
654 target_prepare_to_store ();
655
656 memcpy (&registers[REGISTER_BYTE (regno)], myaddr, size);
657
658 register_valid [regno] = 1;
659
660 target_store_registers (regno);
661 }
662
663 /* Copy INLEN bytes of consecutive data from memory at MYADDR
664 into registers starting with the MYREGSTART'th byte of register data. */
665
666 void
667 write_register_bytes (myregstart, myaddr, inlen)
668 int myregstart;
669 char *myaddr;
670 int inlen;
671 {
672 int myregend = myregstart + inlen;
673 int regno;
674
675 target_prepare_to_store ();
676
677 /* Scan through the registers updating any that are covered by the range
678 myregstart<=>myregend using write_register_gen, which does nice things
679 like handling threads, and avoiding updates when the new and old contents
680 are the same. */
681
682 for (regno = 0; regno < NUM_REGS; regno++)
683 {
684 int regstart, regend;
685 int startin, endin;
686 char regbuf[MAX_REGISTER_RAW_SIZE];
687
688 regstart = REGISTER_BYTE (regno);
689 regend = regstart + REGISTER_RAW_SIZE (regno);
690
691 startin = regstart >= myregstart && regstart < myregend;
692 endin = regend > myregstart && regend <= myregend;
693
694 if (!startin && !endin)
695 continue; /* Register is completely out of range */
696
697 if (startin && endin) /* register is completely in range */
698 {
699 write_register_gen (regno, myaddr + (regstart - myregstart));
700 continue;
701 }
702
703 /* We may be doing a partial update of an invalid register. Update it
704 from the target before scribbling on it. */
705 read_register_gen (regno, regbuf);
706
707 if (startin)
708 memcpy (registers + regstart,
709 myaddr + regstart - myregstart,
710 myregend - regstart);
711 else /* endin */
712 memcpy (registers + myregstart,
713 myaddr,
714 regend - myregstart);
715 target_store_registers (regno);
716 }
717 }
718
719 /* Return the raw contents of register REGNO, regarding it as an integer. */
720 /* This probably should be returning LONGEST rather than CORE_ADDR. */
721
722 CORE_ADDR
723 read_register (regno)
724 int regno;
725 {
726 if (registers_pid != inferior_pid)
727 {
728 registers_changed ();
729 registers_pid = inferior_pid;
730 }
731
732 if (!register_valid[regno])
733 target_fetch_registers (regno);
734
735 return extract_address (&registers[REGISTER_BYTE (regno)],
736 REGISTER_RAW_SIZE(regno));
737 }
738
739 CORE_ADDR
740 read_register_pid (regno, pid)
741 int regno, pid;
742 {
743 int save_pid;
744 CORE_ADDR retval;
745
746 if (pid == inferior_pid)
747 return read_register (regno);
748
749 save_pid = inferior_pid;
750
751 inferior_pid = pid;
752
753 retval = read_register (regno);
754
755 inferior_pid = save_pid;
756
757 return retval;
758 }
759
760 /* Store VALUE, into the raw contents of register number REGNO. */
761
762 void
763 write_register (regno, val)
764 int regno;
765 LONGEST val;
766 {
767 PTR buf;
768 int size;
769
770 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
771 the registers array if something writes to this register. */
772 if (CANNOT_STORE_REGISTER (regno))
773 return;
774
775 if (registers_pid != inferior_pid)
776 {
777 registers_changed ();
778 registers_pid = inferior_pid;
779 }
780
781 size = REGISTER_RAW_SIZE(regno);
782 buf = alloca (size);
783 store_signed_integer (buf, size, (LONGEST) val);
784
785 /* If we have a valid copy of the register, and new value == old value,
786 then don't bother doing the actual store. */
787
788 if (register_valid [regno]
789 && memcmp (&registers[REGISTER_BYTE (regno)], buf, size) == 0)
790 return;
791
792 target_prepare_to_store ();
793
794 memcpy (&registers[REGISTER_BYTE (regno)], buf, size);
795
796 register_valid [regno] = 1;
797
798 target_store_registers (regno);
799 }
800
801 static void
802 write_register_pid (regno, val, pid)
803 int regno;
804 LONGEST val;
805 int pid;
806 {
807 int save_pid;
808
809 if (pid == inferior_pid)
810 {
811 write_register (regno, val);
812 return;
813 }
814
815 save_pid = inferior_pid;
816
817 inferior_pid = pid;
818
819 write_register (regno, val);
820
821 inferior_pid = save_pid;
822 }
823
824 /* Record that register REGNO contains VAL.
825 This is used when the value is obtained from the inferior or core dump,
826 so there is no need to store the value there. */
827
828 void
829 supply_register (regno, val)
830 int regno;
831 char *val;
832 {
833 if (registers_pid != inferior_pid)
834 {
835 registers_changed ();
836 registers_pid = inferior_pid;
837 }
838
839 register_valid[regno] = 1;
840 memcpy (&registers[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
841
842 /* On some architectures, e.g. HPPA, there are a few stray bits in some
843 registers, that the rest of the code would like to ignore. */
844 #ifdef CLEAN_UP_REGISTER_VALUE
845 CLEAN_UP_REGISTER_VALUE(regno, &registers[REGISTER_BYTE(regno)]);
846 #endif
847 }
848
849
850 /* This routine is getting awfully cluttered with #if's. It's probably
851 time to turn this into READ_PC and define it in the tm.h file.
852 Ditto for write_pc. */
853
854 CORE_ADDR
855 read_pc ()
856 {
857 #ifdef TARGET_READ_PC
858 return TARGET_READ_PC (inferior_pid);
859 #else
860 return ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, inferior_pid));
861 #endif
862 }
863
864 CORE_ADDR
865 read_pc_pid (pid)
866 int pid;
867 {
868 #ifdef TARGET_READ_PC
869 return TARGET_READ_PC (pid);
870 #else
871 return ADDR_BITS_REMOVE ((CORE_ADDR) read_register_pid (PC_REGNUM, pid));
872 #endif
873 }
874
875 void
876 write_pc (val)
877 CORE_ADDR val;
878 {
879 #ifdef TARGET_WRITE_PC
880 TARGET_WRITE_PC (val, inferior_pid);
881 #else
882 write_register_pid (PC_REGNUM, val, inferior_pid);
883 #ifdef NPC_REGNUM
884 write_register_pid (NPC_REGNUM, val + 4, inferior_pid);
885 #ifdef NNPC_REGNUM
886 write_register_pid (NNPC_REGNUM, val + 8, inferior_pid);
887 #endif
888 #endif
889 #endif
890 }
891
892 void
893 write_pc_pid (val, pid)
894 CORE_ADDR val;
895 int pid;
896 {
897 #ifdef TARGET_WRITE_PC
898 TARGET_WRITE_PC (val, pid);
899 #else
900 write_register_pid (PC_REGNUM, val, pid);
901 #ifdef NPC_REGNUM
902 write_register_pid (NPC_REGNUM, val + 4, pid);
903 #ifdef NNPC_REGNUM
904 write_register_pid (NNPC_REGNUM, val + 8, pid);
905 #endif
906 #endif
907 #endif
908 }
909
910 /* Cope with strage ways of getting to the stack and frame pointers */
911
912 CORE_ADDR
913 read_sp ()
914 {
915 #ifdef TARGET_READ_SP
916 return TARGET_READ_SP ();
917 #else
918 return read_register (SP_REGNUM);
919 #endif
920 }
921
922 void
923 write_sp (val)
924 CORE_ADDR val;
925 {
926 #ifdef TARGET_WRITE_SP
927 TARGET_WRITE_SP (val);
928 #else
929 write_register (SP_REGNUM, val);
930 #endif
931 }
932
933 CORE_ADDR
934 read_fp ()
935 {
936 #ifdef TARGET_READ_FP
937 return TARGET_READ_FP ();
938 #else
939 return read_register (FP_REGNUM);
940 #endif
941 }
942
943 void
944 write_fp (val)
945 CORE_ADDR val;
946 {
947 #ifdef TARGET_WRITE_FP
948 TARGET_WRITE_FP (val);
949 #else
950 write_register (FP_REGNUM, val);
951 #endif
952 }
953 \f
954 /* Will calling read_var_value or locate_var_value on SYM end
955 up caring what frame it is being evaluated relative to? SYM must
956 be non-NULL. */
957 int
958 symbol_read_needs_frame (sym)
959 struct symbol *sym;
960 {
961 switch (SYMBOL_CLASS (sym))
962 {
963 /* All cases listed explicitly so that gcc -Wall will detect it if
964 we failed to consider one. */
965 case LOC_REGISTER:
966 case LOC_ARG:
967 case LOC_REF_ARG:
968 case LOC_REGPARM:
969 case LOC_REGPARM_ADDR:
970 case LOC_LOCAL:
971 case LOC_LOCAL_ARG:
972 case LOC_BASEREG:
973 case LOC_BASEREG_ARG:
974 return 1;
975
976 case LOC_UNDEF:
977 case LOC_CONST:
978 case LOC_STATIC:
979 case LOC_TYPEDEF:
980
981 case LOC_LABEL:
982 /* Getting the address of a label can be done independently of the block,
983 even if some *uses* of that address wouldn't work so well without
984 the right frame. */
985
986 case LOC_BLOCK:
987 case LOC_CONST_BYTES:
988 case LOC_UNRESOLVED:
989 case LOC_OPTIMIZED_OUT:
990 return 0;
991 }
992 return 1;
993 }
994
995 /* Given a struct symbol for a variable,
996 and a stack frame id, read the value of the variable
997 and return a (pointer to a) struct value containing the value.
998 If the variable cannot be found, return a zero pointer.
999 If FRAME is NULL, use the selected_frame. */
1000
1001 value_ptr
1002 read_var_value (var, frame)
1003 register struct symbol *var;
1004 struct frame_info *frame;
1005 {
1006 register value_ptr v;
1007 struct type *type = SYMBOL_TYPE (var);
1008 CORE_ADDR addr;
1009 register int len;
1010
1011 v = allocate_value (type);
1012 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
1013 len = TYPE_LENGTH (type);
1014
1015 if (frame == NULL) frame = selected_frame;
1016
1017 switch (SYMBOL_CLASS (var))
1018 {
1019 case LOC_CONST:
1020 /* Put the constant back in target format. */
1021 store_signed_integer (VALUE_CONTENTS_RAW (v), len,
1022 (LONGEST) SYMBOL_VALUE (var));
1023 VALUE_LVAL (v) = not_lval;
1024 return v;
1025
1026 case LOC_LABEL:
1027 /* Put the constant back in target format. */
1028 store_address (VALUE_CONTENTS_RAW (v), len, SYMBOL_VALUE_ADDRESS (var));
1029 VALUE_LVAL (v) = not_lval;
1030 return v;
1031
1032 case LOC_CONST_BYTES:
1033 {
1034 char *bytes_addr;
1035 bytes_addr = SYMBOL_VALUE_BYTES (var);
1036 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
1037 VALUE_LVAL (v) = not_lval;
1038 return v;
1039 }
1040
1041 case LOC_STATIC:
1042 addr = SYMBOL_VALUE_ADDRESS (var);
1043 break;
1044
1045 case LOC_ARG:
1046 if (frame == NULL)
1047 return 0;
1048 addr = FRAME_ARGS_ADDRESS (frame);
1049 if (!addr)
1050 return 0;
1051 addr += SYMBOL_VALUE (var);
1052 break;
1053
1054 case LOC_REF_ARG:
1055 if (frame == NULL)
1056 return 0;
1057 addr = FRAME_ARGS_ADDRESS (frame);
1058 if (!addr)
1059 return 0;
1060 addr += SYMBOL_VALUE (var);
1061 addr = read_memory_unsigned_integer
1062 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1063 break;
1064
1065 case LOC_LOCAL:
1066 case LOC_LOCAL_ARG:
1067 if (frame == NULL)
1068 return 0;
1069 addr = FRAME_LOCALS_ADDRESS (frame);
1070 addr += SYMBOL_VALUE (var);
1071 break;
1072
1073 case LOC_BASEREG:
1074 case LOC_BASEREG_ARG:
1075 {
1076 char buf[MAX_REGISTER_RAW_SIZE];
1077 get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
1078 NULL);
1079 addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
1080 addr += SYMBOL_VALUE (var);
1081 break;
1082 }
1083
1084 case LOC_TYPEDEF:
1085 error ("Cannot look up value of a typedef");
1086 break;
1087
1088 case LOC_BLOCK:
1089 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
1090 return v;
1091
1092 case LOC_REGISTER:
1093 case LOC_REGPARM:
1094 case LOC_REGPARM_ADDR:
1095 {
1096 struct block *b;
1097
1098 if (frame == NULL)
1099 return 0;
1100 b = get_frame_block (frame);
1101
1102
1103 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
1104 {
1105 addr =
1106 value_as_pointer (value_from_register (lookup_pointer_type (type),
1107 SYMBOL_VALUE (var),
1108 frame));
1109 VALUE_LVAL (v) = lval_memory;
1110 }
1111 else
1112 return value_from_register (type, SYMBOL_VALUE (var), frame);
1113 }
1114 break;
1115
1116 case LOC_UNRESOLVED:
1117 {
1118 struct minimal_symbol *msym;
1119
1120 msym = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
1121 if (msym == NULL)
1122 return 0;
1123 addr = SYMBOL_VALUE_ADDRESS (msym);
1124 }
1125 break;
1126
1127 case LOC_OPTIMIZED_OUT:
1128 VALUE_LVAL (v) = not_lval;
1129 VALUE_OPTIMIZED_OUT (v) = 1;
1130 return v;
1131
1132 default:
1133 error ("Cannot look up value of a botched symbol.");
1134 break;
1135 }
1136
1137 VALUE_ADDRESS (v) = addr;
1138 VALUE_LAZY (v) = 1;
1139 return v;
1140 }
1141
1142 /* Return a value of type TYPE, stored in register REGNUM, in frame
1143 FRAME. */
1144
1145 value_ptr
1146 value_from_register (type, regnum, frame)
1147 struct type *type;
1148 int regnum;
1149 struct frame_info *frame;
1150 {
1151 char raw_buffer [MAX_REGISTER_RAW_SIZE];
1152 CORE_ADDR addr;
1153 int optim;
1154 value_ptr v = allocate_value (type);
1155 char *value_bytes = 0;
1156 int value_bytes_copied = 0;
1157 int num_storage_locs;
1158 enum lval_type lval;
1159 int len;
1160
1161 CHECK_TYPEDEF (type);
1162 len = TYPE_LENGTH (type);
1163
1164 VALUE_REGNO (v) = regnum;
1165
1166 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
1167 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
1168 1);
1169
1170 if (num_storage_locs > 1
1171 #ifdef GDB_TARGET_IS_H8500
1172 || TYPE_CODE (type) == TYPE_CODE_PTR
1173 #endif
1174 )
1175 {
1176 /* Value spread across multiple storage locations. */
1177
1178 int local_regnum;
1179 int mem_stor = 0, reg_stor = 0;
1180 int mem_tracking = 1;
1181 CORE_ADDR last_addr = 0;
1182 CORE_ADDR first_addr = 0;
1183
1184 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
1185
1186 /* Copy all of the data out, whereever it may be. */
1187
1188 #ifdef GDB_TARGET_IS_H8500
1189 /* This piece of hideosity is required because the H8500 treats registers
1190 differently depending upon whether they are used as pointers or not. As a
1191 pointer, a register needs to have a page register tacked onto the front.
1192 An alternate way to do this would be to have gcc output different register
1193 numbers for the pointer & non-pointer form of the register. But, it
1194 doesn't, so we're stuck with this. */
1195
1196 if (TYPE_CODE (type) == TYPE_CODE_PTR
1197 && len > 2)
1198 {
1199 int page_regnum;
1200
1201 switch (regnum)
1202 {
1203 case R0_REGNUM: case R1_REGNUM: case R2_REGNUM: case R3_REGNUM:
1204 page_regnum = SEG_D_REGNUM;
1205 break;
1206 case R4_REGNUM: case R5_REGNUM:
1207 page_regnum = SEG_E_REGNUM;
1208 break;
1209 case R6_REGNUM: case R7_REGNUM:
1210 page_regnum = SEG_T_REGNUM;
1211 break;
1212 }
1213
1214 value_bytes[0] = 0;
1215 get_saved_register (value_bytes + 1,
1216 &optim,
1217 &addr,
1218 frame,
1219 page_regnum,
1220 &lval);
1221
1222 if (lval == lval_register)
1223 reg_stor++;
1224 else
1225 mem_stor++;
1226 first_addr = addr;
1227 last_addr = addr;
1228
1229 get_saved_register (value_bytes + 2,
1230 &optim,
1231 &addr,
1232 frame,
1233 regnum,
1234 &lval);
1235
1236 if (lval == lval_register)
1237 reg_stor++;
1238 else
1239 {
1240 mem_stor++;
1241 mem_tracking = mem_tracking && (addr == last_addr);
1242 }
1243 last_addr = addr;
1244 }
1245 else
1246 #endif /* GDB_TARGET_IS_H8500 */
1247 for (local_regnum = regnum;
1248 value_bytes_copied < len;
1249 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
1250 ++local_regnum))
1251 {
1252 get_saved_register (value_bytes + value_bytes_copied,
1253 &optim,
1254 &addr,
1255 frame,
1256 local_regnum,
1257 &lval);
1258
1259 if (regnum == local_regnum)
1260 first_addr = addr;
1261 if (lval == lval_register)
1262 reg_stor++;
1263 else
1264 {
1265 mem_stor++;
1266
1267 mem_tracking =
1268 (mem_tracking
1269 && (regnum == local_regnum
1270 || addr == last_addr));
1271 }
1272 last_addr = addr;
1273 }
1274
1275 if ((reg_stor && mem_stor)
1276 || (mem_stor && !mem_tracking))
1277 /* Mixed storage; all of the hassle we just went through was
1278 for some good purpose. */
1279 {
1280 VALUE_LVAL (v) = lval_reg_frame_relative;
1281 VALUE_FRAME (v) = FRAME_FP (frame);
1282 VALUE_FRAME_REGNUM (v) = regnum;
1283 }
1284 else if (mem_stor)
1285 {
1286 VALUE_LVAL (v) = lval_memory;
1287 VALUE_ADDRESS (v) = first_addr;
1288 }
1289 else if (reg_stor)
1290 {
1291 VALUE_LVAL (v) = lval_register;
1292 VALUE_ADDRESS (v) = first_addr;
1293 }
1294 else
1295 fatal ("value_from_register: Value not stored anywhere!");
1296
1297 VALUE_OPTIMIZED_OUT (v) = optim;
1298
1299 /* Any structure stored in more than one register will always be
1300 an integral number of registers. Otherwise, you'd need to do
1301 some fiddling with the last register copied here for little
1302 endian machines. */
1303
1304 /* Copy into the contents section of the value. */
1305 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
1306
1307 /* Finally do any conversion necessary when extracting this
1308 type from more than one register. */
1309 #ifdef REGISTER_CONVERT_TO_TYPE
1310 REGISTER_CONVERT_TO_TYPE(regnum, type, VALUE_CONTENTS_RAW(v));
1311 #endif
1312 return v;
1313 }
1314
1315 /* Data is completely contained within a single register. Locate the
1316 register's contents in a real register or in core;
1317 read the data in raw format. */
1318
1319 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
1320 VALUE_OPTIMIZED_OUT (v) = optim;
1321 VALUE_LVAL (v) = lval;
1322 VALUE_ADDRESS (v) = addr;
1323
1324 /* Convert raw data to virtual format if necessary. */
1325
1326 #ifdef REGISTER_CONVERTIBLE
1327 if (REGISTER_CONVERTIBLE (regnum))
1328 {
1329 REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
1330 raw_buffer, VALUE_CONTENTS_RAW (v));
1331 }
1332 else
1333 #endif
1334 {
1335 /* Raw and virtual formats are the same for this register. */
1336
1337 if (TARGET_BYTE_ORDER == BIG_ENDIAN && len < REGISTER_RAW_SIZE (regnum))
1338 {
1339 /* Big-endian, and we want less than full size. */
1340 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
1341 }
1342
1343 memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
1344 }
1345
1346 return v;
1347 }
1348 \f
1349 /* Given a struct symbol for a variable or function,
1350 and a stack frame id,
1351 return a (pointer to a) struct value containing the properly typed
1352 address. */
1353
1354 value_ptr
1355 locate_var_value (var, frame)
1356 register struct symbol *var;
1357 struct frame_info *frame;
1358 {
1359 CORE_ADDR addr = 0;
1360 struct type *type = SYMBOL_TYPE (var);
1361 value_ptr lazy_value;
1362
1363 /* Evaluate it first; if the result is a memory address, we're fine.
1364 Lazy evaluation pays off here. */
1365
1366 lazy_value = read_var_value (var, frame);
1367 if (lazy_value == 0)
1368 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
1369
1370 if (VALUE_LAZY (lazy_value)
1371 || TYPE_CODE (type) == TYPE_CODE_FUNC)
1372 {
1373 addr = VALUE_ADDRESS (lazy_value);
1374 return value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
1375 }
1376
1377 /* Not a memory address; check what the problem was. */
1378 switch (VALUE_LVAL (lazy_value))
1379 {
1380 case lval_register:
1381 case lval_reg_frame_relative:
1382 error ("Address requested for identifier \"%s\" which is in a register.",
1383 SYMBOL_SOURCE_NAME (var));
1384 break;
1385
1386 default:
1387 error ("Can't take address of \"%s\" which isn't an lvalue.",
1388 SYMBOL_SOURCE_NAME (var));
1389 break;
1390 }
1391 return 0; /* For lint -- never reached */
1392 }