Skip to content

Commit

Permalink
Stack_Analyzer:优化代码内容,删减不必要结构体 (#709)
Browse files Browse the repository at this point in the history
* new arrangement

Signed-off-by: LiuLingze <[email protected]>

* update introduction

Signed-off-by: LiuLingze <[email protected]>

* update action

Signed-off-by: LiuLingze <[email protected]>

* modify submodule

Signed-off-by: LiuLingze <[email protected]>

* remove assets

Signed-off-by: LiuLingze <[email protected]>

* modify doc

Signed-off-by: LiuLingze <[email protected]>

* modify submodule

Signed-off-by: LiuLingze <[email protected]>

* modify ignore

Signed-off-by: LiuLingze <[email protected]>

* modify ignore

Signed-off-by: LiuLingze <[email protected]>

* 优化代码内容,删减不必要结构体

---------

Signed-off-by: LiuLingze <[email protected]>
Co-authored-by: LiuLingze <[email protected]>
Co-authored-by: 刘凌泽 <[email protected]>
  • Loading branch information
3 people authored Mar 18, 2024
1 parent b5d90c1 commit e3540e4
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MemoryStackCollector : public StackCollector
skel->progs.prog_name, \
pid, \
object, \
1, \
0, \
&uprobe_opts); \
} while (false)
#else
Expand Down
18 changes: 18 additions & 0 deletions eBPF_Supermarket/Stack_Analyser/include/uprobe_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
/* Copyright (c) 2021 Google LLC. */
#ifndef __UPROBE_HELPERS_H
#define __UPROBE_HELPERS_H

#include <sys/types.h>
#include <unistd.h>
#include <gelf.h>

int get_pid_binary_path(pid_t pid, char *path, size_t path_sz);
int get_pid_lib_path(pid_t pid, const char *lib, char *path, size_t path_sz);
int resolve_binary_path(const char *binary, pid_t pid, char *path, size_t path_sz);
off_t get_elf_func_offset(const char *path, const char *func);
Elf *open_elf(const char *path, int *fd_close);
Elf *open_elf_by_fd(int fd);
void close_elf(Elf *e, int fd_close);

#endif /* __UPROBE_HELPERS_H */
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ IOStackCollector::IOStackCollector()

int IOStackCollector::load(void)
{
StackProgLoadOpen();
StackProgLoadOpen(skel->bss->apid=pid;);
return 0;
}

Expand Down
67 changes: 60 additions & 7 deletions eBPF_Supermarket/Stack_Analyser/src/bpf/ProbeStackCollector.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "bpf/ProbeStackCollector.h"
#include "uprobe_helpers.h"

double StackCountStackCollector::count_value(void *data) {
return *(uint32_t*)data;
double StackCountStackCollector::count_value(void *data)
{
return *(uint32_t *)data;
}

StackCountStackCollector::StackCountStackCollector()
Expand All @@ -13,23 +15,74 @@ StackCountStackCollector::StackCountStackCollector()
};
};

void splitString(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);
}
}
void StackCountStackCollector::setScale(std::string probe)
{
this->probe = probe;
scale.Type = (probe + scale.Type).c_str();
auto type = new std::string(probe+scale.Type);
scale.Type = type->c_str();
};

int StackCountStackCollector::load(void)
{
StackProgLoadOpen();
StackProgLoadOpen(skel->bss->apid = pid;);
return 0;
};

int StackCountStackCollector::attach(void)
{
skel->links.handle =
bpf_program__attach_kprobe(skel->progs.handle, false, probe.c_str());
CHECK_ERR(!skel->links.handle, "Fail to attach kprobe");
std::vector<std::string> strList;
splitString(probe, ':', strList);

if (strList.size() == 1 || (strList.size() == 3 && strList[0] == "p" && strList[1] == ""))
{
// probe a kernel function
std::string func = probe;
if (strList.size() == 3 && strList[0] == "p" && strList[1] == "")
func = strList[2];
skel->links.handle =
bpf_program__attach_kprobe(skel->progs.handle, false, func.c_str());
CHECK_ERR(!skel->links.handle, "Fail to attach kprobe111");
return 0;
}
else if (strList.size() == 3 && strList[0] == "t")
{
// probe a kernel tracepoint
skel->links.handle_tp =
bpf_program__attach_tracepoint(skel->progs.handle_tp, strList[1].c_str(), strList[2].c_str());
CHECK_ERR(!skel->links.handle_tp, "Fail to attach tracepoint");
return 0;
}
else if (strList.size() == 2 || (strList.size() == 3 && strList[0] == "p" && strList[1] != ""))
{
// probe a user-space function in the library 'lib'

return 0;
}
else if (strList.size() == 3 && strList[0] == "u")
{
// probe a USDT tracepoint
return 0;
}
else
{
printf("Type must be 'p', 't', or 'u' or too any args");
}

scale.Type = (probe + scale.Type).c_str();
return 0;
};

Expand Down
1 change: 1 addition & 0 deletions eBPF_Supermarket/Stack_Analyser/src/bpf/stack_count.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "../include/sa_ebpf.h"
#include "../include/task.h"


DeclareCommonMaps(u32);
DeclareCommonVar();

Expand Down
Loading

0 comments on commit e3540e4

Please sign in to comment.