Commit aa7b2797 authored by Brenden Blanco's avatar Brenden Blanco

Add BPFModule::table_storage() helper

Adds a reference to the ts_ member, for use by BPF class to access the
storage object and perform lookups.

Note that get_hash_table/get_stack_table don't have an error return, so
failure to lookup the table name will result in undefined behavior
(which is actually the case prior to this commit as well).

Also update indentation per clang-format
Signed-off-by: default avatarBrenden Blanco <bblanco@gmail.com>
parent faea8c84
...@@ -395,8 +395,14 @@ StatusTuple BPF::detach_perf_event(uint32_t ev_type, uint32_t ev_config) { ...@@ -395,8 +395,14 @@ StatusTuple BPF::detach_perf_event(uint32_t ev_type, uint32_t ev_config) {
StatusTuple BPF::open_perf_buffer(const std::string& name, StatusTuple BPF::open_perf_buffer(const std::string& name,
perf_reader_raw_cb cb, void* cb_cookie, perf_reader_raw_cb cb, void* cb_cookie,
int page_cnt) { int page_cnt) {
if (perf_buffers_.find(name) == perf_buffers_.end()) if (perf_buffers_.find(name) == perf_buffers_.end()) {
perf_buffers_[name] = new BPFPerfBuffer(bpf_module_.get(), name); TableStorage::iterator it;
if (!bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
return StatusTuple(-1,
"open_perf_buffer: unable to find table_storage %s",
name.c_str());
perf_buffers_[name] = new BPFPerfBuffer(it->second);
}
if ((page_cnt & (page_cnt - 1)) != 0) if ((page_cnt & (page_cnt - 1)) != 0)
return StatusTuple(-1, "open_perf_buffer page_cnt must be a power of two"); return StatusTuple(-1, "open_perf_buffer page_cnt must be a power of two");
auto table = perf_buffers_[name]; auto table = perf_buffers_[name];
......
...@@ -46,7 +46,7 @@ public: ...@@ -46,7 +46,7 @@ public:
static const int BPF_MAX_STACK_DEPTH = 127; static const int BPF_MAX_STACK_DEPTH = 127;
explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr) explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr)
: bpf_module_(new BPFModule(flag, ts)), ts_(ts) {} : bpf_module_(new BPFModule(flag, ts)) {}
StatusTuple init(const std::string& bpf_program, StatusTuple init(const std::string& bpf_program,
std::vector<std::string> cflags = {}, std::vector<std::string> cflags = {},
std::vector<USDT> usdt = {}); std::vector<USDT> usdt = {});
...@@ -93,16 +93,17 @@ public: ...@@ -93,16 +93,17 @@ public:
template <class KeyType, class ValueType> template <class KeyType, class ValueType>
BPFHashTable<KeyType, ValueType> get_hash_table(const std::string& name) { BPFHashTable<KeyType, ValueType> get_hash_table(const std::string& name) {
if (ts_) { TableStorage::iterator it;
TableStorage::iterator it; if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
if (ts_->Find(Path({name}), it)) return BPFHashTable<KeyType, ValueType>(it->second);
return BPFHashTable<KeyType, ValueType>(it->second); return BPFHashTable<KeyType, ValueType>({});
}
return BPFHashTable<KeyType, ValueType>(bpf_module_.get(), name);
} }
BPFStackTable get_stack_table(const std::string& name) { BPFStackTable get_stack_table(const std::string& name) {
return BPFStackTable(bpf_module_.get(), name); TableStorage::iterator it;
if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
return BPFStackTable(it->second);
return BPFStackTable({});
} }
StatusTuple open_perf_buffer(const std::string& name, perf_reader_raw_cb cb, StatusTuple open_perf_buffer(const std::string& name, perf_reader_raw_cb cb,
...@@ -160,7 +161,6 @@ private: ...@@ -160,7 +161,6 @@ private:
uint64_t symbol_addr, bcc_symbol* output); uint64_t symbol_addr, bcc_symbol* output);
std::unique_ptr<BPFModule> bpf_module_; std::unique_ptr<BPFModule> bpf_module_;
TableStorage* ts_;
std::map<std::string, int> funcs_; std::map<std::string, int> funcs_;
......
...@@ -34,17 +34,10 @@ namespace ebpf { ...@@ -34,17 +34,10 @@ namespace ebpf {
template <class KeyType, class ValueType> template <class KeyType, class ValueType>
class BPFTableBase { class BPFTableBase {
public: public:
size_t capacity() { return capacity_; } size_t capacity() { return capacity_; }
protected: protected:
BPFTableBase(BPFModule* bpf_module, const std::string& name) {
size_t id_ = bpf_module->table_id(name);
if (id_ >= bpf_module->num_tables())
throw std::invalid_argument("Table " + name + " does not exist");
fd_ = bpf_module->table_fd(id_);
capacity_ = bpf_module->table_max_entries(id_);
};
explicit BPFTableBase(const TableDesc& desc) { explicit BPFTableBase(const TableDesc& desc) {
fd_ = desc.fd; fd_ = desc.fd;
capacity_ = desc.max_entries; capacity_ = desc.max_entries;
...@@ -76,9 +69,8 @@ protected: ...@@ -76,9 +69,8 @@ protected:
template <class KeyType, class ValueType> template <class KeyType, class ValueType>
class BPFHashTable : protected BPFTableBase<KeyType, ValueType> { class BPFHashTable : protected BPFTableBase<KeyType, ValueType> {
public: public:
explicit BPFHashTable(const TableDesc& desc) : BPFTableBase<KeyType, ValueType>(desc) {} explicit BPFHashTable(const TableDesc& desc)
BPFHashTable(BPFModule* bpf_module, const std::string& name) : BPFTableBase<KeyType, ValueType>(desc) {}
: BPFTableBase<KeyType, ValueType>(bpf_module, name) {}
ValueType get_value(const KeyType& key) { ValueType get_value(const KeyType& key) {
ValueType res; ValueType res;
...@@ -115,22 +107,22 @@ struct stacktrace_t { ...@@ -115,22 +107,22 @@ struct stacktrace_t {
}; };
class BPFStackTable : protected BPFTableBase<int, stacktrace_t> { class BPFStackTable : protected BPFTableBase<int, stacktrace_t> {
public: public:
BPFStackTable(BPFModule* bpf_module, const std::string& name) BPFStackTable(const TableDesc& desc)
: BPFTableBase<int, stacktrace_t>(bpf_module, name) {} : BPFTableBase<int, stacktrace_t>(desc) {}
~BPFStackTable(); ~BPFStackTable();
std::vector<intptr_t> get_stack_addr(int stack_id); std::vector<intptr_t> get_stack_addr(int stack_id);
std::vector<std::string> get_stack_symbol(int stack_id, int pid); std::vector<std::string> get_stack_symbol(int stack_id, int pid);
private: private:
std::map<int, void*> pid_sym_; std::map<int, void*> pid_sym_;
}; };
class BPFPerfBuffer : protected BPFTableBase<int, int> { class BPFPerfBuffer : protected BPFTableBase<int, int> {
public: public:
BPFPerfBuffer(BPFModule* bpf_module, const std::string& name) BPFPerfBuffer(const TableDesc& desc)
: BPFTableBase<int, int>(bpf_module, name), epfd_(-1) {} : BPFTableBase<int, int>(desc), epfd_(-1) {}
~BPFPerfBuffer(); ~BPFPerfBuffer();
StatusTuple open_all_cpu(perf_reader_raw_cb cb, void* cb_cookie, StatusTuple open_all_cpu(perf_reader_raw_cb cb, void* cb_cookie,
...@@ -138,7 +130,7 @@ public: ...@@ -138,7 +130,7 @@ public:
StatusTuple close_all_cpu(); StatusTuple close_all_cpu();
void poll(int timeout); void poll(int timeout);
private: private:
StatusTuple open_on_cpu(perf_reader_raw_cb cb, int cpu, void* cb_cookie, StatusTuple open_on_cpu(perf_reader_raw_cb cb, int cpu, void* cb_cookie,
int page_cnt); int page_cnt);
StatusTuple close_on_cpu(int cpu); StatusTuple close_on_cpu(int cpu);
......
...@@ -102,7 +102,10 @@ class MyMemoryManager : public SectionMemoryManager { ...@@ -102,7 +102,10 @@ class MyMemoryManager : public SectionMemoryManager {
}; };
BPFModule::BPFModule(unsigned flags, TableStorage *ts) BPFModule::BPFModule(unsigned flags, TableStorage *ts)
: flags_(flags), ctx_(new LLVMContext), ts_(ts) { : flags_(flags),
ctx_(new LLVMContext),
id_(std::to_string((uintptr_t)this)),
ts_(ts) {
InitializeNativeTarget(); InitializeNativeTarget();
InitializeNativeTargetAsmPrinter(); InitializeNativeTargetAsmPrinter();
LLVMInitializeBPFTarget(); LLVMInitializeBPFTarget();
...@@ -120,7 +123,7 @@ BPFModule::~BPFModule() { ...@@ -120,7 +123,7 @@ BPFModule::~BPFModule() {
engine_.reset(); engine_.reset();
rw_engine_.reset(); rw_engine_.reset();
ctx_.reset(); ctx_.reset();
ts_->DeletePrefix(Path({std::to_string((uintptr_t)this)})); ts_->DeletePrefix(Path({id_}));
} }
static void debug_printf(Module *mod, IRBuilder<> &B, const string &fmt, vector<Value *> args) { static void debug_printf(Module *mod, IRBuilder<> &B, const string &fmt, vector<Value *> args) {
...@@ -322,8 +325,7 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) { ...@@ -322,8 +325,7 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) {
// load an entire c file as a module // load an entire c file as a module
int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags[], int ncflags) { int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags[], int ncflags) {
clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_); clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_);
if (clang_loader_->parse(&mod_, *ts_, file, in_memory, cflags, ncflags, if (clang_loader_->parse(&mod_, *ts_, file, in_memory, cflags, ncflags, id_))
std::to_string((uintptr_t)this)))
return -1; return -1;
return 0; return 0;
} }
...@@ -349,7 +351,7 @@ int BPFModule::annotate() { ...@@ -349,7 +351,7 @@ int BPFModule::annotate() {
auto m = make_unique<Module>("sscanf", *ctx_); auto m = make_unique<Module>("sscanf", *ctx_);
size_t id = 0; size_t id = 0;
Path path({std::to_string((uintptr_t)this)}); Path path({id_});
for (auto it = ts_->lower_bound(path), up = ts_->upper_bound(path); it != up; ++it) { for (auto it = ts_->lower_bound(path), up = ts_->upper_bound(path); it != up; ++it) {
TableDesc &table = it->second; TableDesc &table = it->second;
tables_.push_back(&it->second); tables_.push_back(&it->second);
...@@ -725,8 +727,7 @@ int BPFModule::load_b(const string &filename, const string &proto_filename) { ...@@ -725,8 +727,7 @@ int BPFModule::load_b(const string &filename, const string &proto_filename) {
return rc; return rc;
b_loader_.reset(new BLoader(flags_)); b_loader_.reset(new BLoader(flags_));
if (int rc = if (int rc = b_loader_->parse(&*mod_, filename, proto_filename, *ts_, id_))
b_loader_->parse(&*mod_, filename, proto_filename, *ts_, std::to_string((uintptr_t)this)))
return rc; return rc;
if (int rc = annotate()) if (int rc = annotate())
return rc; return rc;
......
...@@ -58,6 +58,7 @@ class BPFModule { ...@@ -58,6 +58,7 @@ class BPFModule {
int load_b(const std::string &filename, const std::string &proto_filename); int load_b(const std::string &filename, const std::string &proto_filename);
int load_c(const std::string &filename, const char *cflags[], int ncflags); int load_c(const std::string &filename, const char *cflags[], int ncflags);
int load_string(const std::string &text, const char *cflags[], int ncflags); int load_string(const std::string &text, const char *cflags[], int ncflags);
std::string id() const { return id_; }
size_t num_functions() const; size_t num_functions() const;
uint8_t * function_start(size_t id) const; uint8_t * function_start(size_t id) const;
uint8_t * function_start(const std::string &name) const; uint8_t * function_start(const std::string &name) const;
...@@ -89,6 +90,8 @@ class BPFModule { ...@@ -89,6 +90,8 @@ class BPFModule {
int table_leaf_scanf(size_t id, const char *buf, void *leaf); int table_leaf_scanf(size_t id, const char *buf, void *leaf);
char * license() const; char * license() const;
unsigned kern_version() const; unsigned kern_version() const;
TableStorage &table_storage() { return *ts_; }
private: private:
unsigned flags_; // 0x1 for printing unsigned flags_; // 0x1 for printing
std::string filename_; std::string filename_;
...@@ -105,6 +108,7 @@ class BPFModule { ...@@ -105,6 +108,7 @@ class BPFModule {
std::vector<std::string> function_names_; std::vector<std::string> function_names_;
std::map<llvm::Type *, llvm::Function *> readers_; std::map<llvm::Type *, llvm::Function *> readers_;
std::map<llvm::Type *, llvm::Function *> writers_; std::map<llvm::Type *, llvm::Function *> writers_;
std::string id_;
TableStorage *ts_; TableStorage *ts_;
std::unique_ptr<TableStorage> local_ts_; std::unique_ptr<TableStorage> local_ts_;
}; };
......
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