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