Skip to content

Commit

Permalink
Merge pull request #802 from gaoyixiang1/develop
Browse files Browse the repository at this point in the history
Stack_Analyser:使用fentry、fexit替换kprobe和kretprobe
  • Loading branch information
chenamy2017 authored May 21, 2024
2 parents eea66dc + dd09c83 commit 8f8c8ec
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 25 deletions.
19 changes: 15 additions & 4 deletions eBPF_Supermarket/Stack_Analyser/bpf/probe.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ static int entry(void *ctx)
bpf_map_update_elem(&starts, &pid, &nsec, BPF_ANY);
return 0;
}

SEC("fentry/dummy_fentry")
int BPF_PROG(dummy_fentry)
{
entry(ctx);
return 0;
}
SEC("kprobe/dummy_kprobe")
int BPF_KPROBE(dummy_kprobe)
{
Expand All @@ -64,8 +69,7 @@ int BPF_KPROBE(dummy_kprobe)
static int exit(void *ctx)
{
CHECK_ACTIVE;
u32 pid = bpf_get_current_pid_tgid() >> 32;

u32 pid = bpf_get_current_pid_tgid() >> 32;
u64 *start = bpf_map_lookup_elem(&starts, &pid);
if (!start)
return 0;
Expand All @@ -87,14 +91,21 @@ static int exit(void *ctx)
return 0;
}

SEC("fexit/dummy_fexit")
int BPF_PROG(dummy_fexit)
{
exit(ctx);
return 0;
}


SEC("kretprobe/dummy_kretprobe")
int BPF_KRETPROBE(dummy_kretprobe)
{
exit(ctx);
return 0;
}


static int handleCounts(void *ctx)
{
CHECK_ACTIVE;
Expand Down
112 changes: 91 additions & 21 deletions eBPF_Supermarket/Stack_Analyser/src/bpf_wapper/probe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@
#include "uprobe_helpers.h"

// ========== implement virtual func ==========

bool tryf;
std::vector<std::string> strList;
std::string func;
void splitStr(std::string symbol, const char split, std::vector<std::string> &res)
{
if (symbol == "")
return;
std::string strs = symbol + split;
size_t pos = strs.find(split);
while (pos != strs.npos)
{
std::string temp = strs.substr(0, pos);
res.push_back(temp);
strs = strs.substr(pos + 1, strs.size());
pos = strs.find(split);
}
}
uint64_t *ProbeStackCollector::count_values(void *data)
{
time_tuple *p = (time_tuple *)data;
Expand All @@ -34,31 +50,67 @@ uint64_t *ProbeStackCollector::count_values(void *data)
void ProbeStackCollector::setScale(std::string probe)
{
this->probe = probe;
splitStr(probe, ':', strList);
func = probe;
for (int i = 0; i < scale_num; i++)
scales[i].Type = probe + scales[i].Type;
};

int ProbeStackCollector::load(void)
static bool try_fentry(struct probe_bpf *skel, std::string func)
{
EBPF_LOAD_OPEN_INIT(skel->rodata->target_pid = pid;);
return 0;
};
long err;

void splitStr(std::string symbol, const char split, std::vector<std::string> &res)
{
if (symbol == "")
return;
std::string strs = symbol + split;
size_t pos = strs.find(split);
while (pos != strs.npos)
if (!fentry_can_attach(func.c_str(), NULL))
{
std::string temp = strs.substr(0, pos);
res.push_back(temp);
strs = strs.substr(pos + 1, strs.size());
pos = strs.find(split);
return false;
}
err = bpf_program__set_attach_target(skel->progs.dummy_fentry, 0, func.c_str());
if (err)
{
bpf_program__set_autoload(skel->progs.dummy_fentry, false);
bpf_program__set_autoload(skel->progs.dummy_fexit, false);
return false;
}
err = bpf_program__set_attach_target(skel->progs.dummy_fexit, 0, func.c_str());
if (err)
{
bpf_program__set_autoload(skel->progs.dummy_fentry, false);
bpf_program__set_autoload(skel->progs.dummy_fexit, false);

return false;
}

bpf_program__set_autoload(skel->progs.dummy_kprobe, false);
bpf_program__set_autoload(skel->progs.dummy_kretprobe, false);
return true;
}

int ProbeStackCollector::load(void)
{
std::string str = func;
skel = skel->open(NULL);
CHECK_ERR(!skel, "Fail to open BPF skeleton");
if (strList.size() == 3 && strList[0] == "p" && strList[1] == "")
str = strList[2];
if (strList.size() == 1 || (strList.size() == 3 && strList[0] == "p" && strList[1] == "")){
tryf = try_fentry(skel, str);
}else{
bpf_program__set_autoload(skel->progs.dummy_fentry, false);
bpf_program__set_autoload(skel->progs.dummy_fexit, false);
}
skel->rodata->target_pid = pid;
skel->rodata->trace_user = ustack;
skel->rodata->trace_kernel = kstack;
skel->rodata->self_pid = self_pid;
skel->rodata->target_tgid = tgid;
skel->rodata->target_cgroupid = cgroup;
skel->rodata->freq = freq;
err = skel->load(skel);
CHECK_ERR(err, "Fail to load BPF skeleton");
obj = skel->obj;
return 0;
};

static int get_binpath(char *path, int pid)
{
char mode[16], line[128], buf[64];
Expand Down Expand Up @@ -97,6 +149,16 @@ static int attach_kprobes(struct probe_bpf *skel, std::string func)
CHECK_ERR(!skel->links.dummy_kretprobe, "Fail to attach ketprobe");
return 0;
}
static int attach_fentry(struct probe_bpf *skel)
{
skel->links.dummy_fentry =
bpf_program__attach(skel->progs.dummy_fentry);
CHECK_ERR(!skel->links.dummy_fentry, "Fail to attach fentry");
skel->links.dummy_fexit =
bpf_program__attach(skel->progs.dummy_fexit);
CHECK_ERR(!skel->links.dummy_fexit, "Fail to attach fexit");
return 0;
}
static int attach_uprobes(struct probe_bpf *skel, std::string probe, int pid)
{
char *binary, *function;
Expand Down Expand Up @@ -149,16 +211,24 @@ static int attach_usdt(struct probe_bpf *skel, std::string func, int pid)
int ProbeStackCollector::attach(void)
{
// dynamic mounting
std::vector<std::string> strList;
splitStr(probe, ':', strList);
std::string func = probe;
// std::vector<std::string> strList;
// splitStr(probe, ':', strList);
// std::string func = probe;
int err = 0;

if (strList.size() == 3 && strList[0] == "p" && strList[1] == "")
func = strList[2];
if (strList.size() == 1 || (strList.size() == 3 && strList[0] == "p" && strList[1] == ""))
{
err = attach_kprobes(skel, func);
if (!tryf)
{
err = attach_kprobes(skel, func);
return 0;
}
else
{
err = attach_fentry(skel);
return 0;
}
}
else if (strList.size() == 3 && strList[0] == "t")
{
Expand Down

0 comments on commit 8f8c8ec

Please sign in to comment.