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 | ||
223 | /* The pieces themselves. */ | |
224 | struct dwarf_expr_piece *pieces; | |
225 | }; | |
226 | ||
227 | /* Allocate a closure for a value formed from separately-described | |
228 | PIECES. */ | |
229 | ||
230 | static struct piece_closure * | |
231 | allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces) | |
232 | { | |
233 | struct piece_closure *c = XZALLOC (struct piece_closure); | |
234 | ||
235 | c->n_pieces = n_pieces; | |
236 | c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece); | |
237 | ||
238 | memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece)); | |
239 | ||
240 | return c; | |
241 | } | |
242 | ||
243 | static void | |
244 | read_pieced_value (struct value *v) | |
245 | { | |
246 | int i; | |
247 | long offset = 0; | |
248 | gdb_byte *contents; | |
249 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (v); | |
250 | struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v)); | |
251 | ||
252 | contents = value_contents_raw (v); | |
253 | for (i = 0; i < c->n_pieces; i++) | |
254 | { | |
255 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
256 | ||
257 | if (frame == NULL) | |
258 | { | |
259 | memset (contents + offset, 0, p->size); | |
260 | set_value_optimized_out (v, 1); | |
261 | } | |
262 | else if (p->in_reg) | |
263 | { | |
264 | struct gdbarch *arch = get_frame_arch (frame); | |
265 | gdb_byte regval[MAX_REGISTER_SIZE]; | |
266 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->value); | |
267 | ||
268 | get_frame_register (frame, gdb_regnum, regval); | |
269 | memcpy (contents + offset, regval, p->size); | |
270 | } | |
271 | else | |
272 | { | |
273 | read_memory (p->value, contents + offset, p->size); | |
274 | } | |
275 | offset += p->size; | |
276 | } | |
277 | } | |
278 | ||
279 | static void | |
280 | write_pieced_value (struct value *to, struct value *from) | |
281 | { | |
282 | int i; | |
283 | long offset = 0; | |
284 | gdb_byte *contents; | |
285 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (to); | |
286 | struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to)); | |
287 | ||
288 | if (frame == NULL) | |
289 | { | |
290 | set_value_optimized_out (to, 1); | |
291 | return; | |
292 | } | |
293 | ||
294 | contents = value_contents_raw (from); | |
295 | for (i = 0; i < c->n_pieces; i++) | |
296 | { | |
297 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
298 | if (p->in_reg) | |
299 | { | |
300 | struct gdbarch *arch = get_frame_arch (frame); | |
301 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->value); | |
302 | put_frame_register (frame, gdb_regnum, contents + offset); | |
303 | } | |
304 | else | |
305 | { | |
306 | write_memory (p->value, contents + offset, p->size); | |
307 | } | |
308 | offset += p->size; | |
309 | } | |
310 | } | |
311 | ||
312 | static void * | |
313 | copy_pieced_value_closure (struct value *v) | |
314 | { | |
315 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (v); | |
316 | ||
317 | return allocate_piece_closure (c->n_pieces, c->pieces); | |
318 | } | |
319 | ||
320 | static void | |
321 | free_pieced_value_closure (struct value *v) | |
322 | { | |
323 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (v); | |
324 | ||
325 | xfree (c->pieces); | |
326 | xfree (c); | |
327 | } | |
328 | ||
329 | /* Functions for accessing a variable described by DW_OP_piece. */ | |
330 | static struct lval_funcs pieced_value_funcs = { | |
331 | read_pieced_value, | |
332 | write_pieced_value, | |
333 | copy_pieced_value_closure, | |
334 | free_pieced_value_closure | |
335 | }; | |
336 | ||
4c2df51b DJ |
337 | /* Evaluate a location description, starting at DATA and with length |
338 | SIZE, to find the current location of variable VAR in the context | |
339 | of FRAME. */ | |
340 | static struct value * | |
341 | dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame, | |
852483bc | 342 | gdb_byte *data, unsigned short size, |
ae0d2f24 | 343 | struct dwarf2_per_cu_data *per_cu) |
4c2df51b | 344 | { |
4c2df51b DJ |
345 | struct value *retval; |
346 | struct dwarf_expr_baton baton; | |
347 | struct dwarf_expr_context *ctx; | |
4a227398 | 348 | struct cleanup *old_chain; |
4c2df51b | 349 | |
0d53c4c4 DJ |
350 | if (size == 0) |
351 | { | |
352 | retval = allocate_value (SYMBOL_TYPE (var)); | |
353 | VALUE_LVAL (retval) = not_lval; | |
feb13ab0 | 354 | set_value_optimized_out (retval, 1); |
10fb19b6 | 355 | return retval; |
0d53c4c4 DJ |
356 | } |
357 | ||
4c2df51b | 358 | baton.frame = frame; |
ae0d2f24 | 359 | baton.objfile = dwarf2_per_cu_objfile (per_cu); |
4c2df51b DJ |
360 | |
361 | ctx = new_dwarf_expr_context (); | |
4a227398 TT |
362 | old_chain = make_cleanup_free_dwarf_expr_context (ctx); |
363 | ||
f7fd4728 | 364 | ctx->gdbarch = get_objfile_arch (baton.objfile); |
ae0d2f24 | 365 | ctx->addr_size = dwarf2_per_cu_addr_size (per_cu); |
4c2df51b DJ |
366 | ctx->baton = &baton; |
367 | ctx->read_reg = dwarf_expr_read_reg; | |
368 | ctx->read_mem = dwarf_expr_read_mem; | |
369 | ctx->get_frame_base = dwarf_expr_frame_base; | |
e7802207 | 370 | ctx->get_frame_cfa = dwarf_expr_frame_cfa; |
4c2df51b DJ |
371 | ctx->get_tls_address = dwarf_expr_tls_address; |
372 | ||
373 | dwarf_expr_eval (ctx, data, size); | |
87808bd6 JB |
374 | if (ctx->num_pieces > 0) |
375 | { | |
052b9502 NF |
376 | struct piece_closure *c; |
377 | struct frame_id frame_id = get_frame_id (frame); | |
378 | ||
379 | c = allocate_piece_closure (ctx->num_pieces, ctx->pieces); | |
380 | retval = allocate_computed_value (SYMBOL_TYPE (var), | |
381 | &pieced_value_funcs, | |
382 | c); | |
383 | VALUE_FRAME_ID (retval) = frame_id; | |
87808bd6 JB |
384 | } |
385 | else if (ctx->in_reg) | |
051caad9 | 386 | { |
77a732d9 | 387 | struct gdbarch *arch = get_frame_arch (frame); |
5ca2e327 | 388 | CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0); |
77a732d9 | 389 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum); |
5ca2e327 | 390 | retval = value_from_register (SYMBOL_TYPE (var), gdb_regnum, frame); |
051caad9 | 391 | } |
4c2df51b DJ |
392 | else |
393 | { | |
5ca2e327 JB |
394 | CORE_ADDR address = dwarf_expr_fetch (ctx, 0); |
395 | ||
61fbb938 | 396 | retval = allocate_value (SYMBOL_TYPE (var)); |
4c2df51b | 397 | VALUE_LVAL (retval) = lval_memory; |
dfa52d88 | 398 | set_value_lazy (retval, 1); |
4e5d721f | 399 | set_value_stack (retval, 1); |
42ae5230 | 400 | set_value_address (retval, address); |
4c2df51b DJ |
401 | } |
402 | ||
42be36b3 CT |
403 | set_value_initialized (retval, ctx->initialized); |
404 | ||
4a227398 | 405 | do_cleanups (old_chain); |
4c2df51b DJ |
406 | |
407 | return retval; | |
408 | } | |
409 | ||
410 | ||
411 | ||
412 | ||
413 | \f | |
414 | /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */ | |
415 | ||
416 | struct needs_frame_baton | |
417 | { | |
418 | int needs_frame; | |
419 | }; | |
420 | ||
421 | /* Reads from registers do require a frame. */ | |
422 | static CORE_ADDR | |
61fbb938 | 423 | needs_frame_read_reg (void *baton, int regnum) |
4c2df51b DJ |
424 | { |
425 | struct needs_frame_baton *nf_baton = baton; | |
426 | nf_baton->needs_frame = 1; | |
427 | return 1; | |
428 | } | |
429 | ||
430 | /* Reads from memory do not require a frame. */ | |
431 | static void | |
852483bc | 432 | needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len) |
4c2df51b DJ |
433 | { |
434 | memset (buf, 0, len); | |
435 | } | |
436 | ||
437 | /* Frame-relative accesses do require a frame. */ | |
438 | static void | |
852483bc | 439 | needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length) |
4c2df51b | 440 | { |
852483bc | 441 | static gdb_byte lit0 = DW_OP_lit0; |
4c2df51b DJ |
442 | struct needs_frame_baton *nf_baton = baton; |
443 | ||
444 | *start = &lit0; | |
445 | *length = 1; | |
446 | ||
447 | nf_baton->needs_frame = 1; | |
448 | } | |
449 | ||
e7802207 TT |
450 | /* CFA accesses require a frame. */ |
451 | ||
452 | static CORE_ADDR | |
453 | needs_frame_frame_cfa (void *baton) | |
454 | { | |
455 | struct needs_frame_baton *nf_baton = baton; | |
456 | nf_baton->needs_frame = 1; | |
457 | return 1; | |
458 | } | |
459 | ||
4c2df51b DJ |
460 | /* Thread-local accesses do require a frame. */ |
461 | static CORE_ADDR | |
462 | needs_frame_tls_address (void *baton, CORE_ADDR offset) | |
463 | { | |
464 | struct needs_frame_baton *nf_baton = baton; | |
465 | nf_baton->needs_frame = 1; | |
466 | return 1; | |
467 | } | |
468 | ||
469 | /* Return non-zero iff the location expression at DATA (length SIZE) | |
470 | requires a frame to evaluate. */ | |
471 | ||
472 | static int | |
ae0d2f24 UW |
473 | dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size, |
474 | struct dwarf2_per_cu_data *per_cu) | |
4c2df51b DJ |
475 | { |
476 | struct needs_frame_baton baton; | |
477 | struct dwarf_expr_context *ctx; | |
f630a401 | 478 | int in_reg; |
4a227398 | 479 | struct cleanup *old_chain; |
4c2df51b DJ |
480 | |
481 | baton.needs_frame = 0; | |
482 | ||
483 | ctx = new_dwarf_expr_context (); | |
4a227398 TT |
484 | old_chain = make_cleanup_free_dwarf_expr_context (ctx); |
485 | ||
f7fd4728 | 486 | ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu)); |
ae0d2f24 | 487 | ctx->addr_size = dwarf2_per_cu_addr_size (per_cu); |
4c2df51b DJ |
488 | ctx->baton = &baton; |
489 | ctx->read_reg = needs_frame_read_reg; | |
490 | ctx->read_mem = needs_frame_read_mem; | |
491 | ctx->get_frame_base = needs_frame_frame_base; | |
e7802207 | 492 | ctx->get_frame_cfa = needs_frame_frame_cfa; |
4c2df51b DJ |
493 | ctx->get_tls_address = needs_frame_tls_address; |
494 | ||
495 | dwarf_expr_eval (ctx, data, size); | |
496 | ||
f630a401 DJ |
497 | in_reg = ctx->in_reg; |
498 | ||
87808bd6 JB |
499 | if (ctx->num_pieces > 0) |
500 | { | |
501 | int i; | |
502 | ||
503 | /* If the location has several pieces, and any of them are in | |
504 | registers, then we will need a frame to fetch them from. */ | |
505 | for (i = 0; i < ctx->num_pieces; i++) | |
506 | if (ctx->pieces[i].in_reg) | |
507 | in_reg = 1; | |
508 | } | |
509 | ||
4a227398 | 510 | do_cleanups (old_chain); |
4c2df51b | 511 | |
f630a401 | 512 | return baton.needs_frame || in_reg; |
4c2df51b DJ |
513 | } |
514 | ||
0d53c4c4 | 515 | static void |
505e835d UW |
516 | dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
517 | struct agent_expr *ax, struct axs_value *value, | |
518 | gdb_byte *data, int size) | |
0d53c4c4 DJ |
519 | { |
520 | if (size == 0) | |
8a3fe4f8 | 521 | error (_("Symbol \"%s\" has been optimized out."), |
0d53c4c4 DJ |
522 | SYMBOL_PRINT_NAME (symbol)); |
523 | ||
524 | if (size == 1 | |
525 | && data[0] >= DW_OP_reg0 | |
526 | && data[0] <= DW_OP_reg31) | |
527 | { | |
528 | value->kind = axs_lvalue_register; | |
529 | value->u.reg = data[0] - DW_OP_reg0; | |
530 | } | |
531 | else if (data[0] == DW_OP_regx) | |
532 | { | |
533 | ULONGEST reg; | |
534 | read_uleb128 (data + 1, data + size, ®); | |
535 | value->kind = axs_lvalue_register; | |
536 | value->u.reg = reg; | |
537 | } | |
538 | else if (data[0] == DW_OP_fbreg) | |
539 | { | |
540 | /* And this is worse than just minimal; we should honor the frame base | |
541 | as above. */ | |
542 | int frame_reg; | |
543 | LONGEST frame_offset; | |
852483bc | 544 | gdb_byte *buf_end; |
0d53c4c4 DJ |
545 | |
546 | buf_end = read_sleb128 (data + 1, data + size, &frame_offset); | |
547 | if (buf_end != data + size) | |
8a3fe4f8 | 548 | error (_("Unexpected opcode after DW_OP_fbreg for symbol \"%s\"."), |
0d53c4c4 | 549 | SYMBOL_PRINT_NAME (symbol)); |
4c2df51b | 550 | |
505e835d | 551 | gdbarch_virtual_frame_pointer (gdbarch, |
c7bb205c | 552 | ax->scope, &frame_reg, &frame_offset); |
0d53c4c4 DJ |
553 | ax_reg (ax, frame_reg); |
554 | ax_const_l (ax, frame_offset); | |
555 | ax_simple (ax, aop_add); | |
4c2df51b | 556 | |
9c238357 RC |
557 | value->kind = axs_lvalue_memory; |
558 | } | |
559 | else if (data[0] >= DW_OP_breg0 | |
560 | && data[0] <= DW_OP_breg31) | |
561 | { | |
562 | unsigned int reg; | |
563 | LONGEST offset; | |
564 | gdb_byte *buf_end; | |
565 | ||
566 | reg = data[0] - DW_OP_breg0; | |
567 | buf_end = read_sleb128 (data + 1, data + size, &offset); | |
568 | if (buf_end != data + size) | |
569 | error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."), | |
570 | reg, SYMBOL_PRINT_NAME (symbol)); | |
571 | ||
572 | ax_reg (ax, reg); | |
573 | ax_const_l (ax, offset); | |
0d53c4c4 | 574 | ax_simple (ax, aop_add); |
9c238357 | 575 | |
0d53c4c4 DJ |
576 | value->kind = axs_lvalue_memory; |
577 | } | |
578 | else | |
9c238357 RC |
579 | error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."), |
580 | data[0], SYMBOL_PRINT_NAME (symbol)); | |
0d53c4c4 | 581 | } |
4c2df51b DJ |
582 | \f |
583 | /* Return the value of SYMBOL in FRAME using the DWARF-2 expression | |
584 | evaluator to calculate the location. */ | |
585 | static struct value * | |
586 | locexpr_read_variable (struct symbol *symbol, struct frame_info *frame) | |
587 | { | |
588 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
589 | struct value *val; | |
590 | val = dwarf2_evaluate_loc_desc (symbol, frame, dlbaton->data, dlbaton->size, | |
ae0d2f24 | 591 | dlbaton->per_cu); |
4c2df51b DJ |
592 | |
593 | return val; | |
594 | } | |
595 | ||
596 | /* Return non-zero iff we need a frame to evaluate SYMBOL. */ | |
597 | static int | |
598 | locexpr_read_needs_frame (struct symbol *symbol) | |
599 | { | |
600 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
ae0d2f24 UW |
601 | return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size, |
602 | dlbaton->per_cu); | |
4c2df51b DJ |
603 | } |
604 | ||
605 | /* Print a natural-language description of SYMBOL to STREAM. */ | |
606 | static int | |
607 | locexpr_describe_location (struct symbol *symbol, struct ui_file *stream) | |
608 | { | |
609 | /* FIXME: be more extensive. */ | |
610 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
ae0d2f24 | 611 | int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); |
4c2df51b DJ |
612 | |
613 | if (dlbaton->size == 1 | |
614 | && dlbaton->data[0] >= DW_OP_reg0 | |
615 | && dlbaton->data[0] <= DW_OP_reg31) | |
616 | { | |
5e2b427d UW |
617 | struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu); |
618 | struct gdbarch *gdbarch = get_objfile_arch (objfile); | |
619 | int regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, | |
620 | dlbaton->data[0] - DW_OP_reg0); | |
4c2df51b | 621 | fprintf_filtered (stream, |
c9f4d572 | 622 | "a variable in register %s", |
5e2b427d | 623 | gdbarch_register_name (gdbarch, regno)); |
4c2df51b DJ |
624 | return 1; |
625 | } | |
626 | ||
c3228f12 EZ |
627 | /* The location expression for a TLS variable looks like this (on a |
628 | 64-bit LE machine): | |
629 | ||
630 | DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0 | |
631 | (DW_OP_addr: 4; DW_OP_GNU_push_tls_address) | |
632 | ||
633 | 0x3 is the encoding for DW_OP_addr, which has an operand as long | |
634 | as the size of an address on the target machine (here is 8 | |
635 | bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address. | |
636 | The operand represents the offset at which the variable is within | |
637 | the thread local storage. */ | |
638 | ||
639 | if (dlbaton->size > 1 | |
640 | && dlbaton->data[dlbaton->size - 1] == DW_OP_GNU_push_tls_address) | |
641 | if (dlbaton->data[0] == DW_OP_addr) | |
642 | { | |
ae0d2f24 | 643 | struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu); |
f7fd4728 UW |
644 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
645 | CORE_ADDR offset = dwarf2_read_address (gdbarch, | |
646 | &dlbaton->data[1], | |
c193f044 | 647 | &dlbaton->data[dlbaton->size - 1], |
ae0d2f24 | 648 | addr_size); |
c3228f12 | 649 | fprintf_filtered (stream, |
32ffcbed | 650 | "a thread-local variable at offset %s in the " |
c3228f12 | 651 | "thread-local storage for `%s'", |
5af949e3 | 652 | paddress (gdbarch, offset), objfile->name); |
c3228f12 EZ |
653 | return 1; |
654 | } | |
655 | ||
656 | ||
4c2df51b DJ |
657 | fprintf_filtered (stream, |
658 | "a variable with complex or multiple locations (DWARF2)"); | |
659 | return 1; | |
660 | } | |
661 | ||
a55cc764 DJ |
662 | |
663 | /* Describe the location of SYMBOL as an agent value in VALUE, generating | |
664 | any necessary bytecode in AX. | |
665 | ||
666 | NOTE drow/2003-02-26: This function is extremely minimal, because | |
667 | doing it correctly is extremely complicated and there is no | |
668 | publicly available stub with tracepoint support for me to test | |
669 | against. When there is one this function should be revisited. */ | |
670 | ||
0d53c4c4 | 671 | static void |
505e835d UW |
672 | locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
673 | struct agent_expr *ax, struct axs_value *value) | |
a55cc764 DJ |
674 | { |
675 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
676 | ||
505e835d UW |
677 | dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, |
678 | dlbaton->data, dlbaton->size); | |
a55cc764 DJ |
679 | } |
680 | ||
4c2df51b DJ |
681 | /* The set of location functions used with the DWARF-2 expression |
682 | evaluator. */ | |
768a979c | 683 | const struct symbol_computed_ops dwarf2_locexpr_funcs = { |
4c2df51b DJ |
684 | locexpr_read_variable, |
685 | locexpr_read_needs_frame, | |
686 | locexpr_describe_location, | |
a55cc764 | 687 | locexpr_tracepoint_var_ref |
4c2df51b | 688 | }; |
0d53c4c4 DJ |
689 | |
690 | ||
691 | /* Wrapper functions for location lists. These generally find | |
692 | the appropriate location expression and call something above. */ | |
693 | ||
694 | /* Return the value of SYMBOL in FRAME using the DWARF-2 expression | |
695 | evaluator to calculate the location. */ | |
696 | static struct value * | |
697 | loclist_read_variable (struct symbol *symbol, struct frame_info *frame) | |
698 | { | |
699 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
700 | struct value *val; | |
852483bc | 701 | gdb_byte *data; |
b6b08ebf | 702 | size_t size; |
0d53c4c4 DJ |
703 | |
704 | data = find_location_expression (dlbaton, &size, | |
22c6caba JW |
705 | frame ? get_frame_address_in_block (frame) |
706 | : 0); | |
0d53c4c4 | 707 | if (data == NULL) |
806048c6 DJ |
708 | { |
709 | val = allocate_value (SYMBOL_TYPE (symbol)); | |
710 | VALUE_LVAL (val) = not_lval; | |
711 | set_value_optimized_out (val, 1); | |
712 | } | |
713 | else | |
714 | val = dwarf2_evaluate_loc_desc (symbol, frame, data, size, | |
ae0d2f24 | 715 | dlbaton->per_cu); |
0d53c4c4 DJ |
716 | |
717 | return val; | |
718 | } | |
719 | ||
720 | /* Return non-zero iff we need a frame to evaluate SYMBOL. */ | |
721 | static int | |
722 | loclist_read_needs_frame (struct symbol *symbol) | |
723 | { | |
724 | /* If there's a location list, then assume we need to have a frame | |
725 | to choose the appropriate location expression. With tracking of | |
726 | global variables this is not necessarily true, but such tracking | |
727 | is disabled in GCC at the moment until we figure out how to | |
728 | represent it. */ | |
729 | ||
730 | return 1; | |
731 | } | |
732 | ||
733 | /* Print a natural-language description of SYMBOL to STREAM. */ | |
734 | static int | |
735 | loclist_describe_location (struct symbol *symbol, struct ui_file *stream) | |
736 | { | |
737 | /* FIXME: Could print the entire list of locations. */ | |
738 | fprintf_filtered (stream, "a variable with multiple locations"); | |
739 | return 1; | |
740 | } | |
741 | ||
742 | /* Describe the location of SYMBOL as an agent value in VALUE, generating | |
743 | any necessary bytecode in AX. */ | |
744 | static void | |
505e835d UW |
745 | loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
746 | struct agent_expr *ax, struct axs_value *value) | |
0d53c4c4 DJ |
747 | { |
748 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
852483bc | 749 | gdb_byte *data; |
b6b08ebf | 750 | size_t size; |
0d53c4c4 DJ |
751 | |
752 | data = find_location_expression (dlbaton, &size, ax->scope); | |
753 | if (data == NULL) | |
8a3fe4f8 | 754 | error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol)); |
0d53c4c4 | 755 | |
505e835d | 756 | dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size); |
0d53c4c4 DJ |
757 | } |
758 | ||
759 | /* The set of location functions used with the DWARF-2 expression | |
760 | evaluator and location lists. */ | |
768a979c | 761 | const struct symbol_computed_ops dwarf2_loclist_funcs = { |
0d53c4c4 DJ |
762 | loclist_read_variable, |
763 | loclist_read_needs_frame, | |
764 | loclist_describe_location, | |
765 | loclist_tracepoint_var_ref | |
766 | }; |