Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions src/cc/api/BPF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <fcntl.h>
#include <iostream>
#include <memory>
#include <random>
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
Expand Down Expand Up @@ -856,31 +855,23 @@ bool BPF::add_module(std::string module)

namespace {

std::string random_alnum_string(const int len) {
static constexpr char kDict[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static std::mt19937 gen;
static std::uniform_int_distribution<size_t> dist(0, sizeof(kDict) - 2);
std::string res;
res.resize(len);
for (int i = 0; i < len; ++i) {
res[i] = kDict[dist(gen)];
}
return res;
}

constexpr size_t kEventNameSizeLimit = 224;

std::string shorten_event_name(const std::string& name) {
constexpr size_t kRandomSuffixLen = 16;
constexpr size_t kHashSuffixLen = 16;
std::string res;
res.reserve(kEventNameSizeLimit);
res.assign(name);
res.resize(kEventNameSizeLimit - kRandomSuffixLen);
res.append(random_alnum_string(kRandomSuffixLen));
res.resize(kEventNameSizeLimit - kHashSuffixLen);

std::stringstream stream;
size_t hash = std::hash<std::string>{}(name);
stream << std::hex << hash;
res.append(stream.str());
return res;
}

} // namespace
} // namespace

std::string BPF::get_uprobe_event(const std::string& binary_path,
uint64_t offset, bpf_probe_attach_type type,
Expand All @@ -891,13 +882,7 @@ std::string BPF::get_uprobe_event(const std::string& binary_path,
if (pid != -1)
res += "_" + std::to_string(pid);
if (res.size() > kEventNameSizeLimit) {
auto iter = name_map_.find(res);
if (iter != name_map_.end()) {
return iter->second;
}
std::string shortend_name = shorten_event_name(res);
name_map_[res] = shortend_name;
return shortend_name;
return shorten_event_name(res);
}
return res;
}
Expand Down
3 changes: 0 additions & 3 deletions src/cc/api/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,6 @@ class BPF {
std::string all_bpf_program_;

std::map<std::string, open_probe_t> kprobes_;
// For uprobes, if the binary path is longer than 250, we'll shorten them. And then keep the
// mapping.
std::map<std::string, std::string> name_map_;
std::map<std::string, open_probe_t> uprobes_;
std::map<std::string, open_probe_t> tracepoints_;
std::map<std::string, open_probe_t> raw_tracepoints_;
Expand Down