From b56fed297e5f61ef785e3428f2e4ac68ff75dda5 Mon Sep 17 00:00:00 2001 From: Andrii Nakryiko Date: Tue, 26 Mar 2019 12:53:30 -0700 Subject: [PATCH] dwarves_fprintf: Count bitfield member sizes separately Counting field sizes only in bits causes confusion and lots of differing output, when compared to previous logic. This commit changes logic so that it counts bit size of bitfield fields separately from byte size of non-bitfield fields. In the end, if there were bit holes, this bit size is emitted explicitly. This makes output for struct/unions not using bitfields identical, while also preserving correctness (and data completeness) for cases with bitfields and bit holes. Example (-before/+after): struct cfg80211_pmsr_request_peer { u8 addr[6]; /* 0 6 */ /* XXX 2 bytes hole, try to pack */ struct cfg80211_chan_def chandef; /* 8 24 */ /* XXX last struct has 4 bytes of padding */ u8 report_ap_tsf:1; /* 32: 0 1 */ /* XXX 7 bits hole, try to pack */ /* XXX 3 bytes hole, try to pack */ struct cfg80211_pmsr_ftm_request_peer ftm; /* 36 12 */ /* XXX last struct has 1 byte of padding */ /* size: 48, cachelines: 1, members: 4 */ - /* sum members: 43, holes: 2, sum holes: 5 */ - /* bit holes: 1, sum bit holes: 7 bits */ + /* sum members: 42, holes: 2, sum holes: 5 */ + /* sum bitfield members: 1 bits, bit holes: 1, sum bit holes: 7 bits */ /* paddings: 2, sum paddings: 5 */ /* last cacheline: 48 bytes */ }; For cases where there is only byte or bit hole, we still emit total byte and bit sizes of all members as to not mislead user: struct sched_dl_entity { ... Signed-off-by: Andrii Nakryiko Cc: Alexei Starovoitov Cc: Yonghong Song Cc: dwarves@vger.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- dwarves_fprintf.c | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/dwarves_fprintf.c b/dwarves_fprintf.c index 4a9b26c..faae3ba 100644 --- a/dwarves_fprintf.c +++ b/dwarves_fprintf.c @@ -1228,7 +1228,7 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu, size_t last_size = 0, size; uint8_t newline = 0; uint16_t nr_paddings = 0; - uint32_t sum = 0; + uint32_t sum_bytes = 0, sum_bits = 0; uint32_t sum_holes = 0; uint32_t sum_paddings = 0; uint32_t sum_bit_holes = 0; @@ -1444,7 +1444,11 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu, continue; #endif - sum += pos->bitfield_size ? pos->bitfield_size : pos->bit_size; + if (pos->bitfield_size) { + sum_bits += pos->bitfield_size; + } else { + sum_bytes += pos->byte_size; + } if (last == NULL || /* First member */ /* @@ -1486,16 +1490,26 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu, printed += type__fprintf_stats(type, cu, &cconf, fp); - if (sum_holes > 0) - printed += fprintf(fp, "%.*s/* sum members (bits): %u, holes: %d, " - "sum holes: %u */\n", - cconf.indent, tabs, - sum, class->nr_holes, sum_holes); - if (sum_bit_holes > 0) - printed += fprintf(fp, "%.*s/* bit holes: %d, sum bit " - "holes: %u bits */\n", - cconf.indent, tabs, - class->nr_bit_holes, sum_bit_holes); + if (sum_holes > 0 || sum_bit_holes > 0) { + if (sum_bytes > 0) { + printed += fprintf(fp, "%.*s/* sum members: %u", + cconf.indent, tabs, sum_bytes); + if (sum_holes > 0) + printed += fprintf(fp, ", holes: %d, sum holes: %u", + class->nr_holes, sum_holes); + printed += fprintf(fp, " */\n"); + } + if (sum_bits > 0) { + printed += fprintf(fp, "%.*s/* sum bitfield members: %u bits", + cconf.indent, tabs, sum_bits); + if (sum_bit_holes > 0) + printed += fprintf(fp, ", bit holes: %d, sum bit holes: %u bits", + class->nr_bit_holes, sum_bit_holes); + else + printed += fprintf(fp, " (%u bytes)", sum_bits / 8); + printed += fprintf(fp, " */\n"); + } + } if (class->padding > 0) printed += fprintf(fp, "%.*s/* padding: %u */\n", cconf.indent, @@ -1524,13 +1538,14 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu, m->byte_size); } - size_diff = type->size * 8 - (sum + sum_holes * 8 + sum_bit_holes + + size_diff = type->size * 8 - (sum_bytes * 8 + sum_bits + sum_holes * 8 + sum_bit_holes + class->padding * 8 + class->bit_padding); if (size_diff && type->nr_members != 0) - printed += fprintf(fp, "\n%.*s/* BRAIN FART ALERT! %d bytes != %u (member bits) " + printed += fprintf(fp, "\n%.*s/* BRAIN FART ALERT! %d bytes != " + "%u (member bytes) + %u (member bits) " "+ %u (byte holes) + %u (bit holes), diff = %d bits */\n", cconf.indent, tabs, - type->size, sum, sum_holes, sum_bit_holes, size_diff); + type->size, sum_bytes, sum_bits, sum_holes, sum_bit_holes, size_diff); out: return printed + fprintf(fp, "%.*s}%s%s", indent, tabs, cconf.suffix ? " ": "", cconf.suffix ?: "");