GDB: Add a character string limiting option

This commit splits the `set/show print elements' option into two.  We
retain `set/show print elements' for controlling how many elements of an
array we print, but a new `set/show print characters' setting is added
which is used for controlling how many characters of a string are
printed.

The motivation behind this change is to allow users a finer level of
control over how data is printed, reflecting that, although strings can
be thought of as arrays of characters, users often want to treat these
two things differently.

For compatibility reasons by default the `set/show print characters'
option is set to `elements', which makes the limit for character strings
follow the setting of the `set/show print elements' option, as it used
to.  Using `set print characters' with any other value makes the limit
independent from the `set/show print elements' setting, however it can
be restored to the default with the `set print characters elements'
command at any time.

A corresponding `-characters' option for the `print' command is added,
with the same semantics, i.e. one can use `elements' to make a given
`print' invocation follow the limit of elements, be it set with the
`-elements' option also given with the same invocation or taken from the
`set/show print elements' setting, for characters as well regardless of
the current setting of the `set/show print characters' option.

The GDB changes are all pretty straightforward, just changing references
to the old 'print_max' to use a new `get_print_max_chars' helper which
figures out which of the two of `print_max' and `print_max_chars' values
to use.

Likewise, the documentation is just updated to reference the new setting
where appropriate.

To make people's life easier the message shown by `show print elements'
now indicates if the setting also applies to character strings:

(gdb) set print characters elements
(gdb) show print elements
Limit on string chars or array elements to print is 200.
(gdb) set print characters unlimited
(gdb) show print elements
Limit on array elements to print is 200.
(gdb)

and the help text shows the dependency as well:

(gdb) help set print elements
Set limit on array elements to print.
"unlimited" causes there to be no limit.
This setting also applies to string chars when "print characters"
is set to "elements".
(gdb)

In the testsuite there are two minor updates, one to add `-characters'
to the list of completions now shown for the `print' command, and a bare
minimum pair of checks for the right handling of `set print characters'
and `show print characters', copied from the corresponding checks for
`set print elements' and `show print elements' respectively.

Co-Authored-By: Maciej W. Rozycki <macro@embecosm.com>
Approved-By: Simon Marchi <simon.marchi@efficios.com>
This commit is contained in:
Andrew Burgess
2023-01-19 21:15:56 +00:00
committed by Maciej W. Rozycki
parent 7aeb03e2d4
commit 76b58849c5
18 changed files with 189 additions and 51 deletions

View File

@ -174,6 +174,20 @@ set style tui-current-position [on|off]
Whether to style the source and assembly code highlighted by the
TUI's current position indicator. The default is off.
set print characters LIMIT
show print characters
This new setting is like 'set print elements', but controls how many
characters of a string are printed. This functionality used to be
covered by 'set print elements', but it can be controlled separately
now. LIMIT can be set to a numerical value to request that particular
character count, to 'unlimited' to print all characters of a string,
or to 'elements', which is also the default, to follow the setting of
'set print elements' as it used to be.
print -characters LIMIT
This new option to the 'print' command has the same effect as a temporary
use of 'set print characters'.
* Changed commands
document user-defined

View File

@ -469,7 +469,8 @@ printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
return;
}
for (i = 0; i < length && things_printed < options->print_max; i += 1)
unsigned int print_max_chars = get_print_max_chars (options);
for (i = 0; i < length && things_printed < print_max_chars; i += 1)
{
/* Position of the character we are examining
to see whether it is repeated. */
@ -705,12 +706,13 @@ ada_val_print_string (struct type *type, const gdb_byte *valaddr,
elements up to it. */
if (options->stop_print_at_null)
{
unsigned int print_max_chars = get_print_max_chars (options);
int temp_len;
/* Look for a NULL char. */
for (temp_len = 0;
(temp_len < len
&& temp_len < options->print_max
&& temp_len < print_max_chars
&& char_at (valaddr + offset_aligned,
temp_len, eltlen, byte_order) != 0);
temp_len += 1);

View File

@ -185,8 +185,8 @@ language_defn::printchar (int c, struct type *type,
/* Print the character string STRING, printing at most LENGTH
characters. LENGTH is -1 if the string is nul terminated. Each
character is WIDTH bytes long. Printing stops early if the number
hits print_max; repeat counts are printed as appropriate. Print
ellipses at the end if we had to stop before printing LENGTH
hits print_max_chars; repeat counts are printed as appropriate.
Print ellipses at the end if we had to stop before printing LENGTH
characters, or if FORCE_ELLIPSES. */
void

View File

@ -267,11 +267,12 @@ c_value_print_array (struct value *val,
print elements up to it. */
if (options->stop_print_at_null)
{
unsigned int print_max_chars = get_print_max_chars (options);
unsigned int temp_len;
for (temp_len = 0;
(temp_len < len
&& temp_len < options->print_max
&& temp_len < print_max_chars
&& extract_unsigned_integer (valaddr + temp_len * eltlen,
eltlen, byte_order) != 0);
++temp_len)
@ -280,7 +281,7 @@ c_value_print_array (struct value *val,
/* Force printstr to print ellipses if
we've printed the maximum characters and
the next character is not \000. */
if (temp_len == options->print_max && temp_len < len)
if (temp_len == print_max_chars && temp_len < len)
{
ULONGEST ival
= extract_unsigned_integer (valaddr + temp_len * eltlen,

View File

@ -10309,10 +10309,18 @@ Related setting: @ref{set print array}.
Set printing of array indexes.
Related setting: @ref{set print array-indexes}.
@item -elements @var{number-of-elements}|@code{unlimited}
Set limit on string chars or array elements to print. The value
@item -characters @var{number-of-characters}|@code{elements}|@code{unlimited}
Set limit on string characters to print. The value @code{elements}
causes the limit on array elements to print to be used. The value
@code{unlimited} causes there to be no limit. Related setting:
@ref{set print elements}.
@ref{set print characters}.
@item -elements @var{number-of-elements}|@code{unlimited}
Set limit on array elements and optionally string characters to print.
See @ref{set print characters}, and the @code{-characters} option above
for when this option applies to strings. The value @code{unlimited}
causes there to be no limit. @xref{set print elements}, for a related
CLI command.
@item -max-depth @var{depth}|@code{unlimited}
Set the threshold after which nested structures are replaced with
@ -11709,6 +11717,31 @@ Don't printing binary values in groups. This is the default.
@item show print nibbles
Show whether to print binary values in groups of four bits.
@anchor{set print characters}
@item set print characters @var{number-of-characters}
@itemx set print characters elements
@itemx set print characters unlimited
@cindex number of string characters to print
@cindex limit on number of printed string characters
Set a limit on how many characters of a string @value{GDBN} will print.
If @value{GDBN} is printing a large string, it stops printing after it
has printed the number of characters set by the @code{set print
characters} command. This equally applies to multi-byte and wide
character strings, that is for strings whose character type is
@code{wchar_t}, @code{char16_t}, or @code{char32_t} it is the number of
actual characters rather than underlying bytes the encoding uses that
this setting controls.
Setting @var{number-of-characters} to @code{elements} means that the
limit on the number of characters to print follows one for array
elements; see @ref{set print elements}.
Setting @var{number-of-characters} to @code{unlimited} means that the
number of characters to print is unlimited.
When @value{GDBN} starts, this limit is set to @code{elements}.
@item show print characters
Display the number of characters of a large string that @value{GDBN}
will print.
@anchor{set print elements}
@item set print elements @var{number-of-elements}
@itemx set print elements unlimited
@ -11717,7 +11750,8 @@ Show whether to print binary values in groups of four bits.
Set a limit on how many elements of an array @value{GDBN} will print.
If @value{GDBN} is printing a large array, it stops printing after it has
printed the number of elements set by the @code{set print elements} command.
This limit also applies to the display of strings.
By default this limit also applies to the display of strings; see
@ref{set print characters}.
When @value{GDBN} starts, this limit is set to 200.
Setting @var{number-of-elements} to @code{unlimited} or zero means
that the number of elements to print is unlimited.
@ -15233,7 +15267,7 @@ The optional @var{mods} changes the usual handling of the arguments.
@code{s} requests that pointers to chars be handled as strings, in
particular collecting the contents of the memory being pointed at, up
to the first zero. The upper bound is by default the value of the
@code{print elements} variable; if @code{s} is followed by a decimal
@code{print characters} variable; if @code{s} is followed by a decimal
number, that is the upper bound instead. So for instance
@samp{collect/s25 mystr} collects as many as 25 characters at
@samp{mystr}.

View File

@ -1135,6 +1135,11 @@ the @emph{declared} type should be used. (See @code{set print object} in
representation of a C@t{++} object, @code{False} if they shouldn't (see
@code{set print static-members} in @ref{Print Settings}).
@item max_characters
Number of string characters to print, @code{0} to follow
@code{max_elements}, or @code{UINT_MAX} to print an unlimited number
of characters (see @code{set print characters} in @ref{Print Settings}).
@item max_elements
Number of array elements to print, or @code{0} to print an unlimited
number of elements (see @code{set print elements} in @ref{Print

View File

@ -548,7 +548,7 @@ struct language_defn
struct ui_file * stream) const;
/* Print the character string STRING, printing at most LENGTH characters.
Printing stops early if the number hits print_max; repeat counts
Printing stops early if the number hits print_max_chars; repeat counts
are printed as appropriate. Print ellipses at the end if we
had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */

View File

@ -169,7 +169,8 @@ m2_language::printstr (struct ui_file *stream, struct type *elttype,
return;
}
for (i = 0; i < length && things_printed < options->print_max; ++i)
unsigned int print_max_chars = get_print_max_chars (options);
for (i = 0; i < length && things_printed < print_max_chars; ++i)
{
/* Position of the character we are examining
to see whether it is repeated. */

View File

@ -327,12 +327,14 @@ m2_language::value_print_inner (struct value *val, struct ui_file *stream,
elements up to it. */
if (options->stop_print_at_null)
{
unsigned int print_max_chars = get_print_max_chars (options);
unsigned int temp_len;
/* Look for a NULL char. */
for (temp_len = 0;
(valaddr[temp_len]
&& temp_len < len && temp_len < options->print_max);
&& temp_len < len
&& temp_len < print_max_chars);
temp_len++);
len = temp_len;
}

View File

@ -253,7 +253,8 @@ pascal_language::printstr (struct ui_file *stream, struct type *elttype,
return;
}
for (i = 0; i < length && things_printed < options->print_max; ++i)
unsigned int print_max_chars = get_print_max_chars (options);
for (i = 0; i < length && things_printed < print_max_chars; ++i)
{
/* Position of the character we are examining
to see whether it is repeated. */

View File

@ -105,13 +105,16 @@ pascal_language::value_print_inner (struct value *val,
elements up to it. */
if (options->stop_print_at_null)
{
unsigned int print_max_chars
= get_print_max_chars (options);
unsigned int temp_len;
/* Look for a NULL char. */
for (temp_len = 0;
extract_unsigned_integer (valaddr + temp_len * eltlen,
eltlen, byte_order)
&& temp_len < len && temp_len < options->print_max;
(extract_unsigned_integer
(valaddr + temp_len * eltlen, eltlen, byte_order)
&& temp_len < len
&& temp_len < print_max_chars);
temp_len++);
len = temp_len;
}

View File

@ -957,17 +957,18 @@ find_string_backward (struct gdbarch *gdbarch,
chars_to_read * char_size);
chars_read /= char_size;
read_error = (chars_read == chars_to_read) ? 0 : 1;
unsigned int print_max_chars = get_print_max_chars (options);
/* Searching for '\0' from the end of buffer in backward direction. */
for (i = 0; i < chars_read && count > 0 ; ++i, ++chars_counted)
{
int offset = (chars_to_read - i - 1) * char_size;
if (integer_is_zero (&buffer[offset], char_size)
|| chars_counted == options->print_max)
|| chars_counted == print_max_chars)
{
/* Found '\0' or reached print_max. As OFFSET is the offset to
'\0', we add CHAR_SIZE to return the start address of
a string. */
/* Found '\0' or reached `print_max_chars'. As OFFSET
is the offset to '\0', we add CHAR_SIZE to return
the start address of a string. */
--count;
string_start_addr = addr + offset + char_size;
chars_counted = 0;

View File

@ -647,6 +647,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
"actual_objects", /* See set print object on|off. */
"static_members", /* See set print static-members on|off. */
/* C non-bool options. */
"max_characters", /* See set print characters N. */
"max_elements", /* See set print elements N. */
"max_depth", /* See set print max-depth N. */
"repeat_threshold", /* See set print repeats. */
@ -695,7 +696,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
char *format = NULL;
if (!gdb_PyArg_ParseTupleAndKeywords (args,
kw,
"|O!O!O!O!O!O!O!O!O!O!O!O!O!IIIs",
"|O!O!O!O!O!O!O!O!O!O!O!O!O!IIIIs",
keywords,
&PyBool_Type, &raw_obj,
&PyBool_Type, &pretty_arrays_obj,
@ -710,6 +711,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
&PyBool_Type, &deref_refs_obj,
&PyBool_Type, &actual_objects_obj,
&PyBool_Type, &static_members_obj,
&opts.print_max_chars,
&opts.print_max,
&opts.max_depth,
&opts.repeat_count_threshold,

View File

@ -520,6 +520,10 @@ gdb_test_no_output "set print address" "set print address"
gdb_test_no_output "set print array" "set print array"
#test set print asm-demangle
gdb_test_no_output "set print asm-demangle" "set print asm-demangle"
#test set print characters
gdb_test "set print characters" \
"Argument required \\(integer to set it to, or one of:\
\"elements\", \"unlimited\"\\)\\."
#test set print demangle
gdb_test_no_output "set print demangle" "set print demangle"
#test set print elements
@ -664,6 +668,9 @@ gdb_test "show print address" "Printing of addresses is on."
gdb_test "show print array" "Pretty formatting of arrays is on."
#test show print asm-demangle
gdb_test "show print asm-demangle" "Demangling of C\[+\]+/ObjC names in disassembly listings is on."
#test show print characters
gdb_test "show print characters" \
"Limit on string characters to print is elements\\."
#test show print demangle
gdb_test "show print demangle" "Demangling of encoded C\[+\]+/ObjC names when displaying symbols is on."
#test show print elements

View File

@ -172,6 +172,7 @@ proc_with_prefix test-print {{prefix ""}} {
"-address"
"-array"
"-array-indexes"
"-characters"
"-elements"
"-max-depth"
"-memory-tag-violations"

View File

@ -541,9 +541,9 @@ decode_agent_options (const char *exp, int *trace_string)
if (target_supports_string_tracing ())
{
/* Allow an optional decimal number giving an explicit maximum
string length, defaulting it to the "print elements" value;
string length, defaulting it to the "print characters" value;
so "collect/s80 mystr" gets at most 80 bytes of string. */
*trace_string = opts.print_max;
*trace_string = get_print_max_chars (&opts);
exp++;
if (*exp >= '0' && *exp <= '9')
*trace_string = atoi (exp);

View File

@ -94,8 +94,14 @@ static void val_print_type_code_flags (struct type *type,
int embedded_offset,
struct ui_file *stream);
#define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
#define PRINT_MAX_DEPTH_DEFAULT 20 /* Start print_max_depth off at this value. */
/* Start print_max at this value. */
#define PRINT_MAX_DEFAULT 200
/* Start print_max_chars at this value (meaning follow print_max). */
#define PRINT_MAX_CHARS_DEFAULT PRINT_MAX_CHARS_ELEMENTS
/* Start print_max_depth at this value. */
#define PRINT_MAX_DEPTH_DEFAULT 20
struct value_print_options user_print_options =
{
@ -108,6 +114,7 @@ struct value_print_options user_print_options =
false, /* nibblesprint */
false, /* objectprint */
PRINT_MAX_DEFAULT, /* print_max */
PRINT_MAX_CHARS_DEFAULT, /* print_max_chars */
10, /* repeat_count_threshold */
0, /* output_format */
0, /* format */
@ -149,16 +156,30 @@ get_formatted_print_options (struct value_print_options *opts,
opts->format = format;
}
/* Implement 'show print elements'. */
static void
show_print_max (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
gdb_printf (file,
_("Limit on string chars or array "
"elements to print is %s.\n"),
value);
gdb_printf
(file,
(user_print_options.print_max_chars != PRINT_MAX_CHARS_ELEMENTS
? _("Limit on array elements to print is %s.\n")
: _("Limit on string chars or array elements to print is %s.\n")),
value);
}
/* Implement 'show print characters'. */
static void
show_print_max_chars (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
gdb_printf (file,
_("Limit on string characters to print is %s.\n"),
value);
}
/* Default input and output radixes, and output format letter. */
@ -2481,9 +2502,9 @@ print_converted_chars_to_obstack (struct obstack *obstack,
/* Print the character string STRING, printing at most LENGTH
characters. LENGTH is -1 if the string is nul terminated. TYPE is
the type of each character. OPTIONS holds the printing options;
printing stops early if the number hits print_max; repeat counts
are printed as appropriate. Print ellipses at the end if we had to
stop before printing LENGTH characters, or if FORCE_ELLIPSES.
printing stops early if the number hits print_max_chars; repeat
counts are printed as appropriate. Print ellipses at the end if we
had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
QUOTE_CHAR is the character to print at each end of the string. If
C_STYLE_TERMINATOR is true, and the last character is 0, then it is
omitted. */
@ -2537,7 +2558,8 @@ generic_printstr (struct ui_file *stream, struct type *type,
/* Convert characters until the string is over or the maximum
number of printed characters has been reached. */
i = 0;
while (i < options->print_max)
unsigned int print_max_chars = get_print_max_chars (options);
while (i < print_max_chars)
{
int r;
@ -2589,7 +2611,7 @@ generic_printstr (struct ui_file *stream, struct type *type,
/* Print a string from the inferior, starting at ADDR and printing up to LEN
characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
stops at the first null byte, otherwise printing proceeds (including null
bytes) until either print_max or LEN characters have been printed,
bytes) until either print_max_chars or LEN characters have been printed,
whichever is smaller. ENCODING is the name of the string's
encoding. It can be NULL, in which case the target encoding is
assumed. */
@ -2611,15 +2633,17 @@ val_print_string (struct type *elttype, const char *encoding,
int width = elttype->length ();
/* First we need to figure out the limit on the number of characters we are
going to attempt to fetch and print. This is actually pretty simple. If
LEN >= zero, then the limit is the minimum of LEN and print_max. If
LEN is -1, then the limit is print_max. This is true regardless of
whether print_max is zero, UINT_MAX (unlimited), or something in between,
because finding the null byte (or available memory) is what actually
limits the fetch. */
going to attempt to fetch and print. This is actually pretty simple.
If LEN >= zero, then the limit is the minimum of LEN and print_max_chars.
If LEN is -1, then the limit is print_max_chars. This is true regardless
of whether print_max_chars is zero, UINT_MAX (unlimited), or something in
between, because finding the null byte (or available memory) is what
actually limits the fetch. */
fetchlimit = (len == -1 ? options->print_max : std::min ((unsigned) len,
options->print_max));
unsigned int print_max_chars = get_print_max_chars (options);
fetchlimit = (len == -1
? print_max_chars
: std::min ((unsigned) len, print_max_chars));
err = target_read_string (addr, len, width, fetchlimit,
&buffer, &bytes_read);
@ -2864,6 +2888,15 @@ using uinteger_option_def
using pinteger_option_def
= gdb::option::pinteger_option_def<value_print_options>;
/* Extra literals supported with the `set print characters' and
`print -characters' commands. */
static const literal_def print_characters_literals[] =
{
{ "elements", PRINT_MAX_CHARS_ELEMENTS },
{ "unlimited", PRINT_MAX_CHARS_UNLIMITED, 0 },
{ nullptr }
};
/* Definitions of options for the "print" and "compile print"
commands. */
static const gdb::option::option_def value_print_option_defs[] = {
@ -2904,14 +2937,27 @@ static const gdb::option::option_def value_print_option_defs[] = {
NULL, /* help_doc */
},
uinteger_option_def {
"characters",
[] (value_print_options *opt) { return &opt->print_max_chars; },
print_characters_literals,
show_print_max_chars, /* show_cmd_cb */
N_("Set limit on string chars to print."),
N_("Show limit on string chars to print."),
N_("\"elements\" causes the array element limit to be used.\n"
"\"unlimited\" causes there to be no limit."),
},
uinteger_option_def {
"elements",
[] (value_print_options *opt) { return &opt->print_max; },
uinteger_unlimited_literals,
show_print_max, /* show_cmd_cb */
N_("Set limit on string chars or array elements to print."),
N_("Show limit on string chars or array elements to print."),
N_("\"unlimited\" causes there to be no limit."),
N_("Set limit on array elements to print."),
N_("Show limit on array elements to print."),
N_("\"unlimited\" causes there to be no limit.\n"
"This setting also applies to string chars when \"print characters\"\n"
"is set to \"elements\"."),
},
pinteger_option_def {

View File

@ -51,12 +51,15 @@ struct value_print_options
in its vtables. */
bool objectprint;
/* Maximum number of chars to print for a string pointer value or vector
contents, or UINT_MAX for no limit. Note that "set print elements 0"
stores UINT_MAX in print_max, which displays in a show command as
"unlimited". */
/* Maximum number of elements to print for vector contents, or UINT_MAX
for no limit. Note that "set print elements 0" stores UINT_MAX in
print_max, which displays in a show command as "unlimited". */
unsigned int print_max;
/* Maximum number of string chars to print for a string pointer value,
zero if to follow the value of print_max, or UINT_MAX for no limit. */
unsigned int print_max_chars;
/* Print repeat counts if there are more than this many repetitions
of an element in an array. */
unsigned int repeat_count_threshold;
@ -105,6 +108,21 @@ struct value_print_options
int max_depth;
};
/* The value to use for `print_max_chars' to follow `print_max'. */
#define PRINT_MAX_CHARS_ELEMENTS 0
/* The value to use for `print_max_chars' for no limit. */
#define PRINT_MAX_CHARS_UNLIMITED UINT_MAX
/* Return the character count limit for printing strings. */
static inline unsigned int
get_print_max_chars (const struct value_print_options *options)
{
return (options->print_max_chars != PRINT_MAX_CHARS_ELEMENTS
? options->print_max_chars : options->print_max);
}
/* Create an option_def_group for the value_print options, with OPTS
as context. */
extern gdb::option::option_def_group make_value_print_options_def_group