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