1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
8 This file is part of GDB.
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.
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.
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/>. */
34 #include "exceptions.h"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
45 extern int dwarf2_always_disassemble;
48 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
49 const gdb_byte **start, size_t *length);
51 /* A helper function for dealing with location lists. Given a
52 symbol baton (BATON) and a pc value (PC), find the appropriate
53 location expression, set *LOCEXPR_LENGTH, and return a pointer
54 to the beginning of the expression. Returns NULL on failure.
56 For now, only return the first matching location expression; there
57 can be more than one in the list. */
59 static const gdb_byte *
60 find_location_expression (struct dwarf2_loclist_baton *baton,
61 size_t *locexpr_length, CORE_ADDR pc)
64 const gdb_byte *loc_ptr, *buf_end;
66 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
67 struct gdbarch *gdbarch = get_objfile_arch (objfile);
68 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
69 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
70 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
71 /* Adjust base_address for relocatable objects. */
72 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
73 SECT_OFF_TEXT (objfile));
74 CORE_ADDR base_address = baton->base_address + base_offset;
76 loc_ptr = baton->data;
77 buf_end = baton->data + baton->size;
81 if (buf_end - loc_ptr < 2 * addr_size)
82 error (_("find_location_expression: Corrupted DWARF expression."));
84 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
87 /* A base-address-selection entry. */
90 base_address = dwarf2_read_address (gdbarch,
91 loc_ptr, buf_end, addr_size);
96 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
99 /* An end-of-list entry. */
100 if (low == 0 && high == 0)
103 /* Otherwise, a location expression entry. */
105 high += base_address;
107 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
110 if (pc >= low && pc < high)
112 *locexpr_length = length;
120 /* This is the baton used when performing dwarf2 expression
122 struct dwarf_expr_baton
124 struct frame_info *frame;
125 struct objfile *objfile;
128 /* Helper functions for dwarf2_evaluate_loc_desc. */
130 /* Using the frame specified in BATON, return the value of register
131 REGNUM, treated as a pointer. */
133 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
135 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
136 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
140 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
141 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
142 regnum, debaton->frame);
146 /* Read memory at ADDR (length LEN) into BUF. */
149 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
151 read_memory (addr, buf, len);
154 /* Using the frame specified in BATON, find the location expression
155 describing the frame base. Return a pointer to it in START and
156 its length in LENGTH. */
158 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
160 /* FIXME: cagney/2003-03-26: This code should be using
161 get_frame_base_address(), and then implement a dwarf2 specific
163 struct symbol *framefunc;
164 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
166 /* Use block_linkage_function, which returns a real (not inlined)
167 function, instead of get_frame_function, which may return an
169 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
171 /* If we found a frame-relative symbol then it was certainly within
172 some function associated with a frame. If we can't find the frame,
173 something has gone wrong. */
174 gdb_assert (framefunc != NULL);
176 dwarf_expr_frame_base_1 (framefunc,
177 get_frame_address_in_block (debaton->frame),
182 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
183 const gdb_byte **start, size_t *length)
185 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
187 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
189 struct dwarf2_loclist_baton *symbaton;
191 symbaton = SYMBOL_LOCATION_BATON (framefunc);
192 *start = find_location_expression (symbaton, length, pc);
196 struct dwarf2_locexpr_baton *symbaton;
198 symbaton = SYMBOL_LOCATION_BATON (framefunc);
199 if (symbaton != NULL)
201 *length = symbaton->size;
202 *start = symbaton->data;
209 error (_("Could not find the frame base for \"%s\"."),
210 SYMBOL_NATURAL_NAME (framefunc));
213 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
214 the frame in BATON. */
217 dwarf_expr_frame_cfa (void *baton)
219 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
221 return dwarf2_frame_cfa (debaton->frame);
224 /* Using the objfile specified in BATON, find the address for the
225 current thread's thread-local storage with offset OFFSET. */
227 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
229 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
231 return target_translate_tls_address (debaton->objfile, offset);
236 /* Reference count. */
239 /* The number of pieces used to describe this variable. */
242 /* The target address size, used only for DWARF_VALUE_STACK. */
245 /* The pieces themselves. */
246 struct dwarf_expr_piece *pieces;
249 /* Allocate a closure for a value formed from separately-described
252 static struct piece_closure *
253 allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
256 struct piece_closure *c = XZALLOC (struct piece_closure);
259 c->n_pieces = n_pieces;
260 c->addr_size = addr_size;
261 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
263 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
268 /* The lowest-level function to extract bits from a byte buffer.
269 SOURCE is the buffer. It is updated if we read to the end of a
271 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
272 updated to reflect the number of bits actually read.
273 NBITS is the number of bits we want to read. It is updated to
274 reflect the number of bits actually read. This function may read
276 BITS_BIG_ENDIAN is taken directly from gdbarch.
277 This function returns the extracted bits. */
280 extract_bits_primitive (const gdb_byte **source,
281 unsigned int *source_offset_bits,
282 int *nbits, int bits_big_endian)
284 unsigned int avail, mask, datum;
286 gdb_assert (*source_offset_bits < 8);
288 avail = 8 - *source_offset_bits;
292 mask = (1 << avail) - 1;
295 datum >>= 8 - (*source_offset_bits + *nbits);
297 datum >>= *source_offset_bits;
301 *source_offset_bits += avail;
302 if (*source_offset_bits >= 8)
304 *source_offset_bits -= 8;
311 /* Extract some bits from a source buffer and move forward in the
314 SOURCE is the source buffer. It is updated as bytes are read.
315 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
317 NBITS is the number of bits to read.
318 BITS_BIG_ENDIAN is taken directly from gdbarch.
320 This function returns the bits that were read. */
323 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
324 int nbits, int bits_big_endian)
328 gdb_assert (nbits > 0 && nbits <= 8);
330 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
336 more = extract_bits_primitive (source, source_offset_bits, &nbits,
348 /* Write some bits into a buffer and move forward in the buffer.
350 DATUM is the bits to write. The low-order bits of DATUM are used.
351 DEST is the destination buffer. It is updated as bytes are
353 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
355 NBITS is the number of valid bits in DATUM.
356 BITS_BIG_ENDIAN is taken directly from gdbarch. */
359 insert_bits (unsigned int datum,
360 gdb_byte *dest, unsigned int dest_offset_bits,
361 int nbits, int bits_big_endian)
365 gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
367 mask = (1 << nbits) - 1;
370 datum <<= 8 - (dest_offset_bits + nbits);
371 mask <<= 8 - (dest_offset_bits + nbits);
375 datum <<= dest_offset_bits;
376 mask <<= dest_offset_bits;
379 gdb_assert ((datum & ~mask) == 0);
381 *dest = (*dest & ~mask) | datum;
384 /* Copy bits from a source to a destination.
386 DEST is where the bits should be written.
387 DEST_OFFSET_BITS is the bit offset into DEST.
388 SOURCE is the source of bits.
389 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
390 BIT_COUNT is the number of bits to copy.
391 BITS_BIG_ENDIAN is taken directly from gdbarch. */
394 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
395 const gdb_byte *source, unsigned int source_offset_bits,
396 unsigned int bit_count,
399 unsigned int dest_avail;
402 /* Reduce everything to byte-size pieces. */
403 dest += dest_offset_bits / 8;
404 dest_offset_bits %= 8;
405 source += source_offset_bits / 8;
406 source_offset_bits %= 8;
408 dest_avail = 8 - dest_offset_bits % 8;
410 /* See if we can fill the first destination byte. */
411 if (dest_avail < bit_count)
413 datum = extract_bits (&source, &source_offset_bits, dest_avail,
415 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
417 dest_offset_bits = 0;
418 bit_count -= dest_avail;
421 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
422 than 8 bits remaining. */
423 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
424 for (; bit_count >= 8; bit_count -= 8)
426 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
427 *dest++ = (gdb_byte) datum;
430 /* Finally, we may have a few leftover bits. */
431 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
434 datum = extract_bits (&source, &source_offset_bits, bit_count,
436 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
441 read_pieced_value (struct value *v)
445 ULONGEST bits_to_skip;
447 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
448 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
450 size_t buffer_size = 0;
452 struct cleanup *cleanup;
454 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
456 if (value_type (v) != value_enclosing_type (v))
457 internal_error (__FILE__, __LINE__,
458 _("Should not be able to create a lazy value with "
459 "an enclosing type"));
461 cleanup = make_cleanup (free_current_contents, &buffer);
463 contents = value_contents_raw (v);
464 bits_to_skip = 8 * value_offset (v);
465 type_len = 8 * TYPE_LENGTH (value_type (v));
467 for (i = 0; i < c->n_pieces && offset < type_len; i++)
469 struct dwarf_expr_piece *p = &c->pieces[i];
470 size_t this_size, this_size_bits;
471 long dest_offset_bits, source_offset_bits, source_offset;
472 const gdb_byte *intermediate_buffer;
474 /* Compute size, source, and destination offsets for copying, in
476 this_size_bits = p->size;
477 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
479 bits_to_skip -= this_size_bits;
482 if (this_size_bits > type_len - offset)
483 this_size_bits = type_len - offset;
484 if (bits_to_skip > 0)
486 dest_offset_bits = 0;
487 source_offset_bits = bits_to_skip;
488 this_size_bits -= bits_to_skip;
493 dest_offset_bits = offset;
494 source_offset_bits = 0;
497 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
498 source_offset = source_offset_bits / 8;
499 if (buffer_size < this_size)
501 buffer_size = this_size;
502 buffer = xrealloc (buffer, buffer_size);
504 intermediate_buffer = buffer;
506 /* Copy from the source to DEST_BUFFER. */
509 case DWARF_VALUE_REGISTER:
511 struct gdbarch *arch = get_frame_arch (frame);
512 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
514 int reg_offset = source_offset;
516 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
517 && this_size < register_size (arch, gdb_regnum))
519 /* Big-endian, and we want less than full size. */
520 reg_offset = register_size (arch, gdb_regnum) - this_size;
521 /* We want the lower-order THIS_SIZE_BITS of the bytes
522 we extract from the register. */
523 source_offset_bits += 8 * this_size - this_size_bits;
526 if (gdb_regnum != -1)
528 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
533 error (_("Unable to access DWARF register number %s"),
534 paddress (arch, p->v.expr.value));
539 case DWARF_VALUE_MEMORY:
540 if (p->v.expr.in_stack_memory)
541 read_stack (p->v.expr.value + source_offset, buffer, this_size);
543 read_memory (p->v.expr.value + source_offset, buffer, this_size);
546 case DWARF_VALUE_STACK:
548 struct gdbarch *gdbarch = get_type_arch (value_type (v));
549 size_t n = this_size;
551 if (n > c->addr_size - source_offset)
552 n = (c->addr_size >= source_offset
553 ? c->addr_size - source_offset
559 else if (source_offset == 0)
560 store_unsigned_integer (buffer, n,
561 gdbarch_byte_order (gdbarch),
565 gdb_byte bytes[sizeof (ULONGEST)];
567 store_unsigned_integer (bytes, n + source_offset,
568 gdbarch_byte_order (gdbarch),
570 memcpy (buffer, bytes + source_offset, n);
575 case DWARF_VALUE_LITERAL:
577 size_t n = this_size;
579 if (n > p->v.literal.length - source_offset)
580 n = (p->v.literal.length >= source_offset
581 ? p->v.literal.length - source_offset
584 intermediate_buffer = p->v.literal.data + source_offset;
588 case DWARF_VALUE_OPTIMIZED_OUT:
589 /* We just leave the bits empty for now. This is not ideal
590 but gdb currently does not have a nice way to represent
591 optimized-out pieces. */
592 warning (_("bits %ld-%ld in computed object were optimized out; "
593 "replacing with zeroes"),
595 offset + (long) this_size_bits);
599 internal_error (__FILE__, __LINE__, _("invalid location type"));
602 if (p->location != DWARF_VALUE_OPTIMIZED_OUT)
603 copy_bitwise (contents, dest_offset_bits,
604 intermediate_buffer, source_offset_bits % 8,
605 this_size_bits, bits_big_endian);
607 offset += this_size_bits;
610 do_cleanups (cleanup);
614 write_pieced_value (struct value *to, struct value *from)
618 ULONGEST bits_to_skip;
619 const gdb_byte *contents;
620 struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
621 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
623 size_t buffer_size = 0;
625 struct cleanup *cleanup;
627 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
631 set_value_optimized_out (to, 1);
635 cleanup = make_cleanup (free_current_contents, &buffer);
637 contents = value_contents (from);
638 bits_to_skip = 8 * value_offset (to);
639 type_len = 8 * TYPE_LENGTH (value_type (to));
640 for (i = 0; i < c->n_pieces && offset < type_len; i++)
642 struct dwarf_expr_piece *p = &c->pieces[i];
643 size_t this_size_bits, this_size;
644 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
646 const gdb_byte *source_buffer;
648 this_size_bits = p->size;
649 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
651 bits_to_skip -= this_size_bits;
654 if (this_size_bits > type_len - offset)
655 this_size_bits = type_len - offset;
656 if (bits_to_skip > 0)
658 dest_offset_bits = bits_to_skip;
659 source_offset_bits = 0;
660 this_size_bits -= bits_to_skip;
665 dest_offset_bits = 0;
666 source_offset_bits = offset;
669 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
670 source_offset = source_offset_bits / 8;
671 dest_offset = dest_offset_bits / 8;
672 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
674 source_buffer = contents + source_offset;
679 if (buffer_size < this_size)
681 buffer_size = this_size;
682 buffer = xrealloc (buffer, buffer_size);
684 source_buffer = buffer;
690 case DWARF_VALUE_REGISTER:
692 struct gdbarch *arch = get_frame_arch (frame);
693 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
694 int reg_offset = dest_offset;
696 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
697 && this_size <= register_size (arch, gdb_regnum))
698 /* Big-endian, and we want less than full size. */
699 reg_offset = register_size (arch, gdb_regnum) - this_size;
701 if (gdb_regnum != -1)
705 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
707 copy_bitwise (buffer, dest_offset_bits,
708 contents, source_offset_bits,
713 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
714 this_size, source_buffer);
718 error (_("Unable to write to DWARF register number %s"),
719 paddress (arch, p->v.expr.value));
723 case DWARF_VALUE_MEMORY:
726 /* Only the first and last bytes can possibly have any
728 read_memory (p->v.expr.value + dest_offset, buffer, 1);
729 read_memory (p->v.expr.value + dest_offset + this_size - 1,
730 buffer + this_size - 1, 1);
731 copy_bitwise (buffer, dest_offset_bits,
732 contents, source_offset_bits,
737 write_memory (p->v.expr.value + dest_offset,
738 source_buffer, this_size);
741 set_value_optimized_out (to, 1);
744 offset += this_size_bits;
748 do_cleanups (cleanup);
752 copy_pieced_value_closure (struct value *v)
754 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
761 free_pieced_value_closure (struct value *v)
763 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
773 /* Functions for accessing a variable described by DW_OP_piece. */
774 static struct lval_funcs pieced_value_funcs = {
777 copy_pieced_value_closure,
778 free_pieced_value_closure
781 /* Evaluate a location description, starting at DATA and with length
782 SIZE, to find the current location of variable of TYPE in the context
785 static struct value *
786 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
787 const gdb_byte *data, unsigned short size,
788 struct dwarf2_per_cu_data *per_cu)
790 struct value *retval;
791 struct dwarf_expr_baton baton;
792 struct dwarf_expr_context *ctx;
793 struct cleanup *old_chain;
797 retval = allocate_value (type);
798 VALUE_LVAL (retval) = not_lval;
799 set_value_optimized_out (retval, 1);
804 baton.objfile = dwarf2_per_cu_objfile (per_cu);
806 ctx = new_dwarf_expr_context ();
807 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
809 ctx->gdbarch = get_objfile_arch (baton.objfile);
810 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
812 ctx->read_reg = dwarf_expr_read_reg;
813 ctx->read_mem = dwarf_expr_read_mem;
814 ctx->get_frame_base = dwarf_expr_frame_base;
815 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
816 ctx->get_tls_address = dwarf_expr_tls_address;
818 dwarf_expr_eval (ctx, data, size);
819 if (ctx->num_pieces > 0)
821 struct piece_closure *c;
822 struct frame_id frame_id = get_frame_id (frame);
824 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
826 retval = allocate_computed_value (type, &pieced_value_funcs, c);
827 VALUE_FRAME_ID (retval) = frame_id;
831 switch (ctx->location)
833 case DWARF_VALUE_REGISTER:
835 struct gdbarch *arch = get_frame_arch (frame);
836 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
837 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
839 if (gdb_regnum != -1)
840 retval = value_from_register (type, gdb_regnum, frame);
842 error (_("Unable to access DWARF register number %s"),
843 paddress (arch, dwarf_regnum));
847 case DWARF_VALUE_MEMORY:
849 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
850 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
852 retval = allocate_value (type);
853 VALUE_LVAL (retval) = lval_memory;
854 set_value_lazy (retval, 1);
856 set_value_stack (retval, 1);
857 set_value_address (retval, address);
861 case DWARF_VALUE_STACK:
863 ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
865 size_t n = ctx->addr_size;
867 retval = allocate_value (type);
868 contents = value_contents_raw (retval);
869 if (n > TYPE_LENGTH (type))
870 n = TYPE_LENGTH (type);
871 store_unsigned_integer (contents, n,
872 gdbarch_byte_order (ctx->gdbarch),
877 case DWARF_VALUE_LITERAL:
882 retval = allocate_value (type);
883 contents = value_contents_raw (retval);
884 if (n > TYPE_LENGTH (type))
885 n = TYPE_LENGTH (type);
886 memcpy (contents, ctx->data, n);
890 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
891 it can only be encountered when making a piece. */
892 case DWARF_VALUE_OPTIMIZED_OUT:
894 internal_error (__FILE__, __LINE__, _("invalid location type"));
898 set_value_initialized (retval, ctx->initialized);
900 do_cleanups (old_chain);
905 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
907 struct needs_frame_baton
912 /* Reads from registers do require a frame. */
914 needs_frame_read_reg (void *baton, int regnum)
916 struct needs_frame_baton *nf_baton = baton;
918 nf_baton->needs_frame = 1;
922 /* Reads from memory do not require a frame. */
924 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
926 memset (buf, 0, len);
929 /* Frame-relative accesses do require a frame. */
931 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
933 static gdb_byte lit0 = DW_OP_lit0;
934 struct needs_frame_baton *nf_baton = baton;
939 nf_baton->needs_frame = 1;
942 /* CFA accesses require a frame. */
945 needs_frame_frame_cfa (void *baton)
947 struct needs_frame_baton *nf_baton = baton;
949 nf_baton->needs_frame = 1;
953 /* Thread-local accesses do require a frame. */
955 needs_frame_tls_address (void *baton, CORE_ADDR offset)
957 struct needs_frame_baton *nf_baton = baton;
959 nf_baton->needs_frame = 1;
963 /* Return non-zero iff the location expression at DATA (length SIZE)
964 requires a frame to evaluate. */
967 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
968 struct dwarf2_per_cu_data *per_cu)
970 struct needs_frame_baton baton;
971 struct dwarf_expr_context *ctx;
973 struct cleanup *old_chain;
975 baton.needs_frame = 0;
977 ctx = new_dwarf_expr_context ();
978 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
980 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
981 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
983 ctx->read_reg = needs_frame_read_reg;
984 ctx->read_mem = needs_frame_read_mem;
985 ctx->get_frame_base = needs_frame_frame_base;
986 ctx->get_frame_cfa = needs_frame_frame_cfa;
987 ctx->get_tls_address = needs_frame_tls_address;
989 dwarf_expr_eval (ctx, data, size);
991 in_reg = ctx->location == DWARF_VALUE_REGISTER;
993 if (ctx->num_pieces > 0)
997 /* If the location has several pieces, and any of them are in
998 registers, then we will need a frame to fetch them from. */
999 for (i = 0; i < ctx->num_pieces; i++)
1000 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1004 do_cleanups (old_chain);
1006 return baton.needs_frame || in_reg;
1009 /* This struct keeps track of the pieces that make up a multi-location
1010 object, for use in agent expression generation. It is
1011 superficially similar to struct dwarf_expr_piece, but
1012 dwarf_expr_piece is designed for use in immediate evaluation, and
1013 does not, for example, have a way to record both base register and
1018 /* Memory vs register, etc */
1019 enum axs_lvalue_kind kind;
1021 /* If non-zero, number of bytes in this fragment */
1024 /* (GDB-numbered) reg, or base reg if >= 0 */
1027 /* offset from reg */
1031 static const gdb_byte *
1032 dwarf2_tracepoint_var_loc (struct symbol *symbol,
1033 struct agent_expr *ax,
1034 struct axs_var_loc *loc,
1035 struct gdbarch *gdbarch,
1036 const gdb_byte *data, const gdb_byte *end)
1038 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1040 loc->kind = axs_lvalue_register;
1041 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1044 else if (data[0] == DW_OP_regx)
1048 data = read_uleb128 (data + 1, end, ®);
1049 loc->kind = axs_lvalue_register;
1050 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1052 else if (data[0] == DW_OP_fbreg)
1055 struct symbol *framefunc;
1057 LONGEST frame_offset;
1058 const gdb_byte *base_data;
1060 LONGEST base_offset = 0;
1062 b = block_for_pc (ax->scope);
1065 error (_("No block found for address"));
1067 framefunc = block_linkage_function (b);
1070 error (_("No function found for block"));
1072 dwarf_expr_frame_base_1 (framefunc, ax->scope,
1073 &base_data, &base_size);
1075 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1077 const gdb_byte *buf_end;
1079 frame_reg = base_data[0] - DW_OP_breg0;
1080 buf_end = read_sleb128 (base_data + 1,
1081 base_data + base_size, &base_offset);
1082 if (buf_end != base_data + base_size)
1083 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1084 frame_reg, SYMBOL_PRINT_NAME (symbol));
1086 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1088 /* The frame base is just the register, with no offset. */
1089 frame_reg = base_data[0] - DW_OP_reg0;
1094 /* We don't know what to do with the frame base expression,
1095 so we can't trace this variable; give up. */
1096 error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
1097 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1100 data = read_sleb128 (data + 1, end, &frame_offset);
1102 loc->kind = axs_lvalue_memory;
1103 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1104 loc->offset = base_offset + frame_offset;
1106 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
1111 reg = data[0] - DW_OP_breg0;
1112 data = read_sleb128 (data + 1, end, &offset);
1114 loc->kind = axs_lvalue_memory;
1115 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1116 loc->offset = offset;
1119 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
1120 data[0], SYMBOL_PRINT_NAME (symbol));
1125 /* Given the location of a piece, issue bytecodes that will access it. */
1128 dwarf2_tracepoint_var_access (struct agent_expr *ax,
1129 struct axs_value *value,
1130 struct axs_var_loc *loc)
1132 value->kind = loc->kind;
1136 case axs_lvalue_register:
1137 value->u.reg = loc->reg;
1140 case axs_lvalue_memory:
1141 ax_reg (ax, loc->reg);
1144 ax_const_l (ax, loc->offset);
1145 ax_simple (ax, aop_add);
1150 internal_error (__FILE__, __LINE__, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
1155 dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1156 struct agent_expr *ax, struct axs_value *value,
1157 const gdb_byte *data, int size)
1159 const gdb_byte *end = data + size;
1160 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1161 /* In practice, a variable is not going to be spread across
1162 dozens of registers or memory locations. If someone comes up
1163 with a real-world example, revisit this. */
1164 #define MAX_FRAGS 16
1165 struct axs_var_loc fragments[MAX_FRAGS];
1166 int nfrags = 0, frag;
1172 if (!data || size == 0)
1174 value->optimized_out = 1;
1182 if (nfrags == MAX_FRAGS)
1183 error (_("Too many pieces in location for \"%s\"."),
1184 SYMBOL_PRINT_NAME (symbol));
1186 fragments[nfrags].bytes = 0;
1187 data = dwarf2_tracepoint_var_loc (symbol, ax, &fragments[nfrags],
1188 gdbarch, data, end);
1192 else if (data[0] == DW_OP_piece)
1196 data = read_uleb128 (data + 1, end, &bytes);
1197 /* Only deal with 4 byte fragments for now. */
1199 error (_("DW_OP_piece %s not supported in location for \"%s\"."),
1200 pulongest (bytes), SYMBOL_PRINT_NAME (symbol));
1201 fragments[nfrags - 1].bytes = bytes;
1212 if (bad || data > end)
1213 error (_("Corrupted DWARF expression for \"%s\"."),
1214 SYMBOL_PRINT_NAME (symbol));
1216 /* If single expression, no pieces, convert to external format. */
1219 dwarf2_tracepoint_var_access (ax, value, &fragments[0]);
1223 if (length != TYPE_LENGTH (value->type))
1224 error (_("Inconsistent piece information for \"%s\"."),
1225 SYMBOL_PRINT_NAME (symbol));
1227 /* Emit bytecodes to assemble the pieces into a single stack entry. */
1229 for ((frag = (byte_order == BFD_ENDIAN_BIG ? 0 : nfrags - 1));
1231 (frag += (byte_order == BFD_ENDIAN_BIG ? 1 : -1)))
1235 /* shift the previous fragment up 32 bits */
1236 ax_const_l (ax, 32);
1237 ax_simple (ax, aop_lsh);
1240 dwarf2_tracepoint_var_access (ax, value, &fragments[frag]);
1242 switch (value->kind)
1244 case axs_lvalue_register:
1245 ax_reg (ax, value->u.reg);
1248 case axs_lvalue_memory:
1250 extern int trace_kludge; /* Ugh. */
1252 gdb_assert (fragments[frag].bytes == 4);
1254 ax_trace_quick (ax, 4);
1255 ax_simple (ax, aop_ref32);
1262 /* or the new fragment into the previous */
1263 ax_zero_ext (ax, 32);
1264 ax_simple (ax, aop_bit_or);
1268 value->kind = axs_rvalue;
1272 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1273 evaluator to calculate the location. */
1274 static struct value *
1275 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
1277 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1280 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1281 dlbaton->size, dlbaton->per_cu);
1286 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1288 locexpr_read_needs_frame (struct symbol *symbol)
1290 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1292 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1296 /* Return true if DATA points to the end of a piece. END is one past
1297 the last byte in the expression. */
1300 piece_end_p (const gdb_byte *data, const gdb_byte *end)
1302 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
1305 /* Nicely describe a single piece of a location, returning an updated
1306 position in the bytecode sequence. This function cannot recognize
1307 all locations; if a location is not recognized, it simply returns
1310 static const gdb_byte *
1311 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1312 CORE_ADDR addr, struct objfile *objfile,
1313 const gdb_byte *data, const gdb_byte *end,
1314 unsigned int addr_size)
1316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1319 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1321 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1322 fprintf_filtered (stream, _("a variable in $%s"),
1323 gdbarch_register_name (gdbarch, regno));
1326 else if (data[0] == DW_OP_regx)
1330 data = read_uleb128 (data + 1, end, ®);
1331 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1332 fprintf_filtered (stream, _("a variable in $%s"),
1333 gdbarch_register_name (gdbarch, regno));
1335 else if (data[0] == DW_OP_fbreg)
1338 struct symbol *framefunc;
1340 LONGEST frame_offset;
1341 const gdb_byte *base_data, *new_data;
1343 LONGEST base_offset = 0;
1345 new_data = read_sleb128 (data + 1, end, &frame_offset);
1346 if (!piece_end_p (new_data, end))
1350 b = block_for_pc (addr);
1353 error (_("No block found for address for symbol \"%s\"."),
1354 SYMBOL_PRINT_NAME (symbol));
1356 framefunc = block_linkage_function (b);
1359 error (_("No function found for block for symbol \"%s\"."),
1360 SYMBOL_PRINT_NAME (symbol));
1362 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1364 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1366 const gdb_byte *buf_end;
1368 frame_reg = base_data[0] - DW_OP_breg0;
1369 buf_end = read_sleb128 (base_data + 1,
1370 base_data + base_size, &base_offset);
1371 if (buf_end != base_data + base_size)
1372 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1373 frame_reg, SYMBOL_PRINT_NAME (symbol));
1375 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1377 /* The frame base is just the register, with no offset. */
1378 frame_reg = base_data[0] - DW_OP_reg0;
1383 /* We don't know what to do with the frame base expression,
1384 so we can't trace this variable; give up. */
1385 error (_("Cannot describe location of symbol \"%s\"; "
1386 "DWARF 2 encoding not handled, "
1387 "first opcode in base data is 0x%x."),
1388 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1391 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1393 fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1394 gdbarch_register_name (gdbarch, regno),
1395 plongest (base_offset), plongest (frame_offset));
1397 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
1398 && piece_end_p (data, end))
1402 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1404 data = read_sleb128 (data + 1, end, &offset);
1406 fprintf_filtered (stream,
1407 _("a variable at offset %s from base reg $%s"),
1409 gdbarch_register_name (gdbarch, regno));
1412 /* The location expression for a TLS variable looks like this (on a
1415 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1416 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1418 0x3 is the encoding for DW_OP_addr, which has an operand as long
1419 as the size of an address on the target machine (here is 8
1420 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1421 The operand represents the offset at which the variable is within
1422 the thread local storage. */
1424 else if (data + 1 + addr_size < end
1425 && data[0] == DW_OP_addr
1426 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
1427 && piece_end_p (data + 2 + addr_size, end))
1429 CORE_ADDR offset = dwarf2_read_address (gdbarch,
1434 fprintf_filtered (stream,
1435 _("a thread-local variable at offset %s "
1436 "in the thread-local storage for `%s'"),
1437 paddress (gdbarch, offset), objfile->name);
1439 data += 1 + addr_size + 1;
1441 else if (data[0] >= DW_OP_lit0
1442 && data[0] <= DW_OP_lit31
1444 && data[1] == DW_OP_stack_value)
1446 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
1453 /* Disassemble an expression, stopping at the end of a piece or at the
1454 end of the expression. Returns a pointer to the next unread byte
1455 in the input expression. If ALL is nonzero, then this function
1456 will keep going until it reaches the end of the expression. */
1458 static const gdb_byte *
1459 disassemble_dwarf_expression (struct ui_file *stream,
1460 struct gdbarch *arch, unsigned int addr_size,
1462 const gdb_byte *data, const gdb_byte *end,
1465 const gdb_byte *start = data;
1467 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
1471 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
1473 enum dwarf_location_atom op = *data++;
1479 name = dwarf_stack_op_name (op, 0);
1482 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
1483 op, (long) (data - start));
1484 fprintf_filtered (stream, " % 4ld: %s", (long) (data - start), name);
1489 addr = dwarf2_read_address (arch, data, end, addr_size);
1491 fprintf_filtered (stream, " %s", paddress (arch, addr));
1495 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
1497 fprintf_filtered (stream, " %s", pulongest (ul));
1500 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
1502 fprintf_filtered (stream, " %s", plongest (l));
1505 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1507 fprintf_filtered (stream, " %s", pulongest (ul));
1510 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1512 fprintf_filtered (stream, " %s", plongest (l));
1515 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1517 fprintf_filtered (stream, " %s", pulongest (ul));
1520 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
1522 fprintf_filtered (stream, " %s", plongest (l));
1525 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
1527 fprintf_filtered (stream, " %s", pulongest (ul));
1530 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
1532 fprintf_filtered (stream, " %s", plongest (l));
1535 data = read_uleb128 (data, end, &ul);
1536 fprintf_filtered (stream, " %s", pulongest (ul));
1539 data = read_sleb128 (data, end, &ul);
1540 fprintf_filtered (stream, " %s", plongest (l));
1575 fprintf_filtered (stream, " [$%s]",
1576 gdbarch_register_name (arch, op - DW_OP_reg0));
1580 data = read_uleb128 (data, end, &ul);
1581 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1582 gdbarch_register_name (arch, (int) ul));
1585 case DW_OP_implicit_value:
1586 data = read_uleb128 (data, end, &ul);
1588 fprintf_filtered (stream, " %s", pulongest (ul));
1623 data = read_sleb128 (data, end, &ul);
1624 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1625 gdbarch_register_name (arch, op - DW_OP_breg0));
1632 data = read_uleb128 (data, end, &ul);
1633 data = read_sleb128 (data, end, &offset);
1634 fprintf_filtered (stream, " register %s [$%s] offset %s",
1636 gdbarch_register_name (arch, (int) ul),
1637 pulongest (offset));
1642 data = read_sleb128 (data, end, &ul);
1643 fprintf_filtered (stream, " %s", pulongest (ul));
1646 case DW_OP_xderef_size:
1647 case DW_OP_deref_size:
1649 fprintf_filtered (stream, " %d", *data);
1653 case DW_OP_plus_uconst:
1654 data = read_uleb128 (data, end, &ul);
1655 fprintf_filtered (stream, " %s", pulongest (ul));
1659 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1661 fprintf_filtered (stream, " to %ld",
1662 (long) (data + l - start));
1666 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1668 fprintf_filtered (stream, " %ld",
1669 (long) (data + l - start));
1673 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1675 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
1679 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1681 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
1684 case DW_OP_call_ref:
1685 ul = extract_unsigned_integer (data, offset_size,
1686 gdbarch_byte_order (arch));
1687 data += offset_size;
1688 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
1692 data = read_uleb128 (data, end, &ul);
1693 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
1696 case DW_OP_bit_piece:
1700 data = read_uleb128 (data, end, &ul);
1701 data = read_uleb128 (data, end, &offset);
1702 fprintf_filtered (stream, " size %s offset %s (bits)",
1703 pulongest (ul), pulongest (offset));
1708 fprintf_filtered (stream, "\n");
1714 /* Describe a single location, which may in turn consist of multiple
1718 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
1719 struct ui_file *stream,
1720 const gdb_byte *data, int size,
1721 struct objfile *objfile, unsigned int addr_size,
1724 const gdb_byte *end = data + size;
1725 int first_piece = 1, bad = 0;
1729 const gdb_byte *here = data;
1730 int disassemble = 1;
1735 fprintf_filtered (stream, _(", and "));
1737 if (!dwarf2_always_disassemble)
1739 data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
1740 data, end, addr_size);
1741 /* If we printed anything, or if we have an empty piece,
1742 then don't disassemble. */
1744 || data[0] == DW_OP_piece
1745 || data[0] == DW_OP_bit_piece)
1749 data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile),
1750 addr_size, offset_size, data, end,
1751 dwarf2_always_disassemble);
1755 int empty = data == here;
1758 fprintf_filtered (stream, " ");
1759 if (data[0] == DW_OP_piece)
1763 data = read_uleb128 (data + 1, end, &bytes);
1766 fprintf_filtered (stream, _("an empty %s-byte piece"),
1769 fprintf_filtered (stream, _(" [%s-byte piece]"),
1772 else if (data[0] == DW_OP_bit_piece)
1774 ULONGEST bits, offset;
1776 data = read_uleb128 (data + 1, end, &bits);
1777 data = read_uleb128 (data, end, &offset);
1780 fprintf_filtered (stream,
1781 _("an empty %s-bit piece"),
1784 fprintf_filtered (stream,
1785 _(" [%s-bit piece, offset %s bits]"),
1786 pulongest (bits), pulongest (offset));
1796 if (bad || data > end)
1797 error (_("Corrupted DWARF2 expression for \"%s\"."),
1798 SYMBOL_PRINT_NAME (symbol));
1801 /* Print a natural-language description of SYMBOL to STREAM. This
1802 version is for a symbol with a single location. */
1805 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
1806 struct ui_file *stream)
1808 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1809 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1810 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1811 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
1813 locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
1814 objfile, addr_size, offset_size);
1817 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1818 any necessary bytecode in AX. */
1821 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1822 struct agent_expr *ax, struct axs_value *value)
1824 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1826 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
1827 dlbaton->data, dlbaton->size);
1830 /* The set of location functions used with the DWARF-2 expression
1832 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
1833 locexpr_read_variable,
1834 locexpr_read_needs_frame,
1835 locexpr_describe_location,
1836 locexpr_tracepoint_var_ref
1840 /* Wrapper functions for location lists. These generally find
1841 the appropriate location expression and call something above. */
1843 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1844 evaluator to calculate the location. */
1845 static struct value *
1846 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
1848 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1850 const gdb_byte *data;
1853 data = find_location_expression (dlbaton, &size,
1854 frame ? get_frame_address_in_block (frame)
1858 val = allocate_value (SYMBOL_TYPE (symbol));
1859 VALUE_LVAL (val) = not_lval;
1860 set_value_optimized_out (val, 1);
1863 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
1869 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1871 loclist_read_needs_frame (struct symbol *symbol)
1873 /* If there's a location list, then assume we need to have a frame
1874 to choose the appropriate location expression. With tracking of
1875 global variables this is not necessarily true, but such tracking
1876 is disabled in GCC at the moment until we figure out how to
1882 /* Print a natural-language description of SYMBOL to STREAM. This
1883 version applies when there is a list of different locations, each
1884 with a specified address range. */
1887 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
1888 struct ui_file *stream)
1890 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1891 CORE_ADDR low, high;
1892 const gdb_byte *loc_ptr, *buf_end;
1893 int length, first = 1;
1894 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1895 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1896 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1897 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1898 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
1899 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
1900 /* Adjust base_address for relocatable objects. */
1901 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
1902 SECT_OFF_TEXT (objfile));
1903 CORE_ADDR base_address = dlbaton->base_address + base_offset;
1905 loc_ptr = dlbaton->data;
1906 buf_end = dlbaton->data + dlbaton->size;
1908 fprintf_filtered (stream, _("multi-location:\n"));
1910 /* Iterate through locations until we run out. */
1913 if (buf_end - loc_ptr < 2 * addr_size)
1914 error (_("Corrupted DWARF expression for symbol \"%s\"."),
1915 SYMBOL_PRINT_NAME (symbol));
1917 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1918 loc_ptr += addr_size;
1920 /* A base-address-selection entry. */
1921 if (low == base_mask)
1923 base_address = dwarf2_read_address (gdbarch,
1924 loc_ptr, buf_end, addr_size);
1925 fprintf_filtered (stream, _(" Base address %s"),
1926 paddress (gdbarch, base_address));
1927 loc_ptr += addr_size;
1931 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1932 loc_ptr += addr_size;
1934 /* An end-of-list entry. */
1935 if (low == 0 && high == 0)
1938 /* Otherwise, a location expression entry. */
1939 low += base_address;
1940 high += base_address;
1942 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
1945 /* (It would improve readability to print only the minimum
1946 necessary digits of the second number of the range.) */
1947 fprintf_filtered (stream, _(" Range %s-%s: "),
1948 paddress (gdbarch, low), paddress (gdbarch, high));
1950 /* Now describe this particular location. */
1951 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
1952 objfile, addr_size, offset_size);
1954 fprintf_filtered (stream, "\n");
1960 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1961 any necessary bytecode in AX. */
1963 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1964 struct agent_expr *ax, struct axs_value *value)
1966 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1967 const gdb_byte *data;
1970 data = find_location_expression (dlbaton, &size, ax->scope);
1972 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
1975 /* The set of location functions used with the DWARF-2 expression
1976 evaluator and location lists. */
1977 const struct symbol_computed_ops dwarf2_loclist_funcs = {
1978 loclist_read_variable,
1979 loclist_read_needs_frame,
1980 loclist_describe_location,
1981 loclist_tracepoint_var_ref