From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id D438C3836672 for ; Thu, 26 May 2022 16:52:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D438C3836672 Received: by mail-il1-x12a.google.com with SMTP id b11so1440763ilr.4 for ; Thu, 26 May 2022 09:52:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=eCSV29Flt8zFg6MZszQ7zNQ9JQla3HrmnovgoeUgLGk=; b=O7kJfdB7fx6j2ZGUDrxoxgvk0DTU9k+jRiAr+yElZ6ABxgg947Ord0lOFWUK+vmH7O GShzCQMbklGTuIVncWBVeOJme0ratEC3RL07t6jFOW4XEZcCY6pH2PnBuT7TBevioNA0 zkWUlED4LWJvYAG8vE6PmJE0K/ABDtovLYLBo7zc+Nt+qDYrum2B+sF/M1CRCCCb4tv2 LlgedhLmikhUhLY0VG1KH3MpuvQWUbChfrI4FzTUieOlVjE+/Zdab9kpd6WUeIPbRT8H 2qhgoN9cU6QeA0X2pLzAW2Utrt9t3+8U5bjghYqwnn3qVIsKoVPWNB8sJV34bVneK1ew C/Yg== X-Gm-Message-State: AOAM532L2UKcefsz+OzF4iMD/W/odbQo62iLhbTbsuvy66I8Cdxm7Csd EB/WP/c1KWmHFYgTfV8K0rX8+9bZJXRO7A== X-Google-Smtp-Source: ABdhPJwStpvpVABAEaiTxh8iaBmd33S/xGg1VSlfTTDRny/MCBLRpeLV/+CaYvnKjkQOiEZrKfBu/g== X-Received: by 2002:a05:6e02:20ce:b0:2d1:c77c:5634 with SMTP id 14-20020a056e0220ce00b002d1c77c5634mr7066822ilq.73.1653583931071; Thu, 26 May 2022 09:52:11 -0700 (PDT) Received: from murgatroyd.Home (71-211-158-194.hlrn.qwest.net. [71.211.158.194]) by smtp.gmail.com with ESMTPSA id b7-20020a023807000000b0032ec819a560sm512390jaa.169.2022.05.26.09.52.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 May 2022 09:52:10 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Put pretty-printers to_string output in varobj result Date: Thu, 26 May 2022 10:52:08 -0600 Message-Id: <20220526165208.1097144-1-tromey@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 May 2022 16:52:14 -0000 PR mi/11335 points out that an MI varobj will not display the result of a pretty-printer's "to_string" method. Instead, it always shows "{...}". This does not seem very useful, and there have been multiple complaints about it over the years. This patch changes varobj to emit this string when possible, and updates the test suite. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=11335 --- gdb/doc/gdb.texinfo | 5 +++-- gdb/testsuite/gdb.python/py-mi.exp | 19 +++++++++++-------- gdb/testsuite/lib/mi-support.exp | 8 ++++---- gdb/varobj.c | 29 ++++++++--------------------- 4 files changed, 26 insertions(+), 35 deletions(-) diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 68679982919..adb822aacf6 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -33576,8 +33576,9 @@ reliable for a dynamic varobj. Instead, you must examine the @item value The varobj's scalar value. For a varobj whose type is some sort of -aggregate (e.g., a @code{struct}), or for a dynamic varobj, this value -will not be interesting. +aggregate (e.g., a @code{struct}), this value will not be interesting. +For a dynamic varobj, this value comes directly from the Python +pretty-printer object's @code{to_string} method. @item type The varobj's type. This is a string representation of the type, as diff --git a/gdb/testsuite/gdb.python/py-mi.exp b/gdb/testsuite/gdb.python/py-mi.exp index 291980479a3..3c0fa9a2d15 100644 --- a/gdb/testsuite/gdb.python/py-mi.exp +++ b/gdb/testsuite/gdb.python/py-mi.exp @@ -96,7 +96,7 @@ mi_gdb_test "-var-update string" \ with_test_prefix "varobj container" { # The "elements" field of "c" is still empty, so the attribute # "has_more" is expected to be zero. - mi_create_dynamic_varobj container c 0 \ + mi_create_dynamic_varobj container c "container .* with 0 elements" 0 \ "create container varobj" mi_list_varobj_children container { @@ -212,7 +212,7 @@ mi_next "next over update 5" # Regression test: examine an object that has no children, then update # it to ensure that we don't print the children. -mi_create_dynamic_varobj container2 c2 0 \ +mi_create_dynamic_varobj container2 c2 "container .* with 0 elements" 0 \ "create second container varobj" mi_gdb_test "-var-update container2" \ @@ -233,7 +233,7 @@ mi_continue_to_line \ [gdb_get_line_number {MI outer breakpoint here} ${srcfile}] \ "step to first outer breakpoint" -mi_create_dynamic_varobj outer outer 1 \ +mi_create_dynamic_varobj outer outer "x = 0" 1 \ "create outer varobj" mi_list_varobj_children outer { @@ -264,7 +264,7 @@ mi_continue_to_line \ "step to second outer breakpoint" with_test_prefix "varobj nscont" { - mi_create_dynamic_varobj nscont nstype 1 \ + mi_create_dynamic_varobj nscont nstype ".*" 1 \ "create nstype varobj" mi_list_varobj_children nscont { @@ -289,7 +289,7 @@ set cmd "python exception_flag = True" set re [string_list_to_regexp & {"} $cmd \\ n {"} \r\n ^ done] mi_gdb_test $cmd $re -mi_create_dynamic_varobj nstype2 nstype2 1 \ +mi_create_dynamic_varobj nstype2 nstype2 ".*" 1 \ "create nstype2 varobj" mi_list_varobj_children nstype2 { @@ -304,7 +304,8 @@ mi_gdb_test "-var-evaluate-expression me" \ "evaluate me varobj" # Regression test for python/14836. -mi_create_dynamic_varobj children_as_list children_as_list 1 \ +mi_create_dynamic_varobj children_as_list children_as_list \ + children_as_list_val 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 @@ -322,7 +323,7 @@ mi_continue_to_line \ [gdb_get_line_number {breakpoint bug 14741} ${srcfile}] \ "step to breakpoint for bug 14741" -mi_create_dynamic_varobj c c 1 \ +mi_create_dynamic_varobj c c "container .* with 1 elements" 1 \ "create varobj for c" mi_gdb_test "-var-set-visualizer c ArrayPrinter" \ @@ -335,8 +336,10 @@ mi_list_varobj_children c { mi_next "next over change of array element" +# 'c' is noticed as changing here due to an artifact of the +# -var-update implementation. However, it seems harmless. mi_gdb_test "-var-update c" \ - "\\^done,changelist=\\\[{name=\"c.\\\[0\\\]\",in_scope=\"true\",type_changed=\"false\",has_more=\"0\"}\\\]" \ + "\\^done,changelist=\\\[{name=\"c\",in_scope=\"true\",type_changed=\"false\",displayhint=\"array\",dynamic=\"1\",has_more=\"0\"},{name=\"c.\\\[0\\\]\",in_scope=\"true\",type_changed=\"false\",has_more=\"0\"}\\\]" \ "update varobj after element change" # C++ MI tests diff --git a/gdb/testsuite/lib/mi-support.exp b/gdb/testsuite/lib/mi-support.exp index e578a7e6f9b..2282c10d014 100644 --- a/gdb/testsuite/lib/mi-support.exp +++ b/gdb/testsuite/lib/mi-support.exp @@ -1481,11 +1481,11 @@ proc mi_create_varobj_checked { name expression type testname } { } # Same as mi_create_floating_varobj, but assumes the test is creating -# a dynamic varobj that has children, so the value must be "{...}". -# The "has_more" attribute is checked. -proc mi_create_dynamic_varobj {name expression has_more testname} { +# a dynamic varobj that has children. The "value" and "has_more" +# attributes are checked. +proc mi_create_dynamic_varobj {name expression value has_more testname} { mi_gdb_test "-var-create $name @ $expression" \ - "\\^done,name=\"$name\",numchild=\"0\",value=\"{\\.\\.\\.}\",type=.*,has_more=\"${has_more}\"" \ + "\\^done,name=\"$name\",numchild=\"0\",value=\"$value\",type=.*,has_more=\"${has_more}\"" \ $testname } diff --git a/gdb/varobj.c b/gdb/varobj.c index 741fdb6a03b..f6190600395 100644 --- a/gdb/varobj.c +++ b/gdb/varobj.c @@ -635,21 +635,6 @@ install_dynamic_child (struct varobj *var, } } -#if HAVE_PYTHON - -static bool -dynamic_varobj_has_child_method (const struct varobj *var) -{ - PyObject *printer = var->dynamic->pretty_printer; - - if (!gdb_python_initialized) - return false; - - gdbpy_enter_varobj enter_py (var); - return PyObject_HasAttr (printer, gdbpy_children_cst); -} -#endif - /* A factory for creating dynamic varobj's iterators. Returns an iterator object suitable for iterating over VAR's children. */ @@ -2158,11 +2143,6 @@ varobj_value_get_print_value (struct value *value, if (value_formatter) { - /* First check to see if we have any children at all. If so, - we simply return {...}. */ - if (dynamic_varobj_has_child_method (var)) - return "{...}"; - if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst)) { struct value *replacement; @@ -2172,7 +2152,7 @@ varobj_value_get_print_value (struct value *value, &stb); /* If we have string like output ... */ - if (output != NULL) + if (output != nullptr && output != Py_None) { /* If this is a lazy string, extract it. For lazy strings we always print as a string, so set @@ -2224,6 +2204,13 @@ varobj_value_get_print_value (struct value *value, if (replacement) value = replacement; } + else + { + /* No to_string method, so if there is a 'children' + method, return the default. */ + if (PyObject_HasAttr (value_formatter, gdbpy_children_cst)) + return "{...}"; + } } } #endif -- 2.34.1