gdb-3.5
[platform/upstream/binutils.git] / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB 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 1, or (at your option)
9 any later version.
10
11 GDB 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 GDB; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "param.h"
22 #include "symtab.h"
23 #include "frame.h"
24 #include "value.h"
25
26 CORE_ADDR read_register ();
27
28 /* Return the address in which frame FRAME's value of register REGNUM
29    has been saved in memory.  Or return zero if it has not been saved.
30    If REGNUM specifies the SP, the value we return is actually
31    the SP value, not an address where it was saved.  */
32
33 CORE_ADDR
34 find_saved_register (frame, regnum)
35      FRAME frame;
36      int regnum;
37 {
38   struct frame_info *fi;
39   struct frame_saved_regs saved_regs;
40
41   register FRAME frame1 = 0;
42   register CORE_ADDR addr = 0;
43
44 #ifdef HAVE_REGISTER_WINDOWS
45   /* We assume that a register in a register window will only be saved
46      in one place (since the name changes and disappears as you go
47      towards inner frames), so we only call get_frame_saved_regs on
48      the current frame.  This is directly in contradiction to the
49      usage below, which assumes that registers used in a frame must be
50      saved in a lower (more interior) frame.  This change is a result
51      of working on a register window machine; get_frame_saved_regs
52      always returns the registers saved within a frame, within the
53      context (register namespace) of that frame. */
54
55   /* However, note that we don't want this to return anything if
56      nothing is saved (if there's a frame inside of this one).  Also,
57      callers to this routine asking for the stack pointer want the
58      stack pointer saved for *this* frame; this is returned from the
59      next frame.  */
60      
61
62   if (REGISTER_IN_WINDOW_P(regnum))
63     {
64       frame1 = get_next_frame (frame);
65       if (!frame1) return 0;    /* Registers of this frame are
66                                    active.  */
67       
68       /* Get the SP from the next frame in; it will be this
69          current frame.  */
70       if (regnum != SP_REGNUM)
71         frame1 = frame; 
72           
73       fi = get_frame_info (frame1);
74       get_frame_saved_regs (fi, &saved_regs);
75       return (saved_regs.regs[regnum] ?
76               saved_regs.regs[regnum] : 0);
77     }
78 #endif /* HAVE_REGISTER_WINDOWS */
79
80   /* Note that this next routine assumes that registers used in
81      frame x will be saved only in the frame that x calls and
82      frames interior to it.  This is not true on the sparc, but the
83      above macro takes care of it, so we should be all right. */
84   while (1)
85     {
86       QUIT;
87       frame1 = get_prev_frame (frame1);
88       if (frame1 == 0 || frame1 == frame)
89         break;
90       fi = get_frame_info (frame1);
91       get_frame_saved_regs (fi, &saved_regs);
92       if (saved_regs.regs[regnum])
93         addr = saved_regs.regs[regnum];
94     }
95
96   return addr;
97 }
98
99 /* Copy the bytes of register REGNUM, relative to the current stack frame,
100    into our memory at MYADDR.
101    The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).  */
102
103 void
104 read_relative_register_raw_bytes (regnum, myaddr)
105      int regnum;
106      char *myaddr;
107 {
108   register CORE_ADDR addr;
109
110   if (regnum == FP_REGNUM)
111     {
112       bcopy (&FRAME_FP(selected_frame), myaddr, sizeof (CORE_ADDR));
113       return;
114     }
115
116   addr = find_saved_register (selected_frame, regnum);
117
118   if (addr)
119     {
120       if (regnum == SP_REGNUM)
121         {
122           CORE_ADDR buffer = addr;
123           bcopy (&buffer, myaddr, sizeof (CORE_ADDR));
124         }
125       else
126         read_memory (addr, myaddr, REGISTER_RAW_SIZE (regnum));
127       return;
128     }
129   read_register_bytes (REGISTER_BYTE (regnum),
130                        myaddr, REGISTER_RAW_SIZE (regnum));
131 }
132
133 /* Return a `value' with the contents of register REGNUM
134    in its virtual format, with the type specified by
135    REGISTER_VIRTUAL_TYPE.  */
136
137 value
138 value_of_register (regnum)
139      int regnum;
140 {
141   register CORE_ADDR addr;
142   register value val;
143   char raw_buffer[MAX_REGISTER_RAW_SIZE];
144   char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
145
146   if (! (have_inferior_p () || have_core_file_p ()))
147     error ("Can't get value of register without inferior or core file");
148   
149   addr =  find_saved_register (selected_frame, regnum);
150   if (addr)
151     {
152       if (regnum == SP_REGNUM)
153         return value_from_long (builtin_type_int, (LONGEST) addr);
154       read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
155     }
156   else
157     read_register_bytes (REGISTER_BYTE (regnum), raw_buffer,
158                          REGISTER_RAW_SIZE (regnum));
159
160   REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
161   val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
162   bcopy (virtual_buffer, VALUE_CONTENTS (val), REGISTER_VIRTUAL_SIZE (regnum));
163   VALUE_LVAL (val) = addr ? lval_memory : lval_register;
164   VALUE_ADDRESS (val) = addr ? addr : REGISTER_BYTE (regnum);
165   VALUE_REGNO (val) = regnum;
166   return val;
167 }
168 \f
169 /* Low level examining and depositing of registers.
170
171    Note that you must call `fetch_registers' once
172    before examining or depositing any registers.  */
173
174 char registers[REGISTER_BYTES];
175
176 /* Copy LEN bytes of consecutive data from registers
177    starting with the REGBYTE'th byte of register data
178    into memory at MYADDR.  */
179
180 void
181 read_register_bytes (regbyte, myaddr, len)
182      int regbyte;
183      char *myaddr;
184      int len;
185 {
186   bcopy (&registers[regbyte], myaddr, len);
187 }
188
189 /* Copy LEN bytes of consecutive data from memory at MYADDR
190    into registers starting with the REGBYTE'th byte of register data.  */
191
192 void
193 write_register_bytes (regbyte, myaddr, len)
194      int regbyte;
195      char *myaddr;
196      int len;
197 {
198   bcopy (myaddr, &registers[regbyte], len);
199   if (have_inferior_p ())
200     store_inferior_registers (-1);
201 }
202
203 /* Return the contents of register REGNO,
204    regarding it as an integer.  */
205
206 CORE_ADDR
207 read_register (regno)
208      int regno;
209 {
210   /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
211   return *(int *) &registers[REGISTER_BYTE (regno)];
212 }
213
214 /* Store VALUE in the register number REGNO, regarded as an integer.  */
215
216 void
217 write_register (regno, val)
218      int regno, val;
219 {
220   /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
221 #if defined(sun4)
222   /* This is a no-op on a Sun 4.  */
223   if (regno == 0)
224     return;
225 #endif
226
227   *(int *) &registers[REGISTER_BYTE (regno)] = val;
228
229   if (have_inferior_p ())
230     store_inferior_registers (regno);
231 }
232
233 /* Record that register REGNO contains VAL.
234    This is used when the value is obtained from the inferior or core dump,
235    so there is no need to store the value there.  */
236
237 void
238 supply_register (regno, val)
239      int regno;
240      char *val;
241 {
242   bcopy (val, &registers[REGISTER_BYTE (regno)], REGISTER_RAW_SIZE (regno));
243 }
244 \f
245 /* Given a struct symbol for a variable,
246    and a stack frame id, read the value of the variable
247    and return a (pointer to a) struct value containing the value.  */
248
249 value
250 read_var_value (var, frame)
251      register struct symbol *var;
252      FRAME frame;
253 {
254   register value v;
255
256   struct frame_info *fi;
257
258   struct type *type = SYMBOL_TYPE (var);
259   register CORE_ADDR addr = 0;
260   int val = SYMBOL_VALUE (var);
261   register int len;
262
263   v = allocate_value (type);
264   VALUE_LVAL (v) = lval_memory; /* The most likely possibility.  */
265   len = TYPE_LENGTH (type);
266
267   if (frame == 0) frame = selected_frame;
268
269   switch (SYMBOL_CLASS (var))
270     {
271     case LOC_CONST:
272     case LOC_LABEL:
273       bcopy (&val, VALUE_CONTENTS (v), len);
274       VALUE_LVAL (v) = not_lval;
275       return v;
276
277     case LOC_CONST_BYTES:
278       bcopy (val, VALUE_CONTENTS (v), len);
279       VALUE_LVAL (v) = not_lval;
280       return v;
281
282     case LOC_STATIC:
283       addr = val;
284       break;
285
286 /* Nonzero if a struct which is located in a register or a LOC_ARG
287    really contains
288    the address of the struct, not the struct itself.  GCC_P is nonzero
289    if the function was compiled with GCC.  */
290 #if !defined (REG_STRUCT_HAS_ADDR)
291 #define REG_STRUCT_HAS_ADDR(gcc_p) 0
292 #endif
293
294     case LOC_ARG:
295       fi = get_frame_info (frame);
296       addr = val + FRAME_ARGS_ADDRESS (fi);
297       break;
298       
299     case LOC_REF_ARG:
300       fi = get_frame_info (frame);
301       addr = val + FRAME_ARGS_ADDRESS (fi);
302       addr = read_memory_integer (addr, sizeof (CORE_ADDR));
303       break;
304       
305     case LOC_LOCAL:
306       fi = get_frame_info (frame);
307       addr = val + FRAME_LOCALS_ADDRESS (fi);
308       break;
309
310     case LOC_TYPEDEF:
311       error ("Cannot look up value of a typedef");
312
313     case LOC_BLOCK:
314       VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
315       return v;
316
317     case LOC_REGISTER:
318     case LOC_REGPARM:
319       {
320         struct block *b = get_frame_block (frame);
321
322         v = value_from_register (type, val, frame);
323
324         if (REG_STRUCT_HAS_ADDR(b->gcc_compile_flag)
325             && TYPE_CODE (type) == TYPE_CODE_STRUCT)
326           addr = *(CORE_ADDR *)VALUE_CONTENTS (v);
327         else
328           return v;
329       }
330     }
331
332   read_memory (addr, VALUE_CONTENTS (v), len);
333   VALUE_ADDRESS (v) = addr;
334   return v;
335 }
336
337 /* Return a value of type TYPE, stored in register REGNUM, in frame
338    FRAME. */
339
340 value
341 value_from_register (type, regnum, frame)
342      struct type *type;
343      int regnum;
344      FRAME frame;
345 {
346   char raw_buffer [MAX_REGISTER_RAW_SIZE];
347   char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
348   CORE_ADDR addr;
349   value v = allocate_value (type);
350   int len = TYPE_LENGTH (type);
351   char *value_bytes = 0;
352   int value_bytes_copied = 0;
353   int num_storage_locs;
354
355   VALUE_REGNO (v) = regnum;
356
357   num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
358                       ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
359                       1);
360
361   if (num_storage_locs > 1)
362     {
363       /* Value spread across multiple storage locations.  */
364       
365       int local_regnum;
366       int mem_stor = 0, reg_stor = 0;
367       int mem_tracking = 1;
368       CORE_ADDR last_addr = 0;
369
370       value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
371
372       /* Copy all of the data out, whereever it may be.  */
373
374       for (local_regnum = regnum;
375            value_bytes_copied < len;
376            (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
377             ++local_regnum))
378         {
379           int register_index = local_regnum - regnum;
380           addr = find_saved_register (frame, local_regnum);
381           if (addr == 0)
382             {
383               read_register_bytes (REGISTER_BYTE (local_regnum),
384                                    value_bytes + value_bytes_copied,
385                                    REGISTER_RAW_SIZE (local_regnum));
386               reg_stor++;
387             }
388           else
389             {
390               read_memory (addr, value_bytes + value_bytes_copied,
391                            REGISTER_RAW_SIZE (local_regnum));
392               mem_stor++;
393               mem_tracking =
394                 (mem_tracking
395                  && (regnum == local_regnum
396                      || addr == last_addr));
397             }
398           last_addr = addr;
399         }
400
401       if ((reg_stor && mem_stor)
402           || (mem_stor && !mem_tracking))
403         /* Mixed storage; all of the hassle we just went through was
404            for some good purpose.  */
405         {
406           VALUE_LVAL (v) = lval_reg_frame_relative;
407           VALUE_FRAME (v) = FRAME_FP (frame);
408           VALUE_FRAME_REGNUM (v) = regnum;
409         }
410       else if (mem_stor)
411         {
412           VALUE_LVAL (v) = lval_memory;
413           VALUE_ADDRESS (v) = find_saved_register (frame, regnum);
414         }
415       else if (reg_stor)
416         {
417           VALUE_LVAL (v) = lval_register;
418           VALUE_ADDRESS (v) = REGISTER_BYTE (regnum);
419         }
420       else
421         fatal ("value_from_register: Value not stored anywhere!");
422       
423       /* Any structure stored in more than one register will always be
424          an inegral number of registers.  Otherwise, you'd need to do
425          some fiddling with the last register copied here for little
426          endian machines.  */
427
428       /* Copy into the contents section of the value.  */
429       bcopy (value_bytes, VALUE_CONTENTS (v), len);
430
431       return v;
432     }
433
434   /* Data is completely contained within a single register.  Locate the
435      register's contents in a real register or in core;
436      read the data in raw format.  */
437   
438   addr = find_saved_register (frame, regnum);
439   if (addr == 0)
440     {
441       /* Value is really in a register.  */
442       
443       VALUE_LVAL (v) = lval_register;
444       VALUE_ADDRESS (v) = REGISTER_BYTE (regnum);
445       
446       read_register_bytes (REGISTER_BYTE (regnum),
447                            raw_buffer, REGISTER_RAW_SIZE (regnum));
448     }
449   else
450     {
451       /* Value was in a register that has been saved in memory.  */
452       
453       read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
454       VALUE_LVAL (v) = lval_memory;
455       VALUE_ADDRESS (v) = addr;
456     }
457   
458   /* Convert the raw contents to virtual contents.
459      (Just copy them if the formats are the same.)  */
460   
461   REGISTER_CONVERT_TO_VIRTUAL (regnum, raw_buffer, virtual_buffer);
462   
463   if (REGISTER_CONVERTIBLE (regnum))
464     {
465       /* When the raw and virtual formats differ, the virtual format
466          corresponds to a specific data type.  If we want that type,
467          copy the data into the value.
468          Otherwise, do a type-conversion.  */
469       
470       if (type != REGISTER_VIRTUAL_TYPE (regnum))
471         {
472           /* eg a variable of type `float' in a 68881 register
473              with raw type `extended' and virtual type `double'.
474              Fetch it as a `double' and then convert to `float'.  */
475           v = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
476           bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
477           v = value_cast (type, v);
478         }
479       else
480         bcopy (virtual_buffer, VALUE_CONTENTS (v), len);
481     }
482   else
483     {
484       /* Raw and virtual formats are the same for this register.  */
485
486 #ifdef BYTES_BIG_ENDIAN
487       if (len < REGISTER_RAW_SIZE (regnum))
488         {
489           /* Big-endian, and we want less than full size.  */
490           VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
491         }
492 #endif
493
494       bcopy (virtual_buffer + VALUE_OFFSET (v),
495              VALUE_CONTENTS (v), len);
496     }
497   
498   return v;
499 }
500 \f
501 /* Given a struct symbol for a variable,
502    and a stack frame id, 
503    return a (pointer to a) struct value containing the variable's address.  */
504
505 value
506 locate_var_value (var, frame)
507      register struct symbol *var;
508      FRAME frame;
509 {
510   register CORE_ADDR addr = 0;
511   int val = SYMBOL_VALUE (var);
512   struct frame_info *fi;
513   struct type *type = SYMBOL_TYPE (var);
514   struct type *result_type;
515
516   if (frame == 0) frame = selected_frame;
517
518   switch (SYMBOL_CLASS (var))
519     {
520     case LOC_CONST:
521     case LOC_CONST_BYTES:
522       error ("Address requested for identifier \"%s\" which is a constant.",
523              SYMBOL_NAME (var));
524
525     case LOC_REGISTER:
526     case LOC_REGPARM:
527       addr = find_saved_register (frame, val);
528       if (addr != 0)
529         {
530           int len = TYPE_LENGTH (type);
531 #ifdef BYTES_BIG_ENDIAN
532           if (len < REGISTER_RAW_SIZE (val))
533             /* Big-endian, and we want less than full size.  */
534             addr += REGISTER_RAW_SIZE (val) - len;
535 #endif
536           break;
537         }
538       error ("Address requested for identifier \"%s\" which is in a register.",
539              SYMBOL_NAME (var));
540
541     case LOC_STATIC:
542     case LOC_LABEL:
543       addr = val;
544       break;
545
546     case LOC_ARG:
547       fi = get_frame_info (frame);
548       addr = val + FRAME_ARGS_ADDRESS (fi);
549       break;
550
551     case LOC_REF_ARG:
552       fi = get_frame_info (frame);
553       addr = val + FRAME_ARGS_ADDRESS (fi);
554       addr = read_memory_integer (addr, sizeof (CORE_ADDR));
555       break;
556
557     case LOC_LOCAL:
558       fi = get_frame_info (frame);
559       addr = val + FRAME_LOCALS_ADDRESS (fi);
560       break;
561
562     case LOC_TYPEDEF:
563       error ("Address requested for identifier \"%s\" which is a typedef.",
564              SYMBOL_NAME (var));
565
566     case LOC_BLOCK:
567       addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
568       break;
569     }
570
571   /* Address of an array is of the type of address of it's elements.  */
572   result_type =
573     lookup_pointer_type (TYPE_CODE (type) == TYPE_CODE_ARRAY ?
574                          TYPE_TARGET_TYPE (type) : type);
575
576   return value_cast (result_type,
577                      value_from_long (builtin_type_long, (LONGEST) addr));
578 }
579