* mdebugread.c (parse_symbol, psymtab_to_symtab_1): Initialize
[deliverable/binutils-gdb.git] / gdb / findvar.c
CommitLineData
bd5635a1 1/* Find a variable's value in memory, for GDB, the GNU debugger.
7d9884b9 2 Copyright 1986, 1987, 1989, 1991 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"
28
326ae3e2
KH
29static void write_register_pid PARAMS ((int regno, LONGEST val, int pid));
30
ade40d31
RP
31/* Basic byte-swapping routines. GDB has needed these for a long time...
32 All extract a target-format integer at ADDR which is LEN bytes long. */
33
34#if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
35 /* 8 bit characters are a pretty safe assumption these days, so we
36 assume it throughout all these swapping routines. If we had to deal with
37 9 bit characters, we would need to make len be in bits and would have
38 to re-write these routines... */
39 you lose
40#endif
41
42LONGEST
43extract_signed_integer (addr, len)
44 PTR addr;
45 int len;
46{
47 LONGEST retval;
48 unsigned char *p;
49 unsigned char *startaddr = (unsigned char *)addr;
50 unsigned char *endaddr = startaddr + len;
51
52 if (len > sizeof (LONGEST))
53 error ("\
54That operation is not available on integers of more than %d bytes.",
55 sizeof (LONGEST));
56
57 /* Start at the most significant end of the integer, and work towards
58 the least significant. */
326ae3e2 59 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
ade40d31 60 {
326ae3e2
KH
61 p = startaddr;
62 /* Do the sign extension once at the start. */
63 retval = ((LONGEST)*p ^ 0x80) - 0x80;
64 for (++p; p < endaddr; ++p)
65 retval = (retval << 8) | *p;
66 }
67 else
68 {
69 p = endaddr - 1;
70 /* Do the sign extension once at the start. */
71 retval = ((LONGEST)*p ^ 0x80) - 0x80;
72 for (--p; p >= startaddr; --p)
73 retval = (retval << 8) | *p;
ade40d31
RP
74 }
75 return retval;
76}
77
78unsigned LONGEST
79extract_unsigned_integer (addr, len)
80 PTR addr;
81 int len;
82{
83 unsigned LONGEST retval;
84 unsigned char *p;
85 unsigned char *startaddr = (unsigned char *)addr;
86 unsigned char *endaddr = startaddr + len;
87
88 if (len > sizeof (unsigned LONGEST))
89 error ("\
90That operation is not available on integers of more than %d bytes.",
91 sizeof (unsigned LONGEST));
92
93 /* Start at the most significant end of the integer, and work towards
94 the least significant. */
95 retval = 0;
326ae3e2 96 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
ade40d31 97 {
326ae3e2
KH
98 for (p = startaddr; p < endaddr; ++p)
99 retval = (retval << 8) | *p;
100 }
101 else
102 {
103 for (p = endaddr - 1; p >= startaddr; --p)
104 retval = (retval << 8) | *p;
ade40d31
RP
105 }
106 return retval;
107}
108
109CORE_ADDR
110extract_address (addr, len)
111 PTR addr;
112 int len;
113{
114 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
115 whether we want this to be true eventually. */
116 return extract_unsigned_integer (addr, len);
117}
118
119void
120store_signed_integer (addr, len, val)
121 PTR addr;
122 int len;
123 LONGEST val;
124{
125 unsigned char *p;
126 unsigned char *startaddr = (unsigned char *)addr;
127 unsigned char *endaddr = startaddr + len;
128
129 /* Start at the least significant end of the integer, and work towards
130 the most significant. */
326ae3e2 131 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
ade40d31 132 {
326ae3e2
KH
133 for (p = endaddr - 1; p >= startaddr; --p)
134 {
135 *p = val & 0xff;
136 val >>= 8;
137 }
138 }
139 else
140 {
141 for (p = startaddr; p < endaddr; ++p)
142 {
143 *p = val & 0xff;
144 val >>= 8;
145 }
ade40d31
RP
146 }
147}
148
149void
150store_unsigned_integer (addr, len, val)
151 PTR addr;
152 int len;
153 unsigned LONGEST val;
154{
155 unsigned char *p;
156 unsigned char *startaddr = (unsigned char *)addr;
157 unsigned char *endaddr = startaddr + len;
158
159 /* Start at the least significant end of the integer, and work towards
160 the most significant. */
326ae3e2 161 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
ade40d31 162 {
326ae3e2
KH
163 for (p = endaddr - 1; p >= startaddr; --p)
164 {
165 *p = val & 0xff;
166 val >>= 8;
167 }
168 }
169 else
170 {
171 for (p = startaddr; p < endaddr; ++p)
172 {
173 *p = val & 0xff;
174 val >>= 8;
175 }
ade40d31
RP
176 }
177}
178
179void
180store_address (addr, len, val)
181 PTR addr;
182 int len;
183 CORE_ADDR val;
184{
185 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
186 whether we want this to be true eventually. */
187 store_unsigned_integer (addr, len, (LONGEST)val);
188}
189\f
bc28e68d
JK
190/* Swap LEN bytes at BUFFER between target and host byte-order. */
191#define SWAP_FLOATING(buffer,len) \
192 do \
193 { \
194 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
195 { \
196 char tmp; \
197 char *p = (char *)(buffer); \
198 char *q = ((char *)(buffer)) + len - 1; \
199 for (; p < q; p++, q--) \
200 { \
201 tmp = *q; \
202 *q = *p; \
203 *p = tmp; \
204 } \
205 } \
206 } \
207 while (0)
326ae3e2 208
bc28e68d
JK
209/* There are various problems with the extract_floating and store_floating
210 routines.
ad09cb2b
PS
211
212 1. These routines only handle byte-swapping, not conversion of
213 formats. So if host is IEEE floating and target is VAX floating,
214 or vice-versa, it loses. This means that we can't (yet) use these
215 routines for extendeds. Extendeds are handled by
48792545
JK
216 REGISTER_CONVERTIBLE. What we want is to use floatformat.h, but that
217 doesn't yet handle VAX floating at all.
ad09cb2b
PS
218
219 2. We can't deal with it if there is more than one floating point
220 format in use. This has to be fixed at the unpack_double level.
221
222 3. We probably should have a LONGEST_DOUBLE or DOUBLEST or whatever
223 we want to call it which is long double where available. */
224
225double
226extract_floating (addr, len)
227 PTR addr;
228 int len;
229{
230 if (len == sizeof (float))
231 {
232 float retval;
233 memcpy (&retval, addr, sizeof (retval));
bc28e68d 234 SWAP_FLOATING (&retval, sizeof (retval));
ad09cb2b
PS
235 return retval;
236 }
237 else if (len == sizeof (double))
238 {
239 double retval;
240 memcpy (&retval, addr, sizeof (retval));
bc28e68d 241 SWAP_FLOATING (&retval, sizeof (retval));
ad09cb2b
PS
242 return retval;
243 }
244 else
245 {
246 error ("Can't deal with a floating point number of %d bytes.", len);
247 }
248}
249
250void
251store_floating (addr, len, val)
252 PTR addr;
253 int len;
254 double val;
255{
256 if (len == sizeof (float))
257 {
258 float floatval = val;
bc28e68d 259 SWAP_FLOATING (&floatval, sizeof (floatval));
ad09cb2b
PS
260 memcpy (addr, &floatval, sizeof (floatval));
261 }
262 else if (len == sizeof (double))
263 {
bc28e68d 264 SWAP_FLOATING (&val, sizeof (val));
ad09cb2b
PS
265 memcpy (addr, &val, sizeof (val));
266 }
267 else
268 {
269 error ("Can't deal with a floating point number of %d bytes.", len);
270 }
271}
272\f
bd5635a1
RP
273#if !defined (GET_SAVED_REGISTER)
274
275/* Return the address in which frame FRAME's value of register REGNUM
276 has been saved in memory. Or return zero if it has not been saved.
277 If REGNUM specifies the SP, the value we return is actually
278 the SP value, not an address where it was saved. */
279
280CORE_ADDR
281find_saved_register (frame, regnum)
326ae3e2 282 struct frame_info *frame;
bd5635a1
RP
283 int regnum;
284{
bd5635a1
RP
285 struct frame_saved_regs saved_regs;
286
326ae3e2 287 register struct frame_info *frame1 = NULL;
bd5635a1
RP
288 register CORE_ADDR addr = 0;
289
326ae3e2 290 if (frame == NULL) /* No regs saved if want current frame */
bd5635a1
RP
291 return 0;
292
293#ifdef HAVE_REGISTER_WINDOWS
294 /* We assume that a register in a register window will only be saved
295 in one place (since the name changes and/or disappears as you go
296 towards inner frames), so we only call get_frame_saved_regs on
297 the current frame. This is directly in contradiction to the
298 usage below, which assumes that registers used in a frame must be
299 saved in a lower (more interior) frame. This change is a result
300 of working on a register window machine; get_frame_saved_regs
301 always returns the registers saved within a frame, within the
302 context (register namespace) of that frame. */
303
304 /* However, note that we don't want this to return anything if
305 nothing is saved (if there's a frame inside of this one). Also,
306 callers to this routine asking for the stack pointer want the
307 stack pointer saved for *this* frame; this is returned from the
308 next frame. */
309
bd5635a1
RP
310 if (REGISTER_IN_WINDOW_P(regnum))
311 {
312 frame1 = get_next_frame (frame);
326ae3e2 313 if (!frame1) return 0; /* Registers of this frame are active. */
bd5635a1
RP
314
315 /* Get the SP from the next frame in; it will be this
316 current frame. */
317 if (regnum != SP_REGNUM)
318 frame1 = frame;
319
326ae3e2 320 get_frame_saved_regs (frame1, &saved_regs);
bd5635a1
RP
321 return saved_regs.regs[regnum]; /* ... which might be zero */
322 }
323#endif /* HAVE_REGISTER_WINDOWS */
324
325 /* Note that this next routine assumes that registers used in
326 frame x will be saved only in the frame that x calls and
327 frames interior to it. This is not true on the sparc, but the
328 above macro takes care of it, so we should be all right. */
329 while (1)
330 {
331 QUIT;
332 frame1 = get_prev_frame (frame1);
333 if (frame1 == 0 || frame1 == frame)
334 break;
326ae3e2 335 get_frame_saved_regs (frame1, &saved_regs);
bd5635a1
RP
336 if (saved_regs.regs[regnum])
337 addr = saved_regs.regs[regnum];
338 }
339
340 return addr;
341}
342
4d50f90a
JK
343/* Find register number REGNUM relative to FRAME and put its (raw,
344 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
345 variable was optimized out (and thus can't be fetched). Set *LVAL
346 to lval_memory, lval_register, or not_lval, depending on whether
347 the value was fetched from memory, from a register, or in a strange
bd5635a1
RP
348 and non-modifiable way (e.g. a frame pointer which was calculated
349 rather than fetched). Set *ADDRP to the address, either in memory
350 on as a REGISTER_BYTE offset into the registers array.
351
352 Note that this implementation never sets *LVAL to not_lval. But
353 it can be replaced by defining GET_SAVED_REGISTER and supplying
354 your own.
355
356 The argument RAW_BUFFER must point to aligned memory. */
4d50f90a 357
bd5635a1
RP
358void
359get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
360 char *raw_buffer;
361 int *optimized;
362 CORE_ADDR *addrp;
326ae3e2 363 struct frame_info *frame;
bd5635a1
RP
364 int regnum;
365 enum lval_type *lval;
366{
367 CORE_ADDR addr;
326ae3e2
KH
368
369 if (!target_has_registers)
370 error ("No registers.");
371
bd5635a1
RP
372 /* Normal systems don't optimize out things with register numbers. */
373 if (optimized != NULL)
374 *optimized = 0;
375 addr = find_saved_register (frame, regnum);
51b57ded 376 if (addr != 0)
bd5635a1
RP
377 {
378 if (lval != NULL)
379 *lval = lval_memory;
380 if (regnum == SP_REGNUM)
381 {
382 if (raw_buffer != NULL)
4d50f90a 383 {
ade40d31
RP
384 /* Put it back in target format. */
385 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), addr);
4d50f90a 386 }
bd5635a1
RP
387 if (addrp != NULL)
388 *addrp = 0;
389 return;
390 }
391 if (raw_buffer != NULL)
392 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
393 }
394 else
395 {
396 if (lval != NULL)
397 *lval = lval_register;
398 addr = REGISTER_BYTE (regnum);
399 if (raw_buffer != NULL)
400 read_register_gen (regnum, raw_buffer);
401 }
402 if (addrp != NULL)
403 *addrp = addr;
404}
405#endif /* GET_SAVED_REGISTER. */
406
407/* Copy the bytes of register REGNUM, relative to the current stack frame,
408 into our memory at MYADDR, in target byte order.
409 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
410
411 Returns 1 if could not be read, 0 if could. */
412
413int
414read_relative_register_raw_bytes (regnum, myaddr)
415 int regnum;
416 char *myaddr;
417{
418 int optim;
419 if (regnum == FP_REGNUM && selected_frame)
420 {
ade40d31
RP
421 /* Put it back in target format. */
422 store_address (myaddr, REGISTER_RAW_SIZE(FP_REGNUM),
423 FRAME_FP(selected_frame));
bd5635a1
RP
424 return 0;
425 }
426
e1ce8aa5 427 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
bd5635a1
RP
428 regnum, (enum lval_type *)NULL);
429 return optim;
430}
431
432/* Return a `value' with the contents of register REGNUM
433 in its virtual format, with the type specified by
434 REGISTER_VIRTUAL_TYPE. */
435
326ae3e2 436value_ptr
bd5635a1
RP
437value_of_register (regnum)
438 int regnum;
439{
440 CORE_ADDR addr;
441 int optim;
326ae3e2 442 register value_ptr reg_val;
bd5635a1 443 char raw_buffer[MAX_REGISTER_RAW_SIZE];
bd5635a1
RP
444 enum lval_type lval;
445
446 get_saved_register (raw_buffer, &optim, &addr,
447 selected_frame, regnum, &lval);
448
48792545 449 reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
ad09cb2b
PS
450
451 /* Convert raw data to virtual format if necessary. */
452
453#ifdef REGISTER_CONVERTIBLE
454 if (REGISTER_CONVERTIBLE (regnum))
455 {
456 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
48792545 457 raw_buffer, VALUE_CONTENTS_RAW (reg_val));
ad09cb2b
PS
458 }
459 else
460#endif
48792545 461 memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
ad09cb2b 462 REGISTER_RAW_SIZE (regnum));
48792545
JK
463 VALUE_LVAL (reg_val) = lval;
464 VALUE_ADDRESS (reg_val) = addr;
465 VALUE_REGNO (reg_val) = regnum;
466 VALUE_OPTIMIZED_OUT (reg_val) = optim;
467 return reg_val;
bd5635a1
RP
468}
469\f
470/* Low level examining and depositing of registers.
471
472 The caller is responsible for making
473 sure that the inferior is stopped before calling the fetching routines,
474 or it will get garbage. (a change from GDB version 3, in which
475 the caller got the value from the last stop). */
476
477/* Contents of the registers in target byte order.
ade40d31 478 We allocate some extra slop since we do a lot of memcpy's around `registers',
bd5635a1
RP
479 and failing-soft is better than failing hard. */
480char registers[REGISTER_BYTES + /* SLOP */ 256];
481
482/* Nonzero if that register has been fetched. */
483char register_valid[NUM_REGS];
484
326ae3e2
KH
485/* The thread/process associated with the current set of registers. For now,
486 -1 is special, and means `no current process'. */
487int registers_pid = -1;
488
bd5635a1 489/* Indicate that registers may have changed, so invalidate the cache. */
326ae3e2 490
bd5635a1
RP
491void
492registers_changed ()
493{
494 int i;
326ae3e2
KH
495 int numregs = ARCH_NUM_REGS;
496
497 registers_pid = -1;
498
499 for (i = 0; i < numregs; i++)
bd5635a1
RP
500 register_valid[i] = 0;
501}
502
503/* Indicate that all registers have been fetched, so mark them all valid. */
504void
505registers_fetched ()
506{
507 int i;
326ae3e2
KH
508 int numregs = ARCH_NUM_REGS;
509 for (i = 0; i < numregs; i++)
bd5635a1
RP
510 register_valid[i] = 1;
511}
512
513/* Copy LEN bytes of consecutive data from registers
514 starting with the REGBYTE'th byte of register data
515 into memory at MYADDR. */
516
517void
518read_register_bytes (regbyte, myaddr, len)
519 int regbyte;
520 char *myaddr;
521 int len;
522{
523 /* Fetch all registers. */
326ae3e2
KH
524 int i, numregs;
525
526 if (registers_pid != inferior_pid)
527 {
528 registers_changed ();
529 registers_pid = inferior_pid;
530 }
531
532 numregs = ARCH_NUM_REGS;
533 for (i = 0; i < numregs; i++)
bd5635a1
RP
534 if (!register_valid[i])
535 {
536 target_fetch_registers (-1);
537 break;
538 }
539 if (myaddr != NULL)
0791c5ea 540 memcpy (myaddr, &registers[regbyte], len);
bd5635a1
RP
541}
542
543/* Read register REGNO into memory at MYADDR, which must be large enough
f2ebc25f
JK
544 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
545 If the register is known to be the size of a CORE_ADDR or smaller,
546 read_register can be used instead. */
bd5635a1
RP
547void
548read_register_gen (regno, myaddr)
549 int regno;
550 char *myaddr;
551{
326ae3e2
KH
552 if (registers_pid != inferior_pid)
553 {
554 registers_changed ();
555 registers_pid = inferior_pid;
556 }
557
bd5635a1
RP
558 if (!register_valid[regno])
559 target_fetch_registers (regno);
0791c5ea
JK
560 memcpy (myaddr, &registers[REGISTER_BYTE (regno)],
561 REGISTER_RAW_SIZE (regno));
bd5635a1
RP
562}
563
564/* Copy LEN bytes of consecutive data from memory at MYADDR
565 into registers starting with the REGBYTE'th byte of register data. */
566
567void
568write_register_bytes (regbyte, myaddr, len)
569 int regbyte;
570 char *myaddr;
571 int len;
572{
326ae3e2
KH
573 if (registers_pid != inferior_pid)
574 {
575 registers_changed ();
576 registers_pid = inferior_pid;
577 }
578
bd5635a1
RP
579 /* Make sure the entire registers array is valid. */
580 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
0791c5ea 581 memcpy (&registers[regbyte], myaddr, len);
bd5635a1
RP
582 target_store_registers (-1);
583}
584
ade40d31
RP
585/* Return the raw contents of register REGNO, regarding it as an integer. */
586/* This probably should be returning LONGEST rather than CORE_ADDR. */
bd5635a1
RP
587
588CORE_ADDR
589read_register (regno)
590 int regno;
591{
326ae3e2
KH
592 if (registers_pid != inferior_pid)
593 {
594 registers_changed ();
595 registers_pid = inferior_pid;
596 }
597
bd5635a1
RP
598 if (!register_valid[regno])
599 target_fetch_registers (regno);
0791c5ea 600
ade40d31
RP
601 return extract_address (&registers[REGISTER_BYTE (regno)],
602 REGISTER_RAW_SIZE(regno));
bd5635a1
RP
603}
604
326ae3e2
KH
605CORE_ADDR
606read_register_pid (regno, pid)
607 int regno, pid;
608{
609 int save_pid;
610 CORE_ADDR retval;
611
612 if (pid == inferior_pid)
613 return read_register (regno);
614
615 save_pid = inferior_pid;
616
617 inferior_pid = pid;
618
619 retval = read_register (regno);
620
621 inferior_pid = save_pid;
622
623 return retval;
624}
625
bd5635a1
RP
626/* Registers we shouldn't try to store. */
627#if !defined (CANNOT_STORE_REGISTER)
628#define CANNOT_STORE_REGISTER(regno) 0
629#endif
630
ade40d31
RP
631/* Store VALUE, into the raw contents of register number REGNO. */
632/* FIXME: The val arg should probably be a LONGEST. */
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.227339 seconds and 4 git commands to generate.