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