OSDN Git Service

Based on a patch from Daniel Berlin (dberlin@dberlin.org).
[pf3gnuchains/pf3gnuchains3x.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2    Copyright 2003 Free Software Foundation, Inc.
3    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "ui-out.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "inferior.h"
29
30 #include "elf/dwarf2.h"
31 #include "dwarf2expr.h"
32 #include "dwarf2loc.h"
33
34 #include "gdb_string.h"
35
36 #ifndef DWARF2_REG_TO_REGNUM
37 #define DWARF2_REG_TO_REGNUM(REG) (REG)
38 #endif
39
40 /* This is the baton used when performing dwarf2 expression
41    evaluation.  */
42 struct dwarf_expr_baton
43 {
44   struct frame_info *frame;
45   struct objfile *objfile;
46 };
47
48 /* Helper functions for dwarf2_evaluate_loc_desc.  */
49
50 /* Using the frame specified in BATON, read register REGNUM.  The lval
51    type will be returned in LVALP, and for lval_memory the register
52    save address will be returned in ADDRP.  */
53 static CORE_ADDR
54 dwarf_expr_read_reg (void *baton, int regnum, enum lval_type *lvalp,
55                      CORE_ADDR *addrp)
56 {
57   CORE_ADDR result;
58   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
59   char *buf = (char *) alloca (MAX_REGISTER_RAW_SIZE);
60   int optimized, realnum;
61
62   frame_register (debaton->frame, DWARF2_REG_TO_REGNUM (regnum),
63                   &optimized, lvalp, addrp, &realnum, buf);
64   result = extract_address (buf, REGISTER_RAW_SIZE (regnum));
65
66   return result;
67 }
68
69 /* Read memory at ADDR (length LEN) into BUF.  */
70
71 static void
72 dwarf_expr_read_mem (void *baton, char *buf, CORE_ADDR addr, size_t len)
73 {
74   read_memory (addr, buf, len);
75 }
76
77 /* Using the frame specified in BATON, find the location expression
78    describing the frame base.  Return a pointer to it in START and
79    its length in LENGTH.  */
80 static void
81 dwarf_expr_frame_base (void *baton, unsigned char **start, size_t * length)
82 {
83   struct symbol *framefunc;
84   struct dwarf2_locexpr_baton *symbaton;
85   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
86   framefunc = get_frame_function (debaton->frame);
87   symbaton = SYMBOL_LOCATION_BATON (framefunc);
88   *start = symbaton->data;
89   *length = symbaton->size;
90 }
91
92 /* Using the objfile specified in BATON, find the address for the
93    current thread's thread-local storage with offset OFFSET.  */
94 static CORE_ADDR
95 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
96 {
97   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
98   CORE_ADDR addr;
99
100   if (target_get_thread_local_address_p ())
101     addr = target_get_thread_local_address (inferior_ptid,
102                                             debaton->objfile,
103                                             offset);
104   else
105     error ("Cannot find thread-local variables on this target");
106
107   return addr;
108 }
109
110 /* Evaluate a location description, starting at DATA and with length
111    SIZE, to find the current location of variable VAR in the context
112    of FRAME.  */
113 static struct value *
114 dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
115                           unsigned char *data, unsigned short size,
116                           struct objfile *objfile)
117 {
118   CORE_ADDR result;
119   struct value *retval;
120   struct dwarf_expr_baton baton;
121   struct dwarf_expr_context *ctx;
122
123   baton.frame = frame;
124   baton.objfile = objfile;
125
126   ctx = new_dwarf_expr_context ();
127   ctx->baton = &baton;
128   ctx->read_reg = dwarf_expr_read_reg;
129   ctx->read_mem = dwarf_expr_read_mem;
130   ctx->get_frame_base = dwarf_expr_frame_base;
131   ctx->get_tls_address = dwarf_expr_tls_address;
132
133   dwarf_expr_eval (ctx, data, size);
134
135   retval = allocate_value (SYMBOL_TYPE (var));
136   VALUE_BFD_SECTION (retval) = SYMBOL_BFD_SECTION (var);
137
138   if (ctx->in_reg)
139     {
140       store_unsigned_integer (VALUE_CONTENTS_RAW (retval),
141                               TYPE_LENGTH (SYMBOL_TYPE (var)),
142                               dwarf_expr_fetch (ctx, 0));
143       VALUE_LVAL (retval) = lval_register;
144       VALUE_REGNO (retval) = ctx->regnum;
145     }
146   else
147     {
148       result = dwarf_expr_fetch (ctx, 0);
149       VALUE_LVAL (retval) = lval_memory;
150       VALUE_LAZY (retval) = 1;
151       VALUE_ADDRESS (retval) = result;
152     }
153
154   free_dwarf_expr_context (ctx);
155
156   return retval;
157 }
158
159
160
161
162 \f
163 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
164
165 struct needs_frame_baton
166 {
167   int needs_frame;
168 };
169
170 /* Reads from registers do require a frame.  */
171 static CORE_ADDR
172 needs_frame_read_reg (void *baton, int regnum, enum lval_type *lvalp,
173                             CORE_ADDR *addrp)
174 {
175   struct needs_frame_baton *nf_baton = baton;
176   nf_baton->needs_frame = 1;
177   return 1;
178 }
179
180 /* Reads from memory do not require a frame.  */
181 static void
182 needs_frame_read_mem (void *baton, char *buf, CORE_ADDR addr, size_t len)
183 {
184   memset (buf, 0, len);
185 }
186
187 /* Frame-relative accesses do require a frame.  */
188 static void
189 needs_frame_frame_base (void *baton, unsigned char **start, size_t * length)
190 {
191   static char lit0 = DW_OP_lit0;
192   struct needs_frame_baton *nf_baton = baton;
193
194   *start = &lit0;
195   *length = 1;
196
197   nf_baton->needs_frame = 1;
198 }
199
200 /* Thread-local accesses do require a frame.  */
201 static CORE_ADDR
202 needs_frame_tls_address (void *baton, CORE_ADDR offset)
203 {
204   struct needs_frame_baton *nf_baton = baton;
205   nf_baton->needs_frame = 1;
206   return 1;
207 }
208
209 /* Return non-zero iff the location expression at DATA (length SIZE)
210    requires a frame to evaluate.  */
211
212 static int
213 dwarf2_loc_desc_needs_frame (unsigned char *data, unsigned short size)
214 {
215   struct needs_frame_baton baton;
216   struct dwarf_expr_context *ctx;
217
218   baton.needs_frame = 0;
219
220   ctx = new_dwarf_expr_context ();
221   ctx->baton = &baton;
222   ctx->read_reg = needs_frame_read_reg;
223   ctx->read_mem = needs_frame_read_mem;
224   ctx->get_frame_base = needs_frame_frame_base;
225   ctx->get_tls_address = needs_frame_tls_address;
226
227   dwarf_expr_eval (ctx, data, size);
228
229   free_dwarf_expr_context (ctx);
230
231   return baton.needs_frame;
232 }
233
234
235
236 \f
237 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
238    evaluator to calculate the location.  */
239 static struct value *
240 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
241 {
242   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
243   struct value *val;
244   val = dwarf2_evaluate_loc_desc (symbol, frame, dlbaton->data, dlbaton->size,
245                                   dlbaton->objfile);
246
247   return val;
248 }
249
250 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
251 static int
252 locexpr_read_needs_frame (struct symbol *symbol)
253 {
254   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
255   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size);
256 }
257
258 /* Print a natural-language description of SYMBOL to STREAM.  */
259 static int
260 locexpr_describe_location (struct symbol *symbol, struct ui_file *stream)
261 {
262   /* FIXME: be more extensive.  */
263   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
264
265   if (dlbaton->size == 1
266       && dlbaton->data[0] >= DW_OP_reg0
267       && dlbaton->data[0] <= DW_OP_reg31)
268     {
269       int regno = DWARF2_REG_TO_REGNUM (dlbaton->data[0] - DW_OP_reg0);
270       fprintf_filtered (stream,
271                         "a variable in register %s", REGISTER_NAME (regno));
272       return 1;
273     }
274
275   fprintf_filtered (stream,
276                     "a variable with complex or multiple locations (DWARF2)");
277   return 1;
278 }
279
280 /* The set of location functions used with the DWARF-2 expression
281    evaluator.  */
282 struct location_funcs dwarf2_locexpr_funcs = {
283   locexpr_read_variable,
284   locexpr_read_needs_frame,
285   locexpr_describe_location,
286   NULL
287 };