Skip to contentSkip to conversation

improve expanded header

PATCH In review Douglas RAILLARD 3 participants 17 messages Dec 7, 2021 – Dec 17, 2021

lore
Share & tools
lore
#0 lore raw

v2 cover improve expanded header

From: Douglas Raillard <[email protected]>

Improve headers generated with -E in a few ways:

* Print each type only once, to avoid redefinitions

* --forward_decl: Generate forward declaration for struct and union so that uses of pointers will work.

* --expanded_prefix: Allow prefixing names of types added by -E (all the ones not explicitly requested with -C) so they can be namespaced manually. This is important in order to be able to mix pahole-generated headers with existing headers without clash or maintenance.

types.txt:

t1
t4

Original printed type with "-C types.txt -E":

  struct t1 {
    struct t2 *a;
    struct t3 {
      int a;
    } b;
  };

  struct t4 {
    struct t3 {
      int a;
    } a;
  };

After this series and with
"-C types.txt -E --expanded_prefix __pahole --forward_decl":

struct t1
struct t2;
struct t3;

  struct t1 {
    struct t2 *a;
    struct __pahole_t3 {
      int a;
    } b;
  };

  struct t4 {
    struct __pahole_t3 a;
  };

This header can be freely mixed with any other header as long as they don't define t1 or t4 (if there is a clash, then pahole is not necessary and the user can just use the header in the first place).

TODOs:

* Define types on their first use even when they are first used as pointers. Currently, if a type is only used as pointer it will never get defined, which will still compile thanks to --forward_decl but won't allow easy use of the values.

* The prefix system allocates memory to rename types and never frees it. I don't think it's a big issue for now as it's done inside pahole.c and type names are likely necessary until the end of the process anyway, but it makes valgrind angry and could therefore mask other problems. There is another implementation of prefix that modifies dwarves_fprintf.c instead of changing the name but it was very invasive and impossible to check if prefixable spots were missed.

This series is to be applied on the "next" branch as it includes reverts of the --inner_anonymous series, which is dropped since it could not cope with recursive types.

Douglas Raillard (6):
Revert "fprintf: Allow making struct/enum/union anonymous" Revert "pahole.c: Add --inner_anonymous option"
fprintf: Print types only once
pahole.c: Add prefix to expanded type names pahole.c: Add --expanded_prefix option
pahole.c: Add --forward_decl option

 dwarves.h          | 12 ++++--
 dwarves_emit.c     |  2 +-
 dwarves_fprintf.c  | 77 +++++++++++++++++-------------------
 man-pages/pahole.1 |  8 ++--
 pahole.c           | 98 ++++++++++++++++++++++++++++++++++------------
 5 files changed, 121 insertions(+), 76 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/6 Revert "fprintf: Allow making struct/enum/union anonymous"

From: Douglas Raillard <[email protected]>

This reverts commit 7c5e35b63bd26f1de0a22acb6319bf2b1a4166c1.

---
 dwarves.h         |  4 +---
 dwarves_emit.c    |  2 +-
 dwarves_fprintf.c | 52 +++++++++++++++++------------------------------
 3 files changed, 21 insertions(+), 37 deletions(-)
dwarves.h +1 -3
index 2fbeac3..52d162d 100644
@@ -105,7 +105,6 @@ struct conf_fprintf {
 	uint8_t	   indent;
 	uint8_t	   expand_types:1;
 	uint8_t	   expand_pointers:1;
-	uint8_t	   inner_anonymous:1;
 	uint8_t    rel_offset:1;
 	uint8_t	   emit_stats:1;
 	uint8_t	   suppress_comments:1;
@@ -1372,8 +1371,7 @@ static inline const char *enumerator__name(const struct enumerator *enumerator)
 void enumeration__delete(struct type *type);
 void enumeration__add(struct type *type, struct enumerator *enumerator);
 size_t enumeration__fprintf(const struct tag *tag_enum,
-			    const struct conf_fprintf *conf, FILE *fp,
-			    bool anonymous);
+			    const struct conf_fprintf *conf, FILE *fp);
 
 int dwarves__init(void);
 void dwarves__exit(void);
dwarves_emit.c +1 -1
index 5c67559..5bf7946 100644
@@ -91,7 +91,7 @@ static int enumeration__emit_definitions(struct tag *tag,
 		return 0;
 	}
 
-	enumeration__fprintf(tag, conf, fp, false);
+	enumeration__fprintf(tag, conf, fp);
 	fputs(";\n", fp);
 	type_emissions__add_definition(emissions, etype);
 	return 1;
dwarves_fprintf.c +19 -33

164 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/6 Revert "pahole.c: Add --inner_anonymous option"

In reply to Douglas RAILLARD

From: Douglas Raillard <[email protected]>

This reverts commit 6bf42c504b25f7daf9b601655ba1f8ba9ae3dd79.

---
 man-pages/pahole.1 | 7 -------
 pahole.c           | 8 --------
 2 files changed, 15 deletions(-)
man-pages/pahole.1 +0 -7
index 583e8a9..c1ec63e 100644
@@ -122,13 +122,6 @@ Skip COUNT input records.
 Expand class members. Useful to find in what member of inner structs where an
 offset from the beginning of a struct is.
 
-.TP
-.B \-\-inner_anonymous
-Allow making inner structs, enums and unions anonymous, so that when using -E
-to expand types we don't end up with multiple definitions for the expanded
-inner structs/enums/unions, allowing the resulting expanded struct to be
-compilable.
-
 .TP
 .B \-F, \-\-format_path
 Allows specifying a list of debugging formats to try, in order. Right now this
pahole.c +0 -8
index 0f4ca57..f3a51cb 100644
@@ -1127,7 +1127,6 @@ ARGP_PROGRAM_VERSION_HOOK_DEF = dwarves_print_version;
 #define ARGP_skip_encoding_btf_decl_tag 331
 #define ARGP_skip_missing          332
 #define ARGP_skip_encoding_btf_type_tag 333
-#define ARGP_inner_anonymous       334
 
 static const struct argp_option pahole__options[] = {
 	{
@@ -1235,11 +1234,6 @@ static const struct argp_option pahole__options[] = {
 		.key  = 'E',
 		.doc  = "expand class members",
 	},
-	{
-		.name = "inner_anonymous",
-		.key  = ARGP_inner_anonymous,
-		.doc  = "expanded class members are anonymous",
-	},
 	{
 		.name = "nr_members",
 		.key  = 'n',
@@ -1672,8 +1666,6 @@ static error_t pahole__options_parser(int key, char *arg,
 		conf_load.skip_missing = true;          break;
 	case ARGP_skip_encoding_btf_type_tag:
 		conf_load.skip_encoding_btf_type_tag = true;	break;
-	case ARGP_inner_anonymous:
-		conf.inner_anonymous = true;            break;
 	default:
 		return ARGP_ERR_UNKNOWN;
 	}
Signature · 1 line
2.25.1
#3 lore raw

PATCH v2 3/6 fprintf: Print types only once

In reply to Douglas RAILLARD

From: Douglas Raillard <[email protected]>

Use (struct tag).visited member to track what type has been printed already to avoid printing it twice. This allows producing a valid C header along with --expand_types.

---
 dwarves.h         |  3 ++-
 dwarves_fprintf.c | 22 ++++++++++++++++------
 pahole.c          | 34 ++++++++++++++++++----------------
 3 files changed, 36 insertions(+), 23 deletions(-)
dwarves.h +2 -1
index 52d162d..fc5b3fa 100644
@@ -413,6 +413,7 @@ struct tag {
 	type_id_t	 type;
 	uint16_t	 tag;
 	bool		 visited;
+	bool		 printed;
 	bool		 top_level;
 	bool		 has_btf_type_tag;
 	uint16_t	 recursivity_level;
@@ -1370,7 +1371,7 @@ static inline const char *enumerator__name(const struct enumerator *enumerator)
 
 void enumeration__delete(struct type *type);
 void enumeration__add(struct type *type, struct enumerator *enumerator);
-size_t enumeration__fprintf(const struct tag *tag_enum,
+size_t enumeration__fprintf(struct tag *tag_enum,
 			    const struct conf_fprintf *conf, FILE *fp);
 
 int dwarves__init(void);
dwarves_fprintf.c +16 -6
index c5921d7..2c5dce6 100644
@@ -278,8 +278,8 @@ size_t typedef__fprintf(const struct tag *tag, const struct cu *cu,
 {
 	struct type *type = tag__type(tag);
 	const struct conf_fprintf *pconf = conf ?: &conf_fprintf__defaults;
-	const struct tag *tag_type;
-	const struct tag *ptr_type;
+	struct tag *tag_type;
+	struct tag *ptr_type;
 	char bf[512];
 	int is_pointer = 0;
 	size_t printed;
@@ -394,13 +394,14 @@ out:
 	return type->max_tag_name_len;
 }
 
-size_t enumeration__fprintf(const struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
+size_t enumeration__fprintf(struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
 {
 	struct type *type = tag__type(tag);
 	struct enumerator *pos;
 	int max_entry_name_len = enumeration__max_entry_name_len(type);
 	size_t printed = fprintf(fp, "enum%s%s {\n", type__name(type) ? " " : "", type__name(type) ?: "");
 	int indent = conf->indent;
+	tag->printed = 1;
 
 	if (indent >= (int)sizeof(tabs))
 		indent = sizeof(tabs) - 1;
@@ -651,10 +652,14 @@ static size_t type__fprintf(struct tag *type, const struct cu *cu,
 	size_t printed = 0;
 	int expand_types = conf->expand_types;
 	int suppress_offset_comment = conf->suppress_offset_comment;
+	bool type_printed;
 
 	if (type == NULL)
 		goto out_type_not_found;
 
+	type_printed = type->printed;
+	type->printed = 1;
+
 	if (conf->expand_pointers) {
 		int nr_indirections = 0;
 
@@ -794,7 +799,7 @@ print_default:
 	case DW_TAG_structure_type:
 		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
 			printed += fprintf(fp, "%s %-*s %s",
 					   (type->tag == DW_TAG_class_type &&
 					    !tconf.classes_as_structs) ? "class" : "struct",
@@ -813,7 +818,7 @@ print_default:
 	case DW_TAG_union_type:
 		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
 			printed += fprintf(fp, "union %-*s %s", tconf.type_spacing - 6, type__name(ctype), name ?: "");
 		} else {
 			tconf.type_spacing -= 8;
@@ -823,7 +828,7 @@ print_default:
 	case DW_TAG_enumeration_type:
 		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL)
+		if (type__name(ctype) != NULL || type_printed)
 			printed += fprintf(fp, "enum %-*s %s", tconf.type_spacing - 5, type__name(ctype), name ?: "");
 		else
 			printed += enumeration__fprintf(type, &tconf, fp);
@@ -1000,6 +1005,8 @@ static size_t union__fprintf(struct type *type, const struct cu *cu,
 	uint32_t initial_union_cacheline;
 	uint32_t cacheline = 0; /* This will only be used if this is the outermost union */
 
+	type__tag(type)->printed = 1;
+
 	if (indent >= (int)sizeof(tabs))
 		indent = sizeof(tabs) - 1;
 
@@ -1394,6 +1401,8 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu,
 				 type__name(type) ?: "");
 	int indent = cconf.indent;
 
+	class__tag(class)->printed = 1;
+
 	if (indent >= (int)sizeof(tabs))
 		indent = sizeof(tabs) - 1;
 
@@ -1856,6 +1865,7 @@ size_t tag__fprintf(struct tag *tag, const struct cu *cu,
 	size_t printed = 0;
 	struct conf_fprintf tconf;
 	const struct conf_fprintf *pconf = conf;
+	tag->printed = 1;
 
 	if (conf == NULL) {
 		tconf = conf_fprintf__defaults;
pahole.c +18 -16
index f3a51cb..42ba110 100644
@@ -2964,22 +2964,24 @@ out_btf:
 			goto dump_it;
 		}
 
-		if (class)
-			class__find_holes(tag__class(class));
-		if (reorganize) {
-			if (class && tag__is_struct(class))
-				do_reorg(class, cu);
-		} else if (find_containers)
-			print_containers(cu, class_id, 0);
-		else if (find_pointers_in_structs)
-			print_structs_with_pointer_to(cu, class_id);
-		else if (class) {
-			/*
-			 * We don't need to print it for every compile unit
-			 * but the previous options need
-			 */
-			tag__fprintf(class, cu, &conf, stdout);
-			putchar('\n');
+		if (!class->printed) {
+			if (class)
+				class__find_holes(tag__class(class));
+			if (reorganize) {
+				if (class && tag__is_struct(class))
+					do_reorg(class, cu);
+			} else if (find_containers)
+				print_containers(cu, class_id, 0);
+			else if (find_pointers_in_structs)
+				print_structs_with_pointer_to(cu, class_id);
+			else if (class) {
+				/*
+				 * We don't need to print it for every compile unit
+				 * but the previous options need
+				 */
+				tag__fprintf(class, cu, &conf, stdout);
+				putchar('\n');
+			}
 		}
 	}
 
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v2 3/6] fprintf: Print types only once

Em Tue, Dec 07, 2021 at 05:31:48PM +0000, Douglas RAILLARD escreveu:

Quoted 3 lines

From: Douglas Raillard <[email protected]>

Use (struct tag).visited member to track what type has been printed

This doesn't match the patch, where you ended up using a new tar.printed field, have you tried with .visited and ended up finding out .printed was needed?

- Arnaldo

Quoted 177 lines

already to avoid printing it twice. This allows producing a valid C header along with --expand_types.

---
 dwarves.h         |  3 ++-
 dwarves_fprintf.c | 22 ++++++++++++++++------
 pahole.c          | 34 ++++++++++++++++++----------------
 3 files changed, 36 insertions(+), 23 deletions(-)
dwarves.h +2 -1
index 52d162d..fc5b3fa 100644
@@ -413,6 +413,7 @@ struct tag {
 	type_id_t	 type;
 	uint16_t	 tag;
 	bool		 visited;
+	bool		 printed;
 	bool		 top_level;
 	bool		 has_btf_type_tag;
 	uint16_t	 recursivity_level;
@@ -1370,7 +1371,7 @@ static inline const char *enumerator__name(const struct enumerator *enumerator)
 
 void enumeration__delete(struct type *type);
 void enumeration__add(struct type *type, struct enumerator *enumerator);
-size_t enumeration__fprintf(const struct tag *tag_enum,
+size_t enumeration__fprintf(struct tag *tag_enum,
 			    const struct conf_fprintf *conf, FILE *fp);
 
 int dwarves__init(void);
dwarves_fprintf.c +16 -6
index c5921d7..2c5dce6 100644
@@ -278,8 +278,8 @@ size_t typedef__fprintf(const struct tag *tag, const struct cu *cu,
 {
 	struct type *type = tag__type(tag);
 	const struct conf_fprintf *pconf = conf ?: &conf_fprintf__defaults;
-	const struct tag *tag_type;
-	const struct tag *ptr_type;
+	struct tag *tag_type;
+	struct tag *ptr_type;
 	char bf[512];
 	int is_pointer = 0;
 	size_t printed;
@@ -394,13 +394,14 @@ out:
 	return type->max_tag_name_len;
 }
 
-size_t enumeration__fprintf(const struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
+size_t enumeration__fprintf(struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
 {
 	struct type *type = tag__type(tag);
 	struct enumerator *pos;
 	int max_entry_name_len = enumeration__max_entry_name_len(type);
 	size_t printed = fprintf(fp, "enum%s%s {\n", type__name(type) ? " " : "", type__name(type) ?: "");
 	int indent = conf->indent;
+	tag->printed = 1;
 
 	if (indent >= (int)sizeof(tabs))
 		indent = sizeof(tabs) - 1;
@@ -651,10 +652,14 @@ static size_t type__fprintf(struct tag *type, const struct cu *cu,
 	size_t printed = 0;
 	int expand_types = conf->expand_types;
 	int suppress_offset_comment = conf->suppress_offset_comment;
+	bool type_printed;
 
 	if (type == NULL)
 		goto out_type_not_found;
 
+	type_printed = type->printed;
+	type->printed = 1;
+
 	if (conf->expand_pointers) {
 		int nr_indirections = 0;
 
@@ -794,7 +799,7 @@ print_default:
 	case DW_TAG_structure_type:
 		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
 			printed += fprintf(fp, "%s %-*s %s",
 					   (type->tag == DW_TAG_class_type &&
 					    !tconf.classes_as_structs) ? "class" : "struct",
@@ -813,7 +818,7 @@ print_default:
 	case DW_TAG_union_type:
 		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
 			printed += fprintf(fp, "union %-*s %s", tconf.type_spacing - 6, type__name(ctype), name ?: "");
 		} else {
 			tconf.type_spacing -= 8;
@@ -823,7 +828,7 @@ print_default:
 	case DW_TAG_enumeration_type:
 		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL)
+		if (type__name(ctype) != NULL || type_printed)
 			printed += fprintf(fp, "enum %-*s %s", tconf.type_spacing - 5, type__name(ctype), name ?: "");
 		else
 			printed += enumeration__fprintf(type, &tconf, fp);
@@ -1000,6 +1005,8 @@ static size_t union__fprintf(struct type *type, const struct cu *cu,
 	uint32_t initial_union_cacheline;
 	uint32_t cacheline = 0; /* This will only be used if this is the outermost union */
 
+	type__tag(type)->printed = 1;
+
 	if (indent >= (int)sizeof(tabs))
 		indent = sizeof(tabs) - 1;
 
@@ -1394,6 +1401,8 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu,
 				 type__name(type) ?: "");
 	int indent = cconf.indent;
 
+	class__tag(class)->printed = 1;
+
 	if (indent >= (int)sizeof(tabs))
 		indent = sizeof(tabs) - 1;
 
@@ -1856,6 +1865,7 @@ size_t tag__fprintf(struct tag *tag, const struct cu *cu,
 	size_t printed = 0;
 	struct conf_fprintf tconf;
 	const struct conf_fprintf *pconf = conf;
+	tag->printed = 1;
 
 	if (conf == NULL) {
 		tconf = conf_fprintf__defaults;
pahole.c +18 -16
index f3a51cb..42ba110 100644
@@ -2964,22 +2964,24 @@ out_btf:
 			goto dump_it;
 		}
 
-		if (class)
-			class__find_holes(tag__class(class));
-		if (reorganize) {
-			if (class && tag__is_struct(class))
-				do_reorg(class, cu);
-		} else if (find_containers)
-			print_containers(cu, class_id, 0);
-		else if (find_pointers_in_structs)
-			print_structs_with_pointer_to(cu, class_id);
-		else if (class) {
-			/*
-			 * We don't need to print it for every compile unit
-			 * but the previous options need
-			 */
-			tag__fprintf(class, cu, &conf, stdout);
-			putchar('\n');
+		if (!class->printed) {
+			if (class)
+				class__find_holes(tag__class(class));
+			if (reorganize) {
+				if (class && tag__is_struct(class))
+					do_reorg(class, cu);
+			} else if (find_containers)
+				print_containers(cu, class_id, 0);
+			else if (find_pointers_in_structs)
+				print_structs_with_pointer_to(cu, class_id);
+			else if (class) {
+				/*
+				 * We don't need to print it for every compile unit
+				 * but the previous options need
+				 */
+				tag__fprintf(class, cu, &conf, stdout);
+				putchar('\n');
+			}
 		}
 	}
 
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo
#5 lore raw

Re: [PATCH v2 3/6] fprintf: Print types only once

Quoted 9 lines · Arnaldo Carvalho de Melo

Em Tue, Dec 07, 2021 at 05:31:48PM +0000, Douglas RAILLARD escreveu:

Quoted 3 lines

From: Douglas Raillard <[email protected]>

Use (struct tag).visited member to track what type has been printed

This doesn't match the patch, where you ended up using a new tar.printed field, have you tried with .visited and ended up finding out .printed was needed?

Indeed I forgot to update the commit message. I initially used "visited" but decided to add a new member instead as "visited" is already used by other tools for other purposes that should probably not impact printing. We can revert to "visited" easily if needed.

Quoted 180 lines

- Arnaldo

Quoted 177 lines

already to avoid printing it twice. This allows producing a valid C header along with --expand_types.

---
  dwarves.h         |  3 ++-
  dwarves_fprintf.c | 22 ++++++++++++++++------
  pahole.c          | 34 ++++++++++++++++++----------------
  3 files changed, 36 insertions(+), 23 deletions(-)
dwarves.h +2 -1
index 52d162d..fc5b3fa 100644
@@ -413,6 +413,7 @@ struct tag {
  	type_id_t	 type;
  	uint16_t	 tag;
  	bool		 visited;
+	bool		 printed;
  	bool		 top_level;
  	bool		 has_btf_type_tag;
  	uint16_t	 recursivity_level;
@@ -1370,7 +1371,7 @@ static inline const char *enumerator__name(const struct enumerator *enumerator)
 
  void enumeration__delete(struct type *type);
  void enumeration__add(struct type *type, struct enumerator *enumerator);
-size_t enumeration__fprintf(const struct tag *tag_enum,
+size_t enumeration__fprintf(struct tag *tag_enum,
  			    const struct conf_fprintf *conf, FILE *fp);
 
  int dwarves__init(void);
dwarves_fprintf.c +16 -6
index c5921d7..2c5dce6 100644
@@ -278,8 +278,8 @@ size_t typedef__fprintf(const struct tag *tag, const struct cu *cu,
  {
  	struct type *type = tag__type(tag);
  	const struct conf_fprintf *pconf = conf ?: &conf_fprintf__defaults;
-	const struct tag *tag_type;
-	const struct tag *ptr_type;
+	struct tag *tag_type;
+	struct tag *ptr_type;
  	char bf[512];
  	int is_pointer = 0;
  	size_t printed;
@@ -394,13 +394,14 @@ out:
  	return type->max_tag_name_len;
  }
 
-size_t enumeration__fprintf(const struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
+size_t enumeration__fprintf(struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
  {
  	struct type *type = tag__type(tag);
  	struct enumerator *pos;
  	int max_entry_name_len = enumeration__max_entry_name_len(type);
  	size_t printed = fprintf(fp, "enum%s%s {\n", type__name(type) ? " " : "", type__name(type) ?: "");
  	int indent = conf->indent;
+	tag->printed = 1;
 
  	if (indent >= (int)sizeof(tabs))
  		indent = sizeof(tabs) - 1;
@@ -651,10 +652,14 @@ static size_t type__fprintf(struct tag *type, const struct cu *cu,
  	size_t printed = 0;
  	int expand_types = conf->expand_types;
  	int suppress_offset_comment = conf->suppress_offset_comment;
+	bool type_printed;
 
  	if (type == NULL)
  		goto out_type_not_found;
 
+	type_printed = type->printed;
+	type->printed = 1;
+
  	if (conf->expand_pointers) {
  		int nr_indirections = 0;
 
@@ -794,7 +799,7 @@ print_default:
  	case DW_TAG_structure_type:
  		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
  			printed += fprintf(fp, "%s %-*s %s",
  					   (type->tag == DW_TAG_class_type &&
  					    !tconf.classes_as_structs) ? "class" : "struct",
@@ -813,7 +818,7 @@ print_default:
  	case DW_TAG_union_type:
  		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
  			printed += fprintf(fp, "union %-*s %s", tconf.type_spacing - 6, type__name(ctype), name ?: "");
  		} else {
  			tconf.type_spacing -= 8;
@@ -823,7 +828,7 @@ print_default:
  	case DW_TAG_enumeration_type:
  		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL)
+		if (type__name(ctype) != NULL || type_printed)
  			printed += fprintf(fp, "enum %-*s %s", tconf.type_spacing - 5, type__name(ctype), name ?: "");
  		else
  			printed += enumeration__fprintf(type, &tconf, fp);
@@ -1000,6 +1005,8 @@ static size_t union__fprintf(struct type *type, const struct cu *cu,
  	uint32_t initial_union_cacheline;
  	uint32_t cacheline = 0; /* This will only be used if this is the outermost union */
 
+	type__tag(type)->printed = 1;
+
  	if (indent >= (int)sizeof(tabs))
  		indent = sizeof(tabs) - 1;
 
@@ -1394,6 +1401,8 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu,
  				 type__name(type) ?: "");
  	int indent = cconf.indent;
 
+	class__tag(class)->printed = 1;
+
  	if (indent >= (int)sizeof(tabs))
  		indent = sizeof(tabs) - 1;
 
@@ -1856,6 +1865,7 @@ size_t tag__fprintf(struct tag *tag, const struct cu *cu,
  	size_t printed = 0;
  	struct conf_fprintf tconf;
  	const struct conf_fprintf *pconf = conf;
+	tag->printed = 1;
 
  	if (conf == NULL) {
  		tconf = conf_fprintf__defaults;
pahole.c +18 -16
index f3a51cb..42ba110 100644
@@ -2964,22 +2964,24 @@ out_btf:
  			goto dump_it;
  		}
 
-		if (class)
-			class__find_holes(tag__class(class));
-		if (reorganize) {
-			if (class && tag__is_struct(class))
-				do_reorg(class, cu);
-		} else if (find_containers)
-			print_containers(cu, class_id, 0);
-		else if (find_pointers_in_structs)
-			print_structs_with_pointer_to(cu, class_id);
-		else if (class) {
-			/*
-			 * We don't need to print it for every compile unit
-			 * but the previous options need
-			 */
-			tag__fprintf(class, cu, &conf, stdout);
-			putchar('\n');
+		if (!class->printed) {
+			if (class)
+				class__find_holes(tag__class(class));
+			if (reorganize) {
+				if (class && tag__is_struct(class))
+					do_reorg(class, cu);
+			} else if (find_containers)
+				print_containers(cu, class_id, 0);
+			else if (find_pointers_in_structs)
+				print_structs_with_pointer_to(cu, class_id);
+			else if (class) {
+				/*
+				 * We don't need to print it for every compile unit
+				 * but the previous options need
+				 */
+				tag__fprintf(class, cu, &conf, stdout);
+				putchar('\n');
+			}
  		}
  	}
 
Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH v2 3/6] fprintf: Print types only once

In reply to Douglas RAILLARD

Em Tue, Dec 07, 2021 at 05:31:48PM +0000, Douglas RAILLARD escreveu:

Quoted 5 lines

From: Douglas Raillard <[email protected]>

Use (struct tag).visited member to track what type has been printed already to avoid printing it twice. This allows producing a valid C header along with --expand_types.

And here you changed the default, i.e. it is useful to have the current --expand_types behaviour of expanding types as it traverse containers, so I think this needs another option to ask for this new behaviour, I think we can have that as --expand_types_once that would be used instead of --expand_types, ok?

- Arnaldo

Quoted 174 lines
---
 dwarves.h         |  3 ++-
 dwarves_fprintf.c | 22 ++++++++++++++++------
 pahole.c          | 34 ++++++++++++++++++----------------
 3 files changed, 36 insertions(+), 23 deletions(-)
dwarves.h +2 -1
index 52d162d..fc5b3fa 100644
@@ -413,6 +413,7 @@ struct tag {
 	type_id_t	 type;
 	uint16_t	 tag;
 	bool		 visited;
+	bool		 printed;
 	bool		 top_level;
 	bool		 has_btf_type_tag;
 	uint16_t	 recursivity_level;
@@ -1370,7 +1371,7 @@ static inline const char *enumerator__name(const struct enumerator *enumerator)
 
 void enumeration__delete(struct type *type);
 void enumeration__add(struct type *type, struct enumerator *enumerator);
-size_t enumeration__fprintf(const struct tag *tag_enum,
+size_t enumeration__fprintf(struct tag *tag_enum,
 			    const struct conf_fprintf *conf, FILE *fp);
 
 int dwarves__init(void);
dwarves_fprintf.c +16 -6
index c5921d7..2c5dce6 100644
@@ -278,8 +278,8 @@ size_t typedef__fprintf(const struct tag *tag, const struct cu *cu,
 {
 	struct type *type = tag__type(tag);
 	const struct conf_fprintf *pconf = conf ?: &conf_fprintf__defaults;
-	const struct tag *tag_type;
-	const struct tag *ptr_type;
+	struct tag *tag_type;
+	struct tag *ptr_type;
 	char bf[512];
 	int is_pointer = 0;
 	size_t printed;
@@ -394,13 +394,14 @@ out:
 	return type->max_tag_name_len;
 }
 
-size_t enumeration__fprintf(const struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
+size_t enumeration__fprintf(struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
 {
 	struct type *type = tag__type(tag);
 	struct enumerator *pos;
 	int max_entry_name_len = enumeration__max_entry_name_len(type);
 	size_t printed = fprintf(fp, "enum%s%s {\n", type__name(type) ? " " : "", type__name(type) ?: "");
 	int indent = conf->indent;
+	tag->printed = 1;
 
 	if (indent >= (int)sizeof(tabs))
 		indent = sizeof(tabs) - 1;
@@ -651,10 +652,14 @@ static size_t type__fprintf(struct tag *type, const struct cu *cu,
 	size_t printed = 0;
 	int expand_types = conf->expand_types;
 	int suppress_offset_comment = conf->suppress_offset_comment;
+	bool type_printed;
 
 	if (type == NULL)
 		goto out_type_not_found;
 
+	type_printed = type->printed;
+	type->printed = 1;
+
 	if (conf->expand_pointers) {
 		int nr_indirections = 0;
 
@@ -794,7 +799,7 @@ print_default:
 	case DW_TAG_structure_type:
 		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
 			printed += fprintf(fp, "%s %-*s %s",
 					   (type->tag == DW_TAG_class_type &&
 					    !tconf.classes_as_structs) ? "class" : "struct",
@@ -813,7 +818,7 @@ print_default:
 	case DW_TAG_union_type:
 		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
 			printed += fprintf(fp, "union %-*s %s", tconf.type_spacing - 6, type__name(ctype), name ?: "");
 		} else {
 			tconf.type_spacing -= 8;
@@ -823,7 +828,7 @@ print_default:
 	case DW_TAG_enumeration_type:
 		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL)
+		if (type__name(ctype) != NULL || type_printed)
 			printed += fprintf(fp, "enum %-*s %s", tconf.type_spacing - 5, type__name(ctype), name ?: "");
 		else
 			printed += enumeration__fprintf(type, &tconf, fp);
@@ -1000,6 +1005,8 @@ static size_t union__fprintf(struct type *type, const struct cu *cu,
 	uint32_t initial_union_cacheline;
 	uint32_t cacheline = 0; /* This will only be used if this is the outermost union */
 
+	type__tag(type)->printed = 1;
+
 	if (indent >= (int)sizeof(tabs))
 		indent = sizeof(tabs) - 1;
 
@@ -1394,6 +1401,8 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu,
 				 type__name(type) ?: "");
 	int indent = cconf.indent;
 
+	class__tag(class)->printed = 1;
+
 	if (indent >= (int)sizeof(tabs))
 		indent = sizeof(tabs) - 1;
 
@@ -1856,6 +1865,7 @@ size_t tag__fprintf(struct tag *tag, const struct cu *cu,
 	size_t printed = 0;
 	struct conf_fprintf tconf;
 	const struct conf_fprintf *pconf = conf;
+	tag->printed = 1;
 
 	if (conf == NULL) {
 		tconf = conf_fprintf__defaults;
pahole.c +18 -16
index f3a51cb..42ba110 100644
@@ -2964,22 +2964,24 @@ out_btf:
 			goto dump_it;
 		}
 
-		if (class)
-			class__find_holes(tag__class(class));
-		if (reorganize) {
-			if (class && tag__is_struct(class))
-				do_reorg(class, cu);
-		} else if (find_containers)
-			print_containers(cu, class_id, 0);
-		else if (find_pointers_in_structs)
-			print_structs_with_pointer_to(cu, class_id);
-		else if (class) {
-			/*
-			 * We don't need to print it for every compile unit
-			 * but the previous options need
-			 */
-			tag__fprintf(class, cu, &conf, stdout);
-			putchar('\n');
+		if (!class->printed) {
+			if (class)
+				class__find_holes(tag__class(class));
+			if (reorganize) {
+				if (class && tag__is_struct(class))
+					do_reorg(class, cu);
+			} else if (find_containers)
+				print_containers(cu, class_id, 0);
+			else if (find_pointers_in_structs)
+				print_structs_with_pointer_to(cu, class_id);
+			else if (class) {
+				/*
+				 * We don't need to print it for every compile unit
+				 * but the previous options need
+				 */
+				tag__fprintf(class, cu, &conf, stdout);
+				putchar('\n');
+			}
 		}
 	}
 
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo
#7 lore raw

Re: [PATCH v2 3/6] fprintf: Print types only once

Quoted 7 lines · Arnaldo Carvalho de Melo

Em Tue, Dec 07, 2021 at 05:31:48PM +0000, Douglas RAILLARD escreveu:

Quoted 5 lines

From: Douglas Raillard <[email protected]>

Use (struct tag).visited member to track what type has been printed already to avoid printing it twice. This allows producing a valid C header along with --expand_types.

Quoted 5 lines

And here you changed the default, i.e. it is useful to have the current --expand_types behaviour of expanding types as it traverse containers, so I think this needs another option to ask for this new behaviour, I think we can have that as --expand_types_once that would be used instead of --expand_types, ok?

Changed it to have --expand_types_once in the next version I'll send.

Quoted 177 lines

- Arnaldo

Quoted 174 lines
---
  dwarves.h         |  3 ++-
  dwarves_fprintf.c | 22 ++++++++++++++++------
  pahole.c          | 34 ++++++++++++++++++----------------
  3 files changed, 36 insertions(+), 23 deletions(-)
dwarves.h +2 -1
index 52d162d..fc5b3fa 100644
@@ -413,6 +413,7 @@ struct tag {
  	type_id_t	 type;
  	uint16_t	 tag;
  	bool		 visited;
+	bool		 printed;
  	bool		 top_level;
  	bool		 has_btf_type_tag;
  	uint16_t	 recursivity_level;
@@ -1370,7 +1371,7 @@ static inline const char *enumerator__name(const struct enumerator *enumerator)
 
  void enumeration__delete(struct type *type);
  void enumeration__add(struct type *type, struct enumerator *enumerator);
-size_t enumeration__fprintf(const struct tag *tag_enum,
+size_t enumeration__fprintf(struct tag *tag_enum,
  			    const struct conf_fprintf *conf, FILE *fp);
 
  int dwarves__init(void);
dwarves_fprintf.c +16 -6
index c5921d7..2c5dce6 100644
@@ -278,8 +278,8 @@ size_t typedef__fprintf(const struct tag *tag, const struct cu *cu,
  {
  	struct type *type = tag__type(tag);
  	const struct conf_fprintf *pconf = conf ?: &conf_fprintf__defaults;
-	const struct tag *tag_type;
-	const struct tag *ptr_type;
+	struct tag *tag_type;
+	struct tag *ptr_type;
  	char bf[512];
  	int is_pointer = 0;
  	size_t printed;
@@ -394,13 +394,14 @@ out:
  	return type->max_tag_name_len;
  }
 
-size_t enumeration__fprintf(const struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
+size_t enumeration__fprintf(struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
  {
  	struct type *type = tag__type(tag);
  	struct enumerator *pos;
  	int max_entry_name_len = enumeration__max_entry_name_len(type);
  	size_t printed = fprintf(fp, "enum%s%s {\n", type__name(type) ? " " : "", type__name(type) ?: "");
  	int indent = conf->indent;
+	tag->printed = 1;
 
  	if (indent >= (int)sizeof(tabs))
  		indent = sizeof(tabs) - 1;
@@ -651,10 +652,14 @@ static size_t type__fprintf(struct tag *type, const struct cu *cu,
  	size_t printed = 0;
  	int expand_types = conf->expand_types;
  	int suppress_offset_comment = conf->suppress_offset_comment;
+	bool type_printed;
 
  	if (type == NULL)
  		goto out_type_not_found;
 
+	type_printed = type->printed;
+	type->printed = 1;
+
  	if (conf->expand_pointers) {
  		int nr_indirections = 0;
 
@@ -794,7 +799,7 @@ print_default:
  	case DW_TAG_structure_type:
  		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
  			printed += fprintf(fp, "%s %-*s %s",
  					   (type->tag == DW_TAG_class_type &&
  					    !tconf.classes_as_structs) ? "class" : "struct",
@@ -813,7 +818,7 @@ print_default:
  	case DW_TAG_union_type:
  		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
  			printed += fprintf(fp, "union %-*s %s", tconf.type_spacing - 6, type__name(ctype), name ?: "");
  		} else {
  			tconf.type_spacing -= 8;
@@ -823,7 +828,7 @@ print_default:
  	case DW_TAG_enumeration_type:
  		ctype = tag__type(type);
 
-		if (type__name(ctype) != NULL)
+		if (type__name(ctype) != NULL || type_printed)
  			printed += fprintf(fp, "enum %-*s %s", tconf.type_spacing - 5, type__name(ctype), name ?: "");
  		else
  			printed += enumeration__fprintf(type, &tconf, fp);
@@ -1000,6 +1005,8 @@ static size_t union__fprintf(struct type *type, const struct cu *cu,
  	uint32_t initial_union_cacheline;
  	uint32_t cacheline = 0; /* This will only be used if this is the outermost union */
 
+	type__tag(type)->printed = 1;
+
  	if (indent >= (int)sizeof(tabs))
  		indent = sizeof(tabs) - 1;
 
@@ -1394,6 +1401,8 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu,
  				 type__name(type) ?: "");
  	int indent = cconf.indent;
 
+	class__tag(class)->printed = 1;
+
  	if (indent >= (int)sizeof(tabs))
  		indent = sizeof(tabs) - 1;
 
@@ -1856,6 +1865,7 @@ size_t tag__fprintf(struct tag *tag, const struct cu *cu,
  	size_t printed = 0;
  	struct conf_fprintf tconf;
  	const struct conf_fprintf *pconf = conf;
+	tag->printed = 1;
 
  	if (conf == NULL) {
  		tconf = conf_fprintf__defaults;
pahole.c +18 -16
index f3a51cb..42ba110 100644
@@ -2964,22 +2964,24 @@ out_btf:
  			goto dump_it;
  		}
 
-		if (class)
-			class__find_holes(tag__class(class));
-		if (reorganize) {
-			if (class && tag__is_struct(class))
-				do_reorg(class, cu);
-		} else if (find_containers)
-			print_containers(cu, class_id, 0);
-		else if (find_pointers_in_structs)
-			print_structs_with_pointer_to(cu, class_id);
-		else if (class) {
-			/*
-			 * We don't need to print it for every compile unit
-			 * but the previous options need
-			 */
-			tag__fprintf(class, cu, &conf, stdout);
-			putchar('\n');
+		if (!class->printed) {
+			if (class)
+				class__find_holes(tag__class(class));
+			if (reorganize) {
+				if (class && tag__is_struct(class))
+					do_reorg(class, cu);
+			} else if (find_containers)
+				print_containers(cu, class_id, 0);
+			else if (find_pointers_in_structs)
+				print_structs_with_pointer_to(cu, class_id);
+			else if (class) {
+				/*
+				 * We don't need to print it for every compile unit
+				 * but the previous options need
+				 */
+				tag__fprintf(class, cu, &conf, stdout);
+				putchar('\n');
+			}
  		}
  	}
 
Signature · 1 line
2.25.1
#8 lore raw

Re: [PATCH v2 3/6] fprintf: Print types only once

Em Tue, Dec 14, 2021 at 06:23:07PM +0000, Douglas Raillard escreveu:

Quoted 8 lines · Arnaldo Carvalho de Melo
Quoted 7 lines

Em Tue, Dec 07, 2021 at 05:31:48PM +0000, Douglas RAILLARD escreveu:

Quoted 5 lines

From: Douglas Raillard <[email protected]>

Use (struct tag).visited member to track what type has been printed already to avoid printing it twice. This allows producing a valid C header along with --expand_types.

Quoted 7 lines
Quoted 5 lines

And here you changed the default, i.e. it is useful to have the current --expand_types behaviour of expanding types as it traverse containers, so I think this needs another option to ask for this new behaviour, I think we can have that as --expand_types_once that would be used instead of --expand_types, ok?

Changed it to have --expand_types_once in the next version I'll send.

Ok, just one other nit, to make the patch smaller, please just use continue:

pahole.c +3 -0
index f3a51cb2fe74afd2..b8beb71ae2c6a0b0 100644
@@ -2964,6 +2964,9 @@ out_btf:
                        goto dump_it;
                }
 
+               if (class && class->printed)
+                       continue;
+
                if (class)
                        class__find_holes(tag__class(class));
                if (reorganize) {
Quoted 159 lines
Quoted 159 lines

- Arnaldo

Quoted 157 lines
---
  dwarves.h         |  3 ++-
  dwarves_fprintf.c | 22 ++++++++++++++++------
  pahole.c          | 34 ++++++++++++++++++----------------
  3 files changed, 36 insertions(+), 23 deletions(-)
dwarves.h +2 -1
index 52d162d..fc5b3fa 100644
@@ -413,6 +413,7 @@ struct tag {
  	type_id_t	 type;
  	uint16_t	 tag;
  	bool		 visited;
+	bool		 printed;
  	bool		 top_level;
  	bool		 has_btf_type_tag;
  	uint16_t	 recursivity_level;
@@ -1370,7 +1371,7 @@ static inline const char *enumerator__name(const struct enumerator *enumerator)
  void enumeration__delete(struct type *type);
  void enumeration__add(struct type *type, struct enumerator *enumerator);
-size_t enumeration__fprintf(const struct tag *tag_enum,
+size_t enumeration__fprintf(struct tag *tag_enum,
  			    const struct conf_fprintf *conf, FILE *fp);
  int dwarves__init(void);
dwarves_fprintf.c +16 -6
index c5921d7..2c5dce6 100644
@@ -278,8 +278,8 @@ size_t typedef__fprintf(const struct tag *tag, const struct cu *cu,
  {
  	struct type *type = tag__type(tag);
  	const struct conf_fprintf *pconf = conf ?: &conf_fprintf__defaults;
-	const struct tag *tag_type;
-	const struct tag *ptr_type;
+	struct tag *tag_type;
+	struct tag *ptr_type;
  	char bf[512];
  	int is_pointer = 0;
  	size_t printed;
@@ -394,13 +394,14 @@ out:
  	return type->max_tag_name_len;
  }
-size_t enumeration__fprintf(const struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
+size_t enumeration__fprintf(struct tag *tag, const struct conf_fprintf *conf, FILE *fp)
  {
  	struct type *type = tag__type(tag);
  	struct enumerator *pos;
  	int max_entry_name_len = enumeration__max_entry_name_len(type);
  	size_t printed = fprintf(fp, "enum%s%s {\n", type__name(type) ? " " : "", type__name(type) ?: "");
  	int indent = conf->indent;
+	tag->printed = 1;
  	if (indent >= (int)sizeof(tabs))
  		indent = sizeof(tabs) - 1;
@@ -651,10 +652,14 @@ static size_t type__fprintf(struct tag *type, const struct cu *cu,
  	size_t printed = 0;
  	int expand_types = conf->expand_types;
  	int suppress_offset_comment = conf->suppress_offset_comment;
+	bool type_printed;
  	if (type == NULL)
  		goto out_type_not_found;
+	type_printed = type->printed;
+	type->printed = 1;
+
  	if (conf->expand_pointers) {
  		int nr_indirections = 0;
@@ -794,7 +799,7 @@ print_default:
  	case DW_TAG_structure_type:
  		ctype = tag__type(type);
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
  			printed += fprintf(fp, "%s %-*s %s",
  					   (type->tag == DW_TAG_class_type &&
  					    !tconf.classes_as_structs) ? "class" : "struct",
@@ -813,7 +818,7 @@ print_default:
  	case DW_TAG_union_type:
  		ctype = tag__type(type);
-		if (type__name(ctype) != NULL && !expand_types) {
+		if (type__name(ctype) != NULL && (!expand_types || type_printed)) {
  			printed += fprintf(fp, "union %-*s %s", tconf.type_spacing - 6, type__name(ctype), name ?: "");
  		} else {
  			tconf.type_spacing -= 8;
@@ -823,7 +828,7 @@ print_default:
  	case DW_TAG_enumeration_type:
  		ctype = tag__type(type);
-		if (type__name(ctype) != NULL)
+		if (type__name(ctype) != NULL || type_printed)
  			printed += fprintf(fp, "enum %-*s %s", tconf.type_spacing - 5, type__name(ctype), name ?: "");
  		else
  			printed += enumeration__fprintf(type, &tconf, fp);
@@ -1000,6 +1005,8 @@ static size_t union__fprintf(struct type *type, const struct cu *cu,
  	uint32_t initial_union_cacheline;
  	uint32_t cacheline = 0; /* This will only be used if this is the outermost union */
+	type__tag(type)->printed = 1;
+
  	if (indent >= (int)sizeof(tabs))
  		indent = sizeof(tabs) - 1;
@@ -1394,6 +1401,8 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu,
  				 type__name(type) ?: "");
  	int indent = cconf.indent;
+	class__tag(class)->printed = 1;
+
  	if (indent >= (int)sizeof(tabs))
  		indent = sizeof(tabs) - 1;
@@ -1856,6 +1865,7 @@ size_t tag__fprintf(struct tag *tag, const struct cu *cu,
  	size_t printed = 0;
  	struct conf_fprintf tconf;
  	const struct conf_fprintf *pconf = conf;
+	tag->printed = 1;
  	if (conf == NULL) {
  		tconf = conf_fprintf__defaults;
pahole.c +18 -16
index f3a51cb..42ba110 100644
@@ -2964,22 +2964,24 @@ out_btf:
  			goto dump_it;
  		}
-		if (class)
-			class__find_holes(tag__class(class));
-		if (reorganize) {
-			if (class && tag__is_struct(class))
-				do_reorg(class, cu);
-		} else if (find_containers)
-			print_containers(cu, class_id, 0);
-		else if (find_pointers_in_structs)
-			print_structs_with_pointer_to(cu, class_id);
-		else if (class) {
-			/*
-			 * We don't need to print it for every compile unit
-			 * but the previous options need
-			 */
-			tag__fprintf(class, cu, &conf, stdout);
-			putchar('\n');
+		if (!class->printed) {
+			if (class)
+				class__find_holes(tag__class(class));
+			if (reorganize) {
+				if (class && tag__is_struct(class))
+					do_reorg(class, cu);
+			} else if (find_containers)
+				print_containers(cu, class_id, 0);
+			else if (find_pointers_in_structs)
+				print_structs_with_pointer_to(cu, class_id);
+			else if (class) {
+				/*
+				 * We don't need to print it for every compile unit
+				 * but the previous options need
+				 */
+				tag__fprintf(class, cu, &conf, stdout);
+				putchar('\n');
+			}
  		}
  	}
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo
#9 lore raw

PATCH v2 4/6 pahole.c: Add prefix to expanded type names

In reply to Douglas RAILLARD

From: Douglas Raillard <[email protected]>

Add the prefix specified by --expanded_prefix to type names that have not been specificaly requested using -C. This allows manual namespacing so that these inner types will not conflict with existing headers.

---
 dwarves.h |  1 +
 pahole.c  | 29 +++++++++++++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)
dwarves.h +1 -0
index fc5b3fa..0967e5c 100644
@@ -90,6 +90,7 @@ struct conf_load {
  */
 struct conf_fprintf {
 	const char *prefix;
+	const char *name_prefix;
 	const char *suffix;
 	int32_t	   type_spacing;
 	int32_t	   name_spacing;
pahole.c +27 -2
index 42ba110..e0a1438 100644
@@ -2882,6 +2882,33 @@ out_btf:
 
 	bool include_decls = find_pointers_in_structs != 0 || stats_formatter == nr_methods_formatter;
 	struct prototype *prototype, *n;
+	static type_id_t class_id;
+
+	uint32_t id;
+	struct tag *pos;
+	bool skip;
+	const char *prefix = conf_load->conf_fprintf->name_prefix;
+	const size_t prefix_len = prefix ? strlen(prefix) : 0;
+	cu__for_each_type(cu, id, pos) {
+		if (tag__is_type(pos)) {
+			const char *name = type__name(tag__type(pos));
+			if (name && prefix) {
+				skip = false;
+				list_for_each_entry_safe(prototype, n, &class_names, node) {
+					if (!strcmp(prototype->name, name)) {
+						skip = true;
+						break;
+					}
+				}
+				if (!skip) {
+					const size_t len = 1024 + prefix_len;
+					char *bf = malloc(len);
+					snprintf(bf, len, "%s%s", prefix, name);
+					tag__namespace(pos)->name = bf;
+				}
+			}
+		}
+	}
 
 	list_for_each_entry_safe(prototype, n, &class_names, node) {
 
@@ -2891,8 +2918,6 @@ out_btf:
 				prototype->type_enum_resolved = type__find_type_enum(tag__type(prototype->class), cu, prototype->type_enum) == 0;
 			continue;
 		}
-
-		static type_id_t class_id;
 		struct tag *class = cu__find_type_by_name(cu, prototype->name, include_decls, &class_id);
 
 		// couldn't find that class name in this CU, continue to the next one.
Signature · 1 line
2.25.1
#10 lore raw

Re: [PATCH v2 4/6] pahole.c: Add prefix to expanded type names

Em Tue, Dec 07, 2021 at 05:31:49PM +0000, Douglas RAILLARD escreveu:

Quoted 49 lines

From: Douglas Raillard <[email protected]>

Add the prefix specified by --expanded_prefix to type names that have not been specificaly requested using -C. This allows manual namespacing so that these inner types will not conflict with existing headers.

---
 dwarves.h |  1 +
 pahole.c  | 29 +++++++++++++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)
dwarves.h +1 -0
index fc5b3fa..0967e5c 100644
@@ -90,6 +90,7 @@ struct conf_load {
  */
 struct conf_fprintf {
 	const char *prefix;
+	const char *name_prefix;
 	const char *suffix;
 	int32_t	   type_spacing;
 	int32_t	   name_spacing;
pahole.c +17 -0
index 42ba110..e0a1438 100644
@@ -2882,6 +2882,33 @@ out_btf:
 
 	bool include_decls = find_pointers_in_structs != 0 || stats_formatter == nr_methods_formatter;
 	struct prototype *prototype, *n;
+	static type_id_t class_id;
+
+	uint32_t id;
+	struct tag *pos;
+	bool skip;
+	const char *prefix = conf_load->conf_fprintf->name_prefix;
+	const size_t prefix_len = prefix ? strlen(prefix) : 0;
+	cu__for_each_type(cu, id, pos) {
+		if (tag__is_type(pos)) {
+			const char *name = type__name(tag__type(pos));
+			if (name && prefix) {
+				skip = false;
+				list_for_each_entry_safe(prototype, n, &class_names, node) {
+					if (!strcmp(prototype->name, name)) {
+						skip = true;
+						break;
+					}

pahole.c out_btf: · line 2911

Quoted 10 lines
pahole.c +10 -0
+				}
+				if (!skip) {
+					const size_t len = 1024 + prefix_len;
+					char *bf = malloc(len);
+					snprintf(bf, len, "%s%s", prefix, name);
+					tag__namespace(pos)->name = bf;
+				}
+			}
+		}
+	}

I don't like this change in place mode, but I understand it is easier to do it here instead of in all types fprintf routines, but perhaps its better that way, I'll check how big it would be.

Quoted 14 lines
pahole.c +0 -2
 	list_for_each_entry_safe(prototype, n, &class_names, node) {
 
@@ -2891,8 +2918,6 @@ out_btf:
 				prototype->type_enum_resolved = type__find_type_enum(tag__type(prototype->class), cu, prototype->type_enum) == 0;
 			continue;
 		}
-
-		static type_id_t class_id;
 		struct tag *class = cu__find_type_by_name(cu, prototype->name, include_decls, &class_id);
 
 		// couldn't find that class name in this CU, continue to the next one.
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo
#11 lore raw

Re: [PATCH v2 4/6] pahole.c: Add prefix to expanded type names

Quoted 56 lines · Arnaldo Carvalho de Melo

Em Tue, Dec 07, 2021 at 05:31:49PM +0000, Douglas RAILLARD escreveu:

Quoted 55 lines

From: Douglas Raillard <[email protected]>

Add the prefix specified by --expanded_prefix to type names that have not been specificaly requested using -C. This allows manual namespacing so that these inner types will not conflict with existing headers.

---
  dwarves.h |  1 +
  pahole.c  | 29 +++++++++++++++++++++++++++--
  2 files changed, 28 insertions(+), 2 deletions(-)
dwarves.h +1 -0
index fc5b3fa..0967e5c 100644
@@ -90,6 +90,7 @@ struct conf_load {
   */
  struct conf_fprintf {
  	const char *prefix;
+	const char *name_prefix;
  	const char *suffix;
  	int32_t	   type_spacing;
  	int32_t	   name_spacing;
pahole.c +23 -0
index 42ba110..e0a1438 100644
@@ -2882,6 +2882,33 @@ out_btf:
 
  	bool include_decls = find_pointers_in_structs != 0 || stats_formatter == nr_methods_formatter;
  	struct prototype *prototype, *n;
+	static type_id_t class_id;
+
+	uint32_t id;
+	struct tag *pos;
+	bool skip;
+	const char *prefix = conf_load->conf_fprintf->name_prefix;
+	const size_t prefix_len = prefix ? strlen(prefix) : 0;
+	cu__for_each_type(cu, id, pos) {
+		if (tag__is_type(pos)) {
+			const char *name = type__name(tag__type(pos));
+			if (name && prefix) {
+				skip = false;
+				list_for_each_entry_safe(prototype, n, &class_names, node) {
+					if (!strcmp(prototype->name, name)) {
+						skip = true;
+						break;
+					}
+				}
+				if (!skip) {
+					const size_t len = 1024 + prefix_len;
+					char *bf = malloc(len);
+					snprintf(bf, len, "%s%s", prefix, name);
+					tag__namespace(pos)->name = bf;
Quoted 8 lines

pahole.c out_btf: · line 2909

Quoted 4 lines
pahole.c +4 -0
+				}
+			}
+		}
+	}

I don't like this change in place mode, but I understand it is easier to do it here instead of in all types fprintf routines, but perhaps its better that way, I'll check how big it would be.

I don't really like it either but the alternative is something like that:
https://github.com/douglas-raillard-arm/pahole/commit/a3d4e224b2a0cb8196db0f8536bd77f976e364ca

The worst is not the size of the patch, it's the inability to check that it's actually complete. Any spot where the prefix should be applied and where it is not will result in a broken header. This also stands for any future addition to dwarves_fprintf.c.

One way to make the substitution safer is to completely ban direct access to the name and force to go through an accessor that would apply the prefix, but this leads to either memory management issue or inefficiency (as we would need to return copies so the caller can free it).

Quoted 16 lines
Quoted 15 lines
pahole.c +0 -2
  	list_for_each_entry_safe(prototype, n, &class_names, node) {
 
@@ -2891,8 +2918,6 @@ out_btf:
  				prototype->type_enum_resolved = type__find_type_enum(tag__type(prototype->class), cu, prototype->type_enum) == 0;
  			continue;
  		}
-
-		static type_id_t class_id;
  		struct tag *class = cu__find_type_by_name(cu, prototype->name, include_decls, &class_id);
 
  		// couldn't find that class name in this CU, continue to the next one.
Signature · 1 line
2.25.1
#12 lore raw

Re: [PATCH v2 4/6] pahole.c: Add prefix to expanded type names

Quoted 69 lines · Douglas Raillard
Quoted 56 lines · Arnaldo Carvalho de Melo

Em Tue, Dec 07, 2021 at 05:31:49PM +0000, Douglas RAILLARD escreveu:

Quoted 55 lines

From: Douglas Raillard <[email protected]>

Add the prefix specified by --expanded_prefix to type names that have not been specificaly requested using -C. This allows manual namespacing so that these inner types will not conflict with existing headers.

---
  dwarves.h |  1 +
  pahole.c  | 29 +++++++++++++++++++++++++++--
  2 files changed, 28 insertions(+), 2 deletions(-)
dwarves.h +1 -0
index fc5b3fa..0967e5c 100644
@@ -90,6 +90,7 @@ struct conf_load {
   */
  struct conf_fprintf {
  	const char *prefix;
+	const char *name_prefix;
  	const char *suffix;
  	int32_t	   type_spacing;
  	int32_t	   name_spacing;
pahole.c +23 -0
index 42ba110..e0a1438 100644
@@ -2882,6 +2882,33 @@ out_btf:
 
  	bool include_decls = find_pointers_in_structs != 0 || stats_formatter == nr_methods_formatter;
  	struct prototype *prototype, *n;
+	static type_id_t class_id;
+
+	uint32_t id;
+	struct tag *pos;
+	bool skip;
+	const char *prefix = conf_load->conf_fprintf->name_prefix;
+	const size_t prefix_len = prefix ? strlen(prefix) : 0;
+	cu__for_each_type(cu, id, pos) {
+		if (tag__is_type(pos)) {
+			const char *name = type__name(tag__type(pos));
+			if (name && prefix) {
+				skip = false;
+				list_for_each_entry_safe(prototype, n, &class_names, node) {
+					if (!strcmp(prototype->name, name)) {
+						skip = true;
+						break;
+					}
+				}
+				if (!skip) {
+					const size_t len = 1024 + prefix_len;
+					char *bf = malloc(len);
+					snprintf(bf, len, "%s%s", prefix, name);
+					tag__namespace(pos)->name = bf;
Quoted 8 lines

pahole.c out_btf: · line 2909

Quoted 4 lines
pahole.c +4 -0
+				}
+			}
+		}
+	}

I don't like this change in place mode, but I understand it is easier to do it here instead of in all types fprintf routines, but perhaps its better that way, I'll check how big it would be.

I don't really like it either but the alternative is something like that:
https://github.com/douglas-raillard-arm/pahole/commit/a3d4e224b2a0cb8196db0f8536bd77f976e364ca

Quoted 6 lines

The worst is not the size of the patch, it's the inability to check that it's actually complete. Any spot where the prefix should be applied and where it is not will result in a broken header. This also stands for any future addition to dwarves_fprintf.c.

One way to make the substitution safer is to completely ban direct access to the name and force to go through an accessor that would apply the prefix,

I like this, and we have tag__name(), I'm AFK now, will check if it fits.

Looking at your patch in te link you just provided

- Arnaldo

Quoted 20 lines

but this leads to either memory management
issue or inefficiency (as we would need to return copies so the caller can free it).

Quoted 16 lines
Quoted 15 lines
pahole.c +0 -2
  	list_for_each_entry_safe(prototype, n, &class_names, node) {
 
@@ -2891,8 +2918,6 @@ out_btf:
  				prototype->type_enum_resolved = type__find_type_enum(tag__type(prototype->class), cu, prototype->type_enum) == 0;
  			continue;
  		}
-
-		static type_id_t class_id;
  		struct tag *class = cu__find_type_by_name(cu, prototype->name, include_decls, &class_id);
 
  		// couldn't find that class name in this CU, continue to the next one.
Signature · 1 line
2.25.1
#13 lore raw

Re: [PATCH v2 4/6] pahole.c: Add prefix to expanded type names

In reply to Douglas Raillard
Quoted 66 lines · Douglas Raillard
Quoted 56 lines · Arnaldo Carvalho de Melo

Em Tue, Dec 07, 2021 at 05:31:49PM +0000, Douglas RAILLARD escreveu:

Quoted 55 lines

From: Douglas Raillard <[email protected]>

Add the prefix specified by --expanded_prefix to type names that have not been specificaly requested using -C. This allows manual namespacing so that these inner types will not conflict with existing headers.

---
  dwarves.h |  1 +
  pahole.c  | 29 +++++++++++++++++++++++++++--
  2 files changed, 28 insertions(+), 2 deletions(-)
dwarves.h +1 -0
index fc5b3fa..0967e5c 100644
@@ -90,6 +90,7 @@ struct conf_load {
   */
  struct conf_fprintf {
  	const char *prefix;
+	const char *name_prefix;
  	const char *suffix;
  	int32_t	   type_spacing;
  	int32_t	   name_spacing;
pahole.c +23 -0
index 42ba110..e0a1438 100644
@@ -2882,6 +2882,33 @@ out_btf:
 
  	bool include_decls = find_pointers_in_structs != 0 || stats_formatter == nr_methods_formatter;
  	struct prototype *prototype, *n;
+	static type_id_t class_id;
+
+	uint32_t id;
+	struct tag *pos;
+	bool skip;
+	const char *prefix = conf_load->conf_fprintf->name_prefix;
+	const size_t prefix_len = prefix ? strlen(prefix) : 0;
+	cu__for_each_type(cu, id, pos) {
+		if (tag__is_type(pos)) {
+			const char *name = type__name(tag__type(pos));
+			if (name && prefix) {
+				skip = false;
+				list_for_each_entry_safe(prototype, n, &class_names, node) {
+					if (!strcmp(prototype->name, name)) {
+						skip = true;
+						break;
+					}
+				}
+				if (!skip) {
+					const size_t len = 1024 + prefix_len;
+					char *bf = malloc(len);
+					snprintf(bf, len, "%s%s", prefix, name);
+					tag__namespace(pos)->name = bf;
Quoted 8 lines

pahole.c out_btf: · line 2909

Quoted 4 lines
pahole.c +4 -0
+				}
+			}
+		}
+	}

I don't like this change in place mode, but I understand it is easier to do it here instead of in all types fprintf routines, but perhaps its better that way, I'll check how big it would be.

Quoted 2 lines

I don't really like it either but the alternative is something like that:
https://github.com/douglas-raillard-arm/pahole/commit/a3d4e224b2a0cb8196db0f8536bd77f976e364ca

A quick look at the above hints at passing con_fprintf + a buffer + size to either return the type name or use the buffer to concatenate the prefix + the type name, with the type_name() return being the only result to use.

- Arnaldo

Quoted 26 lines

The worst is not the size of the patch, it's the inability to check that it's actually complete. Any spot where the prefix should be applied and where it is not will result in a broken header. This also stands for any future addition to dwarves_fprintf.c.

One way to make the substitution safer is to completely ban direct access to the name and force to go through an accessor that would apply the prefix, but this leads to either memory management issue or inefficiency (as we would need to return copies so the caller can free it).

Quoted 16 lines
Quoted 15 lines
pahole.c +0 -2
  	list_for_each_entry_safe(prototype, n, &class_names, node) {
 
@@ -2891,8 +2918,6 @@ out_btf:
  				prototype->type_enum_resolved = type__find_type_enum(tag__type(prototype->class), cu, prototype->type_enum) == 0;
  			continue;
  		}
-
-		static type_id_t class_id;
  		struct tag *class = cu__find_type_by_name(cu, prototype->name, include_decls, &class_id);
 
  		// couldn't find that class name in this CU, continue to the next one.
Signature · 1 line
2.25.1
#14 lore raw

PATCH v2 5/6 pahole.c: Add --expanded_prefix option

In reply to Douglas RAILLARD

From: Douglas Raillard <[email protected]>

Allow prefixing type names and enum members with an arbitrary string as a way of creating a manual namespace for additional types displayed with --expand_types.

---
 man-pages/pahole.1 | 5 +++++
 pahole.c           | 9 +++++++++
 2 files changed, 14 insertions(+)
man-pages/pahole.1 +5 -0
index c1ec63e..f845e24 100644
@@ -122,6 +122,11 @@ Skip COUNT input records.
 Expand class members. Useful to find in what member of inner structs where an
 offset from the beginning of a struct is.
 
+.TP
+.B \-\-expanded_prefix
+Add prefix to type names that are not specified using --class_name. This helps
+avoiding conflicts with existing headers in types expanded by --expand_types.
+
 .TP
 .B \-F, \-\-format_path
 Allows specifying a list of debugging formats to try, in order. Right now this
pahole.c +9 -0
index e0a1438..e828c31 100644
@@ -1127,6 +1127,7 @@ ARGP_PROGRAM_VERSION_HOOK_DEF = dwarves_print_version;
 #define ARGP_skip_encoding_btf_decl_tag 331
 #define ARGP_skip_missing          332
 #define ARGP_skip_encoding_btf_type_tag 333
+#define ARGP_expanded_prefix 334
 
 static const struct argp_option pahole__options[] = {
 	{
@@ -1234,6 +1235,12 @@ static const struct argp_option pahole__options[] = {
 		.key  = 'E',
 		.doc  = "expand class members",
 	},
+	{
+		.name = "expanded_prefix",
+		.key  = ARGP_expanded_prefix,
+		.arg  = "NAME_PREFIX",
+		.doc  = "Add prefix to all the nested types displayed with --expand_types",
+	},
 	{
 		.name = "nr_members",
 		.key  = 'n',
@@ -1666,6 +1673,8 @@ static error_t pahole__options_parser(int key, char *arg,
 		conf_load.skip_missing = true;          break;
 	case ARGP_skip_encoding_btf_type_tag:
 		conf_load.skip_encoding_btf_type_tag = true;	break;
+	case ARGP_expanded_prefix:
+		conf.name_prefix = arg;                 break;
 	default:
 		return ARGP_ERR_UNKNOWN;
 	}
Signature · 1 line
2.25.1
#15 lore raw

PATCH v2 6/6 pahole.c: Add --forward_decl option

In reply to Douglas RAILLARD

From: Douglas Raillard <[email protected]>

Print forward declaration for types supporting it. This allows creating a self-contained header where it is guaranteed that union and struct pointers will compile correctly regardless of their declaration order.

---
 dwarves.h         |  4 ++++
 dwarves_fprintf.c |  5 +----
 pahole.c          | 22 ++++++++++++++++++++++
 3 files changed, 27 insertions(+), 4 deletions(-)
dwarves.h +4 -0
index 0967e5c..3f4b31b 100644
@@ -1046,6 +1046,10 @@ struct type {
 
 void __type__init(struct type *type);
 
+size_t type__fprintf(struct tag *type, const struct cu *cu,
+		     const char *name, const struct conf_fprintf *conf,
+		     FILE *fp);
+
 size_t tag__natural_alignment(struct tag *tag, const struct cu *cu);
 
 static inline struct class *type__class(const struct type *type)
dwarves_fprintf.c +1 -4
index 2c5dce6..c116184 100644
@@ -199,9 +199,6 @@ size_t tag__fprintf_decl_info(const struct tag *tag,
 
 static size_t __class__fprintf(struct class *class, const struct cu *cu,
 			       const struct conf_fprintf *conf, FILE *fp);
-static size_t type__fprintf(struct tag *type, const struct cu *cu,
-			    const char *name, const struct conf_fprintf *conf,
-			    FILE *fp);
 
 static size_t array_type__fprintf(const struct tag *tag,
 				  const struct cu *cu, const char *name,
@@ -637,7 +634,7 @@ static size_t type__fprintf_stats(struct type *type, const struct cu *cu,
 static size_t union__fprintf(struct type *type, const struct cu *cu,
 			     const struct conf_fprintf *conf, FILE *fp);
 
-static size_t type__fprintf(struct tag *type, const struct cu *cu,
+size_t type__fprintf(struct tag *type, const struct cu *cu,
 			    const char *name, const struct conf_fprintf *conf,
 			    FILE *fp)
 {
pahole.c +22 -0
index e828c31..73579f5 100644
@@ -59,6 +59,8 @@ static size_t cu__exclude_prefix_len;
 static char *decl_exclude_prefix;
 static size_t decl_exclude_prefix_len;
 
+static bool print_forward_decl;
+
 static uint16_t nr_holes;
 static uint16_t nr_bit_holes;
 static uint16_t hole_size_ge;
@@ -1128,6 +1130,7 @@ ARGP_PROGRAM_VERSION_HOOK_DEF = dwarves_print_version;
 #define ARGP_skip_missing          332
 #define ARGP_skip_encoding_btf_type_tag 333
 #define ARGP_expanded_prefix 334
+#define ARGP_forward_decl 335
 
 static const struct argp_option pahole__options[] = {
 	{
@@ -1241,6 +1244,11 @@ static const struct argp_option pahole__options[] = {
 		.arg  = "NAME_PREFIX",
 		.doc  = "Add prefix to all the nested types displayed with --expand_types",
 	},
+	{
+		.name = "forward_decl",
+		.key  = ARGP_forward_decl,
+		.doc  = "Print a forward declaration for types that support it",
+	},
 	{
 		.name = "nr_members",
 		.key  = 'n',
@@ -1675,6 +1683,8 @@ static error_t pahole__options_parser(int key, char *arg,
 		conf_load.skip_encoding_btf_type_tag = true;	break;
 	case ARGP_expanded_prefix:
 		conf.name_prefix = arg;                 break;
+	case ARGP_forward_decl:
+		print_forward_decl = true;                 break;
 	default:
 		return ARGP_ERR_UNKNOWN;
 	}
@@ -2898,6 +2908,10 @@ out_btf:
 	bool skip;
 	const char *prefix = conf_load->conf_fprintf->name_prefix;
 	const size_t prefix_len = prefix ? strlen(prefix) : 0;
+
+	struct conf_fprintf pconf = *conf_load->conf_fprintf;
+	pconf.expand_types = false;
+
 	cu__for_each_type(cu, id, pos) {
 		if (tag__is_type(pos)) {
 			const char *name = type__name(tag__type(pos));
@@ -2917,6 +2931,14 @@ out_btf:
 				}
 			}
 		}
+		if (print_forward_decl && (tag__is_union(pos) || tag__is_struct(pos))) {
+			const char *name = type__name(tag__type(pos));
+			if (name) {
+				type__fprintf(pos, cu, NULL, &pconf, stdout);
+				pos->printed = false;
+				printf(";\n");
+			}
+		}
 	}
 
 	list_for_each_entry_safe(prototype, n, &class_names, node) {
Signature · 1 line
2.25.1
#16 lore raw
In reply to Douglas RAILLARD

Em Tue, Dec 07, 2021 at 05:31:45PM +0000, Douglas RAILLARD escreveu:

Quoted 6 lines

From: Douglas Raillard <[email protected]>

Improve headers generated with -E in a few ways:

* Print each type only once, to avoid redefinitions

Quoted 8 lines

* --forward_decl: Generate forward declaration for struct and union so that uses of pointers will work.

* --expanded_prefix: Allow prefixing names of types added by -E (all the ones not explicitly requested with -C) so they can be namespaced manually. This is important in order to be able to mix pahole-generated headers with existing headers without clash or maintenance.

Looks good from a very quick first view, I'll cut 1.23 today as the BPF guys need a release with the new BTF tag feature and then will review this patchset.

So I've applied so far only the reverts, since yesterday I had flipped next to master.

- Arnaldo

Quoted 83 lines

types.txt:

t1
t4

Original printed type with "-C types.txt -E":

  struct t1 {
    struct t2 *a;
    struct t3 {
      int a;
    } b;
  };

  struct t4 {
    struct t3 {
      int a;
    } a;
  };

After this series and with
"-C types.txt -E --expanded_prefix __pahole --forward_decl":

struct t1
struct t2;
struct t3;

  struct t1 {
    struct t2 *a;
    struct __pahole_t3 {
      int a;
    } b;
  };

  struct t4 {
    struct __pahole_t3 a;
  };

This header can be freely mixed with any other header as long as they don't define t1 or t4 (if there is a clash, then pahole is not necessary and the user can just use the header in the first place).

TODOs:

* Define types on their first use even when they are first used as pointers. Currently, if a type is only used as pointer it will never get defined, which will still compile thanks to --forward_decl but won't allow easy use of the values.

* The prefix system allocates memory to rename types and never frees it. I don't think it's a big issue for now as it's done inside pahole.c and type names are likely necessary until the end of the process anyway, but it makes valgrind angry and could therefore mask other problems. There is another implementation of prefix that modifies dwarves_fprintf.c instead of changing the name but it was very invasive and impossible to check if prefixable spots were missed.

This series is to be applied on the "next" branch as it includes reverts of the --inner_anonymous series, which is dropped since it could not cope with recursive types.

Douglas Raillard (6):
Revert "fprintf: Allow making struct/enum/union anonymous" Revert "pahole.c: Add --inner_anonymous option"
fprintf: Print types only once
pahole.c: Add prefix to expanded type names pahole.c: Add --expanded_prefix option
pahole.c: Add --forward_decl option

 dwarves.h          | 12 ++++--
 dwarves_emit.c     |  2 +-
 dwarves_fprintf.c  | 77 +++++++++++++++++-------------------
 man-pages/pahole.1 |  8 ++--
 pahole.c           | 98 ++++++++++++++++++++++++++++++++++------------
 5 files changed, 121 insertions(+), 76 deletions(-)
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo