From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 100439 invoked by alias); 5 Feb 2018 05:39:16 -0000 Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org Received: (qmail 100429 invoked by uid 89); 5 Feb 2018 05:39:15 -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,SPF_HELO_PASS,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=gmt08, GMT08, GMT-08, gmt-08 X-HELO: simark.ca Received: from simark.ca (HELO simark.ca) (158.69.221.121) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 05 Feb 2018 05:39:14 +0000 Received: from [10.0.0.11] (192-222-251-162.qc.cable.ebox.net [192.222.251.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 705081E4F4; Mon, 5 Feb 2018 00:39:12 -0500 (EST) Subject: Re: GDB returns wrong type when traversing optimized-out Fields To: Roman Popov , gdb@sourceware.org References: From: Simon Marchi Message-ID: Date: Mon, 05 Feb 2018 05:39:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-SW-Source: 2018-02/txt/msg00022.txt.bz2 On 2018-02-04 11:06 PM, Roman Popov wrote: > I apologize for code typo in previous email. Here is correct code sample: > > template > struct TRAITS { > static const unsigned val1 = v1; > static const unsigned val2 = v2; > }; > template < class TRAITS > > struct foo { > static const unsigned x1 = TRAITS::val1; > static const unsigned x2 = TRAITS::val2; > }; > > int main () { > foo> f1; > // SET BREAKPOINT HERE > return 0; > } > > -Roman > > > 2018-02-04 20:02 GMT-08:00 Roman Popov : > >> Hi all, >> I've encountered strange GDB behavior when requesting a value of >> optimized-away field. >> Instead of returning None or raising exception, GDB returns an >> optimizied-out value of wrong type. >> >> Here is a small reproducer *optimize_out.cpp*: >> >> template >> struct TRAITS { >> static const unsigned val1 = v1; >> static const unsigned val2 = v2; >> }; >> template < class TRAITS > >> struct foo { >> static const unsigned x1 = TRAITS::v1; >> static const unsigned x2 = TRAITS::v2; >> }; >> >> int main () { >> foo> f1; >> // SET BREAKPOINT HERE >> return 0; >> } >> >> # Using g++ 7.3 >> $ g++ -g optimize_out.cpp >> >> # Using gdb 8.1 >> $ gdb a.out >> >> (gdb) break optimize_out.cpp:14 >> (gdb) r >> (gdb) p f1 >> $1 = {static x1 = , static x2 = } >> >> Ok, looks good. Now traverse fields: >> >> (gdb) python >>> f1 = gdb.parse_and_eval("f1") >>> for field in f1.type.fields(): >>> print ("field name: ", field.name, "field type: ", field.type) >>> field_val = f1[field] >>> print ("optout?: ",field_val.is_optimized_out, "type: >> ",field_val.type) >>> end >> field name: x1 field type: const unsigned int >> optout?: True type: foo > >> field name: x2 field type: const unsigned int >> optout?: True type: foo > >> >> >> >> So type we get is foo >, not unsigned int. >> >> Looks like GDB-MI has same behavior. At least this code sample totatlly >> confuses GDB GUI I use. >> >> Thanks, >> Roman >> Hi Roman, It just seems to be that we return a value with the type of the structure instead of the type of the field in an error path somewhere. It causes these weird behaviors in the CLI too: (gdb) p f1 $1 = {static x1 = , static x2 = } (gdb) p f1.x1 $2 = (gdb) ptype f1.x1 type = struct foo > [with TRAITS = TRAITS<1, 2>] { static const unsigned int x1; static const unsigned int x2; } (gdb) ptype f1.x1.x1 type = struct foo > [with TRAITS = TRAITS<1, 2>] { static const unsigned int x1; static const unsigned int x2; } (gdb) p f1.x1.x1.x1.x1 $3 = Can you try the patch below? I did not run the testsuite on it. >From ef7f73557e291ed2d8bc7f175765bdbb91e2c817 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 5 Feb 2018 00:34:08 -0500 Subject: [PATCH] Return right type in value_static_field --- gdb/value.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gdb/value.c b/gdb/value.c index 9a144fb7fb..063f57129a 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -2978,7 +2978,10 @@ value_static_field (struct type *type, int fieldno) = lookup_minimal_symbol (phys_name, NULL, NULL); if (!msym.minsym) - return allocate_optimized_out_value (type); + { + struct type *field_type = TYPE_FIELD_TYPE (type, fieldno); + return allocate_optimized_out_value (field_type); + } else { retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno), -- 2.16.1