MI: Allow non-raw varobj evaluation
authorLeszek Swirski via gdb-patches <gdb-patches@sourceware.org>
Fri, 2 Feb 2018 04:23:28 +0000 (23:23 -0500)
committerSimon Marchi <simon.marchi@ericsson.com>
Fri, 2 Feb 2018 19:03:25 +0000 (14:03 -0500)
Make the MI variable object expression evaluation, with the
-var-evaluate-expression command, recursively call pretty printers, to
match the output of normal expression printing.

Consider the following code:

        struct Foo { int val; };
        struct Wrapper { Foo foo; };

        int main() {
                Wrapper w;
                w.foo.val = 23;
        }

and this pretty printer file:

        import gdb.printing

        class FooPrinter:
          def __init__(self, val):
            self.val = val
          def to_string(self):
            return "Foo" + str(self.val["val"])

        class WrapperPrinter:
          def __init__(self, val):
            self.val = val
          def to_string(self):
            return self.val["foo"]

        test_printer = gdb.printing.RegexpCollectionPrettyPrinter("test")
        test_printer.add_printer('Foo', '^Foo$', FooPrinter)
        test_printer.add_printer('Wrapper', '^Wrapper$', WrapperPrinter)

        gdb.printing.register_pretty_printer(None, test_printer)

Setting a breakpoint at the end of the function, we call the following commands:

        -enable-pretty-printing
        ^done

        -var-create var_w @ w
        ^done,name="var_w",numchild="0",value="{val = 23}",type="Wrapper",dynamic="1",has_more="0"
        -var-create var_w_foo @ w.foo
        ^done,name="var_w_foo",numchild="0",value="Foo23",type="Foo",dynamic="1",has_more="0"

        -var-evaluate-expression var_w
        ^done,value="{val = 23}"
        -var-evaluate-expression var_w_foo
        ^done,value="Foo23"

        -data-evaluate-expression w
        ^done,value="Foo23"
        -data-evaluate-expression w.foo
        ^done,value="Foo23"

So, in the -var-evaluate-expression var_w case, we print the "raw" value
of w.foo, while in the -data-evaluate-expression w case, we print the
pretty printed w.foo value. After this patch, all of the above print
"Foo23".

gdb/ChangeLog:

* varobj.c (varobj_formatted_print_options): Allow recursive
pretty printing if pretty printing is enabled.

gdb/testsuite/ChangeLog:

* gdb.python/py-prettyprint.c
(struct to_string_returns_value_inner,
struct to_string_returns_value_wrapper): New.
(main): Add tsrvw variable.
* gdb.python/py-prettyprint.py (ToStringReturnsValueInner,
ToStringReturnsValueWrapper): New classes.
(register_pretty_printers): Register new pretty-printers.
* gdb.python/py-prettyprint.exp (run_lang_tests): Test printing
recursive pretty printer.
* gdb.python/py-mi.exp: Likewise.

gdb/ChangeLog
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-mi.exp
gdb/testsuite/gdb.python/py-prettyprint.c
gdb/testsuite/gdb.python/py-prettyprint.exp
gdb/testsuite/gdb.python/py-prettyprint.py
gdb/varobj.c

index 7081f9ec537f95101ae6ea0e65632dff37bd064c..b341da9886a9be269fb6825e3bbc1b930a47737a 100644 (file)
@@ -1,3 +1,8 @@
+2018-02-01  Leszek Swirski  <leszeks@google.com>
+
+       * varobj.c (varobj_formatted_print_options): Allow recursive
+       pretty printing if pretty printing is enabled.
+
 2018-02-01  Leszek Swirski  <leszeks@google.com>
 
        * c-exp.y (lex_one_token, classify_name, yylex): Don't classify
index 1558ed18fec3cf87910a6222df1e51ec708e8faf..0251584448ba1e562b8be9ef42b1093590b6660a 100644 (file)
@@ -1,3 +1,17 @@
+2018-02-01  Simon Marchi  <simon.marchi@polymtl.ca>
+           Leszek Swirski  <leszeks@google.com>
+
+       * gdb.python/py-prettyprint.c
+       (struct to_string_returns_value_inner,
+       struct to_string_returns_value_wrapper): New.
+       (main): Add tsrvw variable.
+       * gdb.python/py-prettyprint.py (ToStringReturnsValueInner,
+       ToStringReturnsValueWrapper): New classes.
+       (register_pretty_printers): Register new pretty-printers.
+       * gdb.python/py-prettyprint.exp (run_lang_tests): Test printing
+       recursive pretty printer.
+       * gdb.python/py-mi.exp: Likewise.
+
 2018-02-01  Leszek Swirski  <leszeks@google.com>
 
        * gdb.cp/filename.cc, gdb.cp/filename.exp: Test that member
index bbe1266724af59092a5484fda763bb21cc8e6486..f9829478f074c3591214cfb1e7e87c4cba9d9a7f 100644 (file)
@@ -295,6 +295,16 @@ mi_gdb_test "-var-evaluate-expression me" \
 mi_create_dynamic_varobj children_as_list children_as_list 1 \
     "printer whose children are returned as a list"
 
+# Test that when a pretty-printer returns a gdb.Value in its to_string, we call
+# the pretty-printer of that value too.
+mi_create_varobj_checked tsrvw tsrvw  \
+    "struct to_string_returns_value_wrapper" \
+    "create tsrvw varobj"
+mi_check_varobj_value tsrvw "Inner to_string 1989" "check tsrvw varobj value"
+mi_gdb_test "-data-evaluate-expression tsrvw" \
+    "\\^done,value=\"Inner to_string 1989\"" \
+    "check tsrvw expression value"
+
 # Regression test for bug 14741.
 mi_continue_to_line \
     [gdb_get_line_number {breakpoint bug 14741} ${srcfile}] \
index fc0d829e04835225bec96fcaff43f6de8a560f98..35ef5e0756b182c56145bd11961046407f22dede 100644 (file)
@@ -112,6 +112,16 @@ class Fake
 };
 #endif
 
+struct to_string_returns_value_inner
+{
+  int val;
+};
+
+struct to_string_returns_value_wrapper
+{
+  struct to_string_returns_value_inner inner;
+};
+
 struct substruct {
   int a;
   int b;
@@ -284,6 +294,7 @@ main ()
   struct lazystring estring, estring2, estring3;
   struct hint_error hint_error;
   struct children_as_list children_as_list;
+  struct to_string_returns_value_wrapper tsrvw = { { 1989 } };
 
   nstype.elements = narray;
   nstype.len = 0;
index 5df6f6915892a247d97999119b511cfaf92f200f..2671cb8471b8de0dd23cbcf597ff33793b2ca0fd 100644 (file)
@@ -64,6 +64,10 @@ proc run_lang_tests {exefile lang} {
     
     gdb_test "print arraystruct" " = {$nl *y = 7, *$nl *x = { a=<23> b=<$hex>,  a=<24> b=<$hex>} *$nl *}"
 
+    # Test that when a pretty-printer returns a gdb.Value in its to_string, we
+    # call the pretty-printer of that value too.
+    gdb_test "print tsrvw" " = Inner to_string 1989"
+
     if {$lang == "c++"} {
        gdb_test "print cps" "=  a=<8> b=<$hex>"
        gdb_test "print cpss" " = {$nl *zss = 9, *$nl *s =  a=<10> b=<$hex>$nl}"
index ea6caaea436496990abdc93827c4a578159a53ac..7357f05cc93e17cac3cc81d2fd841c23402a6761 100644 (file)
@@ -84,6 +84,25 @@ class NoStringContainerPrinter (object):
     def children(self):
         return _iterator_except (self.val['elements'], self.val['len'])
 
+# See ToStringReturnsValueWrapper.
+class ToStringReturnsValueInner:
+
+    def __init__(self, val):
+        self.val = val
+
+    def to_string(self):
+        return 'Inner to_string {}'.format(int(self.val['val']))
+
+# Test a printer that returns a gdb.Value in its to_string.  That gdb.Value
+# also has its own pretty-printer.
+class ToStringReturnsValueWrapper:
+
+    def __init__(self, val):
+        self.val = val
+
+    def to_string(self):
+        return self.val['inner']
+
 class pp_s (object):
     def __init__(self, val):
         self.val = val
@@ -316,7 +335,12 @@ def register_pretty_printers ():
     pretty_printers_dict[re.compile ('^string_repr$')] = string_print
     pretty_printers_dict[re.compile ('^container$')] = ContainerPrinter
     pretty_printers_dict[re.compile ('^justchildren$')] = NoStringContainerPrinter
-    
+
+    pretty_printers_dict[re.compile ('^struct to_string_returns_value_inner$')] = ToStringReturnsValueInner
+    pretty_printers_dict[re.compile ('^to_string_returns_value_inner$')] = ToStringReturnsValueInner
+    pretty_printers_dict[re.compile ('^struct to_string_returns_value_wrapper$')] = ToStringReturnsValueWrapper
+    pretty_printers_dict[re.compile ('^to_string_returns_value_wrapper$')] = ToStringReturnsValueWrapper
+
     pretty_printers_dict[re.compile ('^struct ns$')]  = pp_ns
     pretty_printers_dict[re.compile ('^ns$')]  = pp_ns
 
index b6a2d8f3696c812535d1bcee1c2d1d986c9503ca..f23243f3b722a664a1fe3c543e8db34dc86f0a2b 100644 (file)
@@ -2274,7 +2274,7 @@ varobj_formatted_print_options (struct value_print_options *opts,
 {
   get_formatted_print_options (opts, format_code[(int) format]);
   opts->deref_ref = 0;
-  opts->raw = 1;
+  opts->raw = !pretty_printing;
 }
 
 std::string
This page took 0.034032 seconds and 4 git commands to generate.