1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2018 Netronome Systems, Inc. */
11 #include <sys/types.h>
12 #include <bpf/libbpf.h>
13 #include <bpf/libbpf_internal.h>
16 #include "json_writer.h"
18 #include "xlated_dumper.h"
20 static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
22 return ((struct kernel_sym *)sym_a)->address -
23 ((struct kernel_sym *)sym_b)->address;
26 void kernel_syms_load(struct dump_data *dd)
28 struct kernel_sym *sym;
33 fp = fopen("/proc/kallsyms", "r");
37 while (fgets(buff, sizeof(buff), fp)) {
38 tmp = libbpf_reallocarray(dd->sym_mapping, dd->sym_count + 1,
39 sizeof(*dd->sym_mapping));
42 free(dd->sym_mapping);
43 dd->sym_mapping = NULL;
47 dd->sym_mapping = tmp;
48 sym = &dd->sym_mapping[dd->sym_count];
49 if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2)
51 sym->address = (unsigned long)address;
52 if (!strcmp(sym->name, "__bpf_call_base")) {
53 dd->address_call_base = sym->address;
54 /* sysctl kernel.kptr_restrict was set */
64 qsort(dd->sym_mapping, dd->sym_count,
65 sizeof(*dd->sym_mapping), kernel_syms_cmp);
68 void kernel_syms_destroy(struct dump_data *dd)
70 free(dd->sym_mapping);
73 struct kernel_sym *kernel_syms_search(struct dump_data *dd,
76 struct kernel_sym sym = {
80 return dd->sym_mapping ?
81 bsearch(&sym, dd->sym_mapping, dd->sym_count,
82 sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
85 static void __printf(2, 3) print_insn(void *private_data, const char *fmt, ...)
94 static void __printf(2, 3)
95 print_insn_for_graph(void *private_data, const char *fmt, ...)
101 vsnprintf(buf, sizeof(buf), fmt, args);
107 memmove(p + 3, p, strlen(buf) + 1 - (p - buf));
108 /* Align each instruction dump row left. */
111 /* Output multiline concatenation. */
113 } else if (*p == '<' || *p == '>' || *p == '|' || *p == '&') {
114 memmove(p + 1, p, strlen(buf) + 1 - (p - buf));
115 /* Escape special character. */
125 static void __printf(2, 3)
126 print_insn_json(void *private_data, const char *fmt, ...)
128 unsigned int l = strlen(fmt);
134 strncpy(chomped_fmt, fmt, l - 1);
135 chomped_fmt[l - 1] = '\0';
137 jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
141 static const char *print_call_pcrel(struct dump_data *dd,
142 struct kernel_sym *sym,
143 unsigned long address,
144 const struct bpf_insn *insn)
146 if (!dd->nr_jited_ksyms)
147 /* Do not show address for interpreted programs */
148 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
151 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
152 "%+d#%s", insn->off, sym->name);
154 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
155 "%+d#0x%lx", insn->off, address);
156 return dd->scratch_buff;
159 static const char *print_call_helper(struct dump_data *dd,
160 struct kernel_sym *sym,
161 unsigned long address)
164 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
167 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
169 return dd->scratch_buff;
172 static const char *print_call(void *private_data,
173 const struct bpf_insn *insn)
175 struct dump_data *dd = private_data;
176 unsigned long address = dd->address_call_base + insn->imm;
177 struct kernel_sym *sym;
179 if (insn->src_reg == BPF_PSEUDO_CALL &&
180 (__u32) insn->imm < dd->nr_jited_ksyms && dd->jited_ksyms)
181 address = dd->jited_ksyms[insn->imm];
183 sym = kernel_syms_search(dd, address);
184 if (insn->src_reg == BPF_PSEUDO_CALL)
185 return print_call_pcrel(dd, sym, address, insn);
187 return print_call_helper(dd, sym, address);
190 static const char *print_imm(void *private_data,
191 const struct bpf_insn *insn,
194 struct dump_data *dd = private_data;
196 if (insn->src_reg == BPF_PSEUDO_MAP_FD)
197 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
198 "map[id:%u]", insn->imm);
199 else if (insn->src_reg == BPF_PSEUDO_MAP_VALUE)
200 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
201 "map[id:%u][0]+%u", insn->imm, (insn + 1)->imm);
202 else if (insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE)
203 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
204 "map[idx:%u]+%u", insn->imm, (insn + 1)->imm);
205 else if (insn->src_reg == BPF_PSEUDO_FUNC)
206 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
207 "subprog[%+d]", insn->imm);
209 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
210 "0x%llx", (unsigned long long)full_imm);
211 return dd->scratch_buff;
214 void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len,
215 bool opcodes, bool linum)
217 const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo;
218 const struct bpf_insn_cbs cbs = {
219 .cb_print = print_insn_json,
220 .cb_call = print_call,
224 struct bpf_func_info *record;
225 struct bpf_insn *insn = buf;
226 struct btf *btf = dd->btf;
227 bool double_insn = false;
228 unsigned int nr_skip = 0;
232 jsonw_start_array(json_wtr);
233 record = dd->func_info;
234 for (i = 0; i < len / sizeof(*insn); i++) {
239 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
241 jsonw_start_object(json_wtr);
244 if (record->insn_off == i) {
245 btf_dumper_type_only(btf, record->type_id,
248 if (func_sig[0] != '\0') {
249 jsonw_name(json_wtr, "proto");
250 jsonw_string(json_wtr, func_sig);
252 record = (void *)record + dd->finfo_rec_size;
257 const struct bpf_line_info *linfo;
259 linfo = bpf_prog_linfo__lfind(prog_linfo, i, nr_skip);
261 btf_dump_linfo_json(btf, linfo, linum);
266 jsonw_name(json_wtr, "disasm");
267 print_bpf_insn(&cbs, insn + i, true);
270 jsonw_name(json_wtr, "opcodes");
271 jsonw_start_object(json_wtr);
273 jsonw_name(json_wtr, "code");
274 jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
276 jsonw_name(json_wtr, "src_reg");
277 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
279 jsonw_name(json_wtr, "dst_reg");
280 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
282 jsonw_name(json_wtr, "off");
283 print_hex_data_json((uint8_t *)(&insn[i].off), 2);
285 jsonw_name(json_wtr, "imm");
286 if (double_insn && i < len - 1)
287 print_hex_data_json((uint8_t *)(&insn[i].imm),
290 print_hex_data_json((uint8_t *)(&insn[i].imm),
292 jsonw_end_object(json_wtr);
294 jsonw_end_object(json_wtr);
296 jsonw_end_array(json_wtr);
299 void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
300 bool opcodes, bool linum)
302 const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo;
303 const struct bpf_insn_cbs cbs = {
304 .cb_print = print_insn,
305 .cb_call = print_call,
309 struct bpf_func_info *record;
310 struct bpf_insn *insn = buf;
311 struct btf *btf = dd->btf;
312 unsigned int nr_skip = 0;
313 bool double_insn = false;
317 record = dd->func_info;
318 for (i = 0; i < len / sizeof(*insn); i++) {
325 if (record->insn_off == i) {
326 btf_dumper_type_only(btf, record->type_id,
329 if (func_sig[0] != '\0')
330 printf("%s:\n", func_sig);
331 record = (void *)record + dd->finfo_rec_size;
336 const struct bpf_line_info *linfo;
338 linfo = bpf_prog_linfo__lfind(prog_linfo, i, nr_skip);
340 btf_dump_linfo_plain(btf, linfo, "; ",
346 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
349 print_bpf_insn(&cbs, insn + i, true);
353 fprint_hex(stdout, insn + i, 8, " ");
354 if (double_insn && i < len - 1) {
356 fprint_hex(stdout, insn + i + 1, 8, " ");
363 void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
364 unsigned int start_idx,
365 bool opcodes, bool linum)
367 const struct bpf_insn_cbs cbs = {
368 .cb_print = print_insn_for_graph,
369 .cb_call = print_call,
373 const struct bpf_prog_linfo *prog_linfo = dd->prog_linfo;
374 const struct bpf_line_info *last_linfo = NULL;
375 struct bpf_func_info *record = dd->func_info;
376 struct bpf_insn *insn_start = buf_start;
377 struct bpf_insn *insn_end = buf_end;
378 struct bpf_insn *cur = insn_start;
379 struct btf *btf = dd->btf;
380 bool double_insn = false;
383 for (; cur <= insn_end; cur++) {
384 unsigned int insn_off;
390 double_insn = cur->code == (BPF_LD | BPF_IMM | BPF_DW);
392 insn_off = (unsigned int)(cur - insn_start + start_idx);
394 if (record->insn_off == insn_off) {
395 btf_dumper_type_only(btf, record->type_id,
398 if (func_sig[0] != '\0')
399 printf("; %s:\\l\\\n", func_sig);
400 record = (void *)record + dd->finfo_rec_size;
405 const struct bpf_line_info *linfo;
407 linfo = bpf_prog_linfo__lfind(prog_linfo, insn_off, 0);
408 if (linfo && linfo != last_linfo) {
409 btf_dump_linfo_dotlabel(btf, linfo, linum);
414 printf("%d: ", insn_off);
415 print_bpf_insn(&cbs, cur, true);
418 printf("\\ \\ \\ \\ ");
419 fprint_hex(stdout, cur, 8, " ");
420 if (double_insn && cur <= insn_end - 1) {
422 fprint_hex(stdout, cur + 1, 8, " ");