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
);
388 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
395 write_pieced_value (struct value
*to
, struct value
*from
)
399 ULONGEST bytes_to_skip
;
400 const gdb_byte
*contents
;
401 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (to
);
402 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (to
));
407 set_value_optimized_out (to
, 1);
411 contents
= value_contents (from
);
412 bytes_to_skip
= value_offset (to
);
413 type_len
= TYPE_LENGTH (value_type (to
));
414 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
416 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
418 long dest_offset
, source_offset
;
420 if (bytes_to_skip
> 0 && bytes_to_skip
>= p
->size
)
422 bytes_to_skip
-= p
->size
;
426 if (this_size
> type_len
- offset
)
427 this_size
= type_len
- offset
;
428 if (bytes_to_skip
> 0)
430 dest_offset
= bytes_to_skip
;
432 this_size
-= bytes_to_skip
;
438 source_offset
= offset
;
443 case DWARF_VALUE_REGISTER
:
445 struct gdbarch
*arch
= get_frame_arch (frame
);
446 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->v
.expr
.value
);
447 int reg_offset
= dest_offset
;
449 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
450 && this_size
<= register_size (arch
, gdb_regnum
))
451 /* Big-endian, and we want less than full size. */
452 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
454 if (gdb_regnum
!= -1)
456 put_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
457 this_size
, contents
+ source_offset
);
461 error (_("Unable to write to DWARF register number %s"),
462 paddress (arch
, p
->v
.expr
.value
));
466 case DWARF_VALUE_MEMORY
:
467 write_memory (p
->v
.expr
.value
+ dest_offset
,
468 contents
+ source_offset
, this_size
);
471 set_value_optimized_out (to
, 1);
479 copy_pieced_value_closure (struct value
*v
)
481 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
483 return allocate_piece_closure (c
->n_pieces
, c
->pieces
, c
->addr_size
);
487 free_pieced_value_closure (struct value
*v
)
489 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
495 /* Functions for accessing a variable described by DW_OP_piece. */
496 static struct lval_funcs pieced_value_funcs
= {
499 copy_pieced_value_closure
,
500 free_pieced_value_closure
503 /* Evaluate a location description, starting at DATA and with length
504 SIZE, to find the current location of variable of TYPE in the context
507 static struct value
*
508 dwarf2_evaluate_loc_desc (struct type
*type
, struct frame_info
*frame
,
509 gdb_byte
*data
, unsigned short size
,
510 struct dwarf2_per_cu_data
*per_cu
)
512 struct value
*retval
;
513 struct dwarf_expr_baton baton
;
514 struct dwarf_expr_context
*ctx
;
515 struct cleanup
*old_chain
;
519 retval
= allocate_value (type
);
520 VALUE_LVAL (retval
) = not_lval
;
521 set_value_optimized_out (retval
, 1);
526 baton
.objfile
= dwarf2_per_cu_objfile (per_cu
);
528 ctx
= new_dwarf_expr_context ();
529 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
531 ctx
->gdbarch
= get_objfile_arch (baton
.objfile
);
532 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
534 ctx
->read_reg
= dwarf_expr_read_reg
;
535 ctx
->read_mem
= dwarf_expr_read_mem
;
536 ctx
->get_frame_base
= dwarf_expr_frame_base
;
537 ctx
->get_frame_cfa
= dwarf_expr_frame_cfa
;
538 ctx
->get_tls_address
= dwarf_expr_tls_address
;
540 dwarf_expr_eval (ctx
, data
, size
);
541 if (ctx
->num_pieces
> 0)
543 struct piece_closure
*c
;
544 struct frame_id frame_id
= get_frame_id (frame
);
546 c
= allocate_piece_closure (ctx
->num_pieces
, ctx
->pieces
,
548 retval
= allocate_computed_value (type
, &pieced_value_funcs
, c
);
549 VALUE_FRAME_ID (retval
) = frame_id
;
553 switch (ctx
->location
)
555 case DWARF_VALUE_REGISTER
:
557 struct gdbarch
*arch
= get_frame_arch (frame
);
558 CORE_ADDR dwarf_regnum
= dwarf_expr_fetch (ctx
, 0);
559 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_regnum
);
561 if (gdb_regnum
!= -1)
562 retval
= value_from_register (type
, gdb_regnum
, frame
);
564 error (_("Unable to access DWARF register number %s"),
565 paddress (arch
, dwarf_regnum
));
569 case DWARF_VALUE_MEMORY
:
571 CORE_ADDR address
= dwarf_expr_fetch (ctx
, 0);
572 int in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
574 retval
= allocate_value (type
);
575 VALUE_LVAL (retval
) = lval_memory
;
576 set_value_lazy (retval
, 1);
578 set_value_stack (retval
, 1);
579 set_value_address (retval
, address
);
583 case DWARF_VALUE_STACK
:
585 ULONGEST value
= (ULONGEST
) dwarf_expr_fetch (ctx
, 0);
587 size_t n
= ctx
->addr_size
;
589 retval
= allocate_value (type
);
590 contents
= value_contents_raw (retval
);
591 if (n
> TYPE_LENGTH (type
))
592 n
= TYPE_LENGTH (type
);
593 store_unsigned_integer (contents
, n
,
594 gdbarch_byte_order (ctx
->gdbarch
),
599 case DWARF_VALUE_LITERAL
:
604 retval
= allocate_value (type
);
605 contents
= value_contents_raw (retval
);
606 if (n
> TYPE_LENGTH (type
))
607 n
= TYPE_LENGTH (type
);
608 memcpy (contents
, ctx
->data
, n
);
613 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
617 set_value_initialized (retval
, ctx
->initialized
);
619 do_cleanups (old_chain
);
624 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
626 struct needs_frame_baton
631 /* Reads from registers do require a frame. */
633 needs_frame_read_reg (void *baton
, int regnum
)
635 struct needs_frame_baton
*nf_baton
= baton
;
637 nf_baton
->needs_frame
= 1;
641 /* Reads from memory do not require a frame. */
643 needs_frame_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
645 memset (buf
, 0, len
);
648 /* Frame-relative accesses do require a frame. */
650 needs_frame_frame_base (void *baton
, gdb_byte
**start
, size_t * length
)
652 static gdb_byte lit0
= DW_OP_lit0
;
653 struct needs_frame_baton
*nf_baton
= baton
;
658 nf_baton
->needs_frame
= 1;
661 /* CFA accesses require a frame. */
664 needs_frame_frame_cfa (void *baton
)
666 struct needs_frame_baton
*nf_baton
= baton
;
668 nf_baton
->needs_frame
= 1;
672 /* Thread-local accesses do require a frame. */
674 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
676 struct needs_frame_baton
*nf_baton
= baton
;
678 nf_baton
->needs_frame
= 1;
682 /* Return non-zero iff the location expression at DATA (length SIZE)
683 requires a frame to evaluate. */
686 dwarf2_loc_desc_needs_frame (gdb_byte
*data
, unsigned short size
,
687 struct dwarf2_per_cu_data
*per_cu
)
689 struct needs_frame_baton baton
;
690 struct dwarf_expr_context
*ctx
;
692 struct cleanup
*old_chain
;
694 baton
.needs_frame
= 0;
696 ctx
= new_dwarf_expr_context ();
697 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
699 ctx
->gdbarch
= get_objfile_arch (dwarf2_per_cu_objfile (per_cu
));
700 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
702 ctx
->read_reg
= needs_frame_read_reg
;
703 ctx
->read_mem
= needs_frame_read_mem
;
704 ctx
->get_frame_base
= needs_frame_frame_base
;
705 ctx
->get_frame_cfa
= needs_frame_frame_cfa
;
706 ctx
->get_tls_address
= needs_frame_tls_address
;
708 dwarf_expr_eval (ctx
, data
, size
);
710 in_reg
= ctx
->location
== DWARF_VALUE_REGISTER
;
712 if (ctx
->num_pieces
> 0)
716 /* If the location has several pieces, and any of them are in
717 registers, then we will need a frame to fetch them from. */
718 for (i
= 0; i
< ctx
->num_pieces
; i
++)
719 if (ctx
->pieces
[i
].location
== DWARF_VALUE_REGISTER
)
723 do_cleanups (old_chain
);
725 return baton
.needs_frame
|| in_reg
;
728 /* This struct keeps track of the pieces that make up a multi-location
729 object, for use in agent expression generation. It is
730 superficially similar to struct dwarf_expr_piece, but
731 dwarf_expr_piece is designed for use in immediate evaluation, and
732 does not, for example, have a way to record both base register and
737 /* Memory vs register, etc */
738 enum axs_lvalue_kind kind
;
740 /* If non-zero, number of bytes in this fragment */
743 /* (GDB-numbered) reg, or base reg if >= 0 */
746 /* offset from reg */
751 dwarf2_tracepoint_var_loc (struct symbol
*symbol
,
752 struct agent_expr
*ax
,
753 struct axs_var_loc
*loc
,
754 struct gdbarch
*gdbarch
,
755 gdb_byte
*data
, gdb_byte
*end
)
757 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
759 loc
->kind
= axs_lvalue_register
;
760 loc
->reg
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, data
[0] - DW_OP_reg0
);
763 else if (data
[0] == DW_OP_regx
)
767 data
= read_uleb128 (data
+ 1, end
, ®
);
768 loc
->kind
= axs_lvalue_register
;
769 loc
->reg
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
);
771 else if (data
[0] == DW_OP_fbreg
)
774 struct symbol
*framefunc
;
776 LONGEST frame_offset
;
779 LONGEST base_offset
= 0;
781 b
= block_for_pc (ax
->scope
);
784 error (_("No block found for address"));
786 framefunc
= block_linkage_function (b
);
789 error (_("No function found for block"));
791 dwarf_expr_frame_base_1 (framefunc
, ax
->scope
,
792 &base_data
, &base_size
);
794 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
798 frame_reg
= base_data
[0] - DW_OP_breg0
;
799 buf_end
= read_sleb128 (base_data
+ 1,
800 base_data
+ base_size
, &base_offset
);
801 if (buf_end
!= base_data
+ base_size
)
802 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
803 frame_reg
, SYMBOL_PRINT_NAME (symbol
));
805 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
807 /* The frame base is just the register, with no offset. */
808 frame_reg
= base_data
[0] - DW_OP_reg0
;
813 /* We don't know what to do with the frame base expression,
814 so we can't trace this variable; give up. */
815 error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
816 SYMBOL_PRINT_NAME (symbol
), base_data
[0]);
819 data
= read_sleb128 (data
+ 1, end
, &frame_offset
);
821 loc
->kind
= axs_lvalue_memory
;
822 loc
->reg
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, frame_reg
);
823 loc
->offset
= base_offset
+ frame_offset
;
825 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
)
830 reg
= data
[0] - DW_OP_breg0
;
831 data
= read_sleb128 (data
+ 1, end
, &offset
);
833 loc
->kind
= axs_lvalue_memory
;
834 loc
->reg
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
);
835 loc
->offset
= offset
;
838 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
839 data
[0], SYMBOL_PRINT_NAME (symbol
));
844 /* Given the location of a piece, issue bytecodes that will access it. */
847 dwarf2_tracepoint_var_access (struct agent_expr
*ax
,
848 struct axs_value
*value
,
849 struct axs_var_loc
*loc
)
851 value
->kind
= loc
->kind
;
855 case axs_lvalue_register
:
856 value
->u
.reg
= loc
->reg
;
859 case axs_lvalue_memory
:
860 ax_reg (ax
, loc
->reg
);
863 ax_const_l (ax
, loc
->offset
);
864 ax_simple (ax
, aop_add
);
869 internal_error (__FILE__
, __LINE__
, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
874 dwarf2_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
875 struct agent_expr
*ax
, struct axs_value
*value
,
876 gdb_byte
*data
, int size
)
878 gdb_byte
*end
= data
+ size
;
879 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
880 /* In practice, a variable is not going to be spread across
881 dozens of registers or memory locations. If someone comes up
882 with a real-world example, revisit this. */
884 struct axs_var_loc fragments
[MAX_FRAGS
];
885 int nfrags
= 0, frag
;
891 if (!data
|| size
== 0)
893 value
->optimized_out
= 1;
901 if (nfrags
== MAX_FRAGS
)
902 error (_("Too many pieces in location for \"%s\"."),
903 SYMBOL_PRINT_NAME (symbol
));
905 fragments
[nfrags
].bytes
= 0;
906 data
= dwarf2_tracepoint_var_loc (symbol
, ax
, &fragments
[nfrags
],
911 else if (data
[0] == DW_OP_piece
)
915 data
= read_uleb128 (data
+ 1, end
, &bytes
);
916 /* Only deal with 4 byte fragments for now. */
918 error (_("DW_OP_piece %s not supported in location for \"%s\"."),
919 pulongest (bytes
), SYMBOL_PRINT_NAME (symbol
));
920 fragments
[nfrags
- 1].bytes
= bytes
;
931 if (bad
|| data
> end
)
932 error (_("Corrupted DWARF expression for \"%s\"."),
933 SYMBOL_PRINT_NAME (symbol
));
935 /* If single expression, no pieces, convert to external format. */
938 dwarf2_tracepoint_var_access (ax
, value
, &fragments
[0]);
942 if (length
!= TYPE_LENGTH (value
->type
))
943 error (_("Inconsistent piece information for \"%s\"."),
944 SYMBOL_PRINT_NAME (symbol
));
946 /* Emit bytecodes to assemble the pieces into a single stack entry. */
948 for ((frag
= (byte_order
== BFD_ENDIAN_BIG
? 0 : nfrags
- 1));
950 (frag
+= (byte_order
== BFD_ENDIAN_BIG
? 1 : -1)))
954 /* shift the previous fragment up 32 bits */
956 ax_simple (ax
, aop_lsh
);
959 dwarf2_tracepoint_var_access (ax
, value
, &fragments
[frag
]);
963 case axs_lvalue_register
:
964 ax_reg (ax
, value
->u
.reg
);
967 case axs_lvalue_memory
:
969 extern int trace_kludge
; /* Ugh. */
971 gdb_assert (fragments
[frag
].bytes
== 4);
973 ax_trace_quick (ax
, 4);
974 ax_simple (ax
, aop_ref32
);
981 /* or the new fragment into the previous */
982 ax_zero_ext (ax
, 32);
983 ax_simple (ax
, aop_bit_or
);
987 value
->kind
= axs_rvalue
;
991 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
992 evaluator to calculate the location. */
993 static struct value
*
994 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
996 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
999 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
1000 dlbaton
->size
, dlbaton
->per_cu
);
1005 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1007 locexpr_read_needs_frame (struct symbol
*symbol
)
1009 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1011 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
,
1015 /* Describe a single piece of a location, returning an updated
1016 position in the bytecode sequence. */
1019 locexpr_describe_location_piece (struct symbol
*symbol
, struct ui_file
*stream
,
1020 CORE_ADDR addr
, struct objfile
*objfile
,
1021 gdb_byte
*data
, int size
, unsigned int addr_size
)
1023 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
1026 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
1028 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, data
[0] - DW_OP_reg0
);
1029 fprintf_filtered (stream
, _("a variable in $%s"),
1030 gdbarch_register_name (gdbarch
, regno
));
1033 else if (data
[0] == DW_OP_regx
)
1037 data
= read_uleb128 (data
+ 1, data
+ size
, ®
);
1038 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
);
1039 fprintf_filtered (stream
, _("a variable in $%s"),
1040 gdbarch_register_name (gdbarch
, regno
));
1042 else if (data
[0] == DW_OP_fbreg
)
1045 struct symbol
*framefunc
;
1047 LONGEST frame_offset
;
1048 gdb_byte
*base_data
;
1050 LONGEST base_offset
= 0;
1052 b
= block_for_pc (addr
);
1055 error (_("No block found for address for symbol \"%s\"."),
1056 SYMBOL_PRINT_NAME (symbol
));
1058 framefunc
= block_linkage_function (b
);
1061 error (_("No function found for block for symbol \"%s\"."),
1062 SYMBOL_PRINT_NAME (symbol
));
1064 dwarf_expr_frame_base_1 (framefunc
, addr
, &base_data
, &base_size
);
1066 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
1070 frame_reg
= base_data
[0] - DW_OP_breg0
;
1071 buf_end
= read_sleb128 (base_data
+ 1,
1072 base_data
+ base_size
, &base_offset
);
1073 if (buf_end
!= base_data
+ base_size
)
1074 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1075 frame_reg
, SYMBOL_PRINT_NAME (symbol
));
1077 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
1079 /* The frame base is just the register, with no offset. */
1080 frame_reg
= base_data
[0] - DW_OP_reg0
;
1085 /* We don't know what to do with the frame base expression,
1086 so we can't trace this variable; give up. */
1087 error (_("Cannot describe location of symbol \"%s\"; "
1088 "DWARF 2 encoding not handled, "
1089 "first opcode in base data is 0x%x."),
1090 SYMBOL_PRINT_NAME (symbol
), base_data
[0]);
1093 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, frame_reg
);
1095 data
= read_sleb128 (data
+ 1, data
+ size
, &frame_offset
);
1097 fprintf_filtered (stream
, _("a variable at frame base reg $%s offset %s+%s"),
1098 gdbarch_register_name (gdbarch
, regno
),
1099 plongest (base_offset
), plongest (frame_offset
));
1101 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
)
1105 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, data
[0] - DW_OP_breg0
);
1107 data
= read_sleb128 (data
+ 1, data
+ size
, &offset
);
1109 fprintf_filtered (stream
,
1110 _("a variable at offset %s from base reg $%s"),
1112 gdbarch_register_name (gdbarch
, regno
));
1115 /* The location expression for a TLS variable looks like this (on a
1118 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1119 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1121 0x3 is the encoding for DW_OP_addr, which has an operand as long
1122 as the size of an address on the target machine (here is 8
1123 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1124 The operand represents the offset at which the variable is within
1125 the thread local storage. */
1128 && data
[size
- 1] == DW_OP_GNU_push_tls_address
1129 && data
[0] == DW_OP_addr
)
1131 CORE_ADDR offset
= dwarf2_read_address (gdbarch
,
1136 fprintf_filtered (stream
,
1137 _("a thread-local variable at offset %s "
1138 "in the thread-local storage for `%s'"),
1139 paddress (gdbarch
, offset
), objfile
->name
);
1141 data
+= 1 + addr_size
+ 1;
1144 fprintf_filtered (stream
,
1145 _("a variable with complex or multiple locations (DWARF2)"));
1150 /* Describe a single location, which may in turn consist of multiple
1154 locexpr_describe_location_1 (struct symbol
*symbol
, CORE_ADDR addr
,
1155 struct ui_file
*stream
, gdb_byte
*data
, int size
,
1156 struct objfile
*objfile
, unsigned int addr_size
)
1158 gdb_byte
*end
= data
+ size
;
1159 int piece_done
= 0, first_piece
= 1, bad
= 0;
1161 /* A multi-piece description consists of multiple sequences of bytes
1162 each followed by DW_OP_piece + length of piece. */
1170 fprintf_filtered (stream
, _(", and "));
1172 data
= locexpr_describe_location_piece (symbol
, stream
, addr
, objfile
,
1173 data
, size
, addr_size
);
1176 else if (data
[0] == DW_OP_piece
)
1180 data
= read_uleb128 (data
+ 1, end
, &bytes
);
1182 fprintf_filtered (stream
, _(" [%s-byte piece]"), pulongest (bytes
));
1193 if (bad
|| data
> end
)
1194 error (_("Corrupted DWARF2 expression for \"%s\"."),
1195 SYMBOL_PRINT_NAME (symbol
));
1198 /* Print a natural-language description of SYMBOL to STREAM. This
1199 version is for a symbol with a single location. */
1202 locexpr_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
1203 struct ui_file
*stream
)
1205 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1206 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
1207 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
1209 locexpr_describe_location_1 (symbol
, addr
, stream
, dlbaton
->data
, dlbaton
->size
,
1210 objfile
, addr_size
);
1213 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1214 any necessary bytecode in AX. */
1217 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
1218 struct agent_expr
*ax
, struct axs_value
*value
)
1220 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1222 dwarf2_tracepoint_var_ref (symbol
, gdbarch
, ax
, value
,
1223 dlbaton
->data
, dlbaton
->size
);
1226 /* The set of location functions used with the DWARF-2 expression
1228 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
1229 locexpr_read_variable
,
1230 locexpr_read_needs_frame
,
1231 locexpr_describe_location
,
1232 locexpr_tracepoint_var_ref
1236 /* Wrapper functions for location lists. These generally find
1237 the appropriate location expression and call something above. */
1239 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1240 evaluator to calculate the location. */
1241 static struct value
*
1242 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
1244 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1249 data
= find_location_expression (dlbaton
, &size
,
1250 frame
? get_frame_address_in_block (frame
)
1254 val
= allocate_value (SYMBOL_TYPE (symbol
));
1255 VALUE_LVAL (val
) = not_lval
;
1256 set_value_optimized_out (val
, 1);
1259 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, data
, size
,
1265 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1267 loclist_read_needs_frame (struct symbol
*symbol
)
1269 /* If there's a location list, then assume we need to have a frame
1270 to choose the appropriate location expression. With tracking of
1271 global variables this is not necessarily true, but such tracking
1272 is disabled in GCC at the moment until we figure out how to
1278 /* Print a natural-language description of SYMBOL to STREAM. This
1279 version applies when there is a list of different locations, each
1280 with a specified address range. */
1283 loclist_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
1284 struct ui_file
*stream
)
1286 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1287 CORE_ADDR low
, high
;
1288 gdb_byte
*loc_ptr
, *buf_end
;
1289 int length
, first
= 1;
1290 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
1291 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
1292 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1293 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
1294 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
1295 /* Adjust base_address for relocatable objects. */
1296 CORE_ADDR base_offset
= ANOFFSET (objfile
->section_offsets
,
1297 SECT_OFF_TEXT (objfile
));
1298 CORE_ADDR base_address
= dlbaton
->base_address
+ base_offset
;
1300 loc_ptr
= dlbaton
->data
;
1301 buf_end
= dlbaton
->data
+ dlbaton
->size
;
1303 fprintf_filtered (stream
, _("multi-location ("));
1305 /* Iterate through locations until we run out. */
1308 if (buf_end
- loc_ptr
< 2 * addr_size
)
1309 error (_("Corrupted DWARF expression for symbol \"%s\"."),
1310 SYMBOL_PRINT_NAME (symbol
));
1312 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
1313 loc_ptr
+= addr_size
;
1315 /* A base-address-selection entry. */
1316 if (low
== base_mask
)
1318 base_address
= dwarf2_read_address (gdbarch
,
1319 loc_ptr
, buf_end
, addr_size
);
1320 fprintf_filtered (stream
, _("[base address %s]"),
1321 paddress (gdbarch
, base_address
));
1322 loc_ptr
+= addr_size
;
1326 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
1327 loc_ptr
+= addr_size
;
1329 /* An end-of-list entry. */
1330 if (low
== 0 && high
== 0)
1332 /* Indicate the end of the list, for readability. */
1333 fprintf_filtered (stream
, _(")"));
1337 /* Otherwise, a location expression entry. */
1338 low
+= base_address
;
1339 high
+= base_address
;
1341 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
1344 /* Separate the different locations with a semicolon. */
1348 fprintf_filtered (stream
, _("; "));
1350 /* (It would improve readability to print only the minimum
1351 necessary digits of the second number of the range.) */
1352 fprintf_filtered (stream
, _("range %s-%s, "),
1353 paddress (gdbarch
, low
), paddress (gdbarch
, high
));
1355 /* Now describe this particular location. */
1356 locexpr_describe_location_1 (symbol
, low
, stream
, loc_ptr
, length
,
1357 objfile
, addr_size
);
1363 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1364 any necessary bytecode in AX. */
1366 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
1367 struct agent_expr
*ax
, struct axs_value
*value
)
1369 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1373 data
= find_location_expression (dlbaton
, &size
, ax
->scope
);
1375 dwarf2_tracepoint_var_ref (symbol
, gdbarch
, ax
, value
, data
, size
);
1378 /* The set of location functions used with the DWARF-2 expression
1379 evaluator and location lists. */
1380 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
1381 loclist_read_variable
,
1382 loclist_read_needs_frame
,
1383 loclist_describe_location
,
1384 loclist_tracepoint_var_ref