* linux-m68k-low.c: Include <asm/ptrace.h>
[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
196 symbaton = SYMBOL_LOCATION_BATON (framefunc);
197 if (symbaton != NULL)
198 {
199 *length = symbaton->size;
200 *start = symbaton->data;
201 }
202 else
203 *start = NULL;
204 }
205
206 if (*start == NULL)
207 error (_("Could not find the frame base for \"%s\"."),
208 SYMBOL_NATURAL_NAME (framefunc));
209 }
210
211 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
212 the frame in BATON. */
213
214 static CORE_ADDR
215 dwarf_expr_frame_cfa (void *baton)
216 {
217 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
218
219 return dwarf2_frame_cfa (debaton->frame);
220 }
221
222 /* Using the objfile specified in BATON, find the address for the
223 current thread's thread-local storage with offset OFFSET. */
224 static CORE_ADDR
225 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
226 {
227 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
228
229 return target_translate_tls_address (debaton->objfile, offset);
230 }
231
232 struct piece_closure
233 {
234 /* The number of pieces used to describe this variable. */
235 int n_pieces;
236
237 /* The target address size, used only for DWARF_VALUE_STACK. */
238 int addr_size;
239
240 /* The pieces themselves. */
241 struct dwarf_expr_piece *pieces;
242 };
243
244 /* Allocate a closure for a value formed from separately-described
245 PIECES. */
246
247 static struct piece_closure *
248 allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
249 int addr_size)
250 {
251 struct piece_closure *c = XZALLOC (struct piece_closure);
252
253 c->n_pieces = n_pieces;
254 c->addr_size = addr_size;
255 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
256
257 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
258
259 return c;
260 }
261
262 static void
263 read_pieced_value (struct value *v)
264 {
265 int i;
266 long offset = 0;
267 gdb_byte *contents;
268 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
269 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
270
271 contents = value_contents_raw (v);
272 for (i = 0; i < c->n_pieces; i++)
273 {
274 struct dwarf_expr_piece *p = &c->pieces[i];
275
276 switch (p->location)
277 {
278 case DWARF_VALUE_REGISTER:
279 {
280 struct gdbarch *arch = get_frame_arch (frame);
281 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
282 p->v.expr.value);
283 int reg_offset = 0;
284
285 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
286 && p->size < register_size (arch, gdb_regnum))
287 /* Big-endian, and we want less than full size. */
288 reg_offset = register_size (arch, gdb_regnum) - p->size;
289
290 if (gdb_regnum != -1)
291 {
292 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
293 p->size, contents + offset);
294 }
295 else
296 {
297 error (_("Unable to access DWARF register number %s"),
298 paddress (arch, p->v.expr.value));
299 }
300 }
301 break;
302
303 case DWARF_VALUE_MEMORY:
304 if (p->v.expr.in_stack_memory)
305 read_stack (p->v.expr.value, contents + offset, p->size);
306 else
307 read_memory (p->v.expr.value, contents + offset, p->size);
308 break;
309
310 case DWARF_VALUE_STACK:
311 {
312 struct gdbarch *gdbarch = get_type_arch (value_type (v));
313 size_t n = p->size;
314
315 if (n > c->addr_size)
316 n = c->addr_size;
317 store_unsigned_integer (contents + offset, n,
318 gdbarch_byte_order (gdbarch),
319 p->v.expr.value);
320 }
321 break;
322
323 case DWARF_VALUE_LITERAL:
324 {
325 size_t n = p->size;
326
327 if (n > p->v.literal.length)
328 n = p->v.literal.length;
329 memcpy (contents + offset, p->v.literal.data, n);
330 }
331 break;
332
333 default:
334 internal_error (__FILE__, __LINE__, _("invalid location type"));
335 }
336 offset += p->size;
337 }
338 }
339
340 static void
341 write_pieced_value (struct value *to, struct value *from)
342 {
343 int i;
344 long offset = 0;
345 gdb_byte *contents;
346 struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
347 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
348
349 if (frame == NULL)
350 {
351 set_value_optimized_out (to, 1);
352 return;
353 }
354
355 contents = value_contents_raw (from);
356 for (i = 0; i < c->n_pieces; i++)
357 {
358 struct dwarf_expr_piece *p = &c->pieces[i];
359
360 switch (p->location)
361 {
362 case DWARF_VALUE_REGISTER:
363 {
364 struct gdbarch *arch = get_frame_arch (frame);
365 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
366 int reg_offset = 0;
367
368 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
369 && p->size < register_size (arch, gdb_regnum))
370 /* Big-endian, and we want less than full size. */
371 reg_offset = register_size (arch, gdb_regnum) - p->size;
372
373 if (gdb_regnum != -1)
374 {
375 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
376 p->size, contents + offset);
377 }
378 else
379 {
380 error (_("Unable to write to DWARF register number %s"),
381 paddress (arch, p->v.expr.value));
382 }
383 }
384 break;
385 case DWARF_VALUE_MEMORY:
386 write_memory (p->v.expr.value, contents + offset, p->size);
387 break;
388 default:
389 set_value_optimized_out (to, 1);
390 return;
391 }
392 offset += p->size;
393 }
394 }
395
396 static void *
397 copy_pieced_value_closure (struct value *v)
398 {
399 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
400
401 return allocate_piece_closure (c->n_pieces, c->pieces, c->addr_size);
402 }
403
404 static void
405 free_pieced_value_closure (struct value *v)
406 {
407 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
408
409 xfree (c->pieces);
410 xfree (c);
411 }
412
413 /* Functions for accessing a variable described by DW_OP_piece. */
414 static struct lval_funcs pieced_value_funcs = {
415 read_pieced_value,
416 write_pieced_value,
417 copy_pieced_value_closure,
418 free_pieced_value_closure
419 };
420
421 /* Evaluate a location description, starting at DATA and with length
422 SIZE, to find the current location of variable of TYPE in the context
423 of FRAME. */
424
425 static struct value *
426 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
427 gdb_byte *data, unsigned short size,
428 struct dwarf2_per_cu_data *per_cu)
429 {
430 struct value *retval;
431 struct dwarf_expr_baton baton;
432 struct dwarf_expr_context *ctx;
433 struct cleanup *old_chain;
434
435 if (size == 0)
436 {
437 retval = allocate_value (type);
438 VALUE_LVAL (retval) = not_lval;
439 set_value_optimized_out (retval, 1);
440 return retval;
441 }
442
443 baton.frame = frame;
444 baton.objfile = dwarf2_per_cu_objfile (per_cu);
445
446 ctx = new_dwarf_expr_context ();
447 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
448
449 ctx->gdbarch = get_objfile_arch (baton.objfile);
450 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
451 ctx->baton = &baton;
452 ctx->read_reg = dwarf_expr_read_reg;
453 ctx->read_mem = dwarf_expr_read_mem;
454 ctx->get_frame_base = dwarf_expr_frame_base;
455 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
456 ctx->get_tls_address = dwarf_expr_tls_address;
457
458 dwarf_expr_eval (ctx, data, size);
459 if (ctx->num_pieces > 0)
460 {
461 struct piece_closure *c;
462 struct frame_id frame_id = get_frame_id (frame);
463
464 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
465 ctx->addr_size);
466 retval = allocate_computed_value (type, &pieced_value_funcs, c);
467 VALUE_FRAME_ID (retval) = frame_id;
468 }
469 else
470 {
471 switch (ctx->location)
472 {
473 case DWARF_VALUE_REGISTER:
474 {
475 struct gdbarch *arch = get_frame_arch (frame);
476 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
477 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
478
479 if (gdb_regnum != -1)
480 retval = value_from_register (type, gdb_regnum, frame);
481 else
482 error (_("Unable to access DWARF register number %s"),
483 paddress (arch, dwarf_regnum));
484 }
485 break;
486
487 case DWARF_VALUE_MEMORY:
488 {
489 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
490 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
491
492 retval = allocate_value (type);
493 VALUE_LVAL (retval) = lval_memory;
494 set_value_lazy (retval, 1);
495 if (in_stack_memory)
496 set_value_stack (retval, 1);
497 set_value_address (retval, address);
498 }
499 break;
500
501 case DWARF_VALUE_STACK:
502 {
503 ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
504 bfd_byte *contents;
505 size_t n = ctx->addr_size;
506
507 retval = allocate_value (type);
508 contents = value_contents_raw (retval);
509 if (n > TYPE_LENGTH (type))
510 n = TYPE_LENGTH (type);
511 store_unsigned_integer (contents, n,
512 gdbarch_byte_order (ctx->gdbarch),
513 value);
514 }
515 break;
516
517 case DWARF_VALUE_LITERAL:
518 {
519 bfd_byte *contents;
520 size_t n = ctx->len;
521
522 retval = allocate_value (type);
523 contents = value_contents_raw (retval);
524 if (n > TYPE_LENGTH (type))
525 n = TYPE_LENGTH (type);
526 memcpy (contents, ctx->data, n);
527 }
528 break;
529
530 default:
531 internal_error (__FILE__, __LINE__, _("invalid location type"));
532 }
533 }
534
535 set_value_initialized (retval, ctx->initialized);
536
537 do_cleanups (old_chain);
538
539 return retval;
540 }
541 \f
542 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
543
544 struct needs_frame_baton
545 {
546 int needs_frame;
547 };
548
549 /* Reads from registers do require a frame. */
550 static CORE_ADDR
551 needs_frame_read_reg (void *baton, int regnum)
552 {
553 struct needs_frame_baton *nf_baton = baton;
554
555 nf_baton->needs_frame = 1;
556 return 1;
557 }
558
559 /* Reads from memory do not require a frame. */
560 static void
561 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
562 {
563 memset (buf, 0, len);
564 }
565
566 /* Frame-relative accesses do require a frame. */
567 static void
568 needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length)
569 {
570 static gdb_byte lit0 = DW_OP_lit0;
571 struct needs_frame_baton *nf_baton = baton;
572
573 *start = &lit0;
574 *length = 1;
575
576 nf_baton->needs_frame = 1;
577 }
578
579 /* CFA accesses require a frame. */
580
581 static CORE_ADDR
582 needs_frame_frame_cfa (void *baton)
583 {
584 struct needs_frame_baton *nf_baton = baton;
585
586 nf_baton->needs_frame = 1;
587 return 1;
588 }
589
590 /* Thread-local accesses do require a frame. */
591 static CORE_ADDR
592 needs_frame_tls_address (void *baton, CORE_ADDR offset)
593 {
594 struct needs_frame_baton *nf_baton = baton;
595
596 nf_baton->needs_frame = 1;
597 return 1;
598 }
599
600 /* Return non-zero iff the location expression at DATA (length SIZE)
601 requires a frame to evaluate. */
602
603 static int
604 dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size,
605 struct dwarf2_per_cu_data *per_cu)
606 {
607 struct needs_frame_baton baton;
608 struct dwarf_expr_context *ctx;
609 int in_reg;
610 struct cleanup *old_chain;
611
612 baton.needs_frame = 0;
613
614 ctx = new_dwarf_expr_context ();
615 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
616
617 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
618 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
619 ctx->baton = &baton;
620 ctx->read_reg = needs_frame_read_reg;
621 ctx->read_mem = needs_frame_read_mem;
622 ctx->get_frame_base = needs_frame_frame_base;
623 ctx->get_frame_cfa = needs_frame_frame_cfa;
624 ctx->get_tls_address = needs_frame_tls_address;
625
626 dwarf_expr_eval (ctx, data, size);
627
628 in_reg = ctx->location == DWARF_VALUE_REGISTER;
629
630 if (ctx->num_pieces > 0)
631 {
632 int i;
633
634 /* If the location has several pieces, and any of them are in
635 registers, then we will need a frame to fetch them from. */
636 for (i = 0; i < ctx->num_pieces; i++)
637 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
638 in_reg = 1;
639 }
640
641 do_cleanups (old_chain);
642
643 return baton.needs_frame || in_reg;
644 }
645
646 /* This struct keeps track of the pieces that make up a multi-location
647 object, for use in agent expression generation. It is
648 superficially similar to struct dwarf_expr_piece, but
649 dwarf_expr_piece is designed for use in immediate evaluation, and
650 does not, for example, have a way to record both base register and
651 offset. */
652
653 struct axs_var_loc
654 {
655 /* Memory vs register, etc */
656 enum axs_lvalue_kind kind;
657
658 /* If non-zero, number of bytes in this fragment */
659 unsigned bytes;
660
661 /* (GDB-numbered) reg, or base reg if >= 0 */
662 int reg;
663
664 /* offset from reg */
665 LONGEST offset;
666 };
667
668 static gdb_byte *
669 dwarf2_tracepoint_var_loc (struct symbol *symbol,
670 struct agent_expr *ax,
671 struct axs_var_loc *loc,
672 struct gdbarch *gdbarch,
673 gdb_byte *data, gdb_byte *end)
674 {
675 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
676 {
677 loc->kind = axs_lvalue_register;
678 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
679 data += 1;
680 }
681 else if (data[0] == DW_OP_regx)
682 {
683 ULONGEST reg;
684
685 data = read_uleb128 (data + 1, end, &reg);
686 loc->kind = axs_lvalue_register;
687 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
688 }
689 else if (data[0] == DW_OP_fbreg)
690 {
691 struct block *b;
692 struct symbol *framefunc;
693 int frame_reg = 0;
694 LONGEST frame_offset;
695 gdb_byte *base_data;
696 size_t base_size;
697 LONGEST base_offset = 0;
698
699 b = block_for_pc (ax->scope);
700
701 if (!b)
702 error (_("No block found for address"));
703
704 framefunc = block_linkage_function (b);
705
706 if (!framefunc)
707 error (_("No function found for block"));
708
709 dwarf_expr_frame_base_1 (framefunc, ax->scope,
710 &base_data, &base_size);
711
712 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
713 {
714 gdb_byte *buf_end;
715
716 frame_reg = base_data[0] - DW_OP_breg0;
717 buf_end = read_sleb128 (base_data + 1,
718 base_data + base_size, &base_offset);
719 if (buf_end != base_data + base_size)
720 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
721 frame_reg, SYMBOL_PRINT_NAME (symbol));
722 }
723 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
724 {
725 /* The frame base is just the register, with no offset. */
726 frame_reg = base_data[0] - DW_OP_reg0;
727 base_offset = 0;
728 }
729 else
730 {
731 /* We don't know what to do with the frame base expression,
732 so we can't trace this variable; give up. */
733 error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
734 SYMBOL_PRINT_NAME (symbol), base_data[0]);
735 }
736
737 data = read_sleb128 (data + 1, end, &frame_offset);
738
739 loc->kind = axs_lvalue_memory;
740 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
741 loc->offset = base_offset + frame_offset;
742 }
743 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
744 {
745 unsigned int reg;
746 LONGEST offset;
747
748 reg = data[0] - DW_OP_breg0;
749 data = read_sleb128 (data + 1, end, &offset);
750
751 loc->kind = axs_lvalue_memory;
752 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
753 loc->offset = offset;
754 }
755 else
756 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
757 data[0], SYMBOL_PRINT_NAME (symbol));
758
759 return data;
760 }
761
762 /* Given the location of a piece, issue bytecodes that will access it. */
763
764 static void
765 dwarf2_tracepoint_var_access (struct agent_expr *ax,
766 struct axs_value *value,
767 struct axs_var_loc *loc)
768 {
769 value->kind = loc->kind;
770
771 switch (loc->kind)
772 {
773 case axs_lvalue_register:
774 value->u.reg = loc->reg;
775 break;
776
777 case axs_lvalue_memory:
778 ax_reg (ax, loc->reg);
779 if (loc->offset)
780 {
781 ax_const_l (ax, loc->offset);
782 ax_simple (ax, aop_add);
783 }
784 break;
785
786 default:
787 internal_error (__FILE__, __LINE__, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
788 }
789 }
790
791 static void
792 dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
793 struct agent_expr *ax, struct axs_value *value,
794 gdb_byte *data, int size)
795 {
796 gdb_byte *end = data + size;
797 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
798 /* In practice, a variable is not going to be spread across
799 dozens of registers or memory locations. If someone comes up
800 with a real-world example, revisit this. */
801 #define MAX_FRAGS 16
802 struct axs_var_loc fragments[MAX_FRAGS];
803 int nfrags = 0, frag;
804 int length = 0;
805 int piece_ok = 0;
806 int bad = 0;
807 int first = 1;
808
809 if (!data || size == 0)
810 {
811 value->optimized_out = 1;
812 return;
813 }
814
815 while (data < end)
816 {
817 if (!piece_ok)
818 {
819 if (nfrags == MAX_FRAGS)
820 error (_("Too many pieces in location for \"%s\"."),
821 SYMBOL_PRINT_NAME (symbol));
822
823 fragments[nfrags].bytes = 0;
824 data = dwarf2_tracepoint_var_loc (symbol, ax, &fragments[nfrags],
825 gdbarch, data, end);
826 nfrags++;
827 piece_ok = 1;
828 }
829 else if (data[0] == DW_OP_piece)
830 {
831 ULONGEST bytes;
832
833 data = read_uleb128 (data + 1, end, &bytes);
834 /* Only deal with 4 byte fragments for now. */
835 if (bytes != 4)
836 error (_("DW_OP_piece %s not supported in location for \"%s\"."),
837 pulongest (bytes), SYMBOL_PRINT_NAME (symbol));
838 fragments[nfrags - 1].bytes = bytes;
839 length += bytes;
840 piece_ok = 0;
841 }
842 else
843 {
844 bad = 1;
845 break;
846 }
847 }
848
849 if (bad || data > end)
850 error (_("Corrupted DWARF expression for \"%s\"."),
851 SYMBOL_PRINT_NAME (symbol));
852
853 /* If single expression, no pieces, convert to external format. */
854 if (length == 0)
855 {
856 dwarf2_tracepoint_var_access (ax, value, &fragments[0]);
857 return;
858 }
859
860 if (length != TYPE_LENGTH (value->type))
861 error (_("Inconsistent piece information for \"%s\"."),
862 SYMBOL_PRINT_NAME (symbol));
863
864 /* Emit bytecodes to assemble the pieces into a single stack entry. */
865
866 for ((frag = (byte_order == BFD_ENDIAN_BIG ? 0 : nfrags - 1));
867 nfrags--;
868 (frag += (byte_order == BFD_ENDIAN_BIG ? 1 : -1)))
869 {
870 if (!first)
871 {
872 /* shift the previous fragment up 32 bits */
873 ax_const_l (ax, 32);
874 ax_simple (ax, aop_lsh);
875 }
876
877 dwarf2_tracepoint_var_access (ax, value, &fragments[frag]);
878
879 switch (value->kind)
880 {
881 case axs_lvalue_register:
882 ax_reg (ax, value->u.reg);
883 break;
884
885 case axs_lvalue_memory:
886 {
887 extern int trace_kludge; /* Ugh. */
888
889 gdb_assert (fragments[frag].bytes == 4);
890 if (trace_kludge)
891 ax_trace_quick (ax, 4);
892 ax_simple (ax, aop_ref32);
893 }
894 break;
895 }
896
897 if (!first)
898 {
899 /* or the new fragment into the previous */
900 ax_zero_ext (ax, 32);
901 ax_simple (ax, aop_bit_or);
902 }
903 first = 0;
904 }
905 value->kind = axs_rvalue;
906 }
907
908 \f
909 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
910 evaluator to calculate the location. */
911 static struct value *
912 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
913 {
914 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
915 struct value *val;
916
917 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
918 dlbaton->size, dlbaton->per_cu);
919
920 return val;
921 }
922
923 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
924 static int
925 locexpr_read_needs_frame (struct symbol *symbol)
926 {
927 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
928
929 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
930 dlbaton->per_cu);
931 }
932
933 /* Describe a single piece of a location, returning an updated
934 position in the bytecode sequence. */
935
936 static gdb_byte *
937 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
938 CORE_ADDR addr, struct objfile *objfile,
939 gdb_byte *data, int size, unsigned int addr_size)
940 {
941 struct gdbarch *gdbarch = get_objfile_arch (objfile);
942 int regno;
943
944 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
945 {
946 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
947 fprintf_filtered (stream, _("a variable in $%s"),
948 gdbarch_register_name (gdbarch, regno));
949 data += 1;
950 }
951 else if (data[0] == DW_OP_regx)
952 {
953 ULONGEST reg;
954
955 data = read_uleb128 (data + 1, data + size, &reg);
956 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
957 fprintf_filtered (stream, _("a variable in $%s"),
958 gdbarch_register_name (gdbarch, regno));
959 }
960 else if (data[0] == DW_OP_fbreg)
961 {
962 struct block *b;
963 struct symbol *framefunc;
964 int frame_reg = 0;
965 LONGEST frame_offset;
966 gdb_byte *base_data;
967 size_t base_size;
968 LONGEST base_offset = 0;
969
970 b = block_for_pc (addr);
971
972 if (!b)
973 error (_("No block found for address for symbol \"%s\"."),
974 SYMBOL_PRINT_NAME (symbol));
975
976 framefunc = block_linkage_function (b);
977
978 if (!framefunc)
979 error (_("No function found for block for symbol \"%s\"."),
980 SYMBOL_PRINT_NAME (symbol));
981
982 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
983
984 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
985 {
986 gdb_byte *buf_end;
987
988 frame_reg = base_data[0] - DW_OP_breg0;
989 buf_end = read_sleb128 (base_data + 1,
990 base_data + base_size, &base_offset);
991 if (buf_end != base_data + base_size)
992 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
993 frame_reg, SYMBOL_PRINT_NAME (symbol));
994 }
995 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
996 {
997 /* The frame base is just the register, with no offset. */
998 frame_reg = base_data[0] - DW_OP_reg0;
999 base_offset = 0;
1000 }
1001 else
1002 {
1003 /* We don't know what to do with the frame base expression,
1004 so we can't trace this variable; give up. */
1005 error (_("Cannot describe location of symbol \"%s\"; "
1006 "DWARF 2 encoding not handled, "
1007 "first opcode in base data is 0x%x."),
1008 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1009 }
1010
1011 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1012
1013 data = read_sleb128 (data + 1, data + size, &frame_offset);
1014
1015 fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1016 gdbarch_register_name (gdbarch, regno),
1017 plongest (base_offset), plongest (frame_offset));
1018 }
1019 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
1020 {
1021 LONGEST offset;
1022
1023 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1024
1025 data = read_sleb128 (data + 1, data + size, &offset);
1026
1027 fprintf_filtered (stream,
1028 _("a variable at offset %s from base reg $%s"),
1029 plongest (offset),
1030 gdbarch_register_name (gdbarch, regno));
1031 }
1032
1033 /* The location expression for a TLS variable looks like this (on a
1034 64-bit LE machine):
1035
1036 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1037 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1038
1039 0x3 is the encoding for DW_OP_addr, which has an operand as long
1040 as the size of an address on the target machine (here is 8
1041 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1042 The operand represents the offset at which the variable is within
1043 the thread local storage. */
1044
1045 else if (size > 1
1046 && data[size - 1] == DW_OP_GNU_push_tls_address
1047 && data[0] == DW_OP_addr)
1048 {
1049 CORE_ADDR offset = dwarf2_read_address (gdbarch,
1050 data + 1,
1051 data + size - 1,
1052 addr_size);
1053
1054 fprintf_filtered (stream,
1055 _("a thread-local variable at offset %s "
1056 "in the thread-local storage for `%s'"),
1057 paddress (gdbarch, offset), objfile->name);
1058
1059 data += 1 + addr_size + 1;
1060 }
1061 else
1062 fprintf_filtered (stream,
1063 _("a variable with complex or multiple locations (DWARF2)"));
1064
1065 return data;
1066 }
1067
1068 /* Describe a single location, which may in turn consist of multiple
1069 pieces. */
1070
1071 static void
1072 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
1073 struct ui_file *stream, gdb_byte *data, int size,
1074 struct objfile *objfile, unsigned int addr_size)
1075 {
1076 gdb_byte *end = data + size;
1077 int piece_done = 0, first_piece = 1, bad = 0;
1078
1079 /* A multi-piece description consists of multiple sequences of bytes
1080 each followed by DW_OP_piece + length of piece. */
1081 while (data < end)
1082 {
1083 if (!piece_done)
1084 {
1085 if (first_piece)
1086 first_piece = 0;
1087 else
1088 fprintf_filtered (stream, _(", and "));
1089
1090 data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
1091 data, size, addr_size);
1092 piece_done = 1;
1093 }
1094 else if (data[0] == DW_OP_piece)
1095 {
1096 ULONGEST bytes;
1097
1098 data = read_uleb128 (data + 1, end, &bytes);
1099
1100 fprintf_filtered (stream, _(" [%s-byte piece]"), pulongest (bytes));
1101
1102 piece_done = 0;
1103 }
1104 else
1105 {
1106 bad = 1;
1107 break;
1108 }
1109 }
1110
1111 if (bad || data > end)
1112 error (_("Corrupted DWARF2 expression for \"%s\"."),
1113 SYMBOL_PRINT_NAME (symbol));
1114 }
1115
1116 /* Print a natural-language description of SYMBOL to STREAM. This
1117 version is for a symbol with a single location. */
1118
1119 static void
1120 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
1121 struct ui_file *stream)
1122 {
1123 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1124 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1125 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1126
1127 locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
1128 objfile, addr_size);
1129 }
1130
1131 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1132 any necessary bytecode in AX. */
1133
1134 static void
1135 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1136 struct agent_expr *ax, struct axs_value *value)
1137 {
1138 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1139
1140 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
1141 dlbaton->data, dlbaton->size);
1142 }
1143
1144 /* The set of location functions used with the DWARF-2 expression
1145 evaluator. */
1146 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
1147 locexpr_read_variable,
1148 locexpr_read_needs_frame,
1149 locexpr_describe_location,
1150 locexpr_tracepoint_var_ref
1151 };
1152
1153
1154 /* Wrapper functions for location lists. These generally find
1155 the appropriate location expression and call something above. */
1156
1157 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1158 evaluator to calculate the location. */
1159 static struct value *
1160 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
1161 {
1162 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1163 struct value *val;
1164 gdb_byte *data;
1165 size_t size;
1166
1167 data = find_location_expression (dlbaton, &size,
1168 frame ? get_frame_address_in_block (frame)
1169 : 0);
1170 if (data == NULL)
1171 {
1172 val = allocate_value (SYMBOL_TYPE (symbol));
1173 VALUE_LVAL (val) = not_lval;
1174 set_value_optimized_out (val, 1);
1175 }
1176 else
1177 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
1178 dlbaton->per_cu);
1179
1180 return val;
1181 }
1182
1183 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1184 static int
1185 loclist_read_needs_frame (struct symbol *symbol)
1186 {
1187 /* If there's a location list, then assume we need to have a frame
1188 to choose the appropriate location expression. With tracking of
1189 global variables this is not necessarily true, but such tracking
1190 is disabled in GCC at the moment until we figure out how to
1191 represent it. */
1192
1193 return 1;
1194 }
1195
1196 /* Print a natural-language description of SYMBOL to STREAM. This
1197 version applies when there is a list of different locations, each
1198 with a specified address range. */
1199
1200 static void
1201 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
1202 struct ui_file *stream)
1203 {
1204 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1205 CORE_ADDR low, high;
1206 gdb_byte *loc_ptr, *buf_end;
1207 int length, first = 1;
1208 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1209 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1210 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1211 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1212 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
1213 /* Adjust base_address for relocatable objects. */
1214 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
1215 SECT_OFF_TEXT (objfile));
1216 CORE_ADDR base_address = dlbaton->base_address + base_offset;
1217
1218 loc_ptr = dlbaton->data;
1219 buf_end = dlbaton->data + dlbaton->size;
1220
1221 fprintf_filtered (stream, _("multi-location ("));
1222
1223 /* Iterate through locations until we run out. */
1224 while (1)
1225 {
1226 if (buf_end - loc_ptr < 2 * addr_size)
1227 error (_("Corrupted DWARF expression for symbol \"%s\"."),
1228 SYMBOL_PRINT_NAME (symbol));
1229
1230 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1231 loc_ptr += addr_size;
1232
1233 /* A base-address-selection entry. */
1234 if (low == base_mask)
1235 {
1236 base_address = dwarf2_read_address (gdbarch,
1237 loc_ptr, buf_end, addr_size);
1238 fprintf_filtered (stream, _("[base address %s]"),
1239 paddress (gdbarch, base_address));
1240 loc_ptr += addr_size;
1241 continue;
1242 }
1243
1244 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1245 loc_ptr += addr_size;
1246
1247 /* An end-of-list entry. */
1248 if (low == 0 && high == 0)
1249 {
1250 /* Indicate the end of the list, for readability. */
1251 fprintf_filtered (stream, _(")"));
1252 return;
1253 }
1254
1255 /* Otherwise, a location expression entry. */
1256 low += base_address;
1257 high += base_address;
1258
1259 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
1260 loc_ptr += 2;
1261
1262 /* Separate the different locations with a semicolon. */
1263 if (first)
1264 first = 0;
1265 else
1266 fprintf_filtered (stream, _("; "));
1267
1268 /* (It would improve readability to print only the minimum
1269 necessary digits of the second number of the range.) */
1270 fprintf_filtered (stream, _("range %s-%s, "),
1271 paddress (gdbarch, low), paddress (gdbarch, high));
1272
1273 /* Now describe this particular location. */
1274 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
1275 objfile, addr_size);
1276
1277 loc_ptr += length;
1278 }
1279 }
1280
1281 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1282 any necessary bytecode in AX. */
1283 static void
1284 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1285 struct agent_expr *ax, struct axs_value *value)
1286 {
1287 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1288 gdb_byte *data;
1289 size_t size;
1290
1291 data = find_location_expression (dlbaton, &size, ax->scope);
1292
1293 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
1294 }
1295
1296 /* The set of location functions used with the DWARF-2 expression
1297 evaluator and location lists. */
1298 const struct symbol_computed_ops dwarf2_loclist_funcs = {
1299 loclist_read_variable,
1300 loclist_read_needs_frame,
1301 loclist_describe_location,
1302 loclist_tracepoint_var_ref
1303 };
This page took 0.05776 seconds and 4 git commands to generate.