Commit 99d36e85 authored by Alastair Robertson's avatar Alastair Robertson

Support for uprobes

parent e5f2c42a
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <unistd.h> #include <unistd.h>
#include "attached_probe.h" #include "attached_probe.h"
#include "bcc_syms.h"
#include "libbpf.h" #include "libbpf.h"
#include "perf_reader.h" #include "perf_reader.h"
...@@ -19,6 +20,10 @@ AttachedProbe::AttachedProbe(Probe &probe, std::tuple<uint8_t *, uintptr_t> &fun ...@@ -19,6 +20,10 @@ AttachedProbe::AttachedProbe(Probe &probe, std::tuple<uint8_t *, uintptr_t> &fun
case ProbeType::kretprobe: case ProbeType::kretprobe:
attach_kprobe(); attach_kprobe();
break; break;
case ProbeType::uprobe:
case ProbeType::uretprobe:
attach_uprobe();
break;
default: default:
abort(); abort();
} }
...@@ -35,6 +40,10 @@ AttachedProbe::~AttachedProbe() ...@@ -35,6 +40,10 @@ AttachedProbe::~AttachedProbe()
case ProbeType::kretprobe: case ProbeType::kretprobe:
err = bpf_detach_kprobe(eventname()); err = bpf_detach_kprobe(eventname());
break; break;
case ProbeType::uprobe:
case ProbeType::uretprobe:
err = bpf_detach_uprobe(eventname());
break;
default: default:
abort(); abort();
} }
...@@ -42,16 +51,33 @@ AttachedProbe::~AttachedProbe() ...@@ -42,16 +51,33 @@ AttachedProbe::~AttachedProbe()
std::cerr << "Error detaching probe: " << probe_.name << std::endl; std::cerr << "Error detaching probe: " << probe_.name << std::endl;
} }
std::string AttachedProbe::eventprefix() const
{
switch (attachtype(probe_.type))
{
case BPF_PROBE_ENTRY:
return "p_";
case BPF_PROBE_RETURN:
return "r_";
default:
abort();
}
}
const char *AttachedProbe::eventname() const const char *AttachedProbe::eventname() const
{ {
std::string event; std::string event;
std::ostringstream offset_str;
switch (probe_.type) switch (probe_.type)
{ {
case ProbeType::kprobe: case ProbeType::kprobe:
event = "p_" + probe_.attach_point;
break;
case ProbeType::kretprobe: case ProbeType::kretprobe:
event = "r_" + probe_.attach_point; event = eventprefix() + probe_.attach_point;
break;
case ProbeType::uprobe:
case ProbeType::uretprobe:
offset_str << std::hex << offset();
event = eventprefix() + probe_.path + "_" + offset_str.str();
break; break;
default: default:
abort(); abort();
...@@ -59,6 +85,18 @@ const char *AttachedProbe::eventname() const ...@@ -59,6 +85,18 @@ const char *AttachedProbe::eventname() const
return event.c_str(); return event.c_str();
} }
uint64_t AttachedProbe::offset() const
{
bcc_symbol sym;
int err = bcc_resolve_symname(probe_.path.c_str(), probe_.attach_point.c_str(),
0, 0, nullptr, &sym);
if (err)
throw std::runtime_error("Could not resolve symbol: " + probe_.path + ":" + probe_.attach_point);
return sym.offset;
}
static unsigned kernel_version() static unsigned kernel_version()
{ {
struct utsname utsname; struct utsname utsname;
...@@ -101,4 +139,20 @@ void AttachedProbe::attach_kprobe() ...@@ -101,4 +139,20 @@ void AttachedProbe::attach_kprobe()
throw std::runtime_error("Error attaching probe: " + probe_.name); throw std::runtime_error("Error attaching probe: " + probe_.name);
} }
void AttachedProbe::attach_uprobe()
{
int pid = -1;
int cpu = 0;
int group_fd = -1;
perf_reader_cb cb = nullptr;
void *cb_cookie = nullptr;
perf_reader_ = bpf_attach_uprobe(progfd_, attachtype(probe_.type),
eventname(), probe_.path.c_str(), offset(),
pid, cpu, group_fd, cb, cb_cookie);
if (perf_reader_ == nullptr)
throw std::runtime_error("Error attaching probe: " + probe_.name);
}
} // namespace bpftrace } // namespace bpftrace
...@@ -13,9 +13,12 @@ public: ...@@ -13,9 +13,12 @@ public:
AttachedProbe& operator=(const AttachedProbe &) = delete; AttachedProbe& operator=(const AttachedProbe &) = delete;
private: private:
std::string eventprefix() const;
const char *eventname() const; const char *eventname() const;
uint64_t offset() const;
void load_prog(); void load_prog();
void attach_kprobe(); void attach_kprobe();
void attach_uprobe();
Probe &probe_; Probe &probe_;
std::tuple<uint8_t *, uintptr_t> &func_; std::tuple<uint8_t *, uintptr_t> &func_;
......
...@@ -13,12 +13,17 @@ namespace bpftrace { ...@@ -13,12 +13,17 @@ namespace bpftrace {
int BPFtrace::add_probe(ast::Probe &p) int BPFtrace::add_probe(ast::Probe &p)
{ {
Probe probe; Probe probe;
probe.path = p.path;
probe.attach_point = p.attach_point; probe.attach_point = p.attach_point;
probe.name = p.name; probe.name = p.name;
if (p.type == "kprobe") if (p.type == "kprobe")
probe.type = ProbeType::kprobe; probe.type = ProbeType::kprobe;
else if (p.type == "kretprobe") else if (p.type == "kretprobe")
probe.type = ProbeType::kretprobe; probe.type = ProbeType::kretprobe;
else if (p.type == "uprobe")
probe.type = ProbeType::uprobe;
else if (p.type == "uretprobe")
probe.type = ProbeType::uretprobe;
else else
return -1; return -1;
probes_.push_back(probe); probes_.push_back(probe);
......
...@@ -40,6 +40,8 @@ bpf_probe_attach_type attachtype(ProbeType t) ...@@ -40,6 +40,8 @@ bpf_probe_attach_type attachtype(ProbeType t)
{ {
case ProbeType::kprobe: return BPF_PROBE_ENTRY; break; case ProbeType::kprobe: return BPF_PROBE_ENTRY; break;
case ProbeType::kretprobe: return BPF_PROBE_RETURN; break; case ProbeType::kretprobe: return BPF_PROBE_RETURN; break;
case ProbeType::uprobe: return BPF_PROBE_ENTRY; break;
case ProbeType::uretprobe: return BPF_PROBE_RETURN; break;
default: abort(); default: abort();
} }
} }
...@@ -50,6 +52,8 @@ bpf_prog_type progtype(ProbeType t) ...@@ -50,6 +52,8 @@ bpf_prog_type progtype(ProbeType t)
{ {
case ProbeType::kprobe: return BPF_PROG_TYPE_KPROBE; break; case ProbeType::kprobe: return BPF_PROG_TYPE_KPROBE; break;
case ProbeType::kretprobe: return BPF_PROG_TYPE_KPROBE; break; case ProbeType::kretprobe: return BPF_PROG_TYPE_KPROBE; break;
case ProbeType::uprobe: return BPF_PROG_TYPE_KPROBE; break;
case ProbeType::uretprobe: return BPF_PROG_TYPE_KPROBE; break;
default: abort(); default: abort();
} }
} }
......
...@@ -38,6 +38,8 @@ enum class ProbeType ...@@ -38,6 +38,8 @@ enum class ProbeType
{ {
kprobe, kprobe,
kretprobe, kretprobe,
uprobe,
uretprobe,
}; };
std::string typestr(Type t); std::string typestr(Type t);
...@@ -48,6 +50,7 @@ class Probe ...@@ -48,6 +50,7 @@ class Probe
{ {
public: public:
ProbeType type; ProbeType type;
std::string path;
std::string attach_point; std::string attach_point;
std::string name; std::string name;
}; };
......
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