Skip to contentSkip to conversation

bpftool: Add generating command to C dumped file.

RFC PATCH In review Francis Laniel 2 participants 4 messages Jul 12, 2022 – Jul 13, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover bpftool: Add generating command to C dumped file.

Hi.

First, I hope you are fine and the same for your relatives.

In this patch, I added the command used to generate a BTF dump at the top of the dump when outputting to C:
/*
* File generated by bpftool using:
* bpftool btf dump file /sys/kernel/btf/vmlinux format c
* DO NOT EDIT.
*/

The goal of this is to first warn users this file must not be edited and also to document the command used to get it.
The idea was gathered from a message posted on iovisor/bcc repository and from message written by bpf2go when it generates a file [1, 2].

This patch is clearly not a big change which impacts the future of bpftool but I think it could be welcomed.
If you see any way to improve it or have any question, feel free to ask.

Francis Laniel (1):
bpftool: Add generating command to dumped file.

 tools/bpf/bpftool/btf.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

Best regards and thank you in advance.
---
[1] https://github.com/iovisor/bcc/pull/4088#pullrequestreview-1032543916
[2] https://github.com/cilium/ebpf/blob/951bb28908d23e50fca063a2d51098ca028352bf/cmd/bpf2go/output.go#L21

Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/1 bpftool: Add generating command to C dumped file.

This commit adds the following lines to file generated by dump:
/*
* File generated by bpftool using:
* bpftool btf dump file /sys/kernel/btf/vmlinux format c
* DO NOT EDIT.
*/
This warns users to not edit the file and documents the command used to generate the file.

---
 tools/bpf/bpftool/btf.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
tools/bpf/bpftool/btf.c +14 -2
index 7e6accb9d9f7..eecfc27370c3 100644
@@ -415,7 +415,8 @@ static void __printf(2, 0) btf_dump_printf(void *ctx,
 }
 
 static int dump_btf_c(const struct btf *btf,
-		      __u32 *root_type_ids, int root_type_cnt)
+		      __u32 *root_type_ids, int root_type_cnt,
+		      int argc, char **argv)
 {
 	struct btf_dump *d;
 	int err = 0, i;
@@ -425,6 +426,14 @@ static int dump_btf_c(const struct btf *btf,
 	if (err)
 		return err;
 
+	printf("/*\n");
+	printf(" * File generated by bpftool using:\n");
+	printf(" * bpftool btf dump");
+	for (i = 0; i < argc; i++)
+		printf(" %s", argv[i]);
+	printf("\n");
+	printf(" * DO NOT EDIT.\n");
+	printf(" */\n");
 	printf("#ifndef __VMLINUX_H__\n");
 	printf("#define __VMLINUX_H__\n");
 	printf("\n");
@@ -507,8 +516,10 @@ static bool btf_is_kernel_module(__u32 btf_id)
 static int do_dump(int argc, char **argv)
 {
 	struct btf *btf = NULL, *base = NULL;
+	char **orig_argv = argv;
 	__u32 root_type_ids[2];
 	int root_type_cnt = 0;
+	int orig_argc = argc;
 	bool dump_c = false;
 	__u32 btf_id = -1;
 	const char *src;
@@ -649,7 +660,8 @@ static int do_dump(int argc, char **argv)
 			err = -ENOTSUP;
 			goto done;
 		}
-		err = dump_btf_c(btf, root_type_ids, root_type_cnt);
+		err = dump_btf_c(btf, root_type_ids, root_type_cnt,
+				 orig_argc, orig_argv);
 	} else {
 		err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
 	}
Signature · 1 line
2.25.1
#2 lore raw
Quoted 35 lines · Francis Laniel

This commit adds the following lines to file generated by dump:
/*
* File generated by bpftool using:
* bpftool btf dump file /sys/kernel/btf/vmlinux format c
* DO NOT EDIT.
*/
This warns users to not edit the file and documents the command used to generate the file.

---
  tools/bpf/bpftool/btf.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
tools/bpf/bpftool/btf.c +5 -1
index 7e6accb9d9f7..eecfc27370c3 100644
@@ -415,7 +415,8 @@ static void __printf(2, 0) btf_dump_printf(void *ctx,
  }
 
  static int dump_btf_c(const struct btf *btf,
-		      __u32 *root_type_ids, int root_type_cnt)
+		      __u32 *root_type_ids, int root_type_cnt,
+		      int argc, char **argv)
  {
  	struct btf_dump *d;
  	int err = 0, i;
@@ -425,6 +426,14 @@ static int dump_btf_c(const struct btf *btf,
  	if (err)
  		return err;
 
+	printf("/*\n");
+	printf(" * File generated by bpftool using:\n");
+	printf(" * bpftool btf dump");

[..]

tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 433

Quoted 2 lines
tools/bpf/bpftool/btf.c +2 -0
+	for (i = 0; i < argc; i++)
+		printf(" %s", argv[i]);

Do we really need that complexity to preserve the arguments? For skeletons we're simply doing:

	/* THIS FILE IS AUTOGENERATED BY BPFTOOL! */

So probably the same should be fine here?

Also, while at it, might be worth adding SPDX license comment? So let's align with whatever we have in gen.c ?

Quoted 29 lines
tools/bpf/bpftool/btf.c +7 -2
+	printf("\n");
+	printf(" * DO NOT EDIT.\n");
+	printf(" */\n");
  	printf("#ifndef __VMLINUX_H__\n");
  	printf("#define __VMLINUX_H__\n");
  	printf("\n");
@@ -507,8 +516,10 @@ static bool btf_is_kernel_module(__u32 btf_id)
  static int do_dump(int argc, char **argv)
  {
  	struct btf *btf = NULL, *base = NULL;
+	char **orig_argv = argv;
  	__u32 root_type_ids[2];
  	int root_type_cnt = 0;
+	int orig_argc = argc;
  	bool dump_c = false;
  	__u32 btf_id = -1;
  	const char *src;
@@ -649,7 +660,8 @@ static int do_dump(int argc, char **argv)
  			err = -ENOTSUP;
  			goto done;
  		}
-		err = dump_btf_c(btf, root_type_ids, root_type_cnt);
+		err = dump_btf_c(btf, root_type_ids, root_type_cnt,
+				 orig_argc, orig_argv);
  	} else {
  		err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
  	}
--

2.25.1

#3 lore raw

Hi.

Quoted 49 lines · Le mardi 12 juillet 2022, 22:47:40 CEST [email protected] a écrit :
Quoted 45 lines · Francis Laniel

This commit adds the following lines to file generated by dump:
/*

* File generated by bpftool using:
* bpftool btf dump file /sys/kernel/btf/vmlinux format c
* DO NOT EDIT.
*/

This warns users to not edit the file and documents the command used to generate the file.

---

  tools/bpf/bpftool/btf.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
tools/bpf/bpftool/btf.c +2 -1
index 7e6accb9d9f7..eecfc27370c3 100644
@@ -415,7 +415,8 @@ static void __printf(2, 0) btf_dump_printf(void *ctx,
 
  }
 
  static int dump_btf_c(const struct btf *btf,
 
-		      __u32 *root_type_ids, int root_type_cnt)
+		      __u32 *root_type_ids, int root_type_cnt,
+		      int argc, char **argv)
 
  {
  
  	struct btf_dump *d;
  	int err = 0, i;
tools/bpf/bpftool/btf.c +3 -0
@@ -425,6 +426,14 @@ static int dump_btf_c(const struct btf *btf,
 
  	if (err)
 
  		return err;
 
+	printf("/*\n");
+	printf(" * File generated by bpftool using:\n");
+	printf(" * bpftool btf dump");

[..]

Quoted 4 lines

tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 433

Quoted 2 lines
tools/bpf/bpftool/btf.c +2 -0
+	for (i = 0; i < argc; i++)
+		printf(" %s", argv[i]);

Do we really need that complexity to preserve the arguments?

I was also a bit sceptickal when I first wrote as I found this code a bit complex for not so much added value.
But in my case, I do not use bpftool often, so having the command documented in the generated file would have been useful.
I will just find another way to document (or I think now I will not forget it anymore since this series).

Quoted 8 lines

For skeletons we're simply doing:

	/* THIS FILE IS AUTOGENERATED BY BPFTOOL! */

So probably the same should be fine here?

Also, while at it, might be worth adding SPDX license comment? So let's align with whatever we have in gen.c ?

I will send a v2 aligned on skeletons in no more than 15 minutes.

Quoted 37 lines
Quoted 37 lines
tools/bpf/bpftool/btf.c +7 -1
+	printf("\n");
+	printf(" * DO NOT EDIT.\n");
+	printf(" */\n");
 
  	printf("#ifndef __VMLINUX_H__\n");
  	printf("#define __VMLINUX_H__\n");
  	printf("\n");
@@ -507,8 +516,10 @@ static bool btf_is_kernel_module(__u32 btf_id)
 
  static int do_dump(int argc, char **argv)
  {
 
  	struct btf *btf = NULL, *base = NULL;
 
+	char **orig_argv = argv;
 
  	__u32 root_type_ids[2];
  	int root_type_cnt = 0;
 
+	int orig_argc = argc;
 
  	bool dump_c = false;
  	__u32 btf_id = -1;
  	const char *src;
@@ -649,7 +660,8 @@ static int do_dump(int argc, char **argv)
 
  			err = -ENOTSUP;
  			goto done;
 
  		}
 
-		err = dump_btf_c(btf, root_type_ids, root_type_cnt);
+		err = dump_btf_c(btf, root_type_ids, root_type_cnt,
+				 orig_argc, orig_argv);
Quoted 8 lines
Quoted 8 lines
  	} else {
  	
  		err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
  	
  	}

--
2.25.1

Best regards.