* top.c (set_endian_from_file): Use new bfd_big_endian macro.
[deliverable/binutils-gdb.git] / gdb / findvar.c
CommitLineData
bd5635a1 1/* Find a variable's value in memory, for GDB, the GNU debugger.
a1a0d974 2 Copyright 1986, 1987, 1989, 1991, 1994, 1995 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
36b9d39c 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
36b9d39c
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
36b9d39c 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
36b9d39c
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
bd5635a1 20#include "defs.h"
bd5635a1 21#include "symtab.h"
51b57ded 22#include "gdbtypes.h"
bd5635a1
RP
23#include "frame.h"
24#include "value.h"
25#include "gdbcore.h"
26#include "inferior.h"
27#include "target.h"
2b576293
C
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
bd5635a1 34
326ae3e2
KH
35static void write_register_pid PARAMS ((int regno, LONGEST val, int pid));
36
ade40d31
RP
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
48LONGEST
49extract_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 ("\
60That 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. */
326ae3e2 65 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
ade40d31 66 {
326ae3e2
KH
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;
ade40d31
RP
80 }
81 return retval;
82}
83
84unsigned LONGEST
85extract_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 ("\
96That 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;
326ae3e2 102 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
ade40d31 103 {
326ae3e2
KH
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;
ade40d31
RP
111 }
112 return retval;
113}
114
115CORE_ADDR
116extract_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
125void
126store_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. */
326ae3e2 137 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
ade40d31 138 {
326ae3e2
KH
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 }
ade40d31
RP
152 }
153}
154
155void
156store_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. */
326ae3e2 167 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
ade40d31 168 {
326ae3e2
KH
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 }
ade40d31
RP
182 }
183}
184
185void
186store_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
bc28e68d
JK
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)
326ae3e2 214
bc28e68d
JK
215/* There are various problems with the extract_floating and store_floating
216 routines.
ad09cb2b
PS
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
48792545
JK
222 REGISTER_CONVERTIBLE. What we want is to use floatformat.h, but that
223 doesn't yet handle VAX floating at all.
ad09cb2b
PS
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
231double
232extract_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));
bc28e68d 240 SWAP_FLOATING (&retval, sizeof (retval));
ad09cb2b
PS
241 return retval;
242 }
243 else if (len == sizeof (double))
244 {
245 double retval;
246 memcpy (&retval, addr, sizeof (retval));
bc28e68d 247 SWAP_FLOATING (&retval, sizeof (retval));
ad09cb2b
PS
248 return retval;
249 }
250 else
251 {
252 error ("Can't deal with a floating point number of %d bytes.", len);
253 }
254}
255
256void
257store_floating (addr, len, val)
258 PTR addr;
259 int len;
260 double val;
261{
262 if (len == sizeof (float))
263 {
264 float floatval = val;
bc28e68d 265 SWAP_FLOATING (&floatval, sizeof (floatval));
ad09cb2b
PS
266 memcpy (addr, &floatval, sizeof (floatval));
267 }
268 else if (len == sizeof (double))
269 {
bc28e68d 270 SWAP_FLOATING (&val, sizeof (val));
ad09cb2b
PS
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
bd5635a1
RP
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
286CORE_ADDR
287find_saved_register (frame, regnum)
326ae3e2 288 struct frame_info *frame;
bd5635a1
RP
289 int regnum;
290{
bd5635a1
RP
291 struct frame_saved_regs saved_regs;
292
326ae3e2 293 register struct frame_info *frame1 = NULL;
bd5635a1
RP
294 register CORE_ADDR addr = 0;
295
326ae3e2 296 if (frame == NULL) /* No regs saved if want current frame */
bd5635a1
RP
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
bd5635a1
RP
316 if (REGISTER_IN_WINDOW_P(regnum))
317 {
318 frame1 = get_next_frame (frame);
326ae3e2 319 if (!frame1) return 0; /* Registers of this frame are active. */
bd5635a1
RP
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
326ae3e2 326 get_frame_saved_regs (frame1, &saved_regs);
bd5635a1
RP
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;
326ae3e2 341 get_frame_saved_regs (frame1, &saved_regs);
bd5635a1
RP
342 if (saved_regs.regs[regnum])
343 addr = saved_regs.regs[regnum];
344 }
345
346 return addr;
347}
348
4d50f90a
JK
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
bd5635a1
RP
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. */
4d50f90a 363
bd5635a1
RP
364void
365get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
366 char *raw_buffer;
367 int *optimized;
368 CORE_ADDR *addrp;
326ae3e2 369 struct frame_info *frame;
bd5635a1
RP
370 int regnum;
371 enum lval_type *lval;
372{
373 CORE_ADDR addr;
326ae3e2
KH
374
375 if (!target_has_registers)
376 error ("No registers.");
377
bd5635a1
RP
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);
51b57ded 382 if (addr != 0)
bd5635a1
RP
383 {
384 if (lval != NULL)
385 *lval = lval_memory;
386 if (regnum == SP_REGNUM)
387 {
388 if (raw_buffer != NULL)
4d50f90a 389 {
ade40d31
RP
390 /* Put it back in target format. */
391 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), addr);
4d50f90a 392 }
bd5635a1
RP
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
419int
420read_relative_register_raw_bytes (regnum, myaddr)
421 int regnum;
422 char *myaddr;
423{
424 int optim;
425 if (regnum == FP_REGNUM && selected_frame)
426 {
ade40d31
RP
427 /* Put it back in target format. */
428 store_address (myaddr, REGISTER_RAW_SIZE(FP_REGNUM),
429 FRAME_FP(selected_frame));
bd5635a1
RP
430 return 0;
431 }
432
e1ce8aa5 433 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
bd5635a1
RP
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
326ae3e2 442value_ptr
bd5635a1
RP
443value_of_register (regnum)
444 int regnum;
445{
446 CORE_ADDR addr;
447 int optim;
326ae3e2 448 register value_ptr reg_val;
bd5635a1 449 char raw_buffer[MAX_REGISTER_RAW_SIZE];
bd5635a1
RP
450 enum lval_type lval;
451
452 get_saved_register (raw_buffer, &optim, &addr,
453 selected_frame, regnum, &lval);
454
48792545 455 reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
ad09cb2b
PS
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),
48792545 463 raw_buffer, VALUE_CONTENTS_RAW (reg_val));
ad09cb2b
PS
464 }
465 else
466#endif
48792545 467 memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
ad09cb2b 468 REGISTER_RAW_SIZE (regnum));
48792545
JK
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;
bd5635a1
RP
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.
ade40d31 484 We allocate some extra slop since we do a lot of memcpy's around `registers',
bd5635a1
RP
485 and failing-soft is better than failing hard. */
486char registers[REGISTER_BYTES + /* SLOP */ 256];
487
488/* Nonzero if that register has been fetched. */
489char register_valid[NUM_REGS];
490
326ae3e2
KH
491/* The thread/process associated with the current set of registers. For now,
492 -1 is special, and means `no current process'. */
493int registers_pid = -1;
494
bd5635a1 495/* Indicate that registers may have changed, so invalidate the cache. */
326ae3e2 496
bd5635a1
RP
497void
498registers_changed ()
499{
500 int i;
326ae3e2
KH
501 int numregs = ARCH_NUM_REGS;
502
503 registers_pid = -1;
504
505 for (i = 0; i < numregs; i++)
bd5635a1 506 register_valid[i] = 0;
2b576293
C
507
508 if (registers_changed_hook)
509 registers_changed_hook ();
bd5635a1
RP
510}
511
512/* Indicate that all registers have been fetched, so mark them all valid. */
513void
514registers_fetched ()
515{
516 int i;
326ae3e2
KH
517 int numregs = ARCH_NUM_REGS;
518 for (i = 0; i < numregs; i++)
bd5635a1
RP
519 register_valid[i] = 1;
520}
521
2b576293
C
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
bd5635a1
RP
539 into memory at MYADDR. */
540
541void
2b576293
C
542read_register_bytes (inregbyte, myaddr, inlen)
543 int inregbyte;
bd5635a1 544 char *myaddr;
2b576293 545 int inlen;
bd5635a1 546{
2b576293
C
547 int inregend = inregbyte + inlen;
548 int regno;
326ae3e2
KH
549
550 if (registers_pid != inferior_pid)
551 {
552 registers_changed ();
553 registers_pid = inferior_pid;
554 }
555
2b576293
C
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
bd5635a1 585 if (myaddr != NULL)
2b576293 586 memcpy (myaddr, &registers[inregbyte], inlen);
bd5635a1
RP
587}
588
589/* Read register REGNO into memory at MYADDR, which must be large enough
f2ebc25f
JK
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. */
bd5635a1
RP
593void
594read_register_gen (regno, myaddr)
595 int regno;
596 char *myaddr;
597{
326ae3e2
KH
598 if (registers_pid != inferior_pid)
599 {
600 registers_changed ();
601 registers_pid = inferior_pid;
602 }
603
bd5635a1
RP
604 if (!register_valid[regno])
605 target_fetch_registers (regno);
0791c5ea
JK
606 memcpy (myaddr, &registers[REGISTER_BYTE (regno)],
607 REGISTER_RAW_SIZE (regno));
bd5635a1
RP
608}
609
2b576293
C
610/* Write register REGNO at MYADDR to the target. MYADDR points at
611 REGISTER_RAW_BYTES(REGNO), which must be in target byte-order. */
bd5635a1
RP
612
613void
2b576293
C
614write_register_gen (regno, myaddr)
615 int regno;
bd5635a1 616 char *myaddr;
bd5635a1 617{
2b576293
C
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
326ae3e2
KH
625 if (registers_pid != inferior_pid)
626 {
627 registers_changed ();
628 registers_pid = inferior_pid;
629 }
630
2b576293
C
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
652void
653write_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 }
bd5635a1
RP
703}
704
ade40d31
RP
705/* Return the raw contents of register REGNO, regarding it as an integer. */
706/* This probably should be returning LONGEST rather than CORE_ADDR. */
bd5635a1
RP
707
708CORE_ADDR
709read_register (regno)
710 int regno;
711{
326ae3e2
KH
712 if (registers_pid != inferior_pid)
713 {
714 registers_changed ();
715 registers_pid = inferior_pid;
716 }
717
bd5635a1
RP
718 if (!register_valid[regno])
719 target_fetch_registers (regno);
0791c5ea 720
ade40d31
RP
721 return extract_address (&registers[REGISTER_BYTE (regno)],
722 REGISTER_RAW_SIZE(regno));
bd5635a1
RP
723}
724
326ae3e2
KH
725CORE_ADDR
726read_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
ade40d31 746/* Store VALUE, into the raw contents of register number REGNO. */
bd5635a1
RP
747
748void
749write_register (regno, val)
5573d7d4 750 int regno;
443abae1 751 LONGEST val;
bd5635a1 752{
ade40d31 753 PTR buf;
df14b38b 754 int size;
ade40d31 755
bd5635a1
RP
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
326ae3e2
KH
761 if (registers_pid != inferior_pid)
762 {
763 registers_changed ();
764 registers_pid = inferior_pid;
765 }
766
ade40d31
RP
767 size = REGISTER_RAW_SIZE(regno);
768 buf = alloca (size);
769 store_signed_integer (buf, size, (LONGEST) val);
770
df14b38b
SC
771 /* If we have a valid copy of the register, and new value == old value,
772 then don't bother doing the actual store. */
bd5635a1 773
326ae3e2
KH
774 if (register_valid [regno]
775 && memcmp (&registers[REGISTER_BYTE (regno)], buf, size) == 0)
776 return;
df14b38b
SC
777
778 target_prepare_to_store ();
779
ade40d31 780 memcpy (&registers[REGISTER_BYTE (regno)], buf, size);
df14b38b
SC
781
782 register_valid [regno] = 1;
bd5635a1
RP
783
784 target_store_registers (regno);
785}
786
326ae3e2
KH
787static void
788write_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
bd5635a1
RP
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
814void
815supply_register (regno, val)
816 int regno;
817 char *val;
818{
326ae3e2
KH
819 if (registers_pid != inferior_pid)
820 {
821 registers_changed ();
822 registers_pid = inferior_pid;
823 }
824
bd5635a1 825 register_valid[regno] = 1;
0791c5ea
JK
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
bd5635a1 833}
326ae3e2
KH
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
840CORE_ADDR
841read_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
850CORE_ADDR
851read_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
861void
862write_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
878void
879write_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
898CORE_ADDR
899read_sp ()
900{
901#ifdef TARGET_READ_SP
902 return TARGET_READ_SP ();
903#else
904 return read_register (SP_REGNUM);
905#endif
906}
907
908void
909write_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
919CORE_ADDR
920read_fp ()
921{
922#ifdef TARGET_READ_FP
923 return TARGET_READ_FP ();
924#else
925 return read_register (FP_REGNUM);
926#endif
927}
928
929void
930write_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}
bd5635a1 939\f
443abae1
JK
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. */
943int
944symbol_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 }
100f92e2 977 return 1;
443abae1
JK
978}
979
bd5635a1
RP
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.
777bef06
JK
983 If the variable cannot be found, return a zero pointer.
984 If FRAME is NULL, use the selected_frame. */
bd5635a1 985
326ae3e2 986value_ptr
bd5635a1
RP
987read_var_value (var, frame)
988 register struct symbol *var;
326ae3e2 989 struct frame_info *frame;
bd5635a1 990{
326ae3e2 991 register value_ptr v;
bd5635a1
RP
992 struct type *type = SYMBOL_TYPE (var);
993 CORE_ADDR addr;
bd5635a1
RP
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
326ae3e2 1000 if (frame == NULL) frame = selected_frame;
bd5635a1
RP
1001
1002 switch (SYMBOL_CLASS (var))
1003 {
1004 case LOC_CONST:
ade40d31
RP
1005 /* Put the constant back in target format. */
1006 store_signed_integer (VALUE_CONTENTS_RAW (v), len,
1007 (LONGEST) SYMBOL_VALUE (var));
bd5635a1
RP
1008 VALUE_LVAL (v) = not_lval;
1009 return v;
1010
1011 case LOC_LABEL:
ade40d31
RP
1012 /* Put the constant back in target format. */
1013 store_address (VALUE_CONTENTS_RAW (v), len, SYMBOL_VALUE_ADDRESS (var));
bd5635a1
RP
1014 VALUE_LVAL (v) = not_lval;
1015 return v;
1016
1017 case LOC_CONST_BYTES:
36b9d39c
JG
1018 {
1019 char *bytes_addr;
1020 bytes_addr = SYMBOL_VALUE_BYTES (var);
0791c5ea 1021 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
36b9d39c
JG
1022 VALUE_LVAL (v) = not_lval;
1023 return v;
1024 }
bd5635a1
RP
1025
1026 case LOC_STATIC:
bd5635a1
RP
1027 addr = SYMBOL_VALUE_ADDRESS (var);
1028 break;
1029
bd5635a1 1030 case LOC_ARG:
326ae3e2 1031 if (frame == NULL)
ade40d31 1032 return 0;
326ae3e2 1033 addr = FRAME_ARGS_ADDRESS (frame);
51b57ded 1034 if (!addr)
326ae3e2 1035 return 0;
bd5635a1
RP
1036 addr += SYMBOL_VALUE (var);
1037 break;
ade40d31 1038
bd5635a1 1039 case LOC_REF_ARG:
326ae3e2 1040 if (frame == NULL)
ade40d31 1041 return 0;
326ae3e2 1042 addr = FRAME_ARGS_ADDRESS (frame);
51b57ded 1043 if (!addr)
326ae3e2 1044 return 0;
bd5635a1 1045 addr += SYMBOL_VALUE (var);
ade40d31
RP
1046 addr = read_memory_unsigned_integer
1047 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
bd5635a1 1048 break;
ade40d31 1049
bd5635a1
RP
1050 case LOC_LOCAL:
1051 case LOC_LOCAL_ARG:
326ae3e2 1052 if (frame == NULL)
ade40d31 1053 return 0;
326ae3e2 1054 addr = FRAME_LOCALS_ADDRESS (frame);
51b57ded 1055 addr += SYMBOL_VALUE (var);
bd5635a1
RP
1056 break;
1057
ade40d31
RP
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
bd5635a1
RP
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:
35247ccd 1079 case LOC_REGPARM_ADDR:
bd5635a1 1080 {
777bef06 1081 struct block *b;
bd5635a1 1082
777bef06
JK
1083 if (frame == NULL)
1084 return 0;
1085 b = get_frame_block (frame);
1086
bd5635a1 1087
35247ccd 1088 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
0791c5ea 1089 {
326ae3e2
KH
1090 addr =
1091 value_as_pointer (value_from_register (lookup_pointer_type (type),
1092 SYMBOL_VALUE (var),
1093 frame));
0791c5ea
JK
1094 VALUE_LVAL (v) = lval_memory;
1095 }
bd5635a1 1096 else
326ae3e2 1097 return value_from_register (type, SYMBOL_VALUE (var), frame);
bd5635a1
RP
1098 }
1099 break;
1100
35247ccd
SG
1101 case LOC_OPTIMIZED_OUT:
1102 VALUE_LVAL (v) = not_lval;
1103 VALUE_OPTIMIZED_OUT (v) = 1;
1104 return v;
1105
bd5635a1
RP
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
326ae3e2 1119value_ptr
bd5635a1
RP
1120value_from_register (type, regnum, frame)
1121 struct type *type;
1122 int regnum;
326ae3e2 1123 struct frame_info *frame;
bd5635a1
RP
1124{
1125 char raw_buffer [MAX_REGISTER_RAW_SIZE];
bd5635a1
RP
1126 CORE_ADDR addr;
1127 int optim;
326ae3e2 1128 value_ptr v = allocate_value (type);
bd5635a1
RP
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
0791c5ea
JK
1141 if (num_storage_locs > 1
1142#ifdef GDB_TARGET_IS_H8500
1143 || TYPE_CODE (type) == TYPE_CODE_PTR
1144#endif
1145 )
bd5635a1
RP
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;
5573d7d4 1153 CORE_ADDR first_addr = 0;
bd5635a1
RP
1154
1155 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
1156
1157 /* Copy all of the data out, whereever it may be. */
1158
0791c5ea
JK
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
35247ccd
SG
1167 if (TYPE_CODE (type) == TYPE_CODE_PTR
1168 && len > 2)
bd5635a1 1169 {
0791c5ea
JK
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,
bd5635a1
RP
1187 &optim,
1188 &addr,
1189 frame,
0791c5ea 1190 page_regnum,
bd5635a1 1191 &lval);
0791c5ea 1192
bd5635a1
RP
1193 if (lval == lval_register)
1194 reg_stor++;
1195 else
df14b38b
SC
1196 mem_stor++;
1197 first_addr = addr;
0791c5ea 1198 last_addr = addr;
bd5635a1 1199
0791c5ea
JK
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);
bd5635a1
RP
1213 }
1214 last_addr = addr;
1215 }
0791c5ea
JK
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);
df14b38b
SC
1229
1230 if (regnum == local_regnum)
1231 first_addr = addr;
0791c5ea
JK
1232 if (lval == lval_register)
1233 reg_stor++;
1234 else
1235 {
1236 mem_stor++;
0791c5ea
JK
1237
1238 mem_tracking =
1239 (mem_tracking
1240 && (regnum == local_regnum
1241 || addr == last_addr));
1242 }
1243 last_addr = addr;
1244 }
bd5635a1
RP
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. */
0791c5ea 1276 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
bd5635a1 1277
df14b38b
SC
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
bd5635a1
RP
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;
ad09cb2b
PS
1294
1295 /* Convert raw data to virtual format if necessary. */
bd5635a1 1296
ad09cb2b 1297#ifdef REGISTER_CONVERTIBLE
bd5635a1
RP
1298 if (REGISTER_CONVERTIBLE (regnum))
1299 {
ad09cb2b
PS
1300 REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
1301 raw_buffer, VALUE_CONTENTS_RAW (v));
bd5635a1
RP
1302 }
1303 else
ad09cb2b 1304#endif
bd5635a1
RP
1305 {
1306 /* Raw and virtual formats are the same for this register. */
1307
326ae3e2 1308 if (TARGET_BYTE_ORDER == BIG_ENDIAN && len < REGISTER_RAW_SIZE (regnum))
bd5635a1
RP
1309 {
1310 /* Big-endian, and we want less than full size. */
1311 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
1312 }
bd5635a1 1313
ad09cb2b 1314 memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
bd5635a1
RP
1315 }
1316
1317 return v;
1318}
1319\f
36b9d39c 1320/* Given a struct symbol for a variable or function,
bd5635a1 1321 and a stack frame id,
36b9d39c
JG
1322 return a (pointer to a) struct value containing the properly typed
1323 address. */
bd5635a1 1324
326ae3e2 1325value_ptr
bd5635a1
RP
1326locate_var_value (var, frame)
1327 register struct symbol *var;
326ae3e2 1328 struct frame_info *frame;
bd5635a1
RP
1329{
1330 CORE_ADDR addr = 0;
1331 struct type *type = SYMBOL_TYPE (var);
326ae3e2 1332 value_ptr lazy_value;
bd5635a1
RP
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)
0791c5ea 1339 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
bd5635a1 1340
36b9d39c
JG
1341 if (VALUE_LAZY (lazy_value)
1342 || TYPE_CODE (type) == TYPE_CODE_FUNC)
bd5635a1
RP
1343 {
1344 addr = VALUE_ADDRESS (lazy_value);
7d9884b9 1345 return value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
bd5635a1
RP
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.",
0791c5ea 1354 SYMBOL_SOURCE_NAME (var));
bd5635a1
RP
1355 break;
1356
1357 default:
1358 error ("Can't take address of \"%s\" which isn't an lvalue.",
0791c5ea 1359 SYMBOL_SOURCE_NAME (var));
bd5635a1
RP
1360 break;
1361 }
1362 return 0; /* For lint -- never reached */
1363}
This page took 0.263775 seconds and 4 git commands to generate.