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