Parcourir la source

tools: bpftool: new command-line option and documentation for 'visual'

This patch adds new command-line option for visualizing the xlated eBPF
sequence.

Documentations are updated accordingly.

Usage:

  bpftool prog dump xlated id 2 visual

Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: Jiong Wang <jiong.wang@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Jiong Wang il y a 7 ans
Parent
commit
b6c1cedb49
2 fichiers modifiés avec 23 ajouts et 7 suppressions
  1. 12 6
      tools/bpf/bpftool/Documentation/bpftool-prog.rst
  2. 11 1
      tools/bpf/bpftool/prog.c

+ 12 - 6
tools/bpf/bpftool/Documentation/bpftool-prog.rst

@@ -21,7 +21,7 @@ MAP COMMANDS
 =============
 =============
 
 
 |	**bpftool** **prog { show | list }** [*PROG*]
 |	**bpftool** **prog { show | list }** [*PROG*]
-|	**bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes**}]
+|	**bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes** | **visual**}]
 |	**bpftool** **prog dump jited**  *PROG* [{**file** *FILE* | **opcodes**}]
 |	**bpftool** **prog dump jited**  *PROG* [{**file** *FILE* | **opcodes**}]
 |	**bpftool** **prog pin** *PROG* *FILE*
 |	**bpftool** **prog pin** *PROG* *FILE*
 |	**bpftool** **prog load** *OBJ* *FILE*
 |	**bpftool** **prog load** *OBJ* *FILE*
@@ -39,12 +39,18 @@ DESCRIPTION
 		  Output will start with program ID followed by program type and
 		  Output will start with program ID followed by program type and
 		  zero or more named attributes (depending on kernel version).
 		  zero or more named attributes (depending on kernel version).
 
 
-	**bpftool prog dump xlated** *PROG* [{ **file** *FILE* | **opcodes** }]
-		  Dump eBPF instructions of the program from the kernel.
-		  If *FILE* is specified image will be written to a file,
-		  otherwise it will be disassembled and printed to stdout.
+	**bpftool prog dump xlated** *PROG* [{ **file** *FILE* | **opcodes** | **visual** }]
+		  Dump eBPF instructions of the program from the kernel. By
+		  default, eBPF will be disassembled and printed to standard
+		  output in human-readable format. In this case, **opcodes**
+		  controls if raw opcodes should be printed as well.
 
 
-		  **opcodes** controls if raw opcodes will be printed.
+		  If **file** is specified, the binary image will instead be
+		  written to *FILE*.
+
+		  If **visual** is specified, control flow graph (CFG) will be
+		  built instead, and eBPF instructions will be presented with
+		  CFG in DOT format, on standard output.
 
 
 	**bpftool prog dump jited**  *PROG* [{ **file** *FILE* | **opcodes** }]
 	**bpftool prog dump jited**  *PROG* [{ **file** *FILE* | **opcodes** }]
 		  Dump jited image (host machine code) of the program.
 		  Dump jited image (host machine code) of the program.

+ 11 - 1
tools/bpf/bpftool/prog.c

@@ -47,6 +47,7 @@
 #include <bpf.h>
 #include <bpf.h>
 #include <libbpf.h>
 #include <libbpf.h>
 
 
+#include "cfg.h"
 #include "main.h"
 #include "main.h"
 #include "xlated_dumper.h"
 #include "xlated_dumper.h"
 
 
@@ -415,6 +416,7 @@ static int do_dump(int argc, char **argv)
 	unsigned int buf_size;
 	unsigned int buf_size;
 	char *filepath = NULL;
 	char *filepath = NULL;
 	bool opcodes = false;
 	bool opcodes = false;
+	bool visual = false;
 	unsigned char *buf;
 	unsigned char *buf;
 	__u32 *member_len;
 	__u32 *member_len;
 	__u64 *member_ptr;
 	__u64 *member_ptr;
@@ -453,6 +455,9 @@ static int do_dump(int argc, char **argv)
 	} else if (is_prefix(*argv, "opcodes")) {
 	} else if (is_prefix(*argv, "opcodes")) {
 		opcodes = true;
 		opcodes = true;
 		NEXT_ARG();
 		NEXT_ARG();
+	} else if (is_prefix(*argv, "visual")) {
+		visual = true;
+		NEXT_ARG();
 	}
 	}
 
 
 	if (argc) {
 	if (argc) {
@@ -536,6 +541,11 @@ static int do_dump(int argc, char **argv)
 		}
 		}
 
 
 		disasm_print_insn(buf, *member_len, opcodes, name);
 		disasm_print_insn(buf, *member_len, opcodes, name);
+	} else if (visual) {
+		if (json_output)
+			jsonw_null(json_wtr);
+		else
+			dump_xlated_cfg(buf, *member_len);
 	} else {
 	} else {
 		kernel_syms_load(&dd);
 		kernel_syms_load(&dd);
 		if (json_output)
 		if (json_output)
@@ -596,7 +606,7 @@ static int do_help(int argc, char **argv)
 
 
 	fprintf(stderr,
 	fprintf(stderr,
 		"Usage: %s %s { show | list } [PROG]\n"
 		"Usage: %s %s { show | list } [PROG]\n"
-		"       %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
+		"       %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
 		"       %s %s dump jited  PROG [{ file FILE | opcodes }]\n"
 		"       %s %s dump jited  PROG [{ file FILE | opcodes }]\n"
 		"       %s %s pin   PROG FILE\n"
 		"       %s %s pin   PROG FILE\n"
 		"       %s %s load  OBJ  FILE\n"
 		"       %s %s load  OBJ  FILE\n"