From patchwork Tue Dec 12 00:27:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergio Durigan Junior X-Patchwork-Id: 24886 Received: (qmail 93475 invoked by alias); 12 Dec 2017 00:27:26 -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 92495 invoked by uid 89); 12 Dec 2017 00:27:25 -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, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=Hx-languages-length:4832 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 12 Dec 2017 00:27:23 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id ACC567AE92; Tue, 12 Dec 2017 00:27:22 +0000 (UTC) Received: from localhost (unused-10-15-17-193.yyz.redhat.com [10.15.17.193]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 69C3C60C9E; Tue, 12 Dec 2017 00:27:22 +0000 (UTC) From: Sergio Durigan Junior To: GDB Patches Cc: Tom Tromey , Eli Zaretskii , Simon Marchi , Pedro Alves , =?utf-8?B?QW5kcsOpIFDDtm5pdHo=?= , Keith Seitz Subject: Re: [PATCH v4 2/2] Implement pahole-like 'ptype /o' option References: <20171121160709.23248-1-sergiodj@redhat.com> <20171211234345.27351-1-sergiodj@redhat.com> <20171211234345.27351-3-sergiodj@redhat.com> Date: Mon, 11 Dec 2017 19:27:21 -0500 In-Reply-To: <20171211234345.27351-3-sergiodj@redhat.com> (Sergio Durigan Junior's message of "Mon, 11 Dec 2017 18:43:45 -0500") Message-ID: <87zi6o7pue.fsf@redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) MIME-Version: 1.0 X-IsSubscribed: yes On Monday, December 11 2017, I wrote: > This commit implements the pahole-like '/o' option for 'ptype', which > prints the offsets and sizes of struct fields, reporting whenever > there is a hole found. > > The output is heavily based on pahole(1), with a few modifications > here and there to adjust it to our reality. Here's an example: > > (gdb) ptype /o stap_probe > /* offset | size */ > struct stap_probe { > /* 0 | 40 */ struct probe { > /* 0 | 8 */ const probe_ops *pops; > /* 8 | 8 */ gdbarch *arch; > /* 16 | 8 */ const char *name; > /* 24 | 8 */ const char *provider; > /* 32 | 8 */ CORE_ADDR address; > } /* total size: 40 bytes */ p; > /* 40 | 8 */ CORE_ADDR sem_addr; > /* 48:31 | 4 */ unsigned int args_parsed : 1; > /* XXX 7-bit hole */ > /* XXX 7-byte hole */ > /* 56 | 8 */ union { > /* 8 */ const char *text; > /* 8 */ VEC_stap_probe_arg_s *vec; > } /* total size: 8 bytes */ args_u; > } /* total size: 64 bytes */ > > A big part of this patch handles the formatting logic of 'ptype', > which is a bit messy. I tried to be not very invasive, but I had to > do some cleanups here and there to make life easier. > > This patch is the start of a long-term work I'll do to flush the local > patches we carry for Fedora GDB. In this specific case, I'm aiming at > upstreaming the feature implemented by the 'pahole.py' script that is > shipped with Fedora GDB: > > > > This has been regression-tested on the BuildBot. There's a new > testcase for it, along with an update to the documentation. I also > thought it was worth mentioning this feature in the NEWS file. The patch below applies on top of this one and extends the output to include offsets of union fields. diff --git a/gdb/c-typeprint.c b/gdb/c-typeprint.c index 23138d8a40..e66129f643 100644 --- a/gdb/c-typeprint.c +++ b/gdb/c-typeprint.c @@ -906,11 +906,15 @@ output_access_specifier (struct ui_file *stream, static void c_print_type_union_field_offset (struct type *type, unsigned int field_idx, - struct ui_file *stream) + struct ui_file *stream, + unsigned int offset_bitpos) { struct type *ftype = check_typedef (TYPE_FIELD_TYPE (type, field_idx)); + unsigned int bitpos = TYPE_FIELD_BITPOS (type, field_idx); - fprintf_filtered (stream, "/* %4u */", TYPE_LENGTH (ftype)); + fprintf_filtered (stream, "/* %4u | %4u */", + (bitpos + offset_bitpos) / TARGET_CHAR_BIT, + TYPE_LENGTH (ftype)); } /* Print information about field at index FIELD_IDX of the struct type @@ -1169,7 +1173,8 @@ c_type_print_base_struct_union (struct type *type, struct ui_file *stream, c_print_type_struct_field_offset (type, i, &endpos, stream, flags->offset_bitpos); else if (TYPE_CODE (type) == TYPE_CODE_UNION) - c_print_type_union_field_offset (type, i, stream); + c_print_type_union_field_offset (type, i, stream, + flags->offset_bitpos); } else print_spaces_filtered (OFFSET_SPC_LEN, stream); diff --git a/gdb/testsuite/gdb.base/ptype-offsets.exp b/gdb/testsuite/gdb.base/ptype-offsets.exp index fa1c0cb41c..7566dcaaab 100644 --- a/gdb/testsuite/gdb.base/ptype-offsets.exp +++ b/gdb/testsuite/gdb.base/ptype-offsets.exp @@ -44,8 +44,8 @@ gdb_test "ptype /o struct abc" \ "/\\\* XXX 7-byte hole \\\*/" \ "/\\\* 32 | 8 \\\*/ uint64_t field5;" \ "/\\\* 40 | 8 \\\*/ union {" \ -"/\\\* 8 \\\*/ void \\\*field6;" \ -"/\\\* 4 \\\*/ int field7;" \ +"/\\\* 40 | 8 \\\*/ void \\\*field6;" \ +"/\\\* 40 | 4 \\\*/ int field7;" \ " } /\\\* total size: 8 bytes \\\*/ field8;" \ "" \ " abc\\(void\\);" \ @@ -82,13 +82,13 @@ gdb_test "ptype /o struct pqr" \ gdb_test "ptype /o union qwe" \ [multi_line \ "/\\\* offset | size \\\*/" \ -"/\\\* 24 \\\*/ struct tuv {" \ +"/\\\* 0 | 24 \\\*/ struct tuv {" \ "/\\\* 0 | 4 \\\*/ int a1;" \ "/\\\* XXX 4-byte hole \\\*/" \ "/\\\* 8 | 8 \\\*/ char \\\*a2;" \ "/\\\* 16 | 4 \\\*/ int a3;" \ " } /\\\* total size: 24 bytes \\\*/ fff1;" \ -"/\\\* 40 \\\*/ struct xyz {" \ +"/\\\* 0 | 40 \\\*/ struct xyz {" \ "/\\\* 0 | 4 \\\*/ int f1;" \ "/\\\* 4 | 1 \\\*/ char f2;" \ "/\\\* XXX 3-byte hole \\\*/" \ @@ -111,13 +111,13 @@ gdb_test "ptype /o struct poi" \ "/\\\* 0 | 4 \\\*/ int f1;" \ "/\\\* XXX 4-byte hole \\\*/" \ "/\\\* 8 | 40 \\\*/ union qwe {" \ -"/\\\* 24 \\\*/ struct tuv {" \ +"/\\\* 8 | 24 \\\*/ struct tuv {" \ "/\\\* 8 | 4 \\\*/ int a1;" \ "/\\\* XXX 4-byte hole \\\*/" \ "/\\\* 16 | 8 \\\*/ char \\\*a2;" \ "/\\\* 24 | 4 \\\*/ int a3;" \ " } /\\\* total size: 24 bytes \\\*/ fff1;" \ -"/\\\* 40 \\\*/ struct xyz {" \ +"/\\\* 8 | 40 \\\*/ struct xyz {" \ "/\\\* 8 | 4 \\\*/ int f1;" \ "/\\\* 12 | 1 \\\*/ char f2;" \ "/\\\* XXX 3-byte hole \\\*/" \