2007-06-09 Markus Deuling <deuling@de.ibm.com>
[external/binutils.git] / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4    1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2007
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor,
22    Boston, MA 02110-1301, USA.  */
23
24 #include "defs.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "frame.h"
28 #include "value.h"
29 #include "gdbcore.h"
30 #include "inferior.h"
31 #include "target.h"
32 #include "gdb_string.h"
33 #include "gdb_assert.h"
34 #include "floatformat.h"
35 #include "symfile.h"            /* for overlay functions */
36 #include "regcache.h"
37 #include "user-regs.h"
38 #include "block.h"
39
40 /* Basic byte-swapping routines.  GDB has needed these for a long time...
41    All extract a target-format integer at ADDR which is LEN bytes long.  */
42
43 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
44   /* 8 bit characters are a pretty safe assumption these days, so we
45      assume it throughout all these swapping routines.  If we had to deal with
46      9 bit characters, we would need to make len be in bits and would have
47      to re-write these routines...  */
48 you lose
49 #endif
50
51 LONGEST
52 extract_signed_integer (const gdb_byte *addr, int len)
53 {
54   LONGEST retval;
55   const unsigned char *p;
56   const unsigned char *startaddr = addr;
57   const unsigned char *endaddr = startaddr + len;
58
59   if (len > (int) sizeof (LONGEST))
60     error (_("\
61 That operation is not available on integers of more than %d bytes."),
62            (int) sizeof (LONGEST));
63
64   /* Start at the most significant end of the integer, and work towards
65      the least significant.  */
66   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
67     {
68       p = startaddr;
69       /* Do the sign extension once at the start.  */
70       retval = ((LONGEST) * p ^ 0x80) - 0x80;
71       for (++p; p < endaddr; ++p)
72         retval = (retval << 8) | *p;
73     }
74   else
75     {
76       p = endaddr - 1;
77       /* Do the sign extension once at the start.  */
78       retval = ((LONGEST) * p ^ 0x80) - 0x80;
79       for (--p; p >= startaddr; --p)
80         retval = (retval << 8) | *p;
81     }
82   return retval;
83 }
84
85 ULONGEST
86 extract_unsigned_integer (const gdb_byte *addr, int len)
87 {
88   ULONGEST retval;
89   const unsigned char *p;
90   const unsigned char *startaddr = addr;
91   const unsigned char *endaddr = startaddr + len;
92
93   if (len > (int) sizeof (ULONGEST))
94     error (_("\
95 That operation is not available on integers of more than %d bytes."),
96            (int) sizeof (ULONGEST));
97
98   /* Start at the most significant end of the integer, and work towards
99      the least significant.  */
100   retval = 0;
101   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
102     {
103       for (p = startaddr; p < endaddr; ++p)
104         retval = (retval << 8) | *p;
105     }
106   else
107     {
108       for (p = endaddr - 1; p >= startaddr; --p)
109         retval = (retval << 8) | *p;
110     }
111   return retval;
112 }
113
114 /* Sometimes a long long unsigned integer can be extracted as a
115    LONGEST value.  This is done so that we can print these values
116    better.  If this integer can be converted to a LONGEST, this
117    function returns 1 and sets *PVAL.  Otherwise it returns 0.  */
118
119 int
120 extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
121                                LONGEST *pval)
122 {
123   const gdb_byte *p;
124   const gdb_byte *first_addr;
125   int len;
126
127   len = orig_len;
128   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
129     {
130       for (p = addr;
131            len > (int) sizeof (LONGEST) && p < addr + orig_len;
132            p++)
133         {
134           if (*p == 0)
135             len--;
136           else
137             break;
138         }
139       first_addr = p;
140     }
141   else
142     {
143       first_addr = addr;
144       for (p = addr + orig_len - 1;
145            len > (int) sizeof (LONGEST) && p >= addr;
146            p--)
147         {
148           if (*p == 0)
149             len--;
150           else
151             break;
152         }
153     }
154
155   if (len <= (int) sizeof (LONGEST))
156     {
157       *pval = (LONGEST) extract_unsigned_integer (first_addr,
158                                                   sizeof (LONGEST));
159       return 1;
160     }
161
162   return 0;
163 }
164
165
166 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
167    address it represents.  */
168 CORE_ADDR
169 extract_typed_address (const gdb_byte *buf, struct type *type)
170 {
171   if (TYPE_CODE (type) != TYPE_CODE_PTR
172       && TYPE_CODE (type) != TYPE_CODE_REF)
173     internal_error (__FILE__, __LINE__,
174                     _("extract_typed_address: "
175                     "type is not a pointer or reference"));
176
177   return gdbarch_pointer_to_address (current_gdbarch, type, buf);
178 }
179
180
181 void
182 store_signed_integer (gdb_byte *addr, int len, LONGEST val)
183 {
184   gdb_byte *p;
185   gdb_byte *startaddr = addr;
186   gdb_byte *endaddr = startaddr + len;
187
188   /* Start at the least significant end of the integer, and work towards
189      the most significant.  */
190   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
191     {
192       for (p = endaddr - 1; p >= startaddr; --p)
193         {
194           *p = val & 0xff;
195           val >>= 8;
196         }
197     }
198   else
199     {
200       for (p = startaddr; p < endaddr; ++p)
201         {
202           *p = val & 0xff;
203           val >>= 8;
204         }
205     }
206 }
207
208 void
209 store_unsigned_integer (gdb_byte *addr, int len, ULONGEST val)
210 {
211   unsigned char *p;
212   unsigned char *startaddr = (unsigned char *) addr;
213   unsigned char *endaddr = startaddr + len;
214
215   /* Start at the least significant end of the integer, and work towards
216      the most significant.  */
217   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
218     {
219       for (p = endaddr - 1; p >= startaddr; --p)
220         {
221           *p = val & 0xff;
222           val >>= 8;
223         }
224     }
225   else
226     {
227       for (p = startaddr; p < endaddr; ++p)
228         {
229           *p = val & 0xff;
230           val >>= 8;
231         }
232     }
233 }
234
235 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
236    form.  */
237 void
238 store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
239 {
240   if (TYPE_CODE (type) != TYPE_CODE_PTR
241       && TYPE_CODE (type) != TYPE_CODE_REF)
242     internal_error (__FILE__, __LINE__,
243                     _("store_typed_address: "
244                     "type is not a pointer or reference"));
245
246   gdbarch_address_to_pointer (current_gdbarch, type, buf, addr);
247 }
248
249
250
251 /* Return a `value' with the contents of (virtual or cooked) register
252    REGNUM as found in the specified FRAME.  The register's type is
253    determined by register_type().  */
254
255 struct value *
256 value_of_register (int regnum, struct frame_info *frame)
257 {
258   CORE_ADDR addr;
259   int optim;
260   struct value *reg_val;
261   int realnum;
262   gdb_byte raw_buffer[MAX_REGISTER_SIZE];
263   enum lval_type lval;
264
265   /* User registers lie completely outside of the range of normal
266      registers.  Catch them early so that the target never sees them.  */
267   if (regnum >= gdbarch_num_regs (current_gdbarch)
268                 + gdbarch_num_pseudo_regs (current_gdbarch))
269     return value_of_user_reg (regnum, frame);
270
271   frame_register (frame, regnum, &optim, &lval, &addr, &realnum, raw_buffer);
272
273   reg_val = allocate_value (register_type (current_gdbarch, regnum));
274
275   memcpy (value_contents_raw (reg_val), raw_buffer,
276           register_size (current_gdbarch, regnum));
277   VALUE_LVAL (reg_val) = lval;
278   VALUE_ADDRESS (reg_val) = addr;
279   VALUE_REGNUM (reg_val) = regnum;
280   set_value_optimized_out (reg_val, optim);
281   VALUE_FRAME_ID (reg_val) = get_frame_id (frame);
282   return reg_val;
283 }
284
285 /* Given a pointer of type TYPE in target form in BUF, return the
286    address it represents.  */
287 CORE_ADDR
288 unsigned_pointer_to_address (struct type *type, const gdb_byte *buf)
289 {
290   return extract_unsigned_integer (buf, TYPE_LENGTH (type));
291 }
292
293 CORE_ADDR
294 signed_pointer_to_address (struct type *type, const gdb_byte *buf)
295 {
296   return extract_signed_integer (buf, TYPE_LENGTH (type));
297 }
298
299 /* Given an address, store it as a pointer of type TYPE in target
300    format in BUF.  */
301 void
302 unsigned_address_to_pointer (struct type *type, gdb_byte *buf,
303                              CORE_ADDR addr)
304 {
305   store_unsigned_integer (buf, TYPE_LENGTH (type), addr);
306 }
307
308 void
309 address_to_signed_pointer (struct type *type, gdb_byte *buf, CORE_ADDR addr)
310 {
311   store_signed_integer (buf, TYPE_LENGTH (type), addr);
312 }
313 \f
314 /* Will calling read_var_value or locate_var_value on SYM end
315    up caring what frame it is being evaluated relative to?  SYM must
316    be non-NULL.  */
317 int
318 symbol_read_needs_frame (struct symbol *sym)
319 {
320   switch (SYMBOL_CLASS (sym))
321     {
322       /* All cases listed explicitly so that gcc -Wall will detect it if
323          we failed to consider one.  */
324     case LOC_COMPUTED:
325     case LOC_COMPUTED_ARG:
326       /* FIXME: cagney/2004-01-26: It should be possible to
327          unconditionally call the SYMBOL_OPS method when available.
328          Unfortunately DWARF 2 stores the frame-base (instead of the
329          function) location in a function's symbol.  Oops!  For the
330          moment enable this when/where applicable.  */
331       return SYMBOL_OPS (sym)->read_needs_frame (sym);
332
333     case LOC_REGISTER:
334     case LOC_ARG:
335     case LOC_REF_ARG:
336     case LOC_REGPARM:
337     case LOC_REGPARM_ADDR:
338     case LOC_LOCAL:
339     case LOC_LOCAL_ARG:
340     case LOC_BASEREG:
341     case LOC_BASEREG_ARG:
342     case LOC_HP_THREAD_LOCAL_STATIC:
343       return 1;
344
345     case LOC_UNDEF:
346     case LOC_CONST:
347     case LOC_STATIC:
348     case LOC_INDIRECT:
349     case LOC_TYPEDEF:
350
351     case LOC_LABEL:
352       /* Getting the address of a label can be done independently of the block,
353          even if some *uses* of that address wouldn't work so well without
354          the right frame.  */
355
356     case LOC_BLOCK:
357     case LOC_CONST_BYTES:
358     case LOC_UNRESOLVED:
359     case LOC_OPTIMIZED_OUT:
360       return 0;
361     }
362   return 1;
363 }
364
365 /* Given a struct symbol for a variable,
366    and a stack frame id, read the value of the variable
367    and return a (pointer to a) struct value containing the value. 
368    If the variable cannot be found, return a zero pointer.
369    If FRAME is NULL, use the selected frame.  */
370
371 struct value *
372 read_var_value (struct symbol *var, struct frame_info *frame)
373 {
374   struct value *v;
375   struct type *type = SYMBOL_TYPE (var);
376   CORE_ADDR addr;
377   int len;
378
379   if (SYMBOL_CLASS (var) == LOC_COMPUTED
380       || SYMBOL_CLASS (var) == LOC_COMPUTED_ARG
381       || SYMBOL_CLASS (var) == LOC_REGISTER
382       || SYMBOL_CLASS (var) == LOC_REGPARM)
383     /* These cases do not use V.  */
384     v = NULL;
385   else
386     {
387       v = allocate_value (type);
388       VALUE_LVAL (v) = lval_memory;     /* The most likely possibility.  */
389     }
390
391   len = TYPE_LENGTH (type);
392
393   /* FIXME drow/2003-09-06: this call to the selected frame should be
394      pushed upwards to the callers.  */
395   if (frame == NULL)
396     frame = deprecated_safe_get_selected_frame ();
397
398   switch (SYMBOL_CLASS (var))
399     {
400     case LOC_CONST:
401       /* Put the constant back in target format.  */
402       store_signed_integer (value_contents_raw (v), len,
403                             (LONGEST) SYMBOL_VALUE (var));
404       VALUE_LVAL (v) = not_lval;
405       return v;
406
407     case LOC_LABEL:
408       /* Put the constant back in target format.  */
409       if (overlay_debugging)
410         {
411           CORE_ADDR addr
412             = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
413                                         SYMBOL_BFD_SECTION (var));
414           store_typed_address (value_contents_raw (v), type, addr);
415         }
416       else
417         store_typed_address (value_contents_raw (v), type,
418                               SYMBOL_VALUE_ADDRESS (var));
419       VALUE_LVAL (v) = not_lval;
420       return v;
421
422     case LOC_CONST_BYTES:
423       {
424         memcpy (value_contents_raw (v), SYMBOL_VALUE_BYTES (var), len);
425         VALUE_LVAL (v) = not_lval;
426         return v;
427       }
428
429     case LOC_STATIC:
430       if (overlay_debugging)
431         addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
432                                          SYMBOL_BFD_SECTION (var));
433       else
434         addr = SYMBOL_VALUE_ADDRESS (var);
435       break;
436
437     case LOC_INDIRECT:
438       {
439         /* The import slot does not have a real address in it from the
440            dynamic loader (dld.sl on HP-UX), if the target hasn't
441            begun execution yet, so check for that. */
442         CORE_ADDR locaddr;
443         struct value *loc;
444         if (!target_has_execution)
445           error (_("\
446 Attempt to access variable defined in different shared object or load module when\n\
447 addresses have not been bound by the dynamic loader. Try again when executable is running."));
448
449         locaddr = SYMBOL_VALUE_ADDRESS (var);
450         loc = value_at (lookup_pointer_type (type), locaddr);
451         addr = value_as_address (loc);
452         break;
453       }
454
455     case LOC_ARG:
456       if (frame == NULL)
457         return 0;
458       addr = get_frame_args_address (frame);
459       if (!addr)
460         return 0;
461       addr += SYMBOL_VALUE (var);
462       break;
463
464     case LOC_REF_ARG:
465       {
466         struct value *ref;
467         CORE_ADDR argref;
468         if (frame == NULL)
469           return 0;
470         argref = get_frame_args_address (frame);
471         if (!argref)
472           return 0;
473         argref += SYMBOL_VALUE (var);
474         ref = value_at (lookup_pointer_type (type), argref);
475         addr = value_as_address (ref);
476         break;
477       }
478
479     case LOC_LOCAL:
480     case LOC_LOCAL_ARG:
481       if (frame == NULL)
482         return 0;
483       addr = get_frame_locals_address (frame);
484       addr += SYMBOL_VALUE (var);
485       break;
486
487     case LOC_BASEREG:
488     case LOC_BASEREG_ARG:
489     case LOC_HP_THREAD_LOCAL_STATIC:
490       {
491         struct value *regval;
492
493         regval = value_from_register (lookup_pointer_type (type),
494                                       SYMBOL_BASEREG (var), frame);
495         if (regval == NULL)
496           error (_("Value of base register not available."));
497         addr = value_as_address (regval);
498         addr += SYMBOL_VALUE (var);
499         break;
500       }
501
502     case LOC_TYPEDEF:
503       error (_("Cannot look up value of a typedef"));
504       break;
505
506     case LOC_BLOCK:
507       if (overlay_debugging)
508         VALUE_ADDRESS (v) = symbol_overlayed_address
509           (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var));
510       else
511         VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
512       return v;
513
514     case LOC_REGISTER:
515     case LOC_REGPARM:
516     case LOC_REGPARM_ADDR:
517       {
518         struct block *b;
519         int regno = SYMBOL_VALUE (var);
520         struct value *regval;
521
522         if (frame == NULL)
523           return 0;
524         b = get_frame_block (frame, 0);
525
526         if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
527           {
528             regval = value_from_register (lookup_pointer_type (type),
529                                           regno,
530                                           frame);
531
532             if (regval == NULL)
533               error (_("Value of register variable not available."));
534
535             addr = value_as_address (regval);
536             VALUE_LVAL (v) = lval_memory;
537           }
538         else
539           {
540             regval = value_from_register (type, regno, frame);
541
542             if (regval == NULL)
543               error (_("Value of register variable not available."));
544             return regval;
545           }
546       }
547       break;
548
549     case LOC_COMPUTED:
550     case LOC_COMPUTED_ARG:
551       /* FIXME: cagney/2004-01-26: It should be possible to
552          unconditionally call the SYMBOL_OPS method when available.
553          Unfortunately DWARF 2 stores the frame-base (instead of the
554          function) location in a function's symbol.  Oops!  For the
555          moment enable this when/where applicable.  */
556       if (frame == 0 && SYMBOL_OPS (var)->read_needs_frame (var))
557         return 0;
558       return SYMBOL_OPS (var)->read_variable (var, frame);
559
560     case LOC_UNRESOLVED:
561       {
562         struct minimal_symbol *msym;
563
564         msym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (var), NULL, NULL);
565         if (msym == NULL)
566           return 0;
567         if (overlay_debugging)
568           addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
569                                            SYMBOL_BFD_SECTION (msym));
570         else
571           addr = SYMBOL_VALUE_ADDRESS (msym);
572       }
573       break;
574
575     case LOC_OPTIMIZED_OUT:
576       VALUE_LVAL (v) = not_lval;
577       set_value_optimized_out (v, 1);
578       return v;
579
580     default:
581       error (_("Cannot look up value of a botched symbol."));
582       break;
583     }
584
585   VALUE_ADDRESS (v) = addr;
586   set_value_lazy (v, 1);
587   return v;
588 }
589
590 /* Install default attributes for register values.  */
591
592 struct value *
593 default_value_from_register (struct type *type, int regnum,
594                              struct frame_info *frame)
595 {
596   struct gdbarch *gdbarch = get_frame_arch (frame);
597   int len = TYPE_LENGTH (type);
598   struct value *value = allocate_value (type);
599
600   VALUE_LVAL (value) = lval_register;
601   VALUE_FRAME_ID (value) = get_frame_id (frame);
602   VALUE_REGNUM (value) = regnum;
603
604   /* Any structure stored in more than one register will always be
605      an integral number of registers.  Otherwise, you need to do
606      some fiddling with the last register copied here for little
607      endian machines.  */
608   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG
609       && len < register_size (gdbarch, regnum))
610     /* Big-endian, and we want less than full size.  */
611     set_value_offset (value, register_size (gdbarch, regnum) - len);
612   else
613     set_value_offset (value, 0);
614
615   return value;
616 }
617
618 /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME.  */
619
620 struct value *
621 value_from_register (struct type *type, int regnum, struct frame_info *frame)
622 {
623   struct gdbarch *gdbarch = get_frame_arch (frame);
624   struct type *type1 = check_typedef (type);
625   struct value *v;
626
627   if (CONVERT_REGISTER_P (regnum, type1))
628     {
629       /* The ISA/ABI need to something weird when obtaining the
630          specified value from this register.  It might need to
631          re-order non-adjacent, starting with REGNUM (see MIPS and
632          i386).  It might need to convert the [float] register into
633          the corresponding [integer] type (see Alpha).  The assumption
634          is that REGISTER_TO_VALUE populates the entire value
635          including the location.  */
636       v = allocate_value (type);
637       VALUE_LVAL (v) = lval_register;
638       VALUE_FRAME_ID (v) = get_frame_id (frame);
639       VALUE_REGNUM (v) = regnum;
640       REGISTER_TO_VALUE (frame, regnum, type1, value_contents_raw (v));
641     }
642   else
643     {
644       int len = TYPE_LENGTH (type);
645
646       /* Construct the value.  */
647       v = gdbarch_value_from_register (gdbarch, type, regnum, frame);
648
649       /* Get the data.  */
650       if (!get_frame_register_bytes (frame, regnum, value_offset (v), len,
651                                      value_contents_raw (v)))
652         set_value_optimized_out (v, 1);
653     }
654   return v;
655 }
656
657 /* Return contents of register REGNUM in frame FRAME as address,
658    interpreted as value of type TYPE.   Will abort if register
659    value is not available.  */
660
661 CORE_ADDR
662 address_from_register (struct type *type, int regnum, struct frame_info *frame)
663 {
664   struct value *value;
665   CORE_ADDR result;
666
667   value = value_from_register (type, regnum, frame);
668   gdb_assert (value);
669
670   result = value_as_address (value);
671   release_value (value);
672   value_free (value);
673
674   return result;
675 }
676
677 \f
678 /* Given a struct symbol for a variable or function,
679    and a stack frame id, 
680    return a (pointer to a) struct value containing the properly typed
681    address.  */
682
683 struct value *
684 locate_var_value (struct symbol *var, struct frame_info *frame)
685 {
686   CORE_ADDR addr = 0;
687   struct type *type = SYMBOL_TYPE (var);
688   struct value *lazy_value;
689
690   /* Evaluate it first; if the result is a memory address, we're fine.
691      Lazy evaluation pays off here. */
692
693   lazy_value = read_var_value (var, frame);
694   if (lazy_value == 0)
695     error (_("Address of \"%s\" is unknown."), SYMBOL_PRINT_NAME (var));
696
697   if (value_lazy (lazy_value)
698       || TYPE_CODE (type) == TYPE_CODE_FUNC)
699     {
700       struct value *val;
701
702       addr = VALUE_ADDRESS (lazy_value);
703       val = value_from_pointer (lookup_pointer_type (type), addr);
704       return val;
705     }
706
707   /* Not a memory address; check what the problem was.  */
708   switch (VALUE_LVAL (lazy_value))
709     {
710     case lval_register:
711       gdb_assert (gdbarch_register_name
712                    (current_gdbarch, VALUE_REGNUM (lazy_value)) != NULL
713                   && *gdbarch_register_name
714                     (current_gdbarch, VALUE_REGNUM (lazy_value)) != '\0');
715       error (_("Address requested for identifier "
716                "\"%s\" which is in register $%s"),
717             SYMBOL_PRINT_NAME (var), 
718             gdbarch_register_name (current_gdbarch, VALUE_REGNUM (lazy_value)));
719       break;
720
721     default:
722       error (_("Can't take address of \"%s\" which isn't an lvalue."),
723              SYMBOL_PRINT_NAME (var));
724       break;
725     }
726   return 0;                     /* For lint -- never reached */
727 }