gdb: Fix numerical field extraction for target description "flags"
authorShahab Vahedi <shahab@synopsys.com>
Mon, 26 Jul 2021 12:51:35 +0000 (14:51 +0200)
committerShahab Vahedi <shahab@synopsys.com>
Mon, 26 Jul 2021 13:05:09 +0000 (15:05 +0200)
commit70417f28b5b6d01e130fe506bf25e1a5c104573b
treee1dc64de6e272b21ca0f4ddfc5b8d6e1a991b7dd
parent86271cfa326990fc05b5abada2831da161c0bf4b
gdb: Fix numerical field extraction for target description "flags"

The "val_print_type_code_flags ()" function is responsible for
extraction of fields for "flags" data type.  These data types are
used when describing a custom register type in a target description
XML.  The logic used for the extraction though is not sound:

    unsigned field_len = TYPE_FIELD_BITSIZE (type, field);
    ULONGEST field_val
      = val >> (TYPE_FIELD_BITPOS (type, field) - field_len + 1);

TYPE_FIELD_BITSIZE: The bit length of the field to be extracted.
TYPE_FIELD_BITPOS:  The starting position of the field; 0 is LSB.
val:                The register value.

Imagine you have a field that starts at position 1 and its length
is 4 bits.  According to the third line of the code snippet the
shifting right would become "val >> -2", or "val >> 0xfff...fe"
to be precise.  That will result in a "field_val" of 0.

The correct extraction should be:

    ULONGEST field_val = val >> TYPE_FIELD_BITPOS (type, field);

The rest of the algorithm that masks out the higher bits is OK.

gdb/ChangeLog:
2021-07-26  Shahab Vahedi  <shahab@synopsys.com>
            Simon Marchi  <simon.marchi@efficios.com>

PR gdb/28103
* valprint.c (val_print_type_code_flags): Merely shift the VAL
to the right to get rid of the lower bits.
(test_print_flags): New.
(_initialize_valprint): Invoke the "test_print_flags" as a unit-test.

Co-Authored-By: Simon Marchi <simon.marchi@efficios.com>
gdb/ChangeLog
gdb/valprint.c
This page took 0.033663 seconds and 4 git commands to generate.