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