hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/tools/bpf/bpftool/jit_disasm.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
12 /*
23 * Based on:
34 *
....@@ -14,36 +15,27 @@
1415 #include <stdio.h>
1516 #include <stdarg.h>
1617 #include <stdint.h>
17
-#include <stdio.h>
1818 #include <stdlib.h>
1919 #include <assert.h>
2020 #include <unistd.h>
2121 #include <string.h>
2222 #include <bfd.h>
2323 #include <dis-asm.h>
24
-#include <sys/types.h>
2524 #include <sys/stat.h>
2625 #include <limits.h>
26
+#include <bpf/libbpf.h>
2727
2828 #include "json_writer.h"
2929 #include "main.h"
3030
3131 static void get_exec_path(char *tpath, size_t size)
3232 {
33
+ const char *path = "/proc/self/exe";
3334 ssize_t len;
34
- char *path;
35
-
36
- snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
37
- tpath[size - 1] = 0;
38
-
39
- path = strdup(tpath);
40
- assert(path);
4135
4236 len = readlink(path, tpath, size - 1);
4337 assert(len > 0);
4438 tpath[len] = 0;
45
-
46
- free(path);
4739 }
4840
4941 static int oper_count;
....@@ -82,10 +74,16 @@
8274 }
8375
8476 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
85
- const char *arch)
77
+ const char *arch, const char *disassembler_options,
78
+ const struct btf *btf,
79
+ const struct bpf_prog_linfo *prog_linfo,
80
+ __u64 func_ksym, unsigned int func_idx,
81
+ bool linum)
8682 {
83
+ const struct bpf_line_info *linfo = NULL;
8784 disassembler_ftype disassemble;
8885 struct disassemble_info info;
86
+ unsigned int nr_skip = 0;
8987 int count, i, pc = 0;
9088 char tpath[PATH_MAX];
9189 bfd *bfdf;
....@@ -114,13 +112,15 @@
114112 if (inf) {
115113 bfdf->arch_info = inf;
116114 } else {
117
- p_err("No libfd support for %s", arch);
115
+ p_err("No libbfd support for %s", arch);
118116 return;
119117 }
120118 }
121119
122120 info.arch = bfd_get_arch(bfdf);
123121 info.mach = bfd_get_mach(bfdf);
122
+ if (disassembler_options)
123
+ info.disassembler_options = disassembler_options;
124124 info.buffer = image;
125125 info.buffer_length = len;
126126
....@@ -139,12 +139,26 @@
139139 if (json_output)
140140 jsonw_start_array(json_wtr);
141141 do {
142
+ if (prog_linfo) {
143
+ linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
144
+ func_ksym + pc,
145
+ func_idx,
146
+ nr_skip);
147
+ if (linfo)
148
+ nr_skip++;
149
+ }
150
+
142151 if (json_output) {
143152 jsonw_start_object(json_wtr);
144153 oper_count = 0;
154
+ if (linfo)
155
+ btf_dump_linfo_json(btf, linfo, linum);
145156 jsonw_name(json_wtr, "pc");
146157 jsonw_printf(json_wtr, "\"0x%x\"", pc);
147158 } else {
159
+ if (linfo)
160
+ btf_dump_linfo_plain(btf, linfo, "; ",
161
+ linum);
148162 printf("%4x:\t", pc);
149163 }
150164
....@@ -186,3 +200,9 @@
186200
187201 bfd_close(bfdf);
188202 }
203
+
204
+int disasm_init(void)
205
+{
206
+ bfd_init();
207
+ return 0;
208
+}