scripts/bpf_doc.py: implement json output for helpers
PATCH 1/1 scripts/bpf_doc.py: implement json output for helpers
bpf_doc.py parses bpf.h header to collect information about various functions (such as BPF helpers) and dump them in one of the supported
forms: rst docs and a C header.
It's useful for external tools to be able to consume information about BPF helpers - list of helpers and their args - in an easy-to-parse format such as JSON. Given that bpf_doc.py already does the work of searching and collecting the helpers, implement trivial JSON printer and add --json option for helpers target.
--- scripts/bpf_doc.py | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-)
scripts/bpf_doc.py +37 -5
@@ -8,6 +8,7 @@ from __future__ import print_function import argparse +import json import re import sys, os import subprocess
@@ -675,7 +676,7 @@ COMMANDS self.print_elem(command) -class PrinterHelpers(Printer): +class PrinterHelpersHeader(Printer): """ A printer for dumping collected information about helpers as C header to be included from BPF program.
@@ -896,6 +897,27 @@ class PrinterHelpers(Printer): print(') = (void *) %d;' % helper.enum_val) print('') + +class PrinterHelpersJSON(Printer): + """ + A printer for dumping collected information about helpers as a JSON file. + @parser: A HeaderParser with Helper objects to print to standard output + """ + + def __init__(self, parser): + self.elements = parser.helpers + self.elem_number_check( + parser.desc_unique_helpers, + parser.define_unique_helpers, + "helper", + "___BPF_FUNC_MAPPER", + ) + + def print_all(self): + protos = [helper.proto_break_down() for helper in self.elements] + out_dict = {"helpers": protos} + print(json.dumps(out_dict, indent=4)) + ############################################################################### # If script is launched from scripts/ from kernel tree and can access
@@ -917,6 +939,8 @@ rst2man utility. """) argParser.add_argument('--header', action='store_true', help='generate C header file') +argParser.add_argument("--json", action="store_true", + help="generate a JSON with information about helpers") if (os.path.isfile(bpfh)): argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h', default=bpfh)
@@ -930,11 +954,19 @@ args = argParser.parse_args() headerParser = HeaderParser(args.filename) headerParser.run() -# Print formatted output to standard output. +if args.header and args.json: + print("bpf_doc.py: Use --header or --json, not both") + exit(1) +if args.target != "helpers" and (args.header or args.json): + print("bpf_doc.py: Only helpers header/json generation is supported") + exit(1) + if args.header: - if args.target != 'helpers': - raise NotImplementedError('Only helpers header generation is supported') - printer = PrinterHelpers(headerParser) + printer = PrinterHelpersHeader(headerParser) +elif args.json: + printer = PrinterHelpersJSON(headerParser) else: printer = printers[args.target](headerParser) + +# Print formatted output to standard output. printer.print_all()
Signature · 1 line
2.47.1
Quoted 11 lines · Ihor Solodrai
bpf_doc.pyparsesbpf.hheader to collect information about various functions (such as BPF helpers) and dump them in one of the supported
forms: rst docs and a C header.It's useful for external tools to be able to consume information about BPF helpers - list of helpers and their args - in an easy-to-parse format such as JSON. Given that
bpf_doc.pyalready does the work of searching and collecting the helpers, implement trivial JSON printer and add --json option for helpers target.
Quoted 5 lines
Signed-off-by Ihor Solodrai <[email protected]>--- scripts/bpf_doc.py | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-)
Ihor, do you have an example on how JSON output would look like?
Quentin, can you please take a look? Do you have any objections?
Quoted 85 lines
scripts/bpf_doc.py +37
-6@@ -8,6 +8,7 @@ from __future__ import print_function import argparse +import json import re import sys, os import subprocess@@ -675,7 +676,7 @@ COMMANDS self.print_elem(command) -class PrinterHelpers(Printer): +class PrinterHelpersHeader(Printer): """ A printer for dumping collected information about helpers as C header to be included from BPF program.@@ -896,6 +897,27 @@ class PrinterHelpers(Printer): print(') = (void *) %d;' % helper.enum_val) print('') + +class PrinterHelpersJSON(Printer): + """ + A printer for dumping collected information about helpers as a JSON file. + @parser: A HeaderParser with Helper objects to print to standard output + """ + + def __init__(self, parser): + self.elements = parser.helpers + self.elem_number_check( + parser.desc_unique_helpers, + parser.define_unique_helpers, + "helper", + "___BPF_FUNC_MAPPER", + ) + + def print_all(self): + protos = [helper.proto_break_down() for helper in self.elements] + out_dict = {"helpers": protos} + print(json.dumps(out_dict, indent=4)) + ############################################################################### # If script is launched from scripts/ from kernel tree and can access@@ -917,6 +939,8 @@ rst2man utility. """) argParser.add_argument('--header', action='store_true', help='generate C header file') +argParser.add_argument("--json", action="store_true", + help="generate a JSON with information about helpers") if (os.path.isfile(bpfh)): argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h', default=bpfh)@@ -930,11 +954,19 @@ args = argParser.parse_args() headerParser = HeaderParser(args.filename) headerParser.run() -# Print formatted output to standard output. +if args.header and args.json: + print("bpf_doc.py: Use --header or --json, not both") + exit(1) +if args.target != "helpers" and (args.header or args.json): + print("bpf_doc.py: Only helpers header/json generation is supported") + exit(1) + if args.header: - if args.target != 'helpers': - raise NotImplementedError('Only helpers header generation is supported') - printer = PrinterHelpers(headerParser) + printer = PrinterHelpersHeader(headerParser) +elif args.json: + printer = PrinterHelpersJSON(headerParser) else: printer = printers[args.target](headerParser) + +# Print formatted output to standard output. printer.print_all() --2.47.1
Ihor Solodrai
reviewerQuoted 12 lines · Andrii Nakryiko
Quoted 11 lines · Ihor Solodrai
bpf_doc.pyparsesbpf.hheader to collect information about various functions (such as BPF helpers) and dump them in one of the supported
forms: rst docs and a C header.It's useful for external tools to be able to consume information about BPF helpers - list of helpers and their args - in an easy-to-parse format such as JSON. Given that
bpf_doc.pyalready does the work of searching and collecting the helpers, implement trivial JSON printer and add --json option for helpers target.
Quoted 7 lines
Quoted 5 lines
Signed-off-by Ihor Solodrai <[email protected]>--- scripts/bpf_doc.py | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-)Ihor, do you have an example on how JSON output would look like?
Sure. I already use it:
https://github.com/libbpf/bpfvv/blob/master/bpf-helpers.json
I only wanted function names and arg list, so the current output is good enough for my use-case.
Quoted 88 lines
Quentin, can you please take a look? Do you have any objections?
Quoted 85 lines
scripts/bpf_doc.py +37
-6@@ -8,6 +8,7 @@ from __future__ import print_function import argparse +import json import re import sys, os import subprocess@@ -675,7 +676,7 @@ COMMANDS self.print_elem(command) -class PrinterHelpers(Printer): +class PrinterHelpersHeader(Printer): """ A printer for dumping collected information about helpers as C header to be included from BPF program.@@ -896,6 +897,27 @@ class PrinterHelpers(Printer): print(') = (void *) %d;' % helper.enum_val) print('') + +class PrinterHelpersJSON(Printer): + """ + A printer for dumping collected information about helpers as a JSON file. + @parser: A HeaderParser with Helper objects to print to standard output + """ + + def __init__(self, parser): + self.elements = parser.helpers + self.elem_number_check( + parser.desc_unique_helpers, + parser.define_unique_helpers, + "helper", + "___BPF_FUNC_MAPPER", + ) + + def print_all(self): + protos = [helper.proto_break_down() for helper in self.elements] + out_dict = {"helpers": protos} + print(json.dumps(out_dict, indent=4)) + ############################################################################### # If script is launched from scripts/ from kernel tree and can access@@ -917,6 +939,8 @@ rst2man utility. """) argParser.add_argument('--header', action='store_true', help='generate C header file') +argParser.add_argument("--json", action="store_true", + help="generate a JSON with information about helpers") if (os.path.isfile(bpfh)): argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h', default=bpfh)@@ -930,11 +954,19 @@ args = argParser.parse_args() headerParser = HeaderParser(args.filename) headerParser.run() -# Print formatted output to standard output. +if args.header and args.json: + print("bpf_doc.py: Use --header or --json, not both") + exit(1) +if args.target != "helpers" and (args.header or args.json): + print("bpf_doc.py: Only helpers header/json generation is supported") + exit(1) + if args.header: - if args.target != 'helpers': - raise NotImplementedError('Only helpers header generation is supported') - printer = PrinterHelpers(headerParser) + printer = PrinterHelpersHeader(headerParser) +elif args.json: + printer = PrinterHelpersJSON(headerParser) else: printer = printers[args.target](headerParser) + +# Print formatted output to standard output. printer.print_all() --2.47.1
Quoted 20 lines · Ihor Solodrai
Quoted 18 lines · Andrii Nakryiko
Quoted 16 lines · Ihor Solodrai
bpf_doc.pyparsesbpf.hheader to collect information about various functions (such as BPF helpers) and dump them in one of the supported
forms: rst docs and a C header.It's useful for external tools to be able to consume information about BPF helpers - list of helpers and their args - in an easy-to-parse format such as JSON. Given that
bpf_doc.pyalready does the work of searching and collecting the helpers, implement trivial JSON printer and add --json option for helpers target.Signed-off-by Ihor Solodrai <[email protected]>--- scripts/bpf_doc.py | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-)
Quoted 8 lines
Quoted 1 line
Ihor, do you have an example on how JSON output would look like?
Sure. I already use it:
https://github.com/libbpf/bpfvv/blob/master/bpf-helpers.json
I only wanted function names and arg list, so the current output is good enough for my use-case.
Nice, thanks! We also have doc comments, right? I'd add them for completeness (maybe your tool will show it when hovering over the helper call instruction or something...)
Quoted 90 lines
Quoted 89 lines
Quentin, can you please take a look? Do you have any objections?
Quoted 85 lines
scripts/bpf_doc.py +37
-6@@ -8,6 +8,7 @@ from __future__ import print_function import argparse +import json import re import sys, os import subprocess@@ -675,7 +676,7 @@ COMMANDS self.print_elem(command) -class PrinterHelpers(Printer): +class PrinterHelpersHeader(Printer): """ A printer for dumping collected information about helpers as C header to be included from BPF program.@@ -896,6 +897,27 @@ class PrinterHelpers(Printer): print(') = (void *) %d;' % helper.enum_val) print('') + +class PrinterHelpersJSON(Printer): + """ + A printer for dumping collected information about helpers as a JSON file. + @parser: A HeaderParser with Helper objects to print to standard output + """ + + def __init__(self, parser): + self.elements = parser.helpers + self.elem_number_check( + parser.desc_unique_helpers, + parser.define_unique_helpers, + "helper", + "___BPF_FUNC_MAPPER", + ) + + def print_all(self): + protos = [helper.proto_break_down() for helper in self.elements] + out_dict = {"helpers": protos} + print(json.dumps(out_dict, indent=4)) + ############################################################################### # If script is launched from scripts/ from kernel tree and can access@@ -917,6 +939,8 @@ rst2man utility. """) argParser.add_argument('--header', action='store_true', help='generate C header file') +argParser.add_argument("--json", action="store_true", + help="generate a JSON with information about helpers") if (os.path.isfile(bpfh)): argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h', default=bpfh)@@ -930,11 +954,19 @@ args = argParser.parse_args() headerParser = HeaderParser(args.filename) headerParser.run() -# Print formatted output to standard output. +if args.header and args.json: + print("bpf_doc.py: Use --header or --json, not both") + exit(1) +if args.target != "helpers" and (args.header or args.json): + print("bpf_doc.py: Only helpers header/json generation is supported") + exit(1) + if args.header: - if args.target != 'helpers': - raise NotImplementedError('Only helpers header generation is supported') - printer = PrinterHelpers(headerParser) + printer = PrinterHelpersHeader(headerParser) +elif args.json: + printer = PrinterHelpersJSON(headerParser) else: printer = printers[args.target](headerParser) + +# Print formatted output to standard output. printer.print_all() --2.47.1
2025-05-06 15:25 UTC-0700 ~ Andrii Nakryiko <[email protected]>
Quoted 25 lines · Ihor Solodrai
Quoted 24 lines · Andrii Nakryiko
Quoted 12 lines · Ihor Solodrai
Quoted 11 lines
bpf_doc.pyparsesbpf.hheader to collect information about various functions (such as BPF helpers) and dump them in one of the supported
forms: rst docs and a C header.It's useful for external tools to be able to consume information about BPF helpers - list of helpers and their args - in an easy-to-parse format such as JSON. Given that
bpf_doc.pyalready does the work of searching and collecting the helpers, implement trivial JSON printer and add --json option for helpers target.Quoted 7 lines
Quoted 5 lines
Signed-off-by Ihor Solodrai <[email protected]>--- scripts/bpf_doc.py | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-)Ihor, do you have an example on how JSON output would look like?
Sure. I already use it:
Quoted 12 lines
Quoted 4 lines
https://github.com/libbpf/bpfvv/blob/master/bpf-helpers.json
I only wanted function names and arg list, so the current output is good enough for my use-case.
Nice, thanks! We also have doc comments, right? I'd add them for completeness (maybe your tool will show it when hovering over the helper call instruction or something...)
Quoted 3 lines
Quoted 3 lines
Quentin, can you please take a look? Do you have any objections?
I'd missed this patch, thank you Andrii.
No objection, I think JSON support for the script is a nice addition, thanks for this! Cc Dylan, not sure if this could be of interest for your docs.
I agree with Andrii, we should just as well add the comments to the JSON file. I'd even add support for printing syscalls info as JSON? It should be easy to do too.
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
scripts/bpf_doc.py +23
-1@@ -8,6 +8,7 @@ from __future__ import print_function import argparse +import json import re import sys, os import subprocess@@ -675,7 +676,7 @@ COMMANDS self.print_elem(command) -class PrinterHelpers(Printer): +class PrinterHelpersHeader(Printer): """ A printer for dumping collected information about helpers as C header to be included from BPF program.@@ -896,6 +897,27 @@ class PrinterHelpers(Printer): print(') = (void *) %d;' % helper.enum_val) print('') + +class PrinterHelpersJSON(Printer): + """ + A printer for dumping collected information about helpers as a JSON file. + @parser: A HeaderParser with Helper objects to print to standard output + """ + + def __init__(self, parser): + self.elements = parser.helpers + self.elem_number_check( + parser.desc_unique_helpers, + parser.define_unique_helpers, + "helper", + "___BPF_FUNC_MAPPER", + ) + + def print_all(self): + protos = [helper.proto_break_down() for helper in self.elements] + out_dict = {"helpers": protos} + print(json.dumps(out_dict, indent=4)) + ###############################################################################
Quoted 1 line
Quoted 1 line
Quoted 1 line
scripts/bpf_doc.py class PrinterHelpers(Printer): · line 923
Quoted 1 line
scripts/bpf_doc.py +0
-0# If script is launched from scripts/ from kernel tree and can access
Then we could add a dimension for formatting in the "printers" dictionary:
printers = {
'helpers': {
'rst': PrinterHelpersRST,
'json': PrinterHelpersJSON,
'header': PrinterHelpersHeader,
},
'syscall': {
'rst': PrinterSyscallRST,
# JSON here?
},
}Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
scripts/bpf_doc.py +2
-0@@ -917,6 +939,8 @@ rst2man utility. """) argParser.add_argument('--header', action='store_true', help='generate C header file') +argParser.add_argument("--json", action="store_true", + help="generate a JSON with information about helpers") if (os.path.isfile(bpfh)): argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h', default=bpfh)@@ -930,11 +954,19 @@ args = argParser.parse_args() headerParser = HeaderParser(args.filename) headerParser.run()
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
scripts/bpf_doc.py args = argParser.parse_args() · line 959
Quoted 4 lines
scripts/bpf_doc.py +3
-1-# Print formatted output to standard output. +if args.header and args.json: + print("bpf_doc.py: Use --header or --json, not both") + exit(1)
Could we change this to recall the usage and print to stderr, please?
argParser.print_usage(file=sys.stderr) print("Use either --header or --json, not both",
file=sys.stderr)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
scripts/bpf_doc.py +4
-3+if args.target != "helpers" and (args.header or args.json): + print("bpf_doc.py: Only helpers header/json generation is supported") + exit(1) + if args.header: - if args.target != 'helpers': - raise NotImplementedError('Only helpers header generation is supported') - printer = PrinterHelpers(headerParser)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
scripts/bpf_doc.py args = argParser.parse_args() · line 972
Quoted 8 lines
scripts/bpf_doc.py +5
-0+ printer = PrinterHelpersHeader(headerParser) +elif args.json: + printer = PrinterHelpersJSON(headerParser) else: printer = printers[args.target](headerParser) + +# Print formatted output to standard output. printer.print_all()
With the change to the "printers" dictionary we could error out if we don't find the target/format combination:
output_format = 'rst'
if args.header:
output_format = 'header'
elif args.json:
output_format = 'json'
try:
printer = printers[args.target][output_format](headerParser)
except KeyError:
argParser.print_usage(file=sys.stderr) print("Unsupported target/format combination: '%s', '%s'" %
(args.target, output_format), file=sys.stderr)
exit(1)
# Print formatted output to standard output.
printer.print_all()
What do you think?
Thanks,
Quentin