Commit 918e4c82 authored by Alastair Robertson's avatar Alastair Robertson

Basic attaching of kprobes

parent e865a011
#include <iostream>
#include "bpftrace.h"
#include "libbpf.h"
namespace ebpf {
namespace bpftrace {
......@@ -15,5 +18,88 @@ std::string typestr(Type t)
}
}
std::string typestr(ProbeType t)
{
switch (t)
{
case ProbeType::kprobe: return "kprobe"; break;
case ProbeType::kretprobe: return "kretprobe"; break;
default: abort();
}
}
int BPFtrace::attach_probes()
{
int err = 0;
for (Probe &probe : probes_) {
if (probe.type == ProbeType::kprobe ||
probe.type == ProbeType::kretprobe) {
err = attach_kprobe(probe);
}
else {
abort();
}
if (err) {
std::cerr << "Error attaching probe: " << typestr(probe.type) << ":" << probe.attach_point << std::endl;
return err;
}
}
return 0;
}
int BPFtrace::add_probe(ast::Probe &p)
{
Probe probe;
probe.attach_point = p.attach_point;
if (p.type == "kprobe") {
probe.type = ProbeType::kprobe;
}
else if (p.type == "kretprobe") {
probe.type = ProbeType::kretprobe;
}
else {
return -1;
}
probes_.push_back(probe);
return 0;
}
int BPFtrace::attach_kprobe(Probe &probe)
{
std::string event;
std::string event_desc;
int pid = -1;
int cpu = 0;
int group_fd = -1;
perf_reader_cb cb = nullptr;
void *cb_cookie = nullptr;
switch (probe.type) {
case ProbeType::kprobe:
event = "p_" + probe.attach_point;
break;
case ProbeType::kretprobe:
event = "r_" + probe.attach_point;
break;
default:
abort();
}
switch (probe.type) {
case ProbeType::kprobe:
event_desc = "p:kprobes/" + event + " " + probe.attach_point;
break;
case ProbeType::kretprobe:
event_desc = "r:kprobes/" + event + " " + probe.attach_point;
break;
default:
abort();
}
bpf_attach_kprobe(probe.progfd, event.c_str(), event_desc.c_str(),
pid, cpu, group_fd, cb, cb_cookie);
}
} // namespace bpftrace
} // namespace ebpf
......@@ -4,6 +4,7 @@
#include <memory>
#include <vector>
#include "ast.h"
#include "map.h"
namespace ebpf {
......@@ -17,14 +18,37 @@ enum class Type
count,
};
enum class ProbeType
{
kprobe,
kretprobe,
};
std::string typestr(Type t);
std::string typestr(ProbeType t);
class Probe
{
public:
ProbeType type;
std::string attach_point;
int progfd;
};
class BPFtrace
{
public:
int attach_probes();
int add_probe(ast::Probe &p);
std::map<std::string, Type> map_val_;
std::map<std::string, std::vector<Type>> map_args_;
std::map<std::string, std::unique_ptr<ebpf::bpftrace::Map>> maps_;
private:
std::vector<Probe> probes_;
int attach_kprobe(Probe &probe);
};
} // namespace bpftrace
......
......@@ -169,6 +169,10 @@ void SemanticAnalyser::visit(Probe &probe)
for (Statement *stmt : *probe.stmts) {
stmt->accept(*this);
}
if (bpftrace_.add_probe(probe)) {
err_ << "Invalid probe type: '" << probe.type << "'" << std::endl;
}
}
void SemanticAnalyser::visit(Program &program)
......
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