gdb: Carry default property type around with dynamic properties
authorAndrew Burgess <andrew.burgess@embecosm.com>
Wed, 8 May 2019 12:16:03 +0000 (13:16 +0100)
committerAndrew Burgess <andrew.burgess@embecosm.com>
Fri, 12 Jul 2019 11:09:54 +0000 (12:09 +0100)
commit9a49df9d4bfc7ff03fed751e12b1bc32fbee4fb2
tree8cc573908053c52a291263c54c6a214541b2ab1c
parentb86352cfc17fb7f07d5da9efcfd59f8b2bd08eee
gdb: Carry default property type around with dynamic properties

This commit is preparation for the next one, with the aim of better
supporting signed dynamic properties on targets where the address size
specified in the DWARF headers is smaller than a CORE_ADDR, for
example debugging an i386 application on x86-64.

Consider this small Fortran program 'bounds.f90':

    program test
      integer, allocatable :: array (:)
      allocate (array (-5:5))
      array(3) = 1
    end program test

Compiled with 'gfortran -m32 -g3 -O0 -o bounds bounds.f90'.  The DWARF
for 'array' looks like this:

   <2><97>: Abbrev Number: 10 (DW_TAG_variable)
      <98>   DW_AT_name        : (indirect string, offset: 0x0): array
      <9c>   DW_AT_decl_file   : 1
      <9d>   DW_AT_decl_line   : 2
      <9e>   DW_AT_type        : <0xaf>
      <a2>   DW_AT_location    : 2 byte block: 91 58              (DW_OP_fbreg: -40)
   <2><a5>: Abbrev Number: 11 (DW_TAG_lexical_block)
      <a6>   DW_AT_low_pc      : 0x80485c3
      <aa>   DW_AT_high_pc     : 0x8b
   <2><ae>: Abbrev Number: 0
   <1><af>: Abbrev Number: 12 (DW_TAG_array_type)
      <b0>   DW_AT_data_location: 2 byte block: 97 6              (DW_OP_push_object_address; DW_OP_deref)
      <b3>   DW_AT_allocated   : 4 byte block: 97 6 30 2e         (DW_OP_push_object_address; DW_OP_deref; DW_OP_lit0; DW_OP_ne)
      <b8>   DW_AT_type        : <0x2a>
   <2><bc>: Abbrev Number: 13 (DW_TAG_subrange_type)
      <bd>   DW_AT_lower_bound : 4 byte block: 97 23 10 6         (DW_OP_push_object_address; DW_OP_plus_uconst: 16; DW_OP_deref)
      <c2>   DW_AT_upper_bound : 4 byte block: 97 23 14 6         (DW_OP_push_object_address; DW_OP_plus_uconst: 20; DW_OP_deref)
      <c7>   DW_AT_byte_stride : 6 byte block: 97 23 c 6 34 1e    (DW_OP_push_object_address; DW_OP_plus_uconst: 12; DW_OP_deref; DW_OP_lit4; DW_OP_mul)
   <2><ce>: Abbrev Number: 0

If we look at the DW_AT_lower_bound attribute, which will become a
dynamic property that GDB evaluates when needed by calling
dwarf2_evaluate_property.

The process of evaluating a dynamic property requires GDB to execute
each DW_OP_* operation, the results of these operations is held on a
stack of 'struct value *'s.

When the entire expression is evaluated the result is on top of the
stack.

If we look at DW_AT_lower_bound then the last operation is
DW_OP_deref, this loads a signed address the size of which matches the
DWARF address size, and so in our i386 on x86-64 situation, the top of
the stack will be a signed 4-byte value.

The problem is how these values are fetched from the stack.  Currently
they are always fetched by a call to dwarf_expr_context::fetch_address,
which converts the value to an unsigned value with a length matching
the values current length, before converting to a CORE_ADDR.  This
means we loose the signed nature of the property.

I wonder if the best solution for dealing with signed properties will
be to move away from an over reliance on fetch_address, and instead
come up with a new solution that considers the current type of the
value on the stack, and the type that the value needs to become;
basically a solution built around casting rather than assuming we
always want an address.

However, before we can start to even think about moving away from
fetch_address, there is a more urgent issue to fix, which is we don't
currently know what type each property should be.  We just hold the
value of the property in a CORE_ADDR as returned by fetch_address, and
rely on higher level code (outside of the DWARF expression evaluation
code) to fix things up for us.  This is what this patch aims to
address.

When creating a dynamic property (see attr_to_dynamic_prop in
dwarf2read.c) we can sometimes figure out the type of a property; if
the property is a reference to another DIE then it will have a
DW_AT_type attribute.

However, the DW_AT_lower_bound case above isn't a reference to another
DIE, it's just a DWARF expression.  We don't have any indication for
what type the property should have.

Luckily, the DWARF spec helps us out, for the lower and upper bounds
5.13 of the DWARFv5 spec tells us that without any other type
information the bounds are signed integers the same size as a DWARF
address.

It is my belief that we can find a suitable default type for every
dynamic property, either specified explicitly in the DWARF spec, or we
can infer an obvious choice if the spec doesn't help us.

This commit extends the creation of all dynamic properties to include
suggesting a suitable default type, all dynamic properties now always
carry their type around with them.

In later commits we can use this property type to ensure that the
value we extract from the DWARF stack is handled in a suitable manor
to correctly maintain its sign extension.

There should be no user visible changes from this commit.  The actual
fix to correctly support negative array bounds will come later.

gdb/ChangeLog:

* dwarf2loc.c (dwarf2_evaluate_property): Update to take account
of changes to field names, and use new is_reference field to
decide if a property is a reference or not.
* dwarf2loc.h (struct dwarf2_locexpr_baton): Add 'is_reference'
field.
(struct dwarf2_property_baton): Update header comment, rename
'referenced_type' to 'property_type' and update comments.
* dwarf2read.c (attr_to_dynamic_prop): Add extra parameter to hold
default property type, store in property baton, update to take
accound of renamed field.
(read_func_scope): Update call to attr_to_dynamic_prop.
(read_array_type): Likewise.
(dwarf2_per_cu_addr_sized_int_type): New function.
(read_subrange_index_type): Move type finding code to
dwarf2_per_cu_addr_sized_int_type.
(read_subrange_type): Update calls to attr_to_dynamic_prop.
(dwarf2_per_cu_addr_type): New function.
(set_die_type): Update calls to attr_to_dynamic_prop.
gdb/ChangeLog
gdb/dwarf2loc.c
gdb/dwarf2loc.h
gdb/dwarf2read.c
This page took 0.025519 seconds and 4 git commands to generate.