]> Git Repo - binutils.git/blob - gdb/findvar.c
*** empty log message ***
[binutils.git] / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2    Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "frame.h"
24 #include "value.h"
25 #include "gdbcore.h"
26 #include "inferior.h"
27 #include "target.h"
28
29 #if !defined (GET_SAVED_REGISTER)
30
31 /* Return the address in which frame FRAME's value of register REGNUM
32    has been saved in memory.  Or return zero if it has not been saved.
33    If REGNUM specifies the SP, the value we return is actually
34    the SP value, not an address where it was saved.  */
35
36 CORE_ADDR
37 find_saved_register (frame, regnum)
38      FRAME frame;
39      int regnum;
40 {
41   struct frame_info *fi;
42   struct frame_saved_regs saved_regs;
43
44   register FRAME frame1 = 0;
45   register CORE_ADDR addr = 0;
46
47   if (frame == 0)               /* No regs saved if want current frame */
48     return 0;
49
50 #ifdef HAVE_REGISTER_WINDOWS
51   /* We assume that a register in a register window will only be saved
52      in one place (since the name changes and/or disappears as you go
53      towards inner frames), so we only call get_frame_saved_regs on
54      the current frame.  This is directly in contradiction to the
55      usage below, which assumes that registers used in a frame must be
56      saved in a lower (more interior) frame.  This change is a result
57      of working on a register window machine; get_frame_saved_regs
58      always returns the registers saved within a frame, within the
59      context (register namespace) of that frame. */
60
61   /* However, note that we don't want this to return anything if
62      nothing is saved (if there's a frame inside of this one).  Also,
63      callers to this routine asking for the stack pointer want the
64      stack pointer saved for *this* frame; this is returned from the
65      next frame.  */
66      
67
68   if (REGISTER_IN_WINDOW_P(regnum))
69     {
70       frame1 = get_next_frame (frame);
71       if (!frame1) return 0;    /* Registers of this frame are
72                                    active.  */
73       
74       /* Get the SP from the next frame in; it will be this
75          current frame.  */
76       if (regnum != SP_REGNUM)
77         frame1 = frame; 
78           
79       fi = get_frame_info (frame1);
80       get_frame_saved_regs (fi, &saved_regs);
81       return saved_regs.regs[regnum];   /* ... which might be zero */
82     }
83 #endif /* HAVE_REGISTER_WINDOWS */
84
85   /* Note that this next routine assumes that registers used in
86      frame x will be saved only in the frame that x calls and
87      frames interior to it.  This is not true on the sparc, but the
88      above macro takes care of it, so we should be all right. */
89   while (1)
90     {
91       QUIT;
92       frame1 = get_prev_frame (frame1);
93       if (frame1 == 0 || frame1 == frame)
94         break;
95       fi = get_frame_info (frame1);
96       get_frame_saved_regs (fi, &saved_regs);
97       if (saved_regs.regs[regnum])
98         addr = saved_regs.regs[regnum];
99     }
100
101   return addr;
102 }
103
104 /* Find register number REGNUM relative to FRAME and put its
105    (raw) contents in *RAW_BUFFER.  Set *OPTIMIZED if the variable
106    was optimized out (and thus can't be fetched).  Set *LVAL to
107    lval_memory, lval_register, or not_lval, depending on whether the
108    value was fetched from memory, from a register, or in a strange
109    and non-modifiable way (e.g. a frame pointer which was calculated
110    rather than fetched).  Set *ADDRP to the address, either in memory
111    on as a REGISTER_BYTE offset into the registers array.
112
113    Note that this implementation never sets *LVAL to not_lval.  But
114    it can be replaced by defining GET_SAVED_REGISTER and supplying
115    your own.
116
117    The argument RAW_BUFFER must point to aligned memory.  */
118 void
119 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
120      char *raw_buffer;
121      int *optimized;
122      CORE_ADDR *addrp;
123      FRAME frame;
124      int regnum;
125      enum lval_type *lval;
126 {
127   CORE_ADDR addr;
128   /* Normal systems don't optimize out things with register numbers.  */
129   if (optimized != NULL)
130     *optimized = 0;
131   addr = find_saved_register (frame, regnum);
132   if (addr != 0)
133     {
134       if (lval != NULL)
135         *lval = lval_memory;
136       if (regnum == SP_REGNUM)
137         {
138           if (raw_buffer != NULL)
139             *(CORE_ADDR *)raw_buffer = addr;
140           if (addrp != NULL)
141             *addrp = 0;
142           return;
143         }
144       if (raw_buffer != NULL)
145         read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
146     }
147   else
148     {
149       if (lval != NULL)
150         *lval = lval_register;
151       addr = REGISTER_BYTE (regnum);
152       if (raw_buffer != NULL)
153         read_register_gen (regnum, raw_buffer);
154     }
155   if (addrp != NULL)
156     *addrp = addr;
157 }
158 #endif /* GET_SAVED_REGISTER.  */
159
160 /* Copy the bytes of register REGNUM, relative to the current stack frame,
161    into our memory at MYADDR, in target byte order.
162    The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
163
164    Returns 1 if could not be read, 0 if could.  */
165
166 int
167 read_relative_register_raw_bytes (regnum, myaddr)
168      int regnum;
169      char *myaddr;
170 {
171   int optim;
172   if (regnum == FP_REGNUM && selected_frame)
173     {
174       (void) memcpy (myaddr, &FRAME_FP(selected_frame),
175                      REGISTER_RAW_SIZE(FP_REGNUM));
176       SWAP_TARGET_AND_HOST (myaddr, REGISTER_RAW_SIZE(FP_REGNUM)); /* in target order */
177       return 0;
178     }
179
180   get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
181                       regnum, (enum lval_type *)NULL);
182   return optim;
183 }
184
185 /* Return a `value' with the contents of register REGNUM
186    in its virtual format, with the type specified by
187    REGISTER_VIRTUAL_TYPE.  */
188
189 value
190 value_of_register (regnum)
191      int regnum;
192 {
193   CORE_ADDR addr;
194   int optim;
195   register value val;
196   char raw_buffer[MAX_REGISTER_RAW_SIZE];
197   char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
198   enum lval_type lval;
199
200   get_saved_register (raw_buffer, &optim, &addr,
201                       selected_frame, regnum, &lval);
202
203   target_convert_to_virtual (regnum, raw_buffer, virtual_buffer);
204   val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
205   (void) memcpy (VALUE_CONTENTS_RAW (val), virtual_buffer,
206                  REGISTER_VIRTUAL_SIZE (regnum));
207   VALUE_LVAL (val) = lval;
208   VALUE_ADDRESS (val) = addr;
209   VALUE_REGNO (val) = regnum;
210   VALUE_OPTIMIZED_OUT (val) = optim;
211   return val;
212 }
213 \f
214 /* Low level examining and depositing of registers.
215
216    The caller is responsible for making
217    sure that the inferior is stopped before calling the fetching routines,
218    or it will get garbage.  (a change from GDB version 3, in which
219    the caller got the value from the last stop).  */
220
221 /* Contents of the registers in target byte order.
222    We allocate some extra slop since we do a lot of bcopy's around `registers',
223    and failing-soft is better than failing hard.  */
224 char registers[REGISTER_BYTES + /* SLOP */ 256];
225
226 /* Nonzero if that register has been fetched.  */
227 char register_valid[NUM_REGS];
228
229 /* Indicate that registers may have changed, so invalidate the cache.  */
230 void
231 registers_changed ()
232 {
233   int i;
234   for (i = 0; i < NUM_REGS; i++)
235     register_valid[i] = 0;
236 }
237
238 /* Indicate that all registers have been fetched, so mark them all valid.  */
239 void
240 registers_fetched ()
241 {
242   int i;
243   for (i = 0; i < NUM_REGS; i++)
244     register_valid[i] = 1;
245 }
246
247 /* Copy LEN bytes of consecutive data from registers
248    starting with the REGBYTE'th byte of register data
249    into memory at MYADDR.  */
250
251 void
252 read_register_bytes (regbyte, myaddr, len)
253      int regbyte;
254      char *myaddr;
255      int len;
256 {
257   /* Fetch all registers.  */
258   int i;
259   for (i = 0; i < NUM_REGS; i++)
260     if (!register_valid[i])
261       {
262         target_fetch_registers (-1);
263         break;
264       }
265   if (myaddr != NULL)
266     (void) memcpy (myaddr, &registers[regbyte], len);
267 }
268
269 /* Read register REGNO into memory at MYADDR, which must be large enough
270    for REGISTER_RAW_BYTES (REGNO).  Target byte-order.
271    If the register is known to be the size of a CORE_ADDR or smaller,
272    read_register can be used instead.  */
273 void
274 read_register_gen (regno, myaddr)
275      int regno;
276      char *myaddr;
277 {
278   if (!register_valid[regno])
279     target_fetch_registers (regno);
280   (void) memcpy (myaddr, &registers[REGISTER_BYTE (regno)],
281                  REGISTER_RAW_SIZE (regno));
282 }
283
284 /* Copy LEN bytes of consecutive data from memory at MYADDR
285    into registers starting with the REGBYTE'th byte of register data.  */
286
287 void
288 write_register_bytes (regbyte, myaddr, len)
289      int regbyte;
290      char *myaddr;
291      int len;
292 {
293   /* Make sure the entire registers array is valid.  */
294   read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
295   (void) memcpy (&registers[regbyte], myaddr, len);
296   target_store_registers (-1);
297 }
298
299 /* Return the contents of register REGNO, regarding it as an integer.  */
300 /* FIXME, this loses when the REGISTER_VIRTUAL (REGNO) is true.  Also,
301    why is the return type CORE_ADDR rather than some integer type?  */
302
303 CORE_ADDR
304 read_register (regno)
305      int regno;
306 {
307   REGISTER_TYPE reg;
308
309   if (!register_valid[regno])
310     target_fetch_registers (regno);
311   memcpy (&reg, &registers[REGISTER_BYTE (regno)], sizeof (REGISTER_TYPE));
312   SWAP_TARGET_AND_HOST (&reg, sizeof (REGISTER_TYPE));
313   return reg;
314 }
315
316 /* Registers we shouldn't try to store.  */
317 #if !defined (CANNOT_STORE_REGISTER)
318 #define CANNOT_STORE_REGISTER(regno) 0
319 #endif
320
321 /* Store VALUE in the register number REGNO, regarded as an integer.  */
322 /* FIXME, this loses when REGISTER_VIRTUAL (REGNO) is true.  Also, 
323    shouldn't the val arg be a LONGEST or something?  */
324
325 void
326 write_register (regno, val)
327      int regno, val;
328 {
329   REGISTER_TYPE reg;
330
331   /* On the sparc, writing %g0 is a no-op, so we don't even want to change
332      the registers array if something writes to this register.  */
333   if (CANNOT_STORE_REGISTER (regno))
334     return;
335
336   reg = val;
337   SWAP_TARGET_AND_HOST (&reg, sizeof (REGISTER_TYPE));
338
339   target_prepare_to_store ();
340
341   register_valid [regno] = 1;
342   memcpy (&registers[REGISTER_BYTE (regno)], &reg, sizeof (REGISTER_TYPE));
343
344   target_store_registers (regno);
345 }
346
347 /* Record that register REGNO contains VAL.
348    This is used when the value is obtained from the inferior or core dump,
349    so there is no need to store the value there.  */
350
351 void
352 supply_register (regno, val)
353      int regno;
354      char *val;
355 {
356   register_valid[regno] = 1;
357   (void) memcpy (&registers[REGISTER_BYTE (regno)], val,
358                  REGISTER_RAW_SIZE (regno));
359 }
360 \f
361 /* Given a struct symbol for a variable,
362    and a stack frame id, read the value of the variable
363    and return a (pointer to a) struct value containing the value. 
364    If the variable cannot be found, return a zero pointer.
365    If FRAME is NULL, use the selected_frame.  */
366
367 value
368 read_var_value (var, frame)
369      register struct symbol *var;
370      FRAME frame;
371 {
372   register value v;
373   struct frame_info *fi;
374   struct type *type = SYMBOL_TYPE (var);
375   CORE_ADDR addr;
376   register int len;
377
378   v = allocate_value (type);
379   VALUE_LVAL (v) = lval_memory; /* The most likely possibility.  */
380   len = TYPE_LENGTH (type);
381
382   if (frame == 0) frame = selected_frame;
383
384   switch (SYMBOL_CLASS (var))
385     {
386     case LOC_CONST:
387       (void) memcpy (VALUE_CONTENTS_RAW (v), &SYMBOL_VALUE (var), len);
388       SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v), len);
389       VALUE_LVAL (v) = not_lval;
390       return v;
391
392     case LOC_LABEL:
393       addr = SYMBOL_VALUE_ADDRESS (var);
394       (void) memcpy (VALUE_CONTENTS_RAW (v), &addr, len);
395       SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (v), len);
396       VALUE_LVAL (v) = not_lval;
397       return v;
398
399     case LOC_CONST_BYTES:
400       {
401         char *bytes_addr;
402         bytes_addr = SYMBOL_VALUE_BYTES (var);
403         (void) memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
404         VALUE_LVAL (v) = not_lval;
405         return v;
406       }
407
408     case LOC_STATIC:
409       addr = SYMBOL_VALUE_ADDRESS (var);
410       break;
411
412     case LOC_ARG:
413       if (SYMBOL_BASEREG_VALID (var))
414         {
415           addr = FRAME_GET_BASEREG_VALUE (frame, SYMBOL_BASEREG (var));
416         }
417       else
418         {
419           fi = get_frame_info (frame);
420           if (fi == NULL)
421             return 0;
422           addr = FRAME_ARGS_ADDRESS (fi);
423         }
424       if (!addr)
425         {
426           return 0;
427         }
428       addr += SYMBOL_VALUE (var);
429       break;
430       
431     case LOC_REF_ARG:
432       if (SYMBOL_BASEREG_VALID (var))
433         {
434           addr = FRAME_GET_BASEREG_VALUE (frame, SYMBOL_BASEREG (var));
435         }
436       else
437         {
438           fi = get_frame_info (frame);
439           if (fi == NULL)
440             return 0;
441           addr = FRAME_ARGS_ADDRESS (fi);
442         }
443       if (!addr)
444         {
445           return 0;
446         }
447       addr += SYMBOL_VALUE (var);
448       read_memory (addr, (char *) &addr, sizeof (CORE_ADDR));
449       break;
450       
451     case LOC_LOCAL:
452     case LOC_LOCAL_ARG:
453       if (SYMBOL_BASEREG_VALID (var))
454         {
455           addr = FRAME_GET_BASEREG_VALUE (frame, SYMBOL_BASEREG (var));
456         }
457       else
458         {
459           fi = get_frame_info (frame);
460           if (fi == NULL)
461             return 0;
462           addr = FRAME_LOCALS_ADDRESS (fi);
463         }
464       addr += SYMBOL_VALUE (var);
465       break;
466
467     case LOC_TYPEDEF:
468       error ("Cannot look up value of a typedef");
469       break;
470
471     case LOC_BLOCK:
472       VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
473       return v;
474
475     case LOC_REGISTER:
476     case LOC_REGPARM:
477       {
478         struct block *b;
479
480         if (frame == NULL)
481           return 0;
482         b = get_frame_block (frame);
483         
484         v = value_from_register (type, SYMBOL_VALUE (var), frame);
485
486         /* Nonzero if a struct which is located in a register or a LOC_ARG
487            really contains
488            the address of the struct, not the struct itself.  GCC_P is nonzero
489            if the function was compiled with GCC.  */
490 #if !defined (REG_STRUCT_HAS_ADDR)
491 #define REG_STRUCT_HAS_ADDR(gcc_p) 0
492 #endif
493
494         if (REG_STRUCT_HAS_ADDR (BLOCK_GCC_COMPILED (b))
495             && (   (TYPE_CODE (type) == TYPE_CODE_STRUCT)
496                 || (TYPE_CODE (type) == TYPE_CODE_UNION)))
497           addr = *(CORE_ADDR *)VALUE_CONTENTS (v);
498         else
499           return v;
500       }
501       break;
502
503     default:
504       error ("Cannot look up value of a botched symbol.");
505       break;
506     }
507
508   VALUE_ADDRESS (v) = addr;
509   VALUE_LAZY (v) = 1;
510   return v;
511 }
512
513 /* Return a value of type TYPE, stored in register REGNUM, in frame
514    FRAME. */
515
516 value
517 value_from_register (type, regnum, frame)
518      struct type *type;
519      int regnum;
520      FRAME frame;
521 {
522   char raw_buffer [MAX_REGISTER_RAW_SIZE];
523   char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
524   CORE_ADDR addr;
525   int optim;
526   value v = allocate_value (type);
527   int len = TYPE_LENGTH (type);
528   char *value_bytes = 0;
529   int value_bytes_copied = 0;
530   int num_storage_locs;
531   enum lval_type lval;
532
533   VALUE_REGNO (v) = regnum;
534
535   num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
536                       ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
537                       1);
538
539   if (num_storage_locs > 1)
540     {
541       /* Value spread across multiple storage locations.  */
542       
543       int local_regnum;
544       int mem_stor = 0, reg_stor = 0;
545       int mem_tracking = 1;
546       CORE_ADDR last_addr = 0;
547       CORE_ADDR first_addr;
548
549       value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
550
551       /* Copy all of the data out, whereever it may be.  */
552
553       for (local_regnum = regnum;
554            value_bytes_copied < len;
555            (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
556             ++local_regnum))
557         {
558           get_saved_register (value_bytes + value_bytes_copied,
559                               &optim,
560                               &addr,
561                               frame,
562                               local_regnum,
563                               &lval);
564           if (lval == lval_register)
565             reg_stor++;
566           else
567             {
568               mem_stor++;
569
570               if (regnum == local_regnum)
571                 first_addr = addr;
572               
573               mem_tracking =
574                 (mem_tracking
575                  && (regnum == local_regnum
576                      || addr == last_addr));
577             }
578           last_addr = addr;
579         }
580
581       if ((reg_stor && mem_stor)
582           || (mem_stor && !mem_tracking))
583         /* Mixed storage; all of the hassle we just went through was
584            for some good purpose.  */
585         {
586           VALUE_LVAL (v) = lval_reg_frame_relative;
587           VALUE_FRAME (v) = FRAME_FP (frame);
588           VALUE_FRAME_REGNUM (v) = regnum;
589         }
590       else if (mem_stor)
591         {
592           VALUE_LVAL (v) = lval_memory;
593           VALUE_ADDRESS (v) = first_addr;
594         }
595       else if (reg_stor)
596         {
597           VALUE_LVAL (v) = lval_register;
598           VALUE_ADDRESS (v) = first_addr;
599         }
600       else
601         fatal ("value_from_register: Value not stored anywhere!");
602
603       VALUE_OPTIMIZED_OUT (v) = optim;
604
605       /* Any structure stored in more than one register will always be
606          an integral number of registers.  Otherwise, you'd need to do
607          some fiddling with the last register copied here for little
608          endian machines.  */
609
610       /* Copy into the contents section of the value.  */
611       (void) memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
612
613       return v;
614     }
615
616   /* Data is completely contained within a single register.  Locate the
617      register's contents in a real register or in core;
618      read the data in raw format.  */
619
620   get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
621   VALUE_OPTIMIZED_OUT (v) = optim;
622   VALUE_LVAL (v) = lval;
623   VALUE_ADDRESS (v) = addr;
624   
625   /* Convert the raw contents to virtual contents.
626      (Just copy them if the formats are the same.)  */
627   
628   target_convert_to_virtual (regnum, raw_buffer, virtual_buffer);
629   
630   if (REGISTER_CONVERTIBLE (regnum))
631     {
632       /* When the raw and virtual formats differ, the virtual format
633          corresponds to a specific data type.  If we want that type,
634          copy the data into the value.
635          Otherwise, do a type-conversion.  */
636       
637       if (type != REGISTER_VIRTUAL_TYPE (regnum))
638         {
639           /* eg a variable of type `float' in a 68881 register
640              with raw type `extended' and virtual type `double'.
641              Fetch it as a `double' and then convert to `float'.  */
642           v = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
643           (void) memcpy (VALUE_CONTENTS_RAW (v), virtual_buffer, len);
644           v = value_cast (type, v);
645         }
646       else
647         (void) memcpy (VALUE_CONTENTS_RAW (v), virtual_buffer, len);
648     }
649   else
650     {
651       /* Raw and virtual formats are the same for this register.  */
652
653 #if TARGET_BYTE_ORDER == BIG_ENDIAN
654       if (len < REGISTER_RAW_SIZE (regnum))
655         {
656           /* Big-endian, and we want less than full size.  */
657           VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
658         }
659 #endif
660
661       (void) memcpy (VALUE_CONTENTS_RAW (v), virtual_buffer + VALUE_OFFSET (v),
662                      len);
663     }
664   
665   return v;
666 }
667 \f
668 /* Given a struct symbol for a variable or function,
669    and a stack frame id, 
670    return a (pointer to a) struct value containing the properly typed
671    address.  */
672
673 value
674 locate_var_value (var, frame)
675      register struct symbol *var;
676      FRAME frame;
677 {
678   CORE_ADDR addr = 0;
679   struct type *type = SYMBOL_TYPE (var);
680   value lazy_value;
681
682   /* Evaluate it first; if the result is a memory address, we're fine.
683      Lazy evaluation pays off here. */
684
685   lazy_value = read_var_value (var, frame);
686   if (lazy_value == 0)
687     error ("Address of \"%s\" is unknown.", SYMBOL_NAME (var));
688
689   if (VALUE_LAZY (lazy_value)
690       || TYPE_CODE (type) == TYPE_CODE_FUNC)
691     {
692       addr = VALUE_ADDRESS (lazy_value);
693       return value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
694     }
695
696   /* Not a memory address; check what the problem was.  */
697   switch (VALUE_LVAL (lazy_value)) 
698     {
699     case lval_register:
700     case lval_reg_frame_relative:
701       error ("Address requested for identifier \"%s\" which is in a register.",
702              SYMBOL_NAME (var));
703       break;
704
705     default:
706       error ("Can't take address of \"%s\" which isn't an lvalue.",
707              SYMBOL_NAME (var));
708       break;
709     }
710   return 0;  /* For lint -- never reached */
711 }
This page took 0.100064 seconds and 4 git commands to generate.