.. | .. |
---|
| 1 | +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) |
---|
1 | 2 | /* |
---|
2 | 3 | * Based on: |
---|
3 | 4 | * |
---|
.. | .. |
---|
14 | 15 | #include <stdio.h> |
---|
15 | 16 | #include <stdarg.h> |
---|
16 | 17 | #include <stdint.h> |
---|
17 | | -#include <stdio.h> |
---|
18 | 18 | #include <stdlib.h> |
---|
19 | 19 | #include <assert.h> |
---|
20 | 20 | #include <unistd.h> |
---|
21 | 21 | #include <string.h> |
---|
22 | 22 | #include <bfd.h> |
---|
23 | 23 | #include <dis-asm.h> |
---|
24 | | -#include <sys/types.h> |
---|
25 | 24 | #include <sys/stat.h> |
---|
26 | 25 | #include <limits.h> |
---|
| 26 | +#include <bpf/libbpf.h> |
---|
27 | 27 | |
---|
28 | 28 | #include "json_writer.h" |
---|
29 | 29 | #include "main.h" |
---|
30 | 30 | |
---|
31 | 31 | static void get_exec_path(char *tpath, size_t size) |
---|
32 | 32 | { |
---|
| 33 | + const char *path = "/proc/self/exe"; |
---|
33 | 34 | 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); |
---|
41 | 35 | |
---|
42 | 36 | len = readlink(path, tpath, size - 1); |
---|
43 | 37 | assert(len > 0); |
---|
44 | 38 | tpath[len] = 0; |
---|
45 | | - |
---|
46 | | - free(path); |
---|
47 | 39 | } |
---|
48 | 40 | |
---|
49 | 41 | static int oper_count; |
---|
.. | .. |
---|
82 | 74 | } |
---|
83 | 75 | |
---|
84 | 76 | 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) |
---|
86 | 82 | { |
---|
| 83 | + const struct bpf_line_info *linfo = NULL; |
---|
87 | 84 | disassembler_ftype disassemble; |
---|
88 | 85 | struct disassemble_info info; |
---|
| 86 | + unsigned int nr_skip = 0; |
---|
89 | 87 | int count, i, pc = 0; |
---|
90 | 88 | char tpath[PATH_MAX]; |
---|
91 | 89 | bfd *bfdf; |
---|
.. | .. |
---|
114 | 112 | if (inf) { |
---|
115 | 113 | bfdf->arch_info = inf; |
---|
116 | 114 | } else { |
---|
117 | | - p_err("No libfd support for %s", arch); |
---|
| 115 | + p_err("No libbfd support for %s", arch); |
---|
118 | 116 | return; |
---|
119 | 117 | } |
---|
120 | 118 | } |
---|
121 | 119 | |
---|
122 | 120 | info.arch = bfd_get_arch(bfdf); |
---|
123 | 121 | info.mach = bfd_get_mach(bfdf); |
---|
| 122 | + if (disassembler_options) |
---|
| 123 | + info.disassembler_options = disassembler_options; |
---|
124 | 124 | info.buffer = image; |
---|
125 | 125 | info.buffer_length = len; |
---|
126 | 126 | |
---|
.. | .. |
---|
139 | 139 | if (json_output) |
---|
140 | 140 | jsonw_start_array(json_wtr); |
---|
141 | 141 | 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 | + |
---|
142 | 151 | if (json_output) { |
---|
143 | 152 | jsonw_start_object(json_wtr); |
---|
144 | 153 | oper_count = 0; |
---|
| 154 | + if (linfo) |
---|
| 155 | + btf_dump_linfo_json(btf, linfo, linum); |
---|
145 | 156 | jsonw_name(json_wtr, "pc"); |
---|
146 | 157 | jsonw_printf(json_wtr, "\"0x%x\"", pc); |
---|
147 | 158 | } else { |
---|
| 159 | + if (linfo) |
---|
| 160 | + btf_dump_linfo_plain(btf, linfo, "; ", |
---|
| 161 | + linum); |
---|
148 | 162 | printf("%4x:\t", pc); |
---|
149 | 163 | } |
---|
150 | 164 | |
---|
.. | .. |
---|
186 | 200 | |
---|
187 | 201 | bfd_close(bfdf); |
---|
188 | 202 | } |
---|
| 203 | + |
---|
| 204 | +int disasm_init(void) |
---|
| 205 | +{ |
---|
| 206 | + bfd_init(); |
---|
| 207 | + return 0; |
---|
| 208 | +} |
---|