This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v4 2/2] Implement pahole-like 'ptype /o' option


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:
>
>   <https://src.fedoraproject.org/rpms/gdb/blob/master/f/gdb-archer.patch#_311>
>
> 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.

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

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  \\\*/" \


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]