2009-12-23 Stan Shebs <stan@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3 Copyright (C) 2003, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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"
29 #include "ax.h"
30 #include "ax-gdb.h"
31 #include "regcache.h"
32 #include "objfiles.h"
33 #include "exceptions.h"
34 #include "block.h"
35
36 #include "dwarf2.h"
37 #include "dwarf2expr.h"
38 #include "dwarf2loc.h"
39 #include "dwarf2-frame.h"
40
41 #include "gdb_string.h"
42 #include "gdb_assert.h"
43
44 static void
45 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
46 gdb_byte **start, size_t *length);
47
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
56 static gdb_byte *
57 find_location_expression (struct dwarf2_loclist_baton *baton,
58 size_t *locexpr_length, CORE_ADDR pc)
59 {
60 CORE_ADDR low, high;
61 gdb_byte *loc_ptr, *buf_end;
62 int length;
63 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
64 struct gdbarch *gdbarch = get_objfile_arch (objfile);
65 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
66 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
67 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
68 /* Adjust base_address for relocatable objects. */
69 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
70 SECT_OFF_TEXT (objfile));
71 CORE_ADDR base_address = baton->base_address + base_offset;
72
73 loc_ptr = baton->data;
74 buf_end = baton->data + baton->size;
75
76 while (1)
77 {
78 if (buf_end - loc_ptr < 2 * addr_size)
79 error (_("find_location_expression: Corrupted DWARF expression."));
80
81 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
82 loc_ptr += addr_size;
83
84 /* A base-address-selection entry. */
85 if (low == base_mask)
86 {
87 base_address = dwarf2_read_address (gdbarch,
88 loc_ptr, buf_end, addr_size);
89 loc_ptr += addr_size;
90 continue;
91 }
92
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
100 /* Otherwise, a location expression entry. */
101 low += base_address;
102 high += base_address;
103
104 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
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
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
127 /* Using the frame specified in BATON, return the value of register
128 REGNUM, treated as a pointer. */
129 static CORE_ADDR
130 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
131 {
132 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
133 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
134 CORE_ADDR result;
135 int regnum;
136
137 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
138 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
139 regnum, debaton->frame);
140 return result;
141 }
142
143 /* Read memory at ADDR (length LEN) into BUF. */
144
145 static void
146 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
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
155 dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length)
156 {
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. */
160 struct symbol *framefunc;
161 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
162
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));
167
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
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 {
182 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
183 *start = NULL;
184 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
185 {
186 struct dwarf2_loclist_baton *symbaton;
187
188 symbaton = SYMBOL_LOCATION_BATON (framefunc);
189 *start = find_location_expression (symbaton, length, pc);
190 }
191 else
192 {
193 struct dwarf2_locexpr_baton *symbaton;
194 symbaton = SYMBOL_LOCATION_BATON (framefunc);
195 if (symbaton != NULL)
196 {
197 *length = symbaton->size;
198 *start = symbaton->data;
199 }
200 else
201 *start = NULL;
202 }
203
204 if (*start == NULL)
205 error (_("Could not find the frame base for \"%s\"."),
206 SYMBOL_NATURAL_NAME (framefunc));
207 }
208
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
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;
225
226 return target_translate_tls_address (debaton->objfile, offset);
227 }
228
229 struct piece_closure
230 {
231 /* The number of pieces used to describe this variable. */
232 int n_pieces;
233
234 /* The architecture, used only for DWARF_VALUE_STACK. */
235 struct gdbarch *arch;
236
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 *
245 allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
246 struct gdbarch *arch)
247 {
248 struct piece_closure *c = XZALLOC (struct piece_closure);
249
250 c->n_pieces = n_pieces;
251 c->arch = arch;
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];
272 switch (p->location)
273 {
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,
279 p->v.expr.value);
280 get_frame_register (frame, gdb_regnum, regval);
281 memcpy (contents + offset, regval, p->size);
282 }
283 break;
284
285 case DWARF_VALUE_MEMORY:
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);
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),
299 p->v.expr.value);
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"));
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];
342 switch (p->location)
343 {
344 case DWARF_VALUE_REGISTER:
345 {
346 struct gdbarch *arch = get_frame_arch (frame);
347 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
348 put_frame_register (frame, gdb_regnum, contents + offset);
349 }
350 break;
351 case DWARF_VALUE_MEMORY:
352 write_memory (p->v.expr.value, contents + offset, p->size);
353 break;
354 default:
355 set_value_optimized_out (to, 1);
356 return;
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
367 return allocate_piece_closure (c->n_pieces, c->pieces, c->arch);
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
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,
392 gdb_byte *data, unsigned short size,
393 struct dwarf2_per_cu_data *per_cu)
394 {
395 struct value *retval;
396 struct dwarf_expr_baton baton;
397 struct dwarf_expr_context *ctx;
398 struct cleanup *old_chain;
399
400 if (size == 0)
401 {
402 retval = allocate_value (SYMBOL_TYPE (var));
403 VALUE_LVAL (retval) = not_lval;
404 set_value_optimized_out (retval, 1);
405 return retval;
406 }
407
408 baton.frame = frame;
409 baton.objfile = dwarf2_per_cu_objfile (per_cu);
410
411 ctx = new_dwarf_expr_context ();
412 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
413
414 ctx->gdbarch = get_objfile_arch (baton.objfile);
415 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
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;
420 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
421 ctx->get_tls_address = dwarf_expr_tls_address;
422
423 dwarf_expr_eval (ctx, data, size);
424 if (ctx->num_pieces > 0)
425 {
426 struct piece_closure *c;
427 struct frame_id frame_id = get_frame_id (frame);
428
429 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces, ctx->gdbarch);
430 retval = allocate_computed_value (SYMBOL_TYPE (var),
431 &pieced_value_funcs,
432 c);
433 VALUE_FRAME_ID (retval) = frame_id;
434 }
435 else
436 {
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);
451 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
452
453 retval = allocate_value (SYMBOL_TYPE (var));
454 VALUE_LVAL (retval) = lval_memory;
455 set_value_lazy (retval, 1);
456 if (in_stack_memory)
457 set_value_stack (retval, 1);
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 }
496 }
497
498 set_value_initialized (retval, ctx->initialized);
499
500 do_cleanups (old_chain);
501
502 return retval;
503 }
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
514 needs_frame_read_reg (void *baton, int regnum)
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
523 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
524 {
525 memset (buf, 0, len);
526 }
527
528 /* Frame-relative accesses do require a frame. */
529 static void
530 needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length)
531 {
532 static gdb_byte lit0 = DW_OP_lit0;
533 struct needs_frame_baton *nf_baton = baton;
534
535 *start = &lit0;
536 *length = 1;
537
538 nf_baton->needs_frame = 1;
539 }
540
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
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
564 dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size,
565 struct dwarf2_per_cu_data *per_cu)
566 {
567 struct needs_frame_baton baton;
568 struct dwarf_expr_context *ctx;
569 int in_reg;
570 struct cleanup *old_chain;
571
572 baton.needs_frame = 0;
573
574 ctx = new_dwarf_expr_context ();
575 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
576
577 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
578 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
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;
583 ctx->get_frame_cfa = needs_frame_frame_cfa;
584 ctx->get_tls_address = needs_frame_tls_address;
585
586 dwarf_expr_eval (ctx, data, size);
587
588 in_reg = ctx->location == DWARF_VALUE_REGISTER;
589
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++)
597 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
598 in_reg = 1;
599 }
600
601 do_cleanups (old_chain);
602
603 return baton.needs_frame || in_reg;
604 }
605
606 static void
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)
610 {
611 if (size == 0)
612 error (_("Symbol \"%s\" has been optimized out."),
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, &reg);
626 value->kind = axs_lvalue_register;
627 value->u.reg = reg;
628 }
629 else if (data[0] == DW_OP_fbreg)
630 {
631 struct block *b;
632 struct symbol *framefunc;
633 int frame_reg = 0;
634 LONGEST frame_offset;
635 gdb_byte *buf_end;
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 }
669
670 buf_end = read_sleb128 (data + 1, data + size, &frame_offset);
671 if (buf_end != data + size)
672 error (_("Unexpected opcode after DW_OP_fbreg for symbol \"%s\"."),
673 SYMBOL_PRINT_NAME (symbol));
674
675 ax_reg (ax, frame_reg);
676 ax_const_l (ax, base_offset + frame_offset);
677 ax_simple (ax, aop_add);
678
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);
696 ax_simple (ax, aop_add);
697
698 value->kind = axs_lvalue_memory;
699 }
700 else
701 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
702 data[0], SYMBOL_PRINT_NAME (symbol));
703 }
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,
713 dlbaton->per_cu);
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);
723 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
724 dlbaton->per_cu);
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);
733 int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
734
735 if (dlbaton->size == 1
736 && dlbaton->data[0] >= DW_OP_reg0
737 && dlbaton->data[0] <= DW_OP_reg31)
738 {
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);
743 fprintf_filtered (stream,
744 "a variable in register %s",
745 gdbarch_register_name (gdbarch, regno));
746 return 1;
747 }
748
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 {
765 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
766 struct gdbarch *gdbarch = get_objfile_arch (objfile);
767 CORE_ADDR offset = dwarf2_read_address (gdbarch,
768 &dlbaton->data[1],
769 &dlbaton->data[dlbaton->size - 1],
770 addr_size);
771 fprintf_filtered (stream,
772 "a thread-local variable at offset %s in the "
773 "thread-local storage for `%s'",
774 paddress (gdbarch, offset), objfile->name);
775 return 1;
776 }
777
778
779 fprintf_filtered (stream,
780 "a variable with complex or multiple locations (DWARF2)");
781 return 1;
782 }
783
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
793 static void
794 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
795 struct agent_expr *ax, struct axs_value *value)
796 {
797 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
798
799 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
800 dlbaton->data, dlbaton->size);
801 }
802
803 /* The set of location functions used with the DWARF-2 expression
804 evaluator. */
805 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
806 locexpr_read_variable,
807 locexpr_read_needs_frame,
808 locexpr_describe_location,
809 locexpr_tracepoint_var_ref
810 };
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;
823 gdb_byte *data;
824 size_t size;
825
826 data = find_location_expression (dlbaton, &size,
827 frame ? get_frame_address_in_block (frame)
828 : 0);
829 if (data == NULL)
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,
837 dlbaton->per_cu);
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
867 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
868 struct agent_expr *ax, struct axs_value *value)
869 {
870 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
871 gdb_byte *data;
872 size_t size;
873
874 data = find_location_expression (dlbaton, &size, ax->scope);
875 if (data == NULL)
876 error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol));
877
878 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
879 }
880
881 /* The set of location functions used with the DWARF-2 expression
882 evaluator and location lists. */
883 const struct symbol_computed_ops dwarf2_loclist_funcs = {
884 loclist_read_variable,
885 loclist_read_needs_frame,
886 loclist_describe_location,
887 loclist_tracepoint_var_ref
888 };
This page took 0.052223 seconds and 5 git commands to generate.