OSDN Git Service

bpftool: Support inline annotations when dumping the CFG of a program
authorQuentin Monnet <quentin@isovalent.com>
Wed, 5 Apr 2023 13:21:16 +0000 (14:21 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 6 Apr 2023 04:27:27 +0000 (21:27 -0700)
commit9fd496848b1c4cd04fee5f152bb7bcec11e1b901
tree736f17f4be64158a7c13c9755702e7828bcea010
parent67cf52cdb6c8fa6365d29106555dacf95c9fd374
bpftool: Support inline annotations when dumping the CFG of a program

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>
Link: https://lore.kernel.org/r/20230405132120.59886-4-quentin@isovalent.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/bpf/bpftool/btf_dumper.c
tools/bpf/bpftool/cfg.c
tools/bpf/bpftool/cfg.h
tools/bpf/bpftool/main.h
tools/bpf/bpftool/prog.c
tools/bpf/bpftool/xlated_dumper.c