bpftool: Add inline annotations when dumping program CFGs
v3 cover bpftool: Add inline annotations when dumping program CFGs
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.
v3:
- Fixed formatting of DOT graph: escape spaces, and remove indent that would cause some unwanted spaces to show up in the resulting graph.
- Don't print line information if the record is empty.
- Add '<' and ' ' to the list of escaped characters for generting the DOT graph.
- Truncate long file paths, use shorter field names ("line", "col") for code location information in the graph, add missing separator space.
- Add a commit to return an error if JSON output and CFG are both required.
- Add a drive-by, clean up commit for bash completion (avoid unnecessary calls to _bpftool_once_attr()).
v2: Replace fputc(..., stdout) with putchar(...) in dotlabel_puts().
Quentin Monnet (7):
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: Return an error on prog dumps if both CFG and JSON are
required
bpftool: Support "opcodes", "linum", "visual" simultaneously
bpftool: Support printing opcodes and source file references in CFG
bpftool: Clean up _bpftool_once_attr() calls in bash completion
.../bpftool/Documentation/bpftool-prog.rst | 18 ++-- tools/bpf/bpftool/bash-completion/bpftool | 42 +++++----- tools/bpf/bpftool/btf_dumper.c | 83 +++++++++++++++++++ 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 | 54 +++++++++++- tools/bpf/bpftool/xlated_dumper.h | 3 +- 9 files changed, 226 insertions(+), 88 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/7 bpftool: Fix documentation about line info display for prog dumps
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.
--- tools/bpf/bpftool/Documentation/bpftool-prog.rst | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-prog.rst +4 -6
@@ -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*.
Signature · 1 line
2.34.1
PATCH v3 2/7 bpftool: Fix bug for long instructions in program CFG dumps
In reply to Quentin MonnetWhen 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.
--- tools/bpf/bpftool/xlated_dumper.c | 7 +++++++ 1 file changed, 7 insertions(+)
tools/bpf/bpftool/xlated_dumper.c +7 -0
@@ -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)
Signature · 1 line
2.34.1
PATCH v3 3/7 bpftool: Support inline annotations when dumping the CFG of a program
In reply to Quentin MonnetWe 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.
--- tools/bpf/bpftool/btf_dumper.c | 34 +++++++++++++++++++++++++++++++ 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 | 34 +++++++++++++++++++++++++++++-- 6 files changed, 88 insertions(+), 26 deletions(-)
tools/bpf/bpftool/btf_dumper.c +34 -0
@@ -821,3 +821,37 @@ 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 '>' : +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 || !strlen (line)) +return ; + line =ltrim (line); + +printf ("; " ); +dotlabel_puts (line); +printf ("\\l\\\n" ); +}
tools/bpf/bpftool/cfg.c +10 -13
@@ -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); }
tools/bpf/bpftool/cfg.h +3 -1
@@ -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 */
tools/bpf/bpftool/main.h +2 -0
@@ -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 ;
tools/bpf/bpftool/prog.c +7 -10
@@ -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); }
tools/bpf/bpftool/xlated_dumper.c +32 -2
@@ -369,21 +369,51 @@ 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 (" | " ); +printf ("| " ); } }
Signature · 1 line
2.34.1
Sven Schnelle
reviewerRe: [PATCH bpf-next v3 3/7] bpftool: Support inline annotations when dumping the CFG of a program
Quoted 13 lines · Quentin Monnet
tools/bpf/bpftool/btf_dumper.c +5
-0@@ -821,3 +821,37 @@ 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) {
tools/bpf/bpftool/btf_dumper.c void btf_dump_linfo_json(const struct btf *btf, · line 838
Quoted 10 lines
tools/bpf/bpftool/btf_dumper.c +10
-0+case '\\' : +case '"' : +case '{' : +case '}' : +case '<' : +case '>' : +case '|' : +case ' ' : +putchar ('\\' ); + __fallthrough;
Is __fallthrough correct? I see the following compile error on s390 in linux-next (20230412):
CC btf_dumper.obtf_dumper.c: In function ‘dotlabel_puts’:
btf_dumper.c:838:25: error: ‘__fallthrough’ undeclared (first use in this function); did you mean ‘fallthrough’?
838 | __fallthrough;
| ^~~~~~~~~~~~~removing the two underscores fixes this.
tools/bpf/bpftool/btf_dumper.c void btf_dump_linfo_json(const struct btf *btf, · line 843
Quoted 5 lines
tools/bpf/bpftool/btf_dumper.c +5
-0+default : +putchar (*s); + } + } +}
Thanks,
Sven
Re: [PATCH bpf-next v3 3/7] bpftool: Support inline annotations when dumping the CFG of a program
2023-04-12 08:04 UTC+0200 ~ Sven Schnelle <[email protected]>
Quoted 29 lines
Quoted 13 lines · Quentin Monnet
tools/bpf/bpftool/btf_dumper.c +5
-0@@ -821,3 +821,37 @@ 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) {tools/bpf/bpftool/btf_dumper.c void btf_dump_linfo_json(const struct btf *btf, · line 838
Quoted 10 lines
tools/bpf/bpftool/btf_dumper.c +10
-0+case '\\' : +case '"' : +case '{' : +case '}' : +case '<' : +case '>' : +case '|' : +case ' ' : +putchar ('\\' ); + __fallthrough;Is
__fallthroughcorrect? I see the following compile error on s390 in linux-next (20230412):
Quoted 7 lines
CC btf_dumper.o
btf_dumper.c: In function ‘dotlabel_puts’:
btf_dumper.c:838:25: error: ‘__fallthrough’ undeclared (first use in this function); did you mean ‘fallthrough’?838 | __fallthrough; | ^~~~~~~~~~~~~removing the two underscores fixes this.
I thought so? Perf seems to use the double underscores as well. Just "fallthrough" does not seem to be the right fix anyway, it gives me an error similar to yours on x86_64 with "fallthrough" undeclared.
The definition should be pulled from tools/include/linux/compiler.h (and .../compiler-gcc.h). I thought this file would be at least included from bpftool's main.h, in turn included in btf_dumper.c. Looking at the chain of inclusions, on my system I get the following path:
$ CFLAGS=-H make btf_dumper.o
[...]
. /root/dev/linux/tools/include/linux/bitops.h
[...]
.. /root/dev/linux/tools/include/linux/bits.h
[...]
... /root/dev/linux/tools/include/linux/build_bug.h
.... /root/dev/linux/tools/include/linux/compiler.h
..... /root/dev/linux/tools/include/linux/compiler_types.h
...... /root/dev/linux/tools/include/linux/compiler-gcc.h
[...]What do you get on your side?
If you add "#include <linux/compiler.h>" to btf_dumper.c directly, does it fix the issue?
Quentin
Sven Schnelle
reviewerRe: [PATCH bpf-next v3 3/7] bpftool: Support inline annotations when dumping the CFG of a program
Quoted 59 lines · Quentin Monnet
2023-04-12 08:04 UTC+0200 ~ Sven Schnelle <[email protected]>
Quoted 29 lines
Quoted 13 lines · Quentin Monnet
tools/bpf/bpftool/btf_dumper.c +5
-0@@ -821,3 +821,37 @@ 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) {tools/bpf/bpftool/btf_dumper.c void btf_dump_linfo_json(const struct btf *btf, · line 838
Quoted 10 lines
tools/bpf/bpftool/btf_dumper.c +10
-0+case '\\' : +case '"' : +case '{' : +case '}' : +case '<' : +case '>' : +case '|' : +case ' ' : +putchar ('\\' ); + __fallthrough;Is
__fallthroughcorrect? I see the following compile error on s390 in linux-next (20230412):Quoted 7 lines
CC btf_dumper.o
btf_dumper.c: In function ‘dotlabel_puts’:
btf_dumper.c:838:25: error: ‘__fallthrough’ undeclared (first use in this function); did you mean ‘fallthrough’?838 | __fallthrough; | ^~~~~~~~~~~~~removing the two underscores fixes this.
I thought so? Perf seems to use the double underscores as well. Just "fallthrough" does not seem to be the right fix anyway, it gives me an error similar to yours on
x86_64with "fallthrough" undeclared.The definition should be pulled from
tools/include/linux/compiler.h(and.../compiler-gcc.h). I thought this file would be at least included from bpftool'smain.h, in turn included inbtf_dumper.c. Looking at the chain of inclusions, on my system I get the following path:$ CFLAGS=-H make btf_dumper.o [...] . /root/dev/linux/tools/include/linux/bitops.h [...] .. /root/dev/linux/tools/include/linux/bits.h [...] ... /root/dev/linux/tools/include/linux/build_bug.h .... /root/dev/linux/tools/include/linux/compiler.h ..... /root/dev/linux/tools/include/linux/compiler_types.h ...... /root/dev/linux/tools/include/linux/compiler-gcc.h [...]
Quoted 4 lines
What do you get on your side?
If you add "#include <
linux/compiler.h>" tobtf_dumper.cdirectly, does it fix the issue?
This seems to clash with:
commit f7a858bffcddaaf70c71b6b656e7cc21b6107cec
Author: Liam Howlett <[email protected]> Date: Fri Nov 25 15:50:16 2022 +0000
tools: Rename __fallthrough to fallthrough
Rename the fallthrough attribute to better align with the kernel version. Copy the definition from include/linux/compiler_attributes.h including the #else clause. Adding the #else clause allows the tools compiler.h header to drop the check for a definition entirely and keeps both definitions together.
Change any __fallthrough statements to fallthrough anywhere it was used within perf.
This allows other tools to use the same key word as the kernel.
Which was also merged in linux-next.
Re: [PATCH bpf-next v3 3/7] bpftool: Support inline annotations when dumping the CFG of a program
2023-04-12 14:26 UTC+0200 ~ Sven Schnelle <[email protected]>
Quoted 80 lines
Quoted 59 lines · Quentin Monnet
2023-04-12 08:04 UTC+0200 ~ Sven Schnelle <[email protected]>
Quoted 29 lines
Quoted 13 lines · Quentin Monnet
tools/bpf/bpftool/btf_dumper.c +5
-0@@ -821,3 +821,37 @@ 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) {tools/bpf/bpftool/btf_dumper.c void btf_dump_linfo_json(const struct btf *btf, · line 838
Quoted 10 lines
tools/bpf/bpftool/btf_dumper.c +10
-0+case '\\' : +case '"' : +case '{' : +case '}' : +case '<' : +case '>' : +case '|' : +case ' ' : +putchar ('\\' ); + __fallthrough;Is
__fallthroughcorrect? I see the following compile error on s390 in linux-next (20230412):Quoted 7 lines
CC btf_dumper.o
btf_dumper.c: In function ‘dotlabel_puts’:
btf_dumper.c:838:25: error: ‘__fallthrough’ undeclared (first use in this function); did you mean ‘fallthrough’?838 | __fallthrough; | ^~~~~~~~~~~~~removing the two underscores fixes this.
I thought so? Perf seems to use the double underscores as well. Just "fallthrough" does not seem to be the right fix anyway, it gives me an error similar to yours on
x86_64with "fallthrough" undeclared.The definition should be pulled from
tools/include/linux/compiler.h(and.../compiler-gcc.h). I thought this file would be at least included from bpftool'smain.h, in turn included inbtf_dumper.c. Looking at the chain of inclusions, on my system I get the following path:$ CFLAGS=-H make btf_dumper.o [...] . /root/dev/linux/tools/include/linux/bitops.h [...] .. /root/dev/linux/tools/include/linux/bits.h [...] ... /root/dev/linux/tools/include/linux/build_bug.h .... /root/dev/linux/tools/include/linux/compiler.h ..... /root/dev/linux/tools/include/linux/compiler_types.h ...... /root/dev/linux/tools/include/linux/compiler-gcc.h [...]Quoted 4 lines
What do you get on your side?
If you add "#include <
linux/compiler.h>" tobtf_dumper.cdirectly, does it fix the issue?This seems to clash with:
commit
f7a858bffcddaaf70c71b6b656e7cc21b6107cecAuthor: Liam Howlett <[email protected]> Date: Fri Nov 25 15:50:16 2022 +0000tools: Rename
__fallthroughto fallthroughRename the fallthrough attribute to better align with the kernel version. Copy the definition from
include/linux/compiler_attributes.hincluding the #else clause. Adding the #else clause allows the toolscompiler.hheader to drop the check for a definition entirely and keeps both definitions together.
Quoted 6 lines
Change any
__fallthroughstatements to fallthrough anywhere it was used within perf.This allows other tools to use the same key word as the kernel.
Which was also merged in linux-next.
Right, I was not aware of this commit. In that case, replacing with "fallthrough" in linux-next makes sense indeed.
Thomas Richter just submitted that fix at
https://lore.kernel.org/all/[email protected]/.
Thanks!
Quentin
PATCH v3 4/7 bpftool: Return an error on prog dumps if both CFG and JSON are required
In reply to Quentin MonnetWe do not support JSON output for control flow graphs of programs with bpftool. So far, requiring both the CFG and JSON output would result in producing a null JSON object. It makes more sense to raise an error directly when parsing command line arguments and options, so that users know they won't get any output they might expect.
If JSON is required for the graph, we leave it to Graphviz instead:
# bpftool prog dump xlated <REF> visual | dot -Tjson
--- tools/bpf/bpftool/bash-completion/bpftool | 9 ++++++--- tools/bpf/bpftool/prog.c | 8 +++++--- 2 files changed, 11 insertions(+), 6 deletions(-)
tools/bpf/bpftool/bash-completion/bpftool +6 -3
@@ -255,16 +255,19 @@ _bpftool_map_update_get_name() _bpftool() { - local cur prev words objword + local cur prev words objword json=0 _init_completion || return # Deal with options if [[ ${words[cword]} == -* ]]; then local c='--version --json --pretty --bpffs --mapcompat --debug \ - --use-loader --base-btf' + --use-loader --base-btf' COMPREPLY=( $( compgen -W "$c" -- "$cur" ) ) return 0 fi + if _bpftool_search_list -j --json -p --pretty; then + json=1 + fi # Deal with simplest keywords case $prev in
@@ -367,7 +370,7 @@ _bpftool() ;; *) _bpftool_once_attr 'file' - if _bpftool_search_list 'xlated'; then + if _bpftool_search_list 'xlated' && [[ "$json" == 0 ]]; then COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \ "$cur" ) ) else
tools/bpf/bpftool/prog.c +5 -3
@@ -849,9 +849,7 @@ 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 && visual) -jsonw_null (json_wtr); -else if (json_output) +if (json_output)dump_xlated_json (&dd, buf, member_len, opcodes, linum);else if (visual)dump_xlated_cfg (&dd, buf, member_len);
@@ -940,6 +938,10 @@ static int do_dump(int argc, char **argv)usage (); goto exit_close; } +if (json_output && visual) { +p_err ("'visual' is not compatible with JSON output" ); + goto exit_close; + }if (json_output && nb_fds >1 )jsonw_start_array (json_wtr);/* root array */
Signature · 1 line
2.34.1
PATCH v3 5/7 bpftool: Support "opcodes", "linum", "visual" simultaneously
In reply to Quentin MonnetWhen 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.
--- .../bpftool/Documentation/bpftool-prog.rst | 8 +-- tools/bpf/bpftool/bash-completion/bpftool | 17 +++--- tools/bpf/bpftool/prog.c | 61 ++++++++++--------- 3 files changed, 47 insertions(+), 39 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-prog.rst +4 -4
@@ -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,
tools/bpf/bpftool/bash-completion/bpftool +10 -7
@@ -271,7 +271,7 @@ _bpftool() # Deal with simplest keywords case $prev in - help|hex|opcodes|visual|linum) + help|hex) return 0 ;; tag)
@@ -369,13 +369,16 @@ _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 opcodes' if _bpftool_search_list 'xlated' && [[ "$json" == 0 ]]; then - COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \ - "$cur" ) ) - else - COMPREPLY+=( $( compgen -W 'opcodes linum' -- \ - "$cur" ) ) + _bpftool_once_attr 'visual' fi return 0 ;;
tools/bpf/bpftool/prog.c +33 -28
78 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 6/7 bpftool: Support printing opcodes and source file references in CFG
In reply to Quentin MonnetAdd 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.
The filepaths in the records are absolute. To avoid blocks on the graph to get too wide, we truncate them when they get too long (but we always keep the entire file name). In the unlikely case where the resulting file name is ambiguous, it remains possible to get the full path with a regular dump (no CFG).
--- tools/bpf/bpftool/btf_dumper.c | 51 ++++++++++++++++++++++++++++++- 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, 83 insertions(+), 15 deletions(-)
tools/bpf/bpftool/btf_dumper.c +50 -1
@@ -842,8 +842,37 @@ static void dotlabel_puts(const char *s) } } +static const char *shorten_path (const char *path) +{ +const unsigned int MAX_PATH_LEN =32 ; +size_t len =strlen (path); +const char *shortpath; + +if (len <=MAX_PATH_LEN ) +return path; + +/* Search for last '/' under the MAX_PATH_LEN limit */ + shortpath =strchr (path + len -MAX_PATH_LEN ,'/' ); +if (shortpath) { +if (shortpath < path +strlen ("..." )) +/* We removed a very short prefix, e.g. "/w", and we'll +* make the path longer by prefixing with the ellipsis. +* Not worth it, keep initial path. +*/ +return path; +return shortpath; + } + +/* File base name length is > MAX_PATH_LEN, search for last '/' */ + shortpath =strrchr (path,'/' ); +if (shortpath) +return shortpath; + +return path; +} +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);
@@ -851,6 +880,26 @@ 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); +const char *shortfile; + +/* More forgiving on file because linum option is +* expected to provide more info than the already +* available src line. +*/ +if (!file) + shortfile ="" ; +else + shortfile =shorten_path (file); + +printf ("; [%s" , shortfile > file ?"..." :"" ); +dotlabel_puts (shortfile); +printf (" line:%u 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" );
tools/bpf/bpftool/cfg.c +14 -8
@@ -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); }
tools/bpf/bpftool/cfg.h +2 -1
@@ -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 */
tools/bpf/bpftool/main.h +1 -1
@@ -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 ;
tools/bpf/bpftool/prog.c +1 -1
@@ -852,7 +852,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,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);
tools/bpf/bpftool/xlated_dumper.c +13 -2
@@ -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 ("| " ); }
tools/bpf/bpftool/xlated_dumper.h +2 -1
@@ -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
Signature · 1 line
2.34.1
PATCH v3 7/7 bpftool: Clean up _bpftool_once_attr() calls in bash completion
In reply to Quentin MonnetIn bpftool's bash completion file, function _bpftool_once_attr() is able to process multiple arguments. There are a few locations where this function is called multiple times in a row, each time for a single argument; let's pass all arguments instead to minimize the number of function calls required for the completion.
--- tools/bpf/bpftool/bash-completion/bpftool | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-)
tools/bpf/bpftool/bash-completion/bpftool +3 -13
@@ -508,10 +508,7 @@ _bpftool() ;; *) COMPREPLY=( $( compgen -W "map" -- "$cur" ) ) - _bpftool_once_attr 'type' - _bpftool_once_attr 'dev' - _bpftool_once_attr 'pinmaps' - _bpftool_once_attr 'autoattach' + _bpftool_once_attr 'type dev pinmaps autoattach' return 0 ;; esac
@@ -736,16 +733,10 @@ _bpftool() esac ;; *) - _bpftool_once_attr 'type' - _bpftool_once_attr 'key' - _bpftool_once_attr 'value' - _bpftool_once_attr 'entries' - _bpftool_once_attr 'name' - _bpftool_once_attr 'flags' + _bpftool_once_attr 'type key value entries name flags dev' if _bpftool_search_list 'array_of_maps' 'hash_of_maps'; then _bpftool_once_attr 'inner_map' fi - _bpftool_once_attr 'dev' return 0 ;; esac
@@ -886,8 +877,7 @@ _bpftool() return 0 ;; *) - _bpftool_once_attr 'cpu' - _bpftool_once_attr 'index' + _bpftool_once_attr 'cpu index' return 0 ;; esac
Signature · 1 line
2.34.1
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Wed, 5 Apr 2023 14:21:13 +0100 you 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.
[...]
Here is the summary with links:
- [bpf-next,v3,1/7] bpftool: Fix documentation about line info display for prog dumps
https://git.kernel.org/bpf/bpf-next/c/e27f0f1620b6 - [bpf-next,v3,2/7] bpftool: Fix bug for long instructions in program CFG dumps https://git.kernel.org/bpf/bpf-next/c/67cf52cdb6c8 - [bpf-next,v3,3/7] bpftool: Support inline annotations when dumping the CFG of a program https://git.kernel.org/bpf/bpf-next/c/9fd496848b1c - [bpf-next,v3,4/7] bpftool: Return an error on prog dumps if both CFG and JSON are required https://git.kernel.org/bpf/bpf-next/c/05a06be72289 - [bpf-next,v3,5/7] bpftool: Support "opcodes", "linum", "visual" simultaneously https://git.kernel.org/bpf/bpf-next/c/9b79f02722bb - [bpf-next,v3,6/7] bpftool: Support printing opcodes and source file references in CFG https://git.kernel.org/bpf/bpf-next/c/7483a7a70a12 - [bpf-next,v3,7/7] bpftool: Clean up _bpftool_once_attr() calls in bash completion https://git.kernel.org/bpf/bpf-next/c/7319296855f1
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html