Commit b68e67b4 authored by Teng Qin's avatar Teng Qin

Clean libbpf interface arguments for tracing events

parent bebb9c8c
...@@ -172,7 +172,7 @@ StatusTuple BPF::attach_kprobe(const std::string& kernel_func, ...@@ -172,7 +172,7 @@ StatusTuple BPF::attach_kprobe(const std::string& kernel_func,
void* res = void* res =
bpf_attach_kprobe(probe_fd, attach_type, probe_event.c_str(), kernel_func.c_str(), bpf_attach_kprobe(probe_fd, attach_type, probe_event.c_str(), kernel_func.c_str(),
pid, cpu, group_fd, cb, cb_cookie); cb, cb_cookie);
if (!res) { if (!res) {
TRY2(unload_func(probe_func)); TRY2(unload_func(probe_func));
...@@ -208,7 +208,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path, ...@@ -208,7 +208,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path,
void* res = void* res =
bpf_attach_uprobe(probe_fd, attach_type, probe_event.c_str(), binary_path.c_str(), bpf_attach_uprobe(probe_fd, attach_type, probe_event.c_str(), binary_path.c_str(),
offset, pid, cpu, group_fd, cb, cb_cookie); offset, pid, cb, cb_cookie);
if (!res) { if (!res) {
TRY2(unload_func(probe_func)); TRY2(unload_func(probe_func));
...@@ -275,8 +275,8 @@ StatusTuple BPF::attach_tracepoint(const std::string& tracepoint, ...@@ -275,8 +275,8 @@ StatusTuple BPF::attach_tracepoint(const std::string& tracepoint,
TRY2(load_func(probe_func, BPF_PROG_TYPE_TRACEPOINT, probe_fd)); TRY2(load_func(probe_func, BPF_PROG_TYPE_TRACEPOINT, probe_fd));
void* res = void* res =
bpf_attach_tracepoint(probe_fd, tp_category.c_str(), tp_name.c_str(), pid, bpf_attach_tracepoint(probe_fd, tp_category.c_str(), tp_name.c_str(), cb,
cpu, group_fd, cb, cb_cookie); cb_cookie);
if (!res) { if (!res) {
TRY2(unload_func(probe_func)); TRY2(unload_func(probe_func));
......
...@@ -529,8 +529,8 @@ int bpf_attach_socket(int sock, int prog) { ...@@ -529,8 +529,8 @@ int bpf_attach_socket(int sock, int prog) {
} }
static int bpf_attach_tracing_event(int progfd, const char *event_path, static int bpf_attach_tracing_event(int progfd, const char *event_path,
struct perf_reader *reader, int pid, int cpu, int group_fd) { struct perf_reader *reader, int pid) {
int efd, pfd; int efd, pfd, cpu = 0;
ssize_t bytes; ssize_t bytes;
char buf[256]; char buf[256];
struct perf_event_attr attr = {}; struct perf_event_attr attr = {};
...@@ -555,7 +555,15 @@ static int bpf_attach_tracing_event(int progfd, const char *event_path, ...@@ -555,7 +555,15 @@ static int bpf_attach_tracing_event(int progfd, const char *event_path,
attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN; attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN;
attr.sample_period = 1; attr.sample_period = 1;
attr.wakeup_events = 1; attr.wakeup_events = 1;
pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, group_fd, PERF_FLAG_FD_CLOEXEC); // PID filter is only possible for uprobe events.
if (pid < 0)
pid = -1;
// perf_event_open API doesn't allow both pid and cpu to be -1.
// So only set it to -1 when PID is not -1.
// Tracing events do not do CPU filtering in any cases.
if (pid != -1)
cpu = -1;
pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
if (pfd < 0) { if (pfd < 0) {
fprintf(stderr, "perf_event_open(%s/id): %s\n", event_path, strerror(errno)); fprintf(stderr, "perf_event_open(%s/id): %s\n", event_path, strerror(errno));
return -1; return -1;
...@@ -577,9 +585,8 @@ static int bpf_attach_tracing_event(int progfd, const char *event_path, ...@@ -577,9 +585,8 @@ static int bpf_attach_tracing_event(int progfd, const char *event_path,
return 0; return 0;
} }
void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name, void *bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type,
const char *fn_name, const char *ev_name, const char *fn_name,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie) perf_reader_cb cb, void *cb_cookie)
{ {
int kfd; int kfd;
...@@ -611,7 +618,7 @@ void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, con ...@@ -611,7 +618,7 @@ void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, con
close(kfd); close(kfd);
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%ss/%s", event_type, event_alias); snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%ss/%s", event_type, event_alias);
if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) < 0) if (bpf_attach_tracing_event(progfd, buf, reader, -1 /* PID */) < 0)
goto error; goto error;
return reader; return reader;
...@@ -683,10 +690,10 @@ static void exit_mount_ns(int fd) { ...@@ -683,10 +690,10 @@ static void exit_mount_ns(int fd) {
perror("setns"); perror("setns");
} }
void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name, void *bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type,
const char *binary_path, uint64_t offset, const char *ev_name, const char *binary_path,
pid_t pid, int cpu, int group_fd, uint64_t offset, pid_t pid, perf_reader_cb cb,
perf_reader_cb cb, void *cb_cookie) void *cb_cookie)
{ {
char buf[PATH_MAX]; char buf[PATH_MAX];
char event_alias[PATH_MAX]; char event_alias[PATH_MAX];
...@@ -728,7 +735,7 @@ void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, con ...@@ -728,7 +735,7 @@ void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, con
ns_fd = -1; ns_fd = -1;
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%ss/%s", event_type, event_alias); snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%ss/%s", event_type, event_alias);
if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) < 0) if (bpf_attach_tracing_event(progfd, buf, reader, pid) < 0)
goto error; goto error;
return reader; return reader;
...@@ -782,9 +789,9 @@ int bpf_detach_uprobe(const char *ev_name) ...@@ -782,9 +789,9 @@ int bpf_detach_uprobe(const char *ev_name)
} }
void * bpf_attach_tracepoint(int progfd, const char *tp_category, void *bpf_attach_tracepoint(int progfd, const char *tp_category,
const char *tp_name, int pid, int cpu, const char *tp_name, perf_reader_cb cb,
int group_fd, perf_reader_cb cb, void *cb_cookie) { void *cb_cookie) {
char buf[256]; char buf[256];
struct perf_reader *reader = NULL; struct perf_reader *reader = NULL;
...@@ -794,7 +801,7 @@ void * bpf_attach_tracepoint(int progfd, const char *tp_category, ...@@ -794,7 +801,7 @@ void * bpf_attach_tracepoint(int progfd, const char *tp_category,
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%s/%s", snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%s/%s",
tp_category, tp_name); tp_category, tp_name);
if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) < 0) if (bpf_attach_tracing_event(progfd, buf, reader, -1 /* PID */) < 0)
goto error; goto error;
return reader; return reader;
......
...@@ -70,23 +70,22 @@ typedef void (*perf_reader_cb)(void *cb_cookie, int pid, uint64_t callchain_num, ...@@ -70,23 +70,22 @@ typedef void (*perf_reader_cb)(void *cb_cookie, int pid, uint64_t callchain_num,
typedef void (*perf_reader_raw_cb)(void *cb_cookie, void *raw, int raw_size); typedef void (*perf_reader_raw_cb)(void *cb_cookie, void *raw, int raw_size);
typedef void (*perf_reader_lost_cb)(uint64_t lost); typedef void (*perf_reader_lost_cb)(uint64_t lost);
void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, void *bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type,
const char *ev_name, const char *fn_name, const char *ev_name, const char *fn_name,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie); perf_reader_cb cb, void *cb_cookie);
int bpf_detach_kprobe(const char *ev_name); int bpf_detach_kprobe(const char *ev_name);
void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, void *bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type,
const char *ev_name, const char *binary_path, uint64_t offset, const char *ev_name, const char *binary_path,
pid_t pid, int cpu, int group_fd, uint64_t offset, pid_t pid, perf_reader_cb cb,
perf_reader_cb cb, void *cb_cookie); void *cb_cookie);
int bpf_detach_uprobe(const char *ev_name); int bpf_detach_uprobe(const char *ev_name);
void * bpf_attach_tracepoint(int progfd, const char *tp_category, void *bpf_attach_tracepoint(int progfd, const char *tp_category,
const char *tp_name, int pid, int cpu, const char *tp_name, perf_reader_cb cb,
int group_fd, perf_reader_cb cb, void *cb_cookie); void *cb_cookie);
int bpf_detach_tracepoint(const char *tp_category, const char *tp_name); int bpf_detach_tracepoint(const char *tp_category, const char *tp_name);
void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb, void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb,
......
...@@ -189,9 +189,7 @@ function Bpf:attach_uprobe(args) ...@@ -189,9 +189,7 @@ function Bpf:attach_uprobe(args)
local retprobe = args.retprobe and 1 or 0 local retprobe = args.retprobe and 1 or 0
local res = libbcc.bpf_attach_uprobe(fn.fd, retprobe, ev_name, path, addr, local res = libbcc.bpf_attach_uprobe(fn.fd, retprobe, ev_name, path, addr,
args.pid or -1, args.pid or -1, nil, nil) -- TODO; reader callback
args.cpu or 0,
args.group_fd or -1, nil, nil) -- TODO; reader callback
assert(res ~= nil, "failed to attach BPF to uprobe") assert(res ~= nil, "failed to attach BPF to uprobe")
self:probe_store("uprobe", ev_name, res) self:probe_store("uprobe", ev_name, res)
...@@ -209,9 +207,7 @@ function Bpf:attach_kprobe(args) ...@@ -209,9 +207,7 @@ function Bpf:attach_kprobe(args)
local retprobe = args.retprobe and 1 or 0 local retprobe = args.retprobe and 1 or 0
local res = libbcc.bpf_attach_kprobe(fn.fd, retprobe, ev_name, event, local res = libbcc.bpf_attach_kprobe(fn.fd, retprobe, ev_name, event,
args.pid or -1, nil, nil) -- TODO; reader callback
args.cpu or 0,
args.group_fd or -1, nil, nil) -- TODO; reader callback
assert(res ~= nil, "failed to attach BPF to kprobe") assert(res ~= nil, "failed to attach BPF to kprobe")
self:probe_store("kprobe", ev_name, res) self:probe_store("kprobe", ev_name, res)
......
...@@ -43,16 +43,14 @@ typedef void (*perf_reader_cb)(void *cb_cookie, int pid, uint64_t callchain_num, ...@@ -43,16 +43,14 @@ typedef void (*perf_reader_cb)(void *cb_cookie, int pid, uint64_t callchain_num,
typedef void (*perf_reader_raw_cb)(void *cb_cookie, void *raw, int raw_size); typedef void (*perf_reader_raw_cb)(void *cb_cookie, void *raw, int raw_size);
typedef void (*perf_reader_lost_cb)(uint64_t lost); typedef void (*perf_reader_lost_cb)(uint64_t lost);
void * bpf_attach_kprobe(int progfd, int attach_type, const char *ev_name, void *bpf_attach_kprobe(int progfd, int attach_type, const char *ev_name,
const char *fn_name, const char *fn_name, perf_reader_cb cb,
int pid, int cpu, int group_fd, void *cb_cookie);
perf_reader_cb cb, void *cb_cookie);
int bpf_detach_kprobe(const char *ev_name); int bpf_detach_kprobe(const char *ev_name);
void * bpf_attach_uprobe(int progfd, int attach_type, const char *ev_name, void *bpf_attach_uprobe(int progfd, int attach_type, const char *ev_name,
const char *binary_path, uint64_t offset, const char *binary_path, uint64_t offset, int pid,
int pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie); perf_reader_cb cb, void *cb_cookie);
int bpf_detach_uprobe(const char *ev_name); int bpf_detach_uprobe(const char *ev_name);
......
...@@ -519,8 +519,8 @@ class BPF(object): ...@@ -519,8 +519,8 @@ class BPF(object):
fn = self.load_func(fn_name, BPF.KPROBE) fn = self.load_func(fn_name, BPF.KPROBE)
ev_name = "p_" + event.replace("+", "_").replace(".", "_") ev_name = "p_" + event.replace("+", "_").replace(".", "_")
res = lib.bpf_attach_kprobe(fn.fd, 0, ev_name.encode("ascii"), res = lib.bpf_attach_kprobe(fn.fd, 0, ev_name.encode("ascii"),
event.encode("ascii"), pid, cpu, group_fd, event.encode("ascii"), self._reader_cb_impl,
self._reader_cb_impl, ct.cast(id(self), ct.py_object)) ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p) res = ct.cast(res, ct.c_void_p)
if not res: if not res:
raise Exception("Failed to attach BPF to kprobe") raise Exception("Failed to attach BPF to kprobe")
...@@ -556,8 +556,8 @@ class BPF(object): ...@@ -556,8 +556,8 @@ class BPF(object):
fn = self.load_func(fn_name, BPF.KPROBE) fn = self.load_func(fn_name, BPF.KPROBE)
ev_name = "r_" + event.replace("+", "_").replace(".", "_") ev_name = "r_" + event.replace("+", "_").replace(".", "_")
res = lib.bpf_attach_kprobe(fn.fd, 1, ev_name.encode("ascii"), res = lib.bpf_attach_kprobe(fn.fd, 1, ev_name.encode("ascii"),
event.encode("ascii"), pid, cpu, group_fd, event.encode("ascii"), self._reader_cb_impl,
self._reader_cb_impl, ct.cast(id(self), ct.py_object)) ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p) res = ct.cast(res, ct.c_void_p)
if not res: if not res:
raise Exception("Failed to attach BPF to kprobe") raise Exception("Failed to attach BPF to kprobe")
...@@ -680,8 +680,8 @@ class BPF(object): ...@@ -680,8 +680,8 @@ class BPF(object):
fn = self.load_func(fn_name, BPF.TRACEPOINT) fn = self.load_func(fn_name, BPF.TRACEPOINT)
(tp_category, tp_name) = tp.split(':') (tp_category, tp_name) = tp.split(':')
res = lib.bpf_attach_tracepoint(fn.fd, tp_category.encode("ascii"), res = lib.bpf_attach_tracepoint(fn.fd, tp_category.encode("ascii"),
tp_name.encode("ascii"), pid, cpu, group_fd, tp_name.encode("ascii"), self._reader_cb_impl,
self._reader_cb_impl, ct.cast(id(self), ct.py_object)) ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p) res = ct.cast(res, ct.c_void_p)
if not res: if not res:
raise Exception("Failed to attach BPF to tracepoint") raise Exception("Failed to attach BPF to tracepoint")
...@@ -833,8 +833,8 @@ class BPF(object): ...@@ -833,8 +833,8 @@ class BPF(object):
fn = self.load_func(fn_name, BPF.KPROBE) fn = self.load_func(fn_name, BPF.KPROBE)
ev_name = self._get_uprobe_evname("p", path, addr, pid) ev_name = self._get_uprobe_evname("p", path, addr, pid)
res = lib.bpf_attach_uprobe(fn.fd, 0, ev_name.encode("ascii"), res = lib.bpf_attach_uprobe(fn.fd, 0, ev_name.encode("ascii"),
path.encode("ascii"), addr, pid, cpu, group_fd, path.encode("ascii"), addr, pid, self._reader_cb_impl,
self._reader_cb_impl, ct.cast(id(self), ct.py_object)) ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p) res = ct.cast(res, ct.c_void_p)
if not res: if not res:
raise Exception("Failed to attach BPF to uprobe") raise Exception("Failed to attach BPF to uprobe")
...@@ -883,8 +883,8 @@ class BPF(object): ...@@ -883,8 +883,8 @@ class BPF(object):
fn = self.load_func(fn_name, BPF.KPROBE) fn = self.load_func(fn_name, BPF.KPROBE)
ev_name = self._get_uprobe_evname("r", path, addr, pid) ev_name = self._get_uprobe_evname("r", path, addr, pid)
res = lib.bpf_attach_uprobe(fn.fd, 1, ev_name.encode("ascii"), res = lib.bpf_attach_uprobe(fn.fd, 1, ev_name.encode("ascii"),
path.encode("ascii"), addr, pid, cpu, group_fd, path.encode("ascii"), addr, pid, self._reader_cb_impl,
self._reader_cb_impl, ct.cast(id(self), ct.py_object)) ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p) res = ct.cast(res, ct.c_void_p)
if not res: if not res:
raise Exception("Failed to attach BPF to uprobe") raise Exception("Failed to attach BPF to uprobe")
......
...@@ -90,18 +90,18 @@ _CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_int, ...@@ -90,18 +90,18 @@ _CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_int,
ct.c_ulonglong, ct.POINTER(ct.c_ulonglong)) ct.c_ulonglong, ct.POINTER(ct.c_ulonglong))
_RAW_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_void_p, ct.c_int) _RAW_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_void_p, ct.c_int)
_LOST_CB_TYPE = ct.CFUNCTYPE(None, ct.c_ulonglong) _LOST_CB_TYPE = ct.CFUNCTYPE(None, ct.c_ulonglong)
lib.bpf_attach_kprobe.argtypes = [ct.c_int, ct.c_int, ct.c_char_p, ct.c_char_p, ct.c_int, lib.bpf_attach_kprobe.argtypes = [ct.c_int, ct.c_int, ct.c_char_p, ct.c_char_p,
ct.c_int, ct.c_int, _CB_TYPE, ct.py_object] _CB_TYPE, ct.py_object]
lib.bpf_detach_kprobe.restype = ct.c_int lib.bpf_detach_kprobe.restype = ct.c_int
lib.bpf_detach_kprobe.argtypes = [ct.c_char_p] lib.bpf_detach_kprobe.argtypes = [ct.c_char_p]
lib.bpf_attach_uprobe.restype = ct.c_void_p lib.bpf_attach_uprobe.restype = ct.c_void_p
lib.bpf_attach_uprobe.argtypes = [ct.c_int, ct.c_int, ct.c_char_p, ct.c_char_p, lib.bpf_attach_uprobe.argtypes = [ct.c_int, ct.c_int, ct.c_char_p, ct.c_char_p,
ct.c_ulonglong, ct.c_int, ct.c_int, ct.c_int, _CB_TYPE, ct.py_object] ct.c_ulonglong, ct.c_int, _CB_TYPE, ct.py_object]
lib.bpf_detach_uprobe.restype = ct.c_int lib.bpf_detach_uprobe.restype = ct.c_int
lib.bpf_detach_uprobe.argtypes = [ct.c_char_p] lib.bpf_detach_uprobe.argtypes = [ct.c_char_p]
lib.bpf_attach_tracepoint.restype = ct.c_void_p lib.bpf_attach_tracepoint.restype = ct.c_void_p
lib.bpf_attach_tracepoint.argtypes = [ct.c_int, ct.c_char_p, ct.c_char_p, ct.c_int, lib.bpf_attach_tracepoint.argtypes = [ct.c_int, ct.c_char_p, ct.c_char_p,
ct.c_int, ct.c_int, _CB_TYPE, ct.py_object] _CB_TYPE, ct.py_object]
lib.bpf_detach_tracepoint.restype = ct.c_int lib.bpf_detach_tracepoint.restype = ct.c_int
lib.bpf_detach_tracepoint.argtypes = [ct.c_char_p, ct.c_char_p] lib.bpf_detach_tracepoint.argtypes = [ct.c_char_p, ct.c_char_p]
lib.bpf_open_perf_buffer.restype = ct.c_void_p lib.bpf_open_perf_buffer.restype = ct.c_void_p
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment