Commit 15488d75 authored by Teng Qin's avatar Teng Qin

Add checks of table type in constructor

parent bbc3fbfe
...@@ -191,6 +191,9 @@ BPFStackTable::BPFStackTable(const TableDesc& desc, ...@@ -191,6 +191,9 @@ BPFStackTable::BPFStackTable(const TableDesc& desc,
bool use_debug_file, bool use_debug_file,
bool check_debug_file_crc) bool check_debug_file_crc)
: BPFTableBase<int, stacktrace_t>(desc) { : BPFTableBase<int, stacktrace_t>(desc) {
if (desc.type != BPF_MAP_TYPE_STACK_TRACE)
throw std::invalid_argument("Table '" + desc.name + "' is not a stack table");
symbol_option_ = { symbol_option_ = {
.use_debug_file = use_debug_file, .use_debug_file = use_debug_file,
.check_debug_file_crc = check_debug_file_crc, .check_debug_file_crc = check_debug_file_crc,
...@@ -254,6 +257,12 @@ std::vector<std::string> BPFStackTable::get_stack_symbol(int stack_id, ...@@ -254,6 +257,12 @@ std::vector<std::string> BPFStackTable::get_stack_symbol(int stack_id,
return res; return res;
} }
BPFPerfBuffer::BPFPerfBuffer(const TableDesc& desc)
: BPFTableBase<int, int>(desc), epfd_(-1) {
if (desc.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
throw std::invalid_argument("Table '" + desc.name + "' is not a perf buffer");
}
StatusTuple BPFPerfBuffer::open_on_cpu(perf_reader_raw_cb cb, StatusTuple BPFPerfBuffer::open_on_cpu(perf_reader_raw_cb cb,
perf_reader_lost_cb lost_cb, int cpu, perf_reader_lost_cb lost_cb, int cpu,
void* cb_cookie, int page_cnt) { void* cb_cookie, int page_cnt) {
...@@ -363,6 +372,12 @@ BPFPerfBuffer::~BPFPerfBuffer() { ...@@ -363,6 +372,12 @@ BPFPerfBuffer::~BPFPerfBuffer() {
<< std::endl; << std::endl;
} }
BPFPerfEventArray::BPFPerfEventArray(const TableDesc& desc)
: BPFTableBase<int, int>(desc) {
if (desc.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
throw std::invalid_argument("Table '" + desc.name + "' is not a perf event array");
}
StatusTuple BPFPerfEventArray::open_all_cpu(uint32_t type, uint64_t config) { StatusTuple BPFPerfEventArray::open_all_cpu(uint32_t type, uint64_t config) {
if (cpu_fds_.size() != 0) if (cpu_fds_.size() != 0)
return StatusTuple(-1, "Previously opened perf event not cleaned"); return StatusTuple(-1, "Previously opened perf event not cleaned");
...@@ -436,6 +451,12 @@ BPFPerfEventArray::~BPFPerfEventArray() { ...@@ -436,6 +451,12 @@ BPFPerfEventArray::~BPFPerfEventArray() {
} }
} }
BPFProgTable::BPFProgTable(const TableDesc& desc)
: BPFTableBase<int, int>(desc) {
if (desc.type != BPF_MAP_TYPE_PROG_ARRAY)
throw std::invalid_argument("Table '" + desc.name + "' is not a prog table");
}
StatusTuple BPFProgTable::update_value(const int& index, const int& prog_fd) { StatusTuple BPFProgTable::update_value(const int& index, const int& prog_fd) {
if (!this->update(const_cast<int*>(&index), const_cast<int*>(&prog_fd))) if (!this->update(const_cast<int*>(&index), const_cast<int*>(&prog_fd)))
return StatusTuple(-1, "Error updating value: %s", std::strerror(errno)); return StatusTuple(-1, "Error updating value: %s", std::strerror(errno));
...@@ -448,6 +469,12 @@ StatusTuple BPFProgTable::remove_value(const int& index) { ...@@ -448,6 +469,12 @@ StatusTuple BPFProgTable::remove_value(const int& index) {
return StatusTuple(0); return StatusTuple(0);
} }
BPFCgroupArray::BPFCgroupArray(const TableDesc& desc)
: BPFTableBase<int, int>(desc) {
if (desc.type != BPF_MAP_TYPE_CGROUP_ARRAY)
throw std::invalid_argument("Table '" + desc.name + "' is not a cgroup array");
}
StatusTuple BPFCgroupArray::update_value(const int& index, StatusTuple BPFCgroupArray::update_value(const int& index,
const int& cgroup2_fd) { const int& cgroup2_fd) {
if (!this->update(const_cast<int*>(&index), const_cast<int*>(&cgroup2_fd))) if (!this->update(const_cast<int*>(&index), const_cast<int*>(&cgroup2_fd)))
......
...@@ -299,8 +299,7 @@ class BPFStackTable : public BPFTableBase<int, stacktrace_t> { ...@@ -299,8 +299,7 @@ class BPFStackTable : public BPFTableBase<int, stacktrace_t> {
class BPFPerfBuffer : public BPFTableBase<int, int> { class BPFPerfBuffer : public BPFTableBase<int, int> {
public: public:
BPFPerfBuffer(const TableDesc& desc) BPFPerfBuffer(const TableDesc& desc);
: BPFTableBase<int, int>(desc), epfd_(-1) {}
~BPFPerfBuffer(); ~BPFPerfBuffer();
StatusTuple open_all_cpu(perf_reader_raw_cb cb, perf_reader_lost_cb lost_cb, StatusTuple open_all_cpu(perf_reader_raw_cb cb, perf_reader_lost_cb lost_cb,
...@@ -321,8 +320,7 @@ class BPFPerfBuffer : public BPFTableBase<int, int> { ...@@ -321,8 +320,7 @@ class BPFPerfBuffer : public BPFTableBase<int, int> {
class BPFPerfEventArray : public BPFTableBase<int, int> { class BPFPerfEventArray : public BPFTableBase<int, int> {
public: public:
BPFPerfEventArray(const TableDesc& desc) BPFPerfEventArray(const TableDesc& desc);
: BPFTableBase<int, int>(desc) {}
~BPFPerfEventArray(); ~BPFPerfEventArray();
StatusTuple open_all_cpu(uint32_t type, uint64_t config); StatusTuple open_all_cpu(uint32_t type, uint64_t config);
...@@ -337,11 +335,7 @@ class BPFPerfEventArray : public BPFTableBase<int, int> { ...@@ -337,11 +335,7 @@ class BPFPerfEventArray : public BPFTableBase<int, int> {
class BPFProgTable : public BPFTableBase<int, int> { class BPFProgTable : public BPFTableBase<int, int> {
public: public:
BPFProgTable(const TableDesc& desc) BPFProgTable(const TableDesc& desc);
: BPFTableBase<int, int>(desc) {
if (desc.type != BPF_MAP_TYPE_PROG_ARRAY)
throw std::invalid_argument("Table '" + desc.name + "' is not a prog table");
}
StatusTuple update_value(const int& index, const int& prog_fd); StatusTuple update_value(const int& index, const int& prog_fd);
StatusTuple remove_value(const int& index); StatusTuple remove_value(const int& index);
...@@ -349,11 +343,7 @@ public: ...@@ -349,11 +343,7 @@ public:
class BPFCgroupArray : public BPFTableBase<int, int> { class BPFCgroupArray : public BPFTableBase<int, int> {
public: public:
BPFCgroupArray(const TableDesc& desc) BPFCgroupArray(const TableDesc& desc);
: BPFTableBase<int, int>(desc) {
if (desc.type != BPF_MAP_TYPE_CGROUP_ARRAY)
throw std::invalid_argument("Table '" + desc.name + "' is not a cgroup array");
}
StatusTuple update_value(const int& index, const int& cgroup2_fd); StatusTuple update_value(const int& index, const int& cgroup2_fd);
StatusTuple update_value(const int& index, const std::string& cgroup2_path); StatusTuple update_value(const int& index, const std::string& cgroup2_path);
......
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