This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC/RFA] print arrays with indexes


Hello,

This is a suggestion to enhance GDB when printing arrays. The idea is
that the default and currently only way for GDB to print arrays is to
print them as a sequence of elements, separated by a coma, like this:

        (gdb) p array
        $1 = {1, 2, 3, 4}

The idea is that, for large arrays, it's sometimes convenient to print
them in a form where the index of each element is printed prior to the
array element itself. In Ada, the notation for doing is

        <index> => <array element>

So, the idea would be for GDB to print the above array as:

        (gdb) p array
        $2 = {0 => 1, 1 => 2, 2 => 3, 3 => 4}

For my personal usage, I find this to be very convenient when dealing
with super large arrays such as the regnum-to-regname arrays that we
frequently have in GDB. I'm always concerned about being off by one
when counting. Having the index simplifies the process a little bit.

So, the suggestion is to have the feature being controled by a set/show
command; I suggest:

        (gdb) set/show print array-indexes

With a default of "off", to preserve the current behavior.

For the moment, the patch I am submitting here is using the Ada notation,
because C/C++/ObjC don't provide this way of "qualifying" each element.
Not sure about Fortran or Pascal. We can certainly make this a language
method, with a default method.

Testcase and documentation will come shortly after we've agreed on
the principle and the design.

2005-09-06  Joel Brobecker  <brobecker@adacore.com>

        * valprint.c (print_array_indexes): New variable.
        (show_print_array_indexes): New function.
        (print_array_indexes_p): New function.
        (maybe_print_array_index): New function.
        (val_print_array_elements): Add new parameter real_index_offset.
        Print the index of each element if required by the user.
        (_initialize_valprint): Add new print-array-indexes set/show command.
        * valprint.h (print_array_indexes_p): Add declaration.
        (maybe_print_array_index): Likewise.
        (val_print_array_elements): Update parameter list.
        * c-valprint.c (c_val_print): Update call to val_print_array_elements.
        * p-valprint.c (pascal_val_print): Likewise.
        * ada-valprint.c (ada_get_array_low_bound_and_type): New function,
        mostly extracted from print_optional_low_bound().
        (print_optional_low_bound): Replace extracted code by call to
        ada_get_array_low_bound_and_type(). Stop printing the low bound
        if indexes will be printed for all elements of the array.
        (val_print_packed_array_elements): Print the index of each element
        of the array if necessary.
        (ada_val_print_1): For non-packed arrays, compute the array low
        bound, and pass it to val_print_array_elements().

Tested on x86-linux, no regression.

Opinions?
-- 
Joel

Attachment: indexes.diff
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]