2015-10-14 20:41:13 +08:00
|
|
|
/*
|
|
|
|
* bpf-loader.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
|
|
|
|
* Copyright (C) 2015 Huawei Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <bpf/libbpf.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include "perf.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "bpf-loader.h"
|
perf tools: Create probe points for BPF programs
This patch introduces bpf__{un,}probe() functions to enable callers to
create kprobe points based on section names a BPF program. It parses the
section names in the program and creates corresponding 'struct
perf_probe_event' structures. The parse_perf_probe_command() function is
used to do the main parsing work. The resuling 'struct perf_probe_event'
is stored into program private data for further using.
By utilizing the new probing API, this patch creates probe points during
event parsing.
To ensure probe points be removed correctly, register an atexit hook so
even perf quit through exit() bpf__clear() is still called, so probing
points are cleared. Note that bpf_clear() should be registered before
bpf__probe() is called, so failure of bpf__probe() can still trigger
bpf__clear() to remove probe points which are already probed.
strerror style error reporting scaffold is created by this patch.
bpf__strerror_probe() is the first error reporting function in
bpf-loader.c.
Committer note:
Trying it:
To build a test eBPF object file:
I am testing using a script I built from the 'perf test -v LLVM' output:
$ cat ~/bin/hello-ebpf
export KERNEL_INC_OPTIONS="-nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.3/include -I/home/acme/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/acme/git/linux/include -Iinclude -I/home/acme/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -Iinclude/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h"
export WORKING_DIR=/lib/modules/4.2.0/build
export CLANG_SOURCE=-
export CLANG_OPTIONS=-xc
OBJ=/tmp/foo.o
rm -f $OBJ
echo '__attribute__((section("fork=do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | \
clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o /tmp/foo.o && file $OBJ
---
First asking to put a probe in a function not present in the kernel
(misses the initial _):
$ perf record --event /tmp/foo.o sleep 1
Probe point 'do_fork' not found.
event syntax error: '/tmp/foo.o'
\___ You need to check probing points in BPF file
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
$
---
Now, with "__attribute__((section("fork=_do_fork"), used)):
$ grep _do_fork /proc/kallsyms
ffffffff81099ab0 T _do_fork
$ perf record --event /tmp/foo.o sleep 1
Failed to open kprobe_events: Permission denied
event syntax error: '/tmp/foo.o'
\___ Permission denied
---
Cool, we need to provide some better hints, "kprobe_events" is too low
level, one doesn't strictly need to know the precise details of how
these things are put in place, so something that shows the command
needed to fix the permissions would be more helpful.
Lets try as root instead:
# perf record --event /tmp/foo.o sleep 1
Lowering default frequency rate to 1000.
Please consider tweaking /proc/sys/kernel/perf_event_max_sample_rate.
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.013 MB perf.data ]
# perf evlist
/tmp/foo.o
[root@felicio ~]# perf evlist -v
/tmp/foo.o: type: 1, size: 112, config: 0x9, { sample_period,
sample_freq }: 1000, sample_type: IP|TID|TIME|PERIOD, disabled: 1,
inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1, task: 1,
sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1
---
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1444826502-49291-5-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-10-14 20:41:15 +08:00
|
|
|
#include "probe-event.h"
|
|
|
|
#include "probe-finder.h" // for MAX_PROBES
|
2015-10-14 20:41:13 +08:00
|
|
|
|
|
|
|
#define DEFINE_PRINT_FN(name, level) \
|
|
|
|
static int libbpf_##name(const char *fmt, ...) \
|
|
|
|
{ \
|
|
|
|
va_list args; \
|
|
|
|
int ret; \
|
|
|
|
\
|
|
|
|
va_start(args, fmt); \
|
|
|
|
ret = veprintf(level, verbose, pr_fmt(fmt), args);\
|
|
|
|
va_end(args); \
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_PRINT_FN(warning, 0)
|
|
|
|
DEFINE_PRINT_FN(info, 0)
|
|
|
|
DEFINE_PRINT_FN(debug, 1)
|
|
|
|
|
perf tools: Create probe points for BPF programs
This patch introduces bpf__{un,}probe() functions to enable callers to
create kprobe points based on section names a BPF program. It parses the
section names in the program and creates corresponding 'struct
perf_probe_event' structures. The parse_perf_probe_command() function is
used to do the main parsing work. The resuling 'struct perf_probe_event'
is stored into program private data for further using.
By utilizing the new probing API, this patch creates probe points during
event parsing.
To ensure probe points be removed correctly, register an atexit hook so
even perf quit through exit() bpf__clear() is still called, so probing
points are cleared. Note that bpf_clear() should be registered before
bpf__probe() is called, so failure of bpf__probe() can still trigger
bpf__clear() to remove probe points which are already probed.
strerror style error reporting scaffold is created by this patch.
bpf__strerror_probe() is the first error reporting function in
bpf-loader.c.
Committer note:
Trying it:
To build a test eBPF object file:
I am testing using a script I built from the 'perf test -v LLVM' output:
$ cat ~/bin/hello-ebpf
export KERNEL_INC_OPTIONS="-nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.3/include -I/home/acme/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/acme/git/linux/include -Iinclude -I/home/acme/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -Iinclude/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h"
export WORKING_DIR=/lib/modules/4.2.0/build
export CLANG_SOURCE=-
export CLANG_OPTIONS=-xc
OBJ=/tmp/foo.o
rm -f $OBJ
echo '__attribute__((section("fork=do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | \
clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o /tmp/foo.o && file $OBJ
---
First asking to put a probe in a function not present in the kernel
(misses the initial _):
$ perf record --event /tmp/foo.o sleep 1
Probe point 'do_fork' not found.
event syntax error: '/tmp/foo.o'
\___ You need to check probing points in BPF file
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
$
---
Now, with "__attribute__((section("fork=_do_fork"), used)):
$ grep _do_fork /proc/kallsyms
ffffffff81099ab0 T _do_fork
$ perf record --event /tmp/foo.o sleep 1
Failed to open kprobe_events: Permission denied
event syntax error: '/tmp/foo.o'
\___ Permission denied
---
Cool, we need to provide some better hints, "kprobe_events" is too low
level, one doesn't strictly need to know the precise details of how
these things are put in place, so something that shows the command
needed to fix the permissions would be more helpful.
Lets try as root instead:
# perf record --event /tmp/foo.o sleep 1
Lowering default frequency rate to 1000.
Please consider tweaking /proc/sys/kernel/perf_event_max_sample_rate.
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.013 MB perf.data ]
# perf evlist
/tmp/foo.o
[root@felicio ~]# perf evlist -v
/tmp/foo.o: type: 1, size: 112, config: 0x9, { sample_period,
sample_freq }: 1000, sample_type: IP|TID|TIME|PERIOD, disabled: 1,
inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1, task: 1,
sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1
---
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1444826502-49291-5-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-10-14 20:41:15 +08:00
|
|
|
struct bpf_prog_priv {
|
|
|
|
struct perf_probe_event pev;
|
|
|
|
};
|
|
|
|
|
2015-10-14 20:41:13 +08:00
|
|
|
struct bpf_object *bpf__prepare_load(const char *filename)
|
|
|
|
{
|
|
|
|
struct bpf_object *obj;
|
|
|
|
static bool libbpf_initialized;
|
|
|
|
|
|
|
|
if (!libbpf_initialized) {
|
|
|
|
libbpf_set_print(libbpf_warning,
|
|
|
|
libbpf_info,
|
|
|
|
libbpf_debug);
|
|
|
|
libbpf_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = bpf_object__open(filename);
|
|
|
|
if (!obj) {
|
|
|
|
pr_debug("bpf: failed to load %s\n", filename);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bpf__clear(void)
|
|
|
|
{
|
|
|
|
struct bpf_object *obj, *tmp;
|
|
|
|
|
perf tools: Create probe points for BPF programs
This patch introduces bpf__{un,}probe() functions to enable callers to
create kprobe points based on section names a BPF program. It parses the
section names in the program and creates corresponding 'struct
perf_probe_event' structures. The parse_perf_probe_command() function is
used to do the main parsing work. The resuling 'struct perf_probe_event'
is stored into program private data for further using.
By utilizing the new probing API, this patch creates probe points during
event parsing.
To ensure probe points be removed correctly, register an atexit hook so
even perf quit through exit() bpf__clear() is still called, so probing
points are cleared. Note that bpf_clear() should be registered before
bpf__probe() is called, so failure of bpf__probe() can still trigger
bpf__clear() to remove probe points which are already probed.
strerror style error reporting scaffold is created by this patch.
bpf__strerror_probe() is the first error reporting function in
bpf-loader.c.
Committer note:
Trying it:
To build a test eBPF object file:
I am testing using a script I built from the 'perf test -v LLVM' output:
$ cat ~/bin/hello-ebpf
export KERNEL_INC_OPTIONS="-nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.3/include -I/home/acme/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/acme/git/linux/include -Iinclude -I/home/acme/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -Iinclude/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h"
export WORKING_DIR=/lib/modules/4.2.0/build
export CLANG_SOURCE=-
export CLANG_OPTIONS=-xc
OBJ=/tmp/foo.o
rm -f $OBJ
echo '__attribute__((section("fork=do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | \
clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o /tmp/foo.o && file $OBJ
---
First asking to put a probe in a function not present in the kernel
(misses the initial _):
$ perf record --event /tmp/foo.o sleep 1
Probe point 'do_fork' not found.
event syntax error: '/tmp/foo.o'
\___ You need to check probing points in BPF file
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
$
---
Now, with "__attribute__((section("fork=_do_fork"), used)):
$ grep _do_fork /proc/kallsyms
ffffffff81099ab0 T _do_fork
$ perf record --event /tmp/foo.o sleep 1
Failed to open kprobe_events: Permission denied
event syntax error: '/tmp/foo.o'
\___ Permission denied
---
Cool, we need to provide some better hints, "kprobe_events" is too low
level, one doesn't strictly need to know the precise details of how
these things are put in place, so something that shows the command
needed to fix the permissions would be more helpful.
Lets try as root instead:
# perf record --event /tmp/foo.o sleep 1
Lowering default frequency rate to 1000.
Please consider tweaking /proc/sys/kernel/perf_event_max_sample_rate.
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.013 MB perf.data ]
# perf evlist
/tmp/foo.o
[root@felicio ~]# perf evlist -v
/tmp/foo.o: type: 1, size: 112, config: 0x9, { sample_period,
sample_freq }: 1000, sample_type: IP|TID|TIME|PERIOD, disabled: 1,
inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1, task: 1,
sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1
---
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1444826502-49291-5-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-10-14 20:41:15 +08:00
|
|
|
bpf_object__for_each_safe(obj, tmp) {
|
|
|
|
bpf__unprobe(obj);
|
2015-10-14 20:41:13 +08:00
|
|
|
bpf_object__close(obj);
|
perf tools: Create probe points for BPF programs
This patch introduces bpf__{un,}probe() functions to enable callers to
create kprobe points based on section names a BPF program. It parses the
section names in the program and creates corresponding 'struct
perf_probe_event' structures. The parse_perf_probe_command() function is
used to do the main parsing work. The resuling 'struct perf_probe_event'
is stored into program private data for further using.
By utilizing the new probing API, this patch creates probe points during
event parsing.
To ensure probe points be removed correctly, register an atexit hook so
even perf quit through exit() bpf__clear() is still called, so probing
points are cleared. Note that bpf_clear() should be registered before
bpf__probe() is called, so failure of bpf__probe() can still trigger
bpf__clear() to remove probe points which are already probed.
strerror style error reporting scaffold is created by this patch.
bpf__strerror_probe() is the first error reporting function in
bpf-loader.c.
Committer note:
Trying it:
To build a test eBPF object file:
I am testing using a script I built from the 'perf test -v LLVM' output:
$ cat ~/bin/hello-ebpf
export KERNEL_INC_OPTIONS="-nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.3/include -I/home/acme/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/acme/git/linux/include -Iinclude -I/home/acme/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -Iinclude/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h"
export WORKING_DIR=/lib/modules/4.2.0/build
export CLANG_SOURCE=-
export CLANG_OPTIONS=-xc
OBJ=/tmp/foo.o
rm -f $OBJ
echo '__attribute__((section("fork=do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | \
clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o /tmp/foo.o && file $OBJ
---
First asking to put a probe in a function not present in the kernel
(misses the initial _):
$ perf record --event /tmp/foo.o sleep 1
Probe point 'do_fork' not found.
event syntax error: '/tmp/foo.o'
\___ You need to check probing points in BPF file
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
$
---
Now, with "__attribute__((section("fork=_do_fork"), used)):
$ grep _do_fork /proc/kallsyms
ffffffff81099ab0 T _do_fork
$ perf record --event /tmp/foo.o sleep 1
Failed to open kprobe_events: Permission denied
event syntax error: '/tmp/foo.o'
\___ Permission denied
---
Cool, we need to provide some better hints, "kprobe_events" is too low
level, one doesn't strictly need to know the precise details of how
these things are put in place, so something that shows the command
needed to fix the permissions would be more helpful.
Lets try as root instead:
# perf record --event /tmp/foo.o sleep 1
Lowering default frequency rate to 1000.
Please consider tweaking /proc/sys/kernel/perf_event_max_sample_rate.
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.013 MB perf.data ]
# perf evlist
/tmp/foo.o
[root@felicio ~]# perf evlist -v
/tmp/foo.o: type: 1, size: 112, config: 0x9, { sample_period,
sample_freq }: 1000, sample_type: IP|TID|TIME|PERIOD, disabled: 1,
inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1, task: 1,
sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1
---
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1444826502-49291-5-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-10-14 20:41:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused,
|
|
|
|
void *_priv)
|
|
|
|
{
|
|
|
|
struct bpf_prog_priv *priv = _priv;
|
|
|
|
|
|
|
|
cleanup_perf_probe_events(&priv->pev, 1);
|
|
|
|
free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_bpf_program(struct bpf_program *prog)
|
|
|
|
{
|
|
|
|
struct perf_probe_event *pev = NULL;
|
|
|
|
struct bpf_prog_priv *priv = NULL;
|
|
|
|
const char *config_str;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
config_str = bpf_program__title(prog, false);
|
|
|
|
if (!config_str) {
|
|
|
|
pr_debug("bpf: unable to get title for program\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv = calloc(sizeof(*priv), 1);
|
|
|
|
if (!priv) {
|
|
|
|
pr_debug("bpf: failed to alloc priv\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
pev = &priv->pev;
|
|
|
|
|
|
|
|
pr_debug("bpf: config program '%s'\n", config_str);
|
|
|
|
err = parse_perf_probe_command(config_str, pev);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_debug("bpf: '%s' is not a valid config string\n",
|
|
|
|
config_str);
|
|
|
|
err = -EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) {
|
|
|
|
pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
|
|
|
|
config_str, PERF_BPF_PROBE_GROUP);
|
|
|
|
err = -EINVAL;
|
|
|
|
goto errout;
|
|
|
|
} else if (!pev->group)
|
|
|
|
pev->group = strdup(PERF_BPF_PROBE_GROUP);
|
|
|
|
|
|
|
|
if (!pev->group) {
|
|
|
|
pr_debug("bpf: strdup failed\n");
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pev->event) {
|
|
|
|
pr_debug("bpf: '%s': event name is missing\n",
|
|
|
|
config_str);
|
|
|
|
err = -EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
pr_debug("bpf: config '%s' is ok\n", config_str);
|
|
|
|
|
|
|
|
err = bpf_program__set_private(prog, priv, bpf_prog_priv__clear);
|
|
|
|
if (err) {
|
|
|
|
pr_debug("Failed to set priv for program '%s'\n", config_str);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
if (pev)
|
|
|
|
clear_perf_probe_event(pev);
|
|
|
|
free(priv);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bpf__prepare_probe(void)
|
|
|
|
{
|
|
|
|
static int err = 0;
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make err static, so if init failed the first, bpf__prepare_probe()
|
|
|
|
* fails each time without calling init_probe_symbol_maps multiple
|
|
|
|
* times.
|
|
|
|
*/
|
|
|
|
if (initialized)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
err = init_probe_symbol_maps(false);
|
|
|
|
if (err < 0)
|
|
|
|
pr_debug("Failed to init_probe_symbol_maps\n");
|
|
|
|
probe_conf.max_probes = MAX_PROBES;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bpf__probe(struct bpf_object *obj)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct bpf_program *prog;
|
|
|
|
struct bpf_prog_priv *priv;
|
|
|
|
struct perf_probe_event *pev;
|
|
|
|
|
|
|
|
err = bpf__prepare_probe();
|
|
|
|
if (err) {
|
|
|
|
pr_debug("bpf__prepare_probe failed\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpf_object__for_each_program(prog, obj) {
|
|
|
|
err = config_bpf_program(prog);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = bpf_program__get_private(prog, (void **)&priv);
|
|
|
|
if (err || !priv)
|
|
|
|
goto out;
|
|
|
|
pev = &priv->pev;
|
|
|
|
|
|
|
|
err = convert_perf_probe_events(pev, 1);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_debug("bpf_probe: failed to convert perf probe events");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = apply_perf_probe_events(pev, 1);
|
|
|
|
if (err < 0) {
|
|
|
|
pr_debug("bpf_probe: failed to apply perf probe events");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return err < 0 ? err : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EVENTS_WRITE_BUFSIZE 4096
|
|
|
|
int bpf__unprobe(struct bpf_object *obj)
|
|
|
|
{
|
|
|
|
int err, ret = 0;
|
|
|
|
struct bpf_program *prog;
|
|
|
|
struct bpf_prog_priv *priv;
|
|
|
|
|
|
|
|
bpf_object__for_each_program(prog, obj) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
err = bpf_program__get_private(prog, (void **)&priv);
|
|
|
|
if (err || !priv)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (i = 0; i < priv->pev.ntevs; i++) {
|
|
|
|
struct probe_trace_event *tev = &priv->pev.tevs[i];
|
|
|
|
char name_buf[EVENTS_WRITE_BUFSIZE];
|
|
|
|
struct strfilter *delfilter;
|
|
|
|
|
|
|
|
snprintf(name_buf, EVENTS_WRITE_BUFSIZE,
|
|
|
|
"%s:%s", tev->group, tev->event);
|
|
|
|
name_buf[EVENTS_WRITE_BUFSIZE - 1] = '\0';
|
|
|
|
|
|
|
|
delfilter = strfilter__new(name_buf, NULL);
|
|
|
|
if (!delfilter) {
|
|
|
|
pr_debug("Failed to create filter for unprobing\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = del_perf_probe_events(delfilter);
|
|
|
|
strfilter__delete(delfilter);
|
|
|
|
if (err) {
|
|
|
|
pr_debug("Failed to delete %s\n", name_buf);
|
|
|
|
ret = err;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define bpf__strerror_head(err, buf, size) \
|
|
|
|
char sbuf[STRERR_BUFSIZE], *emsg;\
|
|
|
|
if (!size)\
|
|
|
|
return 0;\
|
|
|
|
if (err < 0)\
|
|
|
|
err = -err;\
|
|
|
|
emsg = strerror_r(err, sbuf, sizeof(sbuf));\
|
|
|
|
switch (err) {\
|
|
|
|
default:\
|
|
|
|
scnprintf(buf, size, "%s", emsg);\
|
|
|
|
break;
|
|
|
|
|
|
|
|
#define bpf__strerror_entry(val, fmt...)\
|
|
|
|
case val: {\
|
|
|
|
scnprintf(buf, size, fmt);\
|
|
|
|
break;\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define bpf__strerror_end(buf, size)\
|
|
|
|
}\
|
|
|
|
buf[size - 1] = '\0';
|
|
|
|
|
|
|
|
int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
|
|
|
|
int err, char *buf, size_t size)
|
|
|
|
{
|
|
|
|
bpf__strerror_head(err, buf, size);
|
|
|
|
bpf__strerror_entry(EEXIST, "Probe point exist. Try use 'perf probe -d \"*\"'");
|
|
|
|
bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0\n");
|
|
|
|
bpf__strerror_entry(ENOENT, "You need to check probing points in BPF file\n");
|
|
|
|
bpf__strerror_end(buf, size);
|
|
|
|
return 0;
|
2015-10-14 20:41:13 +08:00
|
|
|
}
|