1 /* DWARF 2 location expression support for GDB.
2 Copyright 2003 Free Software Foundation, Inc.
3 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
5 This file is part of GDB.
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.
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.
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. */
30 #include "elf/dwarf2.h"
31 #include "dwarf2expr.h"
32 #include "dwarf2loc.h"
34 #include "gdb_string.h"
36 #ifndef DWARF2_REG_TO_REGNUM
37 #define DWARF2_REG_TO_REGNUM(REG) (REG)
40 /* This is the baton used when performing dwarf2 expression
42 struct dwarf_expr_baton
44 struct frame_info
*frame
;
45 struct objfile
*objfile
;
48 /* Helper functions for dwarf2_evaluate_loc_desc. */
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. */
54 dwarf_expr_read_reg (void *baton
, int regnum
, enum lval_type
*lvalp
,
58 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
59 char *buf
= (char *) alloca (MAX_REGISTER_RAW_SIZE
);
60 int optimized
, realnum
;
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
));
69 /* Read memory at ADDR (length LEN) into BUF. */
72 dwarf_expr_read_mem (void *baton
, char *buf
, CORE_ADDR addr
, size_t len
)
74 read_memory (addr
, buf
, len
);
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. */
81 dwarf_expr_frame_base (void *baton
, unsigned char **start
, size_t * length
)
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
;
92 /* Using the objfile specified in BATON, find the address for the
93 current thread's thread-local storage with offset OFFSET. */
95 dwarf_expr_tls_address (void *baton
, CORE_ADDR offset
)
97 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
100 if (target_get_thread_local_address_p ())
101 addr
= target_get_thread_local_address (inferior_ptid
,
105 error ("Cannot find thread-local variables on this target");
110 /* Evaluate a location description, starting at DATA and with length
111 SIZE, to find the current location of variable VAR in the context
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
)
119 struct value
*retval
;
120 struct dwarf_expr_baton baton
;
121 struct dwarf_expr_context
*ctx
;
124 baton
.objfile
= objfile
;
126 ctx
= new_dwarf_expr_context ();
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
;
133 dwarf_expr_eval (ctx
, data
, size
);
135 retval
= allocate_value (SYMBOL_TYPE (var
));
136 VALUE_BFD_SECTION (retval
) = SYMBOL_BFD_SECTION (var
);
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
;
148 result
= dwarf_expr_fetch (ctx
, 0);
149 VALUE_LVAL (retval
) = lval_memory
;
150 VALUE_LAZY (retval
) = 1;
151 VALUE_ADDRESS (retval
) = result
;
154 free_dwarf_expr_context (ctx
);
163 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
165 struct needs_frame_baton
170 /* Reads from registers do require a frame. */
172 needs_frame_read_reg (void *baton
, int regnum
, enum lval_type
*lvalp
,
175 struct needs_frame_baton
*nf_baton
= baton
;
176 nf_baton
->needs_frame
= 1;
180 /* Reads from memory do not require a frame. */
182 needs_frame_read_mem (void *baton
, char *buf
, CORE_ADDR addr
, size_t len
)
184 memset (buf
, 0, len
);
187 /* Frame-relative accesses do require a frame. */
189 needs_frame_frame_base (void *baton
, unsigned char **start
, size_t * length
)
191 static char lit0
= DW_OP_lit0
;
192 struct needs_frame_baton
*nf_baton
= baton
;
197 nf_baton
->needs_frame
= 1;
200 /* Thread-local accesses do require a frame. */
202 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
204 struct needs_frame_baton
*nf_baton
= baton
;
205 nf_baton
->needs_frame
= 1;
209 /* Return non-zero iff the location expression at DATA (length SIZE)
210 requires a frame to evaluate. */
213 dwarf2_loc_desc_needs_frame (unsigned char *data
, unsigned short size
)
215 struct needs_frame_baton baton
;
216 struct dwarf_expr_context
*ctx
;
218 baton
.needs_frame
= 0;
220 ctx
= new_dwarf_expr_context ();
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
;
227 dwarf_expr_eval (ctx
, data
, size
);
229 free_dwarf_expr_context (ctx
);
231 return baton
.needs_frame
;
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
)
242 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
244 val
= dwarf2_evaluate_loc_desc (symbol
, frame
, dlbaton
->data
, dlbaton
->size
,
250 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
252 locexpr_read_needs_frame (struct symbol
*symbol
)
254 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
255 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
);
258 /* Print a natural-language description of SYMBOL to STREAM. */
260 locexpr_describe_location (struct symbol
*symbol
, struct ui_file
*stream
)
262 /* FIXME: be more extensive. */
263 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
265 if (dlbaton
->size
== 1
266 && dlbaton
->data
[0] >= DW_OP_reg0
267 && dlbaton
->data
[0] <= DW_OP_reg31
)
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
));
275 fprintf_filtered (stream
,
276 "a variable with complex or multiple locations (DWARF2)");
280 /* The set of location functions used with the DWARF-2 expression
282 struct location_funcs dwarf2_locexpr_funcs
= {
283 locexpr_read_variable
,
284 locexpr_read_needs_frame
,
285 locexpr_describe_location
,