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