bpftool: Fix some json formatting for struct_ops
cover bpftool: Fix some json formatting for struct_ops
When dumping struct_ops with bpftool, the json produced was invalid.
1) pointer values where not printed with surrounding quotes, causing an invalid json integer to be emitted
2) when bpftool struct_ops dump id <id>, the 2 dictionaries were not wrapped in a array, here also causing an invalid json payload to be emitted.
Manu Bretelle (2):
bpftool: fix printing of pointer value
bpftool: wrap struct_ops dump in an array
tools/bpf/bpftool/btf_dumper.c | 2 +- tools/bpf/bpftool/struct_ops.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
Signature · 1 line
2.39.3
PATCH 1/2 bpftool: fix printing of pointer value
When printing a pointer value, "%p" will either print the hexadecimal value of the pointer (e.g 0x1234), or (nil) when NULL.
Both of those are invalid json "integer" values and need to be wrapped in quotes.
Before:
```
$ sudo bpftool struct_ops dump name ned_dummy_cca | grep next
"next": (nil),
$ sudo bpftool struct_ops dump name ned_dummy_cca | \
jq '.[1].bpf_struct_ops_tcp_congestion_ops.data.list.next'
parse error: Invalid numeric literal at line 29, column 34
```After:
```
$ sudo ./bpftool struct_ops dump name ned_dummy_cca | grep next
"next": "(nil)",
$ sudo ./bpftool struct_ops dump name ned_dummy_cca | \
jq '.[1].bpf_struct_ops_tcp_congestion_ops.data.list.next'
"(nil)"
```--- tools/bpf/bpftool/btf_dumper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/btf_dumper.c +1 -1
@@ -127,7 +127,7 @@ static void btf_dumper_ptr(const struct btf_dumper *d, print_ptr_value:if (d->is_plain_text) -jsonw_printf (d->jw,"%p" , (void *)value); +jsonw_printf (d->jw,"\"%p\"" , (void *)value);else jsonw_printf (d->jw,"%lu" , value); }
Signature · 1 line
2.39.3
PATCH 2/2 bpftool: wrap struct_ops dump in an array
In reply to Manu BretelleWhen dumping a struct_ops, 2 dictionaries are emitted.
When using name, they were already wrapped in an array, but not when using id. Causing jq to fail at parsing the payload as it reached the comma following the first dict.
This change wraps those dictionaries in an array so valid json is emitted.
Before, jq fails to parse the output:
``` $ sudo bpftool struct_ops dump id 1523612 | jq . > /dev/null parse error: Expected value before ',' at line 19, column 2 ```
After, no error parsing the output:
``` sudo ./bpftool struct_ops dump id 1523612 | jq . > /dev/null ```
--- tools/bpf/bpftool/struct_ops.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/bpf/bpftool/struct_ops.c +6 -0
@@ -276,6 +276,9 @@ static struct res do_one_id(const char *id_str, work_func func, void *data, res.nr_maps++; +if (wtr) +jsonw_start_array (wtr); +if (func (fd, info, data, wtr)) res.nr_errs++;else if (!wtr && json_output)
@@ -288,6 +291,9 @@ static struct res do_one_id(const char *id_str, work_func func, void *data,*/ jsonw_null (json_wtr); +if (wtr) +jsonw_end_array (wtr); + done:free (info);close (fd);
Signature · 1 line
2.39.3
Quoted 6 lines · Manu Bretelle
When dumping
struct_opswith bpftool, the json produced was invalid.
1) pointer values where not printed with surrounding quotes, causing an invalid json integer to be emitted
2) when bpftoolstruct_opsdump id <id>, the 2 dictionaries were not wrapped in a array, here also causing an invalid json payload to be emitted.
Hi Manu,
I've tested this patch-set and everything seems to work as expected.
Thanks,
Eduard
Quoted 8 lines
Manu Bretelle (2):
bpftool: fix printing of pointer value
bpftool: wrapstruct_opsdump in an arraytools/bpf/bpftool/btf_dumper.c | 2 +- tools/bpf/bpftool/struct_ops.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
Quoted 7 lines · Manu Bretelle
When dumping
struct_opswith bpftool, the json produced was invalid.
1) pointer values where not printed with surrounding quotes, causing an invalid json integer to be emitted
2) when bpftoolstruct_opsdump id <id>, the 2 dictionaries were not wrapped in a array, here also causing an invalid json payload to be emitted.
Quoted 8 lines
Manu Bretelle (2):
bpftool: fix printing of pointer value
bpftool: wrapstruct_opsdump in an arraytools/bpf/bpftool/btf_dumper.c | 2 +- tools/bpf/bpftool/struct_ops.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
Thanks a lot for these fixes!
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 8 lines · On Wed, 18 Oct 2023 16:01:31 -0700 you wrote:
When dumping
struct_opswith bpftool, the json produced was invalid.
1) pointer values where not printed with surrounding quotes, causing an invalid json integer to be emitted
2) when bpftoolstruct_opsdump id <id>, the 2 dictionaries were not wrapped in a array, here also causing an invalid json payload to be emitted.[...]
Here is the summary with links:
- [bpf-next,1/2] bpftool: fix printing of pointer value
https://git.kernel.org/bpf/bpf-next/c/90704b4be0b0 - [bpf-next,2/2] bpftool: wrap struct_ops dump in an array https://git.kernel.org/bpf/bpf-next/c/6bd5e167af2e
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html