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