Re: [PATCH 3/9] PR gdb/21226: Take DWARF stack value pieces from LSB end

Andreas Arnez
2017-04-18T16:32:03Z

On Thu, Apr 13 2017, Simon Marchi wrote:

> On 2017-04-07 13:38, Andreas Arnez wrote:
>> When taking a DW_OP_piece or DW_OP_bit_piece from a DW_OP_stack_value,
>> the
>> existing logic always takes the piece from the lowest-addressed end,
>> which
>> is wrong on big-endian targets.
>
> I'd like if you could clarify this (just here not necessarily in the
> patch). DWARF locations are computed inside GDB, on the host. So does it
> really depend on the target endianness, or it's that of the host, or both?
>
> Let's consider these cases of remote debugging:
>
> host -> target
> x86 -> x86
> x86 -> s390
> s390 -> x86
> s390 -> s390
>
> In which cases is the value found at the high memory address vs low memory
> address?

DWARF stack values are represented in the target architecture's
endianness, independent from the host. For instance, if the target is
x86, then the DWARF stack uses little-endian byte order, even on s390
hosts. See also dwarf_expr_context::execute_stack_op().

[...]

>>
>> case DWARF_VALUE_STACK:
>> {
>> - size_t n = this_size;
>> + struct objfile *objfile = dwarf2_per_cu_objfile (c->per_cu);
>> + struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
>> + ULONGEST obj_size = 8 * TYPE_LENGTH (value_type (p->v.value));
>
> It would be really nice for the readers if you could put some comment like
> this, even though it may seem obvious to you:
>
> /* The size of a DWARF stack value. */
> ULONGEST obj_size = 8 * TYPE_LENGTH (value_type (p->v.value));
>
> I found I had to add them to the code to be able to follow.

OK, but it seems that the variable name 'obj_size' causes the confusion;
so I'd rather skip the comment, change the variable name to
stack_value_size_bits instead and let it speak for itself.

>
>>
>> - if (n > c->addr_size - source_offset)
>> - n = (c->addr_size >= source_offset
>> - ? c->addr_size - source_offset
>> - : 0);
>> - if (n == 0)
>> - {
>> - /* Nothing. */
>> - }
>> - else
>> - {
>> - const gdb_byte *val_bytes = value_contents_all (p->v.value);
>
>> + /* Use zeroes if piece reaches beyond stack value. */
>> + if (p->size > obj_size)
>> + break;
>
> Does this happen, for example, if a DWARF stack value is 32 bits long, but
> the piece is 64 bits? I suppose that's not something we'd want a compiler
> to emit, and would be considered a bug in the compiler?

Yes, right now I would consider it a compiler bug. And I haven't seen a
compiler emit such DWARF code yet. This is just to avoid crashing GDB,
and to behave predictably instead if it occurs anyway. (If you're
interested, I've written more thoughts about this topic in section 5,
"padding", of this article:
https://sourceware.org/ml/gdb/2016-01/msg00013.html)

>
> How does breaking out of the loop will use zeroes? Is the value buffer
> cleared beforehand?

Yes, value_contents_raw returns a zeroed buffer.

>
>>
>> - intermediate_buffer = val_bytes + source_offset;
>> - }
>> + /* Piece is anchored at least significant bit end. */
>> + if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
>> + source_offset_bits += obj_size - p->size;
>
> Just a nit, but I find it more readable when there's an empty line between
> the if and the following lines not included in the if (so here, right
> where I cut the quote). It reads like two separate sentences:
>
> - If the byte order is big endian, adjust offset in the source.
> - Copy bitwise from the source buffer to the destination buffer.

OK.

>
>> + copy_bitwise (contents, dest_offset_bits,
>> + value_contents_all (p->v.value),
>> + source_offset_bits,
>> + this_size_bits, bits_big_endian);
>
> Thanks,
>
> Simon

Thanks,
Andreas