1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
3 * Copyright (C) 2018 Netronome Systems, Inc.
5 * This software is dual licensed under the GNU General License Version 2,
6 * June 1991 as shown in the file COPYING in the top-level directory of this
7 * source tree or the BSD 2-Clause License provided below. You have the
8 * option to license this software under the complete terms of either license.
10 * The BSD 2-Clause License:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * 1. Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * 2. Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/types.h>
46 #include "json_writer.h"
48 #include "xlated_dumper.h"
50 static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
52 return ((struct kernel_sym *)sym_a)->address -
53 ((struct kernel_sym *)sym_b)->address;
56 void kernel_syms_load(struct dump_data *dd)
58 struct kernel_sym *sym;
63 fp = fopen("/proc/kallsyms", "r");
68 if (!fgets(buff, sizeof(buff), fp))
70 tmp = reallocarray(dd->sym_mapping, dd->sym_count + 1,
71 sizeof(*dd->sym_mapping));
74 free(dd->sym_mapping);
75 dd->sym_mapping = NULL;
79 dd->sym_mapping = tmp;
80 sym = &dd->sym_mapping[dd->sym_count];
81 if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2)
83 sym->address = (unsigned long)address;
84 if (!strcmp(sym->name, "__bpf_call_base")) {
85 dd->address_call_base = sym->address;
86 /* sysctl kernel.kptr_restrict was set */
96 qsort(dd->sym_mapping, dd->sym_count,
97 sizeof(*dd->sym_mapping), kernel_syms_cmp);
100 void kernel_syms_destroy(struct dump_data *dd)
102 free(dd->sym_mapping);
105 struct kernel_sym *kernel_syms_search(struct dump_data *dd,
108 struct kernel_sym sym = {
112 return dd->sym_mapping ?
113 bsearch(&sym, dd->sym_mapping, dd->sym_count,
114 sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
117 static void print_insn(void *private_data, const char *fmt, ...)
127 print_insn_for_graph(void *private_data, const char *fmt, ...)
133 vsnprintf(buf, sizeof(buf), fmt, args);
139 memmove(p + 3, p, strlen(buf) + 1 - (p - buf));
140 /* Align each instruction dump row left. */
143 /* Output multiline concatenation. */
145 } else if (*p == '<' || *p == '>' || *p == '|' || *p == '&') {
146 memmove(p + 1, p, strlen(buf) + 1 - (p - buf));
147 /* Escape special character. */
157 static void print_insn_json(void *private_data, const char *fmt, ...)
159 unsigned int l = strlen(fmt);
165 strncpy(chomped_fmt, fmt, l - 1);
166 chomped_fmt[l - 1] = '\0';
168 jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
172 static const char *print_call_pcrel(struct dump_data *dd,
173 struct kernel_sym *sym,
174 unsigned long address,
175 const struct bpf_insn *insn)
177 if (!dd->nr_jited_ksyms)
178 /* Do not show address for interpreted programs */
179 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
182 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
183 "%+d#%s", insn->off, sym->name);
185 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
186 "%+d#0x%lx", insn->off, address);
187 return dd->scratch_buff;
190 static const char *print_call_helper(struct dump_data *dd,
191 struct kernel_sym *sym,
192 unsigned long address)
195 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
198 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
200 return dd->scratch_buff;
203 static const char *print_call(void *private_data,
204 const struct bpf_insn *insn)
206 struct dump_data *dd = private_data;
207 unsigned long address = dd->address_call_base + insn->imm;
208 struct kernel_sym *sym;
210 if (insn->src_reg == BPF_PSEUDO_CALL &&
211 (__u32) insn->imm < dd->nr_jited_ksyms)
212 address = dd->jited_ksyms[insn->imm];
214 sym = kernel_syms_search(dd, address);
215 if (insn->src_reg == BPF_PSEUDO_CALL)
216 return print_call_pcrel(dd, sym, address, insn);
218 return print_call_helper(dd, sym, address);
221 static const char *print_imm(void *private_data,
222 const struct bpf_insn *insn,
225 struct dump_data *dd = private_data;
227 if (insn->src_reg == BPF_PSEUDO_MAP_FD)
228 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
229 "map[id:%u]", insn->imm);
231 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
232 "0x%llx", (unsigned long long)full_imm);
233 return dd->scratch_buff;
236 void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len,
239 const struct bpf_insn_cbs cbs = {
240 .cb_print = print_insn_json,
241 .cb_call = print_call,
245 struct bpf_insn *insn = buf;
246 bool double_insn = false;
249 jsonw_start_array(json_wtr);
250 for (i = 0; i < len / sizeof(*insn); i++) {
255 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
257 jsonw_start_object(json_wtr);
258 jsonw_name(json_wtr, "disasm");
259 print_bpf_insn(&cbs, insn + i, true);
262 jsonw_name(json_wtr, "opcodes");
263 jsonw_start_object(json_wtr);
265 jsonw_name(json_wtr, "code");
266 jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
268 jsonw_name(json_wtr, "src_reg");
269 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
271 jsonw_name(json_wtr, "dst_reg");
272 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
274 jsonw_name(json_wtr, "off");
275 print_hex_data_json((uint8_t *)(&insn[i].off), 2);
277 jsonw_name(json_wtr, "imm");
278 if (double_insn && i < len - 1)
279 print_hex_data_json((uint8_t *)(&insn[i].imm),
282 print_hex_data_json((uint8_t *)(&insn[i].imm),
284 jsonw_end_object(json_wtr);
286 jsonw_end_object(json_wtr);
288 jsonw_end_array(json_wtr);
291 void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
294 const struct bpf_insn_cbs cbs = {
295 .cb_print = print_insn,
296 .cb_call = print_call,
300 struct bpf_insn *insn = buf;
301 bool double_insn = false;
304 for (i = 0; i < len / sizeof(*insn); i++) {
310 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
313 print_bpf_insn(&cbs, insn + i, true);
317 fprint_hex(stdout, insn + i, 8, " ");
318 if (double_insn && i < len - 1) {
320 fprint_hex(stdout, insn + i + 1, 8, " ");
327 void dump_xlated_for_graph(struct dump_data *dd, void *buf_start, void *buf_end,
328 unsigned int start_idx)
330 const struct bpf_insn_cbs cbs = {
331 .cb_print = print_insn_for_graph,
332 .cb_call = print_call,
336 struct bpf_insn *insn_start = buf_start;
337 struct bpf_insn *insn_end = buf_end;
338 struct bpf_insn *cur = insn_start;
339 bool double_insn = false;
341 for (; cur <= insn_end; cur++) {
346 double_insn = cur->code == (BPF_LD | BPF_IMM | BPF_DW);
348 printf("% 4d: ", (int)(cur - insn_start + start_idx));
349 print_bpf_insn(&cbs, cur, true);