b053b439b7
This patch adds print bpf_line_info function in 'prog dump jitted' and 'prog dump xlated': [root@arch-fb-vm1 bpf]# ~/devshare/fb-kernel/linux/tools/bpf/bpftool/bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv [...] int test_long_fname_2(struct dummy_tracepoint_args * arg): bpf_prog_44a040bf25481309_test_long_fname_2: ; static int test_long_fname_2(struct dummy_tracepoint_args *arg) 0: push %rbp 1: mov %rsp,%rbp 4: sub $0x30,%rsp b: sub $0x28,%rbp f: mov %rbx,0x0(%rbp) 13: mov %r13,0x8(%rbp) 17: mov %r14,0x10(%rbp) 1b: mov %r15,0x18(%rbp) 1f: xor %eax,%eax 21: mov %rax,0x20(%rbp) 25: xor %esi,%esi ; int key = 0; 27: mov %esi,-0x4(%rbp) ; if (!arg->sock) 2a: mov 0x8(%rdi),%rdi ; if (!arg->sock) 2e: cmp $0x0,%rdi 32: je 0x0000000000000070 34: mov %rbp,%rsi ; counts = bpf_map_lookup_elem(&btf_map, &key); 37: add $0xfffffffffffffffc,%rsi 3b: movabs $0xffff8881139d7480,%rdi 45: add $0x110,%rdi 4c: mov 0x0(%rsi),%eax 4f: cmp $0x4,%rax 53: jae 0x000000000000005e 55: shl $0x3,%rax 59: add %rdi,%rax 5c: jmp 0x0000000000000060 5e: xor %eax,%eax ; if (!counts) 60: cmp $0x0,%rax 64: je 0x0000000000000070 ; counts->v6++; 66: mov 0x4(%rax),%edi 69: add $0x1,%rdi 6d: mov %edi,0x4(%rax) 70: mov 0x0(%rbp),%rbx 74: mov 0x8(%rbp),%r13 78: mov 0x10(%rbp),%r14 7c: mov 0x18(%rbp),%r15 80: add $0x28,%rbp 84: leaveq 85: retq [...] With linum: [root@arch-fb-vm1 bpf]# ~/devshare/fb-kernel/linux/tools/bpf/bpftool/bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv linum int _dummy_tracepoint(struct dummy_tracepoint_args * arg): bpf_prog_b07ccb89267cf242__dummy_tracepoint: ; return test_long_fname_1(arg); [file:/data/users/kafai/fb-kernel/linux/tools/testing/selftests/bpf/test_btf_haskv.c line_num:54 line_col:9] 0: push %rbp 1: mov %rsp,%rbp 4: sub $0x28,%rsp b: sub $0x28,%rbp f: mov %rbx,0x0(%rbp) 13: mov %r13,0x8(%rbp) 17: mov %r14,0x10(%rbp) 1b: mov %r15,0x18(%rbp) 1f: xor %eax,%eax 21: mov %rax,0x20(%rbp) 25: callq 0x000000000000851e ; return test_long_fname_1(arg); [file:/data/users/kafai/fb-kernel/linux/tools/testing/selftests/bpf/test_btf_haskv.c line_num:54 line_col:2] 2a: xor %eax,%eax 2c: mov 0x0(%rbp),%rbx 30: mov 0x8(%rbp),%r13 34: mov 0x10(%rbp),%r14 38: mov 0x18(%rbp),%r15 3c: add $0x28,%rbp 40: leaveq 41: retq [...] Signed-off-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
204 lines
4.2 KiB
C
204 lines
4.2 KiB
C
/*
|
|
* Based on:
|
|
*
|
|
* Minimal BPF JIT image disassembler
|
|
*
|
|
* Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
|
|
* debugging or verification purposes.
|
|
*
|
|
* Copyright 2013 Daniel Borkmann <daniel@iogearbox.net>
|
|
* Licensed under the GNU General Public License, version 2.0 (GPLv2)
|
|
*/
|
|
|
|
#include <stdarg.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <bfd.h>
|
|
#include <dis-asm.h>
|
|
#include <sys/stat.h>
|
|
#include <limits.h>
|
|
#include <libbpf.h>
|
|
|
|
#include "json_writer.h"
|
|
#include "main.h"
|
|
|
|
static void get_exec_path(char *tpath, size_t size)
|
|
{
|
|
const char *path = "/proc/self/exe";
|
|
ssize_t len;
|
|
|
|
len = readlink(path, tpath, size - 1);
|
|
assert(len > 0);
|
|
tpath[len] = 0;
|
|
}
|
|
|
|
static int oper_count;
|
|
static int fprintf_json(void *out, const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
char *s;
|
|
|
|
va_start(ap, fmt);
|
|
if (!oper_count) {
|
|
int i;
|
|
|
|
s = va_arg(ap, char *);
|
|
|
|
/* Strip trailing spaces */
|
|
i = strlen(s) - 1;
|
|
while (s[i] == ' ')
|
|
s[i--] = '\0';
|
|
|
|
jsonw_string_field(json_wtr, "operation", s);
|
|
jsonw_name(json_wtr, "operands");
|
|
jsonw_start_array(json_wtr);
|
|
oper_count++;
|
|
} else if (!strcmp(fmt, ",")) {
|
|
/* Skip */
|
|
} else {
|
|
s = va_arg(ap, char *);
|
|
jsonw_string(json_wtr, s);
|
|
oper_count++;
|
|
}
|
|
va_end(ap);
|
|
return 0;
|
|
}
|
|
|
|
void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
|
|
const char *arch, const char *disassembler_options,
|
|
const struct btf *btf,
|
|
const struct bpf_prog_linfo *prog_linfo,
|
|
__u64 func_ksym, unsigned int func_idx,
|
|
bool linum)
|
|
{
|
|
const struct bpf_line_info *linfo = NULL;
|
|
disassembler_ftype disassemble;
|
|
struct disassemble_info info;
|
|
unsigned int nr_skip = 0;
|
|
int count, i, pc = 0;
|
|
char tpath[PATH_MAX];
|
|
bfd *bfdf;
|
|
|
|
if (!len)
|
|
return;
|
|
|
|
memset(tpath, 0, sizeof(tpath));
|
|
get_exec_path(tpath, sizeof(tpath));
|
|
|
|
bfdf = bfd_openr(tpath, NULL);
|
|
assert(bfdf);
|
|
assert(bfd_check_format(bfdf, bfd_object));
|
|
|
|
if (json_output)
|
|
init_disassemble_info(&info, stdout,
|
|
(fprintf_ftype) fprintf_json);
|
|
else
|
|
init_disassemble_info(&info, stdout,
|
|
(fprintf_ftype) fprintf);
|
|
|
|
/* Update architecture info for offload. */
|
|
if (arch) {
|
|
const bfd_arch_info_type *inf = bfd_scan_arch(arch);
|
|
|
|
if (inf) {
|
|
bfdf->arch_info = inf;
|
|
} else {
|
|
p_err("No libbfd support for %s", arch);
|
|
return;
|
|
}
|
|
}
|
|
|
|
info.arch = bfd_get_arch(bfdf);
|
|
info.mach = bfd_get_mach(bfdf);
|
|
if (disassembler_options)
|
|
info.disassembler_options = disassembler_options;
|
|
info.buffer = image;
|
|
info.buffer_length = len;
|
|
|
|
disassemble_init_for_target(&info);
|
|
|
|
#ifdef DISASM_FOUR_ARGS_SIGNATURE
|
|
disassemble = disassembler(info.arch,
|
|
bfd_big_endian(bfdf),
|
|
info.mach,
|
|
bfdf);
|
|
#else
|
|
disassemble = disassembler(bfdf);
|
|
#endif
|
|
assert(disassemble);
|
|
|
|
if (json_output)
|
|
jsonw_start_array(json_wtr);
|
|
do {
|
|
if (prog_linfo) {
|
|
linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
|
|
func_ksym + pc,
|
|
func_idx,
|
|
nr_skip);
|
|
if (linfo)
|
|
nr_skip++;
|
|
}
|
|
|
|
if (json_output) {
|
|
jsonw_start_object(json_wtr);
|
|
oper_count = 0;
|
|
if (linfo)
|
|
btf_dump_linfo_json(btf, linfo, linum);
|
|
jsonw_name(json_wtr, "pc");
|
|
jsonw_printf(json_wtr, "\"0x%x\"", pc);
|
|
} else {
|
|
if (linfo)
|
|
btf_dump_linfo_plain(btf, linfo, "; ",
|
|
linum);
|
|
printf("%4x:\t", pc);
|
|
}
|
|
|
|
count = disassemble(pc, &info);
|
|
if (json_output) {
|
|
/* Operand array, was started in fprintf_json. Before
|
|
* that, make sure we have a _null_ value if no operand
|
|
* other than operation code was present.
|
|
*/
|
|
if (oper_count == 1)
|
|
jsonw_null(json_wtr);
|
|
jsonw_end_array(json_wtr);
|
|
}
|
|
|
|
if (opcodes) {
|
|
if (json_output) {
|
|
jsonw_name(json_wtr, "opcodes");
|
|
jsonw_start_array(json_wtr);
|
|
for (i = 0; i < count; ++i)
|
|
jsonw_printf(json_wtr, "\"0x%02hhx\"",
|
|
(uint8_t)image[pc + i]);
|
|
jsonw_end_array(json_wtr);
|
|
} else {
|
|
printf("\n\t");
|
|
for (i = 0; i < count; ++i)
|
|
printf("%02x ",
|
|
(uint8_t)image[pc + i]);
|
|
}
|
|
}
|
|
if (json_output)
|
|
jsonw_end_object(json_wtr);
|
|
else
|
|
printf("\n");
|
|
|
|
pc += count;
|
|
} while (count > 0 && pc < len);
|
|
if (json_output)
|
|
jsonw_end_array(json_wtr);
|
|
|
|
bfd_close(bfdf);
|
|
}
|
|
|
|
int disasm_init(void)
|
|
{
|
|
bfd_init();
|
|
return 0;
|
|
}
|