2018-12-13 11:59:25 +08:00
|
|
|
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
|
|
|
|
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
|
2017-10-05 11:10:04 +08:00
|
|
|
|
2018-07-11 05:43:07 +08:00
|
|
|
#define _GNU_SOURCE
|
2017-10-05 11:10:04 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-10-10 01:30:13 +08:00
|
|
|
#include <stdarg.h>
|
2017-10-05 11:10:04 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2018-07-11 05:42:58 +08:00
|
|
|
#include <net/if.h>
|
2017-10-05 11:10:04 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2018-07-11 05:43:03 +08:00
|
|
|
#include <linux/err.h>
|
2019-07-06 01:54:33 +08:00
|
|
|
#include <linux/sizes.h>
|
2018-07-11 05:43:03 +08:00
|
|
|
|
2020-01-20 21:06:46 +08:00
|
|
|
#include <bpf/bpf.h>
|
|
|
|
#include <bpf/btf.h>
|
|
|
|
#include <bpf/libbpf.h>
|
2017-10-05 11:10:04 +08:00
|
|
|
|
2018-03-02 10:01:22 +08:00
|
|
|
#include "cfg.h"
|
2017-10-05 11:10:04 +08:00
|
|
|
#include "main.h"
|
2018-03-02 10:01:17 +08:00
|
|
|
#include "xlated_dumper.h"
|
2017-10-05 11:10:04 +08:00
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
enum dump_mode {
|
|
|
|
DUMP_JITED,
|
|
|
|
DUMP_XLATED,
|
|
|
|
};
|
|
|
|
|
2018-10-16 02:19:50 +08:00
|
|
|
static const char * const attach_type_strings[] = {
|
|
|
|
[BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
|
|
|
|
[BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
|
|
|
|
[BPF_SK_MSG_VERDICT] = "msg_verdict",
|
2018-11-10 00:21:46 +08:00
|
|
|
[BPF_FLOW_DISSECTOR] = "flow_dissector",
|
2018-10-16 02:19:50 +08:00
|
|
|
[__MAX_BPF_ATTACH_TYPE] = NULL,
|
|
|
|
};
|
|
|
|
|
2018-12-14 21:56:01 +08:00
|
|
|
static enum bpf_attach_type parse_attach_type(const char *str)
|
2018-10-16 02:19:50 +08:00
|
|
|
{
|
|
|
|
enum bpf_attach_type type;
|
|
|
|
|
|
|
|
for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
|
|
|
|
if (attach_type_strings[type] &&
|
|
|
|
is_prefix(str, attach_type_strings[type]))
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __MAX_BPF_ATTACH_TYPE;
|
|
|
|
}
|
|
|
|
|
2017-10-05 11:10:04 +08:00
|
|
|
static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
|
|
|
|
{
|
|
|
|
struct timespec real_time_ts, boot_time_ts;
|
|
|
|
time_t wallclock_secs;
|
|
|
|
struct tm load_tm;
|
|
|
|
|
|
|
|
buf[--size] = '\0';
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
|
|
|
|
clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
|
|
|
|
perror("Can't read clocks");
|
|
|
|
snprintf(buf, size, "%llu", nsecs / 1000000000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
|
2018-06-15 02:06:55 +08:00
|
|
|
(real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
|
|
|
|
1000000000;
|
|
|
|
|
2017-10-05 11:10:04 +08:00
|
|
|
|
|
|
|
if (!localtime_r(&wallclock_secs, &load_tm)) {
|
|
|
|
snprintf(buf, size, "%llu", nsecs / 1000000000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
tools: bpftool: change time format for program 'loaded at:' information
To make eBPF program load time easier to parse from "bpftool prog"
output for machines, change the time format used by the program. The
format now differs for plain and JSON version:
- Plain version uses a string formatted according to ISO 8601.
- JSON uses the number of seconds since the Epoch, wich is less friendly
for humans but even easier to process.
Example output:
# ./bpftool prog
41298: xdp tag a04f5eef06a7f555 dev foo
loaded_at 2018-04-18T17:19:47+0100 uid 0
xlated 16B not jited memlock 4096B
# ./bpftool prog -p
[{
"id": 41298,
"type": "xdp",
"tag": "a04f5eef06a7f555",
"gpl_compatible": false,
"dev": {
"ifindex": 14,
"ns_dev": 3,
"ns_inode": 4026531993,
"ifname": "foo"
},
"loaded_at": 1524068387,
"uid": 0,
"bytes_xlated": 16,
"jited": false,
"bytes_memlock": 4096
}
]
Previously, "Apr 18/17:19" would be used at both places.
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-02 04:18:38 +08:00
|
|
|
if (json_output)
|
|
|
|
strftime(buf, size, "%s", &load_tm);
|
|
|
|
else
|
|
|
|
strftime(buf, size, "%FT%T%z", &load_tm);
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 03:10:17 +08:00
|
|
|
static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)
|
2017-10-05 11:10:04 +08:00
|
|
|
{
|
|
|
|
unsigned int id = 0;
|
2019-12-14 03:10:04 +08:00
|
|
|
int fd, nb_fds = 0;
|
|
|
|
void *tmp;
|
2017-10-05 11:10:04 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
while (true) {
|
2019-01-18 20:58:17 +08:00
|
|
|
struct bpf_prog_info info = {};
|
|
|
|
__u32 len = sizeof(info);
|
|
|
|
|
2017-10-05 11:10:04 +08:00
|
|
|
err = bpf_prog_get_next_id(id, &id);
|
|
|
|
if (err) {
|
2019-12-14 03:10:04 +08:00
|
|
|
if (errno != ENOENT) {
|
|
|
|
p_err("%s", strerror(errno));
|
|
|
|
goto err_close_fds;
|
|
|
|
}
|
|
|
|
return nb_fds;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = bpf_prog_get_fd_by_id(id);
|
|
|
|
if (fd < 0) {
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("can't get prog by id (%u): %s",
|
|
|
|
id, strerror(errno));
|
2019-12-14 03:10:04 +08:00
|
|
|
goto err_close_fds;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = bpf_obj_get_info_by_fd(fd, &info, &len);
|
|
|
|
if (err) {
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("can't get prog info (%u): %s",
|
|
|
|
id, strerror(errno));
|
2019-12-14 03:10:04 +08:00
|
|
|
goto err_close_fd;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 03:10:17 +08:00
|
|
|
if ((tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) ||
|
|
|
|
(!tag && strncmp(nametag, info.name, BPF_OBJ_NAME_LEN))) {
|
2019-12-14 03:10:04 +08:00
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-05 11:10:04 +08:00
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
if (nb_fds > 0) {
|
|
|
|
tmp = realloc(*fds, (nb_fds + 1) * sizeof(int));
|
|
|
|
if (!tmp) {
|
|
|
|
p_err("failed to realloc");
|
|
|
|
goto err_close_fd;
|
|
|
|
}
|
|
|
|
*fds = tmp;
|
|
|
|
}
|
|
|
|
(*fds)[nb_fds++] = fd;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
2019-12-14 03:10:04 +08:00
|
|
|
|
|
|
|
err_close_fd:
|
|
|
|
close(fd);
|
|
|
|
err_close_fds:
|
|
|
|
while (--nb_fds >= 0)
|
|
|
|
close((*fds)[nb_fds]);
|
|
|
|
return -1;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
static int prog_parse_fds(int *argc, char ***argv, int **fds)
|
2017-10-05 11:10:04 +08:00
|
|
|
{
|
|
|
|
if (is_prefix(**argv, "id")) {
|
|
|
|
unsigned int id;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
|
|
|
id = strtoul(**argv, &endptr, 0);
|
|
|
|
if (*endptr) {
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("can't parse %s as ID", **argv);
|
2017-10-05 11:10:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
(*fds)[0] = bpf_prog_get_fd_by_id(id);
|
|
|
|
if ((*fds)[0] < 0) {
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("get by id (%u): %s", id, strerror(errno));
|
2019-12-14 03:10:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
2017-10-05 11:10:04 +08:00
|
|
|
} else if (is_prefix(**argv, "tag")) {
|
|
|
|
unsigned char tag[BPF_TAG_SIZE];
|
|
|
|
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
|
|
|
if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
|
|
|
|
tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
|
|
|
|
!= BPF_TAG_SIZE) {
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("can't parse tag");
|
2017-10-05 11:10:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
2019-12-14 03:10:17 +08:00
|
|
|
return prog_fd_by_nametag(tag, fds, true);
|
|
|
|
} else if (is_prefix(**argv, "name")) {
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
|
|
|
name = **argv;
|
|
|
|
if (strlen(name) > BPF_OBJ_NAME_LEN - 1) {
|
|
|
|
p_err("can't parse name");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
|
|
|
return prog_fd_by_nametag(name, fds, false);
|
2017-10-05 11:10:04 +08:00
|
|
|
} else if (is_prefix(**argv, "pinned")) {
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
|
|
|
path = **argv;
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
(*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_PROG);
|
|
|
|
if ((*fds)[0] < 0)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 03:10:17 +08:00
|
|
|
p_err("expected 'id', 'tag', 'name' or 'pinned', got: '%s'?", **argv);
|
2017-10-05 11:10:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
int prog_parse_fd(int *argc, char ***argv)
|
|
|
|
{
|
|
|
|
int *fds = NULL;
|
|
|
|
int nb_fds, fd;
|
|
|
|
|
|
|
|
fds = malloc(sizeof(int));
|
|
|
|
if (!fds) {
|
|
|
|
p_err("mem alloc failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
nb_fds = prog_parse_fds(argc, argv, &fds);
|
|
|
|
if (nb_fds != 1) {
|
|
|
|
if (nb_fds > 1) {
|
|
|
|
p_err("several programs match this handle");
|
|
|
|
while (nb_fds--)
|
|
|
|
close(fds[nb_fds]);
|
|
|
|
}
|
|
|
|
fd = -1;
|
|
|
|
goto exit_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = fds[0];
|
|
|
|
exit_free:
|
|
|
|
free(fds);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2017-10-05 11:10:04 +08:00
|
|
|
static void show_prog_maps(int fd, u32 num_maps)
|
|
|
|
{
|
|
|
|
struct bpf_prog_info info = {};
|
|
|
|
__u32 len = sizeof(info);
|
|
|
|
__u32 map_ids[num_maps];
|
|
|
|
unsigned int i;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
info.nr_map_ids = num_maps;
|
|
|
|
info.map_ids = ptr_to_u64(map_ids);
|
|
|
|
|
|
|
|
err = bpf_obj_get_info_by_fd(fd, &info, &len);
|
|
|
|
if (err || !info.nr_map_ids)
|
|
|
|
return;
|
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
if (json_output) {
|
|
|
|
jsonw_name(json_wtr, "map_ids");
|
|
|
|
jsonw_start_array(json_wtr);
|
|
|
|
for (i = 0; i < info.nr_map_ids; i++)
|
|
|
|
jsonw_uint(json_wtr, map_ids[i]);
|
|
|
|
jsonw_end_array(json_wtr);
|
|
|
|
} else {
|
|
|
|
printf(" map_ids ");
|
|
|
|
for (i = 0; i < info.nr_map_ids; i++)
|
|
|
|
printf("%u%s", map_ids[i],
|
|
|
|
i == info.nr_map_ids - 1 ? "" : ",");
|
|
|
|
}
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
static void print_prog_header_json(struct bpf_prog_info *info)
|
2017-10-05 11:10:04 +08:00
|
|
|
{
|
2017-10-24 00:24:08 +08:00
|
|
|
jsonw_uint_field(json_wtr, "id", info->id);
|
|
|
|
if (info->type < ARRAY_SIZE(prog_type_name))
|
|
|
|
jsonw_string_field(json_wtr, "type",
|
|
|
|
prog_type_name[info->type]);
|
|
|
|
else
|
|
|
|
jsonw_uint_field(json_wtr, "type", info->type);
|
|
|
|
|
|
|
|
if (*info->name)
|
|
|
|
jsonw_string_field(json_wtr, "name", info->name);
|
|
|
|
|
|
|
|
jsonw_name(json_wtr, "tag");
|
|
|
|
jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
|
|
|
|
info->tag[0], info->tag[1], info->tag[2], info->tag[3],
|
|
|
|
info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
|
|
|
|
|
tools, bpftool: Display license GPL compatible in prog show/list
Display the license "gpl" string in bpftool prog command, like:
# bpftool prog list
5: tracepoint name func tag 57cd311f2e27366b gpl
loaded_at Apr 26/09:37 uid 0
xlated 16B not jited memlock 4096B
# bpftool --json --pretty prog show
[{
"id": 5,
"type": "tracepoint",
"name": "func",
"tag": "57cd311f2e27366b",
"gpl_compatible": true,
"loaded_at": "Apr 26/09:37",
"uid": 0,
"bytes_xlated": 16,
"jited": false,
"bytes_memlock": 4096
}
]
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-04-26 16:18:01 +08:00
|
|
|
jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
|
tools/bpftool: recognize bpf_prog_info run_time_ns and run_cnt
$ bpftool p s
1: kprobe tag a56587d488d216c9 gpl run_time_ns 79786 run_cnt 8
loaded_at 2019-02-22T12:22:51-0800 uid 0
xlated 352B not jited memlock 4096B
$ bpftool --json --pretty p s
[{
"id": 1,
"type": "kprobe",
"tag": "a56587d488d216c9",
"gpl_compatible": true,
"run_time_ns": 79786,
"run_cnt": 8,
"loaded_at": 1550866971,
"uid": 0,
"bytes_xlated": 352,
"jited": false,
"bytes_memlock": 4096
}
]
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-02-26 06:28:42 +08:00
|
|
|
if (info->run_time_ns) {
|
|
|
|
jsonw_uint_field(json_wtr, "run_time_ns", info->run_time_ns);
|
|
|
|
jsonw_uint_field(json_wtr, "run_cnt", info->run_cnt);
|
|
|
|
}
|
2019-12-14 03:10:04 +08:00
|
|
|
}
|
tools, bpftool: Display license GPL compatible in prog show/list
Display the license "gpl" string in bpftool prog command, like:
# bpftool prog list
5: tracepoint name func tag 57cd311f2e27366b gpl
loaded_at Apr 26/09:37 uid 0
xlated 16B not jited memlock 4096B
# bpftool --json --pretty prog show
[{
"id": 5,
"type": "tracepoint",
"name": "func",
"tag": "57cd311f2e27366b",
"gpl_compatible": true,
"loaded_at": "Apr 26/09:37",
"uid": 0,
"bytes_xlated": 16,
"jited": false,
"bytes_memlock": 4096
}
]
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-04-26 16:18:01 +08:00
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
static void print_prog_json(struct bpf_prog_info *info, int fd)
|
|
|
|
{
|
|
|
|
char *memlock;
|
tools, bpftool: Display license GPL compatible in prog show/list
Display the license "gpl" string in bpftool prog command, like:
# bpftool prog list
5: tracepoint name func tag 57cd311f2e27366b gpl
loaded_at Apr 26/09:37 uid 0
xlated 16B not jited memlock 4096B
# bpftool --json --pretty prog show
[{
"id": 5,
"type": "tracepoint",
"name": "func",
"tag": "57cd311f2e27366b",
"gpl_compatible": true,
"loaded_at": "Apr 26/09:37",
"uid": 0,
"bytes_xlated": 16,
"jited": false,
"bytes_memlock": 4096
}
]
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-04-26 16:18:01 +08:00
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
jsonw_start_object(json_wtr);
|
|
|
|
print_prog_header_json(info);
|
2017-12-28 10:39:10 +08:00
|
|
|
print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
|
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
if (info->load_time) {
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
print_boot_time(info->load_time, buf, sizeof(buf));
|
|
|
|
|
|
|
|
/* Piggy back on load_time, since 0 uid is a valid one */
|
tools: bpftool: change time format for program 'loaded at:' information
To make eBPF program load time easier to parse from "bpftool prog"
output for machines, change the time format used by the program. The
format now differs for plain and JSON version:
- Plain version uses a string formatted according to ISO 8601.
- JSON uses the number of seconds since the Epoch, wich is less friendly
for humans but even easier to process.
Example output:
# ./bpftool prog
41298: xdp tag a04f5eef06a7f555 dev foo
loaded_at 2018-04-18T17:19:47+0100 uid 0
xlated 16B not jited memlock 4096B
# ./bpftool prog -p
[{
"id": 41298,
"type": "xdp",
"tag": "a04f5eef06a7f555",
"gpl_compatible": false,
"dev": {
"ifindex": 14,
"ns_dev": 3,
"ns_inode": 4026531993,
"ifname": "foo"
},
"loaded_at": 1524068387,
"uid": 0,
"bytes_xlated": 16,
"jited": false,
"bytes_memlock": 4096
}
]
Previously, "Apr 18/17:19" would be used at both places.
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-02 04:18:38 +08:00
|
|
|
jsonw_name(json_wtr, "loaded_at");
|
|
|
|
jsonw_printf(json_wtr, "%s", buf);
|
2017-10-24 00:24:08 +08:00
|
|
|
jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
|
|
|
|
|
|
|
|
if (info->jited_prog_len) {
|
|
|
|
jsonw_bool_field(json_wtr, "jited", true);
|
|
|
|
jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
|
|
|
|
} else {
|
|
|
|
jsonw_bool_field(json_wtr, "jited", false);
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
memlock = get_fdinfo(fd, "memlock");
|
|
|
|
if (memlock)
|
|
|
|
jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
|
|
|
|
free(memlock);
|
|
|
|
|
|
|
|
if (info->nr_map_ids)
|
|
|
|
show_prog_maps(fd, info->nr_map_ids);
|
|
|
|
|
2019-04-10 12:56:42 +08:00
|
|
|
if (info->btf_id)
|
|
|
|
jsonw_int_field(json_wtr, "btf_id", info->btf_id);
|
|
|
|
|
tools: bpftool: show filenames of pinned objects
Added support to show filenames of pinned objects.
For example:
root@test# ./bpftool prog
3: tracepoint name tracepoint__irq tag f677a7dd722299a3
loaded_at Oct 26/11:39 uid 0
xlated 160B not jited memlock 4096B map_ids 4
pinned /sys/fs/bpf/softirq_prog
4: tracepoint name tracepoint__irq tag ea5dc530d00b92b6
loaded_at Oct 26/11:39 uid 0
xlated 392B not jited memlock 4096B map_ids 4,6
root@test# ./bpftool --json --pretty prog
[{
"id": 3,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "f677a7dd722299a3",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 160,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4
],
"pinned": ["/sys/fs/bpf/softirq_prog"
]
},{
"id": 4,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "ea5dc530d00b92b6",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 392,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4,6
],
"pinned": []
}
]
root@test# ./bpftool map
4: hash name start flags 0x0
key 4B value 16B max_entries 10240 memlock 1003520B
pinned /sys/fs/bpf/softirq_map1
5: hash name iptr flags 0x0
key 4B value 8B max_entries 10240 memlock 921600B
root@test# ./bpftool --json --pretty map
[{
"id": 4,
"type": "hash",
"name": "start",
"flags": 0,
"bytes_key": 4,
"bytes_value": 16,
"max_entries": 10240,
"bytes_memlock": 1003520,
"pinned": ["/sys/fs/bpf/softirq_map1"
]
},{
"id": 5,
"type": "hash",
"name": "iptr",
"flags": 0,
"bytes_key": 4,
"bytes_value": 8,
"max_entries": 10240,
"bytes_memlock": 921600,
"pinned": []
}
]
Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-11-08 12:55:48 +08:00
|
|
|
if (!hash_empty(prog_table.table)) {
|
|
|
|
struct pinned_obj *obj;
|
|
|
|
|
|
|
|
jsonw_name(json_wtr, "pinned");
|
|
|
|
jsonw_start_array(json_wtr);
|
|
|
|
hash_for_each_possible(prog_table.table, obj, hash, info->id) {
|
|
|
|
if (obj->id == info->id)
|
|
|
|
jsonw_string(json_wtr, obj->path);
|
|
|
|
}
|
|
|
|
jsonw_end_array(json_wtr);
|
|
|
|
}
|
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
jsonw_end_object(json_wtr);
|
|
|
|
}
|
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
static void print_prog_header_plain(struct bpf_prog_info *info)
|
2017-10-24 00:24:08 +08:00
|
|
|
{
|
|
|
|
printf("%u: ", info->id);
|
|
|
|
if (info->type < ARRAY_SIZE(prog_type_name))
|
|
|
|
printf("%s ", prog_type_name[info->type]);
|
2017-10-05 11:10:04 +08:00
|
|
|
else
|
2017-10-24 00:24:08 +08:00
|
|
|
printf("type %u ", info->type);
|
2017-10-05 11:10:04 +08:00
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
if (*info->name)
|
|
|
|
printf("name %s ", info->name);
|
2017-10-05 11:10:04 +08:00
|
|
|
|
|
|
|
printf("tag ");
|
2017-10-24 00:24:08 +08:00
|
|
|
fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
|
2017-12-28 10:39:10 +08:00
|
|
|
print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
|
tools, bpftool: Display license GPL compatible in prog show/list
Display the license "gpl" string in bpftool prog command, like:
# bpftool prog list
5: tracepoint name func tag 57cd311f2e27366b gpl
loaded_at Apr 26/09:37 uid 0
xlated 16B not jited memlock 4096B
# bpftool --json --pretty prog show
[{
"id": 5,
"type": "tracepoint",
"name": "func",
"tag": "57cd311f2e27366b",
"gpl_compatible": true,
"loaded_at": "Apr 26/09:37",
"uid": 0,
"bytes_xlated": 16,
"jited": false,
"bytes_memlock": 4096
}
]
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-04-26 16:18:01 +08:00
|
|
|
printf("%s", info->gpl_compatible ? " gpl" : "");
|
tools/bpftool: recognize bpf_prog_info run_time_ns and run_cnt
$ bpftool p s
1: kprobe tag a56587d488d216c9 gpl run_time_ns 79786 run_cnt 8
loaded_at 2019-02-22T12:22:51-0800 uid 0
xlated 352B not jited memlock 4096B
$ bpftool --json --pretty p s
[{
"id": 1,
"type": "kprobe",
"tag": "a56587d488d216c9",
"gpl_compatible": true,
"run_time_ns": 79786,
"run_cnt": 8,
"loaded_at": 1550866971,
"uid": 0,
"bytes_xlated": 352,
"jited": false,
"bytes_memlock": 4096
}
]
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-02-26 06:28:42 +08:00
|
|
|
if (info->run_time_ns)
|
|
|
|
printf(" run_time_ns %lld run_cnt %lld",
|
|
|
|
info->run_time_ns, info->run_cnt);
|
2017-10-05 11:10:04 +08:00
|
|
|
printf("\n");
|
2019-12-14 03:10:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_prog_plain(struct bpf_prog_info *info, int fd)
|
|
|
|
{
|
|
|
|
char *memlock;
|
|
|
|
|
|
|
|
print_prog_header_plain(info);
|
2017-10-05 11:10:04 +08:00
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
if (info->load_time) {
|
2017-10-05 11:10:04 +08:00
|
|
|
char buf[32];
|
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
print_boot_time(info->load_time, buf, sizeof(buf));
|
2017-10-05 11:10:04 +08:00
|
|
|
|
|
|
|
/* Piggy back on load_time, since 0 uid is a valid one */
|
2017-10-24 00:24:08 +08:00
|
|
|
printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
printf("\txlated %uB", info->xlated_prog_len);
|
2017-10-05 11:10:04 +08:00
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
if (info->jited_prog_len)
|
|
|
|
printf(" jited %uB", info->jited_prog_len);
|
2017-10-05 11:10:04 +08:00
|
|
|
else
|
|
|
|
printf(" not jited");
|
|
|
|
|
|
|
|
memlock = get_fdinfo(fd, "memlock");
|
|
|
|
if (memlock)
|
|
|
|
printf(" memlock %sB", memlock);
|
|
|
|
free(memlock);
|
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
if (info->nr_map_ids)
|
|
|
|
show_prog_maps(fd, info->nr_map_ids);
|
2017-10-05 11:10:04 +08:00
|
|
|
|
tools: bpftool: show filenames of pinned objects
Added support to show filenames of pinned objects.
For example:
root@test# ./bpftool prog
3: tracepoint name tracepoint__irq tag f677a7dd722299a3
loaded_at Oct 26/11:39 uid 0
xlated 160B not jited memlock 4096B map_ids 4
pinned /sys/fs/bpf/softirq_prog
4: tracepoint name tracepoint__irq tag ea5dc530d00b92b6
loaded_at Oct 26/11:39 uid 0
xlated 392B not jited memlock 4096B map_ids 4,6
root@test# ./bpftool --json --pretty prog
[{
"id": 3,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "f677a7dd722299a3",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 160,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4
],
"pinned": ["/sys/fs/bpf/softirq_prog"
]
},{
"id": 4,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "ea5dc530d00b92b6",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 392,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4,6
],
"pinned": []
}
]
root@test# ./bpftool map
4: hash name start flags 0x0
key 4B value 16B max_entries 10240 memlock 1003520B
pinned /sys/fs/bpf/softirq_map1
5: hash name iptr flags 0x0
key 4B value 8B max_entries 10240 memlock 921600B
root@test# ./bpftool --json --pretty map
[{
"id": 4,
"type": "hash",
"name": "start",
"flags": 0,
"bytes_key": 4,
"bytes_value": 16,
"max_entries": 10240,
"bytes_memlock": 1003520,
"pinned": ["/sys/fs/bpf/softirq_map1"
]
},{
"id": 5,
"type": "hash",
"name": "iptr",
"flags": 0,
"bytes_key": 4,
"bytes_value": 8,
"max_entries": 10240,
"bytes_memlock": 921600,
"pinned": []
}
]
Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-11-08 12:55:48 +08:00
|
|
|
if (!hash_empty(prog_table.table)) {
|
|
|
|
struct pinned_obj *obj;
|
|
|
|
|
|
|
|
hash_for_each_possible(prog_table.table, obj, hash, info->id) {
|
|
|
|
if (obj->id == info->id)
|
2018-11-08 19:52:26 +08:00
|
|
|
printf("\n\tpinned %s", obj->path);
|
tools: bpftool: show filenames of pinned objects
Added support to show filenames of pinned objects.
For example:
root@test# ./bpftool prog
3: tracepoint name tracepoint__irq tag f677a7dd722299a3
loaded_at Oct 26/11:39 uid 0
xlated 160B not jited memlock 4096B map_ids 4
pinned /sys/fs/bpf/softirq_prog
4: tracepoint name tracepoint__irq tag ea5dc530d00b92b6
loaded_at Oct 26/11:39 uid 0
xlated 392B not jited memlock 4096B map_ids 4,6
root@test# ./bpftool --json --pretty prog
[{
"id": 3,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "f677a7dd722299a3",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 160,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4
],
"pinned": ["/sys/fs/bpf/softirq_prog"
]
},{
"id": 4,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "ea5dc530d00b92b6",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 392,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4,6
],
"pinned": []
}
]
root@test# ./bpftool map
4: hash name start flags 0x0
key 4B value 16B max_entries 10240 memlock 1003520B
pinned /sys/fs/bpf/softirq_map1
5: hash name iptr flags 0x0
key 4B value 8B max_entries 10240 memlock 921600B
root@test# ./bpftool --json --pretty map
[{
"id": 4,
"type": "hash",
"name": "start",
"flags": 0,
"bytes_key": 4,
"bytes_value": 16,
"max_entries": 10240,
"bytes_memlock": 1003520,
"pinned": ["/sys/fs/bpf/softirq_map1"
]
},{
"id": 5,
"type": "hash",
"name": "iptr",
"flags": 0,
"bytes_key": 4,
"bytes_value": 8,
"max_entries": 10240,
"bytes_memlock": 921600,
"pinned": []
}
]
Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-11-08 12:55:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 12:56:42 +08:00
|
|
|
if (info->btf_id)
|
2019-04-12 21:29:34 +08:00
|
|
|
printf("\n\tbtf_id %d", info->btf_id);
|
2019-04-10 12:56:42 +08:00
|
|
|
|
2017-10-05 11:10:04 +08:00
|
|
|
printf("\n");
|
2017-10-24 00:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int show_prog(int fd)
|
|
|
|
{
|
|
|
|
struct bpf_prog_info info = {};
|
|
|
|
__u32 len = sizeof(info);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = bpf_obj_get_info_by_fd(fd, &info, &len);
|
|
|
|
if (err) {
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("can't get prog info: %s", strerror(errno));
|
2017-10-24 00:24:08 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
print_prog_json(&info, fd);
|
|
|
|
else
|
|
|
|
print_prog_plain(&info, fd);
|
2017-10-05 11:10:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
static int do_show_subset(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int *fds = NULL;
|
|
|
|
int nb_fds, i;
|
|
|
|
int err = -1;
|
|
|
|
|
|
|
|
fds = malloc(sizeof(int));
|
|
|
|
if (!fds) {
|
|
|
|
p_err("mem alloc failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
nb_fds = prog_parse_fds(&argc, &argv, &fds);
|
|
|
|
if (nb_fds < 1)
|
|
|
|
goto exit_free;
|
|
|
|
|
|
|
|
if (json_output && nb_fds > 1)
|
|
|
|
jsonw_start_array(json_wtr); /* root array */
|
|
|
|
for (i = 0; i < nb_fds; i++) {
|
|
|
|
err = show_prog(fds[i]);
|
|
|
|
if (err) {
|
|
|
|
for (; i < nb_fds; i++)
|
|
|
|
close(fds[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fds[i]);
|
|
|
|
}
|
|
|
|
if (json_output && nb_fds > 1)
|
|
|
|
jsonw_end_array(json_wtr); /* root array */
|
|
|
|
|
|
|
|
exit_free:
|
|
|
|
free(fds);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-10-05 11:10:04 +08:00
|
|
|
static int do_show(int argc, char **argv)
|
2017-10-24 00:24:08 +08:00
|
|
|
{
|
|
|
|
__u32 id = 0;
|
2017-10-05 11:10:04 +08:00
|
|
|
int err;
|
|
|
|
int fd;
|
|
|
|
|
2017-11-08 12:55:49 +08:00
|
|
|
if (show_pinned)
|
|
|
|
build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
|
tools: bpftool: show filenames of pinned objects
Added support to show filenames of pinned objects.
For example:
root@test# ./bpftool prog
3: tracepoint name tracepoint__irq tag f677a7dd722299a3
loaded_at Oct 26/11:39 uid 0
xlated 160B not jited memlock 4096B map_ids 4
pinned /sys/fs/bpf/softirq_prog
4: tracepoint name tracepoint__irq tag ea5dc530d00b92b6
loaded_at Oct 26/11:39 uid 0
xlated 392B not jited memlock 4096B map_ids 4,6
root@test# ./bpftool --json --pretty prog
[{
"id": 3,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "f677a7dd722299a3",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 160,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4
],
"pinned": ["/sys/fs/bpf/softirq_prog"
]
},{
"id": 4,
"type": "tracepoint",
"name": "tracepoint__irq",
"tag": "ea5dc530d00b92b6",
"loaded_at": "Oct 26/11:39",
"uid": 0,
"bytes_xlated": 392,
"jited": false,
"bytes_memlock": 4096,
"map_ids": [4,6
],
"pinned": []
}
]
root@test# ./bpftool map
4: hash name start flags 0x0
key 4B value 16B max_entries 10240 memlock 1003520B
pinned /sys/fs/bpf/softirq_map1
5: hash name iptr flags 0x0
key 4B value 8B max_entries 10240 memlock 921600B
root@test# ./bpftool --json --pretty map
[{
"id": 4,
"type": "hash",
"name": "start",
"flags": 0,
"bytes_key": 4,
"bytes_value": 16,
"max_entries": 10240,
"bytes_memlock": 1003520,
"pinned": ["/sys/fs/bpf/softirq_map1"
]
},{
"id": 5,
"type": "hash",
"name": "iptr",
"flags": 0,
"bytes_key": 4,
"bytes_value": 8,
"max_entries": 10240,
"bytes_memlock": 921600,
"pinned": []
}
]
Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-11-08 12:55:48 +08:00
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
if (argc == 2)
|
|
|
|
return do_show_subset(argc, argv);
|
2017-10-05 11:10:04 +08:00
|
|
|
|
|
|
|
if (argc)
|
|
|
|
return BAD_ARG();
|
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
if (json_output)
|
|
|
|
jsonw_start_array(json_wtr);
|
2017-10-05 11:10:04 +08:00
|
|
|
while (true) {
|
|
|
|
err = bpf_prog_get_next_id(id, &id);
|
|
|
|
if (err) {
|
2017-10-20 06:46:20 +08:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
err = 0;
|
2017-10-05 11:10:04 +08:00
|
|
|
break;
|
2017-10-20 06:46:20 +08:00
|
|
|
}
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("can't get next program: %s%s", strerror(errno),
|
|
|
|
errno == EINVAL ? " -- kernel too old?" : "");
|
2017-10-24 00:24:08 +08:00
|
|
|
err = -1;
|
|
|
|
break;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = bpf_prog_get_fd_by_id(id);
|
|
|
|
if (fd < 0) {
|
2017-12-23 03:36:06 +08:00
|
|
|
if (errno == ENOENT)
|
|
|
|
continue;
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("can't get prog by id (%u): %s",
|
|
|
|
id, strerror(errno));
|
2017-10-24 00:24:08 +08:00
|
|
|
err = -1;
|
|
|
|
break;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = show_prog(fd);
|
|
|
|
close(fd);
|
|
|
|
if (err)
|
2017-10-24 00:24:08 +08:00
|
|
|
break;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2017-10-24 00:24:08 +08:00
|
|
|
if (json_output)
|
|
|
|
jsonw_end_array(json_wtr);
|
|
|
|
|
|
|
|
return err;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
static int
|
|
|
|
prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
|
|
|
|
char *filepath, bool opcodes, bool visual, bool linum)
|
2017-10-05 11:10:04 +08:00
|
|
|
{
|
bpf: libbpf: bpftool: Print bpf_line_info during prog dump
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>
2018-12-08 08:42:32 +08:00
|
|
|
struct bpf_prog_linfo *prog_linfo = NULL;
|
2018-10-19 02:34:55 +08:00
|
|
|
const char *disasm_opt = NULL;
|
bpf: allow for correlation of maps and helpers in dump
Currently a dump of an xlated prog (post verifier stage) doesn't
correlate used helpers as well as maps. The prog info lists
involved map ids, however there's no correlation of where in the
program they are used as of today. Likewise, bpftool does not
correlate helper calls with the target functions.
The latter can be done w/o any kernel changes through kallsyms,
and also has the advantage that this works with inlined helpers
and BPF calls.
Example, via interpreter:
# tc filter show dev foo ingress
filter protocol all pref 49152 bpf chain 0
filter protocol all pref 49152 bpf chain 0 handle 0x1 foo.o:[ingress] \
direct-action not_in_hw id 1 tag c74773051b364165 <-- prog id:1
* Output before patch (calls/maps remain unclear):
# bpftool prog dump xlated id 1 <-- dump prog id:1
0: (b7) r1 = 2
1: (63) *(u32 *)(r10 -4) = r1
2: (bf) r2 = r10
3: (07) r2 += -4
4: (18) r1 = 0xffff95c47a8d4800
6: (85) call unknown#73040
7: (15) if r0 == 0x0 goto pc+18
8: (bf) r2 = r10
9: (07) r2 += -4
10: (bf) r1 = r0
11: (85) call unknown#73040
12: (15) if r0 == 0x0 goto pc+23
[...]
* Output after patch:
# bpftool prog dump xlated id 1
0: (b7) r1 = 2
1: (63) *(u32 *)(r10 -4) = r1
2: (bf) r2 = r10
3: (07) r2 += -4
4: (18) r1 = map[id:2] <-- map id:2
6: (85) call bpf_map_lookup_elem#73424 <-- helper call
7: (15) if r0 == 0x0 goto pc+18
8: (bf) r2 = r10
9: (07) r2 += -4
10: (bf) r1 = r0
11: (85) call bpf_map_lookup_elem#73424
12: (15) if r0 == 0x0 goto pc+23
[...]
# bpftool map show id 2 <-- show/dump/etc map id:2
2: hash_of_maps flags 0x0
key 4B value 4B max_entries 3 memlock 4096B
Example, JITed, same prog:
# tc filter show dev foo ingress
filter protocol all pref 49152 bpf chain 0
filter protocol all pref 49152 bpf chain 0 handle 0x1 foo.o:[ingress] \
direct-action not_in_hw id 3 tag c74773051b364165 jited
# bpftool prog show id 3
3: sched_cls tag c74773051b364165
loaded_at Dec 19/13:48 uid 0
xlated 384B jited 257B memlock 4096B map_ids 2
# bpftool prog dump xlated id 3
0: (b7) r1 = 2
1: (63) *(u32 *)(r10 -4) = r1
2: (bf) r2 = r10
3: (07) r2 += -4
4: (18) r1 = map[id:2] <-- map id:2
6: (85) call __htab_map_lookup_elem#77408 <-+ inlined rewrite
7: (15) if r0 == 0x0 goto pc+2 |
8: (07) r0 += 56 |
9: (79) r0 = *(u64 *)(r0 +0) <-+
10: (15) if r0 == 0x0 goto pc+24
11: (bf) r2 = r10
12: (07) r2 += -4
[...]
Example, same prog, but kallsyms disabled (in that case we are
also not allowed to pass any relative offsets, etc, so prog
becomes pointer sanitized on dump):
# sysctl kernel.kptr_restrict=2
kernel.kptr_restrict = 2
# bpftool prog dump xlated id 3
0: (b7) r1 = 2
1: (63) *(u32 *)(r10 -4) = r1
2: (bf) r2 = r10
3: (07) r2 += -4
4: (18) r1 = map[id:2]
6: (85) call bpf_unspec#0
7: (15) if r0 == 0x0 goto pc+2
[...]
Example, BPF calls via interpreter:
# bpftool prog dump xlated id 1
0: (85) call pc+2#__bpf_prog_run_args32
1: (b7) r0 = 1
2: (95) exit
3: (b7) r0 = 2
4: (95) exit
Example, BPF calls via JIT:
# sysctl net.core.bpf_jit_enable=1
net.core.bpf_jit_enable = 1
# sysctl net.core.bpf_jit_kallsyms=1
net.core.bpf_jit_kallsyms = 1
# bpftool prog dump xlated id 1
0: (85) call pc+2#bpf_prog_3b185187f1855c4c_F
1: (b7) r0 = 1
2: (95) exit
3: (b7) r0 = 2
4: (95) exit
And finally, an example for tail calls that is now working
as well wrt correlation:
# bpftool prog dump xlated id 2
[...]
10: (b7) r2 = 8
11: (85) call bpf_trace_printk#-41312
12: (bf) r1 = r6
13: (18) r2 = map[id:1]
15: (b7) r3 = 0
16: (85) call bpf_tail_call#12
17: (b7) r1 = 42
18: (6b) *(u16 *)(r6 +46) = r1
19: (b7) r0 = 0
20: (95) exit
# bpftool map show id 1
1: prog_array flags 0x0
key 4B value 4B max_entries 1 memlock 4096B
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2017-12-20 20:42:57 +08:00
|
|
|
struct dump_data dd = {};
|
2019-03-12 13:30:39 +08:00
|
|
|
void *func_info = NULL;
|
2018-11-20 07:29:21 +08:00
|
|
|
struct btf *btf = NULL;
|
|
|
|
char func_sig[1024];
|
2017-10-05 11:10:04 +08:00
|
|
|
unsigned char *buf;
|
2019-03-12 13:30:39 +08:00
|
|
|
__u32 member_len;
|
2017-10-05 11:10:04 +08:00
|
|
|
ssize_t n;
|
|
|
|
int fd;
|
|
|
|
|
2019-03-12 13:30:39 +08:00
|
|
|
if (mode == DUMP_JITED) {
|
2019-12-11 02:14:12 +08:00
|
|
|
if (info->jited_prog_len == 0 || !info->jited_prog_insns) {
|
2019-03-12 13:30:39 +08:00
|
|
|
p_info("no instructions returned");
|
2019-12-14 03:10:04 +08:00
|
|
|
return -1;
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
}
|
2019-03-12 13:30:39 +08:00
|
|
|
buf = (unsigned char *)(info->jited_prog_insns);
|
|
|
|
member_len = info->jited_prog_len;
|
|
|
|
} else { /* DUMP_XLATED */
|
2020-02-06 18:29:06 +08:00
|
|
|
if (info->xlated_prog_len == 0 || !info->xlated_prog_insns) {
|
2019-03-12 13:30:39 +08:00
|
|
|
p_err("error retrieving insn dump: kernel.kptr_restrict set?");
|
2019-12-14 03:10:04 +08:00
|
|
|
return -1;
|
2018-11-20 07:29:21 +08:00
|
|
|
}
|
2019-03-12 13:30:39 +08:00
|
|
|
buf = (unsigned char *)info->xlated_prog_insns;
|
|
|
|
member_len = info->xlated_prog_len;
|
2018-11-20 07:29:21 +08:00
|
|
|
}
|
|
|
|
|
2019-03-12 13:30:39 +08:00
|
|
|
if (info->btf_id && btf__get_from_id(info->btf_id, &btf)) {
|
2018-11-20 07:29:21 +08:00
|
|
|
p_err("failed to get btf");
|
2019-12-14 03:10:04 +08:00
|
|
|
return -1;
|
2018-11-20 07:29:21 +08:00
|
|
|
}
|
|
|
|
|
2019-03-12 13:30:39 +08:00
|
|
|
func_info = (void *)info->func_info;
|
|
|
|
|
|
|
|
if (info->nr_line_info) {
|
|
|
|
prog_linfo = bpf_prog_linfo__new(info);
|
bpf: libbpf: bpftool: Print bpf_line_info during prog dump
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>
2018-12-08 08:42:32 +08:00
|
|
|
if (!prog_linfo)
|
2018-12-11 02:53:24 +08:00
|
|
|
p_info("error in processing bpf_line_info. continue without it.");
|
bpf: libbpf: bpftool: Print bpf_line_info during prog dump
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>
2018-12-08 08:42:32 +08:00
|
|
|
}
|
|
|
|
|
2017-10-05 11:10:04 +08:00
|
|
|
if (filepath) {
|
|
|
|
fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
|
|
if (fd < 0) {
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("can't open file %s: %s", filepath,
|
|
|
|
strerror(errno));
|
2019-12-14 03:10:04 +08:00
|
|
|
return -1;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2019-03-12 13:30:39 +08:00
|
|
|
n = write(fd, buf, member_len);
|
2017-10-05 11:10:04 +08:00
|
|
|
close(fd);
|
2019-03-12 13:30:39 +08:00
|
|
|
if (n != member_len) {
|
2017-10-24 00:24:13 +08:00
|
|
|
p_err("error writing output file: %s",
|
|
|
|
n < 0 ? strerror(errno) : "short write");
|
2019-12-14 03:10:04 +08:00
|
|
|
return -1;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
2018-02-15 14:42:54 +08:00
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_null(json_wtr);
|
2019-03-12 13:30:39 +08:00
|
|
|
} else if (mode == DUMP_JITED) {
|
2018-03-02 10:01:16 +08:00
|
|
|
const char *name = NULL;
|
|
|
|
|
2019-03-12 13:30:39 +08:00
|
|
|
if (info->ifindex) {
|
|
|
|
name = ifindex_to_bfd_params(info->ifindex,
|
|
|
|
info->netns_dev,
|
|
|
|
info->netns_ino,
|
2018-10-19 02:34:55 +08:00
|
|
|
&disasm_opt);
|
2018-03-02 10:01:16 +08:00
|
|
|
if (!name)
|
2019-12-14 03:10:04 +08:00
|
|
|
return -1;
|
bpf: allow for correlation of maps and helpers in dump
Currently a dump of an xlated prog (post verifier stage) doesn't
correlate used helpers as well as maps. The prog info lists
involved map ids, however there's no correlation of where in the
program they are used as of today. Likewise, bpftool does not
correlate helper calls with the target functions.
The latter can be done w/o any kernel changes through kallsyms,
and also has the advantage that this works with inlined helpers
and BPF calls.
Example, via interpreter:
# tc filter show dev foo ingress
filter protocol all pref 49152 bpf chain 0
filter protocol all pref 49152 bpf chain 0 handle 0x1 foo.o:[ingress] \
direct-action not_in_hw id 1 tag c74773051b364165 <-- prog id:1
* Output before patch (calls/maps remain unclear):
# bpftool prog dump xlated id 1 <-- dump prog id:1
0: (b7) r1 = 2
1: (63) *(u32 *)(r10 -4) = r1
2: (bf) r2 = r10
3: (07) r2 += -4
4: (18) r1 = 0xffff95c47a8d4800
6: (85) call unknown#73040
7: (15) if r0 == 0x0 goto pc+18
8: (bf) r2 = r10
9: (07) r2 += -4
10: (bf) r1 = r0
11: (85) call unknown#73040
12: (15) if r0 == 0x0 goto pc+23
[...]
* Output after patch:
# bpftool prog dump xlated id 1
0: (b7) r1 = 2
1: (63) *(u32 *)(r10 -4) = r1
2: (bf) r2 = r10
3: (07) r2 += -4
4: (18) r1 = map[id:2] <-- map id:2
6: (85) call bpf_map_lookup_elem#73424 <-- helper call
7: (15) if r0 == 0x0 goto pc+18
8: (bf) r2 = r10
9: (07) r2 += -4
10: (bf) r1 = r0
11: (85) call bpf_map_lookup_elem#73424
12: (15) if r0 == 0x0 goto pc+23
[...]
# bpftool map show id 2 <-- show/dump/etc map id:2
2: hash_of_maps flags 0x0
key 4B value 4B max_entries 3 memlock 4096B
Example, JITed, same prog:
# tc filter show dev foo ingress
filter protocol all pref 49152 bpf chain 0
filter protocol all pref 49152 bpf chain 0 handle 0x1 foo.o:[ingress] \
direct-action not_in_hw id 3 tag c74773051b364165 jited
# bpftool prog show id 3
3: sched_cls tag c74773051b364165
loaded_at Dec 19/13:48 uid 0
xlated 384B jited 257B memlock 4096B map_ids 2
# bpftool prog dump xlated id 3
0: (b7) r1 = 2
1: (63) *(u32 *)(r10 -4) = r1
2: (bf) r2 = r10
3: (07) r2 += -4
4: (18) r1 = map[id:2] <-- map id:2
6: (85) call __htab_map_lookup_elem#77408 <-+ inlined rewrite
7: (15) if r0 == 0x0 goto pc+2 |
8: (07) r0 += 56 |
9: (79) r0 = *(u64 *)(r0 +0) <-+
10: (15) if r0 == 0x0 goto pc+24
11: (bf) r2 = r10
12: (07) r2 += -4
[...]
Example, same prog, but kallsyms disabled (in that case we are
also not allowed to pass any relative offsets, etc, so prog
becomes pointer sanitized on dump):
# sysctl kernel.kptr_restrict=2
kernel.kptr_restrict = 2
# bpftool prog dump xlated id 3
0: (b7) r1 = 2
1: (63) *(u32 *)(r10 -4) = r1
2: (bf) r2 = r10
3: (07) r2 += -4
4: (18) r1 = map[id:2]
6: (85) call bpf_unspec#0
7: (15) if r0 == 0x0 goto pc+2
[...]
Example, BPF calls via interpreter:
# bpftool prog dump xlated id 1
0: (85) call pc+2#__bpf_prog_run_args32
1: (b7) r0 = 1
2: (95) exit
3: (b7) r0 = 2
4: (95) exit
Example, BPF calls via JIT:
# sysctl net.core.bpf_jit_enable=1
net.core.bpf_jit_enable = 1
# sysctl net.core.bpf_jit_kallsyms=1
net.core.bpf_jit_kallsyms = 1
# bpftool prog dump xlated id 1
0: (85) call pc+2#bpf_prog_3b185187f1855c4c_F
1: (b7) r0 = 1
2: (95) exit
3: (b7) r0 = 2
4: (95) exit
And finally, an example for tail calls that is now working
as well wrt correlation:
# bpftool prog dump xlated id 2
[...]
10: (b7) r2 = 8
11: (85) call bpf_trace_printk#-41312
12: (bf) r1 = r6
13: (18) r2 = map[id:1]
15: (b7) r3 = 0
16: (85) call bpf_tail_call#12
17: (b7) r1 = 42
18: (6b) *(u16 *)(r6 +46) = r1
19: (b7) r0 = 0
20: (95) exit
# bpftool map show id 1
1: prog_array flags 0x0
key 4B value 4B max_entries 1 memlock 4096B
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2017-12-20 20:42:57 +08:00
|
|
|
}
|
2018-03-02 10:01:16 +08:00
|
|
|
|
2019-03-12 13:30:39 +08:00
|
|
|
if (info->nr_jited_func_lens && info->jited_func_lens) {
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
struct kernel_sym *sym = NULL;
|
2018-11-20 07:29:21 +08:00
|
|
|
struct bpf_func_info *record;
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
char sym_name[SYM_MAX_NAME];
|
|
|
|
unsigned char *img = buf;
|
|
|
|
__u64 *ksyms = NULL;
|
|
|
|
__u32 *lens;
|
|
|
|
__u32 i;
|
2019-03-12 13:30:39 +08:00
|
|
|
if (info->nr_jited_ksyms) {
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
kernel_syms_load(&dd);
|
2019-03-12 13:30:39 +08:00
|
|
|
ksyms = (__u64 *) info->jited_ksyms;
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_start_array(json_wtr);
|
|
|
|
|
2019-03-12 13:30:39 +08:00
|
|
|
lens = (__u32 *) info->jited_func_lens;
|
|
|
|
for (i = 0; i < info->nr_jited_func_lens; i++) {
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
if (ksyms) {
|
|
|
|
sym = kernel_syms_search(&dd, ksyms[i]);
|
|
|
|
if (sym)
|
|
|
|
sprintf(sym_name, "%s", sym->name);
|
|
|
|
else
|
|
|
|
sprintf(sym_name, "0x%016llx", ksyms[i]);
|
|
|
|
} else {
|
|
|
|
strcpy(sym_name, "unknown");
|
|
|
|
}
|
|
|
|
|
2018-11-20 07:29:21 +08:00
|
|
|
if (func_info) {
|
2019-03-12 13:30:39 +08:00
|
|
|
record = func_info + i * info->func_info_rec_size;
|
2018-11-20 07:29:21 +08:00
|
|
|
btf_dumper_type_only(btf, record->type_id,
|
|
|
|
func_sig,
|
|
|
|
sizeof(func_sig));
|
|
|
|
}
|
|
|
|
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
if (json_output) {
|
|
|
|
jsonw_start_object(json_wtr);
|
2018-11-20 07:29:21 +08:00
|
|
|
if (func_info && func_sig[0] != '\0') {
|
|
|
|
jsonw_name(json_wtr, "proto");
|
|
|
|
jsonw_string(json_wtr, func_sig);
|
|
|
|
}
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
jsonw_name(json_wtr, "name");
|
|
|
|
jsonw_string(json_wtr, sym_name);
|
|
|
|
jsonw_name(json_wtr, "insns");
|
|
|
|
} else {
|
2018-11-20 07:29:21 +08:00
|
|
|
if (func_info && func_sig[0] != '\0')
|
|
|
|
printf("%s:\n", func_sig);
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
printf("%s:\n", sym_name);
|
|
|
|
}
|
|
|
|
|
bpf: libbpf: bpftool: Print bpf_line_info during prog dump
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>
2018-12-08 08:42:32 +08:00
|
|
|
disasm_print_insn(img, lens[i], opcodes,
|
|
|
|
name, disasm_opt, btf,
|
|
|
|
prog_linfo, ksyms[i], i,
|
|
|
|
linum);
|
|
|
|
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
img += lens[i];
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_end_object(json_wtr);
|
|
|
|
else
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_end_array(json_wtr);
|
|
|
|
} else {
|
2019-03-12 13:30:39 +08:00
|
|
|
disasm_print_insn(buf, member_len, opcodes, name,
|
bpf: libbpf: bpftool: Print bpf_line_info during prog dump
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>
2018-12-08 08:42:32 +08:00
|
|
|
disasm_opt, btf, NULL, 0, 0, false);
|
tools: bpftool: add delimiters to multi-function JITed dumps
This splits up the contiguous JITed dump obtained via the bpf
system call into more relatable chunks for each function in
the program. If the kernel symbols corresponding to these are
known, they are printed in the header for each JIT image dump
otherwise the masked start address is printed.
Before applying this patch:
# bpftool prog dump jited id 1
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
72: push %rbp
73: mov %rsp,%rbp
...
dd: leaveq
de: retq
# bpftool -p prog dump jited id 1
[{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
},{
"pc": "0x72",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0xde",
"operation": "retq",
"operands": [null
]
}
]
After applying this patch:
# echo 0 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
0xffffffffc02c7000:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
0xffffffffc02cf000:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "0xffffffffc02c7000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "0xffffffffc02cf000",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
# echo 1 > /proc/sys/net/core/bpf_jit_kallsyms
# bpftool prog dump jited id 1
bpf_prog_b811aab41a39ad3d_foo:
0: push %rbp
1: mov %rsp,%rbp
...
70: leaveq
71: retq
bpf_prog_cf418ac8b67bebd9_F:
0: push %rbp
1: mov %rsp,%rbp
...
6b: leaveq
6c: retq
# bpftool -p prog dump jited id 1
[{
"name": "bpf_prog_b811aab41a39ad3d_foo",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x71",
"operation": "retq",
"operands": [null
]
}
]
},{
"name": "bpf_prog_cf418ac8b67bebd9_F",
"insns": [{
"pc": "0x0",
"operation": "push",
"operands": ["%rbp"
]
},{
...
},{
"pc": "0x6c",
"operation": "retq",
"operands": [null
]
}
]
}
]
Signed-off-by: Sandipan Das <sandipan@linux.vnet.ibm.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-24 14:56:54 +08:00
|
|
|
}
|
2018-03-02 10:01:22 +08:00
|
|
|
} else if (visual) {
|
|
|
|
if (json_output)
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
else
|
2019-03-12 13:30:39 +08:00
|
|
|
dump_xlated_cfg(buf, member_len);
|
2018-03-02 10:01:16 +08:00
|
|
|
} else {
|
|
|
|
kernel_syms_load(&dd);
|
2019-03-12 13:30:39 +08:00
|
|
|
dd.nr_jited_ksyms = info->nr_jited_ksyms;
|
|
|
|
dd.jited_ksyms = (__u64 *) info->jited_ksyms;
|
2018-11-20 07:29:21 +08:00
|
|
|
dd.btf = btf;
|
|
|
|
dd.func_info = func_info;
|
2019-03-12 13:30:39 +08:00
|
|
|
dd.finfo_rec_size = info->func_info_rec_size;
|
bpf: libbpf: bpftool: Print bpf_line_info during prog dump
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>
2018-12-08 08:42:32 +08:00
|
|
|
dd.prog_linfo = prog_linfo;
|
2018-05-24 14:56:50 +08:00
|
|
|
|
2018-03-02 10:01:16 +08:00
|
|
|
if (json_output)
|
2019-03-12 13:30:39 +08:00
|
|
|
dump_xlated_json(&dd, buf, member_len, opcodes,
|
bpf: libbpf: bpftool: Print bpf_line_info during prog dump
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>
2018-12-08 08:42:32 +08:00
|
|
|
linum);
|
2018-03-02 10:01:16 +08:00
|
|
|
else
|
2019-03-12 13:30:39 +08:00
|
|
|
dump_xlated_plain(&dd, buf, member_len, opcodes,
|
bpf: libbpf: bpftool: Print bpf_line_info during prog dump
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>
2018-12-08 08:42:32 +08:00
|
|
|
linum);
|
2018-03-02 10:01:16 +08:00
|
|
|
kernel_syms_destroy(&dd);
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-12-14 03:10:04 +08:00
|
|
|
}
|
2017-10-05 11:10:04 +08:00
|
|
|
|
2019-12-14 03:10:04 +08:00
|
|
|
static int do_dump(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct bpf_prog_info_linear *info_linear;
|
|
|
|
char *filepath = NULL;
|
|
|
|
bool opcodes = false;
|
|
|
|
bool visual = false;
|
|
|
|
enum dump_mode mode;
|
|
|
|
bool linum = false;
|
|
|
|
int *fds = NULL;
|
|
|
|
int nb_fds, i = 0;
|
|
|
|
int err = -1;
|
|
|
|
__u64 arrays;
|
|
|
|
|
|
|
|
if (is_prefix(*argv, "jited")) {
|
|
|
|
if (disasm_init())
|
|
|
|
return -1;
|
|
|
|
mode = DUMP_JITED;
|
|
|
|
} else if (is_prefix(*argv, "xlated")) {
|
|
|
|
mode = DUMP_XLATED;
|
|
|
|
} else {
|
|
|
|
p_err("expected 'xlated' or 'jited', got: %s", *argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARG();
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
fds = malloc(sizeof(int));
|
|
|
|
if (!fds) {
|
|
|
|
p_err("mem alloc failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
nb_fds = prog_parse_fds(&argc, &argv, &fds);
|
|
|
|
if (nb_fds < 1)
|
|
|
|
goto exit_free;
|
|
|
|
|
|
|
|
if (is_prefix(*argv, "file")) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (!argc) {
|
|
|
|
p_err("expected file path");
|
|
|
|
goto exit_close;
|
|
|
|
}
|
|
|
|
if (nb_fds > 1) {
|
|
|
|
p_err("several programs matched");
|
|
|
|
goto exit_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
filepath = *argv;
|
|
|
|
NEXT_ARG();
|
|
|
|
} else if (is_prefix(*argv, "opcodes")) {
|
|
|
|
opcodes = true;
|
|
|
|
NEXT_ARG();
|
|
|
|
} else if (is_prefix(*argv, "visual")) {
|
|
|
|
if (nb_fds > 1) {
|
|
|
|
p_err("several programs matched");
|
|
|
|
goto exit_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
visual = true;
|
|
|
|
NEXT_ARG();
|
|
|
|
} else if (is_prefix(*argv, "linum")) {
|
|
|
|
linum = true;
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc) {
|
|
|
|
usage();
|
|
|
|
goto exit_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == DUMP_JITED)
|
|
|
|
arrays = 1UL << BPF_PROG_INFO_JITED_INSNS;
|
|
|
|
else
|
|
|
|
arrays = 1UL << BPF_PROG_INFO_XLATED_INSNS;
|
|
|
|
|
|
|
|
arrays |= 1UL << BPF_PROG_INFO_JITED_KSYMS;
|
|
|
|
arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
|
|
|
|
arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
|
|
|
|
arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
|
|
|
|
arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
|
|
|
|
|
|
|
|
if (json_output && nb_fds > 1)
|
|
|
|
jsonw_start_array(json_wtr); /* root array */
|
|
|
|
for (i = 0; i < nb_fds; i++) {
|
|
|
|
info_linear = bpf_program__get_prog_info_linear(fds[i], arrays);
|
|
|
|
if (IS_ERR_OR_NULL(info_linear)) {
|
|
|
|
p_err("can't get prog info: %s", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_output && nb_fds > 1) {
|
|
|
|
jsonw_start_object(json_wtr); /* prog object */
|
|
|
|
print_prog_header_json(&info_linear->info);
|
|
|
|
jsonw_name(json_wtr, "insns");
|
|
|
|
} else if (nb_fds > 1) {
|
|
|
|
print_prog_header_plain(&info_linear->info);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = prog_dump(&info_linear->info, mode, filepath, opcodes,
|
|
|
|
visual, linum);
|
|
|
|
|
|
|
|
if (json_output && nb_fds > 1)
|
|
|
|
jsonw_end_object(json_wtr); /* prog object */
|
|
|
|
else if (i != nb_fds - 1 && nb_fds > 1)
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
free(info_linear);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
close(fds[i]);
|
|
|
|
}
|
|
|
|
if (json_output && nb_fds > 1)
|
|
|
|
jsonw_end_array(json_wtr); /* root array */
|
|
|
|
|
|
|
|
exit_close:
|
|
|
|
for (; i < nb_fds; i++)
|
|
|
|
close(fds[i]);
|
|
|
|
exit_free:
|
|
|
|
free(fds);
|
|
|
|
return err;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int do_pin(int argc, char **argv)
|
|
|
|
{
|
2017-10-24 00:24:14 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
|
|
|
|
if (!err && json_output)
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
return err;
|
2017-10-05 11:10:04 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 05:43:07 +08:00
|
|
|
struct map_replace {
|
|
|
|
int idx;
|
|
|
|
int fd;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
2018-12-14 21:56:01 +08:00
|
|
|
static int map_replace_compar(const void *p1, const void *p2)
|
2018-07-11 05:43:07 +08:00
|
|
|
{
|
|
|
|
const struct map_replace *a = p1, *b = p2;
|
|
|
|
|
|
|
|
return a->idx - b->idx;
|
|
|
|
}
|
|
|
|
|
2018-11-10 00:21:46 +08:00
|
|
|
static int parse_attach_detach_args(int argc, char **argv, int *progfd,
|
|
|
|
enum bpf_attach_type *attach_type,
|
|
|
|
int *mapfd)
|
2018-10-16 02:19:50 +08:00
|
|
|
{
|
2018-11-10 00:21:46 +08:00
|
|
|
if (!REQ_ARGS(3))
|
2018-10-16 02:19:50 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-11-10 00:21:46 +08:00
|
|
|
*progfd = prog_parse_fd(&argc, &argv);
|
|
|
|
if (*progfd < 0)
|
|
|
|
return *progfd;
|
2018-10-16 02:19:50 +08:00
|
|
|
|
2018-11-10 00:21:46 +08:00
|
|
|
*attach_type = parse_attach_type(*argv);
|
|
|
|
if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
|
|
|
|
p_err("invalid attach/detach type");
|
2018-10-16 02:19:50 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-11-10 00:21:46 +08:00
|
|
|
|
|
|
|
if (*attach_type == BPF_FLOW_DISSECTOR) {
|
|
|
|
*mapfd = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-16 02:19:50 +08:00
|
|
|
NEXT_ARG();
|
2018-11-10 00:21:46 +08:00
|
|
|
if (!REQ_ARGS(2))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*mapfd = map_parse_fd(&argc, &argv);
|
|
|
|
if (*mapfd < 0)
|
|
|
|
return *mapfd;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-16 02:19:50 +08:00
|
|
|
|
2018-11-10 00:21:46 +08:00
|
|
|
static int do_attach(int argc, char **argv)
|
|
|
|
{
|
|
|
|
enum bpf_attach_type attach_type;
|
|
|
|
int err, progfd;
|
|
|
|
int mapfd;
|
|
|
|
|
|
|
|
err = parse_attach_detach_args(argc, argv,
|
|
|
|
&progfd, &attach_type, &mapfd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2018-10-16 02:19:50 +08:00
|
|
|
|
|
|
|
err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
|
|
|
|
if (err) {
|
|
|
|
p_err("failed prog attach to map");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_detach(int argc, char **argv)
|
|
|
|
{
|
|
|
|
enum bpf_attach_type attach_type;
|
2018-11-10 00:21:46 +08:00
|
|
|
int err, progfd;
|
|
|
|
int mapfd;
|
2018-10-16 02:19:50 +08:00
|
|
|
|
2018-11-10 00:21:46 +08:00
|
|
|
err = parse_attach_detach_args(argc, argv,
|
|
|
|
&progfd, &attach_type, &mapfd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2018-10-16 02:19:50 +08:00
|
|
|
|
|
|
|
err = bpf_prog_detach2(progfd, mapfd, attach_type);
|
|
|
|
if (err) {
|
|
|
|
p_err("failed prog detach from map");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-10 00:21:44 +08:00
|
|
|
|
2019-07-06 01:54:33 +08:00
|
|
|
static int check_single_stdin(char *file_data_in, char *file_ctx_in)
|
|
|
|
{
|
|
|
|
if (file_data_in && file_ctx_in &&
|
|
|
|
!strcmp(file_data_in, "-") && !strcmp(file_ctx_in, "-")) {
|
|
|
|
p_err("cannot use standard input for both data_in and ctx_in");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
|
|
|
|
{
|
|
|
|
size_t block_size = 256;
|
|
|
|
size_t buf_size = block_size;
|
|
|
|
size_t nb_read = 0;
|
|
|
|
void *tmp;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (!fname) {
|
|
|
|
*data_ptr = NULL;
|
|
|
|
*size = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(fname, "-"))
|
|
|
|
f = stdin;
|
|
|
|
else
|
|
|
|
f = fopen(fname, "r");
|
|
|
|
if (!f) {
|
|
|
|
p_err("failed to open %s: %s", fname, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data_ptr = malloc(block_size);
|
|
|
|
if (!*data_ptr) {
|
|
|
|
p_err("failed to allocate memory for data_in/ctx_in: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto err_fclose;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((nb_read += fread(*data_ptr + nb_read, 1, block_size, f))) {
|
|
|
|
if (feof(f))
|
|
|
|
break;
|
|
|
|
if (ferror(f)) {
|
|
|
|
p_err("failed to read data_in/ctx_in from %s: %s",
|
|
|
|
fname, strerror(errno));
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
if (nb_read > buf_size - block_size) {
|
|
|
|
if (buf_size == UINT32_MAX) {
|
|
|
|
p_err("data_in/ctx_in is too long (max: %d)",
|
|
|
|
UINT32_MAX);
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
/* No space for fread()-ing next chunk; realloc() */
|
|
|
|
buf_size *= 2;
|
|
|
|
tmp = realloc(*data_ptr, buf_size);
|
|
|
|
if (!tmp) {
|
|
|
|
p_err("failed to reallocate data_in/ctx_in: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
*data_ptr = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (f != stdin)
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
*size = nb_read;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_free:
|
|
|
|
free(*data_ptr);
|
|
|
|
*data_ptr = NULL;
|
|
|
|
err_fclose:
|
|
|
|
if (f != stdin)
|
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hex_print(void *data, unsigned int size, FILE *f)
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i += 16) {
|
|
|
|
/* Row offset */
|
|
|
|
fprintf(f, "%07zx\t", i);
|
|
|
|
|
|
|
|
/* Hexadecimal values */
|
|
|
|
for (j = i; j < i + 16 && j < size; j++)
|
|
|
|
fprintf(f, "%02x%s", *(uint8_t *)(data + j),
|
|
|
|
j % 2 ? " " : "");
|
|
|
|
for (; j < i + 16; j++)
|
|
|
|
fprintf(f, " %s", j % 2 ? " " : "");
|
|
|
|
|
|
|
|
/* ASCII values (if relevant), '.' otherwise */
|
|
|
|
fprintf(f, "| ");
|
|
|
|
for (j = i; j < i + 16 && j < size; j++) {
|
|
|
|
c = *(char *)(data + j);
|
|
|
|
if (c < ' ' || c > '~')
|
|
|
|
c = '.';
|
|
|
|
fprintf(f, "%c%s", c, j == i + 7 ? " " : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
print_run_output(void *data, unsigned int size, const char *fname,
|
|
|
|
const char *json_key)
|
|
|
|
{
|
|
|
|
size_t nb_written;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (!fname)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!strcmp(fname, "-")) {
|
|
|
|
f = stdout;
|
|
|
|
if (json_output) {
|
|
|
|
jsonw_name(json_wtr, json_key);
|
|
|
|
print_data_json(data, size);
|
|
|
|
} else {
|
|
|
|
hex_print(data, size, f);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = fopen(fname, "w");
|
|
|
|
if (!f) {
|
|
|
|
p_err("failed to open %s: %s", fname, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_written = fwrite(data, 1, size, f);
|
|
|
|
fclose(f);
|
|
|
|
if (nb_written != size) {
|
|
|
|
p_err("failed to write output data/ctx: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int alloc_run_data(void **data_ptr, unsigned int size_out)
|
|
|
|
{
|
|
|
|
*data_ptr = calloc(size_out, 1);
|
|
|
|
if (!*data_ptr) {
|
|
|
|
p_err("failed to allocate memory for output data/ctx: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_run(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *data_fname_in = NULL, *data_fname_out = NULL;
|
|
|
|
char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
|
|
|
|
struct bpf_prog_test_run_attr test_attr = {0};
|
|
|
|
const unsigned int default_size = SZ_32K;
|
|
|
|
void *data_in = NULL, *data_out = NULL;
|
|
|
|
void *ctx_in = NULL, *ctx_out = NULL;
|
|
|
|
unsigned int repeat = 1;
|
|
|
|
int fd, err;
|
|
|
|
|
|
|
|
if (!REQ_ARGS(4))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fd = prog_parse_fd(&argc, &argv);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (argc) {
|
|
|
|
if (detect_common_prefix(*argv, "data_in", "data_out",
|
|
|
|
"data_size_out", NULL))
|
|
|
|
return -1;
|
|
|
|
if (detect_common_prefix(*argv, "ctx_in", "ctx_out",
|
|
|
|
"ctx_size_out", NULL))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (is_prefix(*argv, "data_in")) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (!REQ_ARGS(1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
data_fname_in = GET_ARG();
|
|
|
|
if (check_single_stdin(data_fname_in, ctx_fname_in))
|
|
|
|
return -1;
|
|
|
|
} else if (is_prefix(*argv, "data_out")) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (!REQ_ARGS(1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
data_fname_out = GET_ARG();
|
|
|
|
} else if (is_prefix(*argv, "data_size_out")) {
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
if (!REQ_ARGS(1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
test_attr.data_size_out = strtoul(*argv, &endptr, 0);
|
|
|
|
if (*endptr) {
|
|
|
|
p_err("can't parse %s as output data size",
|
|
|
|
*argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARG();
|
|
|
|
} else if (is_prefix(*argv, "ctx_in")) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (!REQ_ARGS(1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ctx_fname_in = GET_ARG();
|
|
|
|
if (check_single_stdin(data_fname_in, ctx_fname_in))
|
|
|
|
return -1;
|
|
|
|
} else if (is_prefix(*argv, "ctx_out")) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (!REQ_ARGS(1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ctx_fname_out = GET_ARG();
|
|
|
|
} else if (is_prefix(*argv, "ctx_size_out")) {
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
if (!REQ_ARGS(1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
test_attr.ctx_size_out = strtoul(*argv, &endptr, 0);
|
|
|
|
if (*endptr) {
|
|
|
|
p_err("can't parse %s as output context size",
|
|
|
|
*argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARG();
|
|
|
|
} else if (is_prefix(*argv, "repeat")) {
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
if (!REQ_ARGS(1))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
repeat = strtoul(*argv, &endptr, 0);
|
|
|
|
if (*endptr) {
|
|
|
|
p_err("can't parse %s as repeat number",
|
|
|
|
*argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARG();
|
|
|
|
} else {
|
|
|
|
p_err("expected no more arguments, 'data_in', 'data_out', 'data_size_out', 'ctx_in', 'ctx_out', 'ctx_size_out' or 'repeat', got: '%s'?",
|
|
|
|
*argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = get_run_data(data_fname_in, &data_in, &test_attr.data_size_in);
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (data_in) {
|
|
|
|
if (!test_attr.data_size_out)
|
|
|
|
test_attr.data_size_out = default_size;
|
|
|
|
err = alloc_run_data(&data_out, test_attr.data_size_out);
|
|
|
|
if (err)
|
|
|
|
goto free_data_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = get_run_data(ctx_fname_in, &ctx_in, &test_attr.ctx_size_in);
|
|
|
|
if (err)
|
|
|
|
goto free_data_out;
|
|
|
|
|
|
|
|
if (ctx_in) {
|
|
|
|
if (!test_attr.ctx_size_out)
|
|
|
|
test_attr.ctx_size_out = default_size;
|
|
|
|
err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
|
|
|
|
if (err)
|
|
|
|
goto free_ctx_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
test_attr.prog_fd = fd;
|
|
|
|
test_attr.repeat = repeat;
|
|
|
|
test_attr.data_in = data_in;
|
|
|
|
test_attr.data_out = data_out;
|
|
|
|
test_attr.ctx_in = ctx_in;
|
|
|
|
test_attr.ctx_out = ctx_out;
|
|
|
|
|
|
|
|
err = bpf_prog_test_run_xattr(&test_attr);
|
|
|
|
if (err) {
|
|
|
|
p_err("failed to run program: %s", strerror(errno));
|
|
|
|
goto free_ctx_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_start_object(json_wtr); /* root */
|
|
|
|
|
|
|
|
/* Do not exit on errors occurring when printing output data/context,
|
|
|
|
* we still want to print return value and duration for program run.
|
|
|
|
*/
|
|
|
|
if (test_attr.data_size_out)
|
|
|
|
err += print_run_output(test_attr.data_out,
|
|
|
|
test_attr.data_size_out,
|
|
|
|
data_fname_out, "data_out");
|
|
|
|
if (test_attr.ctx_size_out)
|
|
|
|
err += print_run_output(test_attr.ctx_out,
|
|
|
|
test_attr.ctx_size_out,
|
|
|
|
ctx_fname_out, "ctx_out");
|
|
|
|
|
|
|
|
if (json_output) {
|
|
|
|
jsonw_uint_field(json_wtr, "retval", test_attr.retval);
|
|
|
|
jsonw_uint_field(json_wtr, "duration", test_attr.duration);
|
|
|
|
jsonw_end_object(json_wtr); /* root */
|
|
|
|
} else {
|
|
|
|
fprintf(stdout, "Return value: %u, duration%s: %uns\n",
|
|
|
|
test_attr.retval,
|
|
|
|
repeat > 1 ? " (average)" : "", test_attr.duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
free_ctx_out:
|
|
|
|
free(ctx_out);
|
|
|
|
free_ctx_in:
|
|
|
|
free(ctx_in);
|
|
|
|
free_data_out:
|
|
|
|
free(data_out);
|
|
|
|
free_data_in:
|
|
|
|
free(data_in);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-11-10 00:21:44 +08:00
|
|
|
static int load_with_options(int argc, char **argv, bool first_prog_only)
|
2017-12-13 23:18:53 +08:00
|
|
|
{
|
2019-10-08 06:56:04 +08:00
|
|
|
enum bpf_prog_type common_prog_type = BPF_PROG_TYPE_UNSPEC;
|
2019-10-23 01:21:00 +08:00
|
|
|
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts,
|
|
|
|
.relaxed_maps = relaxed_maps,
|
|
|
|
);
|
|
|
|
struct bpf_object_load_attr load_attr = { 0 };
|
2019-05-24 18:36:48 +08:00
|
|
|
enum bpf_attach_type expected_attach_type;
|
2018-07-11 05:43:07 +08:00
|
|
|
struct map_replace *map_replace = NULL;
|
2018-11-10 00:21:44 +08:00
|
|
|
struct bpf_program *prog = NULL, *pos;
|
2018-07-11 05:43:07 +08:00
|
|
|
unsigned int old_map_fds = 0;
|
2018-11-10 00:21:45 +08:00
|
|
|
const char *pinmaps = NULL;
|
2017-12-13 23:18:53 +08:00
|
|
|
struct bpf_object *obj;
|
2018-07-11 05:43:03 +08:00
|
|
|
struct bpf_map *map;
|
|
|
|
const char *pinfile;
|
2018-07-11 05:43:07 +08:00
|
|
|
unsigned int i, j;
|
2018-07-11 05:43:03 +08:00
|
|
|
__u32 ifindex = 0;
|
2019-10-08 06:56:04 +08:00
|
|
|
const char *file;
|
2018-07-11 05:43:07 +08:00
|
|
|
int idx, err;
|
2017-12-13 23:18:53 +08:00
|
|
|
|
2019-10-08 06:56:04 +08:00
|
|
|
|
2018-07-11 05:42:57 +08:00
|
|
|
if (!REQ_ARGS(2))
|
|
|
|
return -1;
|
2019-10-08 06:56:04 +08:00
|
|
|
file = GET_ARG();
|
2018-07-11 05:42:57 +08:00
|
|
|
pinfile = GET_ARG();
|
2017-12-13 23:18:53 +08:00
|
|
|
|
2018-07-11 05:42:58 +08:00
|
|
|
while (argc) {
|
2018-07-11 05:43:00 +08:00
|
|
|
if (is_prefix(*argv, "type")) {
|
|
|
|
char *type;
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
|
2019-10-08 06:56:04 +08:00
|
|
|
if (common_prog_type != BPF_PROG_TYPE_UNSPEC) {
|
2018-07-11 05:43:00 +08:00
|
|
|
p_err("program type already specified");
|
2018-07-11 05:43:07 +08:00
|
|
|
goto err_free_reuse_maps;
|
2018-07-11 05:43:00 +08:00
|
|
|
}
|
|
|
|
if (!REQ_ARGS(1))
|
2018-07-11 05:43:07 +08:00
|
|
|
goto err_free_reuse_maps;
|
2018-07-11 05:43:00 +08:00
|
|
|
|
|
|
|
/* Put a '/' at the end of type to appease libbpf */
|
|
|
|
type = malloc(strlen(*argv) + 2);
|
|
|
|
if (!type) {
|
|
|
|
p_err("mem alloc failed");
|
2018-07-11 05:43:07 +08:00
|
|
|
goto err_free_reuse_maps;
|
2018-07-11 05:43:00 +08:00
|
|
|
}
|
|
|
|
*type = 0;
|
|
|
|
strcat(type, *argv);
|
|
|
|
strcat(type, "/");
|
|
|
|
|
2019-10-08 06:56:04 +08:00
|
|
|
err = libbpf_prog_type_by_name(type, &common_prog_type,
|
2018-07-11 05:43:03 +08:00
|
|
|
&expected_attach_type);
|
2018-07-11 05:43:00 +08:00
|
|
|
free(type);
|
2019-01-21 21:06:38 +08:00
|
|
|
if (err < 0)
|
2018-07-11 05:43:07 +08:00
|
|
|
goto err_free_reuse_maps;
|
2019-01-21 21:06:38 +08:00
|
|
|
|
2018-07-11 05:43:00 +08:00
|
|
|
NEXT_ARG();
|
2018-07-11 05:43:07 +08:00
|
|
|
} else if (is_prefix(*argv, "map")) {
|
2018-11-22 05:53:17 +08:00
|
|
|
void *new_map_replace;
|
2018-07-11 05:43:07 +08:00
|
|
|
char *endptr, *name;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
|
|
|
|
if (!REQ_ARGS(4))
|
|
|
|
goto err_free_reuse_maps;
|
|
|
|
|
|
|
|
if (is_prefix(*argv, "idx")) {
|
|
|
|
NEXT_ARG();
|
|
|
|
|
|
|
|
idx = strtoul(*argv, &endptr, 0);
|
|
|
|
if (*endptr) {
|
|
|
|
p_err("can't parse %s as IDX", *argv);
|
|
|
|
goto err_free_reuse_maps;
|
|
|
|
}
|
|
|
|
name = NULL;
|
|
|
|
} else if (is_prefix(*argv, "name")) {
|
|
|
|
NEXT_ARG();
|
|
|
|
|
|
|
|
name = *argv;
|
|
|
|
idx = -1;
|
|
|
|
} else {
|
|
|
|
p_err("expected 'idx' or 'name', got: '%s'?",
|
|
|
|
*argv);
|
|
|
|
goto err_free_reuse_maps;
|
|
|
|
}
|
|
|
|
NEXT_ARG();
|
|
|
|
|
|
|
|
fd = map_parse_fd(&argc, &argv);
|
|
|
|
if (fd < 0)
|
|
|
|
goto err_free_reuse_maps;
|
|
|
|
|
2018-11-22 05:53:17 +08:00
|
|
|
new_map_replace = reallocarray(map_replace,
|
|
|
|
old_map_fds + 1,
|
|
|
|
sizeof(*map_replace));
|
|
|
|
if (!new_map_replace) {
|
2018-07-11 05:43:07 +08:00
|
|
|
p_err("mem alloc failed");
|
|
|
|
goto err_free_reuse_maps;
|
|
|
|
}
|
2018-11-22 05:53:17 +08:00
|
|
|
map_replace = new_map_replace;
|
|
|
|
|
2018-07-11 05:43:07 +08:00
|
|
|
map_replace[old_map_fds].idx = idx;
|
|
|
|
map_replace[old_map_fds].name = name;
|
|
|
|
map_replace[old_map_fds].fd = fd;
|
|
|
|
old_map_fds++;
|
2018-07-11 05:43:00 +08:00
|
|
|
} else if (is_prefix(*argv, "dev")) {
|
2018-07-11 05:42:58 +08:00
|
|
|
NEXT_ARG();
|
|
|
|
|
2018-07-11 05:43:03 +08:00
|
|
|
if (ifindex) {
|
2018-07-11 05:42:58 +08:00
|
|
|
p_err("offload device already specified");
|
2018-07-11 05:43:07 +08:00
|
|
|
goto err_free_reuse_maps;
|
2018-07-11 05:42:58 +08:00
|
|
|
}
|
|
|
|
if (!REQ_ARGS(1))
|
2018-07-11 05:43:07 +08:00
|
|
|
goto err_free_reuse_maps;
|
2018-07-11 05:42:58 +08:00
|
|
|
|
2018-07-11 05:43:03 +08:00
|
|
|
ifindex = if_nametoindex(*argv);
|
|
|
|
if (!ifindex) {
|
2018-07-11 05:42:58 +08:00
|
|
|
p_err("unrecognized netdevice '%s': %s",
|
|
|
|
*argv, strerror(errno));
|
2018-07-11 05:43:07 +08:00
|
|
|
goto err_free_reuse_maps;
|
2018-07-11 05:42:58 +08:00
|
|
|
}
|
|
|
|
NEXT_ARG();
|
2018-11-10 00:21:45 +08:00
|
|
|
} else if (is_prefix(*argv, "pinmaps")) {
|
|
|
|
NEXT_ARG();
|
|
|
|
|
|
|
|
if (!REQ_ARGS(1))
|
|
|
|
goto err_free_reuse_maps;
|
|
|
|
|
|
|
|
pinmaps = GET_ARG();
|
2018-07-11 05:42:58 +08:00
|
|
|
} else {
|
2018-07-11 05:43:07 +08:00
|
|
|
p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
|
2018-07-11 05:42:58 +08:00
|
|
|
*argv);
|
2018-07-11 05:43:07 +08:00
|
|
|
goto err_free_reuse_maps;
|
2018-07-11 05:42:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 01:17:31 +08:00
|
|
|
set_max_rlimit();
|
|
|
|
|
2019-10-08 06:56:04 +08:00
|
|
|
obj = bpf_object__open_file(file, &open_opts);
|
2018-07-11 05:43:03 +08:00
|
|
|
if (IS_ERR_OR_NULL(obj)) {
|
|
|
|
p_err("failed to open object file");
|
2018-07-11 05:43:07 +08:00
|
|
|
goto err_free_reuse_maps;
|
2017-12-13 23:18:53 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 00:21:44 +08:00
|
|
|
bpf_object__for_each_program(pos, obj) {
|
2019-10-08 06:56:04 +08:00
|
|
|
enum bpf_prog_type prog_type = common_prog_type;
|
2018-07-11 05:43:03 +08:00
|
|
|
|
2019-10-08 06:56:04 +08:00
|
|
|
if (prog_type == BPF_PROG_TYPE_UNSPEC) {
|
2018-11-10 00:21:44 +08:00
|
|
|
const char *sec_name = bpf_program__title(pos, false);
|
2018-07-11 05:43:03 +08:00
|
|
|
|
2018-11-10 00:21:44 +08:00
|
|
|
err = libbpf_prog_type_by_name(sec_name, &prog_type,
|
|
|
|
&expected_attach_type);
|
2019-01-21 21:06:38 +08:00
|
|
|
if (err < 0)
|
2018-11-10 00:21:44 +08:00
|
|
|
goto err_close_obj;
|
2018-07-11 05:43:03 +08:00
|
|
|
}
|
2018-11-10 00:21:44 +08:00
|
|
|
|
|
|
|
bpf_program__set_ifindex(pos, ifindex);
|
|
|
|
bpf_program__set_type(pos, prog_type);
|
|
|
|
bpf_program__set_expected_attach_type(pos, expected_attach_type);
|
2018-07-11 05:43:03 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 05:43:07 +08:00
|
|
|
qsort(map_replace, old_map_fds, sizeof(*map_replace),
|
|
|
|
map_replace_compar);
|
|
|
|
|
|
|
|
/* After the sort maps by name will be first on the list, because they
|
|
|
|
* have idx == -1. Resolve them.
|
|
|
|
*/
|
|
|
|
j = 0;
|
|
|
|
while (j < old_map_fds && map_replace[j].name) {
|
|
|
|
i = 0;
|
2019-02-28 11:04:12 +08:00
|
|
|
bpf_object__for_each_map(map, obj) {
|
2018-07-11 05:43:07 +08:00
|
|
|
if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
|
|
|
|
map_replace[j].idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (map_replace[j].idx == -1) {
|
|
|
|
p_err("unable to find map '%s'", map_replace[j].name);
|
|
|
|
goto err_close_obj;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
/* Resort if any names were resolved */
|
|
|
|
if (j)
|
|
|
|
qsort(map_replace, old_map_fds, sizeof(*map_replace),
|
|
|
|
map_replace_compar);
|
|
|
|
|
|
|
|
/* Set ifindex and name reuse */
|
|
|
|
j = 0;
|
|
|
|
idx = 0;
|
2019-02-28 11:04:12 +08:00
|
|
|
bpf_object__for_each_map(map, obj) {
|
2018-07-11 05:43:03 +08:00
|
|
|
if (!bpf_map__is_offload_neutral(map))
|
|
|
|
bpf_map__set_ifindex(map, ifindex);
|
|
|
|
|
2018-07-11 05:43:07 +08:00
|
|
|
if (j < old_map_fds && idx == map_replace[j].idx) {
|
|
|
|
err = bpf_map__reuse_fd(map, map_replace[j++].fd);
|
|
|
|
if (err) {
|
|
|
|
p_err("unable to set up map reuse: %d", err);
|
|
|
|
goto err_close_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next reuse wants to apply to the same map */
|
|
|
|
if (j < old_map_fds && map_replace[j].idx == idx) {
|
|
|
|
p_err("replacement for map idx %d specified more than once",
|
|
|
|
idx);
|
|
|
|
goto err_close_obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
if (j < old_map_fds) {
|
|
|
|
p_err("map idx '%d' not used", map_replace[j].idx);
|
|
|
|
goto err_close_obj;
|
|
|
|
}
|
|
|
|
|
2019-05-24 18:36:48 +08:00
|
|
|
load_attr.obj = obj;
|
|
|
|
if (verifier_logs)
|
|
|
|
/* log_level1 + log_level2 + stats, but not stable UAPI */
|
|
|
|
load_attr.log_level = 1 + 2 + 4;
|
|
|
|
|
|
|
|
err = bpf_object__load_xattr(&load_attr);
|
2018-07-11 05:43:03 +08:00
|
|
|
if (err) {
|
|
|
|
p_err("failed to load object file");
|
|
|
|
goto err_close_obj;
|
|
|
|
}
|
|
|
|
|
2018-11-10 00:21:44 +08:00
|
|
|
err = mount_bpffs_for_pin(pinfile);
|
|
|
|
if (err)
|
2018-06-21 02:42:46 +08:00
|
|
|
goto err_close_obj;
|
2017-12-13 23:18:53 +08:00
|
|
|
|
2018-11-10 00:21:44 +08:00
|
|
|
if (first_prog_only) {
|
|
|
|
prog = bpf_program__next(NULL, obj);
|
|
|
|
if (!prog) {
|
|
|
|
p_err("object file doesn't contain any bpf program");
|
|
|
|
goto err_close_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
|
|
|
|
if (err) {
|
|
|
|
p_err("failed to pin program %s",
|
|
|
|
bpf_program__title(prog, false));
|
|
|
|
goto err_close_obj;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = bpf_object__pin_programs(obj, pinfile);
|
|
|
|
if (err) {
|
|
|
|
p_err("failed to pin all programs");
|
|
|
|
goto err_close_obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 00:21:45 +08:00
|
|
|
if (pinmaps) {
|
|
|
|
err = bpf_object__pin_maps(obj, pinmaps);
|
|
|
|
if (err) {
|
|
|
|
p_err("failed to pin all maps");
|
|
|
|
goto err_unpin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:18:53 +08:00
|
|
|
if (json_output)
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
|
2018-06-21 02:42:46 +08:00
|
|
|
bpf_object__close(obj);
|
2018-07-11 05:43:07 +08:00
|
|
|
for (i = 0; i < old_map_fds; i++)
|
|
|
|
close(map_replace[i].fd);
|
|
|
|
free(map_replace);
|
2018-06-21 02:42:46 +08:00
|
|
|
|
2017-12-13 23:18:53 +08:00
|
|
|
return 0;
|
2018-06-21 02:42:46 +08:00
|
|
|
|
2018-11-10 00:21:45 +08:00
|
|
|
err_unpin:
|
|
|
|
if (first_prog_only)
|
|
|
|
unlink(pinfile);
|
|
|
|
else
|
|
|
|
bpf_object__unpin_programs(obj, pinfile);
|
2018-06-21 02:42:46 +08:00
|
|
|
err_close_obj:
|
|
|
|
bpf_object__close(obj);
|
2018-07-11 05:43:07 +08:00
|
|
|
err_free_reuse_maps:
|
|
|
|
for (i = 0; i < old_map_fds; i++)
|
|
|
|
close(map_replace[i].fd);
|
|
|
|
free(map_replace);
|
2018-06-21 02:42:46 +08:00
|
|
|
return -1;
|
2017-12-13 23:18:53 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 00:21:44 +08:00
|
|
|
static int do_load(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return load_with_options(argc, argv, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_loadall(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return load_with_options(argc, argv, false);
|
|
|
|
}
|
|
|
|
|
2017-10-05 11:10:04 +08:00
|
|
|
static int do_help(int argc, char **argv)
|
|
|
|
{
|
2017-10-24 00:24:14 +08:00
|
|
|
if (json_output) {
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-05 11:10:04 +08:00
|
|
|
fprintf(stderr,
|
2018-01-03 06:48:36 +08:00
|
|
|
"Usage: %s %s { show | list } [PROG]\n"
|
bpf: libbpf: bpftool: Print bpf_line_info during prog dump
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>
2018-12-08 08:42:32 +08:00
|
|
|
" %s %s dump xlated PROG [{ file FILE | opcodes | visual | linum }]\n"
|
|
|
|
" %s %s dump jited PROG [{ file FILE | opcodes | linum }]\n"
|
2017-10-05 11:10:04 +08:00
|
|
|
" %s %s pin PROG FILE\n"
|
2018-11-10 00:21:44 +08:00
|
|
|
" %s %s { load | loadall } OBJ PATH \\\n"
|
|
|
|
" [type TYPE] [dev NAME] \\\n"
|
2018-11-10 00:21:45 +08:00
|
|
|
" [map { idx IDX | name NAME } MAP]\\\n"
|
|
|
|
" [pinmaps MAP_DIR]\n"
|
2018-11-10 00:21:46 +08:00
|
|
|
" %s %s attach PROG ATTACH_TYPE [MAP]\n"
|
|
|
|
" %s %s detach PROG ATTACH_TYPE [MAP]\n"
|
2019-07-06 01:54:33 +08:00
|
|
|
" %s %s run PROG \\\n"
|
|
|
|
" data_in FILE \\\n"
|
|
|
|
" [data_out FILE [data_size_out L]] \\\n"
|
|
|
|
" [ctx_in FILE [ctx_out FILE [ctx_size_out M]]] \\\n"
|
|
|
|
" [repeat N]\n"
|
2018-12-05 18:28:24 +08:00
|
|
|
" %s %s tracelog\n"
|
2017-10-05 11:10:04 +08:00
|
|
|
" %s %s help\n"
|
|
|
|
"\n"
|
2018-07-11 05:43:07 +08:00
|
|
|
" " HELP_SPEC_MAP "\n"
|
2017-10-05 11:10:04 +08:00
|
|
|
" " HELP_SPEC_PROGRAM "\n"
|
2018-07-11 05:43:00 +08:00
|
|
|
" TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
|
|
|
|
" tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
|
|
|
|
" cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
|
|
|
|
" lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
|
2019-04-17 04:13:47 +08:00
|
|
|
" sk_reuseport | flow_dissector | cgroup/sysctl |\n"
|
2018-07-11 05:43:00 +08:00
|
|
|
" cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
|
|
|
|
" cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
|
2019-06-07 07:49:00 +08:00
|
|
|
" cgroup/sendmsg4 | cgroup/sendmsg6 | cgroup/recvmsg4 |\n"
|
2019-06-28 04:38:55 +08:00
|
|
|
" cgroup/recvmsg6 | cgroup/getsockopt |\n"
|
|
|
|
" cgroup/setsockopt }\n"
|
2019-02-19 22:13:32 +08:00
|
|
|
" ATTACH_TYPE := { msg_verdict | stream_verdict | stream_parser |\n"
|
2018-11-10 00:21:46 +08:00
|
|
|
" flow_dissector }\n"
|
2017-10-24 00:24:16 +08:00
|
|
|
" " HELP_SPEC_OPTIONS "\n"
|
2017-10-05 11:10:04 +08:00
|
|
|
"",
|
|
|
|
bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
|
2018-10-16 02:19:50 +08:00
|
|
|
bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
|
2019-07-06 01:54:33 +08:00
|
|
|
bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
|
|
|
|
bin_name, argv[-2]);
|
2017-10-05 11:10:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct cmd cmds[] = {
|
|
|
|
{ "show", do_show },
|
2018-01-03 06:48:36 +08:00
|
|
|
{ "list", do_show },
|
2017-10-20 06:46:22 +08:00
|
|
|
{ "help", do_help },
|
2017-10-05 11:10:04 +08:00
|
|
|
{ "dump", do_dump },
|
|
|
|
{ "pin", do_pin },
|
2017-12-13 23:18:53 +08:00
|
|
|
{ "load", do_load },
|
2018-11-10 00:21:44 +08:00
|
|
|
{ "loadall", do_loadall },
|
2018-10-16 02:19:50 +08:00
|
|
|
{ "attach", do_attach },
|
|
|
|
{ "detach", do_detach },
|
2018-12-05 18:28:24 +08:00
|
|
|
{ "tracelog", do_tracelog },
|
2019-07-06 01:54:33 +08:00
|
|
|
{ "run", do_run },
|
2017-10-05 11:10:04 +08:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int do_prog(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return cmd_select(cmds, argc, argv, do_help);
|
|
|
|
}
|