bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs
@ 2023-03-27 11:06 Quentin Monnet
  2023-03-27 11:06 ` [PATCH bpf-next v2 1/5] bpftool: Fix documentation about line info display for prog dumps Quentin Monnet
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: Quentin Monnet @ 2023-03-27 11:06 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
	Quentin Monnet

This set contains some improvements for bpftool's "visual" program dump
option, which produces the control flow graph in a DOT format. The main
objective is to add support for inline annotations on such graphs, so that
we can have the C source code for the program showing up alongside the
instructions, when available. The last commits also make it possible to
display the line numbers or the bare opcodes in the graph, as supported by
regular program dumps.

v2: Replace fputc(..., stdout) with putchar(...) in dotlabel_puts().

Quentin Monnet (5):
  bpftool: Fix documentation about line info display for prog dumps
  bpftool: Fix bug for long instructions in program CFG dumps
  bpftool: Support inline annotations when dumping the CFG of a program
  bpftool: Support "opcodes", "linum", "visual" simultaneously
  bpftool: Support printing opcodes and source file references in CFG

 .../bpftool/Documentation/bpftool-prog.rst    | 18 ++---
 tools/bpf/bpftool/bash-completion/bpftool     | 18 +++--
 tools/bpf/bpftool/btf_dumper.c                | 49 ++++++++++++
 tools/bpf/bpftool/cfg.c                       | 29 +++----
 tools/bpf/bpftool/cfg.h                       |  5 +-
 tools/bpf/bpftool/main.h                      |  2 +
 tools/bpf/bpftool/prog.c                      | 78 ++++++++++---------
 tools/bpf/bpftool/xlated_dumper.c             | 52 ++++++++++++-
 tools/bpf/bpftool/xlated_dumper.h             |  3 +-
 9 files changed, 182 insertions(+), 72 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH bpf-next v2 1/5] bpftool: Fix documentation about line info display for prog dumps
  2023-03-27 11:06 [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Quentin Monnet
@ 2023-03-27 11:06 ` Quentin Monnet
  2023-03-27 11:06 ` [PATCH bpf-next v2 2/5] bpftool: Fix bug for long instructions in program CFG dumps Quentin Monnet
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: Quentin Monnet @ 2023-03-27 11:06 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
	Quentin Monnet

The documentation states that when line_info is available when dumping a
program, the source line will be displayed "by default". There is no
notion of "default" here: the line is always displayed if available,
there is no way currently to turn it off.

In the next sentence, the documentation states that if "linum" is used
on the command line, the relevant filename, line, and column will be
displayed "on top of the source line". This is incorrect, as they are
currently displayed on the right side of the source line (or on top of
the eBPF instruction, not the source).

This commit fixes the documentation to address these points.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Stanislav Fomichev <sdf@google.com>
---
 tools/bpf/bpftool/Documentation/bpftool-prog.rst | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/tools/bpf/bpftool/Documentation/bpftool-prog.rst b/tools/bpf/bpftool/Documentation/bpftool-prog.rst
index 14de72544995..06d1e4314406 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-prog.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-prog.rst
@@ -106,9 +106,8 @@ DESCRIPTION
 		  CFG in DOT format, on standard output.
 
 		  If the programs have line_info available, the source line will
-		  be displayed by default.  If **linum** is specified,
-		  the filename, line number and line column will also be
-		  displayed on top of the source line.
+		  be displayed.  If **linum** is specified, the filename, line
+		  number and line column will also be displayed.
 
 	**bpftool prog dump jited**  *PROG* [{ **file** *FILE* | **opcodes** | **linum** }]
 		  Dump jited image (host machine code) of the program.
@@ -120,9 +119,8 @@ DESCRIPTION
 		  **opcodes** controls if raw opcodes will be printed.
 
 		  If the prog has line_info available, the source line will
-		  be displayed by default.  If **linum** is specified,
-		  the filename, line number and line column will also be
-		  displayed on top of the source line.
+		  be displayed.  If **linum** is specified, the filename, line
+		  number and line column will also be displayed.
 
 	**bpftool prog pin** *PROG* *FILE*
 		  Pin program *PROG* as *FILE*.
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH bpf-next v2 2/5] bpftool: Fix bug for long instructions in program CFG dumps
  2023-03-27 11:06 [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Quentin Monnet
  2023-03-27 11:06 ` [PATCH bpf-next v2 1/5] bpftool: Fix documentation about line info display for prog dumps Quentin Monnet
@ 2023-03-27 11:06 ` Quentin Monnet
  2023-03-27 11:06 ` [PATCH bpf-next v2 3/5] bpftool: Support inline annotations when dumping the CFG of a program Quentin Monnet
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 13+ messages in thread
From: Quentin Monnet @ 2023-03-27 11:06 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
	Quentin Monnet

When dumping the control flow graphs for programs using the 16-byte long
load instruction, we need to skip the second part of this instruction
when looking for the next instruction to process. Otherwise, we end up
printing "BUG_ld_00" from the kernel disassembler in the CFG.

Fixes: efcef17a6d65 ("tools: bpftool: generate .dot graph from CFG information")
Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Stanislav Fomichev <sdf@google.com>
---
 tools/bpf/bpftool/xlated_dumper.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
index 6fe3134ae45d..3daa05d9bbb7 100644
--- a/tools/bpf/bpftool/xlated_dumper.c
+++ b/tools/bpf/bpftool/xlated_dumper.c
@@ -372,8 +372,15 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
 	struct bpf_insn *insn_start = buf_start;
 	struct bpf_insn *insn_end = buf_end;
 	struct bpf_insn *cur = insn_start;
+	bool double_insn = false;
 
 	for (; cur <= insn_end; cur++) {
+		if (double_insn) {
+			double_insn = false;
+			continue;
+		}
+		double_insn = cur->code == (BPF_LD | BPF_IMM | BPF_DW);
+
 		printf("% 4d: ", (int)(cur - insn_start + start_idx));
 		print_bpf_insn(&cbs, cur, true);
 		if (cur != insn_end)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH bpf-next v2 3/5] bpftool: Support inline annotations when dumping the CFG of a program
  2023-03-27 11:06 [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Quentin Monnet
  2023-03-27 11:06 ` [PATCH bpf-next v2 1/5] bpftool: Fix documentation about line info display for prog dumps Quentin Monnet
  2023-03-27 11:06 ` [PATCH bpf-next v2 2/5] bpftool: Fix bug for long instructions in program CFG dumps Quentin Monnet
@ 2023-03-27 11:06 ` Quentin Monnet
  2023-03-27 17:02   ` Eduard Zingerman
  2023-03-27 11:06 ` [PATCH bpf-next v2 4/5] bpftool: Support "opcodes", "linum", "visual" simultaneously Quentin Monnet
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 13+ messages in thread
From: Quentin Monnet @ 2023-03-27 11:06 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
	Quentin Monnet

We support dumping the control flow graph of loaded programs to the DOT
format with bpftool, but so far this feature wouldn't display the source
code lines available through BTF along with the eBPF bytecode. Let's add
support for these annotations, to make it easier to read the graph.

In prog.c, we move the call to dump_xlated_cfg() in order to pass and
use the full struct dump_data, instead of creating a minimal one in
draw_bb_node().

We pass the pointer to this struct down to dump_xlated_for_graph() in
xlated_dumper.c, where most of the logics is added. We deal with BTF
mostly like we do for plain or JSON output, except that we cannot use a
"nr_skip" value to skip a given number of linfo records (we don't
process the BPF instructions linearly, and apart from the root of the
graph we don't know how many records we should skip, so we just store
the last linfo and make sure the new one we find is different before
printing it).

When printing the source instructions to the label of a DOT graph node,
there are a few subtleties to address. We want some special newline
markers, and there are some characters that we must escape. To deal with
them, we introduce a new dedicated function btf_dump_linfo_dotlabel() in
btf_dumper.c. We'll reuse this function in a later commit to format the
filepath, line, and column references as well.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Stanislav Fomichev <sdf@google.com>
---
 tools/bpf/bpftool/btf_dumper.c    | 32 +++++++++++++++++++++++++++++++
 tools/bpf/bpftool/cfg.c           | 23 ++++++++++------------
 tools/bpf/bpftool/cfg.h           |  4 +++-
 tools/bpf/bpftool/main.h          |  2 ++
 tools/bpf/bpftool/prog.c          | 17 +++++++---------
 tools/bpf/bpftool/xlated_dumper.c | 32 ++++++++++++++++++++++++++++++-
 6 files changed, 85 insertions(+), 25 deletions(-)

diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
index e7f6ec3a8f35..8bfc1b69497d 100644
--- a/tools/bpf/bpftool/btf_dumper.c
+++ b/tools/bpf/bpftool/btf_dumper.c
@@ -821,3 +821,35 @@ void btf_dump_linfo_json(const struct btf *btf,
 					BPF_LINE_INFO_LINE_COL(linfo->line_col));
 	}
 }
+
+static void dotlabel_puts(const char *s)
+{
+	for (; *s; ++s) {
+		switch (*s) {
+		case '\\':
+		case '"':
+		case '{':
+		case '}':
+		case '>':
+		case '|':
+			putchar('\\');
+			__fallthrough;
+		default:
+			putchar(*s);
+		}
+	}
+}
+
+void btf_dump_linfo_dotlabel(const struct btf *btf,
+			     const struct bpf_line_info *linfo)
+{
+	const char *line = btf__name_by_offset(btf, linfo->line_off);
+
+	if (!line)
+		return;
+	line = ltrim(line);
+
+	printf("; ");
+	dotlabel_puts(line);
+	printf("\\l\\\n");
+}
diff --git a/tools/bpf/bpftool/cfg.c b/tools/bpf/bpftool/cfg.c
index 1951219a9af7..9fdc1f0cdd6e 100644
--- a/tools/bpf/bpftool/cfg.c
+++ b/tools/bpf/bpftool/cfg.c
@@ -380,7 +380,8 @@ static void cfg_destroy(struct cfg *cfg)
 	}
 }
 
-static void draw_bb_node(struct func_node *func, struct bb_node *bb)
+static void
+draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd)
 {
 	const char *shape;
 
@@ -398,13 +399,9 @@ static void draw_bb_node(struct func_node *func, struct bb_node *bb)
 		printf("EXIT");
 	} else {
 		unsigned int start_idx;
-		struct dump_data dd = {};
-
-		printf("{");
-		kernel_syms_load(&dd);
+		printf("{\\\n");
 		start_idx = bb->head - func->start;
-		dump_xlated_for_graph(&dd, bb->head, bb->tail, start_idx);
-		kernel_syms_destroy(&dd);
+		dump_xlated_for_graph(dd, bb->head, bb->tail, start_idx);
 		printf("}");
 	}
 
@@ -430,12 +427,12 @@ static void draw_bb_succ_edges(struct func_node *func, struct bb_node *bb)
 	}
 }
 
-static void func_output_bb_def(struct func_node *func)
+static void func_output_bb_def(struct func_node *func, struct dump_data *dd)
 {
 	struct bb_node *bb;
 
 	list_for_each_entry(bb, &func->bbs, l) {
-		draw_bb_node(func, bb);
+		draw_bb_node(func, bb, dd);
 	}
 }
 
@@ -455,7 +452,7 @@ static void func_output_edges(struct func_node *func)
 	       func_idx, ENTRY_BLOCK_INDEX, func_idx, EXIT_BLOCK_INDEX);
 }
 
-static void cfg_dump(struct cfg *cfg)
+static void cfg_dump(struct cfg *cfg, struct dump_data *dd)
 {
 	struct func_node *func;
 
@@ -463,14 +460,14 @@ static void cfg_dump(struct cfg *cfg)
 	list_for_each_entry(func, &cfg->funcs, l) {
 		printf("subgraph \"cluster_%d\" {\n\tstyle=\"dashed\";\n\tcolor=\"black\";\n\tlabel=\"func_%d ()\";\n",
 		       func->idx, func->idx);
-		func_output_bb_def(func);
+		func_output_bb_def(func, dd);
 		func_output_edges(func);
 		printf("}\n");
 	}
 	printf("}\n");
 }
 
-void dump_xlated_cfg(void *buf, unsigned int len)
+void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len)
 {
 	struct bpf_insn *insn = buf;
 	struct cfg cfg;
@@ -479,7 +476,7 @@ void dump_xlated_cfg(void *buf, unsigned int len)
 	if (cfg_build(&cfg, insn, len))
 		return;
 
-	cfg_dump(&cfg);
+	cfg_dump(&cfg, dd);
 
 	cfg_destroy(&cfg);
 }
diff --git a/tools/bpf/bpftool/cfg.h b/tools/bpf/bpftool/cfg.h
index e144257ea6d2..909d17e6d4c2 100644
--- a/tools/bpf/bpftool/cfg.h
+++ b/tools/bpf/bpftool/cfg.h
@@ -4,6 +4,8 @@
 #ifndef __BPF_TOOL_CFG_H
 #define __BPF_TOOL_CFG_H
 
-void dump_xlated_cfg(void *buf, unsigned int len);
+#include "xlated_dumper.h"
+
+void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len);
 
 #endif /* __BPF_TOOL_CFG_H */
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 0ef373cef4c7..e9ee514b22d4 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -229,6 +229,8 @@ void btf_dump_linfo_plain(const struct btf *btf,
 			  const char *prefix, bool linum);
 void btf_dump_linfo_json(const struct btf *btf,
 			 const struct bpf_line_info *linfo, bool linum);
+void btf_dump_linfo_dotlabel(const struct btf *btf,
+			     const struct bpf_line_info *linfo);
 
 struct nlattr;
 struct ifinfomsg;
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index afbe3ec342c8..d855118f0d96 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -840,11 +840,6 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
 					      false))
 				goto exit_free;
 		}
-	} else if (visual) {
-		if (json_output)
-			jsonw_null(json_wtr);
-		else
-			dump_xlated_cfg(buf, member_len);
 	} else {
 		kernel_syms_load(&dd);
 		dd.nr_jited_ksyms = info->nr_jited_ksyms;
@@ -854,12 +849,14 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
 		dd.finfo_rec_size = info->func_info_rec_size;
 		dd.prog_linfo = prog_linfo;
 
-		if (json_output)
-			dump_xlated_json(&dd, buf, member_len, opcodes,
-					 linum);
+		if (json_output && visual)
+			jsonw_null(json_wtr);
+		else if (json_output)
+			dump_xlated_json(&dd, buf, member_len, opcodes, linum);
+		else if (visual)
+			dump_xlated_cfg(&dd, buf, member_len);
 		else
-			dump_xlated_plain(&dd, buf, member_len, opcodes,
-					  linum);
+			dump_xlated_plain(&dd, buf, member_len, opcodes, linum);
 		kernel_syms_destroy(&dd);
 	}
 
diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
index 3daa05d9bbb7..5fbe94aa8589 100644
--- a/tools/bpf/bpftool/xlated_dumper.c
+++ b/tools/bpf/bpftool/xlated_dumper.c
@@ -369,20 +369,50 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
 		.cb_imm		= print_imm,
 		.private_data	= dd,
 	};
+	const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo;
+	const struct bpf_line_info *last_linfo = NULL;
+	struct bpf_func_info *record = dd->func_info;
 	struct bpf_insn *insn_start = buf_start;
 	struct bpf_insn *insn_end = buf_end;
 	struct bpf_insn *cur = insn_start;
+	struct btf *btf = dd->btf;
 	bool double_insn = false;
+	char func_sig[1024];
 
 	for (; cur <= insn_end; cur++) {
+		unsigned int insn_off;
+
 		if (double_insn) {
 			double_insn = false;
 			continue;
 		}
 		double_insn = cur->code == (BPF_LD | BPF_IMM | BPF_DW);
 
-		printf("% 4d: ", (int)(cur - insn_start + start_idx));
+		insn_off = (unsigned int)(cur - insn_start + start_idx);
+		if (btf && record) {
+			if (record->insn_off == insn_off) {
+				btf_dumper_type_only(btf, record->type_id,
+						     func_sig,
+						     sizeof(func_sig));
+				if (func_sig[0] != '\0')
+					printf("; %s:\\l\\\n", func_sig);
+				record = (void *)record + dd->finfo_rec_size;
+			}
+		}
+
+		if (prog_linfo) {
+			const struct bpf_line_info *linfo;
+
+			linfo = bpf_prog_linfo__lfind(prog_linfo, insn_off, 0);
+			if (linfo && linfo != last_linfo) {
+				btf_dump_linfo_dotlabel(btf, linfo);
+				last_linfo = linfo;
+			}
+		}
+
+		printf("%d: ", insn_off);
 		print_bpf_insn(&cbs, cur, true);
+
 		if (cur != insn_end)
 			printf(" | ");
 	}
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH bpf-next v2 4/5] bpftool: Support "opcodes", "linum", "visual" simultaneously
  2023-03-27 11:06 [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Quentin Monnet
                   ` (2 preceding siblings ...)
  2023-03-27 11:06 ` [PATCH bpf-next v2 3/5] bpftool: Support inline annotations when dumping the CFG of a program Quentin Monnet
@ 2023-03-27 11:06 ` Quentin Monnet
  2023-03-27 11:06 ` [PATCH bpf-next v2 5/5] bpftool: Support printing opcodes and source file references in CFG Quentin Monnet
  2023-03-27 17:56 ` [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Eduard Zingerman
  5 siblings, 0 replies; 13+ messages in thread
From: Quentin Monnet @ 2023-03-27 11:06 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
	Quentin Monnet

When dumping a program, the keywords "opcodes" (for printing the raw
opcodes), "linum" (for displaying the filename, line number, column
number along with the source code), and "visual" (for generating the
control flow graph for translated programs) are mutually exclusive. But
there's no reason why they should be. Let's make it possible to pass
several of them at once. The "file FILE" option, which makes bpftool
output a binary image to a file, remains incompatible with the others.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Stanislav Fomichev <sdf@google.com>
---
 .../bpftool/Documentation/bpftool-prog.rst    |  8 +--
 tools/bpf/bpftool/bash-completion/bpftool     | 18 +++---
 tools/bpf/bpftool/prog.c                      | 61 ++++++++++---------
 3 files changed, 48 insertions(+), 39 deletions(-)

diff --git a/tools/bpf/bpftool/Documentation/bpftool-prog.rst b/tools/bpf/bpftool/Documentation/bpftool-prog.rst
index 06d1e4314406..9443c524bb76 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-prog.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-prog.rst
@@ -28,8 +28,8 @@ PROG COMMANDS
 =============
 
 |	**bpftool** **prog** { **show** | **list** } [*PROG*]
-|	**bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes** | **visual** | **linum**}]
-|	**bpftool** **prog dump jited**  *PROG* [{**file** *FILE* | **opcodes** | **linum**}]
+|	**bpftool** **prog dump xlated** *PROG* [{ **file** *FILE* | [**opcodes**] [**linum**] [**visual**] }]
+|	**bpftool** **prog dump jited**  *PROG* [{ **file** *FILE* | [**opcodes**] [**linum**] }]
 |	**bpftool** **prog pin** *PROG* *FILE*
 |	**bpftool** **prog** { **load** | **loadall** } *OBJ* *PATH* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*] [**pinmaps** *MAP_DIR*] [**autoattach**]
 |	**bpftool** **prog attach** *PROG* *ATTACH_TYPE* [*MAP*]
@@ -88,7 +88,7 @@ DESCRIPTION
 		  programs. On such kernels bpftool will automatically emit this
 		  information as well.
 
-	**bpftool prog dump xlated** *PROG* [{ **file** *FILE* | **opcodes** | **visual** | **linum** }]
+	**bpftool prog dump xlated** *PROG* [{ **file** *FILE* | [**opcodes**] [**linum**] [**visual**] }]
 		  Dump eBPF instructions of the programs from the kernel. By
 		  default, eBPF will be disassembled and printed to standard
 		  output in human-readable format. In this case, **opcodes**
@@ -109,7 +109,7 @@ DESCRIPTION
 		  be displayed.  If **linum** is specified, the filename, line
 		  number and line column will also be displayed.
 
-	**bpftool prog dump jited**  *PROG* [{ **file** *FILE* | **opcodes** | **linum** }]
+	**bpftool prog dump jited**  *PROG* [{ **file** *FILE* | [**opcodes**] [**linum**] }]
 		  Dump jited image (host machine code) of the program.
 
 		  If *FILE* is specified image will be written to a file,
diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
index 35f26f7c1124..98eb3852e771 100644
--- a/tools/bpf/bpftool/bash-completion/bpftool
+++ b/tools/bpf/bpftool/bash-completion/bpftool
@@ -268,7 +268,7 @@ _bpftool()
 
     # Deal with simplest keywords
     case $prev in
-        help|hex|opcodes|visual|linum)
+        help|hex)
             return 0
             ;;
         tag)
@@ -366,13 +366,17 @@ _bpftool()
                             return 0
                             ;;
                         *)
-                            _bpftool_once_attr 'file'
+                            # "file" is not compatible with other keywords here
+                            if _bpftool_search_list 'file'; then
+                                return 0
+                            fi
+                            if ! _bpftool_search_list 'linum opcodes visual'; then
+                                _bpftool_once_attr 'file'
+                            fi
+                            _bpftool_once_attr 'linum'
+                            _bpftool_once_attr 'opcodes'
                             if _bpftool_search_list 'xlated'; then
-                                COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \
-                                    "$cur" ) )
-                            else
-                                COMPREPLY+=( $( compgen -W 'opcodes linum' -- \
-                                    "$cur" ) )
+                                _bpftool_once_attr 'visual'
                             fi
                             return 0
                             ;;
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index d855118f0d96..567ac37dbd86 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -907,37 +907,42 @@ static int do_dump(int argc, char **argv)
 	if (nb_fds < 1)
 		goto exit_free;
 
-	if (is_prefix(*argv, "file")) {
-		NEXT_ARG();
-		if (!argc) {
-			p_err("expected file path");
-			goto exit_close;
-		}
-		if (nb_fds > 1) {
-			p_err("several programs matched");
-			goto exit_close;
-		}
+	while (argc) {
+		if (is_prefix(*argv, "file")) {
+			NEXT_ARG();
+			if (!argc) {
+				p_err("expected file path");
+				goto exit_close;
+			}
+			if (nb_fds > 1) {
+				p_err("several programs matched");
+				goto exit_close;
+			}
+
+			filepath = *argv;
+			NEXT_ARG();
+		} else if (is_prefix(*argv, "opcodes")) {
+			opcodes = true;
+			NEXT_ARG();
+		} else if (is_prefix(*argv, "visual")) {
+			if (nb_fds > 1) {
+				p_err("several programs matched");
+				goto exit_close;
+			}
 
-		filepath = *argv;
-		NEXT_ARG();
-	} else if (is_prefix(*argv, "opcodes")) {
-		opcodes = true;
-		NEXT_ARG();
-	} else if (is_prefix(*argv, "visual")) {
-		if (nb_fds > 1) {
-			p_err("several programs matched");
+			visual = true;
+			NEXT_ARG();
+		} else if (is_prefix(*argv, "linum")) {
+			linum = true;
+			NEXT_ARG();
+		} else {
+			usage();
 			goto exit_close;
 		}
-
-		visual = true;
-		NEXT_ARG();
-	} else if (is_prefix(*argv, "linum")) {
-		linum = true;
-		NEXT_ARG();
 	}
 
-	if (argc) {
-		usage();
+	if (filepath && (opcodes || visual || linum)) {
+		p_err("'file' is not compatible with 'opcodes', 'visual', or 'linum'");
 		goto exit_close;
 	}
 
@@ -2417,8 +2422,8 @@ static int do_help(int argc, char **argv)
 
 	fprintf(stderr,
 		"Usage: %1$s %2$s { show | list } [PROG]\n"
-		"       %1$s %2$s dump xlated PROG [{ file FILE | opcodes | visual | linum }]\n"
-		"       %1$s %2$s dump jited  PROG [{ file FILE | opcodes | linum }]\n"
+		"       %1$s %2$s dump xlated PROG [{ file FILE | [opcodes] [linum] [visual] }]\n"
+		"       %1$s %2$s dump jited  PROG [{ file FILE | [opcodes] [linum] }]\n"
 		"       %1$s %2$s pin   PROG FILE\n"
 		"       %1$s %2$s { load | loadall } OBJ  PATH \\\n"
 		"                         [type TYPE] [dev NAME] \\\n"
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH bpf-next v2 5/5] bpftool: Support printing opcodes and source file references in CFG
  2023-03-27 11:06 [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Quentin Monnet
                   ` (3 preceding siblings ...)
  2023-03-27 11:06 ` [PATCH bpf-next v2 4/5] bpftool: Support "opcodes", "linum", "visual" simultaneously Quentin Monnet
@ 2023-03-27 11:06 ` Quentin Monnet
  2023-03-27 17:04   ` Eduard Zingerman
  2023-03-27 17:56 ` [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Eduard Zingerman
  5 siblings, 1 reply; 13+ messages in thread
From: Quentin Monnet @ 2023-03-27 11:06 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf,
	Quentin Monnet

Add support for displaying opcodes or/and file references (filepath,
line and column numbers) when dumping the control flow graphs of loaded
BPF programs with bpftool.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Stanislav Fomichev <sdf@google.com>
---
 tools/bpf/bpftool/btf_dumper.c    | 19 ++++++++++++++++++-
 tools/bpf/bpftool/cfg.c           | 22 ++++++++++++++--------
 tools/bpf/bpftool/cfg.h           |  3 ++-
 tools/bpf/bpftool/main.h          |  2 +-
 tools/bpf/bpftool/prog.c          |  2 +-
 tools/bpf/bpftool/xlated_dumper.c | 15 +++++++++++++--
 tools/bpf/bpftool/xlated_dumper.h |  3 ++-
 7 files changed, 51 insertions(+), 15 deletions(-)

diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
index 8bfc1b69497d..24835c3f9a1c 100644
--- a/tools/bpf/bpftool/btf_dumper.c
+++ b/tools/bpf/bpftool/btf_dumper.c
@@ -841,7 +841,7 @@ static void dotlabel_puts(const char *s)
 }
 
 void btf_dump_linfo_dotlabel(const struct btf *btf,
-			     const struct bpf_line_info *linfo)
+			     const struct bpf_line_info *linfo, bool linum)
 {
 	const char *line = btf__name_by_offset(btf, linfo->line_off);
 
@@ -849,6 +849,23 @@ void btf_dump_linfo_dotlabel(const struct btf *btf,
 		return;
 	line = ltrim(line);
 
+	if (linum) {
+		const char *file = btf__name_by_offset(btf, linfo->file_name_off);
+
+		/* More forgiving on file because linum option is
+		 * expected to provide more info than the already
+		 * available src line.
+		 */
+		if (!file)
+			file = "";
+
+		printf("; [file:");
+		dotlabel_puts(file);
+		printf("line_num:%u line_col:%u]\\l\\\n",
+		       BPF_LINE_INFO_LINE_NUM(linfo->line_col),
+		       BPF_LINE_INFO_LINE_COL(linfo->line_col));
+	}
+
 	printf("; ");
 	dotlabel_puts(line);
 	printf("\\l\\\n");
diff --git a/tools/bpf/bpftool/cfg.c b/tools/bpf/bpftool/cfg.c
index 9fdc1f0cdd6e..eec437cca2ea 100644
--- a/tools/bpf/bpftool/cfg.c
+++ b/tools/bpf/bpftool/cfg.c
@@ -381,7 +381,8 @@ static void cfg_destroy(struct cfg *cfg)
 }
 
 static void
-draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd)
+draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd,
+	     bool opcodes, bool linum)
 {
 	const char *shape;
 
@@ -401,7 +402,8 @@ draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd)
 		unsigned int start_idx;
 		printf("{\\\n");
 		start_idx = bb->head - func->start;
-		dump_xlated_for_graph(dd, bb->head, bb->tail, start_idx);
+		dump_xlated_for_graph(dd, bb->head, bb->tail, start_idx,
+				      opcodes, linum);
 		printf("}");
 	}
 
@@ -427,12 +429,14 @@ static void draw_bb_succ_edges(struct func_node *func, struct bb_node *bb)
 	}
 }
 
-static void func_output_bb_def(struct func_node *func, struct dump_data *dd)
+static void
+func_output_bb_def(struct func_node *func, struct dump_data *dd,
+		   bool opcodes, bool linum)
 {
 	struct bb_node *bb;
 
 	list_for_each_entry(bb, &func->bbs, l) {
-		draw_bb_node(func, bb, dd);
+		draw_bb_node(func, bb, dd, opcodes, linum);
 	}
 }
 
@@ -452,7 +456,8 @@ static void func_output_edges(struct func_node *func)
 	       func_idx, ENTRY_BLOCK_INDEX, func_idx, EXIT_BLOCK_INDEX);
 }
 
-static void cfg_dump(struct cfg *cfg, struct dump_data *dd)
+static void
+cfg_dump(struct cfg *cfg, struct dump_data *dd, bool opcodes, bool linum)
 {
 	struct func_node *func;
 
@@ -460,14 +465,15 @@ static void cfg_dump(struct cfg *cfg, struct dump_data *dd)
 	list_for_each_entry(func, &cfg->funcs, l) {
 		printf("subgraph \"cluster_%d\" {\n\tstyle=\"dashed\";\n\tcolor=\"black\";\n\tlabel=\"func_%d ()\";\n",
 		       func->idx, func->idx);
-		func_output_bb_def(func, dd);
+		func_output_bb_def(func, dd, opcodes, linum);
 		func_output_edges(func);
 		printf("}\n");
 	}
 	printf("}\n");
 }
 
-void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len)
+void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len,
+		     bool opcodes, bool linum)
 {
 	struct bpf_insn *insn = buf;
 	struct cfg cfg;
@@ -476,7 +482,7 @@ void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len)
 	if (cfg_build(&cfg, insn, len))
 		return;
 
-	cfg_dump(&cfg, dd);
+	cfg_dump(&cfg, dd, opcodes, linum);
 
 	cfg_destroy(&cfg);
 }
diff --git a/tools/bpf/bpftool/cfg.h b/tools/bpf/bpftool/cfg.h
index 909d17e6d4c2..b3793f4e1783 100644
--- a/tools/bpf/bpftool/cfg.h
+++ b/tools/bpf/bpftool/cfg.h
@@ -6,6 +6,7 @@
 
 #include "xlated_dumper.h"
 
-void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len);
+void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len,
+		     bool opcodes, bool linum);
 
 #endif /* __BPF_TOOL_CFG_H */
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index e9ee514b22d4..00d11ca6d3f2 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -230,7 +230,7 @@ void btf_dump_linfo_plain(const struct btf *btf,
 void btf_dump_linfo_json(const struct btf *btf,
 			 const struct bpf_line_info *linfo, bool linum);
 void btf_dump_linfo_dotlabel(const struct btf *btf,
-			     const struct bpf_line_info *linfo);
+			     const struct bpf_line_info *linfo, bool linum);
 
 struct nlattr;
 struct ifinfomsg;
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index 567ac37dbd86..848f57a7d762 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -854,7 +854,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
 		else if (json_output)
 			dump_xlated_json(&dd, buf, member_len, opcodes, linum);
 		else if (visual)
-			dump_xlated_cfg(&dd, buf, member_len);
+			dump_xlated_cfg(&dd, buf, member_len, opcodes, linum);
 		else
 			dump_xlated_plain(&dd, buf, member_len, opcodes, linum);
 		kernel_syms_destroy(&dd);
diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
index 5fbe94aa8589..c5e03833fadf 100644
--- a/tools/bpf/bpftool/xlated_dumper.c
+++ b/tools/bpf/bpftool/xlated_dumper.c
@@ -361,7 +361,8 @@ void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
 }
 
 void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
-			   unsigned int start_idx)
+			   unsigned int start_idx,
+			   bool opcodes, bool linum)
 {
 	const struct bpf_insn_cbs cbs = {
 		.cb_print	= print_insn_for_graph,
@@ -405,7 +406,7 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
 
 			linfo = bpf_prog_linfo__lfind(prog_linfo, insn_off, 0);
 			if (linfo && linfo != last_linfo) {
-				btf_dump_linfo_dotlabel(btf, linfo);
+				btf_dump_linfo_dotlabel(btf, linfo, linum);
 				last_linfo = linfo;
 			}
 		}
@@ -413,6 +414,16 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
 		printf("%d: ", insn_off);
 		print_bpf_insn(&cbs, cur, true);
 
+		if (opcodes) {
+			printf("       ");
+			fprint_hex(stdout, cur, 8, " ");
+			if (double_insn && cur <= insn_end - 1) {
+				printf(" ");
+				fprint_hex(stdout, cur + 1, 8, " ");
+			}
+			printf("\\l\\\n");
+		}
+
 		if (cur != insn_end)
 			printf(" | ");
 	}
diff --git a/tools/bpf/bpftool/xlated_dumper.h b/tools/bpf/bpftool/xlated_dumper.h
index 54847e174273..9a946377b0e6 100644
--- a/tools/bpf/bpftool/xlated_dumper.h
+++ b/tools/bpf/bpftool/xlated_dumper.h
@@ -34,6 +34,7 @@ void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len,
 void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
 		       bool opcodes, bool linum);
 void dump_xlated_for_graph(struct dump_data *dd, void *buf, void *buf_end,
-			   unsigned int start_index);
+			   unsigned int start_index,
+			   bool opcodes, bool linum);
 
 #endif
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH bpf-next v2 3/5] bpftool: Support inline annotations when dumping the CFG of a program
  2023-03-27 11:06 ` [PATCH bpf-next v2 3/5] bpftool: Support inline annotations when dumping the CFG of a program Quentin Monnet
@ 2023-03-27 17:02   ` Eduard Zingerman
  2023-03-31 14:52     ` Quentin Monnet
  0 siblings, 1 reply; 13+ messages in thread
From: Eduard Zingerman @ 2023-03-27 17:02 UTC (permalink / raw)
  To: Quentin Monnet, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf

On Mon, 2023-03-27 at 12:06 +0100, Quentin Monnet wrote:
> We support dumping the control flow graph of loaded programs to the DOT
> format with bpftool, but so far this feature wouldn't display the source
> code lines available through BTF along with the eBPF bytecode. Let's add
> support for these annotations, to make it easier to read the graph.
> 
> In prog.c, we move the call to dump_xlated_cfg() in order to pass and
> use the full struct dump_data, instead of creating a minimal one in
> draw_bb_node().
> 
> We pass the pointer to this struct down to dump_xlated_for_graph() in
> xlated_dumper.c, where most of the logics is added. We deal with BTF
> mostly like we do for plain or JSON output, except that we cannot use a
> "nr_skip" value to skip a given number of linfo records (we don't
> process the BPF instructions linearly, and apart from the root of the
> graph we don't know how many records we should skip, so we just store
> the last linfo and make sure the new one we find is different before
> printing it).
> 
> When printing the source instructions to the label of a DOT graph node,
> there are a few subtleties to address. We want some special newline
> markers, and there are some characters that we must escape. To deal with
> them, we introduce a new dedicated function btf_dump_linfo_dotlabel() in
> btf_dumper.c. We'll reuse this function in a later commit to format the
> filepath, line, and column references as well.
> 
> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
> Acked-by: Stanislav Fomichev <sdf@google.com>
> ---
>  tools/bpf/bpftool/btf_dumper.c    | 32 +++++++++++++++++++++++++++++++
>  tools/bpf/bpftool/cfg.c           | 23 ++++++++++------------
>  tools/bpf/bpftool/cfg.h           |  4 +++-
>  tools/bpf/bpftool/main.h          |  2 ++
>  tools/bpf/bpftool/prog.c          | 17 +++++++---------
>  tools/bpf/bpftool/xlated_dumper.c | 32 ++++++++++++++++++++++++++++++-
>  6 files changed, 85 insertions(+), 25 deletions(-)
> 
> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
> index e7f6ec3a8f35..8bfc1b69497d 100644
> --- a/tools/bpf/bpftool/btf_dumper.c
> +++ b/tools/bpf/bpftool/btf_dumper.c
> @@ -821,3 +821,35 @@ void btf_dump_linfo_json(const struct btf *btf,
>  					BPF_LINE_INFO_LINE_COL(linfo->line_col));
>  	}
>  }
> +
> +static void dotlabel_puts(const char *s)
> +{
> +	for (; *s; ++s) {
> +		switch (*s) {
> +		case '\\':
> +		case '"':
> +		case '{':
> +		case '}':
> +		case '>':

The "case '<':" is missing, w/o it dot reports warnings as follows:

  Error: bad label format {; if (hdr + hdr_size <= data_end)...

I used existing bpt testcase for testing:

  $ cd <kernel>/tools/testing/selftests/bpf
  $ bpftool prog load bpf_flow.bpf.o /sys/fs/bpf/test-prog
  $ prog dump xlated pinned /sys/fs/bpf/test-prog visual > test.cfg
  $ dot -Tpng -O test.cfg
  
Also [1] says the following:

> Braces, vertical bars and angle brackets must be escaped with a
> backslash character if you wish them to appear as a literal
> character. Spaces are interpreted as separators between tokens, so
> they must be escaped if you want spaces in the text.

So, maybe escape spaces as well?

[1] https://graphviz.org/doc/info/shapes.html#record

> +		case '|':
> +			putchar('\\');
> +			__fallthrough;
> +		default:
> +			putchar(*s);
> +		}
> +	}
> +}
> +
> +void btf_dump_linfo_dotlabel(const struct btf *btf,
> +			     const struct bpf_line_info *linfo)
> +{
> +	const char *line = btf__name_by_offset(btf, linfo->line_off);
> +
> +	if (!line)
> +		return;
> +	line = ltrim(line);
> +
> +	printf("; ");
> +	dotlabel_puts(line);
> +	printf("\\l\\\n");
> +}
> diff --git a/tools/bpf/bpftool/cfg.c b/tools/bpf/bpftool/cfg.c
> index 1951219a9af7..9fdc1f0cdd6e 100644
> --- a/tools/bpf/bpftool/cfg.c
> +++ b/tools/bpf/bpftool/cfg.c
> @@ -380,7 +380,8 @@ static void cfg_destroy(struct cfg *cfg)
>  	}
>  }
>  
> -static void draw_bb_node(struct func_node *func, struct bb_node *bb)
> +static void
> +draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd)
>  {
>  	const char *shape;
>  
> @@ -398,13 +399,9 @@ static void draw_bb_node(struct func_node *func, struct bb_node *bb)
>  		printf("EXIT");
>  	} else {
>  		unsigned int start_idx;
> -		struct dump_data dd = {};
> -
> -		printf("{");
> -		kernel_syms_load(&dd);
> +		printf("{\\\n");
>  		start_idx = bb->head - func->start;
> -		dump_xlated_for_graph(&dd, bb->head, bb->tail, start_idx);
> -		kernel_syms_destroy(&dd);
> +		dump_xlated_for_graph(dd, bb->head, bb->tail, start_idx);
>  		printf("}");
>  	}
>  
> @@ -430,12 +427,12 @@ static void draw_bb_succ_edges(struct func_node *func, struct bb_node *bb)
>  	}
>  }
>  
> -static void func_output_bb_def(struct func_node *func)
> +static void func_output_bb_def(struct func_node *func, struct dump_data *dd)
>  {
>  	struct bb_node *bb;
>  
>  	list_for_each_entry(bb, &func->bbs, l) {
> -		draw_bb_node(func, bb);
> +		draw_bb_node(func, bb, dd);
>  	}
>  }
>  
> @@ -455,7 +452,7 @@ static void func_output_edges(struct func_node *func)
>  	       func_idx, ENTRY_BLOCK_INDEX, func_idx, EXIT_BLOCK_INDEX);
>  }
>  
> -static void cfg_dump(struct cfg *cfg)
> +static void cfg_dump(struct cfg *cfg, struct dump_data *dd)
>  {
>  	struct func_node *func;
>  
> @@ -463,14 +460,14 @@ static void cfg_dump(struct cfg *cfg)
>  	list_for_each_entry(func, &cfg->funcs, l) {
>  		printf("subgraph \"cluster_%d\" {\n\tstyle=\"dashed\";\n\tcolor=\"black\";\n\tlabel=\"func_%d ()\";\n",
>  		       func->idx, func->idx);
> -		func_output_bb_def(func);
> +		func_output_bb_def(func, dd);
>  		func_output_edges(func);
>  		printf("}\n");
>  	}
>  	printf("}\n");
>  }
>  
> -void dump_xlated_cfg(void *buf, unsigned int len)
> +void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len)
>  {
>  	struct bpf_insn *insn = buf;
>  	struct cfg cfg;
> @@ -479,7 +476,7 @@ void dump_xlated_cfg(void *buf, unsigned int len)
>  	if (cfg_build(&cfg, insn, len))
>  		return;
>  
> -	cfg_dump(&cfg);
> +	cfg_dump(&cfg, dd);
>  
>  	cfg_destroy(&cfg);
>  }
> diff --git a/tools/bpf/bpftool/cfg.h b/tools/bpf/bpftool/cfg.h
> index e144257ea6d2..909d17e6d4c2 100644
> --- a/tools/bpf/bpftool/cfg.h
> +++ b/tools/bpf/bpftool/cfg.h
> @@ -4,6 +4,8 @@
>  #ifndef __BPF_TOOL_CFG_H
>  #define __BPF_TOOL_CFG_H
>  
> -void dump_xlated_cfg(void *buf, unsigned int len);
> +#include "xlated_dumper.h"
> +
> +void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len);
>  
>  #endif /* __BPF_TOOL_CFG_H */
> diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
> index 0ef373cef4c7..e9ee514b22d4 100644
> --- a/tools/bpf/bpftool/main.h
> +++ b/tools/bpf/bpftool/main.h
> @@ -229,6 +229,8 @@ void btf_dump_linfo_plain(const struct btf *btf,
>  			  const char *prefix, bool linum);
>  void btf_dump_linfo_json(const struct btf *btf,
>  			 const struct bpf_line_info *linfo, bool linum);
> +void btf_dump_linfo_dotlabel(const struct btf *btf,
> +			     const struct bpf_line_info *linfo);
>  
>  struct nlattr;
>  struct ifinfomsg;
> diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
> index afbe3ec342c8..d855118f0d96 100644
> --- a/tools/bpf/bpftool/prog.c
> +++ b/tools/bpf/bpftool/prog.c
> @@ -840,11 +840,6 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
>  					      false))
>  				goto exit_free;
>  		}
> -	} else if (visual) {
> -		if (json_output)
> -			jsonw_null(json_wtr);
> -		else
> -			dump_xlated_cfg(buf, member_len);
>  	} else {
>  		kernel_syms_load(&dd);
>  		dd.nr_jited_ksyms = info->nr_jited_ksyms;
> @@ -854,12 +849,14 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
>  		dd.finfo_rec_size = info->func_info_rec_size;
>  		dd.prog_linfo = prog_linfo;
>  
> -		if (json_output)
> -			dump_xlated_json(&dd, buf, member_len, opcodes,
> -					 linum);
> +		if (json_output && visual)
> +			jsonw_null(json_wtr);

Should this be an error? Maybe check that json_output is false when
arguments are parsed and 'visual' is specified?

> +		else if (json_output)
> +			dump_xlated_json(&dd, buf, member_len, opcodes, linum);
> +		else if (visual)
> +			dump_xlated_cfg(&dd, buf, member_len);
>  		else
> -			dump_xlated_plain(&dd, buf, member_len, opcodes,
> -					  linum);
> +			dump_xlated_plain(&dd, buf, member_len, opcodes, linum);
>  		kernel_syms_destroy(&dd);
>  	}
>  
> diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
> index 3daa05d9bbb7..5fbe94aa8589 100644
> --- a/tools/bpf/bpftool/xlated_dumper.c
> +++ b/tools/bpf/bpftool/xlated_dumper.c
> @@ -369,20 +369,50 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
>  		.cb_imm		= print_imm,
>  		.private_data	= dd,
>  	};
> +	const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo;
> +	const struct bpf_line_info *last_linfo = NULL;
> +	struct bpf_func_info *record = dd->func_info;
>  	struct bpf_insn *insn_start = buf_start;
>  	struct bpf_insn *insn_end = buf_end;
>  	struct bpf_insn *cur = insn_start;
> +	struct btf *btf = dd->btf;
>  	bool double_insn = false;
> +	char func_sig[1024];
>  
>  	for (; cur <= insn_end; cur++) {
> +		unsigned int insn_off;
> +
>  		if (double_insn) {
>  			double_insn = false;
>  			continue;
>  		}
>  		double_insn = cur->code == (BPF_LD | BPF_IMM | BPF_DW);
>  
> -		printf("% 4d: ", (int)(cur - insn_start + start_idx));
> +		insn_off = (unsigned int)(cur - insn_start + start_idx);
> +		if (btf && record) {
> +			if (record->insn_off == insn_off) {
> +				btf_dumper_type_only(btf, record->type_id,
> +						     func_sig,
> +						     sizeof(func_sig));
> +				if (func_sig[0] != '\0')
> +					printf("; %s:\\l\\\n", func_sig);
> +				record = (void *)record + dd->finfo_rec_size;
> +			}
> +		}
> +
> +		if (prog_linfo) {
> +			const struct bpf_line_info *linfo;
> +
> +			linfo = bpf_prog_linfo__lfind(prog_linfo, insn_off, 0);
> +			if (linfo && linfo != last_linfo) {
> +				btf_dump_linfo_dotlabel(btf, linfo);
> +				last_linfo = linfo;
> +			}
> +		}
> +
> +		printf("%d: ", insn_off);
>  		print_bpf_insn(&cbs, cur, true);
> +
>  		if (cur != insn_end)
>  			printf(" | ");
>  	}


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH bpf-next v2 5/5] bpftool: Support printing opcodes and source file references in CFG
  2023-03-27 11:06 ` [PATCH bpf-next v2 5/5] bpftool: Support printing opcodes and source file references in CFG Quentin Monnet
@ 2023-03-27 17:04   ` Eduard Zingerman
  2023-03-31 14:52     ` Quentin Monnet
  0 siblings, 1 reply; 13+ messages in thread
From: Eduard Zingerman @ 2023-03-27 17:04 UTC (permalink / raw)
  To: Quentin Monnet, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf

On Mon, 2023-03-27 at 12:06 +0100, Quentin Monnet wrote:
> Add support for displaying opcodes or/and file references (filepath,
> line and column numbers) when dumping the control flow graphs of loaded
> BPF programs with bpftool.
> 
> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
> Acked-by: Stanislav Fomichev <sdf@google.com>
> ---
>  tools/bpf/bpftool/btf_dumper.c    | 19 ++++++++++++++++++-
>  tools/bpf/bpftool/cfg.c           | 22 ++++++++++++++--------
>  tools/bpf/bpftool/cfg.h           |  3 ++-
>  tools/bpf/bpftool/main.h          |  2 +-
>  tools/bpf/bpftool/prog.c          |  2 +-
>  tools/bpf/bpftool/xlated_dumper.c | 15 +++++++++++++--
>  tools/bpf/bpftool/xlated_dumper.h |  3 ++-
>  7 files changed, 51 insertions(+), 15 deletions(-)
> 
> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
> index 8bfc1b69497d..24835c3f9a1c 100644
> --- a/tools/bpf/bpftool/btf_dumper.c
> +++ b/tools/bpf/bpftool/btf_dumper.c
> @@ -841,7 +841,7 @@ static void dotlabel_puts(const char *s)
>  }
>  
>  void btf_dump_linfo_dotlabel(const struct btf *btf,
> -			     const struct bpf_line_info *linfo)
> +			     const struct bpf_line_info *linfo, bool linum)
>  {
>  	const char *line = btf__name_by_offset(btf, linfo->line_off);
>  
> @@ -849,6 +849,23 @@ void btf_dump_linfo_dotlabel(const struct btf *btf,
>  		return;
>  	line = ltrim(line);
>  
> +	if (linum) {
> +		const char *file = btf__name_by_offset(btf, linfo->file_name_off);
> +
> +		/* More forgiving on file because linum option is
> +		 * expected to provide more info than the already
> +		 * available src line.
> +		 */
> +		if (!file)
> +			file = "";
> +
> +		printf("; [file:");
> +		dotlabel_puts(file);
> +		printf("line_num:%u line_col:%u]\\l\\\n",

Space between file name and 'line_num' is missing.

Also, at-least for BPF test-cases the labels might become quite long,
which makes graph unnecessarily wide, e.g.:

  ; [file:/home/eddy/work/bpf-next/tools/testing/selftests/bpf/progs/bpf_flow.cline_num:97 line_col:34]\l\

The file names are encoded in full during compilation, but maybe
shorten long file names by removing some preceding levels
(and use shorter tags 'line:', 'col:', is 'file:' tag necessary at all?).
For example:

  ; [.../progs/bpf_flow.c line:97 col:34]\l\.

> +		       BPF_LINE_INFO_LINE_NUM(linfo->line_col),
> +		       BPF_LINE_INFO_LINE_COL(linfo->line_col));
> +	}
> +
>  	printf("; ");
>  	dotlabel_puts(line);
>  	printf("\\l\\\n");
> diff --git a/tools/bpf/bpftool/cfg.c b/tools/bpf/bpftool/cfg.c
> index 9fdc1f0cdd6e..eec437cca2ea 100644
> --- a/tools/bpf/bpftool/cfg.c
> +++ b/tools/bpf/bpftool/cfg.c
> @@ -381,7 +381,8 @@ static void cfg_destroy(struct cfg *cfg)
>  }
>  
>  static void
> -draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd)
> +draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd,
> +	     bool opcodes, bool linum)
>  {
>  	const char *shape;
>  
> @@ -401,7 +402,8 @@ draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd)
>  		unsigned int start_idx;
>  		printf("{\\\n");
>  		start_idx = bb->head - func->start;
> -		dump_xlated_for_graph(dd, bb->head, bb->tail, start_idx);
> +		dump_xlated_for_graph(dd, bb->head, bb->tail, start_idx,
> +				      opcodes, linum);
>  		printf("}");
>  	}
>  
> @@ -427,12 +429,14 @@ static void draw_bb_succ_edges(struct func_node *func, struct bb_node *bb)
>  	}
>  }
>  
> -static void func_output_bb_def(struct func_node *func, struct dump_data *dd)
> +static void
> +func_output_bb_def(struct func_node *func, struct dump_data *dd,
> +		   bool opcodes, bool linum)
>  {
>  	struct bb_node *bb;
>  
>  	list_for_each_entry(bb, &func->bbs, l) {
> -		draw_bb_node(func, bb, dd);
> +		draw_bb_node(func, bb, dd, opcodes, linum);
>  	}
>  }
>  
> @@ -452,7 +456,8 @@ static void func_output_edges(struct func_node *func)
>  	       func_idx, ENTRY_BLOCK_INDEX, func_idx, EXIT_BLOCK_INDEX);
>  }
>  
> -static void cfg_dump(struct cfg *cfg, struct dump_data *dd)
> +static void
> +cfg_dump(struct cfg *cfg, struct dump_data *dd, bool opcodes, bool linum)
>  {
>  	struct func_node *func;
>  
> @@ -460,14 +465,15 @@ static void cfg_dump(struct cfg *cfg, struct dump_data *dd)
>  	list_for_each_entry(func, &cfg->funcs, l) {
>  		printf("subgraph \"cluster_%d\" {\n\tstyle=\"dashed\";\n\tcolor=\"black\";\n\tlabel=\"func_%d ()\";\n",
>  		       func->idx, func->idx);
> -		func_output_bb_def(func, dd);
> +		func_output_bb_def(func, dd, opcodes, linum);
>  		func_output_edges(func);
>  		printf("}\n");
>  	}
>  	printf("}\n");
>  }
>  
> -void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len)
> +void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len,
> +		     bool opcodes, bool linum)
>  {
>  	struct bpf_insn *insn = buf;
>  	struct cfg cfg;
> @@ -476,7 +482,7 @@ void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len)
>  	if (cfg_build(&cfg, insn, len))
>  		return;
>  
> -	cfg_dump(&cfg, dd);
> +	cfg_dump(&cfg, dd, opcodes, linum);
>  
>  	cfg_destroy(&cfg);
>  }
> diff --git a/tools/bpf/bpftool/cfg.h b/tools/bpf/bpftool/cfg.h
> index 909d17e6d4c2..b3793f4e1783 100644
> --- a/tools/bpf/bpftool/cfg.h
> +++ b/tools/bpf/bpftool/cfg.h
> @@ -6,6 +6,7 @@
>  
>  #include "xlated_dumper.h"
>  
> -void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len);
> +void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len,
> +		     bool opcodes, bool linum);
>  
>  #endif /* __BPF_TOOL_CFG_H */
> diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
> index e9ee514b22d4..00d11ca6d3f2 100644
> --- a/tools/bpf/bpftool/main.h
> +++ b/tools/bpf/bpftool/main.h
> @@ -230,7 +230,7 @@ void btf_dump_linfo_plain(const struct btf *btf,
>  void btf_dump_linfo_json(const struct btf *btf,
>  			 const struct bpf_line_info *linfo, bool linum);
>  void btf_dump_linfo_dotlabel(const struct btf *btf,
> -			     const struct bpf_line_info *linfo);
> +			     const struct bpf_line_info *linfo, bool linum);
>  
>  struct nlattr;
>  struct ifinfomsg;
> diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
> index 567ac37dbd86..848f57a7d762 100644
> --- a/tools/bpf/bpftool/prog.c
> +++ b/tools/bpf/bpftool/prog.c
> @@ -854,7 +854,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
>  		else if (json_output)
>  			dump_xlated_json(&dd, buf, member_len, opcodes, linum);
>  		else if (visual)
> -			dump_xlated_cfg(&dd, buf, member_len);
> +			dump_xlated_cfg(&dd, buf, member_len, opcodes, linum);
>  		else
>  			dump_xlated_plain(&dd, buf, member_len, opcodes, linum);
>  		kernel_syms_destroy(&dd);
> diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
> index 5fbe94aa8589..c5e03833fadf 100644
> --- a/tools/bpf/bpftool/xlated_dumper.c
> +++ b/tools/bpf/bpftool/xlated_dumper.c
> @@ -361,7 +361,8 @@ void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
>  }
>  
>  void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
> -			   unsigned int start_idx)
> +			   unsigned int start_idx,
> +			   bool opcodes, bool linum)
>  {
>  	const struct bpf_insn_cbs cbs = {
>  		.cb_print	= print_insn_for_graph,
> @@ -405,7 +406,7 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
>  
>  			linfo = bpf_prog_linfo__lfind(prog_linfo, insn_off, 0);
>  			if (linfo && linfo != last_linfo) {
> -				btf_dump_linfo_dotlabel(btf, linfo);
> +				btf_dump_linfo_dotlabel(btf, linfo, linum);
>  				last_linfo = linfo;
>  			}
>  		}
> @@ -413,6 +414,16 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
>  		printf("%d: ", insn_off);
>  		print_bpf_insn(&cbs, cur, true);
>  
> +		if (opcodes) {
> +			printf("       ");

These spaces are treated as a single space by the dot renderer, as [1]
says: "Spaces are interpreted as separators between tokens, so they
must be escaped if you want spaces in the text."

[1] https://graphviz.org/doc/info/shapes.html#record

> +			fprint_hex(stdout, cur, 8, " ");
> +			if (double_insn && cur <= insn_end - 1) {
> +				printf(" ");
> +				fprint_hex(stdout, cur + 1, 8, " ");
> +			}
> +			printf("\\l\\\n");
> +		}
> +
>  		if (cur != insn_end)
>  			printf(" | ");
>  	}
> diff --git a/tools/bpf/bpftool/xlated_dumper.h b/tools/bpf/bpftool/xlated_dumper.h
> index 54847e174273..9a946377b0e6 100644
> --- a/tools/bpf/bpftool/xlated_dumper.h
> +++ b/tools/bpf/bpftool/xlated_dumper.h
> @@ -34,6 +34,7 @@ void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len,
>  void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
>  		       bool opcodes, bool linum);
>  void dump_xlated_for_graph(struct dump_data *dd, void *buf, void *buf_end,
> -			   unsigned int start_index);
> +			   unsigned int start_index,
> +			   bool opcodes, bool linum);
>  
>  #endif


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs
  2023-03-27 11:06 [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Quentin Monnet
                   ` (4 preceding siblings ...)
  2023-03-27 11:06 ` [PATCH bpf-next v2 5/5] bpftool: Support printing opcodes and source file references in CFG Quentin Monnet
@ 2023-03-27 17:56 ` Eduard Zingerman
  2023-03-31 14:53   ` Quentin Monnet
  5 siblings, 1 reply; 13+ messages in thread
From: Eduard Zingerman @ 2023-03-27 17:56 UTC (permalink / raw)
  To: Quentin Monnet, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf

On Mon, 2023-03-27 at 12:06 +0100, Quentin Monnet wrote:
> This set contains some improvements for bpftool's "visual" program dump
> option, which produces the control flow graph in a DOT format. The main
> objective is to add support for inline annotations on such graphs, so that
> we can have the C source code for the program showing up alongside the
> instructions, when available. The last commits also make it possible to
> display the line numbers or the bare opcodes in the graph, as supported by
> regular program dumps.
> 
> v2: Replace fputc(..., stdout) with putchar(...) in dotlabel_puts().

Hi Quentin,

It looks like currently there are no test cases for bpftool prog dump.
Borrowing an idea to mix bpf program with comments parsed by awk from
prog_tests/btf_dump.c it is possible to put together something like
below (although, would be much simpler as a bash script). Is it worth
the effort or dump format is too unstable?

Thanks,
Eduard

---

diff --git a/tools/testing/selftests/bpf/prog_tests/bpftool_cfg.c b/tools/testing/selftests/bpf/prog_tests/bpftool_cfg.c
new file mode 100644
index 000000000000..f582a93b5ee9
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpftool_cfg.c
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+
+#include "bpf/libbpf.h"
+#include "bpftool_cfg.skel.h"
+
+static int system_to_tmp(char *tmp_template, char *cmd)
+{
+	char buf[1024];
+	int fd, err;
+
+	fd = mkstemp(tmp_template);
+	if (!ASSERT_GE(fd, 0, "mkstemp"))
+		return fd;
+
+	snprintf(buf, sizeof(buf), "%s > %s", cmd, tmp_template);
+	err = system(buf);
+	if (err) {
+		PRINT_FAIL("Command %s failed: err %d\n", cmd, err);
+		remove(tmp_template);
+		return err;
+	}
+
+	return 0;
+}
+
+void test_bpftool_cfg(void)
+{
+	const char *prog_pin_path = "/sys/fs/bpf/bpftool_cfg_test_pin";
+	const char *bpftool = "./tools/build/bpftool/bpftool";
+	char bpftool_tmp[256] = "/tmp/bpftool_test_cfg.XXXXXX";
+	char awk_tmp[256] = "/tmp/bpftool_test_awk.XXXXXX";
+	struct bpftool_cfg *skel;
+	const char *test_file;
+	char cmd_buf[1024];
+	FILE *cmd;
+	int err;
+
+	skel = bpftool_cfg__open_and_load();
+	if (!skel) {
+		PRINT_FAIL("failed to load bpftool_cfg program: %d (%s)\n",
+			   errno, strerror(errno));
+		return;
+	}
+
+	err = bpf_program__pin(skel->progs.bpftool_cfg_nanosleep, prog_pin_path);
+	if (err) {
+		PRINT_FAIL("failed to pin bpftool_cfg program: err %d, errno = %d (%s)\n",
+			   err, errno, strerror(errno));
+		goto out;
+	}
+
+	/* When the test is run with O=, kselftest copies TEST_FILES
+	 * without preserving the directory structure.
+	 */
+	if (access("progs/bpftool_cfg.c", R_OK) == 0)
+		test_file = "progs/bpftool_cfg.c";
+	else if (access("bpftool_cfg.c", R_OK) == 0)
+		test_file = "bpftool_cfg.c";
+	else {
+		PRINT_FAIL("Can't find bpftool_cfg.c\n");
+		goto out_unpin;
+	}
+
+	cmd = fmemopen(cmd_buf, sizeof(cmd_buf), "w");
+	fprintf(cmd, "awk '");
+	fprintf(cmd, "    /END-BPFTOOL-CFG/   { out=0 } ");
+	fprintf(cmd, "    out                 { print $0 } ");
+	fprintf(cmd, "    /START-BPFTOOL-CFG/ { out=1 } ");
+	fprintf(cmd, "' '%s' > '%s'", test_file, awk_tmp);
+	fclose(cmd);
+	err = system_to_tmp(awk_tmp, cmd_buf);
+	if (!ASSERT_OK(err, "awk"))
+		goto out_unpin;
+
+	cmd = fmemopen(cmd_buf, sizeof(cmd_buf), "w");
+	fprintf(cmd, "%s prog dump xlated pinned %s visual", bpftool, prog_pin_path);
+	fclose(cmd);
+	err = system_to_tmp(bpftool_tmp, cmd_buf);
+	if (!ASSERT_OK(err, "bpftool"))
+		goto out_delete;
+
+	cmd = fmemopen(cmd_buf, sizeof(cmd_buf), "w");
+	fprintf(cmd, "diff -u %s %s", awk_tmp, bpftool_tmp);
+	fclose(cmd);
+	err = system(cmd_buf);
+	if (!ASSERT_OK(err, "diff"))
+		goto out_delete;
+
+out_delete:
+	if (awk_tmp[0])
+		remove(awk_tmp);
+	if (bpftool_tmp[0])
+		remove(bpftool_tmp);
+out_unpin:
+	bpf_program__unpin(skel->progs.bpftool_cfg_nanosleep, prog_pin_path);
+out:
+	bpftool_cfg__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/bpftool_cfg.c b/tools/testing/selftests/bpf/progs/bpftool_cfg.c
new file mode 100644
index 000000000000..d10c4e2cecbd
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpftool_cfg.c
@@ -0,0 +1,36 @@
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+#include "bpf_misc.h"
+
+/*
+START-BPFTOOL-CFG
+digraph "DOT graph for eBPF program" {
+subgraph "cluster_0" {
+	style="dashed";
+	color="black";
+	label="func_0 ()";
+	fn_0_bb_0 [shape=Mdiamond,style=filled,label="ENTRY"];
+
+	fn_0_bb_2 [shape=record,style=filled,label="{\
+; int bpftool_cfg_nanosleep(void * ctx):\l\
+; return 0;\l\
+0: (b4) w0 = 0\l\
+ | 1: (95) exit\l\
+}"];
+
+	fn_0_bb_1 [shape=Mdiamond,style=filled,label="EXIT"];
+
+	fn_0_bb_0:s -> fn_0_bb_2:n [style="solid,bold", color=black, weight=10, constraint=true];
+	fn_0_bb_2:s -> fn_0_bb_1:n [style="solid,bold", color=black, weight=10, constraint=true];
+	fn_0_bb_0:s -> fn_0_bb_1:n [style="invis", constraint=true];
+}
+}
+END-BPFTOOL-CFG
+*/
+
+SEC("tp/syscalls/sys_enter_getpid")
+int bpftool_cfg_nanosleep(void *ctx)
+{
+	return 0;
+}

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH bpf-next v2 3/5] bpftool: Support inline annotations when dumping the CFG of a program
  2023-03-27 17:02   ` Eduard Zingerman
@ 2023-03-31 14:52     ` Quentin Monnet
  0 siblings, 0 replies; 13+ messages in thread
From: Quentin Monnet @ 2023-03-31 14:52 UTC (permalink / raw)
  To: Eduard Zingerman, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf

Hi Eduard, apologies for the slow answer

2023-03-27 20:02 UTC+0300 ~ Eduard Zingerman <eddyz87@gmail.com>
> On Mon, 2023-03-27 at 12:06 +0100, Quentin Monnet wrote:
>> We support dumping the control flow graph of loaded programs to the DOT
>> format with bpftool, but so far this feature wouldn't display the source
>> code lines available through BTF along with the eBPF bytecode. Let's add
>> support for these annotations, to make it easier to read the graph.
>>
>> In prog.c, we move the call to dump_xlated_cfg() in order to pass and
>> use the full struct dump_data, instead of creating a minimal one in
>> draw_bb_node().
>>
>> We pass the pointer to this struct down to dump_xlated_for_graph() in
>> xlated_dumper.c, where most of the logics is added. We deal with BTF
>> mostly like we do for plain or JSON output, except that we cannot use a
>> "nr_skip" value to skip a given number of linfo records (we don't
>> process the BPF instructions linearly, and apart from the root of the
>> graph we don't know how many records we should skip, so we just store
>> the last linfo and make sure the new one we find is different before
>> printing it).
>>
>> When printing the source instructions to the label of a DOT graph node,
>> there are a few subtleties to address. We want some special newline
>> markers, and there are some characters that we must escape. To deal with
>> them, we introduce a new dedicated function btf_dump_linfo_dotlabel() in
>> btf_dumper.c. We'll reuse this function in a later commit to format the
>> filepath, line, and column references as well.
>>
>> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
>> Acked-by: Stanislav Fomichev <sdf@google.com>
>> ---
>>  tools/bpf/bpftool/btf_dumper.c    | 32 +++++++++++++++++++++++++++++++
>>  tools/bpf/bpftool/cfg.c           | 23 ++++++++++------------
>>  tools/bpf/bpftool/cfg.h           |  4 +++-
>>  tools/bpf/bpftool/main.h          |  2 ++
>>  tools/bpf/bpftool/prog.c          | 17 +++++++---------
>>  tools/bpf/bpftool/xlated_dumper.c | 32 ++++++++++++++++++++++++++++++-
>>  6 files changed, 85 insertions(+), 25 deletions(-)
>>
>> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
>> index e7f6ec3a8f35..8bfc1b69497d 100644
>> --- a/tools/bpf/bpftool/btf_dumper.c
>> +++ b/tools/bpf/bpftool/btf_dumper.c
>> @@ -821,3 +821,35 @@ void btf_dump_linfo_json(const struct btf *btf,
>>  					BPF_LINE_INFO_LINE_COL(linfo->line_col));
>>  	}
>>  }
>> +
>> +static void dotlabel_puts(const char *s)
>> +{
>> +	for (; *s; ++s) {
>> +		switch (*s) {
>> +		case '\\':
>> +		case '"':
>> +		case '{':
>> +		case '}':
>> +		case '>':
> 
> The "case '<':" is missing, w/o it dot reports warnings as follows:
> 
>   Error: bad label format {; if (hdr + hdr_size <= data_end)...

Good catch, thank you!

> 
> I used existing bpt testcase for testing:
> 
>   $ cd <kernel>/tools/testing/selftests/bpf
>   $ bpftool prog load bpf_flow.bpf.o /sys/fs/bpf/test-prog
>   $ prog dump xlated pinned /sys/fs/bpf/test-prog visual > test.cfg
>   $ dot -Tpng -O test.cfg
>   
> Also [1] says the following:
> 
>> Braces, vertical bars and angle brackets must be escaped with a
>> backslash character if you wish them to appear as a literal
>> character. Spaces are interpreted as separators between tokens, so
>> they must be escaped if you want spaces in the text.
> 
> So, maybe escape spaces as well?
> 
> [1] https://graphviz.org/doc/info/shapes.html#record

I hesitated a little, because we risk getting a wider output if we
preserve spaces. But it's maybe better to preserve the initial line
rather than to decide we can get rid of sequences with multiple
spaces... OK I'll escape them, too.

> 
>> +		case '|':
>> +			putchar('\\');
>> +			__fallthrough;
>> +		default:
>> +			putchar(*s);
>> +		}
>> +	}
>> +}
>> +
>> +void btf_dump_linfo_dotlabel(const struct btf *btf,
>> +			     const struct bpf_line_info *linfo)
>> +{
>> +	const char *line = btf__name_by_offset(btf, linfo->line_off);
>> +
>> +	if (!line)
>> +		return;
>> +	line = ltrim(line);
>> +
>> +	printf("; ");
>> +	dotlabel_puts(line);
>> +	printf("\\l\\\n");
>> +}
>> diff --git a/tools/bpf/bpftool/cfg.c b/tools/bpf/bpftool/cfg.c
>> index 1951219a9af7..9fdc1f0cdd6e 100644
>> --- a/tools/bpf/bpftool/cfg.c
>> +++ b/tools/bpf/bpftool/cfg.c
>> @@ -380,7 +380,8 @@ static void cfg_destroy(struct cfg *cfg)
>>  	}
>>  }
>>  
>> -static void draw_bb_node(struct func_node *func, struct bb_node *bb)
>> +static void
>> +draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd)
>>  {
>>  	const char *shape;
>>  
>> @@ -398,13 +399,9 @@ static void draw_bb_node(struct func_node *func, struct bb_node *bb)
>>  		printf("EXIT");
>>  	} else {
>>  		unsigned int start_idx;
>> -		struct dump_data dd = {};
>> -
>> -		printf("{");
>> -		kernel_syms_load(&dd);
>> +		printf("{\\\n");
>>  		start_idx = bb->head - func->start;
>> -		dump_xlated_for_graph(&dd, bb->head, bb->tail, start_idx);
>> -		kernel_syms_destroy(&dd);
>> +		dump_xlated_for_graph(dd, bb->head, bb->tail, start_idx);
>>  		printf("}");
>>  	}
>>  
>> @@ -430,12 +427,12 @@ static void draw_bb_succ_edges(struct func_node *func, struct bb_node *bb)
>>  	}
>>  }
>>  
>> -static void func_output_bb_def(struct func_node *func)
>> +static void func_output_bb_def(struct func_node *func, struct dump_data *dd)
>>  {
>>  	struct bb_node *bb;
>>  
>>  	list_for_each_entry(bb, &func->bbs, l) {
>> -		draw_bb_node(func, bb);
>> +		draw_bb_node(func, bb, dd);
>>  	}
>>  }
>>  
>> @@ -455,7 +452,7 @@ static void func_output_edges(struct func_node *func)
>>  	       func_idx, ENTRY_BLOCK_INDEX, func_idx, EXIT_BLOCK_INDEX);
>>  }
>>  
>> -static void cfg_dump(struct cfg *cfg)
>> +static void cfg_dump(struct cfg *cfg, struct dump_data *dd)
>>  {
>>  	struct func_node *func;
>>  
>> @@ -463,14 +460,14 @@ static void cfg_dump(struct cfg *cfg)
>>  	list_for_each_entry(func, &cfg->funcs, l) {
>>  		printf("subgraph \"cluster_%d\" {\n\tstyle=\"dashed\";\n\tcolor=\"black\";\n\tlabel=\"func_%d ()\";\n",
>>  		       func->idx, func->idx);
>> -		func_output_bb_def(func);
>> +		func_output_bb_def(func, dd);
>>  		func_output_edges(func);
>>  		printf("}\n");
>>  	}
>>  	printf("}\n");
>>  }
>>  
>> -void dump_xlated_cfg(void *buf, unsigned int len)
>> +void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len)
>>  {
>>  	struct bpf_insn *insn = buf;
>>  	struct cfg cfg;
>> @@ -479,7 +476,7 @@ void dump_xlated_cfg(void *buf, unsigned int len)
>>  	if (cfg_build(&cfg, insn, len))
>>  		return;
>>  
>> -	cfg_dump(&cfg);
>> +	cfg_dump(&cfg, dd);
>>  
>>  	cfg_destroy(&cfg);
>>  }
>> diff --git a/tools/bpf/bpftool/cfg.h b/tools/bpf/bpftool/cfg.h
>> index e144257ea6d2..909d17e6d4c2 100644
>> --- a/tools/bpf/bpftool/cfg.h
>> +++ b/tools/bpf/bpftool/cfg.h
>> @@ -4,6 +4,8 @@
>>  #ifndef __BPF_TOOL_CFG_H
>>  #define __BPF_TOOL_CFG_H
>>  
>> -void dump_xlated_cfg(void *buf, unsigned int len);
>> +#include "xlated_dumper.h"
>> +
>> +void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len);
>>  
>>  #endif /* __BPF_TOOL_CFG_H */
>> diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
>> index 0ef373cef4c7..e9ee514b22d4 100644
>> --- a/tools/bpf/bpftool/main.h
>> +++ b/tools/bpf/bpftool/main.h
>> @@ -229,6 +229,8 @@ void btf_dump_linfo_plain(const struct btf *btf,
>>  			  const char *prefix, bool linum);
>>  void btf_dump_linfo_json(const struct btf *btf,
>>  			 const struct bpf_line_info *linfo, bool linum);
>> +void btf_dump_linfo_dotlabel(const struct btf *btf,
>> +			     const struct bpf_line_info *linfo);
>>  
>>  struct nlattr;
>>  struct ifinfomsg;
>> diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
>> index afbe3ec342c8..d855118f0d96 100644
>> --- a/tools/bpf/bpftool/prog.c
>> +++ b/tools/bpf/bpftool/prog.c
>> @@ -840,11 +840,6 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
>>  					      false))
>>  				goto exit_free;
>>  		}
>> -	} else if (visual) {
>> -		if (json_output)
>> -			jsonw_null(json_wtr);
>> -		else
>> -			dump_xlated_cfg(buf, member_len);
>>  	} else {
>>  		kernel_syms_load(&dd);
>>  		dd.nr_jited_ksyms = info->nr_jited_ksyms;
>> @@ -854,12 +849,14 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
>>  		dd.finfo_rec_size = info->func_info_rec_size;
>>  		dd.prog_linfo = prog_linfo;
>>  
>> -		if (json_output)
>> -			dump_xlated_json(&dd, buf, member_len, opcodes,
>> -					 linum);
>> +		if (json_output && visual)
>> +			jsonw_null(json_wtr);
> 
> Should this be an error? Maybe check that json_output is false when
> arguments are parsed and 'visual' is specified?

Right, makes sense to turn this into an error.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH bpf-next v2 5/5] bpftool: Support printing opcodes and source file references in CFG
  2023-03-27 17:04   ` Eduard Zingerman
@ 2023-03-31 14:52     ` Quentin Monnet
  2023-03-31 15:03       ` Eduard Zingerman
  0 siblings, 1 reply; 13+ messages in thread
From: Quentin Monnet @ 2023-03-31 14:52 UTC (permalink / raw)
  To: Eduard Zingerman, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf

2023-03-27 20:04 UTC+0300 ~ Eduard Zingerman <eddyz87@gmail.com>
> On Mon, 2023-03-27 at 12:06 +0100, Quentin Monnet wrote:
>> Add support for displaying opcodes or/and file references (filepath,
>> line and column numbers) when dumping the control flow graphs of loaded
>> BPF programs with bpftool.
>>
>> Signed-off-by: Quentin Monnet <quentin@isovalent.com>
>> Acked-by: Stanislav Fomichev <sdf@google.com>
>> ---
>>  tools/bpf/bpftool/btf_dumper.c    | 19 ++++++++++++++++++-
>>  tools/bpf/bpftool/cfg.c           | 22 ++++++++++++++--------
>>  tools/bpf/bpftool/cfg.h           |  3 ++-
>>  tools/bpf/bpftool/main.h          |  2 +-
>>  tools/bpf/bpftool/prog.c          |  2 +-
>>  tools/bpf/bpftool/xlated_dumper.c | 15 +++++++++++++--
>>  tools/bpf/bpftool/xlated_dumper.h |  3 ++-
>>  7 files changed, 51 insertions(+), 15 deletions(-)
>>
>> diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
>> index 8bfc1b69497d..24835c3f9a1c 100644
>> --- a/tools/bpf/bpftool/btf_dumper.c
>> +++ b/tools/bpf/bpftool/btf_dumper.c
>> @@ -841,7 +841,7 @@ static void dotlabel_puts(const char *s)
>>  }
>>  
>>  void btf_dump_linfo_dotlabel(const struct btf *btf,
>> -			     const struct bpf_line_info *linfo)
>> +			     const struct bpf_line_info *linfo, bool linum)
>>  {
>>  	const char *line = btf__name_by_offset(btf, linfo->line_off);
>>  
>> @@ -849,6 +849,23 @@ void btf_dump_linfo_dotlabel(const struct btf *btf,
>>  		return;
>>  	line = ltrim(line);
>>  
>> +	if (linum) {
>> +		const char *file = btf__name_by_offset(btf, linfo->file_name_off);
>> +
>> +		/* More forgiving on file because linum option is
>> +		 * expected to provide more info than the already
>> +		 * available src line.
>> +		 */
>> +		if (!file)
>> +			file = "";
>> +
>> +		printf("; [file:");
>> +		dotlabel_puts(file);
>> +		printf("line_num:%u line_col:%u]\\l\\\n",
> 
> Space between file name and 'line_num' is missing.

My bad, thanks!

> 
> Also, at-least for BPF test-cases the labels might become quite long,
> which makes graph unnecessarily wide, e.g.:
> 
>   ; [file:/home/eddy/work/bpf-next/tools/testing/selftests/bpf/progs/bpf_flow.cline_num:97 line_col:34]\l\
> 
> The file names are encoded in full during compilation, but maybe
> shorten long file names by removing some preceding levels
> (and use shorter tags 'line:', 'col:', is 'file:' tag necessary at all?).
> For example:
> 
>   ; [.../progs/bpf_flow.c line:97 col:34]\l\.

I thought about that but was unsure. But yeah, I suppose the risk of
users getting confused about where the file is located is limited,
especially given that we still have regular program dump that will keep
the full path. OK I'll look into that for the next version.

> 
>> +		       BPF_LINE_INFO_LINE_NUM(linfo->line_col),
>> +		       BPF_LINE_INFO_LINE_COL(linfo->line_col));
>> +	}
>> +
>>  	printf("; ");
>>  	dotlabel_puts(line);
>>  	printf("\\l\\\n");
>> diff --git a/tools/bpf/bpftool/cfg.c b/tools/bpf/bpftool/cfg.c
>> index 9fdc1f0cdd6e..eec437cca2ea 100644
>> --- a/tools/bpf/bpftool/cfg.c
>> +++ b/tools/bpf/bpftool/cfg.c
>> @@ -381,7 +381,8 @@ static void cfg_destroy(struct cfg *cfg)
>>  }
>>  
>>  static void
>> -draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd)
>> +draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd,
>> +	     bool opcodes, bool linum)
>>  {
>>  	const char *shape;
>>  
>> @@ -401,7 +402,8 @@ draw_bb_node(struct func_node *func, struct bb_node *bb, struct dump_data *dd)
>>  		unsigned int start_idx;
>>  		printf("{\\\n");
>>  		start_idx = bb->head - func->start;
>> -		dump_xlated_for_graph(dd, bb->head, bb->tail, start_idx);
>> +		dump_xlated_for_graph(dd, bb->head, bb->tail, start_idx,
>> +				      opcodes, linum);
>>  		printf("}");
>>  	}
>>  
>> @@ -427,12 +429,14 @@ static void draw_bb_succ_edges(struct func_node *func, struct bb_node *bb)
>>  	}
>>  }
>>  
>> -static void func_output_bb_def(struct func_node *func, struct dump_data *dd)
>> +static void
>> +func_output_bb_def(struct func_node *func, struct dump_data *dd,
>> +		   bool opcodes, bool linum)
>>  {
>>  	struct bb_node *bb;
>>  
>>  	list_for_each_entry(bb, &func->bbs, l) {
>> -		draw_bb_node(func, bb, dd);
>> +		draw_bb_node(func, bb, dd, opcodes, linum);
>>  	}
>>  }
>>  
>> @@ -452,7 +456,8 @@ static void func_output_edges(struct func_node *func)
>>  	       func_idx, ENTRY_BLOCK_INDEX, func_idx, EXIT_BLOCK_INDEX);
>>  }
>>  
>> -static void cfg_dump(struct cfg *cfg, struct dump_data *dd)
>> +static void
>> +cfg_dump(struct cfg *cfg, struct dump_data *dd, bool opcodes, bool linum)
>>  {
>>  	struct func_node *func;
>>  
>> @@ -460,14 +465,15 @@ static void cfg_dump(struct cfg *cfg, struct dump_data *dd)
>>  	list_for_each_entry(func, &cfg->funcs, l) {
>>  		printf("subgraph \"cluster_%d\" {\n\tstyle=\"dashed\";\n\tcolor=\"black\";\n\tlabel=\"func_%d ()\";\n",
>>  		       func->idx, func->idx);
>> -		func_output_bb_def(func, dd);
>> +		func_output_bb_def(func, dd, opcodes, linum);
>>  		func_output_edges(func);
>>  		printf("}\n");
>>  	}
>>  	printf("}\n");
>>  }
>>  
>> -void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len)
>> +void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len,
>> +		     bool opcodes, bool linum)
>>  {
>>  	struct bpf_insn *insn = buf;
>>  	struct cfg cfg;
>> @@ -476,7 +482,7 @@ void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len)
>>  	if (cfg_build(&cfg, insn, len))
>>  		return;
>>  
>> -	cfg_dump(&cfg, dd);
>> +	cfg_dump(&cfg, dd, opcodes, linum);
>>  
>>  	cfg_destroy(&cfg);
>>  }
>> diff --git a/tools/bpf/bpftool/cfg.h b/tools/bpf/bpftool/cfg.h
>> index 909d17e6d4c2..b3793f4e1783 100644
>> --- a/tools/bpf/bpftool/cfg.h
>> +++ b/tools/bpf/bpftool/cfg.h
>> @@ -6,6 +6,7 @@
>>  
>>  #include "xlated_dumper.h"
>>  
>> -void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len);
>> +void dump_xlated_cfg(struct dump_data *dd, void *buf, unsigned int len,
>> +		     bool opcodes, bool linum);
>>  
>>  #endif /* __BPF_TOOL_CFG_H */
>> diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
>> index e9ee514b22d4..00d11ca6d3f2 100644
>> --- a/tools/bpf/bpftool/main.h
>> +++ b/tools/bpf/bpftool/main.h
>> @@ -230,7 +230,7 @@ void btf_dump_linfo_plain(const struct btf *btf,
>>  void btf_dump_linfo_json(const struct btf *btf,
>>  			 const struct bpf_line_info *linfo, bool linum);
>>  void btf_dump_linfo_dotlabel(const struct btf *btf,
>> -			     const struct bpf_line_info *linfo);
>> +			     const struct bpf_line_info *linfo, bool linum);
>>  
>>  struct nlattr;
>>  struct ifinfomsg;
>> diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
>> index 567ac37dbd86..848f57a7d762 100644
>> --- a/tools/bpf/bpftool/prog.c
>> +++ b/tools/bpf/bpftool/prog.c
>> @@ -854,7 +854,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
>>  		else if (json_output)
>>  			dump_xlated_json(&dd, buf, member_len, opcodes, linum);
>>  		else if (visual)
>> -			dump_xlated_cfg(&dd, buf, member_len);
>> +			dump_xlated_cfg(&dd, buf, member_len, opcodes, linum);
>>  		else
>>  			dump_xlated_plain(&dd, buf, member_len, opcodes, linum);
>>  		kernel_syms_destroy(&dd);
>> diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
>> index 5fbe94aa8589..c5e03833fadf 100644
>> --- a/tools/bpf/bpftool/xlated_dumper.c
>> +++ b/tools/bpf/bpftool/xlated_dumper.c
>> @@ -361,7 +361,8 @@ void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
>>  }
>>  
>>  void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
>> -			   unsigned int start_idx)
>> +			   unsigned int start_idx,
>> +			   bool opcodes, bool linum)
>>  {
>>  	const struct bpf_insn_cbs cbs = {
>>  		.cb_print	= print_insn_for_graph,
>> @@ -405,7 +406,7 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
>>  
>>  			linfo = bpf_prog_linfo__lfind(prog_linfo, insn_off, 0);
>>  			if (linfo && linfo != last_linfo) {
>> -				btf_dump_linfo_dotlabel(btf, linfo);
>> +				btf_dump_linfo_dotlabel(btf, linfo, linum);
>>  				last_linfo = linfo;
>>  			}
>>  		}
>> @@ -413,6 +414,16 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
>>  		printf("%d: ", insn_off);
>>  		print_bpf_insn(&cbs, cur, true);
>>  
>> +		if (opcodes) {
>> +			printf("       ");
> 
> These spaces are treated as a single space by the dot renderer, as [1]
> says: "Spaces are interpreted as separators between tokens, so they
> must be escaped if you want spaces in the text."
> 
> [1] https://graphviz.org/doc/info/shapes.html#record

I noticed, I kept the multiple spaces to make the DOT output slightly
more readable. But it would maybe make sense to indent more in the graph
as well, if it doesn't make opcode sequences wider than instructions.
I'll have a look at this.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs
  2023-03-27 17:56 ` [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Eduard Zingerman
@ 2023-03-31 14:53   ` Quentin Monnet
  0 siblings, 0 replies; 13+ messages in thread
From: Quentin Monnet @ 2023-03-31 14:53 UTC (permalink / raw)
  To: Eduard Zingerman, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf

2023-03-27 20:56 UTC+0300 ~ Eduard Zingerman <eddyz87@gmail.com>
> On Mon, 2023-03-27 at 12:06 +0100, Quentin Monnet wrote:
>> This set contains some improvements for bpftool's "visual" program dump
>> option, which produces the control flow graph in a DOT format. The main
>> objective is to add support for inline annotations on such graphs, so that
>> we can have the C source code for the program showing up alongside the
>> instructions, when available. The last commits also make it possible to
>> display the line numbers or the bare opcodes in the graph, as supported by
>> regular program dumps.
>>
>> v2: Replace fputc(..., stdout) with putchar(...) in dotlabel_puts().
> 
> Hi Quentin,
> 
> It looks like currently there are no test cases for bpftool prog dump.
> Borrowing an idea to mix bpf program with comments parsed by awk from
> prog_tests/btf_dump.c it is possible to put together something like
> below (although, would be much simpler as a bash script). Is it worth
> the effort or dump format is too unstable?
> 
> Thanks,
> Eduard

Correct, I don't think we have tests for that at the moment.

But yes, I would love to get a test like this (and more bpftool tests in
general, when I can find cycles). I don't mind a bash script,
personally; your bpftool_cfg.c is mostly a succession of commands, and
the other tests we have for bpftool are in bash or Python anyway
(test_bpftool*). We could use bpftool to load the program (I suppose
it's debatable whether it's good to use bpftool itself to set up a test
for bpftool; on the other hand, it's heavily based on libbpf, so using
libbpf directly doesn't seem to make that much of a difference).

As for the stability of the output, generally the produced BPF bytecode
is obviously subject to change, but for a program so simple as "return
0;" we should be mostly good. Just make sure you specify the ISA version
when you compile the program, with < v3 we would load 64 bits instead of
32 in the first instruction. On bpftool's side, I don't expect the CFG
output to change much, so no problem.

Thanks a lot for your review, and for working on this test!

I'll send a v3, probably sometime next week.
Quentin

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH bpf-next v2 5/5] bpftool: Support printing opcodes and source file references in CFG
  2023-03-31 14:52     ` Quentin Monnet
@ 2023-03-31 15:03       ` Eduard Zingerman
  0 siblings, 0 replies; 13+ messages in thread
From: Eduard Zingerman @ 2023-03-31 15:03 UTC (permalink / raw)
  To: Quentin Monnet, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, bpf

On Fri, 2023-03-31 at 15:52 +0100, Quentin Monnet wrote:
[...]
> > > @@ -413,6 +414,16 @@ void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
> > >  		printf("%d: ", insn_off);
> > >  		print_bpf_insn(&cbs, cur, true);
> > >  
> > > +		if (opcodes) {
> > > +			printf("       ");
> > 
> > These spaces are treated as a single space by the dot renderer, as [1]
> > says: "Spaces are interpreted as separators between tokens, so they
> > must be escaped if you want spaces in the text."
> > 
> > [1] https://graphviz.org/doc/info/shapes.html#record
> 
> I noticed, I kept the multiple spaces to make the DOT output slightly
> more readable. But it would maybe make sense to indent more in the graph
> as well, if it doesn't make opcode sequences wider than instructions.
> I'll have a look at this.

Oh, sorry, that did not occur to me, making DOT itself more readable
makes total sense.

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-03-31 15:03 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-27 11:06 [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Quentin Monnet
2023-03-27 11:06 ` [PATCH bpf-next v2 1/5] bpftool: Fix documentation about line info display for prog dumps Quentin Monnet
2023-03-27 11:06 ` [PATCH bpf-next v2 2/5] bpftool: Fix bug for long instructions in program CFG dumps Quentin Monnet
2023-03-27 11:06 ` [PATCH bpf-next v2 3/5] bpftool: Support inline annotations when dumping the CFG of a program Quentin Monnet
2023-03-27 17:02   ` Eduard Zingerman
2023-03-31 14:52     ` Quentin Monnet
2023-03-27 11:06 ` [PATCH bpf-next v2 4/5] bpftool: Support "opcodes", "linum", "visual" simultaneously Quentin Monnet
2023-03-27 11:06 ` [PATCH bpf-next v2 5/5] bpftool: Support printing opcodes and source file references in CFG Quentin Monnet
2023-03-27 17:04   ` Eduard Zingerman
2023-03-31 14:52     ` Quentin Monnet
2023-03-31 15:03       ` Eduard Zingerman
2023-03-27 17:56 ` [PATCH bpf-next v2 0/5] bpftool: Add inline annotations when dumping program CFGs Eduard Zingerman
2023-03-31 14:53   ` Quentin Monnet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).