From patchwork Fri Mar 23 21:25:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Weimin Pan X-Patchwork-Id: 26456 Received: (qmail 100666 invoked by alias); 23 Mar 2018 21:51:53 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 100331 invoked by uid 89); 23 Mar 2018 21:51:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.2 spammy=H*MI:oracle, runto_main X-HELO: aserp2120.oracle.com Received: from aserp2120.oracle.com (HELO aserp2120.oracle.com) (141.146.126.78) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 23 Mar 2018 21:51:50 +0000 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w2NLg25j087627 for ; Fri, 23 Mar 2018 21:51:49 GMT Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by aserp2120.oracle.com with ESMTP id 2gw9qk812v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 23 Mar 2018 21:51:49 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w2NLplhX004878 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 23 Mar 2018 21:51:48 GMT Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w2NLpl8Z011093 for ; Fri, 23 Mar 2018 21:51:47 GMT Received: from wmpan.us.oracle.com (/10.147.27.127) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 23 Mar 2018 14:51:47 -0700 From: Weimin Pan To: gdb-patches@sourceware.org Subject: [PATCH 3 PR gdb/16959] gdb hangs in infinite recursion Date: Fri, 23 Mar 2018 15:25:52 -0600 Message-Id: <1521840352-75024-1-git-send-email-weimin.pan@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8841 signatures=668695 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=4 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1803230239 The original problem was fixed (see related PR 22242). But using a typedef as the declared type for a static member variable, as commented in this PR, is still causing gdb to get into infinite loop when printing the static member's value. This problem can be reproduced as follows: % cat t.cc class A { typedef A type; public: bool operator==(const type& other) { return true; } static const type INSTANCE; }; const A A::INSTANCE; int main() { A a; if (a == A::INSTANCE) { return -1; } return 0; } % g++ -g t.cc % gdb -ex "start" -ex "p a" a.out The fix is rather trivial - in cp_print_static_field(), should call check_typedef() to get the static member's real type and use it to check whether it's a struct or an array. As Simon suggested, I've added a new test case to the testsuite and am passing the original type, not the real type, as argument to function val_print(). Re-tested on both aarch64-linux-gnu and amd64-linux-gnu. No regressions. --- --- gdb/ChangeLog | 7 ++++ gdb/cp-valprint.c | 8 ++-- gdb/testsuite/ChangeLog | 5 +++ gdb/testsuite/gdb.cp/static-typedef-print.cc | 35 +++++++++++++++++++++ gdb/testsuite/gdb.cp/static-typedef-print.exp | 40 +++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 gdb/testsuite/gdb.cp/static-typedef-print.cc create mode 100644 gdb/testsuite/gdb.cp/static-typedef-print.exp diff --git a/gdb/ChangeLog b/gdb/ChangeLog index d0a8dfd..6fd43de 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,10 @@ +2018-02-07 Weimin Pan + + PR gdb/16959 + * cp-valprint.c: (cp_print_static_field) Use check_typedef() to get + static member's real type for TYPE_CODE_STRUCT and TYPE_CODE_ARRAY + comparisons. + 2018-01-24 Pedro Alves GCC PR libstdc++/83906 diff --git a/gdb/cp-valprint.c b/gdb/cp-valprint.c index 486653f..8b4df98 100644 --- a/gdb/cp-valprint.c +++ b/gdb/cp-valprint.c @@ -633,7 +633,8 @@ cp_print_static_field (struct type *type, return; } - if (TYPE_CODE (type) == TYPE_CODE_STRUCT) + struct type *real_type = check_typedef (type); + if (TYPE_CODE (real_type) == TYPE_CODE_STRUCT) { CORE_ADDR *first_dont_print; CORE_ADDR addr; @@ -658,15 +659,14 @@ cp_print_static_field (struct type *type, addr = value_address (val); obstack_grow (&dont_print_statmem_obstack, (char *) &addr, sizeof (CORE_ADDR)); - type = check_typedef (type); - cp_print_value_fields (type, value_enclosing_type (val), + cp_print_value_fields (real_type, value_enclosing_type (val), value_embedded_offset (val), addr, stream, recurse, val, options, NULL, 1); return; } - if (TYPE_CODE (type) == TYPE_CODE_ARRAY) + if (TYPE_CODE (real_type) == TYPE_CODE_ARRAY) { struct type **first_dont_print; int i; diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 0f02f4a..6849d5a 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-03-20 Weimin Pan + + * gdb.cp/static-typedef-print.exp: New file. + * gdb.cp/static-typedef-print.cc: New file. + 2018-01-22 Pedro Alves Sergio Durigan Junior diff --git a/gdb/testsuite/gdb.cp/static-typedef-print.cc b/gdb/testsuite/gdb.cp/static-typedef-print.cc new file mode 100644 index 0000000..d698d6f --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-typedef-print.cc @@ -0,0 +1,35 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2018 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +class A { + typedef A type; +public: + bool operator==(const type& other) { return true; } + + static const type INSTANCE; +}; + +const A A::INSTANCE = {}; + +int main() { + A a; + if (a == A::INSTANCE) { + return -1; + } + return 0; +} + diff --git a/gdb/testsuite/gdb.cp/static-typedef-print.exp b/gdb/testsuite/gdb.cp/static-typedef-print.exp new file mode 100644 index 0000000..e0da0c9 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-typedef-print.exp @@ -0,0 +1,40 @@ +# Copyright 2018 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +if { [skip_cplus_tests] } { continue } + +standard_testfile .cc + +if [get_compiler_info "c++"] { + return -1 +} + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} { + return -1 +} + +clean_restart $testfile + +if ![runto_main] { + untested "could not run to main" + return -1 +} + +gdb_test "print a" \ + "static INSTANCE = }}.*" \ + "print static member" + +gdb_exit +return 0