aboutsummaryrefslogtreecommitdiffstats
path: root/tools/bpf
diff options
context:
space:
mode:
authorJiong Wang <jiong.wang@netronome.com>2018-03-01 21:01:17 -0500
committerAlexei Starovoitov <ast@kernel.org>2018-03-01 21:29:48 -0500
commit73bb5b4f8f1468f7e433a30d8fbe820b24578991 (patch)
tree535085832b071c55ad00a57a2c04fb0aab636d54 /tools/bpf
parent3197239d24dcecef1dbc260733b377ade731b748 (diff)
tools: bpftool: factor out xlated dump related code into separate file
This patch factors out those code of dumping xlated eBPF instructions into xlated_dumper.[h|c]. They are quite independent dumper functions, so better to be kept separately. New dumper support will be added in later patches in this set. Signed-off-by: Jiong Wang <jiong.wang@netronome.com> Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'tools/bpf')
-rw-r--r--tools/bpf/bpftool/prog.c255
-rw-r--r--tools/bpf/bpftool/xlated_dumper.c286
-rw-r--r--tools/bpf/bpftool/xlated_dumper.h62
3 files changed, 349 insertions, 254 deletions
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index 950d11dd42ab..c5afee9838e6 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -48,7 +48,7 @@
48#include <libbpf.h> 48#include <libbpf.h>
49 49
50#include "main.h" 50#include "main.h"
51#include "disasm.h" 51#include "xlated_dumper.h"
52 52
53static const char * const prog_type_name[] = { 53static const char * const prog_type_name[] = {
54 [BPF_PROG_TYPE_UNSPEC] = "unspec", 54 [BPF_PROG_TYPE_UNSPEC] = "unspec",
@@ -407,259 +407,6 @@ static int do_show(int argc, char **argv)
407 return err; 407 return err;
408} 408}
409 409
410#define SYM_MAX_NAME 256
411
412struct kernel_sym {
413 unsigned long address;
414 char name[SYM_MAX_NAME];
415};
416
417struct dump_data {
418 unsigned long address_call_base;
419 struct kernel_sym *sym_mapping;
420 __u32 sym_count;
421 char scratch_buff[SYM_MAX_NAME];
422};
423
424static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
425{
426 return ((struct kernel_sym *)sym_a)->address -
427 ((struct kernel_sym *)sym_b)->address;
428}
429
430static void kernel_syms_load(struct dump_data *dd)
431{
432 struct kernel_sym *sym;
433 char buff[256];
434 void *tmp, *address;
435 FILE *fp;
436
437 fp = fopen("/proc/kallsyms", "r");
438 if (!fp)
439 return;
440
441 while (!feof(fp)) {
442 if (!fgets(buff, sizeof(buff), fp))
443 break;
444 tmp = realloc(dd->sym_mapping,
445 (dd->sym_count + 1) *
446 sizeof(*dd->sym_mapping));
447 if (!tmp) {
448out:
449 free(dd->sym_mapping);
450 dd->sym_mapping = NULL;
451 fclose(fp);
452 return;
453 }
454 dd->sym_mapping = tmp;
455 sym = &dd->sym_mapping[dd->sym_count];
456 if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2)
457 continue;
458 sym->address = (unsigned long)address;
459 if (!strcmp(sym->name, "__bpf_call_base")) {
460 dd->address_call_base = sym->address;
461 /* sysctl kernel.kptr_restrict was set */
462 if (!sym->address)
463 goto out;
464 }
465 if (sym->address)
466 dd->sym_count++;
467 }
468
469 fclose(fp);
470
471 qsort(dd->sym_mapping, dd->sym_count,
472 sizeof(*dd->sym_mapping), kernel_syms_cmp);
473}
474
475static void kernel_syms_destroy(struct dump_data *dd)
476{
477 free(dd->sym_mapping);
478}
479
480static struct kernel_sym *kernel_syms_search(struct dump_data *dd,
481 unsigned long key)
482{
483 struct kernel_sym sym = {
484 .address = key,
485 };
486
487 return dd->sym_mapping ?
488 bsearch(&sym, dd->sym_mapping, dd->sym_count,
489 sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
490}
491
492static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
493{
494 va_list args;
495
496 va_start(args, fmt);
497 vprintf(fmt, args);
498 va_end(args);
499}
500
501static const char *print_call_pcrel(struct dump_data *dd,
502 struct kernel_sym *sym,
503 unsigned long address,
504 const struct bpf_insn *insn)
505{
506 if (sym)
507 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
508 "%+d#%s", insn->off, sym->name);
509 else
510 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
511 "%+d#0x%lx", insn->off, address);
512 return dd->scratch_buff;
513}
514
515static const char *print_call_helper(struct dump_data *dd,
516 struct kernel_sym *sym,
517 unsigned long address)
518{
519 if (sym)
520 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
521 "%s", sym->name);
522 else
523 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
524 "0x%lx", address);
525 return dd->scratch_buff;
526}
527
528static const char *print_call(void *private_data,
529 const struct bpf_insn *insn)
530{
531 struct dump_data *dd = private_data;
532 unsigned long address = dd->address_call_base + insn->imm;
533 struct kernel_sym *sym;
534
535 sym = kernel_syms_search(dd, address);
536 if (insn->src_reg == BPF_PSEUDO_CALL)
537 return print_call_pcrel(dd, sym, address, insn);
538 else
539 return print_call_helper(dd, sym, address);
540}
541
542static const char *print_imm(void *private_data,
543 const struct bpf_insn *insn,
544 __u64 full_imm)
545{
546 struct dump_data *dd = private_data;
547
548 if (insn->src_reg == BPF_PSEUDO_MAP_FD)
549 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
550 "map[id:%u]", insn->imm);
551 else
552 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
553 "0x%llx", (unsigned long long)full_imm);
554 return dd->scratch_buff;
555}
556
557static void dump_xlated_plain(struct dump_data *dd, void *buf,
558 unsigned int len, bool opcodes)
559{
560 const struct bpf_insn_cbs cbs = {
561 .cb_print = print_insn,
562 .cb_call = print_call,
563 .cb_imm = print_imm,
564 .private_data = dd,
565 };
566 struct bpf_insn *insn = buf;
567 bool double_insn = false;
568 unsigned int i;
569
570 for (i = 0; i < len / sizeof(*insn); i++) {
571 if (double_insn) {
572 double_insn = false;
573 continue;
574 }
575
576 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
577
578 printf("% 4d: ", i);
579 print_bpf_insn(&cbs, NULL, insn + i, true);
580
581 if (opcodes) {
582 printf(" ");
583 fprint_hex(stdout, insn + i, 8, " ");
584 if (double_insn && i < len - 1) {
585 printf(" ");
586 fprint_hex(stdout, insn + i + 1, 8, " ");
587 }
588 printf("\n");
589 }
590 }
591}
592
593static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...)
594{
595 unsigned int l = strlen(fmt);
596 char chomped_fmt[l];
597 va_list args;
598
599 va_start(args, fmt);
600 if (l > 0) {
601 strncpy(chomped_fmt, fmt, l - 1);
602 chomped_fmt[l - 1] = '\0';
603 }
604 jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
605 va_end(args);
606}
607
608static void dump_xlated_json(struct dump_data *dd, void *buf,
609 unsigned int len, bool opcodes)
610{
611 const struct bpf_insn_cbs cbs = {
612 .cb_print = print_insn_json,
613 .cb_call = print_call,
614 .cb_imm = print_imm,
615 .private_data = dd,
616 };
617 struct bpf_insn *insn = buf;
618 bool double_insn = false;
619 unsigned int i;
620
621 jsonw_start_array(json_wtr);
622 for (i = 0; i < len / sizeof(*insn); i++) {
623 if (double_insn) {
624 double_insn = false;
625 continue;
626 }
627 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
628
629 jsonw_start_object(json_wtr);
630 jsonw_name(json_wtr, "disasm");
631 print_bpf_insn(&cbs, NULL, insn + i, true);
632
633 if (opcodes) {
634 jsonw_name(json_wtr, "opcodes");
635 jsonw_start_object(json_wtr);
636
637 jsonw_name(json_wtr, "code");
638 jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
639
640 jsonw_name(json_wtr, "src_reg");
641 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
642
643 jsonw_name(json_wtr, "dst_reg");
644 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
645
646 jsonw_name(json_wtr, "off");
647 print_hex_data_json((uint8_t *)(&insn[i].off), 2);
648
649 jsonw_name(json_wtr, "imm");
650 if (double_insn && i < len - 1)
651 print_hex_data_json((uint8_t *)(&insn[i].imm),
652 12);
653 else
654 print_hex_data_json((uint8_t *)(&insn[i].imm),
655 4);
656 jsonw_end_object(json_wtr);
657 }
658 jsonw_end_object(json_wtr);
659 }
660 jsonw_end_array(json_wtr);
661}
662
663static int do_dump(int argc, char **argv) 410static int do_dump(int argc, char **argv)
664{ 411{
665 struct bpf_prog_info info = {}; 412 struct bpf_prog_info info = {};
diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_dumper.c
new file mode 100644
index 000000000000..dfcdf794c9d1
--- /dev/null
+++ b/tools/bpf/bpftool/xlated_dumper.c
@@ -0,0 +1,286 @@
1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/*
3 * Copyright (C) 2018 Netronome Systems, Inc.
4 *
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.
9 *
10 * The BSD 2-Clause License:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * 1. Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
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.
24 *
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.
36 */
37
38#include <stdarg.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <sys/types.h>
43
44#include "disasm.h"
45#include "json_writer.h"
46#include "main.h"
47#include "xlated_dumper.h"
48
49static int kernel_syms_cmp(const void *sym_a, const void *sym_b)
50{
51 return ((struct kernel_sym *)sym_a)->address -
52 ((struct kernel_sym *)sym_b)->address;
53}
54
55void kernel_syms_load(struct dump_data *dd)
56{
57 struct kernel_sym *sym;
58 char buff[256];
59 void *tmp, *address;
60 FILE *fp;
61
62 fp = fopen("/proc/kallsyms", "r");
63 if (!fp)
64 return;
65
66 while (!feof(fp)) {
67 if (!fgets(buff, sizeof(buff), fp))
68 break;
69 tmp = realloc(dd->sym_mapping,
70 (dd->sym_count + 1) *
71 sizeof(*dd->sym_mapping));
72 if (!tmp) {
73out:
74 free(dd->sym_mapping);
75 dd->sym_mapping = NULL;
76 fclose(fp);
77 return;
78 }
79 dd->sym_mapping = tmp;
80 sym = &dd->sym_mapping[dd->sym_count];
81 if (sscanf(buff, "%p %*c %s", &address, sym->name) != 2)
82 continue;
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 */
87 if (!sym->address)
88 goto out;
89 }
90 if (sym->address)
91 dd->sym_count++;
92 }
93
94 fclose(fp);
95
96 qsort(dd->sym_mapping, dd->sym_count,
97 sizeof(*dd->sym_mapping), kernel_syms_cmp);
98}
99
100void kernel_syms_destroy(struct dump_data *dd)
101{
102 free(dd->sym_mapping);
103}
104
105static struct kernel_sym *kernel_syms_search(struct dump_data *dd,
106 unsigned long key)
107{
108 struct kernel_sym sym = {
109 .address = key,
110 };
111
112 return dd->sym_mapping ?
113 bsearch(&sym, dd->sym_mapping, dd->sym_count,
114 sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
115}
116
117static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
118{
119 va_list args;
120
121 va_start(args, fmt);
122 vprintf(fmt, args);
123 va_end(args);
124}
125
126static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...)
127{
128 unsigned int l = strlen(fmt);
129 char chomped_fmt[l];
130 va_list args;
131
132 va_start(args, fmt);
133 if (l > 0) {
134 strncpy(chomped_fmt, fmt, l - 1);
135 chomped_fmt[l - 1] = '\0';
136 }
137 jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
138 va_end(args);
139}
140
141static const char *print_call_pcrel(struct dump_data *dd,
142 struct kernel_sym *sym,
143 unsigned long address,
144 const struct bpf_insn *insn)
145{
146 if (sym)
147 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
148 "%+d#%s", insn->off, sym->name);
149 else
150 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
151 "%+d#0x%lx", insn->off, address);
152 return dd->scratch_buff;
153}
154
155static const char *print_call_helper(struct dump_data *dd,
156 struct kernel_sym *sym,
157 unsigned long address)
158{
159 if (sym)
160 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
161 "%s", sym->name);
162 else
163 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
164 "0x%lx", address);
165 return dd->scratch_buff;
166}
167
168static const char *print_call(void *private_data,
169 const struct bpf_insn *insn)
170{
171 struct dump_data *dd = private_data;
172 unsigned long address = dd->address_call_base + insn->imm;
173 struct kernel_sym *sym;
174
175 sym = kernel_syms_search(dd, address);
176 if (insn->src_reg == BPF_PSEUDO_CALL)
177 return print_call_pcrel(dd, sym, address, insn);
178 else
179 return print_call_helper(dd, sym, address);
180}
181
182static const char *print_imm(void *private_data,
183 const struct bpf_insn *insn,
184 __u64 full_imm)
185{
186 struct dump_data *dd = private_data;
187
188 if (insn->src_reg == BPF_PSEUDO_MAP_FD)
189 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
190 "map[id:%u]", insn->imm);
191 else
192 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff),
193 "0x%llx", (unsigned long long)full_imm);
194 return dd->scratch_buff;
195}
196
197void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len,
198 bool opcodes)
199{
200 const struct bpf_insn_cbs cbs = {
201 .cb_print = print_insn_json,
202 .cb_call = print_call,
203 .cb_imm = print_imm,
204 .private_data = dd,
205 };
206 struct bpf_insn *insn = buf;
207 bool double_insn = false;
208 unsigned int i;
209
210 jsonw_start_array(json_wtr);
211 for (i = 0; i < len / sizeof(*insn); i++) {
212 if (double_insn) {
213 double_insn = false;
214 continue;
215 }
216 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
217
218 jsonw_start_object(json_wtr);
219 jsonw_name(json_wtr, "disasm");
220 print_bpf_insn(&cbs, NULL, insn + i, true);
221
222 if (opcodes) {
223 jsonw_name(json_wtr, "opcodes");
224 jsonw_start_object(json_wtr);
225
226 jsonw_name(json_wtr, "code");
227 jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
228
229 jsonw_name(json_wtr, "src_reg");
230 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
231
232 jsonw_name(json_wtr, "dst_reg");
233 jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
234
235 jsonw_name(json_wtr, "off");
236 print_hex_data_json((uint8_t *)(&insn[i].off), 2);
237
238 jsonw_name(json_wtr, "imm");
239 if (double_insn && i < len - 1)
240 print_hex_data_json((uint8_t *)(&insn[i].imm),
241 12);
242 else
243 print_hex_data_json((uint8_t *)(&insn[i].imm),
244 4);
245 jsonw_end_object(json_wtr);
246 }
247 jsonw_end_object(json_wtr);
248 }
249 jsonw_end_array(json_wtr);
250}
251
252void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
253 bool opcodes)
254{
255 const struct bpf_insn_cbs cbs = {
256 .cb_print = print_insn,
257 .cb_call = print_call,
258 .cb_imm = print_imm,
259 .private_data = dd,
260 };
261 struct bpf_insn *insn = buf;
262 bool double_insn = false;
263 unsigned int i;
264
265 for (i = 0; i < len / sizeof(*insn); i++) {
266 if (double_insn) {
267 double_insn = false;
268 continue;
269 }
270
271 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
272
273 printf("% 4d: ", i);
274 print_bpf_insn(&cbs, NULL, insn + i, true);
275
276 if (opcodes) {
277 printf(" ");
278 fprint_hex(stdout, insn + i, 8, " ");
279 if (double_insn && i < len - 1) {
280 printf(" ");
281 fprint_hex(stdout, insn + i + 1, 8, " ");
282 }
283 printf("\n");
284 }
285 }
286}
diff --git a/tools/bpf/bpftool/xlated_dumper.h b/tools/bpf/bpftool/xlated_dumper.h
new file mode 100644
index 000000000000..208285f9ab70
--- /dev/null
+++ b/tools/bpf/bpftool/xlated_dumper.h
@@ -0,0 +1,62 @@
1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/*
3 * Copyright (C) 2018 Netronome Systems, Inc.
4 *
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.
9 *
10 * The BSD 2-Clause License:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * 1. Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
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.
24 *
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.
36 */
37
38#ifndef __BPF_TOOL_XLATED_DUMPER_H
39#define __BPF_TOOL_XLATED_DUMPER_H
40
41#define SYM_MAX_NAME 256
42
43struct kernel_sym {
44 unsigned long address;
45 char name[SYM_MAX_NAME];
46};
47
48struct dump_data {
49 unsigned long address_call_base;
50 struct kernel_sym *sym_mapping;
51 __u32 sym_count;
52 char scratch_buff[SYM_MAX_NAME];
53};
54
55void kernel_syms_load(struct dump_data *dd);
56void kernel_syms_destroy(struct dump_data *dd);
57void dump_xlated_json(struct dump_data *dd, void *buf, unsigned int len,
58 bool opcodes);
59void dump_xlated_plain(struct dump_data *dd, void *buf, unsigned int len,
60 bool opcodes);
61
62#endif