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