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"
46 dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
47 gdb_byte
**start
, size_t *length
);
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.
54 For now, only return the first matching location expression; there
55 can be more than one in the list. */
58 find_location_expression (struct dwarf2_loclist_baton
*baton
,
59 size_t *locexpr_length
, CORE_ADDR pc
)
62 gdb_byte
*loc_ptr
, *buf_end
;
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
;
74 loc_ptr
= baton
->data
;
75 buf_end
= baton
->data
+ baton
->size
;
79 if (buf_end
- loc_ptr
< 2 * addr_size
)
80 error (_("find_location_expression: Corrupted DWARF expression."));
82 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
85 /* A base-address-selection entry. */
88 base_address
= dwarf2_read_address (gdbarch
,
89 loc_ptr
, buf_end
, addr_size
);
94 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
97 /* An end-of-list entry. */
98 if (low
== 0 && high
== 0)
101 /* Otherwise, a location expression entry. */
103 high
+= base_address
;
105 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
108 if (pc
>= low
&& pc
< high
)
110 *locexpr_length
= length
;
118 /* This is the baton used when performing dwarf2 expression
120 struct dwarf_expr_baton
122 struct frame_info
*frame
;
123 struct objfile
*objfile
;
126 /* Helper functions for dwarf2_evaluate_loc_desc. */
128 /* Using the frame specified in BATON, return the value of register
129 REGNUM, treated as a pointer. */
131 dwarf_expr_read_reg (void *baton
, int dwarf_regnum
)
133 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
134 struct gdbarch
*gdbarch
= get_frame_arch (debaton
->frame
);
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
);
144 /* Read memory at ADDR (length LEN) into BUF. */
147 dwarf_expr_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
149 read_memory (addr
, buf
, len
);
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. */
156 dwarf_expr_frame_base (void *baton
, gdb_byte
**start
, size_t * length
)
158 /* FIXME: cagney/2003-03-26: This code should be using
159 get_frame_base_address(), and then implement a dwarf2 specific
161 struct symbol
*framefunc
;
162 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
164 /* Use block_linkage_function, which returns a real (not inlined)
165 function, instead of get_frame_function, which may return an
167 framefunc
= block_linkage_function (get_frame_block (debaton
->frame
, NULL
));
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
);
174 dwarf_expr_frame_base_1 (framefunc
,
175 get_frame_address_in_block (debaton
->frame
),
180 dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
181 gdb_byte
**start
, size_t *length
)
183 if (SYMBOL_LOCATION_BATON (framefunc
) == NULL
)
185 else if (SYMBOL_COMPUTED_OPS (framefunc
) == &dwarf2_loclist_funcs
)
187 struct dwarf2_loclist_baton
*symbaton
;
189 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
190 *start
= find_location_expression (symbaton
, length
, pc
);
194 struct dwarf2_locexpr_baton
*symbaton
;
196 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
197 if (symbaton
!= NULL
)
199 *length
= symbaton
->size
;
200 *start
= symbaton
->data
;
207 error (_("Could not find the frame base for \"%s\"."),
208 SYMBOL_NATURAL_NAME (framefunc
));
211 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
212 the frame in BATON. */
215 dwarf_expr_frame_cfa (void *baton
)
217 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
219 return dwarf2_frame_cfa (debaton
->frame
);
222 /* Using the objfile specified in BATON, find the address for the
223 current thread's thread-local storage with offset OFFSET. */
225 dwarf_expr_tls_address (void *baton
, CORE_ADDR offset
)
227 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
229 return target_translate_tls_address (debaton
->objfile
, offset
);
234 /* The number of pieces used to describe this variable. */
237 /* The target address size, used only for DWARF_VALUE_STACK. */
240 /* The pieces themselves. */
241 struct dwarf_expr_piece
*pieces
;
244 /* Allocate a closure for a value formed from separately-described
247 static struct piece_closure
*
248 allocate_piece_closure (int n_pieces
, struct dwarf_expr_piece
*pieces
,
251 struct piece_closure
*c
= XZALLOC (struct piece_closure
);
253 c
->n_pieces
= n_pieces
;
254 c
->addr_size
= addr_size
;
255 c
->pieces
= XCALLOC (n_pieces
, struct dwarf_expr_piece
);
257 memcpy (c
->pieces
, pieces
, n_pieces
* sizeof (struct dwarf_expr_piece
));
263 read_pieced_value (struct value
*v
)
267 ULONGEST bytes_to_skip
;
269 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
270 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (v
));
273 if (value_type (v
) != value_enclosing_type (v
))
274 internal_error (__FILE__
, __LINE__
,
275 _("Should not be able to create a lazy value with "
276 "an enclosing type"));
278 contents
= value_contents_raw (v
);
279 bytes_to_skip
= value_offset (v
);
280 type_len
= TYPE_LENGTH (value_type (v
));
281 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
283 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
285 long dest_offset
, source_offset
;
287 if (bytes_to_skip
> 0 && bytes_to_skip
>= p
->size
)
289 bytes_to_skip
-= p
->size
;
293 if (this_size
> type_len
- offset
)
294 this_size
= type_len
- offset
;
295 if (bytes_to_skip
> 0)
298 source_offset
= bytes_to_skip
;
299 this_size
-= bytes_to_skip
;
304 dest_offset
= offset
;
310 case DWARF_VALUE_REGISTER
:
312 struct gdbarch
*arch
= get_frame_arch (frame
);
313 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
,
315 int reg_offset
= source_offset
;
317 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
318 && this_size
< register_size (arch
, gdb_regnum
))
319 /* Big-endian, and we want less than full size. */
320 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
322 if (gdb_regnum
!= -1)
324 get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
325 this_size
, contents
+ dest_offset
);
329 error (_("Unable to access DWARF register number %s"),
330 paddress (arch
, p
->v
.expr
.value
));
335 case DWARF_VALUE_MEMORY
:
336 if (p
->v
.expr
.in_stack_memory
)
337 read_stack (p
->v
.expr
.value
+ source_offset
,
338 contents
+ dest_offset
, this_size
);
340 read_memory (p
->v
.expr
.value
+ source_offset
,
341 contents
+ dest_offset
, this_size
);
344 case DWARF_VALUE_STACK
:
346 struct gdbarch
*gdbarch
= get_type_arch (value_type (v
));
347 size_t n
= this_size
;
349 if (n
> c
->addr_size
- source_offset
)
350 n
= (c
->addr_size
>= source_offset
351 ? c
->addr_size
- source_offset
357 else if (source_offset
== 0)
358 store_unsigned_integer (contents
+ dest_offset
, n
,
359 gdbarch_byte_order (gdbarch
),
363 gdb_byte bytes
[sizeof (ULONGEST
)];
365 store_unsigned_integer (bytes
, n
+ source_offset
,
366 gdbarch_byte_order (gdbarch
),
368 memcpy (contents
+ dest_offset
, bytes
+ source_offset
, n
);
373 case DWARF_VALUE_LITERAL
:
375 size_t n
= this_size
;
377 if (n
> p
->v
.literal
.length
- source_offset
)
378 n
= (p
->v
.literal
.length
>= source_offset
379 ? p
->v
.literal
.length
- source_offset
382 memcpy (contents
+ dest_offset
,
383 p
->v
.literal
.data
+ source_offset
, n
);
387 case DWARF_VALUE_OPTIMIZED_OUT
:
388 /* We just leave the bits empty for now. This is not ideal
389 but gdb currently does not have a nice way to represent
390 optimized-out pieces. */
391 warning (_("bytes %ld-%ld in computed object were optimized out; "
392 "replacing with zeroes"),
394 offset
+ (long) this_size
);
398 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
405 write_pieced_value (struct value
*to
, struct value
*from
)
409 ULONGEST bytes_to_skip
;
410 const gdb_byte
*contents
;
411 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (to
);
412 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (to
));
417 set_value_optimized_out (to
, 1);
421 contents
= value_contents (from
);
422 bytes_to_skip
= value_offset (to
);
423 type_len
= TYPE_LENGTH (value_type (to
));
424 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
426 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
428 long dest_offset
, source_offset
;
430 if (bytes_to_skip
> 0 && bytes_to_skip
>= p
->size
)
432 bytes_to_skip
-= p
->size
;
436 if (this_size
> type_len
- offset
)
437 this_size
= type_len
- offset
;
438 if (bytes_to_skip
> 0)
440 dest_offset
= bytes_to_skip
;
442 this_size
-= bytes_to_skip
;
448 source_offset
= offset
;
453 case DWARF_VALUE_REGISTER
:
455 struct gdbarch
*arch
= get_frame_arch (frame
);
456 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->v
.expr
.value
);
457 int reg_offset
= dest_offset
;
459 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
460 && this_size
<= register_size (arch
, gdb_regnum
))
461 /* Big-endian, and we want less than full size. */
462 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
464 if (gdb_regnum
!= -1)
466 put_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
467 this_size
, contents
+ source_offset
);
471 error (_("Unable to write to DWARF register number %s"),
472 paddress (arch
, p
->v
.expr
.value
));
476 case DWARF_VALUE_MEMORY
:
477 write_memory (p
->v
.expr
.value
+ dest_offset
,
478 contents
+ source_offset
, this_size
);
481 set_value_optimized_out (to
, 1);
489 copy_pieced_value_closure (struct value
*v
)
491 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
493 return allocate_piece_closure (c
->n_pieces
, c
->pieces
, c
->addr_size
);
497 free_pieced_value_closure (struct value
*v
)
499 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
505 /* Functions for accessing a variable described by DW_OP_piece. */
506 static struct lval_funcs pieced_value_funcs
= {
509 copy_pieced_value_closure
,
510 free_pieced_value_closure
513 /* Evaluate a location description, starting at DATA and with length
514 SIZE, to find the current location of variable of TYPE in the context
517 static struct value
*
518 dwarf2_evaluate_loc_desc (struct type
*type
, struct frame_info
*frame
,
519 gdb_byte
*data
, unsigned short size
,
520 struct dwarf2_per_cu_data
*per_cu
)
522 struct value
*retval
;
523 struct dwarf_expr_baton baton
;
524 struct dwarf_expr_context
*ctx
;
525 struct cleanup
*old_chain
;
529 retval
= allocate_value (type
);
530 VALUE_LVAL (retval
) = not_lval
;
531 set_value_optimized_out (retval
, 1);
536 baton
.objfile
= dwarf2_per_cu_objfile (per_cu
);
538 ctx
= new_dwarf_expr_context ();
539 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
541 ctx
->gdbarch
= get_objfile_arch (baton
.objfile
);
542 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
544 ctx
->read_reg
= dwarf_expr_read_reg
;
545 ctx
->read_mem
= dwarf_expr_read_mem
;
546 ctx
->get_frame_base
= dwarf_expr_frame_base
;
547 ctx
->get_frame_cfa
= dwarf_expr_frame_cfa
;
548 ctx
->get_tls_address
= dwarf_expr_tls_address
;
550 dwarf_expr_eval (ctx
, data
, size
);
551 if (ctx
->num_pieces
> 0)
553 struct piece_closure
*c
;
554 struct frame_id frame_id
= get_frame_id (frame
);
556 c
= allocate_piece_closure (ctx
->num_pieces
, ctx
->pieces
,
558 retval
= allocate_computed_value (type
, &pieced_value_funcs
, c
);
559 VALUE_FRAME_ID (retval
) = frame_id
;
563 switch (ctx
->location
)
565 case DWARF_VALUE_REGISTER
:
567 struct gdbarch
*arch
= get_frame_arch (frame
);
568 CORE_ADDR dwarf_regnum
= dwarf_expr_fetch (ctx
, 0);
569 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_regnum
);
571 if (gdb_regnum
!= -1)
572 retval
= value_from_register (type
, gdb_regnum
, frame
);
574 error (_("Unable to access DWARF register number %s"),
575 paddress (arch
, dwarf_regnum
));
579 case DWARF_VALUE_MEMORY
:
581 CORE_ADDR address
= dwarf_expr_fetch (ctx
, 0);
582 int in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
584 retval
= allocate_value (type
);
585 VALUE_LVAL (retval
) = lval_memory
;
586 set_value_lazy (retval
, 1);
588 set_value_stack (retval
, 1);
589 set_value_address (retval
, address
);
593 case DWARF_VALUE_STACK
:
595 ULONGEST value
= (ULONGEST
) dwarf_expr_fetch (ctx
, 0);
597 size_t n
= ctx
->addr_size
;
599 retval
= allocate_value (type
);
600 contents
= value_contents_raw (retval
);
601 if (n
> TYPE_LENGTH (type
))
602 n
= TYPE_LENGTH (type
);
603 store_unsigned_integer (contents
, n
,
604 gdbarch_byte_order (ctx
->gdbarch
),
609 case DWARF_VALUE_LITERAL
:
614 retval
= allocate_value (type
);
615 contents
= value_contents_raw (retval
);
616 if (n
> TYPE_LENGTH (type
))
617 n
= TYPE_LENGTH (type
);
618 memcpy (contents
, ctx
->data
, n
);
622 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
623 it can only be encountered when making a piece. */
624 case DWARF_VALUE_OPTIMIZED_OUT
:
626 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
630 set_value_initialized (retval
, ctx
->initialized
);
632 do_cleanups (old_chain
);
637 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
639 struct needs_frame_baton
644 /* Reads from registers do require a frame. */
646 needs_frame_read_reg (void *baton
, int regnum
)
648 struct needs_frame_baton
*nf_baton
= baton
;
650 nf_baton
->needs_frame
= 1;
654 /* Reads from memory do not require a frame. */
656 needs_frame_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
658 memset (buf
, 0, len
);
661 /* Frame-relative accesses do require a frame. */
663 needs_frame_frame_base (void *baton
, gdb_byte
**start
, size_t * length
)
665 static gdb_byte lit0
= DW_OP_lit0
;
666 struct needs_frame_baton
*nf_baton
= baton
;
671 nf_baton
->needs_frame
= 1;
674 /* CFA accesses require a frame. */
677 needs_frame_frame_cfa (void *baton
)
679 struct needs_frame_baton
*nf_baton
= baton
;
681 nf_baton
->needs_frame
= 1;
685 /* Thread-local accesses do require a frame. */
687 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
689 struct needs_frame_baton
*nf_baton
= baton
;
691 nf_baton
->needs_frame
= 1;
695 /* Return non-zero iff the location expression at DATA (length SIZE)
696 requires a frame to evaluate. */
699 dwarf2_loc_desc_needs_frame (gdb_byte
*data
, unsigned short size
,
700 struct dwarf2_per_cu_data
*per_cu
)
702 struct needs_frame_baton baton
;
703 struct dwarf_expr_context
*ctx
;
705 struct cleanup
*old_chain
;
707 baton
.needs_frame
= 0;
709 ctx
= new_dwarf_expr_context ();
710 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
712 ctx
->gdbarch
= get_objfile_arch (dwarf2_per_cu_objfile (per_cu
));
713 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
715 ctx
->read_reg
= needs_frame_read_reg
;
716 ctx
->read_mem
= needs_frame_read_mem
;
717 ctx
->get_frame_base
= needs_frame_frame_base
;
718 ctx
->get_frame_cfa
= needs_frame_frame_cfa
;
719 ctx
->get_tls_address
= needs_frame_tls_address
;
721 dwarf_expr_eval (ctx
, data
, size
);
723 in_reg
= ctx
->location
== DWARF_VALUE_REGISTER
;
725 if (ctx
->num_pieces
> 0)
729 /* If the location has several pieces, and any of them are in
730 registers, then we will need a frame to fetch them from. */
731 for (i
= 0; i
< ctx
->num_pieces
; i
++)
732 if (ctx
->pieces
[i
].location
== DWARF_VALUE_REGISTER
)
736 do_cleanups (old_chain
);
738 return baton
.needs_frame
|| in_reg
;
741 /* This struct keeps track of the pieces that make up a multi-location
742 object, for use in agent expression generation. It is
743 superficially similar to struct dwarf_expr_piece, but
744 dwarf_expr_piece is designed for use in immediate evaluation, and
745 does not, for example, have a way to record both base register and
750 /* Memory vs register, etc */
751 enum axs_lvalue_kind kind
;
753 /* If non-zero, number of bytes in this fragment */
756 /* (GDB-numbered) reg, or base reg if >= 0 */
759 /* offset from reg */
764 dwarf2_tracepoint_var_loc (struct symbol
*symbol
,
765 struct agent_expr
*ax
,
766 struct axs_var_loc
*loc
,
767 struct gdbarch
*gdbarch
,
768 gdb_byte
*data
, gdb_byte
*end
)
770 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
772 loc
->kind
= axs_lvalue_register
;
773 loc
->reg
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, data
[0] - DW_OP_reg0
);
776 else if (data
[0] == DW_OP_regx
)
780 data
= read_uleb128 (data
+ 1, end
, ®
);
781 loc
->kind
= axs_lvalue_register
;
782 loc
->reg
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
);
784 else if (data
[0] == DW_OP_fbreg
)
787 struct symbol
*framefunc
;
789 LONGEST frame_offset
;
792 LONGEST base_offset
= 0;
794 b
= block_for_pc (ax
->scope
);
797 error (_("No block found for address"));
799 framefunc
= block_linkage_function (b
);
802 error (_("No function found for block"));
804 dwarf_expr_frame_base_1 (framefunc
, ax
->scope
,
805 &base_data
, &base_size
);
807 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
811 frame_reg
= base_data
[0] - DW_OP_breg0
;
812 buf_end
= read_sleb128 (base_data
+ 1,
813 base_data
+ base_size
, &base_offset
);
814 if (buf_end
!= base_data
+ base_size
)
815 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
816 frame_reg
, SYMBOL_PRINT_NAME (symbol
));
818 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
820 /* The frame base is just the register, with no offset. */
821 frame_reg
= base_data
[0] - DW_OP_reg0
;
826 /* We don't know what to do with the frame base expression,
827 so we can't trace this variable; give up. */
828 error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
829 SYMBOL_PRINT_NAME (symbol
), base_data
[0]);
832 data
= read_sleb128 (data
+ 1, end
, &frame_offset
);
834 loc
->kind
= axs_lvalue_memory
;
835 loc
->reg
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, frame_reg
);
836 loc
->offset
= base_offset
+ frame_offset
;
838 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
)
843 reg
= data
[0] - DW_OP_breg0
;
844 data
= read_sleb128 (data
+ 1, end
, &offset
);
846 loc
->kind
= axs_lvalue_memory
;
847 loc
->reg
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
);
848 loc
->offset
= offset
;
851 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
852 data
[0], SYMBOL_PRINT_NAME (symbol
));
857 /* Given the location of a piece, issue bytecodes that will access it. */
860 dwarf2_tracepoint_var_access (struct agent_expr
*ax
,
861 struct axs_value
*value
,
862 struct axs_var_loc
*loc
)
864 value
->kind
= loc
->kind
;
868 case axs_lvalue_register
:
869 value
->u
.reg
= loc
->reg
;
872 case axs_lvalue_memory
:
873 ax_reg (ax
, loc
->reg
);
876 ax_const_l (ax
, loc
->offset
);
877 ax_simple (ax
, aop_add
);
882 internal_error (__FILE__
, __LINE__
, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
887 dwarf2_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
888 struct agent_expr
*ax
, struct axs_value
*value
,
889 gdb_byte
*data
, int size
)
891 gdb_byte
*end
= data
+ size
;
892 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
893 /* In practice, a variable is not going to be spread across
894 dozens of registers or memory locations. If someone comes up
895 with a real-world example, revisit this. */
897 struct axs_var_loc fragments
[MAX_FRAGS
];
898 int nfrags
= 0, frag
;
904 if (!data
|| size
== 0)
906 value
->optimized_out
= 1;
914 if (nfrags
== MAX_FRAGS
)
915 error (_("Too many pieces in location for \"%s\"."),
916 SYMBOL_PRINT_NAME (symbol
));
918 fragments
[nfrags
].bytes
= 0;
919 data
= dwarf2_tracepoint_var_loc (symbol
, ax
, &fragments
[nfrags
],
924 else if (data
[0] == DW_OP_piece
)
928 data
= read_uleb128 (data
+ 1, end
, &bytes
);
929 /* Only deal with 4 byte fragments for now. */
931 error (_("DW_OP_piece %s not supported in location for \"%s\"."),
932 pulongest (bytes
), SYMBOL_PRINT_NAME (symbol
));
933 fragments
[nfrags
- 1].bytes
= bytes
;
944 if (bad
|| data
> end
)
945 error (_("Corrupted DWARF expression for \"%s\"."),
946 SYMBOL_PRINT_NAME (symbol
));
948 /* If single expression, no pieces, convert to external format. */
951 dwarf2_tracepoint_var_access (ax
, value
, &fragments
[0]);
955 if (length
!= TYPE_LENGTH (value
->type
))
956 error (_("Inconsistent piece information for \"%s\"."),
957 SYMBOL_PRINT_NAME (symbol
));
959 /* Emit bytecodes to assemble the pieces into a single stack entry. */
961 for ((frag
= (byte_order
== BFD_ENDIAN_BIG
? 0 : nfrags
- 1));
963 (frag
+= (byte_order
== BFD_ENDIAN_BIG
? 1 : -1)))
967 /* shift the previous fragment up 32 bits */
969 ax_simple (ax
, aop_lsh
);
972 dwarf2_tracepoint_var_access (ax
, value
, &fragments
[frag
]);
976 case axs_lvalue_register
:
977 ax_reg (ax
, value
->u
.reg
);
980 case axs_lvalue_memory
:
982 extern int trace_kludge
; /* Ugh. */
984 gdb_assert (fragments
[frag
].bytes
== 4);
986 ax_trace_quick (ax
, 4);
987 ax_simple (ax
, aop_ref32
);
994 /* or the new fragment into the previous */
995 ax_zero_ext (ax
, 32);
996 ax_simple (ax
, aop_bit_or
);
1000 value
->kind
= axs_rvalue
;
1004 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1005 evaluator to calculate the location. */
1006 static struct value
*
1007 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
1009 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1012 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
1013 dlbaton
->size
, dlbaton
->per_cu
);
1018 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1020 locexpr_read_needs_frame (struct symbol
*symbol
)
1022 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1024 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
,
1028 /* Describe a single piece of a location, returning an updated
1029 position in the bytecode sequence. */
1032 locexpr_describe_location_piece (struct symbol
*symbol
, struct ui_file
*stream
,
1033 CORE_ADDR addr
, struct objfile
*objfile
,
1034 gdb_byte
*data
, int size
, unsigned int addr_size
)
1036 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
1039 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
1041 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, data
[0] - DW_OP_reg0
);
1042 fprintf_filtered (stream
, _("a variable in $%s"),
1043 gdbarch_register_name (gdbarch
, regno
));
1046 else if (data
[0] == DW_OP_regx
)
1050 data
= read_uleb128 (data
+ 1, data
+ size
, ®
);
1051 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
);
1052 fprintf_filtered (stream
, _("a variable in $%s"),
1053 gdbarch_register_name (gdbarch
, regno
));
1055 else if (data
[0] == DW_OP_fbreg
)
1058 struct symbol
*framefunc
;
1060 LONGEST frame_offset
;
1061 gdb_byte
*base_data
;
1063 LONGEST base_offset
= 0;
1065 b
= block_for_pc (addr
);
1068 error (_("No block found for address for symbol \"%s\"."),
1069 SYMBOL_PRINT_NAME (symbol
));
1071 framefunc
= block_linkage_function (b
);
1074 error (_("No function found for block for symbol \"%s\"."),
1075 SYMBOL_PRINT_NAME (symbol
));
1077 dwarf_expr_frame_base_1 (framefunc
, addr
, &base_data
, &base_size
);
1079 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
1083 frame_reg
= base_data
[0] - DW_OP_breg0
;
1084 buf_end
= read_sleb128 (base_data
+ 1,
1085 base_data
+ base_size
, &base_offset
);
1086 if (buf_end
!= base_data
+ base_size
)
1087 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1088 frame_reg
, SYMBOL_PRINT_NAME (symbol
));
1090 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
1092 /* The frame base is just the register, with no offset. */
1093 frame_reg
= base_data
[0] - DW_OP_reg0
;
1098 /* We don't know what to do with the frame base expression,
1099 so we can't trace this variable; give up. */
1100 error (_("Cannot describe location of symbol \"%s\"; "
1101 "DWARF 2 encoding not handled, "
1102 "first opcode in base data is 0x%x."),
1103 SYMBOL_PRINT_NAME (symbol
), base_data
[0]);
1106 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, frame_reg
);
1108 data
= read_sleb128 (data
+ 1, data
+ size
, &frame_offset
);
1110 fprintf_filtered (stream
, _("a variable at frame base reg $%s offset %s+%s"),
1111 gdbarch_register_name (gdbarch
, regno
),
1112 plongest (base_offset
), plongest (frame_offset
));
1114 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
)
1118 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, data
[0] - DW_OP_breg0
);
1120 data
= read_sleb128 (data
+ 1, data
+ size
, &offset
);
1122 fprintf_filtered (stream
,
1123 _("a variable at offset %s from base reg $%s"),
1125 gdbarch_register_name (gdbarch
, regno
));
1128 /* The location expression for a TLS variable looks like this (on a
1131 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1132 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1134 0x3 is the encoding for DW_OP_addr, which has an operand as long
1135 as the size of an address on the target machine (here is 8
1136 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1137 The operand represents the offset at which the variable is within
1138 the thread local storage. */
1141 && data
[size
- 1] == DW_OP_GNU_push_tls_address
1142 && data
[0] == DW_OP_addr
)
1144 CORE_ADDR offset
= dwarf2_read_address (gdbarch
,
1149 fprintf_filtered (stream
,
1150 _("a thread-local variable at offset %s "
1151 "in the thread-local storage for `%s'"),
1152 paddress (gdbarch
, offset
), objfile
->name
);
1154 data
+= 1 + addr_size
+ 1;
1157 fprintf_filtered (stream
,
1158 _("a variable with complex or multiple locations (DWARF2)"));
1163 /* Describe a single location, which may in turn consist of multiple
1167 locexpr_describe_location_1 (struct symbol
*symbol
, CORE_ADDR addr
,
1168 struct ui_file
*stream
, gdb_byte
*data
, int size
,
1169 struct objfile
*objfile
, unsigned int addr_size
)
1171 gdb_byte
*end
= data
+ size
;
1172 int piece_done
= 0, first_piece
= 1, bad
= 0;
1174 /* A multi-piece description consists of multiple sequences of bytes
1175 each followed by DW_OP_piece + length of piece. */
1183 fprintf_filtered (stream
, _(", and "));
1185 data
= locexpr_describe_location_piece (symbol
, stream
, addr
, objfile
,
1186 data
, size
, addr_size
);
1189 else if (data
[0] == DW_OP_piece
)
1193 data
= read_uleb128 (data
+ 1, end
, &bytes
);
1195 fprintf_filtered (stream
, _(" [%s-byte piece]"), pulongest (bytes
));
1206 if (bad
|| data
> end
)
1207 error (_("Corrupted DWARF2 expression for \"%s\"."),
1208 SYMBOL_PRINT_NAME (symbol
));
1211 /* Print a natural-language description of SYMBOL to STREAM. This
1212 version is for a symbol with a single location. */
1215 locexpr_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
1216 struct ui_file
*stream
)
1218 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1219 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
1220 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
1222 locexpr_describe_location_1 (symbol
, addr
, stream
, dlbaton
->data
, dlbaton
->size
,
1223 objfile
, addr_size
);
1226 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1227 any necessary bytecode in AX. */
1230 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
1231 struct agent_expr
*ax
, struct axs_value
*value
)
1233 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1235 dwarf2_tracepoint_var_ref (symbol
, gdbarch
, ax
, value
,
1236 dlbaton
->data
, dlbaton
->size
);
1239 /* The set of location functions used with the DWARF-2 expression
1241 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
1242 locexpr_read_variable
,
1243 locexpr_read_needs_frame
,
1244 locexpr_describe_location
,
1245 locexpr_tracepoint_var_ref
1249 /* Wrapper functions for location lists. These generally find
1250 the appropriate location expression and call something above. */
1252 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1253 evaluator to calculate the location. */
1254 static struct value
*
1255 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
1257 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1262 data
= find_location_expression (dlbaton
, &size
,
1263 frame
? get_frame_address_in_block (frame
)
1267 val
= allocate_value (SYMBOL_TYPE (symbol
));
1268 VALUE_LVAL (val
) = not_lval
;
1269 set_value_optimized_out (val
, 1);
1272 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, data
, size
,
1278 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1280 loclist_read_needs_frame (struct symbol
*symbol
)
1282 /* If there's a location list, then assume we need to have a frame
1283 to choose the appropriate location expression. With tracking of
1284 global variables this is not necessarily true, but such tracking
1285 is disabled in GCC at the moment until we figure out how to
1291 /* Print a natural-language description of SYMBOL to STREAM. This
1292 version applies when there is a list of different locations, each
1293 with a specified address range. */
1296 loclist_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
1297 struct ui_file
*stream
)
1299 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1300 CORE_ADDR low
, high
;
1301 gdb_byte
*loc_ptr
, *buf_end
;
1302 int length
, first
= 1;
1303 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
1304 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
1305 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1306 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
1307 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
1308 /* Adjust base_address for relocatable objects. */
1309 CORE_ADDR base_offset
= ANOFFSET (objfile
->section_offsets
,
1310 SECT_OFF_TEXT (objfile
));
1311 CORE_ADDR base_address
= dlbaton
->base_address
+ base_offset
;
1313 loc_ptr
= dlbaton
->data
;
1314 buf_end
= dlbaton
->data
+ dlbaton
->size
;
1316 fprintf_filtered (stream
, _("multi-location ("));
1318 /* Iterate through locations until we run out. */
1321 if (buf_end
- loc_ptr
< 2 * addr_size
)
1322 error (_("Corrupted DWARF expression for symbol \"%s\"."),
1323 SYMBOL_PRINT_NAME (symbol
));
1325 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
1326 loc_ptr
+= addr_size
;
1328 /* A base-address-selection entry. */
1329 if (low
== base_mask
)
1331 base_address
= dwarf2_read_address (gdbarch
,
1332 loc_ptr
, buf_end
, addr_size
);
1333 fprintf_filtered (stream
, _("[base address %s]"),
1334 paddress (gdbarch
, base_address
));
1335 loc_ptr
+= addr_size
;
1339 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
1340 loc_ptr
+= addr_size
;
1342 /* An end-of-list entry. */
1343 if (low
== 0 && high
== 0)
1345 /* Indicate the end of the list, for readability. */
1346 fprintf_filtered (stream
, _(")"));
1350 /* Otherwise, a location expression entry. */
1351 low
+= base_address
;
1352 high
+= base_address
;
1354 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
1357 /* Separate the different locations with a semicolon. */
1361 fprintf_filtered (stream
, _("; "));
1363 /* (It would improve readability to print only the minimum
1364 necessary digits of the second number of the range.) */
1365 fprintf_filtered (stream
, _("range %s-%s, "),
1366 paddress (gdbarch
, low
), paddress (gdbarch
, high
));
1368 /* Now describe this particular location. */
1369 locexpr_describe_location_1 (symbol
, low
, stream
, loc_ptr
, length
,
1370 objfile
, addr_size
);
1376 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1377 any necessary bytecode in AX. */
1379 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
1380 struct agent_expr
*ax
, struct axs_value
*value
)
1382 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1386 data
= find_location_expression (dlbaton
, &size
, ax
->scope
);
1388 dwarf2_tracepoint_var_ref (symbol
, gdbarch
, ax
, value
, data
, size
);
1391 /* The set of location functions used with the DWARF-2 expression
1392 evaluator and location lists. */
1393 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
1394 loclist_read_variable
,
1395 loclist_read_needs_frame
,
1396 loclist_describe_location
,
1397 loclist_tracepoint_var_ref