Commit e1abac20 authored by Alastair Robertson's avatar Alastair Robertson

Detach probes

parent b6af7c84
#include <iostream>
#include "bpftrace.h"
#include "libbpf.h"
namespace ebpf {
namespace bpftrace {
......@@ -30,8 +29,8 @@ std::string typestr(ProbeType t)
int BPFtrace::attach_probes()
{
int err = 0;
for (Probe &probe : probes_) {
int err = 0;
if (probe.type == ProbeType::kprobe ||
probe.type == ProbeType::kretprobe) {
err = attach_kprobe(probe);
......@@ -44,6 +43,31 @@ int BPFtrace::attach_probes()
std::cerr << "Error attaching probe: " << typestr(probe.type) << ":" << probe.attach_point << std::endl;
return err;
}
probe.attached = true;
}
return 0;
}
int BPFtrace::detach_probes()
{
for (Probe &probe : probes_) {
if (!probe.attached)
continue;
int err = 0;
if (probe.type == ProbeType::kprobe ||
probe.type == ProbeType::kretprobe) {
err = bpf_detach_kprobe(eventname(probe).c_str());
}
else {
abort();
}
if (err) {
std::cerr << "Error detaching probe: " << typestr(probe.type) << ":" << probe.attach_point << std::endl;
return err;
}
}
return 0;
}
......@@ -67,29 +91,51 @@ int BPFtrace::add_probe(ast::Probe &p)
int BPFtrace::attach_kprobe(Probe &probe)
{
std::string event;
bpf_probe_attach_type attach_type;
int pid = -1;
int cpu = 0;
int group_fd = -1;
perf_reader_cb cb = nullptr;
void *cb_cookie = nullptr;
void *result = bpf_attach_kprobe(probe.progfd, attachtype(probe),
eventname(probe).c_str(), probe.attach_point.c_str(),
pid, cpu, group_fd, cb, cb_cookie);
if (result == nullptr)
return 1;
return 0;
}
std::string BPFtrace::eventname(Probe &probe)
{
std::string event;
switch (probe.type) {
case ProbeType::kprobe:
event = "p_" + probe.attach_point;
attach_type = BPF_PROBE_ENTRY;
break;
case ProbeType::kretprobe:
event = "r_" + probe.attach_point;
attach_type = BPF_PROBE_RETURN;
break;
default:
abort();
}
return event;
}
bpf_attach_kprobe(probe.progfd, attach_type, event.c_str(),
probe.attach_point.c_str(), pid, cpu, group_fd, cb, cb_cookie);
bpf_probe_attach_type BPFtrace::attachtype(Probe &probe)
{
bpf_probe_attach_type attach_type;
switch (probe.type) {
case ProbeType::kprobe:
attach_type = BPF_PROBE_ENTRY;
break;
case ProbeType::kretprobe:
attach_type = BPF_PROBE_RETURN;
break;
default:
abort();
}
return attach_type;
}
} // namespace bpftrace
......
......@@ -4,6 +4,7 @@
#include <memory>
#include <vector>
#include "libbpf.h"
#include "ast.h"
#include "map.h"
......@@ -33,12 +34,14 @@ public:
ProbeType type;
std::string attach_point;
int progfd;
bool attached = false;
};
class BPFtrace
{
public:
int attach_probes();
int detach_probes();
int add_probe(ast::Probe &p);
std::map<std::string, Type> map_val_;
......@@ -49,6 +52,9 @@ private:
std::vector<Probe> probes_;
int attach_kprobe(Probe &probe);
static std::string eventname(Probe &probe);
static bpf_probe_attach_type attachtype(Probe &probe);
};
} // namespace bpftrace
......
......@@ -17,7 +17,7 @@ void usage()
int main(int argc, char *argv[])
{
int result;
int err;
Driver driver;
std::string script;
......@@ -44,7 +44,7 @@ int main(int argc, char *argv[])
return 1;
}
char *file_name = argv[optind];
result = driver.parse_file(file_name);
err = driver.parse_file(file_name);
}
else
{
......@@ -54,11 +54,11 @@ int main(int argc, char *argv[])
usage();
return 1;
}
result = driver.parse_str(script);
err = driver.parse_str(script);
}
if (result)
return result;
if (err)
return err;
BPFtrace bpftrace;
......@@ -66,16 +66,23 @@ int main(int argc, char *argv[])
driver.root_->accept(p);
ast::SemanticAnalyser semantics(driver.root_, bpftrace);
result = semantics.analyse();
if (result)
return result;
err = semantics.analyse();
if (err)
return err;
ast::CodegenLLVM llvm(driver.root_, bpftrace);
result = llvm.compile();
if (result)
return result;
err = llvm.compile();
if (err)
return err;
bpftrace.attach_probes();
err = bpftrace.attach_probes();
if (err)
goto cleanup;
// TODO wait here while script is running
cleanup:
bpftrace.detach_probes();
return 0;
}
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