From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 0QWUGIJYsWmamSMAWB0awg (envelope-from ) for ; Wed, 11 Mar 2026 07:56:50 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=tUhWoB8C; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=FwaP4Ihw; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=tUhWoB8C; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=FwaP4Ihw; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 5CCF01E0DD; Wed, 11 Mar 2026 07:56:50 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id D2CD51E08D for ; Wed, 11 Mar 2026 07:56:48 -0400 (EDT) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id E86E54BB3BE5 for ; Wed, 11 Mar 2026 11:56:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E86E54BB3BE5 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=tUhWoB8C; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=FwaP4Ihw; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=tUhWoB8C; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=FwaP4Ihw Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id DE2774BB3B98 for ; Wed, 11 Mar 2026 11:55:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DE2774BB3B98 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DE2774BB3B98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1773230109; cv=none; b=OXWogQRMwFSxgLVgVynwdJCrKdxZS5x15CmlWJUWodoR2zGe6GoW7vvWVi9i0rhPoIJUfup5tb0EAeEpZrkQ88CWfU+L7g6o/VZdTyHyuKnTUMQQSk8RUiO71aR4/c+usZIUglVamEa0BHC+QitLmfCXsuNe61Xhghu1Aho1two= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1773230109; c=relaxed/simple; bh=TAjo32XTPTXo0hojbCNnD5dWIDurbi6gAunZgx2jPCY=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=Zvk/qMkhAZSjpazUNKEdnuVMcZaK0LltLQWclwlOsX1BVE12pHEeriqTtt0gCtV3puf8rQZYecAjzeLTdny0j5p5LHwkn0VjPtvu1VPgtIIBgi99YY66DPd5Gx7/hwPMPWTchg1bVdSmUQJAv+tCd4Z5+pdQ8U+hp3NL+EADhQU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DE2774BB3B98 Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id BCA394D336 for ; Wed, 11 Mar 2026 11:55:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1773230107; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sags5h/Uxkt2BmzFehB+MxK7BbaMMj+oqXveMWODA6w=; b=tUhWoB8CakRM6c3Xx5t+inhMyYAUSoWfGCmZCuvLzQJgE+b/js0/j2Jk2bO7GrKYldTS0s Rn/TSIeEvWqQM5HHD2QWXHenxDEhrsFOw4hMjRURzhkM0pyTz5fsNkFA+eCE1f5oSdlzxl 8wW+tEXeZWF3igskhiBYLlJdD8BbXk0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1773230107; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sags5h/Uxkt2BmzFehB+MxK7BbaMMj+oqXveMWODA6w=; b=FwaP4IhwbyGzmpRmvha6svtLM9arCZ/KkW51Hl1JoTdOIZdaglxYJrtyNjaR+le6K7qv7h Ap4aYW0S8c0uBbAA== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=tUhWoB8C; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=FwaP4Ihw DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1773230107; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sags5h/Uxkt2BmzFehB+MxK7BbaMMj+oqXveMWODA6w=; b=tUhWoB8CakRM6c3Xx5t+inhMyYAUSoWfGCmZCuvLzQJgE+b/js0/j2Jk2bO7GrKYldTS0s Rn/TSIeEvWqQM5HHD2QWXHenxDEhrsFOw4hMjRURzhkM0pyTz5fsNkFA+eCE1f5oSdlzxl 8wW+tEXeZWF3igskhiBYLlJdD8BbXk0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1773230107; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sags5h/Uxkt2BmzFehB+MxK7BbaMMj+oqXveMWODA6w=; b=FwaP4IhwbyGzmpRmvha6svtLM9arCZ/KkW51Hl1JoTdOIZdaglxYJrtyNjaR+le6K7qv7h Ap4aYW0S8c0uBbAA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id A3F103F9DA for ; Wed, 11 Mar 2026 11:55:07 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id yKSAJhtYsWn/cgAAD6G6ig (envelope-from ) for ; Wed, 11 Mar 2026 11:55:07 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v2 2/2] [gdb] Enable ptype /o for some dynamic types Date: Wed, 11 Mar 2026 12:55:06 +0100 Message-ID: <20260311115506.3101288-3-tdevries@suse.de> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260311115506.3101288-1-tdevries@suse.de> References: <20260311115506.3101288-1-tdevries@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; RCPT_COUNT_ONE(0.00)[1]; MIME_TRACE(0.00)[0:+]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FROM_HAS_DN(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.de:dkim,suse.de:mid,gnu.org:url]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; RCVD_TLS_ALL(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; FUZZY_RATELIMITED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DKIM_TRACE(0.00)[suse.de:+] X-Rspamd-Queue-Id: BCA394D336 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Printing the offsets of a struct containing a flexible array member using "ptype /o" currently fails: ... $ cat test.c struct s { int a; int b[]; }; struct s foo; $ gcc -g test.c -c $ gdb -q -batch test.o -ex "ptype /o struct s" warning: ptype/o does not work with dynamic types; disabling '/o' type = struct s { int a; int b[]; } ... This has been the case since gdb 14, containing commit 0c1aa2a0953 ("Disable ptype/o for dynamic types"). If we revert the commit, we get instead: ... $ gdb -q -batch test.o -ex "ptype /o struct s" /* offset | size */ type = struct s { /* 0 | 4 */ int a; /* 4 | 0 */ int b[]; /* total size (bytes): 4 */ } ... which is similar to what pahole prints: ... struct s { int a; /* 0 4 */ int b[]; /* 4 0 */ /* size: 4, cachelines: 1, members: 2 */ /* last cacheline: 4 bytes */ }; ... The problem is that the commit uses is_dynamic_type: ... if (flags.print_offsets && is_dynamic_type (type)) { warning (_("ptype/o does not work with dynamic types; disabling '/o'")); flags.print_offsets = 0; } ... which is too restrictive. Fix this by using a new function cannot_print_offsets instead. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33966 --- gdb/gdbtypes.c | 39 ++++++++++++-- gdb/gdbtypes.h | 4 ++ gdb/testsuite/gdb.ada/ptype-o.exp | 2 +- gdb/testsuite/gdb.base/ptype-offsets-c.c | 37 +++++++++++++ gdb/testsuite/gdb.base/ptype-offsets-c.exp | 63 ++++++++++++++++++++++ gdb/typeprint.c | 4 +- 6 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 gdb/testsuite/gdb.base/ptype-offsets-c.c create mode 100644 gdb/testsuite/gdb.base/ptype-offsets-c.exp diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index 00ef8122978..8e90ded1d8c 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -1910,10 +1910,11 @@ array_type_has_dynamic_stride (struct type *type) return prop != nullptr && prop->is_constant (); } -/* Worker for is_dynamic_type. */ +/* Worker for is_dynamic_type/cannot_print_offsets. */ static bool -is_dynamic_type_internal_1 (struct type *type) +is_dynamic_type_internal_1 (struct type *type, + bool cannot_print_offsets_p = false) { type = check_typedef (type); @@ -1988,7 +1989,24 @@ is_dynamic_type_internal_1 (struct type *type) continue; /* If the field has dynamic type, then so does TYPE. */ if (is_dynamic_type_internal_1 (f.type ())) - return true; + { + bool last_struct_field_p + = (type->code () == TYPE_CODE_STRUCT + && i == type->num_fields () - 1); + if (cannot_print_offsets_p && last_struct_field_p) + { + if (f.type ()->code () == TYPE_CODE_STRUCT) + /* The last field is a dynamic type and a struct. Check + if we can print the offsets for the struct. */ + return is_dynamic_type_internal_1 (f.type (), true); + + /* The last field is a dynamic type, this is ok to print + offsets for. */ + return false; + } + + return true; + } /* If the field is at a fixed offset, then it is not dynamic. */ if (!f.loc_is_dwarf_block ()) @@ -2031,6 +2049,21 @@ is_dynamic_type (struct type *type) return is_dynamic_type_internal (type, true); } +/* See gdbtypes.h. */ + +bool +cannot_print_offsets (struct type *type) +{ + type = check_typedef (type); + + /* We only want to recognize references and pointers at the outermost + level. */ + if (type->is_pointer_or_reference ()) + type = check_typedef (type->target_type ()); + + return is_dynamic_type_internal_1 (type, true); +} + static struct type *resolve_dynamic_type_internal (struct type *type, const property_addr_info *addr_stack, const frame_info_ptr &frame, bool top_level); diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index 68c272d5fe5..29b03221cb2 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -2644,6 +2644,10 @@ extern struct type *resolve_dynamic_type "dynamic". */ extern bool is_dynamic_type (struct type *type); +/* Return true if TYPE cannot be printed using ptype /o. */ + +extern bool cannot_print_offsets (struct type *type); + /* Resolve any dynamic components of FIELD. FIELD is updated. ADDR_STACK and FRAME are used where necessary to supply information for the resolution process; see resolve_dynamic_type. diff --git a/gdb/testsuite/gdb.ada/ptype-o.exp b/gdb/testsuite/gdb.ada/ptype-o.exp index 3bac7930762..03de262ecc1 100644 --- a/gdb/testsuite/gdb.ada/ptype-o.exp +++ b/gdb/testsuite/gdb.ada/ptype-o.exp @@ -37,7 +37,7 @@ foreach_gnat_encoding scenario flags {all minimal} { "Warning: the current language does not match this frame." if {$scenario == "minimal"} { - set exp "ptype/o does not work with dynamic types.*" + set exp "ptype/o does not work with this dynamic type.*" } else { # In "all" mode this prints nonsense, but at least does not # crash. diff --git a/gdb/testsuite/gdb.base/ptype-offsets-c.c b/gdb/testsuite/gdb.base/ptype-offsets-c.c new file mode 100644 index 00000000000..74ac9ed7b37 --- /dev/null +++ b/gdb/testsuite/gdb.base/ptype-offsets-c.c @@ -0,0 +1,37 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 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 . */ + +struct flexible_array_member +{ + int an_int; + int fam[]; +}; + +struct nested_flexible_array_member +{ + int another_int; + struct flexible_array_member sfam; +}; + +int +main (void) +{ + struct flexible_array_member fam; + struct nested_flexible_array_member nfam; + + return 0; +} diff --git a/gdb/testsuite/gdb.base/ptype-offsets-c.exp b/gdb/testsuite/gdb.base/ptype-offsets-c.exp new file mode 100644 index 00000000000..cffcbbeb2f7 --- /dev/null +++ b/gdb/testsuite/gdb.base/ptype-offsets-c.exp @@ -0,0 +1,63 @@ +# This testcase is part of GDB, the GNU debugger. + +# Copyright 2026 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 . + +# This testcase exercises the "ptype /o" feature, which can be used to +# print the offsets and sizes of each field of a struct/union. +# +# This is similar to ptype-offsets.exp, which uses C++ instead of C. + +standard_testfile .c + +# Test only works on LP64 targets. That's how we guarantee that the +# expected holes will be present in the struct. +if { ![is_lp64_target] } { + untested "test work only on lp64 targets" + return 0 +} + +if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } { + return -1 +} + +# Tests handling flexible array member. Regression tests for PR gdb/33966. +set l { + "ptype /o struct flexible_array_member" + "/* offset | size */ type = struct flexible_array_member {" + "/* 0 | 4 */ int an_int;" + "/* 4 | 0 */ int fam[];" + "" + " /* total size (bytes): 4 */" + " }" +} +gdb_test "ptype /o struct flexible_array_member" \ + [string_to_regexp [multi_line {*}$l]] + +set l { + "/* offset | size */ type = struct nested_flexible_array_member {" + "/* 0 | 4 */ int another_int;" + "/* 4 | 4 */ struct flexible_array_member {" + "/* 4 | 4 */ int an_int;" + "/* 8 | 0 */ int fam[];" + "" + " /* total size (bytes): 4 */" + " } sfam;" + "" + " /* total size (bytes): 8 */" + " }" +} +gdb_test "ptype /o struct nested_flexible_array_member" \ + [string_to_regexp [multi_line {*}$l]] diff --git a/gdb/typeprint.c b/gdb/typeprint.c index e53a2b21af5..31a9c1814a3 100644 --- a/gdb/typeprint.c +++ b/gdb/typeprint.c @@ -451,9 +451,9 @@ whatis_exp (const char *exp, int show) type = val->type (); } - if (flags.print_offsets && is_dynamic_type (type)) + if (flags.print_offsets && cannot_print_offsets (type)) { - warning (_("ptype/o does not work with dynamic types; disabling '/o'")); + warning (_("ptype/o does not work with this dynamic type; disabling '/o'")); flags.print_offsets = 0; } -- 2.51.0