binutils-gdb/gdb/testsuite/gdb.base/max-depth.c
Andrew Burgess 2e62ab400f gdb: Introduce 'print max-depth' feature
Introduce a new print setting max-depth which can be set with 'set
print max-depth DEPTH'.  The default value of DEPTH is 20, but this
can also be set to unlimited.

When GDB is printing a value containing nested structures GDB will
stop descending at depth DEPTH.  Here is a small example:

    typedef struct s1 { int a; } s1;
    typedef struct s2 { s1 b; } s2;
    typedef struct s3 { s2 c; } s3;
    typedef struct s4 { s3 d; } s4;

    s4 var = { { { { 3 } } } };

The following table shows how various depth settings affect printing
of 'var':

    | Depth Setting | Result of 'p var'              |
    |---------------+--------------------------------|
    |     Unlimited | $1 = {d = {c = {b = {a = 3}}}} |
    |             4 | $1 = {d = {c = {b = {a = 3}}}} |
    |             3 | $1 = {d = {c = {b = {...}}}}   |
    |             2 | $1 = {d = {c = {...}}}         |
    |             1 | $1 = {d = {...}}               |
    |             0 | $1 = {...}                     |

Only structures, unions, and arrays are replaced in this way, scalars
and strings are not replaced.

The replacement is counted from the level at which you print, not from
the top level of the structure.  So, consider the above example and
this GDB session:

    (gdb) set print max-depth 2
    (gdb) p var
    $1 = {d = {c = {...}}}
    (gdb) p var.d
    $2 = {c = {b = {...}}}
    (gdb) p var.d.c
    $3 = {b = {a = 3}}

Setting the max-depth to 2 doesn't prevent the user from exploring
deeper into 'var' by asking for specific sub-fields to be printed.

The motivation behind this feature is to try and give the user more
control over how much is printed when examining large, complex data
structures.

The default max-depth of 20 means that there is a change in GDB's
default behaviour.  Someone printing a data structure with 20 levels
of nesting will now see '{...}' instead of their data, they would need
to adjust the max depth, or call print again naming a specific field
in order to dig deeper into their data structure.  If this is
considered a problem then we could increase the default, or even make
the default unlimited.

This commit relies on the previous commit, which added a new field to
the language structure, this new field was a string that contained the
pattern that should be used when a structure/union/array is replaced
in the output, this allows languages to use a syntax that is more
appropriate, mostly this will be selecting the correct types of
bracket '(...)' or '{...}', both of which are currently in use.

This commit should have no impact on MI output, expressions are
printed through the MI using -var-create and then -var-list-children.
As each use of -var-list-children only ever displays a single level of
an expression then the max-depth setting will have no impact.

This commit also adds the max-depth mechanism to the scripting
language pretty printers following basically the same rules as for the
built in value printing.

One quirk is that when printing a value using the display hint 'map',
if the keys of the map are structs then GDB will hide the keys one
depth level after it hides the values, this ensures that GDB produces
output like this:

  $1 = map_object = {[{key1}] = {...}, [{key2}] = {...}}

Instead of this less helpful output:

  $1 = map_object = {[{...}] = {...}, [{...}] = {...}}

This is covered by the new tests in gdb.python/py-nested-maps.exp.

gdb/ChangeLog:

	* cp-valprint.c (cp_print_value_fields): Allow an additional level
	of depth when printing anonymous structs or unions.
	* guile/scm-pretty-print.c (gdbscm_apply_val_pretty_printer):
	Don't print either the top-level value, or the children if the
	max-depth is exceeded.
	(ppscm_print_children): When printing the key of a map, allow one
	extra level of depth.
	* python/py-prettyprint.c (gdbpy_apply_val_pretty_printer): Don't
	print either the top-level value, or the children if the max-depth
	is exceeded.
	(print_children): When printing the key of a map, allow one extra
	level of depth.
	* python/py-value.c (valpy_format_string): Add max_depth keyword.
	* valprint.c: (PRINT_MAX_DEPTH_DEFAULT): Define.
	(user_print_options): Initialise max_depth field.
	(val_print_scalar_or_string_type_p): New function.
	(val_print): Check to see if the max depth has been reached.
	(val_print_check_max_depth): Define new function.
	(show_print_max_depth): New function.
	(_initialize_valprint): Add 'print max-depth' option.
	* valprint.h (struct value_print_options) <max_depth>: New field.
	(val_print_check_max_depth): Declare new function.
	* NEWS: Document new feature.

gdb/doc/ChangeLog:

	* gdb.texinfo (Print Settings): Document 'print max-depth'.
	* guile.texi (Guile Pretty Printing API): Document that 'print
	max-depth' can effect the display of a values children.
	* python.texi (Pretty Printing API): Likewise.
	(Values From Inferior): Document max_depth keyword.

gdb/testsuite/ChangeLog:

	* gdb.base/max-depth.c: New file.
	* gdb.base/max-depth.exp: New file.
	* gdb.python/py-nested-maps.c: New file.
	* gdb.python/py-nested-maps.exp: New file.
	* gdb.python/py-nested-maps.py: New file.
	* gdb.python/py-format-string.exp (test_max_depth): New proc.
	(test_all_common): Call test_max_depth.
	* gdb.fortran/max-depth.exp: New file.
	* gdb.fortran/max-depth.f90: New file.
	* gdb.go/max-depth.exp: New file.
	* gdb.go/max-depth.go: New file.
	* gdb.modula2/max-depth.exp: New file.
	* gdb.modula2/max-depth.c: New file.
	* lib/gdb.exp (get_print_expr_at_depths): New proc.
2019-04-29 22:01:09 +01:00

233 lines
3.0 KiB
C

/* This testcase is part of GDB, the GNU debugger.
Copyright 2019 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
struct s1
{
int x;
int y;
} s1;
struct s2
{
int x;
int y;
struct
{
int z;
int a;
};
} s2;
struct s3
{
int x;
int y;
struct
{
int z;
int a;
struct
{
int b;
int c;
};
};
} s3;
struct s4
{
int x;
int y;
struct
{
int x;
int y;
struct
{
int x;
int y;
} l2;
} l1;
} s4;
struct s5
{
union
{
int raw[3];
struct
{
int x;
int y;
int z;
};
};
} s5;
typedef struct
{
union
{
int raw[3];
struct
{
int x;
int y;
int z;
};
};
} s6_t;
s6_t s6;
struct s7
{
struct
{
int x;
int y;
};
struct
{
int z;
int a;
};
struct
{
int b;
int c;
};
} s7;
struct s8
{
int x;
int y;
struct
{
int z;
int a;
struct
{
int b;
int c;
};
} d1;
} s8;
struct s9
{
int x;
int y;
struct
{
int k;
int j;
struct
{
int z;
int a;
struct
{
int b;
int c;
};
} d1;
};
struct
{
int z;
int a;
struct
{
int b;
int c;
};
} d2;
} s9;
struct s10
{
int x[10];
int y;
struct
{
int k[10];
int j;
struct
{
int z;
int a;
struct
{
int b[10];
int c;
};
} d1;
};
struct
{
int z;
int a;
struct
{
int b[10];
int c;
};
} d2;
} s10;
struct s11
{
int x;
char s[10];
struct
{
int z;
int a;
};
} s11;
/* The following are C++ inheritance testing. */
#ifdef __cplusplus
/* This is non-virtual inheritance. */
struct C1 { int c1 = 1; } c1;
struct C2 { int c2 = 2; } c2;
struct C3 : C2 { int c3 = 3; } c3;
struct C4 { int c4 = 4; } c4;
struct C5 : C4 { int c5 = 5; } c5;
struct C6 : C5 { int c6 = 6; } c6;
struct C7 : C1, C3, C6 { int c7 = 7; } c7;
/* This is virtual inheritance. */
struct V1 { int v1 = 1; } v1;
struct V2 : virtual V1 { int v2 = 2; } v2;
struct V3 : virtual V1 { int v3 = 3; } v3;
struct V4 : virtual V2 { int v4 = 4; } v4;
struct V5 : virtual V2 { int v5 = 1; } v5;
struct V6 : virtual V2, virtual V3 { int v6 = 1; } v6;
struct V7 : virtual V4, virtual V5, virtual V6 { int v7 = 1; } v7;
#endif /* __cplusplus */
int
main ()
{
return 0;
}