Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf bpf: Move BPF disassembly routines to separate file to avoid clash with capstone bpf headers

There is a clash of the libbpf and capstone libraries, that ends up
with:

In file included from /usr/include/capstone/capstone.h:325,
from util/disasm.c:1513:
/usr/include/capstone/bpf.h:94:14: error: ‘bpf_insn’ defined as wrong kind of tag
94 | typedef enum bpf_insn {

So far we're just trying to avoid this by not having both headers
included in the same .c or .h file, do it one more time by moving the
BPF diassembly routines from util/disasm.c to util/disasm_bpf.c.

This is only being hit when building with BUILD_NONDISTRO=1, i.e.
building with binutils-devel, that isn't the in the default build due to
a licencing clash. We need to reimplement what is now isolated in
util/disasm_bpf.c using some other library to have BPF annotation
feature that now only is available with BUILD_NONDISTRO=1.

Fixes: 6d17edc113de1e21 ("perf annotate: Use libcapstone to disassemble")
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/ZqpUSKPxMwaQKORr@x1
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+209 -186
+1
tools/perf/util/Build
··· 13 13 perf-util-y += ctype.o 14 14 perf-util-y += db-export.o 15 15 perf-util-y += disasm.o 16 + perf-util-y += disasm_bpf.o 16 17 perf-util-y += env.o 17 18 perf-util-y += event.o 18 19 perf-util-y += evlist.o
+1 -186
tools/perf/util/disasm.c
··· 16 16 #include "build-id.h" 17 17 #include "debug.h" 18 18 #include "disasm.h" 19 + #include "disasm_bpf.h" 19 20 #include "dso.h" 20 21 #include "env.h" 21 22 #include "evsel.h" ··· 1321 1320 } 1322 1321 1323 1322 free(build_id_path); 1324 - return 0; 1325 - } 1326 - 1327 - #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) 1328 - #define PACKAGE "perf" 1329 - #include <bfd.h> 1330 - #include <dis-asm.h> 1331 - #include <bpf/bpf.h> 1332 - #include <bpf/btf.h> 1333 - #include <bpf/libbpf.h> 1334 - #include <linux/btf.h> 1335 - #include <tools/dis-asm-compat.h> 1336 - 1337 - #include "bpf-event.h" 1338 - #include "bpf-utils.h" 1339 - 1340 - static int symbol__disassemble_bpf(struct symbol *sym, 1341 - struct annotate_args *args) 1342 - { 1343 - struct annotation *notes = symbol__annotation(sym); 1344 - struct bpf_prog_linfo *prog_linfo = NULL; 1345 - struct bpf_prog_info_node *info_node; 1346 - int len = sym->end - sym->start; 1347 - disassembler_ftype disassemble; 1348 - struct map *map = args->ms.map; 1349 - struct perf_bpil *info_linear; 1350 - struct disassemble_info info; 1351 - struct dso *dso = map__dso(map); 1352 - int pc = 0, count, sub_id; 1353 - struct btf *btf = NULL; 1354 - char tpath[PATH_MAX]; 1355 - size_t buf_size; 1356 - int nr_skip = 0; 1357 - char *buf; 1358 - bfd *bfdf; 1359 - int ret; 1360 - FILE *s; 1361 - 1362 - if (dso__binary_type(dso) != DSO_BINARY_TYPE__BPF_PROG_INFO) 1363 - return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE; 1364 - 1365 - pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__, 1366 - sym->name, sym->start, sym->end - sym->start); 1367 - 1368 - memset(tpath, 0, sizeof(tpath)); 1369 - perf_exe(tpath, sizeof(tpath)); 1370 - 1371 - bfdf = bfd_openr(tpath, NULL); 1372 - if (bfdf == NULL) 1373 - abort(); 1374 - 1375 - if (!bfd_check_format(bfdf, bfd_object)) 1376 - abort(); 1377 - 1378 - s = open_memstream(&buf, &buf_size); 1379 - if (!s) { 1380 - ret = errno; 1381 - goto out; 1382 - } 1383 - init_disassemble_info_compat(&info, s, 1384 - (fprintf_ftype) fprintf, 1385 - fprintf_styled); 1386 - info.arch = bfd_get_arch(bfdf); 1387 - info.mach = bfd_get_mach(bfdf); 1388 - 1389 - info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env, 1390 - dso__bpf_prog(dso)->id); 1391 - if (!info_node) { 1392 - ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; 1393 - goto out; 1394 - } 1395 - info_linear = info_node->info_linear; 1396 - sub_id = dso__bpf_prog(dso)->sub_id; 1397 - 1398 - info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns); 1399 - info.buffer_length = info_linear->info.jited_prog_len; 1400 - 1401 - if (info_linear->info.nr_line_info) 1402 - prog_linfo = bpf_prog_linfo__new(&info_linear->info); 1403 - 1404 - if (info_linear->info.btf_id) { 1405 - struct btf_node *node; 1406 - 1407 - node = perf_env__find_btf(dso__bpf_prog(dso)->env, 1408 - info_linear->info.btf_id); 1409 - if (node) 1410 - btf = btf__new((__u8 *)(node->data), 1411 - node->data_size); 1412 - } 1413 - 1414 - disassemble_init_for_target(&info); 1415 - 1416 - #ifdef DISASM_FOUR_ARGS_SIGNATURE 1417 - disassemble = disassembler(info.arch, 1418 - bfd_big_endian(bfdf), 1419 - info.mach, 1420 - bfdf); 1421 - #else 1422 - disassemble = disassembler(bfdf); 1423 - #endif 1424 - if (disassemble == NULL) 1425 - abort(); 1426 - 1427 - fflush(s); 1428 - do { 1429 - const struct bpf_line_info *linfo = NULL; 1430 - struct disasm_line *dl; 1431 - size_t prev_buf_size; 1432 - const char *srcline; 1433 - u64 addr; 1434 - 1435 - addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id]; 1436 - count = disassemble(pc, &info); 1437 - 1438 - if (prog_linfo) 1439 - linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo, 1440 - addr, sub_id, 1441 - nr_skip); 1442 - 1443 - if (linfo && btf) { 1444 - srcline = btf__name_by_offset(btf, linfo->line_off); 1445 - nr_skip++; 1446 - } else 1447 - srcline = NULL; 1448 - 1449 - fprintf(s, "\n"); 1450 - prev_buf_size = buf_size; 1451 - fflush(s); 1452 - 1453 - if (!annotate_opts.hide_src_code && srcline) { 1454 - args->offset = -1; 1455 - args->line = strdup(srcline); 1456 - args->line_nr = 0; 1457 - args->fileloc = NULL; 1458 - args->ms.sym = sym; 1459 - dl = disasm_line__new(args); 1460 - if (dl) { 1461 - annotation_line__add(&dl->al, 1462 - &notes->src->source); 1463 - } 1464 - } 1465 - 1466 - args->offset = pc; 1467 - args->line = buf + prev_buf_size; 1468 - args->line_nr = 0; 1469 - args->fileloc = NULL; 1470 - args->ms.sym = sym; 1471 - dl = disasm_line__new(args); 1472 - if (dl) 1473 - annotation_line__add(&dl->al, &notes->src->source); 1474 - 1475 - pc += count; 1476 - } while (count > 0 && pc < len); 1477 - 1478 - ret = 0; 1479 - out: 1480 - free(prog_linfo); 1481 - btf__free(btf); 1482 - fclose(s); 1483 - bfd_close(bfdf); 1484 - return ret; 1485 - } 1486 - #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) 1487 - static int symbol__disassemble_bpf(struct symbol *sym __maybe_unused, 1488 - struct annotate_args *args __maybe_unused) 1489 - { 1490 - return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF; 1491 - } 1492 - #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) 1493 - 1494 - static int 1495 - symbol__disassemble_bpf_image(struct symbol *sym, 1496 - struct annotate_args *args) 1497 - { 1498 - struct annotation *notes = symbol__annotation(sym); 1499 - struct disasm_line *dl; 1500 - 1501 - args->offset = -1; 1502 - args->line = strdup("to be implemented"); 1503 - args->line_nr = 0; 1504 - args->fileloc = NULL; 1505 - dl = disasm_line__new(args); 1506 - if (dl) 1507 - annotation_line__add(&dl->al, &notes->src->source); 1508 - 1509 - zfree(&args->line); 1510 1323 return 0; 1511 1324 } 1512 1325
+195
tools/perf/util/disasm_bpf.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + #include "util/annotate.h" 4 + #include "util/disasm_bpf.h" 5 + #include "util/symbol.h" 6 + #include <linux/zalloc.h> 7 + #include <string.h> 8 + 9 + #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) 10 + #define PACKAGE "perf" 11 + #include <bfd.h> 12 + #include <bpf/bpf.h> 13 + #include <bpf/btf.h> 14 + #include <bpf/libbpf.h> 15 + #include <dis-asm.h> 16 + #include <errno.h> 17 + #include <linux/btf.h> 18 + #include <tools/dis-asm-compat.h> 19 + 20 + #include "util/bpf-event.h" 21 + #include "util/bpf-utils.h" 22 + #include "util/debug.h" 23 + #include "util/dso.h" 24 + #include "util/map.h" 25 + #include "util/env.h" 26 + #include "util/util.h" 27 + 28 + int symbol__disassemble_bpf(struct symbol *sym, struct annotate_args *args) 29 + { 30 + struct annotation *notes = symbol__annotation(sym); 31 + struct bpf_prog_linfo *prog_linfo = NULL; 32 + struct bpf_prog_info_node *info_node; 33 + int len = sym->end - sym->start; 34 + disassembler_ftype disassemble; 35 + struct map *map = args->ms.map; 36 + struct perf_bpil *info_linear; 37 + struct disassemble_info info; 38 + struct dso *dso = map__dso(map); 39 + int pc = 0, count, sub_id; 40 + struct btf *btf = NULL; 41 + char tpath[PATH_MAX]; 42 + size_t buf_size; 43 + int nr_skip = 0; 44 + char *buf; 45 + bfd *bfdf; 46 + int ret; 47 + FILE *s; 48 + 49 + if (dso__binary_type(dso) != DSO_BINARY_TYPE__BPF_PROG_INFO) 50 + return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE; 51 + 52 + pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__, 53 + sym->name, sym->start, sym->end - sym->start); 54 + 55 + memset(tpath, 0, sizeof(tpath)); 56 + perf_exe(tpath, sizeof(tpath)); 57 + 58 + bfdf = bfd_openr(tpath, NULL); 59 + if (bfdf == NULL) 60 + abort(); 61 + 62 + if (!bfd_check_format(bfdf, bfd_object)) 63 + abort(); 64 + 65 + s = open_memstream(&buf, &buf_size); 66 + if (!s) { 67 + ret = errno; 68 + goto out; 69 + } 70 + init_disassemble_info_compat(&info, s, 71 + (fprintf_ftype) fprintf, 72 + fprintf_styled); 73 + info.arch = bfd_get_arch(bfdf); 74 + info.mach = bfd_get_mach(bfdf); 75 + 76 + info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env, 77 + dso__bpf_prog(dso)->id); 78 + if (!info_node) { 79 + ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; 80 + goto out; 81 + } 82 + info_linear = info_node->info_linear; 83 + sub_id = dso__bpf_prog(dso)->sub_id; 84 + 85 + info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns); 86 + info.buffer_length = info_linear->info.jited_prog_len; 87 + 88 + if (info_linear->info.nr_line_info) 89 + prog_linfo = bpf_prog_linfo__new(&info_linear->info); 90 + 91 + if (info_linear->info.btf_id) { 92 + struct btf_node *node; 93 + 94 + node = perf_env__find_btf(dso__bpf_prog(dso)->env, 95 + info_linear->info.btf_id); 96 + if (node) 97 + btf = btf__new((__u8 *)(node->data), 98 + node->data_size); 99 + } 100 + 101 + disassemble_init_for_target(&info); 102 + 103 + #ifdef DISASM_FOUR_ARGS_SIGNATURE 104 + disassemble = disassembler(info.arch, 105 + bfd_big_endian(bfdf), 106 + info.mach, 107 + bfdf); 108 + #else 109 + disassemble = disassembler(bfdf); 110 + #endif 111 + if (disassemble == NULL) 112 + abort(); 113 + 114 + fflush(s); 115 + do { 116 + const struct bpf_line_info *linfo = NULL; 117 + struct disasm_line *dl; 118 + size_t prev_buf_size; 119 + const char *srcline; 120 + u64 addr; 121 + 122 + addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id]; 123 + count = disassemble(pc, &info); 124 + 125 + if (prog_linfo) 126 + linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo, 127 + addr, sub_id, 128 + nr_skip); 129 + 130 + if (linfo && btf) { 131 + srcline = btf__name_by_offset(btf, linfo->line_off); 132 + nr_skip++; 133 + } else 134 + srcline = NULL; 135 + 136 + fprintf(s, "\n"); 137 + prev_buf_size = buf_size; 138 + fflush(s); 139 + 140 + if (!annotate_opts.hide_src_code && srcline) { 141 + args->offset = -1; 142 + args->line = strdup(srcline); 143 + args->line_nr = 0; 144 + args->fileloc = NULL; 145 + args->ms.sym = sym; 146 + dl = disasm_line__new(args); 147 + if (dl) { 148 + annotation_line__add(&dl->al, 149 + &notes->src->source); 150 + } 151 + } 152 + 153 + args->offset = pc; 154 + args->line = buf + prev_buf_size; 155 + args->line_nr = 0; 156 + args->fileloc = NULL; 157 + args->ms.sym = sym; 158 + dl = disasm_line__new(args); 159 + if (dl) 160 + annotation_line__add(&dl->al, &notes->src->source); 161 + 162 + pc += count; 163 + } while (count > 0 && pc < len); 164 + 165 + ret = 0; 166 + out: 167 + free(prog_linfo); 168 + btf__free(btf); 169 + fclose(s); 170 + bfd_close(bfdf); 171 + return ret; 172 + } 173 + #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) 174 + int symbol__disassemble_bpf(struct symbol *sym __maybe_unused, struct annotate_args *args __maybe_unused) 175 + { 176 + return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF; 177 + } 178 + #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) 179 + 180 + int symbol__disassemble_bpf_image(struct symbol *sym, struct annotate_args *args) 181 + { 182 + struct annotation *notes = symbol__annotation(sym); 183 + struct disasm_line *dl; 184 + 185 + args->offset = -1; 186 + args->line = strdup("to be implemented"); 187 + args->line_nr = 0; 188 + args->fileloc = NULL; 189 + dl = disasm_line__new(args); 190 + if (dl) 191 + annotation_line__add(&dl->al, &notes->src->source); 192 + 193 + zfree(&args->line); 194 + return 0; 195 + }
+12
tools/perf/util/disasm_bpf.h
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + #ifndef __PERF_DISASM_BPF_H 4 + #define __PERF_DISASM_BPF_H 5 + 6 + struct symbol; 7 + struct annotate_args; 8 + 9 + int symbol__disassemble_bpf(struct symbol *sym, struct annotate_args *args); 10 + int symbol__disassemble_bpf_image(struct symbol *sym, struct annotate_args *args); 11 + 12 + #endif /* __PERF_DISASM_BPF_H */