Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools: bpftool: add JSON output for `bpftool prog dump xlated *` command

Add a new printing function to dump translated eBPF instructions as
JSON. As for plain output, opcodes are printed only on request (when
`opcodes` is provided on the command line).

The disassembled output is generated by the same code that is used by
the kernel verifier.

Example output:

$ bpftool --json --pretty prog dump xlated id 1
[{
"disasm": "(bf) r6 = r1"
},{
"disasm": "(61) r7 = *(u32 *)(r6 +16)"
},{
"disasm": "(95) exit"
}
]

$ bpftool --json --pretty prog dump xlated id 1 opcodes
[{
"disasm": "(bf) r6 = r1",
"opcodes": {
"code": "0xbf",
"src_reg": "0x1",
"dst_reg": "0x6",
"off": ["0x00","0x00"
],
"imm": ["0x00","0x00","0x00","0x00"
]
}
},{
"disasm": "(61) r7 = *(u32 *)(r6 +16)",
"opcodes": {
"code": "0x61",
"src_reg": "0x6",
"dst_reg": "0x7",
"off": ["0x10","0x00"
],
"imm": ["0x00","0x00","0x00","0x00"
]
}
},{
"disasm": "(95) exit",
"opcodes": {
"code": "0x95",
"src_reg": "0x0",
"dst_reg": "0x0",
"off": ["0x00","0x00"
],
"imm": ["0x00","0x00","0x00","0x00"
]
}
}
]

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Quentin Monnet and committed by
David S. Miller
f05e2c32 107f0412

+89 -2
+10
tools/bpf/bpftool/common.c
··· 214 214 fclose(fdi); 215 215 return NULL; 216 216 } 217 + 218 + void print_hex_data_json(uint8_t *data, size_t len) 219 + { 220 + unsigned int i; 221 + 222 + jsonw_start_array(json_wtr); 223 + for (i = 0; i < len; i++) 224 + jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]); 225 + jsonw_end_array(json_wtr); 226 + }
+8
tools/bpf/bpftool/json_writer.c
··· 156 156 putc(' ', self->out); 157 157 } 158 158 159 + void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap) 160 + { 161 + jsonw_eor(self); 162 + putc('"', self->out); 163 + vfprintf(self->out, fmt, ap); 164 + putc('"', self->out); 165 + } 166 + 159 167 void jsonw_printf(json_writer_t *self, const char *fmt, ...) 160 168 { 161 169 va_list ap;
+2
tools/bpf/bpftool/json_writer.h
··· 17 17 18 18 #include <stdbool.h> 19 19 #include <stdint.h> 20 + #include <stdarg.h> 20 21 21 22 /* Opaque class structure */ 22 23 typedef struct json_writer json_writer_t; ··· 34 33 void jsonw_name(json_writer_t *self, const char *name); 35 34 36 35 /* Add value */ 36 + void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap); 37 37 void jsonw_printf(json_writer_t *self, const char *fmt, ...); 38 38 void jsonw_string(json_writer_t *self, const char *value); 39 39 void jsonw_bool(json_writer_t *self, bool value);
+1
tools/bpf/bpftool/main.h
··· 95 95 int prog_parse_fd(int *argc, char ***argv); 96 96 97 97 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes); 98 + void print_hex_data_json(uint8_t *data, size_t len); 98 99 99 100 #endif
+68 -2
tools/bpf/bpftool/prog.c
··· 385 385 va_end(args); 386 386 } 387 387 388 - static void dump_xlated(void *buf, unsigned int len, bool opcodes) 388 + static void dump_xlated_plain(void *buf, unsigned int len, bool opcodes) 389 389 { 390 390 struct bpf_insn *insn = buf; 391 391 bool double_insn = false; ··· 412 412 printf("\n"); 413 413 } 414 414 } 415 + } 416 + 417 + static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...) 418 + { 419 + unsigned int l = strlen(fmt); 420 + char chomped_fmt[l]; 421 + va_list args; 422 + 423 + va_start(args, fmt); 424 + if (l > 0) { 425 + strncpy(chomped_fmt, fmt, l - 1); 426 + chomped_fmt[l - 1] = '\0'; 427 + } 428 + jsonw_vprintf_enquote(json_wtr, chomped_fmt, args); 429 + va_end(args); 430 + } 431 + 432 + static void dump_xlated_json(void *buf, unsigned int len, bool opcodes) 433 + { 434 + struct bpf_insn *insn = buf; 435 + bool double_insn = false; 436 + unsigned int i; 437 + 438 + jsonw_start_array(json_wtr); 439 + for (i = 0; i < len / sizeof(*insn); i++) { 440 + if (double_insn) { 441 + double_insn = false; 442 + continue; 443 + } 444 + double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW); 445 + 446 + jsonw_start_object(json_wtr); 447 + jsonw_name(json_wtr, "disasm"); 448 + print_bpf_insn(print_insn_json, NULL, insn + i, true); 449 + 450 + if (opcodes) { 451 + jsonw_name(json_wtr, "opcodes"); 452 + jsonw_start_object(json_wtr); 453 + 454 + jsonw_name(json_wtr, "code"); 455 + jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code); 456 + 457 + jsonw_name(json_wtr, "src_reg"); 458 + jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg); 459 + 460 + jsonw_name(json_wtr, "dst_reg"); 461 + jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg); 462 + 463 + jsonw_name(json_wtr, "off"); 464 + print_hex_data_json((uint8_t *)(&insn[i].off), 2); 465 + 466 + jsonw_name(json_wtr, "imm"); 467 + if (double_insn && i < len - 1) 468 + print_hex_data_json((uint8_t *)(&insn[i].imm), 469 + 12); 470 + else 471 + print_hex_data_json((uint8_t *)(&insn[i].imm), 472 + 4); 473 + jsonw_end_object(json_wtr); 474 + } 475 + jsonw_end_object(json_wtr); 476 + } 477 + jsonw_end_array(json_wtr); 415 478 } 416 479 417 480 static int do_dump(int argc, char **argv) ··· 586 523 if (member_len == &info.jited_prog_len) 587 524 disasm_print_insn(buf, *member_len, opcodes); 588 525 else 589 - dump_xlated(buf, *member_len, opcodes); 526 + if (json_output) 527 + dump_xlated_json(buf, *member_len, opcodes); 528 + else 529 + dump_xlated_plain(buf, *member_len, opcodes); 590 530 } 591 531 592 532 free(buf);