Commit | Line | Data |
---|---|---|
4c2df51b | 1 | /* DWARF 2 location expression support for GDB. |
feb13ab0 | 2 | |
4c38e0a4 JB |
3 | Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010 |
4 | Free Software Foundation, Inc. | |
feb13ab0 | 5 | |
4c2df51b DJ |
6 | Contributed by Daniel Jacobowitz, MontaVista Software, Inc. |
7 | ||
8 | This file is part of GDB. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
a9762ec7 JB |
12 | the Free Software Foundation; either version 3 of the License, or |
13 | (at your option) any later version. | |
4c2df51b | 14 | |
a9762ec7 JB |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
4c2df51b DJ |
19 | |
20 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4c2df51b DJ |
22 | |
23 | #include "defs.h" | |
24 | #include "ui-out.h" | |
25 | #include "value.h" | |
26 | #include "frame.h" | |
27 | #include "gdbcore.h" | |
28 | #include "target.h" | |
29 | #include "inferior.h" | |
a55cc764 DJ |
30 | #include "ax.h" |
31 | #include "ax-gdb.h" | |
e4adbba9 | 32 | #include "regcache.h" |
c3228f12 | 33 | #include "objfiles.h" |
93ad78a7 | 34 | #include "exceptions.h" |
edb3359d | 35 | #include "block.h" |
4c2df51b | 36 | |
fa8f86ff | 37 | #include "dwarf2.h" |
4c2df51b DJ |
38 | #include "dwarf2expr.h" |
39 | #include "dwarf2loc.h" | |
e7802207 | 40 | #include "dwarf2-frame.h" |
4c2df51b DJ |
41 | |
42 | #include "gdb_string.h" | |
eff4f95e | 43 | #include "gdb_assert.h" |
4c2df51b | 44 | |
0936ad1d SS |
45 | static void |
46 | dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc, | |
47 | gdb_byte **start, size_t *length); | |
48 | ||
0d53c4c4 DJ |
49 | /* A helper function for dealing with location lists. Given a |
50 | symbol baton (BATON) and a pc value (PC), find the appropriate | |
51 | location expression, set *LOCEXPR_LENGTH, and return a pointer | |
52 | to the beginning of the expression. Returns NULL on failure. | |
53 | ||
54 | For now, only return the first matching location expression; there | |
55 | can be more than one in the list. */ | |
56 | ||
852483bc | 57 | static gdb_byte * |
0d53c4c4 | 58 | find_location_expression (struct dwarf2_loclist_baton *baton, |
b6b08ebf | 59 | size_t *locexpr_length, CORE_ADDR pc) |
0d53c4c4 | 60 | { |
0d53c4c4 | 61 | CORE_ADDR low, high; |
852483bc MK |
62 | gdb_byte *loc_ptr, *buf_end; |
63 | int length; | |
ae0d2f24 | 64 | struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu); |
f7fd4728 | 65 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
e17a4113 | 66 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
ae0d2f24 | 67 | unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu); |
0d53c4c4 | 68 | CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1)); |
8edfa926 | 69 | /* Adjust base_address for relocatable objects. */ |
ae0d2f24 UW |
70 | CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets, |
71 | SECT_OFF_TEXT (objfile)); | |
8edfa926 | 72 | CORE_ADDR base_address = baton->base_address + base_offset; |
0d53c4c4 DJ |
73 | |
74 | loc_ptr = baton->data; | |
75 | buf_end = baton->data + baton->size; | |
76 | ||
77 | while (1) | |
78 | { | |
b5758fe4 UW |
79 | if (buf_end - loc_ptr < 2 * addr_size) |
80 | error (_("find_location_expression: Corrupted DWARF expression.")); | |
0d53c4c4 | 81 | |
b5758fe4 UW |
82 | low = extract_unsigned_integer (loc_ptr, addr_size, byte_order); |
83 | loc_ptr += addr_size; | |
0d53c4c4 DJ |
84 | |
85 | /* A base-address-selection entry. */ | |
b5758fe4 | 86 | if (low == base_mask) |
0d53c4c4 | 87 | { |
b5758fe4 UW |
88 | base_address = dwarf2_read_address (gdbarch, |
89 | loc_ptr, buf_end, addr_size); | |
90 | loc_ptr += addr_size; | |
0d53c4c4 DJ |
91 | continue; |
92 | } | |
93 | ||
b5758fe4 UW |
94 | high = extract_unsigned_integer (loc_ptr, addr_size, byte_order); |
95 | loc_ptr += addr_size; | |
96 | ||
97 | /* An end-of-list entry. */ | |
98 | if (low == 0 && high == 0) | |
99 | return NULL; | |
100 | ||
0d53c4c4 DJ |
101 | /* Otherwise, a location expression entry. */ |
102 | low += base_address; | |
103 | high += base_address; | |
104 | ||
e17a4113 | 105 | length = extract_unsigned_integer (loc_ptr, 2, byte_order); |
0d53c4c4 DJ |
106 | loc_ptr += 2; |
107 | ||
108 | if (pc >= low && pc < high) | |
109 | { | |
110 | *locexpr_length = length; | |
111 | return loc_ptr; | |
112 | } | |
113 | ||
114 | loc_ptr += length; | |
115 | } | |
116 | } | |
117 | ||
4c2df51b DJ |
118 | /* This is the baton used when performing dwarf2 expression |
119 | evaluation. */ | |
120 | struct dwarf_expr_baton | |
121 | { | |
122 | struct frame_info *frame; | |
123 | struct objfile *objfile; | |
124 | }; | |
125 | ||
126 | /* Helper functions for dwarf2_evaluate_loc_desc. */ | |
127 | ||
4bc9efe1 | 128 | /* Using the frame specified in BATON, return the value of register |
0b2b0195 | 129 | REGNUM, treated as a pointer. */ |
4c2df51b | 130 | static CORE_ADDR |
61fbb938 | 131 | dwarf_expr_read_reg (void *baton, int dwarf_regnum) |
4c2df51b | 132 | { |
4c2df51b | 133 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; |
5e2b427d | 134 | struct gdbarch *gdbarch = get_frame_arch (debaton->frame); |
e5192dd8 | 135 | CORE_ADDR result; |
0b2b0195 | 136 | int regnum; |
e4adbba9 | 137 | |
5e2b427d UW |
138 | regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum); |
139 | result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr, | |
0b2b0195 | 140 | regnum, debaton->frame); |
4c2df51b DJ |
141 | return result; |
142 | } | |
143 | ||
144 | /* Read memory at ADDR (length LEN) into BUF. */ | |
145 | ||
146 | static void | |
852483bc | 147 | dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len) |
4c2df51b DJ |
148 | { |
149 | read_memory (addr, buf, len); | |
150 | } | |
151 | ||
152 | /* Using the frame specified in BATON, find the location expression | |
153 | describing the frame base. Return a pointer to it in START and | |
154 | its length in LENGTH. */ | |
155 | static void | |
852483bc | 156 | dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length) |
4c2df51b | 157 | { |
da62e633 AC |
158 | /* FIXME: cagney/2003-03-26: This code should be using |
159 | get_frame_base_address(), and then implement a dwarf2 specific | |
160 | this_base method. */ | |
4c2df51b | 161 | struct symbol *framefunc; |
4c2df51b | 162 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; |
0d53c4c4 | 163 | |
edb3359d DJ |
164 | /* Use block_linkage_function, which returns a real (not inlined) |
165 | function, instead of get_frame_function, which may return an | |
166 | inlined function. */ | |
167 | framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL)); | |
0d53c4c4 | 168 | |
eff4f95e JG |
169 | /* If we found a frame-relative symbol then it was certainly within |
170 | some function associated with a frame. If we can't find the frame, | |
171 | something has gone wrong. */ | |
172 | gdb_assert (framefunc != NULL); | |
173 | ||
0936ad1d SS |
174 | dwarf_expr_frame_base_1 (framefunc, |
175 | get_frame_address_in_block (debaton->frame), | |
176 | start, length); | |
177 | } | |
178 | ||
179 | static void | |
180 | dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc, | |
181 | gdb_byte **start, size_t *length) | |
182 | { | |
edb3359d DJ |
183 | if (SYMBOL_LOCATION_BATON (framefunc) == NULL) |
184 | *start = NULL; | |
185 | else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs) | |
0d53c4c4 DJ |
186 | { |
187 | struct dwarf2_loclist_baton *symbaton; | |
22c6caba | 188 | |
0d53c4c4 | 189 | symbaton = SYMBOL_LOCATION_BATON (framefunc); |
0936ad1d | 190 | *start = find_location_expression (symbaton, length, pc); |
0d53c4c4 DJ |
191 | } |
192 | else | |
193 | { | |
194 | struct dwarf2_locexpr_baton *symbaton; | |
195 | symbaton = SYMBOL_LOCATION_BATON (framefunc); | |
ebd3bcc1 JK |
196 | if (symbaton != NULL) |
197 | { | |
198 | *length = symbaton->size; | |
199 | *start = symbaton->data; | |
200 | } | |
201 | else | |
202 | *start = NULL; | |
0d53c4c4 DJ |
203 | } |
204 | ||
205 | if (*start == NULL) | |
8a3fe4f8 | 206 | error (_("Could not find the frame base for \"%s\"."), |
0d53c4c4 | 207 | SYMBOL_NATURAL_NAME (framefunc)); |
4c2df51b DJ |
208 | } |
209 | ||
e7802207 TT |
210 | /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for |
211 | the frame in BATON. */ | |
212 | ||
213 | static CORE_ADDR | |
214 | dwarf_expr_frame_cfa (void *baton) | |
215 | { | |
216 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; | |
217 | return dwarf2_frame_cfa (debaton->frame); | |
218 | } | |
219 | ||
4c2df51b DJ |
220 | /* Using the objfile specified in BATON, find the address for the |
221 | current thread's thread-local storage with offset OFFSET. */ | |
222 | static CORE_ADDR | |
223 | dwarf_expr_tls_address (void *baton, CORE_ADDR offset) | |
224 | { | |
225 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; | |
4c2df51b | 226 | |
9e35dae4 | 227 | return target_translate_tls_address (debaton->objfile, offset); |
4c2df51b DJ |
228 | } |
229 | ||
052b9502 NF |
230 | struct piece_closure |
231 | { | |
232 | /* The number of pieces used to describe this variable. */ | |
233 | int n_pieces; | |
234 | ||
cec03d70 TT |
235 | /* The architecture, used only for DWARF_VALUE_STACK. */ |
236 | struct gdbarch *arch; | |
237 | ||
052b9502 NF |
238 | /* The pieces themselves. */ |
239 | struct dwarf_expr_piece *pieces; | |
240 | }; | |
241 | ||
242 | /* Allocate a closure for a value formed from separately-described | |
243 | PIECES. */ | |
244 | ||
245 | static struct piece_closure * | |
cec03d70 TT |
246 | allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces, |
247 | struct gdbarch *arch) | |
052b9502 NF |
248 | { |
249 | struct piece_closure *c = XZALLOC (struct piece_closure); | |
250 | ||
251 | c->n_pieces = n_pieces; | |
cec03d70 | 252 | c->arch = arch; |
052b9502 NF |
253 | c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece); |
254 | ||
255 | memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece)); | |
256 | ||
257 | return c; | |
258 | } | |
259 | ||
260 | static void | |
261 | read_pieced_value (struct value *v) | |
262 | { | |
263 | int i; | |
264 | long offset = 0; | |
265 | gdb_byte *contents; | |
266 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (v); | |
267 | struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v)); | |
268 | ||
269 | contents = value_contents_raw (v); | |
270 | for (i = 0; i < c->n_pieces; i++) | |
271 | { | |
272 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
cec03d70 | 273 | switch (p->location) |
052b9502 | 274 | { |
cec03d70 TT |
275 | case DWARF_VALUE_REGISTER: |
276 | { | |
277 | struct gdbarch *arch = get_frame_arch (frame); | |
278 | bfd_byte regval[MAX_REGISTER_SIZE]; | |
279 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, | |
44353522 | 280 | p->v.expr.value); |
cec03d70 TT |
281 | get_frame_register (frame, gdb_regnum, regval); |
282 | memcpy (contents + offset, regval, p->size); | |
283 | } | |
284 | break; | |
285 | ||
286 | case DWARF_VALUE_MEMORY: | |
44353522 DE |
287 | if (p->v.expr.in_stack_memory) |
288 | read_stack (p->v.expr.value, contents + offset, p->size); | |
289 | else | |
290 | read_memory (p->v.expr.value, contents + offset, p->size); | |
cec03d70 TT |
291 | break; |
292 | ||
293 | case DWARF_VALUE_STACK: | |
294 | { | |
295 | gdb_byte bytes[sizeof (ULONGEST)]; | |
296 | size_t n; | |
297 | int addr_size = gdbarch_addr_bit (c->arch) / 8; | |
298 | store_unsigned_integer (bytes, addr_size, | |
299 | gdbarch_byte_order (c->arch), | |
44353522 | 300 | p->v.expr.value); |
cec03d70 TT |
301 | n = p->size; |
302 | if (n > addr_size) | |
303 | n = addr_size; | |
304 | memcpy (contents + offset, bytes, n); | |
305 | } | |
306 | break; | |
307 | ||
308 | case DWARF_VALUE_LITERAL: | |
309 | { | |
310 | size_t n = p->size; | |
311 | if (n > p->v.literal.length) | |
312 | n = p->v.literal.length; | |
313 | memcpy (contents + offset, p->v.literal.data, n); | |
314 | } | |
315 | break; | |
316 | ||
317 | default: | |
318 | internal_error (__FILE__, __LINE__, _("invalid location type")); | |
052b9502 NF |
319 | } |
320 | offset += p->size; | |
321 | } | |
322 | } | |
323 | ||
324 | static void | |
325 | write_pieced_value (struct value *to, struct value *from) | |
326 | { | |
327 | int i; | |
328 | long offset = 0; | |
329 | gdb_byte *contents; | |
330 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (to); | |
331 | struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to)); | |
332 | ||
333 | if (frame == NULL) | |
334 | { | |
335 | set_value_optimized_out (to, 1); | |
336 | return; | |
337 | } | |
338 | ||
339 | contents = value_contents_raw (from); | |
340 | for (i = 0; i < c->n_pieces; i++) | |
341 | { | |
342 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
cec03d70 | 343 | switch (p->location) |
052b9502 | 344 | { |
cec03d70 TT |
345 | case DWARF_VALUE_REGISTER: |
346 | { | |
347 | struct gdbarch *arch = get_frame_arch (frame); | |
44353522 | 348 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value); |
cec03d70 TT |
349 | put_frame_register (frame, gdb_regnum, contents + offset); |
350 | } | |
351 | break; | |
352 | case DWARF_VALUE_MEMORY: | |
44353522 | 353 | write_memory (p->v.expr.value, contents + offset, p->size); |
cec03d70 TT |
354 | break; |
355 | default: | |
356 | set_value_optimized_out (to, 1); | |
357 | return; | |
052b9502 NF |
358 | } |
359 | offset += p->size; | |
360 | } | |
361 | } | |
362 | ||
363 | static void * | |
364 | copy_pieced_value_closure (struct value *v) | |
365 | { | |
366 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (v); | |
367 | ||
cec03d70 | 368 | return allocate_piece_closure (c->n_pieces, c->pieces, c->arch); |
052b9502 NF |
369 | } |
370 | ||
371 | static void | |
372 | free_pieced_value_closure (struct value *v) | |
373 | { | |
374 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (v); | |
375 | ||
376 | xfree (c->pieces); | |
377 | xfree (c); | |
378 | } | |
379 | ||
380 | /* Functions for accessing a variable described by DW_OP_piece. */ | |
381 | static struct lval_funcs pieced_value_funcs = { | |
382 | read_pieced_value, | |
383 | write_pieced_value, | |
384 | copy_pieced_value_closure, | |
385 | free_pieced_value_closure | |
386 | }; | |
387 | ||
4c2df51b DJ |
388 | /* Evaluate a location description, starting at DATA and with length |
389 | SIZE, to find the current location of variable VAR in the context | |
390 | of FRAME. */ | |
391 | static struct value * | |
392 | dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame, | |
852483bc | 393 | gdb_byte *data, unsigned short size, |
ae0d2f24 | 394 | struct dwarf2_per_cu_data *per_cu) |
4c2df51b | 395 | { |
4c2df51b DJ |
396 | struct value *retval; |
397 | struct dwarf_expr_baton baton; | |
398 | struct dwarf_expr_context *ctx; | |
4a227398 | 399 | struct cleanup *old_chain; |
4c2df51b | 400 | |
0d53c4c4 DJ |
401 | if (size == 0) |
402 | { | |
403 | retval = allocate_value (SYMBOL_TYPE (var)); | |
404 | VALUE_LVAL (retval) = not_lval; | |
feb13ab0 | 405 | set_value_optimized_out (retval, 1); |
10fb19b6 | 406 | return retval; |
0d53c4c4 DJ |
407 | } |
408 | ||
4c2df51b | 409 | baton.frame = frame; |
ae0d2f24 | 410 | baton.objfile = dwarf2_per_cu_objfile (per_cu); |
4c2df51b DJ |
411 | |
412 | ctx = new_dwarf_expr_context (); | |
4a227398 TT |
413 | old_chain = make_cleanup_free_dwarf_expr_context (ctx); |
414 | ||
f7fd4728 | 415 | ctx->gdbarch = get_objfile_arch (baton.objfile); |
ae0d2f24 | 416 | ctx->addr_size = dwarf2_per_cu_addr_size (per_cu); |
4c2df51b DJ |
417 | ctx->baton = &baton; |
418 | ctx->read_reg = dwarf_expr_read_reg; | |
419 | ctx->read_mem = dwarf_expr_read_mem; | |
420 | ctx->get_frame_base = dwarf_expr_frame_base; | |
e7802207 | 421 | ctx->get_frame_cfa = dwarf_expr_frame_cfa; |
4c2df51b DJ |
422 | ctx->get_tls_address = dwarf_expr_tls_address; |
423 | ||
424 | dwarf_expr_eval (ctx, data, size); | |
87808bd6 JB |
425 | if (ctx->num_pieces > 0) |
426 | { | |
052b9502 NF |
427 | struct piece_closure *c; |
428 | struct frame_id frame_id = get_frame_id (frame); | |
429 | ||
cec03d70 | 430 | c = allocate_piece_closure (ctx->num_pieces, ctx->pieces, ctx->gdbarch); |
052b9502 NF |
431 | retval = allocate_computed_value (SYMBOL_TYPE (var), |
432 | &pieced_value_funcs, | |
433 | c); | |
434 | VALUE_FRAME_ID (retval) = frame_id; | |
87808bd6 | 435 | } |
4c2df51b DJ |
436 | else |
437 | { | |
cec03d70 TT |
438 | switch (ctx->location) |
439 | { | |
440 | case DWARF_VALUE_REGISTER: | |
441 | { | |
442 | struct gdbarch *arch = get_frame_arch (frame); | |
443 | CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0); | |
444 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum); | |
445 | retval = value_from_register (SYMBOL_TYPE (var), gdb_regnum, frame); | |
446 | } | |
447 | break; | |
448 | ||
449 | case DWARF_VALUE_MEMORY: | |
450 | { | |
451 | CORE_ADDR address = dwarf_expr_fetch (ctx, 0); | |
44353522 | 452 | int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0); |
cec03d70 TT |
453 | |
454 | retval = allocate_value (SYMBOL_TYPE (var)); | |
455 | VALUE_LVAL (retval) = lval_memory; | |
456 | set_value_lazy (retval, 1); | |
44353522 DE |
457 | if (in_stack_memory) |
458 | set_value_stack (retval, 1); | |
cec03d70 TT |
459 | set_value_address (retval, address); |
460 | } | |
461 | break; | |
462 | ||
463 | case DWARF_VALUE_STACK: | |
464 | { | |
465 | gdb_byte bytes[sizeof (ULONGEST)]; | |
466 | ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0); | |
467 | bfd_byte *contents; | |
468 | size_t n = ctx->addr_size; | |
469 | ||
470 | store_unsigned_integer (bytes, ctx->addr_size, | |
471 | gdbarch_byte_order (ctx->gdbarch), | |
472 | value); | |
473 | retval = allocate_value (SYMBOL_TYPE (var)); | |
474 | contents = value_contents_raw (retval); | |
475 | if (n > TYPE_LENGTH (SYMBOL_TYPE (var))) | |
476 | n = TYPE_LENGTH (SYMBOL_TYPE (var)); | |
477 | memcpy (contents, bytes, n); | |
478 | } | |
479 | break; | |
480 | ||
481 | case DWARF_VALUE_LITERAL: | |
482 | { | |
483 | bfd_byte *contents; | |
484 | size_t n = ctx->len; | |
485 | ||
486 | retval = allocate_value (SYMBOL_TYPE (var)); | |
487 | contents = value_contents_raw (retval); | |
488 | if (n > TYPE_LENGTH (SYMBOL_TYPE (var))) | |
489 | n = TYPE_LENGTH (SYMBOL_TYPE (var)); | |
490 | memcpy (contents, ctx->data, n); | |
491 | } | |
492 | break; | |
493 | ||
494 | default: | |
495 | internal_error (__FILE__, __LINE__, _("invalid location type")); | |
496 | } | |
4c2df51b DJ |
497 | } |
498 | ||
42be36b3 CT |
499 | set_value_initialized (retval, ctx->initialized); |
500 | ||
4a227398 | 501 | do_cleanups (old_chain); |
4c2df51b DJ |
502 | |
503 | return retval; | |
504 | } | |
4c2df51b DJ |
505 | \f |
506 | /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */ | |
507 | ||
508 | struct needs_frame_baton | |
509 | { | |
510 | int needs_frame; | |
511 | }; | |
512 | ||
513 | /* Reads from registers do require a frame. */ | |
514 | static CORE_ADDR | |
61fbb938 | 515 | needs_frame_read_reg (void *baton, int regnum) |
4c2df51b DJ |
516 | { |
517 | struct needs_frame_baton *nf_baton = baton; | |
518 | nf_baton->needs_frame = 1; | |
519 | return 1; | |
520 | } | |
521 | ||
522 | /* Reads from memory do not require a frame. */ | |
523 | static void | |
852483bc | 524 | needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len) |
4c2df51b DJ |
525 | { |
526 | memset (buf, 0, len); | |
527 | } | |
528 | ||
529 | /* Frame-relative accesses do require a frame. */ | |
530 | static void | |
852483bc | 531 | needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length) |
4c2df51b | 532 | { |
852483bc | 533 | static gdb_byte lit0 = DW_OP_lit0; |
4c2df51b DJ |
534 | struct needs_frame_baton *nf_baton = baton; |
535 | ||
536 | *start = &lit0; | |
537 | *length = 1; | |
538 | ||
539 | nf_baton->needs_frame = 1; | |
540 | } | |
541 | ||
e7802207 TT |
542 | /* CFA accesses require a frame. */ |
543 | ||
544 | static CORE_ADDR | |
545 | needs_frame_frame_cfa (void *baton) | |
546 | { | |
547 | struct needs_frame_baton *nf_baton = baton; | |
548 | nf_baton->needs_frame = 1; | |
549 | return 1; | |
550 | } | |
551 | ||
4c2df51b DJ |
552 | /* Thread-local accesses do require a frame. */ |
553 | static CORE_ADDR | |
554 | needs_frame_tls_address (void *baton, CORE_ADDR offset) | |
555 | { | |
556 | struct needs_frame_baton *nf_baton = baton; | |
557 | nf_baton->needs_frame = 1; | |
558 | return 1; | |
559 | } | |
560 | ||
561 | /* Return non-zero iff the location expression at DATA (length SIZE) | |
562 | requires a frame to evaluate. */ | |
563 | ||
564 | static int | |
ae0d2f24 UW |
565 | dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size, |
566 | struct dwarf2_per_cu_data *per_cu) | |
4c2df51b DJ |
567 | { |
568 | struct needs_frame_baton baton; | |
569 | struct dwarf_expr_context *ctx; | |
f630a401 | 570 | int in_reg; |
4a227398 | 571 | struct cleanup *old_chain; |
4c2df51b DJ |
572 | |
573 | baton.needs_frame = 0; | |
574 | ||
575 | ctx = new_dwarf_expr_context (); | |
4a227398 TT |
576 | old_chain = make_cleanup_free_dwarf_expr_context (ctx); |
577 | ||
f7fd4728 | 578 | ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu)); |
ae0d2f24 | 579 | ctx->addr_size = dwarf2_per_cu_addr_size (per_cu); |
4c2df51b DJ |
580 | ctx->baton = &baton; |
581 | ctx->read_reg = needs_frame_read_reg; | |
582 | ctx->read_mem = needs_frame_read_mem; | |
583 | ctx->get_frame_base = needs_frame_frame_base; | |
e7802207 | 584 | ctx->get_frame_cfa = needs_frame_frame_cfa; |
4c2df51b DJ |
585 | ctx->get_tls_address = needs_frame_tls_address; |
586 | ||
587 | dwarf_expr_eval (ctx, data, size); | |
588 | ||
cec03d70 | 589 | in_reg = ctx->location == DWARF_VALUE_REGISTER; |
f630a401 | 590 | |
87808bd6 JB |
591 | if (ctx->num_pieces > 0) |
592 | { | |
593 | int i; | |
594 | ||
595 | /* If the location has several pieces, and any of them are in | |
596 | registers, then we will need a frame to fetch them from. */ | |
597 | for (i = 0; i < ctx->num_pieces; i++) | |
cec03d70 | 598 | if (ctx->pieces[i].location == DWARF_VALUE_REGISTER) |
87808bd6 JB |
599 | in_reg = 1; |
600 | } | |
601 | ||
4a227398 | 602 | do_cleanups (old_chain); |
4c2df51b | 603 | |
f630a401 | 604 | return baton.needs_frame || in_reg; |
4c2df51b DJ |
605 | } |
606 | ||
0d53c4c4 | 607 | static void |
505e835d UW |
608 | dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
609 | struct agent_expr *ax, struct axs_value *value, | |
610 | gdb_byte *data, int size) | |
0d53c4c4 DJ |
611 | { |
612 | if (size == 0) | |
8a3fe4f8 | 613 | error (_("Symbol \"%s\" has been optimized out."), |
0d53c4c4 DJ |
614 | SYMBOL_PRINT_NAME (symbol)); |
615 | ||
616 | if (size == 1 | |
617 | && data[0] >= DW_OP_reg0 | |
618 | && data[0] <= DW_OP_reg31) | |
619 | { | |
620 | value->kind = axs_lvalue_register; | |
621 | value->u.reg = data[0] - DW_OP_reg0; | |
622 | } | |
623 | else if (data[0] == DW_OP_regx) | |
624 | { | |
625 | ULONGEST reg; | |
626 | read_uleb128 (data + 1, data + size, ®); | |
627 | value->kind = axs_lvalue_register; | |
628 | value->u.reg = reg; | |
629 | } | |
630 | else if (data[0] == DW_OP_fbreg) | |
631 | { | |
0936ad1d SS |
632 | struct block *b; |
633 | struct symbol *framefunc; | |
634 | int frame_reg = 0; | |
0d53c4c4 | 635 | LONGEST frame_offset; |
852483bc | 636 | gdb_byte *buf_end; |
0936ad1d SS |
637 | gdb_byte *base_data; |
638 | size_t base_size; | |
639 | LONGEST base_offset = 0; | |
640 | ||
641 | b = block_for_pc (ax->scope); | |
642 | ||
643 | if (!b) | |
644 | error (_("No block found for address")); | |
645 | ||
646 | framefunc = block_linkage_function (b); | |
647 | ||
648 | if (!framefunc) | |
649 | error (_("No function found for block")); | |
650 | ||
651 | dwarf_expr_frame_base_1 (framefunc, ax->scope, | |
652 | &base_data, &base_size); | |
653 | ||
654 | if (base_data[0] >= DW_OP_breg0 | |
655 | && base_data[0] <= DW_OP_breg31) | |
656 | { | |
657 | frame_reg = base_data[0] - DW_OP_breg0; | |
658 | buf_end = read_sleb128 (base_data + 1, base_data + base_size, &base_offset); | |
659 | if (buf_end != base_data + base_size) | |
660 | error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."), | |
661 | frame_reg, SYMBOL_PRINT_NAME (symbol)); | |
662 | } | |
663 | else | |
664 | { | |
665 | /* We don't know what to do with the frame base expression, | |
666 | so we can't trace this variable; give up. */ | |
667 | error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled"), | |
668 | SYMBOL_PRINT_NAME (symbol)); | |
669 | } | |
0d53c4c4 DJ |
670 | |
671 | buf_end = read_sleb128 (data + 1, data + size, &frame_offset); | |
672 | if (buf_end != data + size) | |
8a3fe4f8 | 673 | error (_("Unexpected opcode after DW_OP_fbreg for symbol \"%s\"."), |
0d53c4c4 | 674 | SYMBOL_PRINT_NAME (symbol)); |
4c2df51b | 675 | |
0d53c4c4 | 676 | ax_reg (ax, frame_reg); |
0936ad1d | 677 | ax_const_l (ax, base_offset + frame_offset); |
0d53c4c4 | 678 | ax_simple (ax, aop_add); |
4c2df51b | 679 | |
9c238357 RC |
680 | value->kind = axs_lvalue_memory; |
681 | } | |
682 | else if (data[0] >= DW_OP_breg0 | |
683 | && data[0] <= DW_OP_breg31) | |
684 | { | |
685 | unsigned int reg; | |
686 | LONGEST offset; | |
687 | gdb_byte *buf_end; | |
688 | ||
689 | reg = data[0] - DW_OP_breg0; | |
690 | buf_end = read_sleb128 (data + 1, data + size, &offset); | |
691 | if (buf_end != data + size) | |
692 | error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."), | |
693 | reg, SYMBOL_PRINT_NAME (symbol)); | |
694 | ||
695 | ax_reg (ax, reg); | |
696 | ax_const_l (ax, offset); | |
0d53c4c4 | 697 | ax_simple (ax, aop_add); |
9c238357 | 698 | |
0d53c4c4 DJ |
699 | value->kind = axs_lvalue_memory; |
700 | } | |
701 | else | |
9c238357 RC |
702 | error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."), |
703 | data[0], SYMBOL_PRINT_NAME (symbol)); | |
0d53c4c4 | 704 | } |
4c2df51b DJ |
705 | \f |
706 | /* Return the value of SYMBOL in FRAME using the DWARF-2 expression | |
707 | evaluator to calculate the location. */ | |
708 | static struct value * | |
709 | locexpr_read_variable (struct symbol *symbol, struct frame_info *frame) | |
710 | { | |
711 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
712 | struct value *val; | |
713 | val = dwarf2_evaluate_loc_desc (symbol, frame, dlbaton->data, dlbaton->size, | |
ae0d2f24 | 714 | dlbaton->per_cu); |
4c2df51b DJ |
715 | |
716 | return val; | |
717 | } | |
718 | ||
719 | /* Return non-zero iff we need a frame to evaluate SYMBOL. */ | |
720 | static int | |
721 | locexpr_read_needs_frame (struct symbol *symbol) | |
722 | { | |
723 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
ae0d2f24 UW |
724 | return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size, |
725 | dlbaton->per_cu); | |
4c2df51b DJ |
726 | } |
727 | ||
728 | /* Print a natural-language description of SYMBOL to STREAM. */ | |
729 | static int | |
730 | locexpr_describe_location (struct symbol *symbol, struct ui_file *stream) | |
731 | { | |
732 | /* FIXME: be more extensive. */ | |
733 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
ae0d2f24 | 734 | int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); |
4c2df51b DJ |
735 | |
736 | if (dlbaton->size == 1 | |
737 | && dlbaton->data[0] >= DW_OP_reg0 | |
738 | && dlbaton->data[0] <= DW_OP_reg31) | |
739 | { | |
5e2b427d UW |
740 | struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu); |
741 | struct gdbarch *gdbarch = get_objfile_arch (objfile); | |
742 | int regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, | |
743 | dlbaton->data[0] - DW_OP_reg0); | |
4c2df51b | 744 | fprintf_filtered (stream, |
c9f4d572 | 745 | "a variable in register %s", |
5e2b427d | 746 | gdbarch_register_name (gdbarch, regno)); |
4c2df51b DJ |
747 | return 1; |
748 | } | |
749 | ||
c3228f12 EZ |
750 | /* The location expression for a TLS variable looks like this (on a |
751 | 64-bit LE machine): | |
752 | ||
753 | DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0 | |
754 | (DW_OP_addr: 4; DW_OP_GNU_push_tls_address) | |
755 | ||
756 | 0x3 is the encoding for DW_OP_addr, which has an operand as long | |
757 | as the size of an address on the target machine (here is 8 | |
758 | bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address. | |
759 | The operand represents the offset at which the variable is within | |
760 | the thread local storage. */ | |
761 | ||
762 | if (dlbaton->size > 1 | |
763 | && dlbaton->data[dlbaton->size - 1] == DW_OP_GNU_push_tls_address) | |
764 | if (dlbaton->data[0] == DW_OP_addr) | |
765 | { | |
ae0d2f24 | 766 | struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu); |
f7fd4728 UW |
767 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
768 | CORE_ADDR offset = dwarf2_read_address (gdbarch, | |
769 | &dlbaton->data[1], | |
c193f044 | 770 | &dlbaton->data[dlbaton->size - 1], |
ae0d2f24 | 771 | addr_size); |
c3228f12 | 772 | fprintf_filtered (stream, |
32ffcbed | 773 | "a thread-local variable at offset %s in the " |
c3228f12 | 774 | "thread-local storage for `%s'", |
5af949e3 | 775 | paddress (gdbarch, offset), objfile->name); |
c3228f12 EZ |
776 | return 1; |
777 | } | |
778 | ||
779 | ||
4c2df51b DJ |
780 | fprintf_filtered (stream, |
781 | "a variable with complex or multiple locations (DWARF2)"); | |
782 | return 1; | |
783 | } | |
784 | ||
a55cc764 DJ |
785 | |
786 | /* Describe the location of SYMBOL as an agent value in VALUE, generating | |
787 | any necessary bytecode in AX. | |
788 | ||
789 | NOTE drow/2003-02-26: This function is extremely minimal, because | |
790 | doing it correctly is extremely complicated and there is no | |
791 | publicly available stub with tracepoint support for me to test | |
792 | against. When there is one this function should be revisited. */ | |
793 | ||
0d53c4c4 | 794 | static void |
505e835d UW |
795 | locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
796 | struct agent_expr *ax, struct axs_value *value) | |
a55cc764 DJ |
797 | { |
798 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
799 | ||
505e835d UW |
800 | dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, |
801 | dlbaton->data, dlbaton->size); | |
a55cc764 DJ |
802 | } |
803 | ||
4c2df51b DJ |
804 | /* The set of location functions used with the DWARF-2 expression |
805 | evaluator. */ | |
768a979c | 806 | const struct symbol_computed_ops dwarf2_locexpr_funcs = { |
4c2df51b DJ |
807 | locexpr_read_variable, |
808 | locexpr_read_needs_frame, | |
809 | locexpr_describe_location, | |
a55cc764 | 810 | locexpr_tracepoint_var_ref |
4c2df51b | 811 | }; |
0d53c4c4 DJ |
812 | |
813 | ||
814 | /* Wrapper functions for location lists. These generally find | |
815 | the appropriate location expression and call something above. */ | |
816 | ||
817 | /* Return the value of SYMBOL in FRAME using the DWARF-2 expression | |
818 | evaluator to calculate the location. */ | |
819 | static struct value * | |
820 | loclist_read_variable (struct symbol *symbol, struct frame_info *frame) | |
821 | { | |
822 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
823 | struct value *val; | |
852483bc | 824 | gdb_byte *data; |
b6b08ebf | 825 | size_t size; |
0d53c4c4 DJ |
826 | |
827 | data = find_location_expression (dlbaton, &size, | |
22c6caba JW |
828 | frame ? get_frame_address_in_block (frame) |
829 | : 0); | |
0d53c4c4 | 830 | if (data == NULL) |
806048c6 DJ |
831 | { |
832 | val = allocate_value (SYMBOL_TYPE (symbol)); | |
833 | VALUE_LVAL (val) = not_lval; | |
834 | set_value_optimized_out (val, 1); | |
835 | } | |
836 | else | |
837 | val = dwarf2_evaluate_loc_desc (symbol, frame, data, size, | |
ae0d2f24 | 838 | dlbaton->per_cu); |
0d53c4c4 DJ |
839 | |
840 | return val; | |
841 | } | |
842 | ||
843 | /* Return non-zero iff we need a frame to evaluate SYMBOL. */ | |
844 | static int | |
845 | loclist_read_needs_frame (struct symbol *symbol) | |
846 | { | |
847 | /* If there's a location list, then assume we need to have a frame | |
848 | to choose the appropriate location expression. With tracking of | |
849 | global variables this is not necessarily true, but such tracking | |
850 | is disabled in GCC at the moment until we figure out how to | |
851 | represent it. */ | |
852 | ||
853 | return 1; | |
854 | } | |
855 | ||
856 | /* Print a natural-language description of SYMBOL to STREAM. */ | |
857 | static int | |
858 | loclist_describe_location (struct symbol *symbol, struct ui_file *stream) | |
859 | { | |
860 | /* FIXME: Could print the entire list of locations. */ | |
861 | fprintf_filtered (stream, "a variable with multiple locations"); | |
862 | return 1; | |
863 | } | |
864 | ||
865 | /* Describe the location of SYMBOL as an agent value in VALUE, generating | |
866 | any necessary bytecode in AX. */ | |
867 | static void | |
505e835d UW |
868 | loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
869 | struct agent_expr *ax, struct axs_value *value) | |
0d53c4c4 DJ |
870 | { |
871 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
852483bc | 872 | gdb_byte *data; |
b6b08ebf | 873 | size_t size; |
0d53c4c4 DJ |
874 | |
875 | data = find_location_expression (dlbaton, &size, ax->scope); | |
876 | if (data == NULL) | |
8a3fe4f8 | 877 | error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol)); |
0d53c4c4 | 878 | |
505e835d | 879 | dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size); |
0d53c4c4 DJ |
880 | } |
881 | ||
882 | /* The set of location functions used with the DWARF-2 expression | |
883 | evaluator and location lists. */ | |
768a979c | 884 | const struct symbol_computed_ops dwarf2_loclist_funcs = { |
0d53c4c4 DJ |
885 | loclist_read_variable, |
886 | loclist_read_needs_frame, | |
887 | loclist_describe_location, | |
888 | loclist_tracepoint_var_ref | |
889 | }; |