StatusTuple BPF::attach_kprobe(const std::string& kernel_func,
const std::string& probe_func,
- int attach_type,
+ bpf_probe_attach_type attach_type,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void* cb_cookie) {
std::string probe_event = get_kprobe_event(kernel_func, attach_type);
const std::string& symbol,
const std::string& probe_func,
uint64_t symbol_addr,
- int attach_type,
+ bpf_probe_attach_type attach_type,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void* cb_cookie) {
bcc_symbol sym = bcc_symbol();
}
StatusTuple BPF::detach_kprobe(const std::string& kernel_func,
- int attach_type) {
+ bpf_probe_attach_type attach_type) {
std::string event = get_kprobe_event(kernel_func, attach_type);
auto it = kprobes_.find(event);
StatusTuple BPF::detach_uprobe(const std::string& binary_path,
const std::string& symbol, uint64_t symbol_addr,
- int attach_type) {
+ bpf_probe_attach_type attach_type) {
bcc_symbol sym = bcc_symbol();
TRY2(check_binary_symbol(binary_path, symbol, symbol_addr, &sym));
}
StatusTuple BPF::load_func(const std::string& func_name,
- enum bpf_prog_type type, int& fd) {
+ bpf_prog_type type, int& fd) {
if (funcs_.find(func_name) != funcs_.end()) {
fd = funcs_[func_name];
return StatusTuple(0);
}
std::string BPF::get_kprobe_event(const std::string& kernel_func,
- int type) {
+ bpf_probe_attach_type type) {
std::string res = attach_type_prefix(type) + "_";
res += sanitize_str(kernel_func, &BPF::kprobe_event_validator);
return res;
}
std::string BPF::get_uprobe_event(const std::string& binary_path,
- uint64_t offset, int type) {
+ uint64_t offset, bpf_probe_attach_type type) {
std::string res = attach_type_prefix(type) + "_";
res += sanitize_str(binary_path, &BPF::uprobe_path_validator);
res += "_0x" + uint_to_hex(offset);
StatusTuple attach_kprobe(
const std::string& kernel_func, const std::string& probe_func,
- int attach_type = BPF_PROBE_ENTRY,
+ bpf_probe_attach_type = probe_entry,
pid_t pid = -1, int cpu = 0, int group_fd = -1,
perf_reader_cb cb = nullptr, void* cb_cookie = nullptr);
StatusTuple detach_kprobe(
const std::string& kernel_func,
- int attach_type = BPF_PROBE_ENTRY);
+ bpf_probe_attach_type attach_type = probe_entry);
StatusTuple attach_uprobe(
const std::string& binary_path, const std::string& symbol,
const std::string& probe_func, uint64_t symbol_addr = 0,
- int attach_type = BPF_PROBE_ENTRY,
+ bpf_probe_attach_type attach_type = probe_entry,
pid_t pid = -1, int cpu = 0, int group_fd = -1,
perf_reader_cb cb = nullptr, void* cb_cookie = nullptr);
StatusTuple detach_uprobe(
const std::string& binary_path, const std::string& symbol,
uint64_t symbol_addr = 0,
- int attach_type = BPF_PROBE_ENTRY);
+ bpf_probe_attach_type attach_type = probe_entry);
StatusTuple attach_usdt(const USDT& usdt, pid_t pid = -1, int cpu = 0,
int group_fd = -1);
StatusTuple detach_usdt(const USDT& usdt);
StatusTuple unload_func(const std::string& func_name);
std::string get_kprobe_event(const std::string& kernel_func,
- int attach_type);
+ bpf_probe_attach_type type);
std::string get_uprobe_event(const std::string& binary_path, uint64_t offset,
- int attach_type);
+ bpf_probe_attach_type type);
StatusTuple detach_kprobe_event(const std::string& event, open_probe_t& attr);
StatusTuple detach_uprobe_event(const std::string& event, open_probe_t& attr);
open_probe_t& attr);
StatusTuple detach_perf_event_all_cpu(open_probe_t& attr);
- std::string attach_type_debug(int attach_type) {
- switch (attach_type) {
- case BPF_PROBE_ENTRY:
+ std::string attach_type_debug(bpf_probe_attach_type type) {
+ switch (type) {
+ case probe_entry:
return "";
- case BPF_PROBE_RETURN:
+ case probe_return:
return "return ";
}
return "ERROR";
}
- std::string attach_type_prefix(int attach_type) {
- switch (attach_type) {
- case BPF_PROBE_ENTRY:
+ std::string attach_type_prefix(bpf_probe_attach_type type) {
+ switch (type) {
+ case probe_entry:
return "p";
- case BPF_PROBE_RETURN:
+ case probe_return:
return "r";
}
return "ERROR";
return 0;
}
-void * bpf_attach_kprobe(int progfd, int attach_type, const char *ev_name,
+void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name,
const char *fn_name,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie)
{
int kfd;
- char buf[strlen(ev_name)+256];
- char new_name[strlen(ev_name)+32];
+ char buf[256];
+ char new_name[128];
struct perf_reader *reader = NULL;
static char *event_type = "kprobe";
+ int n;
snprintf(new_name, sizeof(new_name), "%s_bcc_%d", ev_name, getpid());
reader = perf_reader_new(cb, NULL, cb_cookie);
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s", new_name);
if (mkdir(buf, 0755) == -1)
goto retry;
- snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s/events/%ss/%s",
+ n = snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s/events/%ss/%s",
new_name, event_type, new_name);
- if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) == 0) {
+ if (n < sizeof(buf) && bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) == 0)
goto out;
- }
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s", new_name);
rmdir(buf);
}
}
-void * bpf_attach_uprobe(int progfd, int attach_type, const char *ev_name,
+void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name,
const char *binary_path, uint64_t offset,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie)
{
int kfd;
- char buf[strlen(binary_path)+strlen(ev_name)+256];
- char new_name[strlen(ev_name)+32];
+ char buf[PATH_MAX];
+ char new_name[128];
struct perf_reader *reader = NULL;
static char *event_type = "uprobe";
+ int n;
snprintf(new_name, sizeof(new_name), "%s_bcc_%d", ev_name, getpid());
reader = perf_reader_new(cb, NULL, cb_cookie);
goto error;
}
- snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r',
+ n = snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r',
event_type, new_name, binary_path, offset);
+ if (n >= sizeof(buf)) {
+ close(kfd);
+ goto error;
+ }
if (write(kfd, buf, strlen(buf)) < 0) {
if (errno == EINVAL)
fprintf(stderr, "check dmesg output for possible cause\n");
static int bpf_detach_probe(const char *ev_name, const char *event_type)
{
int kfd;
- char buf[strlen(ev_name)+256];
+ char buf[256];
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events", event_type);
kfd = open(buf, O_WRONLY | O_APPEND, 0);
if (kfd < 0) {
int bpf_detach_kprobe(const char *ev_name)
{
- char buf[strlen(ev_name)+256];
+ char buf[256];
int ret = bpf_detach_probe(ev_name, "kprobe");
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/%s_bcc_%d", ev_name, getpid());
if (access(buf, F_OK) != -1) {
extern "C" {
#endif
+enum bpf_probe_attach_type {
+ probe_entry,
+ probe_return
+};
#define BPF_PROBE_ENTRY 0
#define BPF_PROBE_RETURN 1
void *callchain);
typedef void (*perf_reader_raw_cb)(void *cb_cookie, void *raw, int raw_size);
-void * bpf_attach_kprobe(int progfd, int attach_type, const char *ev_name,
+void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name,
const char *fn_name,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie);
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, enum bpf_probe_attach_type attach_type, const char *ev_name,
const char *binary_path, uint64_t offset,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie);