public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] GDB: Add a character string limiting option
@ 2023-01-19 21:17 Maciej W. Rozycki
  0 siblings, 0 replies; only message in thread
From: Maciej W. Rozycki @ 2023-01-19 21:17 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=76b58849c5fc433f71ad7ec18f9f47f782643bc6

commit 76b58849c5fc433f71ad7ec18f9f47f782643bc6
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date:   Thu Jan 19 21:15:56 2023 +0000

    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>

Diff:
---
 gdb/NEWS                           | 14 ++++++
 gdb/ada-valprint.c                 |  6 ++-
 gdb/c-lang.c                       |  4 +-
 gdb/c-valprint.c                   |  5 ++-
 gdb/doc/gdb.texinfo                | 44 ++++++++++++++++---
 gdb/doc/python.texi                |  5 +++
 gdb/language.h                     |  2 +-
 gdb/m2-lang.c                      |  3 +-
 gdb/m2-valprint.c                  |  4 +-
 gdb/p-lang.c                       |  3 +-
 gdb/p-valprint.c                   |  9 ++--
 gdb/printcmd.c                     |  9 ++--
 gdb/python/py-value.c              |  4 +-
 gdb/testsuite/gdb.base/default.exp |  7 +++
 gdb/testsuite/gdb.base/options.exp |  1 +
 gdb/tracepoint.c                   |  4 +-
 gdb/valprint.c                     | 90 ++++++++++++++++++++++++++++----------
 gdb/valprint.h                     | 26 +++++++++--
 18 files changed, 189 insertions(+), 51 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index c0aac212e30..2bc1672632a 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -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
diff --git a/gdb/ada-valprint.c b/gdb/ada-valprint.c
index ef2581dfbc1..10d3b0a41bf 100644
--- a/gdb/ada-valprint.c
+++ b/gdb/ada-valprint.c
@@ -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);
diff --git a/gdb/c-lang.c b/gdb/c-lang.c
index 52010fa8cb1..ef2272e9e6b 100644
--- a/gdb/c-lang.c
+++ b/gdb/c-lang.c
@@ -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
diff --git a/gdb/c-valprint.c b/gdb/c-valprint.c
index ce759bc887f..69aa91d1ec3 100644
--- a/gdb/c-valprint.c
+++ b/gdb/c-valprint.c
@@ -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,
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 9c0018ea5c1..b5fad2cb16e 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -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}.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index d9a9a5f12b6..b04f1de2ddf 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -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
diff --git a/gdb/language.h b/gdb/language.h
index cf94923ecc1..a51ddf97381 100644
--- a/gdb/language.h
+++ b/gdb/language.h
@@ -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.  */
 
diff --git a/gdb/m2-lang.c b/gdb/m2-lang.c
index 00f067d3c88..fe02c45fba5 100644
--- a/gdb/m2-lang.c
+++ b/gdb/m2-lang.c
@@ -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.  */
diff --git a/gdb/m2-valprint.c b/gdb/m2-valprint.c
index 7c830151874..e4335c188aa 100644
--- a/gdb/m2-valprint.c
+++ b/gdb/m2-valprint.c
@@ -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;
 		}
diff --git a/gdb/p-lang.c b/gdb/p-lang.c
index bcb2427c417..2b4720df4ef 100644
--- a/gdb/p-lang.c
+++ b/gdb/p-lang.c
@@ -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.  */
diff --git a/gdb/p-valprint.c b/gdb/p-valprint.c
index 3456fe46b31..69bfeba1477 100644
--- a/gdb/p-valprint.c
+++ b/gdb/p-valprint.c
@@ -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;
 		  }
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index c3c2e5ad612..7f3551327a8 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -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;
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index dcc92e51b60..da2565081e6 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -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,
diff --git a/gdb/testsuite/gdb.base/default.exp b/gdb/testsuite/gdb.base/default.exp
index 4b1a3345f55..d0789a64401 100644
--- a/gdb/testsuite/gdb.base/default.exp
+++ b/gdb/testsuite/gdb.base/default.exp
@@ -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
diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/options.exp
index 5a2b6d871bf..6d90615710e 100644
--- a/gdb/testsuite/gdb.base/options.exp
+++ b/gdb/testsuite/gdb.base/options.exp
@@ -172,6 +172,7 @@ proc_with_prefix test-print {{prefix ""}} {
 	"-address"
 	"-array"
 	"-array-indexes"
+	"-characters"
 	"-elements"
 	"-max-depth"
 	"-memory-tag-violations"
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index f96b9b8a61a..7e172dfb3fc 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -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);
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 77d1f1348eb..04f83f5194a 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -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,17 +156,31 @@ 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,
+     (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 chars or array "
-		"elements to print is %s.\n"),
+	      _("Limit on string characters to print is %s.\n"),
 	      value);
 }
 
-
 /* Default input and output radixes, and output format letter.  */
 
 unsigned input_radix = 10;
@@ -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.  */
-
-  fetchlimit = (len == -1 ? options->print_max : std::min ((unsigned) len,
-							   options->print_max));
+     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.  */
+
+  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 {
diff --git a/gdb/valprint.h b/gdb/valprint.h
index 68382a161a6..cf5e2f210e4 100644
--- a/gdb/valprint.h
+++ b/gdb/valprint.h
@@ -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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-19 21:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-19 21:17 [binutils-gdb] GDB: Add a character string limiting option Maciej W. Rozycki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).