public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/2] More introspection for Value.format_string
@ 2022-06-07 15:42 Tom Tromey
  2022-06-07 15:42 ` [PATCH 1/2] Expose current 'print' settings to Python Tom Tromey
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Tom Tromey @ 2022-06-07 15:42 UTC (permalink / raw)
  To: gdb-patches

This series addresses a longstanding wish to be able to access the
value_print_options from pretty-printers.

First, it exposes this information via a new API.

Second, when a 'print' is in effect, it arranges for the local print
options to be stored and then applied by Value.format_string.  This
makes it so that format_string respect the current settings by
default.

Finally, this series exposes the 'summary' field from
value_print_options.

Regression tested on x86-64 Fedora 34.

Tom



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] Expose current 'print' settings to Python
  2022-06-07 15:42 [PATCH 0/2] More introspection for Value.format_string Tom Tromey
@ 2022-06-07 15:42 ` Tom Tromey
  2022-06-07 16:12   ` Eli Zaretskii
  2022-06-07 15:42 ` [PATCH 2/2] Add 'summary' mode to Value.format_string Tom Tromey
  2022-07-15 15:24 ` [PATCH 0/2] More introspection for Value.format_string Tom Tromey
  2 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2022-06-07 15:42 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

PR python/17291 asks for access to the current print options.  While I
think this need is largely satisfied by the existence of
Value.format_string, it seemed to me that a bit more could be done.

First, while Value.format_string uses the user's settings, it does not
react to temporary settings such as "print/x".  This patch changes
this.

Second, there is no good way to examine the current settings (in
particular the temporary ones in effect for just a single "print").
This patch adds this as well.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17291
---
 gdb/NEWS                                      |  7 ++
 gdb/doc/python.texi                           | 18 ++++
 gdb/python/py-prettyprint.c                   | 98 ++++++++++++++++++-
 gdb/python/py-value.c                         |  4 +-
 gdb/python/py-varobj.c                        | 25 ++++-
 gdb/python/python-internal.h                  | 13 ++-
 gdb/python/python.c                           |  4 +
 gdb/testsuite/gdb.python/py-format-string.exp | 34 +++++--
 gdb/testsuite/gdb.python/py-format-string.py  |  4 +
 gdb/varobj.c                                  | 13 ++-
 10 files changed, 199 insertions(+), 21 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index a85f59f56df..6b32afed82c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -63,6 +63,13 @@ maintenance info line-table
   ** New method gdb.Frame.language that returns the name of the
      frame's language.
 
+  ** New function gdb.print_options that returns a dictionary of the
+     prevailing print options, in the form accepted by
+     gdb.Value.format_string.
+
+  ** gdb.Value.format_string now uses the format provided by 'print',
+     if it is called during a 'print' or other similar operation.
+
 *** Changes in GDB 12
 
 * DBX mode is deprecated, and will be removed in GDB 13
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index ba5a9b315e1..b9b65369db3 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -1735,6 +1735,24 @@ pretty-printer for this value exists, then it is returned.  If no such
 printer exists, then this returns @code{None}.
 @end defun
 
+Normally, a pretty-printer can respect the user's print settings
+(including temporarily applied settings, such as @samp{/x}) simply by
+calling @code{Value.format_string} (@pxref{Values From Inferior}).
+However, these settings can also be queried directly:
+
+@findex gdb.print_options
+@defun gdb.print_options ()
+Return a dictionary whose keys are the valid keywords that can be
+given to @code{Value.format_string}, and whose values are the user's
+settings.  During a @code{print} or other operation, the values will
+reflect any flags that are temporarily in effect.
+
+@smallexample
+(gdb) python print (gdb.print_options ()['max_elements'])
+200
+@end smallexample
+@end defun
+
 @node Selecting Pretty-Printers
 @subsubsection Selecting Pretty-Printers
 @cindex selecting python pretty-printers
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index a25a1b65944..4ef45b283f9 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -39,6 +39,10 @@ enum gdbpy_string_repr_result
     string_repr_ok
   };
 
+/* If non-null, points to options that are in effect while
+   printing.  */
+const struct value_print_options *gdbpy_current_print_options;
+
 /* Helper function for find_pretty_printer which iterates over a list,
    calls each function and inspects output.  This will return a
    printer object if one recognizes VALUE.  If no printer is found, it
@@ -604,6 +608,9 @@ gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
   if (printer == Py_None)
     return EXT_LANG_RC_NOP;
 
+  scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
+						    options);
+
   /* If we are printing a map, we want some special formatting.  */
   gdb::unique_xmalloc_ptr<char> hint (gdbpy_get_display_hint (printer.get ()));
 
@@ -632,8 +639,12 @@ gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
 gdbpy_ref<>
 apply_varobj_pretty_printer (PyObject *printer_obj,
 			     struct value **replacement,
-			     struct ui_file *stream)
+			     struct ui_file *stream,
+			     const value_print_options *opts)
 {
+  scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
+						    opts);
+
   *replacement = NULL;
   gdbpy_ref<> py_str = pretty_print_one_value (printer_obj, replacement);
 
@@ -688,3 +699,88 @@ gdbpy_default_visualizer (PyObject *self, PyObject *args)
 
   return find_pretty_printer (val_obj).release ();
 }
+
+/* Helper function to set a boolean in a dictionary.  */
+static int
+set_boolean (PyObject *dict, const char *name, bool val)
+{
+  gdbpy_ref<> val_obj (PyBool_FromLong (val));
+  if (val_obj == nullptr)
+    return -1;
+  return PyDict_SetItemString (dict, name, val_obj.get ());
+}
+
+/* Helper function to set an integer in a dictionary.  */
+static int
+set_unsigned (PyObject *dict, const char *name, unsigned int val)
+{
+  gdbpy_ref<> val_obj = gdb_py_object_from_ulongest (val);
+  if (val_obj == nullptr)
+    return -1;
+  return PyDict_SetItemString (dict, name, val_obj.get ());
+}
+
+/* Implement gdb.print_options.  */
+PyObject *
+gdbpy_print_options (PyObject *unused1, PyObject *unused2)
+{
+  gdbpy_ref<> result (PyDict_New ());
+  if (result == nullptr)
+    return nullptr;
+
+  value_print_options opts;
+  gdbpy_get_print_options (&opts);
+
+  if (set_boolean (result.get (), "raw",
+		   opts.raw) < 0
+      || set_boolean (result.get (), "pretty_arrays",
+		      opts.prettyformat_arrays) < 0
+      || set_boolean (result.get (), "pretty_structs",
+		      opts.prettyformat_structs) < 0
+      || set_boolean (result.get (), "array_indexes",
+		      opts.print_array_indexes) < 0
+      || set_boolean (result.get (), "symbols",
+		      opts.symbol_print) < 0
+      || set_boolean (result.get (), "unions",
+		      opts.unionprint) < 0
+      || set_boolean (result.get (), "address",
+		      opts.addressprint) < 0
+      || set_boolean (result.get (), "deref_refs",
+		      opts.deref_ref) < 0
+      || set_boolean (result.get (), "actual_objects",
+		      opts.objectprint) < 0
+      || set_boolean (result.get (), "static_members",
+		      opts.static_field_print) < 0
+      || set_boolean (result.get (), "deref_refs",
+		      opts.deref_ref) < 0
+      || set_unsigned (result.get (), "max_elements",
+		       opts.print_max) < 0
+      || set_unsigned (result.get (), "max_depth",
+		       opts.max_depth) < 0
+      || set_unsigned (result.get (), "repeat_threshold",
+		       opts.repeat_count_threshold) < 0)
+    return nullptr;
+
+  if (opts.format != 0)
+    {
+      char str[2] = { (char) opts.format, 0 };
+      gdbpy_ref<> fmtstr = host_string_to_python_string (str);
+      if (fmtstr == nullptr)
+	return nullptr;
+      if (PyDict_SetItemString (result.get (), "format", fmtstr.get ()) < 0)
+	return nullptr;
+    }
+
+  return result.release ();
+}
+
+/* Helper function that either finds the prevailing print options, or
+   calls get_user_print_options.  */
+void
+gdbpy_get_print_options (value_print_options *opts)
+{
+  if (gdbpy_current_print_options != nullptr)
+    *opts = *gdbpy_current_print_options;
+  else
+    get_user_print_options (opts);
+}
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 0f7003363cc..48acfc8d173 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -672,7 +672,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
     }
 
   struct value_print_options opts;
-  get_user_print_options (&opts);
+  gdbpy_get_print_options (&opts);
   opts.deref_ref = 0;
 
   /* We need objects for booleans as the "p" flag for bools is new in
@@ -1158,7 +1158,7 @@ valpy_str (PyObject *self)
 {
   struct value_print_options opts;
 
-  get_user_print_options (&opts);
+  gdbpy_get_print_options (&opts);
   opts.deref_ref = 0;
 
   string_file stb;
diff --git a/gdb/python/py-varobj.c b/gdb/python/py-varobj.c
index 372c91125d9..9e4fb6c58bb 100644
--- a/gdb/python/py-varobj.c
+++ b/gdb/python/py-varobj.c
@@ -17,13 +17,15 @@
 #include "python-internal.h"
 #include "varobj.h"
 #include "varobj-iter.h"
+#include "valprint.h"
 
 /* A dynamic varobj iterator "class" for python pretty-printed
    varobjs.  This inherits struct varobj_iter.  */
 
 struct py_varobj_iter : public varobj_iter
 {
-  py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter);
+  py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter,
+		  const value_print_options *opts);
   ~py_varobj_iter () override;
 
   std::unique_ptr<varobj_item> next () override;
@@ -41,6 +43,9 @@ struct py_varobj_iter : public varobj_iter
   /* The python iterator returned by the printer's 'children' method,
      or NULL if not available.  */
   PyObject *m_iter;
+
+  /* The print options to use.  */
+  value_print_options m_opts;
 };
 
 /* Implementation of the 'dtor' method of pretty-printed varobj
@@ -67,6 +72,9 @@ py_varobj_iter::next ()
 
   gdbpy_enter_varobj enter_py (m_var);
 
+  scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
+						    &m_opts);
+
   gdbpy_ref<> item (PyIter_Next (m_iter));
 
   if (item == NULL)
@@ -124,9 +132,11 @@ py_varobj_iter::next ()
    whose children the iterator will be iterating over.  PYITER is the
    python iterator actually responsible for the iteration.  */
 
-py_varobj_iter::py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter)
+py_varobj_iter::py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter,
+				const value_print_options *opts)
   : m_var (var),
-    m_iter (pyiter.release ())
+    m_iter (pyiter.release ()),
+    m_opts (*opts)
 {
 }
 
@@ -134,13 +144,17 @@ py_varobj_iter::py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter)
    over VAR's children.  */
 
 std::unique_ptr<varobj_iter>
-py_varobj_get_iterator (struct varobj *var, PyObject *printer)
+py_varobj_get_iterator (struct varobj *var, PyObject *printer,
+			const value_print_options *opts)
 {
   gdbpy_enter_varobj enter_py (var);
 
   if (!PyObject_HasAttr (printer, gdbpy_children_cst))
     return NULL;
 
+  scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
+						    opts);
+
   gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
 						    NULL));
   if (children == NULL)
@@ -157,5 +171,6 @@ py_varobj_get_iterator (struct varobj *var, PyObject *printer)
     }
 
   return std::unique_ptr<varobj_iter> (new py_varobj_iter (var,
-							   std::move (iter)));
+							   std::move (iter),
+							   opts));
 }
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 217bc15bb28..9ac9e35ce03 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -740,11 +740,16 @@ int gdbpy_is_value_object (PyObject *obj);
    other pretty-printer functions, because they refer to PyObject.  */
 gdbpy_ref<> apply_varobj_pretty_printer (PyObject *print_obj,
 					 struct value **replacement,
-					 struct ui_file *stream);
+					 struct ui_file *stream,
+					 const value_print_options *opts);
 gdbpy_ref<> gdbpy_get_varobj_pretty_printer (struct value *value);
 gdb::unique_xmalloc_ptr<char> gdbpy_get_display_hint (PyObject *printer);
 PyObject *gdbpy_default_visualizer (PyObject *self, PyObject *args);
 
+PyObject *gdbpy_print_options (PyObject *self, PyObject *args);
+void gdbpy_get_print_options (value_print_options *opts);
+extern const struct value_print_options *gdbpy_current_print_options;
+
 void bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj);
 void bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj);
 
@@ -778,8 +783,10 @@ int gdb_pymodule_addobject (PyObject *module, const char *name,
 
 struct varobj_iter;
 struct varobj;
-std::unique_ptr<varobj_iter> py_varobj_get_iterator (struct varobj *var,
-						     PyObject *printer);
+std::unique_ptr<varobj_iter> py_varobj_get_iterator
+     (struct varobj *var,
+      PyObject *printer,
+      const value_print_options *opts);
 
 /* Deleter for Py_buffer unique_ptr specialization.  */
 
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 9bef2252e88..baec8640556 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -2546,6 +2546,10 @@ the returned string is 'ADDRESS <SYMBOL+OFFSET>' without the quotes." },
     "current_language () -> string\n\
 Return the name of the currently selected language." },
 
+  { "print_options", gdbpy_print_options, METH_NOARGS,
+    "print_options () -> dict\n\
+Return the current print options." },
+
   {NULL, NULL, 0, NULL}
 };
 
diff --git a/gdb/testsuite/gdb.python/py-format-string.exp b/gdb/testsuite/gdb.python/py-format-string.exp
index 63b87e73476..0b7cdc18f5f 100644
--- a/gdb/testsuite/gdb.python/py-format-string.exp
+++ b/gdb/testsuite/gdb.python/py-format-string.exp
@@ -870,9 +870,11 @@ proc_with_prefix test_format {} {
       "0x2a" \
       "42 with option ${opts}"
 
-    check_format_string "a_point_t" $opts
+    check_format_string "a_point_t" $opts \
+      "Pretty Point \\(0x2a, 0xc\\)"
     check_format_string "a_point_t_pointer" $opts
-    check_format_string "another_point" $opts
+    check_format_string "another_point" $opts \
+      "Pretty Point \\(0x7b, 0x1c8\\)"
     check_format_string "a_struct_with_union" $opts \
       "\\{the_union = \\{an_int = 0x2a2a2a2a, a_char = 0x2a\\}\\}"
     check_format_string "an_enum" $opts \
@@ -893,7 +895,8 @@ proc_with_prefix test_format {} {
       $default_pointer_regexp
 
     if { $current_lang == "c++" } {
-      check_format_string "a_point_t_ref" $opts
+      check_format_string "a_point_t_ref" $opts \
+	"Pretty Point \\(0x2a, 0xc\\)"
       check_format_string "a_base_ref" $opts
     }
   }
@@ -906,10 +909,12 @@ proc_with_prefix test_format {} {
       "101010" \
       "42 with option ${opts}"
 
-    check_format_string "a_point_t" $opts
+    check_format_string "a_point_t" $opts \
+      "Pretty Point \\(101010, 1100\\)"
     check_format_string "a_point_t_pointer" $opts \
       $binary_pointer_regexp
-    check_format_string "another_point" $opts
+    check_format_string "another_point" $opts \
+      "Pretty Point \\(1111011, 111001000\\)"
     check_format_string "a_struct_with_union" $opts \
       "\\{the_union = \\{an_int = 101010001010100010101000101010, a_char = 101010\\}\\}"
     check_format_string "an_enum" $opts \
@@ -930,7 +935,8 @@ proc_with_prefix test_format {} {
       $binary_pointer_regexp
 
     if { $current_lang == "c++" } {
-      check_format_string "a_point_t_ref" $opts
+      check_format_string "a_point_t_ref" $opts \
+	"Pretty Point \\(101010, 1100\\)"
       check_format_string "a_base_ref" $opts
     }
   }
@@ -1035,6 +1041,21 @@ proc test_styling {} {
 	"{[style x variable] = 42, [style y variable] = 12}"
 }
 
+# Test the gdb.print_options API.
+proc test_print_options {} {
+    gdb_test_no_output "set print elements 500"
+    gdb_test "python print(gdb.print_options()\['max_elements'\])" "500" \
+	"examine max elements"
+    gdb_test "python print('format' in gdb.print_options())" "False" \
+	"examine format"
+
+    check_format_string "a_point_t" "format='t'" \
+	"Pretty Point \\(101010, 1100\\)" \
+	"print in binary to fetch options"
+    gdb_test "python print(saved_options\['format'\] == 't')" "True" \
+	"format was set"
+}
+
 # Run all the tests in common for both C and C++.
 proc_with_prefix test_all_common {} {
   # No options.
@@ -1058,6 +1079,7 @@ proc_with_prefix test_all_common {} {
   test_mixed
   # Various error conditions.
   test_invalid_args
+  test_print_options
 }
 
 # The current language ("c" or "c++" while running tests).
diff --git a/gdb/testsuite/gdb.python/py-format-string.py b/gdb/testsuite/gdb.python/py-format-string.py
index 4a1743a0f45..aa7b10445cd 100644
--- a/gdb/testsuite/gdb.python/py-format-string.py
+++ b/gdb/testsuite/gdb.python/py-format-string.py
@@ -18,12 +18,16 @@
 
 import gdb
 
+saved_options = {}
+
 
 class PointPrinter(object):
     def __init__(self, val):
         self.val = val
 
     def to_string(self):
+        global saved_options
+        saved_options = gdb.print_options()
         return "Pretty Point (%s, %s)" % (self.val["x"], self.val["y"])
 
 
diff --git a/gdb/varobj.c b/gdb/varobj.c
index 1aca015a21a..9a6d579ddf0 100644
--- a/gdb/varobj.c
+++ b/gdb/varobj.c
@@ -658,7 +658,11 @@ varobj_get_iterator (struct varobj *var)
 {
 #if HAVE_PYTHON
   if (var->dynamic->pretty_printer)
-    return py_varobj_get_iterator (var, var->dynamic->pretty_printer);
+    {
+      value_print_options opts;
+      varobj_formatted_print_options (&opts, var->format);
+      return py_varobj_get_iterator (var, var->dynamic->pretty_printer, &opts);
+    }
 #endif
 
   gdb_assert_not_reached ("requested an iterator from a non-dynamic varobj");
@@ -2146,6 +2150,8 @@ varobj_value_get_print_value (struct value *value,
   string_file stb;
   std::string thevalue;
 
+  varobj_formatted_print_options (&opts, format);
+
 #if HAVE_PYTHON
   if (gdb_python_initialized)
     {
@@ -2166,7 +2172,8 @@ varobj_value_get_print_value (struct value *value,
 
 	      gdbpy_ref<> output = apply_varobj_pretty_printer (value_formatter,
 								&replacement,
-								&stb);
+								&stb,
+								&opts);
 
 	      /* If we have string like output ...  */
 	      if (output != NULL)
@@ -2225,8 +2232,6 @@ varobj_value_get_print_value (struct value *value,
     }
 #endif
 
-  varobj_formatted_print_options (&opts, format);
-
   /* If the THEVALUE has contents, it is a regular string.  */
   if (!thevalue.empty ())
     current_language->printstr (&stb, type, (gdb_byte *) thevalue.c_str (),
-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/2] Add 'summary' mode to Value.format_string
  2022-06-07 15:42 [PATCH 0/2] More introspection for Value.format_string Tom Tromey
  2022-06-07 15:42 ` [PATCH 1/2] Expose current 'print' settings to Python Tom Tromey
@ 2022-06-07 15:42 ` Tom Tromey
  2022-06-07 16:10   ` Eli Zaretskii
  2022-07-15 15:24 ` [PATCH 0/2] More introspection for Value.format_string Tom Tromey
  2 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2022-06-07 15:42 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This adds a 'summary' mode to Value.format_string and to
gdb.print_options.  For the former, it lets Python code format values
using this mode.  For the latter, it lets a printer potentially detect
if it is being called in a backtrace with 'set print frame-arguments'
set to 'scalars'.

I considered adding a new mode here to let a pretty-printer see
whether it was being called in a 'backtrace' context at all, but I'm
not sure if this is really desirable.
---
 gdb/NEWS                                      | 2 ++
 gdb/doc/python.texi                           | 6 ++++++
 gdb/python/py-prettyprint.c                   | 2 ++
 gdb/python/py-value.c                         | 7 ++++++-
 gdb/testsuite/gdb.python/py-format-string.exp | 6 ++++++
 gdb/testsuite/gdb.python/py-format-string.py  | 2 ++
 6 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 6b32afed82c..114818f0ae4 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -70,6 +70,8 @@ maintenance info line-table
   ** gdb.Value.format_string now uses the format provided by 'print',
      if it is called during a 'print' or other similar operation.
 
+  ** gdb.Value.format_string now accepts the 'summary' keyword.
+
 *** Changes in GDB 12
 
 * DBX mode is deprecated, and will be removed in GDB 13
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index b9b65369db3..bf57a378a60 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -1153,6 +1153,12 @@ Additionally, @value{GDBN} only styles some value contents, so not
 every output string will contain escape sequences.
 
 When @code{False}, which is the default, no output styling is applied.
+
+@item summary
+@code{True} when just a summary should be printed.  In this mode,
+scalar values are printed in their entirety, but aggregates such as
+structures or unions are omitted.  This mode is used by @code{set
+print frame-arguments scalars} (@pxref{Print Settings}).
 @end table
 @end defun
 
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index 4ef45b283f9..7b2aa588bb0 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -753,6 +753,8 @@ gdbpy_print_options (PyObject *unused1, PyObject *unused2)
 		      opts.static_field_print) < 0
       || set_boolean (result.get (), "deref_refs",
 		      opts.deref_ref) < 0
+      || set_boolean (result.get (), "summary",
+		      opts.summary) < 0
       || set_unsigned (result.get (), "max_elements",
 		       opts.print_max) < 0
       || set_unsigned (result.get (), "max_depth",
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 48acfc8d173..54f9025a60b 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -640,6 +640,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
       "unions",			/* See set print union on|off.  */
       "address",		/* See set print address on|off.  */
       "styling",		/* Should we apply styling.  */
+      "summary",		/* Summary mode for non-scalars.  */
       /* C++ options.  */
       "deref_refs",		/* No corresponding setting.  */
       "actual_objects",		/* See set print object on|off.  */
@@ -688,10 +689,11 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
   PyObject *deref_refs_obj = NULL;
   PyObject *actual_objects_obj = NULL;
   PyObject *static_members_obj = NULL;
+  PyObject *summary_obj = NULL;
   char *format = NULL;
   if (!gdb_PyArg_ParseTupleAndKeywords (args,
 					kw,
-					"|O!O!O!O!O!O!O!O!O!O!O!IIIs",
+					"|O!O!O!O!O!O!O!O!O!O!O!O!IIIs",
 					keywords,
 					&PyBool_Type, &raw_obj,
 					&PyBool_Type, &pretty_arrays_obj,
@@ -701,6 +703,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
 					&PyBool_Type, &unions_obj,
 					&PyBool_Type, &address_obj,
 					&PyBool_Type, &styling_obj,
+					&PyBool_Type, &summary_obj,
 					&PyBool_Type, &deref_refs_obj,
 					&PyBool_Type, &actual_objects_obj,
 					&PyBool_Type, &static_members_obj,
@@ -731,6 +734,8 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
     return NULL;
   if (!copy_py_bool_obj (&opts.static_field_print, static_members_obj))
     return NULL;
+  if (!copy_py_bool_obj (&opts.summary, summary_obj))
+    return nullptr;
 
   /* Numeric arguments for which 0 means unlimited (which we represent as
      UINT_MAX).  Note that the max-depth numeric argument uses -1 as
diff --git a/gdb/testsuite/gdb.python/py-format-string.exp b/gdb/testsuite/gdb.python/py-format-string.exp
index 0b7cdc18f5f..38794610482 100644
--- a/gdb/testsuite/gdb.python/py-format-string.exp
+++ b/gdb/testsuite/gdb.python/py-format-string.exp
@@ -1054,6 +1054,12 @@ proc test_print_options {} {
 	"print in binary to fetch options"
     gdb_test "python print(saved_options\['format'\] == 't')" "True" \
 	"format was set"
+
+    check_format_string "a_point_t" "summary=True" \
+	"No Data" \
+	"print in summary mode"
+    gdb_test "python print(saved_options\['summary'\])" "True" \
+	"summary was set"
 }
 
 # Run all the tests in common for both C and C++.
diff --git a/gdb/testsuite/gdb.python/py-format-string.py b/gdb/testsuite/gdb.python/py-format-string.py
index aa7b10445cd..8eee211a6ad 100644
--- a/gdb/testsuite/gdb.python/py-format-string.py
+++ b/gdb/testsuite/gdb.python/py-format-string.py
@@ -28,6 +28,8 @@ class PointPrinter(object):
     def to_string(self):
         global saved_options
         saved_options = gdb.print_options()
+        if saved_options['summary']:
+            return "No Data"
         return "Pretty Point (%s, %s)" % (self.val["x"], self.val["y"])
 
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] Add 'summary' mode to Value.format_string
  2022-06-07 15:42 ` [PATCH 2/2] Add 'summary' mode to Value.format_string Tom Tromey
@ 2022-06-07 16:10   ` Eli Zaretskii
  2022-07-15 14:45     ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2022-06-07 16:10 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> Date: Tue,  7 Jun 2022 09:42:26 -0600
> From: Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
> Cc: Tom Tromey <tromey@adacore.com>
> 
> This adds a 'summary' mode to Value.format_string and to
> gdb.print_options.  For the former, it lets Python code format values
> using this mode.  For the latter, it lets a printer potentially detect
> if it is being called in a backtrace with 'set print frame-arguments'
> set to 'scalars'.

Thanks.

> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -70,6 +70,8 @@ maintenance info line-table
>    ** gdb.Value.format_string now uses the format provided by 'print',
>       if it is called during a 'print' or other similar operation.
>  
> +  ** gdb.Value.format_string now accepts the 'summary' keyword.

Maybe say something about what this does?

The documentation parts are OK.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] Expose current 'print' settings to Python
  2022-06-07 15:42 ` [PATCH 1/2] Expose current 'print' settings to Python Tom Tromey
@ 2022-06-07 16:12   ` Eli Zaretskii
  0 siblings, 0 replies; 7+ messages in thread
From: Eli Zaretskii @ 2022-06-07 16:12 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> Date: Tue,  7 Jun 2022 09:42:25 -0600
> From: Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
> Cc: Tom Tromey <tromey@adacore.com>
> 
> PR python/17291 asks for access to the current print options.  While I
> think this need is largely satisfied by the existence of
> Value.format_string, it seemed to me that a bit more could be done.
> 
> First, while Value.format_string uses the user's settings, it does not
> react to temporary settings such as "print/x".  This patch changes
> this.
> 
> Second, there is no good way to examine the current settings (in
> particular the temporary ones in effect for just a single "print").
> This patch adds this as well.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17291
> ---
>  gdb/NEWS                                      |  7 ++
>  gdb/doc/python.texi                           | 18 ++++
>  gdb/python/py-prettyprint.c                   | 98 ++++++++++++++++++-
>  gdb/python/py-value.c                         |  4 +-
>  gdb/python/py-varobj.c                        | 25 ++++-
>  gdb/python/python-internal.h                  | 13 ++-
>  gdb/python/python.c                           |  4 +
>  gdb/testsuite/gdb.python/py-format-string.exp | 34 +++++--
>  gdb/testsuite/gdb.python/py-format-string.py  |  4 +
>  gdb/varobj.c                                  | 13 ++-
>  10 files changed, 199 insertions(+), 21 deletions(-)

OK for the documentation parts, thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] Add 'summary' mode to Value.format_string
  2022-06-07 16:10   ` Eli Zaretskii
@ 2022-07-15 14:45     ` Tom Tromey
  0 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2022-07-15 14:45 UTC (permalink / raw)
  To: Eli Zaretskii via Gdb-patches; +Cc: Tom Tromey, Eli Zaretskii

>>>>> "Eli" == Eli Zaretskii via Gdb-patches <gdb-patches@sourceware.org> writes:

>> --- a/gdb/NEWS
>> +++ b/gdb/NEWS
>> @@ -70,6 +70,8 @@ maintenance info line-table
>> ** gdb.Value.format_string now uses the format provided by 'print',
>> if it is called during a 'print' or other similar operation.
>> 
>> +  ** gdb.Value.format_string now accepts the 'summary' keyword.

Eli> Maybe say something about what this does?

I added a sentence about this.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] More introspection for Value.format_string
  2022-06-07 15:42 [PATCH 0/2] More introspection for Value.format_string Tom Tromey
  2022-06-07 15:42 ` [PATCH 1/2] Expose current 'print' settings to Python Tom Tromey
  2022-06-07 15:42 ` [PATCH 2/2] Add 'summary' mode to Value.format_string Tom Tromey
@ 2022-07-15 15:24 ` Tom Tromey
  2 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2022-07-15 15:24 UTC (permalink / raw)
  To: Tom Tromey via Gdb-patches; +Cc: Tom Tromey

>>>>> "Tom" == Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> writes:

Tom> This series addresses a longstanding wish to be able to access the
Tom> value_print_options from pretty-printers.

Tom> First, it exposes this information via a new API.

Tom> Second, when a 'print' is in effect, it arranges for the local print
Tom> options to be stored and then applied by Value.format_string.  This
Tom> makes it so that format_string respect the current settings by
Tom> default.

Tom> Finally, this series exposes the 'summary' field from
Tom> value_print_options.

Tom> Regression tested on x86-64 Fedora 34.

I've updated this series and re-tested it, and now I'm checking it in.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-07-15 15:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-07 15:42 [PATCH 0/2] More introspection for Value.format_string Tom Tromey
2022-06-07 15:42 ` [PATCH 1/2] Expose current 'print' settings to Python Tom Tromey
2022-06-07 16:12   ` Eli Zaretskii
2022-06-07 15:42 ` [PATCH 2/2] Add 'summary' mode to Value.format_string Tom Tromey
2022-06-07 16:10   ` Eli Zaretskii
2022-07-15 14:45     ` Tom Tromey
2022-07-15 15:24 ` [PATCH 0/2] More introspection for Value.format_string Tom Tromey

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).