summaryrefslogtreecommitdiff
path: root/tools/bpf/bpftool/btf_dumper.c
diff options
context:
space:
mode:
authorQuentin Monnet <quentin@isovalent.com>2023-04-05 16:21:19 +0300
committerAlexei Starovoitov <ast@kernel.org>2023-04-06 07:27:27 +0300
commit7483a7a70a12d7c00c9f80574d533b01689d39a7 (patch)
tree48cade4a372739780e2bf09db0a13ff3c7aa400d /tools/bpf/bpftool/btf_dumper.c
parent9b79f02722bbf24f060b2ab79513ad6e22c8e2f0 (diff)
downloadlinux-7483a7a70a12d7c00c9f80574d533b01689d39a7.tar.xz
bpftool: Support printing opcodes and source file references in CFG
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. 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). Signed-off-by: Quentin Monnet <quentin@isovalent.com> Link: https://lore.kernel.org/r/20230405132120.59886-7-quentin@isovalent.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'tools/bpf/bpftool/btf_dumper.c')
-rw-r--r--tools/bpf/bpftool/btf_dumper.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
index 583aa843df92..6c5e0e82da22 100644
--- a/tools/bpf/bpftool/btf_dumper.c
+++ b/tools/bpf/bpftool/btf_dumper.c
@@ -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");