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