* [RFC/RFA] print arrays with indexes
@ 2005-09-06 20:20 Joel Brobecker
2005-09-06 20:57 ` Daniel Jacobowitz
2005-09-06 21:45 ` Jim Blandy
0 siblings, 2 replies; 37+ messages in thread
From: Joel Brobecker @ 2005-09-06 20:20 UTC (permalink / raw)
To: gdb-patches
[-- Attachment #1: Type: text/plain, Size: 2864 bytes --]
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 #2: indexes.diff --]
[-- Type: text/plain, Size: 9749 bytes --]
Index: valprint.c
===================================================================
RCS file: /cvs/src/src/gdb/valprint.c,v
retrieving revision 1.54
diff -u -p -r1.54 valprint.c
--- valprint.c 10 Jun 2005 06:07:32 -0000 1.54
+++ valprint.c 6 Sep 2005 20:18:00 -0000
@@ -100,6 +100,17 @@ Default output radix for printing of val
}
int output_format = 0;
+/* By default we print arrays without printing the index of each element in
+ the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
+
+static int print_array_indexes = 0;
+static void
+show_print_array_indexes (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c, const char *value)
+{
+ fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
+}
+
/* Print repeat counts if there are more than this many repetitions of an
element in an array. Referenced by the low level language dependent
print routines. */
@@ -859,9 +870,42 @@ print_char_chars (struct ui_file *stream
}
}
+/* Return non-zero if the debugger should print the index of each element
+ when printing array values. */
+
+int
+print_array_indexes_p (void)
+{
+ return print_array_indexes;
+}
+
+/* Print on STREAM using the given FORMAT the index for the element
+ at INDEX of an array whose index type is INDEX_TYPE. */
+
+void
+maybe_print_array_index (struct type *index_type, LONGEST index,
+ struct ui_file *stream, int format,
+ enum val_prettyprint pretty)
+{
+ struct value *index_value;
+
+ if (!print_array_indexes)
+ return;
+
+ index_value = value_from_longest (index_type, index);
+
+ LA_VALUE_PRINT (index_value, stream, format, pretty);
+ fprintf_filtered (stream, " => ");
+}
+
/* Called by various <lang>_val_print routines to print elements of an
array in the form "<elem1>, <elem2>, <elem3>, ...".
+ Some languages such as Ada allow the user to specify arrays where
+ the index of the first element is not zero. REAL_INDEX_OFFSET is
+ the user-level index of the first element of the array. For many
+ languages such as C or C++, it is always zero.
+
(FIXME?) Assumes array element separator is a comma, which is correct
for all languages currently handled.
(FIXME?) Some languages have a notation for repeated array elements,
@@ -873,11 +917,11 @@ val_print_array_elements (struct type *t
CORE_ADDR address, struct ui_file *stream,
int format, int deref_ref,
int recurse, enum val_prettyprint pretty,
- unsigned int i)
+ unsigned int i, LONGEST real_index_offset)
{
unsigned int things_printed = 0;
unsigned len;
- struct type *elttype;
+ struct type *elttype, *index_type;
unsigned eltlen;
/* Position of the array element we are examining to see
whether it is repeated. */
@@ -888,6 +932,7 @@ val_print_array_elements (struct type *t
elttype = TYPE_TARGET_TYPE (type);
eltlen = TYPE_LENGTH (check_typedef (elttype));
len = TYPE_LENGTH (type) / eltlen;
+ index_type = TYPE_INDEX_TYPE (type);
annotate_array_section_begin (i, elttype);
@@ -906,6 +951,8 @@ val_print_array_elements (struct type *t
}
}
wrap_here (n_spaces (2 + 2 * recurse));
+ maybe_print_array_index (index_type, i + real_index_offset,
+ stream, format, pretty);
rep1 = i + 1;
reps = 1;
@@ -1396,6 +1443,12 @@ Show the default input and output number
Use 'show input-radix' or 'show output-radix' to independently show each."),
&showlist);
+ add_setshow_boolean_cmd ("array-indexes", class_support,
+ &print_array_indexes, _("\
+Set printing of array indexes."), _("\
+Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
+ &setprintlist, &showprintlist);
+
/* Give people the defaults which they are used to. */
prettyprint_structs = 0;
prettyprint_arrays = 0;
Index: valprint.h
===================================================================
RCS file: /cvs/src/src/gdb/valprint.h,v
retrieving revision 1.10
diff -u -p -r1.10 valprint.h
--- valprint.h 9 May 2005 21:20:35 -0000 1.10
+++ valprint.h 6 Sep 2005 20:18:00 -0000
@@ -50,10 +50,16 @@ extern int output_format;
extern int stop_print_at_null; /* Stop printing at null char? */
+extern int print_array_indexes_p (void);
+
+extern void maybe_print_array_index (struct type *index_type, LONGEST index,
+ struct ui_file *stream, int format,
+ enum val_prettyprint pretty);
+
extern void val_print_array_elements (struct type *, const gdb_byte *,
CORE_ADDR, struct ui_file *, int,
int, int, enum val_prettyprint,
- unsigned int);
+ unsigned int, LONGEST);
extern void val_print_type_code_int (struct type *, const gdb_byte *,
struct ui_file *);
Index: c-valprint.c
===================================================================
RCS file: /cvs/src/src/gdb/c-valprint.c,v
retrieving revision 1.37
diff -u -p -r1.37 c-valprint.c
--- c-valprint.c 9 May 2005 21:20:30 -0000 1.37
+++ c-valprint.c 6 Sep 2005 20:18:00 -0000
@@ -133,7 +133,7 @@ c_val_print (struct type *type, const gd
i = 0;
}
val_print_array_elements (type, valaddr + embedded_offset, address, stream,
- format, deref_ref, recurse, pretty, i);
+ format, deref_ref, recurse, pretty, i, 0);
fprintf_filtered (stream, "}");
}
break;
Index: ada-valprint.c
===================================================================
RCS file: /cvs/src/src/gdb/ada-valprint.c,v
retrieving revision 1.23
diff -u -p -r1.23 ada-valprint.c
--- ada-valprint.c 9 May 2005 21:20:30 -0000 1.23
+++ ada-valprint.c 6 Sep 2005 20:18:00 -0000
@@ -75,6 +75,44 @@ adjust_type_signedness (struct type *typ
TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
}
+/* Assuming TYPE is a simple, non-empty array type, compute the lower
+ bound and the array index type. Save the low bound into LOW_BOUND
+ if not NULL. Save the index type in INDEX_TYPE if not NULL.
+
+ Return 1 if the operation was successful. Return zero otherwise,
+ in which case the value of LOW_BOUND and INDEX_TYPE is undefined. */
+
+static int
+ada_get_array_low_bound_and_type (struct type *type,
+ long *low_bound,
+ struct type **index_type)
+{
+ struct type *index = TYPE_INDEX_TYPE (type);
+ long low = 0;
+
+ if (index == NULL)
+ return 0;
+
+ if (TYPE_CODE (index) != TYPE_CODE_RANGE
+ && TYPE_CODE (index) != TYPE_CODE_ENUM)
+ return 0;
+
+ low = TYPE_LOW_BOUND (index);
+ if (low > TYPE_HIGH_BOUND (index))
+ return 0;
+
+ if (TYPE_CODE (index) == TYPE_CODE_RANGE)
+ index = TYPE_TARGET_TYPE (index);
+
+ if (low_bound)
+ *low_bound = low;
+
+ if (index_type)
+ *index_type = index;
+
+ return 1;
+}
+
/* Assuming TYPE is a simple, non-empty array type, prints its lower bound
on STREAM, if non-standard (i.e., other than 1 for numbers, other
than lower bound of index type for enumerated type). Returns 1
@@ -86,19 +124,10 @@ print_optional_low_bound (struct ui_file
struct type *index_type;
long low_bound;
- index_type = TYPE_INDEX_TYPE (type);
- low_bound = 0;
-
- if (index_type == NULL)
+ if (print_array_indexes_p ())
return 0;
- if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
- {
- low_bound = TYPE_LOW_BOUND (index_type);
- if (low_bound > TYPE_HIGH_BOUND (index_type))
- return 0;
- index_type = TYPE_TARGET_TYPE (index_type);
- }
- else
+
+ if (!ada_get_array_low_bound_and_type (type, &low_bound, &index_type))
return 0;
switch (TYPE_CODE (index_type))
@@ -137,16 +166,18 @@ val_print_packed_array_elements (struct
unsigned int i;
unsigned int things_printed = 0;
unsigned len;
- struct type *elttype;
+ struct type *elttype, *index_type;
unsigned eltlen;
unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
struct value *mark = value_mark ();
+ LONGEST low = 0;
elttype = TYPE_TARGET_TYPE (type);
eltlen = TYPE_LENGTH (check_typedef (elttype));
+ index_type = TYPE_INDEX_TYPE (type);
{
- LONGEST low, high;
+ LONGEST high;
if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0)
len = 1;
else
@@ -174,6 +205,7 @@ val_print_packed_array_elements (struct
}
}
wrap_here (n_spaces (2 + 2 * recurse));
+ maybe_print_array_index (index_type, i + low, stream, format, pretty);
i0 = i;
v0 = ada_value_primitive_packed_val (NULL, valaddr,
@@ -219,6 +251,8 @@ val_print_packed_array_elements (struct
fprintf_filtered (stream, ", ");
}
wrap_here (n_spaces (2 + 2 * recurse));
+ maybe_print_array_index (index_type, j + low,
+ stream, format, pretty);
}
val_print (elttype, value_contents (v0), 0, 0, stream, format,
0, recurse + 1, pretty);
@@ -824,7 +858,11 @@ ada_val_print_1 (struct type *type, cons
}
else
{
+ long low_bound = 0;
+
len = 0;
+ ada_get_array_low_bound_and_type (type, &low_bound, NULL);
+
fprintf_filtered (stream, "(");
print_optional_low_bound (stream, type);
if (TYPE_FIELD_BITSIZE (type, 0) > 0)
@@ -833,7 +871,7 @@ ada_val_print_1 (struct type *type, cons
else
val_print_array_elements (type, valaddr, address, stream,
format, deref_ref, recurse,
- pretty, 0);
+ pretty, 0, low_bound);
fprintf_filtered (stream, ")");
}
gdb_flush (stream);
^ permalink raw reply [flat|nested] 37+ messages in thread* Re: [RFC/RFA] print arrays with indexes 2005-09-06 20:20 [RFC/RFA] print arrays with indexes Joel Brobecker @ 2005-09-06 20:57 ` Daniel Jacobowitz 2005-09-07 5:40 ` Joel Brobecker 2005-09-06 21:45 ` Jim Blandy 1 sibling, 1 reply; 37+ messages in thread From: Daniel Jacobowitz @ 2005-09-06 20:57 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches On Tue, Sep 06, 2005 at 01:20:18PM -0700, Joel Brobecker wrote: > 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 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. In fact, C99 allows: int A[3] = { [1] = 2 }; GNU C has supported it for a while, I think. I don't think it works for C++. So, yes, a language method would be good. > (gdb) set/show print array-indexes > > With a default of "off", to preserve the current behavior. I suppose we've got to :-) I'd turn it on, that's for sure. Afraid I haven't time to look at the patch just now. I'll be back. -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFC/RFA] print arrays with indexes 2005-09-06 20:57 ` Daniel Jacobowitz @ 2005-09-07 5:40 ` Joel Brobecker 2005-09-07 13:23 ` Daniel Jacobowitz 0 siblings, 1 reply; 37+ messages in thread From: Joel Brobecker @ 2005-09-07 5:40 UTC (permalink / raw) To: gdb-patches > In fact, C99 allows: > int A[3] = { [1] = 2 }; > > GNU C has supported it for a while, I think. I don't think it works > for C++. Aha! Thanks for the tip. > So, yes, a language method would be good. Ok, how about calling it la_print_array_index? The current language_defn structure already la_printchar and la_printstr (no "_" between print and the object name), but I think using underscores is that much clearer in this case. void (*la_print_array_index) (struct value *index_value, struct ui_file *stream, int format, enum val_prettyprint pretty); I'll then define a new LA_PRINT_ARRAY_INDEX macro #define LA_PRINT_ARRAY_INDEX (index_value, stream, format, pretty) \ (current_language->la_print_array_index(index_value, stream, format, pretty)) > > (gdb) set/show print array-indexes > > > > With a default of "off", to preserve the current behavior. > > I suppose we've got to :-) I'd turn it on, that's for sure. There is also Jim's suggestion which has some merits. I'm more of an all or nothing kind of guy, so I prefer the approach I've chosen, but I am flexible. > Afraid I haven't time to look at the patch just now. I'll be back. Sure. Thanks for the early feedback. -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFC/RFA] print arrays with indexes 2005-09-07 5:40 ` Joel Brobecker @ 2005-09-07 13:23 ` Daniel Jacobowitz 2005-09-07 20:24 ` Joel Brobecker 2005-09-07 20:39 ` [RFC/RFA] " Jim Blandy 0 siblings, 2 replies; 37+ messages in thread From: Daniel Jacobowitz @ 2005-09-07 13:23 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches On Tue, Sep 06, 2005 at 10:39:51PM -0700, Joel Brobecker wrote: > > So, yes, a language method would be good. > > Ok, how about calling it la_print_array_index? The current language_defn > structure already la_printchar and la_printstr (no "_" between print and > the object name), but I think using underscores is that much clearer in > this case. > > void (*la_print_array_index) (struct value *index_value, > struct ui_file *stream, > int format, > enum val_prettyprint pretty); > > I'll then define a new LA_PRINT_ARRAY_INDEX macro > > #define LA_PRINT_ARRAY_INDEX (index_value, stream, format, pretty) \ > (current_language->la_print_array_index(index_value, stream, format, pretty)) Fine with me. > > > (gdb) set/show print array-indexes > > > > > > With a default of "off", to preserve the current behavior. > > > > I suppose we've got to :-) I'd turn it on, that's for sure. > > There is also Jim's suggestion which has some merits. I'm more of > an all or nothing kind of guy, so I prefer the approach I've chosen, > but I am flexible. I've got no strong opinions on this either way. Thresholds seem complicated as a UI. -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFC/RFA] print arrays with indexes 2005-09-07 13:23 ` Daniel Jacobowitz @ 2005-09-07 20:24 ` Joel Brobecker 2005-09-14 17:13 ` [RFA] " Joel Brobecker 2005-09-07 20:39 ` [RFC/RFA] " Jim Blandy 1 sibling, 1 reply; 37+ messages in thread From: Joel Brobecker @ 2005-09-07 20:24 UTC (permalink / raw) To: gdb-patches [-- Attachment #1: Type: text/plain, Size: 3004 bytes --] > > There is also Jim's suggestion which has some merits. I'm more of > > an all or nothing kind of guy, so I prefer the approach I've chosen, > > but I am flexible. > > I've got no strong opinions on this either way. Thresholds seem > complicated as a UI. I feel the same way too. Here is a revised patch, that implements the language method. Now, with C, we get: (gdb) p array $1 = {[0] = 1, [1] = 2, [2] = 3, [3] = 4} This is also the default for all the other languages, except Ada, where the debugger will print: (gdb) p one_two_three $1 = (1 => 1, 2 => 2, 3 => 3) 2005-09-07 Joel Brobecker <brobecker@adacore.com> * language.h (language_defn): New field la_print_array_index. (LA_PRINT_ARRAY_INDEX): New macro. (default_print_array_index): Add declaration. * language.c (default_print_array_index): new function. (unknown_language): Add value for new field. (auto_language): Likewise. (local_language): Likewise. * c-lang.c (c_language_defn): Likewise. (cpluc_language_defn): Likewise. (asm_language_defn): Likewise. (minimal_language_defn): Likewise. * f-lang.c (f_language_defn): Likewise. * jv-lang.c (java_language_defn): Likewise. * m2-lang.c (m2_language_defn): Likewise. * objc-lang.c (objc_language_defn): Likewise. * p-lang.c (pascal_language_defn): Likewise. * ada-lang.c (ada_print_array_index): New function. (ada_language_defn): Add value for new field. * valprint.h (print_array_indexes_p): Add declaration. (maybe_print_array_index): Add declaration. (val_print_array_elements): Add new parameter to function profile. * valprint.c (print_array_indexes): New static 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 array-indexes "set/show print" command. * 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. Testcases and documentation to follow after the review. Thanks, -- Joel [-- Attachment #2: indexes.diff --] [-- Type: text/plain, Size: 18615 bytes --] Index: language.h =================================================================== RCS file: /cvs/src/src/gdb/language.h,v retrieving revision 1.35 diff -u -p -r1.35 language.h --- language.h 9 May 2005 21:20:34 -0000 1.35 +++ language.h 7 Sep 2005 20:00:10 -0000 @@ -275,6 +275,12 @@ struct language_defn void (*la_language_arch_info) (struct gdbarch *, struct language_arch_info *); + /* Print the index of an element of an array. */ + void (*la_print_array_index) (struct value *index_value, + struct ui_file *stream, + int format, + enum val_prettyprint pretty); + /* Add fields above this point, so the magic number is always last. */ /* Magic number for compat checking */ @@ -362,6 +368,9 @@ extern enum language set_language (enum #define LA_EMIT_CHAR(ch, stream, quoter) \ (current_language->la_emitchar(ch, stream, quoter)) +#define LA_PRINT_ARRAY_INDEX(index_value, stream, format, pretty) \ + (current_language->la_print_array_index(index_value, stream, format, pretty)) + /* Test a character to decide whether it can be printed in literal form or needs to be printed in another representation. For example, in C the literal form of the character with octal value 141 is 'a' @@ -457,4 +466,10 @@ extern char *language_class_name_from_ph /* Splitting strings into words. */ extern char *default_word_break_characters (void); +/* Print the index of an array element using the C99 syntax. */ +extern void default_print_array_index (struct value *index_value, + struct ui_file *stream, + int format, + enum val_prettyprint pretty); + #endif /* defined (LANGUAGE_H) */ Index: language.c =================================================================== RCS file: /cvs/src/src/gdb/language.c,v retrieving revision 1.62 diff -u -p -r1.62 language.c --- language.c 29 Aug 2005 12:57:49 -0000 1.62 +++ language.c 7 Sep 2005 20:00:09 -0000 @@ -1057,6 +1057,17 @@ default_word_break_characters (void) return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; } +/* Print the index of array elements using the C99 syntax. */ + +void +default_print_array_index (struct value *index_value, struct ui_file *stream, + int format, enum val_prettyprint pretty) +{ + fprintf_filtered (stream, "["); + LA_VALUE_PRINT (index_value, stream, format, pretty); + fprintf_filtered (stream, "] = "); +} + /* Define the language that is no language. */ static int @@ -1181,6 +1192,7 @@ const struct language_defn unknown_langu NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -1217,6 +1229,7 @@ const struct language_defn auto_language NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -1252,6 +1265,7 @@ const struct language_defn local_languag NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; \f Index: c-lang.c =================================================================== RCS file: /cvs/src/src/gdb/c-lang.c,v retrieving revision 1.37 diff -u -p -r1.37 c-lang.c --- c-lang.c 9 May 2005 21:20:30 -0000 1.37 +++ c-lang.c 7 Sep 2005 19:58:49 -0000 @@ -595,6 +595,7 @@ const struct language_defn c_language_de NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; @@ -653,6 +654,7 @@ const struct language_defn cplus_languag &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -688,6 +690,7 @@ const struct language_defn asm_language_ NULL, default_word_break_characters, c_language_arch_info, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -728,6 +731,7 @@ const struct language_defn minimal_langu NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: f-lang.c =================================================================== RCS file: /cvs/src/src/gdb/f-lang.c,v retrieving revision 1.31 diff -u -p -r1.31 f-lang.c --- f-lang.c 9 May 2005 21:20:30 -0000 1.31 +++ f-lang.c 7 Sep 2005 19:59:30 -0000 @@ -485,6 +485,7 @@ const struct language_defn f_language_de &builtin_type_f_character, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: jv-lang.c =================================================================== RCS file: /cvs/src/src/gdb/jv-lang.c,v retrieving revision 1.42 diff -u -p -r1.42 jv-lang.c --- jv-lang.c 27 May 2005 04:39:32 -0000 1.42 +++ jv-lang.c 7 Sep 2005 20:00:08 -0000 @@ -1114,6 +1114,7 @@ const struct language_defn java_language NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: m2-lang.c =================================================================== RCS file: /cvs/src/src/gdb/m2-lang.c,v retrieving revision 1.23 diff -u -p -r1.23 m2-lang.c --- m2-lang.c 9 May 2005 21:20:34 -0000 1.23 +++ m2-lang.c 7 Sep 2005 20:00:14 -0000 @@ -437,6 +437,7 @@ const struct language_defn m2_language_d &builtin_type_m2_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: objc-lang.c =================================================================== RCS file: /cvs/src/src/gdb/objc-lang.c,v retrieving revision 1.49 diff -u -p -r1.49 objc-lang.c --- objc-lang.c 12 Jul 2005 12:11:44 -0000 1.49 +++ objc-lang.c 7 Sep 2005 20:00:33 -0000 @@ -684,6 +684,7 @@ const struct language_defn objc_language &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: p-lang.c =================================================================== RCS file: /cvs/src/src/gdb/p-lang.c,v retrieving revision 1.25 diff -u -p -r1.25 p-lang.c --- p-lang.c 9 May 2005 21:20:34 -0000 1.25 +++ p-lang.c 7 Sep 2005 20:00:36 -0000 @@ -477,6 +477,7 @@ const struct language_defn pascal_langua &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: scm-lang.c =================================================================== RCS file: /cvs/src/src/gdb/scm-lang.c,v retrieving revision 1.32 diff -u -p -r1.32 scm-lang.c --- scm-lang.c 27 May 2005 04:39:32 -0000 1.32 +++ scm-lang.c 7 Sep 2005 20:01:01 -0000 @@ -269,6 +269,7 @@ const struct language_defn scm_language_ NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: ada-lang.c =================================================================== RCS file: /cvs/src/src/gdb/ada-lang.c,v retrieving revision 1.78 diff -u -p -r1.78 ada-lang.c --- ada-lang.c 9 May 2005 21:20:29 -0000 1.78 +++ ada-lang.c 7 Sep 2005 19:58:31 -0000 @@ -303,6 +303,16 @@ ada_get_gdb_completer_word_break_charact return ada_completer_word_break_characters; } +/* Print an array element index using the Ada syntax. */ + +static void +ada_print_array_index (struct value *index_value, struct ui_file *stream, + int format, enum val_prettyprint pretty) +{ + LA_VALUE_PRINT (index_value, stream, format, pretty); + fprintf_filtered (stream, " => "); +} + /* Read the string located at ADDR from the inferior and store the result into BUF. */ @@ -8766,6 +8776,7 @@ const struct language_defn ada_language_ NULL, ada_get_gdb_completer_word_break_characters, ada_language_arch_info, + ada_print_array_index, LANG_MAGIC }; Index: valprint.h =================================================================== RCS file: /cvs/src/src/gdb/valprint.h,v retrieving revision 1.10 diff -u -p -r1.10 valprint.h --- valprint.h 9 May 2005 21:20:35 -0000 1.10 +++ valprint.h 7 Sep 2005 20:01:35 -0000 @@ -50,10 +50,16 @@ extern int output_format; extern int stop_print_at_null; /* Stop printing at null char? */ +extern int print_array_indexes_p (void); + +extern void maybe_print_array_index (struct type *index_type, LONGEST index, + struct ui_file *stream, int format, + enum val_prettyprint pretty); + extern void val_print_array_elements (struct type *, const gdb_byte *, CORE_ADDR, struct ui_file *, int, int, int, enum val_prettyprint, - unsigned int); + unsigned int, LONGEST); extern void val_print_type_code_int (struct type *, const gdb_byte *, struct ui_file *); Index: valprint.c =================================================================== RCS file: /cvs/src/src/gdb/valprint.c,v retrieving revision 1.54 diff -u -p -r1.54 valprint.c --- valprint.c 10 Jun 2005 06:07:32 -0000 1.54 +++ valprint.c 7 Sep 2005 20:01:35 -0000 @@ -100,6 +100,17 @@ Default output radix for printing of val } int output_format = 0; +/* By default we print arrays without printing the index of each element in + the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */ + +static int print_array_indexes = 0; +static void +show_print_array_indexes (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value); +} + /* Print repeat counts if there are more than this many repetitions of an element in an array. Referenced by the low level language dependent print routines. */ @@ -859,9 +870,41 @@ print_char_chars (struct ui_file *stream } } +/* Return non-zero if the debugger should print the index of each element + when printing array values. */ + +int +print_array_indexes_p (void) +{ + return print_array_indexes; +} + +/* Print on STREAM using the given FORMAT the index for the element + at INDEX of an array whose index type is INDEX_TYPE. */ + +void +maybe_print_array_index (struct type *index_type, LONGEST index, + struct ui_file *stream, int format, + enum val_prettyprint pretty) +{ + struct value *index_value; + + if (!print_array_indexes) + return; + + index_value = value_from_longest (index_type, index); + + LA_PRINT_ARRAY_INDEX (index_value, stream, format, pretty); +} + /* Called by various <lang>_val_print routines to print elements of an array in the form "<elem1>, <elem2>, <elem3>, ...". + Some languages such as Ada allow the user to specify arrays where + the index of the first element is not zero. REAL_INDEX_OFFSET is + the user-level index of the first element of the array. For many + languages such as C or C++, it is always zero. + (FIXME?) Assumes array element separator is a comma, which is correct for all languages currently handled. (FIXME?) Some languages have a notation for repeated array elements, @@ -873,11 +916,11 @@ val_print_array_elements (struct type *t CORE_ADDR address, struct ui_file *stream, int format, int deref_ref, int recurse, enum val_prettyprint pretty, - unsigned int i) + unsigned int i, LONGEST real_index_offset) { unsigned int things_printed = 0; unsigned len; - struct type *elttype; + struct type *elttype, *index_type; unsigned eltlen; /* Position of the array element we are examining to see whether it is repeated. */ @@ -888,6 +931,7 @@ val_print_array_elements (struct type *t elttype = TYPE_TARGET_TYPE (type); eltlen = TYPE_LENGTH (check_typedef (elttype)); len = TYPE_LENGTH (type) / eltlen; + index_type = TYPE_INDEX_TYPE (type); annotate_array_section_begin (i, elttype); @@ -906,6 +950,8 @@ val_print_array_elements (struct type *t } } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, i + real_index_offset, + stream, format, pretty); rep1 = i + 1; reps = 1; @@ -1396,6 +1442,12 @@ Show the default input and output number Use 'show input-radix' or 'show output-radix' to independently show each."), &showlist); + add_setshow_boolean_cmd ("array-indexes", class_support, + &print_array_indexes, _("\ +Set printing of array indexes."), _("\ +Show printing of array indexes"), NULL, NULL, show_print_array_indexes, + &setprintlist, &showprintlist); + /* Give people the defaults which they are used to. */ prettyprint_structs = 0; prettyprint_arrays = 0; Index: c-valprint.c =================================================================== RCS file: /cvs/src/src/gdb/c-valprint.c,v retrieving revision 1.37 diff -u -p -r1.37 c-valprint.c --- c-valprint.c 9 May 2005 21:20:30 -0000 1.37 +++ c-valprint.c 7 Sep 2005 19:58:50 -0000 @@ -133,7 +133,7 @@ c_val_print (struct type *type, const gd i = 0; } val_print_array_elements (type, valaddr + embedded_offset, address, stream, - format, deref_ref, recurse, pretty, i); + format, deref_ref, recurse, pretty, i, 0); fprintf_filtered (stream, "}"); } break; Index: p-valprint.c =================================================================== RCS file: /cvs/src/src/gdb/p-valprint.c,v retrieving revision 1.39 diff -u -p -r1.39 p-valprint.c --- p-valprint.c 9 May 2005 21:20:34 -0000 1.39 +++ p-valprint.c 7 Sep 2005 20:00:37 -0000 @@ -123,7 +123,7 @@ pascal_val_print (struct type *type, con i = 0; } val_print_array_elements (type, valaddr + embedded_offset, address, stream, - format, deref_ref, recurse, pretty, i); + format, deref_ref, recurse, pretty, i, 0); fprintf_filtered (stream, "}"); } break; Index: ada-valprint.c =================================================================== RCS file: /cvs/src/src/gdb/ada-valprint.c,v retrieving revision 1.23 diff -u -p -r1.23 ada-valprint.c --- ada-valprint.c 9 May 2005 21:20:30 -0000 1.23 +++ ada-valprint.c 7 Sep 2005 19:58:33 -0000 @@ -75,6 +75,44 @@ adjust_type_signedness (struct type *typ TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED; } +/* Assuming TYPE is a simple, non-empty array type, compute the lower + bound and the array index type. Save the low bound into LOW_BOUND + if not NULL. Save the index type in INDEX_TYPE if not NULL. + + Return 1 if the operation was successful. Return zero otherwise, + in which case the value of LOW_BOUND and INDEX_TYPE is undefined. */ + +static int +ada_get_array_low_bound_and_type (struct type *type, + long *low_bound, + struct type **index_type) +{ + struct type *index = TYPE_INDEX_TYPE (type); + long low = 0; + + if (index == NULL) + return 0; + + if (TYPE_CODE (index) != TYPE_CODE_RANGE + && TYPE_CODE (index) != TYPE_CODE_ENUM) + return 0; + + low = TYPE_LOW_BOUND (index); + if (low > TYPE_HIGH_BOUND (index)) + return 0; + + if (TYPE_CODE (index) == TYPE_CODE_RANGE) + index = TYPE_TARGET_TYPE (index); + + if (low_bound) + *low_bound = low; + + if (index_type) + *index_type = index; + + return 1; +} + /* Assuming TYPE is a simple, non-empty array type, prints its lower bound on STREAM, if non-standard (i.e., other than 1 for numbers, other than lower bound of index type for enumerated type). Returns 1 @@ -86,19 +124,10 @@ print_optional_low_bound (struct ui_file struct type *index_type; long low_bound; - index_type = TYPE_INDEX_TYPE (type); - low_bound = 0; - - if (index_type == NULL) + if (print_array_indexes_p ()) return 0; - if (TYPE_CODE (index_type) == TYPE_CODE_RANGE) - { - low_bound = TYPE_LOW_BOUND (index_type); - if (low_bound > TYPE_HIGH_BOUND (index_type)) - return 0; - index_type = TYPE_TARGET_TYPE (index_type); - } - else + + if (!ada_get_array_low_bound_and_type (type, &low_bound, &index_type)) return 0; switch (TYPE_CODE (index_type)) @@ -137,16 +166,18 @@ val_print_packed_array_elements (struct unsigned int i; unsigned int things_printed = 0; unsigned len; - struct type *elttype; + struct type *elttype, *index_type; unsigned eltlen; unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0); struct value *mark = value_mark (); + LONGEST low = 0; elttype = TYPE_TARGET_TYPE (type); eltlen = TYPE_LENGTH (check_typedef (elttype)); + index_type = TYPE_INDEX_TYPE (type); { - LONGEST low, high; + LONGEST high; if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0) len = 1; else @@ -174,6 +205,7 @@ val_print_packed_array_elements (struct } } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, i + low, stream, format, pretty); i0 = i; v0 = ada_value_primitive_packed_val (NULL, valaddr, @@ -219,6 +251,8 @@ val_print_packed_array_elements (struct fprintf_filtered (stream, ", "); } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, j + low, + stream, format, pretty); } val_print (elttype, value_contents (v0), 0, 0, stream, format, 0, recurse + 1, pretty); @@ -824,7 +858,11 @@ ada_val_print_1 (struct type *type, cons } else { + long low_bound = 0; + len = 0; + ada_get_array_low_bound_and_type (type, &low_bound, NULL); + fprintf_filtered (stream, "("); print_optional_low_bound (stream, type); if (TYPE_FIELD_BITSIZE (type, 0) > 0) @@ -833,7 +871,7 @@ ada_val_print_1 (struct type *type, cons else val_print_array_elements (type, valaddr, address, stream, format, deref_ref, recurse, - pretty, 0); + pretty, 0, low_bound); fprintf_filtered (stream, ")"); } gdb_flush (stream); ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-07 20:24 ` Joel Brobecker @ 2005-09-14 17:13 ` Joel Brobecker 2005-09-17 20:49 ` Daniel Jacobowitz 0 siblings, 1 reply; 37+ messages in thread From: Joel Brobecker @ 2005-09-14 17:13 UTC (permalink / raw) To: gdb-patches Hello, It'd be nice to have this patch reviewed. It touches quite a few files, so I'd rather not wait for too long to avoid merging conflicts. Once we agree on the interface, I think the code will follow naturally. Thank you! On Wed, Sep 07, 2005 at 01:24:02PM -0700, Joel Brobecker wrote: > > > There is also Jim's suggestion which has some merits. I'm more of > > > an all or nothing kind of guy, so I prefer the approach I've chosen, > > > but I am flexible. > > > > I've got no strong opinions on this either way. Thresholds seem > > complicated as a UI. > > I feel the same way too. > > Here is a revised patch, that implements the language method. > Now, with C, we get: > > (gdb) p array > $1 = {[0] = 1, [1] = 2, [2] = 3, [3] = 4} > > This is also the default for all the other languages, except Ada, where > the debugger will print: > > (gdb) p one_two_three > $1 = (1 => 1, 2 => 2, 3 => 3) > > 2005-09-07 Joel Brobecker <brobecker@adacore.com> > > * language.h (language_defn): New field la_print_array_index. > (LA_PRINT_ARRAY_INDEX): New macro. > (default_print_array_index): Add declaration. > * language.c (default_print_array_index): new function. > (unknown_language): Add value for new field. > (auto_language): Likewise. > (local_language): Likewise. > * c-lang.c (c_language_defn): Likewise. > (cpluc_language_defn): Likewise. > (asm_language_defn): Likewise. > (minimal_language_defn): Likewise. > * f-lang.c (f_language_defn): Likewise. > * jv-lang.c (java_language_defn): Likewise. > * m2-lang.c (m2_language_defn): Likewise. > * objc-lang.c (objc_language_defn): Likewise. > * p-lang.c (pascal_language_defn): Likewise. > * ada-lang.c (ada_print_array_index): New function. > (ada_language_defn): Add value for new field. > * valprint.h (print_array_indexes_p): Add declaration. > (maybe_print_array_index): Add declaration. > (val_print_array_elements): Add new parameter to function profile. > * valprint.c (print_array_indexes): New static 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 array-indexes "set/show print" command. > * 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. Testcases and documentation to follow > after the review. > > Thanks, > -- > Joel > Index: language.h > =================================================================== > RCS file: /cvs/src/src/gdb/language.h,v > retrieving revision 1.35 > diff -u -p -r1.35 language.h > --- language.h 9 May 2005 21:20:34 -0000 1.35 > +++ language.h 7 Sep 2005 20:00:10 -0000 > @@ -275,6 +275,12 @@ struct language_defn > void (*la_language_arch_info) (struct gdbarch *, > struct language_arch_info *); > > + /* Print the index of an element of an array. */ > + void (*la_print_array_index) (struct value *index_value, > + struct ui_file *stream, > + int format, > + enum val_prettyprint pretty); > + > /* Add fields above this point, so the magic number is always last. */ > /* Magic number for compat checking */ > > @@ -362,6 +368,9 @@ extern enum language set_language (enum > #define LA_EMIT_CHAR(ch, stream, quoter) \ > (current_language->la_emitchar(ch, stream, quoter)) > > +#define LA_PRINT_ARRAY_INDEX(index_value, stream, format, pretty) \ > + (current_language->la_print_array_index(index_value, stream, format, pretty)) > + > /* Test a character to decide whether it can be printed in literal form > or needs to be printed in another representation. For example, > in C the literal form of the character with octal value 141 is 'a' > @@ -457,4 +466,10 @@ extern char *language_class_name_from_ph > /* Splitting strings into words. */ > extern char *default_word_break_characters (void); > > +/* Print the index of an array element using the C99 syntax. */ > +extern void default_print_array_index (struct value *index_value, > + struct ui_file *stream, > + int format, > + enum val_prettyprint pretty); > + > #endif /* defined (LANGUAGE_H) */ > Index: language.c > =================================================================== > RCS file: /cvs/src/src/gdb/language.c,v > retrieving revision 1.62 > diff -u -p -r1.62 language.c > --- language.c 29 Aug 2005 12:57:49 -0000 1.62 > +++ language.c 7 Sep 2005 20:00:09 -0000 > @@ -1057,6 +1057,17 @@ default_word_break_characters (void) > return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; > } > > +/* Print the index of array elements using the C99 syntax. */ > + > +void > +default_print_array_index (struct value *index_value, struct ui_file *stream, > + int format, enum val_prettyprint pretty) > +{ > + fprintf_filtered (stream, "["); > + LA_VALUE_PRINT (index_value, stream, format, pretty); > + fprintf_filtered (stream, "] = "); > +} > + > /* Define the language that is no language. */ > > static int > @@ -1181,6 +1192,7 @@ const struct language_defn unknown_langu > NULL, > default_word_break_characters, > unknown_language_arch_info, /* la_language_arch_info. */ > + default_print_array_index, > LANG_MAGIC > }; > > @@ -1217,6 +1229,7 @@ const struct language_defn auto_language > NULL, > default_word_break_characters, > unknown_language_arch_info, /* la_language_arch_info. */ > + default_print_array_index, > LANG_MAGIC > }; > > @@ -1252,6 +1265,7 @@ const struct language_defn local_languag > NULL, > default_word_break_characters, > unknown_language_arch_info, /* la_language_arch_info. */ > + default_print_array_index, > LANG_MAGIC > }; > \f > Index: c-lang.c > =================================================================== > RCS file: /cvs/src/src/gdb/c-lang.c,v > retrieving revision 1.37 > diff -u -p -r1.37 c-lang.c > --- c-lang.c 9 May 2005 21:20:30 -0000 1.37 > +++ c-lang.c 7 Sep 2005 19:58:49 -0000 > @@ -595,6 +595,7 @@ const struct language_defn c_language_de > NULL, > default_word_break_characters, > c_language_arch_info, > + default_print_array_index, > LANG_MAGIC > }; > > @@ -653,6 +654,7 @@ const struct language_defn cplus_languag > &builtin_type_char, /* Type of string elements */ > default_word_break_characters, > NULL, /* FIXME: la_language_arch_info. */ > + default_print_array_index, > LANG_MAGIC > }; > > @@ -688,6 +690,7 @@ const struct language_defn asm_language_ > NULL, > default_word_break_characters, > c_language_arch_info, /* FIXME: la_language_arch_info. */ > + default_print_array_index, > LANG_MAGIC > }; > > @@ -728,6 +731,7 @@ const struct language_defn minimal_langu > NULL, > default_word_break_characters, > c_language_arch_info, > + default_print_array_index, > LANG_MAGIC > }; > > Index: f-lang.c > =================================================================== > RCS file: /cvs/src/src/gdb/f-lang.c,v > retrieving revision 1.31 > diff -u -p -r1.31 f-lang.c > --- f-lang.c 9 May 2005 21:20:30 -0000 1.31 > +++ f-lang.c 7 Sep 2005 19:59:30 -0000 > @@ -485,6 +485,7 @@ const struct language_defn f_language_de > &builtin_type_f_character, /* Type of string elements */ > default_word_break_characters, > NULL, /* FIXME: la_language_arch_info. */ > + default_print_array_index, > LANG_MAGIC > }; > > Index: jv-lang.c > =================================================================== > RCS file: /cvs/src/src/gdb/jv-lang.c,v > retrieving revision 1.42 > diff -u -p -r1.42 jv-lang.c > --- jv-lang.c 27 May 2005 04:39:32 -0000 1.42 > +++ jv-lang.c 7 Sep 2005 20:00:08 -0000 > @@ -1114,6 +1114,7 @@ const struct language_defn java_language > NULL, > default_word_break_characters, > c_language_arch_info, > + default_print_array_index, > LANG_MAGIC > }; > > Index: m2-lang.c > =================================================================== > RCS file: /cvs/src/src/gdb/m2-lang.c,v > retrieving revision 1.23 > diff -u -p -r1.23 m2-lang.c > --- m2-lang.c 9 May 2005 21:20:34 -0000 1.23 > +++ m2-lang.c 7 Sep 2005 20:00:14 -0000 > @@ -437,6 +437,7 @@ const struct language_defn m2_language_d > &builtin_type_m2_char, /* Type of string elements */ > default_word_break_characters, > NULL, /* FIXME: la_language_arch_info. */ > + default_print_array_index, > LANG_MAGIC > }; > > Index: objc-lang.c > =================================================================== > RCS file: /cvs/src/src/gdb/objc-lang.c,v > retrieving revision 1.49 > diff -u -p -r1.49 objc-lang.c > --- objc-lang.c 12 Jul 2005 12:11:44 -0000 1.49 > +++ objc-lang.c 7 Sep 2005 20:00:33 -0000 > @@ -684,6 +684,7 @@ const struct language_defn objc_language > &builtin_type_char, /* Type of string elements */ > default_word_break_characters, > NULL, /* FIXME: la_language_arch_info. */ > + default_print_array_index, > LANG_MAGIC > }; > > Index: p-lang.c > =================================================================== > RCS file: /cvs/src/src/gdb/p-lang.c,v > retrieving revision 1.25 > diff -u -p -r1.25 p-lang.c > --- p-lang.c 9 May 2005 21:20:34 -0000 1.25 > +++ p-lang.c 7 Sep 2005 20:00:36 -0000 > @@ -477,6 +477,7 @@ const struct language_defn pascal_langua > &builtin_type_char, /* Type of string elements */ > default_word_break_characters, > NULL, /* FIXME: la_language_arch_info. */ > + default_print_array_index, > LANG_MAGIC > }; > > Index: scm-lang.c > =================================================================== > RCS file: /cvs/src/src/gdb/scm-lang.c,v > retrieving revision 1.32 > diff -u -p -r1.32 scm-lang.c > --- scm-lang.c 27 May 2005 04:39:32 -0000 1.32 > +++ scm-lang.c 7 Sep 2005 20:01:01 -0000 > @@ -269,6 +269,7 @@ const struct language_defn scm_language_ > NULL, > default_word_break_characters, > c_language_arch_info, > + default_print_array_index, > LANG_MAGIC > }; > > Index: ada-lang.c > =================================================================== > RCS file: /cvs/src/src/gdb/ada-lang.c,v > retrieving revision 1.78 > diff -u -p -r1.78 ada-lang.c > --- ada-lang.c 9 May 2005 21:20:29 -0000 1.78 > +++ ada-lang.c 7 Sep 2005 19:58:31 -0000 > @@ -303,6 +303,16 @@ ada_get_gdb_completer_word_break_charact > return ada_completer_word_break_characters; > } > > +/* Print an array element index using the Ada syntax. */ > + > +static void > +ada_print_array_index (struct value *index_value, struct ui_file *stream, > + int format, enum val_prettyprint pretty) > +{ > + LA_VALUE_PRINT (index_value, stream, format, pretty); > + fprintf_filtered (stream, " => "); > +} > + > /* Read the string located at ADDR from the inferior and store the > result into BUF. */ > > @@ -8766,6 +8776,7 @@ const struct language_defn ada_language_ > NULL, > ada_get_gdb_completer_word_break_characters, > ada_language_arch_info, > + ada_print_array_index, > LANG_MAGIC > }; > > Index: valprint.h > =================================================================== > RCS file: /cvs/src/src/gdb/valprint.h,v > retrieving revision 1.10 > diff -u -p -r1.10 valprint.h > --- valprint.h 9 May 2005 21:20:35 -0000 1.10 > +++ valprint.h 7 Sep 2005 20:01:35 -0000 > @@ -50,10 +50,16 @@ extern int output_format; > > extern int stop_print_at_null; /* Stop printing at null char? */ > > +extern int print_array_indexes_p (void); > + > +extern void maybe_print_array_index (struct type *index_type, LONGEST index, > + struct ui_file *stream, int format, > + enum val_prettyprint pretty); > + > extern void val_print_array_elements (struct type *, const gdb_byte *, > CORE_ADDR, struct ui_file *, int, > int, int, enum val_prettyprint, > - unsigned int); > + unsigned int, LONGEST); > > extern void val_print_type_code_int (struct type *, const gdb_byte *, > struct ui_file *); > Index: valprint.c > =================================================================== > RCS file: /cvs/src/src/gdb/valprint.c,v > retrieving revision 1.54 > diff -u -p -r1.54 valprint.c > --- valprint.c 10 Jun 2005 06:07:32 -0000 1.54 > +++ valprint.c 7 Sep 2005 20:01:35 -0000 > @@ -100,6 +100,17 @@ Default output radix for printing of val > } > int output_format = 0; > > +/* By default we print arrays without printing the index of each element in > + the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */ > + > +static int print_array_indexes = 0; > +static void > +show_print_array_indexes (struct ui_file *file, int from_tty, > + struct cmd_list_element *c, const char *value) > +{ > + fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value); > +} > + > /* Print repeat counts if there are more than this many repetitions of an > element in an array. Referenced by the low level language dependent > print routines. */ > @@ -859,9 +870,41 @@ print_char_chars (struct ui_file *stream > } > } > > +/* Return non-zero if the debugger should print the index of each element > + when printing array values. */ > + > +int > +print_array_indexes_p (void) > +{ > + return print_array_indexes; > +} > + > +/* Print on STREAM using the given FORMAT the index for the element > + at INDEX of an array whose index type is INDEX_TYPE. */ > + > +void > +maybe_print_array_index (struct type *index_type, LONGEST index, > + struct ui_file *stream, int format, > + enum val_prettyprint pretty) > +{ > + struct value *index_value; > + > + if (!print_array_indexes) > + return; > + > + index_value = value_from_longest (index_type, index); > + > + LA_PRINT_ARRAY_INDEX (index_value, stream, format, pretty); > +} > + > /* Called by various <lang>_val_print routines to print elements of an > array in the form "<elem1>, <elem2>, <elem3>, ...". > > + Some languages such as Ada allow the user to specify arrays where > + the index of the first element is not zero. REAL_INDEX_OFFSET is > + the user-level index of the first element of the array. For many > + languages such as C or C++, it is always zero. > + > (FIXME?) Assumes array element separator is a comma, which is correct > for all languages currently handled. > (FIXME?) Some languages have a notation for repeated array elements, > @@ -873,11 +916,11 @@ val_print_array_elements (struct type *t > CORE_ADDR address, struct ui_file *stream, > int format, int deref_ref, > int recurse, enum val_prettyprint pretty, > - unsigned int i) > + unsigned int i, LONGEST real_index_offset) > { > unsigned int things_printed = 0; > unsigned len; > - struct type *elttype; > + struct type *elttype, *index_type; > unsigned eltlen; > /* Position of the array element we are examining to see > whether it is repeated. */ > @@ -888,6 +931,7 @@ val_print_array_elements (struct type *t > elttype = TYPE_TARGET_TYPE (type); > eltlen = TYPE_LENGTH (check_typedef (elttype)); > len = TYPE_LENGTH (type) / eltlen; > + index_type = TYPE_INDEX_TYPE (type); > > annotate_array_section_begin (i, elttype); > > @@ -906,6 +950,8 @@ val_print_array_elements (struct type *t > } > } > wrap_here (n_spaces (2 + 2 * recurse)); > + maybe_print_array_index (index_type, i + real_index_offset, > + stream, format, pretty); > > rep1 = i + 1; > reps = 1; > @@ -1396,6 +1442,12 @@ Show the default input and output number > Use 'show input-radix' or 'show output-radix' to independently show each."), > &showlist); > > + add_setshow_boolean_cmd ("array-indexes", class_support, > + &print_array_indexes, _("\ > +Set printing of array indexes."), _("\ > +Show printing of array indexes"), NULL, NULL, show_print_array_indexes, > + &setprintlist, &showprintlist); > + > /* Give people the defaults which they are used to. */ > prettyprint_structs = 0; > prettyprint_arrays = 0; > Index: c-valprint.c > =================================================================== > RCS file: /cvs/src/src/gdb/c-valprint.c,v > retrieving revision 1.37 > diff -u -p -r1.37 c-valprint.c > --- c-valprint.c 9 May 2005 21:20:30 -0000 1.37 > +++ c-valprint.c 7 Sep 2005 19:58:50 -0000 > @@ -133,7 +133,7 @@ c_val_print (struct type *type, const gd > i = 0; > } > val_print_array_elements (type, valaddr + embedded_offset, address, stream, > - format, deref_ref, recurse, pretty, i); > + format, deref_ref, recurse, pretty, i, 0); > fprintf_filtered (stream, "}"); > } > break; > Index: p-valprint.c > =================================================================== > RCS file: /cvs/src/src/gdb/p-valprint.c,v > retrieving revision 1.39 > diff -u -p -r1.39 p-valprint.c > --- p-valprint.c 9 May 2005 21:20:34 -0000 1.39 > +++ p-valprint.c 7 Sep 2005 20:00:37 -0000 > @@ -123,7 +123,7 @@ pascal_val_print (struct type *type, con > i = 0; > } > val_print_array_elements (type, valaddr + embedded_offset, address, stream, > - format, deref_ref, recurse, pretty, i); > + format, deref_ref, recurse, pretty, i, 0); > fprintf_filtered (stream, "}"); > } > break; > Index: ada-valprint.c > =================================================================== > RCS file: /cvs/src/src/gdb/ada-valprint.c,v > retrieving revision 1.23 > diff -u -p -r1.23 ada-valprint.c > --- ada-valprint.c 9 May 2005 21:20:30 -0000 1.23 > +++ ada-valprint.c 7 Sep 2005 19:58:33 -0000 > @@ -75,6 +75,44 @@ adjust_type_signedness (struct type *typ > TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED; > } > > +/* Assuming TYPE is a simple, non-empty array type, compute the lower > + bound and the array index type. Save the low bound into LOW_BOUND > + if not NULL. Save the index type in INDEX_TYPE if not NULL. > + > + Return 1 if the operation was successful. Return zero otherwise, > + in which case the value of LOW_BOUND and INDEX_TYPE is undefined. */ > + > +static int > +ada_get_array_low_bound_and_type (struct type *type, > + long *low_bound, > + struct type **index_type) > +{ > + struct type *index = TYPE_INDEX_TYPE (type); > + long low = 0; > + > + if (index == NULL) > + return 0; > + > + if (TYPE_CODE (index) != TYPE_CODE_RANGE > + && TYPE_CODE (index) != TYPE_CODE_ENUM) > + return 0; > + > + low = TYPE_LOW_BOUND (index); > + if (low > TYPE_HIGH_BOUND (index)) > + return 0; > + > + if (TYPE_CODE (index) == TYPE_CODE_RANGE) > + index = TYPE_TARGET_TYPE (index); > + > + if (low_bound) > + *low_bound = low; > + > + if (index_type) > + *index_type = index; > + > + return 1; > +} > + > /* Assuming TYPE is a simple, non-empty array type, prints its lower bound > on STREAM, if non-standard (i.e., other than 1 for numbers, other > than lower bound of index type for enumerated type). Returns 1 > @@ -86,19 +124,10 @@ print_optional_low_bound (struct ui_file > struct type *index_type; > long low_bound; > > - index_type = TYPE_INDEX_TYPE (type); > - low_bound = 0; > - > - if (index_type == NULL) > + if (print_array_indexes_p ()) > return 0; > - if (TYPE_CODE (index_type) == TYPE_CODE_RANGE) > - { > - low_bound = TYPE_LOW_BOUND (index_type); > - if (low_bound > TYPE_HIGH_BOUND (index_type)) > - return 0; > - index_type = TYPE_TARGET_TYPE (index_type); > - } > - else > + > + if (!ada_get_array_low_bound_and_type (type, &low_bound, &index_type)) > return 0; > > switch (TYPE_CODE (index_type)) > @@ -137,16 +166,18 @@ val_print_packed_array_elements (struct > unsigned int i; > unsigned int things_printed = 0; > unsigned len; > - struct type *elttype; > + struct type *elttype, *index_type; > unsigned eltlen; > unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0); > struct value *mark = value_mark (); > + LONGEST low = 0; > > elttype = TYPE_TARGET_TYPE (type); > eltlen = TYPE_LENGTH (check_typedef (elttype)); > + index_type = TYPE_INDEX_TYPE (type); > > { > - LONGEST low, high; > + LONGEST high; > if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0) > len = 1; > else > @@ -174,6 +205,7 @@ val_print_packed_array_elements (struct > } > } > wrap_here (n_spaces (2 + 2 * recurse)); > + maybe_print_array_index (index_type, i + low, stream, format, pretty); > > i0 = i; > v0 = ada_value_primitive_packed_val (NULL, valaddr, > @@ -219,6 +251,8 @@ val_print_packed_array_elements (struct > fprintf_filtered (stream, ", "); > } > wrap_here (n_spaces (2 + 2 * recurse)); > + maybe_print_array_index (index_type, j + low, > + stream, format, pretty); > } > val_print (elttype, value_contents (v0), 0, 0, stream, format, > 0, recurse + 1, pretty); > @@ -824,7 +858,11 @@ ada_val_print_1 (struct type *type, cons > } > else > { > + long low_bound = 0; > + > len = 0; > + ada_get_array_low_bound_and_type (type, &low_bound, NULL); > + > fprintf_filtered (stream, "("); > print_optional_low_bound (stream, type); > if (TYPE_FIELD_BITSIZE (type, 0) > 0) > @@ -833,7 +871,7 @@ ada_val_print_1 (struct type *type, cons > else > val_print_array_elements (type, valaddr, address, stream, > format, deref_ref, recurse, > - pretty, 0); > + pretty, 0, low_bound); > fprintf_filtered (stream, ")"); > } > gdb_flush (stream); -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-14 17:13 ` [RFA] " Joel Brobecker @ 2005-09-17 20:49 ` Daniel Jacobowitz 2005-09-17 21:51 ` Joel Brobecker 0 siblings, 1 reply; 37+ messages in thread From: Daniel Jacobowitz @ 2005-09-17 20:49 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches On Wed, Sep 14, 2005 at 10:13:20AM -0700, Joel Brobecker wrote: > Hello, > > It'd be nice to have this patch reviewed. It touches quite a few files, > so I'd rather not wait for too long to avoid merging conflicts. Once > we agree on the interface, I think the code will follow naturally. > > Thank you! Then doesn't it make sense to agree on the interface first? :-) You suggested on/off/auto and a separate threshold. Jim suggested on/off/threshold. I prefer on/off/threshold of those two options, although it may be a bit tricky to get GDB to handle that correctly. Want to give it a try, or continue discussing alternatives? -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-17 20:49 ` Daniel Jacobowitz @ 2005-09-17 21:51 ` Joel Brobecker 2005-09-17 22:07 ` Daniel Jacobowitz 2005-09-18 3:37 ` Eli Zaretskii 0 siblings, 2 replies; 37+ messages in thread From: Joel Brobecker @ 2005-09-17 21:51 UTC (permalink / raw) To: gdb-patches > Then doesn't it make sense to agree on the interface first? :-) Right! I just wanted to hear a few other opinions to choose. Perhaps Eli and/or Mark would like to comment? In any case: > You suggested on/off/auto and a separate threshold. Jim suggested > on/off/threshold. I prefer on/off/threshold of those two options, > although it may be a bit tricky to get GDB to handle that correctly. > Want to give it a try, or continue discussing alternatives? I'm willing to give it a try. I couldn't find a mechanism in our "set/show" machinery that handled something like this, though. Unless I missed it, that's something I'll need to add too. But first, let's agree on the interface. I'm OK with Jim's suggestion. How about we give other people a couple more days to think about it and provide some feedback? And then I'll start working on the implementation. -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-17 21:51 ` Joel Brobecker @ 2005-09-17 22:07 ` Daniel Jacobowitz 2005-09-18 3:37 ` Eli Zaretskii 1 sibling, 0 replies; 37+ messages in thread From: Daniel Jacobowitz @ 2005-09-17 22:07 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches On Sat, Sep 17, 2005 at 02:51:38PM -0700, Joel Brobecker wrote: > > Then doesn't it make sense to agree on the interface first? :-) > > Right! I just wanted to hear a few other opinions to choose. > Perhaps Eli and/or Mark would like to comment? > > In any case: > > > You suggested on/off/auto and a separate threshold. Jim suggested > > on/off/threshold. I prefer on/off/threshold of those two options, > > although it may be a bit tricky to get GDB to handle that correctly. > > Want to give it a try, or continue discussing alternatives? > > I'm willing to give it a try. I couldn't find a mechanism in our > "set/show" machinery that handled something like this, though. > Unless I missed it, that's something I'll need to add too. Yes, I'm afraid so. I'd recommend wiring it to a single unsigned integer. For this case 0 meaning "on" and UINT_MAX meaning "off" makes sense; for other uses (limit rather than threshold), the other way around would make more sense. I think it's reasonable to add new var_* items for this; that should be pretty easy. Maybe var_threshold. > But first, let's agree on the interface. I'm OK with Jim's suggestion. > How about we give other people a couple more days to think about it > and provide some feedback? And then I'll start working on the > implementation. OK. -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-17 21:51 ` Joel Brobecker 2005-09-17 22:07 ` Daniel Jacobowitz @ 2005-09-18 3:37 ` Eli Zaretskii 2005-09-18 3:46 ` Daniel Jacobowitz 2005-09-18 8:53 ` Mark Kettenis 1 sibling, 2 replies; 37+ messages in thread From: Eli Zaretskii @ 2005-09-18 3:37 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches > Date: Sat, 17 Sep 2005 14:51:38 -0700 > From: Joel Brobecker <brobecker@adacore.com> > > > Then doesn't it make sense to agree on the interface first? :-) > > Right! I just wanted to hear a few other opinions to choose. > Perhaps Eli and/or Mark would like to comment? > > In any case: > > > You suggested on/off/auto and a separate threshold. Jim suggested > > on/off/threshold. I prefer on/off/threshold of those two options, > > although it may be a bit tricky to get GDB to handle that correctly. > > Want to give it a try, or continue discussing alternatives? > > I'm willing to give it a try. I couldn't find a mechanism in our > "set/show" machinery that handled something like this, though. > Unless I missed it, that's something I'll need to add too. I don't see any disadvantages to the on/off/auto+threshold method that would justify yet another add_* interface. Can someone please tell why is that a good idea? ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-18 3:37 ` Eli Zaretskii @ 2005-09-18 3:46 ` Daniel Jacobowitz 2005-09-18 5:41 ` Joel Brobecker 2005-09-18 8:53 ` Mark Kettenis 1 sibling, 1 reply; 37+ messages in thread From: Daniel Jacobowitz @ 2005-09-18 3:46 UTC (permalink / raw) To: Eli Zaretskii; +Cc: Joel Brobecker, gdb-patches On Sun, Sep 18, 2005 at 06:37:19AM +0300, Eli Zaretskii wrote: > > Date: Sat, 17 Sep 2005 14:51:38 -0700 > > From: Joel Brobecker <brobecker@adacore.com> > > > > > Then doesn't it make sense to agree on the interface first? :-) > > > > Right! I just wanted to hear a few other opinions to choose. > > Perhaps Eli and/or Mark would like to comment? > > > > In any case: > > > > > You suggested on/off/auto and a separate threshold. Jim suggested > > > on/off/threshold. I prefer on/off/threshold of those two options, > > > although it may be a bit tricky to get GDB to handle that correctly. > > > Want to give it a try, or continue discussing alternatives? > > > > I'm willing to give it a try. I couldn't find a mechanism in our > > "set/show" machinery that handled something like this, though. > > Unless I missed it, that's something I'll need to add too. > > I don't see any disadvantages to the on/off/auto+threshold method that > would justify yet another add_* interface. Can someone please tell > why is that a good idea? My first reaction was that it would be confusing. We'd have a variable to hold the threshold, and it would always show up in "show" or "help" output, but most of the time its value would be ignored. The trickier something is to document accurately, the more likely it is to confuse users. I'm not real attached to either one. If you think two separate knobs is simpler, then that's fine with me. -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-18 3:46 ` Daniel Jacobowitz @ 2005-09-18 5:41 ` Joel Brobecker 2005-09-18 19:08 ` Eli Zaretskii 0 siblings, 1 reply; 37+ messages in thread From: Joel Brobecker @ 2005-09-18 5:41 UTC (permalink / raw) To: Eli Zaretskii, gdb-patches > My first reaction was that it would be confusing. We'd have a variable > to hold the threshold, and it would always show up in "show" or "help" > output, but most of the time its value would be ignored. The trickier > something is to document accurately, the more likely it is to confuse > users. I also do not have a strong opinion, but I admit that having one knob seems a bit simpler to me (in terms of the user interface). -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-18 5:41 ` Joel Brobecker @ 2005-09-18 19:08 ` Eli Zaretskii 2005-09-18 19:19 ` Daniel Jacobowitz 0 siblings, 1 reply; 37+ messages in thread From: Eli Zaretskii @ 2005-09-18 19:08 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches > Date: Sat, 17 Sep 2005 22:41:09 -0700 > From: Joel Brobecker <brobecker@adacore.com> > > > My first reaction was that it would be confusing. We'd have a variable > > to hold the threshold, and it would always show up in "show" or "help" > > output, but most of the time its value would be ignored. The trickier > > something is to document accurately, the more likely it is to confuse > > users. > > I also do not have a strong opinion, but I admit that having one > knob seems a bit simpler to me (in terms of the user interface). Okay, but I still am missing something: we already have a couple of "set SOMETHING" commands that use the convention that setting a limit to zero (or -1 in one case) means unlimited. Can't we use this in the case in point, rather than inventing a new add_* interface? Given that we already have so many different add_command interfaces (and all but 2 of them are undocumented), I'd rather not introduce yet another interface unless we really have to. ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-18 19:08 ` Eli Zaretskii @ 2005-09-18 19:19 ` Daniel Jacobowitz 2005-09-18 20:05 ` Eli Zaretskii 0 siblings, 1 reply; 37+ messages in thread From: Daniel Jacobowitz @ 2005-09-18 19:19 UTC (permalink / raw) To: Eli Zaretskii; +Cc: Joel Brobecker, gdb-patches On Sun, Sep 18, 2005 at 10:08:13PM +0300, Eli Zaretskii wrote: > > Date: Sat, 17 Sep 2005 22:41:09 -0700 > > From: Joel Brobecker <brobecker@adacore.com> > > > > > My first reaction was that it would be confusing. We'd have a variable > > > to hold the threshold, and it would always show up in "show" or "help" > > > output, but most of the time its value would be ignored. The trickier > > > something is to document accurately, the more likely it is to confuse > > > users. > > > > I also do not have a strong opinion, but I admit that having one > > knob seems a bit simpler to me (in terms of the user interface). > > Okay, but I still am missing something: we already have a couple of > "set SOMETHING" commands that use the convention that setting a limit > to zero (or -1 in one case) means unlimited. Can't we use this in the > case in point, rather than inventing a new add_* interface? Given Yes, we do have a lot of such interfaces. The problem is that this one has two special values, not just one: always on, always off, and off if the number of elements is above a certain limit. We could do "0 is on, N is threshold, UINT_MAX is off", but in that case, I'd rather give 0 and UINT_MAX names of "on" and "off" so that "show" output is more intelligible. We already do this for var_uinteger and var_integer, which show up as "unlimited". > that we already have so many different add_command interfaces (and > all but 2 of them are undocumented), I'd rather not introduce yet > another interface unless we really have to. Well, then, perhaps this would be a good time for us to document them? :-) If Joel doesn't have time, I'll do it. -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-18 19:19 ` Daniel Jacobowitz @ 2005-09-18 20:05 ` Eli Zaretskii 2005-09-20 7:31 ` Joel Brobecker 0 siblings, 1 reply; 37+ messages in thread From: Eli Zaretskii @ 2005-09-18 20:05 UTC (permalink / raw) To: Joel Brobecker, gdb-patches > Date: Sun, 18 Sep 2005 15:19:43 -0400 > From: Daniel Jacobowitz <drow@false.org> > Cc: Joel Brobecker <brobecker@adacore.com>, gdb-patches@sources.redhat.com > > We could do "0 is on, N is threshold, UINT_MAX is off" Or perhaps -1 is off. > but in that case, I'd rather give 0 and UINT_MAX names of "on" and > "off" so that "show" output is more intelligible. I have no objections for the on and off aliases. > > that we already have so many different add_command interfaces (and > > all but 2 of them are undocumented), I'd rather not introduce yet > > another interface unless we really have to. > > Well, then, perhaps this would be a good time for us to document them? > :-) If Joel doesn't have time, I'll do it. I'll applaud to whoever does that. TIA ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-18 20:05 ` Eli Zaretskii @ 2005-09-20 7:31 ` Joel Brobecker 2005-09-20 19:18 ` Eli Zaretskii 0 siblings, 1 reply; 37+ messages in thread From: Joel Brobecker @ 2005-09-20 7:31 UTC (permalink / raw) To: Eli Zaretskii; +Cc: gdb-patches Now that Mark and Eli have commented as well, it seems that the majority of us prefer a feature controled by a single knob. Eli seems ok with a two-knob approach, but doesn't seemed resistant to the one-knob either, so I'll conclude that the single-know approach is what we are going to implement. Fair enough? In terms of the actual specifics of that control, I think we have a general agreement that a user should be able to use "on" and "off" keywords to set it. So we need a way to provide an "on/off/threashold" interface. So far, I think we are ok with this approach. Something we haven't really formally decided though, is the significance of the threashold. Is it: 1. If array size > threshold, then print indexes 2. If array size < threshold, then print indexes (you can replace the strict comparison operator by <= or >=, to be discussed too). Now, the sticky part: How to implement this new interface. I'll argue that it's best to implement something new. I don't like hijacking an old interface to unsigned integer and adding some aliases to a couple of values. My reasoning is that saying that OFF is an alias for UINT_MAX will make sense for certain cases while it actually won't for other cases. Actually, which value to use for OFF will depend on the what the threashold actually means. Short of implemeting something new, I can suggest enhancing the API for unsigned integer settings, by adding a way to provide aliases and their corresponding values. Drawback, we will probably have to make quite a fair number of changes, at all the places where these routines are already used. I vote for a new API. I think we should also review the usage of the current ones, probably cleanup a bit some of the ones that were on the road to obsolescence (IIRC), maybe rationalize a bit more our API if needed, and add some documentation. But that should be a separate thread. I don't think I will be able to handle all of this, but I can certainly help. -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-20 7:31 ` Joel Brobecker @ 2005-09-20 19:18 ` Eli Zaretskii 2005-09-20 19:31 ` Joel Brobecker 0 siblings, 1 reply; 37+ messages in thread From: Eli Zaretskii @ 2005-09-20 19:18 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches > Date: Tue, 20 Sep 2005 00:30:58 -0700 > From: Joel Brobecker <brobecker@adacore.com> > Cc: gdb-patches@sources.redhat.com > > Something we haven't really formally decided though, is the significance > of the threashold. Is it: > > 1. If array size > threshold, then print indexes > 2. If array size < threshold, then print indexes The second, I think: it makes sense to ask for arrays that are not too large to be printed with additional info. > Now, the sticky part: How to implement this new interface. > > I'll argue that it's best to implement something new. I don't like > hijacking an old interface to unsigned integer and adding some aliases > to a couple of values. My reasoning is that saying that OFF is an alias > for UINT_MAX will make sense for certain cases while it actually won't > for other cases. Actually, which value to use for OFF will depend on > the what the threashold actually means. If you accept my view above, then threshold value of zero means unlimited. We already have several set/show commands that behave this way, so I don't see any problem with having yet another. > I vote for a new API. I don't see any reason for a new API. > I think we should also review the usage of the current ones, probably > cleanup a bit some of the ones that were on the road to obsolescence > (IIRC), maybe rationalize a bit more our API if needed, and add some > documentation. But that should be a separate thread. I don't think I > will be able to handle all of this, but I can certainly help. That sounds like a lot of unnecessary work for such an obscure feature, IMHO. ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-20 19:18 ` Eli Zaretskii @ 2005-09-20 19:31 ` Joel Brobecker 2005-09-20 19:33 ` Daniel Jacobowitz 2005-09-21 3:44 ` Eli Zaretskii 0 siblings, 2 replies; 37+ messages in thread From: Joel Brobecker @ 2005-09-20 19:31 UTC (permalink / raw) To: Eli Zaretskii; +Cc: gdb-patches > > 1. If array size > threshold, then print indexes > > 2. If array size < threshold, then print indexes > > The second, I think: it makes sense to ask for arrays that are not too > large to be printed with additional info. This is fine with me. > > Now, the sticky part: How to implement this new interface. > > > > I'll argue that it's best to implement something new. I don't like > > hijacking an old interface to unsigned integer and adding some aliases > > to a couple of values. My reasoning is that saying that OFF is an alias > > for UINT_MAX will make sense for certain cases while it actually won't > > for other cases. Actually, which value to use for OFF will depend on > > the what the threashold actually means. > > If you accept my view above, then threshold value of zero means > unlimited. We already have several set/show commands that behave this > way, so I don't see any problem with having yet another. Well, looks like you are now suggesting that we drop the idea of having on/off aliases, to which I disagree. How about people like me who want this feature OFF all the time, except in rare occasions? > > I vote for a new API. > > I don't see any reason for a new API. If you accept my view on the necessity of having on/off values instead of just controlling this feature with a plain integer, then maybe we have a more compelling reason? > > I think we should also review the usage of the current ones, probably > > cleanup a bit some of the ones that were on the road to obsolescence > > (IIRC), maybe rationalize a bit more our API if needed, and add some > > documentation. But that should be a separate thread. I don't think I > > will be able to handle all of this, but I can certainly help. > > That sounds like a lot of unnecessary work for such an obscure > feature, IMHO. To me, it's totally unrelated to what we are discussing. It's just a idea that popped up during the course of this discussion. You said that our current API for set/show commands is poorly documented. Daniel and I offer to look into the documentation problem, and I also suggest that this is a good time to do any cleanup if needed. -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-20 19:31 ` Joel Brobecker @ 2005-09-20 19:33 ` Daniel Jacobowitz 2005-09-20 19:39 ` Joel Brobecker 2005-09-21 3:44 ` Eli Zaretskii 1 sibling, 1 reply; 37+ messages in thread From: Daniel Jacobowitz @ 2005-09-20 19:33 UTC (permalink / raw) To: Joel Brobecker; +Cc: Eli Zaretskii, gdb-patches On Tue, Sep 20, 2005 at 12:31:32PM -0700, Joel Brobecker wrote: > > > 1. If array size > threshold, then print indexes > > > 2. If array size < threshold, then print indexes > > > > The second, I think: it makes sense to ask for arrays that are not too > > large to be printed with additional info. > > This is fine with me. I'm confused. Wasn't Jim's point that he wanted "on for large arrays only, too ugly for small arrays"? Which is the first. It sounds like we have legitimate reasons for both thresholds. I'm getting more inclined to implement neither for now, to prevent confusion. -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-20 19:33 ` Daniel Jacobowitz @ 2005-09-20 19:39 ` Joel Brobecker 2005-09-21 3:44 ` Eli Zaretskii 2005-09-22 16:47 ` Joel Brobecker 0 siblings, 2 replies; 37+ messages in thread From: Joel Brobecker @ 2005-09-20 19:39 UTC (permalink / raw) To: Eli Zaretskii, gdb-patches > I'm confused. Wasn't Jim's point that he wanted "on for large arrays > only, too ugly for small arrays"? Which is the first. That was the reason why I asked, because I sensed some disparity between what people wanted. In my particular case, it doesn't matter, because I will turn the feature on on a as-needed basis, so won't use the threshold. However, the cases where I have needed it are when the array was too big for me to do the counting... So Jim's point makes more sense to me too. > It sounds like we have legitimate reasons for both thresholds. I'm > getting more inclined to implement neither for now, to prevent confusion. This sounds good to me too. Perhaps a compromise between what Eli suggested and what you have been suggesting would work. How about we implement a on/off knob for now. Later on, if we understand better in which direction to use the threshold, then we can enhance the knob to include "auto", and then have a second knob. As long as we cross-reference each setting in the help text, I think users won't be confused. -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-20 19:39 ` Joel Brobecker @ 2005-09-21 3:44 ` Eli Zaretskii 2005-09-22 16:47 ` Joel Brobecker 1 sibling, 0 replies; 37+ messages in thread From: Eli Zaretskii @ 2005-09-21 3:44 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches > Date: Tue, 20 Sep 2005 12:39:18 -0700 > From: Joel Brobecker <brobecker@adacore.com> > > > It sounds like we have legitimate reasons for both thresholds. I'm > > getting more inclined to implement neither for now, to prevent confusion. > > This sounds good to me too. Perhaps a compromise between what Eli > suggested and what you have been suggesting would work. How about > we implement a on/off knob for now. Later on, if we understand better > in which direction to use the threshold, then we can enhance the knob > to include "auto", and then have a second knob. Fine with me. ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-20 19:39 ` Joel Brobecker 2005-09-21 3:44 ` Eli Zaretskii @ 2005-09-22 16:47 ` Joel Brobecker 2005-09-26 1:23 ` Daniel Jacobowitz 1 sibling, 1 reply; 37+ messages in thread From: Joel Brobecker @ 2005-09-22 16:47 UTC (permalink / raw) To: Eli Zaretskii, gdb-patches [-- Attachment #1: Type: text/plain, Size: 3176 bytes --] > This sounds good to me too. Perhaps a compromise between what Eli > suggested and what you have been suggesting would work. How about > we implement a on/off knob for now. Later on, if we understand better > in which direction to use the threshold, then we can enhance the knob > to include "auto", and then have a second knob. > > As long as we cross-reference each setting in the help text, I think > users won't be confused. No objection to the above, so far... Fingers crossed. Would you know, it just occured to me that the above is what my last patch actually implemented :). Assuming this intermediate approach is ok, I'm resubmitting that patch again. Note that the patch was first submitted in: http://sources.redhat.com/ml/gdb-patches/2005-09/msg00047.html 2005-09-22 Joel Brobecker <brobecker@adacore.com> * language.h (language_defn): New field la_print_array_index. (LA_PRINT_ARRAY_INDEX): New macro. (default_print_array_index): Add declaration. * language.c (default_print_array_index): new function. (unknown_language): Add value for new field. (auto_language): Likewise. (local_language): Likewise. * c-lang.c (c_language_defn): Likewise. (cpluc_language_defn): Likewise. (asm_language_defn): Likewise. (minimal_language_defn): Likewise. * f-lang.c (f_language_defn): Likewise. * jv-lang.c (java_language_defn): Likewise. * m2-lang.c (m2_language_defn): Likewise. * objc-lang.c (objc_language_defn): Likewise. * p-lang.c (pascal_language_defn): Likewise. * ada-lang.c (ada_print_array_index): New function. (ada_language_defn): Add value for new field. * valprint.h (print_array_indexes_p): Add declaration. (maybe_print_array_index): Add declaration. (val_print_array_elements): Add new parameter to function profile. * valprint.c (print_array_indexes): New static 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 array-indexes "set/show print" command. * 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. Testcases and documentation to follow after the review. Thanks, -- Joel [-- Attachment #2: indexes.diff --] [-- Type: text/plain, Size: 18615 bytes --] Index: language.h =================================================================== RCS file: /cvs/src/src/gdb/language.h,v retrieving revision 1.35 diff -u -p -r1.35 language.h --- language.h 9 May 2005 21:20:34 -0000 1.35 +++ language.h 7 Sep 2005 20:00:10 -0000 @@ -275,6 +275,12 @@ struct language_defn void (*la_language_arch_info) (struct gdbarch *, struct language_arch_info *); + /* Print the index of an element of an array. */ + void (*la_print_array_index) (struct value *index_value, + struct ui_file *stream, + int format, + enum val_prettyprint pretty); + /* Add fields above this point, so the magic number is always last. */ /* Magic number for compat checking */ @@ -362,6 +368,9 @@ extern enum language set_language (enum #define LA_EMIT_CHAR(ch, stream, quoter) \ (current_language->la_emitchar(ch, stream, quoter)) +#define LA_PRINT_ARRAY_INDEX(index_value, stream, format, pretty) \ + (current_language->la_print_array_index(index_value, stream, format, pretty)) + /* Test a character to decide whether it can be printed in literal form or needs to be printed in another representation. For example, in C the literal form of the character with octal value 141 is 'a' @@ -457,4 +466,10 @@ extern char *language_class_name_from_ph /* Splitting strings into words. */ extern char *default_word_break_characters (void); +/* Print the index of an array element using the C99 syntax. */ +extern void default_print_array_index (struct value *index_value, + struct ui_file *stream, + int format, + enum val_prettyprint pretty); + #endif /* defined (LANGUAGE_H) */ Index: language.c =================================================================== RCS file: /cvs/src/src/gdb/language.c,v retrieving revision 1.62 diff -u -p -r1.62 language.c --- language.c 29 Aug 2005 12:57:49 -0000 1.62 +++ language.c 7 Sep 2005 20:00:09 -0000 @@ -1057,6 +1057,17 @@ default_word_break_characters (void) return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; } +/* Print the index of array elements using the C99 syntax. */ + +void +default_print_array_index (struct value *index_value, struct ui_file *stream, + int format, enum val_prettyprint pretty) +{ + fprintf_filtered (stream, "["); + LA_VALUE_PRINT (index_value, stream, format, pretty); + fprintf_filtered (stream, "] = "); +} + /* Define the language that is no language. */ static int @@ -1181,6 +1192,7 @@ const struct language_defn unknown_langu NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -1217,6 +1229,7 @@ const struct language_defn auto_language NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -1252,6 +1265,7 @@ const struct language_defn local_languag NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; \f Index: c-lang.c =================================================================== RCS file: /cvs/src/src/gdb/c-lang.c,v retrieving revision 1.37 diff -u -p -r1.37 c-lang.c --- c-lang.c 9 May 2005 21:20:30 -0000 1.37 +++ c-lang.c 7 Sep 2005 19:58:49 -0000 @@ -595,6 +595,7 @@ const struct language_defn c_language_de NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; @@ -653,6 +654,7 @@ const struct language_defn cplus_languag &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -688,6 +690,7 @@ const struct language_defn asm_language_ NULL, default_word_break_characters, c_language_arch_info, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -728,6 +731,7 @@ const struct language_defn minimal_langu NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: f-lang.c =================================================================== RCS file: /cvs/src/src/gdb/f-lang.c,v retrieving revision 1.31 diff -u -p -r1.31 f-lang.c --- f-lang.c 9 May 2005 21:20:30 -0000 1.31 +++ f-lang.c 7 Sep 2005 19:59:30 -0000 @@ -485,6 +485,7 @@ const struct language_defn f_language_de &builtin_type_f_character, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: jv-lang.c =================================================================== RCS file: /cvs/src/src/gdb/jv-lang.c,v retrieving revision 1.42 diff -u -p -r1.42 jv-lang.c --- jv-lang.c 27 May 2005 04:39:32 -0000 1.42 +++ jv-lang.c 7 Sep 2005 20:00:08 -0000 @@ -1114,6 +1114,7 @@ const struct language_defn java_language NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: m2-lang.c =================================================================== RCS file: /cvs/src/src/gdb/m2-lang.c,v retrieving revision 1.23 diff -u -p -r1.23 m2-lang.c --- m2-lang.c 9 May 2005 21:20:34 -0000 1.23 +++ m2-lang.c 7 Sep 2005 20:00:14 -0000 @@ -437,6 +437,7 @@ const struct language_defn m2_language_d &builtin_type_m2_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: objc-lang.c =================================================================== RCS file: /cvs/src/src/gdb/objc-lang.c,v retrieving revision 1.49 diff -u -p -r1.49 objc-lang.c --- objc-lang.c 12 Jul 2005 12:11:44 -0000 1.49 +++ objc-lang.c 7 Sep 2005 20:00:33 -0000 @@ -684,6 +684,7 @@ const struct language_defn objc_language &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: p-lang.c =================================================================== RCS file: /cvs/src/src/gdb/p-lang.c,v retrieving revision 1.25 diff -u -p -r1.25 p-lang.c --- p-lang.c 9 May 2005 21:20:34 -0000 1.25 +++ p-lang.c 7 Sep 2005 20:00:36 -0000 @@ -477,6 +477,7 @@ const struct language_defn pascal_langua &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: scm-lang.c =================================================================== RCS file: /cvs/src/src/gdb/scm-lang.c,v retrieving revision 1.32 diff -u -p -r1.32 scm-lang.c --- scm-lang.c 27 May 2005 04:39:32 -0000 1.32 +++ scm-lang.c 7 Sep 2005 20:01:01 -0000 @@ -269,6 +269,7 @@ const struct language_defn scm_language_ NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: ada-lang.c =================================================================== RCS file: /cvs/src/src/gdb/ada-lang.c,v retrieving revision 1.78 diff -u -p -r1.78 ada-lang.c --- ada-lang.c 9 May 2005 21:20:29 -0000 1.78 +++ ada-lang.c 7 Sep 2005 19:58:31 -0000 @@ -303,6 +303,16 @@ ada_get_gdb_completer_word_break_charact return ada_completer_word_break_characters; } +/* Print an array element index using the Ada syntax. */ + +static void +ada_print_array_index (struct value *index_value, struct ui_file *stream, + int format, enum val_prettyprint pretty) +{ + LA_VALUE_PRINT (index_value, stream, format, pretty); + fprintf_filtered (stream, " => "); +} + /* Read the string located at ADDR from the inferior and store the result into BUF. */ @@ -8766,6 +8776,7 @@ const struct language_defn ada_language_ NULL, ada_get_gdb_completer_word_break_characters, ada_language_arch_info, + ada_print_array_index, LANG_MAGIC }; Index: valprint.h =================================================================== RCS file: /cvs/src/src/gdb/valprint.h,v retrieving revision 1.10 diff -u -p -r1.10 valprint.h --- valprint.h 9 May 2005 21:20:35 -0000 1.10 +++ valprint.h 7 Sep 2005 20:01:35 -0000 @@ -50,10 +50,16 @@ extern int output_format; extern int stop_print_at_null; /* Stop printing at null char? */ +extern int print_array_indexes_p (void); + +extern void maybe_print_array_index (struct type *index_type, LONGEST index, + struct ui_file *stream, int format, + enum val_prettyprint pretty); + extern void val_print_array_elements (struct type *, const gdb_byte *, CORE_ADDR, struct ui_file *, int, int, int, enum val_prettyprint, - unsigned int); + unsigned int, LONGEST); extern void val_print_type_code_int (struct type *, const gdb_byte *, struct ui_file *); Index: valprint.c =================================================================== RCS file: /cvs/src/src/gdb/valprint.c,v retrieving revision 1.54 diff -u -p -r1.54 valprint.c --- valprint.c 10 Jun 2005 06:07:32 -0000 1.54 +++ valprint.c 7 Sep 2005 20:01:35 -0000 @@ -100,6 +100,17 @@ Default output radix for printing of val } int output_format = 0; +/* By default we print arrays without printing the index of each element in + the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */ + +static int print_array_indexes = 0; +static void +show_print_array_indexes (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value); +} + /* Print repeat counts if there are more than this many repetitions of an element in an array. Referenced by the low level language dependent print routines. */ @@ -859,9 +870,41 @@ print_char_chars (struct ui_file *stream } } +/* Return non-zero if the debugger should print the index of each element + when printing array values. */ + +int +print_array_indexes_p (void) +{ + return print_array_indexes; +} + +/* Print on STREAM using the given FORMAT the index for the element + at INDEX of an array whose index type is INDEX_TYPE. */ + +void +maybe_print_array_index (struct type *index_type, LONGEST index, + struct ui_file *stream, int format, + enum val_prettyprint pretty) +{ + struct value *index_value; + + if (!print_array_indexes) + return; + + index_value = value_from_longest (index_type, index); + + LA_PRINT_ARRAY_INDEX (index_value, stream, format, pretty); +} + /* Called by various <lang>_val_print routines to print elements of an array in the form "<elem1>, <elem2>, <elem3>, ...". + Some languages such as Ada allow the user to specify arrays where + the index of the first element is not zero. REAL_INDEX_OFFSET is + the user-level index of the first element of the array. For many + languages such as C or C++, it is always zero. + (FIXME?) Assumes array element separator is a comma, which is correct for all languages currently handled. (FIXME?) Some languages have a notation for repeated array elements, @@ -873,11 +916,11 @@ val_print_array_elements (struct type *t CORE_ADDR address, struct ui_file *stream, int format, int deref_ref, int recurse, enum val_prettyprint pretty, - unsigned int i) + unsigned int i, LONGEST real_index_offset) { unsigned int things_printed = 0; unsigned len; - struct type *elttype; + struct type *elttype, *index_type; unsigned eltlen; /* Position of the array element we are examining to see whether it is repeated. */ @@ -888,6 +931,7 @@ val_print_array_elements (struct type *t elttype = TYPE_TARGET_TYPE (type); eltlen = TYPE_LENGTH (check_typedef (elttype)); len = TYPE_LENGTH (type) / eltlen; + index_type = TYPE_INDEX_TYPE (type); annotate_array_section_begin (i, elttype); @@ -906,6 +950,8 @@ val_print_array_elements (struct type *t } } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, i + real_index_offset, + stream, format, pretty); rep1 = i + 1; reps = 1; @@ -1396,6 +1442,12 @@ Show the default input and output number Use 'show input-radix' or 'show output-radix' to independently show each."), &showlist); + add_setshow_boolean_cmd ("array-indexes", class_support, + &print_array_indexes, _("\ +Set printing of array indexes."), _("\ +Show printing of array indexes"), NULL, NULL, show_print_array_indexes, + &setprintlist, &showprintlist); + /* Give people the defaults which they are used to. */ prettyprint_structs = 0; prettyprint_arrays = 0; Index: c-valprint.c =================================================================== RCS file: /cvs/src/src/gdb/c-valprint.c,v retrieving revision 1.37 diff -u -p -r1.37 c-valprint.c --- c-valprint.c 9 May 2005 21:20:30 -0000 1.37 +++ c-valprint.c 7 Sep 2005 19:58:50 -0000 @@ -133,7 +133,7 @@ c_val_print (struct type *type, const gd i = 0; } val_print_array_elements (type, valaddr + embedded_offset, address, stream, - format, deref_ref, recurse, pretty, i); + format, deref_ref, recurse, pretty, i, 0); fprintf_filtered (stream, "}"); } break; Index: p-valprint.c =================================================================== RCS file: /cvs/src/src/gdb/p-valprint.c,v retrieving revision 1.39 diff -u -p -r1.39 p-valprint.c --- p-valprint.c 9 May 2005 21:20:34 -0000 1.39 +++ p-valprint.c 7 Sep 2005 20:00:37 -0000 @@ -123,7 +123,7 @@ pascal_val_print (struct type *type, con i = 0; } val_print_array_elements (type, valaddr + embedded_offset, address, stream, - format, deref_ref, recurse, pretty, i); + format, deref_ref, recurse, pretty, i, 0); fprintf_filtered (stream, "}"); } break; Index: ada-valprint.c =================================================================== RCS file: /cvs/src/src/gdb/ada-valprint.c,v retrieving revision 1.23 diff -u -p -r1.23 ada-valprint.c --- ada-valprint.c 9 May 2005 21:20:30 -0000 1.23 +++ ada-valprint.c 7 Sep 2005 19:58:33 -0000 @@ -75,6 +75,44 @@ adjust_type_signedness (struct type *typ TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED; } +/* Assuming TYPE is a simple, non-empty array type, compute the lower + bound and the array index type. Save the low bound into LOW_BOUND + if not NULL. Save the index type in INDEX_TYPE if not NULL. + + Return 1 if the operation was successful. Return zero otherwise, + in which case the value of LOW_BOUND and INDEX_TYPE is undefined. */ + +static int +ada_get_array_low_bound_and_type (struct type *type, + long *low_bound, + struct type **index_type) +{ + struct type *index = TYPE_INDEX_TYPE (type); + long low = 0; + + if (index == NULL) + return 0; + + if (TYPE_CODE (index) != TYPE_CODE_RANGE + && TYPE_CODE (index) != TYPE_CODE_ENUM) + return 0; + + low = TYPE_LOW_BOUND (index); + if (low > TYPE_HIGH_BOUND (index)) + return 0; + + if (TYPE_CODE (index) == TYPE_CODE_RANGE) + index = TYPE_TARGET_TYPE (index); + + if (low_bound) + *low_bound = low; + + if (index_type) + *index_type = index; + + return 1; +} + /* Assuming TYPE is a simple, non-empty array type, prints its lower bound on STREAM, if non-standard (i.e., other than 1 for numbers, other than lower bound of index type for enumerated type). Returns 1 @@ -86,19 +124,10 @@ print_optional_low_bound (struct ui_file struct type *index_type; long low_bound; - index_type = TYPE_INDEX_TYPE (type); - low_bound = 0; - - if (index_type == NULL) + if (print_array_indexes_p ()) return 0; - if (TYPE_CODE (index_type) == TYPE_CODE_RANGE) - { - low_bound = TYPE_LOW_BOUND (index_type); - if (low_bound > TYPE_HIGH_BOUND (index_type)) - return 0; - index_type = TYPE_TARGET_TYPE (index_type); - } - else + + if (!ada_get_array_low_bound_and_type (type, &low_bound, &index_type)) return 0; switch (TYPE_CODE (index_type)) @@ -137,16 +166,18 @@ val_print_packed_array_elements (struct unsigned int i; unsigned int things_printed = 0; unsigned len; - struct type *elttype; + struct type *elttype, *index_type; unsigned eltlen; unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0); struct value *mark = value_mark (); + LONGEST low = 0; elttype = TYPE_TARGET_TYPE (type); eltlen = TYPE_LENGTH (check_typedef (elttype)); + index_type = TYPE_INDEX_TYPE (type); { - LONGEST low, high; + LONGEST high; if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0) len = 1; else @@ -174,6 +205,7 @@ val_print_packed_array_elements (struct } } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, i + low, stream, format, pretty); i0 = i; v0 = ada_value_primitive_packed_val (NULL, valaddr, @@ -219,6 +251,8 @@ val_print_packed_array_elements (struct fprintf_filtered (stream, ", "); } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, j + low, + stream, format, pretty); } val_print (elttype, value_contents (v0), 0, 0, stream, format, 0, recurse + 1, pretty); @@ -824,7 +858,11 @@ ada_val_print_1 (struct type *type, cons } else { + long low_bound = 0; + len = 0; + ada_get_array_low_bound_and_type (type, &low_bound, NULL); + fprintf_filtered (stream, "("); print_optional_low_bound (stream, type); if (TYPE_FIELD_BITSIZE (type, 0) > 0) @@ -833,7 +871,7 @@ ada_val_print_1 (struct type *type, cons else val_print_array_elements (type, valaddr, address, stream, format, deref_ref, recurse, - pretty, 0); + pretty, 0, low_bound); fprintf_filtered (stream, ")"); } gdb_flush (stream); ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-22 16:47 ` Joel Brobecker @ 2005-09-26 1:23 ` Daniel Jacobowitz 2005-09-27 1:04 ` Joel Brobecker 0 siblings, 1 reply; 37+ messages in thread From: Daniel Jacobowitz @ 2005-09-26 1:23 UTC (permalink / raw) To: Joel Brobecker; +Cc: Eli Zaretskii, gdb-patches On Thu, Sep 22, 2005 at 09:46:22AM -0700, Joel Brobecker wrote: > > This sounds good to me too. Perhaps a compromise between what Eli > > suggested and what you have been suggesting would work. How about > > we implement a on/off knob for now. Later on, if we understand better > > in which direction to use the threshold, then we can enhance the knob > > to include "auto", and then have a second knob. > > > > As long as we cross-reference each setting in the help text, I think > > users won't be confused. > > No objection to the above, so far... Fingers crossed. > > Would you know, it just occured to me that the above is what my last > patch actually implemented :). Assuming this intermediate approach is > ok, I'm resubmitting that patch again. > > Note that the patch was first submitted in: > > http://sources.redhat.com/ml/gdb-patches/2005-09/msg00047.html This looks OK to me, with documentation and tests. Minor comments: - Reading the diff it's apparent that there is some space vs tabs confusion in the Ada changes. I prefer tabs, but not violently so; however, I mostly prefer that code match its surroundings. Otherwise indentation in diffs looks very odd. > /* Called by various <lang>_val_print routines to print elements of an > array in the form "<elem1>, <elem2>, <elem3>, ...". > > + Some languages such as Ada allow the user to specify arrays where > + the index of the first element is not zero. REAL_INDEX_OFFSET is > + the user-level index of the first element of the array. For many > + languages such as C or C++, it is always zero. > + Ada is by no means the only language with this feature - namely, Fortran. Can't you compute this value from the array type, instead of passing it in from Ada-specific code? I couldn't see any obvious reasons why not. > +/* Assuming TYPE is a simple, non-empty array type, compute the lower > + bound and the array index type. Save the low bound into LOW_BOUND > + if not NULL. Save the index type in INDEX_TYPE if not NULL. > + > + Return 1 if the operation was successful. Return zero otherwise, > + in which case the value of LOW_BOUND and INDEX_TYPE is undefined. */ s/undefined/unmodified/, since you rely on it below. -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-26 1:23 ` Daniel Jacobowitz @ 2005-09-27 1:04 ` Joel Brobecker 2005-10-02 22:42 ` Daniel Jacobowitz 0 siblings, 1 reply; 37+ messages in thread From: Joel Brobecker @ 2005-09-27 1:04 UTC (permalink / raw) To: gdb-patches [-- Attachment #1: Type: text/plain, Size: 5063 bytes --] > This looks OK to me, with documentation and tests. Minor comments: Thanks for the review. The documentation has been submitted under: http://sources.redhat.com/ml/gdb-patches/2005-09/msg00240.html And the testcase has been submitted under: http://sources.redhat.com/ml/gdb-patches/2005-09/msg00238.html > - Reading the diff it's apparent that there is some space vs tabs > confusion in the Ada changes. I prefer tabs, but not violently so; > however, I mostly prefer that code match its surroundings. Otherwise > indentation in diffs looks very odd. Ok. There is some confusion, probably because I very much prefer spaces (I am always jerked off by the cursor suddenly jumping because of invisible tabs, it makes editing a bit more difficult, etc), but the fact is that this is currently the policy. So I have forced tabs where appropriate. BTW: I think you are using vim, IIRC. Do you have some magic settings that handles that handles the tabs for you. If I were able to have tabs automatically created after I create spaces, and also transformed into spaces when I backspace into them, as well as allowing me to go through them as if they were spaces, I wouldn't mind the tabs so much. > > /* Called by various <lang>_val_print routines to print elements of an > > array in the form "<elem1>, <elem2>, <elem3>, ...". > > > > + Some languages such as Ada allow the user to specify arrays where > > + the index of the first element is not zero. REAL_INDEX_OFFSET is > > + the user-level index of the first element of the array. For many > > + languages such as C or C++, it is always zero. > > + > > Ada is by no means the only language with this feature - namely, > Fortran. Can't you compute this value from the array type, instead > of passing it in from Ada-specific code? I couldn't see any obvious > reasons why not. I cannot either. So what I did was move that function to valprint. I also removed the part computing the index type, it should be as simple as using the TYPE_INDEX_TYPE macro, so no need to complexify this function for that. Note that this had the effect of losing the following code: + if (TYPE_CODE (index) == TYPE_CODE_RANGE) + index = TYPE_TARGET_TYPE (index); which the function used to have. I couldn't understand why this was necessary, and a look at where the type was used showed that this was not necessary, as ada_print_scalar, the only eventual consumer of that type, knew how to handle range types. I would think the same is true of all other languages, right? > > +/* Assuming TYPE is a simple, non-empty array type, compute the lower > > + bound and the array index type. Save the low bound into LOW_BOUND > > + if not NULL. Save the index type in INDEX_TYPE if not NULL. > > + > > + Return 1 if the operation was successful. Return zero otherwise, > > + in which case the value of LOW_BOUND and INDEX_TYPE is undefined. */ > > s/undefined/unmodified/, since you rely on it below. Thanks for the recommendation. I really like this term better. Here is a new patch, hopefully implementing the suggestions you made. 2005-09-22 Joel Brobecker <brobecker@adacore.com> * language.h (language_defn): New field la_print_array_index. (LA_PRINT_ARRAY_INDEX): New macro. (default_print_array_index): Add declaration. * language.c (default_print_array_index): new function. (unknown_language): Add value for new field. (auto_language): Likewise. (local_language): Likewise. * ada-lang.c (ada_print_array_index): New function. (ada_language_defn): Add value for new field. * c-lang.c (c_language_defn): Likewise. (cpluc_language_defn): Likewise. (asm_language_defn): Likewise. (minimal_language_defn): Likewise. * f-lang.c (f_language_defn): Likewise. * jv-lang.c (java_language_defn): Likewise. * m2-lang.c (m2_language_defn): Likewise. * objc-lang.c (objc_language_defn): Likewise. * p-lang.c (pascal_language_defn): Likewise. * valprint.h (print_array_indexes_p): Add declaration. (get_array_low_bound): Add declaration. (maybe_print_array_index): Add declaration. * valprint.c (print_array_indexes): New static variable. (show_print_array_indexes): New function. (print_array_indexes_p): New function. (get_array_low_bound): New function. (maybe_print_array_index): New function. (val_print_array_elements): Print the index of each element if requested by the user. (_initialize_valprint): Add new array-indexes "set/show print" command. * ada-valprint.c (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. Tested on x86-linux, no regression. Thanks, -- Joel [-- Attachment #2: arridx.diff --] [-- Type: text/plain, Size: 15770 bytes --] Index: language.h =================================================================== RCS file: /cvs/src/src/gdb/language.h,v retrieving revision 1.35 diff -u -p -r1.35 language.h --- language.h 9 May 2005 21:20:34 -0000 1.35 +++ language.h 27 Sep 2005 00:48:01 -0000 @@ -275,6 +275,12 @@ struct language_defn void (*la_language_arch_info) (struct gdbarch *, struct language_arch_info *); + /* Print the index of an element of an array. */ + void (*la_print_array_index) (struct value *index_value, + struct ui_file *stream, + int format, + enum val_prettyprint pretty); + /* Add fields above this point, so the magic number is always last. */ /* Magic number for compat checking */ @@ -362,6 +368,9 @@ extern enum language set_language (enum #define LA_EMIT_CHAR(ch, stream, quoter) \ (current_language->la_emitchar(ch, stream, quoter)) +#define LA_PRINT_ARRAY_INDEX(index_value, stream, format, pretty) \ + (current_language->la_print_array_index(index_value, stream, format, pretty)) + /* Test a character to decide whether it can be printed in literal form or needs to be printed in another representation. For example, in C the literal form of the character with octal value 141 is 'a' @@ -457,4 +466,10 @@ extern char *language_class_name_from_ph /* Splitting strings into words. */ extern char *default_word_break_characters (void); +/* Print the index of an array element using the C99 syntax. */ +extern void default_print_array_index (struct value *index_value, + struct ui_file *stream, + int format, + enum val_prettyprint pretty); + #endif /* defined (LANGUAGE_H) */ Index: language.c =================================================================== RCS file: /cvs/src/src/gdb/language.c,v retrieving revision 1.62 diff -u -p -r1.62 language.c --- language.c 29 Aug 2005 12:57:49 -0000 1.62 +++ language.c 27 Sep 2005 00:48:02 -0000 @@ -1057,6 +1057,17 @@ default_word_break_characters (void) return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; } +/* Print the index of array elements using the C99 syntax. */ + +void +default_print_array_index (struct value *index_value, struct ui_file *stream, + int format, enum val_prettyprint pretty) +{ + fprintf_filtered (stream, "["); + LA_VALUE_PRINT (index_value, stream, format, pretty); + fprintf_filtered (stream, "] = "); +} + /* Define the language that is no language. */ static int @@ -1181,6 +1192,7 @@ const struct language_defn unknown_langu NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -1217,6 +1229,7 @@ const struct language_defn auto_language NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -1252,6 +1265,7 @@ const struct language_defn local_languag NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; \f Index: ada-lang.c =================================================================== RCS file: /cvs/src/src/gdb/ada-lang.c,v retrieving revision 1.78 diff -u -p -r1.78 ada-lang.c --- ada-lang.c 9 May 2005 21:20:29 -0000 1.78 +++ ada-lang.c 27 Sep 2005 00:48:07 -0000 @@ -303,6 +303,16 @@ ada_get_gdb_completer_word_break_charact return ada_completer_word_break_characters; } +/* Print an array element index using the Ada syntax. */ + +static void +ada_print_array_index (struct value *index_value, struct ui_file *stream, + int format, enum val_prettyprint pretty) +{ + LA_VALUE_PRINT (index_value, stream, format, pretty); + fprintf_filtered (stream, " => "); +} + /* Read the string located at ADDR from the inferior and store the result into BUF. */ @@ -8766,6 +8776,7 @@ const struct language_defn ada_language_ NULL, ada_get_gdb_completer_word_break_characters, ada_language_arch_info, + ada_print_array_index, LANG_MAGIC }; Index: c-lang.c =================================================================== RCS file: /cvs/src/src/gdb/c-lang.c,v retrieving revision 1.37 diff -u -p -r1.37 c-lang.c --- c-lang.c 9 May 2005 21:20:30 -0000 1.37 +++ c-lang.c 27 Sep 2005 00:48:07 -0000 @@ -595,6 +595,7 @@ const struct language_defn c_language_de NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; @@ -653,6 +654,7 @@ const struct language_defn cplus_languag &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -688,6 +690,7 @@ const struct language_defn asm_language_ NULL, default_word_break_characters, c_language_arch_info, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -728,6 +731,7 @@ const struct language_defn minimal_langu NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: f-lang.c =================================================================== RCS file: /cvs/src/src/gdb/f-lang.c,v retrieving revision 1.31 diff -u -p -r1.31 f-lang.c --- f-lang.c 9 May 2005 21:20:30 -0000 1.31 +++ f-lang.c 27 Sep 2005 00:48:08 -0000 @@ -485,6 +485,7 @@ const struct language_defn f_language_de &builtin_type_f_character, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: jv-lang.c =================================================================== RCS file: /cvs/src/src/gdb/jv-lang.c,v retrieving revision 1.42 diff -u -p -r1.42 jv-lang.c --- jv-lang.c 27 May 2005 04:39:32 -0000 1.42 +++ jv-lang.c 27 Sep 2005 00:48:08 -0000 @@ -1114,6 +1114,7 @@ const struct language_defn java_language NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: m2-lang.c =================================================================== RCS file: /cvs/src/src/gdb/m2-lang.c,v retrieving revision 1.23 diff -u -p -r1.23 m2-lang.c --- m2-lang.c 9 May 2005 21:20:34 -0000 1.23 +++ m2-lang.c 27 Sep 2005 00:48:08 -0000 @@ -437,6 +437,7 @@ const struct language_defn m2_language_d &builtin_type_m2_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: objc-lang.c =================================================================== RCS file: /cvs/src/src/gdb/objc-lang.c,v retrieving revision 1.49 diff -u -p -r1.49 objc-lang.c --- objc-lang.c 12 Jul 2005 12:11:44 -0000 1.49 +++ objc-lang.c 27 Sep 2005 00:48:10 -0000 @@ -684,6 +684,7 @@ const struct language_defn objc_language &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: p-lang.c =================================================================== RCS file: /cvs/src/src/gdb/p-lang.c,v retrieving revision 1.25 diff -u -p -r1.25 p-lang.c --- p-lang.c 9 May 2005 21:20:34 -0000 1.25 +++ p-lang.c 27 Sep 2005 00:48:10 -0000 @@ -477,6 +477,7 @@ const struct language_defn pascal_langua &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: scm-lang.c =================================================================== RCS file: /cvs/src/src/gdb/scm-lang.c,v retrieving revision 1.32 diff -u -p -r1.32 scm-lang.c --- scm-lang.c 27 May 2005 04:39:32 -0000 1.32 +++ scm-lang.c 27 Sep 2005 00:48:10 -0000 @@ -269,6 +269,7 @@ const struct language_defn scm_language_ NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: valprint.h =================================================================== RCS file: /cvs/src/src/gdb/valprint.h,v retrieving revision 1.10 diff -u -p -r1.10 valprint.h --- valprint.h 9 May 2005 21:20:35 -0000 1.10 +++ valprint.h 27 Sep 2005 00:48:10 -0000 @@ -50,6 +50,14 @@ extern int output_format; extern int stop_print_at_null; /* Stop printing at null char? */ +extern int print_array_indexes_p (void); + +extern int get_array_low_bound (struct type *type, long *low_bound); + +extern void maybe_print_array_index (struct type *index_type, LONGEST index, + struct ui_file *stream, int format, + enum val_prettyprint pretty); + extern void val_print_array_elements (struct type *, const gdb_byte *, CORE_ADDR, struct ui_file *, int, int, int, enum val_prettyprint, Index: valprint.c =================================================================== RCS file: /cvs/src/src/gdb/valprint.c,v retrieving revision 1.54 diff -u -p -r1.54 valprint.c --- valprint.c 10 Jun 2005 06:07:32 -0000 1.54 +++ valprint.c 27 Sep 2005 00:48:11 -0000 @@ -100,6 +100,17 @@ Default output radix for printing of val } int output_format = 0; +/* By default we print arrays without printing the index of each element in + the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */ + +static int print_array_indexes = 0; +static void +show_print_array_indexes (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value); +} + /* Print repeat counts if there are more than this many repetitions of an element in an array. Referenced by the low level language dependent print routines. */ @@ -859,6 +870,65 @@ print_char_chars (struct ui_file *stream } } +/* Return non-zero if the debugger should print the index of each element + when printing array values. */ + +int +print_array_indexes_p (void) +{ + return print_array_indexes; +} + +/* Assuming TYPE is a simple, non-empty array type, compute its lower bound. + Save it into LOW_BOUND if not NULL. + + Return 1 if the operation was successful. Return zero otherwise, + in which case the value of LOW_BOUND is unmodified. */ + +int +get_array_low_bound (struct type *type, long *low_bound) +{ + struct type *index = TYPE_INDEX_TYPE (type); + long low = 0; + + if (index == NULL) + return 0; + + if (TYPE_CODE (index) != TYPE_CODE_RANGE + && TYPE_CODE (index) != TYPE_CODE_ENUM) + return 0; + + low = TYPE_LOW_BOUND (index); + if (low > TYPE_HIGH_BOUND (index)) + return 0; + + if (TYPE_CODE (index) == TYPE_CODE_RANGE) + index = TYPE_TARGET_TYPE (index); + + if (low_bound) + *low_bound = low; + + return 1; +} + +/* Print on STREAM using the given FORMAT the index for the element + at INDEX of an array whose index type is INDEX_TYPE. */ + +void +maybe_print_array_index (struct type *index_type, LONGEST index, + struct ui_file *stream, int format, + enum val_prettyprint pretty) +{ + struct value *index_value; + + if (!print_array_indexes) + return; + + index_value = value_from_longest (index_type, index); + + LA_PRINT_ARRAY_INDEX (index_value, stream, format, pretty); +} + /* Called by various <lang>_val_print routines to print elements of an array in the form "<elem1>, <elem2>, <elem3>, ...". @@ -877,17 +947,25 @@ val_print_array_elements (struct type *t { unsigned int things_printed = 0; unsigned len; - struct type *elttype; + struct type *elttype, *index_type; unsigned eltlen; /* Position of the array element we are examining to see whether it is repeated. */ unsigned int rep1; /* Number of repetitions we have detected so far. */ unsigned int reps; + long low_bound_index; + + if (!get_array_low_bound (type, &low_bound_index)) + { + warning ("unable to get low bound of array, using zero as default"); + low_bound_index = 0; + } elttype = TYPE_TARGET_TYPE (type); eltlen = TYPE_LENGTH (check_typedef (elttype)); len = TYPE_LENGTH (type) / eltlen; + index_type = TYPE_INDEX_TYPE (type); annotate_array_section_begin (i, elttype); @@ -906,6 +984,8 @@ val_print_array_elements (struct type *t } } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, i + low_bound_index, + stream, format, pretty); rep1 = i + 1; reps = 1; @@ -1396,6 +1476,12 @@ Show the default input and output number Use 'show input-radix' or 'show output-radix' to independently show each."), &showlist); + add_setshow_boolean_cmd ("array-indexes", class_support, + &print_array_indexes, _("\ +Set printing of array indexes."), _("\ +Show printing of array indexes"), NULL, NULL, show_print_array_indexes, + &setprintlist, &showprintlist); + /* Give people the defaults which they are used to. */ prettyprint_structs = 0; prettyprint_arrays = 0; Index: ada-valprint.c =================================================================== RCS file: /cvs/src/src/gdb/ada-valprint.c,v retrieving revision 1.23 diff -u -p -r1.23 ada-valprint.c --- ada-valprint.c 9 May 2005 21:20:30 -0000 1.23 +++ ada-valprint.c 27 Sep 2005 00:48:11 -0000 @@ -86,21 +86,14 @@ print_optional_low_bound (struct ui_file struct type *index_type; long low_bound; - index_type = TYPE_INDEX_TYPE (type); - low_bound = 0; - - if (index_type == NULL) + if (print_array_indexes_p ()) return 0; - if (TYPE_CODE (index_type) == TYPE_CODE_RANGE) - { - low_bound = TYPE_LOW_BOUND (index_type); - if (low_bound > TYPE_HIGH_BOUND (index_type)) - return 0; - index_type = TYPE_TARGET_TYPE (index_type); - } - else + + if (!get_array_low_bound (type, &low_bound)) return 0; + index_type = TYPE_INDEX_TYPE (type); + switch (TYPE_CODE (index_type)) { case TYPE_CODE_ENUM: @@ -137,16 +130,18 @@ val_print_packed_array_elements (struct unsigned int i; unsigned int things_printed = 0; unsigned len; - struct type *elttype; + struct type *elttype, *index_type; unsigned eltlen; unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0); struct value *mark = value_mark (); + LONGEST low = 0; elttype = TYPE_TARGET_TYPE (type); eltlen = TYPE_LENGTH (check_typedef (elttype)); + index_type = TYPE_INDEX_TYPE (type); { - LONGEST low, high; + LONGEST high; if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0) len = 1; else @@ -174,6 +169,7 @@ val_print_packed_array_elements (struct } } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, i + low, stream, format, pretty); i0 = i; v0 = ada_value_primitive_packed_val (NULL, valaddr, @@ -219,6 +215,8 @@ val_print_packed_array_elements (struct fprintf_filtered (stream, ", "); } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, j + low, + stream, format, pretty); } val_print (elttype, value_contents (v0), 0, 0, stream, format, 0, recurse + 1, pretty); ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-27 1:04 ` Joel Brobecker @ 2005-10-02 22:42 ` Daniel Jacobowitz 2005-10-03 6:17 ` Joel Brobecker 0 siblings, 1 reply; 37+ messages in thread From: Daniel Jacobowitz @ 2005-10-02 22:42 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches On Mon, Sep 26, 2005 at 06:04:20PM -0700, Joel Brobecker wrote: > BTW: I think you are using vim, IIRC. Do you have some magic settings > that handles that handles the tabs for you. If I were able to have tabs > automatically created after I create spaces, and also transformed into > spaces when I backspace into them, as well as allowing me to go through > them as if they were spaces, I wouldn't mind the tabs so much. No, sorry; I use a bastard combination of emacs and joe for most of my code editing. Can't help you with Vim. > + if (TYPE_CODE (index) == TYPE_CODE_RANGE) > + index = TYPE_TARGET_TYPE (index); > > which the function used to have. I couldn't understand why this > was necessary, and a look at where the type was used showed that > this was not necessary, as ada_print_scalar, the only eventual consumer > of that type, knew how to handle range types. I would think the same is > true of all other languages, right? Not sure what you mean by "used to have", to be honest. I guess this code had some prior life in an ACT tree, if you didn't write it? It seems like the sort of check that only Ada would need. And, this code is still in the patch below. It's definitely dead code, though, so I assume you meant to remove it. Should the "return 0;" cases in get_array_low_bound be errors? Or even internal errors (probably not)? There's already lots of code that uses TYPE_LOW_BOUND (TYPE_INDEX_TYPE (array_type)). If that's not valid, we need to know why not. -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-10-02 22:42 ` Daniel Jacobowitz @ 2005-10-03 6:17 ` Joel Brobecker 2005-10-03 15:50 ` Daniel Jacobowitz 2005-10-04 7:02 ` Joel Brobecker 0 siblings, 2 replies; 37+ messages in thread From: Joel Brobecker @ 2005-10-03 6:17 UTC (permalink / raw) To: gdb-patches [-- Attachment #1: Type: text/plain, Size: 4276 bytes --] > > + if (TYPE_CODE (index) == TYPE_CODE_RANGE) > > + index = TYPE_TARGET_TYPE (index); > > > > which the function used to have. I couldn't understand why this > > was necessary, and a look at where the type was used showed that > > this was not necessary, as ada_print_scalar, the only eventual consumer > > of that type, knew how to handle range types. I would think the same is > > true of all other languages, right? > > Not sure what you mean by "used to have", to be honest. I guess this > code had some prior life in an ACT tree, if you didn't write it? It > seems like the sort of check that only Ada would need. I wasn't clear, sorry. The code in get_array_low_bound() has been extracted from ada-valprint.c:print_optional_low_bound(). That's where all the extra code comes from. > And, this code is still in the patch below. It's definitely dead code, > though, so I assume you meant to remove it. Yes. I removed it in one tree, which I tested with our full Ada testsuite, and forgot to remove it from the tree I'm using to submit this change. Good catch. Updated patch attached, with the testsuite rerun one more time, no regression. > Should the "return 0;" cases in get_array_low_bound be errors? Or even > internal errors (probably not)? I don't think we should use an internal error in this case, since the erroneous situation can also come from incorrect debugging information. In this case, it would be misleading to label this as an internal error when the problem is outside the debugger. I wouldn't use an error in this case either, as it allows us to emit a warning instead, and recover in a way that at least allows us to print the array. Otherwise, it's more complicated to still print the array. So I would leave this unchanged. Or perhaps a little comment addition before the function, along with the description, explaining the above? > There's already lots of code that uses TYPE_LOW_BOUND (TYPE_INDEX_TYPE > (array_type)). If that's not valid, we need to know why not. The new function does essentially this, except that it adds a bit of double-checking, probably to avoid falling in a trap laid by incorrect debugging information. I'm including the ChangeLog again, as reviewing it, I noticed that I forgot to include scm-lang.c. 2005-10-02 Joel Brobecker <brobecker@adacore.com> * language.h (language_defn): New field la_print_array_index. (LA_PRINT_ARRAY_INDEX): New macro. (default_print_array_index): Add declaration. * language.c (default_print_array_index): new function. (unknown_language): Add value for new field. (auto_language): Likewise. (local_language): Likewise. * ada-lang.c (ada_print_array_index): New function. (ada_language_defn): Add value for new field. * c-lang.c (c_language_defn): Likewise. (cpluc_language_defn): Likewise. (asm_language_defn): Likewise. (minimal_language_defn): Likewise. * f-lang.c (f_language_defn): Likewise. * jv-lang.c (java_language_defn): Likewise. * m2-lang.c (m2_language_defn): Likewise. * objc-lang.c (objc_language_defn): Likewise. * p-lang.c (pascal_language_defn): Likewise. * scm-lang.c (scm_language_defn): Likewise. * valprint.h (print_array_indexes_p): Add declaration. (get_array_low_bound): Add declaration. (maybe_print_array_index): Add declaration. * valprint.c (print_array_indexes): New static variable. (show_print_array_indexes): New function. (print_array_indexes_p): New function. (get_array_low_bound): New function. (maybe_print_array_index): New function. (val_print_array_elements): Print the index of each element if requested by the user. (_initialize_valprint): Add new array-indexes "set/show print" command. * ada-valprint.c (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. Re-tested on x86-linux, no regression. Thanks, -- Joel [-- Attachment #2: idx.diff --] [-- Type: text/plain, Size: 15671 bytes --] Index: language.h =================================================================== RCS file: /cvs/src/src/gdb/language.h,v retrieving revision 1.35 diff -u -p -r1.35 language.h --- language.h 9 May 2005 21:20:34 -0000 1.35 +++ language.h 3 Oct 2005 06:13:28 -0000 @@ -275,6 +275,12 @@ struct language_defn void (*la_language_arch_info) (struct gdbarch *, struct language_arch_info *); + /* Print the index of an element of an array. */ + void (*la_print_array_index) (struct value *index_value, + struct ui_file *stream, + int format, + enum val_prettyprint pretty); + /* Add fields above this point, so the magic number is always last. */ /* Magic number for compat checking */ @@ -362,6 +368,9 @@ extern enum language set_language (enum #define LA_EMIT_CHAR(ch, stream, quoter) \ (current_language->la_emitchar(ch, stream, quoter)) +#define LA_PRINT_ARRAY_INDEX(index_value, stream, format, pretty) \ + (current_language->la_print_array_index(index_value, stream, format, pretty)) + /* Test a character to decide whether it can be printed in literal form or needs to be printed in another representation. For example, in C the literal form of the character with octal value 141 is 'a' @@ -457,4 +466,10 @@ extern char *language_class_name_from_ph /* Splitting strings into words. */ extern char *default_word_break_characters (void); +/* Print the index of an array element using the C99 syntax. */ +extern void default_print_array_index (struct value *index_value, + struct ui_file *stream, + int format, + enum val_prettyprint pretty); + #endif /* defined (LANGUAGE_H) */ Index: language.c =================================================================== RCS file: /cvs/src/src/gdb/language.c,v retrieving revision 1.62 diff -u -p -r1.62 language.c --- language.c 29 Aug 2005 12:57:49 -0000 1.62 +++ language.c 3 Oct 2005 06:13:29 -0000 @@ -1057,6 +1057,17 @@ default_word_break_characters (void) return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; } +/* Print the index of array elements using the C99 syntax. */ + +void +default_print_array_index (struct value *index_value, struct ui_file *stream, + int format, enum val_prettyprint pretty) +{ + fprintf_filtered (stream, "["); + LA_VALUE_PRINT (index_value, stream, format, pretty); + fprintf_filtered (stream, "] = "); +} + /* Define the language that is no language. */ static int @@ -1181,6 +1192,7 @@ const struct language_defn unknown_langu NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -1217,6 +1229,7 @@ const struct language_defn auto_language NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -1252,6 +1265,7 @@ const struct language_defn local_languag NULL, default_word_break_characters, unknown_language_arch_info, /* la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; \f Index: ada-lang.c =================================================================== RCS file: /cvs/src/src/gdb/ada-lang.c,v retrieving revision 1.78 diff -u -p -r1.78 ada-lang.c --- ada-lang.c 9 May 2005 21:20:29 -0000 1.78 +++ ada-lang.c 3 Oct 2005 06:13:34 -0000 @@ -303,6 +303,16 @@ ada_get_gdb_completer_word_break_charact return ada_completer_word_break_characters; } +/* Print an array element index using the Ada syntax. */ + +static void +ada_print_array_index (struct value *index_value, struct ui_file *stream, + int format, enum val_prettyprint pretty) +{ + LA_VALUE_PRINT (index_value, stream, format, pretty); + fprintf_filtered (stream, " => "); +} + /* Read the string located at ADDR from the inferior and store the result into BUF. */ @@ -8766,6 +8776,7 @@ const struct language_defn ada_language_ NULL, ada_get_gdb_completer_word_break_characters, ada_language_arch_info, + ada_print_array_index, LANG_MAGIC }; Index: c-lang.c =================================================================== RCS file: /cvs/src/src/gdb/c-lang.c,v retrieving revision 1.37 diff -u -p -r1.37 c-lang.c --- c-lang.c 9 May 2005 21:20:30 -0000 1.37 +++ c-lang.c 3 Oct 2005 06:13:34 -0000 @@ -595,6 +595,7 @@ const struct language_defn c_language_de NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; @@ -653,6 +654,7 @@ const struct language_defn cplus_languag &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -688,6 +690,7 @@ const struct language_defn asm_language_ NULL, default_word_break_characters, c_language_arch_info, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; @@ -728,6 +731,7 @@ const struct language_defn minimal_langu NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: f-lang.c =================================================================== RCS file: /cvs/src/src/gdb/f-lang.c,v retrieving revision 1.31 diff -u -p -r1.31 f-lang.c --- f-lang.c 9 May 2005 21:20:30 -0000 1.31 +++ f-lang.c 3 Oct 2005 06:13:35 -0000 @@ -485,6 +485,7 @@ const struct language_defn f_language_de &builtin_type_f_character, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: jv-lang.c =================================================================== RCS file: /cvs/src/src/gdb/jv-lang.c,v retrieving revision 1.42 diff -u -p -r1.42 jv-lang.c --- jv-lang.c 27 May 2005 04:39:32 -0000 1.42 +++ jv-lang.c 3 Oct 2005 06:13:36 -0000 @@ -1114,6 +1114,7 @@ const struct language_defn java_language NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: m2-lang.c =================================================================== RCS file: /cvs/src/src/gdb/m2-lang.c,v retrieving revision 1.23 diff -u -p -r1.23 m2-lang.c --- m2-lang.c 9 May 2005 21:20:34 -0000 1.23 +++ m2-lang.c 3 Oct 2005 06:13:36 -0000 @@ -437,6 +437,7 @@ const struct language_defn m2_language_d &builtin_type_m2_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: objc-lang.c =================================================================== RCS file: /cvs/src/src/gdb/objc-lang.c,v retrieving revision 1.49 diff -u -p -r1.49 objc-lang.c --- objc-lang.c 12 Jul 2005 12:11:44 -0000 1.49 +++ objc-lang.c 3 Oct 2005 06:13:36 -0000 @@ -684,6 +684,7 @@ const struct language_defn objc_language &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: p-lang.c =================================================================== RCS file: /cvs/src/src/gdb/p-lang.c,v retrieving revision 1.25 diff -u -p -r1.25 p-lang.c --- p-lang.c 9 May 2005 21:20:34 -0000 1.25 +++ p-lang.c 3 Oct 2005 06:13:37 -0000 @@ -477,6 +477,7 @@ const struct language_defn pascal_langua &builtin_type_char, /* Type of string elements */ default_word_break_characters, NULL, /* FIXME: la_language_arch_info. */ + default_print_array_index, LANG_MAGIC }; Index: scm-lang.c =================================================================== RCS file: /cvs/src/src/gdb/scm-lang.c,v retrieving revision 1.32 diff -u -p -r1.32 scm-lang.c --- scm-lang.c 27 May 2005 04:39:32 -0000 1.32 +++ scm-lang.c 3 Oct 2005 06:13:37 -0000 @@ -269,6 +269,7 @@ const struct language_defn scm_language_ NULL, default_word_break_characters, c_language_arch_info, + default_print_array_index, LANG_MAGIC }; Index: valprint.h =================================================================== RCS file: /cvs/src/src/gdb/valprint.h,v retrieving revision 1.10 diff -u -p -r1.10 valprint.h --- valprint.h 9 May 2005 21:20:35 -0000 1.10 +++ valprint.h 3 Oct 2005 06:13:37 -0000 @@ -50,6 +50,14 @@ extern int output_format; extern int stop_print_at_null; /* Stop printing at null char? */ +extern int print_array_indexes_p (void); + +extern int get_array_low_bound (struct type *type, long *low_bound); + +extern void maybe_print_array_index (struct type *index_type, LONGEST index, + struct ui_file *stream, int format, + enum val_prettyprint pretty); + extern void val_print_array_elements (struct type *, const gdb_byte *, CORE_ADDR, struct ui_file *, int, int, int, enum val_prettyprint, Index: valprint.c =================================================================== RCS file: /cvs/src/src/gdb/valprint.c,v retrieving revision 1.54 diff -u -p -r1.54 valprint.c --- valprint.c 10 Jun 2005 06:07:32 -0000 1.54 +++ valprint.c 3 Oct 2005 06:13:38 -0000 @@ -100,6 +100,17 @@ Default output radix for printing of val } int output_format = 0; +/* By default we print arrays without printing the index of each element in + the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */ + +static int print_array_indexes = 0; +static void +show_print_array_indexes (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value); +} + /* Print repeat counts if there are more than this many repetitions of an element in an array. Referenced by the low level language dependent print routines. */ @@ -859,6 +870,62 @@ print_char_chars (struct ui_file *stream } } +/* Return non-zero if the debugger should print the index of each element + when printing array values. */ + +int +print_array_indexes_p (void) +{ + return print_array_indexes; +} + +/* Assuming TYPE is a simple, non-empty array type, compute its lower bound. + Save it into LOW_BOUND if not NULL. + + Return 1 if the operation was successful. Return zero otherwise, + in which case the value of LOW_BOUND is unmodified. */ + +int +get_array_low_bound (struct type *type, long *low_bound) +{ + struct type *index = TYPE_INDEX_TYPE (type); + long low = 0; + + if (index == NULL) + return 0; + + if (TYPE_CODE (index) != TYPE_CODE_RANGE + && TYPE_CODE (index) != TYPE_CODE_ENUM) + return 0; + + low = TYPE_LOW_BOUND (index); + if (low > TYPE_HIGH_BOUND (index)) + return 0; + + if (low_bound) + *low_bound = low; + + return 1; +} + +/* Print on STREAM using the given FORMAT the index for the element + at INDEX of an array whose index type is INDEX_TYPE. */ + +void +maybe_print_array_index (struct type *index_type, LONGEST index, + struct ui_file *stream, int format, + enum val_prettyprint pretty) +{ + struct value *index_value; + + if (!print_array_indexes) + return; + + index_value = value_from_longest (index_type, index); + + LA_PRINT_ARRAY_INDEX (index_value, stream, format, pretty); +} + /* Called by various <lang>_val_print routines to print elements of an array in the form "<elem1>, <elem2>, <elem3>, ...". @@ -877,17 +944,25 @@ val_print_array_elements (struct type *t { unsigned int things_printed = 0; unsigned len; - struct type *elttype; + struct type *elttype, *index_type; unsigned eltlen; /* Position of the array element we are examining to see whether it is repeated. */ unsigned int rep1; /* Number of repetitions we have detected so far. */ unsigned int reps; + long low_bound_index; + + if (!get_array_low_bound (type, &low_bound_index)) + { + warning ("unable to get low bound of array, using zero as default"); + low_bound_index = 0; + } elttype = TYPE_TARGET_TYPE (type); eltlen = TYPE_LENGTH (check_typedef (elttype)); len = TYPE_LENGTH (type) / eltlen; + index_type = TYPE_INDEX_TYPE (type); annotate_array_section_begin (i, elttype); @@ -906,6 +981,8 @@ val_print_array_elements (struct type *t } } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, i + low_bound_index, + stream, format, pretty); rep1 = i + 1; reps = 1; @@ -1396,6 +1473,12 @@ Show the default input and output number Use 'show input-radix' or 'show output-radix' to independently show each."), &showlist); + add_setshow_boolean_cmd ("array-indexes", class_support, + &print_array_indexes, _("\ +Set printing of array indexes."), _("\ +Show printing of array indexes"), NULL, NULL, show_print_array_indexes, + &setprintlist, &showprintlist); + /* Give people the defaults which they are used to. */ prettyprint_structs = 0; prettyprint_arrays = 0; Index: ada-valprint.c =================================================================== RCS file: /cvs/src/src/gdb/ada-valprint.c,v retrieving revision 1.23 diff -u -p -r1.23 ada-valprint.c --- ada-valprint.c 9 May 2005 21:20:30 -0000 1.23 +++ ada-valprint.c 3 Oct 2005 06:13:38 -0000 @@ -86,21 +86,14 @@ print_optional_low_bound (struct ui_file struct type *index_type; long low_bound; - index_type = TYPE_INDEX_TYPE (type); - low_bound = 0; - - if (index_type == NULL) + if (print_array_indexes_p ()) return 0; - if (TYPE_CODE (index_type) == TYPE_CODE_RANGE) - { - low_bound = TYPE_LOW_BOUND (index_type); - if (low_bound > TYPE_HIGH_BOUND (index_type)) - return 0; - index_type = TYPE_TARGET_TYPE (index_type); - } - else + + if (!get_array_low_bound (type, &low_bound)) return 0; + index_type = TYPE_INDEX_TYPE (type); + switch (TYPE_CODE (index_type)) { case TYPE_CODE_ENUM: @@ -137,16 +130,18 @@ val_print_packed_array_elements (struct unsigned int i; unsigned int things_printed = 0; unsigned len; - struct type *elttype; + struct type *elttype, *index_type; unsigned eltlen; unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0); struct value *mark = value_mark (); + LONGEST low = 0; elttype = TYPE_TARGET_TYPE (type); eltlen = TYPE_LENGTH (check_typedef (elttype)); + index_type = TYPE_INDEX_TYPE (type); { - LONGEST low, high; + LONGEST high; if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0) len = 1; else @@ -174,6 +169,7 @@ val_print_packed_array_elements (struct } } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, i + low, stream, format, pretty); i0 = i; v0 = ada_value_primitive_packed_val (NULL, valaddr, @@ -219,6 +215,8 @@ val_print_packed_array_elements (struct fprintf_filtered (stream, ", "); } wrap_here (n_spaces (2 + 2 * recurse)); + maybe_print_array_index (index_type, j + low, + stream, format, pretty); } val_print (elttype, value_contents (v0), 0, 0, stream, format, 0, recurse + 1, pretty); ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-10-03 6:17 ` Joel Brobecker @ 2005-10-03 15:50 ` Daniel Jacobowitz 2005-10-03 21:23 ` Joel Brobecker 2005-10-04 7:02 ` Joel Brobecker 1 sibling, 1 reply; 37+ messages in thread From: Daniel Jacobowitz @ 2005-10-03 15:50 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches On Sun, Oct 02, 2005 at 11:17:33PM -0700, Joel Brobecker wrote: > > Should the "return 0;" cases in get_array_low_bound be errors? Or even > > internal errors (probably not)? > > I don't think we should use an internal error in this case, since the > erroneous situation can also come from incorrect debugging information. > In this case, it would be misleading to label this as an internal error > when the problem is outside the debugger. > > I wouldn't use an error in this case either, as it allows us to emit > a warning instead, and recover in a way that at least allows us to > print the array. Otherwise, it's more complicated to still print the > array. > > So I would leave this unchanged. Or perhaps a little comment addition > before the function, along with the description, explaining the above? A comment would be nice. With that change, the patch (and testcase and docs) are OK. > > There's already lots of code that uses TYPE_LOW_BOUND (TYPE_INDEX_TYPE > > (array_type)). If that's not valid, we need to know why not. > > The new function does essentially this, except that it adds a bit > of double-checking, probably to avoid falling in a trap laid by > incorrect debugging information. Nick was recently writing some code that needed the bounds of an array. We found three or four different idioms for finding this used throughout the GDB source. Figuring out which ones are right and which ones are obsolete is a royal pain. That's my only concern here. -- Daniel Jacobowitz CodeSourcery, LLC ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-10-03 15:50 ` Daniel Jacobowitz @ 2005-10-03 21:23 ` Joel Brobecker 0 siblings, 0 replies; 37+ messages in thread From: Joel Brobecker @ 2005-10-03 21:23 UTC (permalink / raw) To: gdb-patches > A comment would be nice. With that change, the patch (and testcase and > docs) are OK. Thank you. Here is the comment I added: Computing the array lower bound is pretty easy, but this function does some additional verifications before returning the low bound. If something incorrect is detected, it is better to return a status rather than throwing an error, making it easier for the caller to implement an error-recovery plan. For instance, it may decide to warn the user that the bound was not found and then use a default value instead. */ It occurs to me that we might eventually want to move this function somewhere more general, add a parameter for the upper bound, and use it everywhere... Some thoughts for a rainy day... Patch checked in. Currently working on the testcase and doc. Thanks, -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-10-03 6:17 ` Joel Brobecker 2005-10-03 15:50 ` Daniel Jacobowitz @ 2005-10-04 7:02 ` Joel Brobecker 2005-10-04 7:41 ` Joel Brobecker 1 sibling, 1 reply; 37+ messages in thread From: Joel Brobecker @ 2005-10-04 7:02 UTC (permalink / raw) To: gdb-patches Hmmmm, I was working on transforming one of our internal testcase (which uses Ada) for the patch below into a dejagnu testcase. It should all be smooth since I did the testing manually with the patch below, but it appears I missed something. I'm seeing some unexpected failures. > 2005-10-02 Joel Brobecker <brobecker@adacore.com> > > * language.h (language_defn): New field la_print_array_index. > (LA_PRINT_ARRAY_INDEX): New macro. > (default_print_array_index): Add declaration. > * language.c (default_print_array_index): new function. > (unknown_language): Add value for new field. > (auto_language): Likewise. > (local_language): Likewise. > * ada-lang.c (ada_print_array_index): New function. > (ada_language_defn): Add value for new field. > * c-lang.c (c_language_defn): Likewise. > (cpluc_language_defn): Likewise. > (asm_language_defn): Likewise. > (minimal_language_defn): Likewise. > * f-lang.c (f_language_defn): Likewise. > * jv-lang.c (java_language_defn): Likewise. > * m2-lang.c (m2_language_defn): Likewise. > * objc-lang.c (objc_language_defn): Likewise. > * p-lang.c (pascal_language_defn): Likewise. > * scm-lang.c (scm_language_defn): Likewise. > * valprint.h (print_array_indexes_p): Add declaration. > (get_array_low_bound): Add declaration. > (maybe_print_array_index): Add declaration. > * valprint.c (print_array_indexes): New static variable. > (show_print_array_indexes): New function. > (print_array_indexes_p): New function. > (get_array_low_bound): New function. > (maybe_print_array_index): New function. > (val_print_array_elements): Print the index of each element if > requested by the user. > (_initialize_valprint): Add new array-indexes "set/show print" command. > * ada-valprint.c (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. Issues: 1. I now understand the why of the if (TYPE_CODE (index_type) == RANGE_TYPE) index_type = TYPE_TARGET_TYPE (index_type) But this is an Ada-specific thing, and I'll take care of this in a separate patch. And I'll add a testcase as a bonus. 2. More annoying, I just tried something else, which is to print an empty Ada array. Ada allows you to create this by using declarations such as this: Table : array (1 .. 0) of Integer; Basically, if the lower/left bound is greater than the right/higher bound, then the array is empty. I just discovered that ada_val_print_1 calls val_print_array_elements even if the array is empty. This triggers one of the guards in get_array_low_bound(), since the function expects a non-empty array. I'm happy to just put a check in the Ada part to avoid calling val_print_array_elements for empty arrays. This should be fine, but I'm wondering if other languages might be doing the same, in which case it's probably going to be safer to modify a bit val_print_array_elements to handle empty arrays... Sorry about that... -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-10-04 7:02 ` Joel Brobecker @ 2005-10-04 7:41 ` Joel Brobecker 0 siblings, 0 replies; 37+ messages in thread From: Joel Brobecker @ 2005-10-04 7:41 UTC (permalink / raw) To: gdb-patches [-- Attachment #1: Type: text/plain, Size: 1410 bytes --] BTW, I forgot to mention a couple of things regarding: > 2. More annoying, I just tried something else, which is to print an > empty Ada array. Ada allows you to create this by using declarations > such as this: > > Table : array (1 .. 0) of Integer; > > Basically, if the lower/left bound is greater than the right/higher > bound, then the array is empty. > > I just discovered that ada_val_print_1 calls val_print_array_elements > even if the array is empty. This triggers one of the guards in > get_array_low_bound(), since the function expects a non-empty array. > > I'm happy to just put a check in the Ada part to avoid calling > val_print_array_elements for empty arrays. This should be fine, but > I'm wondering if other languages might be doing the same, in which > case it's probably going to be safer to modify a bit > val_print_array_elements to handle empty arrays... a. I tried with a C example, something like this: int table [] = {}; And verified that GDB worked correctly (printed the address of the array, but no value). So there is no issue with C, at least. Don't have a pascal compiler handy, though. b. I prefer the second approach. It just feels safer and more elegant. In fact I just finished testing the attached patch. I will submit a proper patch together with a testcase tomorrow. -- Joel [-- Attachment #2: valprint.c.diff --] [-- Type: text/plain, Size: 1225 bytes --] Index: valprint.c =================================================================== RCS file: /cvs/src/src/gdb/valprint.c,v retrieving revision 1.55 diff -u -p -r1.55 valprint.c --- valprint.c 3 Oct 2005 21:21:20 -0000 1.55 +++ valprint.c 4 Oct 2005 07:40:59 -0000 @@ -959,19 +959,21 @@ val_print_array_elements (struct type *t unsigned int rep1; /* Number of repetitions we have detected so far. */ unsigned int reps; - long low_bound_index; - - if (!get_array_low_bound (type, &low_bound_index)) - { - warning ("unable to get low bound of array, using zero as default"); - low_bound_index = 0; - } + long low_bound_index = 0; elttype = TYPE_TARGET_TYPE (type); eltlen = TYPE_LENGTH (check_typedef (elttype)); len = TYPE_LENGTH (type) / eltlen; index_type = TYPE_INDEX_TYPE (type); + /* Get the array low bound. This only makes sense if the array + has one or more element in it. */ + if (len > 0 && !get_array_low_bound (type, &low_bound_index)) + { + warning ("unable to get low bound of array, using zero as default"); + low_bound_index = 0; + } + annotate_array_section_begin (i, elttype); for (; i < len && things_printed < print_max; i++) ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-20 19:31 ` Joel Brobecker 2005-09-20 19:33 ` Daniel Jacobowitz @ 2005-09-21 3:44 ` Eli Zaretskii 1 sibling, 0 replies; 37+ messages in thread From: Eli Zaretskii @ 2005-09-21 3:44 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches > Date: Tue, 20 Sep 2005 12:31:32 -0700 > From: Joel Brobecker <brobecker@adacore.com> > Cc: gdb-patches@sources.redhat.com > > > If you accept my view above, then threshold value of zero means > > unlimited. We already have several set/show commands that behave this > > way, so I don't see any problem with having yet another. > > Well, looks like you are now suggesting that we drop the idea of having > on/off aliases, to which I disagree. The aliases was not my idea, I only agreed to have them. > How about people like me who want > this feature OFF all the time, except in rare occasions? That should be the default, so no problem for people ``like you''. > To me, it's totally unrelated to what we are discussing. It's just > a idea that popped up during the course of this discussion. Then let's discuss it in a separate thread. ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-18 3:37 ` Eli Zaretskii 2005-09-18 3:46 ` Daniel Jacobowitz @ 2005-09-18 8:53 ` Mark Kettenis 2005-09-18 19:10 ` Eli Zaretskii 1 sibling, 1 reply; 37+ messages in thread From: Mark Kettenis @ 2005-09-18 8:53 UTC (permalink / raw) To: eliz; +Cc: brobecker, gdb-patches > Date: Sun, 18 Sep 2005 06:37:19 +0300 > From: Eli Zaretskii <eliz@gnu.org> > > > Date: Sat, 17 Sep 2005 14:51:38 -0700 > > From: Joel Brobecker <brobecker@adacore.com> > > > > > Then doesn't it make sense to agree on the interface first? :-) > > > > Right! I just wanted to hear a few other opinions to choose. > > Perhaps Eli and/or Mark would like to comment? > > > > In any case: > > > > > You suggested on/off/auto and a separate threshold. Jim suggested > > > on/off/threshold. I prefer on/off/threshold of those two options, > > > although it may be a bit tricky to get GDB to handle that correctly. > > > Want to give it a try, or continue discussing alternatives? > > > > I'm willing to give it a try. I couldn't find a mechanism in our > > "set/show" machinery that handled something like this, though. > > Unless I missed it, that's something I'll need to add too. > > I don't see any disadvantages to the on/off/auto+threshold method that > would justify yet another add_* interface. Can someone please tell > why is that a good idea? Because it is more complicated as a user interface; in the on/off/threshold case the user only has to remember a single command! Mark ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFA] print arrays with indexes 2005-09-18 8:53 ` Mark Kettenis @ 2005-09-18 19:10 ` Eli Zaretskii 0 siblings, 0 replies; 37+ messages in thread From: Eli Zaretskii @ 2005-09-18 19:10 UTC (permalink / raw) To: Mark Kettenis; +Cc: brobecker, gdb-patches > Date: Sun, 18 Sep 2005 10:52:54 +0200 (CEST) > From: Mark Kettenis <mark.kettenis@xs4all.nl> > CC: brobecker@adacore.com, gdb-patches@sources.redhat.com > > Because it is more complicated as a user interface; in the > on/off/threshold case the user only has to remember a single command! I don't think we should rely on memory here: who can possibly remember all the commands we have in GDB?? My advice to every user is to use apropos, in which case they should see all the related commands, no matter how many of them are there. ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFC/RFA] print arrays with indexes 2005-09-07 13:23 ` Daniel Jacobowitz 2005-09-07 20:24 ` Joel Brobecker @ 2005-09-07 20:39 ` Jim Blandy 2005-09-07 21:41 ` Joel Brobecker 1 sibling, 1 reply; 37+ messages in thread From: Jim Blandy @ 2005-09-07 20:39 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches Daniel Jacobowitz <drow@false.org> writes: > I've got no strong opinions on this either way. Thresholds seem > complicated as a UI. The printing code seems to have a few thresholds --- 'show print repeats', 'show print elements', maybe others. It's a waste of time when they hide or obscure information I need, and I have to go in and change them. But I don't see this one having that effect; both the labeled and unlabeled forms show all the information there is. It's just a question of whether it's worth avoiding stuff like: (gdb) print pipe $2 = { [0] = 6, [1] = 7 } (gdb) in favor of: (gdb) print pipe $2 = { 6, 7 } (gdb) ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFC/RFA] print arrays with indexes 2005-09-07 20:39 ` [RFC/RFA] " Jim Blandy @ 2005-09-07 21:41 ` Joel Brobecker 2005-09-09 19:14 ` Jim Blandy 0 siblings, 1 reply; 37+ messages in thread From: Joel Brobecker @ 2005-09-07 21:41 UTC (permalink / raw) To: Jim Blandy; +Cc: gdb-patches > > I've got no strong opinions on this either way. Thresholds seem > > complicated as a UI. > > The printing code seems to have a few thresholds --- 'show print > repeats', 'show print elements', maybe others. It's a waste of time > when they hide or obscure information I need, and I have to go in and > change them. But I don't see this one having that effect; both the > labeled and unlabeled forms show all the information there is. It's > just a question of whether it's worth avoiding stuff like: > > (gdb) print pipe > $2 = { [0] = 6, [1] = 7 } > (gdb) > > in favor of: > > (gdb) print pipe > $2 = { 6, 7 } > (gdb) Speaking for my personal usage, I find that the labeled form is pretty hard to read. So I will use it occasionally when there are too many elements in the array for me to count. The rest of the time, I will have that feature turned off. How about we enhance a bit the approach, and have: set print array-indexes on/off/auto If set to auto, then we use the threshold? (I'm so glad I haven't started writing the doc and the testcases yet :-). -- Joel ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFC/RFA] print arrays with indexes 2005-09-07 21:41 ` Joel Brobecker @ 2005-09-09 19:14 ` Jim Blandy 0 siblings, 0 replies; 37+ messages in thread From: Jim Blandy @ 2005-09-09 19:14 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches Joel Brobecker <brobecker@adacore.com> writes: > How about we enhance a bit the approach, and have: > > set print array-indexes on/off/auto > > If set to auto, then we use the threshold? How about: set print array-indexes on/off/THRESHOLD-N ^ permalink raw reply [flat|nested] 37+ messages in thread
* Re: [RFC/RFA] print arrays with indexes 2005-09-06 20:20 [RFC/RFA] print arrays with indexes Joel Brobecker 2005-09-06 20:57 ` Daniel Jacobowitz @ 2005-09-06 21:45 ` Jim Blandy 1 sibling, 0 replies; 37+ messages in thread From: Jim Blandy @ 2005-09-06 21:45 UTC (permalink / raw) To: Joel Brobecker; +Cc: gdb-patches Joel Brobecker <brobecker@adacore.com> writes: > 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. How about a size threshold, below which the indexes would be omitted? It could default to whatever size it becomes hard to see by hand --- seven or ten or so. ^ permalink raw reply [flat|nested] 37+ messages in thread
end of thread, other threads:[~2005-10-04 7:41 UTC | newest] Thread overview: 37+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2005-09-06 20:20 [RFC/RFA] print arrays with indexes Joel Brobecker 2005-09-06 20:57 ` Daniel Jacobowitz 2005-09-07 5:40 ` Joel Brobecker 2005-09-07 13:23 ` Daniel Jacobowitz 2005-09-07 20:24 ` Joel Brobecker 2005-09-14 17:13 ` [RFA] " Joel Brobecker 2005-09-17 20:49 ` Daniel Jacobowitz 2005-09-17 21:51 ` Joel Brobecker 2005-09-17 22:07 ` Daniel Jacobowitz 2005-09-18 3:37 ` Eli Zaretskii 2005-09-18 3:46 ` Daniel Jacobowitz 2005-09-18 5:41 ` Joel Brobecker 2005-09-18 19:08 ` Eli Zaretskii 2005-09-18 19:19 ` Daniel Jacobowitz 2005-09-18 20:05 ` Eli Zaretskii 2005-09-20 7:31 ` Joel Brobecker 2005-09-20 19:18 ` Eli Zaretskii 2005-09-20 19:31 ` Joel Brobecker 2005-09-20 19:33 ` Daniel Jacobowitz 2005-09-20 19:39 ` Joel Brobecker 2005-09-21 3:44 ` Eli Zaretskii 2005-09-22 16:47 ` Joel Brobecker 2005-09-26 1:23 ` Daniel Jacobowitz 2005-09-27 1:04 ` Joel Brobecker 2005-10-02 22:42 ` Daniel Jacobowitz 2005-10-03 6:17 ` Joel Brobecker 2005-10-03 15:50 ` Daniel Jacobowitz 2005-10-03 21:23 ` Joel Brobecker 2005-10-04 7:02 ` Joel Brobecker 2005-10-04 7:41 ` Joel Brobecker 2005-09-21 3:44 ` Eli Zaretskii 2005-09-18 8:53 ` Mark Kettenis 2005-09-18 19:10 ` Eli Zaretskii 2005-09-07 20:39 ` [RFC/RFA] " Jim Blandy 2005-09-07 21:41 ` Joel Brobecker 2005-09-09 19:14 ` Jim Blandy 2005-09-06 21:45 ` Jim Blandy
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox