Skip to content

Commit

Permalink
c++ api: add wrapper for raw tracepoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mokomull authored and yonghong-song committed Feb 21, 2020
1 parent 5e3f9e4 commit 5681ea9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
50 changes: 50 additions & 0 deletions src/cc/api/BPF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ StatusTuple BPF::detach_all() {
}
}

for (auto& it : raw_tracepoints_) {
auto res = detach_raw_tracepoint_event(it.first, it.second);
if (res.code() != 0) {
error_msg += "Failed to detach Raw tracepoint " + it.first + ": ";
error_msg += res.msg() + "\n";
has_error = true;
}
}

for (auto& it : perf_buffers_) {
auto res = it.second->close_all_cpu();
if (res.code() != 0) {
Expand Down Expand Up @@ -326,6 +335,29 @@ StatusTuple BPF::attach_tracepoint(const std::string& tracepoint,
return StatusTuple(0);
}

StatusTuple BPF::attach_raw_tracepoint(const std::string& tracepoint, const std::string& probe_func) {
if (raw_tracepoints_.find(tracepoint) != raw_tracepoints_.end())
return StatusTuple(-1, "Raw tracepoint %s already attached",
tracepoint.c_str());

int probe_fd;
TRY2(load_func(probe_func, BPF_PROG_TYPE_RAW_TRACEPOINT, probe_fd));

int res_fd = bpf_attach_raw_tracepoint(probe_fd, tracepoint.c_str());

if (res_fd < 0) {
TRY2(unload_func(probe_func));
return StatusTuple(-1, "Unable to attach Raw tracepoint %s using %s",
tracepoint.c_str(), probe_func.c_str());
}

open_probe_t p = {};
p.perf_event_fd = res_fd;
p.func = probe_func;
raw_tracepoints_[tracepoint] = std::move(p);
return StatusTuple(0);
}

StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config,
const std::string& probe_func,
uint64_t sample_period, uint64_t sample_freq,
Expand Down Expand Up @@ -485,6 +517,16 @@ StatusTuple BPF::detach_tracepoint(const std::string& tracepoint) {
return StatusTuple(0);
}

StatusTuple BPF::detach_raw_tracepoint(const std::string& tracepoint) {
auto it = raw_tracepoints_.find(tracepoint);
if (it == raw_tracepoints_.end())
return StatusTuple(-1, "No open Raw tracepoint %s", tracepoint.c_str());

TRY2(detach_raw_tracepoint_event(it->first, it->second));
raw_tracepoints_.erase(it);
return StatusTuple(0);
}

StatusTuple BPF::detach_perf_event(uint32_t ev_type, uint32_t ev_config) {
auto it = perf_events_.find(std::make_pair(ev_type, ev_config));
if (it == perf_events_.end())
Expand Down Expand Up @@ -789,6 +831,14 @@ StatusTuple BPF::detach_tracepoint_event(const std::string& tracepoint,
return StatusTuple(0);
}

StatusTuple BPF::detach_raw_tracepoint_event(const std::string& tracepoint,
open_probe_t& attr) {
TRY2(close(attr.perf_event_fd));
TRY2(unload_func(attr.func));

return StatusTuple(0);
}

StatusTuple BPF::detach_perf_event_all_cpu(open_probe_t& attr) {
bool has_error = false;
std::string err_msg;
Expand Down
7 changes: 7 additions & 0 deletions src/cc/api/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class BPF {
const std::string& probe_func);
StatusTuple detach_tracepoint(const std::string& tracepoint);

StatusTuple attach_raw_tracepoint(const std::string& tracepoint,
const std::string& probe_func);
StatusTuple detach_raw_tracepoint(const std::string& tracepoint);

StatusTuple attach_perf_event(uint32_t ev_type, uint32_t ev_config,
const std::string& probe_func,
uint64_t sample_period, uint64_t sample_freq,
Expand Down Expand Up @@ -248,6 +252,8 @@ class BPF {
StatusTuple detach_uprobe_event(const std::string& event, open_probe_t& attr);
StatusTuple detach_tracepoint_event(const std::string& tracepoint,
open_probe_t& attr);
StatusTuple detach_raw_tracepoint_event(const std::string& tracepoint,
open_probe_t& attr);
StatusTuple detach_perf_event_all_cpu(open_probe_t& attr);

std::string attach_type_debug(bpf_probe_attach_type type) {
Expand Down Expand Up @@ -302,6 +308,7 @@ class BPF {
std::map<std::string, open_probe_t> kprobes_;
std::map<std::string, open_probe_t> uprobes_;
std::map<std::string, open_probe_t> tracepoints_;
std::map<std::string, open_probe_t> raw_tracepoints_;
std::map<std::string, BPFPerfBuffer*> perf_buffers_;
std::map<std::string, BPFPerfEventArray*> perf_event_arrays_;
std::map<std::pair<uint32_t, uint32_t>, open_probe_t> perf_events_;
Expand Down
2 changes: 1 addition & 1 deletion src/cc/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ int bpf_detach_tracepoint(const char *tp_category, const char *tp_name) {
return 0;
}

int bpf_attach_raw_tracepoint(int progfd, char *tp_name)
int bpf_attach_raw_tracepoint(int progfd, const char *tp_name)
{
int ret;

Expand Down
2 changes: 1 addition & 1 deletion src/cc/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int bpf_attach_tracepoint(int progfd, const char *tp_category,
const char *tp_name);
int bpf_detach_tracepoint(const char *tp_category, const char *tp_name);

int bpf_attach_raw_tracepoint(int progfd, char *tp_name);
int bpf_attach_raw_tracepoint(int progfd, const char *tp_name);

void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb,
perf_reader_lost_cb lost_cb, void *cb_cookie,
Expand Down

0 comments on commit 5681ea9

Please sign in to comment.